Skip to content

Commit

Permalink
Segmented stack space
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 31, 2021
1 parent 1f2b829 commit fef2074
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
35 changes: 22 additions & 13 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ class ExecutionContext
~Guard() noexcept
{
--m_execution_context.depth;
m_execution_context.stack_space_ptr = stack_space;

m_execution_context.free_stack_space = prev_free_stack_space;
if (prev_stack_space_segment != nullptr)
{
assert(m_execution_context.stack_space_segment == stack_space);
delete[] stack_space;
m_execution_context.stack_space_segment = prev_stack_space_segment;
}
}
};

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

g.prev_free_stack_space = free_stack_space;

if (required_stack_space <= free_stack_space)
{
const auto offset =
g.stack_space =
// Must be a segment of default size or required_stack_space is 0.
const auto offset = DefaultStackSpaceSegmentSize - free_stack_space;
g.stack_space = stack_space_segment + offset;
g.prev_free_stack_space = free_stack_space;
free_stack_space -= required_stack_space;
return g;
}

const auto stack_space_size = std::size(stack_space);
const auto used_space = static_cast<size_t>(stack_space_ptr - stack_space);
[[maybe_unused]] const auto free_space = stack_space_size - used_space;

assert(required_stack_space <= free_space);

++depth;

g.stack_space = stack_space_ptr;
stack_space_ptr += required_stack_space;
g.prev_stack_space_segment = stack_space_segment;
const auto new_segment_size = std::max(DefaultStackSpaceSegmentSize, required_stack_space);
stack_space_segment = new Value[new_segment_size];
g.stack_space = stack_space_segment;
free_stack_space = new_segment_size - required_stack_space;
return g;
}
};
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/execute_call_depth_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ TEST(execute_call_depth, call_host_function_calling_wasm_function_inclusive)
constexpr auto host_f = [](std::any&, Instance& instance, const Value*,
ExecutionContext& ctx) noexcept {
recorded_depth = ctx.depth;
const auto ctx_guard = ctx.increment_call_depth();
const auto ctx_guard = ctx.allocate_stack_space();
return fizzy::execute(instance, 2 /* $leaf */, {}, ctx);
};

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

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

Expand Down Expand Up @@ -537,7 +537,7 @@ TEST(execute_call, call_host_function_calling_wasm_interleaved_infinite_recursio
ExecutionContext& ctx) noexcept {
EXPECT_LT(ctx.depth, DepthLimit);
++counter;
const auto ctx_guard = ctx.increment_call_depth();
const auto ctx_guard = ctx.allocate_stack_space();
return fizzy::execute(instance, 1, {}, ctx);
};

Expand Down

0 comments on commit fef2074

Please sign in to comment.