wasm_c_api.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239
  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. wasm_byte_vec_t local_binary = { 0 };
  1920. struct WASMModuleCommon *module_rt;
  1921. char error_buf[128] = { 0 };
  1922. bool ret;
  1923. bh_assert(singleton_engine);
  1924. if (!store || !binary || binary->size > UINT32_MAX) {
  1925. LOG_ERROR("%s failed", __FUNCTION__);
  1926. return false;
  1927. }
  1928. /* make a copy of binary */
  1929. wasm_byte_vec_copy(&local_binary, binary);
  1930. if (binary->size && !local_binary.data)
  1931. return false;
  1932. module_rt = wasm_runtime_load((uint8 *)local_binary.data,
  1933. (uint32)local_binary.size, error_buf, 128);
  1934. wasm_byte_vec_delete(&local_binary);
  1935. if (module_rt) {
  1936. wasm_runtime_unload(module_rt);
  1937. ret = true;
  1938. }
  1939. else {
  1940. ret = false;
  1941. LOG_VERBOSE(error_buf);
  1942. }
  1943. return ret;
  1944. }
  1945. static void
  1946. wasm_module_delete_internal(wasm_module_t *module)
  1947. {
  1948. wasm_module_ex_t *module_ex;
  1949. if (!module) {
  1950. return;
  1951. }
  1952. module_ex = module_to_module_ext(module);
  1953. os_mutex_lock(&module_ex->lock);
  1954. /* N -> N-1 -> 0 -> UINT32_MAX */
  1955. module_ex->ref_count--;
  1956. if (module_ex->ref_count > 0) {
  1957. os_mutex_unlock(&module_ex->lock);
  1958. return;
  1959. }
  1960. DEINIT_VEC(module_ex->binary, wasm_byte_vec_delete);
  1961. if (module_ex->module_comm_rt) {
  1962. wasm_runtime_unload(module_ex->module_comm_rt);
  1963. module_ex->module_comm_rt = NULL;
  1964. }
  1965. #if WASM_ENABLE_WASM_CACHE != 0
  1966. memset(module_ex->hash, 0, sizeof(module_ex->hash));
  1967. #endif
  1968. os_mutex_unlock(&module_ex->lock);
  1969. }
  1970. void
  1971. wasm_module_delete(wasm_module_t *module)
  1972. {
  1973. /* the module will be released when releasing the store */
  1974. (void)module;
  1975. }
  1976. void
  1977. wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
  1978. {
  1979. uint32 i, import_func_count = 0, import_memory_count = 0,
  1980. import_global_count = 0, import_table_count = 0, import_count = 0;
  1981. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1982. wasm_externtype_t *extern_type = NULL;
  1983. wasm_importtype_t *import_type = NULL;
  1984. if (!module || !out) {
  1985. return;
  1986. }
  1987. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  1988. return;
  1989. #if WASM_ENABLE_INTERP != 0
  1990. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1991. import_func_count = MODULE_INTERP(module)->import_function_count;
  1992. import_global_count = MODULE_INTERP(module)->import_global_count;
  1993. import_memory_count = MODULE_INTERP(module)->import_memory_count;
  1994. import_table_count = MODULE_INTERP(module)->import_table_count;
  1995. }
  1996. #endif
  1997. #if WASM_ENABLE_AOT != 0
  1998. if ((*module)->module_type == Wasm_Module_AoT) {
  1999. import_func_count = MODULE_AOT(module)->import_func_count;
  2000. import_global_count = MODULE_AOT(module)->import_global_count;
  2001. import_memory_count = MODULE_AOT(module)->import_memory_count;
  2002. import_table_count = MODULE_AOT(module)->import_table_count;
  2003. }
  2004. #endif
  2005. import_count = import_func_count + import_global_count + import_table_count
  2006. + import_memory_count;
  2007. wasm_importtype_vec_new_uninitialized(out, import_count);
  2008. /*
  2009. * a wrong combination of module filetype and compilation flags
  2010. * also leads to below branch
  2011. */
  2012. if (!out->data) {
  2013. return;
  2014. }
  2015. for (i = 0; i != import_count; ++i) {
  2016. char *module_name_rt = NULL, *field_name_rt = NULL;
  2017. memset(&module_name, 0, sizeof(wasm_val_vec_t));
  2018. memset(&name, 0, sizeof(wasm_val_vec_t));
  2019. extern_type = NULL;
  2020. import_type = NULL;
  2021. if (i < import_func_count) {
  2022. wasm_functype_t *type = NULL;
  2023. WASMType *type_rt = NULL;
  2024. #if WASM_ENABLE_INTERP != 0
  2025. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2026. WASMImport *import =
  2027. MODULE_INTERP(module)->import_functions + i;
  2028. module_name_rt = import->u.names.module_name;
  2029. field_name_rt = import->u.names.field_name;
  2030. type_rt = import->u.function.func_type;
  2031. }
  2032. #endif
  2033. #if WASM_ENABLE_AOT != 0
  2034. if ((*module)->module_type == Wasm_Module_AoT) {
  2035. AOTImportFunc *import = MODULE_AOT(module)->import_funcs + i;
  2036. module_name_rt = import->module_name;
  2037. field_name_rt = import->func_name;
  2038. type_rt = import->func_type;
  2039. }
  2040. #endif
  2041. if (!module_name_rt || !field_name_rt || !type_rt) {
  2042. continue;
  2043. }
  2044. if (!(type = wasm_functype_new_internal(type_rt))) {
  2045. goto failed;
  2046. }
  2047. extern_type = wasm_functype_as_externtype(type);
  2048. }
  2049. else if (i < import_func_count + import_global_count) {
  2050. wasm_globaltype_t *type = NULL;
  2051. uint8 val_type_rt = 0;
  2052. bool mutability_rt = 0;
  2053. #if WASM_ENABLE_INTERP != 0
  2054. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2055. WASMImport *import = MODULE_INTERP(module)->import_globals
  2056. + (i - import_func_count);
  2057. module_name_rt = import->u.names.module_name;
  2058. field_name_rt = import->u.names.field_name;
  2059. val_type_rt = import->u.global.type;
  2060. mutability_rt = import->u.global.is_mutable;
  2061. }
  2062. #endif
  2063. #if WASM_ENABLE_AOT != 0
  2064. if ((*module)->module_type == Wasm_Module_AoT) {
  2065. AOTImportGlobal *import = MODULE_AOT(module)->import_globals
  2066. + (i - import_func_count);
  2067. module_name_rt = import->module_name;
  2068. field_name_rt = import->global_name;
  2069. val_type_rt = import->type;
  2070. mutability_rt = import->is_mutable;
  2071. }
  2072. #endif
  2073. if (!module_name_rt || !field_name_rt) {
  2074. continue;
  2075. }
  2076. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2077. mutability_rt))) {
  2078. goto failed;
  2079. }
  2080. extern_type = wasm_globaltype_as_externtype(type);
  2081. }
  2082. else if (i < import_func_count + import_global_count
  2083. + import_memory_count) {
  2084. wasm_memorytype_t *type = NULL;
  2085. uint32 min_page = 0, max_page = 0;
  2086. #if WASM_ENABLE_INTERP != 0
  2087. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2088. WASMImport *import =
  2089. MODULE_INTERP(module)->import_memories
  2090. + (i - import_func_count - import_global_count);
  2091. module_name_rt = import->u.names.module_name;
  2092. field_name_rt = import->u.names.field_name;
  2093. min_page = import->u.memory.init_page_count;
  2094. max_page = import->u.memory.max_page_count;
  2095. }
  2096. #endif
  2097. #if WASM_ENABLE_AOT != 0
  2098. if ((*module)->module_type == Wasm_Module_AoT) {
  2099. AOTImportMemory *import =
  2100. MODULE_AOT(module)->import_memories
  2101. + (i - import_func_count - import_global_count);
  2102. module_name_rt = import->module_name;
  2103. field_name_rt = import->memory_name;
  2104. min_page = import->mem_init_page_count;
  2105. max_page = import->mem_max_page_count;
  2106. }
  2107. #endif
  2108. if (!module_name_rt || !field_name_rt) {
  2109. continue;
  2110. }
  2111. if (!(type = wasm_memorytype_new_internal(min_page, max_page))) {
  2112. goto failed;
  2113. }
  2114. extern_type = wasm_memorytype_as_externtype(type);
  2115. }
  2116. else {
  2117. wasm_tabletype_t *type = NULL;
  2118. uint8 elem_type_rt = 0;
  2119. uint32 min_size = 0, max_size = 0;
  2120. #if WASM_ENABLE_INTERP != 0
  2121. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2122. WASMImport *import =
  2123. MODULE_INTERP(module)->import_tables
  2124. + (i - import_func_count - import_global_count
  2125. - import_memory_count);
  2126. module_name_rt = import->u.names.module_name;
  2127. field_name_rt = import->u.names.field_name;
  2128. elem_type_rt = import->u.table.elem_type;
  2129. min_size = import->u.table.init_size;
  2130. max_size = import->u.table.max_size;
  2131. }
  2132. #endif
  2133. #if WASM_ENABLE_AOT != 0
  2134. if ((*module)->module_type == Wasm_Module_AoT) {
  2135. AOTImportTable *import =
  2136. MODULE_AOT(module)->import_tables
  2137. + (i - import_func_count - import_global_count
  2138. - import_memory_count);
  2139. module_name_rt = import->module_name;
  2140. field_name_rt = import->table_name;
  2141. elem_type_rt = import->elem_type;
  2142. min_size = import->table_init_size;
  2143. max_size = import->table_max_size;
  2144. }
  2145. #endif
  2146. if (!module_name_rt || !field_name_rt) {
  2147. continue;
  2148. }
  2149. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2150. max_size))) {
  2151. goto failed;
  2152. }
  2153. extern_type = wasm_tabletype_as_externtype(type);
  2154. }
  2155. bh_assert(extern_type);
  2156. wasm_name_new_from_string_nt(&module_name, module_name_rt);
  2157. if (strlen(module_name_rt) && !module_name.data) {
  2158. goto failed;
  2159. }
  2160. wasm_name_new_from_string_nt(&name, field_name_rt);
  2161. if (strlen(field_name_rt) && !name.data) {
  2162. goto failed;
  2163. }
  2164. if (!(import_type =
  2165. wasm_importtype_new(&module_name, &name, extern_type))) {
  2166. goto failed;
  2167. }
  2168. if (!bh_vector_append((Vector *)out, &import_type)) {
  2169. goto failed_importtype_new;
  2170. }
  2171. continue;
  2172. failed:
  2173. wasm_byte_vec_delete(&module_name);
  2174. wasm_byte_vec_delete(&name);
  2175. wasm_externtype_delete(extern_type);
  2176. failed_importtype_new:
  2177. wasm_importtype_delete(import_type);
  2178. }
  2179. }
  2180. void
  2181. wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
  2182. {
  2183. uint32 i, export_count = 0;
  2184. wasm_byte_vec_t name = { 0 };
  2185. wasm_externtype_t *extern_type = NULL;
  2186. wasm_exporttype_t *export_type = NULL;
  2187. if (!module || !out) {
  2188. return;
  2189. }
  2190. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2191. return;
  2192. #if WASM_ENABLE_INTERP != 0
  2193. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2194. export_count = MODULE_INTERP(module)->export_count;
  2195. }
  2196. #endif
  2197. #if WASM_ENABLE_AOT != 0
  2198. if ((*module)->module_type == Wasm_Module_AoT) {
  2199. export_count = MODULE_AOT(module)->export_count;
  2200. }
  2201. #endif
  2202. wasm_exporttype_vec_new_uninitialized(out, export_count);
  2203. /*
  2204. * a wrong combination of module filetype and compilation flags
  2205. * also leads to below branch
  2206. */
  2207. if (!out->data) {
  2208. return;
  2209. }
  2210. for (i = 0; i != export_count; i++) {
  2211. WASMExport *export = NULL;
  2212. #if WASM_ENABLE_INTERP != 0
  2213. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2214. export = MODULE_INTERP(module)->exports + i;
  2215. }
  2216. #endif
  2217. #if WASM_ENABLE_AOT != 0
  2218. if ((*module)->module_type == Wasm_Module_AoT) {
  2219. export = MODULE_AOT(module)->exports + i;
  2220. }
  2221. #endif
  2222. if (!export) {
  2223. continue;
  2224. }
  2225. /* byte* -> wasm_byte_vec_t */
  2226. wasm_name_new_from_string_nt(&name, export->name);
  2227. if (strlen(export->name) && !name.data) {
  2228. goto failed;
  2229. }
  2230. /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t,
  2231. * wasm_globaltype_t) -> wasm_externtype_t*/
  2232. switch (export->kind) {
  2233. case EXPORT_KIND_FUNC:
  2234. {
  2235. wasm_functype_t *type = NULL;
  2236. WASMType *type_rt;
  2237. if (!wasm_runtime_get_export_func_type(*module, export,
  2238. &type_rt)) {
  2239. goto failed;
  2240. }
  2241. if (!(type = wasm_functype_new_internal(type_rt))) {
  2242. goto failed;
  2243. }
  2244. extern_type = wasm_functype_as_externtype(type);
  2245. break;
  2246. }
  2247. case EXPORT_KIND_GLOBAL:
  2248. {
  2249. wasm_globaltype_t *type = NULL;
  2250. uint8 val_type_rt = 0;
  2251. bool mutability_rt = 0;
  2252. if (!wasm_runtime_get_export_global_type(
  2253. *module, export, &val_type_rt, &mutability_rt)) {
  2254. goto failed;
  2255. }
  2256. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2257. mutability_rt))) {
  2258. goto failed;
  2259. }
  2260. extern_type = wasm_globaltype_as_externtype(type);
  2261. break;
  2262. }
  2263. case EXPORT_KIND_MEMORY:
  2264. {
  2265. wasm_memorytype_t *type = NULL;
  2266. uint32 min_page = 0, max_page = 0;
  2267. if (!wasm_runtime_get_export_memory_type(
  2268. *module, export, &min_page, &max_page)) {
  2269. goto failed;
  2270. }
  2271. if (!(type =
  2272. wasm_memorytype_new_internal(min_page, max_page))) {
  2273. goto failed;
  2274. }
  2275. extern_type = wasm_memorytype_as_externtype(type);
  2276. break;
  2277. }
  2278. case EXPORT_KIND_TABLE:
  2279. {
  2280. wasm_tabletype_t *type = NULL;
  2281. uint8 elem_type_rt = 0;
  2282. uint32 min_size = 0, max_size = 0;
  2283. if (!wasm_runtime_get_export_table_type(
  2284. *module, export, &elem_type_rt, &min_size, &max_size)) {
  2285. goto failed;
  2286. }
  2287. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2288. max_size))) {
  2289. goto failed;
  2290. }
  2291. extern_type = wasm_tabletype_as_externtype(type);
  2292. break;
  2293. }
  2294. default:
  2295. {
  2296. LOG_WARNING("%s meets unsupported type %u", __FUNCTION__,
  2297. export->kind);
  2298. break;
  2299. }
  2300. }
  2301. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  2302. goto failed;
  2303. }
  2304. if (!(bh_vector_append((Vector *)out, &export_type))) {
  2305. goto failed_exporttype_new;
  2306. }
  2307. }
  2308. return;
  2309. failed:
  2310. wasm_byte_vec_delete(&name);
  2311. wasm_externtype_delete(extern_type);
  2312. failed_exporttype_new:
  2313. wasm_exporttype_delete(export_type);
  2314. wasm_exporttype_vec_delete(out);
  2315. }
  2316. #if WASM_ENABLE_JIT == 0 || WASM_ENABLE_LAZY_JIT != 0
  2317. void
  2318. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2319. {
  2320. (void)module;
  2321. (void)out;
  2322. LOG_ERROR("only supported serialization in JIT with eager compilation");
  2323. }
  2324. own wasm_module_t *
  2325. wasm_module_deserialize(wasm_store_t *module, const wasm_byte_vec_t *binary)
  2326. {
  2327. (void)module;
  2328. (void)binary;
  2329. LOG_ERROR("only supported deserialization in JIT with eager compilation");
  2330. return NULL;
  2331. }
  2332. #else
  2333. extern uint8 *
  2334. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  2335. uint32 *p_aot_file_size);
  2336. void
  2337. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2338. {
  2339. wasm_module_ex_t *module_ex;
  2340. AOTCompContext *comp_ctx;
  2341. AOTCompData *comp_data;
  2342. uint8 *aot_file_buf = NULL;
  2343. uint32 aot_file_size = 0;
  2344. if (!module || !out)
  2345. return;
  2346. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2347. return;
  2348. module_ex = module_to_module_ext(module);
  2349. comp_ctx = ((WASMModule *)(module_ex->module_comm_rt))->comp_ctx;
  2350. comp_data = ((WASMModule *)(module_ex->module_comm_rt))->comp_data;
  2351. bh_assert(comp_ctx != NULL && comp_data != NULL);
  2352. aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size);
  2353. if (!aot_file_buf)
  2354. return;
  2355. wasm_byte_vec_new(out, aot_file_size, (wasm_byte_t *)aot_file_buf);
  2356. wasm_runtime_free(aot_file_buf);
  2357. return;
  2358. }
  2359. own wasm_module_t *
  2360. wasm_module_deserialize(wasm_store_t *store, const wasm_byte_vec_t *binary)
  2361. {
  2362. return wasm_module_new(store, binary);
  2363. }
  2364. #endif
  2365. wasm_module_t *
  2366. wasm_module_obtain(wasm_store_t *store, wasm_shared_module_t *shared_module)
  2367. {
  2368. wasm_module_ex_t *module_ex = NULL;
  2369. if (!store || !shared_module)
  2370. return NULL;
  2371. module_ex = (wasm_module_ex_t *)shared_module;
  2372. os_mutex_lock(&module_ex->lock);
  2373. /* deleting the module... */
  2374. if (module_ex->ref_count == 0) {
  2375. LOG_WARNING("wasm_module_obtain re-enter a module under deleting.");
  2376. os_mutex_unlock(&module_ex->lock);
  2377. return NULL;
  2378. }
  2379. /* add it to a watching list in store */
  2380. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  2381. os_mutex_unlock(&module_ex->lock);
  2382. return NULL;
  2383. }
  2384. module_ex->ref_count++;
  2385. os_mutex_unlock(&module_ex->lock);
  2386. return (wasm_module_t *)shared_module;
  2387. }
  2388. wasm_shared_module_t *
  2389. wasm_module_share(wasm_module_t *module)
  2390. {
  2391. wasm_module_ex_t *module_ex = NULL;
  2392. if (!module)
  2393. return NULL;
  2394. module_ex = (wasm_module_ex_t *)module;
  2395. os_mutex_lock(&module_ex->lock);
  2396. /* deleting the module... */
  2397. if (module_ex->ref_count == 0) {
  2398. LOG_WARNING("wasm_module_share re-enter a module under deleting.");
  2399. os_mutex_unlock(&module_ex->lock);
  2400. return NULL;
  2401. }
  2402. module_ex->ref_count++;
  2403. os_mutex_unlock(&module_ex->lock);
  2404. return (wasm_shared_module_t *)module;
  2405. }
  2406. void
  2407. wasm_shared_module_delete(own wasm_shared_module_t *shared_module)
  2408. {
  2409. wasm_module_delete_internal((wasm_module_t *)shared_module);
  2410. }
  2411. static wasm_func_t *
  2412. wasm_func_new_basic(wasm_store_t *store, const wasm_functype_t *type,
  2413. wasm_func_callback_t func_callback)
  2414. {
  2415. wasm_func_t *func = NULL;
  2416. if (!type) {
  2417. goto failed;
  2418. }
  2419. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2420. goto failed;
  2421. }
  2422. func->store = store;
  2423. func->kind = WASM_EXTERN_FUNC;
  2424. func->func_idx_rt = (uint16)-1;
  2425. func->with_env = false;
  2426. func->u.cb = func_callback;
  2427. if (!(func->type = wasm_functype_copy(type))) {
  2428. goto failed;
  2429. }
  2430. RETURN_OBJ(func, wasm_func_delete)
  2431. }
  2432. static wasm_func_t *
  2433. wasm_func_new_with_env_basic(wasm_store_t *store, const wasm_functype_t *type,
  2434. wasm_func_callback_with_env_t callback, void *env,
  2435. void (*finalizer)(void *))
  2436. {
  2437. wasm_func_t *func = NULL;
  2438. if (!type) {
  2439. goto failed;
  2440. }
  2441. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2442. goto failed;
  2443. }
  2444. func->store = store;
  2445. func->kind = WASM_EXTERN_FUNC;
  2446. func->func_idx_rt = (uint16)-1;
  2447. func->with_env = true;
  2448. func->u.cb_env.cb = callback;
  2449. func->u.cb_env.env = env;
  2450. func->u.cb_env.finalizer = finalizer;
  2451. if (!(func->type = wasm_functype_copy(type))) {
  2452. goto failed;
  2453. }
  2454. RETURN_OBJ(func, wasm_func_delete)
  2455. }
  2456. wasm_func_t *
  2457. wasm_func_new(wasm_store_t *store, const wasm_functype_t *type,
  2458. wasm_func_callback_t callback)
  2459. {
  2460. bh_assert(singleton_engine);
  2461. if (!callback) {
  2462. return NULL;
  2463. }
  2464. return wasm_func_new_basic(store, type, callback);
  2465. }
  2466. wasm_func_t *
  2467. wasm_func_new_with_env(wasm_store_t *store, const wasm_functype_t *type,
  2468. wasm_func_callback_with_env_t callback, void *env,
  2469. void (*finalizer)(void *))
  2470. {
  2471. bh_assert(singleton_engine);
  2472. if (!callback) {
  2473. return NULL;
  2474. }
  2475. return wasm_func_new_with_env_basic(store, type, callback, env, finalizer);
  2476. }
  2477. wasm_func_t *
  2478. wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
  2479. WASMModuleInstanceCommon *inst_comm_rt)
  2480. {
  2481. wasm_func_t *func = NULL;
  2482. WASMType *type_rt = NULL;
  2483. bh_assert(singleton_engine);
  2484. if (!inst_comm_rt) {
  2485. return NULL;
  2486. }
  2487. func = malloc_internal(sizeof(wasm_func_t));
  2488. if (!func) {
  2489. goto failed;
  2490. }
  2491. func->kind = WASM_EXTERN_FUNC;
  2492. #if WASM_ENABLE_INTERP != 0
  2493. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2494. bh_assert(func_idx_rt
  2495. < ((WASMModuleInstance *)inst_comm_rt)->e->function_count);
  2496. WASMFunctionInstance *func_interp =
  2497. ((WASMModuleInstance *)inst_comm_rt)->e->functions + func_idx_rt;
  2498. type_rt = func_interp->is_import_func
  2499. ? func_interp->u.func_import->func_type
  2500. : func_interp->u.func->func_type;
  2501. }
  2502. #endif
  2503. #if WASM_ENABLE_AOT != 0
  2504. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2505. /* use same index to trace the function type in AOTFuncType **func_types
  2506. */
  2507. AOTModule *module_aot =
  2508. (AOTModule *)((AOTModuleInstance *)inst_comm_rt)->module;
  2509. if (func_idx_rt < module_aot->import_func_count) {
  2510. type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
  2511. }
  2512. else {
  2513. type_rt =
  2514. module_aot->func_types[module_aot->func_type_indexes
  2515. [func_idx_rt
  2516. - module_aot->import_func_count]];
  2517. }
  2518. }
  2519. #endif
  2520. /*
  2521. * a wrong combination of module filetype and compilation flags
  2522. * also leads to below branch
  2523. */
  2524. if (!type_rt) {
  2525. goto failed;
  2526. }
  2527. func->type = wasm_functype_new_internal(type_rt);
  2528. if (!func->type) {
  2529. goto failed;
  2530. }
  2531. /* will add name information when processing "exports" */
  2532. func->store = store;
  2533. func->module_name = NULL;
  2534. func->name = NULL;
  2535. func->func_idx_rt = func_idx_rt;
  2536. func->inst_comm_rt = inst_comm_rt;
  2537. return func;
  2538. failed:
  2539. LOG_DEBUG("%s failed", __FUNCTION__);
  2540. wasm_func_delete(func);
  2541. return NULL;
  2542. }
  2543. static wasm_func_t *
  2544. wasm_func_new_empty(wasm_store_t *store)
  2545. {
  2546. wasm_func_t *func = NULL;
  2547. if (!(func = malloc_internal(sizeof(wasm_func_t))))
  2548. goto failed;
  2549. func->store = store;
  2550. func->kind = WASM_EXTERN_FUNC;
  2551. RETURN_OBJ(func, wasm_func_delete)
  2552. }
  2553. void
  2554. wasm_func_delete(wasm_func_t *func)
  2555. {
  2556. if (!func) {
  2557. return;
  2558. }
  2559. if (func->type) {
  2560. wasm_functype_delete(func->type);
  2561. func->type = NULL;
  2562. }
  2563. if (func->with_env) {
  2564. if (func->u.cb_env.finalizer) {
  2565. func->u.cb_env.finalizer(func->u.cb_env.env);
  2566. func->u.cb_env.finalizer = NULL;
  2567. func->u.cb_env.env = NULL;
  2568. }
  2569. }
  2570. DELETE_HOST_INFO(func)
  2571. wasm_runtime_free(func);
  2572. }
  2573. own wasm_func_t *
  2574. wasm_func_copy(const wasm_func_t *func)
  2575. {
  2576. wasm_func_t *cloned = NULL;
  2577. if (!func) {
  2578. return NULL;
  2579. }
  2580. if (!(cloned = func->with_env ? wasm_func_new_with_env_basic(
  2581. func->store, func->type, func->u.cb_env.cb,
  2582. func->u.cb_env.env, func->u.cb_env.finalizer)
  2583. : wasm_func_new_basic(func->store, func->type,
  2584. func->u.cb))) {
  2585. goto failed;
  2586. }
  2587. cloned->func_idx_rt = func->func_idx_rt;
  2588. cloned->inst_comm_rt = func->inst_comm_rt;
  2589. RETURN_OBJ(cloned, wasm_func_delete)
  2590. }
  2591. own wasm_functype_t *
  2592. wasm_func_type(const wasm_func_t *func)
  2593. {
  2594. if (!func) {
  2595. return NULL;
  2596. }
  2597. return wasm_functype_copy(func->type);
  2598. }
  2599. static bool
  2600. params_to_argv(const wasm_val_vec_t *params,
  2601. const wasm_valtype_vec_t *param_defs, uint32 *argv,
  2602. uint32 *ptr_argc)
  2603. {
  2604. size_t i = 0;
  2605. if (!param_defs->num_elems) {
  2606. return true;
  2607. }
  2608. if (!params || !params->num_elems || !params->size || !params->data) {
  2609. LOG_ERROR("the parameter params is invalid");
  2610. return false;
  2611. }
  2612. *ptr_argc = 0;
  2613. for (i = 0; i < param_defs->num_elems; ++i) {
  2614. const wasm_val_t *param = params->data + i;
  2615. bh_assert((*(param_defs->data + i))->kind == param->kind);
  2616. switch (param->kind) {
  2617. case WASM_I32:
  2618. *(int32 *)argv = param->of.i32;
  2619. argv += 1;
  2620. *ptr_argc += 1;
  2621. break;
  2622. case WASM_I64:
  2623. *(int64 *)argv = param->of.i64;
  2624. argv += 2;
  2625. *ptr_argc += 2;
  2626. break;
  2627. case WASM_F32:
  2628. *(float32 *)argv = param->of.f32;
  2629. argv += 1;
  2630. *ptr_argc += 1;
  2631. break;
  2632. case WASM_F64:
  2633. *(float64 *)argv = param->of.f64;
  2634. argv += 2;
  2635. *ptr_argc += 2;
  2636. break;
  2637. #if WASM_ENABLE_REF_TYPES != 0
  2638. case WASM_ANYREF:
  2639. *(uintptr_t *)argv = (uintptr_t)param->of.ref;
  2640. argv += sizeof(uintptr_t) / sizeof(uint32);
  2641. *ptr_argc += 1;
  2642. break;
  2643. #endif
  2644. default:
  2645. LOG_WARNING("unexpected parameter val type %d", param->kind);
  2646. return false;
  2647. }
  2648. }
  2649. return true;
  2650. }
  2651. static bool
  2652. argv_to_results(const uint32 *argv, const wasm_valtype_vec_t *result_defs,
  2653. wasm_val_vec_t *results)
  2654. {
  2655. size_t i = 0, argv_i = 0;
  2656. wasm_val_t *result;
  2657. if (!result_defs->num_elems) {
  2658. return true;
  2659. }
  2660. if (!results || !results->size || !results->data) {
  2661. LOG_ERROR("the parameter results is invalid");
  2662. return false;
  2663. }
  2664. for (i = 0, result = results->data, argv_i = 0; i < result_defs->num_elems;
  2665. i++, result++) {
  2666. switch (result_defs->data[i]->kind) {
  2667. case WASM_I32:
  2668. {
  2669. result->kind = WASM_I32;
  2670. result->of.i32 = *(int32 *)(argv + argv_i);
  2671. argv_i += 1;
  2672. break;
  2673. }
  2674. case WASM_I64:
  2675. {
  2676. result->kind = WASM_I64;
  2677. result->of.i64 = *(int64 *)(argv + argv_i);
  2678. argv_i += 2;
  2679. break;
  2680. }
  2681. case WASM_F32:
  2682. {
  2683. result->kind = WASM_F32;
  2684. result->of.f32 = *(float32 *)(argv + argv_i);
  2685. argv_i += 1;
  2686. break;
  2687. }
  2688. case WASM_F64:
  2689. {
  2690. result->kind = WASM_F64;
  2691. result->of.f64 = *(float64 *)(argv + argv_i);
  2692. argv_i += 2;
  2693. break;
  2694. }
  2695. #if WASM_ENABLE_REF_TYPES != 0
  2696. case WASM_ANYREF:
  2697. {
  2698. result->kind = WASM_ANYREF;
  2699. result->of.ref =
  2700. (struct wasm_ref_t *)(*(uintptr_t *)(argv + argv_i));
  2701. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2702. break;
  2703. }
  2704. #endif
  2705. default:
  2706. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  2707. result_defs->data[i]->kind);
  2708. return false;
  2709. }
  2710. }
  2711. return true;
  2712. }
  2713. wasm_trap_t *
  2714. wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
  2715. wasm_val_vec_t *results)
  2716. {
  2717. /* parameters count as if all are uint32 */
  2718. /* a int64 or float64 parameter means 2 */
  2719. uint32 argc = 0;
  2720. /* a parameter list and a return value list */
  2721. uint32 argv_buf[32] = { 0 }, *argv = argv_buf;
  2722. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  2723. WASMExecEnv *exec_env = NULL;
  2724. size_t param_count, result_count, alloc_count;
  2725. if (!func) {
  2726. return NULL;
  2727. }
  2728. if (!func->inst_comm_rt) {
  2729. wasm_name_t message = { 0 };
  2730. wasm_trap_t *trap;
  2731. wasm_name_new_from_string_nt(&message,
  2732. "failed to call unlinked function");
  2733. trap = wasm_trap_new(func->store, &message);
  2734. wasm_byte_vec_delete(&message);
  2735. return trap;
  2736. }
  2737. bh_assert(func->type);
  2738. #if WASM_ENABLE_INTERP != 0
  2739. if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2740. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->e->functions
  2741. + func->func_idx_rt;
  2742. }
  2743. #endif
  2744. #if WASM_ENABLE_AOT != 0
  2745. if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2746. if (!(func_comm_rt = func->func_comm_rt)) {
  2747. AOTModuleInstance *inst_aot =
  2748. (AOTModuleInstance *)func->inst_comm_rt;
  2749. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2750. uint32 export_i = 0, export_func_j = 0;
  2751. for (; export_i < module_aot->export_count; ++export_i) {
  2752. AOTExport *export = module_aot->exports + export_i;
  2753. if (export->kind == EXPORT_KIND_FUNC) {
  2754. if (export->index == func->func_idx_rt) {
  2755. func_comm_rt =
  2756. (AOTFunctionInstance *)inst_aot->export_functions
  2757. + export_func_j;
  2758. ((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
  2759. break;
  2760. }
  2761. export_func_j++;
  2762. }
  2763. }
  2764. }
  2765. }
  2766. #endif
  2767. /*
  2768. * a wrong combination of module filetype and compilation flags
  2769. * also leads to below branch
  2770. */
  2771. if (!func_comm_rt) {
  2772. goto failed;
  2773. }
  2774. param_count = wasm_func_param_arity(func);
  2775. result_count = wasm_func_result_arity(func);
  2776. alloc_count = (param_count > result_count) ? param_count : result_count;
  2777. if (alloc_count > (size_t)sizeof(argv_buf) / sizeof(uint64)) {
  2778. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  2779. goto failed;
  2780. }
  2781. }
  2782. /* copy parametes */
  2783. if (param_count
  2784. && !params_to_argv(params, wasm_functype_params(func->type), argv,
  2785. &argc)) {
  2786. goto failed;
  2787. }
  2788. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2789. exec_env = wasm_runtime_get_exec_env_tls();
  2790. #endif
  2791. #if WASM_ENABLE_THREAD_MGR != 0
  2792. if (!exec_env) {
  2793. exec_env = wasm_clusters_search_exec_env(func->inst_comm_rt);
  2794. }
  2795. #endif
  2796. if (!exec_env) {
  2797. exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
  2798. }
  2799. if (!exec_env) {
  2800. goto failed;
  2801. }
  2802. wasm_runtime_set_exception(func->inst_comm_rt, NULL);
  2803. if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
  2804. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2805. LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
  2806. goto failed;
  2807. }
  2808. }
  2809. /* copy results */
  2810. if (result_count) {
  2811. if (!argv_to_results(argv, wasm_functype_results(func->type),
  2812. results)) {
  2813. goto failed;
  2814. }
  2815. results->num_elems = result_count;
  2816. results->size = result_count;
  2817. }
  2818. if (argv != argv_buf)
  2819. wasm_runtime_free(argv);
  2820. return NULL;
  2821. failed:
  2822. if (argv != argv_buf)
  2823. wasm_runtime_free(argv);
  2824. return wasm_trap_new_internal(
  2825. func->store, func->inst_comm_rt,
  2826. wasm_runtime_get_exception(func->inst_comm_rt));
  2827. }
  2828. size_t
  2829. wasm_func_param_arity(const wasm_func_t *func)
  2830. {
  2831. if (!func || !func->type || !func->type->params) {
  2832. return 0;
  2833. }
  2834. return func->type->params->num_elems;
  2835. }
  2836. size_t
  2837. wasm_func_result_arity(const wasm_func_t *func)
  2838. {
  2839. if (!func || !func->type || !func->type->results) {
  2840. return 0;
  2841. }
  2842. return func->type->results->num_elems;
  2843. }
  2844. wasm_global_t *
  2845. wasm_global_new(wasm_store_t *store, const wasm_globaltype_t *global_type,
  2846. const wasm_val_t *init)
  2847. {
  2848. wasm_global_t *global = NULL;
  2849. bh_assert(singleton_engine);
  2850. if (!global_type || !init) {
  2851. goto failed;
  2852. }
  2853. global = malloc_internal(sizeof(wasm_global_t));
  2854. if (!global) {
  2855. goto failed;
  2856. }
  2857. global->store = store;
  2858. global->kind = WASM_EXTERN_GLOBAL;
  2859. global->type = wasm_globaltype_copy(global_type);
  2860. if (!global->type) {
  2861. goto failed;
  2862. }
  2863. global->init = malloc_internal(sizeof(wasm_val_t));
  2864. if (!global->init) {
  2865. goto failed;
  2866. }
  2867. wasm_val_copy(global->init, init);
  2868. /* TODO: how to check if above is failed */
  2869. return global;
  2870. failed:
  2871. LOG_DEBUG("%s failed", __FUNCTION__);
  2872. wasm_global_delete(global);
  2873. return NULL;
  2874. }
  2875. static wasm_global_t *
  2876. wasm_global_new_empty(wasm_store_t *store)
  2877. {
  2878. wasm_global_t *global = NULL;
  2879. global = malloc_internal(sizeof(wasm_global_t));
  2880. if (!global)
  2881. goto failed;
  2882. global->store = store;
  2883. global->kind = WASM_EXTERN_GLOBAL;
  2884. return global;
  2885. failed:
  2886. LOG_DEBUG("%s failed", __FUNCTION__);
  2887. wasm_global_delete(global);
  2888. return NULL;
  2889. }
  2890. /* almost same with wasm_global_new */
  2891. wasm_global_t *
  2892. wasm_global_copy(const wasm_global_t *src)
  2893. {
  2894. wasm_global_t *global = NULL;
  2895. if (!src) {
  2896. return NULL;
  2897. }
  2898. global = malloc_internal(sizeof(wasm_global_t));
  2899. if (!global) {
  2900. goto failed;
  2901. }
  2902. global->kind = WASM_EXTERN_GLOBAL;
  2903. global->type = wasm_globaltype_copy(src->type);
  2904. if (!global->type) {
  2905. goto failed;
  2906. }
  2907. global->init = malloc_internal(sizeof(wasm_val_t));
  2908. if (!global->init) {
  2909. goto failed;
  2910. }
  2911. wasm_val_copy(global->init, src->init);
  2912. global->global_idx_rt = src->global_idx_rt;
  2913. global->inst_comm_rt = src->inst_comm_rt;
  2914. return global;
  2915. failed:
  2916. LOG_DEBUG("%s failed", __FUNCTION__);
  2917. wasm_global_delete(global);
  2918. return NULL;
  2919. }
  2920. void
  2921. wasm_global_delete(wasm_global_t *global)
  2922. {
  2923. if (!global) {
  2924. return;
  2925. }
  2926. if (global->init) {
  2927. wasm_val_delete(global->init);
  2928. global->init = NULL;
  2929. }
  2930. if (global->type) {
  2931. wasm_globaltype_delete(global->type);
  2932. global->type = NULL;
  2933. }
  2934. DELETE_HOST_INFO(global)
  2935. wasm_runtime_free(global);
  2936. }
  2937. #if WASM_ENABLE_INTERP != 0
  2938. static bool
  2939. interp_global_set(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2940. const wasm_val_t *v)
  2941. {
  2942. const WASMGlobalInstance *global_interp =
  2943. inst_interp->e->globals + global_idx_rt;
  2944. uint8 val_type_rt = global_interp->type;
  2945. #if WASM_ENABLE_MULTI_MODULE != 0
  2946. uint8 *data = global_interp->import_global_inst
  2947. ? global_interp->import_module_inst->global_data
  2948. + global_interp->import_global_inst->data_offset
  2949. : inst_interp->global_data + global_interp->data_offset;
  2950. #else
  2951. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2952. #endif
  2953. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_interp,
  2954. val_type_rt, v, data);
  2955. }
  2956. static bool
  2957. interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2958. wasm_val_t *out)
  2959. {
  2960. WASMGlobalInstance *global_interp = inst_interp->e->globals + global_idx_rt;
  2961. uint8 val_type_rt = global_interp->type;
  2962. #if WASM_ENABLE_MULTI_MODULE != 0
  2963. uint8 *data = global_interp->import_global_inst
  2964. ? global_interp->import_module_inst->global_data
  2965. + global_interp->import_global_inst->data_offset
  2966. : inst_interp->global_data + global_interp->data_offset;
  2967. #else
  2968. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2969. #endif
  2970. return rt_val_to_wasm_val(data, val_type_rt, out);
  2971. }
  2972. #endif
  2973. #if WASM_ENABLE_AOT != 0
  2974. static bool
  2975. aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  2976. const wasm_val_t *v)
  2977. {
  2978. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2979. uint8 val_type_rt = 0;
  2980. uint32 data_offset = 0;
  2981. void *data = NULL;
  2982. if (global_idx_rt < module_aot->import_global_count) {
  2983. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2984. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2985. }
  2986. else {
  2987. data_offset =
  2988. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2989. .data_offset;
  2990. val_type_rt =
  2991. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2992. .type;
  2993. }
  2994. data = (void *)(inst_aot->global_data + data_offset);
  2995. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt,
  2996. v, data);
  2997. }
  2998. static bool
  2999. aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  3000. wasm_val_t *out)
  3001. {
  3002. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3003. uint8 val_type_rt = 0;
  3004. uint32 data_offset = 0;
  3005. uint8 *data = NULL;
  3006. if (global_idx_rt < module_aot->import_global_count) {
  3007. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  3008. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  3009. }
  3010. else {
  3011. data_offset =
  3012. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3013. .data_offset;
  3014. val_type_rt =
  3015. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3016. .type;
  3017. }
  3018. data = inst_aot->global_data + data_offset;
  3019. return rt_val_to_wasm_val(data, val_type_rt, out);
  3020. }
  3021. #endif
  3022. void
  3023. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  3024. {
  3025. if (!global || !v || !global->inst_comm_rt) {
  3026. return;
  3027. }
  3028. #if WASM_ENABLE_INTERP != 0
  3029. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3030. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  3031. global->global_idx_rt, v);
  3032. return;
  3033. }
  3034. #endif
  3035. #if WASM_ENABLE_AOT != 0
  3036. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3037. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  3038. global->global_idx_rt, v);
  3039. return;
  3040. }
  3041. #endif
  3042. /*
  3043. * a wrong combination of module filetype and compilation flags
  3044. * leads to below branch
  3045. */
  3046. UNREACHABLE();
  3047. }
  3048. void
  3049. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  3050. {
  3051. if (!global || !out) {
  3052. return;
  3053. }
  3054. if (!global->inst_comm_rt) {
  3055. return;
  3056. }
  3057. memset(out, 0, sizeof(wasm_val_t));
  3058. #if WASM_ENABLE_INTERP != 0
  3059. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3060. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  3061. global->global_idx_rt, out);
  3062. return;
  3063. }
  3064. #endif
  3065. #if WASM_ENABLE_AOT != 0
  3066. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3067. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  3068. global->global_idx_rt, out);
  3069. return;
  3070. }
  3071. #endif
  3072. /*
  3073. * a wrong combination of module filetype and compilation flags
  3074. * leads to below branch
  3075. */
  3076. UNREACHABLE();
  3077. }
  3078. wasm_global_t *
  3079. wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
  3080. WASMModuleInstanceCommon *inst_comm_rt)
  3081. {
  3082. wasm_global_t *global = NULL;
  3083. uint8 val_type_rt = 0;
  3084. bool is_mutable = 0;
  3085. bool init = false;
  3086. bh_assert(singleton_engine);
  3087. if (!inst_comm_rt) {
  3088. return NULL;
  3089. }
  3090. global = malloc_internal(sizeof(wasm_global_t));
  3091. if (!global) {
  3092. goto failed;
  3093. }
  3094. global->store = store;
  3095. global->kind = WASM_EXTERN_GLOBAL;
  3096. #if WASM_ENABLE_INTERP != 0
  3097. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3098. WASMGlobalInstance *global_interp =
  3099. ((WASMModuleInstance *)inst_comm_rt)->e->globals + global_idx_rt;
  3100. val_type_rt = global_interp->type;
  3101. is_mutable = global_interp->is_mutable;
  3102. init = true;
  3103. }
  3104. #endif
  3105. #if WASM_ENABLE_AOT != 0
  3106. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3107. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3108. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3109. init = true;
  3110. if (global_idx_rt < module_aot->import_global_count) {
  3111. AOTImportGlobal *global_import_aot =
  3112. module_aot->import_globals + global_idx_rt;
  3113. val_type_rt = global_import_aot->type;
  3114. is_mutable = global_import_aot->is_mutable;
  3115. }
  3116. else {
  3117. AOTGlobal *global_aot =
  3118. module_aot->globals
  3119. + (global_idx_rt - module_aot->import_global_count);
  3120. val_type_rt = global_aot->type;
  3121. is_mutable = global_aot->is_mutable;
  3122. }
  3123. }
  3124. #endif
  3125. /*
  3126. * a wrong combination of module filetype and compilation flags
  3127. * leads to below branch
  3128. */
  3129. if (!init) {
  3130. goto failed;
  3131. }
  3132. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  3133. if (!global->type) {
  3134. goto failed;
  3135. }
  3136. global->init = malloc_internal(sizeof(wasm_val_t));
  3137. if (!global->init) {
  3138. goto failed;
  3139. }
  3140. #if WASM_ENABLE_INTERP != 0
  3141. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3142. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  3143. global->init);
  3144. }
  3145. #endif
  3146. #if WASM_ENABLE_AOT != 0
  3147. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3148. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  3149. global->init);
  3150. }
  3151. #endif
  3152. global->inst_comm_rt = inst_comm_rt;
  3153. global->global_idx_rt = global_idx_rt;
  3154. return global;
  3155. failed:
  3156. LOG_DEBUG("%s failed", __FUNCTION__);
  3157. wasm_global_delete(global);
  3158. return NULL;
  3159. }
  3160. wasm_globaltype_t *
  3161. wasm_global_type(const wasm_global_t *global)
  3162. {
  3163. if (!global) {
  3164. return NULL;
  3165. }
  3166. return wasm_globaltype_copy(global->type);
  3167. }
  3168. static wasm_table_t *
  3169. wasm_table_new_basic(wasm_store_t *store, const wasm_tabletype_t *type)
  3170. {
  3171. wasm_table_t *table = NULL;
  3172. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3173. goto failed;
  3174. }
  3175. table->store = store;
  3176. table->kind = WASM_EXTERN_TABLE;
  3177. if (!(table->type = wasm_tabletype_copy(type))) {
  3178. goto failed;
  3179. }
  3180. RETURN_OBJ(table, wasm_table_delete);
  3181. }
  3182. wasm_table_t *
  3183. wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
  3184. WASMModuleInstanceCommon *inst_comm_rt)
  3185. {
  3186. wasm_table_t *table = NULL;
  3187. uint8 val_type_rt = 0;
  3188. uint32 init_size = 0, max_size = 0;
  3189. bh_assert(singleton_engine);
  3190. if (!inst_comm_rt) {
  3191. return NULL;
  3192. }
  3193. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3194. goto failed;
  3195. }
  3196. table->store = store;
  3197. table->kind = WASM_EXTERN_TABLE;
  3198. if (!wasm_runtime_get_table_inst_elem_type(
  3199. inst_comm_rt, table_idx_rt, &val_type_rt, &init_size, &max_size)) {
  3200. /*
  3201. * a wrong combination of module filetype and compilation flags
  3202. * leads to below branch
  3203. */
  3204. goto failed;
  3205. }
  3206. if (!(table->type =
  3207. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  3208. goto failed;
  3209. }
  3210. table->inst_comm_rt = inst_comm_rt;
  3211. table->table_idx_rt = table_idx_rt;
  3212. RETURN_OBJ(table, wasm_table_delete);
  3213. }
  3214. /* will not actually apply this new table into the runtime */
  3215. wasm_table_t *
  3216. wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *table_type,
  3217. wasm_ref_t *init)
  3218. {
  3219. wasm_table_t *table;
  3220. (void)init;
  3221. bh_assert(singleton_engine);
  3222. if ((table = wasm_table_new_basic(store, table_type))) {
  3223. table->store = store;
  3224. }
  3225. return table;
  3226. }
  3227. wasm_table_t *
  3228. wasm_table_copy(const wasm_table_t *src)
  3229. {
  3230. wasm_table_t *table;
  3231. if (!(table = wasm_table_new_basic(src->store, src->type))) {
  3232. return NULL;
  3233. }
  3234. table->table_idx_rt = src->table_idx_rt;
  3235. table->inst_comm_rt = src->inst_comm_rt;
  3236. return table;
  3237. }
  3238. void
  3239. wasm_table_delete(wasm_table_t *table)
  3240. {
  3241. if (!table) {
  3242. return;
  3243. }
  3244. if (table->type) {
  3245. wasm_tabletype_delete(table->type);
  3246. table->type = NULL;
  3247. }
  3248. DELETE_HOST_INFO(table)
  3249. wasm_runtime_free(table);
  3250. }
  3251. wasm_tabletype_t *
  3252. wasm_table_type(const wasm_table_t *table)
  3253. {
  3254. if (!table) {
  3255. return NULL;
  3256. }
  3257. return wasm_tabletype_copy(table->type);
  3258. }
  3259. own wasm_ref_t *
  3260. wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
  3261. {
  3262. uint32 ref_idx = NULL_REF;
  3263. if (!table || !table->inst_comm_rt) {
  3264. return NULL;
  3265. }
  3266. #if WASM_ENABLE_INTERP != 0
  3267. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3268. WASMTableInstance *table_interp =
  3269. ((WASMModuleInstance *)table->inst_comm_rt)
  3270. ->tables[table->table_idx_rt];
  3271. if (index >= table_interp->cur_size) {
  3272. return NULL;
  3273. }
  3274. ref_idx = table_interp->elems[index];
  3275. }
  3276. #endif
  3277. #if WASM_ENABLE_AOT != 0
  3278. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3279. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3280. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3281. if (index >= table_aot->cur_size) {
  3282. return NULL;
  3283. }
  3284. ref_idx = table_aot->elems[index];
  3285. }
  3286. #endif
  3287. /*
  3288. * a wrong combination of module filetype and compilation flags
  3289. * also leads to below branch
  3290. */
  3291. if (ref_idx == NULL_REF) {
  3292. return NULL;
  3293. }
  3294. #if WASM_ENABLE_REF_TYPES != 0
  3295. if (table->type->val_type->kind == WASM_ANYREF) {
  3296. void *externref_obj;
  3297. if (!wasm_externref_ref2obj(ref_idx, &externref_obj)) {
  3298. return NULL;
  3299. }
  3300. return externref_obj;
  3301. }
  3302. else
  3303. #endif
  3304. {
  3305. return wasm_ref_new_internal(table->store, WASM_REF_func, ref_idx,
  3306. table->inst_comm_rt);
  3307. }
  3308. }
  3309. bool
  3310. wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
  3311. own wasm_ref_t *ref)
  3312. {
  3313. uint32 *p_ref_idx = NULL;
  3314. uint32 function_count = 0;
  3315. if (!table || !table->inst_comm_rt) {
  3316. return false;
  3317. }
  3318. if (ref
  3319. #if WASM_ENABLE_REF_TYPES != 0
  3320. && !(WASM_REF_foreign == ref->kind
  3321. && WASM_ANYREF == table->type->val_type->kind)
  3322. #endif
  3323. && !(WASM_REF_func == ref->kind
  3324. && WASM_FUNCREF == table->type->val_type->kind)) {
  3325. return false;
  3326. }
  3327. #if WASM_ENABLE_INTERP != 0
  3328. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3329. WASMTableInstance *table_interp =
  3330. ((WASMModuleInstance *)table->inst_comm_rt)
  3331. ->tables[table->table_idx_rt];
  3332. if (index >= table_interp->cur_size) {
  3333. return false;
  3334. }
  3335. p_ref_idx = table_interp->elems + index;
  3336. function_count =
  3337. ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
  3338. }
  3339. #endif
  3340. #if WASM_ENABLE_AOT != 0
  3341. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3342. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3343. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3344. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3345. if (index >= table_aot->cur_size) {
  3346. return false;
  3347. }
  3348. p_ref_idx = table_aot->elems + index;
  3349. function_count = module_aot->func_count;
  3350. }
  3351. #endif
  3352. /*
  3353. * a wrong combination of module filetype and compilation flags
  3354. * leads to below branch
  3355. */
  3356. if (!p_ref_idx) {
  3357. return false;
  3358. }
  3359. #if WASM_ENABLE_REF_TYPES != 0
  3360. if (table->type->val_type->kind == WASM_ANYREF) {
  3361. return wasm_externref_obj2ref(table->inst_comm_rt, ref, p_ref_idx);
  3362. }
  3363. else
  3364. #endif
  3365. {
  3366. if (ref) {
  3367. if (NULL_REF != ref->ref_idx_rt) {
  3368. if (ref->ref_idx_rt >= function_count) {
  3369. return false;
  3370. }
  3371. }
  3372. *p_ref_idx = ref->ref_idx_rt;
  3373. wasm_ref_delete(ref);
  3374. }
  3375. else {
  3376. *p_ref_idx = NULL_REF;
  3377. }
  3378. }
  3379. return true;
  3380. }
  3381. wasm_table_size_t
  3382. wasm_table_size(const wasm_table_t *table)
  3383. {
  3384. if (!table || !table->inst_comm_rt) {
  3385. return 0;
  3386. }
  3387. #if WASM_ENABLE_INTERP != 0
  3388. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3389. WASMTableInstance *table_interp =
  3390. ((WASMModuleInstance *)table->inst_comm_rt)
  3391. ->tables[table->table_idx_rt];
  3392. return table_interp->cur_size;
  3393. }
  3394. #endif
  3395. #if WASM_ENABLE_AOT != 0
  3396. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3397. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3398. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3399. if (table->table_idx_rt < module_aot->import_table_count) {
  3400. AOTImportTable *table_aot =
  3401. module_aot->import_tables + table->table_idx_rt;
  3402. return table_aot->table_init_size;
  3403. }
  3404. else {
  3405. AOTTable *table_aot =
  3406. module_aot->tables
  3407. + (table->table_idx_rt - module_aot->import_table_count);
  3408. return table_aot->table_init_size;
  3409. }
  3410. }
  3411. #endif
  3412. /*
  3413. * a wrong combination of module filetype and compilation flags
  3414. * leads to below branch
  3415. */
  3416. return 0;
  3417. }
  3418. bool
  3419. wasm_table_grow(wasm_table_t *table, wasm_table_size_t delta,
  3420. own wasm_ref_t *init)
  3421. {
  3422. (void)table;
  3423. (void)delta;
  3424. (void)init;
  3425. LOG_WARNING("Calling wasm_table_grow() by host is not supported."
  3426. "Only allow growing a table via the opcode table.grow");
  3427. return false;
  3428. }
  3429. static wasm_memory_t *
  3430. wasm_memory_new_basic(wasm_store_t *store, const wasm_memorytype_t *type)
  3431. {
  3432. wasm_memory_t *memory = NULL;
  3433. if (!type) {
  3434. goto failed;
  3435. }
  3436. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3437. goto failed;
  3438. }
  3439. memory->store = store;
  3440. memory->kind = WASM_EXTERN_MEMORY;
  3441. memory->type = wasm_memorytype_copy(type);
  3442. RETURN_OBJ(memory, wasm_memory_delete)
  3443. }
  3444. wasm_memory_t *
  3445. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  3446. {
  3447. bh_assert(singleton_engine);
  3448. return wasm_memory_new_basic(store, type);
  3449. }
  3450. wasm_memory_t *
  3451. wasm_memory_copy(const wasm_memory_t *src)
  3452. {
  3453. wasm_memory_t *dst = NULL;
  3454. if (!src) {
  3455. return NULL;
  3456. }
  3457. if (!(dst = wasm_memory_new_basic(src->store, src->type))) {
  3458. goto failed;
  3459. }
  3460. dst->memory_idx_rt = src->memory_idx_rt;
  3461. dst->inst_comm_rt = src->inst_comm_rt;
  3462. RETURN_OBJ(dst, wasm_memory_delete)
  3463. }
  3464. wasm_memory_t *
  3465. wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
  3466. WASMModuleInstanceCommon *inst_comm_rt)
  3467. {
  3468. wasm_memory_t *memory = NULL;
  3469. uint32 min_pages = 0, max_pages = 0;
  3470. bool init_flag = false;
  3471. bh_assert(singleton_engine);
  3472. if (!inst_comm_rt) {
  3473. return NULL;
  3474. }
  3475. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3476. goto failed;
  3477. }
  3478. memory->store = store;
  3479. memory->kind = WASM_EXTERN_MEMORY;
  3480. #if WASM_ENABLE_INTERP != 0
  3481. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3482. WASMMemoryInstance *memory_interp =
  3483. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  3484. min_pages = memory_interp->cur_page_count;
  3485. max_pages = memory_interp->max_page_count;
  3486. init_flag = true;
  3487. }
  3488. #endif
  3489. #if WASM_ENABLE_AOT != 0
  3490. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3491. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3492. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3493. if (memory_idx_rt < module_aot->import_memory_count) {
  3494. min_pages = module_aot->import_memories->mem_init_page_count;
  3495. max_pages = module_aot->import_memories->mem_max_page_count;
  3496. }
  3497. else {
  3498. min_pages = module_aot->memories->mem_init_page_count;
  3499. max_pages = module_aot->memories->mem_max_page_count;
  3500. }
  3501. init_flag = true;
  3502. }
  3503. #endif
  3504. /*
  3505. * a wrong combination of module filetype and compilation flags
  3506. * leads to below branch
  3507. */
  3508. if (!init_flag) {
  3509. goto failed;
  3510. }
  3511. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  3512. goto failed;
  3513. }
  3514. memory->inst_comm_rt = inst_comm_rt;
  3515. memory->memory_idx_rt = memory_idx_rt;
  3516. RETURN_OBJ(memory, wasm_memory_delete);
  3517. }
  3518. void
  3519. wasm_memory_delete(wasm_memory_t *memory)
  3520. {
  3521. if (!memory) {
  3522. return;
  3523. }
  3524. if (memory->type) {
  3525. wasm_memorytype_delete(memory->type);
  3526. memory->type = NULL;
  3527. }
  3528. DELETE_HOST_INFO(memory)
  3529. wasm_runtime_free(memory);
  3530. }
  3531. wasm_memorytype_t *
  3532. wasm_memory_type(const wasm_memory_t *memory)
  3533. {
  3534. if (!memory) {
  3535. return NULL;
  3536. }
  3537. return wasm_memorytype_copy(memory->type);
  3538. }
  3539. byte_t *
  3540. wasm_memory_data(wasm_memory_t *memory)
  3541. {
  3542. WASMModuleInstanceCommon *module_inst_comm;
  3543. if (!memory || !memory->inst_comm_rt) {
  3544. return NULL;
  3545. }
  3546. module_inst_comm = memory->inst_comm_rt;
  3547. #if WASM_ENABLE_INTERP != 0
  3548. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3549. WASMModuleInstance *module_inst =
  3550. (WASMModuleInstance *)module_inst_comm;
  3551. WASMMemoryInstance *memory_inst =
  3552. module_inst->memories[memory->memory_idx_rt];
  3553. return (byte_t *)memory_inst->memory_data;
  3554. }
  3555. #endif
  3556. #if WASM_ENABLE_AOT != 0
  3557. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3558. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3559. AOTMemoryInstance *memory_inst =
  3560. ((AOTMemoryInstance **)
  3561. module_inst->memories)[memory->memory_idx_rt];
  3562. return (byte_t *)memory_inst->memory_data;
  3563. }
  3564. #endif
  3565. /*
  3566. * a wrong combination of module filetype and compilation flags
  3567. * leads to below branch
  3568. */
  3569. return NULL;
  3570. }
  3571. size_t
  3572. wasm_memory_data_size(const wasm_memory_t *memory)
  3573. {
  3574. WASMModuleInstanceCommon *module_inst_comm;
  3575. if (!memory || !memory->inst_comm_rt) {
  3576. return 0;
  3577. }
  3578. module_inst_comm = memory->inst_comm_rt;
  3579. #if WASM_ENABLE_INTERP != 0
  3580. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3581. WASMModuleInstance *module_inst =
  3582. (WASMModuleInstance *)module_inst_comm;
  3583. WASMMemoryInstance *memory_inst =
  3584. module_inst->memories[memory->memory_idx_rt];
  3585. return (size_t)memory_inst->cur_page_count
  3586. * memory_inst->num_bytes_per_page;
  3587. }
  3588. #endif
  3589. #if WASM_ENABLE_AOT != 0
  3590. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3591. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3592. AOTMemoryInstance *memory_inst =
  3593. ((AOTMemoryInstance **)
  3594. module_inst->memories)[memory->memory_idx_rt];
  3595. return (size_t)memory_inst->cur_page_count
  3596. * memory_inst->num_bytes_per_page;
  3597. }
  3598. #endif
  3599. /*
  3600. * a wrong combination of module filetype and compilation flags
  3601. * leads to below branch
  3602. */
  3603. return 0;
  3604. }
  3605. wasm_memory_pages_t
  3606. wasm_memory_size(const wasm_memory_t *memory)
  3607. {
  3608. WASMModuleInstanceCommon *module_inst_comm;
  3609. if (!memory || !memory->inst_comm_rt) {
  3610. return 0;
  3611. }
  3612. module_inst_comm = memory->inst_comm_rt;
  3613. #if WASM_ENABLE_INTERP != 0
  3614. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3615. WASMModuleInstance *module_inst =
  3616. (WASMModuleInstance *)module_inst_comm;
  3617. WASMMemoryInstance *memory_inst =
  3618. module_inst->memories[memory->memory_idx_rt];
  3619. return memory_inst->cur_page_count;
  3620. }
  3621. #endif
  3622. #if WASM_ENABLE_AOT != 0
  3623. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3624. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3625. AOTMemoryInstance *memory_inst =
  3626. ((AOTMemoryInstance **)
  3627. module_inst->memories)[memory->memory_idx_rt];
  3628. return memory_inst->cur_page_count;
  3629. }
  3630. #endif
  3631. /*
  3632. * a wrong combination of module filetype and compilation flags
  3633. * leads to below branch
  3634. */
  3635. return 0;
  3636. }
  3637. bool
  3638. wasm_memory_grow(wasm_memory_t *memory, wasm_memory_pages_t delta)
  3639. {
  3640. (void)memory;
  3641. (void)delta;
  3642. LOG_WARNING("Calling wasm_memory_grow() by host is not supported."
  3643. "Only allow growing a memory via the opcode memory.grow");
  3644. return false;
  3645. }
  3646. #if WASM_ENABLE_INTERP != 0
  3647. static bool
  3648. interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
  3649. uint16 func_idx_rt, wasm_func_t *import)
  3650. {
  3651. WASMImport *imported_func_interp = NULL;
  3652. bh_assert(inst && module_interp && import);
  3653. bh_assert(func_idx_rt < module_interp->import_function_count);
  3654. bh_assert(WASM_EXTERN_FUNC == import->kind);
  3655. imported_func_interp = module_interp->import_functions + func_idx_rt;
  3656. bh_assert(imported_func_interp);
  3657. bh_assert(imported_func_interp->kind == IMPORT_KIND_FUNC);
  3658. /* it is a placeholder and let's skip it*/
  3659. if (!import->type)
  3660. return true;
  3661. /* type comparison */
  3662. if (!wasm_functype_same_internal(
  3663. import->type, imported_func_interp->u.function.func_type))
  3664. return false;
  3665. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  3666. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3667. func_ptr_linked, with_env and env will be stored in module instance's
  3668. c_api_func_imports later and used when calling import function */
  3669. if (import->with_env)
  3670. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  3671. else
  3672. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  3673. bh_assert(imported_func_interp->u.function.func_ptr_linked);
  3674. import->func_idx_rt = func_idx_rt;
  3675. (void)inst;
  3676. return true;
  3677. }
  3678. static bool
  3679. interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
  3680. wasm_global_t *import)
  3681. {
  3682. WASMImport *imported_global_interp = NULL;
  3683. bh_assert(module_interp && import);
  3684. bh_assert(global_idx_rt < module_interp->import_global_count);
  3685. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  3686. imported_global_interp = module_interp->import_globals + global_idx_rt;
  3687. bh_assert(imported_global_interp);
  3688. bh_assert(imported_global_interp->kind == IMPORT_KIND_GLOBAL);
  3689. /* it is a placeholder and let's skip it*/
  3690. if (!import->type)
  3691. return true;
  3692. /* type comparison */
  3693. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
  3694. imported_global_interp->u.global.type))
  3695. return false;
  3696. /* set init value */
  3697. bh_assert(import->init);
  3698. switch (wasm_valtype_kind(import->type->val_type)) {
  3699. case WASM_I32:
  3700. imported_global_interp->u.global.global_data_linked.i32 =
  3701. import->init->of.i32;
  3702. break;
  3703. case WASM_I64:
  3704. imported_global_interp->u.global.global_data_linked.i64 =
  3705. import->init->of.i64;
  3706. break;
  3707. case WASM_F32:
  3708. imported_global_interp->u.global.global_data_linked.f32 =
  3709. import->init->of.f32;
  3710. break;
  3711. case WASM_F64:
  3712. imported_global_interp->u.global.global_data_linked.f64 =
  3713. import->init->of.f64;
  3714. break;
  3715. default:
  3716. return false;
  3717. }
  3718. import->global_idx_rt = global_idx_rt;
  3719. imported_global_interp->u.global.is_linked = true;
  3720. return true;
  3721. }
  3722. static bool
  3723. interp_process_export(wasm_store_t *store,
  3724. const WASMModuleInstance *inst_interp,
  3725. wasm_extern_vec_t *externals)
  3726. {
  3727. WASMExport *exports = NULL;
  3728. WASMExport *export = NULL;
  3729. wasm_extern_t *external = NULL;
  3730. uint32 export_cnt = 0;
  3731. uint32 i = 0;
  3732. bh_assert(store && inst_interp && inst_interp->module && externals);
  3733. exports = inst_interp->module->exports;
  3734. export_cnt = inst_interp->module->export_count;
  3735. for (i = 0; i < export_cnt; ++i) {
  3736. export = exports + i;
  3737. switch (export->kind) {
  3738. case EXPORT_KIND_FUNC:
  3739. {
  3740. wasm_func_t *func;
  3741. if (!(func = wasm_func_new_internal(
  3742. store, export->index,
  3743. (WASMModuleInstanceCommon *)inst_interp))) {
  3744. goto failed;
  3745. }
  3746. external = wasm_func_as_extern(func);
  3747. break;
  3748. }
  3749. case EXPORT_KIND_GLOBAL:
  3750. {
  3751. wasm_global_t *global;
  3752. if (!(global = wasm_global_new_internal(
  3753. store, export->index,
  3754. (WASMModuleInstanceCommon *)inst_interp))) {
  3755. goto failed;
  3756. }
  3757. external = wasm_global_as_extern(global);
  3758. break;
  3759. }
  3760. case EXPORT_KIND_TABLE:
  3761. {
  3762. wasm_table_t *table;
  3763. if (!(table = wasm_table_new_internal(
  3764. store, export->index,
  3765. (WASMModuleInstanceCommon *)inst_interp))) {
  3766. goto failed;
  3767. }
  3768. external = wasm_table_as_extern(table);
  3769. break;
  3770. }
  3771. case EXPORT_KIND_MEMORY:
  3772. {
  3773. wasm_memory_t *memory;
  3774. if (!(memory = wasm_memory_new_internal(
  3775. store, export->index,
  3776. (WASMModuleInstanceCommon *)inst_interp))) {
  3777. goto failed;
  3778. }
  3779. external = wasm_memory_as_extern(memory);
  3780. break;
  3781. }
  3782. default:
  3783. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3784. export->kind);
  3785. goto failed;
  3786. }
  3787. if (!bh_vector_append((Vector *)externals, &external)) {
  3788. goto failed;
  3789. }
  3790. }
  3791. return true;
  3792. failed:
  3793. wasm_extern_delete(external);
  3794. return false;
  3795. }
  3796. #endif /* WASM_ENABLE_INTERP */
  3797. #if WASM_ENABLE_AOT != 0
  3798. static bool
  3799. aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
  3800. uint32 import_func_idx_rt, wasm_func_t *import)
  3801. {
  3802. AOTImportFunc *import_aot_func = NULL;
  3803. bh_assert(inst && module_aot && import);
  3804. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  3805. bh_assert(import_aot_func);
  3806. /* it is a placeholder and let's skip it*/
  3807. if (!import->type)
  3808. return true;
  3809. /* type comparison */
  3810. if (!wasm_functype_same_internal(import->type, import_aot_func->func_type))
  3811. return false;
  3812. import_aot_func->call_conv_wasm_c_api = true;
  3813. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3814. func_ptr_linked, with_env and env will be stored in module instance's
  3815. c_api_func_imports later and used when calling import function */
  3816. if (import->with_env)
  3817. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  3818. else
  3819. import_aot_func->func_ptr_linked = import->u.cb;
  3820. bh_assert(import_aot_func->func_ptr_linked);
  3821. import->func_idx_rt = import_func_idx_rt;
  3822. return true;
  3823. }
  3824. static bool
  3825. aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
  3826. wasm_global_t *import)
  3827. {
  3828. AOTImportGlobal *import_aot_global = NULL;
  3829. const wasm_valtype_t *val_type = NULL;
  3830. bh_assert(module_aot && import);
  3831. import_aot_global = module_aot->import_globals + global_idx_rt;
  3832. bh_assert(import_aot_global);
  3833. /* it is a placeholder and let's skip it*/
  3834. if (!import->type)
  3835. return true;
  3836. val_type = wasm_globaltype_content(import->type);
  3837. bh_assert(val_type);
  3838. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(val_type),
  3839. import_aot_global->type))
  3840. return false;
  3841. bh_assert(import->init);
  3842. switch (wasm_valtype_kind(val_type)) {
  3843. case WASM_I32:
  3844. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  3845. break;
  3846. case WASM_I64:
  3847. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  3848. break;
  3849. case WASM_F32:
  3850. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  3851. break;
  3852. case WASM_F64:
  3853. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  3854. break;
  3855. default:
  3856. goto failed;
  3857. }
  3858. import->global_idx_rt = global_idx_rt;
  3859. import_aot_global->is_linked = true;
  3860. return true;
  3861. failed:
  3862. LOG_DEBUG("%s failed", __FUNCTION__);
  3863. return false;
  3864. }
  3865. static bool
  3866. aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
  3867. wasm_extern_vec_t *externals)
  3868. {
  3869. uint32 i;
  3870. wasm_extern_t *external = NULL;
  3871. AOTModule *module_aot = NULL;
  3872. bh_assert(store && inst_aot && externals);
  3873. module_aot = (AOTModule *)inst_aot->module;
  3874. bh_assert(module_aot);
  3875. for (i = 0; i < module_aot->export_count; ++i) {
  3876. AOTExport *export = module_aot->exports + i;
  3877. switch (export->kind) {
  3878. case EXPORT_KIND_FUNC:
  3879. {
  3880. wasm_func_t *func = NULL;
  3881. if (!(func = wasm_func_new_internal(
  3882. store, export->index,
  3883. (WASMModuleInstanceCommon *)inst_aot))) {
  3884. goto failed;
  3885. }
  3886. external = wasm_func_as_extern(func);
  3887. break;
  3888. }
  3889. case EXPORT_KIND_GLOBAL:
  3890. {
  3891. wasm_global_t *global = NULL;
  3892. if (!(global = wasm_global_new_internal(
  3893. store, export->index,
  3894. (WASMModuleInstanceCommon *)inst_aot))) {
  3895. goto failed;
  3896. }
  3897. external = wasm_global_as_extern(global);
  3898. break;
  3899. }
  3900. case EXPORT_KIND_TABLE:
  3901. {
  3902. wasm_table_t *table;
  3903. if (!(table = wasm_table_new_internal(
  3904. store, export->index,
  3905. (WASMModuleInstanceCommon *)inst_aot))) {
  3906. goto failed;
  3907. }
  3908. external = wasm_table_as_extern(table);
  3909. break;
  3910. }
  3911. case EXPORT_KIND_MEMORY:
  3912. {
  3913. wasm_memory_t *memory;
  3914. if (!(memory = wasm_memory_new_internal(
  3915. store, export->index,
  3916. (WASMModuleInstanceCommon *)inst_aot))) {
  3917. goto failed;
  3918. }
  3919. external = wasm_memory_as_extern(memory);
  3920. break;
  3921. }
  3922. default:
  3923. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3924. export->kind);
  3925. goto failed;
  3926. }
  3927. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3928. goto failed;
  3929. }
  3930. wasm_name_new_from_string_nt(external->name, export->name);
  3931. if (strlen(export->name) && !external->name->data) {
  3932. goto failed;
  3933. }
  3934. if (!bh_vector_append((Vector *)externals, &external)) {
  3935. goto failed;
  3936. }
  3937. }
  3938. return true;
  3939. failed:
  3940. wasm_extern_delete(external);
  3941. return false;
  3942. }
  3943. #endif /* WASM_ENABLE_AOT */
  3944. static bool
  3945. do_link(const wasm_instance_t *inst, const wasm_module_t *module,
  3946. const wasm_extern_vec_t *imports)
  3947. {
  3948. uint32 i, import_func_i, import_global_i;
  3949. bh_assert(inst && module);
  3950. /* we have run a module_type check before. */
  3951. for (i = 0, import_func_i = 0, import_global_i = 0; i < imports->num_elems;
  3952. i++) {
  3953. wasm_extern_t *import = imports->data[i];
  3954. if (!import) {
  3955. LOG_ERROR("imports[%d] is NULL and it is fatal\n", i);
  3956. goto failed;
  3957. }
  3958. switch (wasm_extern_kind(import)) {
  3959. case WASM_EXTERN_FUNC:
  3960. {
  3961. bool ret = false;
  3962. #if WASM_ENABLE_INTERP != 0
  3963. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3964. ret = interp_link_func(inst, MODULE_INTERP(module),
  3965. import_func_i,
  3966. wasm_extern_as_func(import));
  3967. }
  3968. #endif
  3969. #if WASM_ENABLE_AOT != 0
  3970. if ((*module)->module_type == Wasm_Module_AoT) {
  3971. ret = aot_link_func(inst, MODULE_AOT(module), import_func_i,
  3972. wasm_extern_as_func(import));
  3973. }
  3974. #endif
  3975. if (!ret) {
  3976. LOG_WARNING("link function #%d failed", import_func_i);
  3977. goto failed;
  3978. }
  3979. import_func_i++;
  3980. break;
  3981. }
  3982. case WASM_EXTERN_GLOBAL:
  3983. {
  3984. bool ret = false;
  3985. #if WASM_ENABLE_INTERP != 0
  3986. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3987. ret = interp_link_global(MODULE_INTERP(module),
  3988. import_global_i,
  3989. wasm_extern_as_global(import));
  3990. }
  3991. #endif
  3992. #if WASM_ENABLE_AOT != 0
  3993. if ((*module)->module_type == Wasm_Module_AoT) {
  3994. ret = aot_link_global(MODULE_AOT(module), import_global_i,
  3995. wasm_extern_as_global(import));
  3996. }
  3997. #endif
  3998. if (!ret) {
  3999. LOG_WARNING("link global #%d failed", import_global_i);
  4000. goto failed;
  4001. }
  4002. import_global_i++;
  4003. break;
  4004. }
  4005. case WASM_EXTERN_MEMORY:
  4006. case WASM_EXTERN_TABLE:
  4007. {
  4008. LOG_WARNING("doesn't support import memories and tables for "
  4009. "now, ignore them");
  4010. break;
  4011. }
  4012. default:
  4013. {
  4014. UNREACHABLE();
  4015. break;
  4016. }
  4017. }
  4018. }
  4019. return true;
  4020. failed:
  4021. LOG_DEBUG("%s failed", __FUNCTION__);
  4022. return false;
  4023. }
  4024. wasm_instance_t *
  4025. wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
  4026. const wasm_extern_vec_t *imports, own wasm_trap_t **trap)
  4027. {
  4028. return wasm_instance_new_with_args(store, module, imports, trap,
  4029. KILOBYTE(32), KILOBYTE(32));
  4030. }
  4031. wasm_instance_t *
  4032. wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
  4033. const wasm_extern_vec_t *imports,
  4034. own wasm_trap_t **trap, const uint32 stack_size,
  4035. const uint32 heap_size)
  4036. {
  4037. char sub_error_buf[128] = { 0 };
  4038. char error_buf[256] = { 0 };
  4039. wasm_instance_t *instance = NULL;
  4040. CApiFuncImport *func_import = NULL, **p_func_imports = NULL;
  4041. uint32 i = 0, import_func_count = 0;
  4042. uint64 total_size;
  4043. bool build_exported = false;
  4044. bh_assert(singleton_engine);
  4045. if (!module)
  4046. return NULL;
  4047. /*
  4048. * will do the check at the end of wasm_runtime_instantiate
  4049. */
  4050. WASM_C_DUMP_PROC_MEM();
  4051. instance = malloc_internal(sizeof(wasm_instance_t));
  4052. if (!instance) {
  4053. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4054. "Failed to malloc instance");
  4055. goto failed;
  4056. }
  4057. /* executes the instantiate-time linking if provided */
  4058. if (imports) {
  4059. if (!do_link(instance, module, imports)) {
  4060. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4061. "Failed to validate imports");
  4062. goto failed;
  4063. }
  4064. }
  4065. /*
  4066. * will do the linking result check at the end of wasm_runtime_instantiate
  4067. */
  4068. instance->inst_comm_rt = wasm_runtime_instantiate(
  4069. *module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf));
  4070. if (!instance->inst_comm_rt) {
  4071. goto failed;
  4072. }
  4073. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  4074. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4075. "Failed to create exec env singleton");
  4076. goto failed;
  4077. }
  4078. /* create the c-api func import list */
  4079. #if WASM_ENABLE_INTERP != 0
  4080. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4081. WASMModuleInstanceExtra *e =
  4082. ((WASMModuleInstance *)instance->inst_comm_rt)->e;
  4083. p_func_imports = &(e->common.c_api_func_imports);
  4084. import_func_count = MODULE_INTERP(module)->import_function_count;
  4085. }
  4086. #endif
  4087. #if WASM_ENABLE_AOT != 0
  4088. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4089. AOTModuleInstanceExtra *e =
  4090. (AOTModuleInstanceExtra *)((AOTModuleInstance *)
  4091. instance->inst_comm_rt)
  4092. ->e;
  4093. p_func_imports = &(e->common.c_api_func_imports);
  4094. import_func_count = MODULE_AOT(module)->import_func_count;
  4095. }
  4096. #endif
  4097. bh_assert(p_func_imports);
  4098. total_size = (uint64)sizeof(CApiFuncImport) * import_func_count;
  4099. if (total_size > 0
  4100. && !(*p_func_imports = func_import = malloc_internal(total_size))) {
  4101. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4102. "Failed to create wasm-c-api func imports");
  4103. goto failed;
  4104. }
  4105. /* fill in module_inst->e->c_api_func_imports */
  4106. for (i = 0; imports && i < imports->num_elems; i++) {
  4107. wasm_func_t *func_host = NULL;
  4108. wasm_extern_t *in = imports->data[i];
  4109. bh_assert(in);
  4110. if (wasm_extern_kind(in) != WASM_EXTERN_FUNC)
  4111. continue;
  4112. func_host = wasm_extern_as_func(in);
  4113. /* it is a placeholder and let's skip it*/
  4114. if (!func_host->type) {
  4115. func_import++;
  4116. continue;
  4117. }
  4118. func_import->with_env_arg = func_host->with_env;
  4119. if (func_host->with_env) {
  4120. func_import->func_ptr_linked = func_host->u.cb_env.cb;
  4121. func_import->env_arg = func_host->u.cb_env.env;
  4122. }
  4123. else {
  4124. func_import->func_ptr_linked = func_host->u.cb;
  4125. func_import->env_arg = NULL;
  4126. }
  4127. bh_assert(func_import->func_ptr_linked);
  4128. func_import++;
  4129. }
  4130. /* fill with inst */
  4131. for (i = 0; imports && imports->data && i < imports->num_elems; ++i) {
  4132. wasm_extern_t *import = imports->data[i];
  4133. bh_assert(import);
  4134. switch (import->kind) {
  4135. case WASM_EXTERN_FUNC:
  4136. wasm_extern_as_func(import)->inst_comm_rt =
  4137. instance->inst_comm_rt;
  4138. break;
  4139. case WASM_EXTERN_GLOBAL:
  4140. wasm_extern_as_global(import)->inst_comm_rt =
  4141. instance->inst_comm_rt;
  4142. break;
  4143. case WASM_EXTERN_MEMORY:
  4144. wasm_extern_as_memory(import)->inst_comm_rt =
  4145. instance->inst_comm_rt;
  4146. break;
  4147. case WASM_EXTERN_TABLE:
  4148. wasm_extern_as_table(import)->inst_comm_rt =
  4149. instance->inst_comm_rt;
  4150. break;
  4151. default:
  4152. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4153. "Unknown import kind");
  4154. goto failed;
  4155. }
  4156. }
  4157. /* build the exports list */
  4158. #if WASM_ENABLE_INTERP != 0
  4159. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4160. uint32 export_cnt = ((WASMModuleInstance *)instance->inst_comm_rt)
  4161. ->module->export_count;
  4162. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4163. export_cnt);
  4164. if (!interp_process_export(store,
  4165. (WASMModuleInstance *)instance->inst_comm_rt,
  4166. instance->exports)) {
  4167. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4168. "Interpreter failed to process exports");
  4169. goto failed;
  4170. }
  4171. build_exported = true;
  4172. }
  4173. #endif
  4174. #if WASM_ENABLE_AOT != 0
  4175. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4176. uint32 export_cnt =
  4177. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  4178. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  4179. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_table_count
  4180. + ((AOTModuleInstance *)instance->inst_comm_rt)
  4181. ->export_memory_count;
  4182. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4183. export_cnt);
  4184. if (!aot_process_export(store,
  4185. (AOTModuleInstance *)instance->inst_comm_rt,
  4186. instance->exports)) {
  4187. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4188. "AOT failed to process exports");
  4189. goto failed;
  4190. }
  4191. build_exported = true;
  4192. }
  4193. #endif
  4194. /*
  4195. * a wrong combination of module filetype and compilation flags
  4196. * leads to below branch
  4197. */
  4198. if (!build_exported) {
  4199. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4200. "Incorrect filetype and compilation flags");
  4201. goto failed;
  4202. }
  4203. /* add it to a watching list in store */
  4204. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  4205. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4206. "Failed to add to store instances");
  4207. goto failed;
  4208. }
  4209. WASM_C_DUMP_PROC_MEM();
  4210. return instance;
  4211. failed:
  4212. snprintf(error_buf, sizeof(error_buf), "%s failed: %s", __FUNCTION__,
  4213. sub_error_buf);
  4214. if (trap != NULL) {
  4215. wasm_message_t message = { 0 };
  4216. wasm_name_new_from_string_nt(&message, error_buf);
  4217. *trap = wasm_trap_new(store, &message);
  4218. wasm_byte_vec_delete(&message);
  4219. }
  4220. LOG_DEBUG(error_buf);
  4221. wasm_instance_delete_internal(instance);
  4222. return NULL;
  4223. }
  4224. static void
  4225. wasm_instance_delete_internal(wasm_instance_t *instance)
  4226. {
  4227. if (!instance) {
  4228. return;
  4229. }
  4230. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  4231. if (instance->inst_comm_rt) {
  4232. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  4233. instance->inst_comm_rt = NULL;
  4234. }
  4235. wasm_runtime_free(instance);
  4236. }
  4237. void
  4238. wasm_instance_delete(wasm_instance_t *inst)
  4239. {
  4240. DELETE_HOST_INFO(inst)
  4241. /* will release instance when releasing the store */
  4242. }
  4243. void
  4244. wasm_instance_exports(const wasm_instance_t *instance,
  4245. own wasm_extern_vec_t *out)
  4246. {
  4247. if (!instance || !out) {
  4248. return;
  4249. }
  4250. wasm_extern_vec_copy(out, instance->exports);
  4251. }
  4252. wasm_extern_t *
  4253. wasm_extern_copy(const wasm_extern_t *src)
  4254. {
  4255. wasm_extern_t *dst = NULL;
  4256. if (!src) {
  4257. return NULL;
  4258. }
  4259. switch (wasm_extern_kind(src)) {
  4260. case WASM_EXTERN_FUNC:
  4261. dst = wasm_func_as_extern(
  4262. wasm_func_copy(wasm_extern_as_func_const(src)));
  4263. break;
  4264. case WASM_EXTERN_GLOBAL:
  4265. dst = wasm_global_as_extern(
  4266. wasm_global_copy(wasm_extern_as_global_const(src)));
  4267. break;
  4268. case WASM_EXTERN_MEMORY:
  4269. dst = wasm_memory_as_extern(
  4270. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  4271. break;
  4272. case WASM_EXTERN_TABLE:
  4273. dst = wasm_table_as_extern(
  4274. wasm_table_copy(wasm_extern_as_table_const(src)));
  4275. break;
  4276. default:
  4277. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4278. src->kind);
  4279. break;
  4280. }
  4281. if (!dst) {
  4282. goto failed;
  4283. }
  4284. return dst;
  4285. failed:
  4286. LOG_DEBUG("%s failed", __FUNCTION__);
  4287. wasm_extern_delete(dst);
  4288. return NULL;
  4289. }
  4290. void
  4291. wasm_extern_delete(wasm_extern_t *external)
  4292. {
  4293. if (!external) {
  4294. return;
  4295. }
  4296. if (external->name) {
  4297. wasm_byte_vec_delete(external->name);
  4298. wasm_runtime_free(external->name);
  4299. external->name = NULL;
  4300. }
  4301. switch (wasm_extern_kind(external)) {
  4302. case WASM_EXTERN_FUNC:
  4303. wasm_func_delete(wasm_extern_as_func(external));
  4304. break;
  4305. case WASM_EXTERN_GLOBAL:
  4306. wasm_global_delete(wasm_extern_as_global(external));
  4307. break;
  4308. case WASM_EXTERN_MEMORY:
  4309. wasm_memory_delete(wasm_extern_as_memory(external));
  4310. break;
  4311. case WASM_EXTERN_TABLE:
  4312. wasm_table_delete(wasm_extern_as_table(external));
  4313. break;
  4314. default:
  4315. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4316. external->kind);
  4317. break;
  4318. }
  4319. }
  4320. wasm_externkind_t
  4321. wasm_extern_kind(const wasm_extern_t *external)
  4322. {
  4323. if (!external) {
  4324. return WASM_ANYREF;
  4325. }
  4326. return external->kind;
  4327. }
  4328. own wasm_externtype_t *
  4329. wasm_extern_type(const wasm_extern_t *external)
  4330. {
  4331. if (!external) {
  4332. return NULL;
  4333. }
  4334. switch (wasm_extern_kind(external)) {
  4335. case WASM_EXTERN_FUNC:
  4336. return wasm_functype_as_externtype(
  4337. wasm_func_type(wasm_extern_as_func_const(external)));
  4338. case WASM_EXTERN_GLOBAL:
  4339. return wasm_globaltype_as_externtype(
  4340. wasm_global_type(wasm_extern_as_global_const(external)));
  4341. case WASM_EXTERN_MEMORY:
  4342. return wasm_memorytype_as_externtype(
  4343. wasm_memory_type(wasm_extern_as_memory_const(external)));
  4344. case WASM_EXTERN_TABLE:
  4345. return wasm_tabletype_as_externtype(
  4346. wasm_table_type(wasm_extern_as_table_const(external)));
  4347. default:
  4348. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4349. external->kind);
  4350. break;
  4351. }
  4352. return NULL;
  4353. }
  4354. #define BASIC_FOUR_LIST(V) \
  4355. V(func) \
  4356. V(global) \
  4357. V(memory) \
  4358. V(table)
  4359. #define WASM_EXTERN_AS_OTHER(name) \
  4360. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  4361. { \
  4362. return (wasm_##name##_t *)external; \
  4363. }
  4364. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  4365. #undef WASM_EXTERN_AS_OTHER
  4366. #define WASM_OTHER_AS_EXTERN(name) \
  4367. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  4368. { \
  4369. return (wasm_extern_t *)other; \
  4370. }
  4371. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  4372. #undef WASM_OTHER_AS_EXTERN
  4373. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  4374. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  4375. const wasm_extern_t *external) \
  4376. { \
  4377. return (const wasm_##name##_t *)external; \
  4378. }
  4379. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  4380. #undef WASM_EXTERN_AS_OTHER_CONST
  4381. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  4382. const wasm_extern_t *wasm_##name##_as_extern_const( \
  4383. const wasm_##name##_t *other) \
  4384. { \
  4385. return (const wasm_extern_t *)other; \
  4386. }
  4387. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  4388. #undef WASM_OTHER_AS_EXTERN_CONST
  4389. wasm_extern_t *
  4390. wasm_extern_new_empty(wasm_store_t *store, wasm_externkind_t extern_kind)
  4391. {
  4392. if (extern_kind == WASM_EXTERN_FUNC)
  4393. return wasm_func_as_extern(wasm_func_new_empty(store));
  4394. if (extern_kind == WASM_EXTERN_GLOBAL)
  4395. return wasm_global_as_extern(wasm_global_new_empty(store));
  4396. LOG_ERROR("Don't support linking table and memory for now");
  4397. return NULL;
  4398. }