Skip to content

Commit 973741d

Browse files
authored
Merge pull request #408 from bytecodealliance/main
Merge bytecodealliance:main into wenyongh:main
2 parents ff31bfc + b490a22 commit 973741d

File tree

27 files changed

+587
-167
lines changed

27 files changed

+587
-167
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ iwasm VM core
2929
- [Thread management and pthread library](./doc/pthread_library.md), ref to [sample](samples/multi-thread)
3030
- [Linux SGX (Intel Software Guard Extension) support](./doc/linux_sgx.md)
3131
- [Source debugging](./doc/source_debugging.md)
32+
- [XIP (Execution In Place) support](./doc/xip.md)
3233

3334
### post-MVP features
3435
- [Non-trapping float-to-int conversions](https://github.com/WebAssembly/nontrapping-float-to-int-conversions)

build-scripts/config_common.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
6161
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
6262
endif ()
6363
else ()
64-
add_definitions (-m32)
65-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
66-
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
64+
include(CheckCCompilerFlag)
65+
Check_C_Compiler_Flag( -m32 M32_OK )
66+
if (M32_OK)
67+
add_definitions (-m32)
68+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
69+
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
70+
endif ()
6771
endif ()
6872
endif ()
6973

core/iwasm/aot/aot_loader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,13 +2190,16 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
21902190
|| !strcmp(group->section_name, ".text")
21912191
#endif
21922192
) {
2193+
#if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \
2194+
&& !defined(BH_PLATFORM_DARWIN)
21932195
if (module->native_symbol_count > 0) {
21942196
set_error_buf(error_buf, error_buf_size,
21952197
"cannot apply relocation to text section "
21962198
"for aot file generated with "
21972199
"\"--enable-indirect-mode\" flag");
21982200
goto fail;
21992201
}
2202+
#endif
22002203
if (!do_text_relocation(module, group, error_buf, error_buf_size))
22012204
goto fail;
22022205
}

core/iwasm/common/wasm_exec_env.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
148148
wasm_exec_env_destroy_internal(exec_env);
149149
return NULL;
150150
}
151-
#if WASM_ENABLE_DEBUG_INTERP != 0
152-
wasm_debug_instance_create(cluster);
153-
#endif
154151
#endif /* end of WASM_ENABLE_THREAD_MGR */
155152

156153
return exec_env;
@@ -165,7 +162,6 @@ wasm_exec_env_destroy(WASMExecEnv *exec_env)
165162
if (cluster) {
166163
#if WASM_ENABLE_DEBUG_INTERP != 0
167164
wasm_cluster_thread_exited(exec_env);
168-
wasm_debug_instance_destroy(cluster);
169165
#endif
170166
wasm_cluster_terminate_all_except_self(cluster, exec_env);
171167
wasm_cluster_del_exec_env(cluster, exec_env);

core/iwasm/common/wasm_runtime_common.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,84 @@ get_package_type(const uint8 *buf, uint32 size)
294294
return Package_Type_Unknown;
295295
}
296296

297+
#if WASM_ENABLE_AOT != 0
298+
static uint8 *
299+
align_ptr(const uint8 *p, uint32 b)
300+
{
301+
uintptr_t v = (uintptr_t)p;
302+
uintptr_t m = b - 1;
303+
return (uint8 *)((v + m) & ~m);
304+
}
305+
306+
#define CHECK_BUF(buf, buf_end, length) \
307+
do { \
308+
if (buf + length < buf || buf + length > buf_end) \
309+
return false; \
310+
} while (0)
311+
312+
#define read_uint32(p, p_end, res) \
313+
do { \
314+
p = (uint8 *)align_ptr(p, sizeof(uint32)); \
315+
CHECK_BUF(p, p_end, sizeof(uint32)); \
316+
res = *(uint32 *)p; \
317+
p += sizeof(uint32); \
318+
} while (0)
319+
320+
bool
321+
wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
322+
{
323+
const uint8 *p = buf, *p_end = buf + size;
324+
uint32 section_type, sub_section_type, section_size;
325+
326+
if (get_package_type(buf, size) != Wasm_Module_AoT)
327+
return false;
328+
329+
CHECK_BUF(p, p_end, 8);
330+
p += 8;
331+
while (p < p_end) {
332+
read_uint32(p, p_end, section_type);
333+
read_uint32(p, p_end, section_size);
334+
CHECK_BUF(p, p_end, section_size);
335+
336+
if (section_type == AOT_SECTION_TYPE_CUSTOM) {
337+
read_uint32(p, p_end, sub_section_type);
338+
if (sub_section_type == AOT_CUSTOM_SECTION_NATIVE_SYMBOL) {
339+
return true;
340+
}
341+
else {
342+
p -= sizeof(uint32);
343+
}
344+
}
345+
else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
346+
return false;
347+
}
348+
p += section_size;
349+
}
350+
351+
return false;
352+
}
353+
#endif /* end of WASM_ENABLE_AOT */
354+
355+
#if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
356+
uint32
357+
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
358+
{
359+
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
360+
bh_assert(cluster);
361+
362+
if (cluster->debug_inst) {
363+
LOG_WARNING("Cluster already bind to a debug instance");
364+
return cluster->debug_inst->control_thread->port;
365+
}
366+
367+
if (wasm_debug_instance_create(cluster)) {
368+
return cluster->debug_inst->control_thread->port;
369+
}
370+
371+
return 0;
372+
}
373+
#endif
374+
297375
#if WASM_ENABLE_MULTI_MODULE != 0
298376
static module_reader reader;
299377
static module_destroyer destroyer;
@@ -1431,7 +1509,7 @@ wasm_runtime_create_exec_env_and_call_wasm(
14311509
if (module_inst->module_type == Wasm_Module_Bytecode)
14321510
ret = wasm_create_exec_env_and_call_function(
14331511
(WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function,
1434-
argc, argv);
1512+
argc, argv, true);
14351513
#endif
14361514
#if WASM_ENABLE_AOT != 0
14371515
if (module_inst->module_type == Wasm_Module_AoT)

core/iwasm/common/wasm_runtime_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ wasm_runtime_destroy(void);
406406
WASM_RUNTIME_API_EXTERN PackageType
407407
get_package_type(const uint8 *buf, uint32 size);
408408

409+
/* See wasm_export.h for description */
410+
WASM_RUNTIME_API_EXTERN bool
411+
wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
412+
409413
/* See wasm_export.h for description */
410414
WASM_RUNTIME_API_EXTERN WASMModuleCommon *
411415
wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf,
@@ -494,6 +498,12 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
494498
uint32 num_results, wasm_val_t *results,
495499
uint32 num_args, ...);
496500

