aot_loader.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_runtime.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "aot_reloc.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_native.h"
  11. #include "../compilation/aot.h"
  12. #if WASM_ENABLE_JIT != 0
  13. #include "../compilation/aot_llvm.h"
  14. #include "../interpreter/wasm_loader.h"
  15. #endif
  16. static void
  17. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  18. {
  19. if (error_buf != NULL)
  20. snprintf(error_buf, error_buf_size, "%s", string);
  21. }
  22. #define exchange_uint8(p_data) (void)0
  23. static void
  24. exchange_uint16(uint8 *p_data)
  25. {
  26. uint8 value = *p_data;
  27. *p_data = *(p_data + 1);
  28. *(p_data + 1) = value;
  29. }
  30. static void
  31. exchange_uint32(uint8 *p_data)
  32. {
  33. uint8 value = *p_data;
  34. *p_data = *(p_data + 3);
  35. *(p_data + 3) = value;
  36. value = *(p_data + 1);
  37. *(p_data + 1) = *(p_data + 2);
  38. *(p_data + 2) = value;
  39. }
  40. static void
  41. exchange_uint64(uint8 *pData)
  42. {
  43. exchange_uint32(pData);
  44. exchange_uint32(pData + 4);
  45. }
  46. static union {
  47. int a;
  48. char b;
  49. } __ue = { .a = 1 };
  50. #define is_little_endian() (__ue.b == 1)
  51. #define CHECK_BUF(buf, buf_end, length) do { \
  52. if (buf + length > buf_end) { \
  53. set_error_buf(error_buf, error_buf_size, \
  54. "Read data failed: unexpected end."); \
  55. goto fail; \
  56. } \
  57. } while (0)
  58. static uint8*
  59. align_ptr(const uint8 *p, uint32 b)
  60. {
  61. uintptr_t v = (uintptr_t)p;
  62. uintptr_t m = b - 1;
  63. return (uint8*)((v + m) & ~m);
  64. }
  65. static inline uint64
  66. GET_U64_FROM_ADDR(uint32 *addr)
  67. {
  68. union { uint64 val; uint32 parts[2]; } u;
  69. u.parts[0] = addr[0];
  70. u.parts[1] = addr[1];
  71. return u.val;
  72. }
  73. #define TEMPLATE_READ(p, p_end, res, type) do { \
  74. if (sizeof(type) != sizeof(uint64)) \
  75. p = (uint8*)align_ptr(p, sizeof(type)); \
  76. else \
  77. /* align 4 bytes if type is uint64 */ \
  78. p = (uint8*)align_ptr(p, sizeof(uint32)); \
  79. CHECK_BUF(p, p_end, sizeof(type)); \
  80. if (sizeof(type) != sizeof(uint64)) \
  81. res = *(type*)p; \
  82. else \
  83. res = (type)GET_U64_FROM_ADDR((uint32*)p); \
  84. if (!is_little_endian()) \
  85. exchange_##type((uint8*)&res); \
  86. p += sizeof(type); \
  87. } while (0)
  88. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  89. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  90. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  91. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  92. #define read_byte_array(p, p_end, addr, len) do { \
  93. CHECK_BUF(p, p_end, len); \
  94. memcpy(addr, p, len); \
  95. p += len; \
  96. } while (0)
  97. #define read_string(p, p_end, str) do { \
  98. uint16 str_len; \
  99. read_uint16(p, p_end, str_len); \
  100. CHECK_BUF(p, p_end, str_len); \
  101. if (!(str = const_str_set_insert \
  102. (p, str_len, module, \
  103. error_buf, error_buf_size))) { \
  104. goto fail; \
  105. } \
  106. p += str_len; \
  107. } while (0)
  108. /* Legal values for bin_type */
  109. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  110. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  111. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  112. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  113. /* Legal values for e_type (object file type). */
  114. #define E_TYPE_NONE 0 /* No file type */
  115. #define E_TYPE_REL 1 /* Relocatable file */
  116. #define E_TYPE_EXEC 2 /* Executable file */
  117. #define E_TYPE_DYN 3 /* Shared object file */
  118. /* Legal values for e_machine (architecture). */
  119. #define E_MACHINE_386 3 /* Intel 80386 */
  120. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  121. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  122. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  123. #define E_MACHINE_AARCH64 183 /* AArch64 */
  124. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  125. #define E_MACHINE_IA_64 50 /* Intel Merced */
  126. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  127. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  128. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  129. /* Legal values for e_version */
  130. #define E_VERSION_CURRENT 1 /* Current version */
  131. static char*
  132. const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  133. char* error_buf, uint32 error_buf_size)
  134. {
  135. HashMap *set = module->const_str_set;
  136. char *c_str = wasm_runtime_malloc((uint32)len + 1), *value;
  137. if (!c_str) {
  138. set_error_buf(error_buf, error_buf_size,
  139. "AOT module load failed: "
  140. "allocate memory failed.");
  141. return NULL;
  142. }
  143. bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
  144. c_str[len] = '\0';
  145. if ((value = bh_hash_map_find(set, c_str))) {
  146. wasm_runtime_free(c_str);
  147. return value;
  148. }
  149. if (!bh_hash_map_insert(set, c_str, c_str)) {
  150. set_error_buf(error_buf, error_buf_size,
  151. "AOT module load failed: "
  152. "insert string to hash map failed.");
  153. wasm_runtime_free(c_str);
  154. return NULL;
  155. }
  156. return c_str;
  157. }
  158. static bool
  159. get_aot_file_target(AOTTargetInfo *target_info,
  160. char *target_buf, uint32 target_buf_size,
  161. char *error_buf, uint32 error_buf_size)
  162. {
  163. char *machine_type = NULL;
  164. switch (target_info->e_machine) {
  165. case E_MACHINE_X86_64:
  166. machine_type = "x86_64";
  167. break;
  168. case E_MACHINE_386:
  169. machine_type = "i386";
  170. break;
  171. case E_MACHINE_ARM:
  172. case E_MACHINE_AARCH64:
  173. machine_type = target_info->arch;
  174. break;
  175. case E_MACHINE_MIPS:
  176. machine_type = "mips";
  177. break;
  178. case E_MACHINE_XTENSA:
  179. machine_type = "xtensa";
  180. break;
  181. default:
  182. if (error_buf)
  183. snprintf(error_buf, error_buf_size,
  184. "AOT module load failed: unknown machine type %d.",
  185. target_info->e_machine);
  186. return false;
  187. }
  188. if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
  189. if (error_buf)
  190. snprintf(error_buf, error_buf_size,
  191. "AOT module load failed: "
  192. "machine type (%s) isn't consistent with target type (%s).",
  193. machine_type, target_info->arch);
  194. return false;
  195. }
  196. snprintf(target_buf, target_buf_size, "%s", target_info->arch);
  197. return true;
  198. }
  199. static bool
  200. check_machine_info(AOTTargetInfo *target_info,
  201. char *error_buf, uint32 error_buf_size)
  202. {
  203. char target_expected[32], target_got[32];
  204. get_current_target(target_expected, sizeof(target_expected));
  205. if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
  206. error_buf, error_buf_size))
  207. return false;
  208. if (strcmp(target_expected, target_got)) {
  209. if (error_buf) {
  210. snprintf(error_buf, error_buf_size,
  211. "AOT module load failed: invalid target type, "
  212. "expected %s but got %s.",
  213. target_expected, target_got);
  214. }
  215. return false;
  216. }
  217. return true;
  218. }
  219. static bool
  220. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  221. AOTModule *module,
  222. char *error_buf, uint32 error_buf_size)
  223. {
  224. AOTTargetInfo target_info;
  225. const uint8 *p = buf, *p_end = buf_end;
  226. bool is_target_little_endian, is_target_64_bit;
  227. read_uint16(p, p_end, target_info.bin_type);
  228. read_uint16(p, p_end, target_info.abi_type);
  229. read_uint16(p, p_end, target_info.e_type);
  230. read_uint16(p, p_end, target_info.e_machine);
  231. read_uint32(p, p_end, target_info.e_version);
  232. read_uint32(p, p_end, target_info.e_flags);
  233. read_uint32(p, p_end, target_info.reserved);
  234. read_byte_array(p, p_end,
  235. target_info.arch, sizeof(target_info.arch));
  236. if (p != buf_end) {
  237. set_error_buf(error_buf, error_buf_size,
  238. "AOT module load failed: invalid section size.");
  239. return false;
  240. }
  241. /* Check target endian type */
  242. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  243. if (is_little_endian() != is_target_little_endian) {
  244. if (error_buf)
  245. snprintf(error_buf, error_buf_size,
  246. "AOT module load failed: "
  247. "invalid target endian type, expected %s but got %s.",
  248. is_little_endian() ? "little endian" : "big endian",
  249. is_target_little_endian ? "little endian" : "big endian");
  250. return false;
  251. }
  252. /* Check target bit width */
  253. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  254. if ((sizeof(void*) == 8 ? true : false) != is_target_64_bit) {
  255. if (error_buf)
  256. snprintf(error_buf, error_buf_size,
  257. "AOT module load failed: "
  258. "invalid target bit width, expected %s but got %s.",
  259. sizeof(void*) == 8 ? "64-bit" : "32-bit",
  260. is_target_64_bit ? "64-bit" : "32-bit");
  261. return false;
  262. }
  263. /* Check target elf file type */
  264. if (target_info.e_type != E_TYPE_REL) {
  265. set_error_buf(error_buf, error_buf_size,
  266. "AOT module load failed: invalid object file type, "
  267. "expected relocatable file type but got others.");
  268. return false;
  269. }
  270. /* Check machine info */
  271. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  272. return false;
  273. }
  274. if (target_info.e_version != E_VERSION_CURRENT) {
  275. set_error_buf(error_buf, error_buf_size,
  276. "AOT module load failed: invalid elf file version.");
  277. return false;
  278. }
  279. return true;
  280. fail:
  281. return false;
  282. }
  283. static void
  284. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
  285. bool is_jit_mode)
  286. {
  287. if (!is_jit_mode) {
  288. uint32 i;
  289. for (i = 0; i < count; i++)
  290. if (data_list[i])
  291. wasm_runtime_free(data_list[i]);
  292. wasm_runtime_free(data_list);
  293. }
  294. }
  295. static bool
  296. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  297. AOTModule *module,
  298. char *error_buf, uint32 error_buf_size)
  299. {
  300. const uint8 *buf = *p_buf;
  301. AOTMemInitData **data_list;
  302. uint64 size;
  303. uint32 i;
  304. /* Allocate memory */
  305. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  306. if (size >= UINT32_MAX
  307. || !(module->mem_init_data_list =
  308. data_list = wasm_runtime_malloc((uint32)size))) {
  309. set_error_buf(error_buf, error_buf_size,
  310. "AOT module load failed: "
  311. "allocate memory failed.");
  312. return false;
  313. }
  314. memset(data_list, 0, size);
  315. /* Create each memory data segment */
  316. for (i = 0; i < module->mem_init_data_count; i++) {
  317. uint32 init_expr_type, byte_count;
  318. uint64 init_expr_value;
  319. read_uint32(buf, buf_end, init_expr_type);
  320. read_uint64(buf, buf_end, init_expr_value);
  321. read_uint32(buf, buf_end, byte_count);
  322. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  323. if (size >= UINT32_MAX
  324. || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
  325. set_error_buf(error_buf, error_buf_size,
  326. "AOT module load failed: "
  327. "allocate memory failed.");
  328. return false;
  329. }
  330. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  331. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  332. data_list[i]->byte_count = byte_count;
  333. read_byte_array(buf, buf_end,
  334. data_list[i]->bytes, data_list[i]->byte_count);
  335. }
  336. *p_buf = buf;
  337. return true;
  338. fail:
  339. return false;
  340. }
  341. static bool
  342. load_memory_info(const uint8 **p_buf, const uint8 *buf_end,
  343. AOTModule *module,
  344. char *error_buf, uint32 error_buf_size)
  345. {
  346. const uint8 *buf = *p_buf;
  347. read_uint32(buf, buf_end, module->num_bytes_per_page);
  348. read_uint32(buf, buf_end, module->mem_init_page_count);
  349. read_uint32(buf, buf_end, module->mem_max_page_count);
  350. read_uint32(buf, buf_end, module->mem_init_data_count);
  351. /* load memory init data list */
  352. if (module->mem_init_data_count > 0
  353. && !load_mem_init_data_list(&buf, buf_end, module,
  354. error_buf, error_buf_size))
  355. return false;
  356. *p_buf = buf;
  357. return true;
  358. fail:
  359. return false;
  360. }
  361. static void
  362. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
  363. bool is_jit_mode)
  364. {
  365. if (!is_jit_mode) {
  366. uint32 i;
  367. for (i = 0; i < count; i++)
  368. if (data_list[i])
  369. wasm_runtime_free(data_list[i]);
  370. wasm_runtime_free(data_list);
  371. }
  372. }
  373. static bool
  374. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  375. AOTModule *module,
  376. char *error_buf, uint32 error_buf_size)
  377. {
  378. const uint8 *buf = *p_buf;
  379. AOTTableInitData **data_list;
  380. uint64 size;
  381. uint32 i;
  382. /* Allocate memory */
  383. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  384. if (size >= UINT32_MAX
  385. || !(module->table_init_data_list =
  386. data_list = wasm_runtime_malloc((uint32)size))) {
  387. set_error_buf(error_buf, error_buf_size,
  388. "AOT module load failed: "
  389. "allocate memory failed.");
  390. return false;
  391. }
  392. memset(data_list, 0, size);
  393. /* Create each table data segment */
  394. for (i = 0; i < module->table_init_data_count; i++) {
  395. uint32 init_expr_type, func_index_count;
  396. uint64 init_expr_value, size1;
  397. read_uint32(buf, buf_end, init_expr_type);
  398. read_uint64(buf, buf_end, init_expr_value);
  399. read_uint32(buf, buf_end, func_index_count);
  400. size1 = sizeof(uint32) * (uint64)func_index_count;
  401. size = offsetof(AOTTableInitData, func_indexes) + size1;
  402. if (size >= UINT32_MAX
  403. || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
  404. set_error_buf(error_buf, error_buf_size,
  405. "AOT module load failed: "
  406. "allocate memory failed.");
  407. return false;
  408. }
  409. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  410. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  411. data_list[i]->func_index_count = func_index_count;
  412. read_byte_array(buf, buf_end, data_list[i]->func_indexes, size1);
  413. }
  414. *p_buf = buf;
  415. return true;
  416. fail:
  417. return false;
  418. }
  419. static bool
  420. load_table_info(const uint8 **p_buf, const uint8 *buf_end,
  421. AOTModule *module,
  422. char *error_buf, uint32 error_buf_size)
  423. {
  424. const uint8 *buf = *p_buf;
  425. read_uint32(buf, buf_end, module->table_size);
  426. read_uint32(buf, buf_end, module->table_init_data_count);
  427. /* load table init data list */
  428. if (module->table_init_data_count > 0
  429. && !load_table_init_data_list(&buf, buf_end, module,
  430. error_buf, error_buf_size))
  431. return false;
  432. *p_buf = buf;
  433. return true;
  434. fail:
  435. return false;
  436. }
  437. static void
  438. destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
  439. {
  440. if (!is_jit_mode) {
  441. uint32 i;
  442. for (i = 0; i < count; i++)
  443. if (func_types[i])
  444. wasm_runtime_free(func_types[i]);
  445. wasm_runtime_free(func_types);
  446. }
  447. }
  448. static bool
  449. load_func_types(const uint8 **p_buf, const uint8 *buf_end,
  450. AOTModule *module,
  451. char *error_buf, uint32 error_buf_size)
  452. {
  453. const uint8 *buf = *p_buf;
  454. AOTFuncType **func_types;
  455. uint64 size;
  456. uint32 i;
  457. /* Allocate memory */
  458. size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
  459. if (size >= UINT32_MAX
  460. || !(module->func_types = func_types = wasm_runtime_malloc((uint32)size))) {
  461. set_error_buf(error_buf, error_buf_size,
  462. "AOT module load failed: "
  463. "allocate memory failed.");
  464. return false;
  465. }
  466. memset(func_types, 0, size);
  467. /* Create each function type */
  468. for (i = 0; i < module->func_type_count; i++) {
  469. uint32 param_count, result_count;
  470. uint64 size1;
  471. read_uint32(buf, buf_end, param_count);
  472. read_uint32(buf, buf_end, result_count);
  473. size1 = (uint64)param_count + (uint64)result_count;
  474. size = offsetof(AOTFuncType, types) + size1;
  475. if (size >= UINT32_MAX
  476. || !(func_types[i] = wasm_runtime_malloc((uint32)size))) {
  477. set_error_buf(error_buf, error_buf_size,
  478. "AOT module load failed: "
  479. "allocate memory failed.");
  480. return false;
  481. }
  482. func_types[i]->param_count = param_count;
  483. func_types[i]->result_count = result_count;
  484. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  485. }
  486. *p_buf = buf;
  487. return true;
  488. fail:
  489. return false;
  490. }
  491. static bool
  492. load_func_type_info(const uint8 **p_buf, const uint8 *buf_end,
  493. AOTModule *module,
  494. char *error_buf, uint32 error_buf_size)
  495. {
  496. const uint8 *buf = *p_buf;
  497. read_uint32(buf, buf_end, module->func_type_count);
  498. /* load function type */
  499. if (module->func_type_count > 0
  500. && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size))
  501. return false;
  502. *p_buf = buf;
  503. return true;
  504. fail:
  505. return false;
  506. }
  507. static void
  508. destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
  509. {
  510. if (!is_jit_mode)
  511. wasm_runtime_free(import_globals);
  512. }
  513. static bool
  514. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  515. AOTModule *module,
  516. char *error_buf, uint32 error_buf_size)
  517. {
  518. const uint8 *buf = *p_buf;
  519. AOTImportGlobal *import_globals;
  520. uint64 size;
  521. uint32 i, data_offset = 0;
  522. /* Allocate memory */
  523. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  524. if (size >= UINT32_MAX
  525. || !(module->import_globals =
  526. import_globals = wasm_runtime_malloc((uint32)size))) {
  527. set_error_buf(error_buf, error_buf_size,
  528. "AOT module load failed: "
  529. "allocate memory failed.");
  530. return false;
  531. }
  532. memset(import_globals, 0, size);
  533. /* Create each import global */
  534. for (i = 0; i < module->import_global_count; i++) {
  535. buf = (uint8*)align_ptr(buf, 2);
  536. read_uint8(buf, buf_end, import_globals[i].type);
  537. read_uint8(buf, buf_end, import_globals[i].is_mutable);
  538. read_string(buf, buf_end, import_globals[i].module_name);
  539. read_string(buf, buf_end, import_globals[i].global_name);
  540. import_globals[i].size = wasm_value_type_size(import_globals[i].type);
  541. import_globals[i].data_offset = data_offset;
  542. data_offset += import_globals[i].size;
  543. module->global_data_size += import_globals[i].size;
  544. }
  545. *p_buf = buf;
  546. return true;
  547. fail:
  548. return false;
  549. }
  550. static bool
  551. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  552. AOTModule *module,
  553. char *error_buf, uint32 error_buf_size)
  554. {
  555. const uint8 *buf = *p_buf;
  556. read_uint32(buf, buf_end, module->import_global_count);
  557. /* load import globals */
  558. if (module->import_global_count > 0
  559. && !load_import_globals(&buf, buf_end, module,
  560. error_buf, error_buf_size))
  561. return false;
  562. *p_buf = buf;
  563. return true;
  564. fail:
  565. return false;
  566. }
  567. static void
  568. destroy_globals(AOTGlobal *globals, bool is_jit_mode)
  569. {
  570. if (!is_jit_mode)
  571. wasm_runtime_free(globals);
  572. }
  573. static bool
  574. load_globals(const uint8 **p_buf, const uint8 *buf_end,
  575. AOTModule *module,
  576. char *error_buf, uint32 error_buf_size)
  577. {
  578. const uint8 *buf = *p_buf;
  579. AOTGlobal *globals;
  580. uint64 size;
  581. uint32 i, data_offset = 0;
  582. AOTImportGlobal *last_import_global;
  583. /* Allocate memory */
  584. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  585. if (size >= UINT32_MAX
  586. || !(module->globals = globals = wasm_runtime_malloc((uint32)size))) {
  587. set_error_buf(error_buf, error_buf_size,
  588. "AOT module load failed: "
  589. "allocate memory failed.");
  590. return false;
  591. }
  592. memset(globals, 0, size);
  593. if (module->import_global_count > 0) {
  594. last_import_global =
  595. &module->import_globals[module->import_global_count - 1];
  596. data_offset = last_import_global->data_offset
  597. + last_import_global->size;
  598. }
  599. /* Create each global */
  600. for (i = 0; i < module->global_count; i++) {
  601. uint16 init_expr_type;
  602. uint64 init_expr_value;
  603. read_uint8(buf, buf_end, globals[i].type);
  604. read_uint8(buf, buf_end, globals[i].is_mutable);
  605. read_uint16(buf, buf_end, init_expr_type);
  606. read_uint64(buf, buf_end, init_expr_value);
  607. globals[i].init_expr.init_expr_type = (uint8)init_expr_type;
  608. globals[i].init_expr.u.i64 = (int64)init_expr_value;
  609. globals[i].size = wasm_value_type_size(globals[i].type);
  610. globals[i].data_offset = data_offset;
  611. data_offset += globals[i].size;
  612. module->global_data_size += globals[i].size;
  613. }
  614. *p_buf = buf;
  615. return true;
  616. fail:
  617. return false;
  618. }
  619. static bool
  620. load_global_info(const uint8 **p_buf, const uint8 *buf_end,
  621. AOTModule *module,
  622. char *error_buf, uint32 error_buf_size)
  623. {
  624. const uint8 *buf = *p_buf;
  625. read_uint32(buf, buf_end, module->global_count);
  626. /* load globals */
  627. if (module->global_count > 0
  628. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  629. return false;
  630. *p_buf = buf;
  631. return true;
  632. fail:
  633. return false;
  634. }
  635. static void
  636. destroy_import_funcs(AOTImportFunc *import_funcs,
  637. bool is_jit_mode)
  638. {
  639. if (!is_jit_mode)
  640. wasm_runtime_free(import_funcs);
  641. }
  642. static bool
  643. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
  644. AOTModule *module,
  645. char *error_buf, uint32 error_buf_size)
  646. {
  647. const char *module_name, *field_name;
  648. const uint8 *buf = *p_buf;
  649. AOTImportFunc *import_funcs;
  650. uint64 size;
  651. uint32 i;
  652. /* Allocate memory */
  653. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  654. if (size >= UINT32_MAX
  655. || !(module->import_funcs =
  656. import_funcs = wasm_runtime_malloc((uint32)size))) {
  657. set_error_buf(error_buf, error_buf_size,
  658. "AOT module load failed: "
  659. "allocate memory failed.");
  660. return false;
  661. }
  662. memset(import_funcs, 0, size);
  663. /* Create each import func */
  664. for (i = 0; i < module->import_func_count; i++) {
  665. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  666. if (import_funcs[i].func_type_index >= module->func_type_count) {
  667. set_error_buf(error_buf, error_buf_size,
  668. "AOT module load failed: "
  669. "invalid function type index.");
  670. return false;
  671. }
  672. import_funcs[i].func_type = module->func_types[import_funcs[i].func_type_index];
  673. read_string(buf, buf_end, import_funcs[i].module_name);
  674. read_string(buf, buf_end, import_funcs[i].func_name);
  675. module_name = import_funcs[i].module_name;
  676. field_name = import_funcs[i].func_name;
  677. if (!(import_funcs[i].func_ptr_linked =
  678. wasm_native_resolve_symbol(module_name, field_name,
  679. import_funcs[i].func_type,
  680. &import_funcs[i].signature))) {
  681. LOG_WARNING("warning: fail to link import function (%s, %s)\n",
  682. module_name, field_name);
  683. }
  684. #if WASM_ENABLE_LIBC_WASI != 0
  685. if (!strcmp(import_funcs[i].module_name, "wasi_unstable"))
  686. module->is_wasi_module = true;
  687. #endif
  688. }
  689. *p_buf = buf;
  690. return true;
  691. fail:
  692. return false;
  693. }
  694. static bool
  695. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  696. AOTModule *module,
  697. char *error_buf, uint32 error_buf_size)
  698. {
  699. const uint8 *buf = *p_buf;
  700. read_uint32(buf, buf_end, module->import_func_count);
  701. /* load import funcs */
  702. if (module->import_func_count > 0
  703. && !load_import_funcs(&buf, buf_end, module,
  704. error_buf, error_buf_size))
  705. return false;
  706. *p_buf = buf;
  707. return true;
  708. fail:
  709. return false;
  710. }
  711. static void
  712. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  713. uint32 data_section_count)
  714. {
  715. uint32 i;
  716. AOTObjectDataSection *data_section = data_sections;
  717. for (i = 0; i < data_section_count; i++, data_section++)
  718. if (data_section->data)
  719. os_munmap(data_section->data, data_section->size);
  720. wasm_runtime_free(data_sections);
  721. }
  722. static bool
  723. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  724. AOTModule *module,
  725. char *error_buf, uint32 error_buf_size)
  726. {
  727. const uint8 *buf = *p_buf;
  728. AOTObjectDataSection *data_sections;
  729. uint64 size;
  730. uint32 i;
  731. /* Allocate memory */
  732. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  733. if (size >= UINT32_MAX
  734. || !(module->data_sections =
  735. data_sections = wasm_runtime_malloc((uint32)size))) {
  736. set_error_buf(error_buf, error_buf_size,
  737. "AOT module load failed: "
  738. "allocate memory failed.");
  739. return false;
  740. }
  741. memset(data_sections, 0, size);
  742. /* Create each data section */
  743. for (i = 0; i < module->data_section_count; i++) {
  744. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
  745. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  746. /* aot code and data in x86_64 must be in range 0 to 2G due to
  747. relocation for R_X86_64_32/32S/PC32 */
  748. int map_flags = MMAP_MAP_32BIT;
  749. #else
  750. int map_flags = MMAP_MAP_NONE;
  751. #endif
  752. read_string(buf, buf_end, data_sections[i].name);
  753. read_uint32(buf, buf_end, data_sections[i].size);
  754. /* Allocate memory for data */
  755. if (!(data_sections[i].data =
  756. os_mmap(NULL, data_sections[i].size, map_prot, map_flags))) {
  757. set_error_buf(error_buf, error_buf_size,
  758. "AOT module load failed: "
  759. "allocate memory failed.");
  760. return false;
  761. }
  762. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  763. /* address must be in the first 2 Gigabytes of
  764. the process address space */
  765. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  766. #endif
  767. read_byte_array(buf, buf_end,
  768. data_sections[i].data, data_sections[i].size);
  769. }
  770. *p_buf = buf;
  771. return true;
  772. fail:
  773. return false;
  774. }
  775. static bool
  776. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  777. AOTModule *module,
  778. char *error_buf, uint32 error_buf_size)
  779. {
  780. const uint8 *buf = *p_buf;
  781. read_uint32(buf, buf_end, module->data_section_count);
  782. /* load object data sections */
  783. if (module->data_section_count > 0
  784. && !load_object_data_sections(&buf, buf_end, module,
  785. error_buf, error_buf_size))
  786. return false;
  787. *p_buf = buf;
  788. return true;
  789. fail:
  790. return false;
  791. }
  792. static bool
  793. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  794. AOTModule *module,
  795. char *error_buf, uint32 error_buf_size)
  796. {
  797. const uint8 *p = buf, *p_end = buf_end;
  798. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  799. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  800. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  801. || !load_import_global_info(&p, p_end, module, error_buf, error_buf_size)
  802. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  803. || !load_import_func_info(&p, p_end, module, error_buf, error_buf_size))
  804. return false;
  805. /* load function count and start function index */
  806. read_uint32(p, p_end, module->func_count);
  807. read_uint32(p, p_end, module->start_func_index);
  808. /* check start function index */
  809. if (module->start_func_index != (uint32)-1
  810. && (module->start_func_index < module->import_func_count
  811. || module->start_func_index >= module->import_func_count
  812. + module->func_count)) {
  813. set_error_buf(error_buf, error_buf_size,
  814. "AOT module load failed: "
  815. "invalid start function index");
  816. return false;
  817. }
  818. read_uint32(p, p_end, module->llvm_aux_data_end);
  819. read_uint32(p, p_end, module->llvm_aux_stack_bottom);
  820. read_uint32(p, p_end, module->llvm_aux_stack_size);
  821. read_uint32(p, p_end, module->llvm_aux_stack_global_index);
  822. if (!load_object_data_sections_info(&p, p_end, module,
  823. error_buf, error_buf_size))
  824. return false;
  825. if (p != p_end) {
  826. set_error_buf(error_buf, error_buf_size,
  827. "AOT module load failed: "
  828. "invalid init data section size");
  829. return false;
  830. }
  831. return true;
  832. fail:
  833. return false;
  834. }
  835. static bool
  836. load_text_section(const uint8 *buf, const uint8 *buf_end,
  837. AOTModule *module,
  838. char *error_buf, uint32 error_buf_size)
  839. {
  840. uint8 *plt_base;
  841. if (module->func_count > 0 && buf_end == buf) {
  842. set_error_buf(error_buf, error_buf_size,
  843. "AOT module load failed: invalid code size.");
  844. return false;
  845. }
  846. module->code = (void*)buf;
  847. module->code_size = (uint32)(buf_end - buf);
  848. if (module->code_size > 0) {
  849. plt_base = (uint8*)buf_end - get_plt_table_size();
  850. init_plt_table(plt_base);
  851. }
  852. return true;
  853. }
  854. static bool
  855. load_function_section(const uint8 *buf, const uint8 *buf_end,
  856. AOTModule *module,
  857. char *error_buf, uint32 error_buf_size)
  858. {
  859. const uint8 *p = buf, *p_end = buf_end;
  860. uint32 i;
  861. uint64 size, text_offset;
  862. size = sizeof(void*) * (uint64)module->func_count;
  863. if (size >= UINT32_MAX
  864. || !(module->func_ptrs = wasm_runtime_malloc((uint32)size))) {
  865. set_error_buf(error_buf, error_buf_size,
  866. "AOT module load failed: allocate memory failed.");
  867. return false;
  868. }
  869. for (i = 0; i < module->func_count; i++) {
  870. if (sizeof(void*) == 8) {
  871. read_uint64(p, p_end, text_offset);
  872. }
  873. else {
  874. uint32 text_offset32;
  875. read_uint32(p, p_end, text_offset32);
  876. text_offset = text_offset32;
  877. }
  878. if (text_offset >= module->code_size) {
  879. set_error_buf(error_buf, error_buf_size,
  880. "AOT module load failed: "
  881. "invalid function code offset.");
  882. return false;
  883. }
  884. module->func_ptrs[i] = (uint8*)module->code + text_offset;
  885. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  886. /* bits[0] of thumb function address must be 1 */
  887. module->func_ptrs[i] = (void*)((uintptr_t)module->func_ptrs[i] | 1);
  888. #endif
  889. }
  890. /* Set start function when function pointers are resolved */
  891. if (module->start_func_index != (uint32)-1) {
  892. module->start_function =
  893. module->func_ptrs[module->start_func_index
  894. - module->import_func_count];
  895. }
  896. else {
  897. module->start_function = NULL;
  898. }
  899. size = sizeof(uint32) * (uint64)module->func_count;
  900. if (size >= UINT32_MAX
  901. || !(module->func_type_indexes = wasm_runtime_malloc((uint32)size))) {
  902. set_error_buf(error_buf, error_buf_size,
  903. "AOT module load failed: allocate memory failed.");
  904. return false;
  905. }
  906. for (i = 0; i < module->func_count; i++) {
  907. read_uint32(p, p_end, module->func_type_indexes[i]);
  908. if (module->func_type_indexes[i] >= module->func_type_count) {
  909. set_error_buf(error_buf, error_buf_size,
  910. "AOT module load failed: "
  911. "invalid function type index.");
  912. return false;
  913. }
  914. }
  915. if (p != buf_end) {
  916. set_error_buf(error_buf, error_buf_size,
  917. "AOT module load failed: "
  918. "invalid function section size");
  919. return false;
  920. }
  921. return true;
  922. fail:
  923. return false;
  924. }
  925. static void
  926. destroy_export_funcs(AOTExportFunc *export_funcs, bool is_jit_mode)
  927. {
  928. if (!is_jit_mode)
  929. wasm_runtime_free(export_funcs);
  930. }
  931. static bool
  932. load_export_funcs(const uint8 **p_buf, const uint8 *buf_end,
  933. AOTModule *module,
  934. char *error_buf, uint32 error_buf_size)
  935. {
  936. const uint8 *buf = *p_buf;
  937. AOTExportFunc *export_funcs;
  938. uint64 size;
  939. uint32 i;
  940. /* Allocate memory */
  941. size = sizeof(AOTExportFunc) * (uint64)module->export_func_count;
  942. if (size >= UINT32_MAX
  943. || !(module->export_funcs =
  944. export_funcs = wasm_runtime_malloc((uint32)size))) {
  945. set_error_buf(error_buf, error_buf_size,
  946. "AOT module load failed: "
  947. "allocate memory failed.");
  948. return false;
  949. }
  950. memset(export_funcs, 0, size);
  951. /* Create each export func */
  952. for (i = 0; i < module->export_func_count; i++) {
  953. read_uint32(buf, buf_end, export_funcs[i].func_index);
  954. if (export_funcs[i].func_index >=
  955. module->func_count + module->import_func_count) {
  956. set_error_buf(error_buf, error_buf_size,
  957. "AOT module load failed: "
  958. "function index is out of range.");
  959. return false;
  960. }
  961. read_string(buf, buf_end, export_funcs[i].func_name);
  962. }
  963. *p_buf = buf;
  964. return true;
  965. fail:
  966. return false;
  967. }
  968. static bool
  969. load_export_section(const uint8 *buf, const uint8 *buf_end,
  970. AOTModule *module,
  971. char *error_buf, uint32 error_buf_size)
  972. {
  973. const uint8 *p = buf, *p_end = buf_end;
  974. /* load export functions */
  975. read_uint32(p, p_end, module->export_func_count);
  976. if (module->export_func_count > 0
  977. && !load_export_funcs(&p, p_end, module, error_buf, error_buf_size))
  978. return false;
  979. if (p != p_end) {
  980. set_error_buf(error_buf, error_buf_size,
  981. "AOT module load failed: "
  982. "invalid export section size");
  983. return false;
  984. }
  985. return true;
  986. fail:
  987. return false;
  988. }
  989. static void *
  990. get_data_section_addr(AOTModule *module, const char *section_name,
  991. uint32 *p_data_size)
  992. {
  993. uint32 i;
  994. AOTObjectDataSection *data_section = module->data_sections;
  995. for (i = 0; i < module->data_section_count; i++, data_section++)
  996. if (!strcmp(data_section->name, section_name)) {
  997. if (p_data_size)
  998. *p_data_size = data_section->size;
  999. return data_section->data;
  1000. }
  1001. return NULL;
  1002. }
  1003. static void *
  1004. resolve_target_sym(const char *symbol, int32 *p_index)
  1005. {
  1006. uint32 i, num = 0;
  1007. SymbolMap *target_sym_map;
  1008. if (!(target_sym_map = get_target_symbol_map(&num)))
  1009. return NULL;
  1010. for (i = 0; i < num; i++)
  1011. if (!strcmp(target_sym_map[i].symbol_name, symbol)) {
  1012. *p_index = (int32)i;
  1013. return target_sym_map[i].symbol_addr;
  1014. }
  1015. return NULL;
  1016. }
  1017. static bool
  1018. do_text_relocation(AOTModule *module,
  1019. AOTRelocationGroup *group,
  1020. char *error_buf, uint32 error_buf_size)
  1021. {
  1022. uint8 *aot_text = module->code;
  1023. uint32 aot_text_size = module->code_size;
  1024. uint32 i, func_index, symbol_len;
  1025. char symbol_buf[128] = { 0 }, *symbol, *p;
  1026. void *symbol_addr;
  1027. AOTRelocation *relocation = group->relocations;
  1028. if (group->relocation_count > 0 && !aot_text) {
  1029. set_error_buf(error_buf, error_buf_size,
  1030. "AOT module load failed: invalid text relocation count.");
  1031. return false;
  1032. }
  1033. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1034. int32 symbol_index = -1;
  1035. symbol_len = (uint32)strlen(relocation->symbol_name);
  1036. if (symbol_len + 1 <= sizeof(symbol_buf))
  1037. symbol = symbol_buf;
  1038. else {
  1039. if (!(symbol = wasm_runtime_malloc(symbol_len + 1))) {
  1040. set_error_buf(error_buf, error_buf_size,
  1041. "AOT module load failed: "
  1042. "allocate memory failed.");
  1043. return false;
  1044. }
  1045. }
  1046. memcpy(symbol, relocation->symbol_name, symbol_len);
  1047. symbol[symbol_len] = '\0';
  1048. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1049. p = symbol + strlen(AOT_FUNC_PREFIX);
  1050. if (*p == '\0'
  1051. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1052. if (error_buf != NULL)
  1053. snprintf(error_buf, error_buf_size,
  1054. "AOT module load failed: "
  1055. "invalid import symbol %s.",
  1056. symbol);
  1057. goto check_symbol_fail;
  1058. }
  1059. symbol_addr = module->func_ptrs[func_index];
  1060. }
  1061. else if (!strcmp(symbol, ".text")) {
  1062. symbol_addr = module->code;
  1063. }
  1064. else if (!strcmp(symbol, ".data")
  1065. || !strcmp(symbol, ".rodata")
  1066. /* ".rodata.cst4/8/16/.." */
  1067. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) {
  1068. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1069. if (!symbol_addr) {
  1070. if (error_buf != NULL)
  1071. snprintf(error_buf, error_buf_size,
  1072. "AOT module load failed: "
  1073. "invalid data section (%s).",
  1074. symbol);
  1075. goto check_symbol_fail;
  1076. }
  1077. }
  1078. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1079. if (error_buf != NULL)
  1080. snprintf(error_buf, error_buf_size,
  1081. "AOT module load failed: "
  1082. "resolve symbol %s failed.",
  1083. symbol);
  1084. goto check_symbol_fail;
  1085. }
  1086. if (symbol != symbol_buf)
  1087. wasm_runtime_free(symbol);
  1088. if (!apply_relocation(module,
  1089. aot_text, aot_text_size,
  1090. relocation->relocation_offset,
  1091. relocation->relocation_addend,
  1092. relocation->relocation_type,
  1093. symbol_addr, symbol_index,
  1094. error_buf, error_buf_size))
  1095. return false;
  1096. }
  1097. return true;
  1098. check_symbol_fail:
  1099. if (symbol != symbol_buf)
  1100. wasm_runtime_free(symbol);
  1101. return false;
  1102. }
  1103. static bool
  1104. do_data_relocation(AOTModule *module,
  1105. AOTRelocationGroup *group,
  1106. char *error_buf, uint32 error_buf_size)
  1107. {
  1108. uint8 *data_addr;
  1109. uint32 data_size = 0, i;
  1110. AOTRelocation *relocation = group->relocations;
  1111. void *symbol_addr;
  1112. char *symbol, *data_section_name;
  1113. if (!strncmp(group->section_name, ".rela.", 6)) {
  1114. data_section_name = group->section_name + strlen(".rela");
  1115. }
  1116. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1117. data_section_name = group->section_name + strlen(".rel");
  1118. }
  1119. else {
  1120. set_error_buf(error_buf, error_buf_size,
  1121. "AOT module load failed: "
  1122. "invalid data relocation section name.");
  1123. return false;
  1124. }
  1125. data_addr = get_data_section_addr(module, data_section_name,
  1126. &data_size);
  1127. if (group->relocation_count > 0 && !data_addr) {
  1128. set_error_buf(error_buf, error_buf_size,
  1129. "AOT module load failed: invalid data relocation count.");
  1130. return false;
  1131. }
  1132. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1133. symbol = relocation->symbol_name;
  1134. if (!strcmp(symbol, ".text")) {
  1135. symbol_addr = module->code;
  1136. }
  1137. else {
  1138. if (error_buf != NULL)
  1139. snprintf(error_buf, error_buf_size,
  1140. "AOT module load failed: "
  1141. "invalid relocation symbol %s.",
  1142. symbol);
  1143. return false;
  1144. }
  1145. if (!apply_relocation(module,
  1146. data_addr, data_size,
  1147. relocation->relocation_offset,
  1148. relocation->relocation_addend,
  1149. relocation->relocation_type,
  1150. symbol_addr, -1,
  1151. error_buf, error_buf_size))
  1152. return false;
  1153. }
  1154. return true;
  1155. }
  1156. static bool
  1157. validate_symbol_table(uint8 *buf, uint8 *buf_end,
  1158. uint32 *offsets, uint32 count,
  1159. char *error_buf, uint32 error_buf_size)
  1160. {
  1161. uint32 i, str_len_addr = 0;
  1162. uint16 str_len;
  1163. for (i = 0; i < count; i++) {
  1164. if (offsets[i] != str_len_addr)
  1165. return false;
  1166. read_uint16(buf, buf_end, str_len);
  1167. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1168. str_len_addr = align_uint(str_len_addr, 2);
  1169. buf += str_len;
  1170. buf = (uint8*)align_ptr(buf, 2);
  1171. }
  1172. if (buf == buf_end)
  1173. return true;
  1174. fail:
  1175. return false;
  1176. }
  1177. static bool
  1178. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1179. AOTModule *module,
  1180. char *error_buf, uint32 error_buf_size)
  1181. {
  1182. AOTRelocationGroup *groups = NULL, *group;
  1183. uint32 symbol_count = 0;
  1184. uint32 group_count = 0, i, j, func_index, func_type_index;
  1185. uint64 size;
  1186. uint32 *symbol_offsets, total_string_len;
  1187. uint8 *symbol_buf, *symbol_buf_end;
  1188. bool ret = false;
  1189. AOTExportFunc *export_func;
  1190. read_uint32(buf, buf_end, symbol_count);
  1191. symbol_offsets = (uint32 *)buf;
  1192. for (i = 0; i < symbol_count; i++) {
  1193. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1194. buf += sizeof(uint32);
  1195. }
  1196. read_uint32(buf, buf_end, total_string_len);
  1197. symbol_buf = (uint8 *)buf;
  1198. symbol_buf_end = symbol_buf + total_string_len;
  1199. if (!validate_symbol_table(symbol_buf, symbol_buf_end,
  1200. symbol_offsets, symbol_count,
  1201. error_buf, error_buf_size)) {
  1202. set_error_buf(error_buf, error_buf_size,
  1203. "AOT module load failed: "
  1204. "validate symbol table failed.");
  1205. goto fail;
  1206. }
  1207. buf = symbol_buf_end;
  1208. read_uint32(buf, buf_end, group_count);
  1209. /* Allocate memory for relocation groups */
  1210. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1211. if (size >= UINT32_MAX || !(groups = wasm_runtime_malloc((uint32)size))) {
  1212. set_error_buf(error_buf, error_buf_size,
  1213. "AOT module load failed: "
  1214. "allocate memory failed.");
  1215. goto fail;
  1216. }
  1217. memset(groups, 0, size);
  1218. /* Load each relocation group */
  1219. for (i = 0, group = groups; i < group_count; i++, group++) {
  1220. AOTRelocation *relocation;
  1221. uint32 name_index;
  1222. uint16 str_len;
  1223. uint8 *name_addr;
  1224. /* section name address is 4 bytes aligned. */
  1225. buf = (uint8*)align_ptr(buf, sizeof(uint32));
  1226. read_uint32(buf, buf_end, name_index);
  1227. if (name_index >= symbol_count) {
  1228. set_error_buf(error_buf, error_buf_size,
  1229. "AOT module load failed: "
  1230. "symbol index out of range.");
  1231. goto fail;
  1232. }
  1233. name_addr = symbol_buf + symbol_offsets[name_index];
  1234. str_len = *(uint16 *)name_addr;
  1235. if (!(group->section_name =
  1236. const_str_set_insert(name_addr + sizeof(uint16),
  1237. (int32)str_len, module,
  1238. error_buf, error_buf_size))) {
  1239. goto fail;
  1240. }
  1241. read_uint32(buf, buf_end, group->relocation_count);
  1242. /* Allocate memory for relocations */
  1243. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1244. if (size >= UINT32_MAX
  1245. || !(group->relocations = relocation =
  1246. wasm_runtime_malloc((uint32)size))) {
  1247. set_error_buf(error_buf, error_buf_size,
  1248. "AOT module load failed: "
  1249. "allocate memory failed.");
  1250. ret = false;
  1251. goto fail;
  1252. }
  1253. memset(group->relocations, 0, size);
  1254. /* Load each relocation */
  1255. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1256. uint32 symbol_index;
  1257. uint16 str_len;
  1258. uint8 *symbol_addr;
  1259. if (sizeof(void *) == 8) {
  1260. read_uint64(buf, buf_end, relocation->relocation_offset);
  1261. read_uint64(buf, buf_end, relocation->relocation_addend);
  1262. }
  1263. else {
  1264. uint32 offset32, addend32;
  1265. read_uint32(buf, buf_end, offset32);
  1266. relocation->relocation_offset = (uint64)offset32;
  1267. read_uint32(buf, buf_end, addend32);
  1268. relocation->relocation_addend = (uint64)addend32;
  1269. }
  1270. read_uint32(buf, buf_end, relocation->relocation_type);
  1271. read_uint32(buf, buf_end, symbol_index);
  1272. if (symbol_index >= symbol_count) {
  1273. set_error_buf(error_buf, error_buf_size,
  1274. "AOT module load failed: "
  1275. "symbol index out of range.");
  1276. goto fail;
  1277. }
  1278. symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  1279. str_len = *(uint16 *)symbol_addr;
  1280. if (!(relocation->symbol_name =
  1281. const_str_set_insert(symbol_addr + sizeof(uint16),
  1282. (int32)str_len, module,
  1283. error_buf, error_buf_size))) {
  1284. goto fail;
  1285. }
  1286. }
  1287. if (!strcmp(group->section_name, ".rel.text")
  1288. || !strcmp(group->section_name, ".rela.text")) {
  1289. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  1290. return false;
  1291. }
  1292. else {
  1293. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  1294. return false;
  1295. }
  1296. }
  1297. export_func = module->export_funcs;
  1298. for (i = 0; i < module->export_func_count; i++, export_func++) {
  1299. func_index = export_func->func_index - module->import_func_count;
  1300. if (func_index >= module->func_count) {
  1301. set_error_buf(error_buf, error_buf_size,
  1302. "AOT module load failed: "
  1303. "invalid export function index.");
  1304. ret = false;
  1305. goto fail;
  1306. }
  1307. func_type_index = module->func_type_indexes[func_index];
  1308. export_func->func_type = module->func_types[func_type_index];
  1309. export_func->func_ptr = module->func_ptrs[func_index];
  1310. }
  1311. ret = true;
  1312. fail:
  1313. if (groups) {
  1314. for (i = 0, group = groups; i < group_count; i++, group++)
  1315. if (group->relocations)
  1316. wasm_runtime_free(group->relocations);
  1317. wasm_runtime_free(groups);
  1318. }
  1319. return ret;
  1320. }
  1321. static bool
  1322. load_from_sections(AOTModule *module, AOTSection *sections,
  1323. char *error_buf, uint32 error_buf_size)
  1324. {
  1325. AOTSection *section = sections;
  1326. const uint8 *buf, *buf_end;
  1327. uint32 last_section_type = (uint32)-1, section_type;
  1328. while (section) {
  1329. buf = section->section_body;
  1330. buf_end = buf + section->section_body_size;
  1331. /* Check sections */
  1332. section_type = (uint32)section->section_type;
  1333. if ((last_section_type == (uint32)-1
  1334. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  1335. || (last_section_type != (uint32)-1
  1336. && section_type != last_section_type + 1)) {
  1337. set_error_buf(error_buf, error_buf_size,
  1338. "AOT module load failed: invalid section order.");
  1339. return false;
  1340. }
  1341. last_section_type = section_type;
  1342. switch (section_type) {
  1343. case AOT_SECTION_TYPE_TARGET_INFO:
  1344. if (!load_target_info_section(buf, buf_end, module,
  1345. error_buf, error_buf_size))
  1346. return false;
  1347. break;
  1348. case AOT_SECTION_TYPE_INIT_DATA:
  1349. if (!load_init_data_section(buf, buf_end, module,
  1350. error_buf, error_buf_size))
  1351. return false;
  1352. break;
  1353. case AOT_SECTION_TYPE_TEXT:
  1354. if (!load_text_section(buf, buf_end, module,
  1355. error_buf, error_buf_size))
  1356. return false;
  1357. break;
  1358. case AOT_SECTION_TYPE_FUNCTION:
  1359. if (!load_function_section(buf, buf_end, module,
  1360. error_buf, error_buf_size))
  1361. return false;
  1362. break;
  1363. case AOT_SECTION_TYPE_EXPORT:
  1364. if (!load_export_section(buf, buf_end, module,
  1365. error_buf, error_buf_size))
  1366. return false;
  1367. break;
  1368. case AOT_SECTION_TYPE_RELOCATION:
  1369. if (!load_relocation_section(buf, buf_end, module,
  1370. error_buf, error_buf_size))
  1371. return false;
  1372. break;
  1373. }
  1374. section = section->next;
  1375. }
  1376. if (last_section_type != AOT_SECTION_TYPE_RELOCATION) {
  1377. set_error_buf(error_buf, error_buf_size,
  1378. "AOT module load failed: section missing.");
  1379. return false;
  1380. }
  1381. /* Flush data cache before executing AOT code,
  1382. * otherwise unpredictable behavior can occur. */
  1383. os_dcache_flush();
  1384. return true;
  1385. }
  1386. #if BH_ENABLE_MEMORY_PROFILING != 0
  1387. static void aot_free(void *ptr)
  1388. {
  1389. wasm_runtime_free(ptr);
  1390. }
  1391. #else
  1392. #define aot_free wasm_runtime_free
  1393. #endif
  1394. static AOTModule*
  1395. create_module(char *error_buf, uint32 error_buf_size)
  1396. {
  1397. AOTModule *module = wasm_runtime_malloc(sizeof(AOTModule));
  1398. if (!module) {
  1399. set_error_buf(error_buf, error_buf_size,
  1400. "AOT module load failed: "
  1401. "allocate memory failed.");
  1402. return NULL;
  1403. }
  1404. memset(module, 0, sizeof(AOTModule));
  1405. module->module_type = Wasm_Module_AoT;
  1406. if (!(module->const_str_set =
  1407. bh_hash_map_create(32, false,
  1408. (HashFunc)wasm_string_hash,
  1409. (KeyEqualFunc)wasm_string_equal,
  1410. NULL,
  1411. aot_free))) {
  1412. set_error_buf(error_buf, error_buf_size,
  1413. "AOT module load failed: "
  1414. "create const string set failed.");
  1415. wasm_runtime_free(module);
  1416. return NULL;
  1417. }
  1418. return module;
  1419. }
  1420. AOTModule*
  1421. aot_load_from_sections(AOTSection *section_list,
  1422. char *error_buf, uint32 error_buf_size)
  1423. {
  1424. AOTModule *module = create_module(error_buf, error_buf_size);
  1425. if (!module)
  1426. return NULL;
  1427. if (!load_from_sections(module, section_list,
  1428. error_buf, error_buf_size)) {
  1429. aot_unload(module);
  1430. return NULL;
  1431. }
  1432. LOG_VERBOSE("Load module from sections success.\n");
  1433. return module;
  1434. }
  1435. static void
  1436. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  1437. {
  1438. AOTSection *section = section_list, *next;
  1439. while (section) {
  1440. next = section->next;
  1441. if (destroy_aot_text
  1442. && section->section_type == AOT_SECTION_TYPE_TEXT
  1443. && section->section_body)
  1444. os_munmap((uint8*)section->section_body, section->section_body_size);
  1445. wasm_runtime_free(section);
  1446. section = next;
  1447. }
  1448. }
  1449. static bool
  1450. create_sections(const uint8 *buf, uint32 size,
  1451. AOTSection **p_section_list,
  1452. char *error_buf, uint32 error_buf_size)
  1453. {
  1454. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  1455. const uint8 *p = buf, *p_end = buf + size;
  1456. uint32 section_type;
  1457. uint32 section_size;
  1458. uint64 total_size;
  1459. uint8 *aot_text;
  1460. p += 8;
  1461. while (p < p_end) {
  1462. read_uint32(p, p_end, section_type);
  1463. if (section_type < AOT_SECTION_TYPE_SIGANATURE) {
  1464. read_uint32(p, p_end, section_size);
  1465. CHECK_BUF(p, p_end, section_size);
  1466. if (!(section = wasm_runtime_malloc(sizeof(AOTSection)))) {
  1467. set_error_buf(error_buf, error_buf_size,
  1468. "AOT module load failed: "
  1469. "allocate memory failed.");
  1470. goto fail;
  1471. }
  1472. memset(section, 0, sizeof(AOTSection));
  1473. section->section_type = (int32)section_type;
  1474. section->section_body = (uint8*)p;
  1475. section->section_body_size = section_size;
  1476. if (section_type == AOT_SECTION_TYPE_TEXT) {
  1477. if (section_size > 0) {
  1478. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE
  1479. | MMAP_PROT_EXEC;
  1480. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1481. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1482. relocation for R_X86_64_32/32S/PC32 */
  1483. int map_flags = MMAP_MAP_32BIT;
  1484. #else
  1485. int map_flags = MMAP_MAP_NONE;
  1486. #endif
  1487. total_size = (uint64)section_size + aot_get_plt_table_size();
  1488. total_size = (total_size + 3) & ~((uint64)3);
  1489. if (total_size >= UINT32_MAX
  1490. || !(aot_text = os_mmap(NULL, (uint32)total_size,
  1491. map_prot, map_flags))) {
  1492. wasm_runtime_free(section);
  1493. set_error_buf(error_buf, error_buf_size,
  1494. "AOT module load failed: "
  1495. "mmap memory failed.");
  1496. goto fail;
  1497. }
  1498. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1499. /* address must be in the first 2 Gigabytes of
  1500. the process address space */
  1501. bh_assert((uintptr_t)aot_text < INT32_MAX);
  1502. #endif
  1503. bh_memcpy_s(aot_text, (uint32)total_size,
  1504. section->section_body, (uint32)section_size);
  1505. section->section_body = aot_text;
  1506. if ((uint32)total_size > section->section_body_size) {
  1507. memset(aot_text + (uint32)section_size,
  1508. 0, (uint32)total_size - section_size);
  1509. section->section_body_size = (uint32)total_size;
  1510. }
  1511. }
  1512. else
  1513. section->section_body = NULL;
  1514. }
  1515. if (!section_list)
  1516. section_list = section_list_end = section;
  1517. else {
  1518. section_list_end->next = section;
  1519. section_list_end = section;
  1520. }
  1521. p += section_size;
  1522. }
  1523. else {
  1524. set_error_buf(error_buf, error_buf_size,
  1525. "AOT module load failed: invalid section id.");
  1526. goto fail;
  1527. }
  1528. }
  1529. if (!section_list) {
  1530. set_error_buf(error_buf, error_buf_size,
  1531. "AOT module load failed: create section list failed.");
  1532. return false;
  1533. }
  1534. *p_section_list = section_list;
  1535. return true;
  1536. fail:
  1537. if (section_list)
  1538. destroy_sections(section_list, true);
  1539. return false;
  1540. }
  1541. static bool
  1542. load(const uint8 *buf, uint32 size, AOTModule *module,
  1543. char *error_buf, uint32 error_buf_size)
  1544. {
  1545. const uint8 *buf_end = buf + size;
  1546. const uint8 *p = buf, *p_end = buf_end;
  1547. uint32 magic_number, version;
  1548. AOTSection *section_list = NULL;
  1549. bool ret;
  1550. read_uint32(p, p_end, magic_number);
  1551. if (magic_number != AOT_MAGIC_NUMBER) {
  1552. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  1553. return false;
  1554. }
  1555. read_uint32(p, p_end, version);
  1556. if (version != AOT_CURRENT_VERSION) {
  1557. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  1558. return false;
  1559. }
  1560. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size))
  1561. return false;
  1562. ret = load_from_sections(module, section_list, error_buf, error_buf_size);
  1563. if (!ret) {
  1564. /* If load_from_sections() fails, then aot text is destroyed
  1565. in destroy_sections() */
  1566. destroy_sections(section_list, true);
  1567. /* aot_unload() won't destroy aot text again */
  1568. module->code = NULL;
  1569. }
  1570. else {
  1571. /* If load_from_sections() succeeds, then aot text is set to
  1572. module->code and will be destroyed in aot_unload() */
  1573. destroy_sections(section_list, false);
  1574. }
  1575. return ret;
  1576. fail:
  1577. return false;
  1578. }
  1579. AOTModule*
  1580. aot_load_from_aot_file(const uint8 *buf, uint32 size,
  1581. char *error_buf, uint32 error_buf_size)
  1582. {
  1583. AOTModule *module = create_module(error_buf, error_buf_size);
  1584. if (!module)
  1585. return NULL;
  1586. if (!load(buf, size, module, error_buf, error_buf_size)) {
  1587. aot_unload(module);
  1588. return NULL;
  1589. }
  1590. LOG_VERBOSE("Load module success.\n");
  1591. return module;
  1592. }
  1593. #if WASM_ENABLE_JIT != 0
  1594. static AOTModule*
  1595. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  1596. char *error_buf, uint32 error_buf_size)
  1597. {
  1598. uint32 i;
  1599. uint64 size;
  1600. char func_name[32];
  1601. AOTModule *module;
  1602. /* Allocate memory for module */
  1603. if (!(module = wasm_runtime_malloc(sizeof(AOTModule)))) {
  1604. set_error_buf(error_buf, error_buf_size,
  1605. "Allocate memory for AOT module failed.");
  1606. return NULL;
  1607. }
  1608. memset(module, 0, sizeof(AOTModule));
  1609. module->module_type = Wasm_Module_AoT;
  1610. module->num_bytes_per_page = comp_data->num_bytes_per_page;
  1611. module->mem_init_page_count = comp_data->mem_init_page_count;
  1612. module->mem_max_page_count = comp_data->mem_max_page_count;
  1613. module->mem_init_data_list = comp_data->mem_init_data_list;
  1614. module->mem_init_data_count = comp_data->mem_init_data_count;
  1615. module->table_init_data_list = comp_data->table_init_data_list;
  1616. module->table_init_data_count = comp_data->table_init_data_count;
  1617. module->table_size = comp_data->table_size;
  1618. module->func_type_count = comp_data->func_type_count;
  1619. module->func_types = comp_data->func_types;
  1620. module->import_global_count = comp_data->import_global_count;
  1621. module->import_globals = comp_data->import_globals;
  1622. module->global_count = comp_data->global_count;
  1623. module->globals = comp_data->globals;
  1624. module->global_count = comp_data->global_count;
  1625. module->globals = comp_data->globals;
  1626. module->global_data_size = comp_data->global_data_size;
  1627. module->import_func_count = comp_data->import_func_count;
  1628. module->import_funcs = comp_data->import_funcs;
  1629. module->func_count = comp_data->func_count;
  1630. /* Allocate memory for function pointers */
  1631. size = (uint64)module->func_count * sizeof(void *);
  1632. if (size >= UINT32_MAX
  1633. || !(module->func_ptrs = wasm_runtime_malloc((uint32)size))) {
  1634. set_error_buf(error_buf, error_buf_size, "Create func ptrs fail.");
  1635. goto fail1;
  1636. }
  1637. /* Resolve function addresses */
  1638. bh_assert(comp_ctx->exec_engine);
  1639. memset(module->func_ptrs, 0, (uint32)size);
  1640. for (i = 0; i < comp_data->func_count; i++) {
  1641. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  1642. if (!(module->func_ptrs[i] =
  1643. (void *)LLVMGetFunctionAddress(comp_ctx->exec_engine,
  1644. func_name))) {
  1645. set_error_buf(error_buf, error_buf_size,
  1646. "Get function address fail.");
  1647. goto fail2;
  1648. }
  1649. }
  1650. /* Allocation memory for function type indexes */
  1651. size = (uint64)module->func_count * sizeof(uint32);
  1652. if (size >= UINT32_MAX
  1653. || !(module->func_type_indexes = wasm_runtime_malloc((uint32)size))) {
  1654. set_error_buf(error_buf, error_buf_size, "Create func type indexes fail.");
  1655. goto fail2;
  1656. }
  1657. memset(module->func_type_indexes, 0, (uint32)size);
  1658. for (i = 0; i < comp_data->func_count; i++)
  1659. module->func_type_indexes[i] = comp_data->funcs[i]->func_type_index;
  1660. module->export_func_count = comp_data->export_func_count;
  1661. module->export_funcs = comp_data->export_funcs;
  1662. /* Set export function pointers */
  1663. for (i = 0; i < module->export_func_count; i++) {
  1664. module->export_funcs[i].func_ptr =
  1665. module->func_ptrs[module->export_funcs[i].func_index
  1666. - module->import_func_count];
  1667. }
  1668. module->start_func_index = comp_data->start_func_index;
  1669. if (comp_data->start_func_index != (uint32)-1) {
  1670. bh_assert(comp_data->start_func_index >= module->import_func_count
  1671. && comp_data->start_func_index < module->import_func_count
  1672. + module->func_count);
  1673. module->start_function =
  1674. module->func_ptrs[comp_data->start_func_index
  1675. - module->import_func_count];
  1676. }
  1677. else {
  1678. module->start_function = NULL;
  1679. }
  1680. module->llvm_aux_data_end = comp_data->llvm_aux_data_end;
  1681. module->llvm_aux_stack_bottom = comp_data->llvm_aux_stack_bottom;
  1682. module->llvm_aux_stack_size = comp_data->llvm_aux_stack_size;
  1683. module->llvm_aux_stack_global_index = comp_data->llvm_aux_stack_global_index;
  1684. module->code = NULL;
  1685. module->code_size = 0;
  1686. module->is_jit_mode = true;
  1687. module->wasm_module = comp_data->wasm_module;
  1688. module->comp_ctx = comp_ctx;
  1689. module->comp_data = comp_data;
  1690. #if WASM_ENABLE_LIBC_WASI != 0
  1691. module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
  1692. #endif
  1693. return module;
  1694. fail2:
  1695. wasm_runtime_free(module->func_ptrs);
  1696. fail1:
  1697. wasm_runtime_free(module);
  1698. return NULL;
  1699. }
  1700. AOTModule*
  1701. aot_convert_wasm_module(WASMModule *wasm_module,
  1702. char *error_buf, uint32 error_buf_size)
  1703. {
  1704. AOTCompData *comp_data;
  1705. AOTCompContext *comp_ctx;
  1706. AOTModule *aot_module;
  1707. AOTCompOption option = { 0 };
  1708. char *aot_last_error;
  1709. comp_data = aot_create_comp_data(wasm_module);
  1710. if (!comp_data) {
  1711. aot_last_error = aot_get_last_error();
  1712. bh_assert(aot_last_error != NULL);
  1713. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1714. return NULL;
  1715. }
  1716. option.is_jit_mode = true;
  1717. comp_ctx = aot_create_comp_context(comp_data, &option);
  1718. if (!comp_ctx) {
  1719. aot_last_error = aot_get_last_error();
  1720. bh_assert(aot_last_error != NULL);
  1721. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1722. goto fail1;
  1723. }
  1724. if (!aot_compile_wasm(comp_ctx)) {
  1725. aot_last_error = aot_get_last_error();
  1726. bh_assert(aot_last_error != NULL);
  1727. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1728. goto fail2;
  1729. }
  1730. aot_module = aot_load_from_comp_data(comp_data, comp_ctx,
  1731. error_buf, error_buf_size);
  1732. if (!aot_module) {
  1733. goto fail2;
  1734. }
  1735. return aot_module;
  1736. fail2:
  1737. aot_destroy_comp_context(comp_ctx);
  1738. fail1:
  1739. aot_destroy_comp_data(comp_data);
  1740. return NULL;
  1741. }
  1742. #endif
  1743. void
  1744. aot_unload(AOTModule *module)
  1745. {
  1746. #if WASM_ENABLE_JIT != 0
  1747. if (module->comp_data)
  1748. aot_destroy_comp_data(module->comp_data);
  1749. if (module->comp_ctx)
  1750. aot_destroy_comp_context(module->comp_ctx);
  1751. if (module->wasm_module)
  1752. wasm_loader_unload(module->wasm_module);
  1753. #endif
  1754. if (module->mem_init_data_list)
  1755. destroy_mem_init_data_list(module->mem_init_data_list,
  1756. module->mem_init_data_count,
  1757. module->is_jit_mode);
  1758. if (module->table_init_data_list)
  1759. destroy_table_init_data_list(module->table_init_data_list,
  1760. module->table_init_data_count,
  1761. module->is_jit_mode);
  1762. if (module->func_types)
  1763. destroy_func_types(module->func_types,
  1764. module->func_type_count,
  1765. module->is_jit_mode);
  1766. if (module->import_globals)
  1767. destroy_import_globals(module->import_globals,
  1768. module->is_jit_mode);
  1769. if (module->globals)
  1770. destroy_globals(module->globals,
  1771. module->is_jit_mode);
  1772. if (module->import_funcs)
  1773. destroy_import_funcs(module->import_funcs,
  1774. module->is_jit_mode);
  1775. if (module->export_funcs)
  1776. destroy_export_funcs(module->export_funcs,
  1777. module->is_jit_mode);
  1778. if (module->func_type_indexes)
  1779. wasm_runtime_free(module->func_type_indexes);
  1780. if (module->func_ptrs)
  1781. wasm_runtime_free(module->func_ptrs);
  1782. if (module->const_str_set)
  1783. bh_hash_map_destroy(module->const_str_set);
  1784. if (module->code)
  1785. os_munmap(module->code, module->code_size);
  1786. if (module->data_sections)
  1787. destroy_object_data_sections(module->data_sections,
  1788. module->data_section_count);
  1789. wasm_runtime_free(module);
  1790. }
  1791. uint32
  1792. aot_get_plt_table_size()
  1793. {
  1794. return get_plt_table_size();
  1795. }