aot_loader.c 158 KB

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