aot_loader.c 152 KB

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