wasm_c_api.c 150 KB

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