aot_loader.c 149 KB

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