wasm_runtime_common.c 169 KB

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