wasm_runtime_common.c 110 KB

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