wasm_runtime_common.c 247 KB

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