wasm_runtime_common.c 149 KB

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