Skip to content

Commit 39bbd57

Browse files
committed
wasmtime: fix for params array getting out of scope.
Broken in proxy-wasm#217. Signed-off-by: Piotr Sikora <[email protected]>
1 parent 579de9b commit 39bbd57

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ jobs:
180180
arch: x86_64
181181
action: test
182182
flags: --config=clang-asan-strict --define=crypto=system
183+
- name: 'Wasmtime on Linux/x86_64 with TSan'
184+
engine: 'wasmtime'
185+
repo: 'com_github_bytecodealliance_wasmtime'
186+
os: ubuntu-20.04
187+
arch: x86_64
188+
action: test
189+
flags: --config=clang-tsan
183190
- name: 'Wasmtime on Linux/aarch64'
184191
engine: 'wasmtime'
185192
repo: 'com_github_bytecodealliance_wasmtime'

src/v8/v8.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
585585
*function = [func, function_name, this](ContextBase *context, Args... args) -> void {
586586
const bool log = cmpLogLevel(LogLevel::trace);
587587
SaveRestoreContext saved_context(context);
588+
589+
// Workaround for MSVC++ not supporting zero-sized arrays.
588590
wasm::own<wasm::Trap> trap = nullptr;
589591
if constexpr (sizeof...(args) > 0) {
590592
wasm::Val params[] = {makeVal(args)...};
@@ -599,6 +601,7 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
599601
}
600602
trap = func->call(nullptr, nullptr);
601603
}
604+
602605
if (trap) {
603606
fail(FailState::RuntimeError, getFailMessage(std::string(function_name), std::move(trap)));
604607
return;
@@ -634,6 +637,8 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
634637
const bool log = cmpLogLevel(LogLevel::trace);
635638
SaveRestoreContext saved_context(context);
636639
wasm::Val results[1];
640+
641+
// Workaround for MSVC++ not supporting zero-sized arrays.
637642
wasm::own<wasm::Trap> trap = nullptr;
638643
if constexpr (sizeof...(args) > 0) {
639644
wasm::Val params[] = {makeVal(args)...};
@@ -648,6 +653,7 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
648653
}
649654
trap = func->call(nullptr, results);
650655
}
656+
651657
if (trap) {
652658
fail(FailState::RuntimeError, getFailMessage(std::string(function_name), std::move(trap)));
653659
return R{};

src/wasmtime/wasmtime.cc

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -591,21 +591,28 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
591591
}
592592

593593
*function = [func, function_name, this](ContextBase *context, Args... args) -> void {
594-
wasm_val_vec_t params;
594+
const bool log = cmpLogLevel(LogLevel::trace);
595+
SaveRestoreContext saved_context(context);
596+
wasm_val_vec_t results = WASM_EMPTY_VEC;
597+
598+
// Workaround for MSVC++ not supporting zero-sized arrays.
599+
WasmTrapPtr trap;
595600
if constexpr (sizeof...(args) > 0) {
596601
wasm_val_t params_arr[] = {makeVal(args)...};
597-
params = WASM_ARRAY_VEC(params_arr);
602+
wasm_val_vec_t params = WASM_ARRAY_VEC(params_arr);
603+
if (log) {
604+
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
605+
printValues(&params) + ")");
606+
}
607+
trap.reset(wasm_func_call(func, &params, &results));
598608
} else {
599-
params = WASM_EMPTY_VEC;
600-
}
601-
wasm_val_vec_t results = WASM_EMPTY_VEC;
602-
const bool log = cmpLogLevel(LogLevel::trace);
603-
if (log) {
604-
integration()->trace("[host->vm] " + std::string(function_name) + "(" + printValues(&params) +
605-
")");
609+
wasm_val_vec_t params = WASM_EMPTY_VEC;
610+
if (log) {
611+
integration()->trace("[host->vm] " + std::string(function_name) + "()");
612+
}
613+
trap.reset(wasm_func_call(func, &params, &results));
606614
}
607-
SaveRestoreContext saved_context(context);
608-
WasmTrapPtr trap{wasm_func_call(func, &params, &results)};
615+
609616
if (trap) {
610617
WasmByteVec error_message;
611618
wasm_trap_message(trap.get(), error_message.get());
@@ -645,22 +652,29 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
645652
}
646653

647654
*function = [func, function_name, this](ContextBase *context, Args... args) -> R {
648-
wasm_val_vec_t params;
655+
const bool log = cmpLogLevel(LogLevel::trace);
656+
SaveRestoreContext saved_context(context);
657+
wasm_val_t results_arr[1];
658+
wasm_val_vec_t results = WASM_ARRAY_VEC(results_arr);
659+
660+
// Workaround for MSVC++ not supporting zero-sized arrays.
661+
WasmTrapPtr trap;
649662
if constexpr (sizeof...(args) > 0) {
650663
wasm_val_t params_arr[] = {makeVal(args)...};
651-
params = WASM_ARRAY_VEC(params_arr);
664+
wasm_val_vec_t params = WASM_ARRAY_VEC(params_arr);
665+
if (log) {
666+
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
667+
printValues(&params) + ")");
668+
}
669+
trap.reset(wasm_func_call(func, &params, &results));
652670
} else {
653-
params = WASM_EMPTY_VEC;
654-
}
655-
wasm_val_t results_arr[1];
656-
wasm_val_vec_t results = WASM_ARRAY_VEC(results_arr);
657-
const bool log = cmpLogLevel(LogLevel::trace);
658-
if (log) {
659-
integration()->trace("[host->vm] " + std::string(function_name) + "(" + printValues(&params) +
660-
")");
671+
wasm_val_vec_t params = WASM_EMPTY_VEC;
672+
if (log) {
673+
integration()->trace("[host->vm] " + std::string(function_name) + "()");
674+
}
675+
trap.reset(wasm_func_call(func, &params, &results));
661676
}
662-
SaveRestoreContext saved_context(context);
663-
WasmTrapPtr trap{wasm_func_call(func, &params, &results)};
677+
664678
if (trap) {
665679
WasmByteVec error_message;
666680
wasm_trap_message(trap.get(), error_message.get());

0 commit comments

Comments
 (0)