| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "bh_platform.h"
- #include "bh_common.h"
- #include "bh_assert.h"
- #include "bh_log.h"
- #include "wasm_runtime_common.h"
- #include "wasm_memory.h"
- #if WASM_ENABLE_INTERP != 0
- #include "../interpreter/wasm_runtime.h"
- #endif
- #if WASM_ENABLE_AOT != 0
- #include "../aot/aot_runtime.h"
- #endif
- #if WASM_ENABLE_THREAD_MGR != 0
- #include "../libraries/thread-mgr/thread_manager.h"
- #endif
- #if WASM_ENABLE_SHARED_MEMORY != 0
- #include "wasm_shared_memory.h"
- #endif
- #include "../common/wasm_c_api_internal.h"
- #if WASM_ENABLE_MULTI_MODULE != 0
- /*
- * a safety insurance to prevent
- * circular depencies leading a stack overflow
- * try break early
- */
- typedef struct LoadingModule {
- bh_list_link l;
- /* point to a string pool */
- const char *module_name;
- } LoadingModule;
- static bh_list loading_module_list_head;
- static bh_list *const loading_module_list = &loading_module_list_head;
- static korp_mutex loading_module_list_lock;
- /*
- * a list about all exported functions, globals, memories, tables of every
- * fully loaded module
- */
- static bh_list registered_module_list_head;
- static bh_list *const registered_module_list = ®istered_module_list_head;
- static korp_mutex registered_module_list_lock;
- static void
- wasm_runtime_destroy_registered_module_list();
- #endif /* WASM_ENABLE_MULTI_MODULE */
- #if WASM_ENABLE_REF_TYPES != 0
- /* Initialize externref hashmap */
- static bool
- wasm_externref_map_init();
- /* Destroy externref hashmap */
- static void
- wasm_externref_map_destroy();
- #endif /* WASM_ENABLE_REF_TYPES */
- static void
- set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
- {
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size, "%s", string);
- }
- static void *
- runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
- char *error_buf, uint32 error_buf_size)
- {
- void *mem;
- if (size >= UINT32_MAX
- || !(mem = wasm_runtime_malloc((uint32)size))) {
- if (module_inst != NULL) {
- wasm_runtime_set_exception(module_inst,
- "allocate memory failed");
- }
- else if (error_buf != NULL) {
- set_error_buf(error_buf, error_buf_size,
- "allocate memory failed");
- }
- return NULL;
- }
- memset(mem, 0, (uint32)size);
- return mem;
- }
- static bool
- wasm_runtime_env_init()
- {
- if (bh_platform_init() != 0)
- return false;
- if (wasm_native_init() == false) {
- goto fail1;
- }
- #if WASM_ENABLE_MULTI_MODULE
- if (BHT_OK != os_mutex_init(®istered_module_list_lock)) {
- goto fail2;
- }
- if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
- goto fail3;
- }
- #endif
- #if WASM_ENABLE_SHARED_MEMORY
- if (!wasm_shared_memory_init()) {
- goto fail4;
- }
- #endif
- #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
- if (!thread_manager_init()) {
- goto fail5;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- if (!aot_signal_init()) {
- goto fail6;
- }
- #endif
- #endif
- #if WASM_ENABLE_REF_TYPES != 0
- if (!wasm_externref_map_init()) {
- goto fail7;
- }
- #endif
- return true;
- #if WASM_ENABLE_REF_TYPES != 0
- fail7:
- #endif
- #if WASM_ENABLE_AOT != 0
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- aot_signal_destroy();
- fail6:
- #endif
- #endif
- #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
- thread_manager_destroy();
- fail5:
- #endif
- #if WASM_ENABLE_SHARED_MEMORY
- wasm_shared_memory_destroy();
- fail4:
- #endif
- #if WASM_ENABLE_MULTI_MODULE
- os_mutex_destroy(&loading_module_list_lock);
- fail3:
- os_mutex_destroy(®istered_module_list_lock);
- fail2:
- #endif
- wasm_native_destroy();
- fail1:
- bh_platform_destroy();
- return false;
- }
- static bool
- wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
- {
- return exec_env
- && exec_env->module_inst
- && exec_env->wasm_stack_size > 0
- && exec_env->wasm_stack.s.top_boundary ==
- exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size
- && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary;
- }
- bool
- wasm_runtime_init()
- {
- if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
- return false;
- if (!wasm_runtime_env_init()) {
- wasm_runtime_memory_destroy();
- return false;
- }
- return true;
- }
- void
- wasm_runtime_destroy()
- {
- #if WASM_ENABLE_REF_TYPES != 0
- wasm_externref_map_destroy();
- #endif
- #if WASM_ENABLE_AOT != 0
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- aot_signal_destroy();
- #endif
- #endif
- /* runtime env destroy */
- #if WASM_ENABLE_MULTI_MODULE
- wasm_runtime_destroy_loading_module_list();
- os_mutex_destroy(&loading_module_list_lock);
- wasm_runtime_destroy_registered_module_list();
- os_mutex_destroy(®istered_module_list_lock);
- #endif
- #if WASM_ENABLE_SHARED_MEMORY
- wasm_shared_memory_destroy();
- #endif
- #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
- thread_manager_destroy();
- #endif
- wasm_native_destroy();
- bh_platform_destroy();
- wasm_runtime_memory_destroy();
- }
- bool
- wasm_runtime_full_init(RuntimeInitArgs *init_args)
- {
- if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
- &init_args->mem_alloc_option))
- return false;
- if (!wasm_runtime_env_init()) {
- wasm_runtime_memory_destroy();
- return false;
- }
- if (init_args->n_native_symbols > 0
- && !wasm_runtime_register_natives(init_args->native_module_name,
- init_args->native_symbols,
- init_args->n_native_symbols)) {
- wasm_runtime_destroy();
- return false;
- }
- #if WASM_ENABLE_THREAD_MGR != 0
- wasm_cluster_set_max_thread_num(init_args->max_thread_num);
- #endif
- return true;
- }
- PackageType
- get_package_type(const uint8 *buf, uint32 size)
- {
- if (buf && size >= 4) {
- if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
- return Wasm_Module_Bytecode;
- if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
- return Wasm_Module_AoT;
- }
- return Package_Type_Unknown;
- }
- #if WASM_ENABLE_MULTI_MODULE != 0
- static module_reader reader;
- static module_destroyer destroyer;
- void
- wasm_runtime_set_module_reader(const module_reader reader_cb,
- const module_destroyer destroyer_cb)
- {
- reader = reader_cb;
- destroyer = destroyer_cb;
- }
- module_reader
- wasm_runtime_get_module_reader()
- {
- return reader;
- }
- module_destroyer
- wasm_runtime_get_module_destroyer()
- {
- return destroyer;
- }
- static WASMRegisteredModule *
- wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
- {
- WASMRegisteredModule *reg_module = NULL;
- os_mutex_lock(®istered_module_list_lock);
- reg_module = bh_list_first_elem(registered_module_list);
- while (reg_module && module != reg_module->module) {
- reg_module = bh_list_elem_next(reg_module);
- }
- os_mutex_unlock(®istered_module_list_lock);
- return reg_module;
- }
- bool
- wasm_runtime_register_module_internal(const char *module_name,
- WASMModuleCommon *module,
- uint8 *orig_file_buf,
- uint32 orig_file_buf_size,
- char *error_buf,
- uint32_t error_buf_size)
- {
- WASMRegisteredModule *node = NULL;
- node = wasm_runtime_find_module_registered_by_reference(module);
- if (node) { /* module has been registered */
- if (node->module_name) { /* module has name */
- if (!module_name || strcmp(node->module_name, module_name)) {
- /* module has different name */
- LOG_DEBUG("module(%p) has been registered with name %s",
- module, node->module_name);
- set_error_buf(error_buf, error_buf_size,
- "Register module failed: "
- "failed to rename the module");
- return false;
- }
- else {
- /* module has the same name */
- LOG_DEBUG("module(%p) has been registered with the same name %s",
- module, node->module_name);
- return true;
- }
- }
- else {
- /* module has empyt name, reset it */
- node->module_name = module_name;
- return true;
- }
- }
- /* module hasn't been registered */
- node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
- if (!node) {
- LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
- sizeof(WASMRegisteredModule));
- return false;
- }
- /* share the string and the module */
- node->module_name = module_name;
- node->module = module;
- node->orig_file_buf = orig_file_buf;
- node->orig_file_buf_size = orig_file_buf_size;
- os_mutex_lock(®istered_module_list_lock);
- bh_list_status ret = bh_list_insert(registered_module_list, node);
- bh_assert(BH_LIST_SUCCESS == ret);
- (void)ret;
- os_mutex_unlock(®istered_module_list_lock);
- return true;
- }
- bool
- wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
- char *error_buf, uint32_t error_buf_size)
- {
- if (!error_buf || !error_buf_size) {
- LOG_ERROR("error buffer is required");
- return false;
- }
- if (!module_name || !module) {
- LOG_DEBUG("module_name and module are required");
- set_error_buf(error_buf, error_buf_size,
- "Register module failed: "
- "module_name and module are required");
- return false;
- }
- if (wasm_runtime_is_built_in_module(module_name)) {
- LOG_DEBUG("%s is a built-in module name", module_name);
- set_error_buf(error_buf, error_buf_size,
- "Register module failed: "
- "can not register as a built-in module");
- return false;
- }
- return wasm_runtime_register_module_internal(
- module_name, module, NULL, 0,
- error_buf, error_buf_size);
- }
- void
- wasm_runtime_unregister_module(const WASMModuleCommon *module)
- {
- WASMRegisteredModule *registered_module = NULL;
- os_mutex_lock(®istered_module_list_lock);
- registered_module = bh_list_first_elem(registered_module_list);
- while (registered_module && module != registered_module->module) {
- registered_module = bh_list_elem_next(registered_module);
- }
- /* it does not matter if it is not exist. after all, it is gone */
- if (registered_module) {
- bh_list_remove(registered_module_list, registered_module);
- wasm_runtime_free(registered_module);
- }
- os_mutex_unlock(®istered_module_list_lock);
- }
- WASMModuleCommon *
- wasm_runtime_find_module_registered(const char *module_name)
- {
- WASMRegisteredModule *module = NULL, *module_next;
- os_mutex_lock(®istered_module_list_lock);
- module = bh_list_first_elem(registered_module_list);
- while (module) {
- module_next = bh_list_elem_next(module);
- if (module->module_name
- && !strcmp(module_name, module->module_name)) {
- break;
- }
- module = module_next;
- }
- os_mutex_unlock(®istered_module_list_lock);
- return module ? module->module : NULL;
- }
- bool
- wasm_runtime_is_module_registered(const char *module_name)
- {
- return NULL != wasm_runtime_find_module_registered(module_name);
- }
- /*
- * simply destroy all
- */
- static void
- wasm_runtime_destroy_registered_module_list()
- {
- WASMRegisteredModule *reg_module = NULL;
- os_mutex_lock(®istered_module_list_lock);
- reg_module = bh_list_first_elem(registered_module_list);
- while (reg_module) {
- WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
- bh_list_remove(registered_module_list, reg_module);
- /* now, it is time to release every module in the runtime */
- if (reg_module->module->module_type == Wasm_Module_Bytecode) {
- #if WASM_ENABLE_INTERP != 0
- wasm_unload((WASMModule *)reg_module->module);
- #endif
- }
- else {
- #if WASM_ENABLE_AOT != 0
- aot_unload((AOTModule *)reg_module->module);
- #endif
- }
- /* destroy the file buffer */
- if (destroyer && reg_module->orig_file_buf) {
- destroyer(reg_module->orig_file_buf,
- reg_module->orig_file_buf_size);
- reg_module->orig_file_buf = NULL;
- reg_module->orig_file_buf_size = 0;
- }
- wasm_runtime_free(reg_module);
- reg_module = next_reg_module;
- }
- os_mutex_unlock(®istered_module_list_lock);
- }
- bool
- wasm_runtime_add_loading_module(const char *module_name,
- char *error_buf, uint32 error_buf_size)
- {
- LOG_DEBUG("add %s into a loading list", module_name);
- LoadingModule *loadingModule =
- runtime_malloc(sizeof(LoadingModule), NULL,
- error_buf, error_buf_size);
- if (!loadingModule) {
- return false;
- }
- /* share the incoming string */
- loadingModule->module_name = module_name;
- os_mutex_lock(&loading_module_list_lock);
- bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
- bh_assert(BH_LIST_SUCCESS == ret);
- (void)ret;
- os_mutex_unlock(&loading_module_list_lock);
- return true;
- }
- void
- wasm_runtime_delete_loading_module(const char *module_name)
- {
- LOG_DEBUG("delete %s from a loading list", module_name);
- LoadingModule *module = NULL;
- os_mutex_lock(&loading_module_list_lock);
- module = bh_list_first_elem(loading_module_list);
- while (module && strcmp(module->module_name, module_name)) {
- module = bh_list_elem_next(module);
- }
- /* it does not matter if it is not exist. after all, it is gone */
- if (module) {
- bh_list_remove(loading_module_list, module);
- wasm_runtime_free(module);
- }
- os_mutex_unlock(&loading_module_list_lock);
- }
- bool
- wasm_runtime_is_loading_module(const char *module_name)
- {
- LOG_DEBUG("find %s in a loading list", module_name);
- LoadingModule *module = NULL;
- os_mutex_lock(&loading_module_list_lock);
- module = bh_list_first_elem(loading_module_list);
- while (module && strcmp(module_name, module->module_name)) {
- module = bh_list_elem_next(module);
- }
- os_mutex_unlock(&loading_module_list_lock);
- return module != NULL;
- }
- void
- wasm_runtime_destroy_loading_module_list()
- {
- LoadingModule *module = NULL;
- os_mutex_lock(&loading_module_list_lock);
- module = bh_list_first_elem(loading_module_list);
- while (module) {
- LoadingModule *next_module = bh_list_elem_next(module);
- bh_list_remove(loading_module_list, module);
- /*
- * will not free the module_name since it is
- * shared one of the const string pool
- */
- wasm_runtime_free(module);
- module = next_module;
- }
- os_mutex_unlock(&loading_module_list_lock);
- }
- #endif /* WASM_ENABLE_MULTI_MODULE */
- bool
- wasm_runtime_is_built_in_module(const char *module_name)
- {
- return (!strcmp("env", module_name)
- || !strcmp("wasi_unstable", module_name)
- || !strcmp("wasi_snapshot_preview1", module_name)
- #if WASM_ENABLE_SPEC_TEST != 0
- || !strcmp("spectest", module_name)
- #endif
- || !strcmp("", module_name));
- }
- #if WASM_ENABLE_THREAD_MGR != 0
- bool
- wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
- uint32 start_offset, uint32 size)
- {
- WASMModuleInstanceCommon *module_inst
- = wasm_exec_env_get_module_inst(exec_env);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- return wasm_set_aux_stack(exec_env, start_offset, size);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- return aot_set_aux_stack(exec_env, start_offset, size);
- }
- #endif
- return false;
- }
- bool
- wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
- uint32 *start_offset, uint32 *size)
- {
- WASMModuleInstanceCommon *module_inst
- = wasm_exec_env_get_module_inst(exec_env);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- return wasm_get_aux_stack(exec_env, start_offset, size);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- return aot_get_aux_stack(exec_env, start_offset, size);
- }
- #endif
- return false;
- }
- void
- wasm_runtime_set_max_thread_num(uint32 num)
- {
- wasm_cluster_set_max_thread_num(num);
- }
- #endif /* end of WASM_ENABLE_THREAD_MGR */
- static WASMModuleCommon *
- register_module_with_null_name(WASMModuleCommon *module_common,
- char *error_buf, uint32 error_buf_size)
- {
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (module_common) {
- if (!wasm_runtime_register_module_internal(NULL, module_common,
- NULL, 0,
- error_buf,
- error_buf_size)) {
- wasm_runtime_unload(module_common);
- return NULL;
- }
- return module_common;
- }
- else
- return NULL;
- #else
- return module_common;
- #endif
- }
- WASMModuleCommon *
- wasm_runtime_load(const uint8 *buf, uint32 size,
- char *error_buf, uint32 error_buf_size)
- {
- WASMModuleCommon *module_common = NULL;
- if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
- #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
- AOTModule *aot_module;
- WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
- if (!module)
- return NULL;
- if (!(aot_module = aot_convert_wasm_module(module,
- error_buf, error_buf_size))) {
- wasm_unload(module);
- return NULL;
- }
- module_common = (WASMModuleCommon*)aot_module;
- return register_module_with_null_name(module_common,
- error_buf, error_buf_size);
- #elif WASM_ENABLE_INTERP != 0
- module_common = (WASMModuleCommon*)
- wasm_load(buf, size, error_buf, error_buf_size);
- return register_module_with_null_name(module_common,
- error_buf, error_buf_size);
- #endif
- }
- else if (get_package_type(buf, size) == Wasm_Module_AoT) {
- #if WASM_ENABLE_AOT != 0
- module_common = (WASMModuleCommon*)
- aot_load_from_aot_file(buf, size, error_buf, error_buf_size);
- return register_module_with_null_name(module_common,
- error_buf, error_buf_size);
- #endif
- }
- if (size < 4)
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: unexpected end");
- else
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: magic header not detected");
- return NULL;
- }
- WASMModuleCommon *
- wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
- char *error_buf, uint32_t error_buf_size)
- {
- WASMModuleCommon *module_common;
- #if WASM_ENABLE_INTERP != 0
- if (!is_aot) {
- module_common = (WASMModuleCommon*)
- wasm_load_from_sections(section_list,
- error_buf, error_buf_size);
- return register_module_with_null_name(module_common,
- error_buf, error_buf_size);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (is_aot) {
- module_common = (WASMModuleCommon*)
- aot_load_from_sections(section_list,
- error_buf, error_buf_size);
- return register_module_with_null_name(module_common,
- error_buf, error_buf_size);
- }
- #endif
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: invalid section list type");
- return NULL;
- }
- void
- wasm_runtime_unload(WASMModuleCommon *module)
- {
- #if WASM_ENABLE_MULTI_MODULE != 0
- /**
- * since we will unload and free all module when runtime_destroy()
- * we don't want users to unwillingly disrupt it
- */
- return;
- #endif
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode) {
- wasm_unload((WASMModule*)module);
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT) {
- aot_unload((AOTModule*)module);
- return;
- }
- #endif
- }
- WASMModuleInstanceCommon *
- wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
- uint32 stack_size, uint32 heap_size,
- char *error_buf, uint32 error_buf_size)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode)
- return (WASMModuleInstanceCommon*)
- wasm_instantiate((WASMModule*)module, is_sub_inst,
- stack_size, heap_size,
- error_buf, error_buf_size);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT)
- return (WASMModuleInstanceCommon*)
- aot_instantiate((AOTModule*)module, is_sub_inst,
- stack_size, heap_size,
- error_buf, error_buf_size);
- #endif
- set_error_buf(error_buf, error_buf_size,
- "Instantiate module failed, invalid module type");
- return NULL;
- }
- WASMModuleInstanceCommon *
- wasm_runtime_instantiate(WASMModuleCommon *module,
- uint32 stack_size, uint32 heap_size,
- char *error_buf, uint32 error_buf_size)
- {
- return wasm_runtime_instantiate_internal(module, false,
- stack_size, heap_size,
- error_buf, error_buf_size);
- }
- void
- wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
- bool is_sub_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_deinstantiate((WASMModuleInstance*)module_inst, is_sub_inst);
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_deinstantiate((AOTModuleInstance*)module_inst, is_sub_inst);
- return;
- }
- #endif
- }
- void
- wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
- {
- wasm_runtime_deinstantiate_internal(module_inst, false);
- }
- WASMExecEnv *
- wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
- uint32 stack_size)
- {
- return wasm_exec_env_create(module_inst, stack_size);
- }
- void
- wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
- {
- wasm_exec_env_destroy(exec_env);
- }
- bool
- wasm_runtime_init_thread_env()
- {
- #if WASM_ENABLE_AOT != 0
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- return aot_signal_init();
- #endif
- #endif
- return true;
- }
- void
- wasm_runtime_destroy_thread_env()
- {
- #if WASM_ENABLE_AOT != 0
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- aot_signal_destroy();
- #endif
- #endif
- }
- #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
- void
- wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
- {
- WASMModuleMemConsumption mem_conspn = { 0 };
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode) {
- wasm_get_module_mem_consumption((WASMModule*)module, &mem_conspn);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT) {
- aot_get_module_mem_consumption((AOTModule*)module, &mem_conspn);
- }
- #endif
- os_printf("WASM module memory consumption, total size: %u\n",
- mem_conspn.total_size);
- os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
- os_printf(" types size: %u\n", mem_conspn.types_size);
- os_printf(" imports size: %u\n", mem_conspn.imports_size);
- os_printf(" funcs size: %u\n", mem_conspn.functions_size);
- os_printf(" tables size: %u\n", mem_conspn.tables_size);
- os_printf(" memories size: %u\n", mem_conspn.memories_size);
- os_printf(" globals size: %u\n", mem_conspn.globals_size);
- os_printf(" exports size: %u\n", mem_conspn.exports_size);
- os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
- os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
- os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
- #if WASM_ENABLE_AOT != 0
- os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
- #endif
- }
- void
- wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
- *module_inst)
- {
- WASMModuleInstMemConsumption mem_conspn = { 0 };
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_get_module_inst_mem_consumption((WASMModuleInstance*)module_inst,
- &mem_conspn);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_get_module_inst_mem_consumption((AOTModuleInstance*)module_inst,
- &mem_conspn);
- }
- #endif
- os_printf("WASM module inst memory consumption, total size: %u\n",
- mem_conspn.total_size);
- os_printf(" module inst struct size: %u\n",
- mem_conspn.module_inst_struct_size);
- os_printf(" memories size: %u\n", mem_conspn.memories_size);
- os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
- os_printf(" tables size: %u\n", mem_conspn.tables_size);
- os_printf(" functions size: %u\n", mem_conspn.functions_size);
- os_printf(" globals size: %u\n", mem_conspn.globals_size);
- os_printf(" exports size: %u\n", mem_conspn.exports_size);
- }
- void
- wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
- {
- uint32 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
- + exec_env->wasm_stack_size;
- os_printf("Exec env memory consumption, total size: %u\n", total_size);
- os_printf(" exec env struct size: %u\n",
- offsetof(WASMExecEnv, wasm_stack.s.bottom));
- #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
- os_printf(" block addr cache size: %u\n",
- sizeof(exec_env->block_addr_cache));
- #endif
- os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
- }
- uint32
- gc_get_heap_highmark_size(void *heap);
- void
- wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
- {
- WASMModuleInstMemConsumption module_inst_mem_consps;
- WASMModuleMemConsumption module_mem_consps;
- WASMModuleInstanceCommon *module_inst_common;
- WASMModuleCommon *module_common = NULL;
- void *heap_handle = NULL;
- uint32 total_size = 0, app_heap_peak_size = 0;
- uint32 max_aux_stack_used = -1;
- module_inst_common = exec_env->module_inst;
- #if WASM_ENABLE_INTERP != 0
- if (module_inst_common->module_type == Wasm_Module_Bytecode) {
- WASMModuleInstance *wasm_module_inst =
- (WASMModuleInstance*)module_inst_common;
- WASMModule *wasm_module = wasm_module_inst->module;
- module_common = (WASMModuleCommon*)wasm_module;
- if (wasm_module_inst->memories) {
- heap_handle = wasm_module_inst->memories[0]->heap_handle;
- }
- wasm_get_module_inst_mem_consumption
- (wasm_module_inst, &module_inst_mem_consps);
- wasm_get_module_mem_consumption
- (wasm_module, &module_mem_consps);
- if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
- max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst_common->module_type == Wasm_Module_AoT) {
- AOTModuleInstance *aot_module_inst =
- (AOTModuleInstance*)module_inst_common;
- AOTModule *aot_module =
- (AOTModule*)aot_module_inst->aot_module.ptr;
- module_common = (WASMModuleCommon*)aot_module;
- if (aot_module_inst->memories.ptr) {
- AOTMemoryInstance **memories =
- (AOTMemoryInstance **)aot_module_inst->memories.ptr;
- heap_handle = memories[0]->heap_handle.ptr;
- }
- aot_get_module_inst_mem_consumption
- (aot_module_inst, &module_inst_mem_consps);
- aot_get_module_mem_consumption
- (aot_module, &module_mem_consps);
- }
- #endif
- bh_assert(module_common != NULL);
- if (heap_handle) {
- app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
- }
- total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
- + exec_env->wasm_stack_size
- + module_mem_consps.total_size
- + module_inst_mem_consps.total_size;
- os_printf("\nMemory consumption summary (bytes):\n");
- wasm_runtime_dump_module_mem_consumption(module_common);
- wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
- wasm_runtime_dump_exec_env_mem_consumption(exec_env);
- os_printf("\nTotal memory consumption of module, module inst and "
- "exec env: %u\n", total_size);
- os_printf("Total interpreter stack used: %u\n",
- exec_env->max_wasm_stack_used);
- if (max_aux_stack_used != (uint32)-1)
- os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
- else
- os_printf("Total aux stack used: no enough info to profile\n");
- os_printf("Total app heap used: %u\n", app_heap_peak_size);
- }
- #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
- || (WASM_ENABLE_MEMORY_TRACING != 0) */
- #if WASM_ENABLE_PERF_PROFILING != 0
- void
- wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_dump_perf_profiling((WASMModuleInstance*)module_inst);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_dump_perf_profiling((AOTModuleInstance*)module_inst);
- }
- #endif
- }
- #endif
- WASMModuleInstanceCommon *
- wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
- {
- return wasm_exec_env_get_module_inst(exec_env);
- }
- void *
- wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
- {
- return exec_env->attachment;
- }
- void
- wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
- {
- exec_env->user_data = user_data;
- }
- void *
- wasm_runtime_get_user_data(WASMExecEnv *exec_env)
- {
- return exec_env->user_data;
- }
- WASMType *
- wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
- uint32 module_type)
- {
- WASMType *type = NULL;
- #if WASM_ENABLE_INTERP != 0
- if (module_type == Wasm_Module_Bytecode) {
- WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
- type = wasm_func->is_import_func
- ? wasm_func->u.func_import->func_type
- : wasm_func->u.func->func_type;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_type == Wasm_Module_AoT) {
- AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
- type = aot_func->is_import_func
- ? aot_func->u.func_import->func_type
- : aot_func->u.func.func_type;
- }
- #endif
- return type;
- }
- WASMFunctionInstanceCommon *
- wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
- const char *name, const char *signature)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return (WASMFunctionInstanceCommon*)
- wasm_lookup_function((const WASMModuleInstance*)module_inst,
- name, signature);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return (WASMFunctionInstanceCommon*)
- aot_lookup_function((const AOTModuleInstance*)module_inst,
- name, signature);
- #endif
- return NULL;
- }
- #if WASM_ENABLE_REF_TYPES != 0
- static void
- wasm_runtime_reclaim_externref(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function,
- uint32 *argv)
- {
- uint32 i = 0, cell_num = 0;
- WASMType *func_type = wasm_runtime_get_function_type(
- function, exec_env->module_inst->module_type);
- bh_assert(func_type);
- while (i < func_type->result_count) {
- uint8 result_type = func_type->types[func_type->param_count + i];
- if (result_type == VALUE_TYPE_EXTERNREF && argv[i] != NULL_REF) {
- /* Retain the externref returned to runtime embedder */
- (void)wasm_externref_retain(argv[i]);
- }
- cell_num += wasm_value_type_cell_num(result_type);
- i++;
- }
- wasm_externref_reclaim(exec_env->module_inst);
- }
- void
- wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function)
- {
- exec_env->nested_calling_depth++;
- }
- void
- wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function,
- bool ret, uint32 *argv)
- {
- exec_env->nested_calling_depth--;
- if (!exec_env->nested_calling_depth && ret) {
- wasm_runtime_reclaim_externref(exec_env, function, argv);
- }
- }
- #endif
- bool
- wasm_runtime_call_wasm(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function,
- uint32 argc, uint32 argv[])
- {
- bool ret = false;
- if (!wasm_runtime_exec_env_check(exec_env)) {
- LOG_ERROR("Invalid exec env stack info.");
- return false;
- }
- #if WASM_ENABLE_REF_TYPES != 0
- wasm_runtime_prepare_call_function(exec_env, function);
- #endif
- #if WASM_ENABLE_INTERP != 0
- if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
- ret = wasm_call_function(exec_env,
- (WASMFunctionInstance*)function,
- argc, argv);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (exec_env->module_inst->module_type == Wasm_Module_AoT)
- ret = aot_call_function(exec_env,
- (AOTFunctionInstance*)function,
- argc, argv);
- #endif
- #if WASM_ENABLE_REF_TYPES != 0
- wasm_runtime_finalize_call_function(exec_env, function, ret, argv);
- #endif
- return ret;
- }
- static uint32
- parse_args_to_uint32_array(WASMType *type,
- uint32 num_args, wasm_val_t *args,
- uint32 *out_argv)
- {
- uint32 i, p;
- for (i = 0, p = 0; i < num_args; i++) {
- switch (args[i].kind) {
- case WASM_I32:
- out_argv[p++] = args[i].of.i32;
- break;
- case WASM_I64:
- {
- union { uint64 val; uint32 parts[2]; } u;
- u.val = args[i].of.i64;
- out_argv[p++] = u.parts[0];
- out_argv[p++] = u.parts[1];
- break;
- }
- case WASM_F32:
- {
- union { float32 val; uint32 part; } u;
- u.val = args[i].of.f32;
- out_argv[p++] = u.part;
- break;
- }
- case WASM_F64:
- {
- union { float64 val; uint32 parts[2]; } u;
- u.val = args[i].of.f64;
- out_argv[p++] = u.parts[0];
- out_argv[p++] = u.parts[1];
- break;
- }
- default:
- bh_assert(0);
- break;
- }
- }
- return p;
- }
- static uint32
- parse_uint32_array_to_results(WASMType *type,
- uint32 argc, uint32 *argv,
- wasm_val_t *out_results)
- {
- uint32 i, p;
- for (i = 0, p = 0; i < type->result_count; i++) {
- switch (type->types[type->param_count + i]) {
- case VALUE_TYPE_I32:
- out_results[i].kind = WASM_I32;
- out_results[i].of.i32 = (int32)argv[p++];
- break;
- case VALUE_TYPE_I64:
- {
- union { uint64 val; uint32 parts[2]; } u;
- u.parts[0] = argv[p++];
- u.parts[1] = argv[p++];
- out_results[i].kind = WASM_I64;
- out_results[i].of.i64 = u.val;
- break;
- }
- case VALUE_TYPE_F32:
- {
- union { float32 val; uint32 part; } u;
- u.part = argv[p++];
- out_results[i].kind = WASM_F32;
- out_results[i].of.f32 = u.val;
- break;
- }
- case VALUE_TYPE_F64:
- {
- union { float64 val; uint32 parts[2]; } u;
- u.parts[0] = argv[p++];
- u.parts[1] = argv[p++];
- out_results[i].kind = WASM_F64;
- out_results[i].of.f64 = u.val;
- break;
- }
- default:
- bh_assert(0);
- break;
- }
- }
- bh_assert(argc == p);
- return type->result_count;
- }
- bool
- wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function,
- uint32 num_results, wasm_val_t results[],
- uint32 num_args, wasm_val_t args[])
- {
- uint32 argc, *argv, ret_num, cell_num, total_size, module_type;
- WASMType *type;
- bool ret = false;
- module_type = exec_env->module_inst->module_type;
- type = wasm_runtime_get_function_type(function, module_type);
- if (!type) {
- LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be enabled at least one.");
- goto fail1;
- }
- argc = type->param_cell_num;
- cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
- if (num_results != type->result_count) {
- LOG_ERROR("The result value number does not match the function declaration.");
- goto fail1;
- }
- if (num_args != type->param_count) {
- LOG_ERROR("The argument value number does not match the function declaration.");
- goto fail1;
- }
- total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
- if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL, 0))) {
- wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
- goto fail1;
- }
- argc = parse_args_to_uint32_array(type, num_args, args, argv);
- if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
- goto fail2;
- ret_num = parse_uint32_array_to_results(type, type->ret_cell_num, argv, results);
- bh_assert(ret_num == num_results);
- (void)ret_num;
- fail2:
- wasm_runtime_free(argv);
- fail1:
- return ret;
- }
- bool
- wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
- WASMFunctionInstanceCommon *function,
- uint32 num_results, wasm_val_t results[],
- uint32 num_args, ...)
- {
- wasm_val_t *args = NULL;
- WASMType *type = NULL;
- bool ret = false;
- uint32 i = 0, module_type;
- va_list vargs;
- module_type = exec_env->module_inst->module_type;
- type = wasm_runtime_get_function_type(function, module_type);
- if (!type) {
- LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
- "must be enabled at least one.");
- goto fail1;
- }
- if (num_args != type->param_count) {
- LOG_ERROR("The argument value number does not match the "
- "function declaration.");
- goto fail1;
- }
- if (!(args = runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
- wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
- goto fail1;
- }
- va_start(vargs, num_args);
- for (i = 0; i < num_args; i++) {
- switch (type->types[i]) {
- case VALUE_TYPE_I32:
- args[i].kind = WASM_I32;
- args[i].of.i32 = va_arg(vargs, uint32);
- break;
- case VALUE_TYPE_I64:
- args[i].kind = WASM_I64;
- args[i].of.i64 = va_arg(vargs, uint64);
- break;
- case VALUE_TYPE_F32:
- args[i].kind = WASM_F32;
- args[i].of.f32 = (float32)va_arg(vargs, float64);
- break;
- case VALUE_TYPE_F64:
- args[i].kind = WASM_F64;
- args[i].of.f64 = va_arg(vargs, float64);;
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- va_end(vargs);
- ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
- num_args, args);
- wasm_runtime_free(args);
- fail1:
- return ret;
- }
- bool
- wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
- WASMFunctionInstanceCommon *function,
- uint32 argc, uint32 argv[])
- {
- bool ret = false;
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- ret = wasm_create_exec_env_and_call_function(
- (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function,
- argc, argv);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- ret = aot_create_exec_env_and_call_function(
- (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function,
- argc, argv);
- #endif
- return ret;
- }
- bool
- wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
- #endif
- return false;
- }
- WASMExecEnv *
- wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return ((WASMModuleInstance *)module_inst)->exec_env_singleton;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return (WASMExecEnv *)
- ((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr;
- #endif
- return NULL;
- }
- void
- wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
- const char *exception)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_set_exception((WASMModuleInstance*)module_inst, exception);
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_set_exception((AOTModuleInstance*)module_inst, exception);
- return;
- }
- #endif
- }
- const char*
- wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- return wasm_get_exception((WASMModuleInstance*)module_inst);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- return aot_get_exception((AOTModuleInstance*)module_inst);
- }
- #endif
- return NULL;
- }
- void
- wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
- {
- wasm_runtime_set_exception(module_inst, NULL);
- }
- void
- wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
- void *custom_data)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- ((WASMModuleInstance*)module_inst)->custom_data = custom_data;
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data;
- return;
- }
- #endif
- }
- void
- wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
- void *custom_data)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- wasm_cluster_spread_custom_data(module_inst, custom_data);
- #else
- wasm_runtime_set_custom_data_internal(module_inst, custom_data);
- #endif
- }
- void*
- wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return ((WASMModuleInstance*)module_inst)->custom_data;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
- #endif
- return NULL;
- }
- uint32
- wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
- void **p_native_addr)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
- p_native_addr);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_module_malloc((AOTModuleInstance*)module_inst, size,
- p_native_addr);
- #endif
- return 0;
- }
- uint32
- wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
- uint32 size, void **p_native_addr)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_module_realloc((WASMModuleInstance*)module_inst, ptr,
- size, p_native_addr);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_module_realloc((AOTModuleInstance*)module_inst, ptr,
- size, p_native_addr);
- #endif
- return 0;
- }
- void
- wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_module_free((WASMModuleInstance*)module_inst, ptr);
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_module_free((AOTModuleInstance*)module_inst, ptr);
- return;
- }
- #endif
- }
- uint32
- wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
- const char *src, uint32 size)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size);
- }
- #endif
- return 0;
- }
- bool
- wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
- uint32 app_offset, uint32 size)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_validate_app_addr((WASMModuleInstance*)module_inst,
- app_offset, size);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_validate_app_addr((AOTModuleInstance*)module_inst,
- app_offset, size);
- #endif
- return false;
- }
- bool
- wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
- uint32 app_str_offset)
- {
- uint32 app_end_offset;
- char *str, *str_end;
- if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
- NULL, &app_end_offset))
- goto fail;
- str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
- str_end = str + (app_end_offset - app_str_offset);
- while (str < str_end && *str != '\0')
- str++;
- if (str == str_end)
- goto fail;
- return true;
- fail:
- wasm_runtime_set_exception(module_inst, "out of bounds memory access");
- return false;
- }
- bool
- wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
- void *native_ptr, uint32 size)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_validate_native_addr((WASMModuleInstance*)module_inst,
- native_ptr, size);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_validate_native_addr((AOTModuleInstance*)module_inst,
- native_ptr, size);
- #endif
- return false;
- }
- void *
- wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
- uint32 app_offset)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_addr_app_to_native((WASMModuleInstance*)module_inst,
- app_offset);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_addr_app_to_native((AOTModuleInstance*)module_inst,
- app_offset);
- #endif
- return NULL;
- }
- uint32
- wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
- void *native_ptr)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_addr_native_to_app((WASMModuleInstance*)module_inst,
- native_ptr);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_addr_native_to_app((AOTModuleInstance*)module_inst,
- native_ptr);
- #endif
- return 0;
- }
- bool
- wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
- uint32 app_offset,
- uint32 *p_app_start_offset,
- uint32 *p_app_end_offset)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_get_app_addr_range((WASMModuleInstance*)module_inst,
- app_offset, p_app_start_offset,
- p_app_end_offset);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_get_app_addr_range((AOTModuleInstance*)module_inst,
- app_offset, p_app_start_offset,
- p_app_end_offset);
- #endif
- return false;
- }
- bool
- wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
- uint8 *native_ptr,
- uint8 **p_native_start_addr,
- uint8 **p_native_end_addr)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_get_native_addr_range((WASMModuleInstance*)module_inst,
- native_ptr, p_native_start_addr,
- p_native_end_addr);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return aot_get_native_addr_range((AOTModuleInstance*)module_inst,
- native_ptr, p_native_start_addr,
- p_native_end_addr);
- #endif
- return false;
- }
- uint32
- wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return ((WASMModuleInstance*)module_inst)->temp_ret;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return ((AOTModuleInstance*)module_inst)->temp_ret;
- #endif
- return 0;
- }
- void
- wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
- uint32 temp_ret)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret;
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret;
- return;
- }
- #endif
- }
- uint32
- wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return ((WASMModuleInstance*)module_inst)->llvm_stack;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return ((AOTModuleInstance*)module_inst)->llvm_stack;
- #endif
- return 0;
- }
- void
- wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
- uint32 llvm_stack)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack;
- return;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack;
- return;
- }
- #endif
- }
- bool
- wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
- uint32 inc_page_count)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module->module_type == Wasm_Module_Bytecode)
- return wasm_enlarge_memory((WASMModuleInstance*)module,
- inc_page_count);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT)
- return aot_enlarge_memory((AOTModuleInstance*)module,
- inc_page_count);
- #endif
- return false;
- }
- #if WASM_ENABLE_LIBC_WASI != 0
- void
- wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module,
- const char *dir_list[], uint32 dir_count,
- const char *map_dir_list[], uint32 map_dir_count,
- const char *env_list[], uint32 env_count,
- char *argv[], int argc,
- int stdinfd, int stdoutfd, int stderrfd)
- {
- WASIArguments *wasi_args = NULL;
- #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
- if (module->module_type == Wasm_Module_Bytecode)
- wasi_args = &((WASMModule*)module)->wasi_args;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module->module_type == Wasm_Module_AoT)
- wasi_args = &((AOTModule*)module)->wasi_args;
- #endif
- if (wasi_args) {
- wasi_args->dir_list = dir_list;
- wasi_args->dir_count = dir_count;
- wasi_args->map_dir_list = map_dir_list;
- wasi_args->map_dir_count = map_dir_count;
- wasi_args->env = env_list;
- wasi_args->env_count = env_count;
- wasi_args->argv = argv;
- wasi_args->argc = (uint32)argc;
- wasi_args->stdio[0] = stdinfd;
- wasi_args->stdio[1] = stdoutfd;
- wasi_args->stdio[2] = stderrfd;
- }
- }
- void
- wasm_runtime_set_wasi_args(WASMModuleCommon *module,
- const char *dir_list[], uint32 dir_count,
- const char *map_dir_list[], uint32 map_dir_count,
- const char *env_list[], uint32 env_count,
- char *argv[], int argc)
- {
- wasm_runtime_set_wasi_args_ex(module,
- dir_list, dir_count,
- map_dir_list, map_dir_count,
- env_list, env_count,
- argv, argc,
- -1, -1, -1);
- }
- #if WASM_ENABLE_UVWASI == 0
- bool
- wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
- const char *dir_list[], uint32 dir_count,
- const char *map_dir_list[], uint32 map_dir_count,
- const char *env[], uint32 env_count,
- char *argv[], uint32 argc,
- int stdinfd, int stdoutfd, int stderrfd,
- char *error_buf, uint32 error_buf_size)
- {
- WASIContext *wasi_ctx;
- char *argv_buf = NULL;
- char **argv_list = NULL;
- char *env_buf = NULL;
- char **env_list = NULL;
- uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
- uint32 argv_buf_offset = 0, env_buf_offset = 0;
- struct fd_table *curfds = NULL;
- struct fd_prestats *prestats = NULL;
- struct argv_environ_values *argv_environ = NULL;
- bool fd_table_inited = false, fd_prestats_inited = false;
- bool argv_environ_inited = false;
- __wasi_fd_t wasm_fd = 3;
- int32 raw_fd;
- char *path, resolved_path[PATH_MAX];
- uint32 i;
- if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL,
- error_buf, error_buf_size))) {
- return false;
- }
- wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode
- && !((WASMModuleInstance*)module_inst)->default_memory)
- return true;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT
- && !((AOTModuleInstance*)module_inst)->
- global_table_data.memory_instances[0].memory_data.ptr)
- return true;
- #endif
- /* process argv[0], trip the path and suffix, only keep the program name */
- for (i = 0; i < argc; i++)
- argv_buf_size += strlen(argv[i]) + 1;
- total_size = sizeof(char *) * (uint64)argc;
- if (total_size >= UINT32_MAX
- || (total_size > 0 &&
- !(argv_list = wasm_runtime_malloc((uint32)total_size)))
- || argv_buf_size >= UINT32_MAX
- || (argv_buf_size > 0 &&
- !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: allocate memory failed");
- goto fail;
- }
- for (i = 0; i < argc; i++) {
- argv_list[i] = argv_buf + argv_buf_offset;
- bh_strcpy_s(argv_buf + argv_buf_offset,
- (uint32)argv_buf_size - argv_buf_offset, argv[i]);
- argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
- }
- for (i = 0; i < env_count; i++)
- env_buf_size += strlen(env[i]) + 1;
- total_size = sizeof(char *) * (uint64)env_count;
- if (total_size >= UINT32_MAX
- || (total_size > 0
- && !(env_list = wasm_runtime_malloc((uint32)total_size)))
- || env_buf_size >= UINT32_MAX
- || (env_buf_size > 0
- && !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: allocate memory failed");
- goto fail;
- }
- for (i = 0; i < env_count; i++) {
- env_list[i] = env_buf + env_buf_offset;
- bh_strcpy_s(env_buf + env_buf_offset,
- (uint32)env_buf_size - env_buf_offset, env[i]);
- env_buf_offset += (uint32)(strlen(env[i]) + 1);
- }
- if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
- || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
- || !(argv_environ =
- wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: allocate memory failed");
- goto fail;
- }
- if (!fd_table_init(curfds)) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: "
- "init fd table failed");
- goto fail;
- }
- fd_table_inited = true;
- if (!fd_prestats_init(prestats)) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: "
- "init fd prestats failed");
- goto fail;
- }
- fd_prestats_inited = true;
- if (!argv_environ_init(argv_environ,
- argv_buf, argv_buf_size,
- argv_list, argc,
- env_buf, env_buf_size,
- env_list, env_count)) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: "
- "init argument environment failed");
- goto fail;
- }
- argv_environ_inited = true;
- /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
- if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
- || !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
- || !fd_table_insert_existing(curfds, 2, (stderrfd != -1) ? stderrfd : 2)) {
- set_error_buf(error_buf, error_buf_size,
- "Init wasi environment failed: init fd table failed");
- goto fail;
- }
- wasm_fd = 3;
- for (i = 0; i < dir_count; i++, wasm_fd++) {
- path = realpath(dir_list[i], resolved_path);
- if (!path) {
- if (error_buf)
- snprintf(error_buf, error_buf_size,
- "error while pre-opening directory %s: %d\n",
- dir_list[i], errno);
- goto fail;
- }
- raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
- if (raw_fd == -1) {
- if (error_buf)
- snprintf(error_buf, error_buf_size,
- "error while pre-opening directory %s: %d\n",
- dir_list[i], errno);
- goto fail;
- }
- fd_table_insert_existing(curfds, wasm_fd, raw_fd);
- fd_prestats_insert(prestats, dir_list[i], wasm_fd);
- }
- wasi_ctx->curfds = curfds;
- wasi_ctx->prestats = prestats;
- wasi_ctx->argv_environ = argv_environ;
- wasi_ctx->argv_buf = argv_buf;
- wasi_ctx->argv_list = argv_list;
- wasi_ctx->env_buf = env_buf;
- wasi_ctx->env_list = env_list;
- return true;
- fail:
- if (argv_environ_inited)
- argv_environ_destroy(argv_environ);
- if (fd_prestats_inited)
- fd_prestats_destroy(prestats);
- if (fd_table_inited)
- fd_table_destroy(curfds);
- if (curfds)
- wasm_runtime_free(curfds);
- if (prestats)
- wasm_runtime_free(prestats);
- if (argv_environ)
- wasm_runtime_free(argv_environ);
- if (argv_buf)
- wasm_runtime_free(argv_buf);
- if (argv_list)
- wasm_runtime_free(argv_list);
- if (env_buf)
- wasm_runtime_free(env_buf);
- if (env_list)
- wasm_runtime_free(env_list);
- return false;
- }
- #else /* else of WASM_ENABLE_UVWASI == 0 */
- static void *
- wasm_uvwasi_malloc(size_t size, void *mem_user_data)
- {
- return runtime_malloc(size, NULL, NULL, 0);
- (void)mem_user_data;
- }
- static void
- wasm_uvwasi_free(void *ptr, void *mem_user_data)
- {
- if (ptr)
- wasm_runtime_free(ptr);
- (void)mem_user_data;
- }
- static void *
- wasm_uvwasi_calloc(size_t nmemb, size_t size,
- void *mem_user_data)
- {
- uint64 total_size = (uint64)nmemb * size;
- return runtime_malloc(total_size, NULL, NULL, 0);
- (void)mem_user_data;
- }
- static void *
- wasm_uvwasi_realloc(void *ptr, size_t size,
- void *mem_user_data)
- {
- if (size >= UINT32_MAX) {
- return NULL;
- }
- return wasm_runtime_realloc(ptr, (uint32)size);
- }
- static uvwasi_mem_t uvwasi_allocator = {
- .mem_user_data = 0,
- .malloc = wasm_uvwasi_malloc,
- .free = wasm_uvwasi_free,
- .calloc = wasm_uvwasi_calloc,
- .realloc = wasm_uvwasi_realloc
- };
- bool
- wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
- const char *dir_list[], uint32 dir_count,
- const char *map_dir_list[], uint32 map_dir_count,
- const char *env[], uint32 env_count,
- char *argv[], uint32 argc,
- int stdinfd, int stdoutfd, int stderrfd,
- char *error_buf, uint32 error_buf_size)
- {
- uvwasi_t *uvwasi = NULL;
- uvwasi_options_t init_options;
- const char **envp = NULL;
- uint64 total_size;
- uint32 i;
- bool ret = false;
- uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst,
- error_buf, error_buf_size);
- if (!uvwasi)
- return false;
- /* Setup the initialization options */
- uvwasi_options_init(&init_options);
- init_options.allocator = &uvwasi_allocator;
- init_options.argc = argc;
- init_options.argv = (const char **)argv;
- init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
- init_options.out = (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
- init_options.err = (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
- if (dir_count > 0) {
- init_options.preopenc = dir_count;
- total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
- init_options.preopens =
- (uvwasi_preopen_t *)runtime_malloc(total_size, module_inst,
- error_buf, error_buf_size);
- if (init_options.preopens == NULL)
- goto fail;
- for (i = 0; i < init_options.preopenc; i++) {
- init_options.preopens[i].real_path = dir_list[i];
- init_options.preopens[i].mapped_path =
- (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
- }
- }
- if (env_count > 0) {
- total_size = sizeof(char *) * (uint64)(env_count + 1);
- envp = runtime_malloc(total_size, module_inst,
- error_buf, error_buf_size);
- if (envp == NULL)
- goto fail;
- for (i = 0; i < env_count; i++) {
- envp[i] = env[i];
- }
- envp[env_count] = NULL;
- init_options.envp = envp;
- }
- if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
- set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
- goto fail;
- }
- wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
- ret = true;
- fail:
- if (envp)
- wasm_runtime_free((void*)envp);
- if (init_options.preopens)
- wasm_runtime_free(init_options.preopens);
- if (!ret && uvwasi)
- wasm_runtime_free(uvwasi);
- return ret;
- }
- #endif /* end of WASM_ENABLE_UVWASI */
- bool
- wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode
- && ((WASMModuleInstance*)module_inst)->module->is_wasi_module)
- return true;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT
- && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr)
- ->is_wasi_module)
- return true;
- #endif
- return false;
- }
- WASMFunctionInstanceCommon *
- wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
- {
- uint32 i;
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
- WASMFunctionInstance *func;
- for (i = 0; i < wasm_inst->export_func_count; i++) {
- if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
- func = wasm_inst->export_functions[i].function;
- if (func->u.func->func_type->param_count != 0
- || func->u.func->func_type->result_count != 0) {
- LOG_ERROR("Lookup wasi _start function failed: "
- "invalid function type.\n");
- return NULL;
- }
- return (WASMFunctionInstanceCommon*)func;
- }
- }
- return NULL;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
- AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
- aot_inst->export_funcs.ptr;
- for (i = 0; i < aot_inst->export_func_count; i++) {
- if (!strcmp(export_funcs[i].func_name, "_start")) {
- AOTFuncType *func_type = export_funcs[i].u.func.func_type;
- if (func_type->param_count != 0
- || func_type->result_count != 0) {
- LOG_ERROR("Lookup wasi _start function failed: "
- "invalid function type.\n");
- return NULL;
- }
- return (WASMFunctionInstanceCommon*)&export_funcs[i];
- }
- }
- return NULL;
- }
- #endif /* end of WASM_ENABLE_AOT */
- return NULL;
- }
- #if WASM_ENABLE_UVWASI == 0
- void
- wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
- {
- WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
- if (wasi_ctx) {
- if (wasi_ctx->argv_environ) {
- argv_environ_destroy(wasi_ctx->argv_environ);
- wasm_runtime_free(wasi_ctx->argv_environ);
- }
- if (wasi_ctx->curfds) {
- fd_table_destroy(wasi_ctx->curfds);
- wasm_runtime_free(wasi_ctx->curfds);
- }
- if (wasi_ctx->prestats) {
- fd_prestats_destroy(wasi_ctx->prestats);
- wasm_runtime_free(wasi_ctx->prestats);
- }
- if (wasi_ctx->argv_buf)
- wasm_runtime_free(wasi_ctx->argv_buf);
- if (wasi_ctx->argv_list)
- wasm_runtime_free(wasi_ctx->argv_list);
- if (wasi_ctx->env_buf)
- wasm_runtime_free(wasi_ctx->env_buf);
- if (wasi_ctx->env_list)
- wasm_runtime_free(wasi_ctx->env_list);
- wasm_runtime_free(wasi_ctx);
- }
- }
- #else
- void
- wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
- {
- WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
- if (wasi_ctx) {
- uvwasi_destroy(wasi_ctx);
- wasm_runtime_free(wasi_ctx);
- }
- }
- #endif
- WASIContext *
- wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return ((WASMModuleInstance*)module_inst)->wasi_ctx;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr;
- #endif
- return NULL;
- }
- void
- wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
- WASIContext *wasi_ctx)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx;
- #endif
- }
- #endif /* end of WASM_ENABLE_LIBC_WASI */
- WASMModuleCommon*
- wasm_exec_env_get_module(WASMExecEnv *exec_env)
- {
- WASMModuleInstanceCommon *module_inst =
- wasm_runtime_get_module_inst(exec_env);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- return (WASMModuleCommon*)
- ((WASMModuleInstance*)module_inst)->module;
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- return (WASMModuleCommon*)
- ((AOTModuleInstance*)module_inst)->aot_module.ptr;
- #endif
- return NULL;
- }
- static union {
- int a;
- char b;
- } __ue = { .a = 1 };
- #define is_little_endian() (__ue.b == 1)
- bool
- wasm_runtime_register_natives(const char *module_name,
- NativeSymbol *native_symbols,
- uint32 n_native_symbols)
- {
- return wasm_native_register_natives(module_name,
- native_symbols, n_native_symbols);
- }
- bool
- wasm_runtime_register_natives_raw(const char *module_name,
- NativeSymbol *native_symbols,
- uint32 n_native_symbols)
- {
- return wasm_native_register_natives_raw(module_name,
- native_symbols, n_native_symbols);
- }
- bool
- wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
- const WASMType *func_type, const char *signature,
- void *attachment,
- uint32 *argv, uint32 argc, uint32 *argv_ret)
- {
- WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
- typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*);
- NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
- uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
- uint32 *argv_src = argv, i, argc1, ptr_len;
- uint32 arg_i32;
- bool ret = false;
- argc1 = func_type->param_count;
- if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
- size = sizeof(uint64) * (uint64)argc1;
- if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
- NULL, 0))) {
- return false;
- }
- }
- argv_dst = argv1;
- /* Traverse secondly to fill in each argument */
- for (i = 0; i < func_type->param_count; i++, argv_dst++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- {
- *(uint32*)argv_dst = arg_i32 = *argv_src++;
- if (signature) {
- if (signature[i + 1] == '*') {
- /* param is a pointer */
- if (signature[i + 2] == '~')
- /* pointer with length followed */
- ptr_len = *argv_src;
- else
- /* pointer without length followed */
- ptr_len = 1;
- if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
- goto fail;
- *(uintptr_t*)argv_dst = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- else if (signature[i + 1] == '$') {
- /* param is a string */
- if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
- goto fail;
- *(uintptr_t*)argv_dst = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- }
- break;
- }
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2);
- argv_src += 2;
- break;
- case VALUE_TYPE_F32:
- *(float32*)argv_dst = *(float32*)argv_src++;
- break;
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- *(uint32*)argv_dst = *argv_src++;
- break;
- #endif
- default:
- bh_assert(0);
- break;
- }
- }
- exec_env->attachment = attachment;
- invokeNativeRaw(exec_env, argv1);
- exec_env->attachment = NULL;
- if (func_type->result_count > 0) {
- switch (func_type->types[func_type->param_count]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- argv_ret[0] = *(uint32*)argv1;
- break;
- case VALUE_TYPE_F32:
- *(float32*)argv_ret = *(float32*)argv1;
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64));
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- ret = !wasm_runtime_get_exception(module) ? true : false;
- fail:
- if (argv1 != argv_buf)
- wasm_runtime_free(argv1);
- return ret;
- }
- /**
- * Implementation of wasm_runtime_invoke_native()
- */
- /* The invoke native implementation on ARM platform with VFP co-processor */
- #if defined(BUILD_TARGET_ARM_VFP) \
- || defined(BUILD_TARGET_THUMB_VFP) \
- || defined(BUILD_TARGET_RISCV32_ILP32D) \
- || defined(BUILD_TARGET_RISCV32_ILP32)
- typedef void (*GenericFunctionPointer)();
- int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
- typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32);
- typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
- typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32);
- typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
- typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32);
- static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
- static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
- static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
- static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
- static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
- #if !defined(BUILD_TARGET_RISCV32_ILP32D) \
- && !defined(BUILD_TARGET_RISCV32_ILP32)
- #define MAX_REG_INTS 4
- #define MAX_REG_FLOATS 16
- #else
- #define MAX_REG_INTS 8
- #define MAX_REG_FLOATS 8
- #endif
- bool
- wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
- const WASMType *func_type, const char *signature,
- void *attachment,
- uint32 *argv, uint32 argc, uint32 *argv_ret)
- {
- WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
- /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
- uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
- uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
- uint32 arg_i32, ptr_len;
- uint32 result_count = func_type->result_count;
- uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
- bool ret = false;
- #if !defined(BUILD_TARGET_RISCV32_ILP32)
- uint32 *fps;
- int n_fps = 0;
- #else
- #define fps ints
- #define n_fps n_ints
- #endif
- n_ints++; /* exec env */
- /* Traverse firstly to calculate stack args count */
- for (i = 0; i < func_type->param_count; i++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- if (n_ints < MAX_REG_INTS)
- n_ints++;
- else
- n_stacks++;
- break;
- case VALUE_TYPE_I64:
- if (n_ints < MAX_REG_INTS - 1) {
- #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_ints & 1)
- n_ints++;
- #endif
- n_ints += 2;
- }
- #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
- /* part in register, part in stack */
- else if (n_ints == MAX_REG_INTS - 1) {
- n_ints++;
- n_stacks++;
- }
- #endif
- else {
- /* 64-bit data in stack must be 8 bytes aligned
- in arm and riscv32 */
- if (n_stacks & 1)
- n_stacks++;
- n_stacks += 2;
- }
- break;
- #if !defined(BUILD_TARGET_RISCV32_ILP32D)
- case VALUE_TYPE_F32:
- if (n_fps < MAX_REG_FLOATS)
- n_fps++;
- else
- n_stacks++;
- break;
- case VALUE_TYPE_F64:
- if (n_fps < MAX_REG_FLOATS - 1) {
- #if !defined(BUILD_TARGET_RISCV32_ILP32)
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_fps & 1)
- n_fps++;
- #endif
- n_fps += 2;
- }
- #if defined(BUILD_TARGET_RISCV32_ILP32)
- else if (n_fps == MAX_REG_FLOATS - 1) {
- n_fps++;
- n_stacks++;
- }
- #endif
- else {
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_stacks & 1)
- n_stacks++;
- n_stacks += 2;
- }
- break;
- #else /* BUILD_TARGET_RISCV32_ILP32D */
- case VALUE_TYPE_F32:
- case VALUE_TYPE_F64:
- if (n_fps < MAX_REG_FLOATS) {
- n_fps++;
- }
- else if (func_type->types[i] == VALUE_TYPE_F32
- && n_ints < MAX_REG_INTS) {
- /* use int reg firstly if available */
- n_ints++;
- }
- else if (func_type->types[i] == VALUE_TYPE_F64
- && n_ints < MAX_REG_INTS - 1) {
- /* use int regs firstly if available */
- if (n_ints & 1)
- n_ints++;
- ints += 2;
- }
- else {
- /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
- if (n_stacks & 1)
- n_stacks++;
- n_stacks += 2;
- }
- break;
- #endif /* BUILD_TARGET_RISCV32_ILP32D */
- default:
- bh_assert(0);
- break;
- }
- }
- for (i = 0; i < ext_ret_count; i++) {
- if (n_ints < MAX_REG_INTS)
- n_ints++;
- else
- n_stacks++;
- }
- #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
- argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
- #elif defined(BUILD_TARGET_RISCV32_ILP32)
- argc1 = MAX_REG_INTS + n_stacks;
- #else
- argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
- #endif
- if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
- size = sizeof(uint32) * (uint32)argc1;
- if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
- NULL, 0))) {
- return false;
- }
- }
- ints = argv1;
- #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
- fps = ints + MAX_REG_INTS;
- stacks = fps + MAX_REG_FLOATS;
- #elif defined(BUILD_TARGET_RISCV32_ILP32)
- stacks = ints + MAX_REG_INTS;
- #else
- fps = ints + MAX_REG_INTS;
- stacks = fps + MAX_REG_FLOATS * 2;
- #endif
- n_ints = 0;
- n_fps = 0;
- n_stacks = 0;
- ints[n_ints++] = (uint32)(uintptr_t)exec_env;
- /* Traverse secondly to fill in each argument */
- for (i = 0; i < func_type->param_count; i++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- {
- arg_i32 = *argv_src++;
- if (signature) {
- if (signature[i + 1] == '*') {
- /* param is a pointer */
- if (signature[i + 2] == '~')
- /* pointer with length followed */
- ptr_len = *argv_src;
- else
- /* pointer without length followed */
- ptr_len = 1;
- if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
- goto fail;
- arg_i32 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- else if (signature[i + 1] == '$') {
- /* param is a string */
- if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
- goto fail;
- arg_i32 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- }
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = arg_i32;
- else
- stacks[n_stacks++] = arg_i32;
- break;
- }
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- {
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = *argv_src++;
- else
- stacks[n_stacks++] = *argv_src++;
- break;
- }
- #endif
- case VALUE_TYPE_I64:
- {
- if (n_ints < MAX_REG_INTS - 1) {
- #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_ints & 1)
- n_ints++;
- #endif
- *(uint64*)&ints[n_ints] = *(uint64*)argv_src;
- n_ints += 2;
- argv_src += 2;
- }
- #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
- else if (n_ints == MAX_REG_INTS - 1) {
- ints[n_ints++] = *argv_src++;
- stacks[n_stacks++] = *argv_src++;
- }
- #endif
- else {
- /* 64-bit data in stack must be 8 bytes aligned
- in arm and riscv32 */
- if (n_stacks & 1)
- n_stacks++;
- *(uint64*)&stacks[n_stacks] = *(uint64*)argv_src;
- n_stacks += 2;
- argv_src += 2;
- }
- break;
- }
- #if !defined(BUILD_TARGET_RISCV32_ILP32D)
- case VALUE_TYPE_F32:
- {
- if (n_fps < MAX_REG_FLOATS)
- *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
- else
- *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
- break;
- }
- case VALUE_TYPE_F64:
- {
- if (n_fps < MAX_REG_FLOATS - 1) {
- #if !defined(BUILD_TARGET_RISCV32_ILP32)
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_fps & 1)
- n_fps++;
- #endif
- *(float64*)&fps[n_fps] = *(float64*)argv_src;
- n_fps += 2;
- argv_src += 2;
- }
- #if defined(BUILD_TARGET_RISCV32_ILP32)
- else if (n_fps == MAX_REG_FLOATS - 1) {
- fps[n_fps++] = *argv_src++;
- stacks[n_stacks++] = *argv_src++;
- }
- #endif
- else {
- /* 64-bit data must be 8 bytes aligned in arm */
- if (n_stacks & 1)
- n_stacks++;
- *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
- n_stacks += 2;
- argv_src += 2;
- }
- break;
- }
- #else /* BUILD_TARGET_RISCV32_ILP32D */
- case VALUE_TYPE_F32:
- case VALUE_TYPE_F64:
- {
- if (n_fps < MAX_REG_FLOATS) {
- if (func_type->types[i] == VALUE_TYPE_F32) {
- *(float32*)&fps[n_fps * 2] = *(float32*)argv_src++;
- /* NaN boxing, the upper bits of a valid NaN-boxed
- value must be all 1s. */
- fps[n_fps * 2 + 1] = 0xFFFFFFFF;
- }
- else {
- *(float64*)&fps[n_fps * 2] = *(float64*)argv_src;
- argv_src += 2;
- }
- n_fps++;
- }
- else if (func_type->types[i] == VALUE_TYPE_F32
- && n_ints < MAX_REG_INTS) {
- /* use int reg firstly if available */
- *(float32*)&ints[n_ints++] = *(float32*)argv_src++;
- }
- else if (func_type->types[i] == VALUE_TYPE_F64
- && n_ints < MAX_REG_INTS - 1) {
- /* use int regs firstly if available */
- if (n_ints & 1)
- n_ints++;
- *(float64*)&ints[n_ints] = *(float64*)argv_src;
- n_ints += 2;
- argv_src += 2;
- }
- else {
- /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
- if (n_stacks & 1)
- n_stacks++;
- if (func_type->types[i] == VALUE_TYPE_F32) {
- *(float32*)&stacks[n_stacks] = *(float32*)argv_src++;
- /* NaN boxing, the upper bits of a valid NaN-boxed
- value must be all 1s. */
- stacks[n_stacks + 1] = 0xFFFFFFFF;
- }
- else {
- *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
- argv_src += 2;
- }
- n_stacks += 2;
- }
- break;
- }
- #endif /* BUILD_TARGET_RISCV32_ILP32D */
- default:
- bh_assert(0);
- break;
- }
- }
- /* Save extra result values' address to argv1 */
- for (i = 0; i < ext_ret_count; i++) {
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = *(uint32*)argv_src++;
- else
- stacks[n_stacks++] = *(uint32*)argv_src++;
- }
- exec_env->attachment = attachment;
- if (func_type->result_count == 0) {
- invokeNative_Void(func_ptr, argv1, n_stacks);
- }
- else {
- switch (func_type->types[func_type->param_count]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
- break;
- case VALUE_TYPE_I64:
- PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
- break;
- case VALUE_TYPE_F32:
- *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
- break;
- case VALUE_TYPE_F64:
- PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- exec_env->attachment = NULL;
- ret = !wasm_runtime_get_exception(module) ? true : false;
- fail:
- if (argv1 != argv_buf)
- wasm_runtime_free(argv1);
- return ret;
- }
- #endif /* end of defined(BUILD_TARGET_ARM_VFP)
- || defined(BUILD_TARGET_THUMB_VFP) \
- || defined(BUILD_TARGET_RISCV32_ILP32D)
- || defined(BUILD_TARGET_RISCV32_ILP32) */
- #if defined(BUILD_TARGET_X86_32) \
- || defined(BUILD_TARGET_ARM) \
- || defined(BUILD_TARGET_THUMB) \
- || defined(BUILD_TARGET_MIPS) \
- || defined(BUILD_TARGET_XTENSA)
- typedef void (*GenericFunctionPointer)();
- int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
- typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
- typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
- typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
- typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
- typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
- static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
- static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
- static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
- static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
- static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
- static inline void
- word_copy(uint32 *dest, uint32 *src, unsigned num)
- {
- for (; num > 0; num--)
- *dest++ = *src++;
- }
- bool
- wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
- const WASMType *func_type, const char *signature,
- void *attachment,
- uint32 *argv, uint32 argc, uint32 *argv_ret)
- {
- WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
- uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
- uint32 arg_i32, ptr_len;
- uint32 result_count = func_type->result_count;
- uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
- uint64 size;
- bool ret = false;
- #if defined(BUILD_TARGET_X86_32)
- argc1 = argc + ext_ret_count + 2;
- #else
- /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
- so we need to allocate more memory. */
- argc1 = func_type->param_count * 2 + ext_ret_count + 2;
- #endif
- if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
- size = sizeof(uint32) * (uint64)argc1;
- if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
- NULL, 0))) {
- return false;
- }
- }
- for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
- argv1[j++] = ((uint32*)&exec_env)[i];
- for (i = 0; i < func_type->param_count; i++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- {
- arg_i32 = *argv++;
- if (signature) {
- if (signature[i + 1] == '*') {
- /* param is a pointer */
- if (signature[i + 2] == '~')
- /* pointer with length followed */
- ptr_len = *argv;
- else
- /* pointer without length followed */
- ptr_len = 1;
- if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
- goto fail;
- arg_i32 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- else if (signature[i + 1] == '$') {
- /* param is a string */
- if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
- goto fail;
- arg_i32 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- }
- argv1[j++] = arg_i32;
- break;
- }
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- #if !defined(BUILD_TARGET_X86_32)
- /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
- and xtensa */
- if (j & 1)
- j++;
- #endif
- argv1[j++] = *argv++;
- argv1[j++] = *argv++;
- break;
- case VALUE_TYPE_F32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- argv1[j++] = *argv++;
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- /* Save extra result values' address to argv1 */
- word_copy(argv1 + j, argv, ext_ret_count);
- argc1 = j + ext_ret_count;
- exec_env->attachment = attachment;
- if (func_type->result_count == 0) {
- invokeNative_Void(func_ptr, argv1, argc1);
- }
- else {
- switch (func_type->types[func_type->param_count]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
- break;
- case VALUE_TYPE_I64:
- PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
- break;
- case VALUE_TYPE_F32:
- *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
- break;
- case VALUE_TYPE_F64:
- PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
- break;
- default:
- bh_assert(0);
- break;
- }
- }
- exec_env->attachment = NULL;
- ret = !wasm_runtime_get_exception(module) ? true : false;
- fail:
- if (argv1 != argv_buf)
- wasm_runtime_free(argv1);
- return ret;
- }
- #endif /* end of defined(BUILD_TARGET_X86_32) \
- || defined(BUILD_TARGET_ARM) \
- || defined(BUILD_TARGET_THUMB) \
- || defined(BUILD_TARGET_MIPS) \
- || defined(BUILD_TARGET_XTENSA) */
- #if defined(BUILD_TARGET_X86_64) \
- || defined(BUILD_TARGET_AMD_64) \
- || defined(BUILD_TARGET_AARCH64) \
- || defined(BUILD_TARGET_RISCV64_LP64D) \
- || defined(BUILD_TARGET_RISCV64_LP64)
- #if WASM_ENABLE_SIMD != 0
- #ifdef v128
- #undef v128
- #endif
- #if defined(_WIN32) || defined(_WIN32_)
- typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
- __int8 m128i_i8[16];
- __int16 m128i_i16[8];
- __int32 m128i_i32[4];
- __int64 m128i_i64[2];
- unsigned __int8 m128i_u8[16];
- unsigned __int16 m128i_u16[8];
- unsigned __int32 m128i_u32[4];
- unsigned __int64 m128i_u64[2];
- } v128;
- #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
- || defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
- typedef long long v128 __attribute__ ((__vector_size__ (16),
- __may_alias__, __aligned__ (1)));
- #elif defined(BUILD_TARGET_AARCH64)
- #include <arm_neon.h>
- typedef uint32x4_t __m128i;
- #define v128 __m128i
- #endif
- #endif /* end of WASM_ENABLE_SIMD != 0 */
- typedef void (*GenericFunctionPointer)();
- int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
- typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
- typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
- typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
- typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
- typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
- static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
- static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
- static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
- static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
- static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
- #if WASM_ENABLE_SIMD != 0
- typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64*, uint64);
- static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
- #endif
- #if defined(_WIN32) || defined(_WIN32_)
- #define MAX_REG_FLOATS 4
- #define MAX_REG_INTS 4
- #else /* else of defined(_WIN32) || defined(_WIN32_) */
- #define MAX_REG_FLOATS 8
- #if defined(BUILD_TARGET_AARCH64) \
- || defined(BUILD_TARGET_RISCV64_LP64D) \
- || defined(BUILD_TARGET_RISCV64_LP64)
- #define MAX_REG_INTS 8
- #else
- #define MAX_REG_INTS 6
- #endif /* end of defined(BUILD_TARGET_AARCH64) \
- || defined(BUILD_TARGET_RISCV64_LP64D) \
- || defined(BUILD_TARGET_RISCV64_LP64) */
- #endif /* end of defined(_WIN32) || defined(_WIN32_) */
- bool
- wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
- const WASMType *func_type, const char *signature,
- void *attachment,
- uint32 *argv, uint32 argc, uint32 *argv_ret)
- {
- WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
- uint64 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size, arg_i64;
- uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
- uint32 arg_i32, ptr_len;
- uint32 result_count = func_type->result_count;
- uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
- bool ret = false;
- #ifndef BUILD_TARGET_RISCV64_LP64
- #if WASM_ENABLE_SIMD == 0
- uint64 *fps;
- #else
- v128 *fps;
- #endif
- #else /* else of BUILD_TARGET_RISCV64_LP64 */
- #define fps ints
- #endif /* end of BUILD_TARGET_RISCV64_LP64 */
- #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
- /* important difference in calling conventions */
- #define n_fps n_ints
- #else
- int n_fps = 0;
- #endif
- #if WASM_ENABLE_SIMD == 0
- argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count
- + ext_ret_count;
- #else
- argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
- + ext_ret_count;
- #endif
- if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
- size = sizeof(uint64) * (uint64)argc1;
- if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
- NULL, 0))) {
- return false;
- }
- }
- #ifndef BUILD_TARGET_RISCV64_LP64
- #if WASM_ENABLE_SIMD == 0
- fps = argv1;
- ints = fps + MAX_REG_FLOATS;
- #else
- fps = (v128 *)argv1;
- ints = (uint64 *)(fps + MAX_REG_FLOATS);
- #endif
- #else /* else of BUILD_TARGET_RISCV64_LP64 */
- ints = argv1;
- #endif /* end of BUILD_TARGET_RISCV64_LP64 */
- stacks = ints + MAX_REG_INTS;
- ints[n_ints++] = (uint64)(uintptr_t)exec_env;
- for (i = 0; i < func_type->param_count; i++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- {
- arg_i32 = *argv_src++;
- arg_i64 = arg_i32;
- if (signature) {
- if (signature[i + 1] == '*') {
- /* param is a pointer */
- if (signature[i + 2] == '~')
- /* pointer with length followed */
- ptr_len = *argv_src;
- else
- /* pointer without length followed */
- ptr_len = 1;
- if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
- goto fail;
- arg_i64 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- else if (signature[i + 1] == '$') {
- /* param is a string */
- if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
- goto fail;
- arg_i64 = (uintptr_t)
- wasm_runtime_addr_app_to_native(module, arg_i32);
- }
- }
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = arg_i64;
- else
- stacks[n_stacks++] = arg_i64;
- break;
- }
- case VALUE_TYPE_I64:
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = *(uint64*)argv_src;
- else
- stacks[n_stacks++] = *(uint64*)argv_src;
- argv_src += 2;
- break;
- case VALUE_TYPE_F32:
- if (n_fps < MAX_REG_FLOATS) {
- *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
- }
- else {
- *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
- }
- break;
- case VALUE_TYPE_F64:
- if (n_fps < MAX_REG_FLOATS) {
- *(float64*)&fps[n_fps++] = *(float64*)argv_src;
- }
- else {
- *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
- }
- argv_src += 2;
- break;
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = *argv_src++;
- else
- stacks[n_stacks++] = *argv_src++;
- break;
- #endif
- #if WASM_ENABLE_SIMD != 0
- case VALUE_TYPE_V128:
- if (n_fps < MAX_REG_FLOATS) {
- *(v128*)&fps[n_fps++] = *(v128*)argv_src;
- }
- else {
- *(v128*)&stacks[n_stacks++] = *(v128*)argv_src;
- n_stacks++;
- }
- argv_src += 4;
- break;
- #endif
- default:
- bh_assert(0);
- break;
- }
- }
- /* Save extra result values' address to argv1 */
- for (i = 0; i < ext_ret_count; i++) {
- if (n_ints < MAX_REG_INTS)
- ints[n_ints++] = *(uint64*)argv_src;
- else
- stacks[n_stacks++] = *(uint64*)argv_src;
- argv_src += 2;
- }
- exec_env->attachment = attachment;
- if (result_count == 0) {
- invokeNative_Void(func_ptr, argv1, n_stacks);
- }
- else {
- /* Invoke the native function and get the first result value */
- switch (func_type->types[func_type->param_count]) {
- case VALUE_TYPE_I32:
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_FUNCREF:
- case VALUE_TYPE_EXTERNREF:
- #endif
- argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
- break;
- case VALUE_TYPE_I64:
- PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
- break;
- case VALUE_TYPE_F32:
- *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
- break;
- case VALUE_TYPE_F64:
- PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
- break;
- #if WASM_ENABLE_SIMD != 0
- case VALUE_TYPE_V128:
- *(v128*)argv_ret = invokeNative_V128(func_ptr, argv1, n_stacks);
- break;
- #endif
- default:
- bh_assert(0);
- break;
- }
- }
- exec_env->attachment = NULL;
- ret = !wasm_runtime_get_exception(module) ? true : false;
- fail:
- if (argv1 != argv_buf)
- wasm_runtime_free(argv1);
- return ret;
- }
- #endif /* end of defined(BUILD_TARGET_X86_64) \
- || defined(BUILD_TARGET_AMD_64) \
- || defined(BUILD_TARGET_AARCH64) \
- || defined(BUILD_TARGET_RISCV64_LP64D) \
- || defined(BUILD_TARGET_RISCV64_LP64) */
- bool
- wasm_runtime_call_indirect(WASMExecEnv *exec_env,
- uint32_t element_indices,
- uint32_t argc, uint32_t argv[])
- {
- if (!wasm_runtime_exec_env_check(exec_env)) {
- LOG_ERROR("Invalid exec env stack info.");
- return false;
- }
- /* this function is called from native code, so exec_env->handle and
- exec_env->native_stack_boundary must have been set, we don't set
- it again */
- #if WASM_ENABLE_INTERP != 0
- if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
- return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (exec_env->module_inst->module_type == Wasm_Module_AoT)
- return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
- #endif
- return false;
- }
- static void
- exchange_uint32(uint8 *p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 3);
- *(p_data + 3) = value;
- value = *(p_data + 1);
- *(p_data + 1) = *(p_data + 2);
- *(p_data + 2) = value;
- }
- static void
- exchange_uint64(uint8 *p_data)
- {
- uint32 value;
- value = *(uint32 *)p_data;
- *(uint32 *)p_data = *(uint32 *)(p_data + 4);
- *(uint32 *)(p_data + 4) = value;
- exchange_uint32(p_data);
- exchange_uint32(p_data + 4);
- }
- void
- wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
- {
- uint64 u1, u2;
- bh_memcpy_s(&u1, 8, bytes, 8);
- bh_memcpy_s(&u2, 8, bytes + 8, 8);
- if (!is_little_endian()) {
- exchange_uint64((uint8*)&u1);
- exchange_uint64((uint8*)&u2);
- *ret1 = u2;
- *ret2 = u1;
- }
- else {
- *ret1 = u1;
- *ret2 = u2;
- }
- }
- #if WASM_ENABLE_THREAD_MGR != 0
- typedef struct WASMThreadArg {
- WASMExecEnv *new_exec_env;
- wasm_thread_callback_t callback;
- void *arg;
- } WASMThreadArg;
- WASMExecEnv *
- wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
- {
- return wasm_cluster_spawn_exec_env(exec_env);
- }
- void
- wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
- {
- wasm_cluster_destroy_spawned_exec_env(exec_env);
- }
- static void*
- wasm_runtime_thread_routine(void *arg)
- {
- WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
- void *ret;
- bh_assert(thread_arg->new_exec_env);
- ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
- wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
- wasm_runtime_free(thread_arg);
- os_thread_exit(ret);
- return ret;
- }
- int32
- wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
- wasm_thread_callback_t callback, void *arg)
- {
- WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
- WASMThreadArg *thread_arg;
- int32 ret;
- if (!new_exec_env)
- return -1;
- if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
- wasm_runtime_destroy_spawned_exec_env(new_exec_env);
- return -1;
- }
- thread_arg->new_exec_env = new_exec_env;
- thread_arg->callback = callback;
- thread_arg->arg = arg;
- ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
- thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
- if (ret != 0) {
- wasm_runtime_destroy_spawned_exec_env(new_exec_env);
- wasm_runtime_free(thread_arg);
- }
- return ret;
- }
- int32
- wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
- {
- return os_thread_join((korp_tid)tid, retval);
- }
- #endif /* end of WASM_ENABLE_THREAD_MGR */
- #if WASM_ENABLE_REF_TYPES != 0
- static korp_mutex externref_lock;
- static uint32 externref_global_id = 1;
- static HashMap *externref_map;
- typedef struct ExternRefMapNode {
- /* The extern object from runtime embedder */
- void *extern_obj;
- /* The module instance it belongs to */
- WASMModuleInstanceCommon *module_inst;
- /* Whether it is retained */
- bool retained;
- /* Whether it is marked by runtime */
- bool marked;
- } ExternRefMapNode;
- static uint32
- wasm_externref_hash(const void *key)
- {
- uint32 externref_idx = (uint32)(uintptr_t)key;
- return externref_idx;
- }
- static bool
- wasm_externref_equal(void *key1, void *key2)
- {
- uint32 externref_idx1 = (uint32)(uintptr_t)key1;
- uint32 externref_idx2 = (uint32)(uintptr_t)key2;
- return externref_idx1 == externref_idx2 ? true : false;
- }
- static bool
- wasm_externref_map_init()
- {
- if (os_mutex_init(&externref_lock) != 0)
- return false;
- if (!(externref_map = bh_hash_map_create(32, false,
- wasm_externref_hash,
- wasm_externref_equal,
- NULL,
- wasm_runtime_free))) {
- os_mutex_destroy(&externref_lock);
- return false;
- }
- externref_global_id = 1;
- return true;
- }
- static void
- wasm_externref_map_destroy()
- {
- bh_hash_map_destroy(externref_map);
- os_mutex_destroy(&externref_lock);
- }
- typedef struct LookupExtObj_UserData {
- ExternRefMapNode node;
- bool found;
- uint32 externref_idx;
- } LookupExtObj_UserData;
- static void
- lookup_extobj_callback(void *key, void *value, void *user_data)
- {
- uint32 externref_idx = (uint32)(uintptr_t)key;
- ExternRefMapNode *node = (ExternRefMapNode *)value;
- LookupExtObj_UserData *user_data_lookup = (LookupExtObj_UserData *)
- user_data;
- if (node->extern_obj == user_data_lookup->node.extern_obj
- && node->module_inst == user_data_lookup->node.module_inst) {
- user_data_lookup->found = true;
- user_data_lookup->externref_idx = externref_idx;
- }
- }
- bool
- wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst,
- void *extern_obj, uint32 *p_externref_idx)
- {
- LookupExtObj_UserData lookup_user_data;
- ExternRefMapNode *node;
- uint32 externref_idx;
- lookup_user_data.node.extern_obj = extern_obj;
- lookup_user_data.node.module_inst = module_inst;
- lookup_user_data.found = false;
- os_mutex_lock(&externref_lock);
- /* Lookup hashmap firstly */
- bh_hash_map_traverse(externref_map, lookup_extobj_callback,
- (void*)&lookup_user_data);
- if (lookup_user_data.found) {
- *p_externref_idx = lookup_user_data.externref_idx;
- os_mutex_unlock(&externref_lock);
- return true;
- }
- /* Not found in hashmap */
- if (externref_global_id == NULL_REF
- || externref_global_id == 0) {
- goto fail1;
- }
- if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
- goto fail1;
- }
- memset(node, 0, sizeof(ExternRefMapNode));
- node->extern_obj = extern_obj;
- node->module_inst = module_inst;
- externref_idx = externref_global_id;
- if (!bh_hash_map_insert(externref_map,
- (void*)(uintptr_t)externref_idx,
- (void*)node)) {
- goto fail2;
- }
- externref_global_id++;
- *p_externref_idx = externref_idx;
- os_mutex_unlock(&externref_lock);
- return true;
- fail2:
- wasm_runtime_free(node);
- fail1:
- os_mutex_unlock(&externref_lock);
- return false;
- }
- bool
- wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
- {
- ExternRefMapNode *node;
- if (externref_idx == NULL_REF) {
- return false;
- }
- os_mutex_lock(&externref_lock);
- node = bh_hash_map_find(externref_map,
- (void*)(uintptr_t)externref_idx);
- os_mutex_unlock(&externref_lock);
- if (!node)
- return false;
- *p_extern_obj = node->extern_obj;
- return true;
- }
- static void
- reclaim_extobj_callback(void *key, void *value, void *user_data)
- {
- ExternRefMapNode *node = (ExternRefMapNode *)value;
- WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *)
- user_data;
- if (node->module_inst == module_inst) {
- if (!node->marked && !node->retained) {
- bh_hash_map_remove(externref_map, key, NULL, NULL);
- wasm_runtime_free(value);
- }
- else {
- node->marked = false;
- }
- }
- }
- static void
- mark_externref(uint32 externref_idx)
- {
- ExternRefMapNode *node;
- if (externref_idx != NULL_REF) {
- node = bh_hash_map_find(externref_map,
- (void*)(uintptr_t)externref_idx);
- if (node) {
- node->marked = true;
- }
- }
- }
- #if WASM_ENABLE_INTERP != 0
- static void
- interp_mark_all_externrefs(WASMModuleInstance *module_inst)
- {
- uint32 i, j, externref_idx, *table_data;
- uint8 *global_data = module_inst->global_data;
- WASMGlobalInstance *global;
- WASMTableInstance *table;
- global = module_inst->globals;
- for (i = 0; i < module_inst->global_count; i++, global++) {
- if (global->type == VALUE_TYPE_EXTERNREF) {
- externref_idx = *(uint32*)(global_data + global->data_offset);
- mark_externref(externref_idx);
- }
- }
- for (i = 0; i < module_inst->table_count; i++) {
- table = wasm_get_table_inst(module_inst, i);
- if (table->elem_type == VALUE_TYPE_EXTERNREF) {
- table_data = (uint32 *)table->base_addr;
- for (j = 0; j < table->cur_size; j++) {
- externref_idx = table_data[j];
- mark_externref(externref_idx);
- }
- }
- }
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- static void
- aot_mark_all_externrefs(AOTModuleInstance *module_inst)
- {
- uint32 i = 0, j = 0;
- const AOTModule *module = (AOTModule *)(module_inst->aot_module.ptr);
- const AOTTable *table = module->tables;
- const AOTGlobal *global = module->globals;
- const AOTTableInstance *table_inst =
- (AOTTableInstance *)module_inst->tables.ptr;
- for (i = 0; i < module->global_count; i++, global++) {
- if (global->type == VALUE_TYPE_EXTERNREF) {
- mark_externref(*(uint32 *)((uint8 *)module_inst->global_data.ptr
- + global->data_offset));
- }
- }
- for (i = 0; i < module->table_count;
- i++, table_inst = aot_next_tbl_inst(table_inst)) {
- if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
- while (j < table_inst->cur_size) {
- mark_externref(table_inst->data[j++]);
- }
- }
- }
- }
- #endif
- void
- wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
- {
- os_mutex_lock(&externref_lock);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode)
- interp_mark_all_externrefs((WASMModuleInstance*)module_inst);
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT)
- aot_mark_all_externrefs((AOTModuleInstance*)module_inst);
- #endif
- bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
- (void*)module_inst);
- os_mutex_unlock(&externref_lock);
- }
- static void
- cleanup_extobj_callback(void *key, void *value, void *user_data)
- {
- ExternRefMapNode *node = (ExternRefMapNode *)value;
- WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *)
- user_data;
- if (node->module_inst == module_inst) {
- bh_hash_map_remove(externref_map, key, NULL, NULL);
- wasm_runtime_free(value);
- }
- }
- void
- wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
- {
- os_mutex_lock(&externref_lock);
- bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
- (void*)module_inst);
- os_mutex_unlock(&externref_lock);
- }
- bool
- wasm_externref_retain(uint32 externref_idx)
- {
- ExternRefMapNode *node;
- os_mutex_lock(&externref_lock);
- if (externref_idx != NULL_REF) {
- node = bh_hash_map_find(externref_map,
- (void*)(uintptr_t)externref_idx);
- if (node) {
- node->retained = true;
- os_mutex_unlock(&externref_lock);
- return true;
- }
- }
- os_mutex_unlock(&externref_lock);
- return false;
- }
- #endif /* end of WASM_ENABLE_REF_TYPES */
- #if WASM_ENABLE_DUMP_CALL_STACK != 0
- void
- wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
- {
- WASMModuleInstanceCommon *module_inst
- = wasm_exec_env_get_module_inst(exec_env);
- #if WASM_ENABLE_INTERP != 0
- if (module_inst->module_type == Wasm_Module_Bytecode) {
- wasm_interp_dump_call_stack(exec_env);
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_inst->module_type == Wasm_Module_AoT) {
- aot_dump_call_stack(exec_env);
- }
- #endif
- }
- #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
- bool
- wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
- const WASMExport *export,
- WASMType **out)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_comm->module_type == Wasm_Module_Bytecode) {
- WASMModule *module = (WASMModule *)module_comm;
- if (export->index < module->import_function_count) {
- *out =
- module->import_functions[export->index].u.function.func_type;
- }
- else {
- *out =
- module->functions[export->index - module->import_function_count]
- ->func_type;
- }
- return true;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_comm->module_type == Wasm_Module_AoT) {
- AOTModule *module = (AOTModule *)module_comm;
- if (export->index < module->import_func_count) {
- *out = module->func_types[module->import_funcs[export->index]
- .func_type_index];
- }
- else {
- *out =
- module->func_types[module->func_type_indexes
- [export->index - module->import_func_count]];
- }
- return true;
- }
- #endif
- return false;
- }
- bool
- wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
- const WASMExport *export,
- uint8 *out_val_type,
- bool *out_mutability)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_comm->module_type == Wasm_Module_Bytecode) {
- WASMModule *module = (WASMModule *)module_comm;
- if (export->index < module->import_global_count) {
- WASMGlobalImport *import_global =
- &((module->import_globals + export->index)->u.global);
- *out_val_type = import_global->type;
- *out_mutability = import_global->is_mutable;
- }
- else {
- WASMGlobal *global =
- module->globals + (export->index - module->import_global_count);
- *out_val_type = global->type;
- *out_mutability = global->is_mutable;
- }
- return true;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_comm->module_type == Wasm_Module_AoT) {
- AOTModule *module = (AOTModule *)module_comm;
- if (export->index < module->import_global_count) {
- AOTImportGlobal *import_global =
- module->import_globals + export->index;
- *out_val_type = import_global->type;
- *out_mutability = import_global->is_mutable;
- }
- else {
- AOTGlobal *global =
- module->globals + (export->index - module->import_global_count);
- *out_val_type = global->type;
- *out_mutability = global->is_mutable;
- }
- return true;
- }
- #endif
- return false;
- }
- bool
- wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
- const WASMExport *export,
- uint32 *out_min_page,
- uint32 *out_max_page)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_comm->module_type == Wasm_Module_Bytecode) {
- WASMModule *module = (WASMModule *)module_comm;
- if (export->index < module->import_memory_count) {
- WASMMemoryImport *import_memory =
- &((module->import_memories + export->index)->u.memory);
- *out_min_page = import_memory->init_page_count;
- *out_max_page = import_memory->max_page_count;
- }
- else {
- WASMMemory *memory =
- module->memories + (export->index - module->import_memory_count);
- *out_min_page = memory->init_page_count;
- *out_max_page = memory->max_page_count;
- }
- return true;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_comm->module_type == Wasm_Module_AoT) {
- AOTModule *module = (AOTModule *)module_comm;
- if (export->index < module->import_memory_count) {
- AOTImportMemory *import_memory =
- module->import_memories + export->index;
- *out_min_page = import_memory->mem_init_page_count;
- *out_max_page = import_memory->mem_max_page_count;
- }
- else {
- AOTMemory *memory =
- module->memories + (export->index - module->import_memory_count);
- *out_min_page = memory->mem_init_page_count;
- *out_max_page = memory->mem_max_page_count;
- }
- return true;
- }
- #endif
- return false;
- }
- bool
- wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
- const WASMExport *export,
- uint8 *out_elem_type,
- uint32 *out_min_size,
- uint32 *out_max_size)
- {
- #if WASM_ENABLE_INTERP != 0
- if (module_comm->module_type == Wasm_Module_Bytecode) {
- WASMModule *module = (WASMModule *)module_comm;
- if (export->index < module->import_table_count) {
- WASMTableImport *import_table =
- &((module->import_tables + export->index)->u.table);
- *out_elem_type = import_table->elem_type;
- *out_min_size = import_table->init_size;
- *out_max_size = import_table->max_size;
- }
- else {
- WASMTable *table =
- module->tables + (export->index - module->import_table_count);
- *out_elem_type = table->elem_type;
- *out_min_size = table->init_size;
- *out_max_size = table->max_size;
- }
- return true;
- }
- #endif
- #if WASM_ENABLE_AOT != 0
- if (module_comm->module_type == Wasm_Module_AoT) {
- AOTModule *module = (AOTModule *)module_comm;
- if (export->index < module->import_table_count) {
- AOTImportTable *import_table =
- module->import_tables + export->index;
- *out_elem_type = VALUE_TYPE_FUNCREF;
- *out_min_size = import_table->table_init_size;
- *out_max_size = import_table->table_max_size;
- }
- else {
- AOTTable *table =
- module->tables + (export->index - module->import_table_count);
- *out_elem_type = table->elem_type;
- *out_min_size = table->table_init_size;
- *out_max_size = table->table_max_size;
- }
- return true;
- }
- #endif
- return false;
- }
- static inline bool
- argv_to_params(wasm_val_t *out_params,
- const uint32 *argv,
- WASMType *func_type)
- {
- wasm_val_t *param = out_params;
- uint32 i = 0, *u32;
- for (i = 0; i < func_type->param_count; i++, param++) {
- switch (func_type->types[i]) {
- case VALUE_TYPE_I32:
- param->kind = WASM_I32;
- param->of.i32 = *argv++;
- break;
- case VALUE_TYPE_I64:
- param->kind = WASM_I64;
- u32 = (uint32 *)¶m->of.i64;
- u32[0] = *argv++;
- u32[1] = *argv++;
- break;
- case VALUE_TYPE_F32:
- param->kind = WASM_F32;
- param->of.f32 = *(float32 *)argv++;
- break;
- case VALUE_TYPE_F64:
- param->kind = WASM_F64;
- u32 = (uint32 *)¶m->of.i64;
- u32[0] = *argv++;
- u32[1] = *argv++;
- break;
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_EXTERNREF:
- param->kind = WASM_ANYREF;
- if (NULL_REF == *argv) {
- param->of.ref = NULL;
- }
- else {
- if (!wasm_externref_ref2obj(*argv,
- (void **)¶m->of.ref)) {
- return false;
- }
- }
- argv++;
- break;
- #endif
- default:
- return false;
- }
- }
- return true;
- }
- static inline bool
- results_to_argv(WASMModuleInstanceCommon *module_inst,
- uint32 *out_argv,
- const wasm_val_t *results,
- WASMType *func_type)
- {
- const wasm_val_t *result = results;
- uint32 *argv = out_argv, *u32, i;
- uint8 *result_types = func_type->types + func_type->param_count;
- for (i = 0; i < func_type->result_count; i++, result++) {
- switch (result_types[i]) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- *(int32 *)argv++ = result->of.i32;
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- u32 = (uint32 *)&result->of.i64;
- *argv++ = u32[0];
- *argv++ = u32[1];
- break;
- #if WASM_ENABLE_REF_TYPES != 0
- case VALUE_TYPE_EXTERNREF:
- if (!wasm_externref_obj2ref(module_inst, result->of.ref,
- argv)) {
- return false;
- }
- argv++;
- break;
- #endif
- default:
- return false;
- }
- }
- return true;
- }
- bool
- wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
- void *func_ptr, WASMType *func_type,
- uint32 argc, uint32 *argv,
- bool with_env, void *wasm_c_api_env)
- {
- wasm_val_t params_buf[16], results_buf[4];
- wasm_val_t *params = params_buf, *results = results_buf;
- wasm_trap_t *trap = NULL;
- bool ret = false;
- if (func_type->param_count > 16
- && !(params = wasm_runtime_malloc(sizeof(wasm_val_t)
- * func_type->param_count))) {
- wasm_runtime_set_exception(module_inst, "allocate memory failed");
- return false;
- }
- if (!argv_to_params(params, argv, func_type)) {
- wasm_runtime_set_exception(module_inst, "unsupported param type");
- goto fail;
- }
- if (!with_env) {
- wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
- trap = callback(params, results);
- }
- else {
- wasm_func_callback_with_env_t callback =
- (wasm_func_callback_with_env_t)func_ptr;
- trap = callback(wasm_c_api_env, params, results);
- }
- if (trap) {
- if (trap->message->data) {
- /* since trap->message->data does not end with '\0' */
- char trap_message[128] = { 0 };
- bh_memcpy_s(
- trap_message, 127, trap->message->data,
- (trap->message->size < 127 ? (uint32)trap->message->size : 127));
- wasm_runtime_set_exception(module_inst, trap_message);
- }
- else {
- wasm_runtime_set_exception(
- module_inst, "native function throw unknown exception");
- }
- wasm_trap_delete(trap);
- goto fail;
- }
- if (func_type->result_count > 4
- && !(results = wasm_runtime_malloc(sizeof(wasm_val_t)
- * func_type->result_count))) {
- wasm_runtime_set_exception(module_inst, "allocate memory failed");
- goto fail;
- }
- if (!results_to_argv(module_inst, argv, results, func_type)) {
- wasm_runtime_set_exception(module_inst, "unsupported result type");
- goto fail;
- }
- ret = true;
- fail:
- if (params != params_buf)
- wasm_runtime_free(params);
- if (results != results_buf)
- wasm_runtime_free(results);
- return ret;
- }
|