aot_loader.c 69 KB

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