wasm_c_api.c 131 KB

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