@@ -59,13 +59,24 @@ static union {
5959
6060#define is_little_endian () (__ue.b == 1)
6161
62- #define CHECK_BUF (buf , buf_end , length ) do { \
63- if (buf + length > buf_end) { \
64- set_error_buf(error_buf, error_buf_size, \
65- "Read data failed: unexpected end."); \
66- goto fail; \
67- } \
68- } while (0)
62+ static bool
63+ check_buf (const uint8 * buf , const uint8 * buf_end , uint32 length ,
64+ char * error_buf , uint32 error_buf_size )
65+ {
66+ if (buf + length > buf_end ) {
67+ set_error_buf (error_buf , error_buf_size ,
68+ "AOT module load failed: unexpect end." );
69+ return false;
70+ }
71+ return true;
72+ }
73+
74+ #define CHECK_BUF (buf , buf_end , length ) do { \
75+ if (!check_buf(buf, buf_end, length, \
76+ error_buf, error_buf_size)) { \
77+ goto fail; \
78+ } \
79+ } while (0)
6980
7081static uint8 *
7182align_ptr (const uint8 * p , uint32 b )
@@ -150,17 +161,32 @@ GET_U64_FROM_ADDR(uint32 *addr)
150161/* Legal values for e_version */
151162#define E_VERSION_CURRENT 1 /* Current version */
152163
164+ static void *
165+ loader_malloc (uint64 size , char * error_buf , uint32 error_buf_size )
166+ {
167+ void * mem ;
168+
169+ if (size >= UINT32_MAX
170+ || !(mem = wasm_runtime_malloc ((uint32 )size ))) {
171+ set_error_buf (error_buf , error_buf_size ,
172+ "AOT module load failed: "
173+ "allocate memory failed." );
174+ return NULL ;
175+ }
176+
177+ memset (mem , 0 , (uint32 )size );
178+ return mem ;
179+ }
180+
153181static char *
154182const_str_set_insert (const uint8 * str , int32 len , AOTModule * module ,
155183 char * error_buf , uint32 error_buf_size )
156184{
157185 HashMap * set = module -> const_str_set ;
158- char * c_str = wasm_runtime_malloc (( uint32 ) len + 1 ) , * value ;
186+ char * c_str , * value ;
159187
160- if (!c_str ) {
161- set_error_buf (error_buf , error_buf_size ,
162- "AOT module load failed: "
163- "allocate memory failed." );
188+ if (!(c_str = loader_malloc ((uint32 )len + 1 ,
189+ error_buf , error_buf_size ))) {
164190 return NULL ;
165191 }
166192
@@ -348,17 +374,11 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
348374
349375 /* Allocate memory */
350376 size = sizeof (AOTMemInitData * ) * (uint64 )module -> mem_init_data_count ;
351- if (size >= UINT32_MAX
352- || !(module -> mem_init_data_list =
353- data_list = wasm_runtime_malloc ((uint32 )size ))) {
354- set_error_buf (error_buf , error_buf_size ,
355- "AOT module load failed: "
356- "allocate memory failed." );
377+ if (!(module -> mem_init_data_list = data_list =
378+ loader_malloc (size , error_buf , error_buf_size ))) {
357379 return false;
358380 }
359381
360- memset (data_list , 0 , size );
361-
362382 /* Create each memory data segment */
363383 for (i = 0 ; i < module -> mem_init_data_count ; i ++ ) {
364384 uint32 init_expr_type , byte_count ;
@@ -372,11 +392,8 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
372392 read_uint64 (buf , buf_end , init_expr_value );
373393 read_uint32 (buf , buf_end , byte_count );
374394 size = offsetof(AOTMemInitData , bytes ) + (uint64 )byte_count ;
375- if (size >= UINT32_MAX
376- || !(data_list [i ] = wasm_runtime_malloc ((uint32 )size ))) {
377- set_error_buf (error_buf , error_buf_size ,
378- "AOT module load failed: "
379- "allocate memory failed." );
395+ if (!(data_list [i ] = loader_malloc
396+ (size , error_buf , error_buf_size ))) {
380397 return false;
381398 }
382399
@@ -447,17 +464,11 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
447464
448465 /* Allocate memory */
449466 size = sizeof (AOTTableInitData * ) * (uint64 )module -> table_init_data_count ;
450- if (size >= UINT32_MAX
451- || !(module -> table_init_data_list =
452- data_list = wasm_runtime_malloc ((uint32 )size ))) {
453- set_error_buf (error_buf , error_buf_size ,
454- "AOT module load failed: "
455- "allocate memory failed." );
467+ if (!(module -> table_init_data_list = data_list =
468+ loader_malloc (size , error_buf , error_buf_size ))) {
456469 return false;
457470 }
458471
459- memset (data_list , 0 , size );
460-
461472 /* Create each table data segment */
462473 for (i = 0 ; i < module -> table_init_data_count ; i ++ ) {
463474 uint32 init_expr_type , func_index_count ;
@@ -469,11 +480,8 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
469480
470481 size1 = sizeof (uint32 ) * (uint64 )func_index_count ;
471482 size = offsetof(AOTTableInitData , func_indexes ) + size1 ;
472- if (size >= UINT32_MAX
473- || !(data_list [i ] = wasm_runtime_malloc ((uint32 )size ))) {
474- set_error_buf (error_buf , error_buf_size ,
475- "AOT module load failed: "
476- "allocate memory failed." );
483+ if (!(data_list [i ] = loader_malloc
484+ (size , error_buf , error_buf_size ))) {
477485 return false;
478486 }
479487
@@ -535,16 +543,11 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end,
535543
536544 /* Allocate memory */
537545 size = sizeof (AOTFuncType * ) * (uint64 )module -> func_type_count ;
538- if (size >= UINT32_MAX
539- || !(module -> func_types = func_types = wasm_runtime_malloc ((uint32 )size ))) {
540- set_error_buf (error_buf , error_buf_size ,
541- "AOT module load failed: "
542- "allocate memory failed." );
546+ if (!(module -> func_types = func_types = loader_malloc
547+ (size , error_buf , error_buf_size ))) {
543548 return false;
544549 }
545550
546- memset (func_types , 0 , size );
547-
548551 /* Create each function type */
549552 for (i = 0 ; i < module -> func_type_count ; i ++ ) {
550553 uint32 param_count , result_count ;
@@ -555,11 +558,8 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end,
555558
556559 size1 = (uint64 )param_count + (uint64 )result_count ;
557560 size = offsetof(AOTFuncType , types ) + size1 ;
558- if (size >= UINT32_MAX
559- || !(func_types [i ] = wasm_runtime_malloc ((uint32 )size ))) {
560- set_error_buf (error_buf , error_buf_size ,
561- "AOT module load failed: "
562- "allocate memory failed." );
561+ if (!(func_types [i ] = loader_malloc
562+ (size , error_buf , error_buf_size ))) {
563563 return false;
564564 }
565565
@@ -613,17 +613,11 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
613613
614614 /* Allocate memory */
615615 size = sizeof (AOTImportGlobal ) * (uint64 )module -> import_global_count ;
616- if (size >= UINT32_MAX
617- || !(module -> import_globals =
618- import_globals = wasm_runtime_malloc ((uint32 )size ))) {
619- set_error_buf (error_buf , error_buf_size ,
620- "AOT module load failed: "
621- "allocate memory failed." );
616+ if (!(module -> import_globals = import_globals =
617+ loader_malloc (size , error_buf , error_buf_size ))) {
622618 return false;
623619 }
624620
625- memset (import_globals , 0 , size );
626-
627621 /* Create each import global */
628622 for (i = 0 ; i < module -> import_global_count ; i ++ ) {
629623 buf = (uint8 * )align_ptr (buf , 2 );
@@ -685,16 +679,11 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end,
685679
686680 /* Allocate memory */
687681 size = sizeof (AOTGlobal ) * (uint64 )module -> global_count ;
688- if (size >= UINT32_MAX
689- || !(module -> globals = globals = wasm_runtime_malloc ((uint32 )size ))) {
690- set_error_buf (error_buf , error_buf_size ,
691- "AOT module load failed: "
692- "allocate memory failed." );
682+ if (!(module -> globals = globals = loader_malloc
683+ (size , error_buf , error_buf_size ))) {
693684 return false;
694685 }
695686
696- memset (globals , 0 , size );
697-
698687 if (module -> import_global_count > 0 ) {
699688 last_import_global =
700689 & module -> import_globals [module -> import_global_count - 1 ];
@@ -767,17 +756,11 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
767756
768757 /* Allocate memory */
769758 size = sizeof (AOTImportFunc ) * (uint64 )module -> import_func_count ;
770- if (size >= UINT32_MAX
771- || !(module -> import_funcs =
772- import_funcs = wasm_runtime_malloc ((uint32 )size ))) {
773- set_error_buf (error_buf , error_buf_size ,
774- "AOT module load failed: "
775- "allocate memory failed." );
759+ if (!(module -> import_funcs = import_funcs =
760+ loader_malloc (size , error_buf , error_buf_size ))) {
776761 return false;
777762 }
778763
779- memset (import_funcs , 0 , size );
780-
781764 /* Create each import func */
782765 for (i = 0 ; i < module -> import_func_count ; i ++ ) {
783766 read_uint16 (buf , buf_end , import_funcs [i ].func_type_index );
@@ -860,17 +843,11 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
860843
861844 /* Allocate memory */
862845 size = sizeof (AOTObjectDataSection ) * (uint64 )module -> data_section_count ;
863- if (size >= UINT32_MAX
864- || !(module -> data_sections =
865- data_sections = wasm_runtime_malloc ((uint32 )size ))) {
866- set_error_buf (error_buf , error_buf_size ,
867- "AOT module load failed: "
868- "allocate memory failed." );
846+ if (!(module -> data_sections = data_sections =
847+ loader_malloc (size , error_buf , error_buf_size ))) {
869848 return false;
870849 }
871850
872- memset (data_sections , 0 , size );
873-
874851 /* Create each data section */
875852 for (i = 0 ; i < module -> data_section_count ; i ++ ) {
876853 int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE ;
@@ -1021,10 +998,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
1021998 uint64 size , text_offset ;
1022999
10231000 size = sizeof (void * ) * (uint64 )module -> func_count ;
1024- if (size >= UINT32_MAX
1025- || !(module -> func_ptrs = wasm_runtime_malloc ((uint32 )size ))) {
1026- set_error_buf (error_buf , error_buf_size ,
1027- "AOT module load failed: allocate memory failed." );
1001+ if (!(module -> func_ptrs = loader_malloc
1002+ (size , error_buf , error_buf_size ))) {
10281003 return false;
10291004 }
10301005
@@ -1065,10 +1040,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
10651040 }
10661041
10671042 size = sizeof (uint32 ) * (uint64 )module -> func_count ;
1068- if (size >= UINT32_MAX
1069- || !(module -> func_type_indexes = wasm_runtime_malloc ((uint32 )size ))) {
1070- set_error_buf (error_buf , error_buf_size ,
1071- "AOT module load failed: allocate memory failed." );
1043+ if (!(module -> func_type_indexes = loader_malloc
1044+ (size , error_buf , error_buf_size ))) {
10721045 return false;
10731046 }
10741047
@@ -1112,17 +1085,11 @@ load_export_funcs(const uint8 **p_buf, const uint8 *buf_end,
11121085
11131086 /* Allocate memory */
11141087 size = sizeof (AOTExportFunc ) * (uint64 )module -> export_func_count ;
1115- if (size >= UINT32_MAX
1116- || !(module -> export_funcs =
1117- export_funcs = wasm_runtime_malloc ((uint32 )size ))) {
1118- set_error_buf (error_buf , error_buf_size ,
1119- "AOT module load failed: "
1120- "allocate memory failed." );
1088+ if (!(module -> export_funcs = export_funcs =
1089+ loader_malloc (size , error_buf , error_buf_size ))) {
11211090 return false;
11221091 }
11231092
1124- memset (export_funcs , 0 , size );
1125-
11261093 /* Create each export func */
11271094 for (i = 0 ; i < module -> export_func_count ; i ++ ) {
11281095 read_uint32 (buf , buf_end , export_funcs [i ].func_index );
@@ -1234,10 +1201,8 @@ do_text_relocation(AOTModule *module,
12341201 if (symbol_len + 1 <= sizeof (symbol_buf ))
12351202 symbol = symbol_buf ;
12361203 else {
1237- if (!(symbol = wasm_runtime_malloc (symbol_len + 1 ))) {
1238- set_error_buf (error_buf , error_buf_size ,
1239- "AOT module load failed: "
1240- "allocate memory failed." );
1204+ if (!(symbol = loader_malloc (symbol_len + 1 ,
1205+ error_buf , error_buf_size ))) {
12411206 return false;
12421207 }
12431208 }
@@ -1432,15 +1397,10 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
14321397
14331398 /* Allocate memory for relocation groups */
14341399 size = sizeof (AOTRelocationGroup ) * (uint64 )group_count ;
1435- if (size >= UINT32_MAX || !(groups = wasm_runtime_malloc ((uint32 )size ))) {
1436- set_error_buf (error_buf , error_buf_size ,
1437- "AOT module load failed: "
1438- "allocate memory failed." );
1400+ if (!(groups = loader_malloc (size , error_buf , error_buf_size ))) {
14391401 goto fail ;
14401402 }
14411403
1442- memset (groups , 0 , size );
1443-
14441404 /* Load each relocation group */
14451405 for (i = 0 , group = groups ; i < group_count ; i ++ , group ++ ) {
14461406 AOTRelocation * relocation ;
@@ -1473,18 +1433,12 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
14731433
14741434 /* Allocate memory for relocations */
14751435 size = sizeof (AOTRelocation ) * (uint64 )group -> relocation_count ;
1476- if (size >= UINT32_MAX
1477- || !(group -> relocations = relocation =
1478- wasm_runtime_malloc ((uint32 )size ))) {
1479- set_error_buf (error_buf , error_buf_size ,
1480- "AOT module load failed: "
1481- "allocate memory failed." );
1436+ if (!(group -> relocations = relocation =
1437+ loader_malloc (size , error_buf , error_buf_size ))) {
14821438 ret = false;
14831439 goto fail ;
14841440 }
14851441
1486- memset (group -> relocations , 0 , size );
1487-
14881442 /* Load each relocation */
14891443 for (j = 0 ; j < group -> relocation_count ; j ++ , relocation ++ ) {
14901444 uint32 symbol_index ;
0 commit comments