501+
#if WASM_ENABLE_DEBUG_INTERP != 0
502+
/* See wasm_export.h for description */
503+
WASM_RUNTIME_API_EXTERN uint32
504+
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
505+
#endif
506+
497507
/**
498508
* Call a function reference of a given WASM runtime instance with
499509
* arguments.

core/iwasm/include/wasm_export.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ wasm_runtime_free(void *ptr);
235235
WASM_RUNTIME_API_EXTERN package_type_t
236236
get_package_type(const uint8_t *buf, uint32_t size);
237237

238+
/**
239+
* Check whether a file is an AOT XIP (Execution In Place) file
240+
*
241+
* @param buf the package buffer
242+
* @param size the package buffer size
243+
*
244+
* @return true if success, false otherwise
245+
*/
246+
WASM_RUNTIME_API_EXTERN bool
247+
wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
248+
238249
/**
239250
* It is a callback for WAMR providing by embedding to load a module file
240251
* into a buffer
@@ -408,6 +419,26 @@ wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
408419
WASM_RUNTIME_API_EXTERN void
409420
wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
410421

422+
/**
423+
* Start debug instance based on given execution environment.
424+
* Note:
425+
* The debug instance will be destroyed during destroying the
426+
* execution environment, developers don't need to destroy it
427+
* manually.
428+
* If the cluster of this execution environment has already
429+
* been bound to a debug instance, this function will return true
430+
* directly.
431+
* If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
432+
* don't need to call this function for every spawned exec_env as
433+
* they are sharing the same cluster with the main exec_env.
434+
*
435+
* @param exec_env the execution environment to start debug instance
436+
*
437+
* @return debug port if success, 0 otherwise.
438+
*/
439+
WASM_RUNTIME_API_EXTERN uint32_t
440+
wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
441+
411442
/**
412443
* Initialize thread environment.
413444
* Note:

core/iwasm/interpreter/wasm.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,20 @@ struct WASMModule {
419419
uint64 buf_code_size;
420420
#endif
421421

422+
#if WASM_ENABLE_DEBUG_INTERP != 0
423+
/**
424+
* Count how many instances reference this module. When source
425+
* debugging feature enabled, the debugger may modify the code
426+
* section of the module, so we need to report a warning if user
427+
* create several instances based on the same module
428+
*
429+
* Sub_instances created by lib-pthread or spawn API will not
430+
* influence or check the ref count
431+
*/
432+
uint32 ref_count;
433+
korp_mutex ref_count_lock;
434+
#endif
435+
422436
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
423437
const uint8 *name_section_buf;
424438
const uint8 *name_section_buf_end;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,10 @@ create_module(char *error_buf, uint32 error_buf_size)
32373237
#endif
32383238
#if WASM_ENABLE_DEBUG_INTERP != 0
32393239
bh_list_init(&module->fast_opcode_list);
3240+
if (os_mutex_init(&module->ref_count_lock) != 0) {
3241+
wasm_runtime_free(module);
3242+
return NULL;
3243+
}
32403244
#endif
32413245
return module;
32423246
}
@@ -3568,6 +3572,7 @@ wasm_loader_unload(WASMModule *module)
35683572
wasm_runtime_free(fast_opcode);
35693573
fast_opcode = next;
35703574
}
3575+
os_mutex_destroy(&module->ref_count_lock);
35713576
#endif
35723577
wasm_runtime_free(module);
35733578
}

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ execute_post_inst_function(WASMModuleInstance *module_inst)
900900
return true;
901901

