wasm_runtime_common.c 173 KB

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