wasm_runtime_common.c 121 KB

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