This commit is contained in:
Reuben Dunnington 2023-08-03 08:00:13 -07:00
parent 75f719441f
commit 03377bb014
Signed by: rdunnington
GPG Key ID: 4EC5290E704FD482
3 changed files with 85 additions and 42 deletions

View File

@ -19,6 +19,17 @@ enum bb_error
BB_ERROR_UNKNOWNEXPORT,
BB_ERROR_UNKNOWNIMPORT,
BB_ERROR_INCOMPATIBLEIMPORT,
BB_ERROR_TRAP_DEBUG,
BB_ERROR_TRAP_UNREACHABLE,
BB_ERROR_TRAP_INTEGERDIVISIONBYZERO,
BB_ERROR_TRAP_INTEGEROVERFLOW,
BB_ERROR_TRAP_INDIRECTCALLTYPEMISMATCH,
BB_ERROR_TRAP_INVALIDINTEGERCONVERSION,
BB_ERROR_TRAP_OUTOFBOUNDSMEMORYACCESS,
BB_ERROR_TRAP_UNDEFINEDELEMENT,
BB_ERROR_TRAP_UNINITIALIZEDELEMENT,
BB_ERROR_TRAP_OUTOFBOUNDSTABLEACCESS,
BB_ERROR_TRAP_STACKEXHAUSTED,
};
typedef enum bb_error bb_error;
@ -53,10 +64,19 @@ typedef struct bb_module_definition bb_module_definition;
typedef struct bb_module_instance bb_module_instance;
typedef struct bb_import_package bb_import_package;
typedef void bb_host_function(void* userdata, bb_module_instance* module, const bb_val* params, bb_val* returns);
typedef void* bb_wasm_memory_resize(void* mem, size_t new_size_bytes, size_t old_size_bytes, void* userdata);
typedef void bb_wasm_memory_free(void* mem, size_t size_bytes, void* userdata);
struct bb_module_instance_instantiate_opts
{
bb_import_package** packages;
size_t num_packages;
struct {
bb_wasm_memory_resize* resize_callback;
bb_wasm_memory_free* free_callback;
void* userdata;
} wasm_memory_config;
bool enable_debug;
};
typedef struct bb_module_instance_instantiate_opts bb_module_instance_instantiate_opts;
@ -83,15 +103,6 @@ struct bb_func_info
};
typedef struct bb_func_info bb_func_info;
enum bb_debug_trap_mode
{
BB_DEBUG_TRAP_MODE_DISABLED,
BB_DEBUG_TRAP_MODE_ENABLED,
};
typedef enum bb_debug_trap_mode bb_debug_trap_mode;
typedef void bb_host_function(void* userdata, bb_module_instance* module, const bb_val* params, bb_val* returns);
enum bb_global_mut
{
BB_GLOBAL_MUT_IMMUTABLE,
@ -107,6 +118,21 @@ struct bb_global
};
typedef struct bb_global bb_global;
enum bb_debug_trace_mode
{
BB_DEBUG_TRACE_NONE,
BB_DEBUG_TRACE_FUNCTION,
BB_DEBUG_TRACE_INSTRUCTION,
};
typedef enum bb_debug_trace_mode bb_debug_trace_mode;
enum bb_debug_trap_mode
{
BB_DEBUG_TRAP_MODE_DISABLED,
BB_DEBUG_TRAP_MODE_ENABLED,
};
typedef enum bb_debug_trap_mode bb_debug_trap_mode;
// typedef void* bb_malloc_func(size_t size, void* userdata);
// typedef void* bb_realloc_func(void* mem, size_t size, void* userdata);
// typedef void bb_free_func(void* mem, void* userdata);
@ -124,6 +150,8 @@ bb_import_package* bb_import_package_init(const char* name);
void bb_import_package_deinit(bb_import_package* package); // only deinit when all module_instances using the package have been deinited
bb_error bb_import_package_add_function(bb_import_package* package, bb_host_function* func, const char* export_name, bb_valtype* params, size_t num_params, bb_valtype* returns, size_t num_returns, void* userdata);
void bb_set_debug_trace_mode(bb_debug_trace_mode mode);
bb_module_instance* bb_module_instance_init(bb_module_definition* definition);
void bb_module_instance_deinit(bb_module_instance* instance);
bb_error bb_module_instance_instantiate(bb_module_instance* instance, bb_module_instance_instantiate_opts opts);
@ -135,6 +163,7 @@ bb_error bb_module_instance_step(bb_module_instance* instance, bb_val* returns,
bb_error bb_module_instance_debug_set_trap(bb_module_instance* instance, uint32_t address, bb_debug_trap_mode trap_mode);
void* bb_module_instance_mem(bb_module_instance* instance, size_t offset, size_t length);
bb_slice bb_module_instance_mem_all(bb_module_instance* instance);
bb_error bb_module_instance_mem_grow(bb_module_instance* instance, size_t num_pages);
bb_global bb_module_instance_find_global(bb_module_instance* instance, const char* global_name);
bool bb_func_handle_isvalid(bb_func_handle handle);

View File

@ -423,8 +423,19 @@ i32 orca_runloop(void* user)
// return(-1);
// }
// bb_set_debug_trace_mode(BB_DEBUG_TRACE_FUNCTION);
app->runtime.bbModuleInst = bb_module_instance_init(app->runtime.bbModuleDef);
bb_module_instance_instantiate_opts module_inst_instantiate_opts = { .packages = &module_imports, .num_packages = 1, .enable_debug = false, };
bb_module_instance_instantiate_opts module_inst_instantiate_opts = {
.packages = &module_imports,
.num_packages = 1,
.enable_debug = false,
.wasm_memory_config = {
.resize_callback = wasm_memory_resize_callback,
.free_callback = wasm_memory_free_callback,
.userdata = &app->runtime.wasmMemory,
},
};
wasm_init_err = bb_module_instance_instantiate(app->runtime.bbModuleInst, module_inst_instantiate_opts);
if (wasm_init_err != BB_ERROR_OK)

View File

@ -8,63 +8,66 @@
#include"orca_app.h"
void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData)
void* wasm_memory_resize_callback(void* p, size_t new_size, size_t old_size, void* userData)
{
// wasm_memory* memory = (wasm_memory*)userData;
wasm_memory* memory = (wasm_memory*)userData;
// if(memory->committed >= size)
// {
// return(memory->ptr);
// }
// else if(memory->committed < memory->reserved)
// {
// u32 commitSize = size - memory->committed;
if(memory->committed >= new_size)
{
return(memory->ptr);
}
else if(memory->committed < memory->reserved)
{
u32 commitSize = new_size - memory->committed;
// mem_base_allocator* allocator = mem_base_allocator_default();
// mem_base_commit(allocator, memory->ptr + memory->committed, commitSize);
// memory->committed += commitSize;
// return(memory->ptr);
// }
// else
// {
// DEBUG_ASSERT(0, "Out of memory");
// return(0);
// }
mem_base_allocator* allocator = mem_base_allocator_default();
mem_base_commit(allocator, memory->ptr + memory->committed, commitSize);
memory->committed += commitSize;
return(memory->ptr);
}
else
{
DEBUG_ASSERT(0, "Out of memory");
return(0);
}
return NULL;
}
void wasm_memory_free_callback(void* p, void* userData)
void wasm_memory_free_callback(void* p, size_t size, void* userData)
{
// wasm_memory* memory = (wasm_memory*)userData;
wasm_memory* memory = (wasm_memory*)userData;
// mem_base_allocator* allocator = mem_base_allocator_default();
// mem_base_release(allocator, memory->ptr, memory->reserved);
// memset(memory, 0, sizeof(wasm_memory));
mem_base_allocator* allocator = mem_base_allocator_default();
mem_base_release(allocator, memory->ptr, memory->reserved);
memset(memory, 0, sizeof(wasm_memory));
}
extern u32 orca_mem_grow(u64 size)
{
// const size_t PAGE_SIZE = 65536;
const size_t PAGE_SIZE = 65536;
// orca_runtime* runtime = orca_runtime_get();
// wasm_memory* memory = &runtime->wasmMemory;
orca_runtime* runtime = orca_runtime_get();
wasm_memory* memory = &runtime->wasmMemory;
// size = AlignUpOnPow2(size, PAGE_SIZE);
size = AlignUpOnPow2(size, PAGE_SIZE);
// u64 totalSize = size + m3_GetMemorySize(runtime->m3Runtime);
// bb_slice memory = bb_module_instance_mem_all(runtime->bbModuleInst);
// u32 addr = memory->committed;
u32 addr = memory->committed;
// //NOTE: call resize memory, which will call our custom resize callback... this is a bit involved because
// // wasm3 doesn't allow resizing the memory directly
//NOTE: call resize memory, which will call our custom resize callback... this is a bit involved because
// wasm3 doesn't allow resizing the memory directly
// M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/PAGE_SIZE);
bb_module_instance_mem_grow(runtime->bbModuleInst, size/PAGE_SIZE);
return(addr);
}
void* wasm_memory_offset_to_ptr(wasm_memory* memory, u32 offset)
{
DEBUG_ASSERT(offset < bb_module_instance_mem_all(orca_runtime_get()->bbModuleInst).length, "Wasm offset exceeds memory length")
return memory->ptr + offset;
// M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr);
// DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length")
// return memory->ptr + sizeof(M3MemoryHeader) + offset;
return NULL;
}