aot_runtime.c 193 KB

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