Skip to content

Commit f3a37db

Browse files
kr-tPatrickRobbIOL
authored andcommitted
Add shared memory and DNS resolution support (#99)
Update wamr's hash to support networking DNS changes. --------- Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com>
1 parent f531e85 commit f3a37db

5 files changed

Lines changed: 121 additions & 3 deletions

File tree

Kconfig

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,53 @@ config OCRE_NETWORKING
224224
default n
225225
help
226226
Enable networking support for containers.
227-
227+
228+
config OCRE_SHARED_HEAP
229+
bool "Enable container shared heap support"
230+
default n
231+
help
232+
Enable shared heap support for containers.
233+
234+
config OCRE_SHARED_HEAP_BUF_SIZE
235+
int "Shared heap buffer size in bytes"
236+
default 65536
237+
depends on OCRE_SHARED_HEAP
238+
help
239+
Size of the pre-allocated buffer for the shared heap.
240+
This memory is shared between WebAssembly modules.
241+
242+
choice OCRE_SHARED_HEAP_MODE
243+
prompt "Shared heap mode"
244+
depends on OCRE_SHARED_HEAP
245+
default OCRE_SHARED_HEAP_BUF_VIRTUAL
246+
help
247+
Select the shared heap memory mode:
248+
- Physical: Map physical hardware registers (e.g., GPIO) to WASM address space
249+
- Virtual: Allocate shared heap from regular RAM
250+
251+
config OCRE_SHARED_HEAP_BUF_PHYSICAL
252+
bool "Physical (hardware register mapping)"
253+
help
254+
Enable physical memory mapping for hardware access.
255+
Maps physical hardware registers (like GPIO at 0x42020000) to WASM address space.
256+
Use this when containers need direct access to hardware peripherals.
257+
258+
config OCRE_SHARED_HEAP_BUF_VIRTUAL
259+
bool "Virtual (RAM allocation)"
260+
help
261+
Enable virtual shared heap allocated from regular RAM.
262+
Use this for normal inter-module communication without
263+
direct hardware access.
264+
265+
endchoice
266+
267+
config OCRE_SHARED_HEAP_BUF_ADDRESS
268+
hex "Shared heap buffer address"
269+
default 0x00
270+
depends on OCRE_SHARED_HEAP
271+
help
272+
Shared heap buffer address.
273+
- For physical mode: Physical address of hardware registers
274+
- For virtual mode: Leave as 0x00 to auto-allocate from RAM
275+
228276
endmenu

src/ocre/components/container_supervisor/cs_sm_impl.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ static size_t ocre_get_available_memory(void) {
7373
}
7474
#endif
7575

76+
#ifdef CONFIG_OCRE_SHARED_HEAP
77+
// Shared heap for memory-mapped access
78+
wasm_shared_heap_t _shared_heap = NULL;
79+
#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
80+
uint8 preallocated_buf[CONFIG_OCRE_SHARED_HEAP_BUF_SIZE];
81+
#endif
82+
#endif
83+
7684
static bool validate_container_memory(ocre_container_t *container) {
7785
#ifdef CONFIG_OCRE_MEMORY_CHECK_ENABLED
7886
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
266274
#ifdef CONFIG_OCRE_CONTAINER_MESSAGING
267275
ocre_messaging_init();
268276
#endif
277+
#ifdef CONFIG_OCRE_SHARED_HEAP
278+
SharedHeapInitArgs heap_init_args;
279+
memset(&heap_init_args, 0, sizeof(heap_init_args));
280+
281+
#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_PHYSICAL
282+
// Physical mode - map hardware register address
283+
heap_init_args.pre_allocated_addr = (void *)CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS;
284+
LOG_INF("Creating physical memory mapping at 0x%08X (hardware registers)",
285+
CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS);
286+
#elif CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
287+
// Virtual mode - allocate from RAM
288+
heap_init_args.pre_allocated_addr = preallocated_buf;
289+
LOG_INF("Creating virtual shared heap in RAM, size=%d bytes",
290+
CONFIG_OCRE_SHARED_HEAP_BUF_SIZE);
291+
#endif
292+
heap_init_args.size = CONFIG_OCRE_SHARED_HEAP_BUF_SIZE;
293+
_shared_heap = wasm_runtime_create_shared_heap(&heap_init_args);
294+
295+
if (!_shared_heap) {
296+
LOG_ERR("Create preallocated shared heap failed");
297+
return RUNTIME_STATUS_ERROR;
298+
}
299+
#endif
300+
269301
storage_heap_init();
270302
return RUNTIME_STATUS_INITIALIZED;
271303
}
@@ -352,6 +384,15 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
352384
"0.0.0.0/0",
353385
};
354386
wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE);
387+
/**
388+
* Configure which domain names a WebAssembly module is allowed to resolve via DNS lookups
389+
* ns_lookup_pool: An array of domain name patterns (e.g., "example.com", "*.example.com", or "*" for any domain)
390+
*/
391+
392+
const char *ns_lookup_pool[] = {
393+
"*"
394+
};
395+
wasm_runtime_set_wasi_ns_lookup_pool(curr_container_arguments->module, ns_lookup_pool, sizeof(ns_lookup_pool) / sizeof(ns_lookup_pool[0]));
355396
#endif
356397

