Skip to content

Commit b038f27

Browse files
authored
Merge pull request #3823 from bytecodealliance/dev/shared_heap
Implement the shared heap feature for interpreter, aot and llvm jit. Add below runtime APIs: ```C wasm_shared_heap_t wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args); bool wasm_runtime_attach_shared_heap(wasm_module_inst_t module_inst, wasm_shared_heap_t shared_heap); void wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst); uint64_t wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size, void **p_native_addr); void wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr); ``` And allow wasm app to call API shared_heap_malloc and shared_heap_free: ```C void *shared_heap_malloc(uint32_t size); void shared_heap_free(void *ptr); ```
2 parents 0152e2c + 19160f0 commit b038f27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2498
-104
lines changed

.github/scripts/codeql_buildscript.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ if [[ $? != 0 ]]; then
126126
exit 1;
127127
fi
128128

129+
# build iwasm with multi-memory enabled
130+
cd ${WAMR_DIR}/product-mini/platforms/linux
131+
rm -rf build && mkdir build && cd build
132+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BUILD_MULTI_MEMORY=1
133+
make -j
134+
if [[ $? != 0 ]]; then
135+
echo "Failed to build iwasm with multi-memory enabled!"
136+
exit 1;
137+
fi
138+
129139
# build iwasm with hardware boundary check disabled
130140
cd ${WAMR_DIR}/product-mini/platforms/linux
131141
rm -rf build && mkdir build && cd build
@@ -280,3 +290,23 @@ if [[ $? != 0 ]]; then
280290
echo "Failed to build iwasm with linux perf support enabled!"
281291
exit 1;
282292
fi
293+
294+
# build iwasm with shared heap enabled
295+
cd ${WAMR_DIR}/product-mini/platforms/linux
296+
rm -rf build && mkdir build && cd build
297+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BUILD_SHARED_HEAP=1
298+
make -j
299+
if [[ $? != 0 ]]; then
300+
echo "Failed to build iwasm with shared heap enabled!"
301+
exit 1;
302+
fi
303+
304+
# build iwasm with dynamic aot debug enabled
305+
cd ${WAMR_DIR}/product-mini/platforms/linux
306+
rm -rf build && mkdir build && cd build
307+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BUILD_DYNAMIC_AOT_DEBUG=1
308+
make -j
309+
if [[ $? != 0 ]];
310+
echo "Failed to build iwasm dynamic aot debug enabled!"
311+
exit 1;
312+
fi

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ jobs:
578578
./run.sh test1
579579
./run.sh test2
580580
581+
- name: Build Sample [shared-heap]
582+
run: |
583+
cd samples/shared-heap
584+
mkdir build && cd build
585+
cmake ..
586+
cmake --build . --config Debug --parallel 4
587+
./shared_heap_test
588+
./shared_heap_test --aot
589+
581590
test:
582591
needs:
583592
[

.github/workflows/compilation_on_macos.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,12 @@ jobs:
386386
./build.sh
387387
./run.sh test1
388388
./run.sh test2
389+
390+
- name: Build Sample [shared-heap]
391+
run: |
392+
cd samples/shared-heap
393+
mkdir build && cd build
394+
cmake ..
395+
cmake --build . --config Debug --parallel 4
396+
./shared_heap_test
397+
./shared_heap_test --aot

.github/workflows/nightly_run.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,15 @@ jobs:
593593
exit $?
594594
working-directory: ./wamr-app-framework/samples/simple
595595

596+
- name: Build Sample [shared-heap]
597+
run: |
598+
cd samples/shared-heap
599+
mkdir build && cd build
600+
cmake ..
601+
cmake --build . --config Debug --parallel 4
602+
./shared_heap_test
603+
./shared_heap_test --aot
604+
596605
test:
597606
needs:
598607
[

build-scripts/config_common.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ if (WAMR_BUILD_SHARED_MEMORY EQUAL 1)
256256
else ()
257257
add_definitions (-DWASM_ENABLE_SHARED_MEMORY=0)
258258
endif ()
259+
if (WAMR_BUILD_SHARED_HEAP EQUAL 1)
260+
add_definitions (-DWASM_ENABLE_SHARED_HEAP=1)
261+
message (" Shared heap enabled")
262+
endif()
263+
259264
if (WAMR_BUILD_MEMORY64 EQUAL 1)
260265
# if native is 32-bit or cross-compiled to 32-bit
261266
if (NOT WAMR_BUILD_TARGET MATCHES ".*64.*")
@@ -493,7 +498,7 @@ if (WAMR_BUILD_MODULE_INST_CONTEXT EQUAL 1)
493498
message (" Module instance context enabled")
494499
endif ()
495500
if (WAMR_BUILD_GC_HEAP_VERIFY EQUAL 1)
496-
add_definitions (-DWASM_ENABLE_GC_VERIFY=1)
501+
add_definitions (-DBH_ENABLE_GC_VERIFY=1)
497502
message (" GC heap verification enabled")
498503
endif ()
499504
if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)

build-scripts/runtime_lib.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ if (WAMR_BUILD_LIB_WASI_THREADS EQUAL 1)
119119
set (WAMR_BUILD_SHARED_MEMORY 1)
120120
endif ()
121121

122+
if (WAMR_BUILD_SHARED_HEAP EQUAL 1)
123+
include (${IWASM_DIR}/libraries/shared-heap/shared_heap.cmake)
124+
endif ()
125+
122126
if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
123127
set (WAMR_BUILD_THREAD_MGR 1)
124128
include (${IWASM_DIR}/libraries/debug-engine/debug_engine.cmake)
@@ -193,6 +197,7 @@ set (source_all
193197
${LIBC_EMCC_SOURCE}
194198
${LIB_RATS_SOURCE}
195199
${DEBUG_ENGINE_SOURCE}
200+
${LIB_SHARED_HEAP_SOURCE}
196201
)
197202

198203
set (WAMR_RUNTIME_LIB_SOURCE ${source_all})

core/config.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@
396396
#define APP_HEAP_SIZE_DEFAULT (8 * 1024)
397397
#endif
398398
#define APP_HEAP_SIZE_MIN (256)
399-
#define APP_HEAP_SIZE_MAX (512 * 1024 * 1024)
399+
/* The ems memory allocator supports maximal heap size 1GB,
400+
see ems_gc_internal.h */
401+
#define APP_HEAP_SIZE_MAX (1024 * 1024 * 1024)
400402

401403
/* Default min/max gc heap size of each app */
402404
#ifndef GC_HEAP_SIZE_DEFAULT
@@ -692,4 +694,8 @@
692694
#endif
693695
#endif /* WASM_ENABLE_FUZZ_TEST != 0 */
694696

697+
#ifndef WASM_ENABLE_SHARED_HEAP
698+
#define WASM_ENABLE_SHARED_HEAP 0
699+
#endif
700+
695701
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_runtime.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ bh_static_assert(sizeof(AOTMemoryInstance) == 120);
5757
bh_static_assert(offsetof(AOTTableInstance, elems) == 24);
5858

5959
bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
60+
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_base_addr_adj)
61+
== 8);
62+
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_start_off) == 16);
6063

