aot_loader.c 152 KB

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