Comments (12)
Hi, for the first one, could you check whether the native method is called successfully (e.g. add a printf in the native method), and whether the exception is thrown when calling init()
method (e.g. call wasm_runtime_get_exception
after it)?
For the second one, please refer to samples/multi-module.
from wasm-micro-runtime.
1、Create a thread to load the wasm app demo1 and execute init(). At this point, the printf function in the init function works well. However, if the init() function calls a native method NativeA(), which loads and runs a new wasm app demo2, the printf output in the init() method of demo2 does not display, although it runs correctly. I am concerned that there might be other issues, so I wanted to ask.
2.I couldn't find an example of how to pass strings in the multi-module example.
from wasm-micro-runtime.
from wasm-micro-runtime.
It is a little confusing that it runs good in Ubuntu while failed in your environment. What target and what platform do you use? And did you create another exec_env to call the app's method?
BTW, for multi-module, maybe you can try wasm_runtime_module_malloc
in native method to allocate memory from the second module instance's heap, copy the string from the first module instance to it, and then pass the allocated memory offset to second module instance. Refer to:
from wasm-micro-runtime.
target:windows10
platform:windows10
The compilation and running of wamr are both completed on Windows.
The compilation of wasm is completed using the following:
cmake -G "Unix Makefiles" -DWASI_SDK_PREFIX=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m -DCMAKE_TOOLCHAIN_FILE=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m/share/cmake/wasi-sdk.cmake -DCMAKE_SYSROOT=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m/share/wasi-sysroot ..
Code:
I am calling a native method A within a WASM method. a thread is created In this method A. Within this thread, methods such as wasm_runtime_init_thread_env(), wasm_runtime_init(), bh_read_file_to_buffer(...), wasm_runtime_load(...), wasm_runtime_instantiate(...), wasm_runtime_create_exec_env(...), wasm_runtime_lookup_function(...), wasm_runtime_module_malloc(...), wasm_runtime_call_wasm(.test1..), etc., are used. Is there any issue with this approach?
In function test1,Except for printf not displaying, everything else seems to be working fine.
from wasm-micro-runtime.
In the thread created by method A, should not call wasm_runtime_init and wasm_runtime_init_thread_env again since they have been inited. Could you try removing them?
from wasm-micro-runtime.
This is the A method:
`
int NativeApi_A(wasm_exec_env_t exec_env, char* strJson)
{
std::thread theadMamr(ThreadLoadWasm, strJson);
theadMamr.detach();
return 1;
}
static int ThreadLoadWasm(char* param,)
{
//wasm_runtime_init_thread_env();
Json root = json::parse((char*)param);
if (root == NULL)
{
wasm_runtime_destroy_thread_env();
return 0;
}
co::print("NativeApi_Load_Wasm:", root.str());
string wasmFile = str::cat("wasm/", root.get("wasmfile").as_c_str()).c_str();
/* initialize the wasm runtime by default configurations */
//wasm_runtime_init();
uint32_t wasm_file_size;
uint8_t* wasm_file_buf = nullptr;
wasm_file_buf = (uint8_t*)bh_read_file_to_buffer(/*wasm_path*/wasmFile.c_str(), &wasm_file_size);
if (!wasm_file_buf || wasm_file_size <= 0) {
//printf("open wasm app file [%s] failed.\n", /*wasm_path*/wasm_file);
DLOG << "open wasm app file" << wasmFile << "failed.";
wasm_runtime_destroy_thread_env();
return 0;
}
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
char error_buf[255] = { 0 };
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf))))
{
DLOG << "wasm_runtime_load:" << error_buf;
wasm_runtime_destroy_thread_env();
return 0;
}
uint32_t stack_size = /*2*1024*/64 * 1024, heap_size = 64 * 1024;
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
error_buf, sizeof(error_buf))))
{
DLOG << "wasm_runtime_instantiate:" << error_buf;
wasm_runtime_destroy_thread_env();
return 0;
}
wasm_exec_env_t exec_env = NULL;
exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
if (!exec_env) {
//printf("Create wasm execution environment failed.\n");
DLOG << "wasm_runtime_create_exec_env failed";
wasm_runtime_destroy_thread_env();
return 0;
}
wasm_function_inst_t init = NULL;
if (!(init = wasm_runtime_lookup_function(wasm_module_inst, "init")))
{
DLOG << "The init wasm function is not found.";
wasm_runtime_destroy_thread_env();
return 0;
}
int bufLen = strlen((char*)param)+1;
char* buffer1 = NULL;
uint32_t wasmBuffer = wasm_runtime_module_malloc(wasm_module_inst, bufLen, (void**)&buffer1);
if (wasmBuffer != 0)
{
uint32_t argv[2];
strncpy(buffer1, (char*)param, bufLen);
argv[0] = wasmBuffer; /* pass the buffer address for WASM space */
argv[1] = bufLen;
if (!wasm_runtime_call_wasm(exec_env, init, 2, argv))
{
const char* errInfo = wasm_runtime_get_exception(wasm_module_inst);
DLOG << "Native wasm_runtime_call_wasm call test faild.err msg:" << errInfo;
}
int retAppId = argv[0];
wasm_runtime_module_free(wasm_module_inst, wasmBuffer);
}
while (g_bAppIsRuning)
{
co::sleep(100);
}
//wasm_runtime_destroy_thread_env();
}
`
If remove wasm_runtime_init_thread_env and wasm_runtime_init method,an error will occur when run wasm_runtime_instantiate:
WASM module instantiate failed: Exception: thread signal env not inited
from wasm-micro-runtime.
Don't remove wasm_runtime_init_thread_env in ThreadLoadWasm as the thread is not created by runtime, and ensure that wasm_runtime_destroy_thread_env is called before ThreadLoadWasm returns. And keep removing wasm_runtime_init and wasm_runtime_destroy int ThreadLoadWasm since we just need to init runtime one time.
BTW, will the first module instance exits before ThreadLoadWasm calls the wasm function? Since char* strJson
comes from the module instance's linear memory.
from wasm-micro-runtime.
Is it better to create threads using the 'wasm_runtime_spawn_thread' function? Maybe the printf method can display msg since the first module can use printf to display msg.
The first module will not exit before ThreadLoadWasm calls the wasm function.
from wasm-micro-runtime.
No, if the thread is to run a function of the same module instance that the thread belongs to, I think we had better use wasm_runtime_spawn_thread
, otherwise had better create the thread by host embedder.
Could you print string param and buffer1 to ensure they are correct in the code below before calling wasm function:
int bufLen = strlen((char*)param)+1;
char* buffer1 = NULL;
uint32_t wasmBuffer = wasm_runtime_module_malloc(wasm_module_inst, bufLen, (void**)&buffer1);
if (wasmBuffer != 0)
{
uint32_t argv[2];
strncpy(buffer1, (char*)param, bufLen);
argv[0] = wasmBuffer; /* pass the buffer address for WASM space */
argv[1] = bufLen;
if (!wasm_runtime_call_wasm(exec_env, init, 2, argv))
BTW, did you add -Wl,--export=malloc -Wl,--export=free
when building the wasm file? If not using the -nostdlib
flag, had better add them to let runtime call the malloc/free function exported instead of inserting a host managed heap into the linear memory.
from wasm-micro-runtime.
Print well before calling wasm function.
But print nothing in second wasm module. But print ok with native NativeApi_Print() function.
int NativeApi_Print(wasm_exec_env_t exec_env,char* msg) { printf("%s\n",msg); return 1; }
I have added them:
target_link_options(demoS7.wasm PRIVATE LINKER:--export=__heap_base LINKER:--export=__data_end LINKER:--export=malloc LINKER:--export=free LINKER:--export=init LINKER:--export=onMessage LINKER:--export=onDataRecv LINKER:--shared-memory,--max-memory=10485760 LINKER:--no-check-features LINKER:--allow-undefined )
from wasm-micro-runtime.
Could you check whether the pointers of param are the same to ensure that the pointer is correctly passed into NativeApi_Print? e.g. in ThreadLoadWasm, before calling wasm function, and in NativeApi_Print.
from wasm-micro-runtime.
Related Issues (20)
- [RFC] wasm32-wasi-p2-module HOT 7
- [WASI] Should we overlook or restrict the access modes for stdin/stdout/stderr? HOT 4
- CI doesn't cover shared library
- Unable to run C++ program in esp32 HOT 9
- How to use the native API with int64 parameters? HOT 1
- Why do functions implemented in wasm require more than one parameter when calling CallFuncV? HOT 3
- Global mutex does not work HOT 1
- Have an issue while importing jump functions in runtime(iwasm) HOT 1
- File operations are not supporting HOT 6
- An invalid section specifier `.aot_stack_sizes` HOT 1
- AOT module load failed: resolve symbol __atomic_compare_exchange_4 failed HOT 10
- The qWasmStackValue command is missing during runtime debugging
- AOT_SECTION_TYPE_SIGNATURE HOT 2
- WASI sockets: POSIX platform `os_socket_recvfrom` unintended behavior HOT 7
- sysbench mutex workload is taking more execution time with iwasm than native x86_64 execution HOT 2
- sysbench memory and threads workload is giving less benchmarking results with iwasm than native aarch64 gcc execution HOT 12
- Updating Exception Handling in WAMR HOT 20
- [RFC] Option to reduce code size of the generated AOT file when stack trace is enabled HOT 7
- sysbench wasm module of CPU workload is giving unexpected results than native gcc compiled sysbench cpu workload.
- MIPS32 support / alignment issues HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasm-micro-runtime.