Skip to content

Commit fef2074

Browse files
committed
Segmented stack space
1 parent 1f2b829 commit fef2074

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

lib/fizzy/execute.hpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ class ExecutionContext
6363
~Guard() noexcept
6464
{
6565
--m_execution_context.depth;
66-
m_execution_context.stack_space_ptr = stack_space;
66+
67+
m_execution_context.free_stack_space = prev_free_stack_space;
68+
if (prev_stack_space_segment != nullptr)
69+
{
70+
assert(m_execution_context.stack_space_segment == stack_space);
71+
delete[] stack_space;
72+
m_execution_context.stack_space_segment = prev_stack_space_segment;
73+
}
6774
}
6875
};
6976

@@ -78,24 +85,26 @@ class ExecutionContext
7885
/// the call depth back to the original value when going out of scope.
7986
Guard allocate_stack_space(size_t required_stack_space = 0) noexcept
8087
{
88+
++depth;
8189
Guard g{*this};
8290

91+
g.prev_free_stack_space = free_stack_space;
92+
8393
if (required_stack_space <= free_stack_space)
8494
{
85-
const auto offset =
86-
g.stack_space =
95+
// Must be a segment of default size or required_stack_space is 0.
96+
const auto offset = DefaultStackSpaceSegmentSize - free_stack_space;
97+
g.stack_space = stack_space_segment + offset;
98+
g.prev_free_stack_space = free_stack_space;
99+
free_stack_space -= required_stack_space;
100+
return g;
87101
}
88102

89-
const auto stack_space_size = std::size(stack_space);
90-
const auto used_space = static_cast<size_t>(stack_space_ptr - stack_space);
91-
[[maybe_unused]] const auto free_space = stack_space_size - used_space;
92-
93-
assert(required_stack_space <= free_space);
94-
95-
++depth;
96-
97-
g.stack_space = stack_space_ptr;
98-
stack_space_ptr += required_stack_space;
103+
g.prev_stack_space_segment = stack_space_segment;
104+
const auto new_segment_size = std::max(DefaultStackSpaceSegmentSize, required_stack_space);
105+
stack_space_segment = new Value[new_segment_size];
106+
g.stack_space = stack_space_segment;
107+
free_stack_space = new_segment_size - required_stack_space;
99108
return g;
100109
}
101110
};

test/unittests/execute_call_depth_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ TEST(execute_call_depth, call_host_function_calling_wasm_function_inclusive)
213213
constexpr auto host_f = [](std::any&, Instance& instance, const Value*,
214214
ExecutionContext& ctx) noexcept {
215215
recorded_depth = ctx.depth;
216-
const auto ctx_guard = ctx.increment_call_depth();
216+
const auto ctx_guard = ctx.allocate_stack_space();
217217
return fizzy::execute(instance, 2 /* $leaf */, {}, ctx);
218218
};
219219

@@ -304,7 +304,7 @@ TEST(execute_call_depth, call_host_function_calling_another_wasm_module)
304304
ExecutionContext& ctx) noexcept {
305305
recorded_depth = ctx.depth;
306306
auto instance = *std::any_cast<Instance*>(&host_context);
307-
const auto ctx_guard = ctx.increment_call_depth();
307+
const auto ctx_guard = ctx.allocate_stack_space();
308308
return fizzy::execute(*instance, 0, {}, ctx);
309309
};
310310

@@ -466,7 +466,7 @@ TEST(execute_call_depth, execute_host_function_within_wasm_recursion_limit)
466466
constexpr auto host_f = [](std::any&, Instance& instance, const Value*,
467467
ExecutionContext& ctx) noexcept {
468468
max_recorded_wasm_recursion_depth = std::max(max_recorded_wasm_recursion_depth, ctx.depth);
469-
const auto ctx_guard = ctx.increment_call_depth();
469+
const auto ctx_guard = ctx.allocate_stack_space();
470470
return fizzy::execute(instance, 0, {}, ctx);
471471
};
472472

@@ -537,7 +537,7 @@ TEST(execute_call, call_host_function_calling_wasm_interleaved_infinite_recursio
537537
ExecutionContext& ctx) noexcept {
538538
EXPECT_LT(ctx.depth, DepthLimit);
539539
++counter;
540-
const auto ctx_guard = ctx.increment_call_depth();
540+
const auto ctx_guard = ctx.allocate_stack_space();
541541
return fizzy::execute(instance, 1, {}, ctx);
542542
};
543543

0 commit comments

Comments
 (0)