357398
#ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM
@@ -387,8 +428,35 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
387428
defined(CONFIG_OCRE_CONTAINER_MESSAGING)
388429
ocre_register_module(curr_container_arguments->module_inst);
389430
#endif
390-
}
431+
#ifdef CONFIG_OCRE_SHARED_HEAP
432+
LOG_INF("Attaching shared heap to container %d", curr_container_ID);
433+
/* attach module instance to the shared heap */
434+
if (!wasm_runtime_attach_shared_heap(curr_container_arguments->module_inst, _shared_heap)) {
435+
LOG_ERR("Attach shared heap failed.");
436+
return CONTAINER_STATUS_ERROR;
437+
}
391438

439+
#ifdef CONFIG_OCRE_SHARED_HEAP_BUF_PHYSICAL
440+
// For physical mode, get the base address from the shared heap itself
441+
// The WASM address space already knows about the physical mapping
442+
uint32 shared_heap_base_addr = wasm_runtime_addr_native_to_app(
443+
curr_container_arguments->module_inst,
444+
(void*)CONFIG_OCRE_SHARED_HEAP_BUF_ADDRESS);
445+
LOG_INF("Physical shared heap base address in app: 0x%x", shared_heap_base_addr);
446+
#elif CONFIG_OCRE_SHARED_HEAP_BUF_VIRTUAL
447+
// For virtual mode, convert the allocated buffer address
448+
uint32 shared_heap_base_addr = wasm_runtime_addr_native_to_app(
449+
curr_container_arguments->module_inst,
450+
preallocated_buf);
451+
LOG_INF("Virtual shared heap base address in app: 0x%x", shared_heap_base_addr);
452+
#endif
453+
454+
if (shared_heap_base_addr == 0) {
455+
LOG_ERR("Failed to get shared heap WASM address!");
456+
return CONTAINER_STATUS_ERROR;
457+
}
458+
#endif
459+
}
392460
core_mutex_lock(&container_mutex);
393461
int thread_idx = get_available_thread();
394462
if (thread_idx == -1) {

src/shared/platform/posix/ocre_internal.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set (WAMR_BUILD_LIBC_WASI 1)
4343
set (WAMR_BUILD_LIB_PTHREAD 1)
4444
set (WAMR_BUILD_REF_TYPES 1)
4545
set (WASM_ENABLE_LOG 1)
46+
set (WAMR_BUILD_SHARED_HEAP 1)
4647

4748
if (NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
4849
set (WAMR_BUILD_GLOBAL_HEAP_POOL 1)

src/shared/platform/zephyr/ocre_internal.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(WAMR_BUILD_LIBC_WASI 1)
4040
set(WAMR_BUILD_LIB_PTHREAD 1)
4141
set(WAMR_BUILD_REF_TYPES 1)
4242
set(WASM_ENABLE_LOG 1)
43+
set (WAMR_BUILD_SHARED_HEAP 1)
4344

4445
if(NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
4546
set(WAMR_BUILD_GLOBAL_HEAP_POOL 1)

wasm-micro-runtime

Submodule wasm-micro-runtime updated 97 files

0 commit comments

Comments
 (0)