Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,53 @@ config OCRE_NETWORKING
default n
help
Enable networking support for containers.


config OCRE_SHARED_HEAP
bool "Enable container shared heap support"
default n
help
Enable shared heap support for containers.

config OCRE_SHARED_HEAP_BUF_SIZE
int "Shared heap buffer size in bytes"
default 65536
depends on OCRE_SHARED_HEAP
help
Size of the pre-allocated buffer for the shared heap.
This memory is shared between WebAssembly modules.

choice OCRE_SHARED_HEAP_MODE
prompt "Shared heap mode"
depends on OCRE_SHARED_HEAP
default OCRE_SHARED_HEAP_BUF_VIRTUAL
help
Select the shared heap memory mode:
- Physical: Map physical hardware registers (e.g., GPIO) to WASM address space
- Virtual: Allocate shared heap from regular RAM

config OCRE_SHARED_HEAP_BUF_PHYSICAL
bool "Physical (hardware register mapping)"
help
Enable physical memory mapping for hardware access.
Maps physical hardware registers (like GPIO at 0x42020000) to WASM address space.
Use this when containers need direct access to hardware peripherals.

config OCRE_SHARED_HEAP_BUF_VIRTUAL
bool "Virtual (RAM allocation)"
help
Enable virtual shared heap allocated from regular RAM.
Use this for normal inter-module communication without
direct hardware access.

endchoice

config OCRE_SHARED_HEAP_BUF_ADDRESS
hex "Shared heap buffer address"
default 0x00
depends on OCRE_SHARED_HEAP
help
Shared heap buffer address.
- For physical mode: Physical address of hardware registers
- For virtual mode: Leave as 0x00 to auto-allocate from RAM

endmenu
70 changes: 69 additions & 1 deletion src/ocre/components/container_supervisor/cs_sm_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ static size_t ocre_get_available_memory(void) {
}
#endif

#ifdef CONFIG_OCRE_SHARED_HEAP
// Shared heap for memory-mapped access
wasm_shared_heap_t _shared_heap = NULL;
#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
uint8 preallocated_buf[CONFIG_OCRE_SHARED_HEAP_BUF_SIZE];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allocate and put this in the external ram (storage_malloc) or (user_malloc) instead of making it global/static?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with either - but with current solution we would still use system memory (just allocated dynamically) on systems with no PSRAM, will need to cut a bit WAMR's linear memory and allocate/deallocate memory during the runtime init.
I propose the following: we keep it this way as initial commit for this features, and once the shared_memory is available and non-kernel stuff are using the same pull, we will move everything (inc. this) to use that.

#endif
#endif

static bool validate_container_memory(ocre_container_t *container) {
#ifdef CONFIG_OCRE_MEMORY_CHECK_ENABLED
size_t requested_heap = container->ocre_container_data.heap_size;
Expand Down Expand Up @@ -266,6 +274,30 @@ ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container
#ifdef CONFIG_OCRE_CONTAINER_MESSAGING
ocre_messaging_init();
#endif
#ifdef CONFIG_OCRE_SHARED_HEAP
SharedHeapInitArgs heap_init_args;
memset(&heap_init_args, 0, sizeof(heap_init_args));

#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_PHYSICAL
// Physical mode - map hardware register address
heap_init_args.pre_allocated_addr = (void *)CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something very dangerous to do as this memory is potentially used by the OS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to give the user the possibility to manipulate hardware (like GPIOs) from the container - it can be enabled or disabled from the config. But yes, user shall take care and understand, that if they use it it's dangerous.

LOG_INF("Creating physical memory mapping at 0x%08X (hardware registers)",
CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS);
#elif CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
// Virtual mode - allocate from RAM
heap_init_args.pre_allocated_addr = preallocated_buf;
LOG_INF("Creating virtual shared heap in RAM, size=%d bytes",
CONFIG_OCRE_SHARED_HEAP_BUF_SIZE);
#endif
heap_init_args.size = CONFIG_OCRE_SHARED_HEAP_BUF_SIZE;
_shared_heap = wasm_runtime_create_shared_heap(&heap_init_args);

if (!_shared_heap) {
LOG_ERR("Create preallocated shared heap failed");
return RUNTIME_STATUS_ERROR;
}
#endif

storage_heap_init();
return RUNTIME_STATUS_INITIALIZED;
}
Expand Down Expand Up @@ -352,6 +384,15 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
"0.0.0.0/0",
};
wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE);
/**
* Configure which domain names a WebAssembly module is allowed to resolve via DNS lookups
* ns_lookup_pool: An array of domain name patterns (e.g., "example.com", "*.example.com", or "*" for any domain)
*/

