aot.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot.h"
  6. static char aot_error[128];
  7. char *
  8. aot_get_last_error()
  9. {
  10. return aot_error[0] == '\0' ? "" : aot_error;
  11. }
  12. void
  13. aot_set_last_error_v(const char *format, ...)
  14. {
  15. va_list args;
  16. va_start(args, format);
  17. vsnprintf(aot_error, sizeof(aot_error), format, args);
  18. va_end(args);
  19. }
  20. void
  21. aot_set_last_error(const char *error)
  22. {
  23. if (error)
  24. snprintf(aot_error, sizeof(aot_error), "Error: %s", error);
  25. else
  26. aot_error[0] = '\0';
  27. }
  28. static void
  29. aot_destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count)
  30. {
  31. uint32 i;
  32. for (i = 0; i < count; i++)
  33. if (data_list[i])
  34. wasm_runtime_free(data_list[i]);
  35. wasm_runtime_free(data_list);
  36. }
  37. static AOTMemInitData **
  38. aot_create_mem_init_data_list(const WASMModule *module)
  39. {
  40. AOTMemInitData **data_list;
  41. uint64 size;
  42. uint32 i;
  43. /* Allocate memory */
  44. size = sizeof(AOTMemInitData *) * (uint64)module->data_seg_count;
  45. if (size >= UINT32_MAX
  46. || !(data_list = wasm_runtime_malloc((uint32)size))) {
  47. aot_set_last_error("allocate memory failed.");
  48. return NULL;
  49. }
  50. memset(data_list, 0, size);
  51. /* Create each memory data segment */
  52. for (i = 0; i < module->data_seg_count; i++) {
  53. size = offsetof(AOTMemInitData, bytes)
  54. + (uint64)module->data_segments[i]->data_length;
  55. if (size >= UINT32_MAX
  56. || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
  57. aot_set_last_error("allocate memory failed.");
  58. goto fail;
  59. }
  60. #if WASM_ENABLE_BULK_MEMORY != 0
  61. data_list[i]->is_passive = module->data_segments[i]->is_passive;
  62. data_list[i]->memory_index = module->data_segments[i]->memory_index;
  63. #endif
  64. data_list[i]->offset = module->data_segments[i]->base_offset;
  65. data_list[i]->byte_count = module->data_segments[i]->data_length;
  66. memcpy(data_list[i]->bytes, module->data_segments[i]->data,
  67. module->data_segments[i]->data_length);
  68. }
  69. return data_list;
  70. fail:
  71. aot_destroy_mem_init_data_list(data_list, module->data_seg_count);
  72. return NULL;
  73. }
  74. static void
  75. aot_destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
  76. {
  77. uint32 i;
  78. for (i = 0; i < count; i++)
  79. if (data_list[i])
  80. wasm_runtime_free(data_list[i]);
  81. wasm_runtime_free(data_list);
  82. }
  83. static AOTTableInitData **
  84. aot_create_table_init_data_list(const WASMModule *module)
  85. {
  86. AOTTableInitData **data_list;
  87. uint64 size;
  88. uint32 i;
  89. /* Allocate memory */
  90. size = sizeof(AOTTableInitData *) * (uint64)module->table_seg_count;
  91. if (size >= UINT32_MAX
  92. || !(data_list = wasm_runtime_malloc((uint32)size))) {
  93. aot_set_last_error("allocate memory failed.");
  94. return NULL;
  95. }
  96. memset(data_list, 0, size);
  97. /* Create each table data segment */
  98. for (i = 0; i < module->table_seg_count; i++) {
  99. size = offsetof(AOTTableInitData, init_values)
  100. + sizeof(InitializerExpression)
  101. * (uint64)module->table_segments[i].value_count;
  102. if (size >= UINT32_MAX
  103. || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
  104. aot_set_last_error("allocate memory failed.");
  105. goto fail;
  106. }
  107. data_list[i]->offset = module->table_segments[i].base_offset;
  108. data_list[i]->value_count = module->table_segments[i].value_count;
  109. data_list[i]->mode = module->table_segments[i].mode;
  110. data_list[i]->elem_type = module->table_segments[i].elem_type;
  111. /* runtime control it */
  112. data_list[i]->table_index = module->table_segments[i].table_index;
  113. bh_memcpy_s(&data_list[i]->offset, sizeof(AOTInitExpr),
  114. &module->table_segments[i].base_offset,
  115. sizeof(AOTInitExpr));
  116. data_list[i]->value_count = module->table_segments[i].value_count;
  117. #if WASM_ENABLE_GC != 0
  118. data_list[i]->elem_ref_type = module->table_segments[i].elem_ref_type;
  119. #endif
  120. bh_memcpy_s(data_list[i]->init_values,
  121. sizeof(InitializerExpression)
  122. * module->table_segments[i].value_count,
  123. module->table_segments[i].init_values,
  124. sizeof(InitializerExpression)
  125. * module->table_segments[i].value_count);
  126. }
  127. return data_list;
  128. fail:
  129. aot_destroy_table_init_data_list(data_list, module->table_seg_count);
  130. return NULL;
  131. }
  132. static void
  133. get_value_type_size(uint8 value_type, bool gc_enabled, uint32 *p_size_64bit,
  134. uint32 *p_size_32bit)
  135. {
  136. uint32 size_64bit = 0, size_32bit = 0;
  137. if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32)
  138. size_64bit = size_32bit = sizeof(int32);
  139. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  140. size_64bit = size_32bit = sizeof(int64);
  141. else if (value_type == VALUE_TYPE_V128)
  142. size_64bit = size_32bit = sizeof(int64) * 2;
  143. else if (!gc_enabled
  144. && (value_type == VALUE_TYPE_FUNCREF
  145. || value_type == VALUE_TYPE_EXTERNREF))
  146. size_64bit = size_32bit = sizeof(int32);
  147. else if (gc_enabled
  148. && ((value_type >= (uint8)REF_TYPE_ARRAYREF
  149. && value_type <= (uint8)REF_TYPE_NULLFUNCREF)
  150. || (value_type >= (uint8)REF_TYPE_HT_NULLABLE
  151. && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE)
  152. #if WASM_ENABLE_STRINGREF != 0
  153. || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8
  154. && value_type <= (uint8)REF_TYPE_STRINGREF)
  155. || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER
  156. && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16)
  157. #endif
  158. )) {
  159. size_64bit = sizeof(uint64);
  160. size_32bit = sizeof(uint32);
  161. }
  162. else if (gc_enabled && value_type == PACKED_TYPE_I8) {
  163. size_64bit = size_32bit = sizeof(int8);
  164. }
  165. else if (gc_enabled && value_type == PACKED_TYPE_I16) {
  166. size_64bit = size_32bit = sizeof(int16);
  167. }
  168. else {
  169. bh_assert(0);
  170. }
  171. *p_size_64bit = size_64bit;
  172. *p_size_32bit = size_32bit;
  173. }
  174. static AOTImportGlobal *
  175. aot_create_import_globals(const WASMModule *module, bool gc_enabled,
  176. uint32 *p_import_global_data_size_64bit,
  177. uint32 *p_import_global_data_size_32bit)
  178. {
  179. AOTImportGlobal *import_globals;
  180. uint64 size;
  181. uint32 i, data_offset_64bit = 0, data_offset_32bit = 0;
  182. uint32 value_size_64bit, value_size_32bit;
  183. /* Allocate memory */
  184. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  185. if (size >= UINT32_MAX
  186. || !(import_globals = wasm_runtime_malloc((uint32)size))) {
  187. aot_set_last_error("allocate memory failed.");
  188. return NULL;
  189. }
  190. memset(import_globals, 0, (uint32)size);
  191. /* Create each import global */
  192. for (i = 0; i < module->import_global_count; i++) {
  193. WASMGlobalImport *import_global = &module->import_globals[i].u.global;
  194. import_globals[i].module_name = import_global->module_name;
  195. import_globals[i].global_name = import_global->field_name;
  196. import_globals[i].type = import_global->type;
  197. import_globals[i].is_mutable = import_global->is_mutable;
  198. import_globals[i].global_data_linked =
  199. import_global->global_data_linked;
  200. import_globals[i].data_offset_64bit = data_offset_64bit;
  201. import_globals[i].data_offset_32bit = data_offset_32bit;
  202. get_value_type_size(import_global->type, gc_enabled, &value_size_64bit,
  203. &value_size_32bit);
  204. import_globals[i].size_64bit = value_size_64bit;
  205. import_globals[i].size_32bit = value_size_32bit;
  206. data_offset_64bit += value_size_64bit;
  207. data_offset_32bit += value_size_32bit;
  208. }
  209. *p_import_global_data_size_64bit = data_offset_64bit;
  210. *p_import_global_data_size_32bit = data_offset_32bit;
  211. return import_globals;
  212. }
  213. static AOTGlobal *
  214. aot_create_globals(const WASMModule *module, bool gc_enabled,
  215. uint32 global_data_start_offset_64bit,
  216. uint32 global_data_start_offset_32bit,
  217. uint32 *p_global_data_size_64bit,
  218. uint32 *p_global_data_size_32bit)
  219. {
  220. AOTGlobal *globals;
  221. uint64 size;
  222. uint32 i;
  223. uint32 data_offset_64bit = global_data_start_offset_64bit;
  224. uint32 data_offset_32bit = global_data_start_offset_32bit;
  225. uint32 value_size_64bit, value_size_32bit;
  226. /* Allocate memory */
  227. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  228. if (size >= UINT32_MAX || !(globals = wasm_runtime_malloc((uint32)size))) {
  229. aot_set_last_error("allocate memory failed.");
  230. return NULL;
  231. }
  232. memset(globals, 0, (uint32)size);
  233. /* Create each global */
  234. for (i = 0; i < module->global_count; i++) {
  235. WASMGlobal *global = &module->globals[i];
  236. globals[i].type = global->type;
  237. globals[i].is_mutable = global->is_mutable;
  238. memcpy(&globals[i].init_expr, &global->init_expr,
  239. sizeof(global->init_expr));
  240. globals[i].data_offset_64bit = data_offset_64bit;
  241. globals[i].data_offset_32bit = data_offset_32bit;
  242. get_value_type_size(global->type, gc_enabled, &value_size_64bit,
  243. &value_size_32bit);
  244. globals[i].size_64bit = value_size_64bit;
  245. globals[i].size_32bit = value_size_32bit;
  246. data_offset_64bit += value_size_64bit;
  247. data_offset_32bit += value_size_32bit;
  248. }
  249. *p_global_data_size_64bit =
  250. data_offset_64bit - global_data_start_offset_64bit;
  251. *p_global_data_size_32bit =
  252. data_offset_32bit - global_data_start_offset_32bit;
  253. return globals;
  254. }
  255. static AOTImportFunc *
  256. aot_create_import_funcs(const WASMModule *module)
  257. {
  258. AOTImportFunc *import_funcs;
  259. uint64 size;
  260. uint32 i, j;
  261. /* Allocate memory */
  262. size = sizeof(AOTImportFunc) * (uint64)module->import_function_count;
  263. if (size >= UINT32_MAX
  264. || !(import_funcs = wasm_runtime_malloc((uint32)size))) {
  265. aot_set_last_error("allocate memory failed.");
  266. return NULL;
  267. }
  268. /* Create each import function */
  269. for (i = 0; i < module->import_function_count; i++) {
  270. WASMFunctionImport *import_func =
  271. &module->import_functions[i].u.function;
  272. import_funcs[i].module_name = import_func->module_name;
  273. import_funcs[i].func_name = import_func->field_name;
  274. import_funcs[i].func_ptr_linked = import_func->func_ptr_linked;
  275. import_funcs[i].func_type = import_func->func_type;
  276. import_funcs[i].signature = import_func->signature;
  277. import_funcs[i].attachment = import_func->attachment;
  278. import_funcs[i].call_conv_raw = import_func->call_conv_raw;
  279. import_funcs[i].call_conv_wasm_c_api = false;
  280. /* Resolve function type index */
  281. for (j = 0; j < module->type_count; j++)
  282. if (import_func->func_type == (WASMFuncType *)module->types[j]) {
  283. import_funcs[i].func_type_index = j;
  284. break;
  285. }
  286. }
  287. return import_funcs;
  288. }
  289. static void
  290. aot_destroy_funcs(AOTFunc **funcs, uint32 count)
  291. {
  292. uint32 i;
  293. for (i = 0; i < count; i++)
  294. if (funcs[i]) {
  295. if (funcs[i]->local_offsets)
  296. wasm_runtime_free(funcs[i]->local_offsets);
  297. wasm_runtime_free(funcs[i]);
  298. }
  299. wasm_runtime_free(funcs);
  300. }
  301. static AOTFunc **
  302. aot_create_funcs(const WASMModule *module, uint32 pointer_size)
  303. {
  304. AOTFunc **funcs;
  305. uint64 size;
  306. uint32 i, j;
  307. /* Allocate memory */
  308. size = sizeof(AOTFunc *) * (uint64)module->function_count;
  309. if (size >= UINT32_MAX || !(funcs = wasm_runtime_malloc((uint32)size))) {
  310. aot_set_last_error("allocate memory failed.");
  311. return NULL;
  312. }
  313. memset(funcs, 0, size);
  314. /* Create each function */
  315. for (i = 0; i < module->function_count; i++) {
  316. WASMFunction *func = module->functions[i];
  317. AOTFuncType *func_type = NULL;
  318. AOTFunc *aot_func = NULL;
  319. uint64 total_size;
  320. uint32 offset = 0;
  321. size = sizeof(AOTFunc);
  322. if (!(aot_func = funcs[i] = wasm_runtime_malloc((uint32)size))) {
  323. aot_set_last_error("allocate memory failed.");
  324. goto fail;
  325. }
  326. memset(aot_func, 0, sizeof(AOTFunc));
  327. func_type = aot_func->func_type = func->func_type;
  328. /* Resolve function type index */
  329. for (j = 0; j < module->type_count; j++) {
  330. if (func_type == (WASMFuncType *)module->types[j]) {
  331. aot_func->func_type_index = j;
  332. break;
  333. }
  334. }
  335. total_size = sizeof(uint16)
  336. * ((uint64)func_type->param_count + func->local_count);
  337. if ((total_size > 0)
  338. && (total_size >= UINT32_MAX
  339. || !(aot_func->local_offsets =
  340. wasm_runtime_malloc((uint32)total_size)))) {
  341. aot_set_last_error("allocate memory failed.");
  342. goto fail;
  343. }
  344. /* Resolve local variable info and code info */
  345. aot_func->local_count = func->local_count;
  346. aot_func->local_types_wp = func->local_types;
  347. aot_func->code = func->code;
  348. aot_func->code_size = func->code_size;
  349. /* Resolve local offsets */
  350. for (j = 0; j < func_type->param_count; j++) {
  351. aot_func->local_offsets[j] = (uint16)offset;
  352. offset += wasm_value_type_cell_num_internal(func_type->types[j],
  353. pointer_size);
  354. }
  355. aot_func->param_cell_num = offset;
  356. for (j = 0; j < func->local_count; j++) {
  357. aot_func->local_offsets[func_type->param_count + j] =
  358. (uint16)offset;
  359. offset += wasm_value_type_cell_num_internal(func->local_types[j],
  360. pointer_size);
  361. }
  362. aot_func->local_cell_num = offset - aot_func->param_cell_num;
  363. aot_func->max_stack_cell_num = func->max_stack_cell_num;
  364. /* We use max_stack_cell_num calculated from wasm_loader, which is based
  365. * on wamrc's target type.
  366. * - If the wamrc is compiled as 64bit, then the number is enough for
  367. * both 32bit and 64bit runtime target
  368. * - If the wamrc is compiled as 32bit, then we multiply this number by
  369. * two to avoid overflow on 64bit runtime target */
  370. if (sizeof(uintptr_t) == 4) {
  371. aot_func->max_stack_cell_num *= 2;
  372. }
  373. }
  374. return funcs;
  375. fail:
  376. aot_destroy_funcs(funcs, module->function_count);
  377. return NULL;
  378. }
  379. #if WASM_ENABLE_GC != 0
  380. static void
  381. calculate_struct_field_sizes_offsets(AOTCompData *comp_data, bool is_target_x86,
  382. bool gc_enabled)
  383. {
  384. uint32 i;
  385. for (i = 0; i < comp_data->type_count; i++) {
  386. if (comp_data->types[i]->type_flag == WASM_TYPE_STRUCT) {
  387. WASMStructType *struct_type = (WASMStructType *)comp_data->types[i];
  388. WASMStructFieldType *fields = struct_type->fields;
  389. uint32 field_offset_64bit, field_offset_32bit;
  390. uint32 field_size_64bit, field_size_32bit, j;
  391. /* offsetof(WASMStructObject, field_data) in 64-bit */
  392. field_offset_64bit = sizeof(uint64);
  393. /* offsetof(WASMStructObject, field_data) in 32-bit */
  394. field_offset_32bit = sizeof(uint32);
  395. for (j = 0; j < struct_type->field_count; j++) {
  396. get_value_type_size(fields[j].field_type, gc_enabled,
  397. &field_size_64bit, &field_size_32bit);
  398. fields[j].field_size_64bit = field_size_64bit;
  399. fields[j].field_size_32bit = field_size_32bit;
  400. if (!is_target_x86) {
  401. if (field_size_64bit == 2)
  402. field_offset_64bit = align_uint(field_offset_64bit, 2);
  403. else if (field_size_64bit >= 4)
  404. field_offset_64bit = align_uint(field_offset_64bit, 4);
  405. if (field_size_32bit == 2)
  406. field_offset_32bit = align_uint(field_offset_32bit, 2);
  407. else if (field_size_32bit >= 4)
  408. field_offset_32bit = align_uint(field_offset_32bit, 4);
  409. }
  410. fields[j].field_offset_64bit = field_offset_64bit;
  411. fields[j].field_offset_32bit = field_offset_32bit;
  412. field_offset_64bit += field_size_64bit;
  413. field_offset_32bit += field_size_32bit;
  414. }
  415. }
  416. }
  417. }
  418. #endif
  419. AOTCompData *
  420. aot_create_comp_data(WASMModule *module, const char *target_arch,
  421. bool gc_enabled)
  422. {
  423. AOTCompData *comp_data;
  424. uint32 import_global_data_size_64bit = 0, global_data_size_64bit = 0, i, j;
  425. uint32 import_global_data_size_32bit = 0, global_data_size_32bit = 0;
  426. uint64 size;
  427. bool is_64bit_target = false;
  428. #if WASM_ENABLE_GC != 0
  429. bool is_target_x86 = false;
  430. #endif
  431. #if WASM_ENABLE_GC != 0
  432. if (!target_arch) {
  433. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  434. || defined(BUILD_TARGET_X86_32)
  435. is_target_x86 = true;
  436. #endif
  437. }
  438. else {
  439. if (!strncmp(target_arch, "x86_64", 6)
  440. || !strncmp(target_arch, "i386", 4))
  441. is_target_x86 = true;
  442. }
  443. #endif
  444. if (!target_arch) {
  445. #if UINTPTR_MAX == UINT64_MAX
  446. is_64bit_target = true;
  447. #endif
  448. }
  449. else {
  450. /* All 64bit targets contains "64" string in their target name */
  451. if (strstr(target_arch, "64") != NULL) {
  452. is_64bit_target = true;
  453. }
  454. }
  455. /* Allocate memory */
  456. if (!(comp_data = wasm_runtime_malloc(sizeof(AOTCompData)))) {
  457. aot_set_last_error("create compile data failed.\n");
  458. return NULL;
  459. }
  460. memset(comp_data, 0, sizeof(AOTCompData));
  461. comp_data->memory_count =
  462. module->import_memory_count + module->memory_count;
  463. /* TODO: create import memories */
  464. /* Allocate memory for memory array, reserve one AOTMemory space at least */
  465. if (!comp_data->memory_count)
  466. comp_data->memory_count = 1;
  467. size = (uint64)comp_data->memory_count * sizeof(AOTMemory);
  468. if (size >= UINT32_MAX
  469. || !(comp_data->memories = wasm_runtime_malloc((uint32)size))) {
  470. aot_set_last_error("create memories array failed.\n");
  471. goto fail;
  472. }
  473. memset(comp_data->memories, 0, size);
  474. if (!(module->import_memory_count + module->memory_count)) {
  475. comp_data->memories[0].num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  476. }
  477. /* Set memory page count */
  478. for (i = 0; i < module->import_memory_count + module->memory_count; i++) {
  479. if (i < module->import_memory_count) {
  480. comp_data->memories[i].memory_flags =
  481. module->import_memories[i].u.memory.flags;
  482. comp_data->memories[i].num_bytes_per_page =
  483. module->import_memories[i].u.memory.num_bytes_per_page;
  484. comp_data->memories[i].mem_init_page_count =
  485. module->import_memories[i].u.memory.init_page_count;
  486. comp_data->memories[i].mem_max_page_count =
  487. module->import_memories[i].u.memory.max_page_count;
  488. comp_data->memories[i].num_bytes_per_page =
  489. module->import_memories[i].u.memory.num_bytes_per_page;
  490. }
  491. else {
  492. j = i - module->import_memory_count;
  493. comp_data->memories[i].memory_flags = module->memories[j].flags;
  494. comp_data->memories[i].num_bytes_per_page =
  495. module->memories[j].num_bytes_per_page;
  496. comp_data->memories[i].mem_init_page_count =
  497. module->memories[j].init_page_count;
  498. comp_data->memories[i].mem_max_page_count =
  499. module->memories[j].max_page_count;
  500. comp_data->memories[i].num_bytes_per_page =
  501. module->memories[j].num_bytes_per_page;
  502. }
  503. }
  504. /* Create memory data segments */
  505. comp_data->mem_init_data_count = module->data_seg_count;
  506. if (comp_data->mem_init_data_count > 0
  507. && !(comp_data->mem_init_data_list =
  508. aot_create_mem_init_data_list(module)))
  509. goto fail;
  510. /* Create tables */
  511. comp_data->table_count = module->import_table_count + module->table_count;
  512. if (comp_data->table_count > 0) {
  513. size = sizeof(AOTTable) * (uint64)comp_data->table_count;
  514. if (size >= UINT32_MAX
  515. || !(comp_data->tables = wasm_runtime_malloc((uint32)size))) {
  516. aot_set_last_error("create memories array failed.\n");
  517. goto fail;
  518. }
  519. memset(comp_data->tables, 0, size);
  520. for (i = 0; i < comp_data->table_count; i++) {
  521. if (i < module->import_table_count) {
  522. comp_data->tables[i].elem_type =
  523. module->import_tables[i].u.table.elem_type;
  524. comp_data->tables[i].table_flags =
  525. module->import_tables[i].u.table.flags;
  526. comp_data->tables[i].table_init_size =
  527. module->import_tables[i].u.table.init_size;
  528. comp_data->tables[i].table_max_size =
  529. module->import_tables[i].u.table.max_size;
  530. #if WASM_ENABLE_GC != 0
  531. comp_data->tables[i].elem_ref_type =
  532. module->import_tables[i].u.table.elem_ref_type;
  533. #endif
  534. comp_data->tables[i].possible_grow =
  535. module->import_tables[i].u.table.possible_grow;
  536. }
  537. else {
  538. j = i - module->import_table_count;
  539. comp_data->tables[i].elem_type = module->tables[j].elem_type;
  540. comp_data->tables[i].table_flags = module->tables[j].flags;
  541. comp_data->tables[i].table_init_size =
  542. module->tables[j].init_size;
  543. comp_data->tables[i].table_max_size =
  544. module->tables[j].max_size;
  545. comp_data->tables[i].possible_grow =
  546. module->tables[j].possible_grow;
  547. #if WASM_ENABLE_GC != 0
  548. comp_data->tables[j].elem_ref_type =
  549. module->tables[j].elem_ref_type;
  550. /* Note: if the init_expr contains extra data for struct/array
  551. * initialization information (init_expr.u.data), the pointer is
  552. * copied.
  553. * The pointers should still belong to wasm module, so DO NOT
  554. * free the pointers copied to comp_data */
  555. comp_data->tables[j].init_expr = module->tables[j].init_expr;
  556. #endif
  557. }
  558. }
  559. }
  560. /* Create table data segments */
  561. comp_data->table_init_data_count = module->table_seg_count;
  562. if (comp_data->table_init_data_count > 0
  563. && !(comp_data->table_init_data_list =
  564. aot_create_table_init_data_list(module)))
  565. goto fail;
  566. /* Create import globals */
  567. comp_data->import_global_count = module->import_global_count;
  568. if (comp_data->import_global_count > 0
  569. && !(comp_data->import_globals = aot_create_import_globals(
  570. module, gc_enabled, &import_global_data_size_64bit,
  571. &import_global_data_size_32bit)))
  572. goto fail;
  573. /* Create globals */
  574. comp_data->global_count = module->global_count;
  575. if (comp_data->global_count
  576. && !(comp_data->globals = aot_create_globals(
  577. module, gc_enabled, import_global_data_size_64bit,
  578. import_global_data_size_32bit, &global_data_size_64bit,
  579. &global_data_size_32bit)))
  580. goto fail;
  581. comp_data->global_data_size_64bit =
  582. import_global_data_size_64bit + global_data_size_64bit;
  583. comp_data->global_data_size_32bit =
  584. import_global_data_size_32bit + global_data_size_32bit;
  585. /* Create types, they are checked by wasm loader */
  586. comp_data->type_count = module->type_count;
  587. comp_data->types = module->types;
  588. #if WASM_ENABLE_GC != 0
  589. /* Calculate the field sizes and field offsets for 64-bit and 32-bit
  590. targets since they may vary in 32-bit target and 64-bit target */
  591. calculate_struct_field_sizes_offsets(comp_data, is_target_x86, gc_enabled);
  592. #endif
  593. /* Create import functions */
  594. comp_data->import_func_count = module->import_function_count;
  595. if (comp_data->import_func_count
  596. && !(comp_data->import_funcs = aot_create_import_funcs(module)))
  597. goto fail;
  598. /* Create functions */
  599. comp_data->func_count = module->function_count;
  600. if (comp_data->func_count
  601. && !(comp_data->funcs =
  602. aot_create_funcs(module, is_64bit_target ? 8 : 4)))
  603. goto fail;
  604. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  605. /* Create custom name section */
  606. comp_data->name_section_buf = module->name_section_buf;
  607. comp_data->name_section_buf_end = module->name_section_buf_end;
  608. #endif
  609. /* Create aux data/heap/stack information */
  610. comp_data->aux_data_end_global_index = module->aux_data_end_global_index;
  611. comp_data->aux_data_end = module->aux_data_end;
  612. comp_data->aux_heap_base_global_index = module->aux_heap_base_global_index;
  613. comp_data->aux_heap_base = module->aux_heap_base;
  614. comp_data->aux_stack_top_global_index = module->aux_stack_top_global_index;
  615. comp_data->aux_stack_bottom = module->aux_stack_bottom;
  616. comp_data->aux_stack_size = module->aux_stack_size;
  617. comp_data->start_func_index = module->start_function;
  618. comp_data->malloc_func_index = module->malloc_function;
  619. comp_data->free_func_index = module->free_function;
  620. comp_data->retain_func_index = module->retain_function;
  621. #if WASM_ENABLE_STRINGREF != 0
  622. comp_data->string_literal_count = module->string_literal_count;
  623. comp_data->string_literal_ptrs_wp = module->string_literal_ptrs;
  624. comp_data->string_literal_lengths_wp = module->string_literal_lengths;
  625. #endif
  626. comp_data->wasm_module = module;
  627. return comp_data;
  628. fail:
  629. aot_destroy_comp_data(comp_data);
  630. return NULL;
  631. }
  632. void
  633. aot_destroy_comp_data(AOTCompData *comp_data)
  634. {
  635. if (!comp_data)
  636. return;
  637. if (comp_data->import_memories)
  638. wasm_runtime_free(comp_data->import_memories);
  639. if (comp_data->memories)
  640. wasm_runtime_free(comp_data->memories);
  641. if (comp_data->mem_init_data_list)
  642. aot_destroy_mem_init_data_list(comp_data->mem_init_data_list,
  643. comp_data->mem_init_data_count);
  644. if (comp_data->import_tables)
  645. wasm_runtime_free(comp_data->import_tables);
  646. if (comp_data->tables)
  647. wasm_runtime_free(comp_data->tables);
  648. if (comp_data->table_init_data_list)
  649. aot_destroy_table_init_data_list(comp_data->table_init_data_list,
  650. comp_data->table_init_data_count);
  651. if (comp_data->import_globals)
  652. wasm_runtime_free(comp_data->import_globals);
  653. if (comp_data->globals)
  654. wasm_runtime_free(comp_data->globals);
  655. if (comp_data->import_funcs)
  656. wasm_runtime_free(comp_data->import_funcs);
  657. if (comp_data->funcs)
  658. aot_destroy_funcs(comp_data->funcs, comp_data->func_count);
  659. if (comp_data->aot_name_section_buf)
  660. wasm_runtime_free(comp_data->aot_name_section_buf);
  661. wasm_runtime_free(comp_data);
  662. }