wasm_c_api.c 111 KB

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