aot_loader.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  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. #if WASM_ENABLE_DEBUG_AOT != 0
  17. #include "debug/elf_parser.h"
  18. #include "debug/jit_debug.h"
  19. #endif
  20. #define XMM_PLT_PREFIX "__xmm@"
  21. #define REAL_PLT_PREFIX "__real@"
  22. static void
  23. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  24. {
  25. if (error_buf != NULL) {
  26. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  27. string);
  28. }
  29. }
  30. static void
  31. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  32. {
  33. va_list args;
  34. char buf[128];
  35. if (error_buf != NULL) {
  36. va_start(args, format);
  37. vsnprintf(buf, sizeof(buf), format, args);
  38. va_end(args);
  39. snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf);
  40. }
  41. }
  42. #define exchange_uint8(p_data) (void)0
  43. static void
  44. exchange_uint16(uint8 *p_data)
  45. {
  46. uint8 value = *p_data;
  47. *p_data = *(p_data + 1);
  48. *(p_data + 1) = value;
  49. }
  50. static void
  51. exchange_uint32(uint8 *p_data)
  52. {
  53. uint8 value = *p_data;
  54. *p_data = *(p_data + 3);
  55. *(p_data + 3) = value;
  56. value = *(p_data + 1);
  57. *(p_data + 1) = *(p_data + 2);
  58. *(p_data + 2) = value;
  59. }
  60. static void
  61. exchange_uint64(uint8 *pData)
  62. {
  63. uint32 value;
  64. value = *(uint32 *)pData;
  65. *(uint32 *)pData = *(uint32 *)(pData + 4);
  66. *(uint32 *)(pData + 4) = value;
  67. exchange_uint32(pData);
  68. exchange_uint32(pData + 4);
  69. }
  70. static union {
  71. int a;
  72. char b;
  73. } __ue = { .a = 1 };
  74. #define is_little_endian() (__ue.b == 1)
  75. static bool
  76. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  77. char *error_buf, uint32 error_buf_size)
  78. {
  79. if (buf + length > buf_end) {
  80. set_error_buf(error_buf, error_buf_size, "unexpect end");
  81. return false;
  82. }
  83. return true;
  84. }
  85. #define CHECK_BUF(buf, buf_end, length) \
  86. do { \
  87. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  88. goto fail; \
  89. } \
  90. } while (0)
  91. static uint8 *
  92. align_ptr(const uint8 *p, uint32 b)
  93. {
  94. uintptr_t v = (uintptr_t)p;
  95. uintptr_t m = b - 1;
  96. return (uint8 *)((v + m) & ~m);
  97. }
  98. static inline uint64
  99. GET_U64_FROM_ADDR(uint32 *addr)
  100. {
  101. union {
  102. uint64 val;
  103. uint32 parts[2];
  104. } u;
  105. u.parts[0] = addr[0];
  106. u.parts[1] = addr[1];
  107. return u.val;
  108. }
  109. #define TEMPLATE_READ(p, p_end, res, type) \
  110. do { \
  111. if (sizeof(type) != sizeof(uint64)) \
  112. p = (uint8 *)align_ptr(p, sizeof(type)); \
  113. else \
  114. /* align 4 bytes if type is uint64 */ \
  115. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  116. CHECK_BUF(p, p_end, sizeof(type)); \
  117. if (sizeof(type) != sizeof(uint64)) \
  118. res = *(type *)p; \
  119. else \
  120. res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
  121. if (!is_little_endian()) \
  122. exchange_##type((uint8 *)&res); \
  123. p += sizeof(type); \
  124. } while (0)
  125. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  126. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  127. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  128. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  129. #define read_byte_array(p, p_end, addr, len) \
  130. do { \
  131. CHECK_BUF(p, p_end, len); \
  132. memcpy(addr, p, len); \
  133. p += len; \
  134. } while (0)
  135. #define read_string(p, p_end, str) \
  136. do { \
  137. if (!(str = load_string((uint8 **)&p, p_end, module, \
  138. is_load_from_file_buf, error_buf, \
  139. error_buf_size))) \
  140. goto fail; \
  141. } while (0)
  142. #define csp_read_string(p, p_end, str) do { \
  143. uint16 str_len; \
  144. read_uint16(p, p_end, str_len); \
  145. CHECK_BUF(p, p_end, str_len); \
  146. if (!(str = wasm_runtime_records_const_string(\
  147. runtime, (char*)p, str_len, \
  148. error_buf, error_buf_size))) {\
  149. goto fail; \
  150. } \
  151. p += str_len; \
  152. } while (0)
  153. /* Legal values for bin_type */
  154. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  155. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  156. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  157. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  158. #define BIN_TYPE_COFF64 6 /* 64-bit little endian */
  159. /* Legal values for e_type (object file type). */
  160. #define E_TYPE_NONE 0 /* No file type */
  161. #define E_TYPE_REL 1 /* Relocatable file */
  162. #define E_TYPE_EXEC 2 /* Executable file */
  163. #define E_TYPE_DYN 3 /* Shared object file */
  164. /* Legal values for e_machine (architecture). */
  165. #define E_MACHINE_386 3 /* Intel 80386 */
  166. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  167. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  168. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  169. #define E_MACHINE_AARCH64 183 /* AArch64 */
  170. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  171. #define E_MACHINE_IA_64 50 /* Intel Merced */
  172. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  173. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  174. #define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */
  175. #define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
  176. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  177. #define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
  178. #define E_MACHINE_WIN_X86_64 0x8664 /* Windowx x86-64 architecture */
  179. /* Legal values for e_version */
  180. #define E_VERSION_CURRENT 1 /* Current version */
  181. static void *
  182. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  183. {
  184. void *mem;
  185. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  186. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  187. return NULL;
  188. }
  189. memset(mem, 0, (uint32)size);
  190. return mem;
  191. }
  192. static char *
  193. const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  194. char *error_buf, uint32 error_buf_size)
  195. {
  196. HashMap *set = module->const_str_set;
  197. char *c_str, *value;
  198. ConstStrDescription temp_key;
  199. ConstStrDescription * key = NULL;
  200. uint32 req_size = 0;
  201. temp_key.str = (void*)str;
  202. temp_key.len = len;
  203. temp_key.hash = 0;
  204. if ((value = bh_hash_map_find(set, &temp_key))) {
  205. return value;
  206. }
  207. //req_size = (uint32)len + 1 + CONST_STR_HASHMAP_KEY_HEAD_LEN;
  208. //req_size = (req_size > sizeof(ConstStrDescription)) ? req_size : sizeof(ConstStrDescription);
  209. req_size = sizeof(ConstStrDescription) + (uint32)len + 1;
  210. if (!(key = loader_malloc(req_size,
  211. error_buf, error_buf_size))) {
  212. return NULL;
  213. }
  214. //c_str = key->data;
  215. c_str = (char*)(key + 1);
  216. bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
  217. key->str = c_str;
  218. key->len = len;
  219. key->hash = temp_key.hash;
  220. if (!bh_hash_map_insert(set, key, c_str)) {
  221. set_error_buf(error_buf, error_buf_size,
  222. "insert string to hash map failed");
  223. wasm_runtime_free(key);
  224. return NULL;
  225. }
  226. return c_str;
  227. }
  228. static char *
  229. load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  230. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  231. {
  232. uint8 *p = *p_buf;
  233. const uint8 *p_end = buf_end;
  234. char *str;
  235. uint16 str_len;
  236. CHECK_BUF(p, p_end, 1);
  237. if (*p & 0x80) {
  238. /* The string has been adjusted */
  239. str = (char *)++p;
  240. /* Ensure the whole string is in range */
  241. do {
  242. CHECK_BUF(p, p_end, 1);
  243. } while (*p++ != '\0');
  244. }
  245. else {
  246. /* The string hasn't been adjusted */
  247. read_uint16(p, p_end, str_len);
  248. CHECK_BUF(p, p_end, str_len);
  249. if (str_len == 0) {
  250. str = "";
  251. }
  252. else if (p[str_len - 1] == '\0') {
  253. /* The string is terminated with '\0', use it directly */
  254. str = (char *)p;
  255. }
  256. else if (is_load_from_file_buf) {
  257. /* As the file buffer can be referred to after loading,
  258. we use the 2 bytes of size to adjust the string:
  259. mark the flag with the highest bit of size[0],
  260. move string 1 byte backward and then append '\0' */
  261. *(p - 2) |= 0x80;
  262. bh_memmove_s(p - 1, (uint32)(str_len + 1), p, (uint32)str_len);
  263. p[str_len - 1] = '\0';
  264. str = (char *)(p - 1);
  265. }
  266. else {
  267. /* Load from sections, the file buffer cannot be reffered to
  268. after loading, we must create another string and insert it
  269. into const string set */
  270. if (!(str = const_str_set_insert((uint8 *)p, str_len, module,
  271. error_buf, error_buf_size))) {
  272. goto fail;
  273. }
  274. }
  275. p += str_len;
  276. }
  277. *p_buf = p;
  278. return str;
  279. fail:
  280. return NULL;
  281. }
  282. static bool
  283. get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
  284. uint32 target_buf_size, char *error_buf,
  285. uint32 error_buf_size)
  286. {
  287. char *machine_type = NULL;
  288. switch (target_info->e_machine) {
  289. case E_MACHINE_X86_64:
  290. case E_MACHINE_WIN_X86_64:
  291. machine_type = "x86_64";
  292. break;
  293. case E_MACHINE_386:
  294. machine_type = "i386";
  295. break;
  296. case E_MACHINE_ARM:
  297. case E_MACHINE_AARCH64:
  298. machine_type = target_info->arch;
  299. break;
  300. case E_MACHINE_MIPS:
  301. machine_type = "mips";
  302. break;
  303. case E_MACHINE_XTENSA:
  304. machine_type = "xtensa";
  305. break;
  306. case E_MACHINE_RISCV:
  307. machine_type = "riscv";
  308. break;
  309. case E_MACHINE_ARC_COMPACT:
  310. case E_MACHINE_ARC_COMPACT2:
  311. machine_type = "arc";
  312. break;
  313. default:
  314. set_error_buf_v(error_buf, error_buf_size,
  315. "unknown machine type %d", target_info->e_machine);
  316. return false;
  317. }
  318. if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
  319. set_error_buf_v(
  320. error_buf, error_buf_size,
  321. "machine type (%s) isn't consistent with target type (%s)",
  322. machine_type, target_info->arch);
  323. return false;
  324. }
  325. snprintf(target_buf, target_buf_size, "%s", target_info->arch);
  326. return true;
  327. }
  328. static bool
  329. check_machine_info(AOTTargetInfo *target_info, char *error_buf,
  330. uint32 error_buf_size)
  331. {
  332. char target_expected[32], target_got[32];
  333. get_current_target(target_expected, sizeof(target_expected));
  334. if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
  335. error_buf, error_buf_size))
  336. return false;
  337. if (strncmp(target_expected, target_got, strlen(target_expected))) {
  338. set_error_buf_v(error_buf, error_buf_size,
  339. "invalid target type, expected %s but got %s",
  340. target_expected, target_got);
  341. return false;
  342. }
  343. return true;
  344. }
  345. static bool
  346. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  347. AOTModule *module, char *error_buf,
  348. uint32 error_buf_size)
  349. {
  350. AOTTargetInfo target_info;
  351. const uint8 *p = buf, *p_end = buf_end;
  352. bool is_target_little_endian, is_target_64_bit;
  353. read_uint16(p, p_end, target_info.bin_type);
  354. read_uint16(p, p_end, target_info.abi_type);
  355. read_uint16(p, p_end, target_info.e_type);
  356. read_uint16(p, p_end, target_info.e_machine);
  357. read_uint32(p, p_end, target_info.e_version);
  358. read_uint32(p, p_end, target_info.e_flags);
  359. read_uint32(p, p_end, target_info.reserved);
  360. read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
  361. if (p != buf_end) {
  362. set_error_buf(error_buf, error_buf_size, "invalid section size");
  363. return false;
  364. }
  365. /* Check target endian type */
  366. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  367. if (is_little_endian() != is_target_little_endian) {
  368. set_error_buf_v(error_buf, error_buf_size,
  369. "invalid target endian type, expected %s but got %s",
  370. is_little_endian() ? "little endian" : "big endian",
  371. is_target_little_endian ? "little endian"
  372. : "big endian");
  373. return false;
  374. }
  375. /* Check target bit width */
  376. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  377. if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) {
  378. set_error_buf_v(error_buf, error_buf_size,
  379. "invalid target bit width, expected %s but got %s",
  380. sizeof(void *) == 8 ? "64-bit" : "32-bit",
  381. is_target_64_bit ? "64-bit" : "32-bit");
  382. return false;
  383. }
  384. /* Check target elf file type */
  385. if (target_info.e_type != E_TYPE_REL) {
  386. set_error_buf(error_buf, error_buf_size,
  387. "invalid object file type, "
  388. "expected relocatable file type but got others");
  389. return false;
  390. }
  391. /* Check machine info */
  392. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  393. return false;
  394. }
  395. if (target_info.e_version != E_VERSION_CURRENT) {
  396. set_error_buf(error_buf, error_buf_size, "invalid elf file version");
  397. return false;
  398. }
  399. return true;
  400. fail:
  401. return false;
  402. }
  403. static void *
  404. get_native_symbol_by_name(const char *name)
  405. {
  406. void *func = NULL;
  407. uint32 symnum = 0;
  408. SymbolMap *sym = NULL;
  409. sym = get_target_symbol_map(&symnum);
  410. while (symnum--) {
  411. if (strcmp(sym->symbol_name, name) == 0) {
  412. func = sym->symbol_addr;
  413. break;
  414. }
  415. sym++;
  416. }
  417. return func;
  418. }
  419. static bool
  420. load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
  421. AOTModule *module, bool is_load_from_file_buf,
  422. char *error_buf, uint32 error_buf_size)
  423. {
  424. const uint8 *p = buf, *p_end = buf_end;
  425. uint32 cnt;
  426. int32 i;
  427. const char *symbol;
  428. read_uint32(p, p_end, cnt);
  429. module->native_symbol_count = cnt;
  430. if (cnt > 0) {
  431. module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
  432. if (module->native_symbol_list == NULL) {
  433. set_error_buf(error_buf, error_buf_size,
  434. "malloc native symbol list failed");
  435. goto fail;
  436. }
  437. for (i = cnt - 1; i >= 0; i--) {
  438. read_string(p, p_end, symbol);
  439. module->native_symbol_list[i] = get_native_symbol_by_name(symbol);
  440. if (module->native_symbol_list[i] == NULL) {
  441. set_error_buf_v(error_buf, error_buf_size,
  442. "missing native symbol: %s", symbol);
  443. goto fail;
  444. }
  445. }
  446. }
  447. return true;
  448. fail:
  449. return false;
  450. }
  451. static bool
  452. load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  453. bool is_load_from_file_buf, char *error_buf,
  454. uint32 error_buf_size)
  455. {
  456. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  457. const uint8 *p = buf, *p_end = buf_end;
  458. uint32 *aux_func_indexes;
  459. const char **aux_func_names;
  460. uint32 name_type, subsection_size;
  461. uint32 previous_name_type = 0;
  462. uint32 num_func_name;
  463. uint32 func_index;
  464. uint32 previous_func_index = ~0U;
  465. uint32 name_index;
  466. int i = 0;
  467. uint32 name_len;
  468. uint64 size;
  469. if (p >= p_end) {
  470. set_error_buf(error_buf, error_buf_size, "unexpected end");
  471. return false;
  472. }
  473. read_uint32(p, p_end, name_len);
  474. if (name_len != 4 || p + name_len > p_end) {
  475. set_error_buf(error_buf, error_buf_size, "unexpected end");
  476. return false;
  477. }
  478. if (memcmp(p, "name", 4) != 0) {
  479. set_error_buf(error_buf, error_buf_size, "invalid custom name section");
  480. return false;
  481. }
  482. p += name_len;
  483. while (p < p_end) {
  484. read_uint32(p, p_end, name_type);
  485. if (i != 0) {
  486. if (name_type == previous_name_type) {
  487. set_error_buf(error_buf, error_buf_size,
  488. "duplicate sub-section");
  489. return false;
  490. }
  491. if (name_type < previous_name_type) {
  492. set_error_buf(error_buf, error_buf_size,
  493. "out-of-order sub-section");
  494. return false;
  495. }
  496. }
  497. previous_name_type = name_type;
  498. read_uint32(p, p_end, subsection_size);
  499. CHECK_BUF(p, p_end, subsection_size);
  500. switch (name_type) {
  501. case SUB_SECTION_TYPE_FUNC:
  502. if (subsection_size) {
  503. read_uint32(p, p_end, num_func_name);
  504. if (num_func_name
  505. > module->import_func_count + module->func_count) {
  506. set_error_buf(error_buf, error_buf_size,
  507. "function name count out of bounds");
  508. return false;
  509. }
  510. module->aux_func_name_count = num_func_name;
  511. /* Allocate memory */
  512. size = sizeof(uint32) * (uint64)module->aux_func_name_count;
  513. if (!(aux_func_indexes = module->aux_func_indexes =
  514. loader_malloc(size, error_buf, error_buf_size))) {
  515. return false;
  516. }
  517. size =
  518. sizeof(char **) * (uint64)module->aux_func_name_count;
  519. if (!(aux_func_names = module->aux_func_names =
  520. loader_malloc(size, error_buf, error_buf_size))) {
  521. return false;
  522. }
  523. for (name_index = 0; name_index < num_func_name;
  524. name_index++) {
  525. read_uint32(p, p_end, func_index);
  526. if (name_index != 0
  527. && func_index == previous_func_index) {
  528. set_error_buf(error_buf, error_buf_size,
  529. "duplicate function name");
  530. return false;
  531. }
  532. if (name_index != 0
  533. && func_index < previous_func_index) {
  534. set_error_buf(error_buf, error_buf_size,
  535. "out-of-order function index ");
  536. return false;
  537. }
  538. if (func_index
  539. >= module->import_func_count + module->func_count) {
  540. set_error_buf(error_buf, error_buf_size,
  541. "function index out of bounds");
  542. return false;
  543. }
  544. previous_func_index = func_index;
  545. *(aux_func_indexes + name_index) = func_index;
  546. read_string(p, p_end, *(aux_func_names + name_index));
  547. #if 0
  548. LOG_DEBUG("func_index %u -> aux_func_name = %s\n",
  549. func_index, *(aux_func_names + name_index));
  550. #endif
  551. }
  552. }
  553. break;
  554. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  555. */
  556. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  557. default:
  558. p = p + subsection_size;
  559. break;
  560. }
  561. i++;
  562. }
  563. return true;
  564. fail:
  565. return false;
  566. #else
  567. return true;
  568. #endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
  569. }
  570. static bool
  571. load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  572. bool is_load_from_file_buf, char *error_buf,
  573. uint32 error_buf_size)
  574. {
  575. const uint8 *p = buf, *p_end = buf_end;
  576. uint32 sub_section_type;
  577. read_uint32(p, p_end, sub_section_type);
  578. buf = p;
  579. switch (sub_section_type) {
  580. case AOT_CUSTOM_SECTION_NATIVE_SYMBOL:
  581. if (!load_native_symbol_section(buf, buf_end, module,
  582. is_load_from_file_buf, error_buf,
  583. error_buf_size))
  584. goto fail;
  585. break;
  586. case AOT_CUSTOM_SECTION_NAME:
  587. if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
  588. error_buf, error_buf_size))
  589. goto fail;
  590. break;
  591. default:
  592. break;
  593. }
  594. return true;
  595. fail:
  596. return false;
  597. }
  598. static void
  599. destroy_import_memories(AOTImportMemory *import_memories, bool is_jit_mode)
  600. {
  601. if (!is_jit_mode)
  602. wasm_runtime_free(import_memories);
  603. }
  604. static void
  605. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
  606. bool is_jit_mode)
  607. {
  608. if (!is_jit_mode) {
  609. uint32 i;
  610. for (i = 0; i < count; i++)
  611. if (data_list[i])
  612. wasm_runtime_free(data_list[i]);
  613. wasm_runtime_free(data_list);
  614. }
  615. }
  616. static bool
  617. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  618. AOTModule *module, char *error_buf,
  619. uint32 error_buf_size)
  620. {
  621. const uint8 *buf = *p_buf;
  622. AOTMemInitData **data_list;
  623. uint64 size;
  624. uint32 i;
  625. /* Allocate memory */
  626. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  627. if (!(module->mem_init_data_list = data_list =
  628. loader_malloc(size, error_buf, error_buf_size))) {
  629. return false;
  630. }
  631. /* Create each memory data segment */
  632. for (i = 0; i < module->mem_init_data_count; i++) {
  633. uint32 init_expr_type, byte_count;
  634. uint64 init_expr_value;
  635. uint32 is_passive;
  636. uint32 memory_index;
  637. read_uint32(buf, buf_end, is_passive);
  638. read_uint32(buf, buf_end, memory_index);
  639. read_uint32(buf, buf_end, init_expr_type);
  640. read_uint64(buf, buf_end, init_expr_value);
  641. read_uint32(buf, buf_end, byte_count);
  642. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  643. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  644. return false;
  645. }
  646. #if WASM_ENABLE_BULK_MEMORY != 0
  647. /* is_passive and memory_index is only used in bulk memory mode */
  648. data_list[i]->is_passive = (bool)is_passive;
  649. data_list[i]->memory_index = memory_index;
  650. #endif
  651. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  652. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  653. data_list[i]->byte_count = byte_count;
  654. read_byte_array(buf, buf_end, data_list[i]->bytes,
  655. data_list[i]->byte_count);
  656. }
  657. *p_buf = buf;
  658. return true;
  659. fail:
  660. return false;
  661. }
  662. static bool
  663. load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  664. char *error_buf, uint32 error_buf_size)
  665. {
  666. uint32 i;
  667. uint64 total_size;
  668. const uint8 *buf = *p_buf;
  669. read_uint32(buf, buf_end, module->import_memory_count);
  670. /* We don't support import_memory_count > 0 currently */
  671. bh_assert(module->import_memory_count == 0);
  672. read_uint32(buf, buf_end, module->memory_count);
  673. total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
  674. if (!(module->memories =
  675. loader_malloc(total_size, error_buf, error_buf_size))) {
  676. return false;
  677. }
  678. for (i = 0; i < module->memory_count; i++) {
  679. read_uint32(buf, buf_end, module->memories[i].memory_flags);
  680. read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page);
  681. read_uint32(buf, buf_end, module->memories[i].mem_init_page_count);
  682. read_uint32(buf, buf_end, module->memories[i].mem_max_page_count);
  683. }
  684. read_uint32(buf, buf_end, module->mem_init_data_count);
  685. /* load memory init data list */
  686. if (module->mem_init_data_count > 0
  687. && !load_mem_init_data_list(&buf, buf_end, module, error_buf,
  688. error_buf_size))
  689. return false;
  690. *p_buf = buf;
  691. return true;
  692. fail:
  693. return false;
  694. }
  695. static void
  696. destroy_import_tables(AOTImportTable *import_tables, bool is_jit_mode)
  697. {
  698. if (!is_jit_mode)
  699. wasm_runtime_free(import_tables);
  700. }
  701. static void
  702. destroy_tables(AOTTable *tables, bool is_jit_mode)
  703. {
  704. if (!is_jit_mode)
  705. wasm_runtime_free(tables);
  706. }
  707. static void
  708. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
  709. bool is_jit_mode)
  710. {
  711. if (!is_jit_mode) {
  712. uint32 i;
  713. for (i = 0; i < count; i++)
  714. if (data_list[i])
  715. wasm_runtime_free(data_list[i]);
  716. wasm_runtime_free(data_list);
  717. }
  718. }
  719. static bool
  720. load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
  721. AOTModule *module, char *error_buf,
  722. uint32 error_buf_size)
  723. {
  724. const uint8 *buf = *p_buf;
  725. AOTImportTable *import_table;
  726. uint64 size;
  727. uint32 i, possible_grow;
  728. /* Allocate memory */
  729. size = sizeof(AOTImportTable) * (uint64)module->import_table_count;
  730. if (!(module->import_tables = import_table =
  731. loader_malloc(size, error_buf, error_buf_size))) {
  732. return false;
  733. }
  734. /* keep sync with aot_emit_table_info() aot_emit_aot_file */
  735. for (i = 0; i < module->import_table_count; i++, import_table++) {
  736. read_uint32(buf, buf_end, import_table->table_init_size);
  737. read_uint32(buf, buf_end, import_table->table_max_size);
  738. read_uint32(buf, buf_end, possible_grow);
  739. import_table->possible_grow = (possible_grow & 0x1);
  740. }
  741. *p_buf = buf;
  742. return true;
  743. fail:
  744. return false;
  745. }
  746. static bool
  747. load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  748. char *error_buf, uint32 error_buf_size)
  749. {
  750. const uint8 *buf = *p_buf;
  751. AOTTable *table;
  752. uint64 size;
  753. uint32 i, possible_grow;
  754. /* Allocate memory */
  755. size = sizeof(AOTTable) * (uint64)module->table_count;
  756. if (!(module->tables = table =
  757. loader_malloc(size, error_buf, error_buf_size))) {
  758. return false;
  759. }
  760. /* Create each table data segment */
  761. for (i = 0; i < module->table_count; i++, table++) {
  762. read_uint32(buf, buf_end, table->elem_type);
  763. read_uint32(buf, buf_end, table->table_flags);
  764. read_uint32(buf, buf_end, table->table_init_size);
  765. read_uint32(buf, buf_end, table->table_max_size);
  766. read_uint32(buf, buf_end, possible_grow);
  767. table->possible_grow = (possible_grow & 0x1);
  768. }
  769. *p_buf = buf;
  770. return true;
  771. fail:
  772. return false;
  773. }
  774. static bool
  775. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  776. AOTModule *module, char *error_buf,
  777. uint32 error_buf_size)
  778. {
  779. const uint8 *buf = *p_buf;
  780. AOTTableInitData **data_list;
  781. uint64 size;
  782. uint32 i;
  783. /* Allocate memory */
  784. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  785. if (!(module->table_init_data_list = data_list =
  786. loader_malloc(size, error_buf, error_buf_size))) {
  787. return false;
  788. }
  789. /* Create each table data segment */
  790. for (i = 0; i < module->table_init_data_count; i++) {
  791. uint32 mode, elem_type;
  792. uint32 table_index, init_expr_type, func_index_count;
  793. uint64 init_expr_value, size1;
  794. read_uint32(buf, buf_end, mode);
  795. read_uint32(buf, buf_end, elem_type);
  796. read_uint32(buf, buf_end, table_index);
  797. read_uint32(buf, buf_end, init_expr_type);
  798. read_uint64(buf, buf_end, init_expr_value);
  799. read_uint32(buf, buf_end, func_index_count);
  800. size1 = sizeof(uint32) * (uint64)func_index_count;
  801. size = offsetof(AOTTableInitData, func_indexes) + size1;
  802. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  803. return false;
  804. }
  805. data_list[i]->mode = mode;
  806. data_list[i]->elem_type = elem_type;
  807. data_list[i]->is_dropped = false;
  808. data_list[i]->table_index = table_index;
  809. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  810. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  811. data_list[i]->func_index_count = func_index_count;
  812. read_byte_array(buf, buf_end, data_list[i]->func_indexes,
  813. (uint32)size1);
  814. }
  815. *p_buf = buf;
  816. return true;
  817. fail:
  818. return false;
  819. }
  820. static bool
  821. load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  822. char *error_buf, uint32 error_buf_size)
  823. {
  824. const uint8 *buf = *p_buf;
  825. read_uint32(buf, buf_end, module->import_table_count);
  826. if (module->import_table_count > 0
  827. && !load_import_table_list(&buf, buf_end, module, error_buf,
  828. error_buf_size))
  829. return false;
  830. read_uint32(buf, buf_end, module->table_count);
  831. if (module->table_count > 0
  832. && !load_table_list(&buf, buf_end, module, error_buf, error_buf_size))
  833. return false;
  834. read_uint32(buf, buf_end, module->table_init_data_count);
  835. /* load table init data list */
  836. if (module->table_init_data_count > 0
  837. && !load_table_init_data_list(&buf, buf_end, module, error_buf,
  838. error_buf_size))
  839. return false;
  840. *p_buf = buf;
  841. return true;
  842. fail:
  843. return false;
  844. }
  845. static void
  846. destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
  847. {
  848. if (!is_jit_mode) {
  849. uint32 i;
  850. for (i = 0; i < count; i++)
  851. if (func_types[i])
  852. wasm_runtime_free(func_types[i]);
  853. wasm_runtime_free(func_types);
  854. }
  855. }
  856. static bool
  857. load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  858. char *error_buf, uint32 error_buf_size)
  859. {
  860. const uint8 *buf = *p_buf;
  861. AOTFuncType **func_types;
  862. uint64 size;
  863. uint32 i;
  864. /* Allocate memory */
  865. size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
  866. if (!(module->func_types = func_types =
  867. loader_malloc(size, error_buf, error_buf_size))) {
  868. return false;
  869. }
  870. /* Create each function type */
  871. for (i = 0; i < module->func_type_count; i++) {
  872. uint32 param_count, result_count;
  873. uint32 param_cell_num, ret_cell_num;
  874. uint64 size1;
  875. read_uint32(buf, buf_end, param_count);
  876. read_uint32(buf, buf_end, result_count);
  877. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  878. set_error_buf(error_buf, error_buf_size,
  879. "param count or result count too large");
  880. return false;
  881. }
  882. size1 = (uint64)param_count + (uint64)result_count;
  883. size = offsetof(AOTFuncType, types) + size1;
  884. if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) {
  885. return false;
  886. }
  887. func_types[i]->param_count = (uint16)param_count;
  888. func_types[i]->result_count = (uint16)result_count;
  889. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  890. param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count);
  891. ret_cell_num =
  892. wasm_get_cell_num(func_types[i]->types + param_count, result_count);
  893. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  894. set_error_buf(error_buf, error_buf_size,
  895. "param count or result count too large");
  896. return false;
  897. }
  898. func_types[i]->param_cell_num = (uint16)param_cell_num;
  899. func_types[i]->ret_cell_num = (uint16)ret_cell_num;
  900. }
  901. *p_buf = buf;
  902. return true;
  903. fail:
  904. return false;
  905. }
  906. static bool
  907. load_func_type_info(const uint8 **p_buf, const uint8 *buf_end,
  908. AOTModule *module, char *error_buf, uint32 error_buf_size)
  909. {
  910. const uint8 *buf = *p_buf;
  911. read_uint32(buf, buf_end, module->func_type_count);
  912. /* load function type */
  913. if (module->func_type_count > 0
  914. && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size))
  915. return false;
  916. *p_buf = buf;
  917. return true;
  918. fail:
  919. return false;
  920. }
  921. static void
  922. destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
  923. {
  924. if (!is_jit_mode)
  925. wasm_runtime_free(import_globals);
  926. }
  927. static bool
  928. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  929. AOTModule *module, bool is_load_from_file_buf,
  930. char *error_buf, uint32 error_buf_size)
  931. {
  932. WASMRuntime * runtime = module->runtime;
  933. const uint8 *buf = *p_buf;
  934. AOTImportGlobal *import_globals;
  935. uint64 size = 0;
  936. uint32 i, data_offset = 0;
  937. #if WASM_ENABLE_LIBC_BUILTIN != 0
  938. WASMGlobalImport tmp_global;
  939. #endif
  940. /* Allocate memory */
  941. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  942. if (!(module->import_globals = import_globals =
  943. loader_malloc(size, error_buf, error_buf_size))) {
  944. return false;
  945. }
  946. /* Create each import global */
  947. for (i = 0; i < module->import_global_count; i++) {
  948. buf = (uint8 *)align_ptr(buf, 2);
  949. read_uint8(buf, buf_end, import_globals[i].type);
  950. read_uint8(buf, buf_end, import_globals[i].is_mutable);
  951. //read_uint16(buf, buf_end, name_len);
  952. //CHECK_BUF(buf, buf_end, name_len);
  953. //if (!(key = wasm_runtime_records_const_string(module->runtime, (const char*)buf, name_len, error_buf, error_buf_size))) {
  954. // return false;
  955. //}
  956. //import_globals[i].module_name = key;
  957. //buf += name_len;
  958. //read_uint16(buf, buf_end, name_len);
  959. //CHECK_BUF(buf, buf_end, name_len);
  960. //if (!(key = wasm_runtime_records_const_string(module->runtime, (const char*)buf, name_len, error_buf, error_buf_size))) {
  961. // return false;
  962. //}
  963. //import_globals[i].global_name = key->str;
  964. //buf += name_len;
  965. csp_read_string(buf, buf_end, import_globals[i].module_name);
  966. csp_read_string(buf, buf_end, import_globals[i].global_name);
  967. //import_globals[i].global_name = key->str;
  968. #if WASM_ENABLE_LIBC_BUILTIN != 0
  969. if (wasm_native_lookup_libc_builtin_global(
  970. import_globals[i].module_name->str,
  971. import_globals[i].global_name->str,
  972. &tmp_global)) {
  973. if (tmp_global.type != import_globals[i].type
  974. || tmp_global.is_mutable != import_globals[i].is_mutable) {
  975. set_error_buf(error_buf, error_buf_size,
  976. "incompatible import type");
  977. return false;
  978. }
  979. bh_assert(import_globals[i].is_mutable == false);
  980. import_globals[i].global_data_linked =
  981. tmp_global.global_data_linked;
  982. }
  983. #endif
  984. if (!import_globals[i].is_mutable) {
  985. import_globals[i].size = wasm_value_type_size(import_globals[i].type);
  986. } else {
  987. // mutable import, a pointer will be saved into global data
  988. // Note: compiler also should generate global var access instr based on the same offset.
  989. import_globals[i].size = sizeof(void*);
  990. }
  991. import_globals[i].data_offset = data_offset;
  992. data_offset += import_globals[i].size;
  993. module->global_data_size += import_globals[i].size;
  994. }
  995. *p_buf = buf;
  996. return true;
  997. fail:
  998. return false;
  999. }
  1000. static bool
  1001. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  1002. AOTModule *module, bool is_load_from_file_buf,
  1003. char *error_buf, uint32 error_buf_size)
  1004. {
  1005. const uint8 *buf = *p_buf;
  1006. read_uint32(buf, buf_end, module->import_global_count);
  1007. /* load import globals */
  1008. if (module->import_global_count > 0
  1009. && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf,
  1010. error_buf, error_buf_size))
  1011. return false;
  1012. *p_buf = buf;
  1013. return true;
  1014. fail:
  1015. return false;
  1016. }
  1017. static void
  1018. destroy_globals(AOTGlobal *globals, bool is_jit_mode)
  1019. {
  1020. if (!is_jit_mode)
  1021. wasm_runtime_free(globals);
  1022. }
  1023. static bool
  1024. load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1025. char *error_buf, uint32 error_buf_size)
  1026. {
  1027. const uint8 *buf = *p_buf;
  1028. AOTGlobal *globals;
  1029. uint64 size;
  1030. uint32 i, data_offset = 0;
  1031. AOTImportGlobal *last_import_global;
  1032. /* Allocate memory */
  1033. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  1034. if (!(module->globals = globals =
  1035. loader_malloc(size, error_buf, error_buf_size))) {
  1036. return false;
  1037. }
  1038. if (module->import_global_count > 0) {
  1039. last_import_global =
  1040. &module->import_globals[module->import_global_count - 1];
  1041. data_offset =
  1042. last_import_global->data_offset + last_import_global->size;
  1043. }
  1044. /* Create each global */
  1045. for (i = 0; i < module->global_count; i++) {
  1046. uint16 init_expr_type;
  1047. read_uint8(buf, buf_end, globals[i].type);
  1048. read_uint8(buf, buf_end, globals[i].is_mutable);
  1049. read_uint16(buf, buf_end, init_expr_type);
  1050. if (init_expr_type != INIT_EXPR_TYPE_V128_CONST) {
  1051. read_uint64(buf, buf_end, globals[i].init_expr.u.i64);
  1052. }
  1053. else {
  1054. uint64 *i64x2 = (uint64 *)globals[i].init_expr.u.v128.i64x2;
  1055. CHECK_BUF(buf, buf_end, sizeof(uint64) * 2);
  1056. wasm_runtime_read_v128(buf, &i64x2[0], &i64x2[1]);
  1057. buf += sizeof(uint64) * 2;
  1058. }
  1059. globals[i].init_expr.init_expr_type = (uint8)init_expr_type;
  1060. globals[i].size = wasm_value_type_size(globals[i].type);
  1061. globals[i].data_offset = data_offset;
  1062. data_offset += globals[i].size;
  1063. module->global_data_size += globals[i].size;
  1064. }
  1065. *p_buf = buf;
  1066. return true;
  1067. fail:
  1068. return false;
  1069. }
  1070. static bool
  1071. load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1072. char *error_buf, uint32 error_buf_size)
  1073. {
  1074. const uint8 *buf = *p_buf;
  1075. read_uint32(buf, buf_end, module->global_count);
  1076. /* load globals */
  1077. if (module->global_count > 0
  1078. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  1079. return false;
  1080. *p_buf = buf;
  1081. return true;
  1082. fail:
  1083. return false;
  1084. }
  1085. static void
  1086. destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode)
  1087. {
  1088. if (!is_jit_mode)
  1089. wasm_runtime_free(import_funcs);
  1090. }
  1091. static bool
  1092. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1093. bool is_load_from_file_buf, char *error_buf,
  1094. uint32 error_buf_size)
  1095. {
  1096. WASMRuntime * runtime = module->runtime;
  1097. const uint8 *buf = *p_buf;
  1098. AOTImportFunc *import_funcs;
  1099. uint64 size;
  1100. uint32 i;
  1101. /* Allocate memory */
  1102. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  1103. if (!(module->import_funcs = import_funcs =
  1104. loader_malloc(size, error_buf, error_buf_size))) {
  1105. return false;
  1106. }
  1107. /* Create each import func */
  1108. for (i = 0; i < module->import_func_count; i++) {
  1109. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  1110. if (import_funcs[i].func_type_index >= module->func_type_count) {
  1111. set_error_buf(error_buf, error_buf_size, "unknown type");
  1112. return false;
  1113. }
  1114. import_funcs[i].func_type = module->func_types[import_funcs[i].func_type_index];
  1115. csp_read_string(buf, buf_end, import_funcs[i].module_name);
  1116. csp_read_string(buf, buf_end, import_funcs[i].func_name);
  1117. import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol(
  1118. import_funcs[i].module_name, &import_funcs[i].func_name, import_funcs[i].func_type,
  1119. &import_funcs[i].signature, &import_funcs[i].attachment,
  1120. &import_funcs[i].call_conv_raw);
  1121. #if WASM_ENABLE_LIBC_WASI != 0
  1122. if (import_funcs[i].module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_wasi_unstable)
  1123. || import_funcs[i].module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_wasi_snapshot_preview1))
  1124. module->is_wasi_module = true;
  1125. #endif
  1126. }
  1127. *p_buf = buf;
  1128. return true;
  1129. fail:
  1130. return false;
  1131. }
  1132. static bool
  1133. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  1134. AOTModule *module, bool is_load_from_file_buf,
  1135. char *error_buf, uint32 error_buf_size)
  1136. {
  1137. const uint8 *buf = *p_buf;
  1138. read_uint32(buf, buf_end, module->import_func_count);
  1139. /* load import funcs */
  1140. if (module->import_func_count > 0
  1141. && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf,
  1142. error_buf, error_buf_size))
  1143. return false;
  1144. *p_buf = buf;
  1145. return true;
  1146. fail:
  1147. return false;
  1148. }
  1149. static void
  1150. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  1151. uint32 data_section_count)
  1152. {
  1153. uint32 i;
  1154. AOTObjectDataSection *data_section = data_sections;
  1155. for (i = 0; i < data_section_count; i++, data_section++)
  1156. if (data_section->data)
  1157. os_munmap(data_section->data, data_section->size);
  1158. wasm_runtime_free(data_sections);
  1159. }
  1160. static bool
  1161. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  1162. AOTModule *module, bool is_load_from_file_buf,
  1163. char *error_buf, uint32 error_buf_size)
  1164. {
  1165. const uint8 *buf = *p_buf;
  1166. AOTObjectDataSection *data_sections;
  1167. uint64 size;
  1168. uint32 i;
  1169. /* Allocate memory */
  1170. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  1171. if (!(module->data_sections = data_sections =
  1172. loader_malloc(size, error_buf, error_buf_size))) {
  1173. return false;
  1174. }
  1175. /* Create each data section */
  1176. for (i = 0; i < module->data_section_count; i++) {
  1177. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
  1178. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1179. || defined(BUILD_TARGET_RISCV64_LP64D) \
  1180. || defined(BUILD_TARGET_RISCV64_LP64)
  1181. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1182. relocation for R_X86_64_32/32S/PC32 */
  1183. int map_flags = MMAP_MAP_32BIT;
  1184. #else
  1185. int map_flags = MMAP_MAP_NONE;
  1186. #endif
  1187. read_string(buf, buf_end, data_sections[i].name);
  1188. read_uint32(buf, buf_end, data_sections[i].size);
  1189. /* Allocate memory for data */
  1190. if (data_sections[i].size > 0
  1191. && !(data_sections[i].data = os_mmap(NULL, data_sections[i].size,
  1192. map_prot, map_flags))) {
  1193. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  1194. return false;
  1195. }
  1196. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1197. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  1198. && !defined(BH_PLATFORM_DARWIN)
  1199. /* address must be in the first 2 Gigabytes of
  1200. the process address space */
  1201. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  1202. #endif
  1203. #endif
  1204. read_byte_array(buf, buf_end, data_sections[i].data,
  1205. data_sections[i].size);
  1206. }
  1207. *p_buf = buf;
  1208. return true;
  1209. fail:
  1210. return false;
  1211. }
  1212. static bool
  1213. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  1214. AOTModule *module, bool is_load_from_file_buf,
  1215. char *error_buf, uint32 error_buf_size)
  1216. {
  1217. const uint8 *buf = *p_buf;
  1218. read_uint32(buf, buf_end, module->data_section_count);
  1219. /* load object data sections */
  1220. if (module->data_section_count > 0
  1221. && !load_object_data_sections(&buf, buf_end, module,
  1222. is_load_from_file_buf, error_buf,
  1223. error_buf_size))
  1224. return false;
  1225. *p_buf = buf;
  1226. return true;
  1227. fail:
  1228. return false;
  1229. }
  1230. static bool
  1231. load_dylink_section(const uint8 *buf, const uint8 *buf_end,
  1232. AOTModule *module,
  1233. char *error_buf, uint32 error_buf_size)
  1234. {
  1235. WASMRuntime * runtime = module->runtime;
  1236. const uint8 *p = buf, *p_end = buf_end;
  1237. uint32 size = buf_end - buf;
  1238. WASMDylinkSection * dylink_section = NULL;
  1239. WASMDylinkSection tmp_dylink_section;
  1240. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1241. const ConstStrDescription * key = NULL;
  1242. AOTModule* dependency_module = NULL;
  1243. #endif
  1244. memset(&tmp_dylink_section, 0, sizeof(WASMDylinkSection));
  1245. if (buf == buf_end)
  1246. return true;
  1247. if (size < (sizeof(uint32) * 5))
  1248. return false;
  1249. read_uint32(p, p_end, tmp_dylink_section.memory_size);
  1250. read_uint32(p, p_end, tmp_dylink_section.memory_alignment);
  1251. read_uint32(p, p_end, tmp_dylink_section.table_size);
  1252. read_uint32(p, p_end, tmp_dylink_section.memory_alignment);
  1253. read_uint32(p, p_end, tmp_dylink_section.needed_dylib_count);
  1254. // calculate the total size which all entries will take.
  1255. size = tmp_dylink_section.needed_dylib_count * sizeof(ConstStrDescription*);
  1256. size += sizeof(WASMDylinkSection);
  1257. dylink_section = (WASMDylinkSection*)loader_malloc(size, error_buf, error_buf_size);
  1258. if (!dylink_section)
  1259. return false;
  1260. bh_memcpy_s(dylink_section, sizeof(WASMDylinkSection), &tmp_dylink_section,
  1261. sizeof(WASMDylinkSection));
  1262. for (uint32 i = 0; i < dylink_section->needed_dylib_count; i ++) {
  1263. csp_read_string(p, p_end, dylink_section->needed_dylib_entries[i]);
  1264. //read_uint16(p, p_end, name_len);
  1265. //CHECK_BUF(p, p_end, name_len);
  1266. //key = wasm_runtime_records_const_string(runtime, (const char*)p, name_len, error_buf, error_buf_size);
  1267. //if (!key) {
  1268. // wasm_runtime_free(dylink_section);
  1269. // return false;
  1270. //}
  1271. //p += name_len;
  1272. //dylink_section->needed_dylib_entries[i] = key;
  1273. if (!runtime->config.need_load_dependencies)
  1274. continue;
  1275. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1276. key = dylink_section->needed_dylib_entries[i];
  1277. dependency_module = (AOTModule*)load_implicit_dependency_module((const WASMModuleCommon *)module,
  1278. (ConstStrDescription *)key,
  1279. error_buf, error_buf_size);
  1280. if (!dependency_module) {
  1281. return false;
  1282. }
  1283. #endif
  1284. }
  1285. module->dylink_section = dylink_section;
  1286. return true;
  1287. fail:
  1288. if (dylink_section)
  1289. wasm_runtime_free(dylink_section);
  1290. return false;
  1291. }
  1292. static bool
  1293. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  1294. AOTModule *module, bool is_load_from_file_buf,
  1295. char *error_buf, uint32 error_buf_size)
  1296. {
  1297. const uint8 *p = buf, *p_end = buf_end;
  1298. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  1299. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  1300. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  1301. || !load_import_global_info(&p, p_end, module, is_load_from_file_buf,
  1302. error_buf, error_buf_size)
  1303. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  1304. || !load_import_func_info(&p, p_end, module, is_load_from_file_buf,
  1305. error_buf, error_buf_size))
  1306. return false;
  1307. /* load function count and start function index */
  1308. read_uint32(p, p_end, module->func_count);
  1309. read_uint32(p, p_end, module->start_func_index);
  1310. /* check start function index */
  1311. if (module->start_func_index != (uint32)-1
  1312. && (module->start_func_index
  1313. >= module->import_func_count + module->func_count)) {
  1314. set_error_buf(error_buf, error_buf_size,
  1315. "invalid start function index");
  1316. return false;
  1317. }
  1318. read_uint32(p, p_end, module->aux_data_end_global_index);
  1319. read_uint32(p, p_end, module->aux_data_end);
  1320. read_uint32(p, p_end, module->aux_heap_base_global_index);
  1321. read_uint32(p, p_end, module->aux_heap_base);
  1322. read_uint32(p, p_end, module->aux_stack_top_global_index);
  1323. read_uint32(p, p_end, module->aux_stack_bottom);
  1324. read_uint32(p, p_end, module->aux_stack_size);
  1325. if (!load_object_data_sections_info(&p, p_end, module,
  1326. is_load_from_file_buf, error_buf,
  1327. error_buf_size))
  1328. return false;
  1329. if (p != p_end) {
  1330. set_error_buf(error_buf, error_buf_size,
  1331. "invalid init data section size");
  1332. return false;
  1333. }
  1334. return true;
  1335. fail:
  1336. return false;
  1337. }
  1338. static bool
  1339. load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1340. char *error_buf, uint32 error_buf_size)
  1341. {
  1342. uint8 *plt_base;
  1343. if (module->func_count > 0 && buf_end == buf) {
  1344. set_error_buf(error_buf, error_buf_size, "invalid code size");
  1345. return false;
  1346. }
  1347. /* The layout is: literal size + literal + code (with plt table) */
  1348. read_uint32(buf, buf_end, module->literal_size);
  1349. /* literal data is at beginning of the text section */
  1350. module->literal = (uint8 *)buf;
  1351. module->code = (void *)(buf + module->literal_size);
  1352. module->code_size = (uint32)(buf_end - (uint8 *)module->code);
  1353. #if WASM_ENABLE_DEBUG_AOT != 0
  1354. module->elf_size = module->code_size;
  1355. if (is_ELF(module->code)) {
  1356. /* Now code points to an ELF object, we pull it down to .text section */
  1357. uint64 offset;
  1358. uint64 size;
  1359. char *buf = module->code;
  1360. module->elf_hdr = buf;
  1361. if (!get_text_section(buf, &offset, &size)) {
  1362. set_error_buf(error_buf, error_buf_size,
  1363. "get text section of ELF failed");
  1364. return false;
  1365. }
  1366. module->code = buf + offset;
  1367. module->code_size -= (uint32)offset;
  1368. }
  1369. #endif
  1370. if ((module->code_size > 0) && (module->native_symbol_count == 0)) {
  1371. plt_base = (uint8 *)buf_end - get_plt_table_size();
  1372. init_plt_table(plt_base);
  1373. }
  1374. return true;
  1375. fail:
  1376. return false;
  1377. }
  1378. static bool
  1379. load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1380. char *error_buf, uint32 error_buf_size)
  1381. {
  1382. const uint8 *p = buf, *p_end = buf_end;
  1383. uint32 i;
  1384. uint64 size, text_offset;
  1385. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1386. RUNTIME_FUNCTION *rtl_func_table;
  1387. AOTUnwindInfo *unwind_info;
  1388. uint32 unwind_info_offset = module->code_size - sizeof(AOTUnwindInfo);
  1389. uint32 unwind_code_offset = unwind_info_offset - PLT_ITEM_SIZE;
  1390. #endif
  1391. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1392. unwind_info = (AOTUnwindInfo *)((uint8 *)module->code + module->code_size
  1393. - sizeof(AOTUnwindInfo));
  1394. unwind_info->Version = 1;
  1395. unwind_info->Flags = UNW_FLAG_NHANDLER;
  1396. *(uint32 *)&unwind_info->UnwindCode[0] = unwind_code_offset;
  1397. size = sizeof(RUNTIME_FUNCTION) * (uint64)module->func_count;
  1398. if (size > 0
  1399. && !(rtl_func_table = module->rtl_func_table =
  1400. loader_malloc(size, error_buf, error_buf_size))) {
  1401. return false;
  1402. }
  1403. #endif
  1404. size = sizeof(void *) * (uint64)module->func_count;
  1405. if (size > 0
  1406. && !(module->func_ptrs =
  1407. loader_malloc(size, error_buf, error_buf_size))) {
  1408. return false;
  1409. }
  1410. for (i = 0; i < module->func_count; i++) {
  1411. if (sizeof(void *) == 8) {
  1412. read_uint64(p, p_end, text_offset);
  1413. }
  1414. else {
  1415. uint32 text_offset32;
  1416. read_uint32(p, p_end, text_offset32);
  1417. text_offset = text_offset32;
  1418. }
  1419. if (text_offset >= module->code_size) {
  1420. set_error_buf(error_buf, error_buf_size,
  1421. "invalid function code offset");
  1422. return false;
  1423. }
  1424. module->func_ptrs[i] = (uint8 *)module->code + text_offset;
  1425. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1426. /* bits[0] of thumb function address must be 1 */
  1427. module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1);
  1428. #endif
  1429. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1430. rtl_func_table[i].BeginAddress = (DWORD)text_offset;
  1431. if (i > 0) {
  1432. rtl_func_table[i - 1].EndAddress = rtl_func_table[i].BeginAddress;
  1433. }
  1434. rtl_func_table[i].UnwindInfoAddress = (DWORD)unwind_info_offset;
  1435. #endif
  1436. }
  1437. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1438. if (module->func_count > 0) {
  1439. rtl_func_table[module->func_count - 1].EndAddress =
  1440. (DWORD)(module->code_size - get_plt_table_size());
  1441. if (!RtlAddFunctionTable(rtl_func_table, module->func_count,
  1442. (DWORD64)(uintptr_t)module->code)) {
  1443. set_error_buf(error_buf, error_buf_size,
  1444. "add dynamic function table failed");
  1445. return false;
  1446. }
  1447. module->rtl_func_table_registered = true;
  1448. }
  1449. #endif
  1450. /* Set start function when function pointers are resolved */
  1451. if (module->start_func_index != (uint32)-1) {
  1452. if (module->start_func_index >= module->import_func_count)
  1453. module->start_function =
  1454. module->func_ptrs[module->start_func_index
  1455. - module->import_func_count];
  1456. else
  1457. /* TODO: fix start function can be import function issue */
  1458. module->start_function = NULL;
  1459. }
  1460. else {
  1461. module->start_function = NULL;
  1462. }
  1463. size = sizeof(uint32) * (uint64)module->func_count;
  1464. if (size > 0
  1465. && !(module->func_type_indexes =
  1466. loader_malloc(size, error_buf, error_buf_size))) {
  1467. return false;
  1468. }
  1469. for (i = 0; i < module->func_count; i++) {
  1470. read_uint32(p, p_end, module->func_type_indexes[i]);
  1471. if (module->func_type_indexes[i] >= module->func_type_count) {
  1472. set_error_buf(error_buf, error_buf_size, "unknown type");
  1473. return false;
  1474. }
  1475. }
  1476. if (p != buf_end) {
  1477. set_error_buf(error_buf, error_buf_size,
  1478. "invalid function section size");
  1479. return false;
  1480. }
  1481. return true;
  1482. fail:
  1483. return false;
  1484. }
  1485. static void
  1486. destroy_exports(AOTExport *exports, bool is_jit_mode)
  1487. {
  1488. if (!is_jit_mode)
  1489. wasm_runtime_free(exports);
  1490. }
  1491. static bool
  1492. load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1493. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  1494. {
  1495. const uint8 *buf = *p_buf;
  1496. AOTExport *exports;
  1497. uint64 size;
  1498. uint32 i;
  1499. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1500. WASMRuntime * runtime = module->runtime;
  1501. #endif
  1502. /* Allocate memory */
  1503. size = sizeof(AOTExport) * (uint64)module->export_count;
  1504. if (!(module->exports = exports =
  1505. loader_malloc(size, error_buf, error_buf_size))) {
  1506. return false;
  1507. }
  1508. /* Create each export */
  1509. for (i = 0; i < module->export_count; i++) {
  1510. read_uint32(buf, buf_end, exports[i].index);
  1511. read_uint8(buf, buf_end, exports[i].kind);
  1512. read_string(buf, buf_end, exports[i].name);
  1513. switch (exports[i].kind) {
  1514. case EXPORT_KIND_FUNC:
  1515. module->export_func_count++;
  1516. break;
  1517. case EXPORT_KIND_GLOBAL:
  1518. module->export_global_count++;
  1519. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1520. if (!strcmp(exports[i].name, CONST_STR_POOL_STR(runtime, WAMR_CSP_var_stack_pointer)) ||
  1521. !strcmp(exports[i].name, CONST_STR_POOL_STR(runtime, WAMR_CSP_var_user_stack_pointer)))
  1522. module->export_sp_global_id = i;
  1523. #endif
  1524. break;
  1525. case EXPORT_KIND_TABLE:
  1526. module->export_tab_count++;
  1527. break;
  1528. case EXPORT_KIND_MEMORY:
  1529. module->export_mem_count++;
  1530. break;
  1531. default:
  1532. return false;
  1533. }
  1534. #if 0 /* TODO: check kind and index */
  1535. if (export_funcs[i].index >=
  1536. module->func_count + module->import_func_count) {
  1537. set_error_buf(error_buf, error_buf_size,
  1538. "function index is out of range");
  1539. return false;
  1540. }
  1541. #endif
  1542. }
  1543. *p_buf = buf;
  1544. return true;
  1545. fail:
  1546. return false;
  1547. }
  1548. static bool
  1549. load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1550. bool is_load_from_file_buf, char *error_buf,
  1551. uint32 error_buf_size)
  1552. {
  1553. const uint8 *p = buf, *p_end = buf_end;
  1554. /* load export functions */
  1555. read_uint32(p, p_end, module->export_count);
  1556. if (module->export_count > 0
  1557. && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf,
  1558. error_buf_size))
  1559. return false;
  1560. if (p != p_end) {
  1561. set_error_buf(error_buf, error_buf_size, "invalid export section size");
  1562. return false;
  1563. }
  1564. return true;
  1565. fail:
  1566. return false;
  1567. }
  1568. static void *
  1569. get_data_section_addr(AOTModule *module, const char *section_name,
  1570. uint32 *p_data_size)
  1571. {
  1572. uint32 i;
  1573. AOTObjectDataSection *data_section = module->data_sections;
  1574. for (i = 0; i < module->data_section_count; i++, data_section++) {
  1575. if (!strcmp(data_section->name, section_name)) {
  1576. if (p_data_size)
  1577. *p_data_size = data_section->size;
  1578. return data_section->data;
  1579. }
  1580. }
  1581. return NULL;
  1582. }
  1583. static void *
  1584. resolve_target_sym(const char *symbol, int32 *p_index)
  1585. {
  1586. uint32 i, num = 0;
  1587. SymbolMap *target_sym_map;
  1588. if (!(target_sym_map = get_target_symbol_map(&num)))
  1589. return NULL;
  1590. for (i = 0; i < num; i++)
  1591. if (!strcmp(target_sym_map[i].symbol_name, symbol)) {
  1592. *p_index = (int32)i;
  1593. return target_sym_map[i].symbol_addr;
  1594. }
  1595. return NULL;
  1596. }
  1597. static bool
  1598. is_literal_relocation(const char *reloc_sec_name)
  1599. {
  1600. return !strcmp(reloc_sec_name, ".rela.literal");
  1601. }
  1602. #if defined(BH_PLATFORM_WINDOWS)
  1603. static bool
  1604. str2uint32(const char *buf, uint32 *p_res)
  1605. {
  1606. uint32 res = 0, val;
  1607. const char *buf_end = buf + 8;
  1608. char ch;
  1609. while (buf < buf_end) {
  1610. ch = *buf++;
  1611. if (ch >= '0' && ch <= '9')
  1612. val = ch - '0';
  1613. else if (ch >= 'a' && ch <= 'f')
  1614. val = ch - 'a' + 0xA;
  1615. else if (ch >= 'A' && ch <= 'F')
  1616. val = ch - 'A' + 0xA;
  1617. else
  1618. return false;
  1619. res = (res << 4) | val;
  1620. }
  1621. *p_res = res;
  1622. return true;
  1623. }
  1624. static bool
  1625. str2uint64(const char *buf, uint64 *p_res)
  1626. {
  1627. uint64 res = 0, val;
  1628. const char *buf_end = buf + 16;
  1629. char ch;
  1630. while (buf < buf_end) {
  1631. ch = *buf++;
  1632. if (ch >= '0' && ch <= '9')
  1633. val = ch - '0';
  1634. else if (ch >= 'a' && ch <= 'f')
  1635. val = ch - 'a' + 0xA;
  1636. else if (ch >= 'A' && ch <= 'F')
  1637. val = ch - 'A' + 0xA;
  1638. else
  1639. return false;
  1640. res = (res << 4) | val;
  1641. }
  1642. *p_res = res;
  1643. return true;
  1644. }
  1645. #endif
  1646. static bool
  1647. do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
  1648. char *error_buf, uint32 error_buf_size)
  1649. {
  1650. bool is_literal = is_literal_relocation(group->section_name);
  1651. uint8 *aot_text = is_literal ? module->literal : module->code;
  1652. uint32 aot_text_size =
  1653. is_literal ? module->literal_size : module->code_size;
  1654. uint32 i, func_index, symbol_len;
  1655. #if defined(BH_PLATFORM_WINDOWS)
  1656. uint32 xmm_plt_index = 0, real_plt_index = 0, float_plt_index = 0;
  1657. #endif
  1658. char symbol_buf[128] = { 0 }, *symbol, *p;
  1659. void *symbol_addr;
  1660. AOTRelocation *relocation = group->relocations;
  1661. if (group->relocation_count > 0 && !aot_text) {
  1662. set_error_buf(error_buf, error_buf_size,
  1663. "invalid text relocation count");
  1664. return false;
  1665. }
  1666. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1667. int32 symbol_index = -1;
  1668. symbol_len = (uint32)strlen(relocation->symbol_name);
  1669. if (symbol_len + 1 <= sizeof(symbol_buf))
  1670. symbol = symbol_buf;
  1671. else {
  1672. if (!(symbol = loader_malloc(symbol_len + 1, error_buf,
  1673. error_buf_size))) {
  1674. return false;
  1675. }
  1676. }
  1677. memcpy(symbol, relocation->symbol_name, symbol_len);
  1678. symbol[symbol_len] = '\0';
  1679. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1680. p = symbol + strlen(AOT_FUNC_PREFIX);
  1681. if (*p == '\0'
  1682. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1683. set_error_buf_v(error_buf, error_buf_size,
  1684. "invalid import symbol %s", symbol);
  1685. goto check_symbol_fail;
  1686. }
  1687. symbol_addr = module->func_ptrs[func_index];
  1688. }
  1689. else if (!strcmp(symbol, ".text")) {
  1690. symbol_addr = module->code;
  1691. }
  1692. else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata")
  1693. || !strcmp(symbol, ".rdata")
  1694. || !strcmp(symbol, ".rodata")
  1695. /* ".rodata.cst4/8/16/.." */
  1696. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) {
  1697. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1698. if (!symbol_addr) {
  1699. set_error_buf_v(error_buf, error_buf_size,
  1700. "invalid data section (%s)", symbol);
  1701. goto check_symbol_fail;
  1702. }
  1703. }
  1704. else if (!strcmp(symbol, ".literal")) {
  1705. symbol_addr = module->literal;
  1706. }
  1707. #if defined(BH_PLATFORM_WINDOWS)
  1708. /* Relocation for symbols which start with "__xmm@" or "__real@" and
  1709. end with the xmm value or real value. In Windows PE file, the data
  1710. is stored in some individual ".rdata" sections. We simply create
  1711. extra plt data, parse the values from the symbols and stored them
  1712. into the extra plt data. */
  1713. else if (!strcmp(group->section_name, ".text")
  1714. && !strncmp(symbol, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX))
  1715. && strlen(symbol) == strlen(XMM_PLT_PREFIX) + 32) {
  1716. char xmm_buf[17] = { 0 };
  1717. symbol_addr = module->extra_plt_data + xmm_plt_index * 16;
  1718. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  1719. symbol + strlen(XMM_PLT_PREFIX) + 16, 16);
  1720. if (!str2uint64(xmm_buf, (uint64 *)symbol_addr)) {
  1721. set_error_buf_v(error_buf, error_buf_size,
  1722. "resolve symbol %s failed", symbol);
  1723. goto check_symbol_fail;
  1724. }
  1725. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  1726. symbol + strlen(XMM_PLT_PREFIX), 16);
  1727. if (!str2uint64(xmm_buf, (uint64 *)((uint8 *)symbol_addr + 8))) {
  1728. set_error_buf_v(error_buf, error_buf_size,
  1729. "resolve symbol %s failed", symbol);
  1730. goto check_symbol_fail;
  1731. }
  1732. xmm_plt_index++;
  1733. }
  1734. else if (!strcmp(group->section_name, ".text")
  1735. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1736. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 16) {
  1737. char real_buf[17] = { 0 };
  1738. symbol_addr = module->extra_plt_data + module->xmm_plt_count * 16
  1739. + real_plt_index * 8;
  1740. bh_memcpy_s(real_buf, sizeof(real_buf),
  1741. symbol + strlen(REAL_PLT_PREFIX), 16);
  1742. if (!str2uint64(real_buf, (uint64 *)symbol_addr)) {
  1743. set_error_buf_v(error_buf, error_buf_size,
  1744. "resolve symbol %s failed", symbol);
  1745. goto check_symbol_fail;
  1746. }
  1747. real_plt_index++;
  1748. }
  1749. else if (!strcmp(group->section_name, ".text")
  1750. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1751. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 8) {
  1752. char float_buf[9] = { 0 };
  1753. symbol_addr = module->extra_plt_data + module->xmm_plt_count * 16
  1754. + module->real_plt_count * 8 + float_plt_index * 4;
  1755. bh_memcpy_s(float_buf, sizeof(float_buf),
  1756. symbol + strlen(REAL_PLT_PREFIX), 8);
  1757. if (!str2uint32(float_buf, (uint32 *)symbol_addr)) {
  1758. set_error_buf_v(error_buf, error_buf_size,
  1759. "resolve symbol %s failed", symbol);
  1760. goto check_symbol_fail;
  1761. }
  1762. float_plt_index++;
  1763. }
  1764. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1765. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1766. set_error_buf_v(error_buf, error_buf_size,
  1767. "resolve symbol %s failed", symbol);
  1768. goto check_symbol_fail;
  1769. }
  1770. if (symbol != symbol_buf)
  1771. wasm_runtime_free(symbol);
  1772. if (!apply_relocation(
  1773. module, aot_text, aot_text_size, relocation->relocation_offset,
  1774. relocation->relocation_addend, relocation->relocation_type,
  1775. symbol_addr, symbol_index, error_buf, error_buf_size))
  1776. return false;
  1777. }
  1778. return true;
  1779. check_symbol_fail:
  1780. if (symbol != symbol_buf)
  1781. wasm_runtime_free(symbol);
  1782. return false;
  1783. }
  1784. static bool
  1785. do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
  1786. char *error_buf, uint32 error_buf_size)
  1787. {
  1788. uint8 *data_addr;
  1789. uint32 data_size = 0, i;
  1790. AOTRelocation *relocation = group->relocations;
  1791. void *symbol_addr;
  1792. char *symbol, *data_section_name;
  1793. if (!strncmp(group->section_name, ".rela.", 6)) {
  1794. data_section_name = group->section_name + strlen(".rela");
  1795. }
  1796. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1797. data_section_name = group->section_name + strlen(".rel");
  1798. }
  1799. else if (!strcmp(group->section_name, ".rdata")) {
  1800. data_section_name = group->section_name;
  1801. }
  1802. else {
  1803. set_error_buf(error_buf, error_buf_size,
  1804. "invalid data relocation section name");
  1805. return false;
  1806. }
  1807. data_addr = get_data_section_addr(module, data_section_name, &data_size);
  1808. if (group->relocation_count > 0 && !data_addr) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "invalid data relocation count");
  1811. return false;
  1812. }
  1813. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1814. symbol = relocation->symbol_name;
  1815. if (!strcmp(symbol, ".text")) {
  1816. symbol_addr = module->code;
  1817. }
  1818. else {
  1819. set_error_buf_v(error_buf, error_buf_size,
  1820. "invalid relocation symbol %s", symbol);
  1821. return false;
  1822. }
  1823. if (!apply_relocation(
  1824. module, data_addr, data_size, relocation->relocation_offset,
  1825. relocation->relocation_addend, relocation->relocation_type,
  1826. symbol_addr, -1, error_buf, error_buf_size))
  1827. return false;
  1828. }
  1829. return true;
  1830. }
  1831. static bool
  1832. validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count,
  1833. char *error_buf, uint32 error_buf_size)
  1834. {
  1835. uint32 i, str_len_addr = 0;
  1836. uint16 str_len;
  1837. for (i = 0; i < count; i++) {
  1838. if (offsets[i] != str_len_addr)
  1839. return false;
  1840. read_uint16(buf, buf_end, str_len);
  1841. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1842. str_len_addr = align_uint(str_len_addr, 2);
  1843. buf += str_len;
  1844. buf = (uint8 *)align_ptr(buf, 2);
  1845. }
  1846. if (buf == buf_end)
  1847. return true;
  1848. fail:
  1849. return false;
  1850. }
  1851. static bool
  1852. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1853. AOTModule *module, bool is_load_from_file_buf,
  1854. char *error_buf, uint32 error_buf_size)
  1855. {
  1856. AOTRelocationGroup *groups = NULL, *group;
  1857. uint32 symbol_count = 0;
  1858. uint32 group_count = 0, i, j;
  1859. uint64 size;
  1860. uint32 *symbol_offsets, total_string_len;
  1861. uint8 *symbol_buf, *symbol_buf_end;
  1862. int map_prot, map_flags;
  1863. bool ret = false;
  1864. read_uint32(buf, buf_end, symbol_count);
  1865. symbol_offsets = (uint32 *)buf;
  1866. for (i = 0; i < symbol_count; i++) {
  1867. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1868. buf += sizeof(uint32);
  1869. }
  1870. read_uint32(buf, buf_end, total_string_len);
  1871. symbol_buf = (uint8 *)buf;
  1872. symbol_buf_end = symbol_buf + total_string_len;
  1873. if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets,
  1874. symbol_count, error_buf, error_buf_size)) {
  1875. set_error_buf(error_buf, error_buf_size,
  1876. "validate symbol table failed");
  1877. goto fail;
  1878. }
  1879. #if defined(BH_PLATFORM_WINDOWS)
  1880. buf = symbol_buf_end;
  1881. read_uint32(buf, buf_end, group_count);
  1882. for (i = 0; i < group_count; i++) {
  1883. uint32 name_index, relocation_count;
  1884. uint16 group_name_len;
  1885. uint8 *group_name;
  1886. /* section name address is 4 bytes aligned. */
  1887. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1888. read_uint32(buf, buf_end, name_index);
  1889. if (name_index >= symbol_count) {
  1890. set_error_buf(error_buf, error_buf_size,
  1891. "symbol index out of range");
  1892. goto fail;
  1893. }
  1894. group_name = symbol_buf + symbol_offsets[name_index];
  1895. group_name_len = *(uint16 *)group_name;
  1896. group_name += sizeof(uint16);
  1897. read_uint32(buf, buf_end, relocation_count);
  1898. for (j = 0; j < relocation_count; j++) {
  1899. AOTRelocation relocation = { 0 };
  1900. uint32 symbol_index, offset32;
  1901. int32 addend32;
  1902. uint16 symbol_name_len;
  1903. uint8 *symbol_name;
  1904. if (sizeof(void *) == 8) {
  1905. read_uint64(buf, buf_end, relocation.relocation_offset);
  1906. read_uint64(buf, buf_end, relocation.relocation_addend);
  1907. }
  1908. else {
  1909. read_uint32(buf, buf_end, offset32);
  1910. relocation.relocation_offset = (uint64)offset32;
  1911. read_uint32(buf, buf_end, addend32);
  1912. relocation.relocation_addend = (int64)addend32;
  1913. }
  1914. read_uint32(buf, buf_end, relocation.relocation_type);
  1915. read_uint32(buf, buf_end, symbol_index);
  1916. if (symbol_index >= symbol_count) {
  1917. set_error_buf(error_buf, error_buf_size,
  1918. "symbol index out of range");
  1919. goto fail;
  1920. }
  1921. symbol_name = symbol_buf + symbol_offsets[symbol_index];
  1922. symbol_name_len = *(uint16 *)symbol_name;
  1923. symbol_name += sizeof(uint16);
  1924. char group_name_buf[128] = { 0 };
  1925. char symbol_name_buf[128] = { 0 };
  1926. memcpy(group_name_buf, group_name, group_name_len);
  1927. memcpy(symbol_name_buf, symbol_name, symbol_name_len);
  1928. if (group_name_len == strlen(".text")
  1929. && !strncmp(group_name, ".text", strlen(".text"))) {
  1930. if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
  1931. && !strncmp(symbol_name, XMM_PLT_PREFIX,
  1932. strlen(XMM_PLT_PREFIX))) {
  1933. module->xmm_plt_count++;
  1934. }
  1935. else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
  1936. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1937. strlen(REAL_PLT_PREFIX))) {
  1938. module->real_plt_count++;
  1939. }
  1940. else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 8
  1941. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1942. strlen(REAL_PLT_PREFIX))) {
  1943. module->float_plt_count++;
  1944. }
  1945. }
  1946. }
  1947. }
  1948. /* Allocate memory for extra plt data */
  1949. size = sizeof(uint64) * 2 * module->xmm_plt_count
  1950. + sizeof(uint64) * module->real_plt_count
  1951. + sizeof(uint32) * module->float_plt_count;
  1952. if (size > 0) {
  1953. map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  1954. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1955. relocation for R_X86_64_32/32S/PC32 */
  1956. map_flags = MMAP_MAP_32BIT;
  1957. if (size > UINT32_MAX
  1958. || !(module->extra_plt_data =
  1959. os_mmap(NULL, (uint32)size, map_prot, map_flags))) {
  1960. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  1961. goto fail;
  1962. }
  1963. module->extra_plt_data_size = (uint32)size;
  1964. }
  1965. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1966. buf = symbol_buf_end;
  1967. read_uint32(buf, buf_end, group_count);
  1968. /* Allocate memory for relocation groups */
  1969. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1970. if (size > 0
  1971. && !(groups = loader_malloc(size, error_buf, error_buf_size))) {
  1972. goto fail;
  1973. }
  1974. /* Load each relocation group */
  1975. for (i = 0, group = groups; i < group_count; i++, group++) {
  1976. AOTRelocation *relocation;
  1977. uint32 name_index;
  1978. uint8 *name_addr;
  1979. /* section name address is 4 bytes aligned. */
  1980. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1981. read_uint32(buf, buf_end, name_index);
  1982. if (name_index >= symbol_count) {
  1983. set_error_buf(error_buf, error_buf_size,
  1984. "symbol index out of range");
  1985. goto fail;
  1986. }
  1987. name_addr = symbol_buf + symbol_offsets[name_index];
  1988. read_string(name_addr, buf_end, group->section_name);
  1989. read_uint32(buf, buf_end, group->relocation_count);
  1990. /* Allocate memory for relocations */
  1991. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1992. if (!(group->relocations = relocation =
  1993. loader_malloc(size, error_buf, error_buf_size))) {
  1994. ret = false;
  1995. goto fail;
  1996. }
  1997. /* Load each relocation */
  1998. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1999. uint32 symbol_index;
  2000. uint8 *symbol_addr;
  2001. if (sizeof(void *) == 8) {
  2002. read_uint64(buf, buf_end, relocation->relocation_offset);
  2003. read_uint64(buf, buf_end, relocation->relocation_addend);
  2004. }
  2005. else {
  2006. uint32 offset32, addend32;
  2007. read_uint32(buf, buf_end, offset32);
  2008. relocation->relocation_offset = (uint64)offset32;
  2009. read_uint32(buf, buf_end, addend32);
  2010. relocation->relocation_addend = (uint64)addend32;
  2011. }
  2012. read_uint32(buf, buf_end, relocation->relocation_type);
  2013. read_uint32(buf, buf_end, symbol_index);
  2014. if (symbol_index >= symbol_count) {
  2015. set_error_buf(error_buf, error_buf_size,
  2016. "symbol index out of range");
  2017. goto fail;
  2018. }
  2019. symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  2020. read_string(symbol_addr, buf_end, relocation->symbol_name);
  2021. }
  2022. if (!strcmp(group->section_name, ".rel.text")
  2023. || !strcmp(group->section_name, ".rela.text")
  2024. || !strcmp(group->section_name, ".rela.literal")
  2025. #ifdef BH_PLATFORM_WINDOWS
  2026. || !strcmp(group->section_name, ".text")
  2027. #endif
  2028. ) {
  2029. if (module->native_symbol_count > 0) {
  2030. set_error_buf(error_buf, error_buf_size,
  2031. "cannot apply relocation to text section "
  2032. "for aot file generated with "
  2033. "\"--enable-indirect-mode\" flag");
  2034. goto fail;
  2035. }
  2036. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  2037. goto fail;
  2038. }
  2039. else {
  2040. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  2041. goto fail;
  2042. }
  2043. }
  2044. /* Set read only for AOT code and some data sections */
  2045. map_prot = MMAP_PROT_READ | MMAP_PROT_EXEC;
  2046. if (module->code) {
  2047. /* The layout is: literal size + literal + code (with plt table) */
  2048. uint8 *mmap_addr = module->literal - sizeof(uint32);
  2049. uint32 total_size =
  2050. sizeof(uint32) + module->literal_size + module->code_size;
  2051. os_mprotect(mmap_addr, total_size, map_prot);
  2052. }
  2053. map_prot = MMAP_PROT_READ;
  2054. #if defined(BH_PLATFORM_WINDOWS)
  2055. if (module->extra_plt_data) {
  2056. os_mprotect(module->extra_plt_data, module->extra_plt_data_size,
  2057. map_prot);
  2058. }
  2059. #endif
  2060. for (i = 0; i < module->data_section_count; i++) {
  2061. AOTObjectDataSection *data_section = module->data_sections + i;
  2062. if (!strcmp(data_section->name, ".rdata")
  2063. || !strcmp(data_section->name, ".rodata")
  2064. /* ".rodata.cst4/8/16/.." */
  2065. || !strncmp(data_section->name, ".rodata.cst",
  2066. strlen(".rodata.cst"))) {
  2067. os_mprotect(data_section->data, data_section->size, map_prot);
  2068. }
  2069. }
  2070. ret = true;
  2071. fail:
  2072. if (groups) {
  2073. for (i = 0, group = groups; i < group_count; i++, group++)
  2074. if (group->relocations)
  2075. wasm_runtime_free(group->relocations);
  2076. wasm_runtime_free(groups);
  2077. }
  2078. (void)map_flags;
  2079. return ret;
  2080. }
  2081. static bool
  2082. load_from_sections(AOTModule *module, AOTSection *sections,
  2083. bool is_load_from_file_buf, char *error_buf,
  2084. uint32 error_buf_size)
  2085. {
  2086. AOTSection *section = sections;
  2087. const uint8 *buf, *buf_end;
  2088. uint32 last_section_type = (uint32)-1, section_type;
  2089. uint32 i, func_index, func_type_index;
  2090. AOTFuncType *func_type;
  2091. AOTExport *exports;
  2092. while (section) {
  2093. buf = section->section_body;
  2094. buf_end = buf + section->section_body_size;
  2095. /* Check sections */
  2096. section_type = (uint32)section->section_type;
  2097. if ((last_section_type == (uint32)-1
  2098. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  2099. || (last_section_type != (uint32)-1
  2100. && (section_type != last_section_type + 1
  2101. && section_type != AOT_SECTION_TYPE_CUSTOM))) {
  2102. set_error_buf(error_buf, error_buf_size, "invalid section order");
  2103. return false;
  2104. }
  2105. last_section_type = section_type;
  2106. switch (section_type) {
  2107. case AOT_SECTION_TYPE_TARGET_INFO:
  2108. if (!load_target_info_section(buf, buf_end, module, error_buf,
  2109. error_buf_size))
  2110. return false;
  2111. break;
  2112. case AOT_SECTION_TYPE_DYLINK:
  2113. if (!load_dylink_section(buf, buf_end, module,
  2114. error_buf, error_buf_size))
  2115. return false;
  2116. break;
  2117. case AOT_SECTION_TYPE_INIT_DATA:
  2118. if (!load_init_data_section(buf, buf_end, module,
  2119. is_load_from_file_buf, error_buf,
  2120. error_buf_size))
  2121. return false;
  2122. break;
  2123. case AOT_SECTION_TYPE_TEXT:
  2124. if (!load_text_section(buf, buf_end, module, error_buf,
  2125. error_buf_size))
  2126. return false;
  2127. break;
  2128. case AOT_SECTION_TYPE_FUNCTION:
  2129. if (!load_function_section(buf, buf_end, module, error_buf,
  2130. error_buf_size))
  2131. return false;
  2132. break;
  2133. case AOT_SECTION_TYPE_EXPORT:
  2134. if (!load_export_section(buf, buf_end, module,
  2135. is_load_from_file_buf, error_buf,
  2136. error_buf_size))
  2137. return false;
  2138. break;
  2139. case AOT_SECTION_TYPE_RELOCATION:
  2140. if (!load_relocation_section(buf, buf_end, module,
  2141. is_load_from_file_buf, error_buf,
  2142. error_buf_size))
  2143. return false;
  2144. break;
  2145. case AOT_SECTION_TYPE_CUSTOM:
  2146. if (!load_custom_section(buf, buf_end, module,
  2147. is_load_from_file_buf, error_buf,
  2148. error_buf_size))
  2149. return false;
  2150. break;
  2151. default:
  2152. set_error_buf(error_buf, error_buf_size,
  2153. "invalid aot section type");
  2154. return false;
  2155. }
  2156. section = section->next;
  2157. }
  2158. if (last_section_type != AOT_SECTION_TYPE_RELOCATION
  2159. && last_section_type != AOT_SECTION_TYPE_CUSTOM) {
  2160. set_error_buf(error_buf, error_buf_size, "section missing");
  2161. return false;
  2162. }
  2163. /* Resolve malloc and free function */
  2164. module->malloc_func_index = (uint32)-1;
  2165. module->free_func_index = (uint32)-1;
  2166. module->retain_func_index = (uint32)-1;
  2167. exports = module->exports;
  2168. for (i = 0; i < module->export_count; i++) {
  2169. if (exports[i].kind == EXPORT_KIND_FUNC
  2170. && exports[i].index >= module->import_func_count) {
  2171. if (!strcmp(exports[i].name, "malloc")) {
  2172. func_index = exports[i].index - module->import_func_count;
  2173. func_type_index = module->func_type_indexes[func_index];
  2174. func_type = module->func_types[func_type_index];
  2175. if (func_type->param_count == 1 && func_type->result_count == 1
  2176. && func_type->types[0] == VALUE_TYPE_I32
  2177. && func_type->types[1] == VALUE_TYPE_I32) {
  2178. bh_assert(module->malloc_func_index == (uint32)-1);
  2179. module->malloc_func_index = exports[i].index ;
  2180. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2181. exports[i].name, exports[i].index);
  2182. }
  2183. }
  2184. else if (!strcmp(exports[i].name, "__new")) {
  2185. func_index = exports[i].index - module->import_func_count;
  2186. func_type_index = module->func_type_indexes[func_index];
  2187. func_type = module->func_types[func_type_index];
  2188. if (func_type->param_count == 2 && func_type->result_count == 1
  2189. && func_type->types[0] == VALUE_TYPE_I32
  2190. && func_type->types[1] == VALUE_TYPE_I32
  2191. && func_type->types[2] == VALUE_TYPE_I32) {
  2192. uint32 j;
  2193. WASMExport *export_tmp;
  2194. bh_assert(module->malloc_func_index == (uint32)-1);
  2195. module->malloc_func_index = exports[i].index;
  2196. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2197. exports[i].name, exports[i].index);
  2198. /* resolve retain function.
  2199. If not find, reset malloc function index */
  2200. export_tmp = module->exports;
  2201. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2202. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2203. && (!strcmp(export_tmp->name, "__retain")
  2204. || !strcmp(export_tmp->name, "__pin"))) {
  2205. func_index =
  2206. export_tmp->index - module->import_func_count;
  2207. func_type_index =
  2208. module->func_type_indexes[func_index];
  2209. func_type = module->func_types[func_type_index];
  2210. if (func_type->param_count == 1
  2211. && func_type->result_count == 1
  2212. && func_type->types[0] == VALUE_TYPE_I32
  2213. && func_type->types[1] == VALUE_TYPE_I32) {
  2214. bh_assert(module->retain_func_index
  2215. == (uint32)-1);
  2216. module->retain_func_index = export_tmp->index;
  2217. LOG_VERBOSE("Found retain function, name: %s, "
  2218. "index: %u",
  2219. export_tmp->name,
  2220. export_tmp->index);
  2221. break;
  2222. }
  2223. }
  2224. }
  2225. if (j == module->export_count) {
  2226. module->malloc_func_index = (uint32)-1;
  2227. LOG_VERBOSE("Can't find retain function,"
  2228. "reset malloc function index to -1");
  2229. }
  2230. }
  2231. }
  2232. else if ((!strcmp(exports[i].name, "free"))
  2233. || (!strcmp(exports[i].name, "__release"))
  2234. || (!strcmp(exports[i].name, "__unpin"))) {
  2235. func_index = exports[i].index - module->import_func_count;
  2236. func_type_index = module->func_type_indexes[func_index];
  2237. func_type = module->func_types[func_type_index];
  2238. if (func_type->param_count == 1 && func_type->result_count == 0
  2239. && func_type->types[0] == VALUE_TYPE_I32) {
  2240. bh_assert(module->free_func_index == (uint32)-1);
  2241. module->free_func_index = exports[i].index;
  2242. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2243. exports[i].name, exports[i].index);
  2244. }
  2245. }
  2246. }
  2247. }
  2248. /* Flush data cache before executing AOT code,
  2249. * otherwise unpredictable behavior can occur. */
  2250. os_dcache_flush();
  2251. #if WASM_ENABLE_MEMORY_TRACING != 0
  2252. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  2253. #endif
  2254. #if WASM_ENABLE_DEBUG_AOT != 0
  2255. if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) {
  2256. set_error_buf(error_buf, error_buf_size,
  2257. "create jit code entry failed");
  2258. return false;
  2259. }
  2260. #endif
  2261. return true;
  2262. }
  2263. static AOTModule *
  2264. create_module(char *error_buf, uint32 error_buf_size)
  2265. {
  2266. AOTModule *module =
  2267. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
  2268. if (!module) {
  2269. return NULL;
  2270. }
  2271. module->module_type = Wasm_Module_AoT;
  2272. if (!(module->const_str_set =
  2273. bh_hash_map_create(64, false,
  2274. (HashFunc)const_str_hash,
  2275. (KeyEqualFunc)const_str_equal,
  2276. (ValueDestroyFunc)const_str_destroy_key,
  2277. NULL))) {
  2278. set_error_buf(error_buf, error_buf_size,
  2279. "create const string set failed");
  2280. wasm_runtime_free(module);
  2281. return NULL;
  2282. }
  2283. module->file_buf = NULL;
  2284. module->runtime = wasm_runtime_get_runtime();
  2285. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2286. // initial dependency related attributes.
  2287. module->implicit_dependency_modules_hmap = NULL;
  2288. module->ref_cnt = 0;
  2289. #endif
  2290. module->module_name = NULL;
  2291. return module;
  2292. }
  2293. AOTModule *
  2294. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  2295. uint32 error_buf_size)
  2296. {
  2297. AOTModule *module = create_module(error_buf, error_buf_size);
  2298. if (!module)
  2299. return NULL;
  2300. if (!load_from_sections(module, section_list, false, error_buf,
  2301. error_buf_size)) {
  2302. aot_unload(module);
  2303. return NULL;
  2304. }
  2305. LOG_VERBOSE("Load module from sections success.\n");
  2306. return module;
  2307. }
  2308. static void
  2309. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  2310. {
  2311. AOTSection *section = section_list, *next;
  2312. while (section) {
  2313. next = section->next;
  2314. if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT
  2315. && section->section_body)
  2316. os_munmap((uint8 *)section->section_body,
  2317. section->section_body_size);
  2318. wasm_runtime_free(section);
  2319. section = next;
  2320. }
  2321. }
  2322. #define AOT_HEADER_SIZE (sizeof(uint32) + sizeof(uint32) + sizeof(uint64) + sizeof(uint32))
  2323. static bool
  2324. resolve_native_symbols(const uint8 *buf, uint32 size, uint32 *p_count,
  2325. char *error_buf, uint32 error_buf_size)
  2326. {
  2327. const uint8 *p = buf, *p_end = buf + size;
  2328. uint32 section_type;
  2329. uint32 section_size = 0;
  2330. p += AOT_HEADER_SIZE;
  2331. while (p < p_end) {
  2332. read_uint32(p, p_end, section_type);
  2333. if (section_type <= AOT_SECTION_TYPE_SIGANATURE
  2334. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  2335. read_uint32(p, p_end, section_size);
  2336. CHECK_BUF(p, p_end, section_size);
  2337. if (section_type == AOT_SECTION_TYPE_CUSTOM) {
  2338. read_uint32(p, p_end, section_type);
  2339. if (section_type == AOT_CUSTOM_SECTION_NATIVE_SYMBOL) {
  2340. /* Read the count of native symbol */
  2341. read_uint32(p, p_end, *p_count);
  2342. return true;
  2343. }
  2344. p -= sizeof(uint32);
  2345. }
  2346. }
  2347. else if (section_type > AOT_SECTION_TYPE_SIGANATURE) {
  2348. set_error_buf(error_buf, error_buf_size,
  2349. "resolve native symbol failed");
  2350. break;
  2351. }
  2352. p += section_size;
  2353. }
  2354. return true;
  2355. fail:
  2356. return false;
  2357. }
  2358. static bool
  2359. create_sections(AOTModule *module, const uint8 *buf, uint32 size,
  2360. AOTSection **p_section_list, char *error_buf,
  2361. uint32 error_buf_size)
  2362. {
  2363. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  2364. const uint8 *p = buf, *p_end = buf + size;
  2365. bool destroy_aot_text = false;
  2366. uint32 native_symbol_count = 0;
  2367. uint32 section_type;
  2368. uint32 section_size;
  2369. uint64 total_size;
  2370. uint8 *aot_text;
  2371. if (!resolve_native_symbols(buf, size, &native_symbol_count, error_buf,
  2372. error_buf_size)) {
  2373. goto fail;
  2374. }
  2375. module->native_symbol_count = native_symbol_count;
  2376. p += AOT_HEADER_SIZE;
  2377. while (p < p_end) {
  2378. read_uint32(p, p_end, section_type);
  2379. if (section_type < AOT_SECTION_TYPE_SIGANATURE
  2380. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  2381. read_uint32(p, p_end, section_size);
  2382. CHECK_BUF(p, p_end, section_size);
  2383. if (!(section = loader_malloc(sizeof(AOTSection), error_buf,
  2384. error_buf_size))) {
  2385. goto fail;
  2386. }
  2387. memset(section, 0, sizeof(AOTSection));
  2388. section->section_type = (int32)section_type;
  2389. section->section_body = (uint8 *)p;
  2390. section->section_body_size = section_size;
  2391. if (section_type == AOT_SECTION_TYPE_TEXT) {
  2392. if ((section_size > 0) && (native_symbol_count == 0)) {
  2393. int map_prot =
  2394. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  2395. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  2396. || defined(BUILD_TARGET_RISCV64_LP64D) \
  2397. || defined(BUILD_TARGET_RISCV64_LP64)
  2398. /* aot code and data in x86_64 must be in range 0 to 2G due
  2399. to relocation for R_X86_64_32/32S/PC32 */
  2400. int map_flags = MMAP_MAP_32BIT;
  2401. #else
  2402. int map_flags = MMAP_MAP_NONE;
  2403. #endif
  2404. total_size =
  2405. (uint64)section_size + aot_get_plt_table_size();
  2406. total_size = (total_size + 3) & ~((uint64)3);
  2407. if (total_size >= UINT32_MAX
  2408. || !(aot_text = os_mmap(NULL, (uint32)total_size,
  2409. map_prot, map_flags))) {
  2410. wasm_runtime_free(section);
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "mmap memory failed");
  2413. goto fail;
  2414. }
  2415. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  2416. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  2417. && !defined(BH_PLATFORM_DARWIN)
  2418. /* address must be in the first 2 Gigabytes of
  2419. the process address space */
  2420. bh_assert((uintptr_t)aot_text < INT32_MAX);
  2421. #endif
  2422. #endif
  2423. bh_memcpy_s(aot_text, (uint32)total_size,
  2424. section->section_body, (uint32)section_size);
  2425. section->section_body = aot_text;
  2426. destroy_aot_text = true;
  2427. if ((uint32)total_size > section->section_body_size) {
  2428. memset(aot_text + (uint32)section_size, 0,
  2429. (uint32)total_size - section_size);
  2430. section->section_body_size = (uint32)total_size;
  2431. }
  2432. }
  2433. }
  2434. if (!section_list)
  2435. section_list = section_list_end = section;
  2436. else {
  2437. section_list_end->next = section;
  2438. section_list_end = section;
  2439. }
  2440. p += section_size;
  2441. }
  2442. else {
  2443. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2444. goto fail;
  2445. }
  2446. }
  2447. if (!section_list) {
  2448. set_error_buf(error_buf, error_buf_size, "create section list failed");
  2449. return false;
  2450. }
  2451. *p_section_list = section_list;
  2452. return true;
  2453. fail:
  2454. if (section_list)
  2455. destroy_sections(section_list, destroy_aot_text);
  2456. return false;
  2457. }
  2458. static bool
  2459. load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf,
  2460. uint32 error_buf_size)
  2461. {
  2462. const uint8 *buf_end = buf + size;
  2463. const uint8 *p = buf, *p_end = buf_end;
  2464. uint32 magic_number, version, verification;
  2465. uint64 aot_features = 0, vm_features = 0;
  2466. AOTSection *section_list = NULL;
  2467. bool ret;
  2468. read_uint32(p, p_end, magic_number);
  2469. if (magic_number != AOT_MAGIC_NUMBER) {
  2470. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  2471. return false;
  2472. }
  2473. read_uint32(p, p_end, version);
  2474. if (version != AOT_CURRENT_VERSION) {
  2475. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2476. return false;
  2477. }
  2478. read_uint64(p, p_end, aot_features);
  2479. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2480. vm_features |= AOT_FEATURE_ENABLE_DYNAMIC_LINKING;
  2481. #endif
  2482. if (aot_features != vm_features) {
  2483. set_error_buf(error_buf, error_buf_size, "unsupported features exist in VM or AOT file");
  2484. return false;
  2485. }
  2486. read_uint32(p, p_end, verification);
  2487. if (verification != offsetof(AOTModuleInstance, global_table_data.bytes)) {
  2488. set_error_buf(error_buf, error_buf_size, "AOT file built by imcompatiable AOT compiler");
  2489. return false;
  2490. }
  2491. bh_assert((p - buf) == AOT_HEADER_SIZE);
  2492. if (!create_sections(module, buf, size, &section_list,
  2493. error_buf, error_buf_size))
  2494. return false;
  2495. ret = load_from_sections(module, section_list, true, error_buf,
  2496. error_buf_size);
  2497. if (!ret) {
  2498. /* If load_from_sections() fails, then aot text is destroyed
  2499. in destroy_sections() */
  2500. destroy_sections(section_list,
  2501. module->native_symbol_count == 0 ? true : false);
  2502. /* aot_unload() won't destroy aot text again */
  2503. module->code = NULL;
  2504. }
  2505. else {
  2506. /* If load_from_sections() succeeds, then aot text is set to
  2507. module->code and will be destroyed in aot_unload() */
  2508. destroy_sections(section_list, false);
  2509. }
  2510. return ret;
  2511. fail:
  2512. return false;
  2513. }
  2514. #undef AOT_HEADER_SIZE
  2515. AOTModule *
  2516. aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf,
  2517. uint32 error_buf_size)
  2518. {
  2519. AOTModule *module = create_module(error_buf, error_buf_size);
  2520. if (!module)
  2521. return NULL;
  2522. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2523. aot_unload(module);
  2524. return NULL;
  2525. }
  2526. LOG_VERBOSE("Load module success.\n");
  2527. return module;
  2528. }
  2529. #if WASM_ENABLE_JIT != 0
  2530. static AOTModule *
  2531. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  2532. char *error_buf, uint32 error_buf_size)
  2533. {
  2534. uint32 i;
  2535. uint64 size;
  2536. char func_name[32];
  2537. AOTModule *module;
  2538. #if WASM_ENABLE_LAZY_JIT != 0
  2539. LLVMOrcThreadSafeModuleRef ts_module;
  2540. LLVMOrcJITDylibRef main_dylib;
  2541. LLVMErrorRef error;
  2542. LLVMOrcJITTargetAddress func_addr = 0;
  2543. #endif
  2544. /* Allocate memory for module */
  2545. if (!(module =
  2546. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) {
  2547. return NULL;
  2548. }
  2549. module->module_type = Wasm_Module_AoT;
  2550. module->import_memory_count = comp_data->import_memory_count;
  2551. module->import_memories = comp_data->import_memories;
  2552. module->memory_count = comp_data->memory_count;
  2553. if (module->memory_count) {
  2554. size = sizeof(AOTMemory) * (uint64)module->memory_count;
  2555. if (!(module->memories =
  2556. loader_malloc(size, error_buf, error_buf_size))) {
  2557. goto fail1;
  2558. }
  2559. bh_memcpy_s(module->memories, (uint32)size, comp_data->memories,
  2560. (uint32)size);
  2561. }
  2562. module->mem_init_data_list = comp_data->mem_init_data_list;
  2563. module->mem_init_data_count = comp_data->mem_init_data_count;
  2564. module->import_table_count = comp_data->import_table_count;
  2565. module->import_tables = comp_data->import_tables;
  2566. module->table_count = comp_data->table_count;
  2567. module->tables = comp_data->tables;
  2568. module->table_init_data_list = comp_data->table_init_data_list;
  2569. module->table_init_data_count = comp_data->table_init_data_count;
  2570. module->func_type_count = comp_data->func_type_count;
  2571. module->func_types = comp_data->func_types;
  2572. module->import_global_count = comp_data->import_global_count;
  2573. module->import_globals = comp_data->import_globals;
  2574. module->global_count = comp_data->global_count;
  2575. module->globals = comp_data->globals;
  2576. module->global_count = comp_data->global_count;
  2577. module->globals = comp_data->globals;
  2578. module->global_data_size = comp_data->global_data_size;
  2579. module->import_func_count = comp_data->import_func_count;
  2580. module->import_funcs = comp_data->import_funcs;
  2581. module->func_count = comp_data->func_count;
  2582. /* Allocate memory for function pointers */
  2583. size = (uint64)module->func_count * sizeof(void *);
  2584. if (size > 0
  2585. && !(module->func_ptrs =
  2586. loader_malloc(size, error_buf, error_buf_size))) {
  2587. goto fail2;
  2588. }
  2589. #if WASM_ENABLE_LAZY_JIT != 0
  2590. bh_assert(comp_ctx->lazy_orcjit);
  2591. main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->lazy_orcjit);
  2592. if (!main_dylib) {
  2593. set_error_buf(error_buf, error_buf_size,
  2594. "failed to get dynmaic library reference");
  2595. goto fail3;
  2596. }
  2597. ts_module = LLVMOrcCreateNewThreadSafeModule(comp_ctx->module,
  2598. comp_ctx->ts_context);
  2599. if (!ts_module) {
  2600. set_error_buf(error_buf, error_buf_size,
  2601. "failed to create thread safe module");
  2602. goto fail3;
  2603. }
  2604. if ((error = LLVMOrcLLLazyJITAddLLVMIRModule(comp_ctx->lazy_orcjit,
  2605. main_dylib, ts_module))) {
  2606. /*
  2607. * If adding the ThreadSafeModule fails then we need to clean it up
  2608. * ourselves. If adding it succeeds the JIT will manage the memory.
  2609. */
  2610. aot_handle_llvm_errmsg(error_buf, error_buf_size,
  2611. "failed to addIRModule: ", error);
  2612. goto fail4;
  2613. }
  2614. for (i = 0; i < comp_data->func_count; i++) {
  2615. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2616. if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->lazy_orcjit, &func_addr,
  2617. func_name))) {
  2618. aot_handle_llvm_errmsg(error_buf, error_buf_size,
  2619. "cannot lookup: ", error);
  2620. goto fail3;
  2621. }
  2622. module->func_ptrs[i] = (void *)func_addr;
  2623. func_addr = 0;
  2624. }
  2625. #else
  2626. /* Resolve function addresses */
  2627. bh_assert(comp_ctx->exec_engine);
  2628. for (i = 0; i < comp_data->func_count; i++) {
  2629. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2630. if (!(module->func_ptrs[i] = (void *)LLVMGetFunctionAddress(
  2631. comp_ctx->exec_engine, func_name))) {
  2632. set_error_buf(error_buf, error_buf_size,
  2633. "get function address failed");
  2634. goto fail3;
  2635. }
  2636. }
  2637. #endif /* WASM_ENABLE_LAZY_JIT != 0 */
  2638. /* Allocation memory for function type indexes */
  2639. size = (uint64)module->func_count * sizeof(uint32);
  2640. if (size > 0
  2641. && !(module->func_type_indexes =
  2642. loader_malloc(size, error_buf, error_buf_size))) {
  2643. goto fail3;
  2644. }
  2645. for (i = 0; i < comp_data->func_count; i++)
  2646. module->func_type_indexes[i] = comp_data->funcs[i].func_type_index;
  2647. module->export_count = comp_data->wasm_module->export_count;
  2648. module->exports = comp_data->wasm_module->exports;
  2649. module->export_func_count = comp_data->wasm_module->export_func_count;
  2650. module->export_tab_count = comp_data->wasm_module->export_tab_count;
  2651. module->export_mem_count = comp_data->wasm_module->export_mem_count;
  2652. module->export_global_count = comp_data->wasm_module->export_global_count;
  2653. module->start_func_index = comp_data->start_func_index;
  2654. if (comp_data->start_func_index != (uint32)-1) {
  2655. bh_assert(comp_data->start_func_index
  2656. < module->import_func_count + module->func_count);
  2657. /* TODO: fix issue that start func cannot be import func */
  2658. if (comp_data->start_func_index >= module->import_func_count) {
  2659. module->start_function =
  2660. module->func_ptrs[comp_data->start_func_index
  2661. - module->import_func_count];
  2662. }
  2663. }
  2664. module->malloc_func_index = comp_data->malloc_func_index;
  2665. module->free_func_index = comp_data->free_func_index;
  2666. module->retain_func_index = comp_data->retain_func_index;
  2667. module->aux_data_end_global_index = comp_data->aux_data_end_global_index;
  2668. module->aux_data_end = comp_data->aux_data_end;
  2669. module->aux_heap_base_global_index = comp_data->aux_heap_base_global_index;
  2670. module->aux_heap_base = comp_data->aux_heap_base;
  2671. module->aux_stack_top_global_index = comp_data->aux_stack_top_global_index;
  2672. module->aux_stack_bottom = comp_data->aux_stack_bottom;
  2673. module->aux_stack_size = comp_data->aux_stack_size;
  2674. module->code = NULL;
  2675. module->code_size = 0;
  2676. module->is_jit_mode = true;
  2677. module->wasm_module = comp_data->wasm_module;
  2678. module->comp_ctx = comp_ctx;
  2679. module->comp_data = comp_data;
  2680. #if WASM_ENABLE_LIBC_WASI != 0
  2681. module->is_wasi_module = comp_data->wasm_module->is_wasi_module;
  2682. #endif
  2683. return module;
  2684. #if WASM_ENABLE_LAZY_JIT != 0
  2685. fail4:
  2686. LLVMOrcDisposeThreadSafeModule(ts_module);
  2687. #endif
  2688. fail3:
  2689. if (module->func_ptrs)
  2690. wasm_runtime_free(module->func_ptrs);
  2691. fail2:
  2692. if (module->memory_count > 0)
  2693. wasm_runtime_free(module->memories);
  2694. fail1:
  2695. wasm_runtime_free(module);
  2696. return NULL;
  2697. }
  2698. AOTModule *
  2699. aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf,
  2700. uint32 error_buf_size)
  2701. {
  2702. AOTCompData *comp_data;
  2703. AOTCompContext *comp_ctx;
  2704. AOTModule *aot_module;
  2705. AOTCompOption option = { 0 };
  2706. char *aot_last_error;
  2707. option.is_jit_mode = true;
  2708. #if WASM_ENABLE_BULK_MEMORY != 0
  2709. option.enable_bulk_memory = true;
  2710. #endif
  2711. #if WASM_ENABLE_THREAD_MGR != 0
  2712. option.enable_thread_mgr = true;
  2713. #endif
  2714. #if WASM_ENABLE_TAIL_CALL != 0
  2715. option.enable_tail_call = true;
  2716. #endif
  2717. #if WASM_ENABLE_SIMD != 0
  2718. option.enable_simd = true;
  2719. #endif
  2720. #if WASM_ENABLE_REF_TYPES != 0
  2721. option.enable_ref_types = true;
  2722. #endif
  2723. option.enable_aux_stack_check = true;
  2724. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2725. option.enable_aux_stack_frame = true;
  2726. #endif
  2727. comp_ctx = aot_create_comp_context(&option);
  2728. if (!comp_ctx) {
  2729. aot_last_error = aot_get_last_error();
  2730. bh_assert(aot_last_error != NULL);
  2731. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2732. return NULL;
  2733. }
  2734. comp_data = aot_create_comp_data(wasm_module, sizeof(void*));
  2735. if (!comp_data) {
  2736. aot_last_error = aot_get_last_error();
  2737. bh_assert(aot_last_error != NULL);
  2738. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2739. goto fail1;
  2740. }
  2741. if (!aot_bind_comp_context_data(comp_ctx, comp_data)) {
  2742. aot_last_error = aot_get_last_error();
  2743. bh_assert(aot_last_error != NULL);
  2744. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2745. goto fail2;
  2746. }
  2747. if (!aot_compile_wasm(comp_ctx)) {
  2748. aot_last_error = aot_get_last_error();
  2749. bh_assert(aot_last_error != NULL);
  2750. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2751. goto fail2;
  2752. }
  2753. aot_module =
  2754. aot_load_from_comp_data(comp_data, comp_ctx, error_buf, error_buf_size);
  2755. if (!aot_module) {
  2756. goto fail2;
  2757. }
  2758. return aot_module;
  2759. fail2:
  2760. aot_destroy_comp_data(comp_data);
  2761. fail1:
  2762. aot_destroy_comp_context(comp_ctx);
  2763. return NULL;
  2764. }
  2765. #endif
  2766. void
  2767. aot_unload(AOTModule *module)
  2768. {
  2769. #if WASM_ENABLE_JIT != 0
  2770. if (module->comp_data)
  2771. aot_destroy_comp_data(module->comp_data);
  2772. if (module->comp_ctx)
  2773. aot_destroy_comp_context(module->comp_ctx);
  2774. if (module->wasm_module)
  2775. wasm_loader_unload(module->wasm_module);
  2776. #endif
  2777. if (module->import_memories)
  2778. destroy_import_memories(module->import_memories, module->is_jit_mode);
  2779. if (module->memories)
  2780. wasm_runtime_free(module->memories);
  2781. if (module->mem_init_data_list)
  2782. destroy_mem_init_data_list(module->mem_init_data_list,
  2783. module->mem_init_data_count,
  2784. module->is_jit_mode);
  2785. if (module->native_symbol_list)
  2786. wasm_runtime_free(module->native_symbol_list);
  2787. if (module->import_tables)
  2788. destroy_import_tables(module->import_tables, module->is_jit_mode);
  2789. if (module->tables)
  2790. destroy_tables(module->tables, module->is_jit_mode);
  2791. if (module->table_init_data_list)
  2792. destroy_table_init_data_list(module->table_init_data_list,
  2793. module->table_init_data_count,
  2794. module->is_jit_mode);
  2795. if (module->func_types)
  2796. destroy_func_types(module->func_types, module->func_type_count,
  2797. module->is_jit_mode);
  2798. if (module->import_globals)
  2799. destroy_import_globals(module->import_globals, module->is_jit_mode);
  2800. if (module->globals)
  2801. destroy_globals(module->globals, module->is_jit_mode);
  2802. if (module->import_funcs)
  2803. destroy_import_funcs(module->import_funcs, module->is_jit_mode);
  2804. if (module->exports)
  2805. destroy_exports(module->exports, module->is_jit_mode);
  2806. if (module->func_type_indexes)
  2807. wasm_runtime_free(module->func_type_indexes);
  2808. if (module->func_ptrs)
  2809. wasm_runtime_free(module->func_ptrs);
  2810. if (module->const_str_set)
  2811. bh_hash_map_destroy(module->const_str_set);
  2812. if (module->code && (module->native_symbol_count == 0)) {
  2813. /* The layout is: literal size + literal + code (with plt table) */
  2814. uint8 *mmap_addr = module->literal - sizeof(uint32);
  2815. uint32 total_size =
  2816. sizeof(uint32) + module->literal_size + module->code_size;
  2817. os_munmap(mmap_addr, total_size);
  2818. }
  2819. #if defined(BH_PLATFORM_WINDOWS)
  2820. if (module->extra_plt_data) {
  2821. os_munmap(module->extra_plt_data, module->extra_plt_data_size);
  2822. }
  2823. #endif
  2824. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  2825. if (module->rtl_func_table) {
  2826. if (module->rtl_func_table_registered)
  2827. RtlDeleteFunctionTable(module->rtl_func_table);
  2828. wasm_runtime_free(module->rtl_func_table);
  2829. }
  2830. #endif
  2831. if (module->data_sections)
  2832. destroy_object_data_sections(module->data_sections,
  2833. module->data_section_count);
  2834. #if WASM_ENABLE_DEBUG_AOT != 0
  2835. jit_code_entry_destroy(module->elf_hdr);
  2836. #endif
  2837. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2838. if (module->aux_func_indexes) {
  2839. wasm_runtime_free(module->aux_func_indexes);
  2840. }
  2841. if (module->aux_func_names) {
  2842. wasm_runtime_free(module->aux_func_names);
  2843. }
  2844. #endif
  2845. if (module->dylink_section)
  2846. wasm_runtime_free(module->dylink_section);
  2847. if (module->file_buf)
  2848. wasm_runtime_free((void*)module->file_buf);
  2849. wasm_runtime_free(module);
  2850. }
  2851. uint32
  2852. aot_get_plt_table_size()
  2853. {
  2854. return get_plt_table_size();
  2855. }