wasm_runtime_common.c 108 KB

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