902902
return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
903-
0, NULL);
903+
0, NULL, false);
904904
}
905905

906906
#if WASM_ENABLE_BULK_MEMORY != 0
@@ -929,7 +929,7 @@ execute_memory_init_function(WASMModuleInstance *module_inst)
929929
return true;
930930

931931
return wasm_create_exec_env_and_call_function(module_inst, memory_init_func,
932-
0, NULL);
932+
0, NULL, false);
933933
}
934934
#endif
935935

@@ -944,7 +944,8 @@ execute_start_function(WASMModuleInstance *module_inst)
944944
bh_assert(!func->is_import_func && func->param_cell_num == 0
945945
&& func->ret_cell_num == 0);
946946

947-
return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
947+
return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL,
948+
false);
948949
}
949950

950951
static bool
@@ -972,11 +973,11 @@ execute_malloc_function(WASMModuleInstance *module_inst,
972973
}
973974

974975
ret = wasm_create_exec_env_and_call_function(module_inst, malloc_func, argc,
975-
argv);
976+
argv, false);
976977

977978
if (retain_func && ret) {
978979
ret = wasm_create_exec_env_and_call_function(module_inst, retain_func,
979-
1, argv);
980+
1, argv, false);
980981
}
981982

982983
if (ret)
@@ -992,7 +993,7 @@ execute_free_function(WASMModuleInstance *module_inst,
992993

993994
argv[0] = offset;
994995
return wasm_create_exec_env_and_call_function(module_inst, free_func, 1,
995-
argv);
996+
argv, false);
996997
}
997998

998999
#if WASM_ENABLE_MULTI_MODULE != 0
@@ -1125,6 +1126,19 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
11251126
if (!module)
11261127
return NULL;
11271128

1129+
#if WASM_ENABLE_DEBUG_INTERP != 0
1130+
if (!is_sub_inst) {
1131+
os_mutex_lock(&module->ref_count_lock);
1132+
if (module->ref_count != 0) {
1133+
LOG_WARNING(
1134+
"warning: multiple instances referencing the same module may "
1135+
"cause unexpected behaviour during debugging");
1136+
}
1137+
module->ref_count++;
1138+
os_mutex_unlock(&module->ref_count_lock);
1139+
}
1140+
#endif
1141+
11281142
/* Check heap size */
11291143
heap_size = align_uint(heap_size, 8);
11301144
if (heap_size > APP_HEAP_SIZE_MAX)
@@ -1133,6 +1147,13 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
11331147
/* Allocate the memory */
11341148
if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance), error_buf,
11351149
error_buf_size))) {
1150+
#if WASM_ENABLE_DEBUG_INTERP != 0
1151+
if (!is_sub_inst) {
1152+
os_mutex_lock(&module->ref_count_lock);
1153+
module->ref_count--;
1154+
os_mutex_unlock(&module->ref_count_lock);
1155+
}
1156+
#endif
11361157
return NULL;
11371158
}
11381159

@@ -1519,7 +1540,9 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
15191540
(WASMModuleInstanceCommon *)module_inst);
15201541
#endif
15211542
(void)global_data_end;
1543+
15221544
return module_inst;
1545+
15231546
fail:
15241547
wasm_deinstantiate(module_inst, false);
15251548
return NULL;
@@ -1576,6 +1599,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
15761599
}
15771600
#endif
15781601

1602+
#if WASM_ENABLE_DEBUG_INTERP != 0
1603+
if (!is_sub_inst) {
1604+
os_mutex_lock(&module_inst->module->ref_count_lock);
1605+
module_inst->module->ref_count--;
1606+
os_mutex_unlock(&module_inst->module->ref_count_lock);
1607+
}
1608+
#endif
1609+
15791610
wasm_runtime_free(module_inst);
15801611
}
15811612

@@ -1661,7 +1692,8 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
16611692
bool
16621693
wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
16631694
WASMFunctionInstance *func,
1664-
unsigned argc, uint32 argv[])
1695+
unsigned argc, uint32 argv[],
1696+
bool enable_debug)
16651697
{
16661698
WASMExecEnv *exec_env;
16671699
bool ret;
@@ -1680,6 +1712,11 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
16801712
}
16811713

16821714
#if WASM_ENABLE_THREAD_MGR != 0
1715+
if (enable_debug) {
1716+
#if WASM_ENABLE_DEBUG_INTERP != 0
1717+
wasm_runtime_start_debug_instance(exec_env);
1718+
#endif
1719+
}
16831720
}
16841721
#endif
16851722

0 commit comments

Comments
 (0)