wasm_runtime_common.c 141 KB

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