wasm_c_api.c 107 KB

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