From 099a0150e1ba3af96d52629a04fe5def489c4b34 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:15:06 -0700 Subject: [PATCH] [wasm] Add HB_STRUCT_TYPE --- src/hb-wasm-api-blob.hh | 5 ++--- src/hb-wasm-api-buffer.hh | 5 ++--- src/hb-wasm-api.hh | 9 +++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index 8a8f9e2a9..e9aff8968 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -35,11 +35,10 @@ void blob_free (HB_WASM_EXEC_ENV ptr_t(blob_t) blobptr) { - if (unlikely (!validate_app_addr (blobptr, sizeof (blob_t)))) + HB_STRUCT_TYPE (blob_t, blob); + if (unlikely (!blob)) return; - blob_t *blob = (blob_t *) addr_app_to_native (blobptr); - module_free (blob->data); } diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 139899a8f..949195167 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -37,11 +37,10 @@ void buffer_contents_free (HB_WASM_EXEC_ENV 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; - buffer_contents_t *contents = (buffer_contents_t *) addr_app_to_native (contentsptr); - module_free (contents->info); module_free (contents->pos); } diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index a005024c9..4fc95fd94 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -68,6 +68,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; (void *) hb_wasm_ref_type_##obj)) \ obj = hb_##obj##_get_empty (); \ } HB_STMT_END + #define HB_OBJ2REF(obj) \ uint32_t obj##ref = nullref; \ HB_STMT_START { \ @@ -90,5 +91,13 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; } \ 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 */