aot_loader.c 108 KB

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