Skip to content

Commit 338ba4d

Browse files
committed
wasm fix, asset label fix
fix that shortening asset name could throw exception fix initializing thread environment in wasm calls disable hw bounds checks in wasm
1 parent 8e779ae commit 338ba4d

7 files changed

Lines changed: 85 additions & 33 deletions

File tree

externals/wamr/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ set(WAMR_BUILD_TAIL_CALL 1)
3434
set(WAMR_BUILD_THREAD_MGR 0)
3535
set(WAMR_BUILD_WASI_NN 0)
3636
set(WAMR_DISABLE_APP_ENTRY 1)
37-
set(WAMR_DISABLE_HW_BOUND_CHECK 0)
38-
set(WAMR_DISABLE_STACK_HW_BOUND_CHECK 0)
37+
set(WAMR_DISABLE_HW_BOUND_CHECK 1)
38+
set(WAMR_DISABLE_STACK_HW_BOUND_CHECK 1)
3939
set(WAMR_DISABLE_WAKEUP_BLOCKING_OP 0)
4040
add_compile_definitions(BUILTIN_LIBC_BUFFERED_PRINTF=1 BUILTIN_LIBC_BUFFERED_PRINT_SIZE=512)
4141
add_compile_definitions("$<$<CONFIG:Debug>:BH_DEBUG=1>")
@@ -65,11 +65,3 @@ if(MSVC)
6565
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
6666
endif()
6767
endif()
68-
69-
if(WAMR_DISABLE_STACK_OVERFLOW_CHECK)
70-
message(STATUS "disabling stack overflow detection")
71-
set(file "${CMAKE_CURRENT_LIST_DIR}/wamr/core/shared/platform/common/posix/posix_thread.c")
72-
set(flags "-Dos_thread_get_stack_boundary=os_thread_get_stack_boundary_original")
73-
set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS "${flags}")
74-
target_sources(iwasm_static PRIVATE os_thread_get_stack_boundary_dummy.c)
75-
endif()

externals/wamr/os_thread_get_stack_boundary_dummy.c

Lines changed: 0 additions & 8 deletions
This file was deleted.

sources/include/cage-core/wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ namespace cage
233233
class CAGE_CORE_API WasmInstance : private Immovable
234234
{
235235
public:
236-
String instanceName;
236+
AssetLabel instanceName;
237237

238238
Holder<WasmModule> module() const;
239239

sources/libcore/assets/assetsManager.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <cage-core/assetsManager.h>
1313
#include <cage-core/concurrent.h>
1414
#include <cage-core/concurrentQueue.h>
15-
#include <cage-core/config.h>
1615
#include <cage-core/debug.h>
1716
#include <cage-core/files.h>
1817
#include <cage-core/hashString.h>
@@ -355,9 +354,17 @@ namespace cage
355354
fetch = defaultFetch;
356355
}
357356

