wasm_c_api.c 148 KB

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