wasm-micro-runtime/core/iwasm
YAMAMOTO Takashi 70c39bae77
wasi-nn: fix context lifetime issues (#4396)
* wasi-nn: fix context lifetime issues

use the module instance context api instead of trying to roll
our own with a hashmap. this fixes context lifetime problems mentioned in
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313.

namely,

* wasi-nn resources will be freed earlier now. before this change,
  they used to be kept until the runtime shutdown. (wasm_runtime_destroy)
  after this change, they will be freed together with the associated
  instances.

* wasm_module_inst_t pointer uniqueness assumption (which is wrong
  after wasm_runtime_deinstantiate) was lifted.

as a side effect, this change also makes a context shared among threads
within a cluster. note that this is a user-visible api/abi breaking change.
before this change, wasi-nn "handles" like wasi_ephemeral_nn_graph were
thread-local. after this change, they are shared among threads within
a cluster, similarly to wasi file descriptors. spec-wise, either behavior
should be ok simply because wasi officially doesn't have threads yet.
althogh i feel the latter semantics is more intuitive, if your application
depends on the thread-local behavior, this change breaks your application.

tested with wamr-wasi-extensions/samples/nn-cli, modified to
call each wasi-nn operations on different threads. (if you are
interested, you can find the modification at
https://github.com/yamt/wasm-micro-runtime/tree/yamt-nn-wip-20250619.)

cf.
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2430

* runtime_lib.cmake: enable WAMR_BUILD_MODULE_INST_CONTEXT for wasi-nn

as we do for wasi (WAMR_BUILD_LIBC_WASI)
2025-06-24 20:37:56 +08:00
..
aot add validation for array type in load_init_expr(GC only) (#4370) 2025-06-17 11:01:38 +08:00
common Collective fix for typos and minor bugs (#4369) 2025-06-17 09:26:00 +08:00
compilation Modify AOT static PGO to conform to llvm-18 and add a CI job to test static PGO on the coremark benchmark (#4345) 2025-06-12 16:57:11 +08:00
doc Fix some more spelling issues (#3393) 2024-05-08 09:30:29 +08:00
fast-jit Keep fix the CMake compatibility issue (#4180) 2025-04-15 12:51:19 +08:00
include feat: Add instruction metering for interpreter (#4122) 2025-05-26 16:16:42 +08:00
interpreter Collective fix for typos and minor bugs (#4369) 2025-06-17 09:26:00 +08:00
libraries wasi-nn: fix context lifetime issues (#4396) 2025-06-24 20:37:56 +08:00
README.md Add architecture diagram for wasm globals and classic-interp stack frame (#2058) 2023-03-25 09:39:20 +08:00