Skip to content

Commit 41cb336

Browse files
authored
wasmtime: fix params array getting out of scope. (#272)
Broken in #217. Signed-off-by: Piotr Sikora <[email protected]>
1 parent 0c0bcd2 commit 41cb336

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
os: ubuntu-20.04
173173
arch: x86_64
174174
action: test
175-
flags: --config=clang
175+
flags: --config=clang -c opt
176176
- name: 'Wasmtime on Linux/x86_64 with ASan'
177177
engine: 'wasmtime'
178178
repo: 'com_github_bytecodealliance_wasmtime'

src/v8/v8.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
586586
const bool log = cmpLogLevel(LogLevel::trace);
587587
SaveRestoreContext saved_context(context);
588588
wasm::own<wasm::Trap> trap = nullptr;
589+
590+
// Workaround for MSVC++ not supporting zero-sized arrays.
589591
if constexpr (sizeof...(args) > 0) {
590592
wasm::Val params[] = {makeVal(args)...};
591593
if (log) {
@@ -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;
@@ -635,6 +638,8 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
635638
SaveRestoreContext saved_context(context);
636639
wasm::Val results[1];
637640
wasm::own<wasm::Trap> trap = nullptr;
641+
642+
// Workaround for MSVC++ not supporting zero-sized arrays.
638643
if constexpr (sizeof...(args) > 0) {
639644
wasm::Val params[] = {makeVal(args)...};
640645
if (log) {
@@ -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+
WasmTrapPtr trap;
598+
599+
// Workaround for MSVC++ not supporting zero-sized arrays.
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+
WasmTrapPtr trap;
660+
661+
// Workaround for MSVC++ not supporting zero-sized arrays.
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)