wasm_runtime_common.c 167 KB

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