wasm_runtime_common.c 143 KB

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