357+
AssetLabel convert(const String &name)
358+
{
359+
if (name.length() <= AssetLabel::MaxLength)
360+
return name;
361+
const uint32 start = name.length() - AssetLabel::MaxLength + 5;
362+
return Stringizer() + "..." + subString(name, start, m);
363+
}
364+
358365
Asset::Asset(AssetsManagerImpl *impl, uint32 scheme_, uint32 assetId, const String &textId_, Holder<void> &&value_) : AssetContext(impl, assetId)
359366
{
360-
textId = textId_.empty() ? (Stringizer() + "<" + assetId + "> with value") : textId_;
367+
textId = textId_.empty() ? (Stringizer() + "<" + assetId + "> with value") : convert(textId_);
361368
scheme = scheme_;
362369
impl->existsCounter++;
363370
*(AssetsScheme *)this = impl->schemes[scheme];
@@ -366,7 +373,7 @@ namespace cage
366373

367374
Asset::Asset(AssetsManagerImpl *impl, uint32 scheme_, uint32 assetId, const String &textId_, const AssetsScheme &customScheme_, Holder<void> &&customData_) : AssetContext(impl, assetId)
368375
{
369-
textId = textId_.empty() ? (Stringizer() + "<" + assetId + "> with custom scheme") : textId_;
376+
textId = textId_.empty() ? (Stringizer() + "<" + assetId + "> with custom scheme") : convert(textId_);
370377
scheme = scheme_;
371378
impl->existsCounter++;
372379
*(AssetsScheme *)this = customScheme_;
@@ -870,7 +877,7 @@ namespace cage
870877
CAGE_THROW_ERROR(Exception, "file is not a cage asset");
871878
if (h.version != CurrentAssetVersion)
872879
CAGE_THROW_ERROR(Exception, "cage asset version mismatch");
873-
asset->textId = h.textId.data();
880+
asset->textId = h.textId;
874881
if (h.scheme >= impl->schemes.size())
875882
CAGE_THROW_ERROR(Exception, "cage asset scheme out of range");
876883
asset->scheme = h.scheme;

sources/libcore/wasm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ namespace cage
236236

237237
WasmFunctionImpl(WasmInstanceImpl *instance, const String &name) : name(name), instance(instance)
238238
{
239-
CAGE_ASSERT(wasm_runtime_thread_env_inited());
239+
initialize();
240240

241241
func = wasm_runtime_lookup_function(instance->instance, name.c_str());
242242
if (!func)
@@ -480,6 +480,7 @@ namespace cage
480480

481481
void WasmFunctionInternal::call()
482482
{
483+
initialize();
483484
WasmFunctionImpl *impl = (WasmFunctionImpl *)this;
484485
if (!wasm_runtime_call_wasm(impl->instance->exec, impl->func, impl->data.size(), impl->data.data()))
485486
{

sources/test-core/assetsManager.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ void testAssetManager()
133133
{
134134
CAGE_TESTCASE("asset manager");
135135

136-
//configSetUint32("cage/assets/logLevel", 2);
137-
138136
{
139137
CAGE_TESTCASE("basics");
140138
Holder<AssetsManager> man = instantiate();
@@ -368,6 +366,22 @@ void testAssetManager()
368366
man->waitTillEmpty();
369367
}
370368

369+
{
370+
CAGE_TESTCASE("fabricated with long name");
371+
Holder<AssetsManager> man = instantiate();
372+
{
373+
Holder<AssetCounter> f = systemMemory().createHolder<AssetCounter>();
374+
man->loadValue<AssetSchemeIndexCounter, AssetCounter>(10, std::move(f), "really very very much very long long long name with a lot of text here to make sure it does not fit in the asset id string which is shorter than the string limit");
375+
}
376+
waitProcessing(man);
377+
CAGE_TEST(AssetCounter::counter == 1);
378+
CAGE_TEST((man->get<AssetSchemeIndexCounter, AssetCounter>(10)));
379+
man->unload(10);
380+
waitProcessing(man);
381+
CAGE_TEST(AssetCounter::counter == 0);
382+
man->waitTillEmpty();
383+
}
384+
371385
{
372386
CAGE_TESTCASE("non-existent asset");
373387
Holder<AssetsManager> man = instantiate();

sources/test-core/wasm.cpp

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cmath>
22

3+
#include <cage-core/concurrent.h>
34
#include <cage-core/wasm.h>
45

56
#include "main.h"
@@ -38,12 +39,8 @@ namespace
3839
WasmBuffer buf = inst->adapt(wasmAddr, inst->strLen(wasmAddr) + 1);
3940
return buf.read() == s;
4041
}
41-
}
42-
43-
void testWasm()
44-
{
45-
CAGE_TESTCASE("wasm");
4642

43+
void testSimpleFunctions()
4744
{
4845
CAGE_TESTCASE("simple functions");
4946
Holder<WasmModule> mod = newWasmModule(sums_wasm().cast<const char>());
@@ -112,6 +109,7 @@ void testWasm()
112109
}
113110
}
114111

112+
void testFunctionsWithBuffers()
115113
{
116114
CAGE_TESTCASE("functions with buffers");
117115
Holder<WasmModule> mod = newWasmModule(strings_wasm().cast<const char>());
@@ -150,8 +148,9 @@ void testWasm()
150148
}
151149
}
152150

151+
void testCppObjectsLifetimes()
153152
{
154-
CAGE_TESTCASE("foo (c++ objects lifetime)");
153+
CAGE_TESTCASE("c++ objects lifetime");
155154
Holder<WasmModule> mod = newWasmModule(foo_wasm().cast<const char>());
156155
printModuleDetails(+mod);
157156
Holder<WasmInstance> inst = wasmInstantiate(mod.share());
@@ -174,6 +173,7 @@ void testWasm()
174173
CAGE_TEST(get_map(30) == 900);
175174
}
176175

176+
void testMultipleInstantiations()
177177
{
178178
CAGE_TESTCASE("multiple instantiations of same module");
179179
Holder<WasmModule> mod = newWasmModule(strings_wasm().cast<const char>());
@@ -191,6 +191,7 @@ void testWasm()
191191
CAGE_TEST(testStr(+inst2, get2(), "ABCDEFGHIJ"));
192192
}
193193

194+
void testUnlinkedNativeFunction()
194195
{
195196
CAGE_TESTCASE("unlinked native functions");
196197
Holder<WasmModule> mod = newWasmModule(natives_wasm().cast<const char>());
@@ -200,9 +201,10 @@ void testWasm()
200201
CAGE_TEST_THROWN(call1());
201202
}
202203

204+
void testLinkedNativeFunctions()
203205
{
204206
CAGE_TESTCASE("linked native functions");
205-
uint32 fnc1cnt = 0, fnc2cnt = 0, fnc3cnt = 0;
207+
static uint32 fnc1cnt = 0, fnc2cnt = 0, fnc3cnt = 0;
206208
{
207209
Holder<WasmNatives> nats = newWasmNatives();
208210
nats->add(Delegate<void()>([&]() { fnc1cnt++; }), "cage_test_func1");
@@ -250,8 +252,52 @@ void testWasm()
250252
CAGE_TEST(fnc3cnt == 1);
251253
}
252254

255+
void testEmptyModule()
253256
{
254257
CAGE_TESTCASE("empty module");
255258
CAGE_TEST_THROWN(newWasmModule({}));
256259
}
260+
261+
void testInThreads()
262+
{
263+
CAGE_TESTCASE("wasm in threads");
264+
265+
struct Context
266+
{
267+
Holder<WasmModule> mod;
268+
Holder<WasmInstance> inst;
269+
WasmFunction<void()> call;
270+
};
271+
Context context;
272+
273+
{
274+
Holder<Thread> t1 = newThread([&]() { context.mod = newWasmModule(natives_wasm().cast<const char>()); }, "t1");
275+
t1->wait();
276+
}
277+
{
278+
Holder<Thread> t2 = newThread([&]() { context.inst = wasmInstantiate(context.mod.share()); }, "t2");
279+
t2->wait();
280+
}
281+
{
282+
Holder<Thread> t3 = newThread([&]() { context.call = context.inst->function<void()>("cage_test_call1"); }, "t3");
283+
t3->wait();
284+
}
285+
{
286+
Holder<Thread> t4 = newThread([&]() { context.call(); }, "t4");
287+
t4->wait();
288+
}
289+
}
290+
}
291+
292+
void testWasm()
293+
{
294+
CAGE_TESTCASE("wasm");
295+
testSimpleFunctions();
296+
testFunctionsWithBuffers();
297+
testCppObjectsLifetimes();
298+
testMultipleInstantiations();
299+
testUnlinkedNativeFunction();
300+
testLinkedNativeFunctions();
301+
testEmptyModule();
302+
testInThreads();
257303
}

0 commit comments

Comments
 (0)