wasm_runtime_common.c 132 KB

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