[wasm] Add HB_STRUCT_TYPE

This commit is contained in:
Behdad Esfahbod 2023-02-23 15:15:06 -07:00
parent 851ef1380a
commit 099a0150e1
3 changed files with 13 additions and 6 deletions

View File

@ -35,11 +35,10 @@ void
blob_free (HB_WASM_EXEC_ENV blob_free (HB_WASM_EXEC_ENV
ptr_t(blob_t) blobptr) ptr_t(blob_t) blobptr)
{ {
if (unlikely (!validate_app_addr (blobptr, sizeof (blob_t)))) HB_STRUCT_TYPE (blob_t, blob);
if (unlikely (!blob))
return; return;
blob_t *blob = (blob_t *) addr_app_to_native (blobptr);
module_free (blob->data); module_free (blob->data);
} }

View File

@ -37,11 +37,10 @@ void
buffer_contents_free (HB_WASM_EXEC_ENV buffer_contents_free (HB_WASM_EXEC_ENV
ptr_t(buffer_contents_t) contentsptr) ptr_t(buffer_contents_t) contentsptr)
{ {
if (unlikely (!validate_app_addr (contentsptr, sizeof (buffer_contents_t)))) HB_STRUCT_TYPE (buffer_contents_t, contents);
if (unlikely (!contents))
return; return;
buffer_contents_t *contents = (buffer_contents_t *) addr_app_to_native (contentsptr);
module_free (contents->info); module_free (contents->info);
module_free (contents->pos); module_free (contents->pos);
} }

View File

@ -68,6 +68,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key;
(void *) hb_wasm_ref_type_##obj)) \ (void *) hb_wasm_ref_type_##obj)) \
obj = hb_##obj##_get_empty (); \ obj = hb_##obj##_get_empty (); \
} HB_STMT_END } HB_STMT_END
#define HB_OBJ2REF(obj) \ #define HB_OBJ2REF(obj) \
uint32_t obj##ref = nullref; \ uint32_t obj##ref = nullref; \
HB_STMT_START { \ HB_STMT_START { \
@ -90,5 +91,13 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key;
} \ } \
type &name = *_name_ptr type &name = *_name_ptr
#define HB_STRUCT_TYPE(type, name) \
type *name = nullptr; \
HB_STMT_START { \
if (likely (wasm_runtime_validate_app_addr (module_inst, \
name##ptr, sizeof (type)))) \
name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \
} HB_STMT_END
#endif /* HB_WASM_API_HH */ #endif /* HB_WASM_API_HH */