diff --git a/externals/wamr/CMakeLists.txt b/externals/wamr/CMakeLists.txt index a4f1a1a7..9e82f1fc 100644 --- a/externals/wamr/CMakeLists.txt +++ b/externals/wamr/CMakeLists.txt @@ -28,7 +28,7 @@ set(WAMR_BUILD_LIB_WASI_THREADS 0) set(WAMR_BUILD_MINI_LOADER 0) set(WAMR_BUILD_MODULE_INST_CONTEXT 0) set(WAMR_BUILD_MULTI_MODULE 0) -set(WAMR_BUILD_REF_TYPES 0) +set(WAMR_BUILD_REF_TYPES 1) set(WAMR_BUILD_SHARED_MEMORY 0) set(WAMR_BUILD_SIMD 1) set(WAMR_BUILD_TAIL_CALL 1) diff --git a/sources/libcore/wasm.cpp b/sources/libcore/wasm.cpp index fc6f80e8..0c71264c 100644 --- a/sources/libcore/wasm.cpp +++ b/sources/libcore/wasm.cpp @@ -27,10 +27,11 @@ extern "C" void cage_wamr_log(uint32 log_level, const char *file, int line, const char *format, ...) { + String str; std::va_list va; va_start(va, format); - String str; std::vsnprintf(str.rawData(), str.MaxLength - 100, format, va); + va_end(va); str.rawLength() = std::strlen(str.rawData()); str = trim(str); SeverityEnum sev = SeverityEnum::Info; @@ -256,6 +257,15 @@ namespace cage data.resize(std::max(sz, countOfWasmVal(returns))); } }; + + template + void strCopyTruncate(detail::StringBase &dst, const char *src) + { + uint32 len = std::strlen(src); + if (len > A) + len = A; + dst = detail::StringBase(PointerRange(src, src + len)); + } } namespace privat @@ -332,7 +342,7 @@ namespace cage wasm_runtime_get_import_type(impl->module, i, &t); WasmSymbol w; w.moduleName = t.module_name; - w.name = t.name; + strCopyTruncate(w.name, t.name); w.kind = toString(t.kind); w.linked = t.linked; arr.push_back(w); @@ -351,7 +361,7 @@ namespace cage wasm_export_t t = {}; wasm_runtime_get_export_type(impl->module, i, &t); WasmSymbol w; - w.name = t.name; + strCopyTruncate(w.name, t.name); w.kind = toString(t.kind); w.linked = true; arr.push_back(w); diff --git a/sources/test-core/wasm.cpp b/sources/test-core/wasm.cpp index aa39fa53..405a14c3 100644 --- a/sources/test-core/wasm.cpp +++ b/sources/test-core/wasm.cpp @@ -7,6 +7,7 @@ PointerRange sums_wasm(); PointerRange strings_wasm(); PointerRange natives_wasm(); +PointerRange foo_wasm(); namespace { @@ -149,6 +150,30 @@ void testWasm() } } + { + CAGE_TESTCASE("foo (c++ objects lifetime)"); + Holder mod = newWasmModule(foo_wasm().cast()); + printModuleDetails(+mod); + Holder inst = wasmInstantiate(mod.share()); + WasmFunction get_foo = inst->function("get_foo"); + WasmFunction set_foo = inst->function("set_foo"); + WasmFunction get_map = inst->function("get_map"); + WasmFunction set_map = inst->function("set_map"); + CAGE_TEST(get_foo() == 2); + set_foo(123); + CAGE_TEST(get_foo() == 123); + CAGE_TEST(get_map(10) == 2); // previously non-existent key + set_map(10, 100); + WasmBuffer tmp = inst->allocate(12'000); + set_map(20, 400); + CAGE_TEST(get_map(10) == 100); + CAGE_TEST(get_map(20) == 400); + set_map(30, 900); + CAGE_TEST(get_map(10) == 100); + CAGE_TEST(get_map(20) == 400); + CAGE_TEST(get_map(30) == 900); + } + { CAGE_TESTCASE("multiple instantiations of same module"); Holder mod = newWasmModule(strings_wasm().cast()); diff --git a/sources/test-core/wasm/compile.sh b/sources/test-core/wasm/compile.sh index 5af6da7d..49e7f477 100644 --- a/sources/test-core/wasm/compile.sh +++ b/sources/test-core/wasm/compile.sh @@ -3,3 +3,5 @@ PATH="$PATH:/c/Program Files/LLVM/bin" clang --target=wasm32 -Wl,--export-all -Wl,--no-entry -nostdlib -o sums.wasm sums.c clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o strings.wasm strings.c clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o natives.wasm natives.c +#clang++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -nostartfiles -o foo.wasm foo.cpp +em++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostartfiles -O2 -o foo.wasm foo.cpp diff --git a/sources/test-core/wasm/foo.cpp b/sources/test-core/wasm/foo.cpp new file mode 100644 index 00000000..f8a30a7b --- /dev/null +++ b/sources/test-core/wasm/foo.cpp @@ -0,0 +1,38 @@ +#include + +struct Foo +{ + int data = 1; + Foo() { data = 2; } + ~Foo() { data = 3; } +}; + +Foo foo; + +extern "C" +{ + int get_foo(void) + { + return foo.data; + } + + void set_foo(int v) + { + foo.data = v; + } +} // extern C + +std::map map; + +extern "C" +{ + int get_map(int key) + { + return map[key].data; + } + + void set_map(int key, int val) + { + map[key].data = val; + } +} // extern C diff --git a/sources/test-core/wasm/foo.wasm b/sources/test-core/wasm/foo.wasm new file mode 100644 index 00000000..921c8402 Binary files /dev/null and b/sources/test-core/wasm/foo.wasm differ