wasm_c_api.c 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_log.h"
  6. #include "wasm_c_api_internal.h"
  7. #include "bh_assert.h"
  8. #include "wasm_export.h"
  9. #include "wasm_memory.h"
  10. #if WASM_ENABLE_INTERP != 0
  11. #include "wasm_runtime.h"
  12. #endif
  13. #if WASM_ENABLE_AOT != 0
  14. #include "aot_runtime.h"
  15. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT == 0
  16. #include "aot.h"
  17. #include "aot_llvm.h"
  18. #endif /*WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT == 0*/
  19. #endif /*WASM_ENABLE_AOT != 0*/
  20. #if WASM_ENABLE_WASM_CACHE != 0
  21. #include <openssl/sha.h>
  22. #endif
  23. #if WASM_ENABLE_THREAD_MGR != 0
  24. #include "thread_manager.h"
  25. #endif
  26. /*
  27. * Thread Model:
  28. * - Only one wasm_engine_t in one process
  29. * - One wasm_store_t is only accessed by one thread. wasm_store_t can't be
  30. * shared in threads
  31. * - wasm_module_t can be shared in threads
  32. * - wasm_instance_t can not be shared in threads
  33. */
  34. #define ASSERT_NOT_IMPLEMENTED() bh_assert(!"not implemented")
  35. #define UNREACHABLE() bh_assert(!"unreachable")
  36. typedef struct wasm_module_ex_t {
  37. struct WASMModuleCommon *module_comm_rt;
  38. wasm_byte_vec_t *binary;
  39. korp_mutex lock;
  40. uint32 ref_count;
  41. #if WASM_ENABLE_WASM_CACHE != 0
  42. char hash[SHA256_DIGEST_LENGTH];
  43. #endif
  44. } wasm_module_ex_t;
  45. #ifndef os_thread_local_attribute
  46. typedef struct thread_local_stores {
  47. korp_tid tid;
  48. unsigned stores_num;
  49. } thread_local_stores;
  50. #endif
  51. static void
  52. wasm_module_delete_internal(wasm_module_t *);
  53. static void
  54. wasm_instance_delete_internal(wasm_instance_t *);
  55. /* temporarily put stubs here */
  56. static wasm_store_t *
  57. wasm_store_copy(const wasm_store_t *src)
  58. {
  59. (void)src;
  60. LOG_WARNING("in the stub of %s", __FUNCTION__);
  61. return NULL;
  62. }
  63. wasm_module_t *
  64. wasm_module_copy(const wasm_module_t *src)
  65. {
  66. (void)src;
  67. LOG_WARNING("in the stub of %s", __FUNCTION__);
  68. return NULL;
  69. }
  70. wasm_instance_t *
  71. wasm_instance_copy(const wasm_instance_t *src)
  72. {
  73. (void)src;
  74. LOG_WARNING("in the stub of %s", __FUNCTION__);
  75. return NULL;
  76. }
  77. /* ---------------------------------------------------------------------- */
  78. static inline void *
  79. malloc_internal(uint64 size)
  80. {
  81. void *mem = NULL;
  82. if (size < UINT32_MAX && (mem = wasm_runtime_malloc((uint32)size))) {
  83. memset(mem, 0, size);
  84. }
  85. return mem;
  86. }
  87. /* clang-format off */
  88. #define RETURN_OBJ(obj, obj_del_func) \
  89. return obj; \
  90. failed: \
  91. obj_del_func(obj); \
  92. return NULL;
  93. #define RETURN_VOID(obj, obj_del_func) \
  94. return; \
  95. failed: \
  96. obj_del_func(obj); \
  97. return;
  98. /* clang-format on */
  99. /* Vectors */
  100. #define INIT_VEC(vector_p, init_func, ...) \
  101. do { \
  102. if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
  103. goto failed; \
  104. } \
  105. \
  106. init_func(vector_p, ##__VA_ARGS__); \
  107. if (vector_p->size && !vector_p->data) { \
  108. LOG_DEBUG("%s failed", #init_func); \
  109. goto failed; \
  110. } \
  111. } while (false)
  112. #define DEINIT_VEC(vector_p, deinit_func) \
  113. if ((vector_p)) { \
  114. deinit_func(vector_p); \
  115. wasm_runtime_free(vector_p); \
  116. vector_p = NULL; \
  117. }
  118. #define WASM_DEFINE_VEC(name) \
  119. void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \
  120. { \
  121. wasm_##name##_vec_new_uninitialized(out, 0); \
  122. } \
  123. void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \
  124. size_t size) \
  125. { \
  126. wasm_##name##_vec_new(out, size, NULL); \
  127. }
  128. /* vectors with no ownership management of elements */
  129. #define WASM_DEFINE_VEC_PLAIN(name) \
  130. WASM_DEFINE_VEC(name) \
  131. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  132. own wasm_##name##_t const data[]) \
  133. { \
  134. if (!out) { \
  135. return; \
  136. } \
  137. \
  138. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  139. \
  140. if (!size) { \
  141. return; \
  142. } \
  143. \
  144. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t), \
  145. true)) { \
  146. LOG_DEBUG("bh_vector_init failed"); \
  147. goto failed; \
  148. } \
  149. \
  150. if (data) { \
  151. uint32 size_in_bytes = 0; \
  152. size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t)); \
  153. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  154. out->num_elems = size; \
  155. } \
  156. \
  157. RETURN_VOID(out, wasm_##name##_vec_delete) \
  158. } \
  159. void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \
  160. const wasm_##name##_vec_t *src) \
  161. { \
  162. if (!src) { \
  163. return; \
  164. } \
  165. wasm_##name##_vec_new(out, src->size, src->data); \
  166. } \
  167. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  168. { \
  169. if (v) { \
  170. bh_vector_destroy((Vector *)v); \
  171. } \
  172. }
  173. /* vectors that own their elements */
  174. #define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \
  175. WASM_DEFINE_VEC(name) \
  176. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  177. own wasm_##name##_t *const data[]) \
  178. { \
  179. if (!out) { \
  180. return; \
  181. } \
  182. \
  183. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  184. \
  185. if (!size) { \
  186. return; \
  187. } \
  188. \
  189. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t *), \
  190. true)) { \
  191. LOG_DEBUG("bh_vector_init failed"); \
  192. goto failed; \
  193. } \
  194. \
  195. if (data) { \
  196. uint32 size_in_bytes = 0; \
  197. size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t *)); \
  198. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  199. out->num_elems = size; \
  200. } \
  201. \
  202. RETURN_VOID(out, wasm_##name##_vec_delete) \
  203. } \
  204. void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \
  205. const wasm_##name##_vec_t *src) \
  206. { \
  207. size_t i = 0; \
  208. \
  209. if (!out) { \
  210. return; \
  211. } \
  212. memset(out, 0, sizeof(Vector)); \
  213. \
  214. if (!src || !src->size) { \
  215. return; \
  216. } \
  217. \
  218. if (!bh_vector_init((Vector *)out, src->size, \
  219. sizeof(wasm_##name##_t *), true)) { \
  220. LOG_DEBUG("bh_vector_init failed"); \
  221. goto failed; \
  222. } \
  223. \
  224. for (i = 0; i != src->num_elems; ++i) { \
  225. if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \
  226. LOG_DEBUG("wasm_%s_copy failed", #name); \
  227. goto failed; \
  228. } \
  229. } \
  230. out->num_elems = src->num_elems; \
  231. \
  232. RETURN_VOID(out, wasm_##name##_vec_delete) \
  233. } \
  234. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  235. { \
  236. size_t i = 0; \
  237. if (!v) { \
  238. return; \
  239. } \
  240. for (i = 0; i != v->num_elems && v->data; ++i) { \
  241. elem_destroy_func(*(v->data + i)); \
  242. } \
  243. bh_vector_destroy((Vector *)v); \
  244. }
  245. WASM_DEFINE_VEC_PLAIN(byte)
  246. WASM_DEFINE_VEC_PLAIN(val)
  247. WASM_DEFINE_VEC_OWN(exporttype, wasm_exporttype_delete)
  248. WASM_DEFINE_VEC_OWN(extern, wasm_extern_delete)
  249. WASM_DEFINE_VEC_OWN(frame, wasm_frame_delete)
  250. WASM_DEFINE_VEC_OWN(functype, wasm_functype_delete)
  251. WASM_DEFINE_VEC_OWN(importtype, wasm_importtype_delete)
  252. WASM_DEFINE_VEC_OWN(instance, wasm_instance_delete_internal)
  253. WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal)
  254. WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
  255. WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
  256. #ifndef NDEBUG
  257. #if WASM_ENABLE_MEMORY_PROFILING != 0
  258. #define WASM_C_DUMP_PROC_MEM() LOG_PROC_MEM()
  259. #else
  260. #define WASM_C_DUMP_PROC_MEM() (void)0
  261. #endif
  262. #else
  263. #define WASM_C_DUMP_PROC_MEM() (void)0
  264. #endif
  265. /* Runtime Environment */
  266. own wasm_config_t *
  267. wasm_config_new(void)
  268. {
  269. /* since wasm_runtime_malloc is not ready */
  270. wasm_config_t *config = os_malloc(sizeof(wasm_config_t));
  271. if (!config)
  272. return NULL;
  273. memset(config, 0, sizeof(wasm_config_t));
  274. config->mem_alloc_type = Alloc_With_System_Allocator;
  275. return config;
  276. }
  277. void
  278. wasm_config_delete(own wasm_config_t *config)
  279. {
  280. if (config)
  281. os_free(config);
  282. }
  283. wasm_config_t *
  284. wasm_config_set_mem_alloc_opt(wasm_config_t *config,
  285. mem_alloc_type_t mem_alloc_type,
  286. MemAllocOption *mem_alloc_option)
  287. {
  288. if (!config)
  289. return NULL;
  290. config->mem_alloc_type = mem_alloc_type;
  291. if (mem_alloc_option)
  292. memcpy(&config->mem_alloc_option, mem_alloc_option,
  293. sizeof(MemAllocOption));
  294. return config;
  295. }
  296. wasm_config_t *
  297. wasm_config_set_linux_perf_opt(wasm_config_t *config, bool enable)
  298. {
  299. if (!config)
  300. return NULL;
  301. config->enable_linux_perf = enable;
  302. return config;
  303. }
  304. wasm_config_t *
  305. wasm_config_set_segue_flags(wasm_config_t *config, uint32 segue_flags)
  306. {
  307. if (!config)
  308. return NULL;
  309. config->segue_flags = segue_flags;
  310. return config;
  311. }
  312. static void
  313. wasm_engine_delete_internal(wasm_engine_t *engine)
  314. {
  315. if (engine) {
  316. /* clean all created wasm_module_t and their locks */
  317. unsigned i;
  318. for (i = 0; i < engine->modules.num_elems; i++) {
  319. wasm_module_ex_t *module;
  320. if (bh_vector_get(&engine->modules, i, &module)) {
  321. os_mutex_destroy(&module->lock);
  322. wasm_runtime_free(module);
  323. }
  324. }
  325. bh_vector_destroy(&engine->modules);
  326. #ifndef os_thread_local_attribute
  327. bh_vector_destroy(&engine->stores_by_tid);
  328. #endif
  329. wasm_runtime_free(engine);
  330. }
  331. wasm_runtime_destroy();
  332. }
  333. static wasm_engine_t *
  334. wasm_engine_new_internal(wasm_config_t *config)
  335. {
  336. wasm_engine_t *engine = NULL;
  337. /* init runtime */
  338. RuntimeInitArgs init_args = { 0 };
  339. #if WASM_ENABLE_JIT != 0
  340. LLVMJITOptions *jit_options = wasm_runtime_get_llvm_jit_options();
  341. #endif
  342. #ifndef NDEBUG
  343. bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
  344. #else
  345. bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
  346. #endif
  347. WASM_C_DUMP_PROC_MEM();
  348. /* wasm_config_t->MemAllocOption -> RuntimeInitArgs->MemAllocOption */
  349. init_args.mem_alloc_type = config->mem_alloc_type;
  350. memcpy(&init_args.mem_alloc_option, &config->mem_alloc_option,
  351. sizeof(MemAllocOption));
  352. init_args.enable_linux_perf = config->enable_linux_perf;
  353. init_args.segue_flags = config->segue_flags;
  354. #if WASM_ENABLE_JIT != 0
  355. jit_options->quick_invoke_c_api_import = true;
  356. #endif
  357. if (!wasm_runtime_full_init(&init_args)) {
  358. LOG_DEBUG("wasm_runtime_full_init failed");
  359. goto failed;
  360. }
  361. /* create wasm_engine_t */
  362. if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) {
  363. goto failed;
  364. }
  365. if (!bh_vector_init(&engine->modules, DEFAULT_VECTOR_INIT_SIZE,
  366. sizeof(wasm_module_ex_t *), true))
  367. goto failed;
  368. #ifndef os_thread_local_attribute
  369. if (!bh_vector_init(&engine->stores_by_tid, DEFAULT_VECTOR_INIT_SIZE,
  370. sizeof(thread_local_stores), true))
  371. goto failed;
  372. #endif
  373. engine->ref_count = 1;
  374. WASM_C_DUMP_PROC_MEM();
  375. RETURN_OBJ(engine, wasm_engine_delete_internal)
  376. }
  377. /* global engine instance */
  378. static wasm_engine_t *singleton_engine;
  379. #ifdef os_thread_local_attribute
  380. /* categorize wasm_store_t as threads*/
  381. static os_thread_local_attribute unsigned thread_local_stores_num = 0;
  382. #endif
  383. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  384. /**
  385. * lock for the singleton_engine
  386. * Note: if the platform has mutex initializer, we use a global lock to
  387. * lock the operations of the singleton_engine, otherwise when there are
  388. * operations happening simultaneously in multiple threads, developer
  389. * must create the lock by himself, and use it to lock the operations
  390. */
  391. static korp_mutex engine_lock = OS_THREAD_MUTEX_INITIALIZER;
  392. #endif
  393. own wasm_engine_t *
  394. wasm_engine_new()
  395. {
  396. wasm_config_t config = { 0 };
  397. wasm_config_set_mem_alloc_opt(&config, Alloc_With_System_Allocator, NULL);
  398. wasm_engine_t *engine = wasm_engine_new_with_config(&config);
  399. return engine;
  400. }
  401. own wasm_engine_t *
  402. wasm_engine_new_with_config(wasm_config_t *config)
  403. {
  404. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  405. os_mutex_lock(&engine_lock);
  406. #endif
  407. if (!singleton_engine)
  408. singleton_engine = wasm_engine_new_internal(config);
  409. else
  410. singleton_engine->ref_count++;
  411. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  412. os_mutex_unlock(&engine_lock);
  413. #endif
  414. return singleton_engine;
  415. }
  416. own wasm_engine_t *
  417. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts)
  418. {
  419. wasm_config_t config = { 0 };
  420. config.mem_alloc_type = type;
  421. memcpy(&config.mem_alloc_option, opts, sizeof(MemAllocOption));
  422. return wasm_engine_new_with_config(&config);
  423. }
  424. void
  425. wasm_engine_delete(wasm_engine_t *engine)
  426. {
  427. if (!engine)
  428. return;
  429. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  430. os_mutex_lock(&engine_lock);
  431. #endif
  432. if (!singleton_engine) {
  433. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  434. os_mutex_unlock(&engine_lock);
  435. #endif
  436. return;
  437. }
  438. bh_assert(engine == singleton_engine);
  439. bh_assert(singleton_engine->ref_count > 0);
  440. singleton_engine->ref_count--;
  441. if (singleton_engine->ref_count == 0) {
  442. wasm_engine_delete_internal(engine);
  443. singleton_engine = NULL;
  444. }
  445. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  446. os_mutex_unlock(&engine_lock);
  447. #endif
  448. }
  449. #ifndef os_thread_local_attribute
  450. static bool
  451. search_thread_local_store_num(Vector *stores_by_tid, korp_tid tid,
  452. thread_local_stores *out_ts, unsigned *out_i)
  453. {
  454. unsigned i;
  455. for (i = 0; i < stores_by_tid->num_elems; i++) {
  456. bool ret = bh_vector_get(stores_by_tid, i, out_ts);
  457. bh_assert(ret);
  458. (void)ret;
  459. if (out_ts->tid == tid) {
  460. *out_i = i;
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. #endif
  467. static unsigned
  468. retrive_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  469. {
  470. #ifndef os_thread_local_attribute
  471. unsigned i = 0;
  472. thread_local_stores ts = { 0 };
  473. unsigned ret = 0;
  474. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  475. os_mutex_lock(&engine_lock);
  476. #endif
  477. if (search_thread_local_store_num(stores_by_tid, tid, &ts, &i))
  478. ret = ts.stores_num;
  479. else
  480. ret = 0;
  481. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  482. os_mutex_unlock(&engine_lock);
  483. #endif
  484. return ret;
  485. #else
  486. (void)stores_by_tid;
  487. (void)tid;
  488. return thread_local_stores_num;
  489. #endif
  490. }
  491. static bool
  492. increase_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  493. {
  494. #ifndef os_thread_local_attribute
  495. unsigned i = 0;
  496. thread_local_stores ts = { 0 };
  497. bool ret = false;
  498. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  499. os_mutex_lock(&engine_lock);
  500. #endif
  501. if (search_thread_local_store_num(stores_by_tid, tid, &ts, &i)) {
  502. /* just in case if integer overflow */
  503. if (ts.stores_num + 1 < ts.stores_num) {
  504. ret = false;
  505. }
  506. else {
  507. ts.stores_num++;
  508. ret = bh_vector_set(stores_by_tid, i, &ts);
  509. bh_assert(ret);
  510. }
  511. }
  512. else {
  513. ts.tid = tid;
  514. ts.stores_num = 1;
  515. ret = bh_vector_append(stores_by_tid, &ts);
  516. }
  517. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  518. os_mutex_unlock(&engine_lock);
  519. #endif
  520. return ret;
  521. #else
  522. (void)stores_by_tid;
  523. (void)tid;
  524. /* just in case if integer overflow */
  525. if (thread_local_stores_num + 1 < thread_local_stores_num)
  526. return false;
  527. thread_local_stores_num++;
  528. return true;
  529. #endif
  530. }
  531. static bool
  532. decrease_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  533. {
  534. #ifndef os_thread_local_attribute
  535. unsigned i = 0;
  536. thread_local_stores ts = { 0 };
  537. bool ret = false;
  538. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  539. os_mutex_lock(&engine_lock);
  540. #endif
  541. ret = search_thread_local_store_num(stores_by_tid, tid, &ts, &i);
  542. bh_assert(ret);
  543. /* just in case if integer overflow */
  544. if (ts.stores_num - 1 > ts.stores_num) {
  545. ret = false;
  546. }
  547. else {
  548. ts.stores_num--;
  549. ret = bh_vector_set(stores_by_tid, i, &ts);
  550. bh_assert(ret);
  551. }
  552. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  553. os_mutex_unlock(&engine_lock);
  554. #endif
  555. return ret;
  556. #else
  557. (void)stores_by_tid;
  558. (void)tid;
  559. /* just in case if integer overflow */
  560. if (thread_local_stores_num - 1 > thread_local_stores_num)
  561. return false;
  562. thread_local_stores_num--;
  563. return true;
  564. #endif
  565. }
  566. wasm_store_t *
  567. wasm_store_new(wasm_engine_t *engine)
  568. {
  569. wasm_store_t *store = NULL;
  570. WASM_C_DUMP_PROC_MEM();
  571. if (!engine || singleton_engine != engine)
  572. return NULL;
  573. if (!retrive_thread_local_store_num(&engine->stores_by_tid,
  574. os_self_thread())) {
  575. if (!wasm_runtime_init_thread_env()) {
  576. LOG_ERROR("init thread environment failed");
  577. return NULL;
  578. }
  579. if (!increase_thread_local_store_num(&engine->stores_by_tid,
  580. os_self_thread())) {
  581. wasm_runtime_destroy_thread_env();
  582. return NULL;
  583. }
  584. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  585. decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  586. os_self_thread());
  587. wasm_runtime_destroy_thread_env();
  588. return NULL;
  589. }
  590. }
  591. else {
  592. if (!increase_thread_local_store_num(&engine->stores_by_tid,
  593. os_self_thread()))
  594. return NULL;
  595. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  596. decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  597. os_self_thread());
  598. return NULL;
  599. }
  600. }
  601. /* new a vector, and new its data */
  602. INIT_VEC(store->modules, wasm_module_vec_new_uninitialized,
  603. DEFAULT_VECTOR_INIT_LENGTH);
  604. INIT_VEC(store->instances, wasm_instance_vec_new_uninitialized,
  605. DEFAULT_VECTOR_INIT_LENGTH);
  606. if (!(store->foreigns = malloc_internal(sizeof(Vector)))
  607. || !(bh_vector_init(store->foreigns, 24, sizeof(wasm_foreign_t *),
  608. true))) {
  609. goto failed;
  610. }
  611. WASM_C_DUMP_PROC_MEM();
  612. return store;
  613. failed:
  614. wasm_store_delete(store);
  615. return NULL;
  616. }
  617. void
  618. wasm_store_delete(wasm_store_t *store)
  619. {
  620. if (!store) {
  621. return;
  622. }
  623. DEINIT_VEC(store->instances, wasm_instance_vec_delete);
  624. DEINIT_VEC(store->modules, wasm_module_vec_delete);
  625. if (store->foreigns) {
  626. bh_vector_destroy(store->foreigns);
  627. wasm_runtime_free(store->foreigns);
  628. }
  629. wasm_runtime_free(store);
  630. if (decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  631. os_self_thread())) {
  632. if (!retrive_thread_local_store_num(&singleton_engine->stores_by_tid,
  633. os_self_thread())) {
  634. wasm_runtime_destroy_thread_env();
  635. }
  636. }
  637. }
  638. /* Type Representations */
  639. static inline wasm_valkind_t
  640. val_type_rt_2_valkind(uint8 val_type_rt)
  641. {
  642. switch (val_type_rt) {
  643. #define WAMR_VAL_TYPE_2_WASM_VAL_KIND(name) \
  644. case VALUE_TYPE_##name: \
  645. return WASM_##name;
  646. WAMR_VAL_TYPE_2_WASM_VAL_KIND(I32)
  647. WAMR_VAL_TYPE_2_WASM_VAL_KIND(I64)
  648. WAMR_VAL_TYPE_2_WASM_VAL_KIND(F32)
  649. WAMR_VAL_TYPE_2_WASM_VAL_KIND(F64)
  650. WAMR_VAL_TYPE_2_WASM_VAL_KIND(FUNCREF)
  651. #undef WAMR_VAL_TYPE_2_WASM_VAL_KIND
  652. default:
  653. return WASM_ANYREF;
  654. }
  655. }
  656. static wasm_valtype_t *
  657. wasm_valtype_new_internal(uint8 val_type_rt)
  658. {
  659. return wasm_valtype_new(val_type_rt_2_valkind(val_type_rt));
  660. }
  661. wasm_valtype_t *
  662. wasm_valtype_new(wasm_valkind_t kind)
  663. {
  664. wasm_valtype_t *val_type;
  665. if (kind > WASM_F64 && WASM_FUNCREF != kind
  666. #if WASM_ENABLE_REF_TYPES != 0
  667. && WASM_ANYREF != kind
  668. #endif
  669. ) {
  670. return NULL;
  671. }
  672. if (!(val_type = malloc_internal(sizeof(wasm_valtype_t)))) {
  673. return NULL;
  674. }
  675. val_type->kind = kind;
  676. return val_type;
  677. }
  678. void
  679. wasm_valtype_delete(wasm_valtype_t *val_type)
  680. {
  681. if (val_type) {
  682. wasm_runtime_free(val_type);
  683. }
  684. }
  685. wasm_valtype_t *
  686. wasm_valtype_copy(const wasm_valtype_t *src)
  687. {
  688. return src ? wasm_valtype_new(src->kind) : NULL;
  689. }
  690. wasm_valkind_t
  691. wasm_valtype_kind(const wasm_valtype_t *val_type)
  692. {
  693. return val_type ? val_type->kind : WASM_ANYREF;
  694. }
  695. static wasm_functype_t *
  696. wasm_functype_new_internal(WASMType *type_rt)
  697. {
  698. wasm_functype_t *type = NULL;
  699. wasm_valtype_t *param_type = NULL, *result_type = NULL;
  700. uint32 i = 0;
  701. if (!type_rt) {
  702. return NULL;
  703. }
  704. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  705. return NULL;
  706. }
  707. type->extern_kind = WASM_EXTERN_FUNC;
  708. /* WASMType->types[0 : type_rt->param_count) -> type->params */
  709. INIT_VEC(type->params, wasm_valtype_vec_new_uninitialized,
  710. type_rt->param_count);
  711. for (i = 0; i < type_rt->param_count; ++i) {
  712. if (!(param_type = wasm_valtype_new_internal(*(type_rt->types + i)))) {
  713. goto failed;
  714. }
  715. if (!bh_vector_append((Vector *)type->params, &param_type)) {
  716. LOG_DEBUG("bh_vector_append failed");
  717. goto failed;
  718. }
  719. }
  720. /* WASMType->types[type_rt->param_count : type_rt->result_count) ->
  721. * type->results */
  722. INIT_VEC(type->results, wasm_valtype_vec_new_uninitialized,
  723. type_rt->result_count);
  724. for (i = 0; i < type_rt->result_count; ++i) {
  725. if (!(result_type = wasm_valtype_new_internal(
  726. *(type_rt->types + type_rt->param_count + i)))) {
  727. goto failed;
  728. }
  729. if (!bh_vector_append((Vector *)type->results, &result_type)) {
  730. LOG_DEBUG("bh_vector_append failed");
  731. goto failed;
  732. }
  733. }
  734. return type;
  735. failed:
  736. wasm_valtype_delete(param_type);
  737. wasm_valtype_delete(result_type);
  738. wasm_functype_delete(type);
  739. return NULL;
  740. }
  741. wasm_functype_t *
  742. wasm_functype_new(own wasm_valtype_vec_t *params,
  743. own wasm_valtype_vec_t *results)
  744. {
  745. wasm_functype_t *type = NULL;
  746. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  747. goto failed;
  748. }
  749. type->extern_kind = WASM_EXTERN_FUNC;
  750. /* take ownership */
  751. if (!(type->params = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  752. goto failed;
  753. }
  754. if (params) {
  755. bh_memcpy_s(type->params, sizeof(wasm_valtype_vec_t), params,
  756. sizeof(wasm_valtype_vec_t));
  757. }
  758. if (!(type->results = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  759. goto failed;
  760. }
  761. if (results) {
  762. bh_memcpy_s(type->results, sizeof(wasm_valtype_vec_t), results,
  763. sizeof(wasm_valtype_vec_t));
  764. }
  765. return type;
  766. failed:
  767. wasm_functype_delete(type);
  768. return NULL;
  769. }
  770. wasm_functype_t *
  771. wasm_functype_copy(const wasm_functype_t *src)
  772. {
  773. wasm_functype_t *functype;
  774. wasm_valtype_vec_t params = { 0 }, results = { 0 };
  775. if (!src) {
  776. return NULL;
  777. }
  778. wasm_valtype_vec_copy(&params, src->params);
  779. if (src->params->size && !params.data) {
  780. goto failed;
  781. }
  782. wasm_valtype_vec_copy(&results, src->results);
  783. if (src->results->size && !results.data) {
  784. goto failed;
  785. }
  786. if (!(functype = wasm_functype_new(&params, &results))) {
  787. goto failed;
  788. }
  789. return functype;
  790. failed:
  791. wasm_valtype_vec_delete(&params);
  792. wasm_valtype_vec_delete(&results);
  793. return NULL;
  794. }
  795. void
  796. wasm_functype_delete(wasm_functype_t *func_type)
  797. {
  798. if (!func_type) {
  799. return;
  800. }
  801. DEINIT_VEC(func_type->params, wasm_valtype_vec_delete);
  802. DEINIT_VEC(func_type->results, wasm_valtype_vec_delete);
  803. wasm_runtime_free(func_type);
  804. }
  805. const wasm_valtype_vec_t *
  806. wasm_functype_params(const wasm_functype_t *func_type)
  807. {
  808. if (!func_type) {
  809. return NULL;
  810. }
  811. return func_type->params;
  812. }
  813. const wasm_valtype_vec_t *
  814. wasm_functype_results(const wasm_functype_t *func_type)
  815. {
  816. if (!func_type) {
  817. return NULL;
  818. }
  819. return func_type->results;
  820. }
  821. static bool
  822. cmp_val_kind_with_val_type(wasm_valkind_t v_k, uint8 v_t)
  823. {
  824. return (v_k == WASM_I32 && v_t == VALUE_TYPE_I32)
  825. || (v_k == WASM_I64 && v_t == VALUE_TYPE_I64)
  826. || (v_k == WASM_F32 && v_t == VALUE_TYPE_F32)
  827. || (v_k == WASM_F64 && v_t == VALUE_TYPE_F64)
  828. || (v_k == WASM_ANYREF && v_t == VALUE_TYPE_EXTERNREF)
  829. || (v_k == WASM_FUNCREF && v_t == VALUE_TYPE_FUNCREF);
  830. }
  831. /*
  832. *to compare a function type of wasm-c-api with a function type of wasm_runtime
  833. */
  834. static bool
  835. wasm_functype_same_internal(const wasm_functype_t *type,
  836. const WASMType *type_intl)
  837. {
  838. uint32 i = 0;
  839. if (!type || !type_intl || type->params->num_elems != type_intl->param_count
  840. || type->results->num_elems != type_intl->result_count)
  841. return false;
  842. for (i = 0; i < type->params->num_elems; i++) {
  843. wasm_valtype_t *v_t = type->params->data[i];
  844. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(v_t),
  845. type_intl->types[i]))
  846. return false;
  847. }
  848. for (i = 0; i < type->results->num_elems; i++) {
  849. wasm_valtype_t *v_t = type->results->data[i];
  850. if (!cmp_val_kind_with_val_type(
  851. wasm_valtype_kind(v_t),
  852. type_intl->types[i + type->params->num_elems]))
  853. return false;
  854. }
  855. return true;
  856. }
  857. wasm_globaltype_t *
  858. wasm_globaltype_new(own wasm_valtype_t *val_type, wasm_mutability_t mut)
  859. {
  860. wasm_globaltype_t *global_type = NULL;
  861. if (!val_type) {
  862. return NULL;
  863. }
  864. if (!(global_type = malloc_internal(sizeof(wasm_globaltype_t)))) {
  865. return NULL;
  866. }
  867. global_type->extern_kind = WASM_EXTERN_GLOBAL;
  868. global_type->val_type = val_type;
  869. global_type->mutability = mut;
  870. return global_type;
  871. }
  872. wasm_globaltype_t *
  873. wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable)
  874. {
  875. wasm_globaltype_t *globaltype;
  876. wasm_valtype_t *val_type;
  877. if (!(val_type = wasm_valtype_new(val_type_rt_2_valkind(val_type_rt)))) {
  878. return NULL;
  879. }
  880. if (!(globaltype = wasm_globaltype_new(
  881. val_type, is_mutable ? WASM_VAR : WASM_CONST))) {
  882. wasm_valtype_delete(val_type);
  883. }
  884. return globaltype;
  885. }
  886. void
  887. wasm_globaltype_delete(wasm_globaltype_t *global_type)
  888. {
  889. if (!global_type) {
  890. return;
  891. }
  892. if (global_type->val_type) {
  893. wasm_valtype_delete(global_type->val_type);
  894. global_type->val_type = NULL;
  895. }
  896. wasm_runtime_free(global_type);
  897. }
  898. wasm_globaltype_t *
  899. wasm_globaltype_copy(const wasm_globaltype_t *src)
  900. {
  901. wasm_globaltype_t *global_type;
  902. wasm_valtype_t *val_type;
  903. if (!src) {
  904. return NULL;
  905. }
  906. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  907. return NULL;
  908. }
  909. if (!(global_type = wasm_globaltype_new(val_type, src->mutability))) {
  910. wasm_valtype_delete(val_type);
  911. }
  912. return global_type;
  913. }
  914. const wasm_valtype_t *
  915. wasm_globaltype_content(const wasm_globaltype_t *global_type)
  916. {
  917. if (!global_type) {
  918. return NULL;
  919. }
  920. return global_type->val_type;
  921. }
  922. wasm_mutability_t
  923. wasm_globaltype_mutability(const wasm_globaltype_t *global_type)
  924. {
  925. if (!global_type) {
  926. return false;
  927. }
  928. return global_type->mutability;
  929. }
  930. static wasm_tabletype_t *
  931. wasm_tabletype_new_internal(uint8 val_type_rt, uint32 init_size,
  932. uint32 max_size)
  933. {
  934. wasm_tabletype_t *table_type;
  935. wasm_limits_t limits = { init_size, max_size };
  936. wasm_valtype_t *val_type;
  937. if (!(val_type = wasm_valtype_new_internal(val_type_rt))) {
  938. return NULL;
  939. }
  940. if (!(table_type = wasm_tabletype_new(val_type, &limits))) {
  941. wasm_valtype_delete(val_type);
  942. }
  943. return table_type;
  944. }
  945. wasm_tabletype_t *
  946. wasm_tabletype_new(own wasm_valtype_t *val_type, const wasm_limits_t *limits)
  947. {
  948. wasm_tabletype_t *table_type = NULL;
  949. if (!val_type || !limits) {
  950. return NULL;
  951. }
  952. if (wasm_valtype_kind(val_type) != WASM_FUNCREF
  953. #if WASM_ENABLE_REF_TYPES != 0
  954. && wasm_valtype_kind(val_type) != WASM_ANYREF
  955. #endif
  956. ) {
  957. return NULL;
  958. }
  959. if (!(table_type = malloc_internal(sizeof(wasm_tabletype_t)))) {
  960. return NULL;
  961. }
  962. table_type->extern_kind = WASM_EXTERN_TABLE;
  963. table_type->val_type = val_type;
  964. table_type->limits.min = limits->min;
  965. table_type->limits.max = limits->max;
  966. return table_type;
  967. }
  968. wasm_tabletype_t *
  969. wasm_tabletype_copy(const wasm_tabletype_t *src)
  970. {
  971. wasm_tabletype_t *table_type;
  972. wasm_valtype_t *val_type;
  973. if (!src) {
  974. return NULL;
  975. }
  976. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  977. return NULL;
  978. }
  979. if (!(table_type = wasm_tabletype_new(val_type, &src->limits))) {
  980. wasm_valtype_delete(val_type);
  981. }
  982. return table_type;
  983. }
  984. void
  985. wasm_tabletype_delete(wasm_tabletype_t *table_type)
  986. {
  987. if (!table_type) {
  988. return;
  989. }
  990. if (table_type->val_type) {
  991. wasm_valtype_delete(table_type->val_type);
  992. table_type->val_type = NULL;
  993. }
  994. wasm_runtime_free(table_type);
  995. }
  996. const wasm_valtype_t *
  997. wasm_tabletype_element(const wasm_tabletype_t *table_type)
  998. {
  999. if (!table_type) {
  1000. return NULL;
  1001. }
  1002. return table_type->val_type;
  1003. }
  1004. const wasm_limits_t *
  1005. wasm_tabletype_limits(const wasm_tabletype_t *table_type)
  1006. {
  1007. if (!table_type) {
  1008. return NULL;
  1009. }
  1010. return &(table_type->limits);
  1011. }
  1012. static wasm_memorytype_t *
  1013. wasm_memorytype_new_internal(uint32 min_pages, uint32 max_pages)
  1014. {
  1015. wasm_limits_t limits = { min_pages, max_pages };
  1016. return wasm_memorytype_new(&limits);
  1017. }
  1018. wasm_memorytype_t *
  1019. wasm_memorytype_new(const wasm_limits_t *limits)
  1020. {
  1021. wasm_memorytype_t *memory_type = NULL;
  1022. if (!limits) {
  1023. return NULL;
  1024. }
  1025. if (!(memory_type = malloc_internal(sizeof(wasm_memorytype_t)))) {
  1026. return NULL;
  1027. }
  1028. memory_type->extern_kind = WASM_EXTERN_MEMORY;
  1029. memory_type->limits.min = limits->min;
  1030. memory_type->limits.max = limits->max;
  1031. return memory_type;
  1032. }
  1033. wasm_memorytype_t *
  1034. wasm_memorytype_copy(const wasm_memorytype_t *src)
  1035. {
  1036. if (!src) {
  1037. return NULL;
  1038. }
  1039. return wasm_memorytype_new(&src->limits);
  1040. }
  1041. void
  1042. wasm_memorytype_delete(wasm_memorytype_t *memory_type)
  1043. {
  1044. if (memory_type) {
  1045. wasm_runtime_free(memory_type);
  1046. }
  1047. }
  1048. const wasm_limits_t *
  1049. wasm_memorytype_limits(const wasm_memorytype_t *memory_type)
  1050. {
  1051. if (!memory_type) {
  1052. return NULL;
  1053. }
  1054. return &(memory_type->limits);
  1055. }
  1056. wasm_externkind_t
  1057. wasm_externtype_kind(const wasm_externtype_t *extern_type)
  1058. {
  1059. if (!extern_type) {
  1060. return WASM_EXTERN_FUNC;
  1061. }
  1062. return extern_type->extern_kind;
  1063. }
  1064. #define BASIC_FOUR_TYPE_LIST(V) \
  1065. V(functype) \
  1066. V(globaltype) \
  1067. V(memorytype) \
  1068. V(tabletype)
  1069. #define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \
  1070. wasm_##name##_t *wasm_externtype_as_##name(wasm_externtype_t *extern_type) \
  1071. { \
  1072. return (wasm_##name##_t *)extern_type; \
  1073. }
  1074. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE)
  1075. #undef WASM_EXTERNTYPE_AS_OTHERTYPE
  1076. #define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \
  1077. wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \
  1078. { \
  1079. return (wasm_externtype_t *)other; \
  1080. }
  1081. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE)
  1082. #undef WASM_OTHERTYPE_AS_EXTERNTYPE
  1083. #define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \
  1084. const wasm_##name##_t *wasm_externtype_as_##name##_const( \
  1085. const wasm_externtype_t *extern_type) \
  1086. { \
  1087. return (const wasm_##name##_t *)extern_type; \
  1088. }
  1089. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE_CONST)
  1090. #undef WASM_EXTERNTYPE_AS_OTHERTYPE_CONST
  1091. #define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \
  1092. const wasm_externtype_t *wasm_##name##_as_externtype_const( \
  1093. const wasm_##name##_t *other) \
  1094. { \
  1095. return (const wasm_externtype_t *)other; \
  1096. }
  1097. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE_CONST)
  1098. #undef WASM_OTHERTYPE_AS_EXTERNTYPE_CONST
  1099. wasm_externtype_t *
  1100. wasm_externtype_copy(const wasm_externtype_t *src)
  1101. {
  1102. wasm_externtype_t *extern_type = NULL;
  1103. if (!src) {
  1104. return NULL;
  1105. }
  1106. switch (src->extern_kind) {
  1107. #define COPY_EXTERNTYPE(NAME, name) \
  1108. case WASM_EXTERN_##NAME: \
  1109. { \
  1110. extern_type = wasm_##name##_as_externtype( \
  1111. wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \
  1112. break; \
  1113. }
  1114. COPY_EXTERNTYPE(FUNC, functype)
  1115. COPY_EXTERNTYPE(GLOBAL, globaltype)
  1116. COPY_EXTERNTYPE(MEMORY, memorytype)
  1117. COPY_EXTERNTYPE(TABLE, tabletype)
  1118. #undef COPY_EXTERNTYPE
  1119. default:
  1120. LOG_WARNING("%s meets unsupported kind %u", __FUNCTION__,
  1121. src->extern_kind);
  1122. break;
  1123. }
  1124. return extern_type;
  1125. }
  1126. void
  1127. wasm_externtype_delete(wasm_externtype_t *extern_type)
  1128. {
  1129. if (!extern_type) {
  1130. return;
  1131. }
  1132. switch (wasm_externtype_kind(extern_type)) {
  1133. case WASM_EXTERN_FUNC:
  1134. wasm_functype_delete(wasm_externtype_as_functype(extern_type));
  1135. break;
  1136. case WASM_EXTERN_GLOBAL:
  1137. wasm_globaltype_delete(wasm_externtype_as_globaltype(extern_type));
  1138. break;
  1139. case WASM_EXTERN_MEMORY:
  1140. wasm_memorytype_delete(wasm_externtype_as_memorytype(extern_type));
  1141. break;
  1142. case WASM_EXTERN_TABLE:
  1143. wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type));
  1144. break;
  1145. default:
  1146. LOG_WARNING("%s meets unsupported type %u", __FUNCTION__,
  1147. wasm_externtype_kind(extern_type));
  1148. break;
  1149. }
  1150. }
  1151. own wasm_importtype_t *
  1152. wasm_importtype_new(own wasm_byte_vec_t *module_name,
  1153. own wasm_byte_vec_t *field_name,
  1154. own wasm_externtype_t *extern_type)
  1155. {
  1156. wasm_importtype_t *import_type = NULL;
  1157. if (!module_name || !field_name || !extern_type) {
  1158. return NULL;
  1159. }
  1160. if (!(import_type = malloc_internal(sizeof(wasm_importtype_t)))) {
  1161. return NULL;
  1162. }
  1163. /* take ownership */
  1164. if (!(import_type->module_name =
  1165. malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1166. goto failed;
  1167. }
  1168. bh_memcpy_s(import_type->module_name, sizeof(wasm_byte_vec_t), module_name,
  1169. sizeof(wasm_byte_vec_t));
  1170. if (!(import_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1171. goto failed;
  1172. }
  1173. bh_memcpy_s(import_type->name, sizeof(wasm_byte_vec_t), field_name,
  1174. sizeof(wasm_byte_vec_t));
  1175. import_type->extern_type = extern_type;
  1176. return import_type;
  1177. failed:
  1178. wasm_importtype_delete(import_type);
  1179. return NULL;
  1180. }
  1181. void
  1182. wasm_importtype_delete(own wasm_importtype_t *import_type)
  1183. {
  1184. if (!import_type) {
  1185. return;
  1186. }
  1187. DEINIT_VEC(import_type->module_name, wasm_byte_vec_delete);
  1188. DEINIT_VEC(import_type->name, wasm_byte_vec_delete);
  1189. wasm_externtype_delete(import_type->extern_type);
  1190. import_type->extern_type = NULL;
  1191. wasm_runtime_free(import_type);
  1192. }
  1193. own wasm_importtype_t *
  1194. wasm_importtype_copy(const wasm_importtype_t *src)
  1195. {
  1196. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1197. wasm_externtype_t *extern_type = NULL;
  1198. wasm_importtype_t *import_type = NULL;
  1199. if (!src) {
  1200. return NULL;
  1201. }
  1202. wasm_byte_vec_copy(&module_name, src->module_name);
  1203. if (src->module_name->size && !module_name.data) {
  1204. goto failed;
  1205. }
  1206. wasm_byte_vec_copy(&name, src->name);
  1207. if (src->name->size && !name.data) {
  1208. goto failed;
  1209. }
  1210. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1211. goto failed;
  1212. }
  1213. if (!(import_type =
  1214. wasm_importtype_new(&module_name, &name, extern_type))) {
  1215. goto failed;
  1216. }
  1217. return import_type;
  1218. failed:
  1219. wasm_byte_vec_delete(&module_name);
  1220. wasm_byte_vec_delete(&name);
  1221. wasm_externtype_delete(extern_type);
  1222. wasm_importtype_delete(import_type);
  1223. return NULL;
  1224. }
  1225. const wasm_byte_vec_t *
  1226. wasm_importtype_module(const wasm_importtype_t *import_type)
  1227. {
  1228. if (!import_type) {
  1229. return NULL;
  1230. }
  1231. return import_type->module_name;
  1232. }
  1233. const wasm_byte_vec_t *
  1234. wasm_importtype_name(const wasm_importtype_t *import_type)
  1235. {
  1236. if (!import_type) {
  1237. return NULL;
  1238. }
  1239. return import_type->name;
  1240. }
  1241. const wasm_externtype_t *
  1242. wasm_importtype_type(const wasm_importtype_t *import_type)
  1243. {
  1244. if (!import_type) {
  1245. return NULL;
  1246. }
  1247. return import_type->extern_type;
  1248. }
  1249. bool
  1250. wasm_importtype_is_linked(const wasm_importtype_t *import_type)
  1251. {
  1252. if (!import_type)
  1253. return false;
  1254. const wasm_name_t *module_name = wasm_importtype_module(import_type);
  1255. const wasm_name_t *field_name = wasm_importtype_name(import_type);
  1256. switch (wasm_externtype_kind(wasm_importtype_type(import_type))) {
  1257. case WASM_EXTERN_FUNC:
  1258. return wasm_runtime_is_import_func_linked(module_name->data,
  1259. field_name->data);
  1260. case WASM_EXTERN_GLOBAL:
  1261. return wasm_runtime_is_import_global_linked(module_name->data,
  1262. field_name->data);
  1263. case WASM_EXTERN_MEMORY:
  1264. case WASM_EXTERN_TABLE:
  1265. default:
  1266. break;
  1267. }
  1268. return false;
  1269. }
  1270. own wasm_exporttype_t *
  1271. wasm_exporttype_new(own wasm_byte_vec_t *name,
  1272. own wasm_externtype_t *extern_type)
  1273. {
  1274. wasm_exporttype_t *export_type = NULL;
  1275. if (!name || !extern_type) {
  1276. return NULL;
  1277. }
  1278. if (!(export_type = malloc_internal(sizeof(wasm_exporttype_t)))) {
  1279. return NULL;
  1280. }
  1281. if (!(export_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1282. wasm_exporttype_delete(export_type);
  1283. return NULL;
  1284. }
  1285. bh_memcpy_s(export_type->name, sizeof(wasm_byte_vec_t), name,
  1286. sizeof(wasm_byte_vec_t));
  1287. export_type->extern_type = extern_type;
  1288. return export_type;
  1289. }
  1290. wasm_exporttype_t *
  1291. wasm_exporttype_copy(const wasm_exporttype_t *src)
  1292. {
  1293. wasm_exporttype_t *export_type;
  1294. wasm_byte_vec_t name = { 0 };
  1295. wasm_externtype_t *extern_type = NULL;
  1296. if (!src) {
  1297. return NULL;
  1298. }
  1299. wasm_byte_vec_copy(&name, src->name);
  1300. if (src->name->size && !name.data) {
  1301. goto failed;
  1302. }
  1303. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1304. goto failed;
  1305. }
  1306. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1307. goto failed;
  1308. }
  1309. return export_type;
  1310. failed:
  1311. wasm_byte_vec_delete(&name);
  1312. wasm_externtype_delete(extern_type);
  1313. return NULL;
  1314. }
  1315. void
  1316. wasm_exporttype_delete(wasm_exporttype_t *export_type)
  1317. {
  1318. if (!export_type) {
  1319. return;
  1320. }
  1321. DEINIT_VEC(export_type->name, wasm_byte_vec_delete);
  1322. wasm_externtype_delete(export_type->extern_type);
  1323. wasm_runtime_free(export_type);
  1324. }
  1325. const wasm_byte_vec_t *
  1326. wasm_exporttype_name(const wasm_exporttype_t *export_type)
  1327. {
  1328. if (!export_type) {
  1329. return NULL;
  1330. }
  1331. return export_type->name;
  1332. }
  1333. const wasm_externtype_t *
  1334. wasm_exporttype_type(const wasm_exporttype_t *export_type)
  1335. {
  1336. if (!export_type) {
  1337. return NULL;
  1338. }
  1339. return export_type->extern_type;
  1340. }
  1341. /* Runtime Objects */
  1342. void
  1343. wasm_val_delete(wasm_val_t *v)
  1344. {
  1345. if (v)
  1346. wasm_runtime_free(v);
  1347. }
  1348. void
  1349. wasm_val_copy(wasm_val_t *out, const wasm_val_t *src)
  1350. {
  1351. if (!out || !src) {
  1352. return;
  1353. }
  1354. bh_memcpy_s(out, sizeof(wasm_val_t), src, sizeof(wasm_val_t));
  1355. }
  1356. bool
  1357. rt_val_to_wasm_val(const uint8 *data, uint8 val_type_rt, wasm_val_t *out)
  1358. {
  1359. bool ret = true;
  1360. switch (val_type_rt) {
  1361. case VALUE_TYPE_I32:
  1362. out->kind = WASM_I32;
  1363. out->of.i32 = *((int32 *)data);
  1364. break;
  1365. case VALUE_TYPE_F32:
  1366. out->kind = WASM_F32;
  1367. out->of.f32 = *((float32 *)data);
  1368. break;
  1369. case VALUE_TYPE_I64:
  1370. out->kind = WASM_I64;
  1371. out->of.i64 = *((int64 *)data);
  1372. break;
  1373. case VALUE_TYPE_F64:
  1374. out->kind = WASM_F64;
  1375. out->of.f64 = *((float64 *)data);
  1376. break;
  1377. #if WASM_ENABLE_REF_TYPES != 0
  1378. case VALUE_TYPE_EXTERNREF:
  1379. out->kind = WASM_ANYREF;
  1380. if (NULL_REF == *(uint32 *)data) {
  1381. out->of.ref = NULL;
  1382. }
  1383. else {
  1384. ret = wasm_externref_ref2obj(*(uint32 *)data,
  1385. (void **)&out->of.ref);
  1386. }
  1387. break;
  1388. #endif
  1389. default:
  1390. LOG_WARNING("unexpected value type %d", val_type_rt);
  1391. ret = false;
  1392. }
  1393. return ret;
  1394. }
  1395. bool
  1396. wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, uint8 val_type_rt,
  1397. const wasm_val_t *v, uint8 *data)
  1398. {
  1399. bool ret = true;
  1400. switch (val_type_rt) {
  1401. case VALUE_TYPE_I32:
  1402. bh_assert(WASM_I32 == v->kind);
  1403. *((int32 *)data) = v->of.i32;
  1404. break;
  1405. case VALUE_TYPE_F32:
  1406. bh_assert(WASM_F32 == v->kind);
  1407. *((float32 *)data) = v->of.f32;
  1408. break;
  1409. case VALUE_TYPE_I64:
  1410. bh_assert(WASM_I64 == v->kind);
  1411. *((int64 *)data) = v->of.i64;
  1412. break;
  1413. case VALUE_TYPE_F64:
  1414. bh_assert(WASM_F64 == v->kind);
  1415. *((float64 *)data) = v->of.f64;
  1416. break;
  1417. #if WASM_ENABLE_REF_TYPES != 0
  1418. case VALUE_TYPE_EXTERNREF:
  1419. bh_assert(WASM_ANYREF == v->kind);
  1420. ret =
  1421. wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data);
  1422. break;
  1423. #endif
  1424. default:
  1425. LOG_WARNING("unexpected value type %d", val_type_rt);
  1426. ret = false;
  1427. break;
  1428. }
  1429. (void)inst_comm_rt;
  1430. return ret;
  1431. }
  1432. wasm_ref_t *
  1433. wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind,
  1434. uint32 ref_idx_rt, WASMModuleInstanceCommon *inst_comm_rt)
  1435. {
  1436. wasm_ref_t *ref;
  1437. if (!store) {
  1438. return NULL;
  1439. }
  1440. if (!(ref = malloc_internal(sizeof(wasm_ref_t)))) {
  1441. return NULL;
  1442. }
  1443. ref->store = store;
  1444. ref->kind = kind;
  1445. ref->ref_idx_rt = ref_idx_rt;
  1446. ref->inst_comm_rt = inst_comm_rt;
  1447. /* workaround */
  1448. if (WASM_REF_foreign == kind) {
  1449. wasm_foreign_t *foreign;
  1450. if (!(bh_vector_get(ref->store->foreigns, ref->ref_idx_rt, &foreign))
  1451. || !foreign) {
  1452. wasm_runtime_free(ref);
  1453. return NULL;
  1454. }
  1455. foreign->ref_cnt++;
  1456. }
  1457. /* others doesn't include ref counters */
  1458. return ref;
  1459. }
  1460. own wasm_ref_t *
  1461. wasm_ref_copy(const wasm_ref_t *src)
  1462. {
  1463. if (!src)
  1464. return NULL;
  1465. /* host_info are different in wasm_ref_t(s) */
  1466. return wasm_ref_new_internal(src->store, src->kind, src->ref_idx_rt,
  1467. src->inst_comm_rt);
  1468. }
  1469. #define DELETE_HOST_INFO(obj) \
  1470. if (obj->host_info.info) { \
  1471. if (obj->host_info.finalizer) { \
  1472. obj->host_info.finalizer(obj->host_info.info); \
  1473. } \
  1474. }
  1475. void
  1476. wasm_ref_delete(own wasm_ref_t *ref)
  1477. {
  1478. if (!ref || !ref->store)
  1479. return;
  1480. DELETE_HOST_INFO(ref);
  1481. if (WASM_REF_foreign == ref->kind) {
  1482. wasm_foreign_t *foreign = NULL;
  1483. if (bh_vector_get(ref->store->foreigns, ref->ref_idx_rt, &foreign)
  1484. && foreign) {
  1485. wasm_foreign_delete(foreign);
  1486. }
  1487. }
  1488. wasm_runtime_free(ref);
  1489. }
  1490. #define WASM_DEFINE_REF_BASE(name) \
  1491. bool wasm_##name##_same(const wasm_##name##_t *o1, \
  1492. const wasm_##name##_t *o2) \
  1493. { \
  1494. return (!o1 && !o2) ? true \
  1495. : (!o1 || !o2) ? false \
  1496. : (o1->kind != o2->kind) \
  1497. ? false \
  1498. : o1->name##_idx_rt == o2->name##_idx_rt; \
  1499. } \
  1500. \
  1501. void *wasm_##name##_get_host_info(const wasm_##name##_t *obj) \
  1502. { \
  1503. return obj ? obj->host_info.info : NULL; \
  1504. } \
  1505. \
  1506. void wasm_##name##_set_host_info(wasm_##name##_t *obj, void *host_info) \
  1507. { \
  1508. if (obj) { \
  1509. obj->host_info.info = host_info; \
  1510. obj->host_info.finalizer = NULL; \
  1511. } \
  1512. } \
  1513. \
  1514. void wasm_##name##_set_host_info_with_finalizer( \
  1515. wasm_##name##_t *obj, void *host_info, void (*finalizer)(void *)) \
  1516. { \
  1517. if (obj) { \
  1518. obj->host_info.info = host_info; \
  1519. obj->host_info.finalizer = finalizer; \
  1520. } \
  1521. }
  1522. #define WASM_DEFINE_REF(name) \
  1523. WASM_DEFINE_REF_BASE(name) \
  1524. \
  1525. wasm_ref_t *wasm_##name##_as_ref(wasm_##name##_t *name) \
  1526. { \
  1527. if (!name) { \
  1528. return NULL; \
  1529. } \
  1530. \
  1531. return wasm_ref_new_internal(name->store, WASM_REF_##name, \
  1532. name->name##_idx_rt, name->inst_comm_rt); \
  1533. } \
  1534. \
  1535. const wasm_ref_t *wasm_##name##_as_ref_const(const wasm_##name##_t *name) \
  1536. { \
  1537. if (!name) { \
  1538. return NULL; \
  1539. } \
  1540. \
  1541. return wasm_ref_new_internal(name->store, WASM_REF_##name, \
  1542. name->name##_idx_rt, name->inst_comm_rt); \
  1543. } \
  1544. \
  1545. wasm_##name##_t *wasm_ref_as_##name(wasm_ref_t *ref) \
  1546. { \
  1547. if (!ref || WASM_REF_##name != ref->kind) { \
  1548. return NULL; \
  1549. } \
  1550. \
  1551. return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \
  1552. ref->inst_comm_rt); \
  1553. } \
  1554. \
  1555. const wasm_##name##_t *wasm_ref_as_##name##_const(const wasm_ref_t *ref) \
  1556. { \
  1557. if (!ref || WASM_REF_##name != ref->kind) { \
  1558. return NULL; \
  1559. } \
  1560. \
  1561. return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \
  1562. ref->inst_comm_rt); \
  1563. }
  1564. WASM_DEFINE_REF_BASE(ref)
  1565. WASM_DEFINE_REF(foreign)
  1566. WASM_DEFINE_REF(func)
  1567. WASM_DEFINE_REF(global)
  1568. WASM_DEFINE_REF(memory)
  1569. WASM_DEFINE_REF(table)
  1570. static wasm_frame_t *
  1571. wasm_frame_new(wasm_instance_t *instance, size_t module_offset,
  1572. uint32 func_index, size_t func_offset)
  1573. {
  1574. wasm_frame_t *frame;
  1575. if (!(frame = malloc_internal(sizeof(wasm_frame_t)))) {
  1576. return NULL;
  1577. }
  1578. frame->instance = instance;
  1579. frame->module_offset = (uint32)module_offset;
  1580. frame->func_index = func_index;
  1581. frame->func_offset = (uint32)func_offset;
  1582. return frame;
  1583. }
  1584. own wasm_frame_t *
  1585. wasm_frame_copy(const wasm_frame_t *src)
  1586. {
  1587. if (!src) {
  1588. return NULL;
  1589. }
  1590. return wasm_frame_new(src->instance, src->module_offset, src->func_index,
  1591. src->func_offset);
  1592. }
  1593. void
  1594. wasm_frame_delete(own wasm_frame_t *frame)
  1595. {
  1596. if (frame) {
  1597. wasm_runtime_free(frame);
  1598. }
  1599. }
  1600. struct wasm_instance_t *
  1601. wasm_frame_instance(const wasm_frame_t *frame)
  1602. {
  1603. return frame ? frame->instance : NULL;
  1604. }
  1605. size_t
  1606. wasm_frame_module_offset(const wasm_frame_t *frame)
  1607. {
  1608. return frame ? frame->module_offset : 0;
  1609. }
  1610. uint32_t
  1611. wasm_frame_func_index(const wasm_frame_t *frame)
  1612. {
  1613. return frame ? frame->func_index : 0;
  1614. }
  1615. size_t
  1616. wasm_frame_func_offset(const wasm_frame_t *frame)
  1617. {
  1618. return frame ? frame->func_offset : 0;
  1619. }
  1620. void
  1621. wasm_frame_vec_clone_internal(Vector *src, Vector *out)
  1622. {
  1623. bh_assert(src->num_elems != 0 && src->data);
  1624. bh_vector_destroy(out);
  1625. if (!bh_vector_init(out, src->num_elems, sizeof(WASMCApiFrame), false)) {
  1626. bh_vector_destroy(out);
  1627. return;
  1628. }
  1629. bh_memcpy_s(out->data, src->num_elems * sizeof(WASMCApiFrame), src->data,
  1630. src->num_elems * sizeof(WASMCApiFrame));
  1631. out->num_elems = src->num_elems;
  1632. }
  1633. static wasm_trap_t *
  1634. wasm_trap_new_internal(wasm_store_t *store,
  1635. WASMModuleInstanceCommon *inst_comm_rt,
  1636. const char *error_info, Vector *cluster_frames)
  1637. {
  1638. wasm_trap_t *trap;
  1639. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1640. wasm_instance_vec_t *instances;
  1641. wasm_instance_t *frame_instance = NULL;
  1642. uint32 i;
  1643. #endif
  1644. if (!singleton_engine)
  1645. return NULL;
  1646. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1647. return NULL;
  1648. }
  1649. /* fill in message */
  1650. if (error_info && strlen(error_info) > 0) {
  1651. if (!(trap->message = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1652. goto failed;
  1653. }
  1654. wasm_name_new_from_string_nt(trap->message, error_info);
  1655. if (!trap->message->data) {
  1656. goto failed;
  1657. }
  1658. }
  1659. /* fill in frames */
  1660. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1661. trap->frames = cluster_frames
  1662. ? cluster_frames
  1663. : ((WASMModuleInstance *)inst_comm_rt)->frames;
  1664. if (trap->frames) {
  1665. /* fill in instances */
  1666. instances = store->instances;
  1667. bh_assert(instances != NULL);
  1668. for (i = 0; i < instances->num_elems; i++) {
  1669. if (instances->data[i]->inst_comm_rt == inst_comm_rt) {
  1670. frame_instance = instances->data[i];
  1671. break;
  1672. }
  1673. }
  1674. for (i = 0; i < trap->frames->num_elems; i++) {
  1675. (((wasm_frame_t *)trap->frames->data) + i)->instance =
  1676. frame_instance;
  1677. }
  1678. }
  1679. #else
  1680. (void)store;
  1681. (void)inst_comm_rt;
  1682. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1683. return trap;
  1684. failed:
  1685. wasm_trap_delete(trap);
  1686. return NULL;
  1687. }
  1688. wasm_trap_t *
  1689. wasm_trap_new(wasm_store_t *store, const wasm_message_t *message)
  1690. {
  1691. wasm_trap_t *trap;
  1692. if (!store) {
  1693. return NULL;
  1694. }
  1695. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1696. return NULL;
  1697. }
  1698. if (message) {
  1699. INIT_VEC(trap->message, wasm_byte_vec_new, message->size,
  1700. message->data);
  1701. }
  1702. return trap;
  1703. failed:
  1704. wasm_trap_delete(trap);
  1705. return NULL;
  1706. }
  1707. void
  1708. wasm_trap_delete(wasm_trap_t *trap)
  1709. {
  1710. if (!trap) {
  1711. return;
  1712. }
  1713. DEINIT_VEC(trap->message, wasm_byte_vec_delete);
  1714. /* reuse frames of WASMModuleInstance, do not free it here */
  1715. wasm_runtime_free(trap);
  1716. }
  1717. void
  1718. wasm_trap_message(const wasm_trap_t *trap, own wasm_message_t *out)
  1719. {
  1720. if (!trap || !out) {
  1721. return;
  1722. }
  1723. wasm_byte_vec_copy(out, trap->message);
  1724. }
  1725. own wasm_frame_t *
  1726. wasm_trap_origin(const wasm_trap_t *trap)
  1727. {
  1728. wasm_frame_t *latest_frame;
  1729. if (!trap || !trap->frames || !trap->frames->num_elems) {
  1730. return NULL;
  1731. }
  1732. /* first frame is the latest frame */
  1733. latest_frame = (wasm_frame_t *)trap->frames->data;
  1734. return wasm_frame_copy(latest_frame);
  1735. }
  1736. void
  1737. wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out)
  1738. {
  1739. uint32 i;
  1740. if (!trap || !out) {
  1741. return;
  1742. }
  1743. if (!trap->frames || !trap->frames->num_elems) {
  1744. wasm_frame_vec_new_empty(out);
  1745. return;
  1746. }
  1747. wasm_frame_vec_new_uninitialized(out, trap->frames->num_elems);
  1748. if (out->size == 0 || !out->data) {
  1749. return;
  1750. }
  1751. for (i = 0; i < trap->frames->num_elems; i++) {
  1752. wasm_frame_t *frame = ((wasm_frame_t *)trap->frames->data) + i;
  1753. if (!(out->data[i] =
  1754. wasm_frame_new(frame->instance, frame->module_offset,
  1755. frame->func_index, frame->func_offset))) {
  1756. goto failed;
  1757. }
  1758. out->num_elems++;
  1759. }
  1760. return;
  1761. failed:
  1762. for (i = 0; i < out->num_elems; i++) {
  1763. if (out->data[i]) {
  1764. wasm_runtime_free(out->data[i]);
  1765. }
  1766. }
  1767. wasm_runtime_free(out->data);
  1768. }
  1769. wasm_foreign_t *
  1770. wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
  1771. WASMModuleInstanceCommon *inst_comm_rt)
  1772. {
  1773. wasm_foreign_t *foreign = NULL;
  1774. if (!store || !store->foreigns)
  1775. return NULL;
  1776. if (!(bh_vector_get(store->foreigns, foreign_idx_rt, &foreign))
  1777. || !foreign) {
  1778. return NULL;
  1779. }
  1780. foreign->ref_cnt++;
  1781. (void)inst_comm_rt;
  1782. return foreign;
  1783. }
  1784. own wasm_foreign_t *
  1785. wasm_foreign_new(wasm_store_t *store)
  1786. {
  1787. wasm_foreign_t *foreign;
  1788. if (!store)
  1789. return NULL;
  1790. if (!(foreign = malloc_internal(sizeof(wasm_foreign_t))))
  1791. return NULL;
  1792. foreign->store = store;
  1793. foreign->kind = WASM_REF_foreign;
  1794. foreign->foreign_idx_rt = (uint32)bh_vector_size(store->foreigns);
  1795. if (!(bh_vector_append(store->foreigns, &foreign))) {
  1796. wasm_runtime_free(foreign);
  1797. return NULL;
  1798. }
  1799. return foreign;
  1800. }
  1801. void
  1802. wasm_foreign_delete(wasm_foreign_t *foreign)
  1803. {
  1804. if (!foreign)
  1805. return;
  1806. if (foreign->ref_cnt < 1) {
  1807. return;
  1808. }
  1809. foreign->ref_cnt--;
  1810. if (!foreign->ref_cnt) {
  1811. wasm_runtime_free(foreign);
  1812. }
  1813. }
  1814. static inline wasm_module_t *
  1815. module_ext_to_module(wasm_module_ex_t *module_ex)
  1816. {
  1817. return (wasm_module_t *)module_ex;
  1818. }
  1819. static inline wasm_module_ex_t *
  1820. module_to_module_ext(wasm_module_t *module)
  1821. {
  1822. return (wasm_module_ex_t *)module;
  1823. }
  1824. #if WASM_ENABLE_INTERP != 0
  1825. #define MODULE_INTERP(module_comm) ((WASMModule *)(*module_comm))
  1826. #endif
  1827. #if WASM_ENABLE_AOT != 0
  1828. #define MODULE_AOT(module_comm) ((AOTModule *)(*module_comm))
  1829. #endif
  1830. #if WASM_ENABLE_WASM_CACHE != 0
  1831. static wasm_module_ex_t *
  1832. check_loaded_module(Vector *modules, char *binary_hash)
  1833. {
  1834. unsigned i;
  1835. wasm_module_ex_t *module = NULL;
  1836. for (i = 0; i < modules->num_elems; i++) {
  1837. bh_vector_get(modules, i, &module);
  1838. if (!module) {
  1839. LOG_ERROR("Unexpected failure at %d\n", __LINE__);
  1840. return NULL;
  1841. }
  1842. if (!module->ref_count)
  1843. /* deleted */
  1844. continue;
  1845. if (memcmp(module->hash, binary_hash, SHA256_DIGEST_LENGTH) == 0)
  1846. return module;
  1847. }
  1848. return NULL;
  1849. }
  1850. static wasm_module_ex_t *
  1851. try_reuse_loaded_module(wasm_store_t *store, char *binary_hash)
  1852. {
  1853. wasm_module_ex_t *cached = NULL;
  1854. wasm_module_ex_t *ret = NULL;
  1855. cached = check_loaded_module(&singleton_engine->modules, binary_hash);
  1856. if (!cached)
  1857. goto quit;
  1858. os_mutex_lock(&cached->lock);
  1859. if (!cached->ref_count)
  1860. goto unlock;
  1861. if (!bh_vector_append((Vector *)store->modules, &cached))
  1862. goto unlock;
  1863. cached->ref_count += 1;
  1864. ret = cached;
  1865. unlock:
  1866. os_mutex_unlock(&cached->lock);
  1867. quit:
  1868. return ret;
  1869. }
  1870. #endif /* WASM_ENABLE_WASM_CACHE != 0 */
  1871. wasm_module_t *
  1872. wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1873. {
  1874. char error_buf[128] = { 0 };
  1875. wasm_module_ex_t *module_ex = NULL;
  1876. #if WASM_ENABLE_WASM_CACHE != 0
  1877. char binary_hash[SHA256_DIGEST_LENGTH] = { 0 };
  1878. #endif
  1879. bh_assert(singleton_engine);
  1880. if (!store || !binary || binary->size == 0 || binary->size > UINT32_MAX)
  1881. goto quit;
  1882. /* whether the combination of compilation flags are compatable with the
  1883. * package type */
  1884. {
  1885. PackageType pkg_type;
  1886. pkg_type =
  1887. get_package_type((uint8 *)binary->data, (uint32)binary->size);
  1888. bool result = false;
  1889. #if WASM_ENABLE_INTERP != 0
  1890. result = (pkg_type == Wasm_Module_Bytecode);
  1891. #endif
  1892. #if WASM_ENABLE_AOT != 0
  1893. result = result || (pkg_type == Wasm_Module_AoT);
  1894. #endif
  1895. if (!result) {
  1896. LOG_VERBOSE("current building isn't compatiable with the module,"
  1897. "may need recompile");
  1898. goto quit;
  1899. }
  1900. }
  1901. #if WASM_ENABLE_WASM_CACHE != 0
  1902. /* if cached */
  1903. SHA256((void *)binary->data, binary->num_elems, (uint8_t *)binary_hash);
  1904. module_ex = try_reuse_loaded_module(store, binary_hash);
  1905. if (module_ex)
  1906. return module_ext_to_module(module_ex);
  1907. #endif
  1908. WASM_C_DUMP_PROC_MEM();
  1909. module_ex = malloc_internal(sizeof(wasm_module_ex_t));
  1910. if (!module_ex)
  1911. goto quit;
  1912. module_ex->binary = malloc_internal(sizeof(wasm_byte_vec_t));
  1913. if (!module_ex->binary)
  1914. goto free_module;
  1915. wasm_byte_vec_copy(module_ex->binary, binary);
  1916. if (!module_ex->binary->data)
  1917. goto free_binary;
  1918. module_ex->module_comm_rt = wasm_runtime_load(
  1919. (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size,
  1920. error_buf, (uint32)sizeof(error_buf));
  1921. if (!(module_ex->module_comm_rt)) {
  1922. LOG_ERROR(error_buf);
  1923. goto free_vec;
  1924. }
  1925. /* append it to a watching list in store */
  1926. if (!bh_vector_append((Vector *)store->modules, &module_ex))
  1927. goto unload;
  1928. if (os_mutex_init(&module_ex->lock) != BHT_OK)
  1929. goto remove_last;
  1930. if (!bh_vector_append(&singleton_engine->modules, &module_ex))
  1931. goto destroy_lock;
  1932. #if WASM_ENABLE_WASM_CACHE != 0
  1933. bh_memcpy_s(module_ex->hash, sizeof(module_ex->hash), binary_hash,
  1934. sizeof(binary_hash));
  1935. #endif
  1936. module_ex->ref_count = 1;
  1937. WASM_C_DUMP_PROC_MEM();
  1938. return module_ext_to_module(module_ex);
  1939. destroy_lock:
  1940. os_mutex_destroy(&module_ex->lock);
  1941. remove_last:
  1942. bh_vector_remove((Vector *)store->modules,
  1943. (uint32)(store->modules->num_elems - 1), NULL);
  1944. unload:
  1945. wasm_runtime_unload(module_ex->module_comm_rt);
  1946. free_vec:
  1947. wasm_byte_vec_delete(module_ex->binary);
  1948. free_binary:
  1949. wasm_runtime_free(module_ex->binary);
  1950. free_module:
  1951. wasm_runtime_free(module_ex);
  1952. quit:
  1953. LOG_ERROR("%s failed", __FUNCTION__);
  1954. return NULL;
  1955. }
  1956. bool
  1957. wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1958. {
  1959. wasm_byte_vec_t local_binary = { 0 };
  1960. struct WASMModuleCommon *module_rt;
  1961. char error_buf[128] = { 0 };
  1962. bool ret;
  1963. bh_assert(singleton_engine);
  1964. if (!store || !binary || binary->size > UINT32_MAX) {
  1965. LOG_ERROR("%s failed", __FUNCTION__);
  1966. return false;
  1967. }
  1968. /* make a copy of binary */
  1969. wasm_byte_vec_copy(&local_binary, binary);
  1970. if (binary->size && !local_binary.data)
  1971. return false;
  1972. module_rt = wasm_runtime_load((uint8 *)local_binary.data,
  1973. (uint32)local_binary.size, error_buf, 128);
  1974. wasm_byte_vec_delete(&local_binary);
  1975. if (module_rt) {
  1976. wasm_runtime_unload(module_rt);
  1977. ret = true;
  1978. }
  1979. else {
  1980. ret = false;
  1981. LOG_VERBOSE(error_buf);
  1982. }
  1983. return ret;
  1984. }
  1985. static void
  1986. wasm_module_delete_internal(wasm_module_t *module)
  1987. {
  1988. wasm_module_ex_t *module_ex;
  1989. if (!module) {
  1990. return;
  1991. }
  1992. module_ex = module_to_module_ext(module);
  1993. os_mutex_lock(&module_ex->lock);
  1994. /* N -> N-1 -> 0 -> UINT32_MAX */
  1995. module_ex->ref_count--;
  1996. if (module_ex->ref_count > 0) {
  1997. os_mutex_unlock(&module_ex->lock);
  1998. return;
  1999. }
  2000. DEINIT_VEC(module_ex->binary, wasm_byte_vec_delete);
  2001. if (module_ex->module_comm_rt) {
  2002. wasm_runtime_unload(module_ex->module_comm_rt);
  2003. module_ex->module_comm_rt = NULL;
  2004. }
  2005. #if WASM_ENABLE_WASM_CACHE != 0
  2006. memset(module_ex->hash, 0, sizeof(module_ex->hash));
  2007. #endif
  2008. os_mutex_unlock(&module_ex->lock);
  2009. }
  2010. void
  2011. wasm_module_delete(wasm_module_t *module)
  2012. {
  2013. /* the module will be released when releasing the store */
  2014. (void)module;
  2015. }
  2016. void
  2017. wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
  2018. {
  2019. uint32 i, import_func_count = 0, import_memory_count = 0,
  2020. import_global_count = 0, import_table_count = 0, import_count = 0;
  2021. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  2022. wasm_externtype_t *extern_type = NULL;
  2023. wasm_importtype_t *import_type = NULL;
  2024. if (!module || !out) {
  2025. return;
  2026. }
  2027. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2028. return;
  2029. #if WASM_ENABLE_INTERP != 0
  2030. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2031. import_func_count = MODULE_INTERP(module)->import_function_count;
  2032. import_global_count = MODULE_INTERP(module)->import_global_count;
  2033. import_memory_count = MODULE_INTERP(module)->import_memory_count;
  2034. import_table_count = MODULE_INTERP(module)->import_table_count;
  2035. }
  2036. #endif
  2037. #if WASM_ENABLE_AOT != 0
  2038. if ((*module)->module_type == Wasm_Module_AoT) {
  2039. import_func_count = MODULE_AOT(module)->import_func_count;
  2040. import_global_count = MODULE_AOT(module)->import_global_count;
  2041. import_memory_count = MODULE_AOT(module)->import_memory_count;
  2042. import_table_count = MODULE_AOT(module)->import_table_count;
  2043. }
  2044. #endif
  2045. import_count = import_func_count + import_global_count + import_table_count
  2046. + import_memory_count;
  2047. wasm_importtype_vec_new_uninitialized(out, import_count);
  2048. /*
  2049. * a wrong combination of module filetype and compilation flags
  2050. * also leads to below branch
  2051. */
  2052. if (!out->data) {
  2053. return;
  2054. }
  2055. for (i = 0; i != import_count; ++i) {
  2056. char *module_name_rt = NULL, *field_name_rt = NULL;
  2057. memset(&module_name, 0, sizeof(wasm_val_vec_t));
  2058. memset(&name, 0, sizeof(wasm_val_vec_t));
  2059. extern_type = NULL;
  2060. import_type = NULL;
  2061. if (i < import_func_count) {
  2062. wasm_functype_t *type = NULL;
  2063. WASMType *type_rt = NULL;
  2064. #if WASM_ENABLE_INTERP != 0
  2065. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2066. WASMImport *import =
  2067. MODULE_INTERP(module)->import_functions + i;
  2068. module_name_rt = import->u.names.module_name;
  2069. field_name_rt = import->u.names.field_name;
  2070. type_rt = import->u.function.func_type;
  2071. }
  2072. #endif
  2073. #if WASM_ENABLE_AOT != 0
  2074. if ((*module)->module_type == Wasm_Module_AoT) {
  2075. AOTImportFunc *import = MODULE_AOT(module)->import_funcs + i;
  2076. module_name_rt = import->module_name;
  2077. field_name_rt = import->func_name;
  2078. type_rt = import->func_type;
  2079. }
  2080. #endif
  2081. if (!module_name_rt || !field_name_rt || !type_rt) {
  2082. continue;
  2083. }
  2084. if (!(type = wasm_functype_new_internal(type_rt))) {
  2085. goto failed;
  2086. }
  2087. extern_type = wasm_functype_as_externtype(type);
  2088. }
  2089. else if (i < import_func_count + import_global_count) {
  2090. wasm_globaltype_t *type = NULL;
  2091. uint8 val_type_rt = 0;
  2092. bool mutability_rt = 0;
  2093. #if WASM_ENABLE_INTERP != 0
  2094. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2095. WASMImport *import = MODULE_INTERP(module)->import_globals
  2096. + (i - import_func_count);
  2097. module_name_rt = import->u.names.module_name;
  2098. field_name_rt = import->u.names.field_name;
  2099. val_type_rt = import->u.global.type;
  2100. mutability_rt = import->u.global.is_mutable;
  2101. }
  2102. #endif
  2103. #if WASM_ENABLE_AOT != 0
  2104. if ((*module)->module_type == Wasm_Module_AoT) {
  2105. AOTImportGlobal *import = MODULE_AOT(module)->import_globals
  2106. + (i - import_func_count);
  2107. module_name_rt = import->module_name;
  2108. field_name_rt = import->global_name;
  2109. val_type_rt = import->type;
  2110. mutability_rt = import->is_mutable;
  2111. }
  2112. #endif
  2113. if (!module_name_rt || !field_name_rt) {
  2114. continue;
  2115. }
  2116. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2117. mutability_rt))) {
  2118. goto failed;
  2119. }
  2120. extern_type = wasm_globaltype_as_externtype(type);
  2121. }
  2122. else if (i < import_func_count + import_global_count
  2123. + import_memory_count) {
  2124. wasm_memorytype_t *type = NULL;
  2125. uint32 min_page = 0, max_page = 0;
  2126. #if WASM_ENABLE_INTERP != 0
  2127. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2128. WASMImport *import =
  2129. MODULE_INTERP(module)->import_memories
  2130. + (i - import_func_count - import_global_count);
  2131. module_name_rt = import->u.names.module_name;
  2132. field_name_rt = import->u.names.field_name;
  2133. min_page = import->u.memory.init_page_count;
  2134. max_page = import->u.memory.max_page_count;
  2135. }
  2136. #endif
  2137. #if WASM_ENABLE_AOT != 0
  2138. if ((*module)->module_type == Wasm_Module_AoT) {
  2139. AOTImportMemory *import =
  2140. MODULE_AOT(module)->import_memories
  2141. + (i - import_func_count - import_global_count);
  2142. module_name_rt = import->module_name;
  2143. field_name_rt = import->memory_name;
  2144. min_page = import->mem_init_page_count;
  2145. max_page = import->mem_max_page_count;
  2146. }
  2147. #endif
  2148. if (!module_name_rt || !field_name_rt) {
  2149. continue;
  2150. }
  2151. if (!(type = wasm_memorytype_new_internal(min_page, max_page))) {
  2152. goto failed;
  2153. }
  2154. extern_type = wasm_memorytype_as_externtype(type);
  2155. }
  2156. else {
  2157. wasm_tabletype_t *type = NULL;
  2158. uint8 elem_type_rt = 0;
  2159. uint32 min_size = 0, max_size = 0;
  2160. #if WASM_ENABLE_INTERP != 0
  2161. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2162. WASMImport *import =
  2163. MODULE_INTERP(module)->import_tables
  2164. + (i - import_func_count - import_global_count
  2165. - import_memory_count);
  2166. module_name_rt = import->u.names.module_name;
  2167. field_name_rt = import->u.names.field_name;
  2168. elem_type_rt = import->u.table.elem_type;
  2169. min_size = import->u.table.init_size;
  2170. max_size = import->u.table.max_size;
  2171. }
  2172. #endif
  2173. #if WASM_ENABLE_AOT != 0
  2174. if ((*module)->module_type == Wasm_Module_AoT) {
  2175. AOTImportTable *import =
  2176. MODULE_AOT(module)->import_tables
  2177. + (i - import_func_count - import_global_count
  2178. - import_memory_count);
  2179. module_name_rt = import->module_name;
  2180. field_name_rt = import->table_name;
  2181. elem_type_rt = import->elem_type;
  2182. min_size = import->table_init_size;
  2183. max_size = import->table_max_size;
  2184. }
  2185. #endif
  2186. if (!module_name_rt || !field_name_rt) {
  2187. continue;
  2188. }
  2189. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2190. max_size))) {
  2191. goto failed;
  2192. }
  2193. extern_type = wasm_tabletype_as_externtype(type);
  2194. }
  2195. bh_assert(extern_type);
  2196. wasm_name_new_from_string_nt(&module_name, module_name_rt);
  2197. if (strlen(module_name_rt) && !module_name.data) {
  2198. goto failed;
  2199. }
  2200. wasm_name_new_from_string_nt(&name, field_name_rt);
  2201. if (strlen(field_name_rt) && !name.data) {
  2202. goto failed;
  2203. }
  2204. if (!(import_type =
  2205. wasm_importtype_new(&module_name, &name, extern_type))) {
  2206. goto failed;
  2207. }
  2208. if (!bh_vector_append((Vector *)out, &import_type)) {
  2209. goto failed_importtype_new;
  2210. }
  2211. continue;
  2212. failed:
  2213. wasm_byte_vec_delete(&module_name);
  2214. wasm_byte_vec_delete(&name);
  2215. wasm_externtype_delete(extern_type);
  2216. failed_importtype_new:
  2217. wasm_importtype_delete(import_type);
  2218. }
  2219. }
  2220. void
  2221. wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
  2222. {
  2223. uint32 i, export_count = 0;
  2224. wasm_byte_vec_t name = { 0 };
  2225. wasm_externtype_t *extern_type = NULL;
  2226. wasm_exporttype_t *export_type = NULL;
  2227. if (!module || !out) {
  2228. return;
  2229. }
  2230. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2231. return;
  2232. #if WASM_ENABLE_INTERP != 0
  2233. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2234. export_count = MODULE_INTERP(module)->export_count;
  2235. }
  2236. #endif
  2237. #if WASM_ENABLE_AOT != 0
  2238. if ((*module)->module_type == Wasm_Module_AoT) {
  2239. export_count = MODULE_AOT(module)->export_count;
  2240. }
  2241. #endif
  2242. wasm_exporttype_vec_new_uninitialized(out, export_count);
  2243. /*
  2244. * a wrong combination of module filetype and compilation flags
  2245. * also leads to below branch
  2246. */
  2247. if (!out->data) {
  2248. return;
  2249. }
  2250. for (i = 0; i != export_count; i++) {
  2251. WASMExport *export = NULL;
  2252. #if WASM_ENABLE_INTERP != 0
  2253. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2254. export = MODULE_INTERP(module)->exports + i;
  2255. }
  2256. #endif
  2257. #if WASM_ENABLE_AOT != 0
  2258. if ((*module)->module_type == Wasm_Module_AoT) {
  2259. export = MODULE_AOT(module)->exports + i;
  2260. }
  2261. #endif
  2262. if (!export) {
  2263. continue;
  2264. }
  2265. /* byte* -> wasm_byte_vec_t */
  2266. wasm_name_new_from_string_nt(&name, export->name);
  2267. if (strlen(export->name) && !name.data) {
  2268. goto failed;
  2269. }
  2270. /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t,
  2271. * wasm_globaltype_t) -> wasm_externtype_t*/
  2272. switch (export->kind) {
  2273. case EXPORT_KIND_FUNC:
  2274. {
  2275. wasm_functype_t *type = NULL;
  2276. WASMType *type_rt;
  2277. if (!wasm_runtime_get_export_func_type(*module, export,
  2278. &type_rt)) {
  2279. goto failed;
  2280. }
  2281. if (!(type = wasm_functype_new_internal(type_rt))) {
  2282. goto failed;
  2283. }
  2284. extern_type = wasm_functype_as_externtype(type);
  2285. break;
  2286. }
  2287. case EXPORT_KIND_GLOBAL:
  2288. {
  2289. wasm_globaltype_t *type = NULL;
  2290. uint8 val_type_rt = 0;
  2291. bool mutability_rt = 0;
  2292. if (!wasm_runtime_get_export_global_type(
  2293. *module, export, &val_type_rt, &mutability_rt)) {
  2294. goto failed;
  2295. }
  2296. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2297. mutability_rt))) {
  2298. goto failed;
  2299. }
  2300. extern_type = wasm_globaltype_as_externtype(type);
  2301. break;
  2302. }
  2303. case EXPORT_KIND_MEMORY:
  2304. {
  2305. wasm_memorytype_t *type = NULL;
  2306. uint32 min_page = 0, max_page = 0;
  2307. if (!wasm_runtime_get_export_memory_type(
  2308. *module, export, &min_page, &max_page)) {
  2309. goto failed;
  2310. }
  2311. if (!(type =
  2312. wasm_memorytype_new_internal(min_page, max_page))) {
  2313. goto failed;
  2314. }
  2315. extern_type = wasm_memorytype_as_externtype(type);
  2316. break;
  2317. }
  2318. case EXPORT_KIND_TABLE:
  2319. {
  2320. wasm_tabletype_t *type = NULL;
  2321. uint8 elem_type_rt = 0;
  2322. uint32 min_size = 0, max_size = 0;
  2323. if (!wasm_runtime_get_export_table_type(
  2324. *module, export, &elem_type_rt, &min_size, &max_size)) {
  2325. goto failed;
  2326. }
  2327. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2328. max_size))) {
  2329. goto failed;
  2330. }
  2331. extern_type = wasm_tabletype_as_externtype(type);
  2332. break;
  2333. }
  2334. default:
  2335. {
  2336. LOG_WARNING("%s meets unsupported type %u", __FUNCTION__,
  2337. export->kind);
  2338. break;
  2339. }
  2340. }
  2341. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  2342. goto failed;
  2343. }
  2344. if (!(bh_vector_append((Vector *)out, &export_type))) {
  2345. goto failed_exporttype_new;
  2346. }
  2347. }
  2348. return;
  2349. failed:
  2350. wasm_byte_vec_delete(&name);
  2351. wasm_externtype_delete(extern_type);
  2352. failed_exporttype_new:
  2353. wasm_exporttype_delete(export_type);
  2354. wasm_exporttype_vec_delete(out);
  2355. }
  2356. #if WASM_ENABLE_JIT == 0 || WASM_ENABLE_LAZY_JIT != 0
  2357. void
  2358. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2359. {
  2360. (void)module;
  2361. (void)out;
  2362. LOG_ERROR("only supported serialization in JIT with eager compilation");
  2363. }
  2364. own wasm_module_t *
  2365. wasm_module_deserialize(wasm_store_t *module, const wasm_byte_vec_t *binary)
  2366. {
  2367. (void)module;
  2368. (void)binary;
  2369. LOG_ERROR("only supported deserialization in JIT with eager compilation");
  2370. return NULL;
  2371. }
  2372. #else
  2373. extern uint8 *
  2374. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  2375. uint32 *p_aot_file_size);
  2376. void
  2377. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2378. {
  2379. wasm_module_ex_t *module_ex;
  2380. AOTCompContext *comp_ctx;
  2381. AOTCompData *comp_data;
  2382. uint8 *aot_file_buf = NULL;
  2383. uint32 aot_file_size = 0;
  2384. if (!module || !out)
  2385. return;
  2386. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2387. return;
  2388. module_ex = module_to_module_ext(module);
  2389. comp_ctx = ((WASMModule *)(module_ex->module_comm_rt))->comp_ctx;
  2390. comp_data = ((WASMModule *)(module_ex->module_comm_rt))->comp_data;
  2391. bh_assert(comp_ctx != NULL && comp_data != NULL);
  2392. aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size);
  2393. if (!aot_file_buf)
  2394. return;
  2395. wasm_byte_vec_new(out, aot_file_size, (wasm_byte_t *)aot_file_buf);
  2396. wasm_runtime_free(aot_file_buf);
  2397. return;
  2398. }
  2399. own wasm_module_t *
  2400. wasm_module_deserialize(wasm_store_t *store, const wasm_byte_vec_t *binary)
  2401. {
  2402. return wasm_module_new(store, binary);
  2403. }
  2404. #endif
  2405. wasm_module_t *
  2406. wasm_module_obtain(wasm_store_t *store, wasm_shared_module_t *shared_module)
  2407. {
  2408. wasm_module_ex_t *module_ex = NULL;
  2409. if (!store || !shared_module)
  2410. return NULL;
  2411. module_ex = (wasm_module_ex_t *)shared_module;
  2412. os_mutex_lock(&module_ex->lock);
  2413. /* deleting the module... */
  2414. if (module_ex->ref_count == 0) {
  2415. LOG_WARNING("wasm_module_obtain re-enter a module under deleting.");
  2416. os_mutex_unlock(&module_ex->lock);
  2417. return NULL;
  2418. }
  2419. /* add it to a watching list in store */
  2420. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  2421. os_mutex_unlock(&module_ex->lock);
  2422. return NULL;
  2423. }
  2424. module_ex->ref_count++;
  2425. os_mutex_unlock(&module_ex->lock);
  2426. return (wasm_module_t *)shared_module;
  2427. }
  2428. wasm_shared_module_t *
  2429. wasm_module_share(wasm_module_t *module)
  2430. {
  2431. wasm_module_ex_t *module_ex = NULL;
  2432. if (!module)
  2433. return NULL;
  2434. module_ex = (wasm_module_ex_t *)module;
  2435. os_mutex_lock(&module_ex->lock);
  2436. /* deleting the module... */
  2437. if (module_ex->ref_count == 0) {
  2438. LOG_WARNING("wasm_module_share re-enter a module under deleting.");
  2439. os_mutex_unlock(&module_ex->lock);
  2440. return NULL;
  2441. }
  2442. module_ex->ref_count++;
  2443. os_mutex_unlock(&module_ex->lock);
  2444. return (wasm_shared_module_t *)module;
  2445. }
  2446. void
  2447. wasm_shared_module_delete(own wasm_shared_module_t *shared_module)
  2448. {
  2449. wasm_module_delete_internal((wasm_module_t *)shared_module);
  2450. }
  2451. static wasm_func_t *
  2452. wasm_func_new_basic(wasm_store_t *store, const wasm_functype_t *type,
  2453. wasm_func_callback_t func_callback)
  2454. {
  2455. wasm_func_t *func = NULL;
  2456. if (!type) {
  2457. goto failed;
  2458. }
  2459. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2460. goto failed;
  2461. }
  2462. func->store = store;
  2463. func->kind = WASM_EXTERN_FUNC;
  2464. func->func_idx_rt = (uint16)-1;
  2465. func->with_env = false;
  2466. func->u.cb = func_callback;
  2467. if (!(func->type = wasm_functype_copy(type))) {
  2468. goto failed;
  2469. }
  2470. func->param_count = func->type->params->num_elems;
  2471. func->result_count = func->type->results->num_elems;
  2472. RETURN_OBJ(func, wasm_func_delete)
  2473. }
  2474. static wasm_func_t *
  2475. wasm_func_new_with_env_basic(wasm_store_t *store, const wasm_functype_t *type,
  2476. wasm_func_callback_with_env_t callback, void *env,
  2477. void (*finalizer)(void *))
  2478. {
  2479. wasm_func_t *func = NULL;
  2480. if (!type) {
  2481. goto failed;
  2482. }
  2483. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2484. goto failed;
  2485. }
  2486. func->store = store;
  2487. func->kind = WASM_EXTERN_FUNC;
  2488. func->func_idx_rt = (uint16)-1;
  2489. func->with_env = true;
  2490. func->u.cb_env.cb = callback;
  2491. func->u.cb_env.env = env;
  2492. func->u.cb_env.finalizer = finalizer;
  2493. if (!(func->type = wasm_functype_copy(type))) {
  2494. goto failed;
  2495. }
  2496. func->param_count = func->type->params->num_elems;
  2497. func->result_count = func->type->results->num_elems;
  2498. RETURN_OBJ(func, wasm_func_delete)
  2499. }
  2500. wasm_func_t *
  2501. wasm_func_new(wasm_store_t *store, const wasm_functype_t *type,
  2502. wasm_func_callback_t callback)
  2503. {
  2504. bh_assert(singleton_engine);
  2505. if (!callback) {
  2506. return NULL;
  2507. }
  2508. return wasm_func_new_basic(store, type, callback);
  2509. }
  2510. wasm_func_t *
  2511. wasm_func_new_with_env(wasm_store_t *store, const wasm_functype_t *type,
  2512. wasm_func_callback_with_env_t callback, void *env,
  2513. void (*finalizer)(void *))
  2514. {
  2515. bh_assert(singleton_engine);
  2516. if (!callback) {
  2517. return NULL;
  2518. }
  2519. return wasm_func_new_with_env_basic(store, type, callback, env, finalizer);
  2520. }
  2521. wasm_func_t *
  2522. wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
  2523. WASMModuleInstanceCommon *inst_comm_rt)
  2524. {
  2525. wasm_func_t *func = NULL;
  2526. WASMType *type_rt = NULL;
  2527. bh_assert(singleton_engine);
  2528. if (!inst_comm_rt) {
  2529. return NULL;
  2530. }
  2531. func = malloc_internal(sizeof(wasm_func_t));
  2532. if (!func) {
  2533. goto failed;
  2534. }
  2535. func->kind = WASM_EXTERN_FUNC;
  2536. #if WASM_ENABLE_INTERP != 0
  2537. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2538. bh_assert(func_idx_rt
  2539. < ((WASMModuleInstance *)inst_comm_rt)->e->function_count);
  2540. WASMFunctionInstance *func_interp =
  2541. ((WASMModuleInstance *)inst_comm_rt)->e->functions + func_idx_rt;
  2542. type_rt = func_interp->is_import_func
  2543. ? func_interp->u.func_import->func_type
  2544. : func_interp->u.func->func_type;
  2545. }
  2546. #endif
  2547. #if WASM_ENABLE_AOT != 0
  2548. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2549. /* use same index to trace the function type in AOTFuncType **func_types
  2550. */
  2551. AOTModule *module_aot =
  2552. (AOTModule *)((AOTModuleInstance *)inst_comm_rt)->module;
  2553. if (func_idx_rt < module_aot->import_func_count) {
  2554. type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
  2555. }
  2556. else {
  2557. type_rt =
  2558. module_aot->func_types[module_aot->func_type_indexes
  2559. [func_idx_rt
  2560. - module_aot->import_func_count]];
  2561. }
  2562. }
  2563. #endif
  2564. /*
  2565. * a wrong combination of module filetype and compilation flags
  2566. * also leads to below branch
  2567. */
  2568. if (!type_rt) {
  2569. goto failed;
  2570. }
  2571. func->type = wasm_functype_new_internal(type_rt);
  2572. if (!func->type) {
  2573. goto failed;
  2574. }
  2575. func->param_count = func->type->params->num_elems;
  2576. func->result_count = func->type->results->num_elems;
  2577. /* will add name information when processing "exports" */
  2578. func->store = store;
  2579. func->module_name = NULL;
  2580. func->name = NULL;
  2581. func->func_idx_rt = func_idx_rt;
  2582. func->inst_comm_rt = inst_comm_rt;
  2583. return func;
  2584. failed:
  2585. LOG_DEBUG("%s failed", __FUNCTION__);
  2586. wasm_func_delete(func);
  2587. return NULL;
  2588. }
  2589. static wasm_func_t *
  2590. wasm_func_new_empty(wasm_store_t *store)
  2591. {
  2592. wasm_func_t *func = NULL;
  2593. if (!(func = malloc_internal(sizeof(wasm_func_t))))
  2594. goto failed;
  2595. func->store = store;
  2596. func->kind = WASM_EXTERN_FUNC;
  2597. RETURN_OBJ(func, wasm_func_delete)
  2598. }
  2599. void
  2600. wasm_func_delete(wasm_func_t *func)
  2601. {
  2602. if (!func) {
  2603. return;
  2604. }
  2605. if (func->type) {
  2606. wasm_functype_delete(func->type);
  2607. func->type = NULL;
  2608. }
  2609. if (func->with_env) {
  2610. if (func->u.cb_env.finalizer) {
  2611. func->u.cb_env.finalizer(func->u.cb_env.env);
  2612. func->u.cb_env.finalizer = NULL;
  2613. func->u.cb_env.env = NULL;
  2614. }
  2615. }
  2616. DELETE_HOST_INFO(func)
  2617. wasm_runtime_free(func);
  2618. }
  2619. own wasm_func_t *
  2620. wasm_func_copy(const wasm_func_t *func)
  2621. {
  2622. wasm_func_t *cloned = NULL;
  2623. if (!func) {
  2624. return NULL;
  2625. }
  2626. if (!(cloned = func->with_env ? wasm_func_new_with_env_basic(
  2627. func->store, func->type, func->u.cb_env.cb,
  2628. func->u.cb_env.env, func->u.cb_env.finalizer)
  2629. : wasm_func_new_basic(func->store, func->type,
  2630. func->u.cb))) {
  2631. goto failed;
  2632. }
  2633. cloned->func_idx_rt = func->func_idx_rt;
  2634. cloned->inst_comm_rt = func->inst_comm_rt;
  2635. RETURN_OBJ(cloned, wasm_func_delete)
  2636. }
  2637. own wasm_functype_t *
  2638. wasm_func_type(const wasm_func_t *func)
  2639. {
  2640. if (!func) {
  2641. return NULL;
  2642. }
  2643. return wasm_functype_copy(func->type);
  2644. }
  2645. static bool
  2646. params_to_argv(const wasm_val_vec_t *params,
  2647. const wasm_valtype_vec_t *param_defs, uint32 *argv,
  2648. uint32 *ptr_argc)
  2649. {
  2650. uint32 *argv_org = argv;
  2651. const wasm_val_t *param, *param_end;
  2652. bh_assert(params && params->num_elems && params->size && params->data);
  2653. param = params->data;
  2654. param_end = param + param_defs->num_elems;
  2655. for (; param < param_end; param++) {
  2656. switch (param->kind) {
  2657. case WASM_I32:
  2658. case WASM_F32:
  2659. *(int32 *)argv = param->of.i32;
  2660. argv += 1;
  2661. break;
  2662. case WASM_I64:
  2663. case WASM_F64:
  2664. *(int64 *)argv = param->of.i64;
  2665. argv += 2;
  2666. break;
  2667. break;
  2668. #if WASM_ENABLE_REF_TYPES != 0
  2669. case WASM_ANYREF:
  2670. *(uintptr_t *)argv = (uintptr_t)param->of.ref;
  2671. argv += sizeof(uintptr_t) / sizeof(uint32);
  2672. break;
  2673. #endif
  2674. default:
  2675. LOG_WARNING("unexpected parameter val type %d", param->kind);
  2676. return false;
  2677. }
  2678. }
  2679. *ptr_argc = (uint32)(argv - argv_org);
  2680. return true;
  2681. }
  2682. static bool
  2683. argv_to_results(const uint32 *argv, const wasm_valtype_vec_t *result_defs,
  2684. wasm_val_vec_t *results)
  2685. {
  2686. wasm_valtype_t **result_def, **result_def_end;
  2687. wasm_val_t *result;
  2688. bh_assert(results && results->size && results->data);
  2689. result_def = result_defs->data;
  2690. result_def_end = result_def + result_defs->num_elems;
  2691. result = results->data;
  2692. for (; result_def < result_def_end; result_def++, result++) {
  2693. result->kind = result_def[0]->kind;
  2694. switch (result->kind) {
  2695. case WASM_I32:
  2696. case WASM_F32:
  2697. result->of.i32 = *(int32 *)argv;
  2698. argv += 1;
  2699. break;
  2700. case WASM_I64:
  2701. case WASM_F64:
  2702. result->of.i64 = *(int64 *)argv;
  2703. argv += 2;
  2704. break;
  2705. #if WASM_ENABLE_REF_TYPES != 0
  2706. case WASM_ANYREF:
  2707. result->of.ref = (struct wasm_ref_t *)(*(uintptr_t *)argv);
  2708. argv += sizeof(uintptr_t) / sizeof(uint32);
  2709. break;
  2710. #endif
  2711. default:
  2712. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  2713. result->kind);
  2714. return false;
  2715. }
  2716. }
  2717. return true;
  2718. }
  2719. wasm_trap_t *
  2720. wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
  2721. wasm_val_vec_t *results)
  2722. {
  2723. /* parameters count as if all are uint32 */
  2724. /* a int64 or float64 parameter means 2 */
  2725. uint32 argc = 0;
  2726. /* a parameter list and a return value list */
  2727. uint32 argv_buf[32] = { 0 }, *argv = argv_buf;
  2728. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  2729. WASMExecEnv *exec_env = NULL;
  2730. size_t param_count, result_count, alloc_count;
  2731. Vector *cluster_frames = NULL;
  2732. bh_assert(func && func->type);
  2733. if (!func->inst_comm_rt) {
  2734. wasm_name_t message = { 0 };
  2735. wasm_trap_t *trap;
  2736. wasm_name_new_from_string_nt(&message,
  2737. "failed to call unlinked function");
  2738. trap = wasm_trap_new(func->store, &message);
  2739. wasm_byte_vec_delete(&message);
  2740. return trap;
  2741. }
  2742. if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2743. #if WASM_ENABLE_INTERP != 0
  2744. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->e->functions
  2745. + func->func_idx_rt;
  2746. #endif
  2747. }
  2748. else if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2749. #if WASM_ENABLE_AOT != 0
  2750. if (!(func_comm_rt = func->func_comm_rt)) {
  2751. AOTModuleInstance *inst_aot =
  2752. (AOTModuleInstance *)func->inst_comm_rt;
  2753. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2754. uint32 export_i = 0, export_func_j = 0;
  2755. for (; export_i < module_aot->export_count; ++export_i) {
  2756. AOTExport *export = module_aot->exports + export_i;
  2757. if (export->kind == EXPORT_KIND_FUNC) {
  2758. if (export->index == func->func_idx_rt) {
  2759. func_comm_rt =
  2760. (AOTFunctionInstance *)inst_aot->export_functions
  2761. + export_func_j;
  2762. ((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
  2763. break;
  2764. }
  2765. export_func_j++;
  2766. }
  2767. }
  2768. }
  2769. #endif
  2770. }
  2771. /*
  2772. * a wrong combination of module filetype and compilation flags
  2773. * also leads to below branch
  2774. */
  2775. if (!func_comm_rt) {
  2776. goto failed;
  2777. }
  2778. param_count = wasm_func_param_arity(func);
  2779. result_count = wasm_func_result_arity(func);
  2780. alloc_count = (param_count > result_count) ? param_count : result_count;
  2781. if (alloc_count > (size_t)sizeof(argv_buf) / sizeof(uint64)) {
  2782. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  2783. goto failed;
  2784. }
  2785. }
  2786. /* copy parametes */
  2787. if (param_count
  2788. && !params_to_argv(params, wasm_functype_params(func->type), argv,
  2789. &argc)) {
  2790. goto failed;
  2791. }
  2792. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2793. exec_env = wasm_runtime_get_exec_env_tls();
  2794. #endif
  2795. #if WASM_ENABLE_THREAD_MGR != 0
  2796. if (!exec_env) {
  2797. exec_env = wasm_clusters_search_exec_env(func->inst_comm_rt);
  2798. }
  2799. #endif
  2800. if (!exec_env) {
  2801. exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
  2802. }
  2803. if (!exec_env) {
  2804. goto failed;
  2805. }
  2806. wasm_runtime_set_exception(func->inst_comm_rt, NULL);
  2807. if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
  2808. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2809. LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
  2810. goto failed;
  2811. }
  2812. }
  2813. /* copy results */
  2814. if (result_count) {
  2815. if (!argv_to_results(argv, wasm_functype_results(func->type),
  2816. results)) {
  2817. goto failed;
  2818. }
  2819. results->num_elems = result_count;
  2820. results->size = result_count;
  2821. }
  2822. if (argv != argv_buf)
  2823. wasm_runtime_free(argv);
  2824. return NULL;
  2825. failed:
  2826. if (argv != argv_buf)
  2827. wasm_runtime_free(argv);
  2828. #if WASM_ENABLE_DUMP_CALL_STACK != 0 && WASM_ENABLE_THREAD_MGR != 0
  2829. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  2830. cluster_frames = &cluster->exception_frames;
  2831. wasm_cluster_traverse_lock(exec_env);
  2832. #endif
  2833. wasm_trap_t *trap = wasm_trap_new_internal(
  2834. func->store, func->inst_comm_rt,
  2835. wasm_runtime_get_exception(func->inst_comm_rt), cluster_frames);
  2836. #if WASM_ENABLE_DUMP_CALL_STACK != 0 && WASM_ENABLE_THREAD_MGR != 0
  2837. wasm_cluster_traverse_unlock(exec_env);
  2838. #endif
  2839. return trap;
  2840. }
  2841. size_t
  2842. wasm_func_param_arity(const wasm_func_t *func)
  2843. {
  2844. return func->param_count;
  2845. }
  2846. size_t
  2847. wasm_func_result_arity(const wasm_func_t *func)
  2848. {
  2849. return func->result_count;
  2850. }
  2851. wasm_global_t *
  2852. wasm_global_new(wasm_store_t *store, const wasm_globaltype_t *global_type,
  2853. const wasm_val_t *init)
  2854. {
  2855. wasm_global_t *global = NULL;
  2856. bh_assert(singleton_engine);
  2857. if (!global_type || !init) {
  2858. goto failed;
  2859. }
  2860. global = malloc_internal(sizeof(wasm_global_t));
  2861. if (!global) {
  2862. goto failed;
  2863. }
  2864. global->store = store;
  2865. global->kind = WASM_EXTERN_GLOBAL;
  2866. global->type = wasm_globaltype_copy(global_type);
  2867. if (!global->type) {
  2868. goto failed;
  2869. }
  2870. global->init = malloc_internal(sizeof(wasm_val_t));
  2871. if (!global->init) {
  2872. goto failed;
  2873. }
  2874. wasm_val_copy(global->init, init);
  2875. /* TODO: how to check if above is failed */
  2876. return global;
  2877. failed:
  2878. LOG_DEBUG("%s failed", __FUNCTION__);
  2879. wasm_global_delete(global);
  2880. return NULL;
  2881. }
  2882. static wasm_global_t *
  2883. wasm_global_new_empty(wasm_store_t *store)
  2884. {
  2885. wasm_global_t *global = NULL;
  2886. global = malloc_internal(sizeof(wasm_global_t));
  2887. if (!global)
  2888. goto failed;
  2889. global->store = store;
  2890. global->kind = WASM_EXTERN_GLOBAL;
  2891. return global;
  2892. failed:
  2893. LOG_DEBUG("%s failed", __FUNCTION__);
  2894. wasm_global_delete(global);
  2895. return NULL;
  2896. }
  2897. /* almost same with wasm_global_new */
  2898. wasm_global_t *
  2899. wasm_global_copy(const wasm_global_t *src)
  2900. {
  2901. wasm_global_t *global = NULL;
  2902. if (!src) {
  2903. return NULL;
  2904. }
  2905. global = malloc_internal(sizeof(wasm_global_t));
  2906. if (!global) {
  2907. goto failed;
  2908. }
  2909. global->kind = WASM_EXTERN_GLOBAL;
  2910. global->type = wasm_globaltype_copy(src->type);
  2911. if (!global->type) {
  2912. goto failed;
  2913. }
  2914. global->init = malloc_internal(sizeof(wasm_val_t));
  2915. if (!global->init) {
  2916. goto failed;
  2917. }
  2918. wasm_val_copy(global->init, src->init);
  2919. global->global_idx_rt = src->global_idx_rt;
  2920. global->inst_comm_rt = src->inst_comm_rt;
  2921. return global;
  2922. failed:
  2923. LOG_DEBUG("%s failed", __FUNCTION__);
  2924. wasm_global_delete(global);
  2925. return NULL;
  2926. }
  2927. void
  2928. wasm_global_delete(wasm_global_t *global)
  2929. {
  2930. if (!global) {
  2931. return;
  2932. }
  2933. if (global->init) {
  2934. wasm_val_delete(global->init);
  2935. global->init = NULL;
  2936. }
  2937. if (global->type) {
  2938. wasm_globaltype_delete(global->type);
  2939. global->type = NULL;
  2940. }
  2941. DELETE_HOST_INFO(global)
  2942. wasm_runtime_free(global);
  2943. }
  2944. #if WASM_ENABLE_INTERP != 0
  2945. static bool
  2946. interp_global_set(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2947. const wasm_val_t *v)
  2948. {
  2949. const WASMGlobalInstance *global_interp =
  2950. inst_interp->e->globals + global_idx_rt;
  2951. uint8 val_type_rt = global_interp->type;
  2952. #if WASM_ENABLE_MULTI_MODULE != 0
  2953. uint8 *data = global_interp->import_global_inst
  2954. ? global_interp->import_module_inst->global_data
  2955. + global_interp->import_global_inst->data_offset
  2956. : inst_interp->global_data + global_interp->data_offset;
  2957. #else
  2958. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2959. #endif
  2960. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_interp,
  2961. val_type_rt, v, data);
  2962. }
  2963. static bool
  2964. interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2965. wasm_val_t *out)
  2966. {
  2967. WASMGlobalInstance *global_interp = inst_interp->e->globals + global_idx_rt;
  2968. uint8 val_type_rt = global_interp->type;
  2969. #if WASM_ENABLE_MULTI_MODULE != 0
  2970. uint8 *data = global_interp->import_global_inst
  2971. ? global_interp->import_module_inst->global_data
  2972. + global_interp->import_global_inst->data_offset
  2973. : inst_interp->global_data + global_interp->data_offset;
  2974. #else
  2975. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2976. #endif
  2977. return rt_val_to_wasm_val(data, val_type_rt, out);
  2978. }
  2979. #endif
  2980. #if WASM_ENABLE_AOT != 0
  2981. static bool
  2982. aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  2983. const wasm_val_t *v)
  2984. {
  2985. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2986. uint8 val_type_rt = 0;
  2987. uint32 data_offset = 0;
  2988. void *data = NULL;
  2989. if (global_idx_rt < module_aot->import_global_count) {
  2990. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2991. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2992. }
  2993. else {
  2994. data_offset =
  2995. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2996. .data_offset;
  2997. val_type_rt =
  2998. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2999. .type;
  3000. }
  3001. data = (void *)(inst_aot->global_data + data_offset);
  3002. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt,
  3003. v, data);
  3004. }
  3005. static bool
  3006. aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  3007. wasm_val_t *out)
  3008. {
  3009. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3010. uint8 val_type_rt = 0;
  3011. uint32 data_offset = 0;
  3012. uint8 *data = NULL;
  3013. if (global_idx_rt < module_aot->import_global_count) {
  3014. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  3015. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  3016. }
  3017. else {
  3018. data_offset =
  3019. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3020. .data_offset;
  3021. val_type_rt =
  3022. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3023. .type;
  3024. }
  3025. data = inst_aot->global_data + data_offset;
  3026. return rt_val_to_wasm_val(data, val_type_rt, out);
  3027. }
  3028. #endif
  3029. void
  3030. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  3031. {
  3032. if (!global || !v || !global->inst_comm_rt) {
  3033. return;
  3034. }
  3035. #if WASM_ENABLE_INTERP != 0
  3036. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3037. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  3038. global->global_idx_rt, v);
  3039. return;
  3040. }
  3041. #endif
  3042. #if WASM_ENABLE_AOT != 0
  3043. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3044. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  3045. global->global_idx_rt, v);
  3046. return;
  3047. }
  3048. #endif
  3049. /*
  3050. * a wrong combination of module filetype and compilation flags
  3051. * leads to below branch
  3052. */
  3053. UNREACHABLE();
  3054. }
  3055. void
  3056. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  3057. {
  3058. if (!global || !out) {
  3059. return;
  3060. }
  3061. if (!global->inst_comm_rt) {
  3062. return;
  3063. }
  3064. memset(out, 0, sizeof(wasm_val_t));
  3065. #if WASM_ENABLE_INTERP != 0
  3066. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3067. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  3068. global->global_idx_rt, out);
  3069. return;
  3070. }
  3071. #endif
  3072. #if WASM_ENABLE_AOT != 0
  3073. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3074. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  3075. global->global_idx_rt, out);
  3076. return;
  3077. }
  3078. #endif
  3079. /*
  3080. * a wrong combination of module filetype and compilation flags
  3081. * leads to below branch
  3082. */
  3083. UNREACHABLE();
  3084. }
  3085. wasm_global_t *
  3086. wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
  3087. WASMModuleInstanceCommon *inst_comm_rt)
  3088. {
  3089. wasm_global_t *global = NULL;
  3090. uint8 val_type_rt = 0;
  3091. bool is_mutable = 0;
  3092. bool init = false;
  3093. bh_assert(singleton_engine);
  3094. if (!inst_comm_rt) {
  3095. return NULL;
  3096. }
  3097. global = malloc_internal(sizeof(wasm_global_t));
  3098. if (!global) {
  3099. goto failed;
  3100. }
  3101. global->store = store;
  3102. global->kind = WASM_EXTERN_GLOBAL;
  3103. #if WASM_ENABLE_INTERP != 0
  3104. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3105. WASMGlobalInstance *global_interp =
  3106. ((WASMModuleInstance *)inst_comm_rt)->e->globals + global_idx_rt;
  3107. val_type_rt = global_interp->type;
  3108. is_mutable = global_interp->is_mutable;
  3109. init = true;
  3110. }
  3111. #endif
  3112. #if WASM_ENABLE_AOT != 0
  3113. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3114. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3115. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3116. init = true;
  3117. if (global_idx_rt < module_aot->import_global_count) {
  3118. AOTImportGlobal *global_import_aot =
  3119. module_aot->import_globals + global_idx_rt;
  3120. val_type_rt = global_import_aot->type;
  3121. is_mutable = global_import_aot->is_mutable;
  3122. }
  3123. else {
  3124. AOTGlobal *global_aot =
  3125. module_aot->globals
  3126. + (global_idx_rt - module_aot->import_global_count);
  3127. val_type_rt = global_aot->type;
  3128. is_mutable = global_aot->is_mutable;
  3129. }
  3130. }
  3131. #endif
  3132. /*
  3133. * a wrong combination of module filetype and compilation flags
  3134. * leads to below branch
  3135. */
  3136. if (!init) {
  3137. goto failed;
  3138. }
  3139. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  3140. if (!global->type) {
  3141. goto failed;
  3142. }
  3143. global->init = malloc_internal(sizeof(wasm_val_t));
  3144. if (!global->init) {
  3145. goto failed;
  3146. }
  3147. #if WASM_ENABLE_INTERP != 0
  3148. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3149. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  3150. global->init);
  3151. }
  3152. #endif
  3153. #if WASM_ENABLE_AOT != 0
  3154. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3155. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  3156. global->init);
  3157. }
  3158. #endif
  3159. global->inst_comm_rt = inst_comm_rt;
  3160. global->global_idx_rt = global_idx_rt;
  3161. return global;
  3162. failed:
  3163. LOG_DEBUG("%s failed", __FUNCTION__);
  3164. wasm_global_delete(global);
  3165. return NULL;
  3166. }
  3167. wasm_globaltype_t *
  3168. wasm_global_type(const wasm_global_t *global)
  3169. {
  3170. if (!global) {
  3171. return NULL;
  3172. }
  3173. return wasm_globaltype_copy(global->type);
  3174. }
  3175. static wasm_table_t *
  3176. wasm_table_new_basic(wasm_store_t *store, const wasm_tabletype_t *type)
  3177. {
  3178. wasm_table_t *table = NULL;
  3179. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3180. goto failed;
  3181. }
  3182. table->store = store;
  3183. table->kind = WASM_EXTERN_TABLE;
  3184. if (!(table->type = wasm_tabletype_copy(type))) {
  3185. goto failed;
  3186. }
  3187. RETURN_OBJ(table, wasm_table_delete);
  3188. }
  3189. wasm_table_t *
  3190. wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
  3191. WASMModuleInstanceCommon *inst_comm_rt)
  3192. {
  3193. wasm_table_t *table = NULL;
  3194. uint8 val_type_rt = 0;
  3195. uint32 init_size = 0, max_size = 0;
  3196. bh_assert(singleton_engine);
  3197. if (!inst_comm_rt) {
  3198. return NULL;
  3199. }
  3200. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3201. goto failed;
  3202. }
  3203. table->store = store;
  3204. table->kind = WASM_EXTERN_TABLE;
  3205. if (!wasm_runtime_get_table_inst_elem_type(
  3206. inst_comm_rt, table_idx_rt, &val_type_rt, &init_size, &max_size)) {
  3207. /*
  3208. * a wrong combination of module filetype and compilation flags
  3209. * leads to below branch
  3210. */
  3211. goto failed;
  3212. }
  3213. if (!(table->type =
  3214. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  3215. goto failed;
  3216. }
  3217. table->inst_comm_rt = inst_comm_rt;
  3218. table->table_idx_rt = table_idx_rt;
  3219. RETURN_OBJ(table, wasm_table_delete);
  3220. }
  3221. /* will not actually apply this new table into the runtime */
  3222. wasm_table_t *
  3223. wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *table_type,
  3224. wasm_ref_t *init)
  3225. {
  3226. wasm_table_t *table;
  3227. (void)init;
  3228. bh_assert(singleton_engine);
  3229. if ((table = wasm_table_new_basic(store, table_type))) {
  3230. table->store = store;
  3231. }
  3232. return table;
  3233. }
  3234. wasm_table_t *
  3235. wasm_table_copy(const wasm_table_t *src)
  3236. {
  3237. wasm_table_t *table;
  3238. if (!(table = wasm_table_new_basic(src->store, src->type))) {
  3239. return NULL;
  3240. }
  3241. table->table_idx_rt = src->table_idx_rt;
  3242. table->inst_comm_rt = src->inst_comm_rt;
  3243. return table;
  3244. }
  3245. void
  3246. wasm_table_delete(wasm_table_t *table)
  3247. {
  3248. if (!table) {
  3249. return;
  3250. }
  3251. if (table->type) {
  3252. wasm_tabletype_delete(table->type);
  3253. table->type = NULL;
  3254. }
  3255. DELETE_HOST_INFO(table)
  3256. wasm_runtime_free(table);
  3257. }
  3258. wasm_tabletype_t *
  3259. wasm_table_type(const wasm_table_t *table)
  3260. {
  3261. if (!table) {
  3262. return NULL;
  3263. }
  3264. return wasm_tabletype_copy(table->type);
  3265. }
  3266. own wasm_ref_t *
  3267. wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
  3268. {
  3269. uint32 ref_idx = NULL_REF;
  3270. if (!table || !table->inst_comm_rt) {
  3271. return NULL;
  3272. }
  3273. #if WASM_ENABLE_INTERP != 0
  3274. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3275. WASMTableInstance *table_interp =
  3276. ((WASMModuleInstance *)table->inst_comm_rt)
  3277. ->tables[table->table_idx_rt];
  3278. if (index >= table_interp->cur_size) {
  3279. return NULL;
  3280. }
  3281. ref_idx = table_interp->elems[index];
  3282. }
  3283. #endif
  3284. #if WASM_ENABLE_AOT != 0
  3285. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3286. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3287. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3288. if (index >= table_aot->cur_size) {
  3289. return NULL;
  3290. }
  3291. ref_idx = table_aot->elems[index];
  3292. }
  3293. #endif
  3294. /*
  3295. * a wrong combination of module filetype and compilation flags
  3296. * also leads to below branch
  3297. */
  3298. if (ref_idx == NULL_REF) {
  3299. return NULL;
  3300. }
  3301. #if WASM_ENABLE_REF_TYPES != 0
  3302. if (table->type->val_type->kind == WASM_ANYREF) {
  3303. void *externref_obj;
  3304. if (!wasm_externref_ref2obj(ref_idx, &externref_obj)) {
  3305. return NULL;
  3306. }
  3307. return externref_obj;
  3308. }
  3309. else
  3310. #endif
  3311. {
  3312. return wasm_ref_new_internal(table->store, WASM_REF_func, ref_idx,
  3313. table->inst_comm_rt);
  3314. }
  3315. }
  3316. bool
  3317. wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
  3318. own wasm_ref_t *ref)
  3319. {
  3320. uint32 *p_ref_idx = NULL;
  3321. uint32 function_count = 0;
  3322. if (!table || !table->inst_comm_rt) {
  3323. return false;
  3324. }
  3325. if (ref
  3326. #if WASM_ENABLE_REF_TYPES != 0
  3327. && !(WASM_REF_foreign == ref->kind
  3328. && WASM_ANYREF == table->type->val_type->kind)
  3329. #endif
  3330. && !(WASM_REF_func == ref->kind
  3331. && WASM_FUNCREF == table->type->val_type->kind)) {
  3332. return false;
  3333. }
  3334. #if WASM_ENABLE_INTERP != 0
  3335. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3336. WASMTableInstance *table_interp =
  3337. ((WASMModuleInstance *)table->inst_comm_rt)
  3338. ->tables[table->table_idx_rt];
  3339. if (index >= table_interp->cur_size) {
  3340. return false;
  3341. }
  3342. p_ref_idx = table_interp->elems + index;
  3343. function_count =
  3344. ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
  3345. }
  3346. #endif
  3347. #if WASM_ENABLE_AOT != 0
  3348. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3349. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3350. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3351. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3352. if (index >= table_aot->cur_size) {
  3353. return false;
  3354. }
  3355. p_ref_idx = table_aot->elems + index;
  3356. function_count = module_aot->func_count;
  3357. }
  3358. #endif
  3359. /*
  3360. * a wrong combination of module filetype and compilation flags
  3361. * leads to below branch
  3362. */
  3363. if (!p_ref_idx) {
  3364. return false;
  3365. }
  3366. #if WASM_ENABLE_REF_TYPES != 0
  3367. if (table->type->val_type->kind == WASM_ANYREF) {
  3368. return wasm_externref_obj2ref(table->inst_comm_rt, ref, p_ref_idx);
  3369. }
  3370. else
  3371. #endif
  3372. {
  3373. if (ref) {
  3374. if (NULL_REF != ref->ref_idx_rt) {
  3375. if (ref->ref_idx_rt >= function_count) {
  3376. return false;
  3377. }
  3378. }
  3379. *p_ref_idx = ref->ref_idx_rt;
  3380. wasm_ref_delete(ref);
  3381. }
  3382. else {
  3383. *p_ref_idx = NULL_REF;
  3384. }
  3385. }
  3386. return true;
  3387. }
  3388. wasm_table_size_t
  3389. wasm_table_size(const wasm_table_t *table)
  3390. {
  3391. if (!table || !table->inst_comm_rt) {
  3392. return 0;
  3393. }
  3394. #if WASM_ENABLE_INTERP != 0
  3395. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3396. WASMTableInstance *table_interp =
  3397. ((WASMModuleInstance *)table->inst_comm_rt)
  3398. ->tables[table->table_idx_rt];
  3399. return table_interp->cur_size;
  3400. }
  3401. #endif
  3402. #if WASM_ENABLE_AOT != 0
  3403. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3404. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3405. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3406. if (table->table_idx_rt < module_aot->import_table_count) {
  3407. AOTImportTable *table_aot =
  3408. module_aot->import_tables + table->table_idx_rt;
  3409. return table_aot->table_init_size;
  3410. }
  3411. else {
  3412. AOTTable *table_aot =
  3413. module_aot->tables
  3414. + (table->table_idx_rt - module_aot->import_table_count);
  3415. return table_aot->table_init_size;
  3416. }
  3417. }
  3418. #endif
  3419. /*
  3420. * a wrong combination of module filetype and compilation flags
  3421. * leads to below branch
  3422. */
  3423. return 0;
  3424. }
  3425. bool
  3426. wasm_table_grow(wasm_table_t *table, wasm_table_size_t delta,
  3427. own wasm_ref_t *init)
  3428. {
  3429. (void)table;
  3430. (void)delta;
  3431. (void)init;
  3432. LOG_WARNING("Calling wasm_table_grow() by host is not supported."
  3433. "Only allow growing a table via the opcode table.grow");
  3434. return false;
  3435. }
  3436. static wasm_memory_t *
  3437. wasm_memory_new_basic(wasm_store_t *store, const wasm_memorytype_t *type)
  3438. {
  3439. wasm_memory_t *memory = NULL;
  3440. if (!type) {
  3441. goto failed;
  3442. }
  3443. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3444. goto failed;
  3445. }
  3446. memory->store = store;
  3447. memory->kind = WASM_EXTERN_MEMORY;
  3448. memory->type = wasm_memorytype_copy(type);
  3449. RETURN_OBJ(memory, wasm_memory_delete)
  3450. }
  3451. wasm_memory_t *
  3452. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  3453. {
  3454. bh_assert(singleton_engine);
  3455. return wasm_memory_new_basic(store, type);
  3456. }
  3457. wasm_memory_t *
  3458. wasm_memory_copy(const wasm_memory_t *src)
  3459. {
  3460. wasm_memory_t *dst = NULL;
  3461. if (!src) {
  3462. return NULL;
  3463. }
  3464. if (!(dst = wasm_memory_new_basic(src->store, src->type))) {
  3465. goto failed;
  3466. }
  3467. dst->memory_idx_rt = src->memory_idx_rt;
  3468. dst->inst_comm_rt = src->inst_comm_rt;
  3469. RETURN_OBJ(dst, wasm_memory_delete)
  3470. }
  3471. wasm_memory_t *
  3472. wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
  3473. WASMModuleInstanceCommon *inst_comm_rt)
  3474. {
  3475. wasm_memory_t *memory = NULL;
  3476. uint32 min_pages = 0, max_pages = 0;
  3477. bool init_flag = false;
  3478. bh_assert(singleton_engine);
  3479. if (!inst_comm_rt) {
  3480. return NULL;
  3481. }
  3482. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3483. goto failed;
  3484. }
  3485. memory->store = store;
  3486. memory->kind = WASM_EXTERN_MEMORY;
  3487. #if WASM_ENABLE_INTERP != 0
  3488. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3489. WASMMemoryInstance *memory_interp =
  3490. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  3491. min_pages = memory_interp->cur_page_count;
  3492. max_pages = memory_interp->max_page_count;
  3493. init_flag = true;
  3494. }
  3495. #endif
  3496. #if WASM_ENABLE_AOT != 0
  3497. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3498. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3499. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3500. if (memory_idx_rt < module_aot->import_memory_count) {
  3501. min_pages = module_aot->import_memories->mem_init_page_count;
  3502. max_pages = module_aot->import_memories->mem_max_page_count;
  3503. }
  3504. else {
  3505. min_pages = module_aot->memories->mem_init_page_count;
  3506. max_pages = module_aot->memories->mem_max_page_count;
  3507. }
  3508. init_flag = true;
  3509. }
  3510. #endif
  3511. /*
  3512. * a wrong combination of module filetype and compilation flags
  3513. * leads to below branch
  3514. */
  3515. if (!init_flag) {
  3516. goto failed;
  3517. }
  3518. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  3519. goto failed;
  3520. }
  3521. memory->inst_comm_rt = inst_comm_rt;
  3522. memory->memory_idx_rt = memory_idx_rt;
  3523. RETURN_OBJ(memory, wasm_memory_delete);
  3524. }
  3525. void
  3526. wasm_memory_delete(wasm_memory_t *memory)
  3527. {
  3528. if (!memory) {
  3529. return;
  3530. }
  3531. if (memory->type) {
  3532. wasm_memorytype_delete(memory->type);
  3533. memory->type = NULL;
  3534. }
  3535. DELETE_HOST_INFO(memory)
  3536. wasm_runtime_free(memory);
  3537. }
  3538. wasm_memorytype_t *
  3539. wasm_memory_type(const wasm_memory_t *memory)
  3540. {
  3541. if (!memory) {
  3542. return NULL;
  3543. }
  3544. return wasm_memorytype_copy(memory->type);
  3545. }
  3546. byte_t *
  3547. wasm_memory_data(wasm_memory_t *memory)
  3548. {
  3549. WASMModuleInstanceCommon *module_inst_comm;
  3550. if (!memory || !memory->inst_comm_rt) {
  3551. return NULL;
  3552. }
  3553. module_inst_comm = memory->inst_comm_rt;
  3554. #if WASM_ENABLE_INTERP != 0
  3555. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3556. WASMModuleInstance *module_inst =
  3557. (WASMModuleInstance *)module_inst_comm;
  3558. WASMMemoryInstance *memory_inst =
  3559. module_inst->memories[memory->memory_idx_rt];
  3560. return (byte_t *)memory_inst->memory_data;
  3561. }
  3562. #endif
  3563. #if WASM_ENABLE_AOT != 0
  3564. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3565. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3566. AOTMemoryInstance *memory_inst =
  3567. ((AOTMemoryInstance **)
  3568. module_inst->memories)[memory->memory_idx_rt];
  3569. return (byte_t *)memory_inst->memory_data;
  3570. }
  3571. #endif
  3572. /*
  3573. * a wrong combination of module filetype and compilation flags
  3574. * leads to below branch
  3575. */
  3576. return NULL;
  3577. }
  3578. size_t
  3579. wasm_memory_data_size(const wasm_memory_t *memory)
  3580. {
  3581. WASMModuleInstanceCommon *module_inst_comm;
  3582. if (!memory || !memory->inst_comm_rt) {
  3583. return 0;
  3584. }
  3585. module_inst_comm = memory->inst_comm_rt;
  3586. #if WASM_ENABLE_INTERP != 0
  3587. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3588. WASMModuleInstance *module_inst =
  3589. (WASMModuleInstance *)module_inst_comm;
  3590. WASMMemoryInstance *memory_inst =
  3591. module_inst->memories[memory->memory_idx_rt];
  3592. return (size_t)memory_inst->cur_page_count
  3593. * memory_inst->num_bytes_per_page;
  3594. }
  3595. #endif
  3596. #if WASM_ENABLE_AOT != 0
  3597. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3598. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3599. AOTMemoryInstance *memory_inst =
  3600. ((AOTMemoryInstance **)
  3601. module_inst->memories)[memory->memory_idx_rt];
  3602. return (size_t)memory_inst->cur_page_count
  3603. * memory_inst->num_bytes_per_page;
  3604. }
  3605. #endif
  3606. /*
  3607. * a wrong combination of module filetype and compilation flags
  3608. * leads to below branch
  3609. */
  3610. return 0;
  3611. }
  3612. wasm_memory_pages_t
  3613. wasm_memory_size(const wasm_memory_t *memory)
  3614. {
  3615. WASMModuleInstanceCommon *module_inst_comm;
  3616. if (!memory || !memory->inst_comm_rt) {
  3617. return 0;
  3618. }
  3619. module_inst_comm = memory->inst_comm_rt;
  3620. #if WASM_ENABLE_INTERP != 0
  3621. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3622. WASMModuleInstance *module_inst =
  3623. (WASMModuleInstance *)module_inst_comm;
  3624. WASMMemoryInstance *memory_inst =
  3625. module_inst->memories[memory->memory_idx_rt];
  3626. return memory_inst->cur_page_count;
  3627. }
  3628. #endif
  3629. #if WASM_ENABLE_AOT != 0
  3630. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3631. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3632. AOTMemoryInstance *memory_inst =
  3633. ((AOTMemoryInstance **)
  3634. module_inst->memories)[memory->memory_idx_rt];
  3635. return memory_inst->cur_page_count;
  3636. }
  3637. #endif
  3638. /*
  3639. * a wrong combination of module filetype and compilation flags
  3640. * leads to below branch
  3641. */
  3642. return 0;
  3643. }
  3644. bool
  3645. wasm_memory_grow(wasm_memory_t *memory, wasm_memory_pages_t delta)
  3646. {
  3647. (void)memory;
  3648. (void)delta;
  3649. LOG_WARNING("Calling wasm_memory_grow() by host is not supported."
  3650. "Only allow growing a memory via the opcode memory.grow");
  3651. return false;
  3652. }
  3653. #if WASM_ENABLE_INTERP != 0
  3654. static bool
  3655. interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
  3656. uint16 func_idx_rt, wasm_func_t *import)
  3657. {
  3658. WASMImport *imported_func_interp = NULL;
  3659. bh_assert(inst && module_interp && import);
  3660. bh_assert(func_idx_rt < module_interp->import_function_count);
  3661. bh_assert(WASM_EXTERN_FUNC == import->kind);
  3662. imported_func_interp = module_interp->import_functions + func_idx_rt;
  3663. bh_assert(imported_func_interp);
  3664. bh_assert(imported_func_interp->kind == IMPORT_KIND_FUNC);
  3665. /* it is a placeholder and let's skip it*/
  3666. if (!import->type)
  3667. return true;
  3668. /* type comparison */
  3669. if (!wasm_functype_same_internal(
  3670. import->type, imported_func_interp->u.function.func_type))
  3671. return false;
  3672. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  3673. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3674. func_ptr_linked, with_env and env will be stored in module instance's
  3675. c_api_func_imports later and used when calling import function */
  3676. if (import->with_env)
  3677. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  3678. else
  3679. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  3680. bh_assert(imported_func_interp->u.function.func_ptr_linked);
  3681. import->func_idx_rt = func_idx_rt;
  3682. (void)inst;
  3683. return true;
  3684. }
  3685. static bool
  3686. interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
  3687. wasm_global_t *import)
  3688. {
  3689. WASMImport *imported_global_interp = NULL;
  3690. bh_assert(module_interp && import);
  3691. bh_assert(global_idx_rt < module_interp->import_global_count);
  3692. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  3693. imported_global_interp = module_interp->import_globals + global_idx_rt;
  3694. bh_assert(imported_global_interp);
  3695. bh_assert(imported_global_interp->kind == IMPORT_KIND_GLOBAL);
  3696. /* it is a placeholder and let's skip it*/
  3697. if (!import->type)
  3698. return true;
  3699. /* type comparison */
  3700. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
  3701. imported_global_interp->u.global.type))
  3702. return false;
  3703. /* set init value */
  3704. bh_assert(import->init);
  3705. switch (wasm_valtype_kind(import->type->val_type)) {
  3706. case WASM_I32:
  3707. imported_global_interp->u.global.global_data_linked.i32 =
  3708. import->init->of.i32;
  3709. break;
  3710. case WASM_I64:
  3711. imported_global_interp->u.global.global_data_linked.i64 =
  3712. import->init->of.i64;
  3713. break;
  3714. case WASM_F32:
  3715. imported_global_interp->u.global.global_data_linked.f32 =
  3716. import->init->of.f32;
  3717. break;
  3718. case WASM_F64:
  3719. imported_global_interp->u.global.global_data_linked.f64 =
  3720. import->init->of.f64;
  3721. break;
  3722. default:
  3723. return false;
  3724. }
  3725. import->global_idx_rt = global_idx_rt;
  3726. imported_global_interp->u.global.is_linked = true;
  3727. return true;
  3728. }
  3729. static bool
  3730. interp_process_export(wasm_store_t *store,
  3731. const WASMModuleInstance *inst_interp,
  3732. wasm_extern_vec_t *externals)
  3733. {
  3734. WASMExport *exports = NULL;
  3735. WASMExport *export = NULL;
  3736. wasm_extern_t *external = NULL;
  3737. uint32 export_cnt = 0;
  3738. uint32 i = 0;
  3739. bh_assert(store && inst_interp && inst_interp->module && externals);
  3740. exports = inst_interp->module->exports;
  3741. export_cnt = inst_interp->module->export_count;
  3742. for (i = 0; i < export_cnt; ++i) {
  3743. export = exports + i;
  3744. switch (export->kind) {
  3745. case EXPORT_KIND_FUNC:
  3746. {
  3747. wasm_func_t *func;
  3748. if (!(func = wasm_func_new_internal(
  3749. store, export->index,
  3750. (WASMModuleInstanceCommon *)inst_interp))) {
  3751. goto failed;
  3752. }
  3753. external = wasm_func_as_extern(func);
  3754. break;
  3755. }
  3756. case EXPORT_KIND_GLOBAL:
  3757. {
  3758. wasm_global_t *global;
  3759. if (!(global = wasm_global_new_internal(
  3760. store, export->index,
  3761. (WASMModuleInstanceCommon *)inst_interp))) {
  3762. goto failed;
  3763. }
  3764. external = wasm_global_as_extern(global);
  3765. break;
  3766. }
  3767. case EXPORT_KIND_TABLE:
  3768. {
  3769. wasm_table_t *table;
  3770. if (!(table = wasm_table_new_internal(
  3771. store, export->index,
  3772. (WASMModuleInstanceCommon *)inst_interp))) {
  3773. goto failed;
  3774. }
  3775. external = wasm_table_as_extern(table);
  3776. break;
  3777. }
  3778. case EXPORT_KIND_MEMORY:
  3779. {
  3780. wasm_memory_t *memory;
  3781. if (!(memory = wasm_memory_new_internal(
  3782. store, export->index,
  3783. (WASMModuleInstanceCommon *)inst_interp))) {
  3784. goto failed;
  3785. }
  3786. external = wasm_memory_as_extern(memory);
  3787. break;
  3788. }
  3789. default:
  3790. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3791. export->kind);
  3792. goto failed;
  3793. }
  3794. if (!bh_vector_append((Vector *)externals, &external)) {
  3795. goto failed;
  3796. }
  3797. }
  3798. return true;
  3799. failed:
  3800. wasm_extern_delete(external);
  3801. return false;
  3802. }
  3803. #endif /* WASM_ENABLE_INTERP */
  3804. #if WASM_ENABLE_AOT != 0
  3805. static bool
  3806. aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
  3807. uint32 import_func_idx_rt, wasm_func_t *import)
  3808. {
  3809. AOTImportFunc *import_aot_func = NULL;
  3810. bh_assert(inst && module_aot && import);
  3811. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  3812. bh_assert(import_aot_func);
  3813. /* it is a placeholder and let's skip it*/
  3814. if (!import->type)
  3815. return true;
  3816. /* type comparison */
  3817. if (!wasm_functype_same_internal(import->type, import_aot_func->func_type))
  3818. return false;
  3819. import_aot_func->call_conv_wasm_c_api = true;
  3820. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3821. func_ptr_linked, with_env and env will be stored in module instance's
  3822. c_api_func_imports later and used when calling import function */
  3823. if (import->with_env)
  3824. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  3825. else
  3826. import_aot_func->func_ptr_linked = import->u.cb;
  3827. bh_assert(import_aot_func->func_ptr_linked);
  3828. import->func_idx_rt = import_func_idx_rt;
  3829. return true;
  3830. }
  3831. static bool
  3832. aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
  3833. wasm_global_t *import)
  3834. {
  3835. AOTImportGlobal *import_aot_global = NULL;
  3836. const wasm_valtype_t *val_type = NULL;
  3837. bh_assert(module_aot && import);
  3838. import_aot_global = module_aot->import_globals + global_idx_rt;
  3839. bh_assert(import_aot_global);
  3840. /* it is a placeholder and let's skip it*/
  3841. if (!import->type)
  3842. return true;
  3843. val_type = wasm_globaltype_content(import->type);
  3844. bh_assert(val_type);
  3845. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(val_type),
  3846. import_aot_global->type))
  3847. return false;
  3848. bh_assert(import->init);
  3849. switch (wasm_valtype_kind(val_type)) {
  3850. case WASM_I32:
  3851. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  3852. break;
  3853. case WASM_I64:
  3854. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  3855. break;
  3856. case WASM_F32:
  3857. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  3858. break;
  3859. case WASM_F64:
  3860. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  3861. break;
  3862. default:
  3863. goto failed;
  3864. }
  3865. import->global_idx_rt = global_idx_rt;
  3866. import_aot_global->is_linked = true;
  3867. return true;
  3868. failed:
  3869. LOG_DEBUG("%s failed", __FUNCTION__);
  3870. return false;
  3871. }
  3872. static bool
  3873. aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
  3874. wasm_extern_vec_t *externals)
  3875. {
  3876. uint32 i;
  3877. wasm_extern_t *external = NULL;
  3878. AOTModule *module_aot = NULL;
  3879. bh_assert(store && inst_aot && externals);
  3880. module_aot = (AOTModule *)inst_aot->module;
  3881. bh_assert(module_aot);
  3882. for (i = 0; i < module_aot->export_count; ++i) {
  3883. AOTExport *export = module_aot->exports + i;
  3884. switch (export->kind) {
  3885. case EXPORT_KIND_FUNC:
  3886. {
  3887. wasm_func_t *func = NULL;
  3888. if (!(func = wasm_func_new_internal(
  3889. store, export->index,
  3890. (WASMModuleInstanceCommon *)inst_aot))) {
  3891. goto failed;
  3892. }
  3893. external = wasm_func_as_extern(func);
  3894. break;
  3895. }
  3896. case EXPORT_KIND_GLOBAL:
  3897. {
  3898. wasm_global_t *global = NULL;
  3899. if (!(global = wasm_global_new_internal(
  3900. store, export->index,
  3901. (WASMModuleInstanceCommon *)inst_aot))) {
  3902. goto failed;
  3903. }
  3904. external = wasm_global_as_extern(global);
  3905. break;
  3906. }
  3907. case EXPORT_KIND_TABLE:
  3908. {
  3909. wasm_table_t *table;
  3910. if (!(table = wasm_table_new_internal(
  3911. store, export->index,
  3912. (WASMModuleInstanceCommon *)inst_aot))) {
  3913. goto failed;
  3914. }
  3915. external = wasm_table_as_extern(table);
  3916. break;
  3917. }
  3918. case EXPORT_KIND_MEMORY:
  3919. {
  3920. wasm_memory_t *memory;
  3921. if (!(memory = wasm_memory_new_internal(
  3922. store, export->index,
  3923. (WASMModuleInstanceCommon *)inst_aot))) {
  3924. goto failed;
  3925. }
  3926. external = wasm_memory_as_extern(memory);
  3927. break;
  3928. }
  3929. default:
  3930. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3931. export->kind);
  3932. goto failed;
  3933. }
  3934. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3935. goto failed;
  3936. }
  3937. wasm_name_new_from_string_nt(external->name, export->name);
  3938. if (strlen(export->name) && !external->name->data) {
  3939. goto failed;
  3940. }
  3941. if (!bh_vector_append((Vector *)externals, &external)) {
  3942. goto failed;
  3943. }
  3944. }
  3945. return true;
  3946. failed:
  3947. wasm_extern_delete(external);
  3948. return false;
  3949. }
  3950. #endif /* WASM_ENABLE_AOT */
  3951. static bool
  3952. do_link(const wasm_instance_t *inst, const wasm_module_t *module,
  3953. const wasm_extern_vec_t *imports)
  3954. {
  3955. uint32 i, import_func_i, import_global_i;
  3956. bh_assert(inst && module);
  3957. /* we have run a module_type check before. */
  3958. for (i = 0, import_func_i = 0, import_global_i = 0; i < imports->num_elems;
  3959. i++) {
  3960. wasm_extern_t *import = imports->data[i];
  3961. if (!import) {
  3962. LOG_ERROR("imports[%d] is NULL and it is fatal\n", i);
  3963. goto failed;
  3964. }
  3965. switch (wasm_extern_kind(import)) {
  3966. case WASM_EXTERN_FUNC:
  3967. {
  3968. bool ret = false;
  3969. #if WASM_ENABLE_INTERP != 0
  3970. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3971. ret = interp_link_func(inst, MODULE_INTERP(module),
  3972. import_func_i,
  3973. wasm_extern_as_func(import));
  3974. }
  3975. #endif
  3976. #if WASM_ENABLE_AOT != 0
  3977. if ((*module)->module_type == Wasm_Module_AoT) {
  3978. ret = aot_link_func(inst, MODULE_AOT(module), import_func_i,
  3979. wasm_extern_as_func(import));
  3980. }
  3981. #endif
  3982. if (!ret) {
  3983. LOG_WARNING("link function #%d failed", import_func_i);
  3984. goto failed;
  3985. }
  3986. import_func_i++;
  3987. break;
  3988. }
  3989. case WASM_EXTERN_GLOBAL:
  3990. {
  3991. bool ret = false;
  3992. #if WASM_ENABLE_INTERP != 0
  3993. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3994. ret = interp_link_global(MODULE_INTERP(module),
  3995. import_global_i,
  3996. wasm_extern_as_global(import));
  3997. }
  3998. #endif
  3999. #if WASM_ENABLE_AOT != 0
  4000. if ((*module)->module_type == Wasm_Module_AoT) {
  4001. ret = aot_link_global(MODULE_AOT(module), import_global_i,
  4002. wasm_extern_as_global(import));
  4003. }
  4004. #endif
  4005. if (!ret) {
  4006. LOG_WARNING("link global #%d failed", import_global_i);
  4007. goto failed;
  4008. }
  4009. import_global_i++;
  4010. break;
  4011. }
  4012. case WASM_EXTERN_MEMORY:
  4013. case WASM_EXTERN_TABLE:
  4014. {
  4015. LOG_WARNING("doesn't support import memories and tables for "
  4016. "now, ignore them");
  4017. break;
  4018. }
  4019. default:
  4020. {
  4021. UNREACHABLE();
  4022. break;
  4023. }
  4024. }
  4025. }
  4026. return true;
  4027. failed:
  4028. LOG_DEBUG("%s failed", __FUNCTION__);
  4029. return false;
  4030. }
  4031. wasm_instance_t *
  4032. wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
  4033. const wasm_extern_vec_t *imports, own wasm_trap_t **trap)
  4034. {
  4035. return wasm_instance_new_with_args(store, module, imports, trap,
  4036. KILOBYTE(32), KILOBYTE(32));
  4037. }
  4038. wasm_instance_t *
  4039. wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
  4040. const wasm_extern_vec_t *imports,
  4041. own wasm_trap_t **trap, const uint32 stack_size,
  4042. const uint32 heap_size)
  4043. {
  4044. char sub_error_buf[128] = { 0 };
  4045. char error_buf[256] = { 0 };
  4046. wasm_instance_t *instance = NULL;
  4047. CApiFuncImport *func_import = NULL, **p_func_imports = NULL;
  4048. uint32 i = 0, import_func_count = 0;
  4049. uint64 total_size;
  4050. bool build_exported = false;
  4051. bh_assert(singleton_engine);
  4052. if (!module)
  4053. return NULL;
  4054. /*
  4055. * will do the check at the end of wasm_runtime_instantiate
  4056. */
  4057. WASM_C_DUMP_PROC_MEM();
  4058. instance = malloc_internal(sizeof(wasm_instance_t));
  4059. if (!instance) {
  4060. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4061. "Failed to malloc instance");
  4062. goto failed;
  4063. }
  4064. /* executes the instantiate-time linking if provided */
  4065. if (imports) {
  4066. if (!do_link(instance, module, imports)) {
  4067. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4068. "Failed to validate imports");
  4069. goto failed;
  4070. }
  4071. }
  4072. /*
  4073. * will do the linking result check at the end of wasm_runtime_instantiate
  4074. */
  4075. instance->inst_comm_rt = wasm_runtime_instantiate(
  4076. *module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf));
  4077. if (!instance->inst_comm_rt) {
  4078. goto failed;
  4079. }
  4080. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  4081. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4082. "Failed to create exec env singleton");
  4083. goto failed;
  4084. }
  4085. /* create the c-api func import list */
  4086. #if WASM_ENABLE_INTERP != 0
  4087. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4088. WASMModuleInstanceExtra *e =
  4089. ((WASMModuleInstance *)instance->inst_comm_rt)->e;
  4090. p_func_imports = &(e->common.c_api_func_imports);
  4091. import_func_count = MODULE_INTERP(module)->import_function_count;
  4092. }
  4093. #endif
  4094. #if WASM_ENABLE_AOT != 0
  4095. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4096. AOTModuleInstanceExtra *e =
  4097. (AOTModuleInstanceExtra *)((AOTModuleInstance *)
  4098. instance->inst_comm_rt)
  4099. ->e;
  4100. p_func_imports = &(e->common.c_api_func_imports);
  4101. import_func_count = MODULE_AOT(module)->import_func_count;
  4102. }
  4103. #endif
  4104. bh_assert(p_func_imports);
  4105. total_size = (uint64)sizeof(CApiFuncImport) * import_func_count;
  4106. if (total_size > 0
  4107. && !(*p_func_imports = func_import = malloc_internal(total_size))) {
  4108. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4109. "Failed to create wasm-c-api func imports");
  4110. goto failed;
  4111. }
  4112. /* fill in module_inst->e->c_api_func_imports */
  4113. for (i = 0; imports && i < imports->num_elems; i++) {
  4114. wasm_func_t *func_host = NULL;
  4115. wasm_extern_t *in = imports->data[i];
  4116. bh_assert(in);
  4117. if (wasm_extern_kind(in) != WASM_EXTERN_FUNC)
  4118. continue;
  4119. func_host = wasm_extern_as_func(in);
  4120. /* it is a placeholder and let's skip it*/
  4121. if (!func_host->type) {
  4122. func_import++;
  4123. continue;
  4124. }
  4125. func_import->with_env_arg = func_host->with_env;
  4126. if (func_host->with_env) {
  4127. func_import->func_ptr_linked = func_host->u.cb_env.cb;
  4128. func_import->env_arg = func_host->u.cb_env.env;
  4129. }
  4130. else {
  4131. func_import->func_ptr_linked = func_host->u.cb;
  4132. func_import->env_arg = NULL;
  4133. }
  4134. bh_assert(func_import->func_ptr_linked);
  4135. func_import++;
  4136. }
  4137. /* fill with inst */
  4138. for (i = 0; imports && imports->data && i < imports->num_elems; ++i) {
  4139. wasm_extern_t *import = imports->data[i];
  4140. bh_assert(import);
  4141. switch (import->kind) {
  4142. case WASM_EXTERN_FUNC:
  4143. wasm_extern_as_func(import)->inst_comm_rt =
  4144. instance->inst_comm_rt;
  4145. break;
  4146. case WASM_EXTERN_GLOBAL:
  4147. wasm_extern_as_global(import)->inst_comm_rt =
  4148. instance->inst_comm_rt;
  4149. break;
  4150. case WASM_EXTERN_MEMORY:
  4151. wasm_extern_as_memory(import)->inst_comm_rt =
  4152. instance->inst_comm_rt;
  4153. break;
  4154. case WASM_EXTERN_TABLE:
  4155. wasm_extern_as_table(import)->inst_comm_rt =
  4156. instance->inst_comm_rt;
  4157. break;
  4158. default:
  4159. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4160. "Unknown import kind");
  4161. goto failed;
  4162. }
  4163. }
  4164. /* build the exports list */
  4165. #if WASM_ENABLE_INTERP != 0
  4166. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4167. uint32 export_cnt = ((WASMModuleInstance *)instance->inst_comm_rt)
  4168. ->module->export_count;
  4169. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4170. export_cnt);
  4171. if (!interp_process_export(store,
  4172. (WASMModuleInstance *)instance->inst_comm_rt,
  4173. instance->exports)) {
  4174. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4175. "Interpreter failed to process exports");
  4176. goto failed;
  4177. }
  4178. build_exported = true;
  4179. }
  4180. #endif
  4181. #if WASM_ENABLE_AOT != 0
  4182. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4183. uint32 export_cnt =
  4184. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  4185. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  4186. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_table_count
  4187. + ((AOTModuleInstance *)instance->inst_comm_rt)
  4188. ->export_memory_count;
  4189. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4190. export_cnt);
  4191. if (!aot_process_export(store,
  4192. (AOTModuleInstance *)instance->inst_comm_rt,
  4193. instance->exports)) {
  4194. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4195. "AOT failed to process exports");
  4196. goto failed;
  4197. }
  4198. build_exported = true;
  4199. }
  4200. #endif
  4201. /*
  4202. * a wrong combination of module filetype and compilation flags
  4203. * leads to below branch
  4204. */
  4205. if (!build_exported) {
  4206. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4207. "Incorrect filetype and compilation flags");
  4208. goto failed;
  4209. }
  4210. /* add it to a watching list in store */
  4211. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  4212. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4213. "Failed to add to store instances");
  4214. goto failed;
  4215. }
  4216. WASM_C_DUMP_PROC_MEM();
  4217. return instance;
  4218. failed:
  4219. snprintf(error_buf, sizeof(error_buf), "%s failed: %s", __FUNCTION__,
  4220. sub_error_buf);
  4221. if (trap != NULL) {
  4222. wasm_message_t message = { 0 };
  4223. wasm_name_new_from_string_nt(&message, error_buf);
  4224. *trap = wasm_trap_new(store, &message);
  4225. wasm_byte_vec_delete(&message);
  4226. }
  4227. LOG_DEBUG(error_buf);
  4228. wasm_instance_delete_internal(instance);
  4229. return NULL;
  4230. }
  4231. static void
  4232. wasm_instance_delete_internal(wasm_instance_t *instance)
  4233. {
  4234. if (!instance) {
  4235. return;
  4236. }
  4237. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  4238. if (instance->inst_comm_rt) {
  4239. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  4240. instance->inst_comm_rt = NULL;
  4241. }
  4242. wasm_runtime_free(instance);
  4243. }
  4244. void
  4245. wasm_instance_delete(wasm_instance_t *inst)
  4246. {
  4247. DELETE_HOST_INFO(inst)
  4248. /* will release instance when releasing the store */
  4249. }
  4250. void
  4251. wasm_instance_exports(const wasm_instance_t *instance,
  4252. own wasm_extern_vec_t *out)
  4253. {
  4254. if (!instance || !out) {
  4255. return;
  4256. }
  4257. wasm_extern_vec_copy(out, instance->exports);
  4258. }
  4259. wasm_extern_t *
  4260. wasm_extern_copy(const wasm_extern_t *src)
  4261. {
  4262. wasm_extern_t *dst = NULL;
  4263. if (!src) {
  4264. return NULL;
  4265. }
  4266. switch (wasm_extern_kind(src)) {
  4267. case WASM_EXTERN_FUNC:
  4268. dst = wasm_func_as_extern(
  4269. wasm_func_copy(wasm_extern_as_func_const(src)));
  4270. break;
  4271. case WASM_EXTERN_GLOBAL:
  4272. dst = wasm_global_as_extern(
  4273. wasm_global_copy(wasm_extern_as_global_const(src)));
  4274. break;
  4275. case WASM_EXTERN_MEMORY:
  4276. dst = wasm_memory_as_extern(
  4277. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  4278. break;
  4279. case WASM_EXTERN_TABLE:
  4280. dst = wasm_table_as_extern(
  4281. wasm_table_copy(wasm_extern_as_table_const(src)));
  4282. break;
  4283. default:
  4284. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4285. src->kind);
  4286. break;
  4287. }
  4288. if (!dst) {
  4289. goto failed;
  4290. }
  4291. return dst;
  4292. failed:
  4293. LOG_DEBUG("%s failed", __FUNCTION__);
  4294. wasm_extern_delete(dst);
  4295. return NULL;
  4296. }
  4297. void
  4298. wasm_extern_delete(wasm_extern_t *external)
  4299. {
  4300. if (!external) {
  4301. return;
  4302. }
  4303. if (external->name) {
  4304. wasm_byte_vec_delete(external->name);
  4305. wasm_runtime_free(external->name);
  4306. external->name = NULL;
  4307. }
  4308. switch (wasm_extern_kind(external)) {
  4309. case WASM_EXTERN_FUNC:
  4310. wasm_func_delete(wasm_extern_as_func(external));
  4311. break;
  4312. case WASM_EXTERN_GLOBAL:
  4313. wasm_global_delete(wasm_extern_as_global(external));
  4314. break;
  4315. case WASM_EXTERN_MEMORY:
  4316. wasm_memory_delete(wasm_extern_as_memory(external));
  4317. break;
  4318. case WASM_EXTERN_TABLE:
  4319. wasm_table_delete(wasm_extern_as_table(external));
  4320. break;
  4321. default:
  4322. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4323. external->kind);
  4324. break;
  4325. }
  4326. }
  4327. wasm_externkind_t
  4328. wasm_extern_kind(const wasm_extern_t *external)
  4329. {
  4330. if (!external) {
  4331. return WASM_ANYREF;
  4332. }
  4333. return external->kind;
  4334. }
  4335. own wasm_externtype_t *
  4336. wasm_extern_type(const wasm_extern_t *external)
  4337. {
  4338. if (!external) {
  4339. return NULL;
  4340. }
  4341. switch (wasm_extern_kind(external)) {
  4342. case WASM_EXTERN_FUNC:
  4343. return wasm_functype_as_externtype(
  4344. wasm_func_type(wasm_extern_as_func_const(external)));
  4345. case WASM_EXTERN_GLOBAL:
  4346. return wasm_globaltype_as_externtype(
  4347. wasm_global_type(wasm_extern_as_global_const(external)));
  4348. case WASM_EXTERN_MEMORY:
  4349. return wasm_memorytype_as_externtype(
  4350. wasm_memory_type(wasm_extern_as_memory_const(external)));
  4351. case WASM_EXTERN_TABLE:
  4352. return wasm_tabletype_as_externtype(
  4353. wasm_table_type(wasm_extern_as_table_const(external)));
  4354. default:
  4355. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4356. external->kind);
  4357. break;
  4358. }
  4359. return NULL;
  4360. }
  4361. #define BASIC_FOUR_LIST(V) \
  4362. V(func) \
  4363. V(global) \
  4364. V(memory) \
  4365. V(table)
  4366. #define WASM_EXTERN_AS_OTHER(name) \
  4367. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  4368. { \
  4369. return (wasm_##name##_t *)external; \
  4370. }
  4371. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  4372. #undef WASM_EXTERN_AS_OTHER
  4373. #define WASM_OTHER_AS_EXTERN(name) \
  4374. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  4375. { \
  4376. return (wasm_extern_t *)other; \
  4377. }
  4378. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  4379. #undef WASM_OTHER_AS_EXTERN
  4380. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  4381. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  4382. const wasm_extern_t *external) \
  4383. { \
  4384. return (const wasm_##name##_t *)external; \
  4385. }
  4386. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  4387. #undef WASM_EXTERN_AS_OTHER_CONST
  4388. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  4389. const wasm_extern_t *wasm_##name##_as_extern_const( \
  4390. const wasm_##name##_t *other) \
  4391. { \
  4392. return (const wasm_extern_t *)other; \
  4393. }
  4394. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  4395. #undef WASM_OTHER_AS_EXTERN_CONST
  4396. wasm_extern_t *
  4397. wasm_extern_new_empty(wasm_store_t *store, wasm_externkind_t extern_kind)
  4398. {
  4399. if (extern_kind == WASM_EXTERN_FUNC)
  4400. return wasm_func_as_extern(wasm_func_new_empty(store));
  4401. if (extern_kind == WASM_EXTERN_GLOBAL)
  4402. return wasm_global_as_extern(wasm_global_new_empty(store));
  4403. LOG_ERROR("Don't support linking table and memory for now");
  4404. return NULL;
  4405. }