wasm_runtime_common.c 225 KB

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