const char *ns_lookup_pool[] = {
"*"
};
wasm_runtime_set_wasi_ns_lookup_pool(curr_container_arguments->module, ns_lookup_pool, sizeof(ns_lookup_pool) / sizeof(ns_lookup_pool[0]));
#endif

#ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM
Expand Down Expand Up @@ -387,8 +428,35 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
defined(CONFIG_OCRE_CONTAINER_MESSAGING)
ocre_register_module(curr_container_arguments->module_inst);
#endif
}
#ifdef CONFIG_OCRE_SHARED_HEAP
LOG_INF("Attaching shared heap to container %d", curr_container_ID);
/* attach module instance to the shared heap */
if (!wasm_runtime_attach_shared_heap(curr_container_arguments->module_inst, _shared_heap)) {
LOG_ERR("Attach shared heap failed.");
return CONTAINER_STATUS_ERROR;
}

#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_PHYSICAL
// For physical mode, get the base address from the shared heap itself
// The WASM address space already knows about the physical mapping
uint32 shared_heap_base_addr = wasm_runtime_addr_native_to_app(
curr_container_arguments->module_inst,
(void*)CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS);
LOG_INF("Physical shared heap base address in app: 0x%x", shared_heap_base_addr);
#elif CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
// For virtual mode, convert the allocated buffer address
uint32 shared_heap_base_addr = wasm_runtime_addr_native_to_app(
curr_container_arguments->module_inst,
preallocated_buf);
LOG_INF("Virtual shared heap base address in app: 0x%x", shared_heap_base_addr);
#endif

