Skip to content

Commit c3e9b66

Browse files
authored
Fix jit memory overwritten after instance deinstantiate (#1936)
When de-instantiating the wasm module instance, remove it from the module's instance list before freeing func_ptrs and fast_jit_func_ptrs of the instance, to avoid accessing these freed memory in the JIT backend compilation threads.
1 parent 17f3375 commit c3e9b66

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,35 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
20662066
if (!module_inst)
20672067
return;
20682068

2069+
#if WASM_ENABLE_DEBUG_INTERP != 0 \
2070+
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
2071+
&& WASM_ENABLE_LAZY_JIT != 0)
2072+
/* Remove instance from module's instance list before freeing
2073+
func_ptrs and fast_jit_func_ptrs of the instance, to avoid
2074+
accessing the freed memory in the jit backend compilation
2075+
threads */
2076+
if (!is_sub_inst) {
2077+
WASMModule *module = module_inst->module;
2078+
WASMModuleInstance *instance_prev = NULL, *instance;
2079+
os_mutex_lock(&module->instance_list_lock);
2080+
2081+
instance = module->instance_list;
2082+
while (instance) {
2083+
if (instance == module_inst) {
2084+
if (!instance_prev)
2085+
module->instance_list = instance->e->next;
2086+
else
2087+
instance_prev->e->next = instance->e->next;
2088+
break;
2089+
}
2090+
instance_prev = instance;
2091+
instance = instance->e->next;
2092+
}
2093+
2094+
os_mutex_unlock(&module->instance_list_lock);
2095+
}
2096+
#endif
2097+
20692098
#if WASM_ENABLE_JIT != 0
20702099
if (module_inst->func_ptrs)
20712100
wasm_runtime_free(module_inst->func_ptrs);
@@ -2130,31 +2159,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
21302159
}
21312160
#endif
21322161

2133-
#if WASM_ENABLE_DEBUG_INTERP != 0 \
2134-
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
2135-
&& WASM_ENABLE_LAZY_JIT != 0)
2136-
if (!is_sub_inst) {
2137-
WASMModule *module = module_inst->module;
2138-
WASMModuleInstance *instance_prev = NULL, *instance;
2139-
os_mutex_lock(&module->instance_list_lock);
2140-
2141-
instance = module->instance_list;
2142-
while (instance) {
2143-
if (instance == module_inst) {
2144-
if (!instance_prev)
2145-
module->instance_list = instance->e->next;
2146-
else
2147-
instance_prev->e->next = instance->e->next;
2148-
break;
2149-
}
2150-
instance_prev = instance;
2151-
instance = instance->e->next;
2152-
}
2153-
2154-
os_mutex_unlock(&module->instance_list_lock);
2155-
}
2156-
#endif
2157-
21582162
#if WASM_ENABLE_SHARED_MEMORY != 0
21592163
if (module_inst->e->mem_lock_inited)
21602164
os_mutex_destroy(&module_inst->e->mem_lock);

0 commit comments

Comments
 (0)