wasm_runtime_common.c 130 KB

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