aot_runtime.c 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113
  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 "../compilation/aot_stack_frame.h"
  7. #include "bh_log.h"
  8. #include "mem_alloc.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_memory.h"
  11. #include "../interpreter/wasm_runtime.h"
  12. #if WASM_ENABLE_SHARED_MEMORY != 0
  13. #include "../common/wasm_shared_memory.h"
  14. #endif
  15. #if WASM_ENABLE_THREAD_MGR != 0
  16. #include "../libraries/thread-mgr/thread_manager.h"
  17. #endif
  18. /*
  19. * Note: These offsets need to match the values hardcoded in
  20. * AoT compilation code: aot_create_func_context, check_suspend_flags.
  21. */
  22. bh_static_assert(offsetof(WASMExecEnv, cur_frame) == 1 * sizeof(uintptr_t));
  23. bh_static_assert(offsetof(WASMExecEnv, module_inst) == 2 * sizeof(uintptr_t));
  24. bh_static_assert(offsetof(WASMExecEnv, argv_buf) == 3 * sizeof(uintptr_t));
  25. bh_static_assert(offsetof(WASMExecEnv, native_stack_boundary)
  26. == 4 * sizeof(uintptr_t));
  27. bh_static_assert(offsetof(WASMExecEnv, suspend_flags) == 5 * sizeof(uintptr_t));
  28. bh_static_assert(offsetof(WASMExecEnv, aux_stack_boundary)
  29. == 6 * sizeof(uintptr_t));
  30. bh_static_assert(offsetof(WASMExecEnv, aux_stack_bottom)
  31. == 7 * sizeof(uintptr_t));
  32. bh_static_assert(offsetof(WASMExecEnv, native_symbol) == 8 * sizeof(uintptr_t));
  33. bh_static_assert(offsetof(WASMExecEnv, native_stack_top_min)
  34. == 9 * sizeof(uintptr_t));
  35. bh_static_assert(offsetof(WASMExecEnv, wasm_stack.top_boundary)
  36. == 10 * sizeof(uintptr_t));
  37. bh_static_assert(offsetof(WASMExecEnv, wasm_stack.top)
  38. == 11 * sizeof(uintptr_t));
  39. bh_static_assert(offsetof(WASMExecEnv, wasm_stack.bottom)
  40. == 12 * sizeof(uintptr_t));
  41. bh_static_assert(offsetof(AOTModuleInstance, memories) == 1 * sizeof(uint64));
  42. bh_static_assert(offsetof(AOTModuleInstance, func_ptrs) == 5 * sizeof(uint64));
  43. bh_static_assert(offsetof(AOTModuleInstance, func_type_indexes)
  44. == 6 * sizeof(uint64));
  45. bh_static_assert(offsetof(AOTModuleInstance, cur_exception)
  46. == 13 * sizeof(uint64));
  47. bh_static_assert(offsetof(AOTModuleInstance, c_api_func_imports)
  48. == 13 * sizeof(uint64) + 128 + 7 * sizeof(uint64));
  49. bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
  50. == 13 * sizeof(uint64) + 128 + 14 * sizeof(uint64));
  51. bh_static_assert(sizeof(AOTMemoryInstance) == 120);
  52. bh_static_assert(offsetof(AOTTableInstance, elems) == 24);
  53. bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
  54. bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);
  55. bh_static_assert(sizeof(wasm_val_t) == 16);
  56. bh_static_assert(offsetof(wasm_val_t, of) == 8);
  57. bh_static_assert(offsetof(AOTFrame, prev_frame) == sizeof(uintptr_t) * 0);
  58. bh_static_assert(offsetof(AOTFrame, func_index) == sizeof(uintptr_t) * 1);
  59. bh_static_assert(offsetof(AOTFrame, time_started) == sizeof(uintptr_t) * 2);
  60. bh_static_assert(offsetof(AOTFrame, func_perf_prof_info)
  61. == sizeof(uintptr_t) * 3);
  62. bh_static_assert(offsetof(AOTFrame, ip_offset) == sizeof(uintptr_t) * 4);
  63. bh_static_assert(offsetof(AOTFrame, sp) == sizeof(uintptr_t) * 5);
  64. bh_static_assert(offsetof(AOTFrame, frame_ref) == sizeof(uintptr_t) * 6);
  65. bh_static_assert(offsetof(AOTFrame, lp) == sizeof(uintptr_t) * 7);
  66. bh_static_assert(offsetof(AOTTinyFrame, func_index) == sizeof(uint32) * 0);
  67. bh_static_assert(offsetof(AOTTinyFrame, ip_offset) == sizeof(uint32) * 1);
  68. bh_static_assert(sizeof(AOTTinyFrame) == sizeof(uint32) * 2);
  69. static void
  70. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  71. {
  72. if (error_buf != NULL) {
  73. snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s",
  74. string);
  75. }
  76. }
  77. static void
  78. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  79. {
  80. va_list args;
  81. char buf[128];
  82. if (error_buf != NULL) {
  83. va_start(args, format);
  84. vsnprintf(buf, sizeof(buf), format, args);
  85. va_end(args);
  86. snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s",
  87. buf);
  88. }
  89. }
  90. static void *
  91. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  92. {
  93. void *mem;
  94. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  95. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  96. return NULL;
  97. }
  98. memset(mem, 0, (uint32)size);
  99. return mem;
  100. }
  101. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  102. static bool
  103. is_tiny_frame(WASMExecEnv *exec_env)
  104. {
  105. AOTModule *module =
  106. (AOTModule *)((AOTModuleInstance *)exec_env->module_inst)->module;
  107. return module->feature_flags & WASM_FEATURE_TINY_STACK_FRAME;
  108. }
  109. static bool
  110. is_frame_per_function(WASMExecEnv *exec_env)
  111. {
  112. AOTModule *module =
  113. (AOTModule *)((AOTModuleInstance *)exec_env->module_inst)->module;
  114. return module->feature_flags & WASM_FEATURE_FRAME_PER_FUNCTION;
  115. }
  116. static bool
  117. is_frame_func_idx_disabled(WASMExecEnv *exec_env)
  118. {
  119. AOTModule *module =
  120. (AOTModule *)((AOTModuleInstance *)exec_env->module_inst)->module;
  121. return module->feature_flags & WASM_FEATURE_FRAME_NO_FUNC_IDX;
  122. }
  123. static void *
  124. get_top_frame(WASMExecEnv *exec_env)
  125. {
  126. if (is_tiny_frame(exec_env)) {
  127. return exec_env->wasm_stack.top > exec_env->wasm_stack.bottom
  128. ? exec_env->wasm_stack.top - sizeof(AOTTinyFrame)
  129. : NULL;
  130. }
  131. else {
  132. return exec_env->cur_frame;
  133. }
  134. }
  135. static void *
  136. get_prev_frame(WASMExecEnv *exec_env, void *cur_frame)
  137. {
  138. bh_assert(cur_frame);
  139. if (is_tiny_frame(exec_env)) {
  140. if ((uint8 *)cur_frame == exec_env->wasm_stack.bottom) {
  141. return NULL;
  142. }
  143. return ((AOTTinyFrame *)cur_frame) - 1;
  144. }
  145. else {
  146. return ((AOTFrame *)cur_frame)->prev_frame;
  147. }
  148. }
  149. #endif
  150. static bool
  151. check_global_init_expr(const AOTModule *module, uint32 global_index,
  152. char *error_buf, uint32 error_buf_size)
  153. {
  154. if (global_index >= module->import_global_count + module->global_count) {
  155. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  156. global_index);
  157. return false;
  158. }
  159. /**
  160. * Currently, constant expressions occurring as initializers of
  161. * globals are further constrained in that contained global.get
  162. * instructions are only allowed to refer to imported globals.
  163. *
  164. * And initializer expression cannot reference a mutable global.
  165. */
  166. if (global_index >= module->import_global_count
  167. /* make spec test happy */
  168. #if WASM_ENABLE_GC != 0
  169. + module->global_count
  170. #endif
  171. ) {
  172. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  173. global_index);
  174. return false;
  175. }
  176. if (
  177. /* make spec test happy */
  178. #if WASM_ENABLE_GC != 0
  179. global_index < module->import_global_count &&
  180. #endif
  181. module->import_globals[global_index].type.is_mutable) {
  182. set_error_buf(error_buf, error_buf_size,
  183. "constant expression required");
  184. return false;
  185. }
  186. return true;
  187. }
  188. static void
  189. init_global_data(uint8 *global_data, uint8 type, WASMValue *initial_value)
  190. {
  191. switch (type) {
  192. case VALUE_TYPE_I32:
  193. case VALUE_TYPE_F32:
  194. #if WASM_ENABLE_REF_TYPES != 0
  195. case VALUE_TYPE_FUNCREF:
  196. case VALUE_TYPE_EXTERNREF:
  197. #endif
  198. *(int32 *)global_data = initial_value->i32;
  199. break;
  200. case VALUE_TYPE_I64:
  201. case VALUE_TYPE_F64:
  202. bh_memcpy_s(global_data, sizeof(int64), &initial_value->i64,
  203. sizeof(int64));
  204. break;
  205. #if WASM_ENABLE_SIMD != 0
  206. case VALUE_TYPE_V128:
  207. bh_memcpy_s(global_data, sizeof(V128), &initial_value->v128,
  208. sizeof(V128));
  209. break;
  210. #endif
  211. default:
  212. #if WASM_ENABLE_GC != 0
  213. if ((type >= (uint8)REF_TYPE_ARRAYREF
  214. && type <= (uint8)REF_TYPE_NULLFUNCREF)
  215. || (type >= (uint8)REF_TYPE_HT_NULLABLE
  216. && type <= (uint8)REF_TYPE_HT_NON_NULLABLE)
  217. #if WASM_ENABLE_STRINGREF != 0
  218. || (type >= (uint8)REF_TYPE_STRINGVIEWWTF8
  219. && type <= (uint8)REF_TYPE_STRINGREF)
  220. || (type >= (uint8)REF_TYPE_STRINGVIEWITER
  221. && type <= (uint8)REF_TYPE_STRINGVIEWWTF16)
  222. #endif
  223. ) {
  224. bh_memcpy_s(global_data, sizeof(wasm_obj_t),
  225. &initial_value->gc_obj, sizeof(wasm_obj_t));
  226. break;
  227. }
  228. #endif /* end of WASM_ENABLE_GC */
  229. bh_assert(0);
  230. }
  231. }
  232. #if WASM_ENABLE_GC != 0
  233. static bool
  234. assign_table_init_value(AOTModuleInstance *module_inst, AOTModule *module,
  235. InitializerExpression *init_expr, void *addr,
  236. char *error_buf, uint32 error_buf_size)
  237. {
  238. uint8 flag = init_expr->init_expr_type;
  239. bh_assert(flag >= INIT_EXPR_TYPE_GET_GLOBAL
  240. && flag <= INIT_EXPR_TYPE_EXTERN_CONVERT_ANY);
  241. switch (flag) {
  242. case INIT_EXPR_TYPE_GET_GLOBAL:
  243. {
  244. if (!check_global_init_expr(module, init_expr->u.global_index,
  245. error_buf, error_buf_size)) {
  246. return false;
  247. }
  248. if (init_expr->u.global_index < module->import_global_count) {
  249. PUT_REF_TO_ADDR(
  250. addr, module->import_globals[init_expr->u.global_index]
  251. .global_data_linked.gc_obj);
  252. }
  253. else {
  254. uint32 global_idx =
  255. init_expr->u.global_index - module->import_global_count;
  256. return assign_table_init_value(
  257. module_inst, module, &module->globals[global_idx].init_expr,
  258. addr, error_buf, error_buf_size);
  259. }
  260. break;
  261. }
  262. case INIT_EXPR_TYPE_REFNULL_CONST:
  263. {
  264. WASMObjectRef gc_obj = NULL_REF;
  265. PUT_REF_TO_ADDR(addr, gc_obj);
  266. break;
  267. }
  268. case INIT_EXPR_TYPE_FUNCREF_CONST:
  269. {
  270. WASMFuncObjectRef func_obj = NULL;
  271. uint32 func_idx = init_expr->u.u32;
  272. if (func_idx != UINT32_MAX) {
  273. if (!(func_obj =
  274. aot_create_func_obj(module_inst, func_idx, false,
  275. error_buf, error_buf_size))) {
  276. return false;
  277. }
  278. }
  279. PUT_REF_TO_ADDR(addr, func_obj);
  280. break;
  281. }
  282. case INIT_EXPR_TYPE_I31_NEW:
  283. {
  284. WASMI31ObjectRef i31_obj = wasm_i31_obj_new(init_expr->u.i32);
  285. PUT_REF_TO_ADDR(addr, i31_obj);
  286. break;
  287. }
  288. case INIT_EXPR_TYPE_STRUCT_NEW:
  289. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  290. {
  291. WASMRttType *rtt_type;
  292. WASMStructObjectRef struct_obj;
  293. WASMStructType *struct_type;
  294. WASMStructNewInitValues *init_values = NULL;
  295. uint32 type_idx;
  296. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  297. init_values = (WASMStructNewInitValues *)init_expr->u.data;
  298. type_idx = init_values->type_idx;
  299. }
  300. else {
  301. type_idx = init_expr->u.type_index;
  302. }
  303. struct_type = (WASMStructType *)module->types[type_idx];
  304. if (!(rtt_type = wasm_rtt_type_new(
  305. (WASMType *)struct_type, type_idx, module->rtt_types,
  306. module->type_count, &module->rtt_type_lock))) {
  307. set_error_buf(error_buf, error_buf_size,
  308. "create rtt object failed");
  309. return false;
  310. }
  311. if (!(struct_obj = wasm_struct_obj_new_internal(
  312. ((AOTModuleInstanceExtra *)module_inst->e)
  313. ->common.gc_heap_handle,
  314. rtt_type))) {
  315. set_error_buf(error_buf, error_buf_size,
  316. "create struct object failed");
  317. return false;
  318. }
  319. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  320. uint32 field_idx;
  321. bh_assert(init_values->count == struct_type->field_count);
  322. for (field_idx = 0; field_idx < init_values->count;
  323. field_idx++) {
  324. wasm_struct_obj_set_field(struct_obj, field_idx,
  325. &init_values->fields[field_idx]);
  326. }
  327. }
  328. PUT_REF_TO_ADDR(addr, struct_obj);
  329. break;
  330. }
  331. case INIT_EXPR_TYPE_ARRAY_NEW:
  332. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  333. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  334. {
  335. WASMRttType *rtt_type;
  336. WASMArrayObjectRef array_obj;
  337. WASMArrayType *array_type;
  338. WASMArrayNewInitValues *init_values = NULL;
  339. WASMValue *arr_init_val = NULL, empty_val = { 0 };
  340. uint32 type_idx, len;
  341. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  342. type_idx = init_expr->u.array_new_default.type_index;
  343. len = init_expr->u.array_new_default.length;
  344. arr_init_val = &empty_val;
  345. }
  346. else {
  347. init_values = (WASMArrayNewInitValues *)init_expr->u.data;
  348. type_idx = init_values->type_idx;
  349. len = init_values->length;
  350. if (flag == INIT_EXPR_TYPE_ARRAY_NEW) {
  351. arr_init_val = init_values->elem_data;
  352. }
  353. }
  354. array_type = (WASMArrayType *)module->types[type_idx];
  355. if (!(rtt_type = wasm_rtt_type_new(
  356. (WASMType *)array_type, type_idx, module->rtt_types,
  357. module->type_count, &module->rtt_type_lock))) {
  358. set_error_buf(error_buf, error_buf_size,
  359. "create rtt object failed");
  360. return false;
  361. }
  362. if (!(array_obj = wasm_array_obj_new_internal(
  363. ((AOTModuleInstanceExtra *)module_inst->e)
  364. ->common.gc_heap_handle,
  365. rtt_type, len, arr_init_val))) {
  366. set_error_buf(error_buf, error_buf_size,
  367. "create array object failed");
  368. return false;
  369. }
  370. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  371. uint32 elem_idx;
  372. bh_assert(init_values);
  373. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  374. wasm_array_obj_set_elem(array_obj, elem_idx,
  375. &init_values->elem_data[elem_idx]);
  376. }
  377. }
  378. PUT_REF_TO_ADDR(addr, array_obj);
  379. break;
  380. }
  381. default:
  382. set_error_buf(error_buf, error_buf_size, "invalid init expr type.");
  383. return false;
  384. }
  385. return true;
  386. }
  387. #endif /* end of WASM_ENABLE_GC != 0 */
  388. static bool
  389. global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
  390. char *error_buf, uint32 error_buf_size)
  391. {
  392. uint32 i;
  393. InitializerExpression *init_expr;
  394. uint8 *p = module_inst->global_data;
  395. AOTImportGlobal *import_global = module->import_globals;
  396. AOTGlobal *global = module->globals;
  397. /* Initialize import global data */
  398. for (i = 0; i < module->import_global_count; i++, import_global++) {
  399. bh_assert(import_global->data_offset
  400. == (uint32)(p - module_inst->global_data));
  401. init_global_data(p, import_global->type.val_type,
  402. &import_global->global_data_linked);
  403. p += import_global->size;
  404. }
  405. /* Initialize defined global data */
  406. for (i = 0; i < module->global_count; i++, global++) {
  407. uint8 flag;
  408. bh_assert(global->data_offset
  409. == (uint32)(p - module_inst->global_data));
  410. init_expr = &global->init_expr;
  411. flag = init_expr->init_expr_type;
  412. switch (flag) {
  413. case INIT_EXPR_TYPE_GET_GLOBAL:
  414. {
  415. if (!check_global_init_expr(module, init_expr->u.global_index,
  416. error_buf, error_buf_size)) {
  417. return false;
  418. }
  419. #if WASM_ENABLE_GC == 0
  420. init_global_data(
  421. p, global->type.val_type,
  422. &module->import_globals[init_expr->u.global_index]
  423. .global_data_linked);
  424. #else
  425. if (init_expr->u.global_index < module->import_global_count) {
  426. init_global_data(
  427. p, global->type.val_type,
  428. &module->import_globals[init_expr->u.global_index]
  429. .global_data_linked);
  430. }
  431. else {
  432. uint32 global_idx =
  433. init_expr->u.global_index - module->import_global_count;
  434. init_global_data(p, global->type.val_type,
  435. &module->globals[global_idx].init_expr.u);
  436. }
  437. #endif
  438. break;
  439. }
  440. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  441. case INIT_EXPR_TYPE_REFNULL_CONST:
  442. {
  443. *(uint32 *)p = NULL_REF;
  444. break;
  445. }
  446. #elif WASM_ENABLE_GC != 0
  447. case INIT_EXPR_TYPE_REFNULL_CONST:
  448. {
  449. WASMObjectRef gc_obj = NULL_REF;
  450. PUT_REF_TO_ADDR(p, gc_obj);
  451. break;
  452. }
  453. #endif
  454. #if WASM_ENABLE_GC != 0
  455. case INIT_EXPR_TYPE_FUNCREF_CONST:
  456. {
  457. WASMFuncObjectRef func_obj = NULL;
  458. uint32 func_idx = init_expr->u.u32;
  459. if (func_idx != UINT32_MAX) {
  460. if (!(func_obj =
  461. aot_create_func_obj(module_inst, func_idx, false,
  462. error_buf, error_buf_size))) {
  463. return false;
  464. }
  465. }
  466. PUT_REF_TO_ADDR(p, func_obj);
  467. break;
  468. }
  469. case INIT_EXPR_TYPE_I31_NEW:
  470. {
  471. WASMI31ObjectRef i31_obj = wasm_i31_obj_new(init_expr->u.i32);
  472. PUT_REF_TO_ADDR(p, i31_obj);
  473. break;
  474. }
  475. case INIT_EXPR_TYPE_STRUCT_NEW:
  476. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  477. {
  478. WASMRttType *rtt_type;
  479. WASMStructObjectRef struct_obj;
  480. WASMStructType *struct_type;
  481. WASMStructNewInitValues *init_values = NULL;
  482. uint32 type_idx;
  483. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  484. init_values = (WASMStructNewInitValues *)init_expr->u.data;
  485. type_idx = init_values->type_idx;
  486. }
  487. else {
  488. type_idx = init_expr->u.type_index;
  489. }
  490. struct_type = (WASMStructType *)module->types[type_idx];
  491. if (!(rtt_type = wasm_rtt_type_new(
  492. (WASMType *)struct_type, type_idx, module->rtt_types,
  493. module->type_count, &module->rtt_type_lock))) {
  494. set_error_buf(error_buf, error_buf_size,
  495. "create rtt object failed");
  496. return false;
  497. }
  498. if (!(struct_obj = wasm_struct_obj_new_internal(
  499. ((AOTModuleInstanceExtra *)module_inst->e)
  500. ->common.gc_heap_handle,
  501. rtt_type))) {
  502. set_error_buf(error_buf, error_buf_size,
  503. "create struct object failed");
  504. return false;
  505. }
  506. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  507. uint32 field_idx;
  508. bh_assert(init_values->count == struct_type->field_count);
  509. for (field_idx = 0; field_idx < init_values->count;
  510. field_idx++) {
  511. wasm_struct_obj_set_field(
  512. struct_obj, field_idx,
  513. &init_values->fields[field_idx]);
  514. }
  515. }
  516. PUT_REF_TO_ADDR(p, struct_obj);
  517. break;
  518. }
  519. case INIT_EXPR_TYPE_ARRAY_NEW:
  520. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  521. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  522. {
  523. WASMRttType *rtt_type;
  524. WASMArrayObjectRef array_obj;
  525. WASMArrayType *array_type;
  526. WASMArrayNewInitValues *init_values = NULL;
  527. WASMValue *arr_init_val = NULL, empty_val = { 0 };
  528. uint32 type_idx, len;
  529. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  530. type_idx = init_expr->u.array_new_default.type_index;
  531. len = init_expr->u.array_new_default.length;
  532. arr_init_val = &empty_val;
  533. }
  534. else {
  535. init_values = (WASMArrayNewInitValues *)init_expr->u.data;
  536. type_idx = init_values->type_idx;
  537. len = init_values->length;
  538. if (flag == INIT_EXPR_TYPE_ARRAY_NEW) {
  539. arr_init_val = init_values->elem_data;
  540. }
  541. }
  542. array_type = (WASMArrayType *)module->types[type_idx];
  543. if (!(rtt_type = wasm_rtt_type_new(
  544. (WASMType *)array_type, type_idx, module->rtt_types,
  545. module->type_count, &module->rtt_type_lock))) {
  546. set_error_buf(error_buf, error_buf_size,
  547. "create rtt object failed");
  548. return false;
  549. }
  550. if (!(array_obj = wasm_array_obj_new_internal(
  551. ((AOTModuleInstanceExtra *)module_inst->e)
  552. ->common.gc_heap_handle,
  553. rtt_type, len, arr_init_val))) {
  554. set_error_buf(error_buf, error_buf_size,
  555. "create array object failed");
  556. return false;
  557. }
  558. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  559. uint32 elem_idx;
  560. bh_assert(init_values);
  561. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  562. wasm_array_obj_set_elem(
  563. array_obj, elem_idx,
  564. &init_values->elem_data[elem_idx]);
  565. }
  566. }
  567. PUT_REF_TO_ADDR(p, array_obj);
  568. break;
  569. }
  570. #endif /* end of WASM_ENABLE_GC != 0 */
  571. default:
  572. {
  573. init_global_data(p, global->type.val_type, &init_expr->u);
  574. break;
  575. }
  576. }
  577. p += global->size;
  578. }
  579. bh_assert(module_inst->global_data_size
  580. == (uint32)(p - module_inst->global_data));
  581. return true;
  582. }
  583. static bool
  584. tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
  585. AOTTableInstance *first_tbl_inst, char *error_buf,
  586. uint32 error_buf_size)
  587. {
  588. uint32 i, global_index, global_data_offset, base_offset, length;
  589. uint64 total_size;
  590. AOTTableInitData *table_seg;
  591. AOTTableInstance *tbl_inst = first_tbl_inst;
  592. total_size = (uint64)sizeof(AOTTableInstance *) * module_inst->table_count;
  593. if (total_size > 0
  594. && !(module_inst->tables =
  595. runtime_malloc(total_size, error_buf, error_buf_size))) {
  596. return false;
  597. }
  598. /*
  599. * treat import table like a local one until we enable module linking
  600. * in AOT mode
  601. */
  602. for (i = 0; i != module_inst->table_count; ++i) {
  603. if (i < module->import_table_count) {
  604. AOTImportTable *import_table = module->import_tables + i;
  605. tbl_inst->cur_size = import_table->table_type.init_size;
  606. tbl_inst->max_size =
  607. aot_get_imp_tbl_data_slots(import_table, false);
  608. tbl_inst->elem_type = module->tables[i].table_type.elem_type;
  609. #if WASM_ENABLE_GC != 0
  610. tbl_inst->elem_ref_type.elem_ref_type =
  611. module->tables[i].table_type.elem_ref_type;
  612. #endif
  613. }
  614. else {
  615. AOTTable *table = module->tables + (i - module->import_table_count);
  616. tbl_inst->cur_size = table->table_type.init_size;
  617. tbl_inst->max_size = aot_get_tbl_data_slots(table, false);
  618. tbl_inst->elem_type = module->tables[i].table_type.elem_type;
  619. #if WASM_ENABLE_GC != 0
  620. tbl_inst->elem_ref_type.elem_ref_type =
  621. module->tables[i].table_type.elem_ref_type;
  622. #endif
  623. }
  624. /* Set all elements to -1 or NULL_REF to mark them as uninitialized
  625. * elements */
  626. #if WASM_ENABLE_GC == 0
  627. memset(tbl_inst->elems, 0xff,
  628. sizeof(table_elem_type_t) * tbl_inst->max_size);
  629. #else
  630. memset(tbl_inst->elems, 0x00,
  631. sizeof(table_elem_type_t) * tbl_inst->max_size);
  632. #endif
  633. module_inst->tables[i] = tbl_inst;
  634. tbl_inst = (AOTTableInstance *)((uint8 *)tbl_inst
  635. + offsetof(AOTTableInstance, elems)
  636. + sizeof(table_elem_type_t)
  637. * tbl_inst->max_size);
  638. }
  639. /* fill table with element segment content */
  640. for (i = 0; i < module->table_init_data_count; i++) {
  641. #if WASM_ENABLE_GC == 0
  642. uint32 j;
  643. #endif
  644. table_seg = module->table_init_data_list[i];
  645. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  646. if (!wasm_elem_is_active(table_seg->mode))
  647. continue;
  648. #endif
  649. bh_assert(table_seg->table_index < module_inst->table_count);
  650. tbl_inst = module_inst->tables[table_seg->table_index];
  651. bh_assert(tbl_inst);
  652. #if WASM_ENABLE_REF_TYPES != 0
  653. bh_assert(
  654. table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  655. || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  656. || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  657. || table_seg->offset.init_expr_type
  658. == INIT_EXPR_TYPE_REFNULL_CONST);
  659. #else
  660. bh_assert(table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  661. || table_seg->offset.init_expr_type
  662. == INIT_EXPR_TYPE_GET_GLOBAL);
  663. #endif
  664. /* Resolve table data base offset */
  665. if (table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  666. global_index = table_seg->offset.u.global_index;
  667. if (!check_global_init_expr(module, global_index, error_buf,
  668. error_buf_size)) {
  669. return false;
  670. }
  671. if (global_index < module->import_global_count)
  672. global_data_offset =
  673. module->import_globals[global_index].data_offset;
  674. else
  675. global_data_offset =
  676. module->globals[global_index - module->import_global_count]
  677. .data_offset;
  678. base_offset =
  679. *(uint32 *)(module_inst->global_data + global_data_offset);
  680. }
  681. else
  682. base_offset = (uint32)table_seg->offset.u.i32;
  683. /* Copy table data */
  684. /* base_offset only since length might negative */
  685. if (base_offset > tbl_inst->cur_size) {
  686. #if WASM_ENABLE_REF_TYPES != 0
  687. set_error_buf(error_buf, error_buf_size,
  688. "out of bounds table access");
  689. #else
  690. set_error_buf(error_buf, error_buf_size,
  691. "type mismatch: elements segment does not fit");
  692. #endif
  693. return false;
  694. }
  695. /* base_offset + length(could be zero) */
  696. length = table_seg->value_count;
  697. if (base_offset + length > tbl_inst->cur_size) {
  698. #if WASM_ENABLE_REF_TYPES != 0
  699. set_error_buf(error_buf, error_buf_size,
  700. "out of bounds table access");
  701. #else
  702. set_error_buf(error_buf, error_buf_size,
  703. "type mismatch: elements segment does not fit");
  704. #endif
  705. return false;
  706. }
  707. /**
  708. * Check function index in the current module inst for now.
  709. * will check the linked table inst owner in future
  710. */
  711. #if WASM_ENABLE_GC == 0
  712. for (j = 0; j < length; j++) {
  713. tbl_inst->elems[base_offset + j] =
  714. table_seg->init_values[j].u.ref_index;
  715. }
  716. #endif
  717. }
  718. return true;
  719. }
  720. static void
  721. memories_deinstantiate(AOTModuleInstance *module_inst)
  722. {
  723. uint32 i;
  724. AOTMemoryInstance *memory_inst;
  725. for (i = 0; i < module_inst->memory_count; i++) {
  726. memory_inst = module_inst->memories[i];
  727. if (memory_inst) {
  728. #if WASM_ENABLE_SHARED_MEMORY != 0
  729. if (shared_memory_is_shared(memory_inst)) {
  730. uint32 ref_count = shared_memory_dec_reference(memory_inst);
  731. /* if the reference count is not zero,
  732. don't free the memory */
  733. if (ref_count > 0)
  734. continue;
  735. }
  736. #endif
  737. if (memory_inst->heap_handle) {
  738. mem_allocator_destroy(memory_inst->heap_handle);
  739. wasm_runtime_free(memory_inst->heap_handle);
  740. }
  741. if (memory_inst->memory_data) {
  742. wasm_deallocate_linear_memory(memory_inst);
  743. }
  744. }
  745. }
  746. wasm_runtime_free(module_inst->memories);
  747. }
  748. static AOTMemoryInstance *
  749. memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
  750. AOTModule *module, AOTMemoryInstance *memory_inst,
  751. AOTMemory *memory, uint32 memory_idx, uint32 heap_size,
  752. uint32 max_memory_pages, char *error_buf,
  753. uint32 error_buf_size)
  754. {
  755. void *heap_handle;
  756. uint32 num_bytes_per_page = memory->num_bytes_per_page;
  757. uint32 init_page_count = memory->init_page_count;
  758. uint32 max_page_count = wasm_runtime_get_max_mem(
  759. max_memory_pages, memory->init_page_count, memory->max_page_count);
  760. uint32 default_max_pages;
  761. uint32 inc_page_count, global_idx;
  762. uint32 bytes_of_last_page, bytes_to_page_end;
  763. uint64 aux_heap_base,
  764. heap_offset = (uint64)num_bytes_per_page * init_page_count;
  765. uint64 memory_data_size, max_memory_data_size;
  766. uint8 *p = NULL, *global_addr;
  767. bool is_memory64 = memory->flags & MEMORY64_FLAG;
  768. bool is_shared_memory = false;
  769. #if WASM_ENABLE_SHARED_MEMORY != 0
  770. is_shared_memory = memory->flags & SHARED_MEMORY_FLAG ? true : false;
  771. /* Shared memory */
  772. if (is_shared_memory && parent != NULL) {
  773. AOTMemoryInstance *shared_memory_instance;
  774. bh_assert(memory_idx == 0);
  775. bh_assert(parent->memory_count > memory_idx);
  776. shared_memory_instance = parent->memories[memory_idx];
  777. shared_memory_inc_reference(shared_memory_instance);
  778. return shared_memory_instance;
  779. }
  780. #endif
  781. #if WASM_ENABLE_MEMORY64 != 0
  782. if (is_memory64) {
  783. default_max_pages = DEFAULT_MEM64_MAX_PAGES;
  784. }
  785. else
  786. #endif
  787. {
  788. default_max_pages = DEFAULT_MAX_PAGES;
  789. }
  790. if (heap_size > 0 && module->malloc_func_index != (uint32)-1
  791. && module->free_func_index != (uint32)-1) {
  792. /* Disable app heap, use malloc/free function exported
  793. by wasm app to allocate/free memory instead */
  794. heap_size = 0;
  795. }
  796. /* If initial memory is the largest size allowed, disallowing insert host
  797. * managed heap */
  798. if (heap_size > 0 && heap_offset == MAX_LINEAR_MEMORY_SIZE) {
  799. set_error_buf(error_buf, error_buf_size,
  800. "failed to insert app heap into linear memory, "
  801. "try using `--heap-size=0` option");
  802. return NULL;
  803. }
  804. if (init_page_count == max_page_count && init_page_count == 1) {
  805. /* If only one page and at most one page, we just append
  806. the app heap to the end of linear memory, enlarge the
  807. num_bytes_per_page, and don't change the page count */
  808. heap_offset = num_bytes_per_page;
  809. num_bytes_per_page += heap_size;
  810. if (num_bytes_per_page < heap_size) {
  811. set_error_buf(error_buf, error_buf_size,
  812. "failed to insert app heap into linear memory, "
  813. "try using `--heap-size=0` option");
  814. return NULL;
  815. }
  816. }
  817. else if (heap_size > 0) {
  818. if (init_page_count == max_page_count && init_page_count == 0) {
  819. /* If the memory data size is always 0, we resize it to
  820. one page for app heap */
  821. num_bytes_per_page = heap_size;
  822. heap_offset = 0;
  823. inc_page_count = 1;
  824. }
  825. else if (module->aux_heap_base_global_index != (uint32)-1
  826. && module->aux_heap_base
  827. < (uint64)num_bytes_per_page * init_page_count) {
  828. /* Insert app heap before __heap_base */
  829. aux_heap_base = module->aux_heap_base;
  830. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  831. if (bytes_of_last_page == 0)
  832. bytes_of_last_page = num_bytes_per_page;
  833. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  834. inc_page_count =
  835. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  836. / num_bytes_per_page;
  837. heap_offset = aux_heap_base;
  838. aux_heap_base += heap_size;
  839. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  840. if (bytes_of_last_page == 0)
  841. bytes_of_last_page = num_bytes_per_page;
  842. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  843. if (bytes_to_page_end < 1 * BH_KB) {
  844. aux_heap_base += 1 * BH_KB;
  845. inc_page_count++;
  846. }
  847. /* Adjust __heap_base global value */
  848. global_idx = module->aux_heap_base_global_index
  849. - module->import_global_count;
  850. global_addr = module_inst->global_data
  851. + module->globals[global_idx].data_offset;
  852. *(uint32 *)global_addr = (uint32)aux_heap_base;
  853. LOG_VERBOSE("Reset __heap_base global to %" PRIu64, aux_heap_base);
  854. }
  855. else {
  856. /* Insert app heap before new page */
  857. inc_page_count =
  858. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  859. heap_offset = (uint64)num_bytes_per_page * init_page_count;
  860. heap_size = (uint64)num_bytes_per_page * inc_page_count;
  861. if (heap_size > 0)
  862. heap_size -= 1 * BH_KB;
  863. }
  864. init_page_count += inc_page_count;
  865. max_page_count += inc_page_count;
  866. if (init_page_count > default_max_pages) {
  867. set_error_buf(error_buf, error_buf_size,
  868. "failed to insert app heap into linear memory, "
  869. "try using `--heap-size=0` option");
  870. return NULL;
  871. }
  872. if (max_page_count > default_max_pages)
  873. max_page_count = default_max_pages;
  874. }
  875. LOG_VERBOSE("Memory instantiate:");
  876. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  877. num_bytes_per_page, init_page_count, max_page_count);
  878. LOG_VERBOSE(" data offset: %" PRIu64 ", stack size: %d",
  879. module->aux_data_end, module->aux_stack_size);
  880. LOG_VERBOSE(" heap offset: %" PRIu64 ", heap size: %d\n", heap_offset,
  881. heap_size);
  882. max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  883. bh_assert(max_memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  884. (void)max_memory_data_size;
  885. /* TODO: memory64 uses is_memory64 flag */
  886. if (wasm_allocate_linear_memory(&p, is_shared_memory, is_memory64,
  887. num_bytes_per_page, init_page_count,
  888. max_page_count, &memory_data_size)
  889. != BHT_OK) {
  890. set_error_buf(error_buf, error_buf_size,
  891. "allocate linear memory failed");
  892. return NULL;
  893. }
  894. memory_inst->module_type = Wasm_Module_AoT;
  895. memory_inst->num_bytes_per_page = num_bytes_per_page;
  896. memory_inst->cur_page_count = init_page_count;
  897. memory_inst->max_page_count = max_page_count;
  898. memory_inst->memory_data_size = memory_data_size;
  899. #if WASM_ENABLE_MEMORY64 != 0
  900. if (is_memory64) {
  901. memory_inst->is_memory64 = 1;
  902. }
  903. #endif
  904. /* Init memory info */
  905. memory_inst->memory_data = p;
  906. memory_inst->memory_data_end = p + memory_data_size;
  907. /* Initialize heap info */
  908. memory_inst->heap_data = p + heap_offset;
  909. memory_inst->heap_data_end = p + heap_offset + heap_size;
  910. if (heap_size > 0) {
  911. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  912. if (!(heap_handle = runtime_malloc((uint64)heap_struct_size, error_buf,
  913. error_buf_size))) {
  914. goto fail1;
  915. }
  916. memory_inst->heap_handle = heap_handle;
  917. if (!mem_allocator_create_with_struct_and_pool(
  918. heap_handle, heap_struct_size, memory_inst->heap_data,
  919. heap_size)) {
  920. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  921. goto fail2;
  922. }
  923. }
  924. if (memory_data_size > 0) {
  925. wasm_runtime_set_mem_bound_check_bytes(memory_inst, memory_data_size);
  926. }
  927. #if WASM_ENABLE_SHARED_MEMORY != 0
  928. if (is_shared_memory) {
  929. memory_inst->is_shared_memory = 1;
  930. memory_inst->ref_count = 1;
  931. }
  932. #endif
  933. return memory_inst;
  934. fail2:
  935. if (heap_size > 0)
  936. wasm_runtime_free(memory_inst->heap_handle);
  937. fail1:
  938. wasm_deallocate_linear_memory(memory_inst);
  939. return NULL;
  940. }
  941. AOTMemoryInstance *
  942. aot_lookup_memory(AOTModuleInstance *module_inst, char const *name)
  943. {
  944. #if WASM_ENABLE_MULTI_MEMORY != 0
  945. uint32 i;
  946. for (i = 0; i < module_inst->export_memory_count; i++)
  947. if (!strcmp(module_inst->export_memories[i].name, name))
  948. return module_inst->export_memories[i].memory;
  949. return NULL;
  950. #else
  951. (void)module_inst->export_memories;
  952. if (!module_inst->memories)
  953. return NULL;
  954. return module_inst->memories[0];
  955. #endif
  956. }
  957. AOTMemoryInstance *
  958. aot_get_default_memory(AOTModuleInstance *module_inst)
  959. {
  960. if (module_inst->memories)
  961. return module_inst->memories[0];
  962. else
  963. return NULL;
  964. }
  965. AOTMemoryInstance *
  966. aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
  967. {
  968. if ((index >= module_inst->memory_count) || !module_inst->memories)
  969. return NULL;
  970. return module_inst->memories[index];
  971. }
  972. static bool
  973. memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
  974. AOTModule *module, uint32 heap_size,
  975. uint32 max_memory_pages, char *error_buf,
  976. uint32 error_buf_size)
  977. {
  978. uint32 global_index, global_data_offset, length;
  979. uint32 i, memory_count = module->memory_count;
  980. AOTMemoryInstance *memories, *memory_inst;
  981. AOTMemInitData *data_seg;
  982. uint64 total_size;
  983. mem_offset_t base_offset;
  984. module_inst->memory_count = memory_count;
  985. total_size = sizeof(AOTMemoryInstance *) * (uint64)memory_count;
  986. if (!(module_inst->memories =
  987. runtime_malloc(total_size, error_buf, error_buf_size))) {
  988. return false;
  989. }
  990. memories = module_inst->global_table_data.memory_instances;
  991. for (i = 0; i < memory_count; i++, memories++) {
  992. memory_inst = memory_instantiate(
  993. module_inst, parent, module, memories, &module->memories[i], i,
  994. heap_size, max_memory_pages, error_buf, error_buf_size);
  995. if (!memory_inst) {
  996. return false;
  997. }
  998. module_inst->memories[i] = memory_inst;
  999. }
  1000. /* Get default memory instance */
  1001. memory_inst = aot_get_default_memory(module_inst);
  1002. if (!memory_inst) {
  1003. /* Ignore setting memory init data if no memory inst is created */
  1004. return true;
  1005. }
  1006. for (i = 0; i < module->mem_init_data_count; i++) {
  1007. data_seg = module->mem_init_data_list[i];
  1008. #if WASM_ENABLE_BULK_MEMORY != 0
  1009. if (data_seg->is_passive)
  1010. continue;
  1011. #endif
  1012. if (parent != NULL)
  1013. /* Ignore setting memory init data if the memory has been
  1014. initialized */
  1015. continue;
  1016. bh_assert(data_seg->offset.init_expr_type
  1017. == (memory_inst->is_memory64 ? INIT_EXPR_TYPE_I64_CONST
  1018. : INIT_EXPR_TYPE_I32_CONST)
  1019. || data_seg->offset.init_expr_type
  1020. == INIT_EXPR_TYPE_GET_GLOBAL);
  1021. /* Resolve memory data base offset */
  1022. if (data_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1023. global_index = data_seg->offset.u.global_index;
  1024. if (!check_global_init_expr(module, global_index, error_buf,
  1025. error_buf_size)) {
  1026. return false;
  1027. }
  1028. if (global_index < module->import_global_count)
  1029. global_data_offset =
  1030. module->import_globals[global_index].data_offset;
  1031. else
  1032. global_data_offset =
  1033. module->globals[global_index - module->import_global_count]
  1034. .data_offset;
  1035. #if WASM_ENABLE_MEMORY64 != 0
  1036. if (memory_inst->is_memory64) {
  1037. base_offset =
  1038. *(uint64 *)(module_inst->global_data + global_data_offset);
  1039. }
  1040. else
  1041. #endif
  1042. {
  1043. base_offset =
  1044. *(uint32 *)(module_inst->global_data + global_data_offset);
  1045. }
  1046. }
  1047. else {
  1048. #if WASM_ENABLE_MEMORY64 != 0
  1049. if (memory_inst->is_memory64) {
  1050. base_offset = data_seg->offset.u.i64;
  1051. }
  1052. else
  1053. #endif
  1054. {
  1055. base_offset = data_seg->offset.u.u32;
  1056. }
  1057. }
  1058. /* Copy memory data */
  1059. bh_assert(memory_inst->memory_data
  1060. || memory_inst->memory_data_size == 0);
  1061. /* Check memory data */
  1062. /* check offset since length might negative */
  1063. if (base_offset > memory_inst->memory_data_size) {
  1064. LOG_DEBUG("base_offset(%" PR_MEM_OFFSET
  1065. ") > memory_data_size(%" PRIu64 ")",
  1066. base_offset, memory_inst->memory_data_size);
  1067. #if WASM_ENABLE_REF_TYPES != 0
  1068. set_error_buf(error_buf, error_buf_size,
  1069. "out of bounds memory access");
  1070. #else
  1071. set_error_buf(error_buf, error_buf_size,
  1072. "data segment does not fit");
  1073. #endif
  1074. return false;
  1075. }
  1076. /* check offset + length(could be zero) */
  1077. length = data_seg->byte_count;
  1078. if (base_offset + length > memory_inst->memory_data_size) {
  1079. LOG_DEBUG("base_offset(%" PR_MEM_OFFSET
  1080. ") + length(%d) > memory_data_size(%" PRIu64 ")",
  1081. base_offset, length, memory_inst->memory_data_size);
  1082. #if WASM_ENABLE_REF_TYPES != 0
  1083. set_error_buf(error_buf, error_buf_size,
  1084. "out of bounds memory access");
  1085. #else
  1086. set_error_buf(error_buf, error_buf_size,
  1087. "data segment does not fit");
  1088. #endif
  1089. return false;
  1090. }
  1091. if (memory_inst->memory_data) {
  1092. bh_memcpy_s((uint8 *)memory_inst->memory_data + base_offset,
  1093. (uint32)(memory_inst->memory_data_size - base_offset),
  1094. data_seg->bytes, length);
  1095. }
  1096. }
  1097. return true;
  1098. }
  1099. static bool
  1100. init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
  1101. char *error_buf, uint32 error_buf_size)
  1102. {
  1103. uint32 i;
  1104. void **func_ptrs;
  1105. uint64 total_size = ((uint64)module->import_func_count + module->func_count)
  1106. * sizeof(void *);
  1107. if (module->import_func_count + module->func_count == 0)
  1108. return true;
  1109. /* Allocate memory */
  1110. if (!(module_inst->func_ptrs =
  1111. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1112. return false;
  1113. }
  1114. /* Set import function pointers */
  1115. func_ptrs = (void **)module_inst->func_ptrs;
  1116. for (i = 0; i < module->import_func_count; i++, func_ptrs++) {
  1117. *func_ptrs = (void *)module->import_funcs[i].func_ptr_linked;
  1118. if (!*func_ptrs) {
  1119. const char *module_name = module->import_funcs[i].module_name;
  1120. const char *field_name = module->import_funcs[i].func_name;
  1121. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1122. module_name, field_name);
  1123. }
  1124. }
  1125. /* Set defined function pointers */
  1126. bh_memcpy_s(func_ptrs, sizeof(void *) * module->func_count,
  1127. module->func_ptrs, sizeof(void *) * module->func_count);
  1128. return true;
  1129. }
  1130. AOTFunctionInstance *
  1131. aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
  1132. {
  1133. AOTModule *module = (AOTModule *)module_inst->module;
  1134. AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
  1135. AOTFunctionInstance *export_funcs =
  1136. (AOTFunctionInstance *)module_inst->export_functions;
  1137. uint32 i;
  1138. /* export functions are pre-instantiated */
  1139. for (i = 0; i < module_inst->export_func_count; i++) {
  1140. if (export_funcs[i].func_index == func_idx)
  1141. return &export_funcs[i];
  1142. }
  1143. exception_lock(module_inst);
  1144. /* allocate functions array if needed */
  1145. if (!extra->functions) {
  1146. uint64 func_count =
  1147. ((uint64)module->import_func_count + module->func_count);
  1148. uint64 total_size = func_count * (uint64)sizeof(AOTFunctionInstance *);
  1149. if ((func_count == 0)
  1150. || !(extra->functions = runtime_malloc(total_size, NULL, 0))) {
  1151. exception_unlock(module_inst);
  1152. return NULL;
  1153. }
  1154. extra->function_count = (uint32)func_count;
  1155. }
  1156. /* instantiate function if needed */
  1157. bh_assert(func_idx < extra->function_count);
  1158. if (!extra->functions[func_idx]) {
  1159. AOTFunctionInstance *function = (AOTFunctionInstance *)runtime_malloc(
  1160. sizeof(AOTFunctionInstance), NULL, 0);
  1161. if (!function) {
  1162. exception_unlock(module_inst);
  1163. return NULL;
  1164. }
  1165. if (func_idx < module->import_func_count) {
  1166. /* instantiate function from import section */
  1167. function->is_import_func = true;
  1168. function->func_name = module->import_funcs[func_idx].func_name;
  1169. function->func_index = func_idx;
  1170. function->u.func_import = &module->import_funcs[func_idx];
  1171. }
  1172. else {
  1173. /* instantiate non-import function */
  1174. uint32 ftype_index =
  1175. module->func_type_indexes[func_idx - module->import_func_count];
  1176. function->is_import_func = false;
  1177. function->func_index = func_idx;
  1178. function->u.func.func_type =
  1179. (AOTFuncType *)module->types[ftype_index];
  1180. function->u.func.func_ptr =
  1181. module->func_ptrs[func_idx - module->import_func_count];
  1182. }
  1183. extra->functions[func_idx] = function;
  1184. }
  1185. exception_unlock(module_inst);
  1186. return extra->functions[func_idx];
  1187. }
  1188. static bool
  1189. init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module,
  1190. char *error_buf, uint32 error_buf_size)
  1191. {
  1192. uint32 i;
  1193. uint32 *func_type_index;
  1194. uint64 total_size = ((uint64)module->import_func_count + module->func_count)
  1195. * sizeof(uint32);
  1196. if (module->import_func_count + module->func_count == 0)
  1197. return true;
  1198. /* Allocate memory */
  1199. if (!(module_inst->func_type_indexes =
  1200. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1201. return false;
  1202. }
  1203. /* Set import function type indexes */
  1204. func_type_index = module_inst->func_type_indexes;
  1205. for (i = 0; i < module->import_func_count; i++, func_type_index++)
  1206. *func_type_index = module->import_funcs[i].func_type_index;
  1207. bh_memcpy_s(func_type_index, sizeof(uint32) * module->func_count,
  1208. module->func_type_indexes, sizeof(uint32) * module->func_count);
  1209. return true;
  1210. }
  1211. static bool
  1212. create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module,
  1213. char *error_buf, uint32 error_buf_size)
  1214. {
  1215. AOTExport *exports = module->exports;
  1216. AOTFunctionInstance *export_func;
  1217. uint64 size;
  1218. uint32 i, func_index, ftype_index;
  1219. if (module_inst->export_func_count > 0) {
  1220. /* Allocate memory */
  1221. size = sizeof(AOTFunctionInstance)
  1222. * (uint64)module_inst->export_func_count;
  1223. if (!(export_func = runtime_malloc(size, error_buf, error_buf_size))) {
  1224. return false;
  1225. }
  1226. module_inst->export_functions = (void *)export_func;
  1227. for (i = 0; i < module->export_count; i++) {
  1228. if (exports[i].kind == EXPORT_KIND_FUNC) {
  1229. export_func->func_name = exports[i].name;
  1230. export_func->func_index = exports[i].index;
  1231. if (export_func->func_index < module->import_func_count) {
  1232. export_func->is_import_func = true;
  1233. export_func->u.func_import =
  1234. &module->import_funcs[export_func->func_index];
  1235. }
  1236. else {
  1237. export_func->is_import_func = false;
  1238. func_index =
  1239. export_func->func_index - module->import_func_count;
  1240. ftype_index = module->func_type_indexes[func_index];
  1241. export_func->u.func.func_type =
  1242. (AOTFuncType *)module->types[ftype_index];
  1243. export_func->u.func.func_ptr =
  1244. module->func_ptrs[func_index];
  1245. }
  1246. export_func++;
  1247. }
  1248. }
  1249. }
  1250. return true;
  1251. }
  1252. #if WASM_ENABLE_MULTI_MEMORY != 0
  1253. static WASMExportMemInstance *
  1254. export_memories_instantiate(const AOTModule *module,
  1255. AOTModuleInstance *module_inst,
  1256. uint32 export_mem_count, char *error_buf,
  1257. uint32 error_buf_size)
  1258. {
  1259. WASMExportMemInstance *export_memories, *export_memory;
  1260. AOTExport *export = module->exports;
  1261. uint32 i;
  1262. uint64 total_size =
  1263. sizeof(WASMExportMemInstance) * (uint64)export_mem_count;
  1264. if (!(export_memory = export_memories =
  1265. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1266. return NULL;
  1267. }
  1268. for (i = 0; i < module->export_count; i++, export ++)
  1269. if (export->kind == EXPORT_KIND_MEMORY) {
  1270. export_memory->name = export->name;
  1271. export_memory->memory = module_inst->memories[export->index];
  1272. export_memory++;
  1273. }
  1274. bh_assert((uint32)(export_memory - export_memories) == export_mem_count);
  1275. return export_memories;
  1276. }
  1277. #endif /* end of if WASM_ENABLE_MULTI_MEMORY != 0 */
  1278. static bool
  1279. create_exports(AOTModuleInstance *module_inst, AOTModule *module,
  1280. char *error_buf, uint32 error_buf_size)
  1281. {
  1282. AOTExport *exports = module->exports;
  1283. uint32 i;
  1284. for (i = 0; i < module->export_count; i++) {
  1285. switch (exports[i].kind) {
  1286. case EXPORT_KIND_FUNC:
  1287. module_inst->export_func_count++;
  1288. break;
  1289. case EXPORT_KIND_GLOBAL:
  1290. module_inst->export_global_count++;
  1291. break;
  1292. case EXPORT_KIND_TABLE:
  1293. module_inst->export_table_count++;
  1294. break;
  1295. case EXPORT_KIND_MEMORY:
  1296. module_inst->export_memory_count++;
  1297. break;
  1298. default:
  1299. return false;
  1300. }
  1301. }
  1302. #if WASM_ENABLE_MULTI_MEMORY == 0
  1303. bh_assert(module_inst->export_memory_count <= 1);
  1304. #else
  1305. if (module_inst->export_memory_count) {
  1306. module_inst->export_memories = export_memories_instantiate(
  1307. module, module_inst, module_inst->export_memory_count, error_buf,
  1308. error_buf_size);
  1309. if (!module_inst->export_memories) {
  1310. return false;
  1311. }
  1312. }
  1313. #endif
  1314. return create_export_funcs(module_inst, module, error_buf, error_buf_size);
  1315. }
  1316. static AOTFunctionInstance *
  1317. lookup_post_instantiate_func(AOTModuleInstance *module_inst,
  1318. const char *func_name)
  1319. {
  1320. AOTFunctionInstance *func;
  1321. AOTFuncType *func_type;
  1322. if (!(func = aot_lookup_function(module_inst, func_name)))
  1323. /* Not found */
  1324. return NULL;
  1325. func_type = func->u.func.func_type;
  1326. if (!(func_type->param_count == 0 && func_type->result_count == 0))
  1327. /* Not a valid function type, ignore it */
  1328. return NULL;
  1329. return func;
  1330. }
  1331. static bool
  1332. execute_post_instantiate_functions(AOTModuleInstance *module_inst,
  1333. bool is_sub_inst, WASMExecEnv *exec_env_main)
  1334. {
  1335. AOTModule *module = (AOTModule *)module_inst->module;
  1336. AOTFunctionInstance *initialize_func = NULL;
  1337. AOTFunctionInstance *post_inst_func = NULL;
  1338. AOTFunctionInstance *call_ctors_func = NULL;
  1339. WASMModuleInstanceCommon *module_inst_main = NULL;
  1340. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1341. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1342. #endif
  1343. WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
  1344. bool ret = false;
  1345. #if WASM_ENABLE_LIBC_WASI != 0
  1346. /*
  1347. * WASI reactor instances may assume that _initialize will be called by
  1348. * the environment at most once, and that none of their other exports
  1349. * are accessed before that call.
  1350. */
  1351. if (!is_sub_inst && module->import_wasi_api) {
  1352. initialize_func =
  1353. lookup_post_instantiate_func(module_inst, "_initialize");
  1354. }
  1355. #endif
  1356. /* Execute possible "__post_instantiate" function if wasm app is
  1357. compiled by emsdk's early version */
  1358. if (!is_sub_inst) {
  1359. post_inst_func =
  1360. lookup_post_instantiate_func(module_inst, "__post_instantiate");
  1361. }
  1362. #if WASM_ENABLE_BULK_MEMORY != 0
  1363. /* Only execute the memory init function for main instance since
  1364. the data segments will be dropped once initialized */
  1365. if (!is_sub_inst
  1366. #if WASM_ENABLE_LIBC_WASI != 0
  1367. && !module->import_wasi_api
  1368. #endif
  1369. ) {
  1370. call_ctors_func =
  1371. lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
  1372. }
  1373. #endif
  1374. if (!module->start_function && !initialize_func && !post_inst_func
  1375. && !call_ctors_func) {
  1376. /* No post instantiation functions to call */
  1377. return true;
  1378. }
  1379. if (is_sub_inst) {
  1380. bh_assert(exec_env_main);
  1381. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1382. bh_assert(exec_env_tls == exec_env_main);
  1383. (void)exec_env_tls;
  1384. #endif
  1385. exec_env = exec_env_main;
  1386. /* Temporarily replace parent exec_env's module inst to current
  1387. module inst to avoid checking failure when calling the
  1388. wasm functions, and ensure that the exec_env's module inst
  1389. is the correct one. */
  1390. module_inst_main = exec_env_main->module_inst;
  1391. wasm_exec_env_set_module_inst(exec_env,
  1392. (WASMModuleInstanceCommon *)module_inst);
  1393. }
  1394. else {
  1395. /* Try using the existing exec_env */
  1396. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1397. exec_env = exec_env_tls;
  1398. #endif
  1399. #if WASM_ENABLE_THREAD_MGR != 0
  1400. if (!exec_env)
  1401. exec_env = wasm_clusters_search_exec_env(
  1402. (WASMModuleInstanceCommon *)module_inst);
  1403. #endif
  1404. if (!exec_env) {
  1405. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1406. (WASMModuleInstanceCommon *)module_inst,
  1407. module_inst->default_wasm_stack_size))) {
  1408. aot_set_exception(module_inst, "allocate memory failed");
  1409. return false;
  1410. }
  1411. }
  1412. else {
  1413. /* Temporarily replace exec_env's module inst with current
  1414. module inst to ensure that the exec_env's module inst
  1415. is the correct one. */
  1416. module_inst_main = exec_env->module_inst;
  1417. wasm_exec_env_set_module_inst(
  1418. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1419. }
  1420. }
  1421. #if defined(os_writegsbase)
  1422. {
  1423. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  1424. if (memory_inst)
  1425. /* write base addr of linear memory to GS segment register */
  1426. os_writegsbase(memory_inst->memory_data);
  1427. }
  1428. #endif
  1429. /* Execute start function for both main instance and sub instance */
  1430. if (module->start_function) {
  1431. AOTFunctionInstance start_func = { 0 };
  1432. uint32 func_type_idx;
  1433. start_func.func_name = "";
  1434. start_func.func_index = module->start_func_index;
  1435. start_func.is_import_func = false;
  1436. func_type_idx = module->func_type_indexes[module->start_func_index
  1437. - module->import_func_count];
  1438. start_func.u.func.func_type =
  1439. (AOTFuncType *)module->types[func_type_idx];
  1440. start_func.u.func.func_ptr = module->start_function;
  1441. if (!aot_call_function(exec_env, &start_func, 0, NULL)) {
  1442. goto fail;
  1443. }
  1444. }
  1445. if (initialize_func
  1446. && !aot_call_function(exec_env, initialize_func, 0, NULL)) {
  1447. goto fail;
  1448. }
  1449. if (post_inst_func
  1450. && !aot_call_function(exec_env, post_inst_func, 0, NULL)) {
  1451. goto fail;
  1452. }
  1453. if (call_ctors_func
  1454. && !aot_call_function(exec_env, call_ctors_func, 0, NULL)) {
  1455. goto fail;
  1456. }
  1457. ret = true;
  1458. fail:
  1459. if (is_sub_inst) {
  1460. /* Restore the parent exec_env's module inst */
  1461. wasm_exec_env_restore_module_inst(exec_env_main, module_inst_main);
  1462. }
  1463. else {
  1464. if (module_inst_main)
  1465. /* Restore the existing exec_env's module inst */
  1466. wasm_exec_env_restore_module_inst(exec_env, module_inst_main);
  1467. if (exec_env_created)
  1468. wasm_exec_env_destroy(exec_env_created);
  1469. }
  1470. return ret;
  1471. }
  1472. static bool
  1473. check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
  1474. {
  1475. uint32 i;
  1476. /* init_func_ptrs() will go through import functions */
  1477. for (i = 0; i < module->import_global_count; i++) {
  1478. AOTImportGlobal *global = module->import_globals + i;
  1479. if (!global->is_linked) {
  1480. set_error_buf_v(error_buf, error_buf_size,
  1481. "failed to link import global (%s, %s)",
  1482. global->module_name, global->global_name);
  1483. return false;
  1484. }
  1485. }
  1486. return true;
  1487. }
  1488. AOTModuleInstance *
  1489. aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
  1490. WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size,
  1491. uint32 max_memory_pages, char *error_buf, uint32 error_buf_size)
  1492. {
  1493. AOTModuleInstance *module_inst;
  1494. #if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
  1495. WASMModuleInstanceExtraCommon *common;
  1496. #endif
  1497. AOTModuleInstanceExtra *extra = NULL;
  1498. const uint32 module_inst_struct_size =
  1499. offsetof(AOTModuleInstance, global_table_data.bytes);
  1500. const uint64 module_inst_mem_inst_size =
  1501. (uint64)module->memory_count * sizeof(AOTMemoryInstance);
  1502. uint64 total_size, table_size = 0;
  1503. uint8 *p;
  1504. uint32 i, extra_info_offset;
  1505. const bool is_sub_inst = parent != NULL;
  1506. #if WASM_ENABLE_MULTI_MODULE != 0
  1507. bool ret = false;
  1508. #endif
  1509. /* Check heap size */
  1510. heap_size = align_uint(heap_size, 8);
  1511. if (heap_size > APP_HEAP_SIZE_MAX)
  1512. heap_size = APP_HEAP_SIZE_MAX;
  1513. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1514. + module->global_data_size;
  1515. /*
  1516. * calculate size of table data
  1517. */
  1518. for (i = 0; i != module->import_table_count; ++i) {
  1519. table_size += offsetof(AOTTableInstance, elems);
  1520. table_size += (uint64)sizeof(table_elem_type_t)
  1521. * (uint64)aot_get_imp_tbl_data_slots(
  1522. module->import_tables + i, false);
  1523. }
  1524. for (i = 0; i != module->table_count; ++i) {
  1525. table_size += offsetof(AOTTableInstance, elems);
  1526. table_size +=
  1527. (uint64)sizeof(table_elem_type_t)
  1528. * (uint64)aot_get_tbl_data_slots(module->tables + i, false);
  1529. }
  1530. total_size += table_size;
  1531. /* The offset of AOTModuleInstanceExtra, make it 8-byte aligned */
  1532. total_size = (total_size + 7LL) & ~7LL;
  1533. extra_info_offset = (uint32)total_size;
  1534. total_size += sizeof(AOTModuleInstanceExtra);
  1535. /* Allocate module instance, global data, table data and heap data */
  1536. if (!(module_inst =
  1537. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1538. return NULL;
  1539. }
  1540. module_inst->module_type = Wasm_Module_AoT;
  1541. module_inst->module = (void *)module;
  1542. module_inst->e =
  1543. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1544. extra = (AOTModuleInstanceExtra *)module_inst->e;
  1545. #if WASM_ENABLE_GC != 0
  1546. /* Initialize gc heap first since it may be used when initializing
  1547. globals and others */
  1548. if (!is_sub_inst) {
  1549. uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default();
  1550. if (gc_heap_size < GC_HEAP_SIZE_MIN)
  1551. gc_heap_size = GC_HEAP_SIZE_MIN;
  1552. if (gc_heap_size > GC_HEAP_SIZE_MAX)
  1553. gc_heap_size = GC_HEAP_SIZE_MAX;
  1554. extra->common.gc_heap_pool =
  1555. runtime_malloc(gc_heap_size, error_buf, error_buf_size);
  1556. if (!extra->common.gc_heap_pool)
  1557. goto fail;
  1558. extra->common.gc_heap_handle =
  1559. mem_allocator_create(extra->common.gc_heap_pool, gc_heap_size);
  1560. if (!extra->common.gc_heap_handle)
  1561. goto fail;
  1562. }
  1563. #endif
  1564. #if WASM_ENABLE_MULTI_MODULE != 0
  1565. extra->sub_module_inst_list = &extra->sub_module_inst_list_head;
  1566. /* Allocate memory for import_func_module_insts*/
  1567. if (module->import_func_count > 0
  1568. && !(extra->import_func_module_insts =
  1569. runtime_malloc((uint64)module->import_func_count
  1570. * sizeof(WASMModuleInstanceCommon *),
  1571. error_buf, error_buf_size))) {
  1572. goto fail;
  1573. }
  1574. ret = wasm_runtime_sub_module_instantiate(
  1575. (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
  1576. stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
  1577. if (!ret) {
  1578. LOG_DEBUG("build a sub module list failed");
  1579. goto fail;
  1580. }
  1581. #endif
  1582. /* Initialize function type indexes before initializing global info,
  1583. module_inst->func_type_indexes may be used in the latter */
  1584. if (!init_func_type_indexes(module_inst, module, error_buf, error_buf_size))
  1585. goto fail;
  1586. #if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
  1587. common = &extra->common;
  1588. #endif
  1589. #if WASM_ENABLE_BULK_MEMORY != 0
  1590. if (module->mem_init_data_count > 0) {
  1591. common->data_dropped = bh_bitmap_new(0, module->mem_init_data_count);
  1592. if (common->data_dropped == NULL) {
  1593. LOG_DEBUG("failed to allocate bitmaps");
  1594. set_error_buf(error_buf, error_buf_size,
  1595. "failed to allocate bitmaps");
  1596. goto fail;
  1597. }
  1598. for (i = 0; i < module->mem_init_data_count; i++) {
  1599. if (!module->mem_init_data_list[i]->is_passive)
  1600. bh_bitmap_set_bit(common->data_dropped, i);
  1601. }
  1602. }
  1603. #endif
  1604. #if WASM_ENABLE_REF_TYPES != 0
  1605. if (module->table_init_data_count > 0) {
  1606. common->elem_dropped = bh_bitmap_new(0, module->table_init_data_count);
  1607. if (common->elem_dropped == NULL) {
  1608. LOG_DEBUG("failed to allocate bitmaps");
  1609. set_error_buf(error_buf, error_buf_size,
  1610. "failed to allocate bitmaps");
  1611. goto fail;
  1612. }
  1613. for (i = 0; i < module->table_init_data_count; i++) {
  1614. if (wasm_elem_is_active(module->table_init_data_list[i]->mode))
  1615. bh_bitmap_set_bit(common->elem_dropped, i);
  1616. }
  1617. }
  1618. #endif
  1619. /* Initialize global info */
  1620. p = (uint8 *)module_inst + module_inst_struct_size
  1621. + module_inst_mem_inst_size;
  1622. module_inst->global_data = p;
  1623. module_inst->global_data_size = module->global_data_size;
  1624. if (!global_instantiate(module_inst, module, error_buf, error_buf_size))
  1625. goto fail;
  1626. /* Initialize table info */
  1627. p += module->global_data_size;
  1628. module_inst->table_count = module->table_count + module->import_table_count;
  1629. if (!tables_instantiate(module_inst, module, (AOTTableInstance *)p,
  1630. error_buf, error_buf_size))
  1631. goto fail;
  1632. /* Initialize memory space */
  1633. if (!memories_instantiate(module_inst, parent, module, heap_size,
  1634. max_memory_pages, error_buf, error_buf_size))
  1635. goto fail;
  1636. /* Initialize function pointers */
  1637. if (!init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1638. goto fail;
  1639. if (!check_linked_symbol(module, error_buf, error_buf_size))
  1640. goto fail;
  1641. if (!create_exports(module_inst, module, error_buf, error_buf_size))
  1642. goto fail;
  1643. #if WASM_ENABLE_LIBC_WASI != 0
  1644. if (!is_sub_inst) {
  1645. if (!wasm_runtime_init_wasi(
  1646. (WASMModuleInstanceCommon *)module_inst,
  1647. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1648. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1649. module->wasi_args.env, module->wasi_args.env_count,
  1650. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1651. module->wasi_args.ns_lookup_pool,
  1652. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  1653. module->wasi_args.argc, module->wasi_args.stdio[0],
  1654. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  1655. error_buf, error_buf_size))
  1656. goto fail;
  1657. }
  1658. #endif
  1659. /* Initialize the thread related data */
  1660. if (stack_size == 0)
  1661. stack_size = DEFAULT_WASM_STACK_SIZE;
  1662. module_inst->default_wasm_stack_size = stack_size;
  1663. extra->stack_sizes =
  1664. aot_get_data_section_addr(module, AOT_STACK_SIZES_SECTION_NAME, NULL);
  1665. #if WASM_ENABLE_PERF_PROFILING != 0
  1666. total_size = sizeof(AOTFuncPerfProfInfo)
  1667. * ((uint64)module->import_func_count + module->func_count);
  1668. if (!(module_inst->func_perf_profilings =
  1669. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1670. goto fail;
  1671. }
  1672. #endif
  1673. #if WASM_ENABLE_GC != 0
  1674. for (i = 0; i < module_inst->table_count; i++) {
  1675. uint32 j;
  1676. AOTTable *table;
  1677. AOTTableInstance *table_inst;
  1678. table_elem_type_t *table_data;
  1679. table = &module->tables[i];
  1680. bh_assert(table);
  1681. if (table->init_expr.init_expr_type == INIT_EXPR_NONE) {
  1682. continue;
  1683. }
  1684. table_inst = module_inst->tables[i];
  1685. bh_assert(table_inst);
  1686. table_data = table_inst->elems;
  1687. bh_assert(table_data);
  1688. for (j = 0; j < table_inst->cur_size; j++) {
  1689. if (!assign_table_init_value(module_inst, module, &table->init_expr,
  1690. table_data + j, error_buf,
  1691. error_buf_size)) {
  1692. goto fail;
  1693. }
  1694. }
  1695. }
  1696. /* Initialize the table data with table init data */
  1697. for (i = 0;
  1698. module_inst->table_count > 0 && i < module->table_init_data_count;
  1699. i++) {
  1700. AOTTableInitData *table_init_data = module->table_init_data_list[i];
  1701. AOTTableInstance *table;
  1702. table_elem_type_t *table_data;
  1703. uint8 tbl_elem_type;
  1704. uint32 tbl_init_size, tbl_max_size, j;
  1705. WASMRefType *tbl_elem_ref_type;
  1706. bh_assert(table_init_data);
  1707. bh_assert(table_init_data->table_index < module_inst->table_count);
  1708. table = module_inst->tables[table_init_data->table_index];
  1709. bh_assert(table);
  1710. table_data = table->elems;
  1711. bh_assert(table_data);
  1712. wasm_runtime_get_table_inst_elem_type(
  1713. (WASMModuleInstanceCommon *)module_inst,
  1714. table_init_data->table_index, &tbl_elem_type, &tbl_elem_ref_type,
  1715. &tbl_init_size, &tbl_max_size);
  1716. if (!wasm_elem_is_declarative(table_init_data->mode)
  1717. && !wasm_reftype_is_subtype_of(
  1718. table_init_data->elem_type, table_init_data->elem_ref_type,
  1719. table->elem_type, table->elem_ref_type.elem_ref_type,
  1720. module->types, module->type_count)) {
  1721. set_error_buf(error_buf, error_buf_size,
  1722. "type mismatch: elements segment does not fit");
  1723. goto fail;
  1724. }
  1725. (void)tbl_init_size;
  1726. (void)tbl_max_size;
  1727. if (!wasm_elem_is_active(table_init_data->mode)) {
  1728. continue;
  1729. }
  1730. bh_assert(table_init_data->offset.init_expr_type
  1731. == INIT_EXPR_TYPE_I32_CONST
  1732. || table_init_data->offset.init_expr_type
  1733. == INIT_EXPR_TYPE_GET_GLOBAL
  1734. || table_init_data->offset.init_expr_type
  1735. == INIT_EXPR_TYPE_FUNCREF_CONST
  1736. || table_init_data->offset.init_expr_type
  1737. == INIT_EXPR_TYPE_REFNULL_CONST);
  1738. /* init vec(funcidx) or vec(expr) */
  1739. if (table_init_data->offset.init_expr_type
  1740. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1741. uint32 data_offset;
  1742. if (!check_global_init_expr(module,
  1743. table_init_data->offset.u.global_index,
  1744. error_buf, error_buf_size)) {
  1745. goto fail;
  1746. }
  1747. if (table_init_data->offset.u.global_index
  1748. < module->import_global_count) {
  1749. data_offset =
  1750. module
  1751. ->import_globals[table_init_data->offset.u.global_index]
  1752. .data_offset;
  1753. }
  1754. else {
  1755. data_offset =
  1756. module
  1757. ->globals[table_init_data->offset.u.global_index
  1758. - module->import_global_count]
  1759. .data_offset;
  1760. }
  1761. table_init_data->offset.u.i32 =
  1762. *(uint32 *)(module_inst->global_data + data_offset);
  1763. }
  1764. /* check offset since length might negative */
  1765. if ((uint32)table_init_data->offset.u.i32 > table->cur_size) {
  1766. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1767. table_init_data->offset.u.i32, table->cur_size);
  1768. set_error_buf(error_buf, error_buf_size,
  1769. "out of bounds table access");
  1770. goto fail;
  1771. }
  1772. if ((uint32)table_init_data->offset.u.i32 + table_init_data->value_count
  1773. > table->cur_size) {
  1774. LOG_DEBUG("base_offset(%d) + length(%d) > table->cur_size(%d)",
  1775. table_init_data->offset.u.i32,
  1776. table_init_data->value_count, table->cur_size);
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "out of bounds table access");
  1779. goto fail;
  1780. }
  1781. for (j = 0; j < module->table_init_data_list[i]->value_count; j++) {
  1782. if (!assign_table_init_value(
  1783. module_inst, module, &table_init_data->init_values[j],
  1784. table_data + table_init_data->offset.u.i32 + j, error_buf,
  1785. error_buf_size)) {
  1786. goto fail;
  1787. }
  1788. }
  1789. }
  1790. #endif
  1791. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1792. if (!(module_inst->frames =
  1793. runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) {
  1794. goto fail;
  1795. }
  1796. #endif
  1797. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  1798. exec_env_main)) {
  1799. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1800. goto fail;
  1801. }
  1802. #if WASM_ENABLE_MEMORY_TRACING != 0
  1803. wasm_runtime_dump_module_inst_mem_consumption(
  1804. (WASMModuleInstanceCommon *)module_inst);
  1805. #endif
  1806. return module_inst;
  1807. fail:
  1808. aot_deinstantiate(module_inst, is_sub_inst);
  1809. return NULL;
  1810. }
  1811. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1812. static void
  1813. destroy_c_api_frames(Vector *frames)
  1814. {
  1815. WASMCApiFrame frame = { 0 };
  1816. uint32 i, total_frames, ret;
  1817. total_frames = (uint32)bh_vector_size(frames);
  1818. for (i = 0; i < total_frames; i++) {
  1819. ret = bh_vector_get(frames, i, &frame);
  1820. bh_assert(ret);
  1821. if (frame.lp)
  1822. wasm_runtime_free(frame.lp);
  1823. }
  1824. ret = bh_vector_destroy(frames);
  1825. bh_assert(ret);
  1826. (void)ret;
  1827. }
  1828. #endif
  1829. void
  1830. aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
  1831. {
  1832. AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
  1833. WASMModuleInstanceExtraCommon *common = &extra->common;
  1834. if (module_inst->exec_env_singleton) {
  1835. /* wasm_exec_env_destroy will call
  1836. wasm_cluster_wait_for_all_except_self to wait for other
  1837. threads, so as to destroy their exec_envs and module
  1838. instances first, and avoid accessing the shared resources
  1839. of current module instance after it is deinstantiated. */
  1840. wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton);
  1841. }
  1842. #if WASM_ENABLE_PERF_PROFILING != 0
  1843. if (module_inst->func_perf_profilings)
  1844. wasm_runtime_free(module_inst->func_perf_profilings);
  1845. #endif
  1846. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1847. if (module_inst->frames) {
  1848. destroy_c_api_frames(module_inst->frames);
  1849. wasm_runtime_free(module_inst->frames);
  1850. module_inst->frames = NULL;
  1851. }
  1852. #endif
  1853. #if WASM_ENABLE_MULTI_MODULE != 0
  1854. wasm_runtime_sub_module_deinstantiate(
  1855. (WASMModuleInstanceCommon *)module_inst);
  1856. if (extra->import_func_module_insts)
  1857. wasm_runtime_free(extra->import_func_module_insts);
  1858. #endif
  1859. if (module_inst->tables)
  1860. wasm_runtime_free(module_inst->tables);
  1861. if (module_inst->memories)
  1862. memories_deinstantiate(module_inst);
  1863. if (module_inst->export_functions)
  1864. wasm_runtime_free(module_inst->export_functions);
  1865. #if WASM_ENABLE_MULTI_MEMORY != 0
  1866. if (module_inst->export_memories)
  1867. wasm_runtime_free(module_inst->export_memories);
  1868. #endif
  1869. if (extra->functions) {
  1870. uint32 func_idx;
  1871. for (func_idx = 0; func_idx < extra->function_count; ++func_idx) {
  1872. if (extra->functions[func_idx]) {
  1873. wasm_runtime_free(extra->functions[func_idx]);
  1874. }
  1875. }
  1876. wasm_runtime_free(extra->functions);
  1877. }
  1878. if (module_inst->func_ptrs)
  1879. wasm_runtime_free(module_inst->func_ptrs);
  1880. if (module_inst->func_type_indexes)
  1881. wasm_runtime_free(module_inst->func_type_indexes);
  1882. if (module_inst->c_api_func_imports)
  1883. wasm_runtime_free(module_inst->c_api_func_imports);
  1884. #if WASM_ENABLE_GC != 0
  1885. if (!is_sub_inst) {
  1886. if (common->gc_heap_handle)
  1887. mem_allocator_destroy(common->gc_heap_handle);
  1888. if (common->gc_heap_pool)
  1889. wasm_runtime_free(common->gc_heap_pool);
  1890. }
  1891. #endif
  1892. if (!is_sub_inst) {
  1893. wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
  1894. }
  1895. #if WASM_ENABLE_BULK_MEMORY != 0
  1896. bh_bitmap_delete(common->data_dropped);
  1897. #endif
  1898. #if WASM_ENABLE_REF_TYPES != 0
  1899. bh_bitmap_delete(common->elem_dropped);
  1900. #endif
  1901. wasm_runtime_free(module_inst);
  1902. }
  1903. AOTFunctionInstance *
  1904. aot_lookup_function(const AOTModuleInstance *module_inst, const char *name)
  1905. {
  1906. uint32 i;
  1907. AOTFunctionInstance *export_funcs =
  1908. (AOTFunctionInstance *)module_inst->export_functions;
  1909. for (i = 0; i < module_inst->export_func_count; i++)
  1910. if (!strcmp(export_funcs[i].func_name, name))
  1911. return &export_funcs[i];
  1912. return NULL;
  1913. }
  1914. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1915. static bool
  1916. invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
  1917. const WASMFuncType *func_type,
  1918. const char *signature, void *attachment,
  1919. uint32 *argv, uint32 argc, uint32 *argv_ret)
  1920. {
  1921. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  1922. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1923. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  1924. #ifdef BH_PLATFORM_WINDOWS
  1925. int result;
  1926. bool has_exception;
  1927. char exception[EXCEPTION_BUF_LEN];
  1928. #endif
  1929. bool ret;
  1930. /* Check native stack overflow firstly to ensure we have enough
  1931. native stack to run the following codes before actually calling
  1932. the aot function in invokeNative function. */
  1933. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1934. return false;
  1935. }
  1936. if (!exec_env_tls) {
  1937. if (!os_thread_signal_inited()) {
  1938. aot_set_exception(module_inst, "thread signal env not inited");
  1939. return false;
  1940. }
  1941. /* Set thread handle and stack boundary if they haven't been set */
  1942. wasm_exec_env_set_thread_info(exec_env);
  1943. wasm_runtime_set_exec_env_tls(exec_env);
  1944. }
  1945. else {
  1946. if (exec_env_tls != exec_env) {
  1947. aot_set_exception(module_inst, "invalid exec env");
  1948. return false;
  1949. }
  1950. }
  1951. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  1952. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  1953. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1954. /* Quick call if the quick aot entry is registered */
  1955. if (!signature && func_type->quick_aot_entry) {
  1956. void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
  1957. uint32 *argv_ret) =
  1958. func_type->quick_aot_entry;
  1959. exec_env->attachment = attachment;
  1960. invoke_native(func_ptr, exec_env, argv, argv_ret);
  1961. exec_env->attachment = NULL;
  1962. ret = !aot_copy_exception(module_inst, NULL);
  1963. }
  1964. else
  1965. #endif
  1966. {
  1967. ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type,
  1968. signature, attachment, argv, argc,
  1969. argv_ret);
  1970. }
  1971. #ifdef BH_PLATFORM_WINDOWS
  1972. has_exception = aot_copy_exception(module_inst, exception);
  1973. if (has_exception && strstr(exception, "native stack overflow")) {
  1974. /* After a stack overflow, the stack was left
  1975. in a damaged state, let the CRT repair it */
  1976. result = _resetstkoflw();
  1977. bh_assert(result != 0);
  1978. }
  1979. #endif
  1980. }
  1981. else {
  1982. /* Exception has been set in signal handler before calling longjmp */
  1983. ret = false;
  1984. }
  1985. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  1986. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  1987. if (!exec_env->jmpbuf_stack_top) {
  1988. wasm_runtime_set_exec_env_tls(NULL);
  1989. }
  1990. if (!ret) {
  1991. os_sigreturn();
  1992. os_signal_unmask();
  1993. }
  1994. (void)jmpbuf_node_pop;
  1995. return ret;
  1996. }
  1997. #define invoke_native_internal invoke_native_with_hw_bound_check /* NOLINT */
  1998. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  1999. static inline bool
  2000. invoke_native_internal(WASMExecEnv *exec_env, void *func_ptr,
  2001. const WASMFuncType *func_type, const char *signature,
  2002. void *attachment, uint32 *argv, uint32 argc,
  2003. uint32 *argv_ret)
  2004. {
  2005. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  2006. /* Quick call if the quick aot entry is registered */
  2007. if (!signature && func_type->quick_aot_entry) {
  2008. AOTModuleInstance *module_inst =
  2009. (AOTModuleInstance *)exec_env->module_inst;
  2010. void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
  2011. uint32 *argv_ret) = func_type->quick_aot_entry;
  2012. invoke_native(func_ptr, exec_env, argv, argv_ret);
  2013. return !aot_copy_exception(module_inst, NULL);
  2014. }
  2015. #endif
  2016. return wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2017. attachment, argv, argc, argv_ret);
  2018. }
  2019. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  2020. #ifdef AOT_STACK_FRAME_DEBUG
  2021. typedef void (*stack_frame_callback_t)(struct WASMExecEnv *exec_env);
  2022. static stack_frame_callback_t aot_stack_frame_callback;
  2023. /* set the callback, only for debug purpose */
  2024. void
  2025. aot_set_stack_frame_callback(stack_frame_callback_t callback)
  2026. {
  2027. aot_stack_frame_callback = callback;
  2028. }
  2029. #endif
  2030. bool
  2031. aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
  2032. unsigned argc, uint32 argv[])
  2033. {
  2034. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  2035. AOTModule *module = (AOTModule *)module_inst->module;
  2036. AOTFuncType *func_type = function->is_import_func
  2037. ? function->u.func_import->func_type
  2038. : function->u.func.func_type;
  2039. uint32 result_count = func_type->result_count;
  2040. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2041. bool ret;
  2042. void *func_ptr = function->is_import_func
  2043. ? function->u.func_import->func_ptr_linked
  2044. : function->u.func.func_ptr;
  2045. void *attachment = NULL;
  2046. #if WASM_ENABLE_MULTI_MODULE != 0
  2047. bh_list *sub_module_list_node = NULL;
  2048. const char *sub_inst_name = NULL;
  2049. const char *func_name = function->u.func_import->module_name;
  2050. if (function->is_import_func) {
  2051. sub_module_list_node =
  2052. ((AOTModuleInstanceExtra *)module_inst->e)->sub_module_inst_list;
  2053. sub_module_list_node = bh_list_first_elem(sub_module_list_node);
  2054. while (sub_module_list_node) {
  2055. sub_inst_name =
  2056. ((AOTSubModInstNode *)sub_module_list_node)->module_name;
  2057. if (strcmp(sub_inst_name, func_name) == 0) {
  2058. exec_env = wasm_runtime_get_exec_env_singleton(
  2059. (WASMModuleInstanceCommon *)((AOTSubModInstNode *)
  2060. sub_module_list_node)
  2061. ->module_inst);
  2062. module_inst = (AOTModuleInstance *)exec_env->module_inst;
  2063. break;
  2064. }
  2065. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  2066. }
  2067. if (exec_env == NULL) {
  2068. wasm_runtime_set_exception((WASMModuleInstanceCommon *)module_inst,
  2069. "create singleton exec_env failed");
  2070. return false;
  2071. }
  2072. }
  2073. #endif
  2074. if (argc < func_type->param_cell_num) {
  2075. char buf[108];
  2076. snprintf(buf, sizeof(buf),
  2077. "invalid argument count %u, must be no smaller than %u", argc,
  2078. func_type->param_cell_num);
  2079. aot_set_exception(module_inst, buf);
  2080. return false;
  2081. }
  2082. argc = func_type->param_cell_num;
  2083. #if defined(os_writegsbase)
  2084. {
  2085. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  2086. if (memory_inst)
  2087. /* write base addr of linear memory to GS segment register */
  2088. os_writegsbase(memory_inst->memory_data);
  2089. }
  2090. #endif
  2091. /* func pointer was looked up previously */
  2092. bh_assert(func_ptr != NULL);
  2093. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2094. /* Set thread handle and stack boundary */
  2095. wasm_exec_env_set_thread_info(exec_env);
  2096. #else
  2097. /* Set thread info in invoke_native_with_hw_bound_check when
  2098. hw bound check is enabled */
  2099. #endif
  2100. if (function->func_index < module->import_func_count) {
  2101. attachment = function->u.func_import->attachment;
  2102. }
  2103. /* Set exec env, so it can be later retrieved from instance */
  2104. module_inst->cur_exec_env = exec_env;
  2105. if (ext_ret_count > 0) {
  2106. uint32 cell_num = 0, i;
  2107. uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;
  2108. uint32 argv1_buf[32], *argv1 = argv1_buf, *ext_rets = NULL;
  2109. uint32 *argv_ret = argv;
  2110. uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  2111. uint64 size;
  2112. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2113. void *prev_frame = get_top_frame(exec_env);
  2114. #endif
  2115. /* Allocate memory all arguments */
  2116. size =
  2117. sizeof(uint32) * (uint64)argc /* original arguments */
  2118. + sizeof(void *)
  2119. * (uint64)ext_ret_count /* extra result values' addr */
  2120. + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */
  2121. if (size > sizeof(argv1_buf)
  2122. && !(argv1 = runtime_malloc(size, module_inst->cur_exception,
  2123. sizeof(module_inst->cur_exception)))) {
  2124. aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY);
  2125. return false;
  2126. }
  2127. /* Copy original arguments */
  2128. bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc);
  2129. /* Get the extra result value's address */
  2130. ext_rets =
  2131. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  2132. /* Append each extra result value's address to original arguments */
  2133. for (i = 0; i < ext_ret_count; i++) {
  2134. *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) =
  2135. (uintptr_t)(ext_rets + cell_num);
  2136. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  2137. }
  2138. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2139. if (!is_frame_per_function(exec_env)
  2140. && !aot_alloc_frame(exec_env, function->func_index)) {
  2141. if (argv1 != argv1_buf)
  2142. wasm_runtime_free(argv1);
  2143. return false;
  2144. }
  2145. #endif
  2146. ret = invoke_native_internal(exec_env, function->u.func.func_ptr,
  2147. func_type, NULL, attachment, argv1, argc,
  2148. argv);
  2149. if (!ret) {
  2150. #ifdef AOT_STACK_FRAME_DEBUG
  2151. if (aot_stack_frame_callback) {
  2152. aot_stack_frame_callback(exec_env);
  2153. }
  2154. #endif
  2155. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2156. if (aot_create_call_stack(exec_env)) {
  2157. aot_dump_call_stack(exec_env, true, NULL, 0);
  2158. }
  2159. #endif
  2160. }
  2161. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2162. /* Free all frames allocated, note that some frames
  2163. may be allocated in AOT code and haven't been
  2164. freed if exception occurred */
  2165. while (get_top_frame(exec_env) != prev_frame)
  2166. aot_free_frame(exec_env);
  2167. #endif
  2168. if (!ret) {
  2169. if (argv1 != argv1_buf)
  2170. wasm_runtime_free(argv1);
  2171. return ret;
  2172. }
  2173. /* Get extra result values */
  2174. switch (func_type->types[func_type->param_count]) {
  2175. case VALUE_TYPE_I32:
  2176. case VALUE_TYPE_F32:
  2177. #if WASM_ENABLE_REF_TYPES != 0
  2178. case VALUE_TYPE_FUNCREF:
  2179. case VALUE_TYPE_EXTERNREF:
  2180. #endif
  2181. argv_ret++;
  2182. break;
  2183. case VALUE_TYPE_I64:
  2184. case VALUE_TYPE_F64:
  2185. argv_ret += 2;
  2186. break;
  2187. #if WASM_ENABLE_SIMD != 0
  2188. case VALUE_TYPE_V128:
  2189. argv_ret += 4;
  2190. break;
  2191. #endif
  2192. default:
  2193. bh_assert(0);
  2194. break;
  2195. }
  2196. ext_rets =
  2197. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  2198. bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
  2199. sizeof(uint32) * cell_num);
  2200. if (argv1 != argv1_buf)
  2201. wasm_runtime_free(argv1);
  2202. return true;
  2203. }
  2204. else {
  2205. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2206. void *prev_frame = get_top_frame(exec_env);
  2207. /* Only allocate frame for frame-per-call mode; in the
  2208. frame-per-function mode the frame is allocated at the
  2209. beginning of the function. */
  2210. if (!is_frame_per_function(exec_env)
  2211. && !aot_alloc_frame(exec_env, function->func_index)) {
  2212. return false;
  2213. }
  2214. #endif
  2215. ret = invoke_native_internal(exec_env, func_ptr, func_type, NULL,
  2216. attachment, argv, argc, argv);
  2217. if (aot_copy_exception(module_inst, NULL)) {
  2218. #ifdef AOT_STACK_FRAME_DEBUG
  2219. if (aot_stack_frame_callback) {
  2220. aot_stack_frame_callback(exec_env);
  2221. }
  2222. #endif
  2223. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2224. if (aot_create_call_stack(exec_env)) {
  2225. aot_dump_call_stack(exec_env, true, NULL, 0);
  2226. }
  2227. #endif
  2228. }
  2229. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2230. /* Free all frames allocated, note that some frames
  2231. may be allocated in AOT code and haven't been
  2232. freed if exception occurred */
  2233. while (get_top_frame(exec_env) != prev_frame)
  2234. aot_free_frame(exec_env);
  2235. #endif
  2236. return ret && !aot_copy_exception(module_inst, NULL) ? true : false;
  2237. }
  2238. }
  2239. void
  2240. aot_set_exception(AOTModuleInstance *module_inst, const char *exception)
  2241. {
  2242. wasm_set_exception(module_inst, exception);
  2243. }
  2244. void
  2245. aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id)
  2246. {
  2247. if (id != EXCE_ALREADY_THROWN)
  2248. wasm_set_exception_with_id(module_inst, id);
  2249. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2250. wasm_runtime_access_exce_check_guard_page();
  2251. #endif
  2252. }
  2253. const char *
  2254. aot_get_exception(AOTModuleInstance *module_inst)
  2255. {
  2256. return wasm_get_exception(module_inst);
  2257. }
  2258. bool
  2259. aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf)
  2260. {
  2261. /* The field offsets of cur_exception in AOTModuleInstance and
  2262. WASMModuleInstance are the same */
  2263. return wasm_copy_exception(module_inst, exception_buf);
  2264. }
  2265. static bool
  2266. execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
  2267. AOTFunctionInstance *malloc_func,
  2268. AOTFunctionInstance *retain_func, uint64 size,
  2269. uint64 *p_result)
  2270. {
  2271. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2272. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2273. #endif
  2274. WASMExecEnv *exec_env_created = NULL;
  2275. WASMModuleInstanceCommon *module_inst_old = NULL;
  2276. union {
  2277. uint32 u32[3];
  2278. uint64 u64;
  2279. } argv;
  2280. uint32 argc;
  2281. bool ret;
  2282. #if WASM_ENABLE_MEMORY64 != 0
  2283. bool is_memory64 = module_inst->memories[0]->is_memory64;
  2284. if (is_memory64) {
  2285. argc = 2;
  2286. PUT_I64_TO_ADDR(&argv.u64, size);
  2287. }
  2288. else
  2289. #endif
  2290. {
  2291. argc = 1;
  2292. argv.u32[0] = (uint32)size;
  2293. }
  2294. /* if __retain is exported, then this module is compiled by
  2295. assemblyscript, the memory should be managed by as's runtime,
  2296. in this case we need to call the retain function after malloc
  2297. the memory */
  2298. if (retain_func) {
  2299. /* the malloc function from assemblyscript is:
  2300. function __new(size: usize, id: u32)
  2301. id = 0 means this is an ArrayBuffer object */
  2302. argv.u32[argc] = 0;
  2303. argc++;
  2304. }
  2305. if (exec_env) {
  2306. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2307. if (exec_env_tls) {
  2308. bh_assert(exec_env_tls == exec_env);
  2309. }
  2310. #endif
  2311. bh_assert(exec_env->module_inst
  2312. == (WASMModuleInstanceCommon *)module_inst);
  2313. }
  2314. else {
  2315. /* Try using the existing exec_env */
  2316. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2317. exec_env = exec_env_tls;
  2318. #endif
  2319. #if WASM_ENABLE_THREAD_MGR != 0
  2320. if (!exec_env)
  2321. exec_env = wasm_clusters_search_exec_env(
  2322. (WASMModuleInstanceCommon *)module_inst);
  2323. #endif
  2324. if (!exec_env) {
  2325. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  2326. (WASMModuleInstanceCommon *)module_inst,
  2327. module_inst->default_wasm_stack_size))) {
  2328. wasm_set_exception(module_inst, "allocate memory failed");
  2329. return false;
  2330. }
  2331. }
  2332. else {
  2333. /* Temporarily replace exec_env's module inst with current
  2334. module inst to ensure that the exec_env's module inst
  2335. is the correct one. */
  2336. module_inst_old = exec_env->module_inst;
  2337. wasm_exec_env_set_module_inst(
  2338. exec_env, (WASMModuleInstanceCommon *)module_inst);
  2339. }
  2340. }
  2341. ret = aot_call_function(exec_env, malloc_func, argc, argv.u32);
  2342. if (retain_func && ret)
  2343. ret = aot_call_function(exec_env, retain_func, 1, argv.u32);
  2344. if (module_inst_old)
  2345. /* Restore the existing exec_env's module inst */
  2346. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  2347. if (exec_env_created)
  2348. wasm_exec_env_destroy(exec_env_created);
  2349. if (ret) {
  2350. #if WASM_ENABLE_MEMORY64 != 0
  2351. if (is_memory64)
  2352. *p_result = argv.u64;
  2353. else
  2354. #endif
  2355. {
  2356. *p_result = argv.u32[0];
  2357. }
  2358. }
  2359. return ret;
  2360. }
  2361. static bool
  2362. execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
  2363. AOTFunctionInstance *free_func, uint64 offset)
  2364. {
  2365. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2366. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2367. #endif
  2368. WASMExecEnv *exec_env_created = NULL;
  2369. WASMModuleInstanceCommon *module_inst_old = NULL;
  2370. union {
  2371. uint32 u32[2];
  2372. uint64 u64;
  2373. } argv;
  2374. uint32 argc;
  2375. bool ret;
  2376. #if WASM_ENABLE_MEMORY64 != 0
  2377. if (module_inst->memories[0]->is_memory64) {
  2378. PUT_I64_TO_ADDR(&argv.u64, offset);
  2379. argc = 2;
  2380. }
  2381. else
  2382. #endif
  2383. {
  2384. argv.u32[0] = (uint32)offset;
  2385. argc = 1;
  2386. }
  2387. if (exec_env) {
  2388. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2389. if (exec_env_tls) {
  2390. bh_assert(exec_env_tls == exec_env);
  2391. }
  2392. #endif
  2393. bh_assert(exec_env->module_inst
  2394. == (WASMModuleInstanceCommon *)module_inst);
  2395. }
  2396. else {
  2397. /* Try using the existing exec_env */
  2398. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2399. exec_env = exec_env_tls;
  2400. #endif
  2401. #if WASM_ENABLE_THREAD_MGR != 0
  2402. if (!exec_env)
  2403. exec_env = wasm_clusters_search_exec_env(
  2404. (WASMModuleInstanceCommon *)module_inst);
  2405. #endif
  2406. if (!exec_env) {
  2407. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  2408. (WASMModuleInstanceCommon *)module_inst,
  2409. module_inst->default_wasm_stack_size))) {
  2410. wasm_set_exception(module_inst, "allocate memory failed");
  2411. return false;
  2412. }
  2413. }
  2414. else {
  2415. /* Temporarily replace exec_env's module inst with current
  2416. module inst to ensure that the exec_env's module inst
  2417. is the correct one. */
  2418. module_inst_old = exec_env->module_inst;
  2419. wasm_exec_env_set_module_inst(
  2420. exec_env, (WASMModuleInstanceCommon *)module_inst);
  2421. }
  2422. }
  2423. ret = aot_call_function(exec_env, free_func, argc, argv.u32);
  2424. if (module_inst_old)
  2425. /* Restore the existing exec_env's module inst */
  2426. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  2427. if (exec_env_created)
  2428. wasm_exec_env_destroy(exec_env_created);
  2429. return ret;
  2430. }
  2431. uint64
  2432. aot_module_malloc_internal(AOTModuleInstance *module_inst,
  2433. WASMExecEnv *exec_env, uint64 size,
  2434. void **p_native_addr)
  2435. {
  2436. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  2437. AOTModule *module = (AOTModule *)module_inst->module;
  2438. uint8 *addr = NULL;
  2439. uint64 offset = 0;
  2440. /* TODO: Memory64 size check based on memory idx type */
  2441. bh_assert(size <= UINT32_MAX);
  2442. if (!memory_inst) {
  2443. aot_set_exception(module_inst, "uninitialized memory");
  2444. return 0;
  2445. }
  2446. if (memory_inst->heap_handle) {
  2447. addr = mem_allocator_malloc(memory_inst->heap_handle, (uint32)size);
  2448. }
  2449. else if (module->malloc_func_index != (uint32)-1
  2450. && module->free_func_index != (uint32)-1) {
  2451. AOTFunctionInstance *malloc_func, *retain_func = NULL;
  2452. char *malloc_func_name;
  2453. if (module->retain_func_index != (uint32)-1) {
  2454. malloc_func_name = "__new";
  2455. retain_func = aot_lookup_function(module_inst, "__retain");
  2456. if (!retain_func)
  2457. retain_func = aot_lookup_function(module_inst, "__pin");
  2458. bh_assert(retain_func);
  2459. }
  2460. else {
  2461. malloc_func_name = "malloc";
  2462. }
  2463. malloc_func = aot_lookup_function(module_inst, malloc_func_name);
  2464. if (!malloc_func
  2465. || !execute_malloc_function(module_inst, exec_env, malloc_func,
  2466. retain_func, size, &offset)) {
  2467. return 0;
  2468. }
  2469. addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL;
  2470. }
  2471. if (!addr) {
  2472. if (memory_inst->heap_handle
  2473. && mem_allocator_is_heap_corrupted(memory_inst->heap_handle)) {
  2474. wasm_runtime_show_app_heap_corrupted_prompt();
  2475. aot_set_exception(module_inst, "app heap corrupted");
  2476. }
  2477. else {
  2478. LOG_WARNING("warning: allocate %" PRIu64 " bytes memory failed",
  2479. size);
  2480. }
  2481. return 0;
  2482. }
  2483. if (p_native_addr)
  2484. *p_native_addr = addr;
  2485. return (uint64)(addr - memory_inst->memory_data);
  2486. }
  2487. uint64
  2488. aot_module_realloc_internal(AOTModuleInstance *module_inst,
  2489. WASMExecEnv *exec_env, uint64 ptr, uint64 size,
  2490. void **p_native_addr)
  2491. {
  2492. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  2493. uint8 *addr = NULL;
  2494. /* TODO: Memory64 ptr and size check based on memory idx type */
  2495. bh_assert(ptr <= UINT32_MAX);
  2496. bh_assert(size <= UINT32_MAX);
  2497. if (!memory_inst) {
  2498. aot_set_exception(module_inst, "uninitialized memory");
  2499. return 0;
  2500. }
  2501. if (memory_inst->heap_handle) {
  2502. addr = mem_allocator_realloc(
  2503. memory_inst->heap_handle,
  2504. (uint32)ptr ? memory_inst->memory_data + (uint32)ptr : NULL,
  2505. (uint32)size);
  2506. }
  2507. /* Only support realloc in WAMR's app heap */
  2508. (void)exec_env;
  2509. if (!addr) {
  2510. if (memory_inst->heap_handle
  2511. && mem_allocator_is_heap_corrupted(memory_inst->heap_handle)) {
  2512. aot_set_exception(module_inst, "app heap corrupted");
  2513. }
  2514. else {
  2515. aot_set_exception(module_inst, "out of memory");
  2516. }
  2517. return 0;
  2518. }
  2519. if (p_native_addr)
  2520. *p_native_addr = addr;
  2521. return (uint64)(addr - memory_inst->memory_data);
  2522. }
  2523. void
  2524. aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
  2525. uint64 ptr)
  2526. {
  2527. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  2528. AOTModule *module = (AOTModule *)module_inst->module;
  2529. if (!memory_inst) {
  2530. return;
  2531. }
  2532. /* TODO: Memory64 ptr and size check based on memory idx type */
  2533. bh_assert(ptr <= UINT32_MAX);
  2534. if (ptr) {
  2535. uint8 *addr = memory_inst->memory_data + (uint32)ptr;
  2536. uint8 *memory_data_end;
  2537. /* memory->memory_data_end may be changed in memory grow */
  2538. SHARED_MEMORY_LOCK(memory_inst);
  2539. memory_data_end = memory_inst->memory_data_end;
  2540. SHARED_MEMORY_UNLOCK(memory_inst);
  2541. if (memory_inst->heap_handle && memory_inst->heap_data < addr
  2542. && addr < memory_inst->heap_data_end) {
  2543. mem_allocator_free(memory_inst->heap_handle, addr);
  2544. }
  2545. else if (module->malloc_func_index != (uint32)-1
  2546. && module->free_func_index != (uint32)-1
  2547. && memory_inst->memory_data <= addr
  2548. && addr < memory_data_end) {
  2549. AOTFunctionInstance *free_func;
  2550. char *free_func_name;
  2551. if (module->retain_func_index != (uint32)-1) {
  2552. free_func_name = "__release";
  2553. }
  2554. else {
  2555. free_func_name = "free";
  2556. }
  2557. free_func = aot_lookup_function(module_inst, free_func_name);
  2558. if (!free_func && module->retain_func_index != (uint32)-1)
  2559. free_func = aot_lookup_function(module_inst, "__unpin");
  2560. if (free_func)
  2561. execute_free_function(module_inst, exec_env, free_func, ptr);
  2562. }
  2563. }
  2564. }
  2565. uint64
  2566. aot_module_malloc(AOTModuleInstance *module_inst, uint64 size,
  2567. void **p_native_addr)
  2568. {
  2569. return aot_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  2570. }
  2571. uint64
  2572. aot_module_realloc(AOTModuleInstance *module_inst, uint64 ptr, uint64 size,
  2573. void **p_native_addr)
  2574. {
  2575. return aot_module_realloc_internal(module_inst, NULL, ptr, size,
  2576. p_native_addr);
  2577. }
  2578. void
  2579. aot_module_free(AOTModuleInstance *module_inst, uint64 ptr)
  2580. {
  2581. aot_module_free_internal(module_inst, NULL, ptr);
  2582. }
  2583. uint64
  2584. aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
  2585. uint64 size)
  2586. {
  2587. char *buffer;
  2588. uint64 buffer_offset;
  2589. /* TODO: Memory64 size check based on memory idx type */
  2590. bh_assert(size <= UINT32_MAX);
  2591. buffer_offset = aot_module_malloc(module_inst, size, (void **)&buffer);
  2592. if (buffer_offset != 0) {
  2593. buffer = wasm_runtime_addr_app_to_native(
  2594. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2595. bh_memcpy_s(buffer, (uint32)size, src, (uint32)size);
  2596. }
  2597. return buffer_offset;
  2598. }
  2599. bool
  2600. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
  2601. {
  2602. return wasm_enlarge_memory(module_inst, inc_page_count);
  2603. }
  2604. bool
  2605. aot_enlarge_memory_with_idx(AOTModuleInstance *module_inst,
  2606. uint32 inc_page_count, uint32 memidx)
  2607. {
  2608. return wasm_enlarge_memory_with_idx(module_inst, inc_page_count, memidx);
  2609. }
  2610. bool
  2611. aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  2612. uint32 *argv)
  2613. {
  2614. AOTModuleInstance *module_inst =
  2615. (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2616. AOTModule *aot_module = (AOTModule *)module_inst->module;
  2617. CApiFuncImport *c_api_func_import =
  2618. module_inst->c_api_func_imports
  2619. ? module_inst->c_api_func_imports + func_idx
  2620. : NULL;
  2621. uint32 *func_type_indexes = module_inst->func_type_indexes;
  2622. uint32 func_type_idx = func_type_indexes[func_idx];
  2623. AOTFuncType *func_type = (AOTFuncType *)aot_module->types[func_type_idx];
  2624. void **func_ptrs = module_inst->func_ptrs;
  2625. void *func_ptr = func_ptrs[func_idx];
  2626. AOTImportFunc *import_func;
  2627. const char *signature;
  2628. void *attachment;
  2629. char buf[96];
  2630. bool ret = false;
  2631. bh_assert(func_idx < aot_module->import_func_count);
  2632. import_func = aot_module->import_funcs + func_idx;
  2633. if (import_func->call_conv_wasm_c_api)
  2634. func_ptr =
  2635. c_api_func_import ? c_api_func_import->func_ptr_linked : NULL;
  2636. if (!func_ptr) {
  2637. snprintf(buf, sizeof(buf),
  2638. "failed to call unlinked import function (%s, %s)",
  2639. import_func->module_name, import_func->func_name);
  2640. aot_set_exception(module_inst, buf);
  2641. goto fail;
  2642. }
  2643. attachment = import_func->attachment;
  2644. if (import_func->call_conv_wasm_c_api) {
  2645. ret = wasm_runtime_invoke_c_api_native(
  2646. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  2647. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  2648. }
  2649. else if (!import_func->call_conv_raw) {
  2650. signature = import_func->signature;
  2651. #if WASM_ENABLE_MULTI_MODULE != 0
  2652. WASMModuleInstanceCommon *sub_inst = NULL;
  2653. if ((sub_inst = ((AOTModuleInstanceExtra *)module_inst->e)
  2654. ->import_func_module_insts[func_idx])) {
  2655. exec_env = wasm_runtime_get_exec_env_singleton(sub_inst);
  2656. }
  2657. if (exec_env == NULL) {
  2658. wasm_runtime_set_exception((WASMModuleInstanceCommon *)module_inst,
  2659. "create singleton exec_env failed");
  2660. goto fail;
  2661. }
  2662. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  2663. void *prev_frame = get_top_frame(exec_env);
  2664. if (!aot_alloc_frame(exec_env, func_idx)) {
  2665. goto fail;
  2666. }
  2667. #endif
  2668. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2669. ret =
  2670. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2671. attachment, argv, argc, argv);
  2672. #if WASM_ENABLE_MULTI_MODULE != 0 && WASM_ENABLE_AOT_STACK_FRAME != 0
  2673. /* Free all frames allocated, note that some frames
  2674. may be allocated in AOT code and haven't been
  2675. freed if exception occurred */
  2676. while (get_top_frame(exec_env) != prev_frame)
  2677. aot_free_frame(exec_env);
  2678. #endif
  2679. }
  2680. else {
  2681. signature = import_func->signature;
  2682. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2683. signature, attachment, argv, argc,
  2684. argv);
  2685. }
  2686. fail:
  2687. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2688. if (!ret)
  2689. wasm_runtime_access_exce_check_guard_page();
  2690. #endif
  2691. return ret;
  2692. }
  2693. bool
  2694. aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
  2695. uint32 argc, uint32 *argv)
  2696. {
  2697. AOTModuleInstance *module_inst =
  2698. (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2699. AOTModule *aot_module = (AOTModule *)module_inst->module;
  2700. uint32 *func_type_indexes = module_inst->func_type_indexes;
  2701. AOTTableInstance *tbl_inst;
  2702. AOTFuncType *func_type;
  2703. void **func_ptrs = module_inst->func_ptrs, *func_ptr;
  2704. uint32 func_type_idx, func_idx, ext_ret_count;
  2705. table_elem_type_t tbl_elem_val = NULL_REF;
  2706. AOTImportFunc *import_func;
  2707. const char *signature = NULL;
  2708. void *attachment = NULL;
  2709. char buf[96];
  2710. bool ret;
  2711. /* this function is called from native code, so exec_env->handle and
  2712. exec_env->native_stack_boundary must have been set, we don't set
  2713. it again */
  2714. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  2715. goto fail;
  2716. }
  2717. tbl_inst = module_inst->tables[tbl_idx];
  2718. bh_assert(tbl_inst);
  2719. if (table_elem_idx >= tbl_inst->cur_size) {
  2720. aot_set_exception_with_id(module_inst, EXCE_UNDEFINED_ELEMENT);
  2721. goto fail;
  2722. }
  2723. tbl_elem_val = ((table_elem_type_t *)tbl_inst->elems)[table_elem_idx];
  2724. if (tbl_elem_val == NULL_REF) {
  2725. aot_set_exception_with_id(module_inst, EXCE_UNINITIALIZED_ELEMENT);
  2726. goto fail;
  2727. }
  2728. #if WASM_ENABLE_GC == 0
  2729. func_idx = (uint32)tbl_elem_val;
  2730. #else
  2731. func_idx =
  2732. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  2733. #endif
  2734. func_type_idx = func_type_indexes[func_idx];
  2735. func_type = (AOTFuncType *)aot_module->types[func_type_idx];
  2736. if (func_idx >= aot_module->import_func_count) {
  2737. /* func pointer was looked up previously */
  2738. bh_assert(func_ptrs[func_idx] != NULL);
  2739. }
  2740. if (!(func_ptr = func_ptrs[func_idx])) {
  2741. bh_assert(func_idx < aot_module->import_func_count);
  2742. import_func = aot_module->import_funcs + func_idx;
  2743. snprintf(buf, sizeof(buf),
  2744. "failed to call unlinked import function (%s, %s)",
  2745. import_func->module_name, import_func->func_name);
  2746. aot_set_exception(module_inst, buf);
  2747. goto fail;
  2748. }
  2749. if (func_idx < aot_module->import_func_count) {
  2750. /* Call native function */
  2751. import_func = aot_module->import_funcs + func_idx;
  2752. signature = import_func->signature;
  2753. attachment = import_func->attachment;
  2754. if (import_func->call_conv_raw) {
  2755. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2756. signature, attachment, argv,
  2757. argc, argv);
  2758. if (!ret)
  2759. goto fail;
  2760. return true;
  2761. }
  2762. }
  2763. ext_ret_count =
  2764. func_type->result_count > 1 ? func_type->result_count - 1 : 0;
  2765. if (ext_ret_count > 0) {
  2766. uint32 argv1_buf[32], *argv1 = argv1_buf;
  2767. uint32 *ext_rets = NULL, *argv_ret = argv;
  2768. uint32 cell_num = 0, i;
  2769. uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;
  2770. uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  2771. uint64 size;
  2772. /* Allocate memory all arguments */
  2773. size =
  2774. sizeof(uint32) * (uint64)argc /* original arguments */
  2775. + sizeof(void *)
  2776. * (uint64)ext_ret_count /* extra result values' addr */
  2777. + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */
  2778. if (size > sizeof(argv1_buf)
  2779. && !(argv1 = runtime_malloc(size, module_inst->cur_exception,
  2780. sizeof(module_inst->cur_exception)))) {
  2781. aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY);
  2782. goto fail;
  2783. }
  2784. /* Copy original arguments */
  2785. bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc);
  2786. /* Get the extra result value's address */
  2787. ext_rets =
  2788. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  2789. /* Append each extra result value's address to original arguments */
  2790. for (i = 0; i < ext_ret_count; i++) {
  2791. *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) =
  2792. (uintptr_t)(ext_rets + cell_num);
  2793. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  2794. }
  2795. ret = invoke_native_internal(exec_env, func_ptr, func_type, signature,
  2796. attachment, argv1, argc, argv);
  2797. if (!ret) {
  2798. if (argv1 != argv1_buf)
  2799. wasm_runtime_free(argv1);
  2800. goto fail;
  2801. }
  2802. /* Get extra result values */
  2803. switch (func_type->types[func_type->param_count]) {
  2804. case VALUE_TYPE_I32:
  2805. case VALUE_TYPE_F32:
  2806. #if WASM_ENABLE_REF_TYPES != 0
  2807. case VALUE_TYPE_FUNCREF:
  2808. case VALUE_TYPE_EXTERNREF:
  2809. #endif
  2810. argv_ret++;
  2811. break;
  2812. case VALUE_TYPE_I64:
  2813. case VALUE_TYPE_F64:
  2814. argv_ret += 2;
  2815. break;
  2816. #if WASM_ENABLE_SIMD != 0
  2817. case VALUE_TYPE_V128:
  2818. argv_ret += 4;
  2819. break;
  2820. #endif
  2821. default:
  2822. bh_assert(0);
  2823. break;
  2824. }
  2825. ext_rets =
  2826. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  2827. bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
  2828. sizeof(uint32) * cell_num);
  2829. if (argv1 != argv1_buf)
  2830. wasm_runtime_free(argv1);
  2831. return true;
  2832. }
  2833. else {
  2834. ret = invoke_native_internal(exec_env, func_ptr, func_type, signature,
  2835. attachment, argv, argc, argv);
  2836. if (!ret)
  2837. goto fail;
  2838. return true;
  2839. }
  2840. fail:
  2841. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2842. wasm_runtime_access_exce_check_guard_page();
  2843. #endif
  2844. return false;
  2845. }
  2846. bool
  2847. aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
  2848. uint64 app_buf_addr, uint64 app_buf_size,
  2849. void **p_native_addr)
  2850. {
  2851. bool ret;
  2852. ret = wasm_check_app_addr_and_convert(module_inst, is_str, app_buf_addr,
  2853. app_buf_size, p_native_addr);
  2854. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2855. if (!ret)
  2856. wasm_runtime_access_exce_check_guard_page();
  2857. #endif
  2858. return ret;
  2859. }
  2860. void *
  2861. aot_memmove(void *dest, const void *src, size_t n)
  2862. {
  2863. return memmove(dest, src, n);
  2864. }
  2865. void *
  2866. aot_memset(void *s, int c, size_t n)
  2867. {
  2868. return memset(s, c, n);
  2869. }
  2870. double
  2871. aot_sqrt(double x)
  2872. {
  2873. return sqrt(x);
  2874. }
  2875. float
  2876. aot_sqrtf(float x)
  2877. {
  2878. return sqrtf(x);
  2879. }
  2880. #if WASM_ENABLE_BULK_MEMORY != 0
  2881. bool
  2882. aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
  2883. uint32 len, size_t dst)
  2884. {
  2885. AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
  2886. AOTModule *aot_module;
  2887. uint8 *data;
  2888. uint8 *maddr;
  2889. uint64 seg_len;
  2890. if (bh_bitmap_get_bit(
  2891. ((AOTModuleInstanceExtra *)module_inst->e)->common.data_dropped,
  2892. seg_index)) {
  2893. seg_len = 0;
  2894. data = NULL;
  2895. }
  2896. else {
  2897. aot_module = (AOTModule *)module_inst->module;
  2898. seg_len = aot_module->mem_init_data_list[seg_index]->byte_count;
  2899. data = aot_module->mem_init_data_list[seg_index]->bytes;
  2900. }
  2901. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  2902. (uint64)dst, (uint64)len))
  2903. return false;
  2904. if ((uint64)offset + (uint64)len > seg_len) {
  2905. aot_set_exception(module_inst, "out of bounds memory access");
  2906. return false;
  2907. }
  2908. maddr = wasm_runtime_addr_app_to_native(
  2909. (WASMModuleInstanceCommon *)module_inst, (uint64)dst);
  2910. SHARED_MEMORY_LOCK(memory_inst);
  2911. bh_memcpy_s(maddr, CLAMP_U64_TO_U32(memory_inst->memory_data_size - dst),
  2912. data + offset, len);
  2913. SHARED_MEMORY_UNLOCK(memory_inst);
  2914. return true;
  2915. }
  2916. bool
  2917. aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index)
  2918. {
  2919. bh_bitmap_set_bit(
  2920. ((AOTModuleInstanceExtra *)module_inst->e)->common.data_dropped,
  2921. seg_index);
  2922. /* Currently we can't free the dropped data segment
  2923. as the mem_init_data_count is a continuous array */
  2924. return true;
  2925. }
  2926. #endif /* WASM_ENABLE_BULK_MEMORY */
  2927. #if WASM_ENABLE_THREAD_MGR != 0
  2928. bool
  2929. aot_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size)
  2930. {
  2931. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  2932. AOTModule *module = (AOTModule *)module_inst->module;
  2933. uint32 stack_top_idx = module->aux_stack_top_global_index;
  2934. uint64 data_end = module->aux_data_end;
  2935. uint64 stack_bottom = module->aux_stack_bottom;
  2936. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2937. /* Check the aux stack space, currently we don't allocate space in heap */
  2938. if ((is_stack_before_data && (size > start_offset))
  2939. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2940. return false;
  2941. if (stack_top_idx != (uint32)-1) {
  2942. /* The aux stack top is a wasm global,
  2943. set the initial value for the global */
  2944. uint32 global_offset = module->globals[stack_top_idx].data_offset;
  2945. uint8 *global_addr = module_inst->global_data + global_offset;
  2946. /* TODO: Memory64 the type i32/i64 depends on memory idx type*/
  2947. *(int32 *)global_addr = (uint32)start_offset;
  2948. /* The aux stack boundary is a constant value,
  2949. set the value to exec_env */
  2950. exec_env->aux_stack_boundary = (uintptr_t)start_offset - size;
  2951. exec_env->aux_stack_bottom = (uintptr_t)start_offset;
  2952. return true;
  2953. }
  2954. return false;
  2955. }
  2956. bool
  2957. aot_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size)
  2958. {
  2959. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  2960. AOTModule *module = (AOTModule *)module_inst->module;
  2961. /* The aux stack information is resolved in loader
  2962. and store in module */
  2963. uint64 stack_bottom = module->aux_stack_bottom;
  2964. uint32 total_aux_stack_size = module->aux_stack_size;
  2965. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2966. if (start_offset)
  2967. *start_offset = stack_bottom;
  2968. if (size)
  2969. *size = total_aux_stack_size;
  2970. return true;
  2971. }
  2972. return false;
  2973. }
  2974. #endif
  2975. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2976. static void
  2977. const_string_node_size_cb(void *key, void *value, void *p_const_string_size)
  2978. {
  2979. uint32 const_string_size = 0;
  2980. const_string_size += bh_hash_map_get_elem_struct_size();
  2981. const_string_size += strlen((const char *)value) + 1;
  2982. *(uint32 *)p_const_string_size += const_string_size;
  2983. }
  2984. void
  2985. aot_get_module_mem_consumption(const AOTModule *module,
  2986. WASMModuleMemConsumption *mem_conspn)
  2987. {
  2988. uint32 i, size;
  2989. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2990. mem_conspn->module_struct_size = sizeof(AOTModule);
  2991. mem_conspn->types_size = sizeof(AOTFuncType *) * module->type_count;
  2992. for (i = 0; i < module->type_count; i++) {
  2993. AOTFuncType *type = (AOTFuncType *)module->types[i];
  2994. size = offsetof(AOTFuncType, types)
  2995. + sizeof(uint8) * (type->param_count + type->result_count);
  2996. mem_conspn->types_size += size;
  2997. }
  2998. mem_conspn->imports_size =
  2999. sizeof(AOTImportMemory) * module->import_memory_count
  3000. + sizeof(AOTImportTable) * module->import_table_count
  3001. + sizeof(AOTImportGlobal) * module->import_global_count
  3002. + sizeof(AOTImportFunc) * module->import_func_count;
  3003. /* func_ptrs and func_type_indexes */
  3004. mem_conspn->functions_size =
  3005. (sizeof(void *) + sizeof(uint32)) * module->func_count;
  3006. mem_conspn->tables_size = sizeof(AOTTable) * module->table_count;
  3007. mem_conspn->memories_size = sizeof(AOTMemory) * module->memory_count;
  3008. mem_conspn->globals_size = sizeof(AOTGlobal) * module->global_count;
  3009. mem_conspn->exports_size = sizeof(AOTExport) * module->export_count;
  3010. mem_conspn->table_segs_size =
  3011. sizeof(AOTTableInitData *) * module->table_init_data_count;
  3012. for (i = 0; i < module->table_init_data_count; i++) {
  3013. AOTTableInitData *init_data = module->table_init_data_list[i];
  3014. size = offsetof(AOTTableInitData, init_values)
  3015. + sizeof(InitializerExpression) * init_data->value_count;
  3016. mem_conspn->table_segs_size += size;
  3017. }
  3018. mem_conspn->data_segs_size =
  3019. sizeof(AOTMemInitData *) * module->mem_init_data_count;
  3020. for (i = 0; i < module->mem_init_data_count; i++) {
  3021. mem_conspn->data_segs_size += sizeof(AOTMemInitData);
  3022. }
  3023. if (module->const_str_set) {
  3024. uint32 const_string_size = 0;
  3025. mem_conspn->const_strs_size =
  3026. bh_hash_map_get_struct_size(module->const_str_set);
  3027. bh_hash_map_traverse(module->const_str_set, const_string_node_size_cb,
  3028. (void *)&const_string_size);
  3029. mem_conspn->const_strs_size += const_string_size;
  3030. }
  3031. /* code size + literal size + object data section size */
  3032. mem_conspn->aot_code_size =
  3033. module->code_size + module->literal_size
  3034. + sizeof(AOTObjectDataSection) * module->data_section_count;
  3035. for (i = 0; i < module->data_section_count; i++) {
  3036. AOTObjectDataSection *obj_data = module->data_sections + i;
  3037. mem_conspn->aot_code_size += sizeof(uint8) * obj_data->size;
  3038. }
  3039. mem_conspn->total_size += mem_conspn->module_struct_size;
  3040. mem_conspn->total_size += mem_conspn->types_size;
  3041. mem_conspn->total_size += mem_conspn->imports_size;
  3042. mem_conspn->total_size += mem_conspn->functions_size;
  3043. mem_conspn->total_size += mem_conspn->tables_size;
  3044. mem_conspn->total_size += mem_conspn->memories_size;
  3045. mem_conspn->total_size += mem_conspn->globals_size;
  3046. mem_conspn->total_size += mem_conspn->exports_size;
  3047. mem_conspn->total_size += mem_conspn->table_segs_size;
  3048. mem_conspn->total_size += mem_conspn->data_segs_size;
  3049. mem_conspn->total_size += mem_conspn->const_strs_size;
  3050. mem_conspn->total_size += mem_conspn->aot_code_size;
  3051. }
  3052. void
  3053. aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
  3054. WASMModuleInstMemConsumption *mem_conspn)
  3055. {
  3056. AOTTableInstance *tbl_inst;
  3057. uint32 i;
  3058. memset(mem_conspn, 0, sizeof(*mem_conspn));
  3059. mem_conspn->module_inst_struct_size = sizeof(AOTModuleInstance);
  3060. mem_conspn->memories_size =
  3061. sizeof(void *) * module_inst->memory_count
  3062. + sizeof(AOTMemoryInstance) * module_inst->memory_count;
  3063. for (i = 0; i < module_inst->memory_count; i++) {
  3064. AOTMemoryInstance *mem_inst = module_inst->memories[i];
  3065. mem_conspn->memories_size +=
  3066. mem_inst->num_bytes_per_page * mem_inst->cur_page_count;
  3067. mem_conspn->app_heap_size =
  3068. mem_inst->heap_data_end - mem_inst->heap_data;
  3069. /* size of app heap structure */
  3070. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  3071. }
  3072. mem_conspn->tables_size +=
  3073. sizeof(AOTTableInstance *) * module_inst->table_count;
  3074. for (i = 0; i < module_inst->table_count; i++) {
  3075. tbl_inst = module_inst->tables[i];
  3076. mem_conspn->tables_size += offsetof(AOTTableInstance, elems);
  3077. mem_conspn->tables_size += sizeof(uint32) * tbl_inst->max_size;
  3078. }
  3079. /* func_ptrs and func_type_indexes */
  3080. mem_conspn->functions_size =
  3081. (sizeof(void *) + sizeof(uint32))
  3082. * (((AOTModule *)module_inst->module)->import_func_count
  3083. + ((AOTModule *)module_inst->module)->func_count);
  3084. mem_conspn->globals_size = module_inst->global_data_size;
  3085. mem_conspn->exports_size =
  3086. sizeof(AOTFunctionInstance) * (uint64)module_inst->export_func_count;
  3087. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  3088. mem_conspn->total_size += mem_conspn->memories_size;
  3089. mem_conspn->total_size += mem_conspn->functions_size;
  3090. mem_conspn->total_size += mem_conspn->tables_size;
  3091. mem_conspn->total_size += mem_conspn->globals_size;
  3092. mem_conspn->total_size += mem_conspn->exports_size;
  3093. }
  3094. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  3095. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  3096. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3097. void
  3098. aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx)
  3099. {
  3100. bh_bitmap_set_bit(
  3101. ((AOTModuleInstanceExtra *)module_inst->e)->common.elem_dropped,
  3102. tbl_seg_idx);
  3103. }
  3104. void
  3105. aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx,
  3106. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  3107. uint32 dst_offset)
  3108. {
  3109. AOTTableInstance *tbl_inst;
  3110. AOTTableInitData *tbl_seg;
  3111. const AOTModule *module = (AOTModule *)module_inst->module;
  3112. table_elem_type_t *table_elems;
  3113. InitializerExpression *tbl_seg_init_values = NULL, *init_values;
  3114. uint32 i, tbl_seg_len = 0;
  3115. #if WASM_ENABLE_GC != 0
  3116. void *func_obj;
  3117. #endif
  3118. tbl_inst = module_inst->tables[tbl_idx];
  3119. bh_assert(tbl_inst);
  3120. tbl_seg = module->table_init_data_list[tbl_seg_idx];
  3121. bh_assert(tbl_seg);
  3122. if (!bh_bitmap_get_bit(
  3123. ((AOTModuleInstanceExtra *)module_inst->e)->common.elem_dropped,
  3124. tbl_seg_idx)) {
  3125. /* table segment isn't dropped */
  3126. tbl_seg_init_values = tbl_seg->init_values;
  3127. tbl_seg_len = tbl_seg->value_count;
  3128. }
  3129. if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len)
  3130. || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
  3131. aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3132. return;
  3133. }
  3134. if (!length) {
  3135. return;
  3136. }
  3137. table_elems = tbl_inst->elems + dst_offset;
  3138. init_values = tbl_seg_init_values + src_offset;
  3139. for (i = 0; i < length; i++) {
  3140. #if WASM_ENABLE_GC != 0
  3141. /* UINT32_MAX indicates that it is a null ref */
  3142. if (init_values[i].u.ref_index != UINT32_MAX) {
  3143. if (!(func_obj = aot_create_func_obj(module_inst,
  3144. init_values[i].u.ref_index,
  3145. true, NULL, 0))) {
  3146. aot_set_exception_with_id(module_inst, EXCE_NULL_FUNC_OBJ);
  3147. return;
  3148. }
  3149. table_elems[i] = func_obj;
  3150. }
  3151. else {
  3152. table_elems[i] = NULL_REF;
  3153. }
  3154. #else
  3155. table_elems[i] = init_values[i].u.ref_index;
  3156. #endif
  3157. }
  3158. }
  3159. void
  3160. aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
  3161. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  3162. uint32 dst_offset)
  3163. {
  3164. AOTTableInstance *src_tbl_inst, *dst_tbl_inst;
  3165. src_tbl_inst = module_inst->tables[src_tbl_idx];
  3166. bh_assert(src_tbl_inst);
  3167. dst_tbl_inst = module_inst->tables[dst_tbl_idx];
  3168. bh_assert(dst_tbl_inst);
  3169. if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
  3170. || offset_len_out_of_bounds(src_offset, length,
  3171. src_tbl_inst->cur_size)) {
  3172. aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3173. return;
  3174. }
  3175. /* if src_offset >= dst_offset, copy from front to back */
  3176. /* if src_offset < dst_offset, copy from back to front */
  3177. /* merge all together */
  3178. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(AOTTableInstance, elems)
  3179. + dst_offset * sizeof(table_elem_type_t),
  3180. (dst_tbl_inst->cur_size - dst_offset)
  3181. * sizeof(table_elem_type_t),
  3182. (uint8 *)src_tbl_inst + offsetof(AOTTableInstance, elems)
  3183. + src_offset * sizeof(table_elem_type_t),
  3184. length * sizeof(table_elem_type_t));
  3185. }
  3186. void
  3187. aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length,
  3188. table_elem_type_t val, uint32 data_offset)
  3189. {
  3190. AOTTableInstance *tbl_inst;
  3191. tbl_inst = module_inst->tables[tbl_idx];
  3192. bh_assert(tbl_inst);
  3193. if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
  3194. aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3195. return;
  3196. }
  3197. for (; length != 0; data_offset++, length--) {
  3198. tbl_inst->elems[data_offset] = val;
  3199. }
  3200. }
  3201. uint32
  3202. aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 inc_size,
  3203. table_elem_type_t init_val)
  3204. {
  3205. AOTTableInstance *tbl_inst;
  3206. uint32 i, orig_size, total_size;
  3207. tbl_inst = module_inst->tables[tbl_idx];
  3208. if (!tbl_inst) {
  3209. return (uint32)-1;
  3210. }
  3211. orig_size = tbl_inst->cur_size;
  3212. if (!inc_size) {
  3213. return orig_size;
  3214. }
  3215. if (tbl_inst->cur_size > UINT32_MAX - inc_size) {
  3216. #if WASM_ENABLE_SPEC_TEST == 0
  3217. LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
  3218. ") failed because of integer overflow",
  3219. tbl_inst->cur_size, inc_size);
  3220. #endif
  3221. return (uint32)-1;
  3222. }
  3223. total_size = tbl_inst->cur_size + inc_size;
  3224. if (total_size > tbl_inst->max_size) {
  3225. #if WASM_ENABLE_SPEC_TEST == 0
  3226. LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
  3227. ") failed because of over max size",
  3228. tbl_inst->cur_size, inc_size);
  3229. #endif
  3230. return (uint32)-1;
  3231. }
  3232. /* fill in */
  3233. for (i = 0; i < inc_size; ++i) {
  3234. tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
  3235. }
  3236. tbl_inst->cur_size = total_size;
  3237. return orig_size;
  3238. }
  3239. #endif /* WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3240. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  3241. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  3242. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3243. static const char *
  3244. lookup_func_name(const char **func_names, uint32 *func_indexes,
  3245. uint32 func_index_count, uint32 func_index)
  3246. {
  3247. int64 low = 0, mid;
  3248. int64 high = func_index_count - 1;
  3249. if (!func_names || !func_indexes || func_index_count == 0)
  3250. return NULL;
  3251. while (low <= high) {
  3252. mid = (low + high) / 2;
  3253. if (func_index == func_indexes[mid]) {
  3254. return func_names[mid];
  3255. }
  3256. else if (func_index < func_indexes[mid])
  3257. high = mid - 1;
  3258. else
  3259. low = mid + 1;
  3260. }
  3261. return NULL;
  3262. }
  3263. #endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
  3264. static const char *
  3265. get_func_name_from_index(const AOTModuleInstance *module_inst,
  3266. uint32 func_index)
  3267. {
  3268. const char *func_name = NULL;
  3269. AOTModule *module = (AOTModule *)module_inst->module;
  3270. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3271. if ((func_name =
  3272. lookup_func_name(module->aux_func_names, module->aux_func_indexes,
  3273. module->aux_func_name_count, func_index))) {
  3274. return func_name;
  3275. }
  3276. #endif
  3277. if (func_index < module->import_func_count) {
  3278. func_name = module->import_funcs[func_index].func_name;
  3279. }
  3280. else {
  3281. uint32 i;
  3282. for (i = 0; i < module->export_count; i++) {
  3283. AOTExport export = module->exports[i];
  3284. if (export.index == func_index && export.kind == EXPORT_KIND_FUNC) {
  3285. func_name = export.name;
  3286. break;
  3287. }
  3288. }
  3289. }
  3290. return func_name;
  3291. }
  3292. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 || \
  3293. WASM_ENABLE_PERF_PROFILING != 0 */
  3294. #if WASM_ENABLE_GC == 0
  3295. static bool
  3296. aot_alloc_standard_frame(WASMExecEnv *exec_env, uint32 func_index)
  3297. {
  3298. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  3299. #if WASM_ENABLE_PERF_PROFILING != 0
  3300. AOTFuncPerfProfInfo *func_perf_prof =
  3301. module_inst->func_perf_profilings + func_index;
  3302. #endif
  3303. AOTFrame *cur_frame, *frame;
  3304. uint32 size = (uint32)offsetof(AOTFrame, lp);
  3305. cur_frame = (AOTFrame *)exec_env->cur_frame;
  3306. if (!cur_frame)
  3307. frame = (AOTFrame *)exec_env->wasm_stack.bottom;
  3308. else
  3309. frame = (AOTFrame *)((uint8 *)cur_frame + size);
  3310. if ((uint8 *)frame + size > exec_env->wasm_stack.top_boundary) {
  3311. aot_set_exception(module_inst, "wasm operand stack overflow");
  3312. return false;
  3313. }
  3314. frame->func_index = func_index;
  3315. /* No need to initialize ip, it will be committed in jitted code
  3316. when needed */
  3317. /* frame->ip = NULL; */
  3318. frame->prev_frame = (AOTFrame *)exec_env->cur_frame;
  3319. #if WASM_ENABLE_PERF_PROFILING != 0
  3320. frame->time_started = (uintptr_t)os_time_thread_cputime_us();
  3321. frame->func_perf_prof_info = func_perf_prof;
  3322. #endif
  3323. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3324. {
  3325. uint32 wasm_stack_used =
  3326. (uint8 *)frame + size - exec_env->wasm_stack.bottom;
  3327. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  3328. exec_env->max_wasm_stack_used = wasm_stack_used;
  3329. }
  3330. #endif
  3331. exec_env->cur_frame = (struct WASMInterpFrame *)frame;
  3332. return true;
  3333. }
  3334. #else /* else of WASM_ENABLE_GC == 0 */
  3335. static bool
  3336. aot_alloc_standard_frame(WASMExecEnv *exec_env, uint32 func_index)
  3337. {
  3338. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  3339. AOTModule *module = (AOTModule *)module_inst->module;
  3340. #if WASM_ENABLE_PERF_PROFILING != 0
  3341. AOTFuncPerfProfInfo *func_perf_prof =
  3342. module_inst->func_perf_profilings + func_index;
  3343. #endif
  3344. AOTFrame *frame;
  3345. uint32 max_local_cell_num, max_stack_cell_num, all_cell_num;
  3346. uint32 aot_func_idx, frame_size;
  3347. if (func_index >= module->import_func_count) {
  3348. aot_func_idx = func_index - module->import_func_count;
  3349. max_local_cell_num = module->max_local_cell_nums[aot_func_idx];
  3350. max_stack_cell_num = module->max_stack_cell_nums[aot_func_idx];
  3351. }
  3352. else {
  3353. AOTFuncType *func_type = module->import_funcs[func_index].func_type;
  3354. max_local_cell_num =
  3355. func_type->param_cell_num > 2 ? func_type->param_cell_num : 2;
  3356. max_stack_cell_num = 0;
  3357. }
  3358. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3359. #if WASM_ENABLE_GC == 0
  3360. frame_size = (uint32)offsetof(AOTFrame, lp) + all_cell_num * 4;
  3361. #else
  3362. frame_size =
  3363. (uint32)offsetof(AOTFrame, lp) + align_uint(all_cell_num * 5, 4);
  3364. #endif
  3365. frame = wasm_exec_env_alloc_wasm_frame(exec_env, frame_size);
  3366. if (!frame) {
  3367. aot_set_exception(module_inst, "wasm operand stack overflow");
  3368. return false;
  3369. }
  3370. #if WASM_ENABLE_PERF_PROFILING != 0
  3371. frame->time_started = (uintptr_t)os_time_thread_cputime_us();
  3372. frame->func_perf_prof_info = func_perf_prof;
  3373. #endif
  3374. #if WASM_ENABLE_GC != 0
  3375. frame->sp = frame->lp + max_local_cell_num;
  3376. frame->frame_ref = (uint8 *)(frame->sp + max_stack_cell_num);
  3377. #endif
  3378. frame->prev_frame = (AOTFrame *)exec_env->cur_frame;
  3379. exec_env->cur_frame = (struct WASMInterpFrame *)frame;
  3380. frame->func_index = func_index;
  3381. return true;
  3382. }
  3383. #endif /* end of WASM_ENABLE_GC == 0 */
  3384. static bool
  3385. aot_alloc_tiny_frame(WASMExecEnv *exec_env, uint32 func_index)
  3386. {
  3387. AOTTinyFrame *new_frame = (AOTTinyFrame *)exec_env->wasm_stack.top;
  3388. if ((uint8 *)new_frame > exec_env->wasm_stack.top_boundary) {
  3389. aot_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3390. "wasm operand stack overflow");
  3391. return false;
  3392. }
  3393. new_frame->func_index = func_index;
  3394. exec_env->wasm_stack.top += sizeof(AOTTinyFrame);
  3395. return true;
  3396. }
  3397. bool
  3398. aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  3399. {
  3400. AOTModule *module =
  3401. (AOTModule *)((AOTModuleInstance *)exec_env->module_inst)->module;
  3402. if (is_frame_per_function(exec_env)
  3403. && func_index >= module->import_func_count) {
  3404. /* in frame per function mode the frame is allocated at
  3405. the beginning of each frame, so we only need to allocate
  3406. the frame for imported functions */
  3407. return true;
  3408. }
  3409. if (is_tiny_frame(exec_env)) {
  3410. return aot_alloc_tiny_frame(exec_env, func_index);
  3411. }
  3412. else {
  3413. return aot_alloc_standard_frame(exec_env, func_index);
  3414. }
  3415. }
  3416. static inline void
  3417. aot_free_standard_frame(WASMExecEnv *exec_env)
  3418. {
  3419. AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame;
  3420. AOTFrame *prev_frame = (AOTFrame *)cur_frame->prev_frame;
  3421. #if WASM_ENABLE_PERF_PROFILING != 0
  3422. uint64 time_elapsed =
  3423. (uintptr_t)os_time_thread_cputime_us() - cur_frame->time_started;
  3424. cur_frame->func_perf_prof_info->total_exec_time += time_elapsed;
  3425. cur_frame->func_perf_prof_info->total_exec_cnt++;
  3426. /* parent function */
  3427. if (prev_frame)
  3428. prev_frame->func_perf_prof_info->children_exec_time += time_elapsed;
  3429. #endif
  3430. #if WASM_ENABLE_GC != 0
  3431. wasm_exec_env_free_wasm_frame(exec_env, cur_frame);
  3432. #endif
  3433. exec_env->cur_frame = (struct WASMInterpFrame *)prev_frame;
  3434. }
  3435. static inline void
  3436. aot_free_tiny_frame(WASMExecEnv *exec_env)
  3437. {
  3438. exec_env->wasm_stack.top =
  3439. get_prev_frame(exec_env, exec_env->wasm_stack.top);
  3440. }
  3441. void
  3442. aot_free_frame(WASMExecEnv *exec_env)
  3443. {
  3444. if (is_tiny_frame(exec_env)) {
  3445. aot_free_tiny_frame(exec_env);
  3446. }
  3447. else {
  3448. aot_free_standard_frame(exec_env);
  3449. }
  3450. }
  3451. void
  3452. aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame)
  3453. {
  3454. #if WASM_ENABLE_PERF_PROFILING != 0
  3455. AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame;
  3456. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  3457. AOTFuncPerfProfInfo *func_perf_prof =
  3458. module_inst->func_perf_profilings + cur_frame->func_index;
  3459. if (alloc_frame) {
  3460. cur_frame->time_started = (uintptr_t)os_time_thread_cputime_us();
  3461. cur_frame->func_perf_prof_info = func_perf_prof;
  3462. }
  3463. else {
  3464. AOTFrame *prev_frame = cur_frame->prev_frame;
  3465. uint64 time_elapsed =
  3466. (uintptr_t)os_time_thread_cputime_us() - cur_frame->time_started;
  3467. cur_frame->func_perf_prof_info->total_exec_time += time_elapsed;
  3468. cur_frame->func_perf_prof_info->total_exec_cnt++;
  3469. /* parent function */
  3470. if (prev_frame)
  3471. prev_frame->func_perf_prof_info->children_exec_time += time_elapsed;
  3472. }
  3473. #endif
  3474. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3475. if (alloc_frame) {
  3476. #if WASM_ENABLE_GC == 0
  3477. uint32 wasm_stack_used = (uint8 *)exec_env->cur_frame
  3478. + (uint32)offsetof(AOTFrame, lp)
  3479. - exec_env->wasm_stack.bottom;
  3480. #else
  3481. uint32 wasm_stack_used =
  3482. exec_env->wasm_stack.top - exec_env->wasm_stack.bottom;
  3483. #endif
  3484. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  3485. exec_env->max_wasm_stack_used = wasm_stack_used;
  3486. }
  3487. #endif
  3488. }
  3489. #endif /* end of WASM_ENABLE_AOT_STACK_FRAME != 0 */
  3490. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3491. bool
  3492. aot_create_call_stack(struct WASMExecEnv *exec_env)
  3493. {
  3494. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  3495. AOTModule *module = (AOTModule *)module_inst->module;
  3496. uint32 n = 0;
  3497. void *top_frame = get_top_frame(exec_env);
  3498. while (top_frame) {
  3499. top_frame = get_prev_frame(exec_env, top_frame);
  3500. n++;
  3501. }
  3502. /* release previous stack frames and create new ones */
  3503. destroy_c_api_frames(module_inst->frames);
  3504. if (!bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame), false)) {
  3505. return false;
  3506. }
  3507. top_frame = get_top_frame(exec_env);
  3508. while (n-- > 0) {
  3509. uint32 func_index, ip_offset;
  3510. uint32 *lp = NULL;
  3511. #if WASM_ENABLE_GC != 0
  3512. uint32 *sp = NULL;
  3513. uint8 *frame_ref = NULL;
  3514. #endif
  3515. if (is_tiny_frame(exec_env)) {
  3516. AOTTinyFrame *frame = (AOTTinyFrame *)top_frame;
  3517. func_index = (uint32)frame->func_index;
  3518. ip_offset = (uint32)frame->ip_offset;
  3519. }
  3520. else {
  3521. AOTFrame *frame = (AOTFrame *)top_frame;
  3522. func_index = (uint32)frame->func_index;
  3523. ip_offset = (uint32)frame->ip_offset;
  3524. lp = frame->lp;
  3525. #if WASM_ENABLE_GC != 0
  3526. sp = frame->sp;
  3527. frame_ref = frame->frame_ref;
  3528. #endif
  3529. }
  3530. WASMCApiFrame frame = { 0 };
  3531. uint32 max_local_cell_num = 0, max_stack_cell_num = 0;
  3532. uint32 all_cell_num, lp_size;
  3533. frame.instance = module_inst;
  3534. frame.module_offset = 0;
  3535. frame.func_index = func_index;
  3536. frame.func_offset = ip_offset;
  3537. frame.func_name_wp = get_func_name_from_index(module_inst, func_index);
  3538. if (!is_frame_func_idx_disabled(exec_env)) {
  3539. if (func_index >= module->import_func_count) {
  3540. uint32 aot_func_idx = func_index - module->import_func_count;
  3541. max_local_cell_num = module->max_local_cell_nums[aot_func_idx];
  3542. max_stack_cell_num = module->max_stack_cell_nums[aot_func_idx];
  3543. }
  3544. else {
  3545. AOTFuncType *func_type =
  3546. module->import_funcs[func_index].func_type;
  3547. max_local_cell_num = func_type->param_cell_num > 2
  3548. ? func_type->param_cell_num
  3549. : 2;
  3550. max_stack_cell_num = 0;
  3551. }
  3552. }
  3553. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3554. #if WASM_ENABLE_GC == 0
  3555. lp_size = all_cell_num * 4;
  3556. #else
  3557. lp_size = align_uint(all_cell_num * 5, 4);
  3558. #endif
  3559. if (lp_size > 0 && !is_tiny_frame(exec_env)) {
  3560. if (!(frame.lp = wasm_runtime_malloc(lp_size))) {
  3561. destroy_c_api_frames(module_inst->frames);
  3562. return false;
  3563. }
  3564. bh_memcpy_s(frame.lp, lp_size, lp, lp_size);
  3565. #if WASM_ENABLE_GC != 0
  3566. uint32 local_ref_flags_cell_num =
  3567. module->func_local_ref_flags[frame.func_index]
  3568. .local_ref_flag_cell_num;
  3569. uint8 *local_ref_flags =
  3570. module->func_local_ref_flags[frame.func_index].local_ref_flags;
  3571. frame.sp = frame.lp + (sp - lp);
  3572. frame.frame_ref = (uint8 *)frame.lp + (frame_ref - (uint8 *)lp);
  3573. /* copy local ref flags from AOT module */
  3574. bh_memcpy_s(frame.frame_ref, local_ref_flags_cell_num,
  3575. local_ref_flags, lp_size);
  3576. #endif
  3577. }
  3578. if (!bh_vector_append(module_inst->frames, &frame)) {
  3579. if (frame.lp)
  3580. wasm_runtime_free(frame.lp);
  3581. destroy_c_api_frames(module_inst->frames);
  3582. return false;
  3583. }
  3584. top_frame = get_prev_frame(exec_env, top_frame);
  3585. }
  3586. return true;
  3587. }
  3588. #define PRINT_OR_DUMP() \
  3589. do { \
  3590. total_len += \
  3591. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  3592. if ((!print) && buf && (len == 0)) { \
  3593. exception_unlock(module_inst); \
  3594. return total_len; \
  3595. } \
  3596. } while (0)
  3597. uint32
  3598. aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len)
  3599. {
  3600. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  3601. uint32 n = 0, total_len = 0, total_frames;
  3602. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  3603. * will be truncated */
  3604. char line_buf[256];
  3605. if (!module_inst->frames) {
  3606. return 0;
  3607. }
  3608. total_frames = (uint32)bh_vector_size(module_inst->frames);
  3609. if (total_frames == 0) {
  3610. return 0;
  3611. }
  3612. exception_lock(module_inst);
  3613. snprintf(line_buf, sizeof(line_buf), "\n");
  3614. PRINT_OR_DUMP();
  3615. while (n < total_frames) {
  3616. WASMCApiFrame frame = { 0 };
  3617. uint32 line_length, i;
  3618. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  3619. exception_unlock(module_inst);
  3620. return 0;
  3621. }
  3622. /* function name not exported, print number instead */
  3623. if (frame.func_name_wp == NULL) {
  3624. line_length = snprintf(line_buf, sizeof(line_buf),
  3625. "#%02" PRIu32 ": 0x%04x - $f%" PRIu32 "\n",
  3626. n, frame.func_offset, frame.func_index);
  3627. }
  3628. else {
  3629. line_length = snprintf(line_buf, sizeof(line_buf),
  3630. "#%02" PRIu32 ": 0x%04x - %s\n", n,
  3631. frame.func_offset, frame.func_name_wp);
  3632. }
  3633. if (line_length >= sizeof(line_buf)) {
  3634. uint32 line_buffer_len = sizeof(line_buf);
  3635. /* If line too long, ensure the last character is '\n' */
  3636. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  3637. line_buf[i] = '.';
  3638. }
  3639. line_buf[line_buffer_len - 2] = '\n';
  3640. }
  3641. PRINT_OR_DUMP();
  3642. n++;
  3643. }
  3644. snprintf(line_buf, sizeof(line_buf), "\n");
  3645. PRINT_OR_DUMP();
  3646. exception_unlock(module_inst);
  3647. return total_len + 1;
  3648. }
  3649. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  3650. #if WASM_ENABLE_PERF_PROFILING != 0
  3651. void
  3652. aot_dump_perf_profiling(const AOTModuleInstance *module_inst)
  3653. {
  3654. AOTFuncPerfProfInfo *perf_prof =
  3655. (AOTFuncPerfProfInfo *)module_inst->func_perf_profilings;
  3656. AOTModule *module = (AOTModule *)module_inst->module;
  3657. uint32 total_func_count = module->import_func_count + module->func_count, i;
  3658. const char *func_name;
  3659. os_printf("Performance profiler data:\n");
  3660. for (i = 0; i < total_func_count; i++, perf_prof++) {
  3661. if (perf_prof->total_exec_cnt == 0)
  3662. continue;
  3663. func_name = get_func_name_from_index(module_inst, i);
  3664. if (func_name)
  3665. os_printf(
  3666. " func %s, execution time: %.3f ms, execution count: %" PRIu32
  3667. " times, children execution time: %.3f ms\n",
  3668. func_name, perf_prof->total_exec_time / 1000.0f,
  3669. perf_prof->total_exec_cnt,
  3670. perf_prof->children_exec_time / 1000.0f);
  3671. else
  3672. os_printf(" func %" PRIu32
  3673. ", execution time: %.3f ms, execution count: %" PRIu32
  3674. " times, children execution time: %.3f ms\n",
  3675. i, perf_prof->total_exec_time / 1000.0f,
  3676. perf_prof->total_exec_cnt,
  3677. perf_prof->children_exec_time / 1000.0f);
  3678. }
  3679. }
  3680. double
  3681. aot_summarize_wasm_execute_time(const AOTModuleInstance *inst)
  3682. {
  3683. double ret = 0;
  3684. AOTModule *module = (AOTModule *)inst->module;
  3685. uint32 total_func_count = module->import_func_count + module->func_count, i;
  3686. for (i = 0; i < total_func_count; i++) {
  3687. AOTFuncPerfProfInfo *perf_prof =
  3688. (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i;
  3689. ret += (perf_prof->total_exec_time - perf_prof->children_exec_time)
  3690. / 1000.0f;
  3691. }
  3692. return ret;
  3693. }
  3694. double
  3695. aot_get_wasm_func_exec_time(const AOTModuleInstance *inst,
  3696. const char *func_name)
  3697. {
  3698. AOTModule *module = (AOTModule *)inst->module;
  3699. uint32 total_func_count = module->import_func_count + module->func_count, i;
  3700. for (i = 0; i < total_func_count; i++) {
  3701. const char *name_in_wasm = get_func_name_from_index(inst, i);
  3702. if (name_in_wasm && strcmp(func_name, name_in_wasm) == 0) {
  3703. AOTFuncPerfProfInfo *perf_prof =
  3704. (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i;
  3705. return (perf_prof->total_exec_time - perf_prof->children_exec_time)
  3706. / 1000.0f;
  3707. }
  3708. }
  3709. return -1.0;
  3710. }
  3711. #endif /* end of WASM_ENABLE_PERF_PROFILING != 0 */
  3712. #if WASM_ENABLE_STATIC_PGO != 0
  3713. /* indirect call target */
  3714. #define IPVK_IndirectCallTarget 0
  3715. /* memory intrinsic functions size */
  3716. #define IPVK_MemOPSize 1
  3717. #define IPVK_First IPVK_IndirectCallTarget
  3718. #define IPVK_Last IPVK_MemOPSize
  3719. #define INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE 24
  3720. #define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
  3721. static int hasNonDefaultValsPerSite = 0;
  3722. static uint32 VPMaxNumValsPerSite = INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE;
  3723. static bool
  3724. cmpxchg_ptr(void **ptr, void *old_val, void *new_val)
  3725. {
  3726. #if defined(os_atomic_cmpxchg)
  3727. return os_atomic_cmpxchg(ptr, &old_val, new_val);
  3728. #else
  3729. /* TODO: add lock when thread-manager is enabled */
  3730. void *read = *ptr;
  3731. if (read == old_val) {
  3732. *ptr = new_val;
  3733. return true;
  3734. }
  3735. return false;
  3736. #endif
  3737. }
  3738. static int
  3739. allocateValueProfileCounters(LLVMProfileData *Data)
  3740. {
  3741. ValueProfNode **Mem;
  3742. uint64 NumVSites = 0, total_size;
  3743. uint32 VKI;
  3744. /* When dynamic allocation is enabled, allow tracking the max number of
  3745. values allowed. */
  3746. if (!hasNonDefaultValsPerSite)
  3747. VPMaxNumValsPerSite = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
  3748. for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
  3749. NumVSites += Data->num_value_sites[VKI];
  3750. /* If NumVSites = 0, calloc is allowed to return a non-null pointer. */
  3751. bh_assert(NumVSites > 0 && "NumVSites can't be zero");
  3752. total_size = (uint64)sizeof(ValueProfNode *) * NumVSites;
  3753. if (total_size > UINT32_MAX
  3754. || !(Mem = (ValueProfNode **)wasm_runtime_malloc((uint32)total_size))) {
  3755. return 0;
  3756. }
  3757. memset(Mem, 0, (uint32)total_size);
  3758. if (!cmpxchg_ptr((void **)&Data->values, NULL, Mem)) {
  3759. wasm_runtime_free(Mem);
  3760. return 0;
  3761. }
  3762. return 1;
  3763. }
  3764. static ValueProfNode *
  3765. allocateOneNode(void)
  3766. {
  3767. ValueProfNode *Node;
  3768. Node = wasm_runtime_malloc((uint32)sizeof(ValueProfNode));
  3769. if (Node)
  3770. memset(Node, 0, sizeof(ValueProfNode));
  3771. return Node;
  3772. }
  3773. static void
  3774. instrumentTargetValueImpl(uint64 TargetValue, void *Data, uint32 CounterIndex,
  3775. uint64 CountValue)
  3776. {
  3777. ValueProfNode **ValueCounters;
  3778. ValueProfNode *PrevVNode = NULL, *MinCountVNode = NULL, *CurVNode;
  3779. LLVMProfileData *PData = (LLVMProfileData *)Data;
  3780. uint64 MinCount = UINT64_MAX;
  3781. uint8 VDataCount = 0;
  3782. bool success = false;
  3783. if (!PData)
  3784. return;
  3785. if (!CountValue)
  3786. return;
  3787. if (!PData->values) {
  3788. if (!allocateValueProfileCounters(PData))
  3789. return;
  3790. }
  3791. ValueCounters = (ValueProfNode **)PData->values;
  3792. CurVNode = ValueCounters[CounterIndex];
  3793. while (CurVNode) {
  3794. if (TargetValue == CurVNode->value) {
  3795. CurVNode->count += CountValue;
  3796. return;
  3797. }
  3798. if (CurVNode->count < MinCount) {
  3799. MinCount = CurVNode->count;
  3800. MinCountVNode = CurVNode;
  3801. }
  3802. PrevVNode = CurVNode;
  3803. CurVNode = CurVNode->next;
  3804. ++VDataCount;
  3805. }
  3806. if (VDataCount >= VPMaxNumValsPerSite) {
  3807. if (MinCountVNode->count <= CountValue) {
  3808. CurVNode = MinCountVNode;
  3809. CurVNode->value = TargetValue;
  3810. CurVNode->count = CountValue;
  3811. }
  3812. else
  3813. MinCountVNode->count -= CountValue;
  3814. return;
  3815. }
  3816. CurVNode = allocateOneNode();
  3817. if (!CurVNode)
  3818. return;
  3819. CurVNode->value = TargetValue;
  3820. CurVNode->count += CountValue;
  3821. if (!ValueCounters[CounterIndex]) {
  3822. success =
  3823. cmpxchg_ptr((void **)&ValueCounters[CounterIndex], NULL, CurVNode);
  3824. }
  3825. else if (PrevVNode && !PrevVNode->next) {
  3826. success = cmpxchg_ptr((void **)&PrevVNode->next, 0, CurVNode);
  3827. }
  3828. if (!success) {
  3829. wasm_runtime_free(CurVNode);
  3830. }
  3831. }
  3832. void
  3833. llvm_profile_instrument_target(uint64 target_value, void *data,
  3834. uint32 counter_idx)
  3835. {
  3836. instrumentTargetValueImpl(target_value, data, counter_idx, 1);
  3837. }
  3838. static inline uint32
  3839. popcount64(uint64 u)
  3840. {
  3841. uint32 ret = 0;
  3842. while (u) {
  3843. u = (u & (u - 1));
  3844. ret++;
  3845. }
  3846. return ret;
  3847. }
  3848. static inline uint32
  3849. clz64(uint64 type)
  3850. {
  3851. uint32 num = 0;
  3852. if (type == 0)
  3853. return 64;
  3854. while (!(type & 0x8000000000000000LL)) {
  3855. num++;
  3856. type <<= 1;
  3857. }
  3858. return num;
  3859. }
  3860. /* Map an (observed) memop size value to the representative value of its range.
  3861. For example, 5 -> 5, 22 -> 17, 99 -> 65, 256 -> 256, 1001 -> 513. */
  3862. static uint64
  3863. InstrProfGetRangeRepValue(uint64 Value)
  3864. {
  3865. if (Value <= 8)
  3866. /* The first ranges are individually tracked. Use the value as is. */
  3867. return Value;
  3868. else if (Value >= 513)
  3869. /* The last range is mapped to its lowest value. */
  3870. return 513;
  3871. else if (popcount64(Value) == 1)
  3872. /* If it's a power of two, use it as is. */
  3873. return Value;
  3874. else
  3875. /* Otherwise, take to the previous power of two + 1. */
  3876. return (((uint64)1) << (64 - clz64(Value) - 1)) + 1;
  3877. }
  3878. void
  3879. llvm_profile_instrument_memop(uint64 target_value, void *data,
  3880. uint32 counter_idx)
  3881. {
  3882. uint64 rep_value = InstrProfGetRangeRepValue(target_value);
  3883. instrumentTargetValueImpl(rep_value, data, counter_idx, 1);
  3884. }
  3885. static uint32
  3886. get_pgo_prof_data_size(AOTModuleInstance *module_inst, uint32 *p_num_prof_data,
  3887. uint32 *p_num_prof_counters, uint32 *p_padding_size,
  3888. uint32 *p_prof_counters_size, uint32 *p_prof_names_size,
  3889. uint32 *p_value_counters_size, uint8 **p_prof_names)
  3890. {
  3891. AOTModule *module = (AOTModule *)module_inst->module;
  3892. LLVMProfileData *prof_data;
  3893. uint8 *prof_names = NULL;
  3894. uint32 num_prof_data = 0, num_prof_counters = 0, padding_size, i;
  3895. uint32 prof_counters_size = 0, prof_names_size = 0;
  3896. uint32 total_size, total_size_wo_value_counters;
  3897. for (i = 0; i < module->data_section_count; i++) {
  3898. if (!strncmp(module->data_sections[i].name, "__llvm_prf_data", 15)) {
  3899. bh_assert(module->data_sections[i].size == sizeof(LLVMProfileData));
  3900. num_prof_data++;
  3901. prof_data = (LLVMProfileData *)module->data_sections[i].data;
  3902. num_prof_counters += prof_data->num_counters;
  3903. }
  3904. else if (!strncmp(module->data_sections[i].name, "__llvm_prf_cnts",
  3905. 15)) {
  3906. prof_counters_size += module->data_sections[i].size;
  3907. }
  3908. else if (!strncmp(module->data_sections[i].name, "__llvm_prf_names",
  3909. 16)) {
  3910. prof_names_size = module->data_sections[i].size;
  3911. prof_names = module->data_sections[i].data;
  3912. }
  3913. }
  3914. if (prof_counters_size != num_prof_counters * sizeof(uint64))
  3915. return 0;
  3916. total_size = sizeof(LLVMProfileRawHeader)
  3917. + num_prof_data * sizeof(LLVMProfileData_64)
  3918. + prof_counters_size + prof_names_size;
  3919. padding_size = sizeof(uint64) - (prof_names_size % sizeof(uint64));
  3920. if (padding_size != sizeof(uint64))
  3921. total_size += padding_size;
  3922. /* Total size excluding value counters */
  3923. total_size_wo_value_counters = total_size;
  3924. for (i = 0; i < module->data_section_count; i++) {
  3925. if (!strncmp(module->data_sections[i].name, "__llvm_prf_data", 15)) {
  3926. uint32 j, k, num_value_sites, num_value_nodes;
  3927. ValueProfNode **values, *value_node;
  3928. prof_data = (LLVMProfileData *)module->data_sections[i].data;
  3929. values = prof_data->values;
  3930. if (prof_data->num_value_sites[0] > 0
  3931. || prof_data->num_value_sites[1] > 0) {
  3932. /* TotalSize (uint32) and NumValueKinds (uint32) */
  3933. total_size += 8;
  3934. for (j = 0; j < 2; j++) {
  3935. if ((num_value_sites = prof_data->num_value_sites[j]) > 0) {
  3936. /* ValueKind (uint32) and NumValueSites (uint32) */
  3937. total_size += 8;
  3938. /* (Value + Counter) group counts of each value site,
  3939. each count is one byte */
  3940. total_size += align_uint(num_value_sites, 8);
  3941. if (values) {
  3942. for (k = 0; k < num_value_sites; k++) {
  3943. num_value_nodes = 0;
  3944. value_node = *values;
  3945. while (value_node) {
  3946. num_value_nodes++;
  3947. value_node = value_node->next;
  3948. }
  3949. if (num_value_nodes) {
  3950. /* (Value + Counter) groups */
  3951. total_size += num_value_nodes * 8 * 2;
  3952. }
  3953. values++;
  3954. }
  3955. }
  3956. }
  3957. }
  3958. }
  3959. }
  3960. }
  3961. if (p_num_prof_data)
  3962. *p_num_prof_data = num_prof_data;
  3963. if (p_num_prof_counters)
  3964. *p_num_prof_counters = num_prof_counters;
  3965. if (p_padding_size)
  3966. *p_padding_size = padding_size;
  3967. if (p_prof_counters_size)
  3968. *p_prof_counters_size = prof_counters_size;
  3969. if (p_prof_names_size)
  3970. *p_prof_names_size = prof_names_size;
  3971. if (p_value_counters_size)
  3972. *p_value_counters_size = total_size - total_size_wo_value_counters;
  3973. if (p_prof_names)
  3974. *p_prof_names = prof_names;
  3975. return total_size;
  3976. }
  3977. uint32
  3978. aot_get_pgo_prof_data_size(AOTModuleInstance *module_inst)
  3979. {
  3980. return get_pgo_prof_data_size(module_inst, NULL, NULL, NULL, NULL, NULL,
  3981. NULL, NULL);
  3982. }
  3983. static union {
  3984. int a;
  3985. char b;
  3986. } __ue = { .a = 1 };
  3987. #define is_little_endian() (__ue.b == 1)
  3988. uint32
  3989. aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
  3990. uint32 len)
  3991. {
  3992. AOTModule *module = (AOTModule *)module_inst->module;
  3993. LLVMProfileRawHeader prof_header = { 0 };
  3994. LLVMProfileData *prof_data;
  3995. uint8 *prof_names = NULL;
  3996. uint32 num_prof_data = 0, num_prof_counters = 0, padding_size, i;
  3997. uint32 prof_counters_size = 0, prof_names_size = 0;
  3998. uint32 value_counters_size = 0, value_counters_size_backup = 0;
  3999. uint32 total_size, size;
  4000. int64 counters_delta, offset_counters;
  4001. total_size = get_pgo_prof_data_size(module_inst, &num_prof_data,
  4002. &num_prof_counters, &padding_size,
  4003. &prof_counters_size, &prof_names_size,
  4004. &value_counters_size, &prof_names);
  4005. if (len < total_size)
  4006. return 0;
  4007. value_counters_size_backup = value_counters_size;
  4008. value_counters_size = 0;
  4009. prof_header.counters_delta = counters_delta =
  4010. sizeof(LLVMProfileData_64) * num_prof_data;
  4011. offset_counters = 0;
  4012. for (i = 0; i < module->data_section_count; i++) {
  4013. if (!strncmp(module->data_sections[i].name, "__llvm_prf_data", 15)) {
  4014. prof_data = (LLVMProfileData *)module->data_sections[i].data;
  4015. prof_data->offset_counters = counters_delta + offset_counters;
  4016. offset_counters += prof_data->num_counters * sizeof(uint64);
  4017. counters_delta -= sizeof(LLVMProfileData_64);
  4018. }
  4019. }
  4020. prof_header.magic = 0xFF6C70726F667281LL;
  4021. /* Version 8 */
  4022. prof_header.version = 0x0000000000000008LL;
  4023. /* with VARIANT_MASK_IR_PROF (IR Instrumentation) */
  4024. prof_header.version |= 0x1ULL << 56;
  4025. /* with VARIANT_MASK_MEMPROF (Memory Profile) */
  4026. prof_header.version |= 0x1ULL << 62;
  4027. prof_header.num_prof_data = num_prof_data;
  4028. prof_header.num_prof_counters = num_prof_counters;
  4029. prof_header.names_size = prof_names_size;
  4030. prof_header.value_kind_last = 1;
  4031. if (!is_little_endian()) {
  4032. aot_exchange_uint64((uint8 *)&prof_header.magic);
  4033. aot_exchange_uint64((uint8 *)&prof_header.version);
  4034. aot_exchange_uint64((uint8 *)&prof_header.num_prof_data);
  4035. aot_exchange_uint64((uint8 *)&prof_header.num_prof_counters);
  4036. aot_exchange_uint64((uint8 *)&prof_header.names_size);
  4037. aot_exchange_uint64((uint8 *)&prof_header.counters_delta);
  4038. aot_exchange_uint64((uint8 *)&prof_header.value_kind_last);
  4039. }
  4040. size = sizeof(LLVMProfileRawHeader);
  4041. bh_memcpy_s(buf, size, &prof_header, size);
  4042. buf += size;
  4043. for (i = 0; i < module->data_section_count; i++) {
  4044. if (!strncmp(module->data_sections[i].name, "__llvm_prf_data", 15)) {
  4045. LLVMProfileData_64 *prof_data_64 = (LLVMProfileData_64 *)buf;
  4046. /* Convert LLVMProfileData to LLVMProfileData_64, the pointer width
  4047. in the output file is always 8 bytes */
  4048. prof_data = (LLVMProfileData *)module->data_sections[i].data;
  4049. prof_data_64->func_md5 = prof_data->func_md5;
  4050. prof_data_64->func_hash = prof_data->func_hash;
  4051. prof_data_64->offset_counters = prof_data->offset_counters;
  4052. prof_data_64->func_ptr = prof_data->func_ptr;
  4053. prof_data_64->values = (uint64)(uintptr_t)prof_data->values;
  4054. prof_data_64->num_counters = prof_data->num_counters;
  4055. prof_data_64->num_value_sites[0] = prof_data->num_value_sites[0];
  4056. prof_data_64->num_value_sites[1] = prof_data->num_value_sites[1];
  4057. if (!is_little_endian()) {
  4058. aot_exchange_uint64((uint8 *)&prof_data_64->func_hash);
  4059. aot_exchange_uint64((uint8 *)&prof_data_64->offset_counters);
  4060. aot_exchange_uint64((uint8 *)&prof_data_64->offset_counters);
  4061. aot_exchange_uint64((uint8 *)&prof_data_64->func_ptr);
  4062. aot_exchange_uint64((uint8 *)&prof_data_64->values);
  4063. aot_exchange_uint32((uint8 *)&prof_data_64->num_counters);
  4064. aot_exchange_uint16((uint8 *)&prof_data_64->num_value_sites[0]);
  4065. aot_exchange_uint16((uint8 *)&prof_data_64->num_value_sites[1]);
  4066. }
  4067. buf += sizeof(LLVMProfileData_64);
  4068. }
  4069. }
  4070. for (i = 0; i < module->data_section_count; i++) {
  4071. if (!strncmp(module->data_sections[i].name, "__llvm_prf_cnts", 15)) {
  4072. size = module->data_sections[i].size;
  4073. bh_memcpy_s(buf, size, module->data_sections[i].data, size);
  4074. buf += size;
  4075. }
  4076. }
  4077. if (prof_names && prof_names_size > 0) {
  4078. size = prof_names_size;
  4079. bh_memcpy_s(buf, size, prof_names, size);
  4080. buf += size;
  4081. padding_size = sizeof(uint64) - (prof_names_size % sizeof(uint64));
  4082. if (padding_size != sizeof(uint64)) {
  4083. char padding_buf[8] = { 0 };
  4084. bh_memcpy_s(buf, padding_size, padding_buf, padding_size);
  4085. buf += padding_size;
  4086. }
  4087. }
  4088. for (i = 0; i < module->data_section_count; i++) {
  4089. if (!strncmp(module->data_sections[i].name, "__llvm_prf_data", 15)) {
  4090. uint32 j, k, num_value_sites, num_value_nodes;
  4091. ValueProfNode **values, **values_tmp, *value_node;
  4092. prof_data = (LLVMProfileData *)module->data_sections[i].data;
  4093. values = values_tmp = prof_data->values;
  4094. if (prof_data->num_value_sites[0] > 0
  4095. || prof_data->num_value_sites[1] > 0) {
  4096. uint32 *buf_total_size = (uint32 *)buf;
  4097. buf += 4; /* emit TotalSize later */
  4098. *(uint32 *)buf = (prof_data->num_value_sites[0] > 0
  4099. && prof_data->num_value_sites[1] > 0)
  4100. ? 2
  4101. : 1;
  4102. if (!is_little_endian())
  4103. aot_exchange_uint32((uint8 *)buf);
  4104. buf += 4;
  4105. for (j = 0; j < 2; j++) {
  4106. if ((num_value_sites = prof_data->num_value_sites[j]) > 0) {
  4107. /* ValueKind */
  4108. *(uint32 *)buf = j;
  4109. if (!is_little_endian())
  4110. aot_exchange_uint32((uint8 *)buf);
  4111. buf += 4;
  4112. /* NumValueSites */
  4113. *(uint32 *)buf = num_value_sites;
  4114. if (!is_little_endian())
  4115. aot_exchange_uint32((uint8 *)buf);
  4116. buf += 4;
  4117. for (k = 0; k < num_value_sites; k++) {
  4118. num_value_nodes = 0;
  4119. if (values_tmp) {
  4120. value_node = *values_tmp;
  4121. while (value_node) {
  4122. num_value_nodes++;
  4123. value_node = value_node->next;
  4124. }
  4125. values_tmp++;
  4126. }
  4127. bh_assert(num_value_nodes < 255);
  4128. *(uint8 *)buf++ = (uint8)num_value_nodes;
  4129. }
  4130. if (num_value_sites % 8) {
  4131. buf += 8 - (num_value_sites % 8);
  4132. }
  4133. for (k = 0; k < num_value_sites; k++) {
  4134. if (values) {
  4135. value_node = *values;
  4136. while (value_node) {
  4137. *(uint64 *)buf = value_node->value;
  4138. if (!is_little_endian())
  4139. aot_exchange_uint64((uint8 *)buf);
  4140. buf += 8;
  4141. *(uint64 *)buf = value_node->count;
  4142. if (!is_little_endian())
  4143. aot_exchange_uint64((uint8 *)buf);
  4144. buf += 8;
  4145. value_node = value_node->next;
  4146. }
  4147. values++;
  4148. }
  4149. }
  4150. }
  4151. }
  4152. /* TotalSize */
  4153. *(uint32 *)buf_total_size =
  4154. (uint8 *)buf - (uint8 *)buf_total_size;
  4155. if (!is_little_endian())
  4156. aot_exchange_uint64((uint8 *)buf_total_size);
  4157. value_counters_size += (uint8 *)buf - (uint8 *)buf_total_size;
  4158. }
  4159. }
  4160. }
  4161. bh_assert(value_counters_size == value_counters_size_backup);
  4162. (void)value_counters_size_backup;
  4163. return total_size;
  4164. }
  4165. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  4166. #if WASM_ENABLE_GC != 0
  4167. void *
  4168. aot_create_func_obj(AOTModuleInstance *module_inst, uint32 func_idx,
  4169. bool throw_exce, char *error_buf, uint32 error_buf_size)
  4170. {
  4171. AOTModule *module = (AOTModule *)module_inst->module;
  4172. WASMRttTypeRef rtt_type;
  4173. WASMFuncObjectRef func_obj;
  4174. AOTFuncType *func_type;
  4175. uint32 type_idx;
  4176. if (throw_exce) {
  4177. error_buf = module_inst->cur_exception;
  4178. error_buf_size = sizeof(module_inst->cur_exception);
  4179. }
  4180. if (func_idx >= module->import_func_count + module->func_count) {
  4181. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  4182. func_idx);
  4183. return NULL;
  4184. }
  4185. type_idx = module_inst->func_type_indexes[func_idx];
  4186. func_type = (AOTFuncType *)module->types[type_idx];
  4187. if (!(rtt_type = wasm_rtt_type_new((AOTType *)func_type, type_idx,
  4188. module->rtt_types, module->type_count,
  4189. &module->rtt_type_lock))) {
  4190. set_error_buf(error_buf, error_buf_size, "create rtt object failed");
  4191. return NULL;
  4192. }
  4193. if (!(func_obj = wasm_func_obj_new_internal(
  4194. ((AOTModuleInstanceExtra *)module_inst->e)->common.gc_heap_handle,
  4195. rtt_type, func_idx))) {
  4196. set_error_buf(error_buf, error_buf_size, "create func object failed");
  4197. return NULL;
  4198. }
  4199. return func_obj;
  4200. }
  4201. bool
  4202. aot_obj_is_instance_of(AOTModuleInstance *module_inst, WASMObjectRef gc_obj,
  4203. uint32 type_index)
  4204. {
  4205. AOTModule *aot_module = (AOTModule *)module_inst->module;
  4206. AOTType **types = aot_module->types;
  4207. uint32 type_count = aot_module->type_count;
  4208. return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
  4209. }
  4210. bool
  4211. aot_func_type_is_super_of(AOTModuleInstance *module_inst, uint32 type_idx1,
  4212. uint32 type_idx2)
  4213. {
  4214. AOTModule *aot_module = (AOTModule *)module_inst->module;
  4215. AOTType **types = aot_module->types;
  4216. if (type_idx1 == type_idx2)
  4217. return true;
  4218. bh_assert(types[type_idx1]->type_flag == WASM_TYPE_FUNC);
  4219. bh_assert(types[type_idx2]->type_flag == WASM_TYPE_FUNC);
  4220. return wasm_func_type_is_super_of((WASMFuncType *)types[type_idx1],
  4221. (WASMFuncType *)types[type_idx2]);
  4222. }
  4223. WASMRttTypeRef
  4224. aot_rtt_type_new(AOTModuleInstance *module_inst, uint32 type_index)
  4225. {
  4226. AOTModule *aot_module = (AOTModule *)module_inst->module;
  4227. AOTType *defined_type = aot_module->types[type_index];
  4228. WASMRttType **rtt_types = aot_module->rtt_types;
  4229. uint32 rtt_type_count = aot_module->type_count;
  4230. korp_mutex *rtt_type_lock = &aot_module->rtt_type_lock;
  4231. return wasm_rtt_type_new(defined_type, type_index, rtt_types,
  4232. rtt_type_count, rtt_type_lock);
  4233. }
  4234. bool
  4235. aot_array_init_with_data(AOTModuleInstance *module_inst, uint32 seg_index,
  4236. uint32 data_seg_offset, WASMArrayObjectRef array_obj,
  4237. uint32 elem_size, uint32 array_len)
  4238. {
  4239. AOTModule *aot_module;
  4240. uint8 *data = NULL;
  4241. uint8 *array_elem_base;
  4242. uint64 seg_len = 0;
  4243. uint64 total_size = (int64)elem_size * array_len;
  4244. aot_module = (AOTModule *)module_inst->module;
  4245. seg_len = aot_module->mem_init_data_list[seg_index]->byte_count;
  4246. data = aot_module->mem_init_data_list[seg_index]->bytes;
  4247. if (data_seg_offset >= seg_len || total_size > seg_len - data_seg_offset) {
  4248. aot_set_exception(module_inst, "out of bounds memory access");
  4249. return false;
  4250. }
  4251. array_elem_base = (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  4252. bh_memcpy_s(array_elem_base, (uint32)total_size, data + data_seg_offset,
  4253. (uint32)total_size);
  4254. return true;
  4255. }
  4256. static bool
  4257. aot_global_traverse_gc_rootset(AOTModuleInstance *module_inst, void *heap)
  4258. {
  4259. AOTModule *module = (AOTModule *)module_inst->module;
  4260. uint8 *global_data = module_inst->global_data;
  4261. AOTImportGlobal *import_global = module->import_globals;
  4262. AOTGlobal *global = module->globals;
  4263. WASMObjectRef gc_obj;
  4264. uint32 i;
  4265. for (i = 0; i < module->import_global_count; i++, import_global++) {
  4266. if (wasm_is_type_reftype(import_global->type.val_type)) {
  4267. gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
  4268. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4269. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  4270. return false;
  4271. }
  4272. }
  4273. global_data += import_global->size;
  4274. }
  4275. for (i = 0; i < module->global_count; i++, global++) {
  4276. if (wasm_is_type_reftype(global->type.val_type)) {
  4277. gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
  4278. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4279. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  4280. return false;
  4281. }
  4282. }
  4283. global_data += global->size;
  4284. }
  4285. return true;
  4286. }
  4287. static bool
  4288. aot_table_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
  4289. {
  4290. AOTTableInstance **tables = (AOTTableInstance **)module_inst->tables;
  4291. AOTTableInstance *table;
  4292. uint32 table_count = module_inst->table_count, i, j;
  4293. WASMObjectRef gc_obj, *table_elems;
  4294. for (i = 0; i < table_count; i++) {
  4295. table = tables[i];
  4296. table_elems = (WASMObjectRef *)table->elems;
  4297. for (j = 0; j < table->cur_size; j++) {
  4298. gc_obj = table_elems[j];
  4299. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4300. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  4301. return false;
  4302. }
  4303. }
  4304. }
  4305. return true;
  4306. }
  4307. static bool
  4308. local_object_refs_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  4309. {
  4310. WASMLocalObjectRef *r;
  4311. WASMObjectRef gc_obj;
  4312. for (r = exec_env->cur_local_object_ref; r; r = r->prev) {
  4313. gc_obj = r->val;
  4314. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4315. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  4316. return false;
  4317. }
  4318. }
  4319. return true;
  4320. }
  4321. static bool
  4322. aot_frame_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  4323. {
  4324. AOTFrame *frame;
  4325. AOTModule *module;
  4326. LocalRefFlag frame_local_flags;
  4327. WASMObjectRef gc_obj;
  4328. uint32 i, local_ref_flag_cell_num;
  4329. module = (AOTModule *)wasm_exec_env_get_module(exec_env);
  4330. frame = (AOTFrame *)wasm_exec_env_get_cur_frame(exec_env);
  4331. for (; frame; frame = frame->prev_frame) {
  4332. /* local ref flags */
  4333. frame_local_flags = module->func_local_ref_flags[frame->func_index];
  4334. local_ref_flag_cell_num = frame_local_flags.local_ref_flag_cell_num;
  4335. for (i = 0; i < local_ref_flag_cell_num; i++) {
  4336. if (frame_local_flags.local_ref_flags[i]) {
  4337. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  4338. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4339. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  4340. return false;
  4341. }
  4342. }
  4343. #if UINTPTR_MAX == UINT64_MAX
  4344. bh_assert(frame_local_flags.local_ref_flags[i + 1]);
  4345. i++;
  4346. #endif
  4347. }
  4348. }
  4349. /* stack ref flags */
  4350. uint8 *frame_ref = frame->frame_ref;
  4351. for (i = local_ref_flag_cell_num; i < (uint32)(frame->sp - frame->lp);
  4352. i++) {
  4353. if (frame_ref[i]) {
  4354. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  4355. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4356. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  4357. return false;
  4358. }
  4359. }
  4360. #if UINTPTR_MAX == UINT64_MAX
  4361. bh_assert(frame_ref[i + 1]);
  4362. i++;
  4363. #endif
  4364. }
  4365. }
  4366. }
  4367. return true;
  4368. }
  4369. bool
  4370. aot_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  4371. {
  4372. AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
  4373. bool ret;
  4374. ret = aot_global_traverse_gc_rootset(module_inst, heap);
  4375. if (!ret)
  4376. return ret;
  4377. ret = aot_table_traverse_gc_rootset(module_inst, heap);
  4378. if (!ret)
  4379. return ret;
  4380. ret = local_object_refs_traverse_gc_rootset(exec_env, heap);
  4381. if (!ret)
  4382. return ret;
  4383. ret = aot_frame_traverse_gc_rootset(exec_env, heap);
  4384. if (!ret)
  4385. return ret;
  4386. return true;
  4387. }
  4388. #endif /* end of WASM_ENABLE_GC != 0 */
  4389. char *
  4390. aot_const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  4391. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  4392. bool is_vram_word_align,
  4393. #endif
  4394. char *error_buf, uint32 error_buf_size)
  4395. {
  4396. HashMap *set = module->const_str_set;
  4397. char *c_str, *value;
  4398. /* Create const string set if it isn't created */
  4399. if (!set
  4400. && !(set = module->const_str_set = bh_hash_map_create(
  4401. 32, false, (HashFunc)wasm_string_hash,
  4402. (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) {
  4403. set_error_buf(error_buf, error_buf_size,
  4404. "create const string set failed");
  4405. return NULL;
  4406. }
  4407. /* Lookup const string set, use the string if found */
  4408. if (!(c_str = runtime_malloc((uint32)len, error_buf, error_buf_size))) {
  4409. return NULL;
  4410. }
  4411. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  4412. if (is_vram_word_align) {
  4413. bh_memcpy_wa(c_str, (uint32)len, str, (uint32)len);
  4414. }
  4415. else
  4416. #endif
  4417. {
  4418. bh_memcpy_s(c_str, len, str, (uint32)len);
  4419. }
  4420. if ((value = bh_hash_map_find(set, c_str))) {
  4421. wasm_runtime_free(c_str);
  4422. return value;
  4423. }
  4424. if (!bh_hash_map_insert(set, c_str, c_str)) {
  4425. set_error_buf(error_buf, error_buf_size,
  4426. "insert string to hash map failed");
  4427. wasm_runtime_free(c_str);
  4428. return NULL;
  4429. }
  4430. return c_str;
  4431. }
  4432. #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
  4433. AOTModule *g_dynamic_aot_module = NULL;
  4434. void __attribute__((noinline)) __enable_dynamic_aot_debug(void)
  4435. {
  4436. /* empty implementation. */
  4437. }
  4438. void (*__enable_dynamic_aot_debug_ptr)(void)
  4439. __attribute__((visibility("default"))) = __enable_dynamic_aot_debug;
  4440. #endif
  4441. bool
  4442. aot_set_module_name(AOTModule *module, const char *name, char *error_buf,
  4443. uint32_t error_buf_size)
  4444. {
  4445. if (!name)
  4446. return false;
  4447. module->name = aot_const_str_set_insert((const uint8 *)name,
  4448. (uint32)(strlen(name) + 1), module,
  4449. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  4450. false,
  4451. #endif
  4452. error_buf, error_buf_size);
  4453. #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
  4454. /* export g_dynamic_aot_module for dynamic aot debug */
  4455. g_dynamic_aot_module = module;
  4456. /* trigger breakpoint __enable_dynamic_aot_debug */
  4457. (*__enable_dynamic_aot_debug_ptr)();
  4458. #endif
  4459. return module->name != NULL;
  4460. }
  4461. const char *
  4462. aot_get_module_name(AOTModule *module)
  4463. {
  4464. return module->name;
  4465. }