refactor: reorder memory_instructions

pull/287/head
Sean McBride 4 years ago
parent 22d3890e06
commit 13b123866f

@ -6,99 +6,84 @@
extern thread_local struct wasm_module_instance current_wasm_module_instance; 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 INLINE uint32_t
instruction_memory_size() instruction_memory_size()
{ {
return wasm_memory_get_page_count(current_wasm_module_instance.memory); return wasm_memory_get_page_count(current_wasm_module_instance.memory);
} }
/** // All of these are pretty generic
* @brief Stub that implements the WebAssembly memory.grow instruction INLINE float
* get_f32(uint32_t offset)
* @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)
{ {
int old_page_count = current_wasm_module_instance.memory->size / WASM_PAGE_SIZE; return wasm_memory_get_float(current_wasm_module_instance.memory, offset);
}
/* 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 INLINE double
// Cache the runtime of the first N page allocations get_f64(uint32_t offset)
for (int i = 0; i < count; i++) { {
if (likely(sandbox->timestamp_of.page_allocations_size < SANDBOX_PAGE_ALLOCATION_TIMESTAMP_COUNT)) { return wasm_memory_get_double(current_wasm_module_instance.memory, offset);
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 int8_t
get_i8(uint32_t offset)
{
return wasm_memory_get_int8(current_wasm_module_instance.memory, offset);
} }
INLINE float INLINE int16_t
get_f32(uint32_t offset) 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 INLINE int32_t
set_f32(uint32_t offset, float v) 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 INLINE int64_t
get_f64(uint32_t offset) 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 INLINE int32_t
set_f64(uint32_t offset, double v) get_global_i32(uint32_t offset)
{ {
wasm_memory_set_double(current_wasm_module_instance.memory, offset, v); return get_i32(offset);
} }
INLINE int8_t INLINE int64_t
get_i8(uint32_t offset) 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 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 INLINE void
get_i16(uint32_t offset) 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 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 INLINE void
get_i32(uint32_t offset) 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 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); 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 INLINE void
set_i64(uint32_t offset, int64_t v) set_i64(uint32_t offset, int64_t v)
{ {
wasm_memory_set_int64(current_wasm_module_instance.memory, offset, v); wasm_memory_set_int64(current_wasm_module_instance.memory, offset, v);
} }
INLINE int32_t INLINE void
get_global_i32(uint32_t offset) set_global_i32(uint32_t offset, int32_t v)
{ {
return get_i32(offset); set_i32(offset, v);
} }
INLINE void 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 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);
} }

Loading…
Cancel
Save