wasm_runtime_common.c 130 KB

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