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