wasm_c_api.c 148 KB

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