wasm_runtime_common.c 155 KB

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