wasm_runtime_common.c 190 KB

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