wasm_runtime_common.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958
  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 (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. || !strcmp("spectest", module_name)
  468. );
  469. }
  470. #if WASM_ENABLE_THREAD_MGR != 0
  471. bool
  472. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  473. uint32 start_offset, uint32 size)
  474. {
  475. WASMModuleInstanceCommon *module_inst
  476. = wasm_exec_env_get_module_inst(exec_env);
  477. #if WASM_ENABLE_INTERP != 0
  478. if (module_inst->module_type == Wasm_Module_Bytecode) {
  479. return wasm_set_aux_stack(exec_env, start_offset, size);
  480. }
  481. #endif
  482. #if WASM_ENABLE_AOT != 0
  483. if (module_inst->module_type == Wasm_Module_AoT) {
  484. return aot_set_aux_stack(exec_env, start_offset, size);
  485. }
  486. #endif
  487. return false;
  488. }
  489. bool
  490. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  491. uint32 *start_offset, uint32 *size)
  492. {
  493. WASMModuleInstanceCommon *module_inst
  494. = wasm_exec_env_get_module_inst(exec_env);
  495. #if WASM_ENABLE_INTERP != 0
  496. if (module_inst->module_type == Wasm_Module_Bytecode) {
  497. return wasm_get_aux_stack(exec_env, start_offset, size);
  498. }
  499. #endif
  500. #if WASM_ENABLE_AOT != 0
  501. if (module_inst->module_type == Wasm_Module_AoT) {
  502. return aot_get_aux_stack(exec_env, start_offset, size);
  503. }
  504. #endif
  505. return false;
  506. }
  507. void
  508. wasm_runtime_set_max_thread_num(uint32 num)
  509. {
  510. wasm_cluster_set_max_thread_num(num);
  511. }
  512. #endif /* end of WASM_ENABLE_THREAD_MGR */
  513. static WASMModuleCommon *
  514. register_module_with_null_name(WASMModuleCommon *module_common,
  515. char *error_buf, uint32 error_buf_size)
  516. {
  517. #if WASM_ENABLE_MULTI_MODULE != 0
  518. if (module_common) {
  519. if (!wasm_runtime_register_module_internal(NULL, module_common,
  520. NULL, 0,
  521. error_buf,
  522. error_buf_size)) {
  523. wasm_runtime_unload(module_common);
  524. return NULL;
  525. }
  526. return module_common;
  527. }
  528. else
  529. return NULL;
  530. #else
  531. return module_common;
  532. #endif
  533. }
  534. WASMModuleCommon *
  535. wasm_runtime_load(const uint8 *buf, uint32 size,
  536. char *error_buf, uint32 error_buf_size)
  537. {
  538. WASMModuleCommon *module_common = NULL;
  539. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  540. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  541. AOTModule *aot_module;
  542. WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
  543. if (!module)
  544. return NULL;
  545. if (!(aot_module = aot_convert_wasm_module(module,
  546. error_buf, error_buf_size))) {
  547. wasm_unload(module);
  548. return NULL;
  549. }
  550. module_common = (WASMModuleCommon*)aot_module;
  551. return register_module_with_null_name(module_common,
  552. error_buf, error_buf_size);
  553. #elif WASM_ENABLE_INTERP != 0
  554. module_common = (WASMModuleCommon*)
  555. wasm_load(buf, size, error_buf, error_buf_size);
  556. return register_module_with_null_name(module_common,
  557. error_buf, error_buf_size);
  558. #endif
  559. }
  560. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  561. #if WASM_ENABLE_AOT != 0
  562. module_common = (WASMModuleCommon*)
  563. aot_load_from_aot_file(buf, size, error_buf, error_buf_size);
  564. return register_module_with_null_name(module_common,
  565. error_buf, error_buf_size);
  566. #endif
  567. }
  568. if (size < 4)
  569. set_error_buf(error_buf, error_buf_size,
  570. "WASM module load failed: unexpected end");
  571. else
  572. set_error_buf(error_buf, error_buf_size,
  573. "WASM module load failed: magic header not detected");
  574. return NULL;
  575. }
  576. WASMModuleCommon *
  577. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  578. char *error_buf, uint32_t error_buf_size)
  579. {
  580. WASMModuleCommon *module_common;
  581. #if WASM_ENABLE_INTERP != 0
  582. if (!is_aot) {
  583. module_common = (WASMModuleCommon*)
  584. wasm_load_from_sections(section_list,
  585. error_buf, error_buf_size);
  586. return register_module_with_null_name(module_common,
  587. error_buf, error_buf_size);
  588. }
  589. #endif
  590. #if WASM_ENABLE_AOT != 0
  591. if (is_aot) {
  592. module_common = (WASMModuleCommon*)
  593. aot_load_from_sections(section_list,
  594. error_buf, error_buf_size);
  595. return register_module_with_null_name(module_common,
  596. error_buf, error_buf_size);
  597. }
  598. #endif
  599. set_error_buf(error_buf, error_buf_size,
  600. "WASM module load failed: invalid section list type");
  601. return NULL;
  602. }
  603. void
  604. wasm_runtime_unload(WASMModuleCommon *module)
  605. {
  606. #if WASM_ENABLE_MULTI_MODULE != 0
  607. /**
  608. * since we will unload and free all module when runtime_destroy()
  609. * we don't want users to unwillingly disrupt it
  610. */
  611. return;
  612. #endif
  613. #if WASM_ENABLE_INTERP != 0
  614. if (module->module_type == Wasm_Module_Bytecode) {
  615. wasm_unload((WASMModule*)module);
  616. return;
  617. }
  618. #endif
  619. #if WASM_ENABLE_AOT != 0
  620. if (module->module_type == Wasm_Module_AoT) {
  621. aot_unload((AOTModule*)module);
  622. return;
  623. }
  624. #endif
  625. }
  626. WASMModuleInstanceCommon *
  627. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  628. uint32 stack_size, uint32 heap_size,
  629. char *error_buf, uint32 error_buf_size)
  630. {
  631. #if WASM_ENABLE_INTERP != 0
  632. if (module->module_type == Wasm_Module_Bytecode)
  633. return (WASMModuleInstanceCommon*)
  634. wasm_instantiate((WASMModule*)module, is_sub_inst,
  635. stack_size, heap_size,
  636. error_buf, error_buf_size);
  637. #endif
  638. #if WASM_ENABLE_AOT != 0
  639. if (module->module_type == Wasm_Module_AoT)
  640. return (WASMModuleInstanceCommon*)
  641. aot_instantiate((AOTModule*)module, is_sub_inst,
  642. stack_size, heap_size,
  643. error_buf, error_buf_size);
  644. #endif
  645. set_error_buf(error_buf, error_buf_size,
  646. "Instantiate module failed, invalid module type");
  647. return NULL;
  648. }
  649. WASMModuleInstanceCommon *
  650. wasm_runtime_instantiate(WASMModuleCommon *module,
  651. uint32 stack_size, uint32 heap_size,
  652. char *error_buf, uint32 error_buf_size)
  653. {
  654. return wasm_runtime_instantiate_internal(module, false,
  655. stack_size, heap_size,
  656. error_buf, error_buf_size);
  657. }
  658. void
  659. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  660. bool is_sub_inst)
  661. {
  662. #if WASM_ENABLE_INTERP != 0
  663. if (module_inst->module_type == Wasm_Module_Bytecode) {
  664. wasm_deinstantiate((WASMModuleInstance*)module_inst, is_sub_inst);
  665. return;
  666. }
  667. #endif
  668. #if WASM_ENABLE_AOT != 0
  669. if (module_inst->module_type == Wasm_Module_AoT) {
  670. aot_deinstantiate((AOTModuleInstance*)module_inst, is_sub_inst);
  671. return;
  672. }
  673. #endif
  674. }
  675. void
  676. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  677. {
  678. return wasm_runtime_deinstantiate_internal(module_inst, false);
  679. }
  680. WASMExecEnv *
  681. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  682. uint32 stack_size)
  683. {
  684. return wasm_exec_env_create(module_inst, stack_size);
  685. }
  686. void
  687. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  688. {
  689. wasm_exec_env_destroy(exec_env);
  690. }
  691. WASMModuleInstanceCommon *
  692. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  693. {
  694. return wasm_exec_env_get_module_inst(exec_env);
  695. }
  696. void *
  697. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  698. {
  699. return exec_env->attachment;
  700. }
  701. void
  702. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  703. {
  704. exec_env->user_data = user_data;
  705. }
  706. void *
  707. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  708. {
  709. return exec_env->user_data;
  710. }
  711. WASMFunctionInstanceCommon *
  712. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  713. const char *name,
  714. const char *signature)
  715. {
  716. #if WASM_ENABLE_INTERP != 0
  717. if (module_inst->module_type == Wasm_Module_Bytecode)
  718. return (WASMFunctionInstanceCommon*)
  719. wasm_lookup_function((const WASMModuleInstance*)module_inst,
  720. name, signature);
  721. #endif
  722. #if WASM_ENABLE_AOT != 0
  723. if (module_inst->module_type == Wasm_Module_AoT)
  724. return (WASMFunctionInstanceCommon*)
  725. aot_lookup_function((const AOTModuleInstance*)module_inst,
  726. name, signature);
  727. #endif
  728. return NULL;
  729. }
  730. bool
  731. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  732. WASMFunctionInstanceCommon *function,
  733. uint32 argc, uint32 argv[])
  734. {
  735. if (!wasm_runtime_exec_env_check(exec_env)) {
  736. LOG_ERROR("Invalid exec env stack info.");
  737. return false;
  738. }
  739. /* set thread handle and stack boundary */
  740. wasm_exec_env_set_thread_info(exec_env);
  741. #if WASM_ENABLE_INTERP != 0
  742. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  743. return wasm_call_function(exec_env,
  744. (WASMFunctionInstance*)function,
  745. argc, argv);
  746. #endif
  747. #if WASM_ENABLE_AOT != 0
  748. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  749. return aot_call_function(exec_env,
  750. (AOTFunctionInstance*)function,
  751. argc, argv);
  752. #endif
  753. return false;
  754. }
  755. bool
  756. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  757. WASMFunctionInstanceCommon *function,
  758. uint32 argc, uint32 argv[])
  759. {
  760. #if WASM_ENABLE_INTERP != 0
  761. if (module_inst->module_type == Wasm_Module_Bytecode)
  762. return wasm_create_exec_env_and_call_function(
  763. (WASMModuleInstance*)module_inst,
  764. (WASMFunctionInstance*)function,
  765. argc, argv);
  766. #endif
  767. #if WASM_ENABLE_AOT != 0
  768. if (module_inst->module_type == Wasm_Module_AoT)
  769. return aot_create_exec_env_and_call_function(
  770. (AOTModuleInstance*)module_inst,
  771. (AOTFunctionInstance*)function,
  772. argc, argv);
  773. #endif
  774. return false;
  775. }
  776. void
  777. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  778. const char *exception)
  779. {
  780. #if WASM_ENABLE_INTERP != 0
  781. if (module_inst->module_type == Wasm_Module_Bytecode) {
  782. wasm_set_exception((WASMModuleInstance*)module_inst, exception);
  783. return;
  784. }
  785. #endif
  786. #if WASM_ENABLE_AOT != 0
  787. if (module_inst->module_type == Wasm_Module_AoT) {
  788. aot_set_exception((AOTModuleInstance*)module_inst, exception);
  789. return;
  790. }
  791. #endif
  792. }
  793. const char*
  794. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  795. {
  796. #if WASM_ENABLE_INTERP != 0
  797. if (module_inst->module_type == Wasm_Module_Bytecode) {
  798. return wasm_get_exception((WASMModuleInstance*)module_inst);
  799. }
  800. #endif
  801. #if WASM_ENABLE_AOT != 0
  802. if (module_inst->module_type == Wasm_Module_AoT) {
  803. return aot_get_exception((AOTModuleInstance*)module_inst);
  804. }
  805. #endif
  806. return NULL;
  807. }
  808. void
  809. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  810. {
  811. wasm_runtime_set_exception(module_inst, NULL);
  812. }
  813. void
  814. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  815. void *custom_data)
  816. {
  817. #if WASM_ENABLE_INTERP != 0
  818. if (module_inst->module_type == Wasm_Module_Bytecode) {
  819. ((WASMModuleInstance*)module_inst)->custom_data = custom_data;
  820. return;
  821. }
  822. #endif
  823. #if WASM_ENABLE_AOT != 0
  824. if (module_inst->module_type == Wasm_Module_AoT) {
  825. ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data;
  826. return;
  827. }
  828. #endif
  829. }
  830. void*
  831. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  832. {
  833. #if WASM_ENABLE_INTERP != 0
  834. if (module_inst->module_type == Wasm_Module_Bytecode)
  835. return ((WASMModuleInstance*)module_inst)->custom_data;
  836. #endif
  837. #if WASM_ENABLE_AOT != 0
  838. if (module_inst->module_type == Wasm_Module_AoT)
  839. return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
  840. #endif
  841. return NULL;
  842. }
  843. uint32
  844. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  845. void **p_native_addr)
  846. {
  847. #if WASM_ENABLE_INTERP != 0
  848. if (module_inst->module_type == Wasm_Module_Bytecode)
  849. return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
  850. p_native_addr);
  851. #endif
  852. #if WASM_ENABLE_AOT != 0
  853. if (module_inst->module_type == Wasm_Module_AoT)
  854. return aot_module_malloc((AOTModuleInstance*)module_inst, size,
  855. p_native_addr);
  856. #endif
  857. return 0;
  858. }
  859. void
  860. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  861. {
  862. #if WASM_ENABLE_INTERP != 0
  863. if (module_inst->module_type == Wasm_Module_Bytecode) {
  864. wasm_module_free((WASMModuleInstance*)module_inst, ptr);
  865. return;
  866. }
  867. #endif
  868. #if WASM_ENABLE_AOT != 0
  869. if (module_inst->module_type == Wasm_Module_AoT) {
  870. aot_module_free((AOTModuleInstance*)module_inst, ptr);
  871. return;
  872. }
  873. #endif
  874. }
  875. uint32
  876. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  877. const char *src, uint32 size)
  878. {
  879. #if WASM_ENABLE_INTERP != 0
  880. if (module_inst->module_type == Wasm_Module_Bytecode) {
  881. return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size);
  882. }
  883. #endif
  884. #if WASM_ENABLE_AOT != 0
  885. if (module_inst->module_type == Wasm_Module_AoT) {
  886. return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size);
  887. }
  888. #endif
  889. return 0;
  890. }
  891. bool
  892. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  893. uint32 app_offset, uint32 size)
  894. {
  895. #if WASM_ENABLE_INTERP != 0
  896. if (module_inst->module_type == Wasm_Module_Bytecode)
  897. return wasm_validate_app_addr((WASMModuleInstance*)module_inst,
  898. app_offset, size);
  899. #endif
  900. #if WASM_ENABLE_AOT != 0
  901. if (module_inst->module_type == Wasm_Module_AoT)
  902. return aot_validate_app_addr((AOTModuleInstance*)module_inst,
  903. app_offset, size);
  904. #endif
  905. return false;
  906. }
  907. bool
  908. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  909. uint32 app_str_offset)
  910. {
  911. uint32 app_end_offset;
  912. char *str, *str_end;
  913. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
  914. NULL, &app_end_offset))
  915. goto fail;
  916. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  917. str_end = str + (app_end_offset - app_str_offset);
  918. while (str < str_end && *str != '\0')
  919. str++;
  920. if (str == str_end)
  921. goto fail;
  922. return true;
  923. fail:
  924. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  925. return false;
  926. }
  927. bool
  928. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  929. void *native_ptr, uint32 size)
  930. {
  931. #if WASM_ENABLE_INTERP != 0
  932. if (module_inst->module_type == Wasm_Module_Bytecode)
  933. return wasm_validate_native_addr((WASMModuleInstance*)module_inst,
  934. native_ptr, size);
  935. #endif
  936. #if WASM_ENABLE_AOT != 0
  937. if (module_inst->module_type == Wasm_Module_AoT)
  938. return aot_validate_native_addr((AOTModuleInstance*)module_inst,
  939. native_ptr, size);
  940. #endif
  941. return false;
  942. }
  943. void *
  944. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  945. uint32 app_offset)
  946. {
  947. #if WASM_ENABLE_INTERP != 0
  948. if (module_inst->module_type == Wasm_Module_Bytecode)
  949. return wasm_addr_app_to_native((WASMModuleInstance*)module_inst,
  950. app_offset);
  951. #endif
  952. #if WASM_ENABLE_AOT != 0
  953. if (module_inst->module_type == Wasm_Module_AoT)
  954. return aot_addr_app_to_native((AOTModuleInstance*)module_inst,
  955. app_offset);
  956. #endif
  957. return NULL;
  958. }
  959. uint32
  960. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  961. void *native_ptr)
  962. {
  963. #if WASM_ENABLE_INTERP != 0
  964. if (module_inst->module_type == Wasm_Module_Bytecode)
  965. return wasm_addr_native_to_app((WASMModuleInstance*)module_inst,
  966. native_ptr);
  967. #endif
  968. #if WASM_ENABLE_AOT != 0
  969. if (module_inst->module_type == Wasm_Module_AoT)
  970. return aot_addr_native_to_app((AOTModuleInstance*)module_inst,
  971. native_ptr);
  972. #endif
  973. return 0;
  974. }
  975. bool
  976. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  977. uint32 app_offset,
  978. uint32 *p_app_start_offset,
  979. uint32 *p_app_end_offset)
  980. {
  981. #if WASM_ENABLE_INTERP != 0
  982. if (module_inst->module_type == Wasm_Module_Bytecode)
  983. return wasm_get_app_addr_range((WASMModuleInstance*)module_inst,
  984. app_offset, p_app_start_offset,
  985. p_app_end_offset);
  986. #endif
  987. #if WASM_ENABLE_AOT != 0
  988. if (module_inst->module_type == Wasm_Module_AoT)
  989. return aot_get_app_addr_range((AOTModuleInstance*)module_inst,
  990. app_offset, p_app_start_offset,
  991. p_app_end_offset);
  992. #endif
  993. return false;
  994. }
  995. bool
  996. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  997. uint8_t *native_ptr,
  998. uint8_t **p_native_start_addr,
  999. uint8_t **p_native_end_addr)
  1000. {
  1001. #if WASM_ENABLE_INTERP != 0
  1002. if (module_inst->module_type == Wasm_Module_Bytecode)
  1003. return wasm_get_native_addr_range((WASMModuleInstance*)module_inst,
  1004. native_ptr, p_native_start_addr,
  1005. p_native_end_addr);
  1006. #endif
  1007. #if WASM_ENABLE_AOT != 0
  1008. if (module_inst->module_type == Wasm_Module_AoT)
  1009. return aot_get_native_addr_range((AOTModuleInstance*)module_inst,
  1010. native_ptr, p_native_start_addr,
  1011. p_native_end_addr);
  1012. #endif
  1013. return false;
  1014. }
  1015. uint32
  1016. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1017. {
  1018. #if WASM_ENABLE_INTERP != 0
  1019. if (module_inst->module_type == Wasm_Module_Bytecode)
  1020. return ((WASMModuleInstance*)module_inst)->temp_ret;
  1021. #endif
  1022. #if WASM_ENABLE_AOT != 0
  1023. if (module_inst->module_type == Wasm_Module_AoT)
  1024. return ((AOTModuleInstance*)module_inst)->temp_ret;
  1025. #endif
  1026. return 0;
  1027. }
  1028. void
  1029. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1030. uint32 temp_ret)
  1031. {
  1032. #if WASM_ENABLE_INTERP != 0
  1033. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1034. ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret;
  1035. return;
  1036. }
  1037. #endif
  1038. #if WASM_ENABLE_AOT != 0
  1039. if (module_inst->module_type == Wasm_Module_AoT) {
  1040. ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret;
  1041. return;
  1042. }
  1043. #endif
  1044. }
  1045. uint32
  1046. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1047. {
  1048. #if WASM_ENABLE_INTERP != 0
  1049. if (module_inst->module_type == Wasm_Module_Bytecode)
  1050. return ((WASMModuleInstance*)module_inst)->llvm_stack;
  1051. #endif
  1052. #if WASM_ENABLE_AOT != 0
  1053. if (module_inst->module_type == Wasm_Module_AoT)
  1054. return ((AOTModuleInstance*)module_inst)->llvm_stack;
  1055. #endif
  1056. return 0;
  1057. }
  1058. void
  1059. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1060. uint32 llvm_stack)
  1061. {
  1062. #if WASM_ENABLE_INTERP != 0
  1063. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1064. ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1065. return;
  1066. }
  1067. #endif
  1068. #if WASM_ENABLE_AOT != 0
  1069. if (module_inst->module_type == Wasm_Module_AoT) {
  1070. ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1071. return;
  1072. }
  1073. #endif
  1074. }
  1075. bool
  1076. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  1077. uint32 inc_page_count)
  1078. {
  1079. #if WASM_ENABLE_INTERP != 0
  1080. if (module->module_type == Wasm_Module_Bytecode)
  1081. return wasm_enlarge_memory((WASMModuleInstance*)module,
  1082. inc_page_count);
  1083. #endif
  1084. #if WASM_ENABLE_AOT != 0
  1085. if (module->module_type == Wasm_Module_AoT)
  1086. return aot_enlarge_memory((AOTModuleInstance*)module,
  1087. inc_page_count);
  1088. #endif
  1089. return false;
  1090. }
  1091. #if WASM_ENABLE_LIBC_WASI != 0
  1092. void
  1093. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  1094. const char *dir_list[], uint32 dir_count,
  1095. const char *map_dir_list[], uint32 map_dir_count,
  1096. const char *env_list[], uint32 env_count,
  1097. char *argv[], int argc)
  1098. {
  1099. WASIArguments *wasi_args = NULL;
  1100. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1101. if (module->module_type == Wasm_Module_Bytecode)
  1102. wasi_args = &((WASMModule*)module)->wasi_args;
  1103. #endif
  1104. #if WASM_ENABLE_AOT != 0
  1105. if (module->module_type == Wasm_Module_AoT)
  1106. wasi_args = &((AOTModule*)module)->wasi_args;
  1107. #endif
  1108. if (wasi_args) {
  1109. wasi_args->dir_list = dir_list;
  1110. wasi_args->dir_count = dir_count;
  1111. wasi_args->map_dir_list = map_dir_list;
  1112. wasi_args->map_dir_count = map_dir_count;
  1113. wasi_args->env = env_list;
  1114. wasi_args->env_count = env_count;
  1115. wasi_args->argv = argv;
  1116. wasi_args->argc = (uint32)argc;
  1117. }
  1118. }
  1119. bool
  1120. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1121. const char *dir_list[], uint32 dir_count,
  1122. const char *map_dir_list[], uint32 map_dir_count,
  1123. const char *env[], uint32 env_count,
  1124. char *argv[], uint32 argc,
  1125. char *error_buf, uint32 error_buf_size)
  1126. {
  1127. WASIContext *wasi_ctx;
  1128. size_t *argv_offsets = NULL;
  1129. char *argv_buf = NULL;
  1130. size_t *env_offsets = NULL;
  1131. char *env_buf = NULL;
  1132. uint64 argv_buf_len = 0, env_buf_len = 0;
  1133. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  1134. struct fd_table *curfds;
  1135. struct fd_prestats *prestats;
  1136. struct argv_environ_values *argv_environ;
  1137. bool fd_table_inited = false, fd_prestats_inited = false;
  1138. bool argv_environ_inited = false;
  1139. uint32 offset_argv_offsets = 0, offset_env_offsets = 0;
  1140. uint32 offset_argv_buf = 0, offset_env_buf = 0;
  1141. uint32 offset_curfds = 0;
  1142. uint32 offset_prestats = 0;
  1143. uint32 offset_argv_environ = 0;
  1144. __wasi_fd_t wasm_fd = 3;
  1145. int32 raw_fd;
  1146. char *path, resolved_path[PATH_MAX];
  1147. uint64 total_size;
  1148. uint32 i;
  1149. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL,
  1150. error_buf, error_buf_size))) {
  1151. return false;
  1152. }
  1153. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  1154. #if WASM_ENABLE_INTERP != 0
  1155. if (module_inst->module_type == Wasm_Module_Bytecode
  1156. && !((WASMModuleInstance*)module_inst)->default_memory)
  1157. return true;
  1158. #endif
  1159. #if WASM_ENABLE_AOT != 0
  1160. if (module_inst->module_type == Wasm_Module_AoT
  1161. && !((AOTModuleInstance*)module_inst)->
  1162. global_table_data.memory_instances[0].memory_data.ptr)
  1163. return true;
  1164. #endif
  1165. /* process argv[0], trip the path and suffix, only keep the program name */
  1166. for (i = 0; i < argc; i++)
  1167. argv_buf_len += strlen(argv[i]) + 1;
  1168. total_size = sizeof(size_t) * (uint64)argc;
  1169. if (total_size >= UINT32_MAX
  1170. || !(offset_argv_offsets = wasm_runtime_module_malloc
  1171. (module_inst, (uint32)total_size,
  1172. (void**)&argv_offsets))
  1173. || argv_buf_len >= UINT32_MAX
  1174. || !(offset_argv_buf = wasm_runtime_module_malloc
  1175. (module_inst, (uint32)argv_buf_len,
  1176. (void**)&argv_buf))) {
  1177. set_error_buf(error_buf, error_buf_size,
  1178. "Init wasi environment failed: allocate memory failed");
  1179. goto fail;
  1180. }
  1181. for (i = 0; i < argc; i++) {
  1182. argv_offsets[i] = argv_buf_offset;
  1183. bh_strcpy_s(argv_buf + argv_buf_offset,
  1184. (uint32)argv_buf_len - argv_buf_offset, argv[i]);
  1185. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  1186. }
  1187. for (i = 0; i < env_count; i++)
  1188. env_buf_len += strlen(env[i]) + 1;
  1189. total_size = sizeof(size_t) * (uint64)argc;
  1190. if (total_size >= UINT32_MAX
  1191. || !(offset_env_offsets = wasm_runtime_module_malloc
  1192. (module_inst, (uint32)total_size,
  1193. (void**)&env_offsets))
  1194. || env_buf_len >= UINT32_MAX
  1195. || !(offset_env_buf = wasm_runtime_module_malloc
  1196. (module_inst, (uint32)env_buf_len,
  1197. (void**)&env_buf))) {
  1198. set_error_buf(error_buf, error_buf_size,
  1199. "Init wasi environment failed: allocate memory failed");
  1200. goto fail;
  1201. }
  1202. for (i = 0; i < env_count; i++) {
  1203. env_offsets[i] = env_buf_offset;
  1204. bh_strcpy_s(env_buf + env_buf_offset,
  1205. (uint32)env_buf_len - env_buf_offset, env[i]);
  1206. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  1207. }
  1208. if (!(offset_curfds = wasm_runtime_module_malloc
  1209. (module_inst, sizeof(struct fd_table), (void**)&curfds))
  1210. || !(offset_prestats = wasm_runtime_module_malloc
  1211. (module_inst, sizeof(struct fd_prestats), (void**)&prestats))
  1212. || !(offset_argv_environ = wasm_runtime_module_malloc
  1213. (module_inst, sizeof(struct argv_environ_values),
  1214. (void**)&argv_environ))) {
  1215. set_error_buf(error_buf, error_buf_size,
  1216. "Init wasi environment failed: allocate memory failed");
  1217. goto fail;
  1218. }
  1219. if (!fd_table_init(curfds)) {
  1220. set_error_buf(error_buf, error_buf_size,
  1221. "Init wasi environment failed: "
  1222. "init fd table failed");
  1223. goto fail;
  1224. }
  1225. fd_table_inited = true;
  1226. if (!fd_prestats_init(prestats)) {
  1227. set_error_buf(error_buf, error_buf_size,
  1228. "Init wasi environment failed: "
  1229. "init fd prestats failed");
  1230. goto fail;
  1231. }
  1232. fd_prestats_inited = true;
  1233. if (!argv_environ_init(argv_environ,
  1234. argv_offsets, argc,
  1235. argv_buf, argv_buf_len,
  1236. env_offsets, env_count,
  1237. env_buf, env_buf_len)) {
  1238. set_error_buf(error_buf, error_buf_size,
  1239. "Init wasi environment failed: "
  1240. "init argument environment failed");
  1241. goto fail;
  1242. }
  1243. argv_environ_inited = true;
  1244. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  1245. if (!fd_table_insert_existing(curfds, 0, 0)
  1246. || !fd_table_insert_existing(curfds, 1, 1)
  1247. || !fd_table_insert_existing(curfds, 2, 2)) {
  1248. set_error_buf(error_buf, error_buf_size,
  1249. "Init wasi environment failed: init fd table failed");
  1250. goto fail;
  1251. }
  1252. wasm_fd = 3;
  1253. for (i = 0; i < dir_count; i++, wasm_fd++) {
  1254. path = realpath(dir_list[i], resolved_path);
  1255. if (!path) {
  1256. if (error_buf)
  1257. snprintf(error_buf, error_buf_size,
  1258. "error while pre-opening directory %s: %d\n",
  1259. dir_list[i], errno);
  1260. goto fail;
  1261. }
  1262. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  1263. if (raw_fd == -1) {
  1264. if (error_buf)
  1265. snprintf(error_buf, error_buf_size,
  1266. "error while pre-opening directory %s: %d\n",
  1267. dir_list[i], errno);
  1268. goto fail;
  1269. }
  1270. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  1271. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  1272. }
  1273. wasi_ctx->curfds_offset = offset_curfds;
  1274. wasi_ctx->prestats_offset = offset_prestats;
  1275. wasi_ctx->argv_environ_offset = offset_argv_environ;
  1276. wasi_ctx->argv_buf_offset = offset_argv_buf;
  1277. wasi_ctx->argv_offsets_offset = offset_argv_offsets;
  1278. wasi_ctx->env_buf_offset = offset_env_buf;
  1279. wasi_ctx->env_offsets_offset = offset_env_offsets;
  1280. return true;
  1281. fail:
  1282. if (argv_environ_inited)
  1283. argv_environ_destroy(argv_environ);
  1284. if (fd_prestats_inited)
  1285. fd_prestats_destroy(prestats);
  1286. if (fd_table_inited)
  1287. fd_table_destroy(curfds);
  1288. if (offset_curfds != 0)
  1289. wasm_runtime_module_free(module_inst, offset_curfds);
  1290. if (offset_prestats != 0)
  1291. wasm_runtime_module_free(module_inst, offset_prestats);
  1292. if (offset_argv_environ != 0)
  1293. wasm_runtime_module_free(module_inst, offset_argv_environ);
  1294. if (offset_argv_buf)
  1295. wasm_runtime_module_free(module_inst, offset_argv_buf);
  1296. if (offset_argv_offsets)
  1297. wasm_runtime_module_free(module_inst, offset_argv_offsets);
  1298. if (offset_env_buf)
  1299. wasm_runtime_module_free(module_inst, offset_env_buf);
  1300. if (offset_env_offsets)
  1301. wasm_runtime_module_free(module_inst, offset_env_offsets);
  1302. return false;
  1303. }
  1304. bool
  1305. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  1306. {
  1307. #if WASM_ENABLE_INTERP != 0
  1308. if (module_inst->module_type == Wasm_Module_Bytecode
  1309. && ((WASMModuleInstance*)module_inst)->module->is_wasi_module)
  1310. return true;
  1311. #endif
  1312. #if WASM_ENABLE_AOT != 0
  1313. if (module_inst->module_type == Wasm_Module_AoT
  1314. && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr)
  1315. ->is_wasi_module)
  1316. return true;
  1317. #endif
  1318. return false;
  1319. }
  1320. WASMFunctionInstanceCommon *
  1321. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  1322. {
  1323. uint32 i;
  1324. #if WASM_ENABLE_INTERP != 0
  1325. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1326. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1327. WASMFunctionInstance *func;
  1328. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1329. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  1330. func = wasm_inst->export_functions[i].function;
  1331. if (func->u.func->func_type->param_count != 0
  1332. || func->u.func->func_type->result_count != 0) {
  1333. LOG_ERROR("Lookup wasi _start function failed: "
  1334. "invalid function type.\n");
  1335. return NULL;
  1336. }
  1337. return (WASMFunctionInstanceCommon*)func;
  1338. }
  1339. }
  1340. return NULL;
  1341. }
  1342. #endif
  1343. #if WASM_ENABLE_AOT != 0
  1344. if (module_inst->module_type == Wasm_Module_AoT) {
  1345. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1346. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1347. aot_inst->export_funcs.ptr;
  1348. for (i = 0; i < aot_inst->export_func_count; i++) {
  1349. if (!strcmp(export_funcs[i].func_name, "_start")) {
  1350. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  1351. if (func_type->param_count != 0
  1352. || func_type->result_count != 0) {
  1353. LOG_ERROR("Lookup wasi _start function failed: "
  1354. "invalid function type.\n");
  1355. return NULL;
  1356. }
  1357. return (WASMFunctionInstanceCommon*)&export_funcs[i];
  1358. }
  1359. }
  1360. return NULL;
  1361. }
  1362. #endif /* end of WASM_ENABLE_AOT */
  1363. return NULL;
  1364. }
  1365. void
  1366. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  1367. {
  1368. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  1369. struct argv_environ_values *argv_environ;
  1370. struct fd_table *curfds;
  1371. struct fd_prestats *prestats;
  1372. if (wasi_ctx) {
  1373. if (wasi_ctx->argv_environ_offset) {
  1374. argv_environ = (struct argv_environ_values *)
  1375. wasm_runtime_addr_app_to_native(module_inst,
  1376. wasi_ctx->argv_environ_offset);
  1377. argv_environ_destroy(argv_environ);
  1378. wasm_runtime_module_free(module_inst, wasi_ctx->argv_environ_offset);
  1379. }
  1380. if (wasi_ctx->curfds_offset) {
  1381. curfds = (struct fd_table *)
  1382. wasm_runtime_addr_app_to_native(module_inst,
  1383. wasi_ctx->curfds_offset);
  1384. fd_table_destroy(curfds);
  1385. wasm_runtime_module_free(module_inst, wasi_ctx->curfds_offset);
  1386. }
  1387. if (wasi_ctx->prestats_offset) {
  1388. prestats = (struct fd_prestats *)
  1389. wasm_runtime_addr_app_to_native(module_inst,
  1390. wasi_ctx->prestats_offset);
  1391. fd_prestats_destroy(prestats);
  1392. wasm_runtime_module_free(module_inst, wasi_ctx->prestats_offset);
  1393. }
  1394. if (wasi_ctx->argv_buf_offset)
  1395. wasm_runtime_module_free(module_inst, wasi_ctx->argv_buf_offset);
  1396. if (wasi_ctx->argv_offsets_offset)
  1397. wasm_runtime_module_free(module_inst, wasi_ctx->argv_offsets_offset);
  1398. if (wasi_ctx->env_buf_offset)
  1399. wasm_runtime_module_free(module_inst, wasi_ctx->env_buf_offset);
  1400. if (wasi_ctx->env_offsets_offset)
  1401. wasm_runtime_module_free(module_inst, wasi_ctx->env_offsets_offset);
  1402. wasm_runtime_free(wasi_ctx);
  1403. }
  1404. }
  1405. WASIContext *
  1406. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  1407. {
  1408. #if WASM_ENABLE_INTERP != 0
  1409. if (module_inst->module_type == Wasm_Module_Bytecode)
  1410. return ((WASMModuleInstance*)module_inst)->wasi_ctx;
  1411. #endif
  1412. #if WASM_ENABLE_AOT != 0
  1413. if (module_inst->module_type == Wasm_Module_AoT)
  1414. return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr;
  1415. #endif
  1416. return NULL;
  1417. }
  1418. void
  1419. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  1420. WASIContext *wasi_ctx)
  1421. {
  1422. #if WASM_ENABLE_INTERP != 0
  1423. if (module_inst->module_type == Wasm_Module_Bytecode)
  1424. ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx;
  1425. #endif
  1426. #if WASM_ENABLE_AOT != 0
  1427. if (module_inst->module_type == Wasm_Module_AoT)
  1428. ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx;
  1429. #endif
  1430. }
  1431. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1432. WASMModuleCommon*
  1433. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  1434. {
  1435. WASMModuleInstanceCommon *module_inst =
  1436. wasm_runtime_get_module_inst(exec_env);
  1437. #if WASM_ENABLE_INTERP != 0
  1438. if (module_inst->module_type == Wasm_Module_Bytecode)
  1439. return (WASMModuleCommon*)
  1440. ((WASMModuleInstance*)module_inst)->module;
  1441. #endif
  1442. #if WASM_ENABLE_AOT != 0
  1443. if (module_inst->module_type == Wasm_Module_AoT)
  1444. return (WASMModuleCommon*)
  1445. ((AOTModuleInstance*)module_inst)->aot_module.ptr;
  1446. #endif
  1447. return NULL;
  1448. }
  1449. /**
  1450. * Implementation of wasm_application_execute_main()
  1451. */
  1452. static WASMFunctionInstanceCommon*
  1453. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1454. const char *name);
  1455. static bool
  1456. check_main_func_type(const WASMType *type)
  1457. {
  1458. if (!(type->param_count == 0 || type->param_count == 2)
  1459. ||type->result_count > 1) {
  1460. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1461. return false;
  1462. }
  1463. if (type->param_count == 2
  1464. && !(type->types[0] == VALUE_TYPE_I32
  1465. && type->types[1] == VALUE_TYPE_I32)) {
  1466. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1467. return false;
  1468. }
  1469. if (type->result_count
  1470. && type->types[type->param_count] != VALUE_TYPE_I32) {
  1471. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1472. return false;
  1473. }
  1474. return true;
  1475. }
  1476. bool
  1477. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  1478. int32 argc, char *argv[])
  1479. {
  1480. WASMFunctionInstanceCommon *func;
  1481. WASMType *func_type = NULL;
  1482. uint32 argc1 = 0, argv1[2] = { 0 };
  1483. uint32 total_argv_size = 0;
  1484. uint64 total_size;
  1485. uint32 argv_buf_offset;
  1486. int32 i;
  1487. char *argv_buf, *p, *p_end;
  1488. uint32 *argv_offsets;
  1489. #if WASM_ENABLE_LIBC_WASI != 0
  1490. if (wasm_runtime_is_wasi_mode(module_inst)) {
  1491. /* In wasi mode, we should call function named "_start"
  1492. which initializes the wasi envrionment and then calls
  1493. the actual main function. Directly call main function
  1494. may cause exception thrown. */
  1495. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
  1496. return wasm_runtime_create_exec_env_and_call_wasm(
  1497. module_inst, func, 0, NULL);
  1498. /* if no start function is found, we execute
  1499. the main function as normal */
  1500. }
  1501. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1502. func = resolve_function(module_inst, "_main");
  1503. if (!func) {
  1504. func = resolve_function(module_inst, "main");
  1505. }
  1506. if (!func) {
  1507. wasm_runtime_set_exception(module_inst,
  1508. "lookup main function failed");
  1509. return false;
  1510. }
  1511. #if WASM_ENABLE_INTERP != 0
  1512. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1513. if (((WASMFunctionInstance*)func)->is_import_func) {
  1514. wasm_runtime_set_exception(module_inst,
  1515. "lookup main function failed");
  1516. return false;
  1517. }
  1518. func_type = ((WASMFunctionInstance*)func)->u.func->func_type;
  1519. }
  1520. #endif
  1521. #if WASM_ENABLE_AOT != 0
  1522. if (module_inst->module_type == Wasm_Module_AoT)
  1523. func_type = ((AOTFunctionInstance*)func)->u.func.func_type;
  1524. #endif
  1525. if (!check_main_func_type(func_type)) {
  1526. wasm_runtime_set_exception(module_inst,
  1527. "invalid function type of main function");
  1528. return false;
  1529. }
  1530. if (func_type->param_count) {
  1531. for (i = 0; i < argc; i++)
  1532. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  1533. total_argv_size = align_uint(total_argv_size, 4);
  1534. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  1535. if (total_size >= UINT32_MAX
  1536. || !(argv_buf_offset =
  1537. wasm_runtime_module_malloc(module_inst, (uint32)total_size,
  1538. (void**)&argv_buf))) {
  1539. wasm_runtime_set_exception(module_inst,
  1540. "allocate memory failed");
  1541. return false;
  1542. }
  1543. p = argv_buf;
  1544. argv_offsets = (uint32*)(p + total_argv_size);
  1545. p_end = p + total_size;
  1546. for (i = 0; i < argc; i++) {
  1547. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  1548. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  1549. p += strlen(argv[i]) + 1;
  1550. }
  1551. argc1 = 2;
  1552. argv1[0] = (uint32)argc;
  1553. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  1554. }
  1555. return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  1556. argc1, argv1);
  1557. }
  1558. #if WASM_ENABLE_MULTI_MODULE != 0
  1559. static WASMModuleInstance *
  1560. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  1561. const char *sub_module_name)
  1562. {
  1563. WASMSubModInstNode *node =
  1564. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  1565. while (node && strcmp(node->module_name, sub_module_name)) {
  1566. node = bh_list_elem_next(node);
  1567. }
  1568. return node ? node->module_inst : NULL;
  1569. }
  1570. static bool
  1571. parse_function_name(char *orig_function_name, char **p_module_name,
  1572. char **p_function_name)
  1573. {
  1574. if (orig_function_name[0] != '$') {
  1575. *p_module_name = NULL;
  1576. *p_function_name = orig_function_name;
  1577. return true;
  1578. }
  1579. /**
  1580. * $module_name$function_name\0
  1581. * ===>
  1582. * module_name\0function_name\0
  1583. * ===>
  1584. * module_name
  1585. * function_name
  1586. */
  1587. char *p1 = orig_function_name;
  1588. char *p2 = strchr(p1 + 1, '$');
  1589. if (!p2) {
  1590. LOG_DEBUG("can not parse the incoming function name");
  1591. return false;
  1592. }
  1593. *p_module_name = p1 + 1;
  1594. *p2 = '\0';
  1595. *p_function_name = p2 + 1;
  1596. return strlen(*p_module_name) && strlen(*p_function_name);
  1597. }
  1598. #endif
  1599. /**
  1600. * Implementation of wasm_application_execute_func()
  1601. */
  1602. static WASMFunctionInstanceCommon*
  1603. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1604. const char *name)
  1605. {
  1606. uint32 i = 0;
  1607. WASMFunctionInstanceCommon *ret = NULL;
  1608. #if WASM_ENABLE_MULTI_MODULE != 0
  1609. WASMModuleInstance *sub_module_inst = NULL;
  1610. char *orig_name = NULL;
  1611. char *sub_module_name = NULL;
  1612. char *function_name = NULL;
  1613. uint32 length = strlen(name) + 1;
  1614. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  1615. if (!orig_name) {
  1616. return NULL;
  1617. }
  1618. strncpy(orig_name, name, length);
  1619. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  1620. goto LEAVE;
  1621. }
  1622. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  1623. if (sub_module_name) {
  1624. sub_module_inst = get_sub_module_inst(
  1625. (WASMModuleInstance *)module_inst, sub_module_name);
  1626. if (!sub_module_inst) {
  1627. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  1628. goto LEAVE;
  1629. }
  1630. }
  1631. #else
  1632. const char *function_name = name;
  1633. #endif
  1634. #if WASM_ENABLE_INTERP != 0
  1635. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1636. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1637. #if WASM_ENABLE_MULTI_MODULE != 0
  1638. wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
  1639. #endif /* WASM_ENABLE_MULTI_MODULE */
  1640. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1641. if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
  1642. ret = wasm_inst->export_functions[i].function;
  1643. break;
  1644. }
  1645. }
  1646. }
  1647. #endif /* WASM_ENABLE_INTERP */
  1648. #if WASM_ENABLE_AOT != 0
  1649. if (module_inst->module_type == Wasm_Module_AoT) {
  1650. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1651. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1652. aot_inst->export_funcs.ptr;
  1653. for (i = 0; i < aot_inst->export_func_count; i++) {
  1654. if (!strcmp(export_funcs[i].func_name, function_name)) {
  1655. ret = &export_funcs[i];
  1656. break;
  1657. }
  1658. }
  1659. }
  1660. #endif
  1661. #if WASM_ENABLE_MULTI_MODULE != 0
  1662. LEAVE:
  1663. wasm_runtime_free(orig_name);
  1664. #endif
  1665. return ret;
  1666. }
  1667. union ieee754_float {
  1668. float f;
  1669. /* This is the IEEE 754 single-precision format. */
  1670. union {
  1671. struct {
  1672. unsigned int negative:1;
  1673. unsigned int exponent:8;
  1674. unsigned int mantissa:23;
  1675. } ieee_big_endian;
  1676. struct {
  1677. unsigned int mantissa:23;
  1678. unsigned int exponent:8;
  1679. unsigned int negative:1;
  1680. } ieee_little_endian;
  1681. } ieee;
  1682. };
  1683. union ieee754_double {
  1684. double d;
  1685. /* This is the IEEE 754 double-precision format. */
  1686. union {
  1687. struct {
  1688. unsigned int negative:1;
  1689. unsigned int exponent:11;
  1690. /* Together these comprise the mantissa. */
  1691. unsigned int mantissa0:20;
  1692. unsigned int mantissa1:32;
  1693. } ieee_big_endian;
  1694. struct {
  1695. /* Together these comprise the mantissa. */
  1696. unsigned int mantissa1:32;
  1697. unsigned int mantissa0:20;
  1698. unsigned int exponent:11;
  1699. unsigned int negative:1;
  1700. } ieee_little_endian;
  1701. } ieee;
  1702. };
  1703. static union {
  1704. int a;
  1705. char b;
  1706. } __ue = { .a = 1 };
  1707. #define is_little_endian() (__ue.b == 1)
  1708. bool
  1709. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  1710. const char *name, int32 argc, char *argv[])
  1711. {
  1712. WASMFunctionInstanceCommon *func;
  1713. WASMType *type = NULL;
  1714. uint32 argc1, *argv1 = NULL, cell_num, j, k = 0;
  1715. int32 i, p;
  1716. uint64 total_size;
  1717. const char *exception;
  1718. char buf[128];
  1719. bh_assert(argc >= 0);
  1720. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  1721. func = resolve_function(module_inst, name);
  1722. if (!func) {
  1723. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  1724. wasm_runtime_set_exception(module_inst, buf);
  1725. goto fail;
  1726. }
  1727. #if WASM_ENABLE_INTERP != 0
  1728. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1729. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func;
  1730. if (wasm_func->is_import_func
  1731. #if WASM_ENABLE_MULTI_MODULE != 0
  1732. && !wasm_func->import_func_inst
  1733. #endif
  1734. ) {
  1735. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  1736. wasm_runtime_set_exception(module_inst, buf);
  1737. goto fail;
  1738. }
  1739. type = wasm_func->u.func->func_type;
  1740. argc1 = wasm_func->param_cell_num;
  1741. cell_num = argc1 > wasm_func->ret_cell_num ?
  1742. argc1 : wasm_func->ret_cell_num;
  1743. }
  1744. #endif
  1745. #if WASM_ENABLE_AOT != 0
  1746. if (module_inst->module_type == Wasm_Module_AoT) {
  1747. type = ((AOTFunctionInstance*)func)->u.func.func_type;
  1748. argc1 = type->param_cell_num;
  1749. cell_num = argc1 > type->ret_cell_num ?
  1750. argc1 : type->ret_cell_num;
  1751. }
  1752. #endif
  1753. if (type->param_count != (uint32)argc) {
  1754. wasm_runtime_set_exception(module_inst,
  1755. "invalid input argument count");
  1756. goto fail;
  1757. }
  1758. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1759. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst,
  1760. NULL, 0)))) {
  1761. goto fail;
  1762. }
  1763. /* Clear errno before parsing arguments */
  1764. errno = 0;
  1765. /* Parse arguments */
  1766. for (i = 0, p = 0; i < argc; i++) {
  1767. char *endptr = NULL;
  1768. bh_assert(argv[i] != NULL);
  1769. if (argv[i][0] == '\0') {
  1770. snprintf(buf, sizeof(buf), "invalid input argument %d", i);
  1771. wasm_runtime_set_exception(module_inst, buf);
  1772. goto fail;
  1773. }
  1774. switch (type->types[i]) {
  1775. case VALUE_TYPE_I32:
  1776. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  1777. break;
  1778. case VALUE_TYPE_I64:
  1779. {
  1780. union { uint64 val; uint32 parts[2]; } u;
  1781. u.val = strtoull(argv[i], &endptr, 0);
  1782. argv1[p++] = u.parts[0];
  1783. argv1[p++] = u.parts[1];
  1784. break;
  1785. }
  1786. case VALUE_TYPE_F32:
  1787. {
  1788. float32 f32 = strtof(argv[i], &endptr);
  1789. if (isnan(f32)) {
  1790. if (argv[i][0] == '-') {
  1791. union ieee754_float u;
  1792. u.f = f32;
  1793. if (is_little_endian())
  1794. u.ieee.ieee_little_endian.negative = 1;
  1795. else
  1796. u.ieee.ieee_big_endian.negative = 1;
  1797. memcpy(&f32, &u.f, sizeof(float));
  1798. }
  1799. if (endptr[0] == ':') {
  1800. uint32 sig;
  1801. union ieee754_float u;
  1802. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  1803. u.f = f32;
  1804. if (is_little_endian())
  1805. u.ieee.ieee_little_endian.mantissa = sig;
  1806. else
  1807. u.ieee.ieee_big_endian.mantissa = sig;
  1808. memcpy(&f32, &u.f, sizeof(float));
  1809. }
  1810. }
  1811. memcpy(&argv1[p++], &f32, sizeof(float));
  1812. break;
  1813. }
  1814. case VALUE_TYPE_F64:
  1815. {
  1816. union { float64 val; uint32 parts[2]; } u;
  1817. u.val = strtod(argv[i], &endptr);
  1818. if (isnan(u.val)) {
  1819. if (argv[i][0] == '-') {
  1820. union ieee754_double ud;
  1821. ud.d = u.val;
  1822. if (is_little_endian())
  1823. ud.ieee.ieee_little_endian.negative = 1;
  1824. else
  1825. ud.ieee.ieee_big_endian.negative = 1;
  1826. memcpy(&u.val, &ud.d, sizeof(double));
  1827. }
  1828. if (endptr[0] == ':') {
  1829. uint64 sig;
  1830. union ieee754_double ud;
  1831. sig = strtoull(endptr + 1, &endptr, 0);
  1832. ud.d = u.val;
  1833. if (is_little_endian()) {
  1834. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  1835. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  1836. }
  1837. else {
  1838. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  1839. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  1840. }
  1841. memcpy(&u.val, &ud.d, sizeof(double));
  1842. }
  1843. }
  1844. argv1[p++] = u.parts[0];
  1845. argv1[p++] = u.parts[1];
  1846. break;
  1847. }
  1848. }
  1849. if (endptr && *endptr != '\0' && *endptr != '_') {
  1850. snprintf(buf, sizeof(buf), "invalid input argument %d: %s",
  1851. i, argv[i]);
  1852. wasm_runtime_set_exception(module_inst, buf);
  1853. goto fail;
  1854. }
  1855. if (errno != 0) {
  1856. snprintf(buf, sizeof(buf),
  1857. "prepare function argument error, errno: %d", errno);
  1858. wasm_runtime_set_exception(module_inst, buf);
  1859. goto fail;
  1860. }
  1861. }
  1862. bh_assert(p == (int32)argc1);
  1863. wasm_runtime_set_exception(module_inst, NULL);
  1864. if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  1865. argc1, argv1)) {
  1866. goto fail;
  1867. }
  1868. /* print return value */
  1869. for (j = 0; j < type->result_count; j++) {
  1870. switch (type->types[type->param_count + j]) {
  1871. case VALUE_TYPE_I32:
  1872. os_printf("0x%x:i32", argv1[k]);
  1873. k++;
  1874. break;
  1875. case VALUE_TYPE_I64:
  1876. {
  1877. union { uint64 val; uint32 parts[2]; } u;
  1878. u.parts[0] = argv1[k];
  1879. u.parts[1] = argv1[k + 1];
  1880. k += 2;
  1881. #ifdef PRIx64
  1882. os_printf("0x%"PRIx64":i64", u.val);
  1883. #else
  1884. char buf[16];
  1885. if (sizeof(long) == 4)
  1886. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  1887. else
  1888. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  1889. os_printf(buf, u.val);
  1890. #endif
  1891. break;
  1892. }
  1893. case VALUE_TYPE_F32:
  1894. os_printf("%.7g:f32", *(float32*)(argv1 + k));
  1895. k++;
  1896. break;
  1897. case VALUE_TYPE_F64:
  1898. {
  1899. union { float64 val; uint32 parts[2]; } u;
  1900. u.parts[0] = argv1[k];
  1901. u.parts[1] = argv1[k + 1];
  1902. k += 2;
  1903. os_printf("%.7g:f64", u.val);
  1904. break;
  1905. }
  1906. }
  1907. if (j < (uint32)(type->result_count - 1))
  1908. os_printf(",");
  1909. }
  1910. os_printf("\n");
  1911. wasm_runtime_free(argv1);
  1912. return true;
  1913. fail:
  1914. if (argv1)
  1915. wasm_runtime_free(argv1);
  1916. exception = wasm_runtime_get_exception(module_inst);
  1917. bh_assert(exception);
  1918. os_printf("%s\n", exception);
  1919. return false;
  1920. }
  1921. bool
  1922. wasm_runtime_register_natives(const char *module_name,
  1923. NativeSymbol *native_symbols,
  1924. uint32 n_native_symbols)
  1925. {
  1926. return wasm_native_register_natives(module_name,
  1927. native_symbols, n_native_symbols);
  1928. }
  1929. bool
  1930. wasm_runtime_register_natives_raw(const char *module_name,
  1931. NativeSymbol *native_symbols,
  1932. uint32 n_native_symbols)
  1933. {
  1934. return wasm_native_register_natives_raw(module_name,
  1935. native_symbols, n_native_symbols);
  1936. }
  1937. bool
  1938. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  1939. const WASMType *func_type, const char *signature,
  1940. void *attachment,
  1941. uint32 *argv, uint32 argc, uint32 *argv_ret)
  1942. {
  1943. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  1944. typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*);
  1945. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  1946. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  1947. uint32 *argv_src = argv, i, argc1, ptr_len;
  1948. uint32 arg_i32;
  1949. bool ret = false;
  1950. argc1 = func_type->param_count;
  1951. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  1952. size = sizeof(uint64) * (uint64)argc1;
  1953. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  1954. NULL, 0))) {
  1955. return false;
  1956. }
  1957. }
  1958. argv_dst = argv1;
  1959. /* Traverse secondly to fill in each argument */
  1960. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  1961. switch (func_type->types[i]) {
  1962. case VALUE_TYPE_I32:
  1963. {
  1964. *(uint32*)argv_dst = arg_i32 = *argv_src++;
  1965. if (signature) {
  1966. if (signature[i + 1] == '*') {
  1967. /* param is a pointer */
  1968. if (signature[i + 2] == '~')
  1969. /* pointer with length followed */
  1970. ptr_len = *argv_src;
  1971. else
  1972. /* pointer without length followed */
  1973. ptr_len = 1;
  1974. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  1975. goto fail;
  1976. *(uintptr_t*)argv_dst = (uintptr_t)
  1977. wasm_runtime_addr_app_to_native(module, arg_i32);
  1978. }
  1979. else if (signature[i + 1] == '$') {
  1980. /* param is a string */
  1981. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  1982. goto fail;
  1983. *(uintptr_t*)argv_dst = (uintptr_t)
  1984. wasm_runtime_addr_app_to_native(module, arg_i32);
  1985. }
  1986. }
  1987. break;
  1988. }
  1989. case VALUE_TYPE_I64:
  1990. case VALUE_TYPE_F64:
  1991. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2);
  1992. argv_src += 2;
  1993. break;
  1994. case VALUE_TYPE_F32:
  1995. *(float32*)argv_dst = *(float32*)argv_src++;
  1996. break;
  1997. default:
  1998. bh_assert(0);
  1999. break;
  2000. }
  2001. }
  2002. exec_env->attachment = attachment;
  2003. invokeNativeRaw(exec_env, argv1);
  2004. exec_env->attachment = NULL;
  2005. if (func_type->result_count > 0) {
  2006. switch (func_type->types[func_type->param_count]) {
  2007. case VALUE_TYPE_I32:
  2008. argv_ret[0] = *(uint32*)argv1;
  2009. break;
  2010. case VALUE_TYPE_F32:
  2011. *(float32*)argv_ret = *(float32*)argv1;
  2012. break;
  2013. case VALUE_TYPE_I64:
  2014. case VALUE_TYPE_F64:
  2015. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64));
  2016. break;
  2017. default:
  2018. bh_assert(0);
  2019. break;
  2020. }
  2021. }
  2022. ret = true;
  2023. fail:
  2024. if (argv1 != argv_buf)
  2025. wasm_runtime_free(argv1);
  2026. return ret;
  2027. }
  2028. /**
  2029. * Implementation of wasm_runtime_invoke_native()
  2030. */
  2031. static inline void
  2032. word_copy(uint32 *dest, uint32 *src, unsigned num)
  2033. {
  2034. for (; num > 0; num--)
  2035. *dest++ = *src++;
  2036. }
  2037. #define PUT_I64_TO_ADDR(addr, value) do { \
  2038. union { int64 val; uint32 parts[2]; } u; \
  2039. u.val = (value); \
  2040. (addr)[0] = u.parts[0]; \
  2041. (addr)[1] = u.parts[1]; \
  2042. } while (0)
  2043. #define PUT_F64_TO_ADDR(addr, value) do { \
  2044. union { float64 val; uint32 parts[2]; } u; \
  2045. u.val = (value); \
  2046. (addr)[0] = u.parts[0]; \
  2047. (addr)[1] = u.parts[1]; \
  2048. } while (0)
  2049. /* The invoke native implementation on ARM platform with VFP co-processor */
  2050. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2051. typedef void (*GenericFunctionPointer)();
  2052. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2053. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2054. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2055. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32);
  2056. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2057. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2058. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  2059. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  2060. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  2061. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  2062. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  2063. #define MAX_REG_INTS 4
  2064. #define MAX_REG_FLOATS 16
  2065. bool
  2066. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2067. const WASMType *func_type, const char *signature,
  2068. void *attachment,
  2069. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2070. {
  2071. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2072. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2073. uint32 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
  2074. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_fps = 0, n_stacks = 0;
  2075. uint32 arg_i32, ptr_len;
  2076. uint32 result_count = func_type->result_count;
  2077. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2078. bool ret = false;
  2079. n_ints++; /* exec env */
  2080. /* Traverse firstly to calculate stack args count */
  2081. for (i = 0; i < func_type->param_count; i++) {
  2082. switch (func_type->types[i]) {
  2083. case VALUE_TYPE_I32:
  2084. if (n_ints < MAX_REG_INTS)
  2085. n_ints++;
  2086. else
  2087. n_stacks++;
  2088. break;
  2089. case VALUE_TYPE_I64:
  2090. if (n_ints < MAX_REG_INTS - 1) {
  2091. /* 64-bit data must be 8 bytes aligned in arm */
  2092. if (n_ints & 1)
  2093. n_ints++;
  2094. n_ints += 2;
  2095. }
  2096. else {
  2097. /* 64-bit data must be 8 bytes aligned in arm */
  2098. if (n_stacks & 1)
  2099. n_stacks++;
  2100. n_stacks += 2;
  2101. }
  2102. break;
  2103. case VALUE_TYPE_F32:
  2104. if (n_fps < MAX_REG_FLOATS)
  2105. n_fps++;
  2106. else
  2107. n_stacks++;
  2108. break;
  2109. case VALUE_TYPE_F64:
  2110. if (n_fps < MAX_REG_FLOATS - 1) {
  2111. /* 64-bit data must be 8 bytes aligned in arm */
  2112. if (n_fps & 1)
  2113. n_fps++;
  2114. n_fps += 2;
  2115. }
  2116. else {
  2117. /* 64-bit data must be 8 bytes aligned in arm */
  2118. if (n_stacks & 1)
  2119. n_stacks++;
  2120. n_stacks += 2;
  2121. }
  2122. break;
  2123. default:
  2124. bh_assert(0);
  2125. break;
  2126. }
  2127. }
  2128. for (i = 0; i < ext_ret_count; i++) {
  2129. if (n_ints < MAX_REG_INTS)
  2130. n_ints++;
  2131. else
  2132. n_stacks++;
  2133. }
  2134. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2135. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2136. size = sizeof(uint32) * (uint32)argc1;
  2137. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2138. NULL, 0))) {
  2139. return false;
  2140. }
  2141. }
  2142. ints = argv1;
  2143. fps = ints + MAX_REG_INTS;
  2144. stacks = fps + MAX_REG_FLOATS;
  2145. n_ints = 0;
  2146. n_fps = 0;
  2147. n_stacks = 0;
  2148. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2149. /* Traverse secondly to fill in each argument */
  2150. for (i = 0; i < func_type->param_count; i++) {
  2151. switch (func_type->types[i]) {
  2152. case VALUE_TYPE_I32:
  2153. {
  2154. arg_i32 = *argv_src++;
  2155. if (signature) {
  2156. if (signature[i + 1] == '*') {
  2157. /* param is a pointer */
  2158. if (signature[i + 2] == '~')
  2159. /* pointer with length followed */
  2160. ptr_len = *argv_src;
  2161. else
  2162. /* pointer without length followed */
  2163. ptr_len = 1;
  2164. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2165. goto fail;
  2166. arg_i32 = (uintptr_t)
  2167. wasm_runtime_addr_app_to_native(module, arg_i32);
  2168. }
  2169. else if (signature[i + 1] == '$') {
  2170. /* param is a string */
  2171. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2172. goto fail;
  2173. arg_i32 = (uintptr_t)
  2174. wasm_runtime_addr_app_to_native(module, arg_i32);
  2175. }
  2176. }
  2177. if (n_ints < MAX_REG_INTS)
  2178. ints[n_ints++] = arg_i32;
  2179. else
  2180. stacks[n_stacks++] = arg_i32;
  2181. break;
  2182. }
  2183. case VALUE_TYPE_I64:
  2184. if (n_ints < MAX_REG_INTS - 1) {
  2185. /* 64-bit data must be 8 bytes aligned in arm */
  2186. if (n_ints & 1)
  2187. n_ints++;
  2188. *(uint64*)&ints[n_ints] = *(uint64*)argv_src;
  2189. n_ints += 2;
  2190. }
  2191. else {
  2192. /* 64-bit data must be 8 bytes aligned in arm */
  2193. if (n_stacks & 1)
  2194. n_stacks++;
  2195. *(uint64*)&stacks[n_stacks] = *(uint64*)argv_src;
  2196. n_stacks += 2;
  2197. }
  2198. argv_src += 2;
  2199. break;
  2200. case VALUE_TYPE_F32:
  2201. if (n_fps < MAX_REG_FLOATS)
  2202. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2203. else
  2204. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2205. break;
  2206. case VALUE_TYPE_F64:
  2207. if (n_fps < MAX_REG_FLOATS - 1) {
  2208. /* 64-bit data must be 8 bytes aligned in arm */
  2209. if (n_fps & 1)
  2210. n_fps++;
  2211. *(float64*)&fps[n_fps] = *(float64*)argv_src;
  2212. n_fps += 2;
  2213. }
  2214. else {
  2215. /* 64-bit data must be 8 bytes aligned in arm */
  2216. if (n_stacks & 1)
  2217. n_stacks++;
  2218. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  2219. n_stacks += 2;
  2220. }
  2221. argv_src += 2;
  2222. break;
  2223. default:
  2224. bh_assert(0);
  2225. break;
  2226. }
  2227. }
  2228. /* Save extra result values' address to argv1 */
  2229. for (i = 0; i < ext_ret_count; i++) {
  2230. if (n_ints < MAX_REG_INTS)
  2231. ints[n_ints++] = *(uint32*)argv_src++;
  2232. else
  2233. stacks[n_stacks++] = *(uint32*)argv_src++;
  2234. }
  2235. exec_env->attachment = attachment;
  2236. if (func_type->result_count == 0) {
  2237. invokeNative_Void(func_ptr, argv1, n_stacks);
  2238. }
  2239. else {
  2240. switch (func_type->types[func_type->param_count]) {
  2241. case VALUE_TYPE_I32:
  2242. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  2243. break;
  2244. case VALUE_TYPE_I64:
  2245. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  2246. break;
  2247. case VALUE_TYPE_F32:
  2248. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  2249. break;
  2250. case VALUE_TYPE_F64:
  2251. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  2252. break;
  2253. default:
  2254. bh_assert(0);
  2255. break;
  2256. }
  2257. }
  2258. exec_env->attachment = NULL;
  2259. ret = true;
  2260. fail:
  2261. if (argv1 != argv_buf)
  2262. wasm_runtime_free(argv1);
  2263. return ret;
  2264. }
  2265. #endif /* end of defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) */
  2266. #if defined(BUILD_TARGET_X86_32) \
  2267. || defined(BUILD_TARGET_ARM) \
  2268. || defined(BUILD_TARGET_THUMB) \
  2269. || defined(BUILD_TARGET_MIPS) \
  2270. || defined(BUILD_TARGET_XTENSA)
  2271. typedef void (*GenericFunctionPointer)();
  2272. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  2273. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2274. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2275. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2276. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2277. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2278. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  2279. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  2280. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  2281. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  2282. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  2283. bool
  2284. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2285. const WASMType *func_type, const char *signature,
  2286. void *attachment,
  2287. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2288. {
  2289. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2290. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  2291. uint32 arg_i32, ptr_len;
  2292. uint32 result_count = func_type->result_count;
  2293. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2294. uint64 size;
  2295. bool ret = false;
  2296. #if defined(BUILD_TARGET_X86_32)
  2297. argc1 = argc + ext_ret_count + 2;
  2298. #else
  2299. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  2300. so we need to allocate more memory. */
  2301. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  2302. #endif
  2303. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2304. size = sizeof(uint32) * (uint64)argc1;
  2305. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2306. NULL, 0))) {
  2307. return false;
  2308. }
  2309. }
  2310. for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
  2311. argv1[j++] = ((uint32*)&exec_env)[i];
  2312. for (i = 0; i < func_type->param_count; i++) {
  2313. switch (func_type->types[i]) {
  2314. case VALUE_TYPE_I32:
  2315. {
  2316. arg_i32 = *argv++;
  2317. if (signature) {
  2318. if (signature[i + 1] == '*') {
  2319. /* param is a pointer */
  2320. if (signature[i + 2] == '~')
  2321. /* pointer with length followed */
  2322. ptr_len = *argv;
  2323. else
  2324. /* pointer without length followed */
  2325. ptr_len = 1;
  2326. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2327. goto fail;
  2328. arg_i32 = (uintptr_t)
  2329. wasm_runtime_addr_app_to_native(module, arg_i32);
  2330. }
  2331. else if (signature[i + 1] == '$') {
  2332. /* param is a string */
  2333. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2334. goto fail;
  2335. arg_i32 = (uintptr_t)
  2336. wasm_runtime_addr_app_to_native(module, arg_i32);
  2337. }
  2338. }
  2339. argv1[j++] = arg_i32;
  2340. break;
  2341. }
  2342. case VALUE_TYPE_I64:
  2343. case VALUE_TYPE_F64:
  2344. #if !defined(BUILD_TARGET_X86_32)
  2345. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  2346. and xtensa */
  2347. if (j & 1)
  2348. j++;
  2349. #endif
  2350. argv1[j++] = *argv++;
  2351. argv1[j++] = *argv++;
  2352. break;
  2353. case VALUE_TYPE_F32:
  2354. argv1[j++] = *argv++;
  2355. break;
  2356. default:
  2357. bh_assert(0);
  2358. break;
  2359. }
  2360. }
  2361. /* Save extra result values' address to argv1 */
  2362. word_copy(argv1 + j, argv, ext_ret_count);
  2363. argc1 = j + ext_ret_count;
  2364. exec_env->attachment = attachment;
  2365. if (func_type->result_count == 0) {
  2366. invokeNative_Void(func_ptr, argv1, argc1);
  2367. }
  2368. else {
  2369. switch (func_type->types[func_type->param_count]) {
  2370. case VALUE_TYPE_I32:
  2371. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  2372. break;
  2373. case VALUE_TYPE_I64:
  2374. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
  2375. break;
  2376. case VALUE_TYPE_F32:
  2377. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
  2378. break;
  2379. case VALUE_TYPE_F64:
  2380. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
  2381. break;
  2382. default:
  2383. bh_assert(0);
  2384. break;
  2385. }
  2386. }
  2387. exec_env->attachment = NULL;
  2388. ret = true;
  2389. fail:
  2390. if (argv1 != argv_buf)
  2391. wasm_runtime_free(argv1);
  2392. return ret;
  2393. }
  2394. #endif /* end of defined(BUILD_TARGET_X86_32) \
  2395. || defined(BUILD_TARGET_ARM) \
  2396. || defined(BUILD_TARGET_THUMB) \
  2397. || defined(BUILD_TARGET_MIPS) \
  2398. || defined(BUILD_TARGET_XTENSA) */
  2399. #if defined(BUILD_TARGET_X86_64) \
  2400. || defined(BUILD_TARGET_AMD_64) \
  2401. || defined(BUILD_TARGET_AARCH64)
  2402. typedef void (*GenericFunctionPointer)();
  2403. int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  2404. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2405. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2406. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*,uint64);
  2407. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2408. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2409. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  2410. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  2411. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2412. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2413. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2414. #if defined(_WIN32) || defined(_WIN32_)
  2415. #define MAX_REG_FLOATS 4
  2416. #define MAX_REG_INTS 4
  2417. #else
  2418. #define MAX_REG_FLOATS 8
  2419. #if defined(BUILD_TARGET_AARCH64)
  2420. #define MAX_REG_INTS 8
  2421. #else
  2422. #define MAX_REG_INTS 6
  2423. #endif /* end of defined(BUILD_TARGET_AARCH64 */
  2424. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  2425. bool
  2426. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2427. const WASMType *func_type, const char *signature,
  2428. void *attachment,
  2429. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2430. {
  2431. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2432. uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size, arg_i64;
  2433. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2434. uint32 arg_i32, ptr_len;
  2435. uint32 result_count = func_type->result_count;
  2436. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2437. bool ret = false;
  2438. #if defined(_WIN32) || defined(_WIN32_)
  2439. /* important difference in calling conventions */
  2440. #define n_fps n_ints
  2441. #else
  2442. int n_fps = 0;
  2443. #endif
  2444. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  2445. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2446. size = sizeof(uint64) * (uint64)argc1;
  2447. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2448. NULL, 0))) {
  2449. return false;
  2450. }
  2451. }
  2452. fps = argv1;
  2453. ints = fps + MAX_REG_FLOATS;
  2454. stacks = ints + MAX_REG_INTS;
  2455. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  2456. for (i = 0; i < func_type->param_count; i++) {
  2457. switch (func_type->types[i]) {
  2458. case VALUE_TYPE_I32:
  2459. {
  2460. arg_i32 = *argv_src++;
  2461. arg_i64 = arg_i32;
  2462. if (signature) {
  2463. if (signature[i + 1] == '*') {
  2464. /* param is a pointer */
  2465. if (signature[i + 2] == '~')
  2466. /* pointer with length followed */
  2467. ptr_len = *argv_src;
  2468. else
  2469. /* pointer without length followed */
  2470. ptr_len = 1;
  2471. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2472. goto fail;
  2473. arg_i64 = (uintptr_t)
  2474. wasm_runtime_addr_app_to_native(module, arg_i32);
  2475. }
  2476. else if (signature[i + 1] == '$') {
  2477. /* param is a string */
  2478. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2479. goto fail;
  2480. arg_i64 = (uintptr_t)
  2481. wasm_runtime_addr_app_to_native(module, arg_i32);
  2482. }
  2483. }
  2484. if (n_ints < MAX_REG_INTS)
  2485. ints[n_ints++] = arg_i64;
  2486. else
  2487. stacks[n_stacks++] = arg_i64;
  2488. break;
  2489. }
  2490. case VALUE_TYPE_I64:
  2491. if (n_ints < MAX_REG_INTS)
  2492. ints[n_ints++] = *(uint64*)argv_src;
  2493. else
  2494. stacks[n_stacks++] = *(uint64*)argv_src;
  2495. argv_src += 2;
  2496. break;
  2497. case VALUE_TYPE_F32:
  2498. if (n_fps < MAX_REG_FLOATS)
  2499. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2500. else
  2501. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2502. break;
  2503. case VALUE_TYPE_F64:
  2504. if (n_fps < MAX_REG_FLOATS)
  2505. *(float64*)&fps[n_fps++] = *(float64*)argv_src;
  2506. else
  2507. *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
  2508. argv_src += 2;
  2509. break;
  2510. default:
  2511. bh_assert(0);
  2512. break;
  2513. }
  2514. }
  2515. /* Save extra result values' address to argv1 */
  2516. for (i = 0; i < ext_ret_count; i++) {
  2517. if (n_ints < MAX_REG_INTS)
  2518. ints[n_ints++] = *(uint64*)argv_src;
  2519. else
  2520. stacks[n_stacks++] = *(uint64*)argv_src;
  2521. argv_src += 2;
  2522. }
  2523. exec_env->attachment = attachment;
  2524. if (result_count == 0) {
  2525. invokeNative_Void(func_ptr, argv1, n_stacks);
  2526. }
  2527. else {
  2528. /* Invoke the native function and get the first result value */
  2529. switch (func_type->types[func_type->param_count]) {
  2530. case VALUE_TYPE_I32:
  2531. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  2532. break;
  2533. case VALUE_TYPE_I64:
  2534. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  2535. break;
  2536. case VALUE_TYPE_F32:
  2537. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  2538. break;
  2539. case VALUE_TYPE_F64:
  2540. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  2541. break;
  2542. default:
  2543. bh_assert(0);
  2544. break;
  2545. }
  2546. }
  2547. exec_env->attachment = NULL;
  2548. ret = true;
  2549. fail:
  2550. if (argv1 != argv_buf)
  2551. wasm_runtime_free(argv1);
  2552. return ret;
  2553. }
  2554. #endif /* end of defined(BUILD_TARGET_X86_64) \
  2555. || defined(BUILD_TARGET_AMD_64) \
  2556. || defined(BUILD_TARGET_AARCH64) */
  2557. bool
  2558. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  2559. uint32_t element_indices,
  2560. uint32_t argc, uint32_t argv[])
  2561. {
  2562. if (!wasm_runtime_exec_env_check(exec_env)) {
  2563. LOG_ERROR("Invalid exec env stack info.");
  2564. return false;
  2565. }
  2566. /* this function is called from native code, so exec_env->handle and
  2567. exec_env->native_stack_boundary must have been set, we don't set
  2568. it again */
  2569. #if WASM_ENABLE_INTERP != 0
  2570. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  2571. return wasm_call_indirect(exec_env,
  2572. element_indices,
  2573. argc, argv);
  2574. #endif
  2575. #if WASM_ENABLE_AOT != 0
  2576. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  2577. return aot_call_indirect(exec_env, false, 0,
  2578. element_indices, argc, argv);
  2579. #endif
  2580. return false;
  2581. }
  2582. #if WASM_ENABLE_THREAD_MGR != 0
  2583. typedef struct WASMThreadArg {
  2584. WASMExecEnv *new_exec_env;
  2585. wasm_thread_callback_t callback;
  2586. void *arg;
  2587. } WASMThreadArg;
  2588. WASMExecEnv *
  2589. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  2590. {
  2591. return wasm_cluster_spawn_exec_env(exec_env);
  2592. }
  2593. void
  2594. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  2595. {
  2596. wasm_cluster_destroy_spawned_exec_env(exec_env);
  2597. }
  2598. static void*
  2599. wasm_runtime_thread_routine(void *arg)
  2600. {
  2601. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  2602. void *ret;
  2603. bh_assert(thread_arg->new_exec_env);
  2604. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  2605. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  2606. wasm_runtime_free(thread_arg);
  2607. os_thread_exit(ret);
  2608. return ret;
  2609. }
  2610. int32
  2611. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  2612. wasm_thread_callback_t callback, void *arg)
  2613. {
  2614. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  2615. WASMThreadArg *thread_arg;
  2616. int32 ret;
  2617. if (!new_exec_env)
  2618. return -1;
  2619. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  2620. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  2621. return -1;
  2622. }
  2623. thread_arg->new_exec_env = new_exec_env;
  2624. thread_arg->callback = callback;
  2625. thread_arg->arg = arg;
  2626. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  2627. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  2628. if (ret != 0) {
  2629. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  2630. wasm_runtime_free(thread_arg);
  2631. }
  2632. return ret;
  2633. }
  2634. int32
  2635. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  2636. {
  2637. return os_thread_join((korp_tid)tid, retval);
  2638. }
  2639. #endif