aot_loader.c 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568
  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 "aot_reloc.h"
  7. #include "bh_platform.h"
  8. #include "../common/wasm_runtime_common.h"
  9. #include "../common/wasm_native.h"
  10. #include "../common/wasm_loader_common.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. #if WASM_ENABLE_LINUX_PERF != 0
  17. #include "aot_perf_map.h"
  18. #endif
  19. #define YMM_PLT_PREFIX "__ymm@"
  20. #define XMM_PLT_PREFIX "__xmm@"
  21. #define REAL_PLT_PREFIX "__real@"
  22. static void
  23. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  24. {
  25. if (error_buf != NULL) {
  26. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  27. string);
  28. }
  29. }
  30. static void
  31. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  32. {
  33. va_list args;
  34. char buf[128];
  35. if (error_buf != NULL) {
  36. va_start(args, format);
  37. vsnprintf(buf, sizeof(buf), format, args);
  38. va_end(args);
  39. snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf);
  40. }
  41. }
  42. #define exchange_uint8(p_data) (void)0
  43. static void
  44. exchange_uint16(uint8 *p_data)
  45. {
  46. uint8 value = *p_data;
  47. *p_data = *(p_data + 1);
  48. *(p_data + 1) = value;
  49. }
  50. static void
  51. exchange_uint32(uint8 *p_data)
  52. {
  53. uint8 value = *p_data;
  54. *p_data = *(p_data + 3);
  55. *(p_data + 3) = value;
  56. value = *(p_data + 1);
  57. *(p_data + 1) = *(p_data + 2);
  58. *(p_data + 2) = value;
  59. }
  60. static void
  61. exchange_uint64(uint8 *p_data)
  62. {
  63. uint32 value;
  64. value = *(uint32 *)p_data;
  65. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  66. *(uint32 *)(p_data + 4) = value;
  67. exchange_uint32(p_data);
  68. exchange_uint32(p_data + 4);
  69. }
  70. static union {
  71. int a;
  72. char b;
  73. } __ue = { .a = 1 };
  74. #define is_little_endian() (__ue.b == 1)
  75. static bool
  76. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  77. char *error_buf, uint32 error_buf_size)
  78. {
  79. if ((uintptr_t)buf + length < (uintptr_t)buf
  80. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  81. set_error_buf(error_buf, error_buf_size, "unexpected end");
  82. return false;
  83. }
  84. return true;
  85. }
  86. #define CHECK_BUF(buf, buf_end, length) \
  87. do { \
  88. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  89. goto fail; \
  90. } \
  91. } while (0)
  92. static uint8 *
  93. align_ptr(const uint8 *p, uint32 b)
  94. {
  95. uintptr_t v = (uintptr_t)p;
  96. uintptr_t m = b - 1;
  97. return (uint8 *)((v + m) & ~m);
  98. }
  99. static inline uint64
  100. GET_U64_FROM_ADDR(uint32 *addr)
  101. {
  102. union {
  103. uint64 val;
  104. uint32 parts[2];
  105. } u;
  106. u.parts[0] = addr[0];
  107. u.parts[1] = addr[1];
  108. return u.val;
  109. }
  110. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  111. static inline uint8
  112. GET_U8_FROM_ADDR(const uint8 *p)
  113. {
  114. uint8 res = 0;
  115. bh_assert(p);
  116. const uint8 *p_aligned = align_ptr(p, 4);
  117. p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned;
  118. uint32 buf32 = *(const uint32 *)p_aligned;
  119. const uint8 *pbuf = (const uint8 *)&buf32;
  120. res = *(uint8 *)(pbuf + (p - p_aligned));
  121. return res;
  122. }
  123. static inline uint16
  124. GET_U16_FROM_ADDR(const uint8 *p)
  125. {
  126. uint16 res = 0;
  127. bh_assert(p);
  128. const uint8 *p_aligned = align_ptr(p, 4);
  129. p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned;
  130. uint32 buf32 = *(const uint32 *)p_aligned;
  131. const uint8 *pbuf = (const uint8 *)&buf32;
  132. res = *(uint16 *)(pbuf + (p - p_aligned));
  133. return res;
  134. }
  135. #define TEMPLATE_READ(p, p_end, res, type) \
  136. do { \
  137. if (sizeof(type) != sizeof(uint64)) \
  138. p = (uint8 *)align_ptr(p, sizeof(type)); \
  139. else \
  140. /* align 4 bytes if type is uint64 */ \
  141. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  142. CHECK_BUF(p, p_end, sizeof(type)); \
  143. if (sizeof(type) == sizeof(uint8)) \
  144. res = GET_U8_FROM_ADDR(p); \
  145. else if (sizeof(type) == sizeof(uint16)) \
  146. res = GET_U16_FROM_ADDR(p); \
  147. else if (sizeof(type) == sizeof(uint32)) \
  148. res = *(type *)p; \
  149. else \
  150. res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
  151. if (!is_little_endian()) \
  152. exchange_##type((uint8 *)&res); \
  153. p += sizeof(type); \
  154. } while (0)
  155. #define read_byte_array(p, p_end, addr, len) \
  156. do { \
  157. CHECK_BUF(p, p_end, len); \
  158. bh_memcpy_wa(addr, len, p, len); \
  159. p += len; \
  160. } while (0)
  161. #define read_string(p, p_end, str) \
  162. do { \
  163. if (!(str = load_string((uint8 **)&p, p_end, module, \
  164. is_load_from_file_buf, true, error_buf, \
  165. error_buf_size))) \
  166. goto fail; \
  167. } while (0)
  168. #else /* else of (WASM_ENABLE_WORD_ALIGN_READ != 0) */
  169. #define TEMPLATE_READ(p, p_end, res, type) \
  170. do { \
  171. if (sizeof(type) != sizeof(uint64)) \
  172. p = (uint8 *)align_ptr(p, sizeof(type)); \
  173. else \
  174. /* align 4 bytes if type is uint64 */ \
  175. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  176. CHECK_BUF(p, p_end, sizeof(type)); \
  177. if (sizeof(type) != sizeof(uint64)) \
  178. res = *(type *)p; \
  179. else \
  180. res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
  181. if (!is_little_endian()) \
  182. exchange_##type((uint8 *)&res); \
  183. p += sizeof(type); \
  184. } while (0)
  185. /* NOLINTBEGIN, disable lint for this region with clang-tidy */
  186. #define read_byte_array(p, p_end, addr, len) \
  187. do { \
  188. CHECK_BUF(p, p_end, len); \
  189. bh_memcpy_s(addr, len, p, len); \
  190. p += len; \
  191. } while (0)
  192. #define read_string(p, p_end, str) \
  193. do { \
  194. if (!(str = load_string((uint8 **)&p, p_end, module, \
  195. is_load_from_file_buf, error_buf, \
  196. error_buf_size))) \
  197. goto fail; \
  198. } while (0)
  199. #endif /* end of (WASM_ENABLE_WORD_ALIGN_READ != 0) */
  200. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  201. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  202. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  203. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  204. /* NOLINTEND */
  205. /* Legal values for bin_type */
  206. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  207. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  208. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  209. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  210. #define BIN_TYPE_COFF32 4 /* 32-bit little endian */
  211. #define BIN_TYPE_COFF64 6 /* 64-bit little endian */
  212. /* Legal values for e_type (object file type). */
  213. #define E_TYPE_NONE 0 /* No file type */
  214. #define E_TYPE_REL 1 /* Relocatable file */
  215. #define E_TYPE_EXEC 2 /* Executable file */
  216. #define E_TYPE_DYN 3 /* Shared object file */
  217. #define E_TYPE_XIP 4 /* eXecute In Place file */
  218. /* Legal values for e_machine (architecture). */
  219. #define E_MACHINE_386 3 /* Intel 80386 */
  220. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  221. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  222. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  223. #define E_MACHINE_AARCH64 183 /* AArch64 */
  224. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  225. #define E_MACHINE_IA_64 50 /* Intel Merced */
  226. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  227. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  228. #define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */
  229. #define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
  230. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  231. #define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
  232. #define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */
  233. #define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */
  234. /* Legal values for e_version */
  235. #define E_VERSION_CURRENT 1 /* Current version */
  236. static void *
  237. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  238. {
  239. void *mem;
  240. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  241. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  242. return NULL;
  243. }
  244. memset(mem, 0, (uint32)size);
  245. return mem;
  246. }
  247. static void *
  248. loader_mmap(uint32 size, bool prot_exec, char *error_buf, uint32 error_buf_size)
  249. {
  250. int map_prot =
  251. MMAP_PROT_READ | MMAP_PROT_WRITE | (prot_exec ? MMAP_PROT_EXEC : 0);
  252. int map_flags;
  253. void *mem;
  254. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  255. || defined(BUILD_TARGET_RISCV64_LP64D) \
  256. || defined(BUILD_TARGET_RISCV64_LP64)
  257. #ifndef __APPLE__
  258. /* The mmapped AOT data and code in 64-bit targets had better be in
  259. range 0 to 2G, or aot loader may fail to apply some relocations,
  260. e.g., R_X86_64_32/R_X86_64_32S/R_X86_64_PC32/R_RISCV_32.
  261. We try to mmap with MMAP_MAP_32BIT flag first, and if fails, mmap
  262. again without the flag. */
  263. map_flags = MMAP_MAP_32BIT;
  264. if ((mem = os_mmap(NULL, size, map_prot, map_flags,
  265. os_get_invalid_handle()))) {
  266. /* The mmapped memory must be in the first 2 Gigabytes of the
  267. process address space */
  268. bh_assert((uintptr_t)mem < INT32_MAX);
  269. return mem;
  270. }
  271. #endif
  272. #endif
  273. map_flags = MMAP_MAP_NONE;
  274. if (!(mem = os_mmap(NULL, size, map_prot, map_flags,
  275. os_get_invalid_handle()))) {
  276. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  277. return NULL;
  278. }
  279. return mem;
  280. }
  281. static char *
  282. load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  283. bool is_load_from_file_buf,
  284. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  285. bool is_vram_word_align,
  286. #endif
  287. char *error_buf, uint32 error_buf_size)
  288. {
  289. uint8 *p = *p_buf;
  290. const uint8 *p_end = buf_end;
  291. char *str;
  292. uint16 str_len;
  293. read_uint16(p, p_end, str_len);
  294. CHECK_BUF(p, p_end, str_len);
  295. if (str_len == 0) {
  296. str = "";
  297. }
  298. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  299. else if (is_vram_word_align) {
  300. if (!(str = aot_const_str_set_insert((uint8 *)p, str_len, module,
  301. is_vram_word_align, error_buf,
  302. error_buf_size))) {
  303. goto fail;
  304. }
  305. }
  306. #endif
  307. else if (is_load_from_file_buf) {
  308. /* The string is always terminated with '\0', use it directly.
  309. * In this case, the file buffer can be referred to after loading.
  310. */
  311. if (p[str_len - 1] != '\0')
  312. goto fail;
  313. str = (char *)p;
  314. }
  315. else {
  316. /* Load from sections, the file buffer cannot be referred to
  317. after loading, we must create another string and insert it
  318. into const string set */
  319. if (p[str_len - 1] != '\0')
  320. goto fail;
  321. if (!(str = aot_const_str_set_insert((uint8 *)p, str_len, module,
  322. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  323. is_vram_word_align,
  324. #endif
  325. error_buf, error_buf_size))) {
  326. goto fail;
  327. }
  328. }
  329. p += str_len;
  330. *p_buf = p;
  331. return str;
  332. fail:
  333. return NULL;
  334. }
  335. static bool
  336. get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
  337. uint32 target_buf_size, char *error_buf,
  338. uint32 error_buf_size)
  339. {
  340. char *machine_type = NULL;
  341. switch (target_info->e_machine) {
  342. case E_MACHINE_X86_64:
  343. case E_MACHINE_WIN_X86_64:
  344. machine_type = "x86_64";
  345. break;
  346. case E_MACHINE_386:
  347. case E_MACHINE_WIN_I386:
  348. machine_type = "i386";
  349. break;
  350. case E_MACHINE_ARM:
  351. case E_MACHINE_AARCH64:
  352. /* TODO: this will make following `strncmp()` ~L392 unnecessary.
  353. * Use const strings here */
  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_MULTI_THREAD) {
  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_GC == 0
  434. if (feature_flags & WASM_FEATURE_GARBAGE_COLLECTION) {
  435. set_error_buf(error_buf, error_buf_size,
  436. "garbage collection is not enabled in this build");
  437. return false;
  438. }
  439. #endif
  440. return true;
  441. }
  442. #if WASM_ENABLE_GC != 0
  443. static WASMRefType *
  444. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  445. char *error_buf, uint32 error_buf_size)
  446. {
  447. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  448. if (!ret) {
  449. set_error_buf(error_buf, error_buf_size,
  450. "insert ref type to hash set failed");
  451. }
  452. return ret;
  453. }
  454. #endif
  455. static bool
  456. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  457. AOTModule *module, char *error_buf,
  458. uint32 error_buf_size)
  459. {
  460. AOTTargetInfo target_info;
  461. const uint8 *p = buf, *p_end = buf_end;
  462. bool is_target_little_endian, is_target_64_bit;
  463. read_uint16(p, p_end, target_info.bin_type);
  464. read_uint16(p, p_end, target_info.abi_type);
  465. read_uint16(p, p_end, target_info.e_type);
  466. read_uint16(p, p_end, target_info.e_machine);
  467. read_uint32(p, p_end, target_info.e_version);
  468. read_uint32(p, p_end, target_info.e_flags);
  469. read_uint64(p, p_end, target_info.feature_flags);
  470. read_uint64(p, p_end, target_info.reserved);
  471. read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
  472. if (target_info.arch[sizeof(target_info.arch) - 1] != '\0') {
  473. set_error_buf(error_buf, error_buf_size, "invalid arch string");
  474. return false;
  475. }
  476. if (p != buf_end) {
  477. set_error_buf(error_buf, error_buf_size, "invalid section size");
  478. return false;
  479. }
  480. /* Check target endian type */
  481. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  482. if (is_little_endian() != is_target_little_endian) {
  483. set_error_buf_v(error_buf, error_buf_size,
  484. "invalid target endian type, expected %s but got %s",
  485. is_little_endian() ? "little endian" : "big endian",
  486. is_target_little_endian ? "little endian"
  487. : "big endian");
  488. return false;
  489. }
  490. /* Check target bit width */
  491. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  492. if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) {
  493. set_error_buf_v(error_buf, error_buf_size,
  494. "invalid target bit width, expected %s but got %s",
  495. sizeof(void *) == 8 ? "64-bit" : "32-bit",
  496. is_target_64_bit ? "64-bit" : "32-bit");
  497. return false;
  498. }
  499. /* Check target elf file type */
  500. if (target_info.e_type != E_TYPE_REL && target_info.e_type != E_TYPE_XIP) {
  501. set_error_buf(error_buf, error_buf_size,
  502. "invalid object file type, "
  503. "expected relocatable or XIP file type but got others");
  504. return false;
  505. }
  506. /* for backwards compatibility with previous wamrc aot files */
  507. if (!strcmp(target_info.arch, "arm64"))
  508. bh_strcpy_s(target_info.arch, sizeof(target_info.arch), "aarch64v8");
  509. /* Check machine info */
  510. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  511. return false;
  512. }
  513. if (target_info.e_version != E_VERSION_CURRENT) {
  514. set_error_buf(error_buf, error_buf_size, "invalid elf file version");
  515. return false;
  516. }
  517. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  518. module->feature_flags = target_info.feature_flags;
  519. #endif
  520. /* Finally, check feature flags */
  521. return check_feature_flags(error_buf, error_buf_size,
  522. target_info.feature_flags);
  523. fail:
  524. return false;
  525. }
  526. static void *
  527. get_native_symbol_by_name(const char *name)
  528. {
  529. void *func = NULL;
  530. uint32 symnum = 0;
  531. SymbolMap *sym = NULL;
  532. sym = get_target_symbol_map(&symnum);
  533. while (symnum && symnum--) {
  534. if (strcmp(sym->symbol_name, name) == 0) {
  535. func = sym->symbol_addr;
  536. break;
  537. }
  538. sym++;
  539. }
  540. return func;
  541. }
  542. static bool
  543. str2uint32(const char *buf, uint32 *p_res);
  544. static bool
  545. str2uint64(const char *buf, uint64 *p_res);
  546. static bool
  547. load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
  548. AOTModule *module, bool is_load_from_file_buf,
  549. char *error_buf, uint32 error_buf_size)
  550. {
  551. const uint8 *p = buf, *p_end = buf_end;
  552. uint32 cnt;
  553. int32 i;
  554. const char *symbol;
  555. if (module->native_symbol_list) {
  556. set_error_buf(error_buf, error_buf_size,
  557. "duplicated native symbol section");
  558. return false;
  559. }
  560. read_uint32(p, p_end, cnt);
  561. if (cnt > 0) {
  562. uint64 list_size = cnt * (uint64)sizeof(void *);
  563. module->native_symbol_list =
  564. loader_malloc(list_size, error_buf, error_buf_size);
  565. if (module->native_symbol_list == NULL) {
  566. goto fail;
  567. }
  568. for (i = cnt - 1; i >= 0; i--) {
  569. read_string(p, p_end, symbol);
  570. if (!strlen(symbol))
  571. continue;
  572. if (!strncmp(symbol, "f32#", 4) || !strncmp(symbol, "i32#", 4)) {
  573. uint32 u32;
  574. /* Resolve the raw int bits of f32 const */
  575. if (!str2uint32(symbol + 4, &u32)) {
  576. set_error_buf_v(error_buf, error_buf_size,
  577. "resolve symbol %s failed", symbol);
  578. goto fail;
  579. }
  580. *(uint32 *)(&module->native_symbol_list[i]) = u32;
  581. }
  582. else if (!strncmp(symbol, "f64#", 4)
  583. || !strncmp(symbol, "i64#", 4)) {
  584. uint64 u64;
  585. /* Resolve the raw int bits of f64 const */
  586. if (!str2uint64(symbol + 4, &u64)) {
  587. set_error_buf_v(error_buf, error_buf_size,
  588. "resolve symbol %s failed", symbol);
  589. goto fail;
  590. }
  591. *(uint64 *)(&module->native_symbol_list[i]) = u64;
  592. }
  593. else if (!strncmp(symbol, "__ignore", 8)) {
  594. /* Padding bytes to make f64 on 8-byte aligned address,
  595. or it is the second 32-bit slot in 32-bit system */
  596. continue;
  597. }
  598. else {
  599. module->native_symbol_list[i] =
  600. get_native_symbol_by_name(symbol);
  601. if (module->native_symbol_list[i] == NULL) {
  602. set_error_buf_v(error_buf, error_buf_size,
  603. "missing native symbol: %s", symbol);
  604. goto fail;
  605. }
  606. }
  607. }
  608. }
  609. return true;
  610. fail:
  611. return false;
  612. }
  613. static bool
  614. load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  615. bool is_load_from_file_buf, char *error_buf,
  616. uint32 error_buf_size)
  617. {
  618. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  619. const uint8 *p = buf, *p_end = buf_end;
  620. uint32 *aux_func_indexes;
  621. const char **aux_func_names;
  622. uint32 name_type, subsection_size;
  623. uint32 previous_name_type = 0;
  624. uint32 num_func_name;
  625. uint32 func_index;
  626. uint32 previous_func_index = ~0U;
  627. uint32 name_index;
  628. int i = 0;
  629. uint32 name_len;
  630. uint64 size;
  631. if (p >= p_end) {
  632. set_error_buf(error_buf, error_buf_size, "unexpected end");
  633. return false;
  634. }
  635. read_uint32(p, p_end, name_len);
  636. if (name_len != 4 || p + name_len > p_end) {
  637. set_error_buf(error_buf, error_buf_size, "unexpected end");
  638. return false;
  639. }
  640. if (memcmp(p, "name", 4) != 0) {
  641. set_error_buf(error_buf, error_buf_size, "invalid custom name section");
  642. return false;
  643. }
  644. p += name_len;
  645. while (p < p_end) {
  646. read_uint32(p, p_end, name_type);
  647. if (i != 0) {
  648. if (name_type == previous_name_type) {
  649. set_error_buf(error_buf, error_buf_size,
  650. "duplicate sub-section");
  651. return false;
  652. }
  653. if (name_type < previous_name_type) {
  654. set_error_buf(error_buf, error_buf_size,
  655. "out-of-order sub-section");
  656. return false;
  657. }
  658. }
  659. previous_name_type = name_type;
  660. read_uint32(p, p_end, subsection_size);
  661. CHECK_BUF(p, p_end, subsection_size);
  662. switch (name_type) {
  663. case SUB_SECTION_TYPE_FUNC:
  664. if (subsection_size) {
  665. read_uint32(p, p_end, num_func_name);
  666. if (num_func_name
  667. > module->import_func_count + module->func_count) {
  668. set_error_buf(error_buf, error_buf_size,
  669. "function name count out of bounds");
  670. return false;
  671. }
  672. module->aux_func_name_count = num_func_name;
  673. /* Allocate memory */
  674. size = sizeof(uint32) * (uint64)module->aux_func_name_count;
  675. if (!(aux_func_indexes = module->aux_func_indexes =
  676. loader_malloc(size, error_buf, error_buf_size))) {
  677. return false;
  678. }
  679. size =
  680. sizeof(char **) * (uint64)module->aux_func_name_count;
  681. if (!(aux_func_names = module->aux_func_names =
  682. loader_malloc(size, error_buf, error_buf_size))) {
  683. return false;
  684. }
  685. for (name_index = 0; name_index < num_func_name;
  686. name_index++) {
  687. read_uint32(p, p_end, func_index);
  688. if (name_index != 0
  689. && func_index == previous_func_index) {
  690. set_error_buf(error_buf, error_buf_size,
  691. "duplicate function name");
  692. return false;
  693. }
  694. if (name_index != 0
  695. && func_index < previous_func_index) {
  696. set_error_buf(error_buf, error_buf_size,
  697. "out-of-order function index ");
  698. return false;
  699. }
  700. if (func_index
  701. >= module->import_func_count + module->func_count) {
  702. set_error_buf(error_buf, error_buf_size,
  703. "function index out of bounds");
  704. return false;
  705. }
  706. previous_func_index = func_index;
  707. *(aux_func_indexes + name_index) = func_index;
  708. read_string(p, p_end, *(aux_func_names + name_index));
  709. #if 0
  710. LOG_DEBUG("func_index %u -> aux_func_name = %s\n",
  711. func_index, *(aux_func_names + name_index));
  712. #endif
  713. }
  714. }
  715. break;
  716. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  717. */
  718. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  719. default:
  720. p = p + subsection_size;
  721. break;
  722. }
  723. i++;
  724. }
  725. return true;
  726. fail:
  727. return false;
  728. #else
  729. return true;
  730. #endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
  731. }
  732. #if WASM_ENABLE_STRINGREF != 0
  733. static bool
  734. load_string_literal_section(const uint8 *buf, const uint8 *buf_end,
  735. AOTModule *module, bool is_load_from_file_buf,
  736. char *error_buf, uint32 error_buf_size)
  737. {
  738. const uint8 *p = buf, *p_end = buf_end;
  739. uint32 reserved = 0, string_count = 0, i;
  740. uint64 size;
  741. read_uint32(p, p_end, reserved);
  742. if (reserved != 0) {
  743. set_error_buf(error_buf, error_buf_size,
  744. "invalid reserved slot in string literal count");
  745. goto fail;
  746. }
  747. read_uint32(p, p_end, string_count);
  748. if (string_count == 0) {
  749. set_error_buf(error_buf, error_buf_size,
  750. "invalid string literal count");
  751. goto fail;
  752. }
  753. module->string_literal_count = string_count;
  754. size = (uint64)sizeof(char *) * string_count;
  755. if (!(module->string_literal_ptrs =
  756. loader_malloc(size, error_buf, error_buf_size))) {
  757. goto fail;
  758. }
  759. size = (uint64)sizeof(uint32) * string_count;
  760. if (!(module->string_literal_lengths =
  761. loader_malloc(size, error_buf, error_buf_size))) {
  762. goto fail;
  763. }
  764. for (i = 0; i < string_count; i++) {
  765. read_uint32(p, p_end, module->string_literal_lengths[i]);
  766. }
  767. for (i = 0; i < string_count; i++) {
  768. module->string_literal_ptrs[i] = p;
  769. p += module->string_literal_lengths[i];
  770. }
  771. return true;
  772. fail:
  773. return false;
  774. }
  775. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  776. static bool
  777. load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  778. bool is_load_from_file_buf, char *error_buf,
  779. uint32 error_buf_size)
  780. {
  781. const uint8 *p = buf, *p_end = buf_end;
  782. uint32 sub_section_type;
  783. read_uint32(p, p_end, sub_section_type);
  784. buf = p;
  785. switch (sub_section_type) {
  786. case AOT_CUSTOM_SECTION_NATIVE_SYMBOL:
  787. if (!load_native_symbol_section(buf, buf_end, module,
  788. is_load_from_file_buf, error_buf,
  789. error_buf_size))
  790. goto fail;
  791. break;
  792. case AOT_CUSTOM_SECTION_NAME:
  793. if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
  794. error_buf, error_buf_size))
  795. LOG_VERBOSE("Load name section failed.");
  796. else
  797. LOG_VERBOSE("Load name section success.");
  798. break;
  799. #if WASM_ENABLE_STRINGREF != 0
  800. case AOT_CUSTOM_SECTION_STRING_LITERAL:
  801. if (!load_string_literal_section(buf, buf_end, module,
  802. is_load_from_file_buf, error_buf,
  803. error_buf_size))
  804. goto fail;
  805. break;
  806. #endif
  807. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  808. case AOT_CUSTOM_SECTION_RAW:
  809. {
  810. const char *section_name;
  811. WASMCustomSection *section;
  812. if (p >= p_end) {
  813. set_error_buf(error_buf, error_buf_size, "unexpected end");
  814. goto fail;
  815. }
  816. read_string(p, p_end, section_name);
  817. section = loader_malloc(sizeof(WASMCustomSection), error_buf,
  818. error_buf_size);
  819. if (!section) {
  820. goto fail;
  821. }
  822. section->name_addr = (char *)section_name;
  823. section->name_len = (uint32)strlen(section_name);
  824. section->content_addr = (uint8 *)p;
  825. section->content_len = (uint32)(p_end - p);
  826. section->next = module->custom_section_list;
  827. module->custom_section_list = section;
  828. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  829. break;
  830. }
  831. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  832. default:
  833. break;
  834. }
  835. return true;
  836. fail:
  837. return false;
  838. }
  839. static void
  840. destroy_import_memories(AOTImportMemory *import_memories)
  841. {
  842. wasm_runtime_free(import_memories);
  843. }
  844. static void
  845. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count)
  846. {
  847. uint32 i;
  848. for (i = 0; i < count; i++)
  849. if (data_list[i])
  850. wasm_runtime_free(data_list[i]);
  851. wasm_runtime_free(data_list);
  852. }
  853. static bool
  854. load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  855. InitializerExpression *expr, char *error_buf,
  856. uint32 error_buf_size);
  857. static bool
  858. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  859. AOTModule *module, char *error_buf,
  860. uint32 error_buf_size)
  861. {
  862. const uint8 *buf = *p_buf;
  863. AOTMemInitData **data_list;
  864. uint64 size;
  865. uint32 i;
  866. /* Allocate memory */
  867. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  868. if (!(module->mem_init_data_list = data_list =
  869. loader_malloc(size, error_buf, error_buf_size))) {
  870. return false;
  871. }
  872. /* Create each memory data segment */
  873. for (i = 0; i < module->mem_init_data_count; i++) {
  874. uint32 byte_count;
  875. uint32 is_passive;
  876. uint32 memory_index;
  877. InitializerExpression init_value;
  878. read_uint32(buf, buf_end, is_passive);
  879. read_uint32(buf, buf_end, memory_index);
  880. if (!load_init_expr(&buf, buf_end, module, &init_value, error_buf,
  881. error_buf_size)) {
  882. return false;
  883. }
  884. read_uint32(buf, buf_end, byte_count);
  885. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  886. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  887. return false;
  888. }
  889. #if WASM_ENABLE_BULK_MEMORY != 0
  890. /* is_passive and memory_index is only used in bulk memory mode */
  891. data_list[i]->is_passive = (bool)is_passive;
  892. data_list[i]->memory_index = memory_index;
  893. #endif
  894. data_list[i]->offset.init_expr_type = init_value.init_expr_type;
  895. data_list[i]->offset.u = init_value.u;
  896. data_list[i]->byte_count = byte_count;
  897. read_byte_array(buf, buf_end, data_list[i]->bytes,
  898. data_list[i]->byte_count);
  899. }
  900. *p_buf = buf;
  901. return true;
  902. fail:
  903. return false;
  904. }
  905. static bool
  906. load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  907. char *error_buf, uint32 error_buf_size)
  908. {
  909. uint32 i;
  910. uint64 total_size;
  911. const uint8 *buf = *p_buf;
  912. read_uint32(buf, buf_end, module->import_memory_count);
  913. /* We don't support import_memory_count > 0 currently */
  914. if (module->import_memory_count > 0)
  915. return false;
  916. read_uint32(buf, buf_end, module->memory_count);
  917. total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
  918. if (!(module->memories =
  919. loader_malloc(total_size, error_buf, error_buf_size))) {
  920. return false;
  921. }
  922. for (i = 0; i < module->memory_count; i++) {
  923. read_uint32(buf, buf_end, module->memories[i].flags);
  924. if (!wasm_memory_check_flags(module->memories[i].flags, error_buf,
  925. error_buf_size, true)) {
  926. return false;
  927. }
  928. read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page);
  929. read_uint32(buf, buf_end, module->memories[i].init_page_count);
  930. read_uint32(buf, buf_end, module->memories[i].max_page_count);
  931. }
  932. read_uint32(buf, buf_end, module->mem_init_data_count);
  933. /* load memory init data list */
  934. if (module->mem_init_data_count > 0
  935. && !load_mem_init_data_list(&buf, buf_end, module, error_buf,
  936. error_buf_size))
  937. return false;
  938. *p_buf = buf;
  939. return true;
  940. fail:
  941. return false;
  942. }
  943. #if WASM_ENABLE_GC != 0
  944. static void
  945. destroy_init_expr(InitializerExpression *expr)
  946. {
  947. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  948. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  949. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  950. wasm_runtime_free(expr->u.data);
  951. }
  952. }
  953. #endif /* end of WASM_ENABLE_GC != 0 */
  954. static void
  955. destroy_import_tables(AOTImportTable *import_tables)
  956. {
  957. wasm_runtime_free(import_tables);
  958. }
  959. static void
  960. destroy_tables(AOTTable *tables)
  961. {
  962. wasm_runtime_free(tables);
  963. }
  964. static void
  965. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
  966. {
  967. uint32 i;
  968. for (i = 0; i < count; i++)
  969. if (data_list[i]) {
  970. #if WASM_ENABLE_GC != 0
  971. uint32 j;
  972. for (j = 0; j < data_list[i]->value_count; j++) {
  973. destroy_init_expr(&data_list[i]->init_values[j]);
  974. }
  975. #endif
  976. wasm_runtime_free(data_list[i]);
  977. }
  978. wasm_runtime_free(data_list);
  979. }
  980. static bool
  981. load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  982. InitializerExpression *expr, char *error_buf,
  983. uint32 error_buf_size)
  984. {
  985. const uint8 *buf = *p_buf;
  986. uint32 init_expr_type = 0;
  987. uint64 *i64x2 = NULL;
  988. bool free_if_fail = false;
  989. buf = (uint8 *)align_ptr(buf, 4);
  990. read_uint32(buf, buf_end, init_expr_type);
  991. switch (init_expr_type) {
  992. case INIT_EXPR_NONE:
  993. break;
  994. case INIT_EXPR_TYPE_I32_CONST:
  995. case INIT_EXPR_TYPE_F32_CONST:
  996. read_uint32(buf, buf_end, expr->u.i32);
  997. break;
  998. case INIT_EXPR_TYPE_I64_CONST:
  999. case INIT_EXPR_TYPE_F64_CONST:
  1000. read_uint64(buf, buf_end, expr->u.i64);
  1001. break;
  1002. case INIT_EXPR_TYPE_V128_CONST:
  1003. i64x2 = (uint64 *)expr->u.v128.i64x2;
  1004. CHECK_BUF(buf, buf_end, sizeof(uint64) * 2);
  1005. wasm_runtime_read_v128(buf, &i64x2[0], &i64x2[1]);
  1006. buf += sizeof(uint64) * 2;
  1007. break;
  1008. case INIT_EXPR_TYPE_GET_GLOBAL:
  1009. read_uint32(buf, buf_end, expr->u.global_index);
  1010. break;
  1011. /* INIT_EXPR_TYPE_FUNCREF_CONST can be used when
  1012. both reference types and GC are disabled */
  1013. case INIT_EXPR_TYPE_FUNCREF_CONST:
  1014. read_uint32(buf, buf_end, expr->u.ref_index);
  1015. break;
  1016. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_REF_TYPES != 0
  1017. case INIT_EXPR_TYPE_REFNULL_CONST:
  1018. read_uint32(buf, buf_end, expr->u.ref_index);
  1019. break;
  1020. #endif /* end of WASM_ENABLE_GC != 0 || WASM_ENABLE_REF_TYPES != 0 */
  1021. #if WASM_ENABLE_GC != 0
  1022. case INIT_EXPR_TYPE_I31_NEW:
  1023. read_uint32(buf, buf_end, expr->u.i32);
  1024. break;
  1025. case INIT_EXPR_TYPE_STRUCT_NEW:
  1026. {
  1027. uint64 size;
  1028. uint32 type_idx, field_count;
  1029. AOTStructType *struct_type = NULL;
  1030. WASMStructNewInitValues *init_values = NULL;
  1031. read_uint32(buf, buf_end, type_idx);
  1032. read_uint32(buf, buf_end, field_count);
  1033. size = offsetof(WASMStructNewInitValues, fields)
  1034. + sizeof(WASMValue) * (uint64)field_count;
  1035. if (!(init_values =
  1036. loader_malloc(size, error_buf, error_buf_size))) {
  1037. return false;
  1038. }
  1039. free_if_fail = true;
  1040. init_values->count = field_count;
  1041. expr->u.data = init_values;
  1042. if (type_idx >= module->type_count) {
  1043. set_error_buf(error_buf, error_buf_size,
  1044. "unknown struct type.");
  1045. goto fail;
  1046. }
  1047. struct_type = (AOTStructType *)module->types[type_idx];
  1048. if (struct_type->field_count != field_count) {
  1049. set_error_buf(error_buf, error_buf_size,
  1050. "invalid field count.");
  1051. goto fail;
  1052. }
  1053. if (field_count > 0) {
  1054. uint32 i;
  1055. for (i = 0; i < field_count; i++) {
  1056. uint32 field_size =
  1057. wasm_value_type_size(struct_type->fields[i].field_type);
  1058. if (field_size <= sizeof(uint32))
  1059. read_uint32(buf, buf_end, init_values->fields[i].u32);
  1060. else if (field_size == sizeof(uint64))
  1061. read_uint64(buf, buf_end, init_values->fields[i].u64);
  1062. else if (field_size == sizeof(uint64) * 2)
  1063. read_byte_array(buf, buf_end, &init_values->fields[i],
  1064. field_size);
  1065. else {
  1066. bh_assert(0);
  1067. }
  1068. }
  1069. }
  1070. break;
  1071. }
  1072. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  1073. read_uint32(buf, buf_end, expr->u.type_index);
  1074. break;
  1075. case INIT_EXPR_TYPE_ARRAY_NEW:
  1076. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  1077. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  1078. {
  1079. uint32 array_elem_type;
  1080. uint32 type_idx, length;
  1081. WASMArrayNewInitValues *init_values = NULL;
  1082. /* Note: at this time the aot types haven't been loaded */
  1083. read_uint32(buf, buf_end, array_elem_type);
  1084. read_uint32(buf, buf_end, type_idx);
  1085. read_uint32(buf, buf_end, length);
  1086. if (init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  1087. expr->u.array_new_default.type_index = type_idx;
  1088. expr->u.array_new_default.length = length;
  1089. }
  1090. else {
  1091. uint32 i, elem_size, elem_data_count;
  1092. uint64 size = offsetof(WASMArrayNewInitValues, elem_data)
  1093. + sizeof(WASMValue) * (uint64)length;
  1094. if (!(init_values =
  1095. loader_malloc(size, error_buf, error_buf_size))) {
  1096. return false;
  1097. }
  1098. free_if_fail = true;
  1099. expr->u.data = init_values;
  1100. init_values->type_idx = type_idx;
  1101. init_values->length = length;
  1102. elem_data_count =
  1103. (init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) ? length
  1104. : 1;
  1105. elem_size = wasm_value_type_size((uint8)array_elem_type);
  1106. for (i = 0; i < elem_data_count; i++) {
  1107. if (elem_size <= sizeof(uint32))
  1108. read_uint32(buf, buf_end,
  1109. init_values->elem_data[i].u32);
  1110. else if (elem_size == sizeof(uint64))
  1111. read_uint64(buf, buf_end,
  1112. init_values->elem_data[i].u64);
  1113. else if (elem_size == sizeof(uint64) * 2)
  1114. read_byte_array(buf, buf_end,
  1115. &init_values->elem_data[i], elem_size);
  1116. else {
  1117. bh_assert(0);
  1118. }
  1119. }
  1120. }
  1121. break;
  1122. }
  1123. #endif /* end of WASM_ENABLE_GC != 0 */
  1124. default:
  1125. set_error_buf(error_buf, error_buf_size, "invalid init expr type.");
  1126. return false;
  1127. }
  1128. expr->init_expr_type = (uint8)init_expr_type;
  1129. *p_buf = buf;
  1130. return true;
  1131. fail:
  1132. #if WASM_ENABLE_GC != 0
  1133. if (free_if_fail) {
  1134. wasm_runtime_free(expr->u.data);
  1135. }
  1136. #else
  1137. (void)free_if_fail;
  1138. #endif
  1139. return false;
  1140. }
  1141. static bool
  1142. load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
  1143. AOTModule *module, char *error_buf,
  1144. uint32 error_buf_size)
  1145. {
  1146. const uint8 *buf = *p_buf;
  1147. AOTImportTable *import_table;
  1148. #if WASM_ENABLE_GC != 0
  1149. WASMRefType ref_type;
  1150. #endif
  1151. uint64 size;
  1152. uint32 i;
  1153. /* Allocate memory */
  1154. size = sizeof(AOTImportTable) * (uint64)module->import_table_count;
  1155. if (!(module->import_tables = import_table =
  1156. loader_malloc(size, error_buf, error_buf_size))) {
  1157. return false;
  1158. }
  1159. /* keep sync with aot_emit_table_info() aot_emit_aot_file */
  1160. for (i = 0; i < module->import_table_count; i++, import_table++) {
  1161. read_uint8(buf, buf_end, import_table->table_type.elem_type);
  1162. read_uint8(buf, buf_end, import_table->table_type.flags);
  1163. read_uint8(buf, buf_end, import_table->table_type.possible_grow);
  1164. #if WASM_ENABLE_GC != 0
  1165. if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
  1166. read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
  1167. }
  1168. #endif
  1169. read_uint32(buf, buf_end, import_table->table_type.init_size);
  1170. read_uint32(buf, buf_end, import_table->table_type.max_size);
  1171. #if WASM_ENABLE_GC != 0
  1172. if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
  1173. read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
  1174. ref_type.ref_type = import_table->table_type.elem_type;
  1175. /* TODO: check ref_type */
  1176. if (!(import_table->table_type.elem_ref_type =
  1177. wasm_reftype_set_insert(module->ref_type_set,
  1178. &ref_type))) {
  1179. set_error_buf(error_buf, error_buf_size,
  1180. "insert ref type to hash set failed");
  1181. return false;
  1182. }
  1183. }
  1184. #endif
  1185. }
  1186. *p_buf = buf;
  1187. return true;
  1188. fail:
  1189. return false;
  1190. }
  1191. static bool
  1192. load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1193. char *error_buf, uint32 error_buf_size)
  1194. {
  1195. const uint8 *buf = *p_buf;
  1196. AOTTable *table;
  1197. #if WASM_ENABLE_GC != 0
  1198. WASMRefType ref_type;
  1199. #endif
  1200. uint64 size;
  1201. uint32 i;
  1202. /* Allocate memory */
  1203. size = sizeof(AOTTable) * (uint64)module->table_count;
  1204. if (!(module->tables = table =
  1205. loader_malloc(size, error_buf, error_buf_size))) {
  1206. return false;
  1207. }
  1208. /* Create each table data segment */
  1209. for (i = 0; i < module->table_count; i++, table++) {
  1210. read_uint8(buf, buf_end, table->table_type.elem_type);
  1211. read_uint8(buf, buf_end, table->table_type.flags);
  1212. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  1213. error_buf_size, true)) {
  1214. return false;
  1215. }
  1216. read_uint8(buf, buf_end, table->table_type.possible_grow);
  1217. #if WASM_ENABLE_GC != 0
  1218. if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
  1219. read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
  1220. }
  1221. #endif
  1222. read_uint32(buf, buf_end, table->table_type.init_size);
  1223. read_uint32(buf, buf_end, table->table_type.max_size);
  1224. #if WASM_ENABLE_GC != 0
  1225. if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
  1226. read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
  1227. ref_type.ref_type = table->table_type.elem_type;
  1228. /* TODO: check ref_type */
  1229. if (!(table->table_type.elem_ref_type = wasm_reftype_set_insert(
  1230. module->ref_type_set, &ref_type))) {
  1231. set_error_buf(error_buf, error_buf_size,
  1232. "insert ref type to hash set failed");
  1233. return false;
  1234. }
  1235. }
  1236. if (!load_init_expr(&buf, buf_end, module, &table->init_expr, error_buf,
  1237. error_buf_size))
  1238. return false;
  1239. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  1240. && table->init_expr.init_expr_type
  1241. <= INIT_EXPR_TYPE_EXTERN_CONVERT_ANY) {
  1242. set_error_buf(error_buf, error_buf_size,
  1243. "unsupported initializer expression for table");
  1244. return false;
  1245. }
  1246. #endif
  1247. }
  1248. *p_buf = buf;
  1249. return true;
  1250. fail:
  1251. return false;
  1252. }
  1253. static bool
  1254. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  1255. AOTModule *module, char *error_buf,
  1256. uint32 error_buf_size)
  1257. {
  1258. const uint8 *buf = *p_buf;
  1259. AOTTableInitData **data_list;
  1260. #if WASM_ENABLE_GC != 0
  1261. WASMRefType reftype;
  1262. #endif
  1263. uint64 size;
  1264. uint32 i, j;
  1265. /* Allocate memory */
  1266. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  1267. if (!(module->table_init_data_list = data_list =
  1268. loader_malloc(size, error_buf, error_buf_size))) {
  1269. return false;
  1270. }
  1271. /* Create each table data segment */
  1272. for (i = 0; i < module->table_init_data_count; i++) {
  1273. uint32 mode, elem_type;
  1274. uint32 table_index, init_expr_type, value_count;
  1275. uint64 init_expr_value, size1;
  1276. read_uint32(buf, buf_end, mode);
  1277. read_uint32(buf, buf_end, elem_type);
  1278. read_uint32(buf, buf_end, table_index);
  1279. read_uint32(buf, buf_end, init_expr_type);
  1280. read_uint64(buf, buf_end, init_expr_value);
  1281. #if WASM_ENABLE_GC != 0
  1282. if (wasm_is_type_multi_byte_type(elem_type)) {
  1283. uint16 ref_type, nullable;
  1284. read_uint16(buf, buf_end, ref_type);
  1285. if (elem_type != ref_type) {
  1286. set_error_buf(error_buf, error_buf_size, "invalid elem type");
  1287. return false;
  1288. }
  1289. reftype.ref_ht_common.ref_type = (uint8)ref_type;
  1290. read_uint16(buf, buf_end, nullable);
  1291. if (nullable != 0 && nullable != 1) {
  1292. set_error_buf(error_buf, error_buf_size,
  1293. "invalid nullable value");
  1294. return false;
  1295. }
  1296. reftype.ref_ht_common.nullable = (uint8)nullable;
  1297. read_uint32(buf, buf_end, reftype.ref_ht_common.heap_type);
  1298. }
  1299. else
  1300. #endif
  1301. {
  1302. /* Skip 8 byte for ref type info */
  1303. buf += 8;
  1304. }
  1305. read_uint32(buf, buf_end, value_count);
  1306. size1 = sizeof(InitializerExpression) * (uint64)value_count;
  1307. size = offsetof(AOTTableInitData, init_values) + size1;
  1308. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  1309. return false;
  1310. }
  1311. data_list[i]->mode = mode;
  1312. data_list[i]->elem_type = elem_type;
  1313. data_list[i]->table_index = table_index;
  1314. #if WASM_ENABLE_GC != 0
  1315. if (wasm_is_type_multi_byte_type(elem_type)) {
  1316. if (!(data_list[i]->elem_ref_type =
  1317. reftype_set_insert(module->ref_type_set, &reftype,
  1318. error_buf, error_buf_size))) {
  1319. goto fail;
  1320. }
  1321. }
  1322. #endif
  1323. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  1324. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  1325. data_list[i]->value_count = value_count;
  1326. for (j = 0; j < data_list[i]->value_count; j++) {
  1327. if (!load_init_expr(&buf, buf_end, module,
  1328. &data_list[i]->init_values[j], error_buf,
  1329. error_buf_size))
  1330. return false;
  1331. }
  1332. }
  1333. *p_buf = buf;
  1334. return true;
  1335. fail:
  1336. return false;
  1337. }
  1338. static bool
  1339. load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1340. char *error_buf, uint32 error_buf_size)
  1341. {
  1342. const uint8 *buf = *p_buf;
  1343. read_uint32(buf, buf_end, module->import_table_count);
  1344. if (module->import_table_count > 0
  1345. && !load_import_table_list(&buf, buf_end, module, error_buf,
  1346. error_buf_size))
  1347. return false;
  1348. read_uint32(buf, buf_end, module->table_count);
  1349. if (module->table_count > 0
  1350. && !load_table_list(&buf, buf_end, module, error_buf, error_buf_size))
  1351. return false;
  1352. read_uint32(buf, buf_end, module->table_init_data_count);
  1353. /* load table init data list */
  1354. if (module->table_init_data_count > 0
  1355. && !load_table_init_data_list(&buf, buf_end, module, error_buf,
  1356. error_buf_size))
  1357. return false;
  1358. *p_buf = buf;
  1359. return true;
  1360. fail:
  1361. return false;
  1362. }
  1363. static void
  1364. destroy_type(AOTType *type)
  1365. {
  1366. #if WASM_ENABLE_GC != 0
  1367. if (type->ref_count > 1) {
  1368. /* The type is referenced by other types
  1369. of current aot module */
  1370. type->ref_count--;
  1371. return;
  1372. }
  1373. if (type->type_flag == WASM_TYPE_FUNC) {
  1374. AOTFuncType *func_type = (AOTFuncType *)type;
  1375. if (func_type->ref_type_maps != NULL) {
  1376. bh_assert(func_type->ref_type_map_count > 0);
  1377. wasm_runtime_free(func_type->ref_type_maps);
  1378. }
  1379. }
  1380. else if (type->type_flag == WASM_TYPE_STRUCT) {
  1381. AOTStructType *struct_type = (AOTStructType *)type;
  1382. if (struct_type->ref_type_maps != NULL) {
  1383. bh_assert(struct_type->ref_type_map_count > 0);
  1384. wasm_runtime_free(struct_type->ref_type_maps);
  1385. }
  1386. }
  1387. #endif
  1388. wasm_runtime_free(type);
  1389. }
  1390. static void
  1391. destroy_types(AOTType **types, uint32 count)
  1392. {
  1393. uint32 i;
  1394. for (i = 0; i < count; i++) {
  1395. if (types[i]) {
  1396. destroy_type(types[i]);
  1397. }
  1398. }
  1399. wasm_runtime_free(types);
  1400. }
  1401. #if WASM_ENABLE_GC != 0
  1402. static void
  1403. init_base_type(AOTType *base_type, uint32 type_idx, uint16 type_flag,
  1404. bool is_sub_final, uint32 parent_type_idx, uint16 rec_count,
  1405. uint16 rec_idx)
  1406. {
  1407. base_type->type_flag = type_flag;
  1408. base_type->ref_count = 1;
  1409. base_type->is_sub_final = is_sub_final;
  1410. base_type->parent_type_idx = parent_type_idx;
  1411. base_type->rec_count = rec_count;
  1412. base_type->rec_idx = rec_idx;
  1413. base_type->rec_begin_type_idx = type_idx - rec_idx;
  1414. }
  1415. static bool
  1416. load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1417. char *error_buf, uint32 error_buf_size)
  1418. {
  1419. const uint8 *buf = *p_buf;
  1420. AOTType **types;
  1421. uint64 size;
  1422. uint32 i, j;
  1423. uint32 type_flag, param_cell_num, ret_cell_num;
  1424. uint16 param_count, result_count, ref_type_map_count, rec_count, rec_idx;
  1425. bool is_equivalence_type, is_sub_final;
  1426. uint32 parent_type_idx;
  1427. WASMRefType ref_type;
  1428. /* Allocate memory */
  1429. size = sizeof(AOTFuncType *) * (uint64)module->type_count;
  1430. if (!(types = loader_malloc(size, error_buf, error_buf_size))) {
  1431. return false;
  1432. }
  1433. module->types = types;
  1434. /* Create each type */
  1435. for (i = 0; i < module->type_count; i++) {
  1436. buf = align_ptr(buf, 4);
  1437. /* Read base type info */
  1438. read_uint16(buf, buf_end, type_flag);
  1439. read_uint8(buf, buf_end, is_equivalence_type);
  1440. /* If there is an equivalence type, re-use it */
  1441. if (is_equivalence_type) {
  1442. uint8 u8;
  1443. /* padding */
  1444. read_uint8(buf, buf_end, u8);
  1445. (void)u8;
  1446. read_uint32(buf, buf_end, j);
  1447. if (module->types[j]->ref_count == UINT16_MAX) {
  1448. set_error_buf(error_buf, error_buf_size,
  1449. "wasm type's ref count too large");
  1450. goto fail;
  1451. }
  1452. module->types[j]->ref_count++;
  1453. module->types[i] = module->types[j];
  1454. continue;
  1455. }
  1456. read_uint8(buf, buf_end, is_sub_final);
  1457. read_uint32(buf, buf_end, parent_type_idx);
  1458. read_uint16(buf, buf_end, rec_count);
  1459. read_uint16(buf, buf_end, rec_idx);
  1460. if (type_flag == WASM_TYPE_FUNC) {
  1461. AOTFuncType *func_type;
  1462. /* Read param count */
  1463. read_uint16(buf, buf_end, param_count);
  1464. /* Read result count */
  1465. read_uint16(buf, buf_end, result_count);
  1466. /* Read ref_type_map_count */
  1467. read_uint16(buf, buf_end, ref_type_map_count);
  1468. func_type =
  1469. loader_malloc(sizeof(AOTFuncType) + param_count + result_count,
  1470. error_buf, error_buf_size);
  1471. if (!func_type) {
  1472. goto fail;
  1473. }
  1474. types[i] = (AOTType *)func_type;
  1475. init_base_type((AOTType *)func_type, i, type_flag, is_sub_final,
  1476. parent_type_idx, rec_count, rec_idx);
  1477. func_type->param_count = param_count;
  1478. func_type->result_count = result_count;
  1479. /* Read types of params */
  1480. read_byte_array(buf, buf_end, func_type->types,
  1481. func_type->param_count + func_type->result_count);
  1482. func_type->ref_type_map_count = ref_type_map_count;
  1483. if (!is_valid_func_type(func_type))
  1484. goto fail;
  1485. param_cell_num = wasm_get_cell_num(func_type->types, param_count);
  1486. ret_cell_num =
  1487. wasm_get_cell_num(func_type->types + param_count, result_count);
  1488. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1489. set_error_buf(error_buf, error_buf_size,
  1490. "param count or result count too large");
  1491. goto fail;
  1492. }
  1493. func_type->param_cell_num = param_cell_num;
  1494. func_type->ret_cell_num = ret_cell_num;
  1495. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1496. func_type->quick_aot_entry =
  1497. wasm_native_lookup_quick_aot_entry(func_type);
  1498. #endif
  1499. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1500. "ref type map count: %d",
  1501. i, param_count, result_count, ref_type_map_count);
  1502. /* If ref_type_map is not empty, read ref_type_map */
  1503. if (ref_type_map_count > 0) {
  1504. bh_assert(func_type->ref_type_map_count
  1505. <= func_type->param_count + func_type->result_count);
  1506. /* align to 4 since param_count + result_count may be odd */
  1507. buf = align_ptr(buf, 4);
  1508. if (!(func_type->ref_type_maps =
  1509. loader_malloc(sizeof(WASMRefTypeMap)
  1510. * func_type->ref_type_map_count,
  1511. error_buf, error_buf_size))) {
  1512. goto fail;
  1513. }
  1514. for (j = 0; j < func_type->ref_type_map_count; j++) {
  1515. read_uint16(buf, buf_end,
  1516. func_type->ref_type_maps[j].index);
  1517. read_uint8(buf, buf_end, ref_type.ref_ht_common.ref_type);
  1518. read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
  1519. read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
  1520. /* TODO: check ref_type */
  1521. if (!(func_type->ref_type_maps[j].ref_type =
  1522. wasm_reftype_set_insert(module->ref_type_set,
  1523. &ref_type))) {
  1524. set_error_buf(error_buf, error_buf_size,
  1525. "insert ref type to hash set failed");
  1526. goto fail;
  1527. }
  1528. }
  1529. func_type->result_ref_type_maps = func_type->ref_type_maps;
  1530. for (j = 0; j < func_type->param_count; j++) {
  1531. if (wasm_is_type_multi_byte_type(func_type->types[j]))
  1532. func_type->result_ref_type_maps++;
  1533. }
  1534. }
  1535. }
  1536. else if (type_flag == WASM_TYPE_STRUCT) {
  1537. AOTStructType *struct_type;
  1538. const uint8 *buf_org;
  1539. uint16 *reference_table;
  1540. uint16 field_count, ref_field_count = 0;
  1541. uint32 offset;
  1542. read_uint16(buf, buf_end, field_count);
  1543. read_uint16(buf, buf_end, ref_type_map_count);
  1544. buf_org = buf;
  1545. /* Traverse first time to get ref_field_count */
  1546. for (j = 0; j < field_count; j++) {
  1547. uint8 field_flags, field_type;
  1548. read_uint8(buf, buf_end, field_flags);
  1549. read_uint8(buf, buf_end, field_type);
  1550. if (wasm_is_type_reftype(field_type))
  1551. ref_field_count++;
  1552. (void)field_flags;
  1553. }
  1554. struct_type = loader_malloc(
  1555. sizeof(AOTStructType)
  1556. + sizeof(WASMStructFieldType) * (uint64)field_count
  1557. + sizeof(uint16) * (uint64)(ref_field_count + 1),
  1558. error_buf, error_buf_size);
  1559. if (!struct_type) {
  1560. goto fail;
  1561. }
  1562. offset = (uint32)offsetof(WASMStructObject, field_data);
  1563. types[i] = (AOTType *)struct_type;
  1564. init_base_type((AOTType *)struct_type, i, type_flag, is_sub_final,
  1565. parent_type_idx, rec_count, rec_idx);
  1566. struct_type->field_count = field_count;
  1567. struct_type->ref_type_map_count = ref_type_map_count;
  1568. struct_type->reference_table = reference_table =
  1569. (uint16 *)((uint8 *)struct_type
  1570. + offsetof(AOTStructType, fields)
  1571. + sizeof(WASMStructFieldType) * field_count);
  1572. *reference_table++ = ref_field_count;
  1573. LOG_VERBOSE(
  1574. "type %u: struct, field count: %d, ref type map count: %d", i,
  1575. field_count, ref_type_map_count);
  1576. buf = buf_org;
  1577. /* Traverse again to read each field */
  1578. for (j = 0; j < field_count; j++) {
  1579. uint8 field_type, field_size;
  1580. read_uint8(buf, buf_end, struct_type->fields[j].field_flags);
  1581. read_uint8(buf, buf_end, field_type);
  1582. struct_type->fields[j].field_type = field_type;
  1583. struct_type->fields[j].field_size = field_size =
  1584. (uint8)wasm_reftype_size(field_type);
  1585. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1586. || defined(BUILD_TARGET_X86_32))
  1587. if (field_size == 2)
  1588. offset = align_uint(offset, 2);
  1589. else if (field_size >= 4) /* field size is 4 or 8 */
  1590. offset = align_uint(offset, 4);
  1591. #endif
  1592. struct_type->fields[j].field_offset = offset;
  1593. if (wasm_is_type_reftype(field_type))
  1594. *reference_table++ = offset;
  1595. offset += field_size;
  1596. LOG_VERBOSE(" field: %d, flags: %d, type: %d", j,
  1597. struct_type->fields[j].field_flags,
  1598. struct_type->fields[j].field_type);
  1599. }
  1600. struct_type->total_size = offset;
  1601. buf = align_ptr(buf, 4);
  1602. /* If ref_type_map is not empty, read ref_type_map */
  1603. if (ref_type_map_count > 0) {
  1604. bh_assert(struct_type->ref_type_map_count <= field_count);
  1605. if (!(struct_type->ref_type_maps =
  1606. loader_malloc(sizeof(WASMRefTypeMap)
  1607. * struct_type->ref_type_map_count,
  1608. error_buf, error_buf_size))) {
  1609. goto fail;
  1610. }
  1611. for (j = 0; j < struct_type->ref_type_map_count; j++) {
  1612. read_uint16(buf, buf_end,
  1613. struct_type->ref_type_maps[j].index);
  1614. read_uint8(buf, buf_end, ref_type.ref_ht_common.ref_type);
  1615. read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
  1616. read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
  1617. /* TODO: check ref_type */
  1618. if (!(struct_type->ref_type_maps[j].ref_type =
  1619. wasm_reftype_set_insert(module->ref_type_set,
  1620. &ref_type))) {
  1621. set_error_buf(error_buf, error_buf_size,
  1622. "insert ref type to hash set failed");
  1623. goto fail;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. else if (type_flag == WASM_TYPE_ARRAY) {
  1629. AOTArrayType *array_type;
  1630. array_type =
  1631. loader_malloc(sizeof(AOTArrayType), error_buf, error_buf_size);
  1632. if (!array_type) {
  1633. goto fail;
  1634. }
  1635. types[i] = (AOTType *)array_type;
  1636. init_base_type((AOTType *)array_type, i, type_flag, is_sub_final,
  1637. parent_type_idx, rec_count, rec_idx);
  1638. read_uint16(buf, buf_end, array_type->elem_flags);
  1639. read_uint8(buf, buf_end, array_type->elem_type);
  1640. if (wasm_is_type_multi_byte_type(array_type->elem_type)) {
  1641. read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
  1642. read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
  1643. ref_type.ref_type = array_type->elem_type;
  1644. /* TODO: check ref_type */
  1645. if (!(array_type->elem_ref_type = wasm_reftype_set_insert(
  1646. module->ref_type_set, &ref_type))) {
  1647. set_error_buf(error_buf, error_buf_size,
  1648. "insert ref type to hash set failed");
  1649. goto fail;
  1650. }
  1651. }
  1652. LOG_VERBOSE("type %u: array", i);
  1653. }
  1654. else {
  1655. set_error_buf_v(error_buf, error_buf_size,
  1656. "invalid type flag: %" PRIu32, type_flag);
  1657. goto fail;
  1658. }
  1659. if ((rec_count == 0) || (rec_idx == rec_count - 1)) {
  1660. if (rec_count == 0) {
  1661. bh_assert(rec_idx == 0);
  1662. }
  1663. for (j = i - rec_idx; j <= i; j++) {
  1664. AOTType *cur_type = module->types[j];
  1665. parent_type_idx = cur_type->parent_type_idx;
  1666. if (parent_type_idx != (uint32)-1) { /* has parent */
  1667. AOTType *parent_type = module->types[parent_type_idx];
  1668. module->types[j]->parent_type = parent_type;
  1669. module->types[j]->root_type = parent_type->root_type;
  1670. if (parent_type->inherit_depth == UINT16_MAX) {
  1671. set_error_buf(error_buf, error_buf_size,
  1672. "parent type's inherit depth too large");
  1673. goto fail;
  1674. }
  1675. module->types[j]->inherit_depth =
  1676. parent_type->inherit_depth + 1;
  1677. }
  1678. else {
  1679. module->types[j]->parent_type = NULL;
  1680. module->types[j]->root_type = module->types[j];
  1681. module->types[j]->inherit_depth = 0;
  1682. }
  1683. }
  1684. for (j = i - rec_idx; j <= i; j++) {
  1685. AOTType *cur_type = module->types[j];
  1686. parent_type_idx = cur_type->parent_type_idx;
  1687. if (parent_type_idx != (uint32)-1) { /* has parent */
  1688. AOTType *parent_type = module->types[parent_type_idx];
  1689. /* subtyping has been checked during compilation */
  1690. bh_assert(wasm_type_is_subtype_of(
  1691. module->types[j], parent_type, module->types, i + 1));
  1692. (void)parent_type;
  1693. }
  1694. }
  1695. }
  1696. }
  1697. if (module->type_count) {
  1698. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1699. * module->type_count,
  1700. error_buf, error_buf_size))) {
  1701. goto fail;
  1702. }
  1703. }
  1704. *p_buf = buf;
  1705. return true;
  1706. fail:
  1707. /* Destroy all types */
  1708. destroy_types(types, module->type_count);
  1709. module->types = NULL;
  1710. return false;
  1711. }
  1712. #else
  1713. static bool
  1714. load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1715. char *error_buf, uint32 error_buf_size)
  1716. {
  1717. const uint8 *buf = *p_buf;
  1718. AOTFuncType **func_types;
  1719. uint64 size;
  1720. uint32 i;
  1721. /* Allocate memory */
  1722. size = sizeof(AOTFuncType *) * (uint64)module->type_count;
  1723. if (!(func_types = loader_malloc(size, error_buf, error_buf_size))) {
  1724. return false;
  1725. }
  1726. module->types = (AOTType **)func_types;
  1727. /* Create each function type */
  1728. for (i = 0; i < module->type_count; i++) {
  1729. uint32 type_flag;
  1730. uint32 param_count, result_count;
  1731. uint32 param_cell_num, ret_cell_num;
  1732. uint64 size1;
  1733. buf = align_ptr(buf, 4);
  1734. read_uint16(buf, buf_end, type_flag);
  1735. read_uint16(buf, buf_end, param_count);
  1736. read_uint16(buf, buf_end, result_count);
  1737. size1 = (uint64)param_count + (uint64)result_count;
  1738. size = offsetof(AOTFuncType, types) + size1;
  1739. if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) {
  1740. return false;
  1741. }
  1742. func_types[i]->param_count = (uint16)param_count;
  1743. func_types[i]->result_count = (uint16)result_count;
  1744. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  1745. if (!is_valid_func_type(func_types[i]))
  1746. goto fail;
  1747. param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count);
  1748. ret_cell_num =
  1749. wasm_get_cell_num(func_types[i]->types + param_count, result_count);
  1750. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1751. set_error_buf(error_buf, error_buf_size,
  1752. "param count or result count too large");
  1753. return false;
  1754. }
  1755. func_types[i]->param_cell_num = (uint16)param_cell_num;
  1756. func_types[i]->ret_cell_num = (uint16)ret_cell_num;
  1757. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1758. func_types[i]->quick_aot_entry =
  1759. wasm_native_lookup_quick_aot_entry(func_types[i]);
  1760. #endif
  1761. }
  1762. *p_buf = buf;
  1763. return true;
  1764. fail:
  1765. return false;
  1766. }
  1767. #endif /* end of WASM_ENABLE_GC != 0 */
  1768. static bool
  1769. load_type_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1770. char *error_buf, uint32 error_buf_size)
  1771. {
  1772. const uint8 *buf = *p_buf;
  1773. read_uint32(buf, buf_end, module->type_count);
  1774. /* load function type */
  1775. if (module->type_count > 0
  1776. && !load_types(&buf, buf_end, module, error_buf, error_buf_size))
  1777. return false;
  1778. *p_buf = buf;
  1779. return true;
  1780. fail:
  1781. return false;
  1782. }
  1783. static void
  1784. destroy_import_globals(AOTImportGlobal *import_globals)
  1785. {
  1786. wasm_runtime_free(import_globals);
  1787. }
  1788. static bool
  1789. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  1790. AOTModule *module, bool is_load_from_file_buf,
  1791. char *error_buf, uint32 error_buf_size)
  1792. {
  1793. const uint8 *buf = *p_buf;
  1794. AOTImportGlobal *import_globals;
  1795. uint64 size;
  1796. uint32 i, data_offset = 0;
  1797. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1798. WASMGlobalImport tmp_global;
  1799. #endif
  1800. /* Allocate memory */
  1801. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  1802. if (!(module->import_globals = import_globals =
  1803. loader_malloc(size, error_buf, error_buf_size))) {
  1804. return false;
  1805. }
  1806. /* Create each import global */
  1807. for (i = 0; i < module->import_global_count; i++) {
  1808. buf = (uint8 *)align_ptr(buf, 2);
  1809. read_uint8(buf, buf_end, import_globals[i].type.val_type);
  1810. read_uint8(buf, buf_end, import_globals[i].type.is_mutable);
  1811. read_string(buf, buf_end, import_globals[i].module_name);
  1812. read_string(buf, buf_end, import_globals[i].global_name);
  1813. if (!is_valid_value_type(import_globals[i].type.val_type)) {
  1814. return false;
  1815. }
  1816. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1817. if (wasm_native_lookup_libc_builtin_global(
  1818. import_globals[i].module_name, import_globals[i].global_name,
  1819. &tmp_global)) {
  1820. if (tmp_global.type.val_type != import_globals[i].type.val_type
  1821. || tmp_global.type.is_mutable
  1822. != import_globals[i].type.is_mutable) {
  1823. set_error_buf(error_buf, error_buf_size,
  1824. "incompatible import type");
  1825. return false;
  1826. }
  1827. import_globals[i].global_data_linked =
  1828. tmp_global.global_data_linked;
  1829. import_globals[i].is_linked = true;
  1830. }
  1831. #else
  1832. import_globals[i].is_linked = false;
  1833. #endif
  1834. import_globals[i].size =
  1835. wasm_value_type_size(import_globals[i].type.val_type);
  1836. import_globals[i].data_offset = data_offset;
  1837. data_offset += import_globals[i].size;
  1838. module->global_data_size += import_globals[i].size;
  1839. }
  1840. *p_buf = buf;
  1841. return true;
  1842. fail:
  1843. return false;
  1844. }
  1845. static bool
  1846. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  1847. AOTModule *module, bool is_load_from_file_buf,
  1848. char *error_buf, uint32 error_buf_size)
  1849. {
  1850. const uint8 *buf = *p_buf;
  1851. read_uint32(buf, buf_end, module->import_global_count);
  1852. /* load import globals */
  1853. if (module->import_global_count > 0
  1854. && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf,
  1855. error_buf, error_buf_size))
  1856. return false;
  1857. *p_buf = buf;
  1858. return true;
  1859. fail:
  1860. return false;
  1861. }
  1862. static void
  1863. destroy_globals(AOTGlobal *globals)
  1864. {
  1865. wasm_runtime_free(globals);
  1866. }
  1867. static bool
  1868. load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1869. char *error_buf, uint32 error_buf_size)
  1870. {
  1871. const uint8 *buf = *p_buf;
  1872. AOTGlobal *globals;
  1873. uint64 size;
  1874. uint32 i, data_offset = 0;
  1875. AOTImportGlobal *last_import_global;
  1876. /* Allocate memory */
  1877. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  1878. if (!(module->globals = globals =
  1879. loader_malloc(size, error_buf, error_buf_size))) {
  1880. return false;
  1881. }
  1882. if (module->import_global_count > 0) {
  1883. last_import_global =
  1884. &module->import_globals[module->import_global_count - 1];
  1885. data_offset =
  1886. last_import_global->data_offset + last_import_global->size;
  1887. }
  1888. /* Create each global */
  1889. for (i = 0; i < module->global_count; i++) {
  1890. read_uint8(buf, buf_end, globals[i].type.val_type);
  1891. read_uint8(buf, buf_end, globals[i].type.is_mutable);
  1892. if (!is_valid_value_type(globals[i].type.val_type))
  1893. return false;
  1894. buf = align_ptr(buf, 4);
  1895. if (!load_init_expr(&buf, buf_end, module, &globals[i].init_expr,
  1896. error_buf, error_buf_size))
  1897. return false;
  1898. globals[i].size = wasm_value_type_size(globals[i].type.val_type);
  1899. globals[i].data_offset = data_offset;
  1900. data_offset += globals[i].size;
  1901. module->global_data_size += globals[i].size;
  1902. }
  1903. *p_buf = buf;
  1904. return true;
  1905. fail:
  1906. return false;
  1907. }
  1908. static bool
  1909. load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1910. char *error_buf, uint32 error_buf_size)
  1911. {
  1912. const uint8 *buf = *p_buf;
  1913. read_uint32(buf, buf_end, module->global_count);
  1914. if (is_indices_overflow(module->import_global_count, module->global_count,
  1915. error_buf, error_buf_size))
  1916. return false;
  1917. /* load globals */
  1918. if (module->global_count > 0
  1919. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  1920. return false;
  1921. *p_buf = buf;
  1922. return true;
  1923. fail:
  1924. return false;
  1925. }
  1926. static void
  1927. destroy_import_funcs(AOTImportFunc *import_funcs)
  1928. {
  1929. wasm_runtime_free(import_funcs);
  1930. }
  1931. static bool
  1932. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1933. bool is_load_from_file_buf, bool no_resolve, char *error_buf,
  1934. uint32 error_buf_size)
  1935. {
  1936. const uint8 *buf = *p_buf;
  1937. AOTImportFunc *import_funcs;
  1938. uint64 size;
  1939. uint32 i;
  1940. /* Allocate memory */
  1941. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  1942. if (!(module->import_funcs = import_funcs =
  1943. loader_malloc(size, error_buf, error_buf_size))) {
  1944. return false;
  1945. }
  1946. /* Create each import func */
  1947. for (i = 0; i < module->import_func_count; i++) {
  1948. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  1949. if (import_funcs[i].func_type_index >= module->type_count) {
  1950. set_error_buf(error_buf, error_buf_size, "unknown type");
  1951. return false;
  1952. }
  1953. import_funcs[i].func_type =
  1954. (AOTFuncType *)module->types[import_funcs[i].func_type_index];
  1955. read_string(buf, buf_end, import_funcs[i].module_name);
  1956. read_string(buf, buf_end, import_funcs[i].func_name);
  1957. import_funcs[i].attachment = NULL;
  1958. import_funcs[i].signature = NULL;
  1959. import_funcs[i].call_conv_raw = false;
  1960. if (!no_resolve) {
  1961. aot_resolve_import_func(module, &import_funcs[i]);
  1962. }
  1963. #if WASM_ENABLE_LIBC_WASI != 0
  1964. if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
  1965. || !strcmp(import_funcs[i].module_name, "wasi_snapshot_preview1"))
  1966. module->import_wasi_api = true;
  1967. #endif
  1968. }
  1969. *p_buf = buf;
  1970. return true;
  1971. fail:
  1972. return false;
  1973. }
  1974. static bool
  1975. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  1976. AOTModule *module, bool is_load_from_file_buf,
  1977. bool no_resolve, char *error_buf, uint32 error_buf_size)
  1978. {
  1979. const uint8 *buf = *p_buf;
  1980. read_uint32(buf, buf_end, module->import_func_count);
  1981. /* load import funcs */
  1982. if (module->import_func_count > 0
  1983. && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf,
  1984. no_resolve, error_buf, error_buf_size))
  1985. return false;
  1986. *p_buf = buf;
  1987. return true;
  1988. fail:
  1989. return false;
  1990. }
  1991. static void
  1992. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  1993. uint32 data_section_count)
  1994. {
  1995. uint32 i;
  1996. AOTObjectDataSection *data_section = data_sections;
  1997. for (i = 0; i < data_section_count; i++, data_section++)
  1998. if (data_section->data) {
  1999. #if WASM_ENABLE_STATIC_PGO != 0
  2000. if (!strncmp(data_section->name, "__llvm_prf_data", 15)) {
  2001. LLVMProfileData *data = (LLVMProfileData *)data_section->data;
  2002. if (data->values) {
  2003. uint32 num_value_sites =
  2004. data->num_value_sites[0] + data->num_value_sites[1];
  2005. uint32 j;
  2006. for (j = 0; j < num_value_sites; j++) {
  2007. ValueProfNode *node = data->values[j], *node_next;
  2008. while (node) {
  2009. node_next = node->next;
  2010. wasm_runtime_free(node);
  2011. node = node_next;
  2012. }
  2013. }
  2014. wasm_runtime_free(data->values);
  2015. }
  2016. }
  2017. #endif
  2018. }
  2019. wasm_runtime_free(data_sections);
  2020. }
  2021. static bool
  2022. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  2023. AOTModule *module, bool is_load_from_file_buf,
  2024. char *error_buf, uint32 error_buf_size)
  2025. {
  2026. const uint8 *buf = *p_buf;
  2027. AOTObjectDataSection *data_sections;
  2028. uint64 size;
  2029. uint32 i;
  2030. uint64 total_size = 0;
  2031. uint32 page_size = os_getpagesize();
  2032. uint8 *merged_sections = NULL;
  2033. /* Allocate memory */
  2034. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  2035. if (!(module->data_sections = data_sections =
  2036. loader_malloc(size, error_buf, error_buf_size))) {
  2037. return false;
  2038. }
  2039. /* First iteration: read data from buf, and calculate total memory needed */
  2040. for (i = 0; i < module->data_section_count; i++) {
  2041. read_string(buf, buf_end, data_sections[i].name);
  2042. read_uint32(buf, buf_end, data_sections[i].size);
  2043. CHECK_BUF(buf, buf_end, data_sections[i].size);
  2044. /* Temporary record data ptr for merge, will be replaced after the
  2045. merged_data_sections is mmapped */
  2046. if (data_sections[i].size > 0)
  2047. data_sections[i].data = (uint8 *)buf;
  2048. buf += data_sections[i].size;
  2049. total_size += align_uint64((uint64)data_sections[i].size, page_size);
  2050. }
  2051. if (total_size > UINT32_MAX) {
  2052. set_error_buf(error_buf, error_buf_size, "data sections too large");
  2053. return false;
  2054. }
  2055. if (total_size > 0) {
  2056. /* Allocate memory for data */
  2057. merged_sections = module->merged_data_sections =
  2058. loader_mmap((uint32)total_size, false, error_buf, error_buf_size);
  2059. if (!merged_sections) {
  2060. return false;
  2061. }
  2062. module->merged_data_sections_size = (uint32)total_size;
  2063. }
  2064. /* Second iteration: Create each data section */
  2065. for (i = 0; i < module->data_section_count; i++) {
  2066. if (data_sections[i].size > 0) {
  2067. bh_memcpy_s(merged_sections, data_sections[i].size,
  2068. data_sections[i].data, data_sections[i].size);
  2069. data_sections[i].data = merged_sections;
  2070. merged_sections += align_uint(data_sections[i].size, page_size);
  2071. }
  2072. }
  2073. *p_buf = buf;
  2074. return true;
  2075. fail:
  2076. return false;
  2077. }
  2078. static bool
  2079. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  2080. AOTModule *module, bool is_load_from_file_buf,
  2081. char *error_buf, uint32 error_buf_size)
  2082. {
  2083. const uint8 *buf = *p_buf;
  2084. read_uint32(buf, buf_end, module->data_section_count);
  2085. /* load object data sections */
  2086. if (module->data_section_count > 0
  2087. && !load_object_data_sections(&buf, buf_end, module,
  2088. is_load_from_file_buf, error_buf,
  2089. error_buf_size))
  2090. return false;
  2091. *p_buf = buf;
  2092. return true;
  2093. fail:
  2094. return false;
  2095. }
  2096. static bool
  2097. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  2098. AOTModule *module, bool is_load_from_file_buf,
  2099. bool no_resolve, char *error_buf, uint32 error_buf_size)
  2100. {
  2101. const uint8 *p = buf, *p_end = buf_end;
  2102. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  2103. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  2104. || !load_type_info(&p, p_end, module, error_buf, error_buf_size)
  2105. || !load_import_global_info(&p, p_end, module, is_load_from_file_buf,
  2106. error_buf, error_buf_size)
  2107. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  2108. || !load_import_func_info(&p, p_end, module, is_load_from_file_buf,
  2109. no_resolve, error_buf, error_buf_size))
  2110. return false;
  2111. /* load function count and start function index */
  2112. read_uint32(p, p_end, module->func_count);
  2113. if (is_indices_overflow(module->import_func_count, module->func_count,
  2114. error_buf, error_buf_size))
  2115. return false;
  2116. read_uint32(p, p_end, module->start_func_index);
  2117. /* check start function index */
  2118. if (module->start_func_index != (uint32)-1
  2119. && (module->start_func_index
  2120. >= module->import_func_count + module->func_count)) {
  2121. set_error_buf(error_buf, error_buf_size,
  2122. "invalid start function index");
  2123. return false;
  2124. }
  2125. read_uint32(p, p_end, module->aux_data_end_global_index);
  2126. read_uint64(p, p_end, module->aux_data_end);
  2127. read_uint32(p, p_end, module->aux_heap_base_global_index);
  2128. read_uint64(p, p_end, module->aux_heap_base);
  2129. read_uint32(p, p_end, module->aux_stack_top_global_index);
  2130. read_uint64(p, p_end, module->aux_stack_bottom);
  2131. read_uint32(p, p_end, module->aux_stack_size);
  2132. if (module->aux_data_end >= MAX_LINEAR_MEMORY_SIZE
  2133. || module->aux_heap_base >= MAX_LINEAR_MEMORY_SIZE
  2134. || module->aux_stack_bottom >= MAX_LINEAR_MEMORY_SIZE) {
  2135. set_error_buf(
  2136. error_buf, error_buf_size,
  2137. "invalid range of aux_date_end/aux_heap_base/aux_stack_bottom");
  2138. return false;
  2139. }
  2140. if (!load_object_data_sections_info(&p, p_end, module,
  2141. is_load_from_file_buf, error_buf,
  2142. error_buf_size))
  2143. return false;
  2144. if (p != p_end) {
  2145. set_error_buf(error_buf, error_buf_size,
  2146. "invalid init data section size");
  2147. return false;
  2148. }
  2149. return true;
  2150. fail:
  2151. return false;
  2152. }
  2153. #if !defined(BH_PLATFORM_NUTTX) && !defined(BH_PLATFORM_ESP_IDF)
  2154. static bool
  2155. try_merge_data_and_text(const uint8 **buf, const uint8 **buf_end,
  2156. AOTModule *module, char *error_buf,
  2157. uint32 error_buf_size)
  2158. {
  2159. uint8 *old_buf = (uint8 *)*buf;
  2160. uint8 *old_end = (uint8 *)*buf_end;
  2161. size_t code_size = (size_t)(old_end - old_buf);
  2162. uint32 page_size = os_getpagesize();
  2163. uint64 total_size = 0;
  2164. uint32 i;
  2165. uint8 *sections;
  2166. if (code_size == 0) {
  2167. return true;
  2168. }
  2169. /* calculate the total memory needed */
  2170. total_size += align_uint64((uint64)code_size, page_size);
  2171. for (i = 0; i < module->data_section_count; ++i) {
  2172. total_size +=
  2173. align_uint64((uint64)module->data_sections[i].size, page_size);
  2174. }
  2175. /* distance between .data and .text should not be greater than 4GB
  2176. for some targets (e.g. arm64 reloc need < 4G distance) */
  2177. if (total_size > UINT32_MAX) {
  2178. return false;
  2179. }
  2180. /* code_size was checked and must be larger than 0 here */
  2181. bh_assert(total_size > 0);
  2182. sections = loader_mmap((uint32)total_size, false, NULL, 0);
  2183. if (!sections) {
  2184. /* merge failed but may be not critical for some targets */
  2185. return false;
  2186. }
  2187. #ifdef BH_PLATFORM_WINDOWS
  2188. if (!os_mem_commit(sections, code_size,
  2189. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)) {
  2190. os_munmap(sections, (uint32)total_size);
  2191. return false;
  2192. }
  2193. #endif
  2194. /* change the code part to be executable */
  2195. if (os_mprotect(sections, code_size,
  2196. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)
  2197. != 0) {
  2198. os_munmap(sections, (uint32)total_size);
  2199. return false;
  2200. }
  2201. module->merged_data_text_sections = sections;
  2202. module->merged_data_text_sections_size = (uint32)total_size;
  2203. /* order not essential just as compiler does: .text section first */
  2204. *buf = sections;
  2205. *buf_end = sections + code_size;
  2206. bh_memcpy_s(sections, (uint32)code_size, old_buf, (uint32)code_size);
  2207. os_munmap(old_buf, code_size);
  2208. sections += align_uint((uint32)code_size, page_size);
  2209. /* then migrate .data sections */
  2210. for (i = 0; i < module->data_section_count; ++i) {
  2211. AOTObjectDataSection *data_section = module->data_sections + i;
  2212. uint8 *old_data = data_section->data;
  2213. data_section->data = sections;
  2214. bh_memcpy_s(data_section->data, data_section->size, old_data,
  2215. data_section->size);
  2216. sections += align_uint(data_section->size, page_size);
  2217. }
  2218. /* free the original data sections */
  2219. if (module->merged_data_sections) {
  2220. os_munmap(module->merged_data_sections,
  2221. module->merged_data_sections_size);
  2222. module->merged_data_sections = NULL;
  2223. module->merged_data_sections_size = 0;
  2224. }
  2225. return true;
  2226. }
  2227. #endif /* ! defined(BH_PLATFORM_NUTTX) && !defined(BH_PLATFORM_ESP_IDF) */
  2228. static bool
  2229. load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  2230. char *error_buf, uint32 error_buf_size)
  2231. {
  2232. uint8 *plt_base;
  2233. if (module->func_count > 0 && buf_end == buf) {
  2234. set_error_buf(error_buf, error_buf_size, "invalid code size");
  2235. return false;
  2236. }
  2237. /* The layout is: literal size + literal + code (with plt table) */
  2238. read_uint32(buf, buf_end, module->literal_size);
  2239. /* literal data is at beginning of the text section */
  2240. module->literal = (uint8 *)buf;
  2241. module->code = (void *)(buf + module->literal_size);
  2242. module->code_size = (uint32)(buf_end - (uint8 *)module->code);
  2243. #if WASM_ENABLE_DEBUG_AOT != 0
  2244. module->elf_size = module->code_size;
  2245. if (is_ELF(module->code)) {
  2246. /* Now code points to an ELF object, we pull it down to .text section */
  2247. uint64 offset;
  2248. uint64 size;
  2249. char *code_buf = module->code;
  2250. module->elf_hdr = code_buf;
  2251. if (!get_text_section(code_buf, &offset, &size)) {
  2252. set_error_buf(error_buf, error_buf_size,
  2253. "get text section of ELF failed");
  2254. return false;
  2255. }
  2256. module->code = code_buf + offset;
  2257. module->code_size -= (uint32)offset;
  2258. }
  2259. #endif
  2260. if ((module->code_size > 0) && !module->is_indirect_mode) {
  2261. plt_base = (uint8 *)buf_end - get_plt_table_size();
  2262. init_plt_table(plt_base);
  2263. }
  2264. return true;
  2265. fail:
  2266. return false;
  2267. }
  2268. static bool
  2269. load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  2270. char *error_buf, uint32 error_buf_size)
  2271. {
  2272. const uint8 *p = buf, *p_end = buf_end;
  2273. uint32 i;
  2274. uint64 size, text_offset;
  2275. size = sizeof(void *) * (uint64)module->func_count;
  2276. if (size > 0
  2277. && !(module->func_ptrs =
  2278. loader_malloc(size, error_buf, error_buf_size))) {
  2279. return false;
  2280. }
  2281. for (i = 0; i < module->func_count; i++) {
  2282. if (sizeof(void *) == 8) {
  2283. read_uint64(p, p_end, text_offset);
  2284. }
  2285. else {
  2286. uint32 text_offset32;
  2287. read_uint32(p, p_end, text_offset32);
  2288. text_offset = text_offset32;
  2289. }
  2290. if (text_offset >= module->code_size) {
  2291. set_error_buf(error_buf, error_buf_size,
  2292. "invalid function code offset");
  2293. return false;
  2294. }
  2295. module->func_ptrs[i] = (uint8 *)module->code + text_offset;
  2296. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  2297. /* bits[0] of thumb function address must be 1 */
  2298. module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1);
  2299. #endif
  2300. }
  2301. /* Set start function when function pointers are resolved */
  2302. if (module->start_func_index != (uint32)-1) {
  2303. if (module->start_func_index >= module->import_func_count)
  2304. module->start_function =
  2305. module->func_ptrs[module->start_func_index
  2306. - module->import_func_count];
  2307. else
  2308. /* TODO: fix start function can be import function issue */
  2309. module->start_function = NULL;
  2310. }
  2311. else {
  2312. module->start_function = NULL;
  2313. }
  2314. size = sizeof(uint32) * (uint64)module->func_count;
  2315. if (size > 0
  2316. && !(module->func_type_indexes =
  2317. loader_malloc(size, error_buf, error_buf_size))) {
  2318. return false;
  2319. }
  2320. for (i = 0; i < module->func_count; i++) {
  2321. read_uint32(p, p_end, module->func_type_indexes[i]);
  2322. if (module->func_type_indexes[i] >= module->type_count) {
  2323. set_error_buf(error_buf, error_buf_size, "unknown type");
  2324. return false;
  2325. }
  2326. }
  2327. size = sizeof(uint32) * (uint64)module->func_count;
  2328. if (size > 0) {
  2329. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2330. if (!(module->max_local_cell_nums =
  2331. loader_malloc(size, error_buf, error_buf_size))) {
  2332. return false;
  2333. }
  2334. for (i = 0; i < module->func_count; i++) {
  2335. read_uint32(p, p_end, module->max_local_cell_nums[i]);
  2336. }
  2337. if (!(module->max_stack_cell_nums =
  2338. loader_malloc(size, error_buf, error_buf_size))) {
  2339. return false;
  2340. }
  2341. for (i = 0; i < module->func_count; i++) {
  2342. read_uint32(p, p_end, module->max_stack_cell_nums[i]);
  2343. }
  2344. #else
  2345. /* Ignore max_local_cell_num and max_stack_cell_num of each function */
  2346. CHECK_BUF(p, p_end, ((uint32)size * 2));
  2347. p += (uint32)size * 2;
  2348. #endif
  2349. }
  2350. #if WASM_ENABLE_GC != 0
  2351. /* Local(params and locals) ref flags for all import and non-imported
  2352. * functions. The flags indicate whether each cell in the AOTFrame local
  2353. * area is a GC reference. */
  2354. size = sizeof(LocalRefFlag)
  2355. * (uint64)(module->import_func_count + module->func_count);
  2356. if (size > 0) {
  2357. if (!(module->func_local_ref_flags =
  2358. loader_malloc(size, error_buf, error_buf_size))) {
  2359. return false;
  2360. }
  2361. for (i = 0; i < module->import_func_count + module->func_count; i++) {
  2362. uint32 local_ref_flag_cell_num;
  2363. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  2364. read_uint32(
  2365. p, p_end,
  2366. module->func_local_ref_flags[i].local_ref_flag_cell_num);
  2367. local_ref_flag_cell_num =
  2368. module->func_local_ref_flags[i].local_ref_flag_cell_num;
  2369. size = sizeof(uint8) * (uint64)local_ref_flag_cell_num;
  2370. if (size > 0) {
  2371. if (!(module->func_local_ref_flags[i].local_ref_flags =
  2372. loader_malloc(size, error_buf, error_buf_size))) {
  2373. return false;
  2374. }
  2375. read_byte_array(p, p_end,
  2376. module->func_local_ref_flags[i].local_ref_flags,
  2377. local_ref_flag_cell_num);
  2378. }
  2379. }
  2380. }
  2381. #endif /* end of WASM_ENABLE_GC != 0 */
  2382. if (p != buf_end) {
  2383. set_error_buf(error_buf, error_buf_size,
  2384. "invalid function section size");
  2385. return false;
  2386. }
  2387. return true;
  2388. fail:
  2389. return false;
  2390. }
  2391. static void
  2392. destroy_exports(AOTExport *exports)
  2393. {
  2394. wasm_runtime_free(exports);
  2395. }
  2396. static int
  2397. cmp_export_name(const void *a, const void *b)
  2398. {
  2399. return strcmp(*(char **)a, *(char **)b);
  2400. }
  2401. static bool
  2402. check_duplicate_exports(AOTModule *module, char *error_buf,
  2403. uint32 error_buf_size)
  2404. {
  2405. uint32 i;
  2406. bool result = false;
  2407. char *names_buf[32], **names = names_buf;
  2408. if (module->export_count > 32) {
  2409. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  2410. error_buf_size);
  2411. if (!names) {
  2412. return result;
  2413. }
  2414. }
  2415. for (i = 0; i < module->export_count; i++) {
  2416. names[i] = module->exports[i].name;
  2417. }
  2418. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  2419. for (i = 1; i < module->export_count; i++) {
  2420. if (!strcmp(names[i], names[i - 1])) {
  2421. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  2422. goto cleanup;
  2423. }
  2424. }
  2425. result = true;
  2426. cleanup:
  2427. if (module->export_count > 32) {
  2428. wasm_runtime_free(names);
  2429. }
  2430. return result;
  2431. }
  2432. static bool
  2433. load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  2434. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  2435. {
  2436. const uint8 *buf = *p_buf;
  2437. AOTExport *exports;
  2438. uint64 size;
  2439. uint32 i;
  2440. /* Allocate memory */
  2441. size = sizeof(AOTExport) * (uint64)module->export_count;
  2442. if (!(module->exports = exports =
  2443. loader_malloc(size, error_buf, error_buf_size))) {
  2444. return false;
  2445. }
  2446. /* Create each export */
  2447. for (i = 0; i < module->export_count; i++) {
  2448. read_uint32(buf, buf_end, exports[i].index);
  2449. read_uint8(buf, buf_end, exports[i].kind);
  2450. read_string(buf, buf_end, exports[i].name);
  2451. /* Check export kind and index */
  2452. switch (exports[i].kind) {
  2453. case EXPORT_KIND_FUNC:
  2454. if (exports[i].index
  2455. >= module->import_func_count + module->func_count) {
  2456. set_error_buf(error_buf, error_buf_size,
  2457. "unknown function");
  2458. return false;
  2459. }
  2460. break;
  2461. case EXPORT_KIND_TABLE:
  2462. if (exports[i].index
  2463. >= module->import_table_count + module->table_count) {
  2464. set_error_buf(error_buf, error_buf_size, "unknown table");
  2465. return false;
  2466. }
  2467. break;
  2468. case EXPORT_KIND_MEMORY:
  2469. if (exports[i].index
  2470. >= module->import_memory_count + module->memory_count) {
  2471. set_error_buf(error_buf, error_buf_size, "unknown memory");
  2472. return false;
  2473. }
  2474. break;
  2475. case EXPORT_KIND_GLOBAL:
  2476. if (exports[i].index
  2477. >= module->import_global_count + module->global_count) {
  2478. set_error_buf(error_buf, error_buf_size, "unknown global");
  2479. return false;
  2480. }
  2481. break;
  2482. #if WASM_ENABLE_TAGS != 0
  2483. /* TODO
  2484. case EXPORT_KIND_TAG:
  2485. if (index >= module->import_tag_count + module->tag_count) {
  2486. set_error_buf(error_buf, error_buf_size, "unknown tag");
  2487. return false;
  2488. }
  2489. break;
  2490. */
  2491. #endif
  2492. default:
  2493. set_error_buf(error_buf, error_buf_size, "invalid export kind");
  2494. return false;
  2495. }
  2496. }
  2497. if (module->export_count > 0) {
  2498. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  2499. return false;
  2500. }
  2501. }
  2502. *p_buf = buf;
  2503. return true;
  2504. fail:
  2505. return false;
  2506. }
  2507. static bool
  2508. load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  2509. bool is_load_from_file_buf, char *error_buf,
  2510. uint32 error_buf_size)
  2511. {
  2512. const uint8 *p = buf, *p_end = buf_end;
  2513. /* load export functions */
  2514. read_uint32(p, p_end, module->export_count);
  2515. if (module->export_count > 0
  2516. && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf,
  2517. error_buf_size))
  2518. return false;
  2519. if (p != p_end) {
  2520. set_error_buf(error_buf, error_buf_size, "invalid export section size");
  2521. return false;
  2522. }
  2523. return true;
  2524. fail:
  2525. return false;
  2526. }
  2527. static void *
  2528. get_data_section_addr(AOTModule *module, const char *section_name,
  2529. uint32 *p_data_size)
  2530. {
  2531. uint32 i;
  2532. AOTObjectDataSection *data_section = module->data_sections;
  2533. for (i = 0; i < module->data_section_count; i++, data_section++) {
  2534. if (!strcmp(data_section->name, section_name)) {
  2535. if (p_data_size)
  2536. *p_data_size = data_section->size;
  2537. return data_section->data;
  2538. }
  2539. }
  2540. return NULL;
  2541. }
  2542. const void *
  2543. aot_get_data_section_addr(AOTModule *module, const char *section_name,
  2544. uint32 *p_data_size)
  2545. {
  2546. return get_data_section_addr(module, section_name, p_data_size);
  2547. }
  2548. static void *
  2549. resolve_target_sym(const char *symbol, int32 *p_index)
  2550. {
  2551. uint32 i, num = 0;
  2552. SymbolMap *target_sym_map;
  2553. if (!(target_sym_map = get_target_symbol_map(&num)))
  2554. return NULL;
  2555. for (i = 0; i < num; i++) {
  2556. if (!strcmp(target_sym_map[i].symbol_name, symbol)
  2557. #if defined(_WIN32) || defined(_WIN32_)
  2558. /* In Win32, the symbol name of function added by
  2559. LLVMAddFunction() is prefixed by '_', ignore it */
  2560. || (strlen(symbol) > 1 && symbol[0] == '_'
  2561. && !strcmp(target_sym_map[i].symbol_name, symbol + 1))
  2562. #endif
  2563. ) {
  2564. *p_index = (int32)i;
  2565. return target_sym_map[i].symbol_addr;
  2566. }
  2567. }
  2568. return NULL;
  2569. }
  2570. static bool
  2571. is_literal_relocation(const char *reloc_sec_name)
  2572. {
  2573. return !strcmp(reloc_sec_name, ".rela.literal");
  2574. }
  2575. static bool
  2576. str2uint32(const char *buf, uint32 *p_res)
  2577. {
  2578. uint32 res = 0, val;
  2579. const char *buf_end = buf + 8;
  2580. char ch;
  2581. while (buf < buf_end) {
  2582. ch = *buf++;
  2583. if (ch >= '0' && ch <= '9')
  2584. val = ch - '0';
  2585. else if (ch >= 'a' && ch <= 'f')
  2586. val = ch - 'a' + 0xA;
  2587. else if (ch >= 'A' && ch <= 'F')
  2588. val = ch - 'A' + 0xA;
  2589. else
  2590. return false;
  2591. res = (res << 4) | val;
  2592. }
  2593. *p_res = res;
  2594. return true;
  2595. }
  2596. static bool
  2597. str2uint64(const char *buf, uint64 *p_res)
  2598. {
  2599. uint64 res = 0, val;
  2600. const char *buf_end = buf + 16;
  2601. char ch;
  2602. while (buf < buf_end) {
  2603. ch = *buf++;
  2604. if (ch >= '0' && ch <= '9')
  2605. val = ch - '0';
  2606. else if (ch >= 'a' && ch <= 'f')
  2607. val = ch - 'a' + 0xA;
  2608. else if (ch >= 'A' && ch <= 'F')
  2609. val = ch - 'A' + 0xA;
  2610. else
  2611. return false;
  2612. res = (res << 4) | val;
  2613. }
  2614. *p_res = res;
  2615. return true;
  2616. }
  2617. #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative offset to GOT */
  2618. static bool
  2619. is_text_section(const char *section_name)
  2620. {
  2621. return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext");
  2622. }
  2623. static bool
  2624. do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
  2625. char *error_buf, uint32 error_buf_size)
  2626. {
  2627. bool is_literal = is_literal_relocation(group->section_name);
  2628. uint8 *aot_text = is_literal ? module->literal : module->code;
  2629. uint32 aot_text_size =
  2630. is_literal ? module->literal_size : module->code_size;
  2631. uint32 i, func_index, symbol_len;
  2632. #if defined(BH_PLATFORM_WINDOWS)
  2633. uint32 ymm_plt_index = 0, xmm_plt_index = 0;
  2634. uint32 real_plt_index = 0, float_plt_index = 0, j;
  2635. #endif
  2636. char symbol_buf[128] = { 0 }, *symbol, *p;
  2637. void *symbol_addr;
  2638. AOTRelocation *relocation = group->relocations;
  2639. if (group->relocation_count > 0 && !aot_text) {
  2640. set_error_buf(error_buf, error_buf_size,
  2641. "invalid text relocation count");
  2642. return false;
  2643. }
  2644. for (i = 0; i < group->relocation_count; i++, relocation++) {
  2645. int32 symbol_index = -1;
  2646. symbol_len = (uint32)strlen(relocation->symbol_name);
  2647. if (symbol_len + 1 <= sizeof(symbol_buf))
  2648. symbol = symbol_buf;
  2649. else {
  2650. if (!(symbol = loader_malloc(symbol_len + 1, error_buf,
  2651. error_buf_size))) {
  2652. return false;
  2653. }
  2654. }
  2655. bh_memcpy_s(symbol, symbol_len, relocation->symbol_name, symbol_len);
  2656. symbol[symbol_len] = '\0';
  2657. #if WASM_ENABLE_STATIC_PGO != 0
  2658. if (!strcmp(symbol, "__llvm_profile_runtime")
  2659. || !strcmp(symbol, "__llvm_profile_register_function")
  2660. || !strcmp(symbol, "__llvm_profile_register_names_function")) {
  2661. continue;
  2662. }
  2663. #endif
  2664. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  2665. p = symbol + strlen(AOT_FUNC_PREFIX);
  2666. if (*p == '\0'
  2667. || (func_index = (uint32)atoi(p)) > module->func_count) {
  2668. set_error_buf_v(error_buf, error_buf_size,
  2669. "invalid import symbol %s", symbol);
  2670. goto check_symbol_fail;
  2671. }
  2672. #if (defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)) \
  2673. && !defined(BH_PLATFORM_WINDOWS)
  2674. if (relocation->relocation_type == R_X86_64_GOTPCREL) {
  2675. GOTItem *got_item = module->got_item_list;
  2676. uint32 got_item_idx = 0;
  2677. while (got_item) {
  2678. if (got_item->func_idx == func_index)
  2679. break;
  2680. got_item_idx++;
  2681. got_item = got_item->next;
  2682. }
  2683. /* Calculate `GOT + G` */
  2684. symbol_addr = module->got_func_ptrs + got_item_idx;
  2685. }
  2686. else
  2687. symbol_addr = module->func_ptrs[func_index];
  2688. #else
  2689. symbol_addr = module->func_ptrs[func_index];
  2690. #endif
  2691. }
  2692. #if defined(BH_PLATFORM_WINDOWS) && defined(BUILD_TARGET_X86_32)
  2693. /* AOT function name starts with '_' in windows x86-32 */
  2694. else if (!strncmp(symbol, "_" AOT_FUNC_PREFIX,
  2695. strlen("_" AOT_FUNC_PREFIX))) {
  2696. p = symbol + strlen("_" AOT_FUNC_PREFIX);
  2697. if (*p == '\0'
  2698. || (func_index = (uint32)atoi(p)) > module->func_count) {
  2699. set_error_buf_v(error_buf, error_buf_size, "invalid symbol %s",
  2700. symbol);
  2701. goto check_symbol_fail;
  2702. }
  2703. symbol_addr = module->func_ptrs[func_index];
  2704. }
  2705. else if (!strncmp(symbol, "_" AOT_FUNC_INTERNAL_PREFIX,
  2706. strlen("_" AOT_FUNC_INTERNAL_PREFIX))) {
  2707. p = symbol + strlen("_" AOT_FUNC_INTERNAL_PREFIX);
  2708. if (*p == '\0'
  2709. || (func_index = (uint32)atoi(p)) > module->func_count) {
  2710. set_error_buf_v(error_buf, error_buf_size, "invalid symbol %s",
  2711. symbol);
  2712. goto check_symbol_fail;
  2713. }
  2714. symbol_addr = module->func_ptrs[func_index];
  2715. }
  2716. #endif
  2717. else if (is_text_section(symbol)) {
  2718. symbol_addr = module->code;
  2719. }
  2720. else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata")
  2721. || !strcmp(symbol, ".rdata")
  2722. || !strcmp(symbol, ".rodata")
  2723. /* ".rodata.cst4/8/16/.." */
  2724. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))
  2725. /* ".rodata.strn.m" */
  2726. || !strncmp(symbol, ".rodata.str", strlen(".rodata.str"))
  2727. || !strcmp(symbol, AOT_STACK_SIZES_SECTION_NAME)
  2728. #if WASM_ENABLE_STATIC_PGO != 0
  2729. || !strncmp(symbol, "__llvm_prf_cnts", 15)
  2730. || !strncmp(symbol, "__llvm_prf_data", 15)
  2731. || !strncmp(symbol, "__llvm_prf_names", 16)
  2732. #endif
  2733. ) {
  2734. symbol_addr = get_data_section_addr(module, symbol, NULL);
  2735. if (!symbol_addr) {
  2736. set_error_buf_v(error_buf, error_buf_size,
  2737. "invalid data section (%s)", symbol);
  2738. goto check_symbol_fail;
  2739. }
  2740. }
  2741. else if (!strcmp(symbol, ".literal")) {
  2742. symbol_addr = module->literal;
  2743. }
  2744. #if defined(BH_PLATFORM_WINDOWS)
  2745. /* Relocation for symbols which start with "__ymm@", "__xmm@" or
  2746. "__real@" and end with the ymm value, xmm value or real value.
  2747. In Windows PE file, the data is stored in some individual ".rdata"
  2748. sections. We simply create extra plt data, parse the values from
  2749. the symbols and stored them into the extra plt data. */
  2750. else if (!strcmp(group->section_name, ".text")
  2751. && !strncmp(symbol, YMM_PLT_PREFIX, strlen(YMM_PLT_PREFIX))
  2752. && strlen(symbol) == strlen(YMM_PLT_PREFIX) + 64) {
  2753. char ymm_buf[17] = { 0 };
  2754. symbol_addr = module->extra_plt_data + ymm_plt_index * 32;
  2755. for (j = 0; j < 4; j++) {
  2756. bh_memcpy_s(ymm_buf, sizeof(ymm_buf),
  2757. symbol + strlen(YMM_PLT_PREFIX) + 48 - 16 * j, 16);
  2758. if (!str2uint64(ymm_buf,
  2759. (uint64 *)((uint8 *)symbol_addr + 8 * j))) {
  2760. set_error_buf_v(error_buf, error_buf_size,
  2761. "resolve symbol %s failed", symbol);
  2762. goto check_symbol_fail;
  2763. }
  2764. }
  2765. ymm_plt_index++;
  2766. }
  2767. else if (!strcmp(group->section_name, ".text")
  2768. && !strncmp(symbol, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX))
  2769. && strlen(symbol) == strlen(XMM_PLT_PREFIX) + 32) {
  2770. char xmm_buf[17] = { 0 };
  2771. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  2772. + xmm_plt_index * 16;
  2773. for (j = 0; j < 2; j++) {
  2774. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  2775. symbol + strlen(XMM_PLT_PREFIX) + 16 - 16 * j, 16);
  2776. if (!str2uint64(xmm_buf,
  2777. (uint64 *)((uint8 *)symbol_addr + 8 * j))) {
  2778. set_error_buf_v(error_buf, error_buf_size,
  2779. "resolve symbol %s failed", symbol);
  2780. goto check_symbol_fail;
  2781. }
  2782. }
  2783. xmm_plt_index++;
  2784. }
  2785. else if (!strcmp(group->section_name, ".text")
  2786. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  2787. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 16) {
  2788. char real_buf[17] = { 0 };
  2789. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  2790. + module->xmm_plt_count * 16 + real_plt_index * 8;
  2791. bh_memcpy_s(real_buf, sizeof(real_buf),
  2792. symbol + strlen(REAL_PLT_PREFIX), 16);
  2793. if (!str2uint64(real_buf, (uint64 *)symbol_addr)) {
  2794. set_error_buf_v(error_buf, error_buf_size,
  2795. "resolve symbol %s failed", symbol);
  2796. goto check_symbol_fail;
  2797. }
  2798. real_plt_index++;
  2799. }
  2800. else if (!strcmp(group->section_name, ".text")
  2801. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  2802. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 8) {
  2803. char float_buf[9] = { 0 };
  2804. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  2805. + module->xmm_plt_count * 16
  2806. + module->real_plt_count * 8 + float_plt_index * 4;
  2807. bh_memcpy_s(float_buf, sizeof(float_buf),
  2808. symbol + strlen(REAL_PLT_PREFIX), 8);
  2809. if (!str2uint32(float_buf, (uint32 *)symbol_addr)) {
  2810. set_error_buf_v(error_buf, error_buf_size,
  2811. "resolve symbol %s failed", symbol);
  2812. goto check_symbol_fail;
  2813. }
  2814. float_plt_index++;
  2815. }
  2816. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  2817. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  2818. set_error_buf_v(error_buf, error_buf_size,
  2819. "resolve symbol %s failed", symbol);
  2820. goto check_symbol_fail;
  2821. }
  2822. if (symbol != symbol_buf)
  2823. wasm_runtime_free(symbol);
  2824. if (!apply_relocation(
  2825. module, aot_text, aot_text_size, relocation->relocation_offset,
  2826. relocation->relocation_addend, relocation->relocation_type,
  2827. symbol_addr, symbol_index, error_buf, error_buf_size))
  2828. return false;
  2829. }
  2830. return true;
  2831. check_symbol_fail:
  2832. if (symbol != symbol_buf)
  2833. wasm_runtime_free(symbol);
  2834. return false;
  2835. }
  2836. static bool
  2837. do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
  2838. char *error_buf, uint32 error_buf_size)
  2839. {
  2840. uint8 *data_addr;
  2841. uint32 data_size = 0, i;
  2842. AOTRelocation *relocation = group->relocations;
  2843. void *symbol_addr;
  2844. char *symbol, *data_section_name;
  2845. if (!strncmp(group->section_name, ".rela.", 6)) {
  2846. data_section_name = group->section_name + strlen(".rela");
  2847. }
  2848. else if (!strncmp(group->section_name, ".rel.", 5)) {
  2849. data_section_name = group->section_name + strlen(".rel");
  2850. }
  2851. else if (!strcmp(group->section_name, ".rdata")) {
  2852. data_section_name = group->section_name;
  2853. }
  2854. #if WASM_ENABLE_STATIC_PGO != 0
  2855. else if (!strncmp(group->section_name, ".rel__llvm_prf_data", 19)) {
  2856. data_section_name = group->section_name + strlen(".rel");
  2857. }
  2858. else if (!strncmp(group->section_name, ".rela__llvm_prf_data", 20)) {
  2859. data_section_name = group->section_name + strlen(".rela");
  2860. }
  2861. #endif
  2862. else {
  2863. set_error_buf(error_buf, error_buf_size,
  2864. "invalid data relocation section name");
  2865. return false;
  2866. }
  2867. data_addr = get_data_section_addr(module, data_section_name, &data_size);
  2868. if (group->relocation_count > 0 && !data_addr) {
  2869. set_error_buf(error_buf, error_buf_size,
  2870. "invalid data relocation count");
  2871. return false;
  2872. }
  2873. for (i = 0; i < group->relocation_count; i++, relocation++) {
  2874. symbol = relocation->symbol_name;
  2875. if (is_text_section(symbol)) {
  2876. symbol_addr = module->code;
  2877. }
  2878. #if WASM_ENABLE_STATIC_PGO != 0
  2879. else if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  2880. char *p = symbol + strlen(AOT_FUNC_PREFIX);
  2881. uint32 func_index;
  2882. if (*p == '\0'
  2883. || (func_index = (uint32)atoi(p)) > module->func_count) {
  2884. set_error_buf_v(error_buf, error_buf_size,
  2885. "invalid relocation symbol %s", symbol);
  2886. return false;
  2887. }
  2888. symbol_addr = module->func_ptrs[func_index];
  2889. }
  2890. else if (!strcmp(symbol, "__llvm_prf_cnts")) {
  2891. uint32 j;
  2892. for (j = 0; j < module->data_section_count; j++) {
  2893. if (!strncmp(module->data_sections[j].name, symbol, 15)) {
  2894. bh_assert(relocation->relocation_addend + sizeof(uint64)
  2895. <= module->data_sections[j].size);
  2896. symbol_addr = module->data_sections[j].data;
  2897. break;
  2898. }
  2899. }
  2900. if (j == module->data_section_count) {
  2901. set_error_buf_v(error_buf, error_buf_size,
  2902. "invalid relocation symbol %s", symbol);
  2903. return false;
  2904. }
  2905. }
  2906. else if (!strncmp(symbol, "__llvm_prf_cnts", 15)) {
  2907. uint32 j;
  2908. for (j = 0; j < module->data_section_count; j++) {
  2909. if (!strcmp(module->data_sections[j].name, symbol)) {
  2910. symbol_addr = module->data_sections[j].data;
  2911. break;
  2912. }
  2913. }
  2914. if (j == module->data_section_count) {
  2915. set_error_buf_v(error_buf, error_buf_size,
  2916. "invalid relocation symbol %s", symbol);
  2917. return false;
  2918. }
  2919. }
  2920. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  2921. else {
  2922. set_error_buf_v(error_buf, error_buf_size,
  2923. "invalid relocation symbol %s", symbol);
  2924. return false;
  2925. }
  2926. if (!apply_relocation(
  2927. module, data_addr, data_size, relocation->relocation_offset,
  2928. relocation->relocation_addend, relocation->relocation_type,
  2929. symbol_addr, -1, error_buf, error_buf_size))
  2930. return false;
  2931. }
  2932. return true;
  2933. }
  2934. static bool
  2935. validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count,
  2936. char *error_buf, uint32 error_buf_size)
  2937. {
  2938. uint32 i, str_len_addr = 0;
  2939. uint16 str_len;
  2940. for (i = 0; i < count; i++) {
  2941. if (offsets[i] != str_len_addr)
  2942. return false;
  2943. read_uint16(buf, buf_end, str_len);
  2944. str_len_addr += (uint32)sizeof(uint16) + str_len;
  2945. str_len_addr = align_uint(str_len_addr, 2);
  2946. buf += str_len;
  2947. buf = (uint8 *)align_ptr(buf, 2);
  2948. }
  2949. if (buf == buf_end)
  2950. return true;
  2951. fail:
  2952. return false;
  2953. }
  2954. static bool
  2955. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  2956. AOTModule *module, bool is_load_from_file_buf,
  2957. char *error_buf, uint32 error_buf_size)
  2958. {
  2959. AOTRelocationGroup *groups = NULL, *group;
  2960. uint32 symbol_count = 0;
  2961. uint32 group_count = 0, i, j, got_item_count = 0;
  2962. uint64 size;
  2963. uint32 *symbol_offsets, total_string_len;
  2964. uint8 *symbol_buf, *symbol_buf_end;
  2965. int map_prot, map_flags;
  2966. bool ret = false;
  2967. char **symbols = NULL;
  2968. read_uint32(buf, buf_end, symbol_count);
  2969. symbol_offsets = (uint32 *)buf;
  2970. for (i = 0; i < symbol_count; i++) {
  2971. CHECK_BUF(buf, buf_end, sizeof(uint32));
  2972. buf += sizeof(uint32);
  2973. }
  2974. read_uint32(buf, buf_end, total_string_len);
  2975. symbol_buf = (uint8 *)buf;
  2976. symbol_buf_end = symbol_buf + total_string_len;
  2977. if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets,
  2978. symbol_count, error_buf, error_buf_size)) {
  2979. set_error_buf(error_buf, error_buf_size,
  2980. "validate symbol table failed");
  2981. goto fail;
  2982. }
  2983. if (symbol_count > 0) {
  2984. symbols = loader_malloc((uint64)sizeof(*symbols) * symbol_count,
  2985. error_buf, error_buf_size);
  2986. if (symbols == NULL) {
  2987. goto fail;
  2988. }
  2989. }
  2990. #if defined(BH_PLATFORM_WINDOWS)
  2991. buf = symbol_buf_end;
  2992. read_uint32(buf, buf_end, group_count);
  2993. for (i = 0; i < group_count; i++) {
  2994. uint32 name_index, relocation_count;
  2995. uint16 group_name_len;
  2996. uint8 *group_name;
  2997. /* section name address is 4 bytes aligned. */
  2998. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  2999. read_uint32(buf, buf_end, name_index);
  3000. if (name_index >= symbol_count) {
  3001. set_error_buf(error_buf, error_buf_size,
  3002. "symbol index out of range");
  3003. goto fail;
  3004. }
  3005. group_name = symbol_buf + symbol_offsets[name_index];
  3006. group_name_len = *(uint16 *)group_name;
  3007. group_name += sizeof(uint16);
  3008. read_uint32(buf, buf_end, relocation_count);
  3009. for (j = 0; j < relocation_count; j++) {
  3010. AOTRelocation relocation = { 0 };
  3011. char group_name_buf[128] = { 0 };
  3012. char symbol_name_buf[128] = { 0 };
  3013. uint32 symbol_index, offset32;
  3014. int32 addend32;
  3015. uint16 symbol_name_len;
  3016. uint8 *symbol_name;
  3017. if (sizeof(void *) == 8) {
  3018. read_uint64(buf, buf_end, relocation.relocation_offset);
  3019. read_uint64(buf, buf_end, relocation.relocation_addend);
  3020. }
  3021. else {
  3022. read_uint32(buf, buf_end, offset32);
  3023. relocation.relocation_offset = (uint64)offset32;
  3024. read_uint32(buf, buf_end, addend32);
  3025. relocation.relocation_addend = (int64)addend32;
  3026. }
  3027. read_uint32(buf, buf_end, relocation.relocation_type);
  3028. read_uint32(buf, buf_end, symbol_index);
  3029. if (symbol_index >= symbol_count) {
  3030. set_error_buf(error_buf, error_buf_size,
  3031. "symbol index out of range");
  3032. goto fail;
  3033. }
  3034. symbol_name = symbol_buf + symbol_offsets[symbol_index];
  3035. symbol_name_len = *(uint16 *)symbol_name;
  3036. symbol_name += sizeof(uint16);
  3037. bh_memcpy_s(group_name_buf, (uint32)sizeof(group_name_buf),
  3038. group_name, group_name_len);
  3039. bh_memcpy_s(symbol_name_buf, (uint32)sizeof(symbol_name_buf),
  3040. symbol_name, symbol_name_len);
  3041. /* aot compiler emits string with '\0' since 2.0.0 */
  3042. if (group_name_len == strlen(".text") + 1
  3043. && !strncmp(group_name, ".text", strlen(".text"))) {
  3044. /* aot compiler emits string with '\0' since 2.0.0 */
  3045. if (symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 + 1
  3046. && !strncmp(symbol_name, YMM_PLT_PREFIX,
  3047. strlen(YMM_PLT_PREFIX))) {
  3048. module->ymm_plt_count++;
  3049. }
  3050. else if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + 1
  3051. && !strncmp(symbol_name, XMM_PLT_PREFIX,
  3052. strlen(XMM_PLT_PREFIX))) {
  3053. module->xmm_plt_count++;
  3054. }
  3055. else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16 + 1
  3056. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  3057. strlen(REAL_PLT_PREFIX))) {
  3058. module->real_plt_count++;
  3059. }
  3060. else if (symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8 + 1
  3061. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  3062. strlen(REAL_PLT_PREFIX))) {
  3063. module->float_plt_count++;
  3064. }
  3065. }
  3066. }
  3067. }
  3068. /* Allocate memory for extra plt data */
  3069. size = sizeof(uint64) * 4 * module->ymm_plt_count
  3070. + sizeof(uint64) * 2 * module->xmm_plt_count
  3071. + sizeof(uint64) * module->real_plt_count
  3072. + sizeof(uint32) * module->float_plt_count;
  3073. if (size > 0) {
  3074. if (size > UINT32_MAX
  3075. || !(module->extra_plt_data = loader_mmap(
  3076. (uint32)size, true, error_buf, error_buf_size))) {
  3077. goto fail;
  3078. }
  3079. module->extra_plt_data_size = (uint32)size;
  3080. }
  3081. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  3082. #if (defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)) \
  3083. && !defined(BH_PLATFORM_WINDOWS)
  3084. buf = symbol_buf_end;
  3085. read_uint32(buf, buf_end, group_count);
  3086. /* Resolve the relocations of type R_X86_64_GOTPCREL */
  3087. for (i = 0; i < group_count; i++) {
  3088. uint32 name_index, relocation_count;
  3089. uint16 group_name_len;
  3090. uint8 *group_name;
  3091. /* section name address is 4 bytes aligned. */
  3092. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  3093. read_uint32(buf, buf_end, name_index);
  3094. if (name_index >= symbol_count) {
  3095. set_error_buf(error_buf, error_buf_size,
  3096. "symbol index out of range");
  3097. goto fail;
  3098. }
  3099. group_name = symbol_buf + symbol_offsets[name_index];
  3100. group_name_len = *(uint16 *)group_name;
  3101. group_name += sizeof(uint16);
  3102. read_uint32(buf, buf_end, relocation_count);
  3103. for (j = 0; j < relocation_count; j++) {
  3104. AOTRelocation relocation = { 0 };
  3105. char group_name_buf[128] = { 0 };
  3106. char symbol_name_buf[128] = { 0 };
  3107. uint32 symbol_index;
  3108. uint16 symbol_name_len;
  3109. uint8 *symbol_name;
  3110. /* relocation offset and addend */
  3111. buf += sizeof(void *) * 2;
  3112. read_uint32(buf, buf_end, relocation.relocation_type);
  3113. read_uint32(buf, buf_end, symbol_index);
  3114. if (symbol_index >= symbol_count) {
  3115. set_error_buf(error_buf, error_buf_size,
  3116. "symbol index out of range");
  3117. goto fail;
  3118. }
  3119. symbol_name = symbol_buf + symbol_offsets[symbol_index];
  3120. symbol_name_len = *(uint16 *)symbol_name;
  3121. symbol_name += sizeof(uint16);
  3122. bh_memcpy_s(group_name_buf, (uint32)sizeof(group_name_buf),
  3123. group_name, group_name_len);
  3124. bh_memcpy_s(symbol_name_buf, (uint32)sizeof(symbol_name_buf),
  3125. symbol_name, symbol_name_len);
  3126. if (relocation.relocation_type == R_X86_64_GOTPCREL
  3127. && !strncmp(symbol_name_buf, AOT_FUNC_PREFIX,
  3128. strlen(AOT_FUNC_PREFIX))) {
  3129. uint32 func_idx =
  3130. atoi(symbol_name_buf + strlen(AOT_FUNC_PREFIX));
  3131. GOTItem *got_item = module->got_item_list;
  3132. if (func_idx >= module->func_count) {
  3133. set_error_buf(error_buf, error_buf_size,
  3134. "func index out of range");
  3135. goto fail;
  3136. }
  3137. while (got_item) {
  3138. if (got_item->func_idx == func_idx)
  3139. break;
  3140. got_item = got_item->next;
  3141. }
  3142. if (!got_item) {
  3143. /* Create the got item and append to the list */
  3144. got_item = wasm_runtime_malloc(sizeof(GOTItem));
  3145. if (!got_item) {
  3146. set_error_buf(error_buf, error_buf_size,
  3147. "allocate memory failed");
  3148. goto fail;
  3149. }
  3150. got_item->func_idx = func_idx;
  3151. got_item->next = NULL;
  3152. if (!module->got_item_list) {
  3153. module->got_item_list = module->got_item_list_end =
  3154. got_item;
  3155. }
  3156. else {
  3157. module->got_item_list_end->next = got_item;
  3158. module->got_item_list_end = got_item;
  3159. }
  3160. got_item_count++;
  3161. }
  3162. }
  3163. }
  3164. }
  3165. if (got_item_count) {
  3166. GOTItem *got_item = module->got_item_list;
  3167. uint32 got_item_idx = 0;
  3168. /* Create the GOT for func_ptrs, note that it is different from
  3169. the .got section of a dynamic object file */
  3170. size = (uint64)sizeof(void *) * got_item_count;
  3171. if (size > UINT32_MAX
  3172. || !(module->got_func_ptrs = loader_mmap(
  3173. (uint32)size, false, error_buf, error_buf_size))) {
  3174. goto fail;
  3175. }
  3176. while (got_item) {
  3177. module->got_func_ptrs[got_item_idx++] =
  3178. module->func_ptrs[got_item->func_idx];
  3179. got_item = got_item->next;
  3180. }
  3181. module->got_item_count = got_item_count;
  3182. }
  3183. #else
  3184. (void)got_item_count;
  3185. #endif /* (defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)) && \
  3186. !defined(BH_PLATFORM_WINDOWS) */
  3187. buf = symbol_buf_end;
  3188. read_uint32(buf, buf_end, group_count);
  3189. /* Allocate memory for relocation groups */
  3190. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  3191. if (size > 0
  3192. && !(groups = loader_malloc(size, error_buf, error_buf_size))) {
  3193. goto fail;
  3194. }
  3195. /* Load each relocation group */
  3196. for (i = 0, group = groups; i < group_count; i++, group++) {
  3197. AOTRelocation *relocation;
  3198. uint32 name_index;
  3199. /* section name address is 4 bytes aligned. */
  3200. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  3201. read_uint32(buf, buf_end, name_index);
  3202. if (name_index >= symbol_count) {
  3203. set_error_buf(error_buf, error_buf_size,
  3204. "symbol index out of range");
  3205. goto fail;
  3206. }
  3207. if (symbols[name_index] == NULL) {
  3208. uint8 *name_addr = symbol_buf + symbol_offsets[name_index];
  3209. read_string(name_addr, buf_end, symbols[name_index]);
  3210. }
  3211. group->section_name = symbols[name_index];
  3212. read_uint32(buf, buf_end, group->relocation_count);
  3213. /* Allocate memory for relocations */
  3214. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  3215. if (!(group->relocations = relocation =
  3216. loader_malloc(size, error_buf, error_buf_size))) {
  3217. ret = false;
  3218. goto fail;
  3219. }
  3220. /* Load each relocation */
  3221. for (j = 0; j < group->relocation_count; j++, relocation++) {
  3222. uint32 symbol_index;
  3223. if (sizeof(void *) == 8) {
  3224. read_uint64(buf, buf_end, relocation->relocation_offset);
  3225. read_uint64(buf, buf_end, relocation->relocation_addend);
  3226. }
  3227. else {
  3228. uint32 offset32, addend32;
  3229. read_uint32(buf, buf_end, offset32);
  3230. relocation->relocation_offset = (uint64)offset32;
  3231. read_uint32(buf, buf_end, addend32);
  3232. relocation->relocation_addend = (uint64)addend32;
  3233. }
  3234. read_uint32(buf, buf_end, relocation->relocation_type);
  3235. read_uint32(buf, buf_end, symbol_index);
  3236. if (symbol_index >= symbol_count) {
  3237. set_error_buf(error_buf, error_buf_size,
  3238. "symbol index out of range");
  3239. goto fail;
  3240. }
  3241. if (symbols[symbol_index] == NULL) {
  3242. uint8 *symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  3243. read_string(symbol_addr, buf_end, symbols[symbol_index]);
  3244. }
  3245. relocation->symbol_name = symbols[symbol_index];
  3246. }
  3247. if (!strcmp(group->section_name, ".rel.text")
  3248. || !strcmp(group->section_name, ".rela.text")
  3249. || !strcmp(group->section_name, ".rel.ltext")
  3250. || !strcmp(group->section_name, ".rela.ltext")
  3251. || !strcmp(group->section_name, ".rela.literal")
  3252. #ifdef BH_PLATFORM_WINDOWS
  3253. || !strcmp(group->section_name, ".text")
  3254. #endif
  3255. ) {
  3256. #if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \
  3257. && !defined(BH_PLATFORM_DARWIN) && !defined(BH_PLATFORM_WINDOWS)
  3258. if (module->is_indirect_mode) {
  3259. set_error_buf(error_buf, error_buf_size,
  3260. "cannot apply relocation to text section "
  3261. "for aot file generated with "
  3262. "\"--enable-indirect-mode\" flag");
  3263. goto fail;
  3264. }
  3265. #endif
  3266. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  3267. goto fail;
  3268. }
  3269. else {
  3270. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  3271. goto fail;
  3272. }
  3273. }
  3274. /* Set read only for AOT code and some data sections */
  3275. map_prot = MMAP_PROT_READ | MMAP_PROT_EXEC;
  3276. if (module->code) {
  3277. /* The layout is: literal size + literal + code (with plt table) */
  3278. uint8 *mmap_addr = module->literal - sizeof(uint32);
  3279. uint32 total_size =
  3280. sizeof(uint32) + module->literal_size + module->code_size;
  3281. os_mprotect(mmap_addr, total_size, map_prot);
  3282. }
  3283. map_prot = MMAP_PROT_READ;
  3284. #if defined(BH_PLATFORM_WINDOWS)
  3285. if (module->extra_plt_data) {
  3286. os_mprotect(module->extra_plt_data, module->extra_plt_data_size,
  3287. map_prot);
  3288. }
  3289. #endif
  3290. for (i = 0; i < module->data_section_count; i++) {
  3291. AOTObjectDataSection *data_section = module->data_sections + i;
  3292. if (!strcmp(data_section->name, ".rdata")
  3293. || !strcmp(data_section->name, ".rodata")
  3294. /* ".rodata.cst4/8/16/.." */
  3295. || !strncmp(data_section->name, ".rodata.cst",
  3296. strlen(".rodata.cst"))
  3297. /* ".rodata.strn.m" */
  3298. || !strncmp(data_section->name, ".rodata.str",
  3299. strlen(".rodata.str"))) {
  3300. os_mprotect(data_section->data, data_section->size, map_prot);
  3301. }
  3302. }
  3303. ret = true;
  3304. fail:
  3305. if (symbols) {
  3306. wasm_runtime_free(symbols);
  3307. }
  3308. if (groups) {
  3309. for (i = 0, group = groups; i < group_count; i++, group++)
  3310. if (group->relocations)
  3311. wasm_runtime_free(group->relocations);
  3312. wasm_runtime_free(groups);
  3313. }
  3314. (void)map_flags;
  3315. return ret;
  3316. }
  3317. #if WASM_ENABLE_MEMORY64 != 0
  3318. static bool
  3319. has_module_memory64(AOTModule *module)
  3320. {
  3321. /* TODO: multi-memories for now assuming the memory idx type is consistent
  3322. * across multi-memories */
  3323. if (module->import_memory_count > 0)
  3324. return !!(module->import_memories[0].mem_type.flags & MEMORY64_FLAG);
  3325. else if (module->memory_count > 0)
  3326. return !!(module->memories[0].flags & MEMORY64_FLAG);
  3327. return false;
  3328. }
  3329. #endif
  3330. static bool
  3331. load_from_sections(AOTModule *module, AOTSection *sections,
  3332. bool is_load_from_file_buf, bool no_resolve, char *error_buf,
  3333. uint32 error_buf_size)
  3334. {
  3335. AOTSection *section = sections;
  3336. const uint8 *buf, *buf_end;
  3337. uint32 last_section_type = (uint32)-1, section_type;
  3338. uint32 i, func_index, func_type_index;
  3339. AOTFuncType *func_type;
  3340. AOTExport *exports;
  3341. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  3342. while (section) {
  3343. buf = section->section_body;
  3344. buf_end = buf + section->section_body_size;
  3345. /* Check sections */
  3346. section_type = (uint32)section->section_type;
  3347. if ((last_section_type == (uint32)-1
  3348. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  3349. || (last_section_type != (uint32)-1
  3350. && (section_type != last_section_type + 1
  3351. && section_type != AOT_SECTION_TYPE_CUSTOM))) {
  3352. set_error_buf(error_buf, error_buf_size, "invalid section order");
  3353. return false;
  3354. }
  3355. last_section_type = section_type;
  3356. switch (section_type) {
  3357. case AOT_SECTION_TYPE_TARGET_INFO:
  3358. if (!load_target_info_section(buf, buf_end, module, error_buf,
  3359. error_buf_size))
  3360. return false;
  3361. break;
  3362. case AOT_SECTION_TYPE_INIT_DATA:
  3363. if (!load_init_data_section(buf, buf_end, module,
  3364. is_load_from_file_buf, no_resolve,
  3365. error_buf, error_buf_size))
  3366. return false;
  3367. break;
  3368. case AOT_SECTION_TYPE_TEXT:
  3369. #if !defined(BH_PLATFORM_NUTTX) && !defined(BH_PLATFORM_ESP_IDF)
  3370. /* try to merge .data and .text, with exceptions:
  3371. * 1. XIP mode
  3372. * 2. pre-mmapped module load from aot_load_from_sections()
  3373. * 3. nuttx & esp-idf: have separate region for MMAP_PROT_EXEC
  3374. */
  3375. if (!module->is_indirect_mode && is_load_from_file_buf)
  3376. if (!try_merge_data_and_text(&buf, &buf_end, module,
  3377. error_buf, error_buf_size))
  3378. LOG_WARNING("merge .data and .text sections failed");
  3379. #endif /* ! defined(BH_PLATFORM_NUTTX) && !defined(BH_PLATFORM_ESP_IDF) */
  3380. if (!load_text_section(buf, buf_end, module, error_buf,
  3381. error_buf_size))
  3382. return false;
  3383. break;
  3384. case AOT_SECTION_TYPE_FUNCTION:
  3385. if (!load_function_section(buf, buf_end, module, error_buf,
  3386. error_buf_size))
  3387. return false;
  3388. break;
  3389. case AOT_SECTION_TYPE_EXPORT:
  3390. if (!load_export_section(buf, buf_end, module,
  3391. is_load_from_file_buf, error_buf,
  3392. error_buf_size))
  3393. return false;
  3394. break;
  3395. case AOT_SECTION_TYPE_RELOCATION:
  3396. if (!load_relocation_section(buf, buf_end, module,
  3397. is_load_from_file_buf, error_buf,
  3398. error_buf_size))
  3399. return false;
  3400. break;
  3401. case AOT_SECTION_TYPE_CUSTOM:
  3402. if (!load_custom_section(buf, buf_end, module,
  3403. is_load_from_file_buf, error_buf,
  3404. error_buf_size))
  3405. return false;
  3406. break;
  3407. default:
  3408. set_error_buf(error_buf, error_buf_size,
  3409. "invalid aot section type");
  3410. return false;
  3411. }
  3412. section = section->next;
  3413. }
  3414. if (last_section_type != AOT_SECTION_TYPE_RELOCATION
  3415. && last_section_type != AOT_SECTION_TYPE_CUSTOM) {
  3416. set_error_buf(error_buf, error_buf_size, "section missing");
  3417. return false;
  3418. }
  3419. /* Resolve malloc and free function */
  3420. module->malloc_func_index = (uint32)-1;
  3421. module->free_func_index = (uint32)-1;
  3422. module->retain_func_index = (uint32)-1;
  3423. #if WASM_ENABLE_MEMORY64 != 0
  3424. if (has_module_memory64(module))
  3425. malloc_free_io_type = VALUE_TYPE_I64;
  3426. #endif
  3427. exports = module->exports;
  3428. for (i = 0; i < module->export_count; i++) {
  3429. if (exports[i].kind == EXPORT_KIND_FUNC
  3430. && exports[i].index >= module->import_func_count) {
  3431. if (!strcmp(exports[i].name, "malloc")) {
  3432. func_index = exports[i].index - module->import_func_count;
  3433. func_type_index = module->func_type_indexes[func_index];
  3434. func_type = (AOTFuncType *)module->types[func_type_index];
  3435. if (func_type->param_count == 1 && func_type->result_count == 1
  3436. && func_type->types[0] == malloc_free_io_type
  3437. && func_type->types[1] == malloc_free_io_type) {
  3438. bh_assert(module->malloc_func_index == (uint32)-1);
  3439. module->malloc_func_index = func_index;
  3440. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3441. exports[i].name, exports[i].index);
  3442. }
  3443. }
  3444. else if (!strcmp(exports[i].name, "__new")) {
  3445. func_index = exports[i].index - module->import_func_count;
  3446. func_type_index = module->func_type_indexes[func_index];
  3447. func_type = (AOTFuncType *)module->types[func_type_index];
  3448. if (func_type->param_count == 2 && func_type->result_count == 1
  3449. && func_type->types[0] == malloc_free_io_type
  3450. && func_type->types[1] == VALUE_TYPE_I32
  3451. && func_type->types[2] == malloc_free_io_type) {
  3452. uint32 j;
  3453. WASMExport *export_tmp;
  3454. bh_assert(module->malloc_func_index == (uint32)-1);
  3455. module->malloc_func_index = func_index;
  3456. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3457. exports[i].name, exports[i].index);
  3458. /* resolve retain function.
  3459. If not find, reset malloc function index */
  3460. export_tmp = module->exports;
  3461. for (j = 0; j < module->export_count; j++, export_tmp++) {
  3462. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  3463. && (!strcmp(export_tmp->name, "__retain")
  3464. || !strcmp(export_tmp->name, "__pin"))) {
  3465. func_index =
  3466. export_tmp->index - module->import_func_count;
  3467. func_type_index =
  3468. module->func_type_indexes[func_index];
  3469. func_type =
  3470. (AOTFuncType *)module->types[func_type_index];
  3471. if (func_type->param_count == 1
  3472. && func_type->result_count == 1
  3473. && func_type->types[0] == malloc_free_io_type
  3474. && func_type->types[1] == malloc_free_io_type) {
  3475. bh_assert(module->retain_func_index
  3476. == (uint32)-1);
  3477. module->retain_func_index = export_tmp->index;
  3478. LOG_VERBOSE("Found retain function, name: %s, "
  3479. "index: %u",
  3480. export_tmp->name,
  3481. export_tmp->index);
  3482. break;
  3483. }
  3484. }
  3485. }
  3486. if (j == module->export_count) {
  3487. module->malloc_func_index = (uint32)-1;
  3488. LOG_VERBOSE("Can't find retain function,"
  3489. "reset malloc function index to -1");
  3490. }
  3491. }
  3492. }
  3493. else if ((!strcmp(exports[i].name, "free"))
  3494. || (!strcmp(exports[i].name, "__release"))
  3495. || (!strcmp(exports[i].name, "__unpin"))) {
  3496. func_index = exports[i].index - module->import_func_count;
  3497. func_type_index = module->func_type_indexes[func_index];
  3498. func_type = (AOTFuncType *)module->types[func_type_index];
  3499. if (func_type->param_count == 1 && func_type->result_count == 0
  3500. && func_type->types[0] == malloc_free_io_type) {
  3501. bh_assert(module->free_func_index == (uint32)-1);
  3502. module->free_func_index = func_index;
  3503. LOG_VERBOSE("Found free function, name: %s, index: %u",
  3504. exports[i].name, exports[i].index);
  3505. }
  3506. }
  3507. }
  3508. }
  3509. /* Flush data cache before executing AOT code,
  3510. * otherwise unpredictable behavior can occur. */
  3511. os_dcache_flush();
  3512. #if WASM_ENABLE_MEMORY_TRACING != 0
  3513. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  3514. #endif
  3515. #if WASM_ENABLE_DEBUG_AOT != 0
  3516. if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) {
  3517. set_error_buf(error_buf, error_buf_size,
  3518. "create jit code entry failed");
  3519. return false;
  3520. }
  3521. #endif
  3522. return true;
  3523. }
  3524. static AOTModule *
  3525. create_module(char *name, char *error_buf, uint32 error_buf_size)
  3526. {
  3527. AOTModule *module =
  3528. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
  3529. bh_list_status ret;
  3530. if (!module) {
  3531. return NULL;
  3532. }
  3533. module->module_type = Wasm_Module_AoT;
  3534. module->name = name;
  3535. module->is_binary_freeable = false;
  3536. #if WASM_ENABLE_MULTI_MODULE != 0
  3537. module->import_module_list = &module->import_module_list_head;
  3538. ret = bh_list_init(module->import_module_list);
  3539. bh_assert(ret == BH_LIST_SUCCESS);
  3540. #endif
  3541. (void)ret;
  3542. #if WASM_ENABLE_GC != 0
  3543. if (!(module->ref_type_set =
  3544. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  3545. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  3546. goto fail1;
  3547. }
  3548. if (os_mutex_init(&module->rtt_type_lock)) {
  3549. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  3550. goto fail2;
  3551. }
  3552. #endif
  3553. return module;
  3554. #if WASM_ENABLE_GC != 0
  3555. fail2:
  3556. bh_hash_map_destroy(module->ref_type_set);
  3557. fail1:
  3558. #endif
  3559. wasm_runtime_free(module);
  3560. return NULL;
  3561. }
  3562. AOTModule *
  3563. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  3564. uint32 error_buf_size)
  3565. {
  3566. AOTModule *module = create_module("", error_buf, error_buf_size);
  3567. if (!module)
  3568. return NULL;
  3569. if (!load_from_sections(module, section_list, false, false, error_buf,
  3570. error_buf_size)) {
  3571. aot_unload(module);
  3572. return NULL;
  3573. }
  3574. LOG_VERBOSE("Load module from sections success.\n");
  3575. return module;
  3576. }
  3577. static void
  3578. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  3579. {
  3580. AOTSection *section = section_list, *next;
  3581. while (section) {
  3582. next = section->next;
  3583. if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT
  3584. && section->section_body)
  3585. os_munmap((uint8 *)section->section_body,
  3586. section->section_body_size);
  3587. wasm_runtime_free(section);
  3588. section = next;
  3589. }
  3590. }
  3591. static bool
  3592. resolve_execute_mode(const uint8 *buf, uint32 size, bool *p_mode,
  3593. char *error_buf, uint32 error_buf_size)
  3594. {
  3595. const uint8 *p = buf, *p_end = buf + size;
  3596. uint32 section_type;
  3597. uint32 section_size = 0;
  3598. uint16 e_type = 0;
  3599. p += 8;
  3600. while (p < p_end) {
  3601. read_uint32(p, p_end, section_type);
  3602. if (section_type <= AOT_SECTION_TYPE_SIGNATURE) {
  3603. read_uint32(p, p_end, section_size);
  3604. CHECK_BUF(p, p_end, section_size);
  3605. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  3606. p += 4;
  3607. read_uint16(p, p_end, e_type);
  3608. if (e_type == E_TYPE_XIP) {
  3609. *p_mode = true;
  3610. }
  3611. else {
  3612. *p_mode = false;
  3613. }
  3614. break;
  3615. }
  3616. }
  3617. else { /* section_type > AOT_SECTION_TYPE_SIGNATURE */
  3618. set_error_buf(error_buf, error_buf_size,
  3619. "resolve execute mode failed");
  3620. break;
  3621. }
  3622. p += section_size;
  3623. }
  3624. return true;
  3625. fail:
  3626. return false;
  3627. }
  3628. static bool
  3629. create_sections(AOTModule *module, const uint8 *buf, uint32 size,
  3630. AOTSection **p_section_list, char *error_buf,
  3631. uint32 error_buf_size)
  3632. {
  3633. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  3634. const uint8 *p = buf, *p_end = buf + size;
  3635. bool destroy_aot_text = false;
  3636. bool is_indirect_mode = false;
  3637. uint32 section_type;
  3638. uint32 section_size;
  3639. uint64 total_size;
  3640. uint8 *aot_text;
  3641. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  3642. uint8 *mirrored_text;
  3643. #endif
  3644. if (!resolve_execute_mode(buf, size, &is_indirect_mode, error_buf,
  3645. error_buf_size)) {
  3646. goto fail;
  3647. }
  3648. module->is_indirect_mode = is_indirect_mode;
  3649. p += 8;
  3650. while (p < p_end) {
  3651. read_uint32(p, p_end, section_type);
  3652. if (section_type < AOT_SECTION_TYPE_SIGNATURE
  3653. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  3654. read_uint32(p, p_end, section_size);
  3655. CHECK_BUF(p, p_end, section_size);
  3656. if (!(section = loader_malloc(sizeof(AOTSection), error_buf,
  3657. error_buf_size))) {
  3658. goto fail;
  3659. }
  3660. memset(section, 0, sizeof(AOTSection));
  3661. section->section_type = (int32)section_type;
  3662. section->section_body = (uint8 *)p;
  3663. section->section_body_size = section_size;
  3664. if (section_type == AOT_SECTION_TYPE_TEXT) {
  3665. if ((section_size > 0) && !module->is_indirect_mode) {
  3666. total_size =
  3667. (uint64)section_size + aot_get_plt_table_size();
  3668. total_size = (total_size + 3) & ~((uint64)3);
  3669. if (total_size >= UINT32_MAX
  3670. || !(aot_text =
  3671. loader_mmap((uint32)total_size, true,
  3672. error_buf, error_buf_size))) {
  3673. wasm_runtime_free(section);
  3674. goto fail;
  3675. }
  3676. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  3677. mirrored_text = os_get_dbus_mirror(aot_text);
  3678. bh_assert(mirrored_text != NULL);
  3679. bh_memcpy_s(mirrored_text, (uint32)total_size,
  3680. section->section_body, (uint32)section_size);
  3681. os_dcache_flush();
  3682. #else
  3683. bh_memcpy_s(aot_text, (uint32)total_size,
  3684. section->section_body, (uint32)section_size);
  3685. #endif
  3686. section->section_body = aot_text;
  3687. destroy_aot_text = true;
  3688. if ((uint32)total_size > section->section_body_size) {
  3689. memset(aot_text + (uint32)section_size, 0,
  3690. (uint32)total_size - section_size);
  3691. section->section_body_size = (uint32)total_size;
  3692. }
  3693. }
  3694. }
  3695. if (!section_list)
  3696. section_list = section_list_end = section;
  3697. else {
  3698. section_list_end->next = section;
  3699. section_list_end = section;
  3700. }
  3701. p += section_size;
  3702. }
  3703. else {
  3704. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3705. goto fail;
  3706. }
  3707. }
  3708. if (!section_list) {
  3709. set_error_buf(error_buf, error_buf_size, "create section list failed");
  3710. return false;
  3711. }
  3712. *p_section_list = section_list;
  3713. return true;
  3714. fail:
  3715. if (section_list)
  3716. destroy_sections(section_list, destroy_aot_text);
  3717. return false;
  3718. }
  3719. static bool
  3720. load(const uint8 *buf, uint32 size, AOTModule *module,
  3721. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  3722. uint32 error_buf_size)
  3723. {
  3724. const uint8 *buf_end = buf + size;
  3725. const uint8 *p = buf, *p_end = buf_end;
  3726. uint32 magic_number, version;
  3727. AOTSection *section_list = NULL;
  3728. bool ret;
  3729. read_uint32(p, p_end, magic_number);
  3730. if (magic_number != AOT_MAGIC_NUMBER) {
  3731. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  3732. return false;
  3733. }
  3734. read_uint32(p, p_end, version);
  3735. if (version != AOT_CURRENT_VERSION) {
  3736. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3737. return false;
  3738. }
  3739. module->package_version = version;
  3740. if (!create_sections(module, buf, size, &section_list, error_buf,
  3741. error_buf_size))
  3742. return false;
  3743. ret = load_from_sections(module, section_list, !wasm_binary_freeable,
  3744. no_resolve, error_buf, error_buf_size);
  3745. if (!ret) {
  3746. /* If load_from_sections() fails, then aot text is destroyed
  3747. in destroy_sections() */
  3748. destroy_sections(section_list,
  3749. module->is_indirect_mode
  3750. || module->merged_data_text_sections
  3751. ? false
  3752. : true);
  3753. /* aot_unload() won't destroy aot text again */
  3754. module->code = NULL;
  3755. }
  3756. else {
  3757. /* If load_from_sections() succeeds, then aot text is set to
  3758. module->code and will be destroyed in aot_unload() */
  3759. destroy_sections(section_list, false);
  3760. }
  3761. #if 0
  3762. {
  3763. uint32 i;
  3764. for (i = 0; i < module->func_count; i++) {
  3765. LOG_VERBOSE("AOT func %u, addr: %p\n", i, module->func_ptrs[i]);
  3766. }
  3767. }
  3768. #endif
  3769. #if WASM_ENABLE_LINUX_PERF != 0
  3770. if (wasm_runtime_get_linux_perf())
  3771. if (!aot_create_perf_map(module, error_buf, error_buf_size))
  3772. goto fail;
  3773. #endif
  3774. return ret;
  3775. fail:
  3776. return false;
  3777. }
  3778. AOTModule *
  3779. aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args,
  3780. char *error_buf, uint32 error_buf_size)
  3781. {
  3782. AOTModule *module = create_module(args->name, error_buf, error_buf_size);
  3783. if (!module)
  3784. return NULL;
  3785. os_thread_jit_write_protect_np(false); /* Make memory writable */
  3786. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  3787. error_buf, error_buf_size)) {
  3788. aot_unload(module);
  3789. return NULL;
  3790. }
  3791. os_thread_jit_write_protect_np(true); /* Make memory executable */
  3792. os_icache_flush(module->code, module->code_size);
  3793. LOG_VERBOSE("Load module success.\n");
  3794. return module;
  3795. }
  3796. void
  3797. aot_unload(AOTModule *module)
  3798. {
  3799. if (module->import_memories)
  3800. destroy_import_memories(module->import_memories);
  3801. if (module->memories)
  3802. wasm_runtime_free(module->memories);
  3803. if (module->mem_init_data_list)
  3804. destroy_mem_init_data_list(module->mem_init_data_list,
  3805. module->mem_init_data_count);
  3806. if (module->native_symbol_list)
  3807. wasm_runtime_free(module->native_symbol_list);
  3808. if (module->import_tables)
  3809. destroy_import_tables(module->import_tables);
  3810. if (module->tables) {
  3811. #if WASM_ENABLE_GC != 0
  3812. uint32 i;
  3813. for (i = 0; i < module->table_count; i++) {
  3814. destroy_init_expr(&module->tables[i].init_expr);
  3815. }
  3816. #endif
  3817. destroy_tables(module->tables);
  3818. }
  3819. if (module->table_init_data_list)
  3820. destroy_table_init_data_list(module->table_init_data_list,
  3821. module->table_init_data_count);
  3822. if (module->types)
  3823. destroy_types(module->types, module->type_count);
  3824. if (module->import_globals)
  3825. destroy_import_globals(module->import_globals);
  3826. if (module->globals) {
  3827. #if WASM_ENABLE_GC != 0
  3828. uint32 i;
  3829. for (i = 0; i < module->global_count; i++) {
  3830. destroy_init_expr(&module->globals[i].init_expr);
  3831. }
  3832. #endif
  3833. destroy_globals(module->globals);
  3834. }
  3835. if (module->import_funcs)
  3836. destroy_import_funcs(module->import_funcs);
  3837. if (module->exports)
  3838. destroy_exports(module->exports);
  3839. if (module->func_type_indexes)
  3840. wasm_runtime_free(module->func_type_indexes);
  3841. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  3842. if (module->max_local_cell_nums)
  3843. wasm_runtime_free(module->max_local_cell_nums);
  3844. if (module->max_stack_cell_nums)
  3845. wasm_runtime_free(module->max_stack_cell_nums);
  3846. #endif
  3847. #if WASM_ENABLE_GC != 0
  3848. if (module->func_local_ref_flags) {
  3849. uint32 i;
  3850. for (i = 0; i < module->import_func_count + module->func_count; i++) {
  3851. if (module->func_local_ref_flags[i].local_ref_flags) {
  3852. wasm_runtime_free(
  3853. module->func_local_ref_flags[i].local_ref_flags);
  3854. }
  3855. }
  3856. wasm_runtime_free(module->func_local_ref_flags);
  3857. }
  3858. #endif
  3859. if (module->func_ptrs)
  3860. wasm_runtime_free(module->func_ptrs);
  3861. if (module->const_str_set)
  3862. bh_hash_map_destroy(module->const_str_set);
  3863. #if WASM_ENABLE_MULTI_MODULE != 0
  3864. /* just release the sub module list */
  3865. if (module->import_module_list) {
  3866. WASMRegisteredModule *node =
  3867. bh_list_first_elem(module->import_module_list);
  3868. while (node) {
  3869. WASMRegisteredModule *next = bh_list_elem_next(node);
  3870. bh_list_remove(module->import_module_list, node);
  3871. wasm_runtime_free(node);
  3872. node = next;
  3873. }
  3874. }
  3875. #endif
  3876. if (module->code && !module->is_indirect_mode
  3877. && !module->merged_data_text_sections) {
  3878. /* The layout is: literal size + literal + code (with plt table) */
  3879. uint8 *mmap_addr = module->literal - sizeof(uint32);
  3880. uint32 total_size =
  3881. sizeof(uint32) + module->literal_size + module->code_size;
  3882. os_munmap(mmap_addr, total_size);
  3883. }
  3884. #if defined(BH_PLATFORM_WINDOWS)
  3885. if (module->extra_plt_data) {
  3886. os_munmap(module->extra_plt_data, module->extra_plt_data_size);
  3887. }
  3888. #endif
  3889. #if (defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)) \
  3890. && !defined(BH_PLATFORM_WINDOWS)
  3891. {
  3892. GOTItem *got_item = module->got_item_list, *got_item_next;
  3893. if (module->got_func_ptrs) {
  3894. os_munmap(module->got_func_ptrs,
  3895. sizeof(void *) * module->got_item_count);
  3896. }
  3897. while (got_item) {
  3898. got_item_next = got_item->next;
  3899. wasm_runtime_free(got_item);
  3900. got_item = got_item_next;
  3901. }
  3902. }
  3903. #endif
  3904. if (module->data_sections)
  3905. destroy_object_data_sections(module->data_sections,
  3906. module->data_section_count);
  3907. if (module->merged_data_sections)
  3908. os_munmap(module->merged_data_sections,
  3909. module->merged_data_sections_size);
  3910. if (module->merged_data_text_sections)
  3911. os_munmap(module->merged_data_text_sections,
  3912. module->merged_data_text_sections_size);
  3913. #if WASM_ENABLE_DEBUG_AOT != 0
  3914. jit_code_entry_destroy(module->elf_hdr);
  3915. #endif
  3916. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3917. if (module->aux_func_indexes) {
  3918. wasm_runtime_free(module->aux_func_indexes);
  3919. }
  3920. if (module->aux_func_names) {
  3921. wasm_runtime_free((void *)module->aux_func_names);
  3922. }
  3923. #endif
  3924. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3925. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  3926. #endif
  3927. #if WASM_ENABLE_GC != 0
  3928. if (module->ref_type_set) {
  3929. bh_hash_map_destroy(module->ref_type_set);
  3930. }
  3931. os_mutex_destroy(&module->rtt_type_lock);
  3932. if (module->rtt_types) {
  3933. uint32 i;
  3934. for (i = 0; i < module->type_count; i++) {
  3935. if (module->rtt_types[i])
  3936. wasm_runtime_free(module->rtt_types[i]);
  3937. }
  3938. wasm_runtime_free(module->rtt_types);
  3939. }
  3940. #if WASM_ENABLE_STRINGREF != 0
  3941. {
  3942. uint32 i;
  3943. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1;
  3944. i++) {
  3945. if (module->stringref_rtts[i])
  3946. wasm_runtime_free(module->stringref_rtts[i]);
  3947. }
  3948. if (module->string_literal_lengths) {
  3949. wasm_runtime_free(module->string_literal_lengths);
  3950. }
  3951. if (module->string_literal_ptrs) {
  3952. wasm_runtime_free((void *)module->string_literal_ptrs);
  3953. }
  3954. }
  3955. #endif
  3956. #endif
  3957. wasm_runtime_free(module);
  3958. }
  3959. uint32
  3960. aot_get_plt_table_size()
  3961. {
  3962. return get_plt_table_size();
  3963. }
  3964. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3965. const uint8 *
  3966. aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len)
  3967. {
  3968. WASMCustomSection *section = module->custom_section_list;
  3969. while (section) {
  3970. if (strcmp(section->name_addr, name) == 0) {
  3971. if (len) {
  3972. *len = section->content_len;
  3973. }
  3974. return section->content_addr;
  3975. }
  3976. section = section->next;
  3977. }
  3978. return NULL;
  3979. }
  3980. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  3981. #if WASM_ENABLE_STATIC_PGO != 0
  3982. void
  3983. aot_exchange_uint16(uint8 *p_data)
  3984. {
  3985. return exchange_uint16(p_data);
  3986. }
  3987. void
  3988. aot_exchange_uint32(uint8 *p_data)
  3989. {
  3990. return exchange_uint32(p_data);
  3991. }
  3992. void
  3993. aot_exchange_uint64(uint8 *p_data)
  3994. {
  3995. return exchange_uint64(p_data);
  3996. }
  3997. #endif