wasm_c_api.c 149 KB

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