6164
bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);
6265

@@ -1895,6 +1898,24 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
18951898
extra->stack_sizes =
18961899
aot_get_data_section_addr(module, AOT_STACK_SIZES_SECTION_NAME, NULL);
18971900

1901+
/*
1902+
* The AOT code checks whether the n bytes to access are in shared heap
1903+
* by checking whether the beginning address meets:
1904+
* addr >= start_off && addr <= end_off - n-bytes + 1
1905+
* where n is 1/2/4/8/16 and `end_off - n-bytes + 1` is constant, e.g.,
1906+
* UINT32_MAX, UINT32_MAX-1, UINT32_MAX-3 for n = 1, 2 or 4 in 32-bit
1907+
* target. To simplify the check, when shared heap is disabled, we set
1908+
* the start off to UINT64_MAX in 64-bit target and UINT32_MAX in 32-bit
1909+
* target, so in the checking, the above formula will be false, we don't
1910+
* need to check whether the shared heap is enabled or not in the AOT
1911+
* code.
1912+
*/
1913+
#if UINTPTR_MAX == UINT64_MAX
1914+
extra->shared_heap_start_off.u64 = UINT64_MAX;
1915+
#else
1916+
extra->shared_heap_start_off.u32[0] = UINT32_MAX;
1917+
#endif
1918+
18981919
#if WASM_ENABLE_PERF_PROFILING != 0
18991920
total_size = sizeof(AOTFuncPerfProfInfo)
19001921
* ((uint64)module->import_func_count + module->func_count);

core/iwasm/aot/aot_runtime.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ typedef struct AOTFunctionInstance {
111111

112112
typedef struct AOTModuleInstanceExtra {
113113
DefPointer(const uint32 *, stack_sizes);
114+
/*
115+
* Adjusted shared heap based addr to simple the calculation
116+
* in the aot code. The value is:
117+
* shared_heap->base_addr - shared_heap->start_off
118+
*/
119+
DefPointer(uint8 *, shared_heap_base_addr_adj);
120+
MemBound shared_heap_start_off;
121+
114122
WASMModuleInstanceExtraCommon common;
115123
AOTFunctionInstance **functions;
116124
uint32 function_count;
@@ -119,6 +127,10 @@ typedef struct AOTModuleInstanceExtra {
119127
bh_list *sub_module_inst_list;
120128
WASMModuleInstanceCommon **import_func_module_insts;
121129
#endif
130+
131+
#if WASM_ENABLE_SHARED_HEAP != 0
132+
WASMSharedHeap *shared_heap;
133+
#endif
122134
} AOTModuleInstanceExtra;
123135

124136
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)

0 commit comments

Comments
 (0)