aot_loader.c 114 KB

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