wasm_runtime_common.c 255 KB

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