wasm_c_api.c 146 KB

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