aot_loader.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  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. /* address must be in the first 2 Gigabytes of
  841. the process address space */
  842. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  843. #endif
  844. read_byte_array(buf, buf_end,
  845. data_sections[i].data, data_sections[i].size);
  846. }
  847. *p_buf = buf;
  848. return true;
  849. fail:
  850. return false;
  851. }
  852. static bool
  853. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  854. AOTModule *module,
  855. char *error_buf, uint32 error_buf_size)
  856. {
  857. const uint8 *buf = *p_buf;
  858. read_uint32(buf, buf_end, module->data_section_count);
  859. /* load object data sections */
  860. if (module->data_section_count > 0
  861. && !load_object_data_sections(&buf, buf_end, module,
  862. error_buf, error_buf_size))
  863. return false;
  864. *p_buf = buf;
  865. return true;
  866. fail:
  867. return false;
  868. }
  869. static bool
  870. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  871. AOTModule *module,
  872. char *error_buf, uint32 error_buf_size)
  873. {
  874. const uint8 *p = buf, *p_end = buf_end;
  875. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  876. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  877. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  878. || !load_import_global_info(&p, p_end, module, error_buf, error_buf_size)
  879. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  880. || !load_import_func_info(&p, p_end, module, error_buf, error_buf_size))
  881. return false;
  882. /* load function count and start function index */
  883. read_uint32(p, p_end, module->func_count);
  884. read_uint32(p, p_end, module->start_func_index);
  885. /* check start function index */
  886. if (module->start_func_index != (uint32)-1
  887. && (module->start_func_index >= module->import_func_count
  888. + module->func_count)) {
  889. set_error_buf(error_buf, error_buf_size,
  890. "AOT module load failed: "
  891. "invalid start function index");
  892. return false;
  893. }
  894. read_uint32(p, p_end, module->llvm_aux_data_end);
  895. read_uint32(p, p_end, module->llvm_aux_stack_bottom);
  896. read_uint32(p, p_end, module->llvm_aux_stack_size);
  897. read_uint32(p, p_end, module->llvm_aux_stack_global_index);
  898. if (!load_object_data_sections_info(&p, p_end, module,
  899. error_buf, error_buf_size))
  900. return false;
  901. if (p != p_end) {
  902. set_error_buf(error_buf, error_buf_size,
  903. "AOT module load failed: "
  904. "invalid init data section size");
  905. return false;
  906. }
  907. return true;
  908. fail:
  909. return false;
  910. }
  911. static bool
  912. load_text_section(const uint8 *buf, const uint8 *buf_end,
  913. AOTModule *module,
  914. char *error_buf, uint32 error_buf_size)
  915. {
  916. uint8 *plt_base;
  917. if (module->func_count > 0 && buf_end == buf) {
  918. set_error_buf(error_buf, error_buf_size,
  919. "AOT module load failed: invalid code size.");
  920. return false;
  921. }
  922. read_uint32(buf, buf_end, module->literal_size);
  923. /* literal data is at begining of the text section */
  924. module->literal = (uint8*)buf;
  925. module->code = (void*)(buf + module->literal_size);
  926. module->code_size = (uint32)(buf_end - (uint8*)module->code);
  927. if (module->code_size > 0) {
  928. plt_base = (uint8*)buf_end - get_plt_table_size();
  929. init_plt_table(plt_base);
  930. }
  931. return true;
  932. fail:
  933. return false;
  934. }
  935. static bool
  936. load_function_section(const uint8 *buf, const uint8 *buf_end,
  937. AOTModule *module,
  938. char *error_buf, uint32 error_buf_size)
  939. {
  940. const uint8 *p = buf, *p_end = buf_end;
  941. uint32 i;
  942. uint64 size, text_offset;
  943. size = sizeof(void*) * (uint64)module->func_count;
  944. if (!(module->func_ptrs = loader_malloc
  945. (size, error_buf, error_buf_size))) {
  946. return false;
  947. }
  948. for (i = 0; i < module->func_count; i++) {
  949. if (sizeof(void*) == 8) {
  950. read_uint64(p, p_end, text_offset);
  951. }
  952. else {
  953. uint32 text_offset32;
  954. read_uint32(p, p_end, text_offset32);
  955. text_offset = text_offset32;
  956. }
  957. if (text_offset >= module->code_size) {
  958. set_error_buf(error_buf, error_buf_size,
  959. "AOT module load failed: "
  960. "invalid function code offset.");
  961. return false;
  962. }
  963. module->func_ptrs[i] = (uint8*)module->code + text_offset;
  964. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  965. /* bits[0] of thumb function address must be 1 */
  966. module->func_ptrs[i] = (void*)((uintptr_t)module->func_ptrs[i] | 1);
  967. #endif
  968. }
  969. /* Set start function when function pointers are resolved */
  970. if (module->start_func_index != (uint32)-1) {
  971. if (module->start_func_index >= module->import_func_count)
  972. module->start_function =
  973. module->func_ptrs[module->start_func_index
  974. - module->import_func_count];
  975. else
  976. /* TODO: fix start function can be import function issue */
  977. module->start_function = NULL;
  978. }
  979. else {
  980. module->start_function = NULL;
  981. }
  982. size = sizeof(uint32) * (uint64)module->func_count;
  983. if (!(module->func_type_indexes = loader_malloc
  984. (size, error_buf, error_buf_size))) {
  985. return false;
  986. }
  987. for (i = 0; i < module->func_count; i++) {
  988. read_uint32(p, p_end, module->func_type_indexes[i]);
  989. if (module->func_type_indexes[i] >= module->func_type_count) {
  990. set_error_buf(error_buf, error_buf_size,
  991. "AOT module load failed: unknown type.");
  992. return false;
  993. }
  994. }
  995. if (p != buf_end) {
  996. set_error_buf(error_buf, error_buf_size,
  997. "AOT module load failed: "
  998. "invalid function section size");
  999. return false;
  1000. }
  1001. return true;
  1002. fail:
  1003. return false;
  1004. }
  1005. static void
  1006. destroy_exports(AOTExport *exports, bool is_jit_mode)
  1007. {
  1008. if (!is_jit_mode)
  1009. wasm_runtime_free(exports);
  1010. }
  1011. static bool
  1012. load_exports(const uint8 **p_buf, const uint8 *buf_end,
  1013. AOTModule *module, char *error_buf, uint32 error_buf_size)
  1014. {
  1015. const uint8 *buf = *p_buf;
  1016. AOTExport *exports;
  1017. uint64 size;
  1018. uint32 i;
  1019. /* Allocate memory */
  1020. size = sizeof(AOTExport) * (uint64)module->export_count;
  1021. if (!(module->exports = exports =
  1022. loader_malloc(size, error_buf, error_buf_size))) {
  1023. return false;
  1024. }
  1025. /* Create each export */
  1026. for (i = 0; i < module->export_count; i++) {
  1027. read_uint32(buf, buf_end, exports[i].index);
  1028. read_uint8(buf, buf_end, exports[i].kind);
  1029. read_string(buf, buf_end, exports[i].name);
  1030. #if 0 /* TODO: check kind and index */
  1031. if (export_funcs[i].index >=
  1032. module->func_count + module->import_func_count) {
  1033. set_error_buf(error_buf, error_buf_size,
  1034. "AOT module load failed: "
  1035. "function index is out of range.");
  1036. return false;
  1037. }
  1038. #endif
  1039. }
  1040. *p_buf = buf;
  1041. return true;
  1042. fail:
  1043. return false;
  1044. }
  1045. static bool
  1046. load_export_section(const uint8 *buf, const uint8 *buf_end,
  1047. AOTModule *module,
  1048. char *error_buf, uint32 error_buf_size)
  1049. {
  1050. const uint8 *p = buf, *p_end = buf_end;
  1051. /* load export functions */
  1052. read_uint32(p, p_end, module->export_count);
  1053. if (module->export_count > 0
  1054. && !load_exports(&p, p_end, module, error_buf, error_buf_size))
  1055. return false;
  1056. if (p != p_end) {
  1057. set_error_buf(error_buf, error_buf_size,
  1058. "AOT module load failed: "
  1059. "invalid export section size");
  1060. return false;
  1061. }
  1062. return true;
  1063. fail:
  1064. return false;
  1065. }
  1066. static void *
  1067. get_data_section_addr(AOTModule *module, const char *section_name,
  1068. uint32 *p_data_size)
  1069. {
  1070. uint32 i;
  1071. AOTObjectDataSection *data_section = module->data_sections;
  1072. for (i = 0; i < module->data_section_count; i++, data_section++)
  1073. if (!strcmp(data_section->name, section_name)) {
  1074. if (p_data_size)
  1075. *p_data_size = data_section->size;
  1076. return data_section->data;
  1077. }
  1078. return NULL;
  1079. }
  1080. static void *
  1081. resolve_target_sym(const char *symbol, int32 *p_index)
  1082. {
  1083. uint32 i, num = 0;
  1084. SymbolMap *target_sym_map;
  1085. if (!(target_sym_map = get_target_symbol_map(&num)))
  1086. return NULL;
  1087. for (i = 0; i < num; i++)
  1088. if (!strcmp(target_sym_map[i].symbol_name, symbol)) {
  1089. *p_index = (int32)i;
  1090. return target_sym_map[i].symbol_addr;
  1091. }
  1092. return NULL;
  1093. }
  1094. static bool
  1095. is_literal_relocation(const char *reloc_sec_name)
  1096. {
  1097. return !strcmp(reloc_sec_name, ".rela.literal");
  1098. }
  1099. static bool
  1100. do_text_relocation(AOTModule *module,
  1101. AOTRelocationGroup *group,
  1102. char *error_buf, uint32 error_buf_size)
  1103. {
  1104. bool is_literal = is_literal_relocation(group->section_name);
  1105. uint8 *aot_text = is_literal ? module->literal : module->code;
  1106. uint32 aot_text_size = is_literal ? module->literal_size : module->code_size;
  1107. uint32 i, func_index, symbol_len;
  1108. char symbol_buf[128] = { 0 }, *symbol, *p;
  1109. void *symbol_addr;
  1110. AOTRelocation *relocation = group->relocations;
  1111. if (group->relocation_count > 0 && !aot_text) {
  1112. set_error_buf(error_buf, error_buf_size,
  1113. "AOT module load failed: invalid text relocation count.");
  1114. return false;
  1115. }
  1116. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1117. int32 symbol_index = -1;
  1118. symbol_len = (uint32)strlen(relocation->symbol_name);
  1119. if (symbol_len + 1 <= sizeof(symbol_buf))
  1120. symbol = symbol_buf;
  1121. else {
  1122. if (!(symbol = loader_malloc(symbol_len + 1,
  1123. error_buf, error_buf_size))) {
  1124. return false;
  1125. }
  1126. }
  1127. memcpy(symbol, relocation->symbol_name, symbol_len);
  1128. symbol[symbol_len] = '\0';
  1129. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1130. p = symbol + strlen(AOT_FUNC_PREFIX);
  1131. if (*p == '\0'
  1132. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1133. if (error_buf != NULL)
  1134. snprintf(error_buf, error_buf_size,
  1135. "AOT module load failed: "
  1136. "invalid import symbol %s.",
  1137. symbol);
  1138. goto check_symbol_fail;
  1139. }
  1140. symbol_addr = module->func_ptrs[func_index];
  1141. }
  1142. else if (!strcmp(symbol, ".text")) {
  1143. symbol_addr = module->code;
  1144. }
  1145. else if (!strcmp(symbol, ".data")
  1146. || !strcmp(symbol, ".rodata")
  1147. /* ".rodata.cst4/8/16/.." */
  1148. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) {
  1149. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1150. if (!symbol_addr) {
  1151. if (error_buf != NULL)
  1152. snprintf(error_buf, error_buf_size,
  1153. "AOT module load failed: "
  1154. "invalid data section (%s).",
  1155. symbol);
  1156. goto check_symbol_fail;
  1157. }
  1158. }
  1159. else if (!strcmp(symbol, ".literal")) {
  1160. symbol_addr = module->literal;
  1161. }
  1162. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1163. if (error_buf != NULL)
  1164. snprintf(error_buf, error_buf_size,
  1165. "AOT module load failed: "
  1166. "resolve symbol %s failed.",
  1167. symbol);
  1168. goto check_symbol_fail;
  1169. }
  1170. if (symbol != symbol_buf)
  1171. wasm_runtime_free(symbol);
  1172. if (!apply_relocation(module,
  1173. aot_text, aot_text_size,
  1174. relocation->relocation_offset,
  1175. relocation->relocation_addend,
  1176. relocation->relocation_type,
  1177. symbol_addr, symbol_index,
  1178. error_buf, error_buf_size))
  1179. return false;
  1180. }
  1181. return true;
  1182. check_symbol_fail:
  1183. if (symbol != symbol_buf)
  1184. wasm_runtime_free(symbol);
  1185. return false;
  1186. }
  1187. static bool
  1188. do_data_relocation(AOTModule *module,
  1189. AOTRelocationGroup *group,
  1190. char *error_buf, uint32 error_buf_size)
  1191. {
  1192. uint8 *data_addr;
  1193. uint32 data_size = 0, i;
  1194. AOTRelocation *relocation = group->relocations;
  1195. void *symbol_addr;
  1196. char *symbol, *data_section_name;
  1197. if (!strncmp(group->section_name, ".rela.", 6)) {
  1198. data_section_name = group->section_name + strlen(".rela");
  1199. }
  1200. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1201. data_section_name = group->section_name + strlen(".rel");
  1202. }
  1203. else {
  1204. set_error_buf(error_buf, error_buf_size,
  1205. "AOT module load failed: "
  1206. "invalid data relocation section name.");
  1207. return false;
  1208. }
  1209. data_addr = get_data_section_addr(module, data_section_name,
  1210. &data_size);
  1211. if (group->relocation_count > 0 && !data_addr) {
  1212. set_error_buf(error_buf, error_buf_size,
  1213. "AOT module load failed: invalid data relocation count.");
  1214. return false;
  1215. }
  1216. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1217. symbol = relocation->symbol_name;
  1218. if (!strcmp(symbol, ".text")) {
  1219. symbol_addr = module->code;
  1220. }
  1221. else {
  1222. if (error_buf != NULL)
  1223. snprintf(error_buf, error_buf_size,
  1224. "AOT module load failed: "
  1225. "invalid relocation symbol %s.",
  1226. symbol);
  1227. return false;
  1228. }
  1229. if (!apply_relocation(module,
  1230. data_addr, data_size,
  1231. relocation->relocation_offset,
  1232. relocation->relocation_addend,
  1233. relocation->relocation_type,
  1234. symbol_addr, -1,
  1235. error_buf, error_buf_size))
  1236. return false;
  1237. }
  1238. return true;
  1239. }
  1240. static bool
  1241. validate_symbol_table(uint8 *buf, uint8 *buf_end,
  1242. uint32 *offsets, uint32 count,
  1243. char *error_buf, uint32 error_buf_size)
  1244. {
  1245. uint32 i, str_len_addr = 0;
  1246. uint16 str_len;
  1247. for (i = 0; i < count; i++) {
  1248. if (offsets[i] != str_len_addr)
  1249. return false;
  1250. read_uint16(buf, buf_end, str_len);
  1251. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1252. str_len_addr = align_uint(str_len_addr, 2);
  1253. buf += str_len;
  1254. buf = (uint8*)align_ptr(buf, 2);
  1255. }
  1256. if (buf == buf_end)
  1257. return true;
  1258. fail:
  1259. return false;
  1260. }
  1261. static bool
  1262. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1263. AOTModule *module,
  1264. char *error_buf, uint32 error_buf_size)
  1265. {
  1266. AOTRelocationGroup *groups = NULL, *group;
  1267. uint32 symbol_count = 0;
  1268. uint32 group_count = 0, i, j;
  1269. uint64 size;
  1270. uint32 *symbol_offsets, total_string_len;
  1271. uint8 *symbol_buf, *symbol_buf_end;
  1272. bool ret = false;
  1273. read_uint32(buf, buf_end, symbol_count);
  1274. symbol_offsets = (uint32 *)buf;
  1275. for (i = 0; i < symbol_count; i++) {
  1276. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1277. buf += sizeof(uint32);
  1278. }
  1279. read_uint32(buf, buf_end, total_string_len);
  1280. symbol_buf = (uint8 *)buf;
  1281. symbol_buf_end = symbol_buf + total_string_len;
  1282. if (!validate_symbol_table(symbol_buf, symbol_buf_end,
  1283. symbol_offsets, symbol_count,
  1284. error_buf, error_buf_size)) {
  1285. set_error_buf(error_buf, error_buf_size,
  1286. "AOT module load failed: "
  1287. "validate symbol table failed.");
  1288. goto fail;
  1289. }
  1290. buf = symbol_buf_end;
  1291. read_uint32(buf, buf_end, group_count);
  1292. /* Allocate memory for relocation groups */
  1293. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1294. if (!(groups = loader_malloc(size, error_buf, error_buf_size))) {
  1295. goto fail;
  1296. }
  1297. /* Load each relocation group */
  1298. for (i = 0, group = groups; i < group_count; i++, group++) {
  1299. AOTRelocation *relocation;
  1300. uint32 name_index;
  1301. uint16 str_len;
  1302. uint8 *name_addr;
  1303. /* section name address is 4 bytes aligned. */
  1304. buf = (uint8*)align_ptr(buf, sizeof(uint32));
  1305. read_uint32(buf, buf_end, name_index);
  1306. if (name_index >= symbol_count) {
  1307. set_error_buf(error_buf, error_buf_size,
  1308. "AOT module load failed: "
  1309. "symbol index out of range.");
  1310. goto fail;
  1311. }
  1312. name_addr = symbol_buf + symbol_offsets[name_index];
  1313. str_len = *(uint16 *)name_addr;
  1314. if (!(group->section_name =
  1315. const_str_set_insert(name_addr + sizeof(uint16),
  1316. (int32)str_len, module,
  1317. error_buf, error_buf_size))) {
  1318. goto fail;
  1319. }
  1320. read_uint32(buf, buf_end, group->relocation_count);
  1321. /* Allocate memory for relocations */
  1322. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1323. if (!(group->relocations = relocation =
  1324. loader_malloc(size, error_buf, error_buf_size))) {
  1325. ret = false;
  1326. goto fail;
  1327. }
  1328. /* Load each relocation */
  1329. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1330. uint32 symbol_index;
  1331. uint16 str_len;
  1332. uint8 *symbol_addr;
  1333. if (sizeof(void *) == 8) {
  1334. read_uint64(buf, buf_end, relocation->relocation_offset);
  1335. read_uint64(buf, buf_end, relocation->relocation_addend);
  1336. }
  1337. else {
  1338. uint32 offset32, addend32;
  1339. read_uint32(buf, buf_end, offset32);
  1340. relocation->relocation_offset = (uint64)offset32;
  1341. read_uint32(buf, buf_end, addend32);
  1342. relocation->relocation_addend = (uint64)addend32;
  1343. }
  1344. read_uint32(buf, buf_end, relocation->relocation_type);
  1345. read_uint32(buf, buf_end, symbol_index);
  1346. if (symbol_index >= symbol_count) {
  1347. set_error_buf(error_buf, error_buf_size,
  1348. "AOT module load failed: "
  1349. "symbol index out of range.");
  1350. goto fail;
  1351. }
  1352. symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  1353. str_len = *(uint16 *)symbol_addr;
  1354. if (!(relocation->symbol_name =
  1355. const_str_set_insert(symbol_addr + sizeof(uint16),
  1356. (int32)str_len, module,
  1357. error_buf, error_buf_size))) {
  1358. goto fail;
  1359. }
  1360. }
  1361. if (!strcmp(group->section_name, ".rel.text")
  1362. || !strcmp(group->section_name, ".rela.text")
  1363. || !strcmp(group->section_name, ".rela.literal")) {
  1364. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  1365. return false;
  1366. }
  1367. else {
  1368. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  1369. return false;
  1370. }
  1371. }
  1372. ret = true;
  1373. fail:
  1374. if (groups) {
  1375. for (i = 0, group = groups; i < group_count; i++, group++)
  1376. if (group->relocations)
  1377. wasm_runtime_free(group->relocations);
  1378. wasm_runtime_free(groups);
  1379. }
  1380. return ret;
  1381. }
  1382. static bool
  1383. load_from_sections(AOTModule *module, AOTSection *sections,
  1384. char *error_buf, uint32 error_buf_size)
  1385. {
  1386. AOTSection *section = sections;
  1387. const uint8 *buf, *buf_end;
  1388. uint32 last_section_type = (uint32)-1, section_type;
  1389. while (section) {
  1390. buf = section->section_body;
  1391. buf_end = buf + section->section_body_size;
  1392. /* Check sections */
  1393. section_type = (uint32)section->section_type;
  1394. if ((last_section_type == (uint32)-1
  1395. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  1396. || (last_section_type != (uint32)-1
  1397. && section_type != last_section_type + 1)) {
  1398. set_error_buf(error_buf, error_buf_size,
  1399. "AOT module load failed: invalid section order.");
  1400. return false;
  1401. }
  1402. last_section_type = section_type;
  1403. switch (section_type) {
  1404. case AOT_SECTION_TYPE_TARGET_INFO:
  1405. if (!load_target_info_section(buf, buf_end, module,
  1406. error_buf, error_buf_size))
  1407. return false;
  1408. break;
  1409. case AOT_SECTION_TYPE_INIT_DATA:
  1410. if (!load_init_data_section(buf, buf_end, module,
  1411. error_buf, error_buf_size))
  1412. return false;
  1413. break;
  1414. case AOT_SECTION_TYPE_TEXT:
  1415. if (!load_text_section(buf, buf_end, module,
  1416. error_buf, error_buf_size))
  1417. return false;
  1418. break;
  1419. case AOT_SECTION_TYPE_FUNCTION:
  1420. if (!load_function_section(buf, buf_end, module,
  1421. error_buf, error_buf_size))
  1422. return false;
  1423. break;
  1424. case AOT_SECTION_TYPE_EXPORT:
  1425. if (!load_export_section(buf, buf_end, module,
  1426. error_buf, error_buf_size))
  1427. return false;
  1428. break;
  1429. case AOT_SECTION_TYPE_RELOCATION:
  1430. if (!load_relocation_section(buf, buf_end, module,
  1431. error_buf, error_buf_size))
  1432. return false;
  1433. break;
  1434. }
  1435. section = section->next;
  1436. }
  1437. if (last_section_type != AOT_SECTION_TYPE_RELOCATION) {
  1438. set_error_buf(error_buf, error_buf_size,
  1439. "AOT module load failed: section missing.");
  1440. return false;
  1441. }
  1442. /* Flush data cache before executing AOT code,
  1443. * otherwise unpredictable behavior can occur. */
  1444. os_dcache_flush();
  1445. return true;
  1446. }
  1447. #if BH_ENABLE_MEMORY_PROFILING != 0
  1448. static void aot_free(void *ptr)
  1449. {
  1450. wasm_runtime_free(ptr);
  1451. }
  1452. #else
  1453. #define aot_free wasm_runtime_free
  1454. #endif
  1455. static AOTModule*
  1456. create_module(char *error_buf, uint32 error_buf_size)
  1457. {
  1458. AOTModule *module =
  1459. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
  1460. if (!module) {
  1461. return NULL;
  1462. }
  1463. module->module_type = Wasm_Module_AoT;
  1464. if (!(module->const_str_set =
  1465. bh_hash_map_create(32, false,
  1466. (HashFunc)wasm_string_hash,
  1467. (KeyEqualFunc)wasm_string_equal,
  1468. NULL,
  1469. aot_free))) {
  1470. set_error_buf(error_buf, error_buf_size,
  1471. "AOT module load failed: "
  1472. "create const string set failed.");
  1473. wasm_runtime_free(module);
  1474. return NULL;
  1475. }
  1476. return module;
  1477. }
  1478. AOTModule*
  1479. aot_load_from_sections(AOTSection *section_list,
  1480. char *error_buf, uint32 error_buf_size)
  1481. {
  1482. AOTModule *module = create_module(error_buf, error_buf_size);
  1483. if (!module)
  1484. return NULL;
  1485. if (!load_from_sections(module, section_list,
  1486. error_buf, error_buf_size)) {
  1487. aot_unload(module);
  1488. return NULL;
  1489. }
  1490. LOG_VERBOSE("Load module from sections success.\n");
  1491. return module;
  1492. }
  1493. static void
  1494. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  1495. {
  1496. AOTSection *section = section_list, *next;
  1497. while (section) {
  1498. next = section->next;
  1499. if (destroy_aot_text
  1500. && section->section_type == AOT_SECTION_TYPE_TEXT
  1501. && section->section_body)
  1502. os_munmap((uint8*)section->section_body, section->section_body_size);
  1503. wasm_runtime_free(section);
  1504. section = next;
  1505. }
  1506. }
  1507. static bool
  1508. create_sections(const uint8 *buf, uint32 size,
  1509. AOTSection **p_section_list,
  1510. char *error_buf, uint32 error_buf_size)
  1511. {
  1512. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  1513. const uint8 *p = buf, *p_end = buf + size;
  1514. uint32 section_type;
  1515. uint32 section_size;
  1516. uint64 total_size;
  1517. uint8 *aot_text;
  1518. p += 8;
  1519. while (p < p_end) {
  1520. read_uint32(p, p_end, section_type);
  1521. if (section_type < AOT_SECTION_TYPE_SIGANATURE) {
  1522. read_uint32(p, p_end, section_size);
  1523. CHECK_BUF(p, p_end, section_size);
  1524. if (!(section =
  1525. loader_malloc(sizeof(AOTSection),
  1526. error_buf, error_buf_size))) {
  1527. goto fail;
  1528. }
  1529. memset(section, 0, sizeof(AOTSection));
  1530. section->section_type = (int32)section_type;
  1531. section->section_body = (uint8*)p;
  1532. section->section_body_size = section_size;
  1533. if (section_type == AOT_SECTION_TYPE_TEXT) {
  1534. if (section_size > 0) {
  1535. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE
  1536. | MMAP_PROT_EXEC;
  1537. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1538. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1539. relocation for R_X86_64_32/32S/PC32 */
  1540. int map_flags = MMAP_MAP_32BIT;
  1541. #else
  1542. int map_flags = MMAP_MAP_NONE;
  1543. #endif
  1544. total_size = (uint64)section_size + aot_get_plt_table_size();
  1545. total_size = (total_size + 3) & ~((uint64)3);
  1546. if (total_size >= UINT32_MAX
  1547. || !(aot_text = os_mmap(NULL, (uint32)total_size,
  1548. map_prot, map_flags))) {
  1549. wasm_runtime_free(section);
  1550. set_error_buf(error_buf, error_buf_size,
  1551. "AOT module load failed: "
  1552. "mmap memory failed.");
  1553. goto fail;
  1554. }
  1555. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1556. /* address must be in the first 2 Gigabytes of
  1557. the process address space */
  1558. bh_assert((uintptr_t)aot_text < INT32_MAX);
  1559. #endif
  1560. bh_memcpy_s(aot_text, (uint32)total_size,
  1561. section->section_body, (uint32)section_size);
  1562. section->section_body = aot_text;
  1563. if ((uint32)total_size > section->section_body_size) {
  1564. memset(aot_text + (uint32)section_size,
  1565. 0, (uint32)total_size - section_size);
  1566. section->section_body_size = (uint32)total_size;
  1567. }
  1568. }
  1569. else
  1570. section->section_body = NULL;
  1571. }
  1572. if (!section_list)
  1573. section_list = section_list_end = section;
  1574. else {
  1575. section_list_end->next = section;
  1576. section_list_end = section;
  1577. }
  1578. p += section_size;
  1579. }
  1580. else {
  1581. set_error_buf(error_buf, error_buf_size,
  1582. "AOT module load failed: invalid section id.");
  1583. goto fail;
  1584. }
  1585. }
  1586. if (!section_list) {
  1587. set_error_buf(error_buf, error_buf_size,
  1588. "AOT module load failed: create section list failed.");
  1589. return false;
  1590. }
  1591. *p_section_list = section_list;
  1592. return true;
  1593. fail:
  1594. if (section_list)
  1595. destroy_sections(section_list, true);
  1596. return false;
  1597. }
  1598. static bool
  1599. load(const uint8 *buf, uint32 size, AOTModule *module,
  1600. char *error_buf, uint32 error_buf_size)
  1601. {
  1602. const uint8 *buf_end = buf + size;
  1603. const uint8 *p = buf, *p_end = buf_end;
  1604. uint32 magic_number, version;
  1605. AOTSection *section_list = NULL;
  1606. bool ret;
  1607. read_uint32(p, p_end, magic_number);
  1608. if (magic_number != AOT_MAGIC_NUMBER) {
  1609. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  1610. return false;
  1611. }
  1612. read_uint32(p, p_end, version);
  1613. if (version != AOT_CURRENT_VERSION) {
  1614. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  1615. return false;
  1616. }
  1617. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size))
  1618. return false;
  1619. ret = load_from_sections(module, section_list, error_buf, error_buf_size);
  1620. if (!ret) {
  1621. /* If load_from_sections() fails, then aot text is destroyed
  1622. in destroy_sections() */
  1623. destroy_sections(section_list, true);
  1624. /* aot_unload() won't destroy aot text again */
  1625. module->code = NULL;
  1626. }
  1627. else {
  1628. /* If load_from_sections() succeeds, then aot text is set to
  1629. module->code and will be destroyed in aot_unload() */
  1630. destroy_sections(section_list, false);
  1631. }
  1632. return ret;
  1633. fail:
  1634. return false;
  1635. }
  1636. AOTModule*
  1637. aot_load_from_aot_file(const uint8 *buf, uint32 size,
  1638. char *error_buf, uint32 error_buf_size)
  1639. {
  1640. AOTModule *module = create_module(error_buf, error_buf_size);
  1641. if (!module)
  1642. return NULL;
  1643. if (!load(buf, size, module, error_buf, error_buf_size)) {
  1644. aot_unload(module);
  1645. return NULL;
  1646. }
  1647. LOG_VERBOSE("Load module success.\n");
  1648. return module;
  1649. }
  1650. #if WASM_ENABLE_JIT != 0
  1651. static AOTModule*
  1652. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  1653. char *error_buf, uint32 error_buf_size)
  1654. {
  1655. uint32 i;
  1656. uint64 size;
  1657. char func_name[32];
  1658. AOTModule *module;
  1659. /* Allocate memory for module */
  1660. if (!(module =
  1661. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) {
  1662. return NULL;
  1663. }
  1664. module->module_type = Wasm_Module_AoT;
  1665. module->import_memory_count = comp_data->import_memory_count;
  1666. module->import_memories = comp_data->import_memories;
  1667. module->memory_count = comp_data->memory_count;
  1668. if (module->memory_count) {
  1669. size = sizeof(AOTMemory) * (uint64)module->memory_count;
  1670. if (!(module->memories =
  1671. loader_malloc(size, error_buf, error_buf_size))) {
  1672. goto fail1;
  1673. }
  1674. bh_memcpy_s(module->memories, size, comp_data->memories, size);
  1675. }
  1676. module->mem_init_data_list = comp_data->mem_init_data_list;
  1677. module->mem_init_data_count = comp_data->mem_init_data_count;
  1678. module->import_table_count = comp_data->import_table_count;
  1679. module->import_tables = comp_data->import_tables;
  1680. module->table_count = comp_data->table_count;
  1681. module->tables = comp_data->tables;
  1682. module->table_init_data_list = comp_data->table_init_data_list;
  1683. module->table_init_data_count = comp_data->table_init_data_count;
  1684. module->func_type_count = comp_data->func_type_count;
  1685. module->func_types = comp_data->func_types;
  1686. module->import_global_count = comp_data->import_global_count;
  1687. module->import_globals = comp_data->import_globals;
  1688. module->global_count = comp_data->global_count;
  1689. module->globals = comp_data->globals;
  1690. module->global_count = comp_data->global_count;
  1691. module->globals = comp_data->globals;
  1692. module->global_data_size = comp_data->global_data_size;
  1693. module->import_func_count = comp_data->import_func_count;
  1694. module->import_funcs = comp_data->import_funcs;
  1695. module->func_count = comp_data->func_count;
  1696. /* Allocate memory for function pointers */
  1697. size = (uint64)module->func_count * sizeof(void *);
  1698. if (!(module->func_ptrs =
  1699. loader_malloc(size, error_buf, error_buf_size))) {
  1700. goto fail2;
  1701. }
  1702. /* Resolve function addresses */
  1703. bh_assert(comp_ctx->exec_engine);
  1704. for (i = 0; i < comp_data->func_count; i++) {
  1705. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  1706. if (!(module->func_ptrs[i] =
  1707. (void *)LLVMGetFunctionAddress(comp_ctx->exec_engine,
  1708. func_name))) {
  1709. set_error_buf(error_buf, error_buf_size,
  1710. "Get function address fail.");
  1711. goto fail3;
  1712. }
  1713. }
  1714. /* Allocation memory for function type indexes */
  1715. size = (uint64)module->func_count * sizeof(uint32);
  1716. if (!(module->func_type_indexes =
  1717. loader_malloc(size, error_buf, error_buf_size))) {
  1718. goto fail3;
  1719. }
  1720. for (i = 0; i < comp_data->func_count; i++)
  1721. module->func_type_indexes[i] = comp_data->funcs[i]->func_type_index;
  1722. module->export_count = comp_data->wasm_module->export_count;
  1723. module->exports = comp_data->wasm_module->exports;
  1724. module->start_func_index = comp_data->start_func_index;
  1725. if (comp_data->start_func_index != (uint32)-1) {
  1726. bh_assert(comp_data->start_func_index < module->import_func_count
  1727. + module->func_count);
  1728. /* TODO: fix issue that start func cannot be import func */
  1729. if (comp_data->start_func_index >= module->import_func_count) {
  1730. module->start_function =
  1731. module->func_ptrs[comp_data->start_func_index
  1732. - module->import_func_count];
  1733. }
  1734. }
  1735. else {
  1736. module->start_function = NULL;
  1737. }
  1738. module->llvm_aux_data_end = comp_data->llvm_aux_data_end;
  1739. module->llvm_aux_stack_bottom = comp_data->llvm_aux_stack_bottom;
  1740. module->llvm_aux_stack_size = comp_data->llvm_aux_stack_size;
  1741. module->llvm_aux_stack_global_index = comp_data->llvm_aux_stack_global_index;
  1742. module->code = NULL;
  1743. module->code_size = 0;
  1744. module->is_jit_mode = true;
  1745. module->wasm_module = comp_data->wasm_module;
  1746. module->comp_ctx = comp_ctx;
  1747. module->comp_data = comp_data;
  1748. #if WASM_ENABLE_LIBC_WASI != 0
  1749. module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
  1750. #endif
  1751. return module;
  1752. fail3:
  1753. wasm_runtime_free(module->func_ptrs);
  1754. fail2:
  1755. if (module->memory_count > 0)
  1756. wasm_runtime_free(module->memories);
  1757. fail1:
  1758. wasm_runtime_free(module);
  1759. return NULL;
  1760. }
  1761. AOTModule*
  1762. aot_convert_wasm_module(WASMModule *wasm_module,
  1763. char *error_buf, uint32 error_buf_size)
  1764. {
  1765. AOTCompData *comp_data;
  1766. AOTCompContext *comp_ctx;
  1767. AOTModule *aot_module;
  1768. AOTCompOption option = { 0 };
  1769. char *aot_last_error;
  1770. comp_data = aot_create_comp_data(wasm_module);
  1771. if (!comp_data) {
  1772. aot_last_error = aot_get_last_error();
  1773. bh_assert(aot_last_error != NULL);
  1774. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1775. return NULL;
  1776. }
  1777. option.is_jit_mode = true;
  1778. #if WASM_ENABLE_THREAD_MGR != 0
  1779. option.enable_thread_mgr = true;
  1780. #endif
  1781. comp_ctx = aot_create_comp_context(comp_data, &option);
  1782. if (!comp_ctx) {
  1783. aot_last_error = aot_get_last_error();
  1784. bh_assert(aot_last_error != NULL);
  1785. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1786. goto fail1;
  1787. }
  1788. if (!aot_compile_wasm(comp_ctx)) {
  1789. aot_last_error = aot_get_last_error();
  1790. bh_assert(aot_last_error != NULL);
  1791. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1792. goto fail2;
  1793. }
  1794. aot_module = aot_load_from_comp_data(comp_data, comp_ctx,
  1795. error_buf, error_buf_size);
  1796. if (!aot_module) {
  1797. goto fail2;
  1798. }
  1799. return aot_module;
  1800. fail2:
  1801. aot_destroy_comp_context(comp_ctx);
  1802. fail1:
  1803. aot_destroy_comp_data(comp_data);
  1804. return NULL;
  1805. }
  1806. #endif
  1807. void
  1808. aot_unload(AOTModule *module)
  1809. {
  1810. #if WASM_ENABLE_JIT != 0
  1811. if (module->comp_data)
  1812. aot_destroy_comp_data(module->comp_data);
  1813. if (module->comp_ctx)
  1814. aot_destroy_comp_context(module->comp_ctx);
  1815. if (module->wasm_module)
  1816. wasm_loader_unload(module->wasm_module);
  1817. #endif
  1818. if (module->import_memories)
  1819. destroy_import_memories(module->import_memories,
  1820. module->is_jit_mode);
  1821. if (module->memories)
  1822. wasm_runtime_free(module->memories);
  1823. if (module->mem_init_data_list)
  1824. destroy_mem_init_data_list(module->mem_init_data_list,
  1825. module->mem_init_data_count,
  1826. module->is_jit_mode);
  1827. if (module->import_tables)
  1828. destroy_import_tables(module->import_tables,
  1829. module->is_jit_mode);
  1830. if (module->tables)
  1831. destroy_tables(module->tables, module->is_jit_mode);
  1832. if (module->table_init_data_list)
  1833. destroy_table_init_data_list(module->table_init_data_list,
  1834. module->table_init_data_count,
  1835. module->is_jit_mode);
  1836. if (module->func_types)
  1837. destroy_func_types(module->func_types,
  1838. module->func_type_count,
  1839. module->is_jit_mode);
  1840. if (module->import_globals)
  1841. destroy_import_globals(module->import_globals,
  1842. module->is_jit_mode);
  1843. if (module->globals)
  1844. destroy_globals(module->globals,
  1845. module->is_jit_mode);
  1846. if (module->import_funcs)
  1847. destroy_import_funcs(module->import_funcs,
  1848. module->is_jit_mode);
  1849. if (module->exports)
  1850. destroy_exports(module->exports,
  1851. module->is_jit_mode);
  1852. if (module->func_type_indexes)
  1853. wasm_runtime_free(module->func_type_indexes);
  1854. if (module->func_ptrs)
  1855. wasm_runtime_free(module->func_ptrs);
  1856. if (module->const_str_set)
  1857. bh_hash_map_destroy(module->const_str_set);
  1858. if (module->code) {
  1859. uint8 *mmap_addr = module->literal - sizeof(module->literal_size);
  1860. uint32 total_size = sizeof(module->literal_size) + module->literal_size + module->code_size;
  1861. os_munmap(mmap_addr, total_size);
  1862. }
  1863. if (module->data_sections)
  1864. destroy_object_data_sections(module->data_sections,
  1865. module->data_section_count);
  1866. wasm_runtime_free(module);
  1867. }
  1868. uint32
  1869. aot_get_plt_table_size()
  1870. {
  1871. return get_plt_table_size();
  1872. }