Skip to content

Commit 5b99b52

Browse files
authored
Merge pull request #779 from wasmx/execution_context
Execution context generalization
2 parents a1a1b61 + 6095fb6 commit 5b99b52

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

lib/fizzy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_sources(
2121
exceptions.hpp
2222
execute.cpp
2323
execute.hpp
24+
execution_context.hpp
2425
instantiate.cpp
2526
instantiate.hpp
2627
instructions.cpp

lib/fizzy/execute.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan
537537
assert(stack.size() >= num_args);
538538
const auto call_args = stack.rend() - num_args;
539539

540-
const auto ctx_guard = ctx.increment_call_depth();
541540
const auto ret = execute(instance, func_idx, call_args, ctx);
542541
// Bubble up traps
543542
if (ret.trapped)
@@ -574,6 +573,8 @@ ExecutionResult execute(
574573
const auto& code = instance.module->get_code(func_idx);
575574
auto* const memory = instance.memory.get();
576575

576+
const auto local_ctx = ctx.create_local_context();
577+
577578
OperandStack stack(args, func_type.inputs.size(), code.local_count,
578579
static_cast<size_t>(code.max_stack_height));
579580

lib/fizzy/execute.hpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "cxx20/span.hpp"
88
#include "exceptions.hpp"
9+
#include "execution_context.hpp"
910
#include "instantiate.hpp"
1011
#include "limits.hpp"
1112
#include "module.hpp"
@@ -41,33 +42,6 @@ constexpr ExecutionResult Void{true};
4142
/// Shortcut for execution that resulted in a trap.
4243
constexpr ExecutionResult Trap{false};
4344

44-
/// The storage for information shared by calls in the same execution "thread".
45-
/// Users may decide how to allocate the execution context, but some good defaults are available.
46-
class ExecutionContext
47-
{
48-
/// Call depth increment guard.
49-
/// It will automatically decrement the call depth to the original value
50-
/// when going out of scope.
51-
class [[nodiscard]] Guard
52-
{
53-
ExecutionContext& m_execution_context; ///< Reference to the guarded execution context.
54-
55-
public:
56-
explicit Guard(ExecutionContext& ctx) noexcept : m_execution_context{ctx} {}
57-
~Guard() noexcept { --m_execution_context.depth; }
58-
};
59-
60-
public:
61-
int depth = 0; ///< Current call depth.
62-
63-
/// Increments the call depth and returns the guard object which decrements
64-
/// the call depth back to the original value when going out of scope.
65-
Guard increment_call_depth() noexcept
66-
{
67-
++depth;
68-
return Guard{*this};
69-
}
70-
};
7145

7246
/// Execute a function from an instance.
7347
///

lib/fizzy/execution_context.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2021 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#pragma once
6+
7+
namespace fizzy
8+
{
9+
/// The storage for information shared by calls in the same execution "thread".
10+
/// Users may decide how to allocate the execution context, but some good defaults are available.
11+
class ExecutionContext
12+
{
13+
/// Call local execution context.
14+
/// It will automatically decrement the call depth to the original value
15+
/// when going out of scope.
16+
class [[nodiscard]] LocalContext
17+
{
18+
ExecutionContext& m_shared_ctx; ///< Reference to the shared execution context.
19+
20+
public:
21+
LocalContext(const LocalContext&) = delete;
22+
LocalContext(LocalContext&&) = delete;
23+
LocalContext& operator=(const LocalContext&) = delete;
24+
LocalContext& operator=(LocalContext&&) = delete;
25+
26+
explicit LocalContext(ExecutionContext& ctx) noexcept : m_shared_ctx{ctx}
27+
{
28+
++m_shared_ctx.depth;
29+
}
30+
31+
~LocalContext() noexcept { --m_shared_ctx.depth; }
32+
};
33+
34+
public:
35+
int depth = 0; ///< Current call depth.
36+
37+
/// Increments the call depth and returns the local call context which
38+
/// decrements the call depth back to the original value when going out of scope.
39+
LocalContext create_local_context() noexcept { return LocalContext{*this}; }
40+
};
41+
} // namespace fizzy

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 local_ctx = ctx.create_local_context();
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 local_ctx = ctx.create_local_context();
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 local_ctx = ctx.create_local_context();
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 local_ctx = ctx.create_local_context();
541541
return fizzy::execute(instance, 1, {}, ctx);
542542
};
543543

0 commit comments

Comments
 (0)