aot_loader.c 160 KB

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