wasm_runtime_common.c 117 KB

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