wasm_c_api.c 133 KB

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