wasm_runtime_common.c 249 KB

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