wasm_runtime_common.c 203 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "bh_common.h"
  7. #include "bh_assert.h"
  8. #include "bh_log.h"
  9. #include "wasm_native.h"
  10. #include "wasm_runtime_common.h"
  11. #include "wasm_memory.h"
  12. #if WASM_ENABLE_INTERP != 0
  13. #include "../interpreter/wasm_runtime.h"
  14. #endif
  15. #if WASM_ENABLE_AOT != 0
  16. #include "../aot/aot_runtime.h"
  17. #if WASM_ENABLE_DEBUG_AOT != 0
  18. #include "../aot/debug/jit_debug.h"
  19. #endif
  20. #endif
  21. #if WASM_ENABLE_GC != 0
  22. #include "gc/gc_object.h"
  23. #endif
  24. #if WASM_ENABLE_THREAD_MGR != 0
  25. #include "../libraries/thread-mgr/thread_manager.h"
  26. #if WASM_ENABLE_DEBUG_INTERP != 0
  27. #include "../libraries/debug-engine/debug_engine.h"
  28. #endif
  29. #endif
  30. #if WASM_ENABLE_SHARED_MEMORY != 0
  31. #include "wasm_shared_memory.h"
  32. #endif
  33. #if WASM_ENABLE_FAST_JIT != 0
  34. #include "../fast-jit/jit_compiler.h"
  35. #endif
  36. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  37. #include "../compilation/aot_llvm.h"
  38. #endif
  39. #include "../common/wasm_c_api_internal.h"
  40. #include "../../version.h"
  41. /**
  42. * For runtime build, BH_MALLOC/BH_FREE should be defined as
  43. * wasm_runtime_malloc/wasm_runtime_free.
  44. */
  45. #define CHECK(a) CHECK1(a)
  46. #define CHECK1(a) SHOULD_BE_##a
  47. #define SHOULD_BE_wasm_runtime_malloc 1
  48. #if !CHECK(BH_MALLOC)
  49. #error unexpected BH_MALLOC
  50. #endif
  51. #undef SHOULD_BE_wasm_runtime_malloc
  52. #define SHOULD_BE_wasm_runtime_free 1
  53. #if !CHECK(BH_FREE)
  54. #error unexpected BH_FREE
  55. #endif
  56. #undef SHOULD_BE_wasm_runtime_free
  57. #undef CHECK
  58. #undef CHECK1
  59. #if WASM_ENABLE_MULTI_MODULE != 0
  60. /**
  61. * A safety insurance to prevent
  62. * circular depencies which leads stack overflow
  63. * try to break early
  64. */
  65. typedef struct LoadingModule {
  66. bh_list_link l;
  67. /* point to a string pool */
  68. const char *module_name;
  69. } LoadingModule;
  70. static bh_list loading_module_list_head;
  71. static bh_list *const loading_module_list = &loading_module_list_head;
  72. static korp_mutex loading_module_list_lock;
  73. /**
  74. * A list to store all exported functions/globals/memories/tables
  75. * of every fully loaded module
  76. */
  77. static bh_list registered_module_list_head;
  78. static bh_list *const registered_module_list = &registered_module_list_head;
  79. static korp_mutex registered_module_list_lock;
  80. static void
  81. wasm_runtime_destroy_registered_module_list();
  82. #endif /* WASM_ENABLE_MULTI_MODULE */
  83. #define E_TYPE_XIP 4
  84. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  85. /* Initialize externref hashmap */
  86. static bool
  87. wasm_externref_map_init();
  88. /* Destroy externref hashmap */
  89. static void
  90. wasm_externref_map_destroy();
  91. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  92. static void
  93. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  94. {
  95. if (error_buf != NULL)
  96. snprintf(error_buf, error_buf_size, "%s", string);
  97. }
  98. static void *
  99. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  100. char *error_buf, uint32 error_buf_size)
  101. {
  102. void *mem;
  103. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  104. if (module_inst != NULL) {
  105. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  106. }
  107. else if (error_buf != NULL) {
  108. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  109. }
  110. return NULL;
  111. }
  112. memset(mem, 0, (uint32)size);
  113. return mem;
  114. }
  115. #if WASM_ENABLE_MULTI_MODULE != 0
  116. /* TODO: Let loader_malloc be a general API both for AOT and WASM. */
  117. #define loader_malloc(size, error_buf, error_buf_size) \
  118. runtime_malloc(size, NULL, error_buf, error_buf_size)
  119. static void
  120. set_error_buf_v(const WASMModuleCommon *module, char *error_buf,
  121. uint32 error_buf_size, const char *format, ...)
  122. {
  123. va_list args;
  124. char buf[128];
  125. if (error_buf != NULL) {
  126. va_start(args, format);
  127. vsnprintf(buf, sizeof(buf), format, args);
  128. va_end(args);
  129. if (module->module_type == Wasm_Module_AoT) {
  130. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  131. buf);
  132. }
  133. else if (module->module_type == Wasm_Module_Bytecode) {
  134. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  135. buf);
  136. }
  137. }
  138. }
  139. #endif
  140. #if WASM_ENABLE_FAST_JIT != 0
  141. static JitCompOptions jit_options = { 0 };
  142. #endif
  143. #if WASM_ENABLE_JIT != 0
  144. static LLVMJITOptions llvm_jit_options = { 3, 3, 0 };
  145. #endif
  146. #if WASM_ENABLE_GC != 0
  147. static uint32 gc_heap_size_default = GC_HEAP_SIZE_DEFAULT;
  148. #endif
  149. static RunningMode runtime_running_mode = Mode_Default;
  150. #ifdef OS_ENABLE_HW_BOUND_CHECK
  151. /* The exec_env of thread local storage, set before calling function
  152. and used in signal handler, as we cannot get it from the argument
  153. of signal handler */
  154. static os_thread_local_attribute WASMExecEnv *exec_env_tls = NULL;
  155. #ifndef BH_PLATFORM_WINDOWS
  156. static void
  157. runtime_signal_handler(void *sig_addr)
  158. {
  159. WASMModuleInstance *module_inst;
  160. WASMMemoryInstance *memory_inst;
  161. WASMJmpBuf *jmpbuf_node;
  162. uint8 *mapped_mem_start_addr = NULL;
  163. uint8 *mapped_mem_end_addr = NULL;
  164. uint32 page_size = os_getpagesize();
  165. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  166. uint8 *stack_min_addr;
  167. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  168. #endif
  169. /* Check whether current thread is running wasm function */
  170. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  171. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  172. /* Get mapped mem info of current instance */
  173. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  174. /* Get the default memory instance */
  175. memory_inst = wasm_get_default_memory(module_inst);
  176. if (memory_inst) {
  177. mapped_mem_start_addr = memory_inst->memory_data;
  178. mapped_mem_end_addr = memory_inst->memory_data + 8 * (uint64)BH_GB;
  179. }
  180. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  181. /* Get stack info of current thread */
  182. stack_min_addr = os_thread_get_stack_boundary();
  183. #endif
  184. if (memory_inst
  185. && (mapped_mem_start_addr <= (uint8 *)sig_addr
  186. && (uint8 *)sig_addr < mapped_mem_end_addr)) {
  187. /* The address which causes segmentation fault is inside
  188. the memory instance's guard regions */
  189. wasm_set_exception(module_inst, "out of bounds memory access");
  190. os_longjmp(jmpbuf_node->jmpbuf, 1);
  191. }
  192. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  193. else if (stack_min_addr - page_size <= (uint8 *)sig_addr
  194. && (uint8 *)sig_addr
  195. < stack_min_addr + page_size * guard_page_count) {
  196. /* The address which causes segmentation fault is inside
  197. native thread's guard page */
  198. wasm_set_exception(module_inst, "native stack overflow");
  199. os_longjmp(jmpbuf_node->jmpbuf, 1);
  200. }
  201. #endif
  202. else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
  203. && (uint8 *)sig_addr
  204. < exec_env_tls->exce_check_guard_page + page_size) {
  205. bh_assert(wasm_copy_exception(module_inst, NULL));
  206. os_longjmp(jmpbuf_node->jmpbuf, 1);
  207. }
  208. }
  209. }
  210. #else /* else of BH_PLATFORM_WINDOWS */
  211. #if WASM_ENABLE_AOT != 0
  212. #include <Zydis/Zydis.h>
  213. static uint32
  214. decode_insn(uint8 *insn)
  215. {
  216. uint8 *data = (uint8 *)insn;
  217. uint32 length = 32; /* reserve enough size */
  218. /* Initialize decoder context */
  219. ZydisDecoder decoder;
  220. ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64,
  221. ZYDIS_STACK_WIDTH_64);
  222. /* Initialize formatter */
  223. ZydisFormatter formatter;
  224. ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
  225. /* Loop over the instructions in our buffer */
  226. ZyanU64 runtime_address = (ZyanU64)(uintptr_t)data;
  227. ZyanUSize offset = 0;
  228. ZydisDecodedInstruction instruction;
  229. ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
  230. char buffer[256];
  231. if (ZYAN_SUCCESS(ZydisDecoderDecodeFull(
  232. &decoder, data + offset, length - offset, &instruction, operands,
  233. ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
  234. ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY))) {
  235. /* Format & print the binary instruction structure to
  236. human readable format */
  237. ZydisFormatterFormatInstruction(&formatter, &instruction, operands,
  238. instruction.operand_count_visible,
  239. buffer, sizeof(buffer),
  240. runtime_address);
  241. /* Print current instruction */
  242. /*
  243. os_printf("%012" PRIX64 " ", runtime_address);
  244. puts(buffer);
  245. */
  246. return instruction.length;
  247. }
  248. /* Decode failed */
  249. return 0;
  250. }
  251. #endif /* end of WASM_ENABLE_AOT != 0 */
  252. static LONG
  253. next_action(WASMModuleInstance *module_inst, EXCEPTION_POINTERS *exce_info)
  254. {
  255. #if WASM_ENABLE_AOT != 0
  256. uint32 insn_size;
  257. #endif
  258. if (module_inst->module_type == Wasm_Module_Bytecode
  259. && module_inst->e->running_mode == Mode_Interp) {
  260. /* Continue to search next exception handler for
  261. interpreter mode as it can be caught by
  262. `__try { .. } __except { .. }` sentences in
  263. wasm_runtime.c */
  264. return EXCEPTION_CONTINUE_SEARCH;
  265. }
  266. #if WASM_ENABLE_AOT != 0
  267. /* Skip current instruction and continue to run for AOT/JIT mode.
  268. TODO: implement unwind support for AOT/JIT code in Windows platform */
  269. insn_size = decode_insn((uint8 *)exce_info->ContextRecord->Rip);
  270. if (insn_size > 0) {
  271. exce_info->ContextRecord->Rip += insn_size;
  272. return EXCEPTION_CONTINUE_EXECUTION;
  273. }
  274. #endif
  275. /* return different value from EXCEPTION_CONTINUE_SEARCH (= 0)
  276. and EXCEPTION_CONTINUE_EXECUTION (= -1) */
  277. return -2;
  278. }
  279. static LONG
  280. runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
  281. {
  282. PEXCEPTION_RECORD ExceptionRecord = exce_info->ExceptionRecord;
  283. uint8 *sig_addr = (uint8 *)ExceptionRecord->ExceptionInformation[1];
  284. WASMModuleInstance *module_inst;
  285. WASMMemoryInstance *memory_inst;
  286. WASMJmpBuf *jmpbuf_node;
  287. uint8 *mapped_mem_start_addr = NULL;
  288. uint8 *mapped_mem_end_addr = NULL;
  289. uint32 page_size = os_getpagesize();
  290. LONG ret;
  291. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  292. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  293. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  294. if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
  295. /* Get the default memory instance */
  296. memory_inst = wasm_get_default_memory(module_inst);
  297. if (memory_inst) {
  298. mapped_mem_start_addr = memory_inst->memory_data;
  299. mapped_mem_end_addr =
  300. memory_inst->memory_data + 8 * (uint64)BH_GB;
  301. }
  302. if (memory_inst && mapped_mem_start_addr <= (uint8 *)sig_addr
  303. && (uint8 *)sig_addr < mapped_mem_end_addr) {
  304. /* The address which causes segmentation fault is inside
  305. the memory instance's guard regions.
  306. Set exception and let the wasm func continue to run, when
  307. the wasm func returns, the caller will check whether the
  308. exception is thrown and return to runtime. */
  309. wasm_set_exception(module_inst, "out of bounds memory access");
  310. ret = next_action(module_inst, exce_info);
  311. if (ret == EXCEPTION_CONTINUE_SEARCH
  312. || ret == EXCEPTION_CONTINUE_EXECUTION)
  313. return ret;
  314. }
  315. else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
  316. && (uint8 *)sig_addr
  317. < exec_env_tls->exce_check_guard_page + page_size) {
  318. bh_assert(wasm_copy_exception(module_inst, NULL));
  319. ret = next_action(module_inst, exce_info);
  320. if (ret == EXCEPTION_CONTINUE_SEARCH
  321. || ret == EXCEPTION_CONTINUE_EXECUTION)
  322. return ret;
  323. }
  324. }
  325. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  326. else if (ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
  327. /* Set stack overflow exception and let the wasm func continue
  328. to run, when the wasm func returns, the caller will check
  329. whether the exception is thrown and return to runtime, and
  330. the damaged stack will be recovered by _resetstkoflw(). */
  331. wasm_set_exception(module_inst, "native stack overflow");
  332. ret = next_action(module_inst, exce_info);
  333. if (ret == EXCEPTION_CONTINUE_SEARCH
  334. || ret == EXCEPTION_CONTINUE_EXECUTION)
  335. return ret;
  336. }
  337. #endif
  338. }
  339. os_printf("Unhandled exception thrown: exception code: 0x%lx, "
  340. "exception address: %p, exception information: %p\n",
  341. ExceptionRecord->ExceptionCode, ExceptionRecord->ExceptionAddress,
  342. sig_addr);
  343. return EXCEPTION_CONTINUE_SEARCH;
  344. }
  345. #endif /* end of BH_PLATFORM_WINDOWS */
  346. static bool
  347. runtime_signal_init()
  348. {
  349. #ifndef BH_PLATFORM_WINDOWS
  350. return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false;
  351. #else
  352. if (os_thread_signal_init() != 0)
  353. return false;
  354. if (!AddVectoredExceptionHandler(1, runtime_exception_handler)) {
  355. os_thread_signal_destroy();
  356. return false;
  357. }
  358. #endif
  359. return true;
  360. }
  361. static void
  362. runtime_signal_destroy()
  363. {
  364. #ifdef BH_PLATFORM_WINDOWS
  365. RemoveVectoredExceptionHandler(runtime_exception_handler);
  366. #endif
  367. os_thread_signal_destroy();
  368. }
  369. void
  370. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env)
  371. {
  372. exec_env_tls = exec_env;
  373. }
  374. WASMExecEnv *
  375. wasm_runtime_get_exec_env_tls()
  376. {
  377. return exec_env_tls;
  378. }
  379. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  380. static bool
  381. wasm_runtime_env_init()
  382. {
  383. if (bh_platform_init() != 0)
  384. return false;
  385. if (wasm_native_init() == false) {
  386. goto fail1;
  387. }
  388. #if WASM_ENABLE_MULTI_MODULE
  389. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  390. goto fail2;
  391. }
  392. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  393. goto fail3;
  394. }
  395. #endif
  396. #if WASM_ENABLE_SHARED_MEMORY
  397. if (!wasm_shared_memory_init()) {
  398. goto fail4;
  399. }
  400. #endif
  401. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  402. if (!thread_manager_init()) {
  403. goto fail5;
  404. }
  405. #endif
  406. #ifdef OS_ENABLE_HW_BOUND_CHECK
  407. if (!runtime_signal_init()) {
  408. goto fail6;
  409. }
  410. #endif
  411. #if WASM_ENABLE_AOT != 0
  412. #if WASM_ENABLE_DEBUG_AOT != 0
  413. if (!jit_debug_engine_init()) {
  414. goto fail7;
  415. }
  416. #endif
  417. #endif
  418. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  419. if (!wasm_externref_map_init()) {
  420. goto fail8;
  421. }
  422. #endif
  423. #if WASM_ENABLE_FAST_JIT != 0
  424. if (!jit_compiler_init(&jit_options)) {
  425. goto fail9;
  426. }
  427. #endif
  428. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  429. if (!aot_compiler_init()) {
  430. goto fail10;
  431. }
  432. #endif
  433. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  434. if (os_blocking_op_init() != BHT_OK) {
  435. goto fail11;
  436. }
  437. os_end_blocking_op();
  438. #endif
  439. return true;
  440. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  441. fail11:
  442. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  443. aot_compiler_destroy();
  444. #endif
  445. #endif
  446. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  447. fail10:
  448. #if WASM_ENABLE_FAST_JIT != 0
  449. jit_compiler_destroy();
  450. #endif
  451. #endif
  452. #if WASM_ENABLE_FAST_JIT != 0
  453. fail9:
  454. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  455. wasm_externref_map_destroy();
  456. #endif
  457. #endif
  458. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  459. fail8:
  460. #endif
  461. #if WASM_ENABLE_AOT != 0
  462. #if WASM_ENABLE_DEBUG_AOT != 0
  463. jit_debug_engine_destroy();
  464. fail7:
  465. #endif
  466. #endif
  467. #ifdef OS_ENABLE_HW_BOUND_CHECK
  468. runtime_signal_destroy();
  469. fail6:
  470. #endif
  471. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  472. thread_manager_destroy();
  473. fail5:
  474. #endif
  475. #if WASM_ENABLE_SHARED_MEMORY
  476. wasm_shared_memory_destroy();
  477. fail4:
  478. #endif
  479. #if WASM_ENABLE_MULTI_MODULE
  480. os_mutex_destroy(&loading_module_list_lock);
  481. fail3:
  482. os_mutex_destroy(&registered_module_list_lock);
  483. fail2:
  484. #endif
  485. wasm_native_destroy();
  486. fail1:
  487. bh_platform_destroy();
  488. return false;
  489. }
  490. static bool
  491. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  492. {
  493. return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0
  494. && exec_env->wasm_stack.top_boundary
  495. == exec_env->wasm_stack.bottom + exec_env->wasm_stack_size
  496. && exec_env->wasm_stack.top <= exec_env->wasm_stack.top_boundary;
  497. }
  498. bool
  499. wasm_runtime_init()
  500. {
  501. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  502. return false;
  503. if (!wasm_runtime_env_init()) {
  504. wasm_runtime_memory_destroy();
  505. return false;
  506. }
  507. return true;
  508. }
  509. void
  510. wasm_runtime_destroy()
  511. {
  512. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  513. wasm_externref_map_destroy();
  514. #endif
  515. #if WASM_ENABLE_AOT != 0
  516. #if WASM_ENABLE_DEBUG_AOT != 0
  517. jit_debug_engine_destroy();
  518. #endif
  519. #endif
  520. #ifdef OS_ENABLE_HW_BOUND_CHECK
  521. runtime_signal_destroy();
  522. #endif
  523. /* runtime env destroy */
  524. #if WASM_ENABLE_MULTI_MODULE
  525. wasm_runtime_destroy_loading_module_list();
  526. os_mutex_destroy(&loading_module_list_lock);
  527. wasm_runtime_destroy_registered_module_list();
  528. os_mutex_destroy(&registered_module_list_lock);
  529. #endif
  530. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  531. /* Destroy LLVM-JIT compiler after destroying the modules
  532. * loaded by multi-module feature, since these modules may
  533. * create backend threads to compile the wasm functions,
  534. * which may access the LLVM resources. We wait until they
  535. * finish the compilation to avoid accessing the destroyed
  536. * resources in the compilation threads.
  537. */
  538. aot_compiler_destroy();
  539. #endif
  540. #if WASM_ENABLE_FAST_JIT != 0
  541. /* Destroy Fast-JIT compiler after destroying the modules
  542. * loaded by multi-module feature, since the Fast JIT's
  543. * code cache allocator may be used by these modules.
  544. */
  545. jit_compiler_destroy();
  546. #endif
  547. #if WASM_ENABLE_SHARED_MEMORY
  548. wasm_shared_memory_destroy();
  549. #endif
  550. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  551. #if WASM_ENABLE_DEBUG_INTERP != 0
  552. wasm_debug_engine_destroy();
  553. #endif
  554. thread_manager_destroy();
  555. #endif
  556. wasm_native_destroy();
  557. bh_platform_destroy();
  558. wasm_runtime_memory_destroy();
  559. }
  560. RunningMode
  561. wasm_runtime_get_default_running_mode(void)
  562. {
  563. return runtime_running_mode;
  564. }
  565. #if WASM_ENABLE_JIT != 0
  566. LLVMJITOptions
  567. wasm_runtime_get_llvm_jit_options(void)
  568. {
  569. return llvm_jit_options;
  570. }
  571. #endif
  572. #if WASM_ENABLE_GC != 0
  573. uint32
  574. wasm_runtime_get_gc_heap_size_default(void)
  575. {
  576. return gc_heap_size_default;
  577. }
  578. #endif
  579. bool
  580. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  581. {
  582. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  583. &init_args->mem_alloc_option))
  584. return false;
  585. if (!wasm_runtime_set_default_running_mode(init_args->running_mode)) {
  586. wasm_runtime_memory_destroy();
  587. return false;
  588. }
  589. #if WASM_ENABLE_FAST_JIT != 0
  590. jit_options.code_cache_size = init_args->fast_jit_code_cache_size;
  591. #endif
  592. #if WASM_ENABLE_GC != 0
  593. gc_heap_size_default = init_args->gc_heap_size;
  594. #endif
  595. #if WASM_ENABLE_JIT != 0
  596. llvm_jit_options.size_level = init_args->llvm_jit_size_level;
  597. llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
  598. llvm_jit_options.segue_flags = init_args->segue_flags;
  599. #endif
  600. #if WASM_ENABLE_LINUX_PERF != 0
  601. wasm_runtime_set_linux_perf(init_args->enable_linux_perf);
  602. #else
  603. if (init_args->enable_linux_perf)
  604. LOG_WARNING("warning: to enable linux perf support, please recompile "
  605. "with -DWAMR_BUILD_LINUX_PERF=1");
  606. #endif
  607. if (!wasm_runtime_env_init()) {
  608. wasm_runtime_memory_destroy();
  609. return false;
  610. }
  611. #if WASM_ENABLE_DEBUG_INTERP != 0
  612. if (strlen(init_args->ip_addr))
  613. if (!wasm_debug_engine_init(init_args->ip_addr,
  614. init_args->instance_port)) {
  615. wasm_runtime_destroy();
  616. return false;
  617. }
  618. #endif
  619. if (init_args->n_native_symbols > 0
  620. && !wasm_runtime_register_natives(init_args->native_module_name,
  621. init_args->native_symbols,
  622. init_args->n_native_symbols)) {
  623. wasm_runtime_destroy();
  624. return false;
  625. }
  626. #if WASM_ENABLE_THREAD_MGR != 0
  627. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  628. #endif
  629. return true;
  630. }
  631. void
  632. wasm_runtime_set_log_level(log_level_t level)
  633. {
  634. bh_log_set_verbose_level(level);
  635. }
  636. bool
  637. wasm_runtime_is_running_mode_supported(RunningMode running_mode)
  638. {
  639. if (running_mode == Mode_Default) {
  640. return true;
  641. }
  642. else if (running_mode == Mode_Interp) {
  643. #if WASM_ENABLE_INTERP != 0
  644. return true;
  645. #endif
  646. }
  647. else if (running_mode == Mode_Fast_JIT) {
  648. #if WASM_ENABLE_FAST_JIT != 0
  649. return true;
  650. #endif
  651. }
  652. else if (running_mode == Mode_LLVM_JIT) {
  653. #if WASM_ENABLE_JIT != 0
  654. return true;
  655. #endif
  656. }
  657. else if (running_mode == Mode_Multi_Tier_JIT) {
  658. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  659. && WASM_ENABLE_LAZY_JIT != 0
  660. return true;
  661. #endif
  662. }
  663. return false;
  664. }
  665. bool
  666. wasm_runtime_set_default_running_mode(RunningMode running_mode)
  667. {
  668. if (wasm_runtime_is_running_mode_supported(running_mode)) {
  669. runtime_running_mode = running_mode;
  670. return true;
  671. }
  672. return false;
  673. }
  674. PackageType
  675. get_package_type(const uint8 *buf, uint32 size)
  676. {
  677. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  678. uint32 buf32 = *(uint32 *)buf;
  679. buf = (const uint8 *)&buf32;
  680. #endif
  681. if (buf && size >= 4) {
  682. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  683. return Wasm_Module_Bytecode;
  684. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  685. return Wasm_Module_AoT;
  686. }
  687. return Package_Type_Unknown;
  688. }
  689. #if WASM_ENABLE_AOT != 0
  690. static uint8 *
  691. align_ptr(const uint8 *p, uint32 b)
  692. {
  693. uintptr_t v = (uintptr_t)p;
  694. uintptr_t m = b - 1;
  695. return (uint8 *)((v + m) & ~m);
  696. }
  697. #define CHECK_BUF(buf, buf_end, length) \
  698. do { \
  699. if ((uintptr_t)buf + length < (uintptr_t)buf \
  700. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  701. return false; \
  702. } while (0)
  703. #define read_uint16(p, p_end, res) \
  704. do { \
  705. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  706. CHECK_BUF(p, p_end, sizeof(uint16)); \
  707. res = *(uint16 *)p; \
  708. p += sizeof(uint16); \
  709. } while (0)
  710. #define read_uint32(p, p_end, res) \
  711. do { \
  712. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  713. CHECK_BUF(p, p_end, sizeof(uint32)); \
  714. res = *(uint32 *)p; \
  715. p += sizeof(uint32); \
  716. } while (0)
  717. bool
  718. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
  719. {
  720. const uint8 *p = buf, *p_end = buf + size;
  721. uint32 section_type, section_size;
  722. uint16 e_type;
  723. if (get_package_type(buf, size) != Wasm_Module_AoT)
  724. return false;
  725. CHECK_BUF(p, p_end, 8);
  726. p += 8;
  727. while (p < p_end) {
  728. read_uint32(p, p_end, section_type);
  729. read_uint32(p, p_end, section_size);
  730. CHECK_BUF(p, p_end, section_size);
  731. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  732. p += 4;
  733. read_uint16(p, p_end, e_type);
  734. return (e_type == E_TYPE_XIP) ? true : false;
  735. }
  736. else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
  737. return false;
  738. }
  739. p += section_size;
  740. }
  741. return false;
  742. }
  743. #endif /* end of WASM_ENABLE_AOT */
  744. #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
  745. uint32
  746. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
  747. {
  748. WASMModuleInstanceCommon *module_inst =
  749. wasm_runtime_get_module_inst(exec_env);
  750. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  751. bh_assert(module_inst);
  752. bh_assert(cluster);
  753. if (module_inst->module_type != Wasm_Module_Bytecode) {
  754. LOG_WARNING("Attempt to create a debug instance for an AOT module");
  755. return 0;
  756. }
  757. if (cluster->debug_inst) {
  758. LOG_WARNING("Cluster already bind to a debug instance");
  759. return cluster->debug_inst->control_thread->port;
  760. }
  761. if (wasm_debug_instance_create(cluster, port)) {
  762. return cluster->debug_inst->control_thread->port;
  763. }
  764. return 0;
  765. }
  766. uint32
  767. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
  768. {
  769. return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
  770. }
  771. #endif
  772. #if WASM_ENABLE_MULTI_MODULE != 0
  773. static module_reader reader;
  774. static module_destroyer destroyer;
  775. void
  776. wasm_runtime_set_module_reader(const module_reader reader_cb,
  777. const module_destroyer destroyer_cb)
  778. {
  779. reader = reader_cb;
  780. destroyer = destroyer_cb;
  781. }
  782. module_reader
  783. wasm_runtime_get_module_reader()
  784. {
  785. return reader;
  786. }
  787. module_destroyer
  788. wasm_runtime_get_module_destroyer()
  789. {
  790. return destroyer;
  791. }
  792. static WASMRegisteredModule *
  793. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  794. {
  795. WASMRegisteredModule *reg_module = NULL;
  796. os_mutex_lock(&registered_module_list_lock);
  797. reg_module = bh_list_first_elem(registered_module_list);
  798. while (reg_module && module != reg_module->module) {
  799. reg_module = bh_list_elem_next(reg_module);
  800. }
  801. os_mutex_unlock(&registered_module_list_lock);
  802. return reg_module;
  803. }
  804. bool
  805. wasm_runtime_register_module_internal(const char *module_name,
  806. WASMModuleCommon *module,
  807. uint8 *orig_file_buf,
  808. uint32 orig_file_buf_size,
  809. char *error_buf, uint32 error_buf_size)
  810. {
  811. WASMRegisteredModule *node = NULL;
  812. node = wasm_runtime_find_module_registered_by_reference(module);
  813. if (node) { /* module has been registered */
  814. if (node->module_name) { /* module has name */
  815. if (!module_name || strcmp(node->module_name, module_name)) {
  816. /* module has different name */
  817. LOG_DEBUG("module(%p) has been registered with name %s", module,
  818. node->module_name);
  819. set_error_buf(error_buf, error_buf_size,
  820. "Register module failed: "
  821. "failed to rename the module");
  822. return false;
  823. }
  824. else {
  825. /* module has the same name */
  826. LOG_DEBUG(
  827. "module(%p) has been registered with the same name %s",
  828. module, node->module_name);
  829. return true;
  830. }
  831. }
  832. else {
  833. /* module has empyt name, reset it */
  834. node->module_name = module_name;
  835. return true;
  836. }
  837. }
  838. /* module hasn't been registered */
  839. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  840. if (!node) {
  841. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  842. sizeof(WASMRegisteredModule));
  843. return false;
  844. }
  845. /* share the string and the module */
  846. node->module_name = module_name;
  847. node->module = module;
  848. node->orig_file_buf = orig_file_buf;
  849. node->orig_file_buf_size = orig_file_buf_size;
  850. os_mutex_lock(&registered_module_list_lock);
  851. bh_list_status ret = bh_list_insert(registered_module_list, node);
  852. bh_assert(BH_LIST_SUCCESS == ret);
  853. (void)ret;
  854. os_mutex_unlock(&registered_module_list_lock);
  855. return true;
  856. }
  857. bool
  858. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  859. char *error_buf, uint32 error_buf_size)
  860. {
  861. if (!error_buf || !error_buf_size) {
  862. LOG_ERROR("error buffer is required");
  863. return false;
  864. }
  865. if (!module_name || !module) {
  866. LOG_DEBUG("module_name and module are required");
  867. set_error_buf(error_buf, error_buf_size,
  868. "Register module failed: "
  869. "module_name and module are required");
  870. return false;
  871. }
  872. if (wasm_runtime_is_built_in_module(module_name)) {
  873. LOG_DEBUG("%s is a built-in module name", module_name);
  874. set_error_buf(error_buf, error_buf_size,
  875. "Register module failed: "
  876. "can not register as a built-in module");
  877. return false;
  878. }
  879. return wasm_runtime_register_module_internal(module_name, module, NULL, 0,
  880. error_buf, error_buf_size);
  881. }
  882. void
  883. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  884. {
  885. WASMRegisteredModule *registered_module = NULL;
  886. os_mutex_lock(&registered_module_list_lock);
  887. registered_module = bh_list_first_elem(registered_module_list);
  888. while (registered_module && module != registered_module->module) {
  889. registered_module = bh_list_elem_next(registered_module);
  890. }
  891. /* it does not matter if it is not exist. after all, it is gone */
  892. if (registered_module) {
  893. bh_list_remove(registered_module_list, registered_module);
  894. wasm_runtime_free(registered_module);
  895. }
  896. os_mutex_unlock(&registered_module_list_lock);
  897. }
  898. WASMModuleCommon *
  899. wasm_runtime_find_module_registered(const char *module_name)
  900. {
  901. WASMRegisteredModule *module = NULL, *module_next;
  902. os_mutex_lock(&registered_module_list_lock);
  903. module = bh_list_first_elem(registered_module_list);
  904. while (module) {
  905. module_next = bh_list_elem_next(module);
  906. if (module->module_name && !strcmp(module_name, module->module_name)) {
  907. break;
  908. }
  909. module = module_next;
  910. }
  911. os_mutex_unlock(&registered_module_list_lock);
  912. return module ? module->module : NULL;
  913. }
  914. /*
  915. * simply destroy all
  916. */
  917. static void
  918. wasm_runtime_destroy_registered_module_list()
  919. {
  920. WASMRegisteredModule *reg_module = NULL;
  921. os_mutex_lock(&registered_module_list_lock);
  922. reg_module = bh_list_first_elem(registered_module_list);
  923. while (reg_module) {
  924. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  925. bh_list_remove(registered_module_list, reg_module);
  926. /* now, it is time to release every module in the runtime */
  927. if (reg_module->module->module_type == Wasm_Module_Bytecode) {
  928. #if WASM_ENABLE_INTERP != 0
  929. wasm_unload((WASMModule *)reg_module->module);
  930. #endif
  931. }
  932. else {
  933. #if WASM_ENABLE_AOT != 0
  934. aot_unload((AOTModule *)reg_module->module);
  935. #endif
  936. }
  937. /* destroy the file buffer */
  938. if (destroyer && reg_module->orig_file_buf) {
  939. destroyer(reg_module->orig_file_buf,
  940. reg_module->orig_file_buf_size);
  941. reg_module->orig_file_buf = NULL;
  942. reg_module->orig_file_buf_size = 0;
  943. }
  944. wasm_runtime_free(reg_module);
  945. reg_module = next_reg_module;
  946. }
  947. os_mutex_unlock(&registered_module_list_lock);
  948. }
  949. bool
  950. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  951. uint32 error_buf_size)
  952. {
  953. LOG_DEBUG("add %s into a loading list", module_name);
  954. LoadingModule *loadingModule =
  955. runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size);
  956. if (!loadingModule) {
  957. return false;
  958. }
  959. /* share the incoming string */
  960. loadingModule->module_name = module_name;
  961. os_mutex_lock(&loading_module_list_lock);
  962. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  963. bh_assert(BH_LIST_SUCCESS == ret);
  964. (void)ret;
  965. os_mutex_unlock(&loading_module_list_lock);
  966. return true;
  967. }
  968. void
  969. wasm_runtime_delete_loading_module(const char *module_name)
  970. {
  971. LOG_DEBUG("delete %s from a loading list", module_name);
  972. LoadingModule *module = NULL;
  973. os_mutex_lock(&loading_module_list_lock);
  974. module = bh_list_first_elem(loading_module_list);
  975. while (module && strcmp(module->module_name, module_name)) {
  976. module = bh_list_elem_next(module);
  977. }
  978. /* it does not matter if it is not exist. after all, it is gone */
  979. if (module) {
  980. bh_list_remove(loading_module_list, module);
  981. wasm_runtime_free(module);
  982. }
  983. os_mutex_unlock(&loading_module_list_lock);
  984. }
  985. bool
  986. wasm_runtime_is_loading_module(const char *module_name)
  987. {
  988. LOG_DEBUG("find %s in a loading list", module_name);
  989. LoadingModule *module = NULL;
  990. os_mutex_lock(&loading_module_list_lock);
  991. module = bh_list_first_elem(loading_module_list);
  992. while (module && strcmp(module_name, module->module_name)) {
  993. module = bh_list_elem_next(module);
  994. }
  995. os_mutex_unlock(&loading_module_list_lock);
  996. return module != NULL;
  997. }
  998. void
  999. wasm_runtime_destroy_loading_module_list()
  1000. {
  1001. LoadingModule *module = NULL;
  1002. os_mutex_lock(&loading_module_list_lock);
  1003. module = bh_list_first_elem(loading_module_list);
  1004. while (module) {
  1005. LoadingModule *next_module = bh_list_elem_next(module);
  1006. bh_list_remove(loading_module_list, module);
  1007. /*
  1008. * will not free the module_name since it is
  1009. * shared one of the const string pool
  1010. */
  1011. wasm_runtime_free(module);
  1012. module = next_module;
  1013. }
  1014. os_mutex_unlock(&loading_module_list_lock);
  1015. }
  1016. #endif /* WASM_ENABLE_MULTI_MODULE */
  1017. bool
  1018. wasm_runtime_is_built_in_module(const char *module_name)
  1019. {
  1020. return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name)
  1021. || !strcmp("wasi_snapshot_preview1", module_name)
  1022. #if WASM_ENABLE_SPEC_TEST != 0
  1023. || !strcmp("spectest", module_name)
  1024. #endif
  1025. || !strcmp("", module_name));
  1026. }
  1027. #if WASM_ENABLE_THREAD_MGR != 0
  1028. bool
  1029. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  1030. uint32 size)
  1031. {
  1032. WASMModuleInstanceCommon *module_inst =
  1033. wasm_exec_env_get_module_inst(exec_env);
  1034. #if WASM_ENABLE_INTERP != 0
  1035. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1036. return wasm_set_aux_stack(exec_env, start_offset, size);
  1037. }
  1038. #endif
  1039. #if WASM_ENABLE_AOT != 0
  1040. if (module_inst->module_type == Wasm_Module_AoT) {
  1041. return aot_set_aux_stack(exec_env, start_offset, size);
  1042. }
  1043. #endif
  1044. return false;
  1045. }
  1046. bool
  1047. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  1048. uint32 *size)
  1049. {
  1050. WASMModuleInstanceCommon *module_inst =
  1051. wasm_exec_env_get_module_inst(exec_env);
  1052. #if WASM_ENABLE_INTERP != 0
  1053. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1054. return wasm_get_aux_stack(exec_env, start_offset, size);
  1055. }
  1056. #endif
  1057. #if WASM_ENABLE_AOT != 0
  1058. if (module_inst->module_type == Wasm_Module_AoT) {
  1059. return aot_get_aux_stack(exec_env, start_offset, size);
  1060. }
  1061. #endif
  1062. return false;
  1063. }
  1064. void
  1065. wasm_runtime_set_max_thread_num(uint32 num)
  1066. {
  1067. wasm_cluster_set_max_thread_num(num);
  1068. }
  1069. #endif /* end of WASM_ENABLE_THREAD_MGR */
  1070. static WASMModuleCommon *
  1071. register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf,
  1072. uint32 error_buf_size)
  1073. {
  1074. #if WASM_ENABLE_MULTI_MODULE != 0
  1075. if (module_common) {
  1076. if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0,
  1077. error_buf, error_buf_size)) {
  1078. wasm_runtime_unload(module_common);
  1079. return NULL;
  1080. }
  1081. return module_common;
  1082. }
  1083. else
  1084. return NULL;
  1085. #else
  1086. return module_common;
  1087. #endif
  1088. }
  1089. WASMModuleCommon *
  1090. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  1091. uint32 error_buf_size)
  1092. {
  1093. WASMModuleCommon *module_common = NULL;
  1094. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  1095. #if WASM_ENABLE_INTERP != 0
  1096. module_common =
  1097. (WASMModuleCommon *)wasm_load(buf, size,
  1098. #if WASM_ENABLE_MULTI_MODULE != 0
  1099. true,
  1100. #endif
  1101. error_buf, error_buf_size);
  1102. #endif
  1103. }
  1104. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  1105. #if WASM_ENABLE_AOT != 0
  1106. module_common = (WASMModuleCommon *)aot_load_from_aot_file(
  1107. buf, size, error_buf, error_buf_size);
  1108. #endif
  1109. }
  1110. else {
  1111. if (size < 4)
  1112. set_error_buf(error_buf, error_buf_size,
  1113. "WASM module load failed: unexpected end");
  1114. else
  1115. set_error_buf(error_buf, error_buf_size,
  1116. "WASM module load failed: magic header not detected");
  1117. return NULL;
  1118. }
  1119. if (!module_common) {
  1120. LOG_DEBUG("WASM module load failed");
  1121. return NULL;
  1122. }
  1123. return register_module_with_null_name(module_common, error_buf,
  1124. error_buf_size);
  1125. }
  1126. WASMModuleCommon *
  1127. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  1128. char *error_buf, uint32 error_buf_size)
  1129. {
  1130. WASMModuleCommon *module_common;
  1131. if (!is_aot) {
  1132. #if WASM_ENABLE_INTERP != 0
  1133. module_common = (WASMModuleCommon *)wasm_load_from_sections(
  1134. section_list, error_buf, error_buf_size);
  1135. if (!module_common) {
  1136. LOG_DEBUG("WASM module load failed from sections");
  1137. return NULL;
  1138. }
  1139. return register_module_with_null_name(module_common, error_buf,
  1140. error_buf_size);
  1141. #endif
  1142. }
  1143. else {
  1144. #if WASM_ENABLE_AOT != 0
  1145. module_common = (WASMModuleCommon *)aot_load_from_sections(
  1146. section_list, error_buf, error_buf_size);
  1147. if (!module_common) {
  1148. LOG_DEBUG("WASM module load failed from sections");
  1149. return NULL;
  1150. }
  1151. return register_module_with_null_name(module_common, error_buf,
  1152. error_buf_size);
  1153. #endif
  1154. }
  1155. #if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
  1156. set_error_buf(error_buf, error_buf_size,
  1157. "WASM module load failed: invalid section list type");
  1158. return NULL;
  1159. #endif
  1160. }
  1161. void
  1162. wasm_runtime_unload(WASMModuleCommon *module)
  1163. {
  1164. #if WASM_ENABLE_MULTI_MODULE != 0
  1165. /**
  1166. * since we will unload and free all module when runtime_destroy()
  1167. * we don't want users to unwillingly disrupt it
  1168. */
  1169. return;
  1170. #endif
  1171. #if WASM_ENABLE_INTERP != 0
  1172. if (module->module_type == Wasm_Module_Bytecode) {
  1173. wasm_unload((WASMModule *)module);
  1174. return;
  1175. }
  1176. #endif
  1177. #if WASM_ENABLE_AOT != 0
  1178. if (module->module_type == Wasm_Module_AoT) {
  1179. aot_unload((AOTModule *)module);
  1180. return;
  1181. }
  1182. #endif
  1183. }
  1184. WASMModuleInstanceCommon *
  1185. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  1186. WASMModuleInstanceCommon *parent,
  1187. WASMExecEnv *exec_env_main, uint32 stack_size,
  1188. uint32 heap_size, char *error_buf,
  1189. uint32 error_buf_size)
  1190. {
  1191. #if WASM_ENABLE_INTERP != 0
  1192. if (module->module_type == Wasm_Module_Bytecode)
  1193. return (WASMModuleInstanceCommon *)wasm_instantiate(
  1194. (WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main,
  1195. stack_size, heap_size, error_buf, error_buf_size);
  1196. #endif
  1197. #if WASM_ENABLE_AOT != 0
  1198. if (module->module_type == Wasm_Module_AoT)
  1199. return (WASMModuleInstanceCommon *)aot_instantiate(
  1200. (AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main,
  1201. stack_size, heap_size, error_buf, error_buf_size);
  1202. #endif
  1203. set_error_buf(error_buf, error_buf_size,
  1204. "Instantiate module failed, invalid module type");
  1205. return NULL;
  1206. }
  1207. WASMModuleInstanceCommon *
  1208. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  1209. uint32 heap_size, char *error_buf,
  1210. uint32 error_buf_size)
  1211. {
  1212. return wasm_runtime_instantiate_internal(
  1213. module, NULL, NULL, stack_size, heap_size, error_buf, error_buf_size);
  1214. }
  1215. void
  1216. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  1217. bool is_sub_inst)
  1218. {
  1219. #if WASM_ENABLE_INTERP != 0
  1220. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1221. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  1222. return;
  1223. }
  1224. #endif
  1225. #if WASM_ENABLE_AOT != 0
  1226. if (module_inst->module_type == Wasm_Module_AoT) {
  1227. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  1228. return;
  1229. }
  1230. #endif
  1231. }
  1232. bool
  1233. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  1234. RunningMode running_mode)
  1235. {
  1236. #if WASM_ENABLE_AOT != 0
  1237. if (module_inst->module_type == Wasm_Module_AoT)
  1238. return true;
  1239. #endif
  1240. #if WASM_ENABLE_INTERP != 0
  1241. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1242. WASMModuleInstance *module_inst_interp =
  1243. (WASMModuleInstance *)module_inst;
  1244. return wasm_set_running_mode(module_inst_interp, running_mode);
  1245. }
  1246. #endif
  1247. return false;
  1248. }
  1249. RunningMode
  1250. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst)
  1251. {
  1252. #if WASM_ENABLE_INTERP != 0
  1253. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1254. WASMModuleInstance *module_inst_interp =
  1255. (WASMModuleInstance *)module_inst;
  1256. return module_inst_interp->e->running_mode;
  1257. }
  1258. #endif
  1259. return Mode_Default;
  1260. }
  1261. void
  1262. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  1263. {
  1264. wasm_runtime_deinstantiate_internal(module_inst, false);
  1265. }
  1266. WASMModuleCommon *
  1267. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst)
  1268. {
  1269. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  1270. }
  1271. WASMExecEnv *
  1272. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  1273. uint32 stack_size)
  1274. {
  1275. return wasm_exec_env_create(module_inst, stack_size);
  1276. }
  1277. void
  1278. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  1279. {
  1280. wasm_exec_env_destroy(exec_env);
  1281. }
  1282. bool
  1283. wasm_runtime_init_thread_env(void)
  1284. {
  1285. #ifdef BH_PLATFORM_WINDOWS
  1286. if (os_thread_env_init() != 0)
  1287. return false;
  1288. #endif
  1289. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1290. if (!runtime_signal_init()) {
  1291. #ifdef BH_PLATFORM_WINDOWS
  1292. os_thread_env_destroy();
  1293. #endif
  1294. return false;
  1295. }
  1296. #endif
  1297. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  1298. os_end_blocking_op();
  1299. #endif
  1300. return true;
  1301. }
  1302. void
  1303. wasm_runtime_destroy_thread_env(void)
  1304. {
  1305. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1306. runtime_signal_destroy();
  1307. #endif
  1308. #ifdef BH_PLATFORM_WINDOWS
  1309. os_thread_env_destroy();
  1310. #endif
  1311. }
  1312. bool
  1313. wasm_runtime_thread_env_inited(void)
  1314. {
  1315. #ifdef BH_PLATFORM_WINDOWS
  1316. if (!os_thread_env_inited())
  1317. return false;
  1318. #endif
  1319. #if WASM_ENABLE_AOT != 0
  1320. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1321. if (!os_thread_signal_inited())
  1322. return false;
  1323. #endif
  1324. #endif
  1325. return true;
  1326. }
  1327. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1328. void
  1329. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  1330. {
  1331. WASMModuleMemConsumption mem_conspn = { 0 };
  1332. #if WASM_ENABLE_INTERP != 0
  1333. if (module->module_type == Wasm_Module_Bytecode) {
  1334. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  1335. }
  1336. #endif
  1337. #if WASM_ENABLE_AOT != 0
  1338. if (module->module_type == Wasm_Module_AoT) {
  1339. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  1340. }
  1341. #endif
  1342. os_printf("WASM module memory consumption, total size: %u\n",
  1343. mem_conspn.total_size);
  1344. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  1345. os_printf(" types size: %u\n", mem_conspn.types_size);
  1346. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  1347. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  1348. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1349. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1350. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1351. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1352. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  1353. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  1354. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  1355. #if WASM_ENABLE_AOT != 0
  1356. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  1357. #endif
  1358. }
  1359. void
  1360. wasm_runtime_dump_module_inst_mem_consumption(
  1361. const WASMModuleInstanceCommon *module_inst)
  1362. {
  1363. WASMModuleInstMemConsumption mem_conspn = { 0 };
  1364. #if WASM_ENABLE_INTERP != 0
  1365. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1366. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  1367. &mem_conspn);
  1368. }
  1369. #endif
  1370. #if WASM_ENABLE_AOT != 0
  1371. if (module_inst->module_type == Wasm_Module_AoT) {
  1372. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  1373. &mem_conspn);
  1374. }
  1375. #endif
  1376. os_printf("WASM module inst memory consumption, total size: %u\n",
  1377. mem_conspn.total_size);
  1378. os_printf(" module inst struct size: %u\n",
  1379. mem_conspn.module_inst_struct_size);
  1380. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1381. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  1382. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1383. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  1384. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1385. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1386. }
  1387. void
  1388. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  1389. {
  1390. uint32 total_size =
  1391. offsetof(WASMExecEnv, wasm_stack_u.bottom) + exec_env->wasm_stack_size;
  1392. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  1393. os_printf(" exec env struct size: %u\n",
  1394. offsetof(WASMExecEnv, wasm_stack_u.bottom));
  1395. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  1396. os_printf(" block addr cache size: %u\n",
  1397. sizeof(exec_env->block_addr_cache));
  1398. #endif
  1399. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  1400. }
  1401. uint32
  1402. gc_get_heap_highmark_size(void *heap);
  1403. void
  1404. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  1405. {
  1406. WASMModuleInstMemConsumption module_inst_mem_consps;
  1407. WASMModuleMemConsumption module_mem_consps;
  1408. WASMModuleInstanceCommon *module_inst_common;
  1409. WASMModuleCommon *module_common = NULL;
  1410. void *heap_handle = NULL;
  1411. uint32 total_size = 0, app_heap_peak_size = 0;
  1412. uint32 max_aux_stack_used = -1;
  1413. module_inst_common = exec_env->module_inst;
  1414. #if WASM_ENABLE_INTERP != 0
  1415. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  1416. WASMModuleInstance *wasm_module_inst =
  1417. (WASMModuleInstance *)module_inst_common;
  1418. WASMModule *wasm_module = wasm_module_inst->module;
  1419. module_common = (WASMModuleCommon *)wasm_module;
  1420. if (wasm_module_inst->memories) {
  1421. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  1422. }
  1423. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  1424. &module_inst_mem_consps);
  1425. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  1426. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  1427. max_aux_stack_used = wasm_module_inst->e->max_aux_stack_used;
  1428. }
  1429. #endif
  1430. #if WASM_ENABLE_AOT != 0
  1431. if (module_inst_common->module_type == Wasm_Module_AoT) {
  1432. AOTModuleInstance *aot_module_inst =
  1433. (AOTModuleInstance *)module_inst_common;
  1434. AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1435. module_common = (WASMModuleCommon *)aot_module;
  1436. if (aot_module_inst->memories) {
  1437. AOTMemoryInstance **memories = aot_module_inst->memories;
  1438. heap_handle = memories[0]->heap_handle;
  1439. }
  1440. aot_get_module_inst_mem_consumption(aot_module_inst,
  1441. &module_inst_mem_consps);
  1442. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  1443. }
  1444. #endif
  1445. bh_assert(module_common != NULL);
  1446. if (heap_handle) {
  1447. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  1448. }
  1449. total_size = offsetof(WASMExecEnv, wasm_stack_u.bottom)
  1450. + exec_env->wasm_stack_size + module_mem_consps.total_size
  1451. + module_inst_mem_consps.total_size;
  1452. os_printf("\nMemory consumption summary (bytes):\n");
  1453. wasm_runtime_dump_module_mem_consumption(module_common);
  1454. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  1455. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  1456. os_printf("\nTotal memory consumption of module, module inst and "
  1457. "exec env: %u\n",
  1458. total_size);
  1459. os_printf("Total interpreter stack used: %u\n",
  1460. exec_env->max_wasm_stack_used);
  1461. if (max_aux_stack_used != (uint32)-1)
  1462. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1463. else
  1464. os_printf("Total aux stack used: no enough info to profile\n");
  1465. /*
  1466. * Report the native stack usage estimation.
  1467. *
  1468. * Unlike the aux stack above, we report the amount unused
  1469. * because we don't know the stack "bottom".
  1470. *
  1471. * Note that this is just about what the runtime itself observed.
  1472. * It doesn't cover host func implementations, signal handlers, etc.
  1473. */
  1474. if (exec_env->native_stack_top_min != (void *)UINTPTR_MAX)
  1475. os_printf("Native stack left: %zd\n",
  1476. exec_env->native_stack_top_min
  1477. - exec_env->native_stack_boundary);
  1478. else
  1479. os_printf("Native stack left: no enough info to profile\n");
  1480. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1481. }
  1482. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1483. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1484. #if WASM_ENABLE_PERF_PROFILING != 0
  1485. void
  1486. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1487. {
  1488. #if WASM_ENABLE_INTERP != 0
  1489. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1490. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1491. }
  1492. #endif
  1493. #if WASM_ENABLE_AOT != 0
  1494. if (module_inst->module_type == Wasm_Module_AoT) {
  1495. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1496. }
  1497. #endif
  1498. }
  1499. #endif
  1500. WASMModuleInstanceCommon *
  1501. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1502. {
  1503. return wasm_exec_env_get_module_inst(exec_env);
  1504. }
  1505. void
  1506. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  1507. WASMModuleInstanceCommon *const module_inst)
  1508. {
  1509. wasm_exec_env_set_module_inst(exec_env, module_inst);
  1510. }
  1511. void *
  1512. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1513. {
  1514. return exec_env->attachment;
  1515. }
  1516. void
  1517. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1518. {
  1519. exec_env->user_data = user_data;
  1520. }
  1521. void *
  1522. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1523. {
  1524. return exec_env->user_data;
  1525. }
  1526. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1527. void
  1528. wasm_runtime_access_exce_check_guard_page()
  1529. {
  1530. if (exec_env_tls && exec_env_tls->handle == os_self_thread()) {
  1531. uint32 page_size = os_getpagesize();
  1532. memset(exec_env_tls->exce_check_guard_page, 0, page_size);
  1533. }
  1534. }
  1535. #endif
  1536. WASMFuncType *
  1537. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  1538. uint32 module_type)
  1539. {
  1540. WASMFuncType *type = NULL;
  1541. #if WASM_ENABLE_INTERP != 0
  1542. if (module_type == Wasm_Module_Bytecode) {
  1543. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  1544. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  1545. : wasm_func->u.func->func_type;
  1546. }
  1547. #endif
  1548. #if WASM_ENABLE_AOT != 0
  1549. if (module_type == Wasm_Module_AoT) {
  1550. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  1551. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  1552. : aot_func->u.func.func_type;
  1553. }
  1554. #endif
  1555. return type;
  1556. }
  1557. WASMFunctionInstanceCommon *
  1558. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  1559. const char *name, const char *signature)
  1560. {
  1561. #if WASM_ENABLE_INTERP != 0
  1562. if (module_inst->module_type == Wasm_Module_Bytecode)
  1563. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  1564. (const WASMModuleInstance *)module_inst, name, signature);
  1565. #endif
  1566. #if WASM_ENABLE_AOT != 0
  1567. if (module_inst->module_type == Wasm_Module_AoT)
  1568. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  1569. (const AOTModuleInstance *)module_inst, name, signature);
  1570. #endif
  1571. return NULL;
  1572. }
  1573. uint32
  1574. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  1575. WASMModuleInstanceCommon *const module_inst)
  1576. {
  1577. WASMFuncType *type =
  1578. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1579. bh_assert(type);
  1580. return type->param_count;
  1581. }
  1582. uint32
  1583. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  1584. WASMModuleInstanceCommon *const module_inst)
  1585. {
  1586. WASMFuncType *type =
  1587. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1588. bh_assert(type);
  1589. return type->result_count;
  1590. }
  1591. static uint8
  1592. val_type_to_val_kind(uint8 value_type)
  1593. {
  1594. switch (value_type) {
  1595. case VALUE_TYPE_I32:
  1596. return WASM_I32;
  1597. case VALUE_TYPE_I64:
  1598. return WASM_I64;
  1599. case VALUE_TYPE_F32:
  1600. return WASM_F32;
  1601. case VALUE_TYPE_F64:
  1602. return WASM_F64;
  1603. case VALUE_TYPE_FUNCREF:
  1604. return WASM_FUNCREF;
  1605. case VALUE_TYPE_EXTERNREF:
  1606. return WASM_ANYREF;
  1607. default:
  1608. bh_assert(0);
  1609. return 0;
  1610. }
  1611. }
  1612. void
  1613. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  1614. WASMModuleInstanceCommon *const module_inst,
  1615. wasm_valkind_t *param_types)
  1616. {
  1617. WASMFuncType *type =
  1618. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1619. uint32 i;
  1620. bh_assert(type);
  1621. for (i = 0; i < type->param_count; i++) {
  1622. param_types[i] = val_type_to_val_kind(type->types[i]);
  1623. }
  1624. }
  1625. void
  1626. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  1627. WASMModuleInstanceCommon *const module_inst,
  1628. wasm_valkind_t *result_types)
  1629. {
  1630. WASMFuncType *type =
  1631. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1632. uint32 i;
  1633. bh_assert(type);
  1634. for (i = 0; i < type->result_count; i++) {
  1635. result_types[i] =
  1636. val_type_to_val_kind(type->types[type->param_count + i]);
  1637. }
  1638. }
  1639. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1640. /* (uintptr_t)externref -> (uint32)index */
  1641. /* argv -> *ret_argv */
  1642. static bool
  1643. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  1644. WASMFunctionInstanceCommon *function,
  1645. uint32 *argv, uint32 argc, uint32 **ret_argv,
  1646. uint32 *ret_argc_param,
  1647. uint32 *ret_argc_result)
  1648. {
  1649. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  1650. result_i = 0;
  1651. bool need_param_transform = false, need_result_transform = false;
  1652. uint64 size = 0;
  1653. WASMFuncType *func_type = wasm_runtime_get_function_type(
  1654. function, exec_env->module_inst->module_type);
  1655. bh_assert(func_type);
  1656. *ret_argc_param = func_type->param_cell_num;
  1657. *ret_argc_result = func_type->ret_cell_num;
  1658. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  1659. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  1660. need_param_transform = true;
  1661. }
  1662. }
  1663. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  1664. if (VALUE_TYPE_EXTERNREF
  1665. == func_type->types[func_type->param_count + result_i]) {
  1666. need_result_transform = true;
  1667. }
  1668. }
  1669. if (!need_param_transform && !need_result_transform) {
  1670. *ret_argv = argv;
  1671. return true;
  1672. }
  1673. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  1674. size = sizeof(uint32) * func_type->param_cell_num;
  1675. }
  1676. else {
  1677. size = sizeof(uint32) * func_type->ret_cell_num;
  1678. }
  1679. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  1680. return false;
  1681. }
  1682. if (!need_param_transform) {
  1683. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  1684. }
  1685. else {
  1686. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  1687. && new_argv_i < func_type->param_cell_num;
  1688. param_i++) {
  1689. uint8 param_type = func_type->types[param_i];
  1690. if (VALUE_TYPE_EXTERNREF == param_type) {
  1691. void *externref_obj;
  1692. uint32 externref_index;
  1693. #if UINTPTR_MAX == UINT32_MAX
  1694. externref_obj = (void *)argv[argv_i];
  1695. #else
  1696. union {
  1697. uintptr_t val;
  1698. uint32 parts[2];
  1699. } u;
  1700. u.parts[0] = argv[argv_i];
  1701. u.parts[1] = argv[argv_i + 1];
  1702. externref_obj = (void *)u.val;
  1703. #endif
  1704. if (!wasm_externref_obj2ref(exec_env->module_inst,
  1705. externref_obj, &externref_index)) {
  1706. wasm_runtime_free(new_argv);
  1707. return false;
  1708. }
  1709. new_argv[new_argv_i] = externref_index;
  1710. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1711. new_argv_i++;
  1712. }
  1713. else {
  1714. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  1715. uint32 param_size = sizeof(uint32) * param_cell_num;
  1716. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  1717. param_size);
  1718. argv_i += param_cell_num;
  1719. new_argv_i += param_cell_num;
  1720. }
  1721. }
  1722. }
  1723. *ret_argv = new_argv;
  1724. return true;
  1725. }
  1726. /* (uintptr_t)externref <- (uint32)index */
  1727. /* argv <- new_argv */
  1728. static bool
  1729. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  1730. WASMFunctionInstanceCommon *function,
  1731. uint32 *argv, uint32 argc, uint32 *ret_argv)
  1732. {
  1733. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  1734. WASMFuncType *func_type;
  1735. bh_assert((argv && ret_argv) || (argc == 0));
  1736. if (argv == ret_argv) {
  1737. /* no need to transfrom externref results */
  1738. return true;
  1739. }
  1740. func_type = wasm_runtime_get_function_type(
  1741. function, exec_env->module_inst->module_type);
  1742. bh_assert(func_type);
  1743. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  1744. result_i++) {
  1745. uint8 result_type = func_type->types[func_type->param_count + result_i];
  1746. if (result_type == VALUE_TYPE_EXTERNREF) {
  1747. void *externref_obj;
  1748. #if UINTPTR_MAX != UINT32_MAX
  1749. union {
  1750. uintptr_t val;
  1751. uint32 parts[2];
  1752. } u;
  1753. #endif
  1754. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  1755. wasm_runtime_free(argv);
  1756. return false;
  1757. }
  1758. #if UINTPTR_MAX == UINT32_MAX
  1759. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  1760. #else
  1761. u.val = (uintptr_t)externref_obj;
  1762. ret_argv[ret_argv_i] = u.parts[0];
  1763. ret_argv[ret_argv_i + 1] = u.parts[1];
  1764. #endif
  1765. argv_i += 1;
  1766. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1767. }
  1768. else {
  1769. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  1770. uint32 result_size = sizeof(uint32) * result_cell_num;
  1771. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  1772. result_size);
  1773. argv_i += result_cell_num;
  1774. ret_argv_i += result_cell_num;
  1775. }
  1776. }
  1777. wasm_runtime_free(argv);
  1778. return true;
  1779. }
  1780. #endif
  1781. bool
  1782. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  1783. WASMFunctionInstanceCommon *function, uint32 argc,
  1784. uint32 argv[])
  1785. {
  1786. bool ret = false;
  1787. uint32 *new_argv = NULL, param_argc;
  1788. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1789. uint32 result_argc = 0;
  1790. #endif
  1791. if (!wasm_runtime_exec_env_check(exec_env)) {
  1792. LOG_ERROR("Invalid exec env stack info.");
  1793. return false;
  1794. }
  1795. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1796. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  1797. &new_argv, &param_argc,
  1798. &result_argc)) {
  1799. wasm_runtime_set_exception(exec_env->module_inst,
  1800. "the arguments conversion is failed");
  1801. return false;
  1802. }
  1803. #else
  1804. new_argv = argv;
  1805. param_argc = argc;
  1806. #endif
  1807. #if WASM_ENABLE_INTERP != 0
  1808. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  1809. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  1810. param_argc, new_argv);
  1811. #endif
  1812. #if WASM_ENABLE_AOT != 0
  1813. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  1814. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  1815. param_argc, new_argv);
  1816. #endif
  1817. if (!ret) {
  1818. if (new_argv != argv) {
  1819. wasm_runtime_free(new_argv);
  1820. }
  1821. return false;
  1822. }
  1823. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1824. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  1825. result_argc, argv)) {
  1826. wasm_runtime_set_exception(exec_env->module_inst,
  1827. "the result conversion is failed");
  1828. return false;
  1829. }
  1830. #endif
  1831. return ret;
  1832. }
  1833. static void
  1834. parse_args_to_uint32_array(WASMFuncType *type, wasm_val_t *args,
  1835. uint32 *out_argv)
  1836. {
  1837. uint32 i, p;
  1838. for (i = 0, p = 0; i < type->param_count; i++) {
  1839. switch (args[i].kind) {
  1840. case WASM_I32:
  1841. out_argv[p++] = args[i].of.i32;
  1842. break;
  1843. case WASM_I64:
  1844. {
  1845. union {
  1846. uint64 val;
  1847. uint32 parts[2];
  1848. } u;
  1849. u.val = args[i].of.i64;
  1850. out_argv[p++] = u.parts[0];
  1851. out_argv[p++] = u.parts[1];
  1852. break;
  1853. }
  1854. case WASM_F32:
  1855. {
  1856. union {
  1857. float32 val;
  1858. uint32 part;
  1859. } u;
  1860. u.val = args[i].of.f32;
  1861. out_argv[p++] = u.part;
  1862. break;
  1863. }
  1864. case WASM_F64:
  1865. {
  1866. union {
  1867. float64 val;
  1868. uint32 parts[2];
  1869. } u;
  1870. u.val = args[i].of.f64;
  1871. out_argv[p++] = u.parts[0];
  1872. out_argv[p++] = u.parts[1];
  1873. break;
  1874. }
  1875. #if WASM_ENABLE_REF_TYPES != 0
  1876. #if WASM_ENABLE_GC == 0
  1877. case WASM_FUNCREF:
  1878. {
  1879. out_argv[p++] = args[i].of.i32;
  1880. break;
  1881. }
  1882. #else
  1883. case WASM_FUNCREF:
  1884. #endif
  1885. case WASM_ANYREF:
  1886. {
  1887. #if UINTPTR_MAX == UINT32_MAX
  1888. out_argv[p++] = args[i].of.foreign;
  1889. #else
  1890. union {
  1891. uintptr_t val;
  1892. uint32 parts[2];
  1893. } u;
  1894. u.val = (uintptr_t)args[i].of.foreign;
  1895. out_argv[p++] = u.parts[0];
  1896. out_argv[p++] = u.parts[1];
  1897. #endif
  1898. break;
  1899. }
  1900. #endif
  1901. default:
  1902. bh_assert(0);
  1903. break;
  1904. }
  1905. }
  1906. }
  1907. static void
  1908. parse_uint32_array_to_results(WASMFuncType *type, uint32 *argv,
  1909. wasm_val_t *out_results)
  1910. {
  1911. uint32 i, p;
  1912. for (i = 0, p = 0; i < type->result_count; i++) {
  1913. switch (type->types[type->param_count + i]) {
  1914. case VALUE_TYPE_I32:
  1915. out_results[i].kind = WASM_I32;
  1916. out_results[i].of.i32 = (int32)argv[p++];
  1917. break;
  1918. case VALUE_TYPE_I64:
  1919. {
  1920. union {
  1921. uint64 val;
  1922. uint32 parts[2];
  1923. } u;
  1924. u.parts[0] = argv[p++];
  1925. u.parts[1] = argv[p++];
  1926. out_results[i].kind = WASM_I64;
  1927. out_results[i].of.i64 = u.val;
  1928. break;
  1929. }
  1930. case VALUE_TYPE_F32:
  1931. {
  1932. union {
  1933. float32 val;
  1934. uint32 part;
  1935. } u;
  1936. u.part = argv[p++];
  1937. out_results[i].kind = WASM_F32;
  1938. out_results[i].of.f32 = u.val;
  1939. break;
  1940. }
  1941. case VALUE_TYPE_F64:
  1942. {
  1943. union {
  1944. float64 val;
  1945. uint32 parts[2];
  1946. } u;
  1947. u.parts[0] = argv[p++];
  1948. u.parts[1] = argv[p++];
  1949. out_results[i].kind = WASM_F64;
  1950. out_results[i].of.f64 = u.val;
  1951. break;
  1952. }
  1953. #if WASM_ENABLE_REF_TYPES != 0
  1954. #if WASM_ENABLE_GC == 0
  1955. case VALUE_TYPE_FUNCREF:
  1956. {
  1957. out_results[i].kind = WASM_I32;
  1958. out_results[i].of.i32 = (int32)argv[p++];
  1959. break;
  1960. }
  1961. case VALUE_TYPE_EXTERNREF:
  1962. #else
  1963. case REF_TYPE_FUNCREF:
  1964. case REF_TYPE_EXTERNREF:
  1965. case REF_TYPE_ANYREF:
  1966. case REF_TYPE_EQREF:
  1967. case REF_TYPE_HT_NULLABLE:
  1968. case REF_TYPE_HT_NON_NULLABLE:
  1969. case REF_TYPE_I31REF:
  1970. case REF_TYPE_NULLFUNCREF:
  1971. case REF_TYPE_NULLEXTERNREF:
  1972. case REF_TYPE_STRUCTREF:
  1973. case REF_TYPE_ARRAYREF:
  1974. case REF_TYPE_NULLREF:
  1975. #endif /* end of WASM_ENABLE_GC == 0 */
  1976. {
  1977. #if UINTPTR_MAX == UINT32_MAX
  1978. out_results[i].kind = WASM_ANYREF;
  1979. out_results[i].of.foreign = (uintptr_t)argv[p++];
  1980. #else
  1981. union {
  1982. uintptr_t val;
  1983. uint32 parts[2];
  1984. } u;
  1985. u.parts[0] = argv[p++];
  1986. u.parts[1] = argv[p++];
  1987. out_results[i].kind = WASM_ANYREF;
  1988. out_results[i].of.foreign = u.val;
  1989. #endif
  1990. break;
  1991. }
  1992. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  1993. default:
  1994. bh_assert(0);
  1995. break;
  1996. }
  1997. }
  1998. }
  1999. bool
  2000. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  2001. WASMFunctionInstanceCommon *function,
  2002. uint32 num_results, wasm_val_t results[],
  2003. uint32 num_args, wasm_val_t args[])
  2004. {
  2005. uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
  2006. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2007. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  2008. #endif
  2009. uint64 total_size;
  2010. WASMFuncType *type;
  2011. bool ret = false;
  2012. module_type = exec_env->module_inst->module_type;
  2013. type = wasm_runtime_get_function_type(function, module_type);
  2014. if (!type) {
  2015. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  2016. "enabled at least one.");
  2017. goto fail1;
  2018. }
  2019. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2020. for (i = 0; i < type->param_count; i++) {
  2021. param_size_in_double_world +=
  2022. wasm_value_type_cell_num_outside(type->types[i]);
  2023. }
  2024. for (i = 0; i < type->result_count; i++) {
  2025. result_size_in_double_world += wasm_value_type_cell_num_outside(
  2026. type->types[type->param_count + i]);
  2027. }
  2028. argc = param_size_in_double_world;
  2029. cell_num = (argc >= result_size_in_double_world)
  2030. ? argc
  2031. : result_size_in_double_world;
  2032. #else
  2033. argc = type->param_cell_num;
  2034. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  2035. #endif
  2036. if (num_results != type->result_count) {
  2037. LOG_ERROR(
  2038. "The result value number does not match the function declaration.");
  2039. goto fail1;
  2040. }
  2041. if (num_args != type->param_count) {
  2042. LOG_ERROR("The argument value number does not match the function "
  2043. "declaration.");
  2044. goto fail1;
  2045. }
  2046. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2047. if (total_size > sizeof(argv_buf)) {
  2048. if (!(argv =
  2049. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2050. goto fail1;
  2051. }
  2052. }
  2053. parse_args_to_uint32_array(type, args, argv);
  2054. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  2055. goto fail2;
  2056. parse_uint32_array_to_results(type, argv, results);
  2057. fail2:
  2058. if (argv != argv_buf)
  2059. wasm_runtime_free(argv);
  2060. fail1:
  2061. return ret;
  2062. }
  2063. bool
  2064. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  2065. WASMFunctionInstanceCommon *function,
  2066. uint32 num_results, wasm_val_t results[],
  2067. uint32 num_args, ...)
  2068. {
  2069. wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
  2070. WASMFuncType *type = NULL;
  2071. bool ret = false;
  2072. uint64 total_size;
  2073. uint32 i = 0, module_type;
  2074. va_list vargs;
  2075. module_type = exec_env->module_inst->module_type;
  2076. type = wasm_runtime_get_function_type(function, module_type);
  2077. if (!type) {
  2078. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  2079. "must be enabled at least one.");
  2080. goto fail1;
  2081. }
  2082. if (num_args != type->param_count) {
  2083. LOG_ERROR("The argument value number does not match the "
  2084. "function declaration.");
  2085. goto fail1;
  2086. }
  2087. total_size = sizeof(wasm_val_t) * (uint64)num_args;
  2088. if (total_size > sizeof(args_buf)) {
  2089. if (!(args =
  2090. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2091. goto fail1;
  2092. }
  2093. }
  2094. va_start(vargs, num_args);
  2095. for (i = 0; i < num_args; i++) {
  2096. switch (type->types[i]) {
  2097. case VALUE_TYPE_I32:
  2098. args[i].kind = WASM_I32;
  2099. args[i].of.i32 = va_arg(vargs, uint32);
  2100. break;
  2101. case VALUE_TYPE_I64:
  2102. args[i].kind = WASM_I64;
  2103. args[i].of.i64 = va_arg(vargs, uint64);
  2104. break;
  2105. case VALUE_TYPE_F32:
  2106. args[i].kind = WASM_F32;
  2107. args[i].of.f32 = (float32)va_arg(vargs, float64);
  2108. break;
  2109. case VALUE_TYPE_F64:
  2110. args[i].kind = WASM_F64;
  2111. args[i].of.f64 = va_arg(vargs, float64);
  2112. break;
  2113. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2114. case VALUE_TYPE_FUNCREF:
  2115. {
  2116. args[i].kind = WASM_FUNCREF;
  2117. args[i].of.i32 = va_arg(vargs, uint32);
  2118. break;
  2119. }
  2120. case VALUE_TYPE_EXTERNREF:
  2121. {
  2122. args[i].kind = WASM_ANYREF;
  2123. args[i].of.foreign = va_arg(vargs, uintptr_t);
  2124. break;
  2125. }
  2126. #endif
  2127. default:
  2128. bh_assert(0);
  2129. break;
  2130. }
  2131. }
  2132. va_end(vargs);
  2133. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  2134. num_args, args);
  2135. if (args != args_buf)
  2136. wasm_runtime_free(args);
  2137. fail1:
  2138. return ret;
  2139. }
  2140. bool
  2141. wasm_runtime_create_exec_env_singleton(
  2142. WASMModuleInstanceCommon *module_inst_comm)
  2143. {
  2144. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2145. WASMExecEnv *exec_env = NULL;
  2146. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2147. || module_inst_comm->module_type == Wasm_Module_AoT);
  2148. if (module_inst->exec_env_singleton) {
  2149. return true;
  2150. }
  2151. exec_env = wasm_exec_env_create(module_inst_comm,
  2152. module_inst->default_wasm_stack_size);
  2153. if (exec_env)
  2154. module_inst->exec_env_singleton = exec_env;
  2155. return exec_env ? true : false;
  2156. }
  2157. WASMExecEnv *
  2158. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
  2159. {
  2160. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2161. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2162. || module_inst_comm->module_type == Wasm_Module_AoT);
  2163. if (!module_inst->exec_env_singleton) {
  2164. wasm_runtime_create_exec_env_singleton(module_inst_comm);
  2165. }
  2166. return module_inst->exec_env_singleton;
  2167. }
  2168. static void
  2169. wasm_set_exception_local(WASMModuleInstance *module_inst, const char *exception)
  2170. {
  2171. exception_lock(module_inst);
  2172. if (exception) {
  2173. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  2174. "Exception: %s", exception);
  2175. }
  2176. else {
  2177. module_inst->cur_exception[0] = '\0';
  2178. }
  2179. exception_unlock(module_inst);
  2180. }
  2181. void
  2182. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  2183. {
  2184. #if WASM_ENABLE_THREAD_MGR != 0
  2185. WASMExecEnv *exec_env =
  2186. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  2187. if (exec_env) {
  2188. wasm_cluster_set_exception(exec_env, exception);
  2189. }
  2190. else {
  2191. wasm_set_exception_local(module_inst, exception);
  2192. }
  2193. #else
  2194. wasm_set_exception_local(module_inst, exception);
  2195. #endif
  2196. }
  2197. /* clang-format off */
  2198. static const char *exception_msgs[] = {
  2199. "unreachable", /* EXCE_UNREACHABLE */
  2200. "allocate memory failed", /* EXCE_OUT_OF_MEMORY */
  2201. "out of bounds memory access", /* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
  2202. "integer overflow", /* EXCE_INTEGER_OVERFLOW */
  2203. "integer divide by zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
  2204. "invalid conversion to integer", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
  2205. "indirect call type mismatch", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
  2206. "invalid function index", /* EXCE_INVALID_FUNCTION_INDEX */
  2207. "undefined element", /* EXCE_UNDEFINED_ELEMENT */
  2208. "uninitialized element", /* EXCE_UNINITIALIZED_ELEMENT */
  2209. "failed to call unlinked import function", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
  2210. "native stack overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
  2211. "unaligned atomic", /* EXCE_UNALIGNED_ATOMIC */
  2212. "wasm auxiliary stack overflow", /* EXCE_AUX_STACK_OVERFLOW */
  2213. "wasm auxiliary stack underflow", /* EXCE_AUX_STACK_UNDERFLOW */
  2214. "out of bounds table access", /* EXCE_OUT_OF_BOUNDS_TABLE_ACCESS */
  2215. "wasm operand stack overflow", /* EXCE_OPERAND_STACK_OVERFLOW */
  2216. "failed to compile fast jit function", /* EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC */
  2217. /* GC related exceptions */
  2218. "null function object", /* EXCE_NULL_FUNC_OBJ */
  2219. "null structure object", /* EXCE_NULL_STRUCT_OBJ */
  2220. "null array reference", /* EXCE_NULL_ARRAY_OBJ */
  2221. "null i31 reference", /* EXCE_NULL_I31_OBJ */
  2222. "null reference", /* EXCE_NULL_REFERENCE */
  2223. "create rtt type failed", /* EXCE_FAILED_TO_CREATE_RTT_TYPE */
  2224. "create struct object failed", /* EXCE_FAILED_TO_CREATE_STRUCT_OBJ */
  2225. "create array object failed", /* EXCE_FAILED_TO_CREATE_ARRAY_OBJ */
  2226. "create externref object failed", /* EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ */
  2227. "cast failure", /* EXCE_CAST_FAILURE */
  2228. "out of bounds array access", /* EXCE_ARRAY_IDX_OOB */
  2229. /* stringref related exceptions */
  2230. "create string object failed", /* EXCE_FAILED_TO_CREATE_STRING */
  2231. "create stringref failed", /* EXCE_FAILED_TO_CREATE_STRINGREF */
  2232. "create stringview failed", /* EXCE_FAILED_TO_CREATE_STRINGVIEW */
  2233. "encode failed", /* EXCE_FAILED_TO_ENCODE_STRING */
  2234. "", /* EXCE_ALREADY_THROWN */
  2235. };
  2236. /* clang-format on */
  2237. void
  2238. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2239. {
  2240. if (id < EXCE_NUM)
  2241. wasm_set_exception(module_inst, exception_msgs[id]);
  2242. else
  2243. wasm_set_exception(module_inst, "unknown exception");
  2244. }
  2245. const char *
  2246. wasm_get_exception(WASMModuleInstance *module_inst)
  2247. {
  2248. if (module_inst->cur_exception[0] == '\0')
  2249. return NULL;
  2250. else
  2251. return module_inst->cur_exception;
  2252. }
  2253. bool
  2254. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
  2255. {
  2256. bool has_exception = false;
  2257. exception_lock(module_inst);
  2258. if (module_inst->cur_exception[0] != '\0') {
  2259. /* NULL is passed if the caller is not interested in getting the
  2260. * exception content, but only in knowing if an exception has been
  2261. * raised
  2262. */
  2263. if (exception_buf != NULL)
  2264. bh_memcpy_s(exception_buf, sizeof(module_inst->cur_exception),
  2265. module_inst->cur_exception,
  2266. sizeof(module_inst->cur_exception));
  2267. has_exception = true;
  2268. }
  2269. exception_unlock(module_inst);
  2270. return has_exception;
  2271. }
  2272. void
  2273. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst_comm,
  2274. const char *exception)
  2275. {
  2276. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2277. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2278. || module_inst_comm->module_type == Wasm_Module_AoT);
  2279. wasm_set_exception(module_inst, exception);
  2280. }
  2281. const char *
  2282. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst_comm)
  2283. {
  2284. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2285. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2286. || module_inst_comm->module_type == Wasm_Module_AoT);
  2287. return wasm_get_exception(module_inst);
  2288. }
  2289. bool
  2290. wasm_runtime_copy_exception(WASMModuleInstanceCommon *module_inst_comm,
  2291. char *exception_buf)
  2292. {
  2293. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2294. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2295. || module_inst_comm->module_type == Wasm_Module_AoT);
  2296. return wasm_copy_exception(module_inst, exception_buf);
  2297. }
  2298. void
  2299. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst_comm)
  2300. {
  2301. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2302. || module_inst_comm->module_type == Wasm_Module_AoT);
  2303. wasm_runtime_set_exception(module_inst_comm, NULL);
  2304. }
  2305. void
  2306. wasm_runtime_terminate(WASMModuleInstanceCommon *module_inst_comm)
  2307. {
  2308. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2309. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2310. || module_inst_comm->module_type == Wasm_Module_AoT);
  2311. wasm_set_exception(module_inst, "terminated by user");
  2312. }
  2313. void
  2314. wasm_runtime_set_custom_data_internal(
  2315. WASMModuleInstanceCommon *module_inst_comm, void *custom_data)
  2316. {
  2317. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2318. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2319. || module_inst_comm->module_type == Wasm_Module_AoT);
  2320. module_inst->custom_data = custom_data;
  2321. }
  2322. void
  2323. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  2324. void *custom_data)
  2325. {
  2326. #if WASM_ENABLE_THREAD_MGR != 0
  2327. wasm_cluster_spread_custom_data(module_inst, custom_data);
  2328. #else
  2329. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  2330. #endif
  2331. }
  2332. void *
  2333. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst_comm)
  2334. {
  2335. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2336. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2337. || module_inst_comm->module_type == Wasm_Module_AoT);
  2338. return module_inst->custom_data;
  2339. }
  2340. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  2341. void
  2342. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  2343. bool enable)
  2344. {
  2345. /* Alwary disable bounds checks if hw bounds checks enabled */
  2346. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2347. enable = false;
  2348. #endif
  2349. #if WASM_ENABLE_INTERP != 0
  2350. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2351. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  2352. ->common.disable_bounds_checks = enable ? false : true;
  2353. }
  2354. #endif
  2355. #if WASM_ENABLE_AOT != 0
  2356. if (module_inst->module_type == Wasm_Module_AoT) {
  2357. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  2358. ->common.disable_bounds_checks = enable ? false : true;
  2359. }
  2360. #endif
  2361. }
  2362. bool
  2363. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  2364. {
  2365. #if WASM_ENABLE_INTERP != 0
  2366. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2367. return !((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2368. ->e)
  2369. ->common.disable_bounds_checks;
  2370. }
  2371. #endif
  2372. #if WASM_ENABLE_AOT != 0
  2373. if (module_inst->module_type == Wasm_Module_AoT) {
  2374. return !((AOTModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2375. ->e)
  2376. ->common.disable_bounds_checks;
  2377. }
  2378. #endif
  2379. return true;
  2380. }
  2381. #endif
  2382. uint32
  2383. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  2384. WASMExecEnv *exec_env, uint32 size,
  2385. void **p_native_addr)
  2386. {
  2387. #if WASM_ENABLE_INTERP != 0
  2388. if (module_inst->module_type == Wasm_Module_Bytecode)
  2389. return wasm_module_malloc_internal((WASMModuleInstance *)module_inst,
  2390. exec_env, size, p_native_addr);
  2391. #endif
  2392. #if WASM_ENABLE_AOT != 0
  2393. if (module_inst->module_type == Wasm_Module_AoT)
  2394. return aot_module_malloc_internal((AOTModuleInstance *)module_inst,
  2395. exec_env, size, p_native_addr);
  2396. #endif
  2397. return 0;
  2398. }
  2399. uint32
  2400. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  2401. WASMExecEnv *exec_env, uint32 ptr,
  2402. uint32 size, void **p_native_addr)
  2403. {
  2404. #if WASM_ENABLE_INTERP != 0
  2405. if (module_inst->module_type == Wasm_Module_Bytecode)
  2406. return wasm_module_realloc_internal((WASMModuleInstance *)module_inst,
  2407. exec_env, ptr, size, p_native_addr);
  2408. #endif
  2409. #if WASM_ENABLE_AOT != 0
  2410. if (module_inst->module_type == Wasm_Module_AoT)
  2411. return aot_module_realloc_internal((AOTModuleInstance *)module_inst,
  2412. exec_env, ptr, size, p_native_addr);
  2413. #endif
  2414. return 0;
  2415. }
  2416. void
  2417. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  2418. WASMExecEnv *exec_env, uint32 ptr)
  2419. {
  2420. #if WASM_ENABLE_INTERP != 0
  2421. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2422. wasm_module_free_internal((WASMModuleInstance *)module_inst, exec_env,
  2423. ptr);
  2424. return;
  2425. }
  2426. #endif
  2427. #if WASM_ENABLE_AOT != 0
  2428. if (module_inst->module_type == Wasm_Module_AoT) {
  2429. aot_module_free_internal((AOTModuleInstance *)module_inst, exec_env,
  2430. ptr);
  2431. return;
  2432. }
  2433. #endif
  2434. }
  2435. uint32
  2436. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  2437. void **p_native_addr)
  2438. {
  2439. #if WASM_ENABLE_INTERP != 0
  2440. if (module_inst->module_type == Wasm_Module_Bytecode)
  2441. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  2442. p_native_addr);
  2443. #endif
  2444. #if WASM_ENABLE_AOT != 0
  2445. if (module_inst->module_type == Wasm_Module_AoT)
  2446. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  2447. p_native_addr);
  2448. #endif
  2449. return 0;
  2450. }
  2451. uint32
  2452. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  2453. uint32 size, void **p_native_addr)
  2454. {
  2455. #if WASM_ENABLE_INTERP != 0
  2456. if (module_inst->module_type == Wasm_Module_Bytecode)
  2457. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  2458. p_native_addr);
  2459. #endif
  2460. #if WASM_ENABLE_AOT != 0
  2461. if (module_inst->module_type == Wasm_Module_AoT)
  2462. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  2463. p_native_addr);
  2464. #endif
  2465. return 0;
  2466. }
  2467. void
  2468. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  2469. {
  2470. #if WASM_ENABLE_INTERP != 0
  2471. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2472. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  2473. return;
  2474. }
  2475. #endif
  2476. #if WASM_ENABLE_AOT != 0
  2477. if (module_inst->module_type == Wasm_Module_AoT) {
  2478. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  2479. return;
  2480. }
  2481. #endif
  2482. }
  2483. uint32
  2484. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  2485. const char *src, uint32 size)
  2486. {
  2487. #if WASM_ENABLE_INTERP != 0
  2488. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2489. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  2490. size);
  2491. }
  2492. #endif
  2493. #if WASM_ENABLE_AOT != 0
  2494. if (module_inst->module_type == Wasm_Module_AoT) {
  2495. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  2496. }
  2497. #endif
  2498. return 0;
  2499. }
  2500. #if WASM_ENABLE_LIBC_WASI != 0
  2501. static WASIArguments *
  2502. get_wasi_args_from_module(wasm_module_t module)
  2503. {
  2504. WASIArguments *wasi_args = NULL;
  2505. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  2506. if (module->module_type == Wasm_Module_Bytecode)
  2507. wasi_args = &((WASMModule *)module)->wasi_args;
  2508. #endif
  2509. #if WASM_ENABLE_AOT != 0
  2510. if (module->module_type == Wasm_Module_AoT)
  2511. wasi_args = &((AOTModule *)module)->wasi_args;
  2512. #endif
  2513. return wasi_args;
  2514. }
  2515. void
  2516. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  2517. uint32 dir_count, const char *map_dir_list[],
  2518. uint32 map_dir_count, const char *env_list[],
  2519. uint32 env_count, char *argv[], int argc,
  2520. int64 stdinfd, int64 stdoutfd, int64 stderrfd)
  2521. {
  2522. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2523. bh_assert(wasi_args);
  2524. wasi_args->dir_list = dir_list;
  2525. wasi_args->dir_count = dir_count;
  2526. wasi_args->map_dir_list = map_dir_list;
  2527. wasi_args->map_dir_count = map_dir_count;
  2528. wasi_args->env = env_list;
  2529. wasi_args->env_count = env_count;
  2530. wasi_args->argv = argv;
  2531. wasi_args->argc = (uint32)argc;
  2532. wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
  2533. wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
  2534. wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
  2535. #if WASM_ENABLE_MULTI_MODULE != 0
  2536. #if WASM_ENABLE_INTERP != 0
  2537. if (module->module_type == Wasm_Module_Bytecode) {
  2538. wasm_propagate_wasi_args((WASMModule *)module);
  2539. }
  2540. #endif
  2541. #endif
  2542. }
  2543. void
  2544. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  2545. uint32 dir_count, const char *map_dir_list[],
  2546. uint32 map_dir_count, const char *env_list[],
  2547. uint32 env_count, char *argv[], int argc)
  2548. {
  2549. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  2550. map_dir_count, env_list, env_count, argv,
  2551. argc, -1, -1, -1);
  2552. }
  2553. void
  2554. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  2555. uint32 addr_pool_size)
  2556. {
  2557. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2558. if (wasi_args) {
  2559. wasi_args->addr_pool = addr_pool;
  2560. wasi_args->addr_count = addr_pool_size;
  2561. }
  2562. }
  2563. void
  2564. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  2565. const char *ns_lookup_pool[],
  2566. uint32 ns_lookup_pool_size)
  2567. {
  2568. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2569. if (wasi_args) {
  2570. wasi_args->ns_lookup_pool = ns_lookup_pool;
  2571. wasi_args->ns_lookup_count = ns_lookup_pool_size;
  2572. }
  2573. }
  2574. #if WASM_ENABLE_UVWASI == 0
  2575. static bool
  2576. copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
  2577. char ***list_ptr, uint64 *out_buf_size)
  2578. {
  2579. uint64 buf_size = 0, total_size;
  2580. uint32 buf_offset = 0, i;
  2581. char *buf = NULL, **list = NULL;
  2582. for (i = 0; i < array_size; i++)
  2583. buf_size += strlen(array[i]) + 1;
  2584. /* We add +1 to generate null-terminated array of strings */
  2585. total_size = sizeof(char *) * ((uint64)array_size + 1);
  2586. if (total_size >= UINT32_MAX
  2587. || (total_size > 0 && !(list = wasm_runtime_malloc((uint32)total_size)))
  2588. || buf_size >= UINT32_MAX
  2589. || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
  2590. if (buf)
  2591. wasm_runtime_free(buf);
  2592. if (list)
  2593. wasm_runtime_free(list);
  2594. return false;
  2595. }
  2596. for (i = 0; i < array_size; i++) {
  2597. list[i] = buf + buf_offset;
  2598. bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
  2599. buf_offset += (uint32)(strlen(array[i]) + 1);
  2600. }
  2601. list[array_size] = NULL;
  2602. *list_ptr = list;
  2603. *buf_ptr = buf;
  2604. if (out_buf_size)
  2605. *out_buf_size = buf_size;
  2606. return true;
  2607. }
  2608. bool
  2609. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2610. const char *dir_list[], uint32 dir_count,
  2611. const char *map_dir_list[], uint32 map_dir_count,
  2612. const char *env[], uint32 env_count,
  2613. const char *addr_pool[], uint32 addr_pool_size,
  2614. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2615. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  2616. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  2617. char *error_buf, uint32 error_buf_size)
  2618. {
  2619. WASIContext *wasi_ctx;
  2620. char *argv_buf = NULL;
  2621. char **argv_list = NULL;
  2622. char *env_buf = NULL;
  2623. char **env_list = NULL;
  2624. char *ns_lookup_buf = NULL;
  2625. char **ns_lookup_list = NULL;
  2626. uint64 argv_buf_size = 0, env_buf_size = 0;
  2627. struct fd_table *curfds = NULL;
  2628. struct fd_prestats *prestats = NULL;
  2629. struct argv_environ_values *argv_environ = NULL;
  2630. struct addr_pool *apool = NULL;
  2631. bool fd_table_inited = false, fd_prestats_inited = false;
  2632. bool argv_environ_inited = false;
  2633. bool addr_pool_inited = false;
  2634. __wasi_fd_t wasm_fd = 3;
  2635. os_file_handle file_handle;
  2636. char *path, resolved_path[PATH_MAX];
  2637. uint32 i;
  2638. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  2639. error_buf_size))) {
  2640. return false;
  2641. }
  2642. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  2643. /* process argv[0], trip the path and suffix, only keep the program name
  2644. */
  2645. if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
  2646. &argv_buf_size)) {
  2647. set_error_buf(error_buf, error_buf_size,
  2648. "Init wasi environment failed: allocate memory failed");
  2649. goto fail;
  2650. }
  2651. if (!copy_string_array(env, env_count, &env_buf, &env_list,
  2652. &env_buf_size)) {
  2653. set_error_buf(error_buf, error_buf_size,
  2654. "Init wasi environment failed: allocate memory failed");
  2655. goto fail;
  2656. }
  2657. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  2658. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  2659. || !(argv_environ =
  2660. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  2661. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  2662. set_error_buf(error_buf, error_buf_size,
  2663. "Init wasi environment failed: allocate memory failed");
  2664. goto fail;
  2665. }
  2666. if (!fd_table_init(curfds)) {
  2667. set_error_buf(error_buf, error_buf_size,
  2668. "Init wasi environment failed: "
  2669. "init fd table failed");
  2670. goto fail;
  2671. }
  2672. fd_table_inited = true;
  2673. if (!fd_prestats_init(prestats)) {
  2674. set_error_buf(error_buf, error_buf_size,
  2675. "Init wasi environment failed: "
  2676. "init fd prestats failed");
  2677. goto fail;
  2678. }
  2679. fd_prestats_inited = true;
  2680. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  2681. argc, env_buf, env_buf_size, env_list, env_count)) {
  2682. set_error_buf(error_buf, error_buf_size,
  2683. "Init wasi environment failed: "
  2684. "init argument environment failed");
  2685. goto fail;
  2686. }
  2687. argv_environ_inited = true;
  2688. if (!addr_pool_init(apool)) {
  2689. set_error_buf(error_buf, error_buf_size,
  2690. "Init wasi environment failed: "
  2691. "init the address pool failed");
  2692. goto fail;
  2693. }
  2694. addr_pool_inited = true;
  2695. os_file_handle stdin_file_handle = os_convert_stdin_handle(stdinfd);
  2696. os_file_handle stdout_file_handle = os_convert_stdout_handle(stdoutfd);
  2697. os_file_handle stderr_file_handle = os_convert_stderr_handle(stderrfd);
  2698. if (!os_is_handle_valid(&stdin_file_handle)
  2699. || !os_is_handle_valid(&stdout_file_handle)
  2700. || !os_is_handle_valid(&stderr_file_handle))
  2701. goto fail;
  2702. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  2703. if (!fd_table_insert_existing(curfds, 0, stdin_file_handle, true)
  2704. || !fd_table_insert_existing(curfds, 1, stdout_file_handle, true)
  2705. || !fd_table_insert_existing(curfds, 2, stderr_file_handle, true)) {
  2706. set_error_buf(error_buf, error_buf_size,
  2707. "Init wasi environment failed: init fd table failed");
  2708. goto fail;
  2709. }
  2710. wasm_fd = 3;
  2711. for (i = 0; i < dir_count; i++, wasm_fd++) {
  2712. path = os_realpath(dir_list[i], resolved_path);
  2713. if (!path) {
  2714. if (error_buf)
  2715. snprintf(error_buf, error_buf_size,
  2716. "error while pre-opening directory %s: %d\n",
  2717. dir_list[i], errno);
  2718. goto fail;
  2719. }
  2720. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  2721. if (error != __WASI_ESUCCESS) {
  2722. if (error_buf)
  2723. snprintf(error_buf, error_buf_size,
  2724. "error while pre-opening directory %s: %d\n",
  2725. dir_list[i], error);
  2726. goto fail;
  2727. }
  2728. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)) {
  2729. if (error_buf)
  2730. snprintf(error_buf, error_buf_size,
  2731. "error inserting preopen fd %u (directory %s) into fd "
  2732. "table",
  2733. (unsigned int)wasm_fd, dir_list[i]);
  2734. goto fail;
  2735. }
  2736. if (!fd_prestats_insert(prestats, dir_list[i], wasm_fd)) {
  2737. if (error_buf)
  2738. snprintf(error_buf, error_buf_size,
  2739. "error inserting preopen fd %u (directory %s) into "
  2740. "prestats table",
  2741. (unsigned int)wasm_fd, dir_list[i]);
  2742. goto fail;
  2743. }
  2744. }
  2745. for (i = 0; i < map_dir_count; i++, wasm_fd++) {
  2746. char mapping_copy_buf[256];
  2747. char *mapping_copy = mapping_copy_buf;
  2748. char *map_mapped = NULL, *map_host = NULL;
  2749. const unsigned long max_len = strlen(map_dir_list[i]) * 2 + 3;
  2750. /* Allocation limit for runtime environments with reduced stack size */
  2751. if (max_len > 256) {
  2752. if (!(mapping_copy = wasm_runtime_malloc(max_len))) {
  2753. snprintf(error_buf, error_buf_size,
  2754. "error while allocating for directory mapping\n");
  2755. goto fail;
  2756. }
  2757. }
  2758. bh_memcpy_s(mapping_copy, max_len, map_dir_list[i],
  2759. (uint32)(strlen(map_dir_list[i]) + 1));
  2760. map_mapped = strtok(mapping_copy, "::");
  2761. map_host = strtok(NULL, "::");
  2762. if (!map_mapped || !map_host) {
  2763. if (error_buf)
  2764. snprintf(error_buf, error_buf_size,
  2765. "error while pre-opening mapped directory: "
  2766. "invalid map\n");
  2767. if (mapping_copy != mapping_copy_buf)
  2768. wasm_runtime_free(mapping_copy);
  2769. goto fail;
  2770. }
  2771. path = os_realpath(map_host, resolved_path);
  2772. if (!path) {
  2773. if (error_buf)
  2774. snprintf(error_buf, error_buf_size,
  2775. "error while pre-opening mapped directory %s: %d\n",
  2776. map_host, errno);
  2777. if (mapping_copy != mapping_copy_buf)
  2778. wasm_runtime_free(mapping_copy);
  2779. goto fail;
  2780. }
  2781. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  2782. if (error != __WASI_ESUCCESS) {
  2783. if (error_buf)
  2784. snprintf(error_buf, error_buf_size,
  2785. "error while pre-opening mapped directory %s: %d\n",
  2786. map_host, errno);
  2787. if (mapping_copy != mapping_copy_buf)
  2788. wasm_runtime_free(mapping_copy);
  2789. goto fail;
  2790. }
  2791. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)
  2792. || !fd_prestats_insert(prestats, map_mapped, wasm_fd)) {
  2793. if (error_buf)
  2794. snprintf(error_buf, error_buf_size,
  2795. "error while pre-opening mapped directory %s: "
  2796. "insertion failed\n",
  2797. dir_list[i]);
  2798. if (mapping_copy != mapping_copy_buf)
  2799. wasm_runtime_free(mapping_copy);
  2800. goto fail;
  2801. }
  2802. if (mapping_copy != mapping_copy_buf)
  2803. wasm_runtime_free(mapping_copy);
  2804. }
  2805. /* addr_pool(textual) -> apool */
  2806. for (i = 0; i < addr_pool_size; i++) {
  2807. char *cp, *address, *mask;
  2808. bool ret = false;
  2809. cp = bh_strdup(addr_pool[i]);
  2810. if (!cp) {
  2811. set_error_buf(error_buf, error_buf_size,
  2812. "Init wasi environment failed: copy address failed");
  2813. goto fail;
  2814. }
  2815. address = strtok(cp, "/");
  2816. mask = strtok(NULL, "/");
  2817. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  2818. wasm_runtime_free(cp);
  2819. if (!ret) {
  2820. set_error_buf(error_buf, error_buf_size,
  2821. "Init wasi environment failed: store address failed");
  2822. goto fail;
  2823. }
  2824. }
  2825. if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
  2826. &ns_lookup_list, NULL)) {
  2827. set_error_buf(error_buf, error_buf_size,
  2828. "Init wasi environment failed: allocate memory failed");
  2829. goto fail;
  2830. }
  2831. wasi_ctx->curfds = curfds;
  2832. wasi_ctx->prestats = prestats;
  2833. wasi_ctx->argv_environ = argv_environ;
  2834. wasi_ctx->addr_pool = apool;
  2835. wasi_ctx->argv_buf = argv_buf;
  2836. wasi_ctx->argv_list = argv_list;
  2837. wasi_ctx->env_buf = env_buf;
  2838. wasi_ctx->env_list = env_list;
  2839. wasi_ctx->ns_lookup_buf = ns_lookup_buf;
  2840. wasi_ctx->ns_lookup_list = ns_lookup_list;
  2841. return true;
  2842. fail:
  2843. if (argv_environ_inited)
  2844. argv_environ_destroy(argv_environ);
  2845. if (fd_prestats_inited)
  2846. fd_prestats_destroy(prestats);
  2847. if (fd_table_inited)
  2848. fd_table_destroy(curfds);
  2849. if (addr_pool_inited)
  2850. addr_pool_destroy(apool);
  2851. if (curfds)
  2852. wasm_runtime_free(curfds);
  2853. if (prestats)
  2854. wasm_runtime_free(prestats);
  2855. if (argv_environ)
  2856. wasm_runtime_free(argv_environ);
  2857. if (apool)
  2858. wasm_runtime_free(apool);
  2859. if (argv_buf)
  2860. wasm_runtime_free(argv_buf);
  2861. if (argv_list)
  2862. wasm_runtime_free(argv_list);
  2863. if (env_buf)
  2864. wasm_runtime_free(env_buf);
  2865. if (env_list)
  2866. wasm_runtime_free(env_list);
  2867. if (ns_lookup_buf)
  2868. wasm_runtime_free(ns_lookup_buf);
  2869. if (ns_lookup_list)
  2870. wasm_runtime_free(ns_lookup_list);
  2871. return false;
  2872. }
  2873. #else /* else of WASM_ENABLE_UVWASI == 0 */
  2874. static void *
  2875. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  2876. {
  2877. return runtime_malloc(size, NULL, NULL, 0);
  2878. (void)mem_user_data;
  2879. }
  2880. static void
  2881. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  2882. {
  2883. if (ptr)
  2884. wasm_runtime_free(ptr);
  2885. (void)mem_user_data;
  2886. }
  2887. static void *
  2888. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  2889. {
  2890. uint64 total_size = (uint64)nmemb * size;
  2891. return runtime_malloc(total_size, NULL, NULL, 0);
  2892. (void)mem_user_data;
  2893. }
  2894. static void *
  2895. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  2896. {
  2897. if (size >= UINT32_MAX) {
  2898. return NULL;
  2899. }
  2900. return wasm_runtime_realloc(ptr, (uint32)size);
  2901. }
  2902. /* clang-format off */
  2903. static uvwasi_mem_t uvwasi_allocator = {
  2904. .mem_user_data = 0,
  2905. .malloc = wasm_uvwasi_malloc,
  2906. .free = wasm_uvwasi_free,
  2907. .calloc = wasm_uvwasi_calloc,
  2908. .realloc = wasm_uvwasi_realloc
  2909. };
  2910. /* clang-format on */
  2911. bool
  2912. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2913. const char *dir_list[], uint32 dir_count,
  2914. const char *map_dir_list[], uint32 map_dir_count,
  2915. const char *env[], uint32 env_count,
  2916. const char *addr_pool[], uint32 addr_pool_size,
  2917. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2918. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  2919. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  2920. char *error_buf, uint32 error_buf_size)
  2921. {
  2922. WASIContext *ctx;
  2923. uvwasi_t *uvwasi;
  2924. uvwasi_options_t init_options;
  2925. const char **envp = NULL;
  2926. uint64 total_size;
  2927. uint32 i;
  2928. bool ret = false;
  2929. ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
  2930. if (!ctx)
  2931. return false;
  2932. uvwasi = &ctx->uvwasi;
  2933. /* Setup the initialization options */
  2934. uvwasi_options_init(&init_options);
  2935. init_options.allocator = &uvwasi_allocator;
  2936. init_options.argc = argc;
  2937. init_options.argv = (const char **)argv;
  2938. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  2939. init_options.out =
  2940. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  2941. init_options.err =
  2942. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  2943. if (dir_count > 0) {
  2944. init_options.preopenc = dir_count;
  2945. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  2946. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  2947. total_size, module_inst, error_buf, error_buf_size);
  2948. if (init_options.preopens == NULL)
  2949. goto fail;
  2950. for (i = 0; i < init_options.preopenc; i++) {
  2951. init_options.preopens[i].real_path = dir_list[i];
  2952. init_options.preopens[i].mapped_path =
  2953. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  2954. }
  2955. }
  2956. if (env_count > 0) {
  2957. total_size = sizeof(char *) * (uint64)(env_count + 1);
  2958. envp =
  2959. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  2960. if (envp == NULL)
  2961. goto fail;
  2962. for (i = 0; i < env_count; i++) {
  2963. envp[i] = env[i];
  2964. }
  2965. envp[env_count] = NULL;
  2966. init_options.envp = envp;
  2967. }
  2968. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  2969. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  2970. goto fail;
  2971. }
  2972. wasm_runtime_set_wasi_ctx(module_inst, ctx);
  2973. ret = true;
  2974. fail:
  2975. if (envp)
  2976. wasm_runtime_free((void *)envp);
  2977. if (init_options.preopens)
  2978. wasm_runtime_free(init_options.preopens);
  2979. if (!ret && uvwasi)
  2980. wasm_runtime_free(uvwasi);
  2981. return ret;
  2982. }
  2983. #endif /* end of WASM_ENABLE_UVWASI */
  2984. bool
  2985. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  2986. {
  2987. #if WASM_ENABLE_INTERP != 0
  2988. if (module_inst->module_type == Wasm_Module_Bytecode
  2989. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  2990. return true;
  2991. #endif
  2992. #if WASM_ENABLE_AOT != 0
  2993. if (module_inst->module_type == Wasm_Module_AoT
  2994. && ((AOTModule *)((AOTModuleInstance *)module_inst)->module)
  2995. ->import_wasi_api)
  2996. return true;
  2997. #endif
  2998. return false;
  2999. }
  3000. WASMFunctionInstanceCommon *
  3001. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  3002. {
  3003. uint32 i;
  3004. #if WASM_ENABLE_INTERP != 0
  3005. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3006. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  3007. WASMFunctionInstance *func;
  3008. for (i = 0; i < wasm_inst->export_func_count; i++) {
  3009. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  3010. func = wasm_inst->export_functions[i].function;
  3011. if (func->u.func->func_type->param_count != 0
  3012. || func->u.func->func_type->result_count != 0) {
  3013. LOG_ERROR("Lookup wasi _start function failed: "
  3014. "invalid function type.\n");
  3015. return NULL;
  3016. }
  3017. return (WASMFunctionInstanceCommon *)func;
  3018. }
  3019. }
  3020. return NULL;
  3021. }
  3022. #endif
  3023. #if WASM_ENABLE_AOT != 0
  3024. if (module_inst->module_type == Wasm_Module_AoT) {
  3025. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  3026. AOTFunctionInstance *export_funcs =
  3027. (AOTFunctionInstance *)aot_inst->export_functions;
  3028. for (i = 0; i < aot_inst->export_func_count; i++) {
  3029. if (!strcmp(export_funcs[i].func_name, "_start")) {
  3030. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  3031. if (func_type->param_count != 0
  3032. || func_type->result_count != 0) {
  3033. LOG_ERROR("Lookup wasi _start function failed: "
  3034. "invalid function type.\n");
  3035. return NULL;
  3036. }
  3037. return (WASMFunctionInstanceCommon *)&export_funcs[i];
  3038. }
  3039. }
  3040. return NULL;
  3041. }
  3042. #endif /* end of WASM_ENABLE_AOT */
  3043. return NULL;
  3044. }
  3045. #if WASM_ENABLE_UVWASI == 0
  3046. void
  3047. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3048. {
  3049. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3050. if (wasi_ctx) {
  3051. if (wasi_ctx->argv_environ) {
  3052. argv_environ_destroy(wasi_ctx->argv_environ);
  3053. wasm_runtime_free(wasi_ctx->argv_environ);
  3054. }
  3055. if (wasi_ctx->curfds) {
  3056. fd_table_destroy(wasi_ctx->curfds);
  3057. wasm_runtime_free(wasi_ctx->curfds);
  3058. }
  3059. if (wasi_ctx->prestats) {
  3060. fd_prestats_destroy(wasi_ctx->prestats);
  3061. wasm_runtime_free(wasi_ctx->prestats);
  3062. }
  3063. if (wasi_ctx->addr_pool) {
  3064. addr_pool_destroy(wasi_ctx->addr_pool);
  3065. wasm_runtime_free(wasi_ctx->addr_pool);
  3066. }
  3067. if (wasi_ctx->argv_buf)
  3068. wasm_runtime_free(wasi_ctx->argv_buf);
  3069. if (wasi_ctx->argv_list)
  3070. wasm_runtime_free(wasi_ctx->argv_list);
  3071. if (wasi_ctx->env_buf)
  3072. wasm_runtime_free(wasi_ctx->env_buf);
  3073. if (wasi_ctx->env_list)
  3074. wasm_runtime_free(wasi_ctx->env_list);
  3075. if (wasi_ctx->ns_lookup_buf)
  3076. wasm_runtime_free(wasi_ctx->ns_lookup_buf);
  3077. if (wasi_ctx->ns_lookup_list)
  3078. wasm_runtime_free(wasi_ctx->ns_lookup_list);
  3079. wasm_runtime_free(wasi_ctx);
  3080. }
  3081. }
  3082. #else
  3083. void
  3084. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3085. {
  3086. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3087. if (wasi_ctx) {
  3088. uvwasi_destroy(&wasi_ctx->uvwasi);
  3089. wasm_runtime_free(wasi_ctx);
  3090. }
  3091. }
  3092. #endif
  3093. uint32_t
  3094. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
  3095. {
  3096. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3097. #if WASM_ENABLE_THREAD_MGR != 0
  3098. WASMCluster *cluster;
  3099. WASMExecEnv *exec_env;
  3100. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  3101. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  3102. /**
  3103. * The main thread may exit earlier than other threads, and
  3104. * the exit_code of wasi_ctx may be changed by other thread
  3105. * when it runs into wasi_proc_exit, here we wait until all
  3106. * other threads exit to avoid getting invalid exit_code.
  3107. */
  3108. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  3109. }
  3110. #endif
  3111. return wasi_ctx->exit_code;
  3112. }
  3113. #endif /* end of WASM_ENABLE_LIBC_WASI */
  3114. WASMModuleCommon *
  3115. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  3116. {
  3117. WASMModuleInstanceCommon *module_inst_comm =
  3118. wasm_runtime_get_module_inst(exec_env);
  3119. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  3120. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  3121. || module_inst_comm->module_type == Wasm_Module_AoT);
  3122. return (WASMModuleCommon *)module_inst->module;
  3123. }
  3124. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3125. const uint8 *
  3126. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  3127. const char *name, uint32 *len)
  3128. {
  3129. #if WASM_ENABLE_INTERP != 0
  3130. if (module_comm->module_type == Wasm_Module_Bytecode)
  3131. return wasm_loader_get_custom_section((WASMModule *)module_comm, name,
  3132. len);
  3133. #endif
  3134. #if WASM_ENABLE_AOT != 0
  3135. if (module_comm->module_type == Wasm_Module_AoT)
  3136. return aot_get_custom_section((AOTModule *)module_comm, name, len);
  3137. #endif
  3138. return NULL;
  3139. }
  3140. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  3141. static union {
  3142. int a;
  3143. char b;
  3144. } __ue = { .a = 1 };
  3145. #define is_little_endian() (__ue.b == 1)
  3146. bool
  3147. wasm_runtime_register_natives(const char *module_name,
  3148. NativeSymbol *native_symbols,
  3149. uint32 n_native_symbols)
  3150. {
  3151. return wasm_native_register_natives(module_name, native_symbols,
  3152. n_native_symbols);
  3153. }
  3154. bool
  3155. wasm_runtime_register_natives_raw(const char *module_name,
  3156. NativeSymbol *native_symbols,
  3157. uint32 n_native_symbols)
  3158. {
  3159. return wasm_native_register_natives_raw(module_name, native_symbols,
  3160. n_native_symbols);
  3161. }
  3162. bool
  3163. wasm_runtime_unregister_natives(const char *module_name,
  3164. NativeSymbol *native_symbols)
  3165. {
  3166. return wasm_native_unregister_natives(module_name, native_symbols);
  3167. }
  3168. bool
  3169. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  3170. const WASMFuncType *func_type,
  3171. const char *signature, void *attachment,
  3172. uint32 *argv, uint32 argc, uint32 *argv_ret)
  3173. {
  3174. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3175. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  3176. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  3177. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  3178. uint32 *argv_src = argv, i, argc1, ptr_len;
  3179. uint32 arg_i32;
  3180. bool ret = false;
  3181. argc1 = func_type->param_count;
  3182. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3183. size = sizeof(uint64) * (uint64)argc1;
  3184. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3185. 0))) {
  3186. return false;
  3187. }
  3188. }
  3189. argv_dst = argv1;
  3190. /* Traverse secondly to fill in each argument */
  3191. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  3192. switch (func_type->types[i]) {
  3193. case VALUE_TYPE_I32:
  3194. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3195. case VALUE_TYPE_FUNCREF:
  3196. #endif
  3197. {
  3198. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  3199. if (signature) {
  3200. if (signature[i + 1] == '*') {
  3201. /* param is a pointer */
  3202. if (signature[i + 2] == '~')
  3203. /* pointer with length followed */
  3204. ptr_len = *argv_src;
  3205. else
  3206. /* pointer without length followed */
  3207. ptr_len = 1;
  3208. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3209. ptr_len))
  3210. goto fail;
  3211. *(uintptr_t *)argv_dst =
  3212. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  3213. arg_i32);
  3214. }
  3215. else if (signature[i + 1] == '$') {
  3216. /* param is a string */
  3217. if (!wasm_runtime_validate_app_str_addr(module,
  3218. arg_i32))
  3219. goto fail;
  3220. *(uintptr_t *)argv_dst =
  3221. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  3222. arg_i32);
  3223. }
  3224. }
  3225. break;
  3226. }
  3227. case VALUE_TYPE_I64:
  3228. case VALUE_TYPE_F64:
  3229. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  3230. sizeof(uint32) * 2);
  3231. argv_src += 2;
  3232. break;
  3233. case VALUE_TYPE_F32:
  3234. *(float32 *)argv_dst = *(float32 *)argv_src++;
  3235. break;
  3236. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3237. case VALUE_TYPE_EXTERNREF:
  3238. {
  3239. uint32 externref_idx = *argv_src++;
  3240. void *externref_obj;
  3241. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3242. goto fail;
  3243. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  3244. sizeof(uintptr_t));
  3245. break;
  3246. }
  3247. #endif
  3248. #if WASM_ENABLE_GC != 0
  3249. case REF_TYPE_FUNCREF:
  3250. case REF_TYPE_EXTERNREF:
  3251. case REF_TYPE_ANYREF:
  3252. case REF_TYPE_EQREF:
  3253. case REF_TYPE_HT_NULLABLE:
  3254. case REF_TYPE_HT_NON_NULLABLE:
  3255. case REF_TYPE_I31REF:
  3256. case REF_TYPE_NULLFUNCREF:
  3257. case REF_TYPE_NULLEXTERNREF:
  3258. case REF_TYPE_STRUCTREF:
  3259. case REF_TYPE_ARRAYREF:
  3260. case REF_TYPE_NULLREF:
  3261. #if WASM_ENABLE_STRINGREF != 0
  3262. case REF_TYPE_STRINGREF:
  3263. case REF_TYPE_STRINGVIEWWTF8:
  3264. case REF_TYPE_STRINGVIEWWTF16:
  3265. case REF_TYPE_STRINGVIEWITER:
  3266. #endif
  3267. {
  3268. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  3269. sizeof(uintptr_t));
  3270. argv_src += sizeof(uintptr_t) / sizeof(uint32);
  3271. break;
  3272. }
  3273. #endif
  3274. default:
  3275. bh_assert(0);
  3276. break;
  3277. }
  3278. }
  3279. exec_env->attachment = attachment;
  3280. invokeNativeRaw(exec_env, argv1);
  3281. exec_env->attachment = NULL;
  3282. if (func_type->result_count > 0) {
  3283. switch (func_type->types[func_type->param_count]) {
  3284. case VALUE_TYPE_I32:
  3285. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3286. case VALUE_TYPE_FUNCREF:
  3287. #endif
  3288. argv_ret[0] = *(uint32 *)argv1;
  3289. break;
  3290. case VALUE_TYPE_F32:
  3291. *(float32 *)argv_ret = *(float32 *)argv1;
  3292. break;
  3293. case VALUE_TYPE_I64:
  3294. case VALUE_TYPE_F64:
  3295. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  3296. sizeof(uint64));
  3297. break;
  3298. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3299. case VALUE_TYPE_EXTERNREF:
  3300. {
  3301. uint32 externref_idx;
  3302. uint64 externref_obj;
  3303. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  3304. sizeof(uint64));
  3305. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3306. (void *)(uintptr_t)externref_obj,
  3307. &externref_idx))
  3308. goto fail;
  3309. argv_ret[0] = externref_idx;
  3310. break;
  3311. }
  3312. #endif
  3313. #if WASM_ENABLE_GC != 0
  3314. case REF_TYPE_FUNCREF:
  3315. case REF_TYPE_EXTERNREF:
  3316. case REF_TYPE_ANYREF:
  3317. case REF_TYPE_EQREF:
  3318. case REF_TYPE_HT_NULLABLE:
  3319. case REF_TYPE_HT_NON_NULLABLE:
  3320. case REF_TYPE_I31REF:
  3321. case REF_TYPE_NULLFUNCREF:
  3322. case REF_TYPE_NULLEXTERNREF:
  3323. case REF_TYPE_STRUCTREF:
  3324. case REF_TYPE_ARRAYREF:
  3325. case REF_TYPE_NULLREF:
  3326. #if WASM_ENABLE_STRINGREF != 0
  3327. case REF_TYPE_STRINGREF:
  3328. case REF_TYPE_STRINGVIEWWTF8:
  3329. case REF_TYPE_STRINGVIEWWTF16:
  3330. case REF_TYPE_STRINGVIEWITER:
  3331. #endif
  3332. {
  3333. bh_memcpy_s(argv_ret, sizeof(uintptr_t), argv1,
  3334. sizeof(uintptr_t));
  3335. break;
  3336. }
  3337. #endif
  3338. default:
  3339. bh_assert(0);
  3340. break;
  3341. }
  3342. }
  3343. ret = !wasm_runtime_copy_exception(module, NULL);
  3344. fail:
  3345. if (argv1 != argv_buf)
  3346. wasm_runtime_free(argv1);
  3347. return ret;
  3348. }
  3349. /**
  3350. * Implementation of wasm_runtime_invoke_native()
  3351. */
  3352. /* The invoke native implementation on ARM platform with VFP co-processor */
  3353. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  3354. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3355. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3356. typedef void (*GenericFunctionPointer)();
  3357. void
  3358. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  3359. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3360. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3361. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3362. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3363. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3364. static volatile Float64FuncPtr invokeNative_Float64 =
  3365. (Float64FuncPtr)(uintptr_t)invokeNative;
  3366. static volatile Float32FuncPtr invokeNative_Float32 =
  3367. (Float32FuncPtr)(uintptr_t)invokeNative;
  3368. static volatile Int64FuncPtr invokeNative_Int64 =
  3369. (Int64FuncPtr)(uintptr_t)invokeNative;
  3370. static volatile Int32FuncPtr invokeNative_Int32 =
  3371. (Int32FuncPtr)(uintptr_t)invokeNative;
  3372. static volatile VoidFuncPtr invokeNative_Void =
  3373. (VoidFuncPtr)(uintptr_t)invokeNative;
  3374. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3375. #define MAX_REG_INTS 4
  3376. #define MAX_REG_FLOATS 16
  3377. #else
  3378. #define MAX_REG_INTS 8
  3379. #define MAX_REG_FLOATS 8
  3380. #endif
  3381. bool
  3382. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3383. const WASMFuncType *func_type, const char *signature,
  3384. void *attachment, uint32 *argv, uint32 argc,
  3385. uint32 *argv_ret)
  3386. {
  3387. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3388. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
  3389. */
  3390. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  3391. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3392. uint32 arg_i32, ptr_len;
  3393. uint32 result_count = func_type->result_count;
  3394. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3395. bool ret = false;
  3396. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3397. bool is_aot_func = (NULL == signature);
  3398. #endif
  3399. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3400. uint32 *fps;
  3401. int n_fps = 0;
  3402. #else
  3403. #define fps ints
  3404. #define n_fps n_ints
  3405. #endif
  3406. n_ints++; /* exec env */
  3407. /* Traverse firstly to calculate stack args count */
  3408. for (i = 0; i < func_type->param_count; i++) {
  3409. switch (func_type->types[i]) {
  3410. case VALUE_TYPE_I32:
  3411. #if WASM_ENABLE_GC != 0
  3412. case REF_TYPE_FUNCREF:
  3413. case REF_TYPE_EXTERNREF:
  3414. case REF_TYPE_ANYREF:
  3415. case REF_TYPE_EQREF:
  3416. case REF_TYPE_HT_NULLABLE:
  3417. case REF_TYPE_HT_NON_NULLABLE:
  3418. case REF_TYPE_I31REF:
  3419. case REF_TYPE_NULLFUNCREF:
  3420. case REF_TYPE_NULLEXTERNREF:
  3421. case REF_TYPE_STRUCTREF:
  3422. case REF_TYPE_ARRAYREF:
  3423. case REF_TYPE_NULLREF:
  3424. #if WASM_ENABLE_STRINGREF != 0
  3425. case REF_TYPE_STRINGREF:
  3426. case REF_TYPE_STRINGVIEWWTF8:
  3427. case REF_TYPE_STRINGVIEWWTF16:
  3428. case REF_TYPE_STRINGVIEWITER:
  3429. #endif
  3430. #endif
  3431. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3432. case VALUE_TYPE_FUNCREF:
  3433. case VALUE_TYPE_EXTERNREF:
  3434. #endif
  3435. if (n_ints < MAX_REG_INTS)
  3436. n_ints++;
  3437. else
  3438. n_stacks++;
  3439. break;
  3440. case VALUE_TYPE_I64:
  3441. if (n_ints < MAX_REG_INTS - 1) {
  3442. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3443. /* 64-bit data must be 8 bytes aligned in arm */
  3444. if (n_ints & 1)
  3445. n_ints++;
  3446. #endif
  3447. n_ints += 2;
  3448. }
  3449. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  3450. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  3451. /* part in register, part in stack */
  3452. else if (n_ints == MAX_REG_INTS - 1) {
  3453. n_ints++;
  3454. n_stacks++;
  3455. }
  3456. #endif
  3457. else {
  3458. /* 64-bit data in stack must be 8 bytes aligned
  3459. in arm and riscv32 */
  3460. #if !defined(BUILD_TARGET_ARC)
  3461. if (n_stacks & 1)
  3462. n_stacks++;
  3463. #endif
  3464. n_stacks += 2;
  3465. }
  3466. break;
  3467. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  3468. case VALUE_TYPE_F32:
  3469. if (n_fps < MAX_REG_FLOATS)
  3470. n_fps++;
  3471. else
  3472. n_stacks++;
  3473. break;
  3474. case VALUE_TYPE_F64:
  3475. if (n_fps < MAX_REG_FLOATS - 1) {
  3476. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3477. /* 64-bit data must be 8 bytes aligned in arm */
  3478. if (n_fps & 1)
  3479. n_fps++;
  3480. #endif
  3481. n_fps += 2;
  3482. }
  3483. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3484. else if (n_fps == MAX_REG_FLOATS - 1) {
  3485. n_fps++;
  3486. n_stacks++;
  3487. }
  3488. #endif
  3489. else {
  3490. /* 64-bit data in stack must be 8 bytes aligned
  3491. in arm and riscv32 */
  3492. #if !defined(BUILD_TARGET_ARC)
  3493. if (n_stacks & 1)
  3494. n_stacks++;
  3495. #endif
  3496. n_stacks += 2;
  3497. }
  3498. break;
  3499. #else /* BUILD_TARGET_RISCV32_ILP32D */
  3500. case VALUE_TYPE_F32:
  3501. case VALUE_TYPE_F64:
  3502. if (n_fps < MAX_REG_FLOATS) {
  3503. n_fps++;
  3504. }
  3505. else if (func_type->types[i] == VALUE_TYPE_F32
  3506. && n_ints < MAX_REG_INTS) {
  3507. /* use int reg firstly if available */
  3508. n_ints++;
  3509. }
  3510. else if (func_type->types[i] == VALUE_TYPE_F64
  3511. && n_ints < MAX_REG_INTS - 1) {
  3512. /* use int regs firstly if available */
  3513. if (n_ints & 1)
  3514. n_ints++;
  3515. ints += 2;
  3516. }
  3517. else {
  3518. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3519. */
  3520. if (n_stacks & 1)
  3521. n_stacks++;
  3522. n_stacks += 2;
  3523. }
  3524. break;
  3525. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3526. default:
  3527. bh_assert(0);
  3528. break;
  3529. }
  3530. }
  3531. for (i = 0; i < ext_ret_count; i++) {
  3532. if (n_ints < MAX_REG_INTS)
  3533. n_ints++;
  3534. else
  3535. n_stacks++;
  3536. }
  3537. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3538. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  3539. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3540. argc1 = MAX_REG_INTS + n_stacks;
  3541. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  3542. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  3543. #endif
  3544. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3545. size = sizeof(uint32) * (uint32)argc1;
  3546. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3547. 0))) {
  3548. return false;
  3549. }
  3550. }
  3551. ints = argv1;
  3552. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3553. fps = ints + MAX_REG_INTS;
  3554. stacks = fps + MAX_REG_FLOATS;
  3555. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3556. stacks = ints + MAX_REG_INTS;
  3557. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  3558. fps = ints + MAX_REG_INTS;
  3559. stacks = fps + MAX_REG_FLOATS * 2;
  3560. #endif
  3561. n_ints = 0;
  3562. n_fps = 0;
  3563. n_stacks = 0;
  3564. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  3565. /* Traverse secondly to fill in each argument */
  3566. for (i = 0; i < func_type->param_count; i++) {
  3567. switch (func_type->types[i]) {
  3568. case VALUE_TYPE_I32:
  3569. #if WASM_ENABLE_GC != 0
  3570. case REF_TYPE_FUNCREF:
  3571. case REF_TYPE_EXTERNREF:
  3572. case REF_TYPE_ANYREF:
  3573. case REF_TYPE_EQREF:
  3574. case REF_TYPE_HT_NULLABLE:
  3575. case REF_TYPE_HT_NON_NULLABLE:
  3576. case REF_TYPE_I31REF:
  3577. case REF_TYPE_NULLFUNCREF:
  3578. case REF_TYPE_NULLEXTERNREF:
  3579. case REF_TYPE_STRUCTREF:
  3580. case REF_TYPE_ARRAYREF:
  3581. case REF_TYPE_NULLREF:
  3582. #if WASM_ENABLE_STRINGREF != 0
  3583. case REF_TYPE_STRINGREF:
  3584. case REF_TYPE_STRINGVIEWWTF8:
  3585. case REF_TYPE_STRINGVIEWWTF16:
  3586. case REF_TYPE_STRINGVIEWITER:
  3587. #endif
  3588. #endif
  3589. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3590. case VALUE_TYPE_FUNCREF:
  3591. #endif
  3592. {
  3593. arg_i32 = *argv_src++;
  3594. if (signature) {
  3595. if (signature[i + 1] == '*') {
  3596. /* param is a pointer */
  3597. if (signature[i + 2] == '~')
  3598. /* pointer with length followed */
  3599. ptr_len = *argv_src;
  3600. else
  3601. /* pointer without length followed */
  3602. ptr_len = 1;
  3603. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3604. ptr_len))
  3605. goto fail;
  3606. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3607. module, arg_i32);
  3608. }
  3609. else if (signature[i + 1] == '$') {
  3610. /* param is a string */
  3611. if (!wasm_runtime_validate_app_str_addr(module,
  3612. arg_i32))
  3613. goto fail;
  3614. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3615. module, arg_i32);
  3616. }
  3617. }
  3618. if (n_ints < MAX_REG_INTS)
  3619. ints[n_ints++] = arg_i32;
  3620. else
  3621. stacks[n_stacks++] = arg_i32;
  3622. break;
  3623. }
  3624. case VALUE_TYPE_I64:
  3625. {
  3626. if (n_ints < MAX_REG_INTS - 1) {
  3627. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3628. /* 64-bit data must be 8 bytes aligned in arm */
  3629. if (n_ints & 1)
  3630. n_ints++;
  3631. #endif
  3632. ints[n_ints++] = *argv_src++;
  3633. ints[n_ints++] = *argv_src++;
  3634. }
  3635. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  3636. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  3637. else if (n_ints == MAX_REG_INTS - 1) {
  3638. ints[n_ints++] = *argv_src++;
  3639. stacks[n_stacks++] = *argv_src++;
  3640. }
  3641. #endif
  3642. else {
  3643. /* 64-bit data in stack must be 8 bytes aligned
  3644. in arm and riscv32 */
  3645. #if !defined(BUILD_TARGET_ARC)
  3646. if (n_stacks & 1)
  3647. n_stacks++;
  3648. #endif
  3649. stacks[n_stacks++] = *argv_src++;
  3650. stacks[n_stacks++] = *argv_src++;
  3651. }
  3652. break;
  3653. }
  3654. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  3655. case VALUE_TYPE_F32:
  3656. {
  3657. if (n_fps < MAX_REG_FLOATS)
  3658. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3659. else
  3660. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3661. break;
  3662. }
  3663. case VALUE_TYPE_F64:
  3664. {
  3665. if (n_fps < MAX_REG_FLOATS - 1) {
  3666. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3667. /* 64-bit data must be 8 bytes aligned in arm */
  3668. if (n_fps & 1)
  3669. n_fps++;
  3670. #endif
  3671. fps[n_fps++] = *argv_src++;
  3672. fps[n_fps++] = *argv_src++;
  3673. }
  3674. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3675. else if (n_fps == MAX_REG_FLOATS - 1) {
  3676. fps[n_fps++] = *argv_src++;
  3677. stacks[n_stacks++] = *argv_src++;
  3678. }
  3679. #endif
  3680. else {
  3681. /* 64-bit data in stack must be 8 bytes aligned
  3682. in arm and riscv32 */
  3683. #if !defined(BUILD_TARGET_ARC)
  3684. if (n_stacks & 1)
  3685. n_stacks++;
  3686. #endif
  3687. stacks[n_stacks++] = *argv_src++;
  3688. stacks[n_stacks++] = *argv_src++;
  3689. }
  3690. break;
  3691. }
  3692. #else /* BUILD_TARGET_RISCV32_ILP32D */
  3693. case VALUE_TYPE_F32:
  3694. case VALUE_TYPE_F64:
  3695. {
  3696. if (n_fps < MAX_REG_FLOATS) {
  3697. if (func_type->types[i] == VALUE_TYPE_F32) {
  3698. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  3699. /* NaN boxing, the upper bits of a valid NaN-boxed
  3700. value must be all 1s. */
  3701. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  3702. }
  3703. else {
  3704. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  3705. argv_src += 2;
  3706. }
  3707. n_fps++;
  3708. }
  3709. else if (func_type->types[i] == VALUE_TYPE_F32
  3710. && n_ints < MAX_REG_INTS) {
  3711. /* use int reg firstly if available */
  3712. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  3713. }
  3714. else if (func_type->types[i] == VALUE_TYPE_F64
  3715. && n_ints < MAX_REG_INTS - 1) {
  3716. /* use int regs firstly if available */
  3717. if (n_ints & 1)
  3718. n_ints++;
  3719. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  3720. n_ints += 2;
  3721. argv_src += 2;
  3722. }
  3723. else {
  3724. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3725. */
  3726. if (n_stacks & 1)
  3727. n_stacks++;
  3728. if (func_type->types[i] == VALUE_TYPE_F32) {
  3729. *(float32 *)&stacks[n_stacks++] =
  3730. *(float32 *)argv_src++;
  3731. }
  3732. else {
  3733. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  3734. argv_src += 2;
  3735. n_stacks += 2;
  3736. }
  3737. }
  3738. break;
  3739. }
  3740. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3741. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3742. case VALUE_TYPE_EXTERNREF:
  3743. {
  3744. uint32 externref_idx = *argv_src++;
  3745. if (is_aot_func) {
  3746. if (n_ints < MAX_REG_INTS)
  3747. ints[n_ints++] = externref_idx;
  3748. else
  3749. stacks[n_stacks++] = externref_idx;
  3750. }
  3751. else {
  3752. void *externref_obj;
  3753. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3754. goto fail;
  3755. if (n_ints < MAX_REG_INTS)
  3756. ints[n_ints++] = (uintptr_t)externref_obj;
  3757. else
  3758. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3759. }
  3760. break;
  3761. }
  3762. #endif
  3763. default:
  3764. bh_assert(0);
  3765. break;
  3766. }
  3767. }
  3768. /* Save extra result values' address to argv1 */
  3769. for (i = 0; i < ext_ret_count; i++) {
  3770. if (n_ints < MAX_REG_INTS)
  3771. ints[n_ints++] = *(uint32 *)argv_src++;
  3772. else
  3773. stacks[n_stacks++] = *(uint32 *)argv_src++;
  3774. }
  3775. exec_env->attachment = attachment;
  3776. if (func_type->result_count == 0) {
  3777. invokeNative_Void(func_ptr, argv1, n_stacks);
  3778. }
  3779. else {
  3780. switch (func_type->types[func_type->param_count]) {
  3781. case VALUE_TYPE_I32:
  3782. #if WASM_ENABLE_GC != 0
  3783. case REF_TYPE_FUNCREF:
  3784. case REF_TYPE_EXTERNREF:
  3785. case REF_TYPE_ANYREF:
  3786. case REF_TYPE_EQREF:
  3787. case REF_TYPE_HT_NULLABLE:
  3788. case REF_TYPE_HT_NON_NULLABLE:
  3789. case REF_TYPE_I31REF:
  3790. case REF_TYPE_NULLFUNCREF:
  3791. case REF_TYPE_NULLEXTERNREF:
  3792. case REF_TYPE_STRUCTREF:
  3793. case REF_TYPE_ARRAYREF:
  3794. case REF_TYPE_NULLREF:
  3795. #if WASM_ENABLE_STRINGREF != 0
  3796. case REF_TYPE_STRINGREF:
  3797. case REF_TYPE_STRINGVIEWWTF8:
  3798. case REF_TYPE_STRINGVIEWWTF16:
  3799. case REF_TYPE_STRINGVIEWITER:
  3800. #endif
  3801. #endif
  3802. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3803. case VALUE_TYPE_FUNCREF:
  3804. #endif
  3805. argv_ret[0] =
  3806. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3807. break;
  3808. case VALUE_TYPE_I64:
  3809. PUT_I64_TO_ADDR(argv_ret,
  3810. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3811. break;
  3812. case VALUE_TYPE_F32:
  3813. *(float32 *)argv_ret =
  3814. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3815. break;
  3816. case VALUE_TYPE_F64:
  3817. PUT_F64_TO_ADDR(
  3818. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3819. break;
  3820. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3821. case VALUE_TYPE_EXTERNREF:
  3822. {
  3823. if (is_aot_func) {
  3824. uint32 externref_idx =
  3825. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3826. argv_ret[0] = externref_idx;
  3827. }
  3828. else {
  3829. uint32 externref_idx;
  3830. void *externref_obj;
  3831. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3832. func_ptr, argv1, argc1);
  3833. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3834. externref_obj, &externref_idx))
  3835. goto fail;
  3836. argv_ret[0] = externref_idx;
  3837. }
  3838. break;
  3839. }
  3840. #endif
  3841. default:
  3842. bh_assert(0);
  3843. break;
  3844. }
  3845. }
  3846. exec_env->attachment = NULL;
  3847. ret = !wasm_runtime_copy_exception(module, NULL);
  3848. fail:
  3849. if (argv1 != argv_buf)
  3850. wasm_runtime_free(argv1);
  3851. return ret;
  3852. }
  3853. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  3854. || defined(BUILD_TARGET_THUMB_VFP) \
  3855. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3856. || defined(BUILD_TARGET_RISCV32_ILP32) \
  3857. || defined(BUILD_TARGET_ARC) */
  3858. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  3859. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  3860. || defined(BUILD_TARGET_XTENSA)
  3861. typedef void (*GenericFunctionPointer)();
  3862. void
  3863. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3864. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3865. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3866. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3867. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3868. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3869. static volatile Int64FuncPtr invokeNative_Int64 =
  3870. (Int64FuncPtr)(uintptr_t)invokeNative;
  3871. static volatile Int32FuncPtr invokeNative_Int32 =
  3872. (Int32FuncPtr)(uintptr_t)invokeNative;
  3873. static volatile Float64FuncPtr invokeNative_Float64 =
  3874. (Float64FuncPtr)(uintptr_t)invokeNative;
  3875. static volatile Float32FuncPtr invokeNative_Float32 =
  3876. (Float32FuncPtr)(uintptr_t)invokeNative;
  3877. static volatile VoidFuncPtr invokeNative_Void =
  3878. (VoidFuncPtr)(uintptr_t)invokeNative;
  3879. static inline void
  3880. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3881. {
  3882. for (; num > 0; num--)
  3883. *dest++ = *src++;
  3884. }
  3885. bool
  3886. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3887. const WASMFuncType *func_type, const char *signature,
  3888. void *attachment, uint32 *argv, uint32 argc,
  3889. uint32 *argv_ret)
  3890. {
  3891. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3892. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3893. uint32 arg_i32, ptr_len;
  3894. uint32 result_count = func_type->result_count;
  3895. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3896. uint64 size;
  3897. bool ret = false;
  3898. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3899. bool is_aot_func = (NULL == signature);
  3900. #endif
  3901. #if defined(BUILD_TARGET_X86_32)
  3902. argc1 = argc + ext_ret_count + 2;
  3903. #else
  3904. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3905. so we need to allocate more memory. */
  3906. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3907. #endif
  3908. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3909. size = sizeof(uint32) * (uint64)argc1;
  3910. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3911. 0))) {
  3912. return false;
  3913. }
  3914. }
  3915. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  3916. argv1[j++] = ((uint32 *)&exec_env)[i];
  3917. for (i = 0; i < func_type->param_count; i++) {
  3918. switch (func_type->types[i]) {
  3919. case VALUE_TYPE_I32:
  3920. #if WASM_ENABLE_GC != 0
  3921. case REF_TYPE_FUNCREF:
  3922. case REF_TYPE_EXTERNREF:
  3923. case REF_TYPE_ANYREF:
  3924. case REF_TYPE_EQREF:
  3925. case REF_TYPE_HT_NULLABLE:
  3926. case REF_TYPE_HT_NON_NULLABLE:
  3927. case REF_TYPE_I31REF:
  3928. case REF_TYPE_NULLFUNCREF:
  3929. case REF_TYPE_NULLEXTERNREF:
  3930. case REF_TYPE_STRUCTREF:
  3931. case REF_TYPE_ARRAYREF:
  3932. case REF_TYPE_NULLREF:
  3933. #if WASM_ENABLE_STRINGREF != 0
  3934. case REF_TYPE_STRINGREF:
  3935. case REF_TYPE_STRINGVIEWWTF8:
  3936. case REF_TYPE_STRINGVIEWWTF16:
  3937. case REF_TYPE_STRINGVIEWITER:
  3938. #endif
  3939. #endif
  3940. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3941. case VALUE_TYPE_FUNCREF:
  3942. #endif
  3943. {
  3944. arg_i32 = *argv++;
  3945. if (signature) {
  3946. if (signature[i + 1] == '*') {
  3947. /* param is a pointer */
  3948. if (signature[i + 2] == '~')
  3949. /* pointer with length followed */
  3950. ptr_len = *argv;
  3951. else
  3952. /* pointer without length followed */
  3953. ptr_len = 1;
  3954. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3955. ptr_len))
  3956. goto fail;
  3957. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3958. module, arg_i32);
  3959. }
  3960. else if (signature[i + 1] == '$') {
  3961. /* param is a string */
  3962. if (!wasm_runtime_validate_app_str_addr(module,
  3963. arg_i32))
  3964. goto fail;
  3965. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3966. module, arg_i32);
  3967. }
  3968. }
  3969. argv1[j++] = arg_i32;
  3970. break;
  3971. }
  3972. case VALUE_TYPE_I64:
  3973. case VALUE_TYPE_F64:
  3974. #if !defined(BUILD_TARGET_X86_32)
  3975. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3976. and xtensa */
  3977. if (j & 1)
  3978. j++;
  3979. #endif
  3980. argv1[j++] = *argv++;
  3981. argv1[j++] = *argv++;
  3982. break;
  3983. case VALUE_TYPE_F32:
  3984. argv1[j++] = *argv++;
  3985. break;
  3986. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3987. case VALUE_TYPE_EXTERNREF:
  3988. {
  3989. uint32 externref_idx = *argv++;
  3990. if (is_aot_func) {
  3991. argv1[j++] = externref_idx;
  3992. }
  3993. else {
  3994. void *externref_obj;
  3995. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3996. goto fail;
  3997. argv1[j++] = (uintptr_t)externref_obj;
  3998. }
  3999. break;
  4000. }
  4001. #endif
  4002. default:
  4003. bh_assert(0);
  4004. break;
  4005. }
  4006. }
  4007. /* Save extra result values' address to argv1 */
  4008. word_copy(argv1 + j, argv, ext_ret_count);
  4009. argc1 = j + ext_ret_count;
  4010. exec_env->attachment = attachment;
  4011. if (func_type->result_count == 0) {
  4012. invokeNative_Void(func_ptr, argv1, argc1);
  4013. }
  4014. else {
  4015. switch (func_type->types[func_type->param_count]) {
  4016. case VALUE_TYPE_I32:
  4017. #if WASM_ENABLE_GC != 0
  4018. case REF_TYPE_FUNCREF:
  4019. case REF_TYPE_EXTERNREF:
  4020. case REF_TYPE_ANYREF:
  4021. case REF_TYPE_EQREF:
  4022. case REF_TYPE_HT_NULLABLE:
  4023. case REF_TYPE_HT_NON_NULLABLE:
  4024. case REF_TYPE_I31REF:
  4025. case REF_TYPE_NULLFUNCREF:
  4026. case REF_TYPE_NULLEXTERNREF:
  4027. case REF_TYPE_STRUCTREF:
  4028. case REF_TYPE_ARRAYREF:
  4029. case REF_TYPE_NULLREF:
  4030. #if WASM_ENABLE_STRINGREF != 0
  4031. case REF_TYPE_STRINGREF:
  4032. case REF_TYPE_STRINGVIEWWTF8:
  4033. case REF_TYPE_STRINGVIEWWTF16:
  4034. case REF_TYPE_STRINGVIEWITER:
  4035. #endif
  4036. #endif
  4037. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4038. case VALUE_TYPE_FUNCREF:
  4039. #endif
  4040. argv_ret[0] =
  4041. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4042. break;
  4043. case VALUE_TYPE_I64:
  4044. PUT_I64_TO_ADDR(argv_ret,
  4045. invokeNative_Int64(func_ptr, argv1, argc1));
  4046. break;
  4047. case VALUE_TYPE_F32:
  4048. *(float32 *)argv_ret =
  4049. invokeNative_Float32(func_ptr, argv1, argc1);
  4050. break;
  4051. case VALUE_TYPE_F64:
  4052. PUT_F64_TO_ADDR(argv_ret,
  4053. invokeNative_Float64(func_ptr, argv1, argc1));
  4054. break;
  4055. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4056. case VALUE_TYPE_EXTERNREF:
  4057. {
  4058. if (is_aot_func) {
  4059. uint32 externref_idx =
  4060. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4061. argv_ret[0] = externref_idx;
  4062. }
  4063. else {
  4064. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  4065. func_ptr, argv1, argc1);
  4066. uint32 externref_idx;
  4067. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4068. externref_obj, &externref_idx))
  4069. goto fail;
  4070. argv_ret[0] = externref_idx;
  4071. }
  4072. break;
  4073. }
  4074. #endif
  4075. default:
  4076. bh_assert(0);
  4077. break;
  4078. }
  4079. }
  4080. exec_env->attachment = NULL;
  4081. ret = !wasm_runtime_copy_exception(module, NULL);
  4082. fail:
  4083. if (argv1 != argv_buf)
  4084. wasm_runtime_free(argv1);
  4085. return ret;
  4086. }
  4087. #endif /* end of defined(BUILD_TARGET_X86_32) \
  4088. || defined(BUILD_TARGET_ARM) \
  4089. || defined(BUILD_TARGET_THUMB) \
  4090. || defined(BUILD_TARGET_MIPS) \
  4091. || defined(BUILD_TARGET_XTENSA) */
  4092. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  4093. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  4094. || defined(BUILD_TARGET_RISCV64_LP64)
  4095. #if WASM_ENABLE_SIMD != 0
  4096. #ifdef v128
  4097. #undef v128
  4098. #endif
  4099. #if defined(_WIN32) || defined(_WIN32_)
  4100. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  4101. __int8 m128i_i8[16];
  4102. __int16 m128i_i16[8];
  4103. __int32 m128i_i32[4];
  4104. __int64 m128i_i64[2];
  4105. unsigned __int8 m128i_u8[16];
  4106. unsigned __int16 m128i_u16[8];
  4107. unsigned __int32 m128i_u32[4];
  4108. unsigned __int64 m128i_u64[2];
  4109. } v128;
  4110. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  4111. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4112. || defined(BUILD_TARGET_RISCV64_LP64)
  4113. typedef long long v128
  4114. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  4115. #elif defined(BUILD_TARGET_AARCH64)
  4116. #include <arm_neon.h>
  4117. typedef uint32x4_t __m128i;
  4118. #define v128 __m128i
  4119. #endif
  4120. #endif /* end of WASM_ENABLE_SIMD != 0 */
  4121. typedef void (*GenericFunctionPointer)();
  4122. void
  4123. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  4124. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4125. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4126. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4127. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4128. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4129. static volatile Float64FuncPtr invokeNative_Float64 =
  4130. (Float64FuncPtr)(uintptr_t)invokeNative;
  4131. static volatile Float32FuncPtr invokeNative_Float32 =
  4132. (Float32FuncPtr)(uintptr_t)invokeNative;
  4133. static volatile Int64FuncPtr invokeNative_Int64 =
  4134. (Int64FuncPtr)(uintptr_t)invokeNative;
  4135. static volatile Int32FuncPtr invokeNative_Int32 =
  4136. (Int32FuncPtr)(uintptr_t)invokeNative;
  4137. static volatile VoidFuncPtr invokeNative_Void =
  4138. (VoidFuncPtr)(uintptr_t)invokeNative;
  4139. #if WASM_ENABLE_SIMD != 0
  4140. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4141. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  4142. #endif
  4143. #if defined(_WIN32) || defined(_WIN32_)
  4144. #define MAX_REG_FLOATS 4
  4145. #define MAX_REG_INTS 4
  4146. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  4147. #define MAX_REG_FLOATS 8
  4148. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  4149. || defined(BUILD_TARGET_RISCV64_LP64)
  4150. #define MAX_REG_INTS 8
  4151. #else
  4152. #define MAX_REG_INTS 6
  4153. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  4154. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4155. || defined(BUILD_TARGET_RISCV64_LP64) */
  4156. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  4157. /*
  4158. * ASAN is not designed to work with custom stack unwind or other low-level
  4159. * things. Ignore a function that does some low-level magic. (e.g. walking
  4160. * through the thread's stack bypassing the frame boundaries)
  4161. */
  4162. #if defined(__GNUC__) || defined(__clang__)
  4163. __attribute__((no_sanitize_address))
  4164. #endif
  4165. bool
  4166. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4167. const WASMFuncType *func_type, const char *signature,
  4168. void *attachment, uint32 *argv, uint32 argc,
  4169. uint32 *argv_ret)
  4170. {
  4171. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4172. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  4173. arg_i64;
  4174. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  4175. uint32 arg_i32, ptr_len;
  4176. uint32 result_count = func_type->result_count;
  4177. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4178. bool ret = false;
  4179. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4180. bool is_aot_func = (NULL == signature);
  4181. #endif
  4182. #ifndef BUILD_TARGET_RISCV64_LP64
  4183. #if WASM_ENABLE_SIMD == 0
  4184. uint64 *fps;
  4185. #else
  4186. v128 *fps;
  4187. #endif
  4188. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  4189. #define fps ints
  4190. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  4191. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  4192. /* important difference in calling conventions */
  4193. #define n_fps n_ints
  4194. #else
  4195. int n_fps = 0;
  4196. #endif
  4197. #if WASM_ENABLE_SIMD == 0
  4198. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  4199. #else
  4200. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  4201. + ext_ret_count;
  4202. #endif
  4203. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  4204. size = sizeof(uint64) * (uint64)argc1;
  4205. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4206. 0))) {
  4207. return false;
  4208. }
  4209. }
  4210. #ifndef BUILD_TARGET_RISCV64_LP64
  4211. #if WASM_ENABLE_SIMD == 0
  4212. fps = argv1;
  4213. ints = fps + MAX_REG_FLOATS;
  4214. #else
  4215. fps = (v128 *)argv1;
  4216. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  4217. #endif
  4218. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  4219. ints = argv1;
  4220. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  4221. stacks = ints + MAX_REG_INTS;
  4222. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  4223. for (i = 0; i < func_type->param_count; i++) {
  4224. switch (func_type->types[i]) {
  4225. case VALUE_TYPE_I32:
  4226. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4227. case VALUE_TYPE_FUNCREF:
  4228. #endif
  4229. {
  4230. arg_i32 = *argv_src++;
  4231. arg_i64 = arg_i32;
  4232. if (signature) {
  4233. if (signature[i + 1] == '*') {
  4234. /* param is a pointer */
  4235. if (signature[i + 2] == '~')
  4236. /* pointer with length followed */
  4237. ptr_len = *argv_src;
  4238. else
  4239. /* pointer without length followed */
  4240. ptr_len = 1;
  4241. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  4242. ptr_len))
  4243. goto fail;
  4244. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4245. module, arg_i32);
  4246. }
  4247. else if (signature[i + 1] == '$') {
  4248. /* param is a string */
  4249. if (!wasm_runtime_validate_app_str_addr(module,
  4250. arg_i32))
  4251. goto fail;
  4252. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4253. module, arg_i32);
  4254. }
  4255. }
  4256. if (n_ints < MAX_REG_INTS)
  4257. ints[n_ints++] = arg_i64;
  4258. else
  4259. stacks[n_stacks++] = arg_i64;
  4260. break;
  4261. }
  4262. case VALUE_TYPE_I64:
  4263. #if WASM_ENABLE_GC != 0
  4264. case REF_TYPE_FUNCREF:
  4265. case REF_TYPE_EXTERNREF:
  4266. case REF_TYPE_ANYREF:
  4267. case REF_TYPE_EQREF:
  4268. case REF_TYPE_HT_NULLABLE:
  4269. case REF_TYPE_HT_NON_NULLABLE:
  4270. case REF_TYPE_I31REF:
  4271. case REF_TYPE_NULLFUNCREF:
  4272. case REF_TYPE_NULLEXTERNREF:
  4273. case REF_TYPE_STRUCTREF:
  4274. case REF_TYPE_ARRAYREF:
  4275. case REF_TYPE_NULLREF:
  4276. #if WASM_ENABLE_STRINGREF != 0
  4277. case REF_TYPE_STRINGREF:
  4278. case REF_TYPE_STRINGVIEWWTF8:
  4279. case REF_TYPE_STRINGVIEWWTF16:
  4280. case REF_TYPE_STRINGVIEWITER:
  4281. #endif
  4282. #endif
  4283. if (n_ints < MAX_REG_INTS)
  4284. ints[n_ints++] = *(uint64 *)argv_src;
  4285. else
  4286. stacks[n_stacks++] = *(uint64 *)argv_src;
  4287. argv_src += 2;
  4288. break;
  4289. case VALUE_TYPE_F32:
  4290. if (n_fps < MAX_REG_FLOATS) {
  4291. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  4292. }
  4293. else {
  4294. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  4295. }
  4296. break;
  4297. case VALUE_TYPE_F64:
  4298. if (n_fps < MAX_REG_FLOATS) {
  4299. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  4300. }
  4301. else {
  4302. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  4303. }
  4304. argv_src += 2;
  4305. break;
  4306. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4307. case VALUE_TYPE_EXTERNREF:
  4308. {
  4309. uint32 externref_idx = *argv_src++;
  4310. if (is_aot_func) {
  4311. if (n_ints < MAX_REG_INTS)
  4312. ints[n_ints++] = externref_idx;
  4313. else
  4314. stacks[n_stacks++] = externref_idx;
  4315. }
  4316. else {
  4317. void *externref_obj;
  4318. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4319. goto fail;
  4320. if (n_ints < MAX_REG_INTS)
  4321. ints[n_ints++] = (uintptr_t)externref_obj;
  4322. else
  4323. stacks[n_stacks++] = (uintptr_t)externref_obj;
  4324. }
  4325. break;
  4326. }
  4327. #endif
  4328. #if WASM_ENABLE_SIMD != 0
  4329. case VALUE_TYPE_V128:
  4330. if (n_fps < MAX_REG_FLOATS) {
  4331. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  4332. }
  4333. else {
  4334. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  4335. n_stacks++;
  4336. }
  4337. argv_src += 4;
  4338. break;
  4339. #endif
  4340. default:
  4341. bh_assert(0);
  4342. break;
  4343. }
  4344. }
  4345. /* Save extra result values' address to argv1 */
  4346. for (i = 0; i < ext_ret_count; i++) {
  4347. if (n_ints < MAX_REG_INTS)
  4348. ints[n_ints++] = *(uint64 *)argv_src;
  4349. else
  4350. stacks[n_stacks++] = *(uint64 *)argv_src;
  4351. argv_src += 2;
  4352. }
  4353. exec_env->attachment = attachment;
  4354. if (result_count == 0) {
  4355. invokeNative_Void(func_ptr, argv1, n_stacks);
  4356. }
  4357. else {
  4358. /* Invoke the native function and get the first result value */
  4359. switch (func_type->types[func_type->param_count]) {
  4360. case VALUE_TYPE_I32:
  4361. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4362. case VALUE_TYPE_FUNCREF:
  4363. #endif
  4364. argv_ret[0] =
  4365. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  4366. break;
  4367. case VALUE_TYPE_I64:
  4368. #if WASM_ENABLE_GC != 0
  4369. case REF_TYPE_FUNCREF:
  4370. case REF_TYPE_EXTERNREF:
  4371. case REF_TYPE_ANYREF:
  4372. case REF_TYPE_EQREF:
  4373. case REF_TYPE_HT_NULLABLE:
  4374. case REF_TYPE_HT_NON_NULLABLE:
  4375. case REF_TYPE_I31REF:
  4376. case REF_TYPE_NULLFUNCREF:
  4377. case REF_TYPE_NULLEXTERNREF:
  4378. case REF_TYPE_STRUCTREF:
  4379. case REF_TYPE_ARRAYREF:
  4380. case REF_TYPE_NULLREF:
  4381. #if WASM_ENABLE_STRINGREF != 0
  4382. case REF_TYPE_STRINGREF:
  4383. case REF_TYPE_STRINGVIEWWTF8:
  4384. case REF_TYPE_STRINGVIEWWTF16:
  4385. case REF_TYPE_STRINGVIEWITER:
  4386. #endif
  4387. #endif
  4388. PUT_I64_TO_ADDR(argv_ret,
  4389. invokeNative_Int64(func_ptr, argv1, n_stacks));
  4390. break;
  4391. case VALUE_TYPE_F32:
  4392. *(float32 *)argv_ret =
  4393. invokeNative_Float32(func_ptr, argv1, n_stacks);
  4394. break;
  4395. case VALUE_TYPE_F64:
  4396. PUT_F64_TO_ADDR(
  4397. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  4398. break;
  4399. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4400. case VALUE_TYPE_EXTERNREF:
  4401. {
  4402. if (is_aot_func) {
  4403. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  4404. }
  4405. else {
  4406. uint32 externref_idx;
  4407. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  4408. func_ptr, argv1, n_stacks);
  4409. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4410. externref_obj, &externref_idx))
  4411. goto fail;
  4412. argv_ret[0] = externref_idx;
  4413. }
  4414. break;
  4415. }
  4416. #endif
  4417. #if WASM_ENABLE_SIMD != 0
  4418. case VALUE_TYPE_V128:
  4419. *(v128 *)argv_ret =
  4420. invokeNative_V128(func_ptr, argv1, n_stacks);
  4421. break;
  4422. #endif
  4423. default:
  4424. bh_assert(0);
  4425. break;
  4426. }
  4427. }
  4428. exec_env->attachment = NULL;
  4429. ret = !wasm_runtime_copy_exception(module, NULL);
  4430. fail:
  4431. if (argv1 != argv_buf)
  4432. wasm_runtime_free(argv1);
  4433. return ret;
  4434. }
  4435. #endif /* end of defined(BUILD_TARGET_X86_64) \
  4436. || defined(BUILD_TARGET_AMD_64) \
  4437. || defined(BUILD_TARGET_AARCH64) \
  4438. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4439. || defined(BUILD_TARGET_RISCV64_LP64) */
  4440. bool
  4441. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  4442. uint32 argc, uint32 argv[])
  4443. {
  4444. bool ret = false;
  4445. if (!wasm_runtime_exec_env_check(exec_env)) {
  4446. LOG_ERROR("Invalid exec env stack info.");
  4447. return false;
  4448. }
  4449. /* this function is called from native code, so exec_env->handle and
  4450. exec_env->native_stack_boundary must have been set, we don't set
  4451. it again */
  4452. #if WASM_ENABLE_INTERP != 0
  4453. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  4454. ret = wasm_call_indirect(exec_env, 0, element_index, argc, argv);
  4455. #endif
  4456. #if WASM_ENABLE_AOT != 0
  4457. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  4458. ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
  4459. #endif
  4460. return ret;
  4461. }
  4462. static void
  4463. exchange_uint32(uint8 *p_data)
  4464. {
  4465. uint8 value = *p_data;
  4466. *p_data = *(p_data + 3);
  4467. *(p_data + 3) = value;
  4468. value = *(p_data + 1);
  4469. *(p_data + 1) = *(p_data + 2);
  4470. *(p_data + 2) = value;
  4471. }
  4472. static void
  4473. exchange_uint64(uint8 *p_data)
  4474. {
  4475. uint32 value;
  4476. value = *(uint32 *)p_data;
  4477. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  4478. *(uint32 *)(p_data + 4) = value;
  4479. exchange_uint32(p_data);
  4480. exchange_uint32(p_data + 4);
  4481. }
  4482. void
  4483. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  4484. {
  4485. uint64 u1, u2;
  4486. bh_memcpy_s(&u1, 8, bytes, 8);
  4487. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  4488. if (!is_little_endian()) {
  4489. exchange_uint64((uint8 *)&u1);
  4490. exchange_uint64((uint8 *)&u2);
  4491. *ret1 = u2;
  4492. *ret2 = u1;
  4493. }
  4494. else {
  4495. *ret1 = u1;
  4496. *ret2 = u2;
  4497. }
  4498. }
  4499. #if WASM_ENABLE_THREAD_MGR != 0
  4500. typedef struct WASMThreadArg {
  4501. WASMExecEnv *new_exec_env;
  4502. wasm_thread_callback_t callback;
  4503. void *arg;
  4504. } WASMThreadArg;
  4505. WASMExecEnv *
  4506. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  4507. {
  4508. return wasm_cluster_spawn_exec_env(exec_env);
  4509. }
  4510. void
  4511. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  4512. {
  4513. wasm_cluster_destroy_spawned_exec_env(exec_env);
  4514. }
  4515. static void *
  4516. wasm_runtime_thread_routine(void *arg)
  4517. {
  4518. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  4519. void *ret;
  4520. bh_assert(thread_arg->new_exec_env);
  4521. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  4522. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  4523. wasm_runtime_free(thread_arg);
  4524. os_thread_exit(ret);
  4525. return ret;
  4526. }
  4527. int32
  4528. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  4529. wasm_thread_callback_t callback, void *arg)
  4530. {
  4531. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  4532. WASMThreadArg *thread_arg;
  4533. int32 ret;
  4534. if (!new_exec_env)
  4535. return -1;
  4536. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  4537. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  4538. return -1;
  4539. }
  4540. thread_arg->new_exec_env = new_exec_env;
  4541. thread_arg->callback = callback;
  4542. thread_arg->arg = arg;
  4543. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  4544. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  4545. if (ret != 0) {
  4546. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  4547. wasm_runtime_free(thread_arg);
  4548. }
  4549. return ret;
  4550. }
  4551. int32
  4552. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  4553. {
  4554. return os_thread_join((korp_tid)tid, retval);
  4555. }
  4556. #endif /* end of WASM_ENABLE_THREAD_MGR */
  4557. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4558. static korp_mutex externref_lock;
  4559. static uint32 externref_global_id = 1;
  4560. static HashMap *externref_map;
  4561. typedef struct ExternRefMapNode {
  4562. /* The extern object from runtime embedder */
  4563. void *extern_obj;
  4564. /* The module instance it belongs to */
  4565. WASMModuleInstanceCommon *module_inst;
  4566. /* Whether it is retained */
  4567. bool retained;
  4568. /* Whether it is marked by runtime */
  4569. bool marked;
  4570. /* cleanup function called when the externref is freed */
  4571. void (*cleanup)(void *);
  4572. } ExternRefMapNode;
  4573. static uint32
  4574. wasm_externref_hash(const void *key)
  4575. {
  4576. uint32 externref_idx = (uint32)(uintptr_t)key;
  4577. return externref_idx;
  4578. }
  4579. static bool
  4580. wasm_externref_equal(void *key1, void *key2)
  4581. {
  4582. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  4583. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  4584. return externref_idx1 == externref_idx2 ? true : false;
  4585. }
  4586. static bool
  4587. wasm_externref_map_init()
  4588. {
  4589. if (os_mutex_init(&externref_lock) != 0)
  4590. return false;
  4591. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  4592. wasm_externref_equal, NULL,
  4593. wasm_runtime_free))) {
  4594. os_mutex_destroy(&externref_lock);
  4595. return false;
  4596. }
  4597. externref_global_id = 1;
  4598. return true;
  4599. }
  4600. static void
  4601. wasm_externref_map_destroy()
  4602. {
  4603. bh_hash_map_destroy(externref_map);
  4604. os_mutex_destroy(&externref_lock);
  4605. }
  4606. typedef struct LookupExtObj_UserData {
  4607. ExternRefMapNode node;
  4608. bool found;
  4609. uint32 externref_idx;
  4610. } LookupExtObj_UserData;
  4611. static void
  4612. lookup_extobj_callback(void *key, void *value, void *user_data)
  4613. {
  4614. uint32 externref_idx = (uint32)(uintptr_t)key;
  4615. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4616. LookupExtObj_UserData *user_data_lookup =
  4617. (LookupExtObj_UserData *)user_data;
  4618. if (node->extern_obj == user_data_lookup->node.extern_obj
  4619. && node->module_inst == user_data_lookup->node.module_inst) {
  4620. user_data_lookup->found = true;
  4621. user_data_lookup->externref_idx = externref_idx;
  4622. }
  4623. }
  4624. static void
  4625. delete_externref(void *key, ExternRefMapNode *node)
  4626. {
  4627. bh_hash_map_remove(externref_map, key, NULL, NULL);
  4628. if (node->cleanup) {
  4629. (*node->cleanup)(node->extern_obj);
  4630. }
  4631. wasm_runtime_free(node);
  4632. }
  4633. static void
  4634. delete_extobj_callback(void *key, void *value, void *user_data)
  4635. {
  4636. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4637. LookupExtObj_UserData *lookup_user_data =
  4638. (LookupExtObj_UserData *)user_data;
  4639. if (node->extern_obj == lookup_user_data->node.extern_obj
  4640. && node->module_inst == lookup_user_data->node.module_inst) {
  4641. lookup_user_data->found = true;
  4642. delete_externref(key, node);
  4643. }
  4644. }
  4645. bool
  4646. wasm_externref_objdel(WASMModuleInstanceCommon *module_inst, void *extern_obj)
  4647. {
  4648. LookupExtObj_UserData lookup_user_data = { 0 };
  4649. bool ok = false;
  4650. /* in a wrapper, extern_obj could be any value */
  4651. lookup_user_data.node.extern_obj = extern_obj;
  4652. lookup_user_data.node.module_inst = module_inst;
  4653. lookup_user_data.found = false;
  4654. os_mutex_lock(&externref_lock);
  4655. /* Lookup hashmap firstly */
  4656. bh_hash_map_traverse(externref_map, delete_extobj_callback,
  4657. (void *)&lookup_user_data);
  4658. if (lookup_user_data.found) {
  4659. ok = true;
  4660. }
  4661. os_mutex_unlock(&externref_lock);
  4662. return ok;
  4663. }
  4664. bool
  4665. wasm_externref_set_cleanup(WASMModuleInstanceCommon *module_inst,
  4666. void *extern_obj, void (*extern_obj_cleanup)(void *))
  4667. {
  4668. LookupExtObj_UserData lookup_user_data = { 0 };
  4669. bool ok = false;
  4670. /* in a wrapper, extern_obj could be any value */
  4671. lookup_user_data.node.extern_obj = extern_obj;
  4672. lookup_user_data.node.module_inst = module_inst;
  4673. lookup_user_data.found = false;
  4674. os_mutex_lock(&externref_lock);
  4675. /* Lookup hashmap firstly */
  4676. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  4677. (void *)&lookup_user_data);
  4678. if (lookup_user_data.found) {
  4679. void *key = (void *)(uintptr_t)lookup_user_data.externref_idx;
  4680. ExternRefMapNode *node = bh_hash_map_find(externref_map, key);
  4681. node->cleanup = extern_obj_cleanup;
  4682. ok = true;
  4683. }
  4684. os_mutex_unlock(&externref_lock);
  4685. return ok;
  4686. }
  4687. bool
  4688. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  4689. uint32 *p_externref_idx)
  4690. {
  4691. LookupExtObj_UserData lookup_user_data = { 0 };
  4692. ExternRefMapNode *node;
  4693. uint32 externref_idx;
  4694. /*
  4695. * to catch a parameter from `wasm_application_execute_func`,
  4696. * which represents a string 'null'
  4697. */
  4698. #if UINTPTR_MAX == UINT32_MAX
  4699. if ((uint32)-1 == (uintptr_t)extern_obj) {
  4700. #else
  4701. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  4702. #endif
  4703. *p_externref_idx = NULL_REF;
  4704. return true;
  4705. }
  4706. /* in a wrapper, extern_obj could be any value */
  4707. lookup_user_data.node.extern_obj = extern_obj;
  4708. lookup_user_data.node.module_inst = module_inst;
  4709. lookup_user_data.found = false;
  4710. os_mutex_lock(&externref_lock);
  4711. /* Lookup hashmap firstly */
  4712. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  4713. (void *)&lookup_user_data);
  4714. if (lookup_user_data.found) {
  4715. *p_externref_idx = lookup_user_data.externref_idx;
  4716. os_mutex_unlock(&externref_lock);
  4717. return true;
  4718. }
  4719. /* Not found in hashmap */
  4720. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  4721. goto fail1;
  4722. }
  4723. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  4724. goto fail1;
  4725. }
  4726. memset(node, 0, sizeof(ExternRefMapNode));
  4727. node->extern_obj = extern_obj;
  4728. node->module_inst = module_inst;
  4729. node->cleanup = NULL;
  4730. externref_idx = externref_global_id;
  4731. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  4732. (void *)node)) {
  4733. goto fail2;
  4734. }
  4735. externref_global_id++;
  4736. *p_externref_idx = externref_idx;
  4737. os_mutex_unlock(&externref_lock);
  4738. return true;
  4739. fail2:
  4740. wasm_runtime_free(node);
  4741. fail1:
  4742. os_mutex_unlock(&externref_lock);
  4743. return false;
  4744. }
  4745. bool
  4746. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  4747. {
  4748. ExternRefMapNode *node;
  4749. /* catch a `ref.null` vairable */
  4750. if (externref_idx == NULL_REF) {
  4751. *p_extern_obj = NULL;
  4752. return true;
  4753. }
  4754. os_mutex_lock(&externref_lock);
  4755. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4756. os_mutex_unlock(&externref_lock);
  4757. if (!node)
  4758. return false;
  4759. *p_extern_obj = node->extern_obj;
  4760. return true;
  4761. }
  4762. static void
  4763. reclaim_extobj_callback(void *key, void *value, void *user_data)
  4764. {
  4765. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4766. WASMModuleInstanceCommon *module_inst =
  4767. (WASMModuleInstanceCommon *)user_data;
  4768. if (node->module_inst == module_inst) {
  4769. if (!node->marked && !node->retained) {
  4770. delete_externref(key, node);
  4771. }
  4772. else {
  4773. node->marked = false;
  4774. }
  4775. }
  4776. }
  4777. static void
  4778. mark_externref(uint32 externref_idx)
  4779. {
  4780. ExternRefMapNode *node;
  4781. if (externref_idx != NULL_REF) {
  4782. node =
  4783. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4784. if (node) {
  4785. node->marked = true;
  4786. }
  4787. }
  4788. }
  4789. #if WASM_ENABLE_INTERP != 0
  4790. static void
  4791. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  4792. {
  4793. uint32 i, j, externref_idx;
  4794. table_elem_type_t *table_data;
  4795. uint8 *global_data = module_inst->global_data;
  4796. WASMGlobalInstance *global;
  4797. WASMTableInstance *table;
  4798. global = module_inst->e->globals;
  4799. for (i = 0; i < module_inst->e->global_count; i++, global++) {
  4800. if (global->type == VALUE_TYPE_EXTERNREF) {
  4801. externref_idx = *(uint32 *)(global_data + global->data_offset);
  4802. mark_externref(externref_idx);
  4803. }
  4804. }
  4805. for (i = 0; i < module_inst->table_count; i++) {
  4806. uint8 elem_type = 0;
  4807. uint32 init_size, max_size;
  4808. table = wasm_get_table_inst(module_inst, i);
  4809. (void)wasm_runtime_get_table_inst_elem_type(
  4810. (WASMModuleInstanceCommon *)module_inst, i, &elem_type, &init_size,
  4811. &max_size);
  4812. if (elem_type == VALUE_TYPE_EXTERNREF) {
  4813. table_data = table->elems;
  4814. for (j = 0; j < table->cur_size; j++) {
  4815. externref_idx = table_data[j];
  4816. mark_externref(externref_idx);
  4817. }
  4818. }
  4819. (void)init_size;
  4820. (void)max_size;
  4821. }
  4822. }
  4823. #endif
  4824. #if WASM_ENABLE_AOT != 0
  4825. static void
  4826. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  4827. {
  4828. uint32 i = 0, j = 0;
  4829. const AOTModule *module = (AOTModule *)module_inst->module;
  4830. const AOTTable *table = module->tables;
  4831. const AOTGlobal *global = module->globals;
  4832. const AOTTableInstance *table_inst;
  4833. for (i = 0; i < module->global_count; i++, global++) {
  4834. if (global->type == VALUE_TYPE_EXTERNREF) {
  4835. mark_externref(
  4836. *(uint32 *)(module_inst->global_data + global->data_offset));
  4837. }
  4838. }
  4839. for (i = 0; i < module->table_count; i++) {
  4840. table_inst = module_inst->tables[i];
  4841. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  4842. while (j < table_inst->cur_size) {
  4843. mark_externref(table_inst->elems[j++]);
  4844. }
  4845. }
  4846. }
  4847. }
  4848. #endif
  4849. void
  4850. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  4851. {
  4852. os_mutex_lock(&externref_lock);
  4853. #if WASM_ENABLE_INTERP != 0
  4854. if (module_inst->module_type == Wasm_Module_Bytecode)
  4855. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  4856. #endif
  4857. #if WASM_ENABLE_AOT != 0
  4858. if (module_inst->module_type == Wasm_Module_AoT)
  4859. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  4860. #endif
  4861. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  4862. (void *)module_inst);
  4863. os_mutex_unlock(&externref_lock);
  4864. }
  4865. static void
  4866. cleanup_extobj_callback(void *key, void *value, void *user_data)
  4867. {
  4868. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4869. WASMModuleInstanceCommon *module_inst =
  4870. (WASMModuleInstanceCommon *)user_data;
  4871. if (node->module_inst == module_inst) {
  4872. delete_externref(key, node);
  4873. }
  4874. }
  4875. void
  4876. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  4877. {
  4878. os_mutex_lock(&externref_lock);
  4879. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  4880. (void *)module_inst);
  4881. os_mutex_unlock(&externref_lock);
  4882. }
  4883. bool
  4884. wasm_externref_retain(uint32 externref_idx)
  4885. {
  4886. ExternRefMapNode *node;
  4887. os_mutex_lock(&externref_lock);
  4888. if (externref_idx != NULL_REF) {
  4889. node =
  4890. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4891. if (node) {
  4892. node->retained = true;
  4893. os_mutex_unlock(&externref_lock);
  4894. return true;
  4895. }
  4896. }
  4897. os_mutex_unlock(&externref_lock);
  4898. return false;
  4899. }
  4900. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  4901. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  4902. uint32
  4903. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  4904. char **buf, uint32 *len)
  4905. {
  4906. if (dump_or_print) {
  4907. return (uint32)os_printf("%s", line_buf);
  4908. }
  4909. else if (*buf) {
  4910. uint32 dump_len;
  4911. dump_len = snprintf(*buf, *len, "%s", line_buf);
  4912. if (dump_len >= *len) {
  4913. dump_len = *len;
  4914. }
  4915. *len = *len - dump_len;
  4916. *buf = *buf + dump_len;
  4917. return dump_len;
  4918. }
  4919. else {
  4920. return (uint32)strlen(line_buf);
  4921. }
  4922. }
  4923. void
  4924. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  4925. {
  4926. WASMModuleInstanceCommon *module_inst =
  4927. wasm_exec_env_get_module_inst(exec_env);
  4928. #if WASM_ENABLE_INTERP != 0
  4929. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4930. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  4931. }
  4932. #endif
  4933. #if WASM_ENABLE_AOT != 0
  4934. if (module_inst->module_type == Wasm_Module_AoT) {
  4935. aot_dump_call_stack(exec_env, true, NULL, 0);
  4936. }
  4937. #endif
  4938. }
  4939. uint32
  4940. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
  4941. {
  4942. WASMModuleInstanceCommon *module_inst =
  4943. wasm_exec_env_get_module_inst(exec_env);
  4944. #if WASM_ENABLE_INTERP != 0
  4945. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4946. return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
  4947. }
  4948. #endif
  4949. #if WASM_ENABLE_AOT != 0
  4950. if (module_inst->module_type == Wasm_Module_AoT) {
  4951. return aot_dump_call_stack(exec_env, false, NULL, 0);
  4952. }
  4953. #endif
  4954. return 0;
  4955. }
  4956. uint32
  4957. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  4958. uint32 len)
  4959. {
  4960. WASMModuleInstanceCommon *module_inst =
  4961. wasm_exec_env_get_module_inst(exec_env);
  4962. #if WASM_ENABLE_INTERP != 0
  4963. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4964. return wasm_interp_dump_call_stack(exec_env, false, buf, len);
  4965. }
  4966. #endif
  4967. #if WASM_ENABLE_AOT != 0
  4968. if (module_inst->module_type == Wasm_Module_AoT) {
  4969. return aot_dump_call_stack(exec_env, false, buf, len);
  4970. }
  4971. #endif
  4972. return 0;
  4973. }
  4974. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  4975. #if WASM_ENABLE_STATIC_PGO != 0
  4976. uint32
  4977. wasm_runtime_get_pgo_prof_data_size(WASMModuleInstanceCommon *module_inst)
  4978. {
  4979. #if WASM_ENABLE_AOT != 0
  4980. if (module_inst->module_type == Wasm_Module_AoT) {
  4981. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  4982. return aot_get_pgo_prof_data_size(aot_inst);
  4983. }
  4984. #endif
  4985. return 0;
  4986. }
  4987. uint32
  4988. wasm_runtime_dump_pgo_prof_data_to_buf(WASMModuleInstanceCommon *module_inst,
  4989. char *buf, uint32 len)
  4990. {
  4991. #if WASM_ENABLE_AOT != 0
  4992. if (module_inst->module_type == Wasm_Module_AoT) {
  4993. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  4994. return aot_dump_pgo_prof_data_to_buf(aot_inst, buf, len);
  4995. }
  4996. #endif
  4997. return 0;
  4998. }
  4999. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  5000. bool
  5001. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  5002. uint32 table_idx, uint8 *out_elem_type,
  5003. #if WASM_ENABLE_GC != 0
  5004. WASMRefType **out_ref_type,
  5005. #endif
  5006. uint32 *out_min_size, uint32 *out_max_size)
  5007. {
  5008. #if WASM_ENABLE_INTERP != 0
  5009. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5010. WASMModule *module = (WASMModule *)module_comm;
  5011. if (table_idx < module->import_table_count) {
  5012. WASMTableImport *import_table =
  5013. &((module->import_tables + table_idx)->u.table);
  5014. *out_elem_type = import_table->elem_type;
  5015. #if WASM_ENABLE_GC != 0
  5016. *out_ref_type = import_table->elem_ref_type;
  5017. #endif
  5018. *out_min_size = import_table->init_size;
  5019. *out_max_size = import_table->max_size;
  5020. }
  5021. else {
  5022. WASMTable *table =
  5023. module->tables + (table_idx - module->import_table_count);
  5024. *out_elem_type = table->elem_type;
  5025. #if WASM_ENABLE_GC != 0
  5026. *out_ref_type = table->elem_ref_type;
  5027. #endif
  5028. *out_min_size = table->init_size;
  5029. *out_max_size = table->max_size;
  5030. }
  5031. return true;
  5032. }
  5033. #endif
  5034. #if WASM_ENABLE_AOT != 0
  5035. if (module_comm->module_type == Wasm_Module_AoT) {
  5036. AOTModule *module = (AOTModule *)module_comm;
  5037. if (table_idx < module->import_table_count) {
  5038. AOTImportTable *import_table = module->import_tables + table_idx;
  5039. *out_elem_type = import_table->elem_type;
  5040. #if WASM_ENABLE_GC != 0
  5041. *out_ref_type = NULL; /* TODO */
  5042. #endif
  5043. *out_min_size = import_table->table_init_size;
  5044. *out_max_size = import_table->table_max_size;
  5045. }
  5046. else {
  5047. AOTTable *table =
  5048. module->tables + (table_idx - module->import_table_count);
  5049. *out_elem_type = table->elem_type;
  5050. #if WASM_ENABLE_GC != 0
  5051. *out_ref_type = NULL; /* TODO */
  5052. #endif
  5053. *out_min_size = table->table_init_size;
  5054. *out_max_size = table->table_max_size;
  5055. }
  5056. return true;
  5057. }
  5058. #endif
  5059. return false;
  5060. }
  5061. bool
  5062. wasm_runtime_get_table_inst_elem_type(
  5063. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  5064. uint8 *out_elem_type,
  5065. #if WASM_ENABLE_GC != 0
  5066. WASMRefType **out_ref_type,
  5067. #endif
  5068. uint32 *out_min_size, uint32 *out_max_size)
  5069. {
  5070. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  5071. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  5072. || module_inst_comm->module_type == Wasm_Module_AoT);
  5073. return wasm_runtime_get_table_elem_type(
  5074. (WASMModuleCommon *)module_inst->module, table_idx, out_elem_type,
  5075. #if WASM_ENABLE_GC != 0
  5076. out_ref_type,
  5077. #endif
  5078. out_min_size, out_max_size);
  5079. }
  5080. bool
  5081. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  5082. const WASMExport *export, WASMFuncType **out)
  5083. {
  5084. #if WASM_ENABLE_INTERP != 0
  5085. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5086. WASMModule *module = (WASMModule *)module_comm;
  5087. if (export->index < module->import_function_count) {
  5088. *out = module->import_functions[export->index].u.function.func_type;
  5089. }
  5090. else {
  5091. *out =
  5092. module->functions[export->index - module->import_function_count]
  5093. ->func_type;
  5094. }
  5095. return true;
  5096. }
  5097. #endif
  5098. #if WASM_ENABLE_AOT != 0
  5099. if (module_comm->module_type == Wasm_Module_AoT) {
  5100. AOTModule *module = (AOTModule *)module_comm;
  5101. if (export->index < module->import_func_count) {
  5102. *out = (WASMFuncType *)
  5103. module->types[module->import_funcs[export->index]
  5104. .func_type_index];
  5105. }
  5106. else {
  5107. *out = (WASMFuncType *)module
  5108. ->types[module->func_type_indexes
  5109. [export->index - module->import_func_count]];
  5110. }
  5111. return true;
  5112. }
  5113. #endif
  5114. return false;
  5115. }
  5116. bool
  5117. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  5118. const WASMExport *export,
  5119. uint8 *out_val_type, bool *out_mutability)
  5120. {
  5121. #if WASM_ENABLE_INTERP != 0
  5122. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5123. WASMModule *module = (WASMModule *)module_comm;
  5124. if (export->index < module->import_global_count) {
  5125. WASMGlobalImport *import_global =
  5126. &((module->import_globals + export->index)->u.global);
  5127. *out_val_type = import_global->type;
  5128. *out_mutability = import_global->is_mutable;
  5129. }
  5130. else {
  5131. WASMGlobal *global =
  5132. module->globals + (export->index - module->import_global_count);
  5133. *out_val_type = global->type;
  5134. *out_mutability = global->is_mutable;
  5135. }
  5136. return true;
  5137. }
  5138. #endif
  5139. #if WASM_ENABLE_AOT != 0
  5140. if (module_comm->module_type == Wasm_Module_AoT) {
  5141. AOTModule *module = (AOTModule *)module_comm;
  5142. if (export->index < module->import_global_count) {
  5143. AOTImportGlobal *import_global =
  5144. module->import_globals + export->index;
  5145. *out_val_type = import_global->type;
  5146. *out_mutability = import_global->is_mutable;
  5147. }
  5148. else {
  5149. AOTGlobal *global =
  5150. module->globals + (export->index - module->import_global_count);
  5151. *out_val_type = global->type;
  5152. *out_mutability = global->is_mutable;
  5153. }
  5154. return true;
  5155. }
  5156. #endif
  5157. return false;
  5158. }
  5159. bool
  5160. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  5161. const WASMExport *export,
  5162. uint32 *out_min_page, uint32 *out_max_page)
  5163. {
  5164. #if WASM_ENABLE_INTERP != 0
  5165. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5166. WASMModule *module = (WASMModule *)module_comm;
  5167. if (export->index < module->import_memory_count) {
  5168. WASMMemoryImport *import_memory =
  5169. &((module->import_memories + export->index)->u.memory);
  5170. *out_min_page = import_memory->init_page_count;
  5171. *out_max_page = import_memory->max_page_count;
  5172. }
  5173. else {
  5174. WASMMemory *memory =
  5175. module->memories
  5176. + (export->index - module->import_memory_count);
  5177. *out_min_page = memory->init_page_count;
  5178. *out_max_page = memory->max_page_count;
  5179. }
  5180. return true;
  5181. }
  5182. #endif
  5183. #if WASM_ENABLE_AOT != 0
  5184. if (module_comm->module_type == Wasm_Module_AoT) {
  5185. AOTModule *module = (AOTModule *)module_comm;
  5186. if (export->index < module->import_memory_count) {
  5187. AOTImportMemory *import_memory =
  5188. module->import_memories + export->index;
  5189. *out_min_page = import_memory->mem_init_page_count;
  5190. *out_max_page = import_memory->mem_max_page_count;
  5191. }
  5192. else {
  5193. AOTMemory *memory = module->memories
  5194. + (export->index - module->import_memory_count);
  5195. *out_min_page = memory->mem_init_page_count;
  5196. *out_max_page = memory->mem_max_page_count;
  5197. }
  5198. return true;
  5199. }
  5200. #endif
  5201. return false;
  5202. }
  5203. bool
  5204. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  5205. const WASMExport *export,
  5206. uint8 *out_elem_type,
  5207. #if WASM_ENABLE_GC != 0
  5208. WASMRefType **out_ref_type,
  5209. #endif
  5210. uint32 *out_min_size, uint32 *out_max_size)
  5211. {
  5212. return wasm_runtime_get_table_elem_type(module_comm, export->index,
  5213. out_elem_type,
  5214. #if WASM_ENABLE_GC != 0
  5215. out_ref_type,
  5216. #endif
  5217. out_min_size, out_max_size);
  5218. }
  5219. static inline bool
  5220. argv_to_params(wasm_val_t *out_params, const uint32 *argv,
  5221. WASMFuncType *func_type)
  5222. {
  5223. wasm_val_t *param = out_params;
  5224. uint32 i = 0, *u32;
  5225. for (i = 0; i < func_type->param_count; i++, param++) {
  5226. switch (func_type->types[i]) {
  5227. case VALUE_TYPE_I32:
  5228. param->kind = WASM_I32;
  5229. param->of.i32 = *argv++;
  5230. break;
  5231. case VALUE_TYPE_I64:
  5232. param->kind = WASM_I64;
  5233. u32 = (uint32 *)&param->of.i64;
  5234. u32[0] = *argv++;
  5235. u32[1] = *argv++;
  5236. break;
  5237. case VALUE_TYPE_F32:
  5238. param->kind = WASM_F32;
  5239. param->of.f32 = *(float32 *)argv++;
  5240. break;
  5241. case VALUE_TYPE_F64:
  5242. param->kind = WASM_F64;
  5243. u32 = (uint32 *)&param->of.i64;
  5244. u32[0] = *argv++;
  5245. u32[1] = *argv++;
  5246. break;
  5247. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5248. case VALUE_TYPE_EXTERNREF:
  5249. param->kind = WASM_ANYREF;
  5250. if (!wasm_externref_ref2obj(*argv,
  5251. (void **)&param->of.foreign)) {
  5252. return false;
  5253. }
  5254. argv++;
  5255. break;
  5256. #endif
  5257. default:
  5258. return false;
  5259. }
  5260. }
  5261. return true;
  5262. }
  5263. static inline bool
  5264. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  5265. const wasm_val_t *results, WASMFuncType *func_type)
  5266. {
  5267. const wasm_val_t *result = results;
  5268. uint32 *argv = out_argv, *u32, i;
  5269. uint8 *result_types = func_type->types + func_type->param_count;
  5270. for (i = 0; i < func_type->result_count; i++, result++) {
  5271. switch (result_types[i]) {
  5272. case VALUE_TYPE_I32:
  5273. case VALUE_TYPE_F32:
  5274. *(int32 *)argv++ = result->of.i32;
  5275. break;
  5276. case VALUE_TYPE_I64:
  5277. case VALUE_TYPE_F64:
  5278. u32 = (uint32 *)&result->of.i64;
  5279. *argv++ = u32[0];
  5280. *argv++ = u32[1];
  5281. break;
  5282. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5283. case VALUE_TYPE_EXTERNREF:
  5284. if (!wasm_externref_obj2ref(module_inst,
  5285. (void *)result->of.foreign,
  5286. (uint32 *)argv)) {
  5287. return false;
  5288. }
  5289. argv++;
  5290. break;
  5291. #endif
  5292. default:
  5293. return false;
  5294. }
  5295. }
  5296. return true;
  5297. }
  5298. bool
  5299. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  5300. void *func_ptr, WASMFuncType *func_type,
  5301. uint32 argc, uint32 *argv, bool with_env,
  5302. void *wasm_c_api_env)
  5303. {
  5304. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  5305. wasm_val_t *params = params_buf, *results = results_buf;
  5306. wasm_trap_t *trap = NULL;
  5307. bool ret = false;
  5308. wasm_val_vec_t params_vec, results_vec;
  5309. if (func_type->param_count > 16) {
  5310. if (!(params =
  5311. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  5312. module_inst, NULL, 0))) {
  5313. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  5314. return false;
  5315. }
  5316. }
  5317. if (!argv_to_params(params, argv, func_type)) {
  5318. wasm_runtime_set_exception(module_inst, "unsupported param type");
  5319. goto fail;
  5320. }
  5321. if (func_type->result_count > 4) {
  5322. if (!(results =
  5323. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  5324. module_inst, NULL, 0))) {
  5325. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  5326. goto fail;
  5327. }
  5328. }
  5329. params_vec.data = params;
  5330. params_vec.num_elems = func_type->param_count;
  5331. params_vec.size = func_type->param_count;
  5332. params_vec.size_of_elem = sizeof(wasm_val_t);
  5333. results_vec.data = results;
  5334. results_vec.num_elems = 0;
  5335. results_vec.size = func_type->result_count;
  5336. results_vec.size_of_elem = sizeof(wasm_val_t);
  5337. if (!with_env) {
  5338. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  5339. trap = callback(&params_vec, &results_vec);
  5340. }
  5341. else {
  5342. wasm_func_callback_with_env_t callback =
  5343. (wasm_func_callback_with_env_t)func_ptr;
  5344. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  5345. }
  5346. if (trap) {
  5347. if (trap->message->data) {
  5348. /* since trap->message->data does not end with '\0' */
  5349. char trap_message[108] = { 0 };
  5350. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  5351. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  5352. ? (uint32)trap->message->size
  5353. : max_size_to_copy;
  5354. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  5355. trap->message->data, size_to_copy);
  5356. wasm_runtime_set_exception(module_inst, trap_message);
  5357. }
  5358. else {
  5359. wasm_runtime_set_exception(
  5360. module_inst, "native function throw unknown exception");
  5361. }
  5362. wasm_trap_delete(trap);
  5363. goto fail;
  5364. }
  5365. if (!results_to_argv(module_inst, argv, results, func_type)) {
  5366. wasm_runtime_set_exception(module_inst, "unsupported result type");
  5367. goto fail;
  5368. }
  5369. results_vec.num_elems = func_type->result_count;
  5370. ret = true;
  5371. fail:
  5372. if (params != params_buf)
  5373. wasm_runtime_free(params);
  5374. if (results != results_buf)
  5375. wasm_runtime_free(results);
  5376. return ret;
  5377. }
  5378. void
  5379. wasm_runtime_show_app_heap_corrupted_prompt()
  5380. {
  5381. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  5382. "is compiled by wasi-sdk-12.0 or higher version, "
  5383. "please add -Wl,--export=malloc -Wl,--export=free "
  5384. "to export malloc and free functions. If it is "
  5385. "compiled by asc, please add --exportRuntime to "
  5386. "export the runtime helpers.");
  5387. }
  5388. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5389. void
  5390. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list)
  5391. {
  5392. WASMCustomSection *section = section_list, *next;
  5393. while (section) {
  5394. next = section->next;
  5395. wasm_runtime_free(section);
  5396. section = next;
  5397. }
  5398. }
  5399. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  5400. void
  5401. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
  5402. {
  5403. *major = WAMR_VERSION_MAJOR;
  5404. *minor = WAMR_VERSION_MINOR;
  5405. *patch = WAMR_VERSION_PATCH;
  5406. }
  5407. bool
  5408. wasm_runtime_is_import_func_linked(const char *module_name,
  5409. const char *func_name)
  5410. {
  5411. return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL,
  5412. NULL);
  5413. }
  5414. bool
  5415. wasm_runtime_is_import_global_linked(const char *module_name,
  5416. const char *global_name)
  5417. {
  5418. #if WASM_ENABLE_LIBC_BUILTIN != 0
  5419. WASMGlobalImport global = { 0 };
  5420. return wasm_native_lookup_libc_builtin_global(module_name, global_name,
  5421. &global);
  5422. #else
  5423. return false;
  5424. #endif
  5425. }
  5426. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  5427. WASMExport *
  5428. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  5429. const char *field_name, uint8 export_kind, char *error_buf,
  5430. uint32 error_buf_size)
  5431. {
  5432. WASMExport *exports = NULL, *result = NULL, *export;
  5433. uint32 export_count = 0, i;
  5434. #if WASM_ENABLE_AOT != 0
  5435. if (module->module_type == Wasm_Module_AoT) {
  5436. AOTModule *aot_module = (AOTModule *)module;
  5437. exports = (WASMExport *)aot_module->exports;
  5438. export_count = aot_module->export_count;
  5439. }
  5440. #endif
  5441. #if WASM_ENABLE_INTERP != 0
  5442. if (module->module_type == Wasm_Module_Bytecode) {
  5443. WASMModule *wasm_module = (WASMModule *)module;
  5444. exports = wasm_module->exports;
  5445. export_count = wasm_module->export_count;
  5446. }
  5447. #endif
  5448. for (i = 0, export = exports; i < export_count; ++i, ++export) {
  5449. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  5450. result = export;
  5451. goto exit;
  5452. }
  5453. }
  5454. if (i == export_count) {
  5455. LOG_DEBUG("can not find an export %d named %s in the module %s",
  5456. export_kind, field_name, module_name);
  5457. set_error_buf(error_buf, error_buf_size,
  5458. "unknown import or incompatible import type");
  5459. }
  5460. exit:
  5461. return result;
  5462. }
  5463. #endif
  5464. #if WASM_ENABLE_MULTI_MODULE != 0
  5465. WASMModuleCommon *
  5466. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  5467. const char *sub_module_name)
  5468. {
  5469. WASMRegisteredModule *node = NULL;
  5470. #if WASM_ENABLE_AOT != 0
  5471. if (parent_module->module_type == Wasm_Module_AoT) {
  5472. node = bh_list_first_elem(
  5473. ((AOTModule *)parent_module)->import_module_list);
  5474. }
  5475. #endif
  5476. #if WASM_ENABLE_INTERP != 0
  5477. if (parent_module->module_type == Wasm_Module_Bytecode) {
  5478. node = bh_list_first_elem(
  5479. ((WASMModule *)parent_module)->import_module_list);
  5480. }
  5481. #endif
  5482. while (node && strcmp(sub_module_name, node->module_name)) {
  5483. node = bh_list_elem_next(node);
  5484. }
  5485. return node ? node->module : NULL;
  5486. }
  5487. bool
  5488. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  5489. const char *sub_module_name,
  5490. WASMModuleCommon *sub_module)
  5491. {
  5492. /* register sub_module into its parent sub module list */
  5493. WASMRegisteredModule *node = NULL;
  5494. bh_list_status ret = BH_LIST_ERROR;
  5495. if (wasm_runtime_search_sub_module(parent_module, sub_module_name)) {
  5496. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  5497. return true;
  5498. }
  5499. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  5500. if (!node) {
  5501. return false;
  5502. }
  5503. node->module_name = sub_module_name;
  5504. node->module = sub_module;
  5505. #if WASM_ENABLE_AOT != 0
  5506. if (parent_module->module_type == Wasm_Module_AoT) {
  5507. ret = bh_list_insert(((AOTModule *)parent_module)->import_module_list,
  5508. node);
  5509. }
  5510. #endif
  5511. #if WASM_ENABLE_INTERP != 0
  5512. if (parent_module->module_type == Wasm_Module_Bytecode) {
  5513. ret = bh_list_insert(((WASMModule *)parent_module)->import_module_list,
  5514. node);
  5515. }
  5516. #endif
  5517. bh_assert(BH_LIST_SUCCESS == ret);
  5518. (void)ret;
  5519. return true;
  5520. }
  5521. WASMModuleCommon *
  5522. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  5523. const char *sub_module_name, char *error_buf,
  5524. uint32 error_buf_size)
  5525. {
  5526. WASMModuleCommon *sub_module = NULL;
  5527. bool ret = false;
  5528. uint8 *buffer = NULL;
  5529. uint32 buffer_size = 0;
  5530. /* check the registered module list of the parent */
  5531. sub_module = wasm_runtime_search_sub_module(parent_module, sub_module_name);
  5532. if (sub_module) {
  5533. LOG_DEBUG("%s has been loaded before", sub_module_name);
  5534. return sub_module;
  5535. }
  5536. /* check the global registered module list */
  5537. sub_module = wasm_runtime_find_module_registered(sub_module_name);
  5538. if (sub_module) {
  5539. LOG_DEBUG("%s has been loaded", sub_module_name);
  5540. goto wasm_runtime_register_sub_module;
  5541. }
  5542. LOG_VERBOSE("loading %s", sub_module_name);
  5543. if (!reader) {
  5544. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5545. "no sub module reader to load %s", sub_module_name);
  5546. return NULL;
  5547. }
  5548. /* start to maintain a loading module list */
  5549. ret = wasm_runtime_is_loading_module(sub_module_name);
  5550. if (ret) {
  5551. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5552. "found circular dependency on %s", sub_module_name);
  5553. return NULL;
  5554. }
  5555. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  5556. error_buf_size);
  5557. if (!ret) {
  5558. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  5559. return NULL;
  5560. }
  5561. ret = reader(parent_module->module_type, sub_module_name, &buffer,
  5562. &buffer_size);
  5563. if (!ret) {
  5564. LOG_DEBUG("read the file of %s failed", sub_module_name);
  5565. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5566. "unknown import", sub_module_name);
  5567. goto delete_loading_module;
  5568. }
  5569. if (get_package_type(buffer, buffer_size) != parent_module->module_type) {
  5570. LOG_DEBUG("moudle %s type error", sub_module_name);
  5571. goto delete_loading_module;
  5572. }
  5573. if (get_package_type(buffer, buffer_size) == Wasm_Module_Bytecode) {
  5574. #if WASM_ENABLE_INTERP != 0
  5575. sub_module = (WASMModuleCommon *)wasm_load(buffer, buffer_size, false,
  5576. error_buf, error_buf_size);
  5577. #endif
  5578. }
  5579. else if (get_package_type(buffer, buffer_size) == Wasm_Module_AoT) {
  5580. #if WASM_ENABLE_AOT != 0
  5581. sub_module = (WASMModuleCommon *)aot_load_from_aot_file(
  5582. buffer, buffer_size, error_buf, error_buf_size);
  5583. #endif
  5584. }
  5585. if (!sub_module) {
  5586. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  5587. /* others will be destroyed in runtime_destroy() */
  5588. goto destroy_file_buffer;
  5589. }
  5590. wasm_runtime_delete_loading_module(sub_module_name);
  5591. /* register on a global list */
  5592. ret = wasm_runtime_register_module_internal(
  5593. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  5594. error_buf, error_buf_size);
  5595. if (!ret) {
  5596. LOG_DEBUG("error: can not register module %s globally\n",
  5597. sub_module_name);
  5598. /* others will be unloaded in runtime_destroy() */
  5599. goto unload_module;
  5600. }
  5601. /* register into its parent list */
  5602. wasm_runtime_register_sub_module:
  5603. ret = wasm_runtime_register_sub_module(parent_module, sub_module_name,
  5604. sub_module);
  5605. if (!ret) {
  5606. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5607. "failed to register sub module %s", sub_module_name);
  5608. /* since it is in the global module list, no need to
  5609. * unload the module. the runtime_destroy() will do it
  5610. */
  5611. return NULL;
  5612. }
  5613. return sub_module;
  5614. unload_module:
  5615. wasm_runtime_unload(sub_module);
  5616. destroy_file_buffer:
  5617. if (destroyer) {
  5618. destroyer(buffer, buffer_size);
  5619. }
  5620. else {
  5621. LOG_WARNING("need to release the reading buffer of %s manually",
  5622. sub_module_name);
  5623. }
  5624. delete_loading_module:
  5625. wasm_runtime_delete_loading_module(sub_module_name);
  5626. return NULL;
  5627. }
  5628. bool
  5629. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  5630. WASMModuleInstanceCommon *module_inst,
  5631. uint32 stack_size, uint32 heap_size,
  5632. char *error_buf, uint32 error_buf_size)
  5633. {
  5634. bh_list *sub_module_inst_list = NULL;
  5635. WASMRegisteredModule *sub_module_list_node = NULL;
  5636. #if WASM_ENABLE_AOT != 0
  5637. if (module->module_type == Wasm_Module_AoT) {
  5638. sub_module_inst_list =
  5639. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  5640. ->sub_module_inst_list;
  5641. sub_module_list_node =
  5642. bh_list_first_elem(((AOTModule *)module)->import_module_list);
  5643. }
  5644. #endif
  5645. #if WASM_ENABLE_INTERP != 0
  5646. if (module->module_type == Wasm_Module_Bytecode) {
  5647. sub_module_inst_list =
  5648. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  5649. ->sub_module_inst_list;
  5650. sub_module_list_node =
  5651. bh_list_first_elem(((WASMModule *)module)->import_module_list);
  5652. }
  5653. #endif
  5654. while (sub_module_list_node) {
  5655. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  5656. WASMModuleCommon *sub_module = sub_module_list_node->module;
  5657. WASMModuleInstanceCommon *sub_module_inst = NULL;
  5658. sub_module_inst = wasm_runtime_instantiate_internal(
  5659. sub_module, NULL, NULL, stack_size, heap_size, error_buf,
  5660. error_buf_size);
  5661. if (!sub_module_inst) {
  5662. LOG_DEBUG("instantiate %s failed",
  5663. sub_module_list_node->module_name);
  5664. return false;
  5665. }
  5666. sub_module_inst_list_node = loader_malloc(sizeof(WASMSubModInstNode),
  5667. error_buf, error_buf_size);
  5668. if (!sub_module_inst_list_node) {
  5669. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  5670. sizeof(WASMSubModInstNode));
  5671. if (sub_module_inst)
  5672. wasm_runtime_deinstantiate_internal(sub_module_inst, false);
  5673. return false;
  5674. }
  5675. sub_module_inst_list_node->module_inst =
  5676. (WASMModuleInstance *)sub_module_inst;
  5677. sub_module_inst_list_node->module_name =
  5678. sub_module_list_node->module_name;
  5679. bh_list_status ret =
  5680. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  5681. bh_assert(BH_LIST_SUCCESS == ret);
  5682. (void)ret;
  5683. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  5684. }
  5685. return true;
  5686. }
  5687. void
  5688. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst)
  5689. {
  5690. bh_list *list = NULL;
  5691. #if WASM_ENABLE_AOT != 0
  5692. if (module_inst->module_type == Wasm_Module_AoT) {
  5693. list = ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  5694. ->sub_module_inst_list;
  5695. }
  5696. #endif
  5697. #if WASM_ENABLE_INTERP != 0
  5698. if (module_inst->module_type == Wasm_Module_Bytecode) {
  5699. list =
  5700. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  5701. ->sub_module_inst_list;
  5702. }
  5703. #endif
  5704. WASMSubModInstNode *node = bh_list_first_elem(list);
  5705. while (node) {
  5706. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  5707. bh_list_remove(list, node);
  5708. wasm_runtime_deinstantiate_internal(
  5709. (WASMModuleInstanceCommon *)node->module_inst, false);
  5710. wasm_runtime_free(node);
  5711. node = next_node;
  5712. }
  5713. }
  5714. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  5715. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  5716. void *
  5717. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  5718. void *ctx))
  5719. {
  5720. return wasm_native_create_context_key(dtor);
  5721. }
  5722. void
  5723. wasm_runtime_destroy_context_key(void *key)
  5724. {
  5725. wasm_native_destroy_context_key(key);
  5726. }
  5727. void
  5728. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
  5729. {
  5730. wasm_native_set_context(inst, key, ctx);
  5731. }
  5732. void
  5733. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  5734. void *ctx)
  5735. {
  5736. wasm_native_set_context_spread(inst, key, ctx);
  5737. }
  5738. void *
  5739. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
  5740. {
  5741. return wasm_native_get_context(inst, key);
  5742. }
  5743. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  5744. #if WASM_ENABLE_LINUX_PERF != 0
  5745. static bool enable_linux_perf = false;
  5746. bool
  5747. wasm_runtime_get_linux_perf(void)
  5748. {
  5749. return enable_linux_perf;
  5750. }
  5751. void
  5752. wasm_runtime_set_linux_perf(bool flag)
  5753. {
  5754. enable_linux_perf = flag;
  5755. }
  5756. #endif