-
Notifications
You must be signed in to change notification settings - Fork 19
Add shared memory and DNS resolution support #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
16bdb09
d5d3d0b
d9c267b
9d3b776
7503618
196c30a
5faa476
6ecf642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]; | ||
| #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; | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
@@ -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 | ||
|
|
@@ -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) { | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.