diff --git a/runtime/compiletime/memory_instructions.c b/runtime/compiletime/memory_instructions.c index a865444..9e02f40 100644 --- a/runtime/compiletime/memory_instructions.c +++ b/runtime/compiletime/memory_instructions.c @@ -6,99 +6,84 @@ extern thread_local struct wasm_module_instance current_wasm_module_instance; -INLINE void -initialize_region(uint32_t offset, uint32_t region_size, uint8_t region[region_size]) -{ - wasm_memory_initialize_region(current_wasm_module_instance.memory, offset, region_size, region); -} - INLINE uint32_t instruction_memory_size() { return wasm_memory_get_page_count(current_wasm_module_instance.memory); } -/** - * @brief Stub that implements the WebAssembly memory.grow instruction - * - * @param count number of pages to grow the WebAssembly linear memory by - * @return The previous size of the linear memory in pages or -1 if enough memory cannot be allocated - */ -INLINE int32_t -instruction_memory_grow(uint32_t count) +// All of these are pretty generic +INLINE float +get_f32(uint32_t offset) { - int old_page_count = current_wasm_module_instance.memory->size / WASM_PAGE_SIZE; - - /* Return -1 if we've hit the linear memory max */ - int rc = wasm_memory_expand(current_wasm_module_instance.memory, WASM_PAGE_SIZE * count); - if (unlikely(rc == -1)) return -1; + return wasm_memory_get_float(current_wasm_module_instance.memory, offset); +} -#ifdef LOG_SANDBOX_MEMORY_PROFILE - // Cache the runtime of the first N page allocations - for (int i = 0; i < count; i++) { - if (likely(sandbox->timestamp_of.page_allocations_size < SANDBOX_PAGE_ALLOCATION_TIMESTAMP_COUNT)) { - sandbox->timestamp_of.page_allocations[sandbox->timestamp_of.page_allocations_size++] = - sandbox->duration_of_state.running - + (uint32_t)(__getcycles() - sandbox->timestamp_of.last_state_change); - } - } -#endif +INLINE double +get_f64(uint32_t offset) +{ + return wasm_memory_get_double(current_wasm_module_instance.memory, offset); +} - return rc; +INLINE int8_t +get_i8(uint32_t offset) +{ + return wasm_memory_get_int8(current_wasm_module_instance.memory, offset); } -INLINE float -get_f32(uint32_t offset) +INLINE int16_t +get_i16(uint32_t offset) { - return wasm_memory_get_float(current_wasm_module_instance.memory, offset); + return wasm_memory_get_int16(current_wasm_module_instance.memory, offset); } -INLINE void -set_f32(uint32_t offset, float v) +INLINE int32_t +get_i32(uint32_t offset) { - wasm_memory_set_float(current_wasm_module_instance.memory, offset, v); + return wasm_memory_get_int32(current_wasm_module_instance.memory, offset); } -INLINE double -get_f64(uint32_t offset) +INLINE int64_t +get_i64(uint32_t offset) { - return wasm_memory_get_double(current_wasm_module_instance.memory, offset); + return wasm_memory_get_int64(current_wasm_module_instance.memory, offset); } -INLINE void -set_f64(uint32_t offset, double v) +INLINE int32_t +get_global_i32(uint32_t offset) { - wasm_memory_set_double(current_wasm_module_instance.memory, offset, v); + return get_i32(offset); } -INLINE int8_t -get_i8(uint32_t offset) +INLINE int64_t +get_global_i64(uint32_t offset) { - return wasm_memory_get_int8(current_wasm_module_instance.memory, offset); + return get_i64(offset); } +// Now setting routines INLINE void -set_i8(uint32_t offset, int8_t v) +set_f32(uint32_t offset, float v) { - wasm_memory_set_int8(current_wasm_module_instance.memory, offset, v); + wasm_memory_set_float(current_wasm_module_instance.memory, offset, v); } -INLINE int16_t -get_i16(uint32_t offset) +INLINE void +set_f64(uint32_t offset, double v) { - return wasm_memory_get_int16(current_wasm_module_instance.memory, offset); + wasm_memory_set_double(current_wasm_module_instance.memory, offset, v); } INLINE void -set_i16(uint32_t offset, int16_t v) +set_i8(uint32_t offset, int8_t v) { - wasm_memory_set_int16(current_wasm_module_instance.memory, offset, v); + wasm_memory_set_int8(current_wasm_module_instance.memory, offset, v); } -INLINE int32_t -get_i32(uint32_t offset) +INLINE void +set_i16(uint32_t offset, int16_t v) { - return wasm_memory_get_int32(current_wasm_module_instance.memory, offset); + wasm_memory_set_int16(current_wasm_module_instance.memory, offset, v); } INLINE void @@ -107,38 +92,56 @@ set_i32(uint32_t offset, int32_t v) wasm_memory_set_int32(current_wasm_module_instance.memory, offset, v); } -INLINE int64_t -get_i64(uint32_t offset) -{ - return wasm_memory_get_int64(current_wasm_module_instance.memory, offset); -} - INLINE void set_i64(uint32_t offset, int64_t v) { wasm_memory_set_int64(current_wasm_module_instance.memory, offset, v); } -INLINE int32_t -get_global_i32(uint32_t offset) +INLINE void +set_global_i32(uint32_t offset, int32_t v) { - return get_i32(offset); + set_i32(offset, v); } INLINE void -set_global_i32(uint32_t offset, int32_t v) +set_global_i64(uint32_t offset, int64_t v) { - set_i32(offset, v); + set_i64(offset, v); } -INLINE int64_t -get_global_i64(uint32_t offset) +/** + * @brief Stub that implements the WebAssembly memory.grow instruction + * + * @param count number of pages to grow the WebAssembly linear memory by + * @return The previous size of the linear memory in pages or -1 if enough memory cannot be allocated + */ +INLINE int32_t +instruction_memory_grow(uint32_t count) { - return get_i64(offset); + int old_page_count = current_wasm_module_instance.memory->size / WASM_PAGE_SIZE; + + /* Return -1 if we've hit the linear memory max */ + int rc = wasm_memory_expand(current_wasm_module_instance.memory, WASM_PAGE_SIZE * count); + if (unlikely(rc == -1)) return -1; + +#ifdef LOG_SANDBOX_MEMORY_PROFILE + // Cache the runtime of the first N page allocations + for (int i = 0; i < count; i++) { + if (likely(sandbox->timestamp_of.page_allocations_size < SANDBOX_PAGE_ALLOCATION_TIMESTAMP_COUNT)) { + sandbox->timestamp_of.page_allocations[sandbox->timestamp_of.page_allocations_size++] = + sandbox->duration_of_state.running + + (uint32_t)(__getcycles() - sandbox->timestamp_of.last_state_change); + } + } +#endif + + return rc; } + INLINE void -set_global_i64(uint32_t offset, int64_t v) +initialize_region(uint32_t offset, uint32_t region_size, uint8_t region[region_size]) { - set_i64(offset, v); + wasm_memory_initialize_region(current_wasm_module_instance.memory, offset, region_size, region); }