if (shared_heap_base_addr == 0) {
LOG_ERR("Failed to get shared heap WASM address!");
return CONTAINER_STATUS_ERROR;
}
#endif
}
core_mutex_lock(&container_mutex);
int thread_idx = get_available_thread();
if (thread_idx == -1) {
Expand Down
1 change: 1 addition & 0 deletions src/shared/platform/posix/ocre_internal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set (WAMR_BUILD_LIBC_WASI 1)
set (WAMR_BUILD_LIB_PTHREAD 1)
set (WAMR_BUILD_REF_TYPES 1)
set (WASM_ENABLE_LOG 1)
set (WAMR_BUILD_SHARED_HEAP 1)

if (NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
set (WAMR_BUILD_GLOBAL_HEAP_POOL 1)
Expand Down
1 change: 1 addition & 0 deletions src/shared/platform/zephyr/ocre_internal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(WAMR_BUILD_LIBC_WASI 1)
set(WAMR_BUILD_LIB_PTHREAD 1)
set(WAMR_BUILD_REF_TYPES 1)
set(WASM_ENABLE_LOG 1)
set (WAMR_BUILD_SHARED_HEAP 1)

if(NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
set(WAMR_BUILD_GLOBAL_HEAP_POOL 1)
Expand Down
2 changes: 1 addition & 1 deletion wasm-micro-runtime
Submodule wasm-micro-runtime updated 97 files
+1 −1 .github/workflows/build_wamr_vscode_ext.yml
+4 −4 .github/workflows/codeql.yml
+5 −4 .github/workflows/compilation_on_android_ubuntu.yml
+1 −1 .github/workflows/spec_test_on_nuttx.yml
+2 −2 .github/workflows/supply_chain.yml
+1 −1 .github/workflows/wamr_wasi_extensions.yml
+16 −0 RELEASE_NOTES.md
+90 −0 SUMMARY.md
+37 −0 build-scripts/code_coverage.cmake
+1 −3 build-scripts/config_common.cmake
+1 −1 build-scripts/version.cmake
+7 −3 core/iwasm/aot/aot_runtime.c
+3 −6 core/iwasm/aot/aot_runtime.h
+4 −4 core/iwasm/common/wasm_memory.c
+1 −1 core/iwasm/common/wasm_memory.h
+25 −25 core/iwasm/common/wasm_runtime_common.c
+7 −5 core/iwasm/common/wasm_runtime_common.h
+5 −1 core/iwasm/common/wasm_shared_memory.c
+6 −2 core/iwasm/include/wasm_export.h
+1 −1 core/iwasm/interpreter/wasm_interp_classic.c
+1 −1 core/iwasm/interpreter/wasm_interp_fast.c
+11 −1 core/iwasm/interpreter/wasm_loader.c
+11 −1 core/iwasm/interpreter/wasm_mini_loader.c
+6 −3 core/iwasm/interpreter/wasm_runtime.c
+2 −2 core/iwasm/interpreter/wasm_runtime.h
+4 −1 core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c
+4 −1 core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
+4 −1 core/iwasm/libraries/thread-mgr/thread_manager.c
+3 −3 core/shared/platform/common/math/math.c
+29 −1 core/shared/platform/zephyr/zephyr_file.c
+100 −44 core/shared/platform/zephyr/zephyr_socket.c
+1 −1 core/version.h
+7 −0 gitbook/advance-tutorial/README.md
+18 −0 gitbook/advance-tutorial/performance-benchmark/README.md
+7 −0 gitbook/advance-tutorial/remote-applicatoin-management/README.md
+61 −0 gitbook/appendix/background_knowledge.md
+6 −0 gitbook/appendix/webassembly_details.md
+13 −0 gitbook/basics/getting-started/README.md
+40 −0 gitbook/basics/getting-started/host_prerequsites.md
+26 −0 gitbook/basics/getting-started/on_docker.md
+26 −0 gitbook/basics/getting-started/on_host.md
+5 −0 gitbook/basics/introduction/README.md
+147 −0 gitbook/basics/introduction/security_feature.md
+65 −0 gitbook/basics/introduction/wamr_project.md
+52 −0 gitbook/basics/introduction/webassembly.md
+11 −0 gitbook/examples/README.md
+39 −0 gitbook/features/README.md
+15 −0 gitbook/features/demo-examples/README.md
+10 −0 gitbook/features/user-case/README.md
+15 −0 gitbook/home_page.md
+3 −0 gitbook/programmer's-manual/C_API_Lists.md
+6 −0 gitbook/programmer's-manual/README.md
+9 −0 gitbook/tutorial/README.md
+36 −0 gitbook/tutorial/build-tutorial/README.md
+23 −0 gitbook/tutorial/build-tutorial/build_wamrc.md
+3 −0 gitbook/tutorial/debugging&IDE-support/README.md
+9 −0 gitbook/tutorial/language-embedding/README.md
+29 −0 gitbook/tutorial/running-modes/README.md
+13 −2 product-mini/platforms/rt-thread/iwasm.c
+3 −1 samples/basic/README.md
+3 −0 samples/file/README.md
+3 −0 samples/multi-module/README.md
+3 −0 samples/multi-thread/README.md
+3 −0 samples/native-lib/README.md
+3 −0 samples/ref-types/README.md
+3 −0 samples/sgx-ra/README.md
+3 −0 samples/socket-api/README.md
+3 −0 samples/spawn-thread/README.md
+3 −0 samples/wasm-c-api/README.md
+3 −0 samples/workload/README.md
+5 −8 tests/unit/CMakeLists.txt
+195 −0 tests/unit/README.md
+10 −18 tests/unit/aot-stack-frame/CMakeLists.txt
+7 −17 tests/unit/aot/CMakeLists.txt
+10 −23 tests/unit/compilation/CMakeLists.txt
+22 −24 tests/unit/custom-section/CMakeLists.txt
+14 −7 tests/unit/custom-section/wasm-apps/CMakeLists.txt
+4 −16 tests/unit/gc/CMakeLists.txt
+8 −11 tests/unit/interpreter/CMakeLists.txt
+10 −5 tests/unit/libc-builtin/CMakeLists.txt
+5 −15 tests/unit/linear-memory-aot/CMakeLists.txt
+0 −12 tests/unit/linear-memory-wasm/CMakeLists.txt
+3 −9 tests/unit/linux-perf/CMakeLists.txt
+4 −18 tests/unit/memory64/CMakeLists.txt
+23 −24 tests/unit/running-modes/CMakeLists.txt
+24 −40 tests/unit/running-modes/wasm-apps/CMakeLists.txt
+12 −21 tests/unit/runtime-common/CMakeLists.txt
+0 −12 tests/unit/runtime-common/wasm_runtime_common_test.cc
+5 −21 tests/unit/shared-heap/CMakeLists.txt
+56 −0 tests/unit/shared-heap/shared_heap_test.cc
+6 −2 tests/unit/shared-utils/CMakeLists.txt
+15 −76 tests/unit/unit_common.cmake
+3 −50 tests/unit/wasm-c-api/CMakeLists.txt
+14 −18 tests/unit/wasm-vm/CMakeLists.txt
+4 −2 tests/wamr-test-suites/spec-test-script/collect_coverage.sh
+27 −0 tests/wamr-test-suites/spec-test-script/gc_array_fill_cases.patch
+21 −11 tests/wamr-test-suites/test_wamr.sh