wasm_interp_fast.c 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "wasm_memory.h"
  11. #include "../common/wasm_exec_env.h"
  12. #if WASM_ENABLE_GC != 0
  13. #include "../common/gc/gc_object.h"
  14. #include "mem_alloc.h"
  15. #endif
  16. #if WASM_ENABLE_SHARED_MEMORY != 0
  17. #include "../common/wasm_shared_memory.h"
  18. #endif
  19. typedef int32 CellType_I32;
  20. typedef int64 CellType_I64;
  21. typedef float32 CellType_F32;
  22. typedef float64 CellType_F64;
  23. #if WASM_ENABLE_THREAD_MGR == 0
  24. #define get_linear_mem_size() linear_mem_size
  25. #else
  26. /**
  27. * Load memory data size in each time boundary check in
  28. * multi-threading mode since it may be changed by other
  29. * threads in memory.grow
  30. */
  31. #define get_linear_mem_size() memory->memory_data_size
  32. #endif
  33. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  34. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  35. #define CHECK_MEMORY_OVERFLOW(bytes) \
  36. do { \
  37. uint64 offset1 = (uint64)offset + (uint64)addr; \
  38. if (offset1 + bytes <= (uint64)get_linear_mem_size()) \
  39. /* If offset1 is in valid range, maddr must also \
  40. be in valid range, no need to check it again. */ \
  41. maddr = memory->memory_data + offset1; \
  42. else \
  43. goto out_of_bounds; \
  44. } while (0)
  45. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  46. do { \
  47. uint64 offset1 = (uint32)(start); \
  48. if (offset1 + bytes <= get_linear_mem_size()) \
  49. /* App heap space is not valid space for \
  50. bulk memory operation */ \
  51. maddr = memory->memory_data + offset1; \
  52. else \
  53. goto out_of_bounds; \
  54. } while (0)
  55. #else
  56. #define CHECK_MEMORY_OVERFLOW(bytes) \
  57. do { \
  58. uint64 offset1 = (uint64)offset + (uint64)addr; \
  59. maddr = memory->memory_data + offset1; \
  60. } while (0)
  61. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  62. do { \
  63. maddr = memory->memory_data + (uint32)(start); \
  64. } while (0)
  65. #endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
  66. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  67. #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
  68. do { \
  69. if (((uintptr_t)maddr & (align - 1)) != 0) \
  70. goto unaligned_atomic; \
  71. } while (0)
  72. static inline uint32
  73. rotl32(uint32 n, uint32 c)
  74. {
  75. const uint32 mask = (31);
  76. c = c % 32;
  77. c &= mask;
  78. return (n << c) | (n >> ((0 - c) & mask));
  79. }
  80. static inline uint32
  81. rotr32(uint32 n, uint32 c)
  82. {
  83. const uint32 mask = (31);
  84. c = c % 32;
  85. c &= mask;
  86. return (n >> c) | (n << ((0 - c) & mask));
  87. }
  88. static inline uint64
  89. rotl64(uint64 n, uint64 c)
  90. {
  91. const uint64 mask = (63);
  92. c = c % 64;
  93. c &= mask;
  94. return (n << c) | (n >> ((0 - c) & mask));
  95. }
  96. static inline uint64
  97. rotr64(uint64 n, uint64 c)
  98. {
  99. const uint64 mask = (63);
  100. c = c % 64;
  101. c &= mask;
  102. return (n >> c) | (n << ((0 - c) & mask));
  103. }
  104. static inline float32
  105. f32_min(float32 a, float32 b)
  106. {
  107. if (isnan(a) || isnan(b))
  108. return NAN;
  109. else if (a == 0 && a == b)
  110. return signbit(a) ? a : b;
  111. else
  112. return a > b ? b : a;
  113. }
  114. static inline float32
  115. f32_max(float32 a, float32 b)
  116. {
  117. if (isnan(a) || isnan(b))
  118. return NAN;
  119. else if (a == 0 && a == b)
  120. return signbit(a) ? b : a;
  121. else
  122. return a > b ? a : b;
  123. }
  124. static inline float64
  125. f64_min(float64 a, float64 b)
  126. {
  127. if (isnan(a) || isnan(b))
  128. return NAN;
  129. else if (a == 0 && a == b)
  130. return signbit(a) ? a : b;
  131. else
  132. return a > b ? b : a;
  133. }
  134. static inline float64
  135. f64_max(float64 a, float64 b)
  136. {
  137. if (isnan(a) || isnan(b))
  138. return NAN;
  139. else if (a == 0 && a == b)
  140. return signbit(a) ? b : a;
  141. else
  142. return a > b ? a : b;
  143. }
  144. static inline uint32
  145. clz32(uint32 type)
  146. {
  147. uint32 num = 0;
  148. if (type == 0)
  149. return 32;
  150. while (!(type & 0x80000000)) {
  151. num++;
  152. type <<= 1;
  153. }
  154. return num;
  155. }
  156. static inline uint32
  157. clz64(uint64 type)
  158. {
  159. uint32 num = 0;
  160. if (type == 0)
  161. return 64;
  162. while (!(type & 0x8000000000000000LL)) {
  163. num++;
  164. type <<= 1;
  165. }
  166. return num;
  167. }
  168. static inline uint32
  169. ctz32(uint32 type)
  170. {
  171. uint32 num = 0;
  172. if (type == 0)
  173. return 32;
  174. while (!(type & 1)) {
  175. num++;
  176. type >>= 1;
  177. }
  178. return num;
  179. }
  180. static inline uint32
  181. ctz64(uint64 type)
  182. {
  183. uint32 num = 0;
  184. if (type == 0)
  185. return 64;
  186. while (!(type & 1)) {
  187. num++;
  188. type >>= 1;
  189. }
  190. return num;
  191. }
  192. static inline uint32
  193. popcount32(uint32 u)
  194. {
  195. uint32 ret = 0;
  196. while (u) {
  197. u = (u & (u - 1));
  198. ret++;
  199. }
  200. return ret;
  201. }
  202. static inline uint32
  203. popcount64(uint64 u)
  204. {
  205. uint32 ret = 0;
  206. while (u) {
  207. u = (u & (u - 1));
  208. ret++;
  209. }
  210. return ret;
  211. }
  212. static float
  213. local_copysignf(float x, float y)
  214. {
  215. union {
  216. float f;
  217. uint32_t i;
  218. } ux = { x }, uy = { y };
  219. ux.i &= 0x7fffffff;
  220. ux.i |= uy.i & 0x80000000;
  221. return ux.f;
  222. }
  223. static double
  224. local_copysign(double x, double y)
  225. {
  226. union {
  227. double f;
  228. uint64_t i;
  229. } ux = { x }, uy = { y };
  230. ux.i &= -1ULL / 2;
  231. ux.i |= uy.i & 1ULL << 63;
  232. return ux.f;
  233. }
  234. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  235. #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
  236. #define LOAD_PTR(addr) (*(void **)(addr))
  237. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  238. static inline uint32
  239. LOAD_U32_WITH_2U16S(void *addr)
  240. {
  241. union {
  242. uint32 val;
  243. uint16 u16[2];
  244. } u;
  245. bh_assert(((uintptr_t)addr & 1) == 0);
  246. u.u16[0] = ((uint16 *)addr)[0];
  247. u.u16[1] = ((uint16 *)addr)[1];
  248. return u.val;
  249. }
  250. #if UINTPTR_MAX == UINT32_MAX
  251. #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
  252. #elif UINTPTR_MAX == UINT64_MAX
  253. static inline void *
  254. LOAD_PTR(void *addr)
  255. {
  256. uintptr_t addr1 = (uintptr_t)addr;
  257. union {
  258. void *val;
  259. uint32 u32[2];
  260. uint16 u16[4];
  261. } u;
  262. bh_assert(((uintptr_t)addr & 1) == 0);
  263. if ((addr1 & (uintptr_t)7) == 0)
  264. return *(void **)addr;
  265. if ((addr1 & (uintptr_t)3) == 0) {
  266. u.u32[0] = ((uint32 *)addr)[0];
  267. u.u32[1] = ((uint32 *)addr)[1];
  268. }
  269. else {
  270. u.u16[0] = ((uint16 *)addr)[0];
  271. u.u16[1] = ((uint16 *)addr)[1];
  272. u.u16[2] = ((uint16 *)addr)[2];
  273. u.u16[3] = ((uint16 *)addr)[3];
  274. }
  275. return u.val;
  276. }
  277. #endif /* end of UINTPTR_MAX */
  278. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  279. #if WASM_ENABLE_GC != 0
  280. static void
  281. init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
  282. {
  283. uint32 i, j;
  284. memset(frame_ref, 0, cell_num);
  285. for (i = 0, j = 0; i < func->param_count; i++) {
  286. if (wasm_is_type_reftype(func->param_types[i])) {
  287. frame_ref[j++] = 1;
  288. #if UINTPTR_MAX == UINT64_MAX
  289. frame_ref[j++] = 1;
  290. #endif
  291. }
  292. else if (func->param_types[i] == VALUE_TYPE_I32
  293. || func->param_types[i] == VALUE_TYPE_F32)
  294. j++;
  295. else
  296. j += 2;
  297. }
  298. for (i = 0; i < func->local_count; i++) {
  299. if (wasm_is_type_reftype(func->local_types[i])) {
  300. frame_ref[j++] = 1;
  301. #if UINTPTR_MAX == UINT64_MAX
  302. frame_ref[j++] = 1;
  303. #endif
  304. }
  305. else if (func->local_types[i] == VALUE_TYPE_I32
  306. || func->local_types[i] == VALUE_TYPE_F32)
  307. j++;
  308. else
  309. j += 2;
  310. }
  311. }
  312. /* Return the corresponding ref slot of the given slot of local
  313. variable or stack pointer. */
  314. #define COMPUTE_FRAME_REF(ref, off) (ref + (unsigned)(off))
  315. #define FRAME_REF(off) COMPUTE_FRAME_REF(frame_ref, off)
  316. #if UINTPTR_MAX == UINT64_MAX
  317. #define SET_FRAME_REF(off) *FRAME_REF(off) = *FRAME_REF(off + 1) = 1
  318. #define CLEAR_FRAME_REF(off) *FRAME_REF(off) = *FRAME_REF(off + 1) = 0
  319. #else
  320. #define SET_FRAME_REF(off) *FRAME_REF(off) = 1
  321. #define CLEAR_FRAME_REF(off) *FRAME_REF(off) = 0
  322. #endif
  323. #define FRAME_REF_FOR(frame, p) \
  324. COMPUTE_FRAME_REF(frame->frame_ref, p - frame->lp)
  325. #define CLEAR_FRAME_REF_FOR(p, n) \
  326. do { \
  327. int32 ref_i, ref_n = (int32)(n); \
  328. uint8 *ref = FRAME_REF(p - frame_lp); \
  329. for (ref_i = 0; ref_i < ref_n; ref_i++) \
  330. ref[ref_i] = 0; \
  331. } while (0)
  332. #endif /* end of WASM_ENABLE_GC != 0 */
  333. #define read_uint32(p) \
  334. (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
  335. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  336. do { \
  337. uint32 param_count = cur_func->param_count; \
  338. local_idx = read_uint32(frame_ip); \
  339. bh_assert(local_idx < param_count + cur_func->local_count); \
  340. local_offset = cur_func->local_offsets[local_idx]; \
  341. if (local_idx < param_count) \
  342. local_type = cur_func->param_types[local_idx]; \
  343. else \
  344. local_type = cur_func->local_types[local_idx - param_count]; \
  345. } while (0)
  346. #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
  347. #define SET_OPERAND_I32(off, value) \
  348. do { \
  349. *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  350. } while (0)
  351. #define SET_OPERAND_F32(off, value) \
  352. do { \
  353. *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  354. } while (0)
  355. #define SET_OPERAND_I64(off, value) \
  356. do { \
  357. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  358. PUT_I64_TO_ADDR(addr_tmp, value); \
  359. } while (0)
  360. #define SET_OPERAND_F64(off, value) \
  361. do { \
  362. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  363. PUT_F64_TO_ADDR(addr_tmp, value); \
  364. } while (0)
  365. #define SET_OPERAND_REF(off, value) \
  366. do { \
  367. uint32 *addr_tmp; \
  368. opnd_off = *(int16 *)(frame_ip + off); \
  369. addr_tmp = frame_lp + opnd_off; \
  370. PUT_REF_TO_ADDR(addr_tmp, value); \
  371. SET_FRAME_REF(ond_off); \
  372. } while (0)
  373. #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
  374. #define GET_OPERAND_I32(type, off) \
  375. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  376. #define GET_OPERAND_F32(type, off) \
  377. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  378. #define GET_OPERAND_I64(type, off) \
  379. (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  380. #define GET_OPERAND_F64(type, off) \
  381. (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  382. #define GET_OPERAND_REF(type, off) \
  383. (type) GET_REF_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  384. #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
  385. #define PUSH_I32(value) \
  386. do { \
  387. *(int32 *)(frame_lp + GET_OFFSET()) = value; \
  388. } while (0)
  389. #define PUSH_F32(value) \
  390. do { \
  391. *(float32 *)(frame_lp + GET_OFFSET()) = value; \
  392. } while (0)
  393. #define PUSH_I64(value) \
  394. do { \
  395. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  396. PUT_I64_TO_ADDR(addr_tmp, value); \
  397. } while (0)
  398. #define PUSH_F64(value) \
  399. do { \
  400. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  401. PUT_F64_TO_ADDR(addr_tmp, value); \
  402. } while (0)
  403. #define PUSH_REF(value) \
  404. do { \
  405. uint32 *addr_tmp; \
  406. opnd_off = GET_OFFSET(); \
  407. addr_tmp = frame_lp + opnd_off; \
  408. PUT_REF_TO_ADDR(addr_tmp, value); \
  409. SET_FRAME_REF(opnd_off); \
  410. } while (0)
  411. #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
  412. #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
  413. #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
  414. #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
  415. #define POP_REF() \
  416. (opnd_off = GET_OFFSET(), CLEAR_FRAME_REF(opnd_off), \
  417. GET_REF_FROM_ADDR(frame_lp + opnd_off))
  418. #if WASM_ENABLE_GC != 0
  419. #define SYNC_FRAME_REF() frame->frame_ref = frame_ref
  420. #define UPDATE_FRAME_REF() frame_ref = frame->frame_ref
  421. #else
  422. #define SYNC_FRAME_REF() (void)0
  423. #define UPDATE_FRAME_REF() (void)0
  424. #endif
  425. #define SYNC_ALL_TO_FRAME() \
  426. do { \
  427. frame->ip = frame_ip; \
  428. SYNC_FRAME_REF(); \
  429. } while (0)
  430. #define UPDATE_ALL_FROM_FRAME() \
  431. do { \
  432. frame_ip = frame->ip; \
  433. UPDATE_FRAME_REF(); \
  434. } while (0)
  435. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  436. #define UPDATE_FRAME_IP_END() (void)0
  437. #else
  438. #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  439. #endif
  440. #if WASM_ENABLE_GC != 0
  441. #define RECOVER_FRAME_REF() frame_ref = frame->frame_ref
  442. #else
  443. #define RECOVER_FRAME_REF() (void)0
  444. #endif
  445. #define RECOVER_CONTEXT(new_frame) \
  446. do { \
  447. frame = (new_frame); \
  448. cur_func = frame->function; \
  449. prev_frame = frame->prev_frame; \
  450. frame_ip = frame->ip; \
  451. UPDATE_FRAME_IP_END(); \
  452. frame_lp = frame->lp; \
  453. RECOVER_FRAME_REF(); \
  454. } while (0)
  455. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  456. #define GET_OPCODE() opcode = *frame_ip++;
  457. #else
  458. #define GET_OPCODE() \
  459. opcode = *frame_ip; \
  460. frame_ip += 2;
  461. #endif
  462. #define DEF_OP_EQZ(ctype, src_op_type) \
  463. do { \
  464. SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
  465. frame_ip += 4; \
  466. } while (0)
  467. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  468. do { \
  469. SET_OPERAND(I32, 4, \
  470. GET_OPERAND(src_type, src_op_type, 2) \
  471. cond GET_OPERAND(src_type, src_op_type, 0)); \
  472. frame_ip += 6; \
  473. } while (0)
  474. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  475. do { \
  476. SET_OPERAND( \
  477. src_op_type, 2, \
  478. (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
  479. frame_ip += 4; \
  480. } while (0)
  481. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  482. do { \
  483. SET_OPERAND(src_op_type, 4, \
  484. GET_OPERAND(src_type1, src_op_type, 2) \
  485. operation GET_OPERAND(src_type2, src_op_type, 0)); \
  486. frame_ip += 6; \
  487. } while (0)
  488. #define DEF_OP_REINTERPRET(src_type, src_op_type) \
  489. do { \
  490. SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
  491. frame_ip += 4; \
  492. } while (0)
  493. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  494. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  495. do { \
  496. SET_OPERAND(src_op_type, 4, \
  497. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  498. GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
  499. frame_ip += 6; \
  500. } while (0)
  501. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  502. do { \
  503. SET_OPERAND(src_op_type, 4, \
  504. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  505. GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
  506. frame_ip += 6; \
  507. } while (0)
  508. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  509. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  510. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  511. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  512. { \
  513. uint32 readv, sval; \
  514. \
  515. sval = POP_I32(); \
  516. addr = POP_I32(); \
  517. \
  518. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  519. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  520. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  521. \
  522. os_mutex_lock(&node->shared_mem_lock); \
  523. readv = (uint32)(*(uint8 *)maddr); \
  524. *(uint8 *)maddr = (uint8)(readv op sval); \
  525. os_mutex_unlock(&node->shared_mem_lock); \
  526. } \
  527. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  528. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  529. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  530. \
  531. os_mutex_lock(&node->shared_mem_lock); \
  532. readv = (uint32)LOAD_U16(maddr); \
  533. STORE_U16(maddr, (uint16)(readv op sval)); \
  534. os_mutex_unlock(&node->shared_mem_lock); \
  535. } \
  536. else { \
  537. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  538. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  539. \
  540. os_mutex_lock(&node->shared_mem_lock); \
  541. readv = LOAD_I32(maddr); \
  542. STORE_U32(maddr, readv op sval); \
  543. os_mutex_unlock(&node->shared_mem_lock); \
  544. } \
  545. PUSH_I32(readv); \
  546. break; \
  547. } \
  548. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  549. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  550. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  551. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  552. { \
  553. uint64 readv, sval; \
  554. \
  555. sval = (uint64)POP_I64(); \
  556. addr = POP_I32(); \
  557. \
  558. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  559. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  560. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  561. \
  562. os_mutex_lock(&node->shared_mem_lock); \
  563. readv = (uint64)(*(uint8 *)maddr); \
  564. *(uint8 *)maddr = (uint8)(readv op sval); \
  565. os_mutex_unlock(&node->shared_mem_lock); \
  566. } \
  567. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  568. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  569. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  570. \
  571. os_mutex_lock(&node->shared_mem_lock); \
  572. readv = (uint64)LOAD_U16(maddr); \
  573. STORE_U16(maddr, (uint16)(readv op sval)); \
  574. os_mutex_unlock(&node->shared_mem_lock); \
  575. } \
  576. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  577. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  578. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  579. \
  580. os_mutex_lock(&node->shared_mem_lock); \
  581. readv = (uint64)LOAD_U32(maddr); \
  582. STORE_U32(maddr, (uint32)(readv op sval)); \
  583. os_mutex_unlock(&node->shared_mem_lock); \
  584. } \
  585. else { \
  586. uint64 op_result; \
  587. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \
  588. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  589. \
  590. os_mutex_lock(&node->shared_mem_lock); \
  591. readv = (uint64)LOAD_I64(maddr); \
  592. op_result = readv op sval; \
  593. STORE_I64(maddr, op_result); \
  594. os_mutex_unlock(&node->shared_mem_lock); \
  595. } \
  596. PUSH_I64(readv); \
  597. break; \
  598. }
  599. #define DEF_OP_MATH(src_type, src_op_type, method) \
  600. do { \
  601. SET_OPERAND(src_op_type, 2, \
  602. (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
  603. frame_ip += 4; \
  604. } while (0)
  605. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  606. static dst_type func_name(src_type src_value, src_type src_min, \
  607. src_type src_max, dst_type dst_min, \
  608. dst_type dst_max, bool is_sign) \
  609. { \
  610. dst_type dst_value = 0; \
  611. if (!isnan(src_value)) { \
  612. if (src_value <= src_min) \
  613. dst_value = dst_min; \
  614. else if (src_value >= src_max) \
  615. dst_value = dst_max; \
  616. else { \
  617. if (is_sign) \
  618. dst_value = (dst_type)(signed_type)src_value; \
  619. else \
  620. dst_value = (dst_type)src_value; \
  621. } \
  622. } \
  623. return dst_value; \
  624. }
  625. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  626. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  627. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  628. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  629. static bool
  630. trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  631. float32 src_min, float32 src_max, bool saturating, bool is_i32,
  632. bool is_sign)
  633. {
  634. float32 src_value = GET_OPERAND(float32, F32, 0);
  635. uint64 dst_value_i64;
  636. uint32 dst_value_i32;
  637. if (!saturating) {
  638. if (isnan(src_value)) {
  639. wasm_set_exception(module, "invalid conversion to integer");
  640. return false;
  641. }
  642. else if (src_value <= src_min || src_value >= src_max) {
  643. wasm_set_exception(module, "integer overflow");
  644. return false;
  645. }
  646. }
  647. if (is_i32) {
  648. uint32 dst_min = is_sign ? INT32_MIN : 0;
  649. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  650. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  651. dst_max, is_sign);
  652. SET_OPERAND(I32, 2, dst_value_i32);
  653. }
  654. else {
  655. uint64 dst_min = is_sign ? INT64_MIN : 0;
  656. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  657. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  658. dst_max, is_sign);
  659. SET_OPERAND(I64, 2, dst_value_i64);
  660. }
  661. return true;
  662. }
  663. static bool
  664. trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  665. float64 src_min, float64 src_max, bool saturating, bool is_i32,
  666. bool is_sign)
  667. {
  668. float64 src_value = GET_OPERAND(float64, F64, 0);
  669. uint64 dst_value_i64;
  670. uint32 dst_value_i32;
  671. if (!saturating) {
  672. if (isnan(src_value)) {
  673. wasm_set_exception(module, "invalid conversion to integer");
  674. return false;
  675. }
  676. else if (src_value <= src_min || src_value >= src_max) {
  677. wasm_set_exception(module, "integer overflow");
  678. return false;
  679. }
  680. }
  681. if (is_i32) {
  682. uint32 dst_min = is_sign ? INT32_MIN : 0;
  683. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  684. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  685. dst_max, is_sign);
  686. SET_OPERAND(I32, 2, dst_value_i32);
  687. }
  688. else {
  689. uint64 dst_min = is_sign ? INT64_MIN : 0;
  690. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  691. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  692. dst_max, is_sign);
  693. SET_OPERAND(I64, 2, dst_value_i64);
  694. }
  695. return true;
  696. }
  697. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  698. do { \
  699. if (!trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
  700. is_i32, is_sign)) \
  701. goto got_exception; \
  702. frame_ip += 4; \
  703. } while (0)
  704. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  705. do { \
  706. if (!trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
  707. is_i32, is_sign)) \
  708. goto got_exception; \
  709. frame_ip += 4; \
  710. } while (0)
  711. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  712. do { \
  713. (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
  714. is_i32, is_sign); \
  715. frame_ip += 4; \
  716. } while (0)
  717. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  718. do { \
  719. (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
  720. is_i32, is_sign); \
  721. frame_ip += 4; \
  722. } while (0)
  723. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  724. do { \
  725. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  726. PUSH_##dst_op_type(value); \
  727. } while (0)
  728. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  729. #define CELL_SIZE sizeof(uint8)
  730. #else
  731. #define CELL_SIZE (sizeof(uint8) * 2)
  732. #endif
  733. static bool
  734. copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
  735. uint32 total_cell_num, const uint8 *cells,
  736. const int16 *src_offsets, const uint16 *dst_offsets)
  737. {
  738. /* To avoid the overlap issue between src offsets and dst offset,
  739. * we use 2 steps to do the copy. First step, copy the src values
  740. * to a tmp buf. Second step, copy the values from tmp buf to dst.
  741. */
  742. uint32 buf[16] = { 0 }, i;
  743. uint32 *tmp_buf = buf;
  744. uint8 cell;
  745. int16 src, buf_index = 0;
  746. uint16 dst;
  747. /* Allocate memory if the buf is not large enough */
  748. if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
  749. uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
  750. if (total_size >= UINT32_MAX
  751. || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
  752. wasm_set_exception(module, "allocate memory failed");
  753. return false;
  754. }
  755. }
  756. /* 1) Copy values from src to tmp buf */
  757. for (i = 0; i < arity; i++) {
  758. cell = cells[i * CELL_SIZE];
  759. src = src_offsets[i];
  760. if (cell == 1)
  761. tmp_buf[buf_index] = frame_lp[src];
  762. else {
  763. tmp_buf[buf_index] = frame_lp[src];
  764. tmp_buf[buf_index + 1] = frame_lp[src + 1];
  765. }
  766. buf_index += cell;
  767. }
  768. /* 2) Copy values from tmp buf to dest */
  769. buf_index = 0;
  770. for (i = 0; i < arity; i++) {
  771. cell = cells[i * CELL_SIZE];
  772. dst = dst_offsets[i];
  773. if (cell == 1)
  774. frame_lp[dst] = tmp_buf[buf_index];
  775. else {
  776. frame_lp[dst] = tmp_buf[buf_index];
  777. frame_lp[dst + 1] = tmp_buf[buf_index + 1];
  778. }
  779. buf_index += cell;
  780. }
  781. if (tmp_buf != buf) {
  782. wasm_runtime_free(tmp_buf);
  783. }
  784. return true;
  785. }
  786. #define RECOVER_BR_INFO() \
  787. do { \
  788. uint32 arity; \
  789. /* read arity */ \
  790. arity = read_uint32(frame_ip); \
  791. if (arity) { \
  792. uint32 total_cell; \
  793. uint16 *dst_offsets = NULL; \
  794. uint8 *cells; \
  795. int16 *src_offsets = NULL; \
  796. /* read total cell num */ \
  797. total_cell = read_uint32(frame_ip); \
  798. /* cells */ \
  799. cells = (uint8 *)frame_ip; \
  800. frame_ip += arity * CELL_SIZE; \
  801. /* src offsets */ \
  802. src_offsets = (int16 *)frame_ip; \
  803. frame_ip += arity * sizeof(int16); \
  804. /* dst offsets */ \
  805. dst_offsets = (uint16 *)frame_ip; \
  806. frame_ip += arity * sizeof(uint16); \
  807. if (arity == 1) { \
  808. if (cells[0] == 1) \
  809. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  810. else if (cells[0] == 2) { \
  811. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  812. frame_lp[dst_offsets[0] + 1] = \
  813. frame_lp[src_offsets[0] + 1]; \
  814. } \
  815. } \
  816. else { \
  817. if (!copy_stack_values(module, frame_lp, arity, total_cell, \
  818. cells, src_offsets, dst_offsets)) \
  819. goto got_exception; \
  820. } \
  821. } \
  822. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  823. } while (0)
  824. #define SKIP_BR_INFO() \
  825. do { \
  826. uint32 arity; \
  827. /* read and skip arity */ \
  828. arity = read_uint32(frame_ip); \
  829. if (arity) { \
  830. /* skip total cell num */ \
  831. frame_ip += sizeof(uint32); \
  832. /* skip cells, src offsets and dst offsets */ \
  833. frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
  834. } \
  835. /* skip target address */ \
  836. frame_ip += sizeof(uint8 *); \
  837. } while (0)
  838. static inline int32
  839. sign_ext_8_32(int8 val)
  840. {
  841. if (val & 0x80)
  842. return (int32)val | (int32)0xffffff00;
  843. return val;
  844. }
  845. static inline int32
  846. sign_ext_16_32(int16 val)
  847. {
  848. if (val & 0x8000)
  849. return (int32)val | (int32)0xffff0000;
  850. return val;
  851. }
  852. static inline int64
  853. sign_ext_8_64(int8 val)
  854. {
  855. if (val & 0x80)
  856. return (int64)val | (int64)0xffffffffffffff00LL;
  857. return val;
  858. }
  859. static inline int64
  860. sign_ext_16_64(int16 val)
  861. {
  862. if (val & 0x8000)
  863. return (int64)val | (int64)0xffffffffffff0000LL;
  864. return val;
  865. }
  866. static inline int64
  867. sign_ext_32_64(int32 val)
  868. {
  869. if (val & (int32)0x80000000)
  870. return (int64)val | (int64)0xffffffff00000000LL;
  871. return val;
  872. }
  873. static inline void
  874. word_copy(uint32 *dest, uint32 *src, unsigned num)
  875. {
  876. bh_assert(dest != NULL);
  877. bh_assert(src != NULL);
  878. bh_assert(num > 0);
  879. if (dest != src) {
  880. /* No overlap buffer */
  881. bh_assert(!((src < dest) && (dest < src + num)));
  882. for (; num > 0; num--)
  883. *dest++ = *src++;
  884. }
  885. }
  886. static inline WASMInterpFrame *
  887. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  888. {
  889. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  890. if (frame) {
  891. frame->prev_frame = prev_frame;
  892. #if WASM_ENABLE_PERF_PROFILING != 0
  893. frame->time_started = os_time_get_boot_microsecond();
  894. #endif
  895. }
  896. else {
  897. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  898. "wasm operand stack overflow");
  899. }
  900. return frame;
  901. }
  902. static inline void
  903. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  904. {
  905. #if WASM_ENABLE_PERF_PROFILING != 0
  906. if (frame->function) {
  907. frame->function->total_exec_time +=
  908. os_time_get_boot_microsecond() - frame->time_started;
  909. frame->function->total_exec_cnt++;
  910. }
  911. #endif
  912. wasm_exec_env_free_wasm_frame(exec_env, frame);
  913. }
  914. static void
  915. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  916. WASMExecEnv *exec_env,
  917. WASMFunctionInstance *cur_func,
  918. WASMInterpFrame *prev_frame)
  919. {
  920. WASMFunctionImport *func_import = cur_func->u.func_import;
  921. CApiFuncImport *c_api_func_import = NULL;
  922. unsigned local_cell_num =
  923. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  924. unsigned all_cell_num;
  925. WASMInterpFrame *frame;
  926. uint32 argv_ret[2], cur_func_index;
  927. void *native_func_pointer = NULL;
  928. bool ret;
  929. all_cell_num = local_cell_num;
  930. #if WASM_ENABLE_GC != 0
  931. all_cell_num += (local_cell_num + 3) / 4;
  932. #endif
  933. if (!(frame =
  934. ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
  935. prev_frame)))
  936. return;
  937. frame->function = cur_func;
  938. frame->ip = NULL;
  939. frame->lp = frame->operand;
  940. #if WASM_ENABLE_GC != 0
  941. frame->frame_ref = (uint8 *)(frame->lp + local_cell_num);
  942. init_frame_refs(frame->frame_ref, local_cell_num, cur_func);
  943. #endif
  944. wasm_exec_env_set_cur_frame(exec_env, frame);
  945. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  946. bh_assert(cur_func_index < module_inst->module->import_function_count);
  947. if (!func_import->call_conv_wasm_c_api) {
  948. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  949. }
  950. else if (module_inst->e->c_api_func_imports) {
  951. c_api_func_import = module_inst->e->c_api_func_imports + cur_func_index;
  952. native_func_pointer = c_api_func_import->func_ptr_linked;
  953. }
  954. if (!native_func_pointer) {
  955. char buf[128];
  956. snprintf(buf, sizeof(buf),
  957. "failed to call unlinked import function (%s, %s)",
  958. func_import->module_name, func_import->field_name);
  959. wasm_set_exception((WASMModuleInstance *)module_inst, buf);
  960. return;
  961. }
  962. if (func_import->call_conv_wasm_c_api) {
  963. ret = wasm_runtime_invoke_c_api_native(
  964. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  965. func_import->func_type, cur_func->param_cell_num, frame->lp,
  966. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  967. if (ret) {
  968. argv_ret[0] = frame->lp[0];
  969. argv_ret[1] = frame->lp[1];
  970. }
  971. }
  972. else if (!func_import->call_conv_raw) {
  973. ret = wasm_runtime_invoke_native(
  974. exec_env, native_func_pointer, func_import->func_type,
  975. func_import->signature, func_import->attachment, frame->lp,
  976. cur_func->param_cell_num, argv_ret);
  977. }
  978. else {
  979. ret = wasm_runtime_invoke_native_raw(
  980. exec_env, native_func_pointer, func_import->func_type,
  981. func_import->signature, func_import->attachment, frame->lp,
  982. cur_func->param_cell_num, argv_ret);
  983. }
  984. if (!ret)
  985. return;
  986. if (cur_func->ret_cell_num == 1) {
  987. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  988. }
  989. else if (cur_func->ret_cell_num == 2) {
  990. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  991. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  992. }
  993. FREE_FRAME(exec_env, frame);
  994. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  995. }
  996. #if WASM_ENABLE_MULTI_MODULE != 0
  997. static void
  998. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  999. WASMExecEnv *exec_env,
  1000. WASMFunctionInstance *cur_func,
  1001. WASMInterpFrame *prev_frame);
  1002. static void
  1003. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  1004. WASMExecEnv *exec_env,
  1005. WASMFunctionInstance *cur_func,
  1006. WASMInterpFrame *prev_frame)
  1007. {
  1008. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  1009. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  1010. WASMFunctionImport *func_import = cur_func->u.func_import;
  1011. uint8 *ip = prev_frame->ip;
  1012. char buf[128];
  1013. WASMExecEnv *sub_module_exec_env = NULL;
  1014. uint32 aux_stack_origin_boundary = 0;
  1015. uint32 aux_stack_origin_bottom = 0;
  1016. if (!sub_func_inst) {
  1017. snprintf(buf, sizeof(buf),
  1018. "failed to call unlinked import function (%s, %s)",
  1019. func_import->module_name, func_import->field_name);
  1020. wasm_set_exception(module_inst, buf);
  1021. return;
  1022. }
  1023. /* Switch exec_env but keep using the same one by replacing necessary
  1024. * variables */
  1025. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  1026. (WASMModuleInstanceCommon *)sub_module_inst);
  1027. if (!sub_module_exec_env) {
  1028. wasm_set_exception(module_inst, "create singleton exec_env failed");
  1029. return;
  1030. }
  1031. /* - module_inst */
  1032. exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst;
  1033. /* - aux_stack_boundary */
  1034. aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
  1035. exec_env->aux_stack_boundary.boundary =
  1036. sub_module_exec_env->aux_stack_boundary.boundary;
  1037. /* - aux_stack_bottom */
  1038. aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
  1039. exec_env->aux_stack_bottom.bottom =
  1040. sub_module_exec_env->aux_stack_bottom.bottom;
  1041. /* set ip NULL to make call_func_bytecode return after executing
  1042. this function */
  1043. prev_frame->ip = NULL;
  1044. /* call function of sub-module*/
  1045. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  1046. prev_frame);
  1047. /* restore ip and other replaced */
  1048. prev_frame->ip = ip;
  1049. exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
  1050. exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
  1051. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  1052. /* transfer exception if it is thrown */
  1053. if (wasm_copy_exception(sub_module_inst, NULL)) {
  1054. bh_memcpy_s(module_inst->cur_exception,
  1055. sizeof(module_inst->cur_exception),
  1056. sub_module_inst->cur_exception,
  1057. sizeof(sub_module_inst->cur_exception));
  1058. }
  1059. }
  1060. #endif
  1061. #if WASM_ENABLE_THREAD_MGR != 0
  1062. #define CHECK_SUSPEND_FLAGS() \
  1063. do { \
  1064. os_mutex_lock(&exec_env->wait_lock); \
  1065. if (exec_env->suspend_flags.flags != 0) { \
  1066. if (exec_env->suspend_flags.flags & 0x01) { \
  1067. /* terminate current thread */ \
  1068. os_mutex_unlock(&exec_env->wait_lock); \
  1069. return; \
  1070. } \
  1071. /* TODO: support suspend and breakpoint */ \
  1072. } \
  1073. os_mutex_unlock(&exec_env->wait_lock); \
  1074. } while (0)
  1075. #endif
  1076. #if WASM_ENABLE_OPCODE_COUNTER != 0
  1077. typedef struct OpcodeInfo {
  1078. char *name;
  1079. uint64 count;
  1080. } OpcodeInfo;
  1081. /* clang-format off */
  1082. #define HANDLE_OPCODE(op) \
  1083. { \
  1084. #op, 0 \
  1085. }
  1086. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  1087. #undef HANDLE_OPCODE
  1088. /* clang-format on */
  1089. static void
  1090. wasm_interp_dump_op_count()
  1091. {
  1092. uint32 i;
  1093. uint64 total_count = 0;
  1094. for (i = 0; i < WASM_OP_IMPDEP; i++)
  1095. total_count += opcode_table[i].count;
  1096. os_printf("total opcode count: %ld\n", total_count);
  1097. for (i = 0; i < WASM_OP_IMPDEP; i++)
  1098. if (opcode_table[i].count > 0)
  1099. os_printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  1100. opcode_table[i].count,
  1101. opcode_table[i].count * 100.0f / total_count);
  1102. }
  1103. #endif
  1104. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1105. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  1106. #if WASM_ENABLE_OPCODE_COUNTER != 0
  1107. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  1108. #else
  1109. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1110. #endif
  1111. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1112. #define FETCH_OPCODE_AND_DISPATCH() \
  1113. do { \
  1114. const void *p_label_addr = *(void **)frame_ip; \
  1115. frame_ip += sizeof(void *); \
  1116. goto *p_label_addr; \
  1117. } while (0)
  1118. #else
  1119. #define FETCH_OPCODE_AND_DISPATCH() \
  1120. do { \
  1121. const void *p_label_addr = label_base + *(int16 *)frame_ip; \
  1122. frame_ip += sizeof(int16); \
  1123. goto *p_label_addr; \
  1124. } while (0)
  1125. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  1126. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1127. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1128. #define HANDLE_OP(opcode) case opcode:
  1129. #define HANDLE_OP_END() continue
  1130. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1131. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1132. static void **global_handle_table;
  1133. #endif
  1134. static inline uint8 *
  1135. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1136. {
  1137. #if WASM_ENABLE_MULTI_MODULE == 0
  1138. return global_data + global->data_offset;
  1139. #else
  1140. return global->import_global_inst
  1141. ? global->import_module_inst->global_data
  1142. + global->import_global_inst->data_offset
  1143. : global_data + global->data_offset;
  1144. #endif
  1145. }
  1146. static void
  1147. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1148. WASMExecEnv *exec_env,
  1149. WASMFunctionInstance *cur_func,
  1150. WASMInterpFrame *prev_frame)
  1151. {
  1152. #if WASM_ENABLE_SHARED_MEMORY != 0
  1153. WASMSharedMemNode *node =
  1154. wasm_module_get_shared_memory((WASMModuleCommon *)module->module);
  1155. #endif
  1156. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1157. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1158. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1159. || WASM_ENABLE_BULK_MEMORY != 0
  1160. uint32 linear_mem_size = memory ? memory->memory_data_size : 0;
  1161. #endif
  1162. WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
  1163. WASMGlobalInstance *global;
  1164. uint8 *global_data = module->global_data;
  1165. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1166. WASMInterpFrame *frame = NULL;
  1167. /* Points to this special opcode so as to jump to the
  1168. * call_method_from_entry. */
  1169. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1170. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1171. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1172. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1173. /* cache of label base addr */
  1174. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  1175. #endif
  1176. #endif
  1177. #if WASM_ENABLE_GC != 0
  1178. register uint8 *frame_ref = NULL; /* cache of frame->ref */
  1179. int16 opnd_off;
  1180. #endif
  1181. uint8 *frame_ip_end = frame_ip + 1;
  1182. uint32 cond, count, fidx, tidx, frame_size = 0;
  1183. uint32 all_cell_num = 0;
  1184. int16 addr1, addr2, addr_ret = 0;
  1185. int32 didx, val;
  1186. uint8 *maddr = NULL;
  1187. uint32 local_idx, local_offset, global_idx;
  1188. uint8 opcode, local_type, *global_addr;
  1189. #if WASM_ENABLE_GC != 0
  1190. WASMObjectRef gc_obj;
  1191. WASMStructObjectRef struct_obj;
  1192. WASMArrayObjectRef array_obj;
  1193. WASMFuncObjectRef func_obj;
  1194. WASMI31ObjectRef i31_obj;
  1195. WASMExternrefObjectRef externref_obj;
  1196. uint32 type_idx;
  1197. #endif
  1198. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1199. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1200. DEFINE_GOTO_TABLE(const void *, handle_table);
  1201. #undef HANDLE_OPCODE
  1202. if (exec_env == NULL) {
  1203. global_handle_table = (void **)handle_table;
  1204. return;
  1205. }
  1206. #endif
  1207. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1208. while (frame_ip < frame_ip_end) {
  1209. opcode = *frame_ip++;
  1210. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1211. frame_ip++;
  1212. #endif
  1213. switch (opcode) {
  1214. #else
  1215. goto *handle_table[WASM_OP_IMPDEP];
  1216. #endif
  1217. /* control instructions */
  1218. HANDLE_OP(WASM_OP_UNREACHABLE)
  1219. {
  1220. wasm_set_exception(module, "unreachable");
  1221. goto got_exception;
  1222. }
  1223. HANDLE_OP(WASM_OP_IF)
  1224. {
  1225. cond = (uint32)POP_I32();
  1226. if (cond == 0) {
  1227. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1228. if (else_addr == NULL) {
  1229. frame_ip =
  1230. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1231. }
  1232. else {
  1233. frame_ip = else_addr;
  1234. }
  1235. }
  1236. else {
  1237. frame_ip += sizeof(uint8 *) * 2;
  1238. }
  1239. HANDLE_OP_END();
  1240. }
  1241. HANDLE_OP(WASM_OP_ELSE)
  1242. {
  1243. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1244. HANDLE_OP_END();
  1245. }
  1246. HANDLE_OP(WASM_OP_BR)
  1247. {
  1248. #if WASM_ENABLE_THREAD_MGR != 0
  1249. CHECK_SUSPEND_FLAGS();
  1250. #endif
  1251. recover_br_info:
  1252. RECOVER_BR_INFO();
  1253. HANDLE_OP_END();
  1254. }
  1255. HANDLE_OP(WASM_OP_BR_IF)
  1256. {
  1257. #if WASM_ENABLE_THREAD_MGR != 0
  1258. CHECK_SUSPEND_FLAGS();
  1259. #endif
  1260. cond = frame_lp[GET_OFFSET()];
  1261. if (cond)
  1262. goto recover_br_info;
  1263. else
  1264. SKIP_BR_INFO();
  1265. HANDLE_OP_END();
  1266. }
  1267. HANDLE_OP(WASM_OP_BR_TABLE)
  1268. {
  1269. uint32 arity, br_item_size;
  1270. #if WASM_ENABLE_THREAD_MGR != 0
  1271. CHECK_SUSPEND_FLAGS();
  1272. #endif
  1273. count = read_uint32(frame_ip);
  1274. didx = GET_OPERAND(uint32, I32, 0);
  1275. frame_ip += 2;
  1276. if (!(didx >= 0 && (uint32)didx < count))
  1277. didx = count;
  1278. /* all br items must have the same arity and item size,
  1279. so we only calculate the first item size */
  1280. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1281. br_item_size = sizeof(uint32); /* arity */
  1282. if (arity) {
  1283. /* total cell num */
  1284. br_item_size += sizeof(uint32);
  1285. /* cells, src offsets and dst offsets */
  1286. br_item_size +=
  1287. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1288. }
  1289. /* target address */
  1290. br_item_size += sizeof(uint8 *);
  1291. frame_ip += br_item_size * didx;
  1292. goto recover_br_info;
  1293. }
  1294. HANDLE_OP(WASM_OP_RETURN)
  1295. {
  1296. uint32 ret_idx;
  1297. WASMFuncType *func_type;
  1298. uint32 off, ret_offset;
  1299. uint8 *ret_types;
  1300. if (cur_func->is_import_func)
  1301. func_type = cur_func->u.func_import->func_type;
  1302. else
  1303. func_type = cur_func->u.func->func_type;
  1304. /* types of each return value */
  1305. ret_types = func_type->types + func_type->param_count;
  1306. ret_offset = prev_frame->ret_offset;
  1307. for (ret_idx = 0,
  1308. off = sizeof(int16) * (func_type->result_count - 1);
  1309. ret_idx < func_type->result_count;
  1310. ret_idx++, off -= sizeof(int16)) {
  1311. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1312. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1313. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1314. GET_OPERAND(uint64, I64, off));
  1315. ret_offset += 2;
  1316. }
  1317. #if WASM_ENABLE_GC != 0
  1318. else if (wasm_is_type_reftype(ret_types[ret_idx])) {
  1319. PUT_REF_TO_ADDR(prev_frame->lp + ret_offset,
  1320. GET_OPERAND(void *, REF, off));
  1321. ret_offset += REF_CELL_NUM;
  1322. }
  1323. #endif
  1324. else {
  1325. prev_frame->lp[ret_offset] =
  1326. GET_OPERAND(uint32, I32, off);
  1327. ret_offset++;
  1328. }
  1329. }
  1330. goto return_func;
  1331. }
  1332. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1333. #if WASM_ENABLE_TAIL_CALL != 0
  1334. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1335. #endif
  1336. {
  1337. WASMFuncType *cur_type, *cur_func_type;
  1338. WASMTableInstance *tbl_inst;
  1339. uint32 tbl_idx;
  1340. #if WASM_ENABLE_TAIL_CALL != 0
  1341. GET_OPCODE();
  1342. #endif
  1343. #if WASM_ENABLE_THREAD_MGR != 0
  1344. CHECK_SUSPEND_FLAGS();
  1345. #endif
  1346. tidx = read_uint32(frame_ip);
  1347. cur_type = (WASMFuncType *)module->module->types[tidx];
  1348. tbl_idx = read_uint32(frame_ip);
  1349. bh_assert(tbl_idx < module->table_count);
  1350. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1351. val = GET_OPERAND(uint32, I32, 0);
  1352. frame_ip += 2;
  1353. if ((uint32)val >= tbl_inst->cur_size) {
  1354. wasm_set_exception(module, "undefined element");
  1355. goto got_exception;
  1356. }
  1357. /* clang-format off */
  1358. #if WASM_ENABLE_GC == 0
  1359. fidx = tbl_inst->elems[val];
  1360. if (fidx == (uint32)-1) {
  1361. wasm_set_exception(module, "uninitialized element");
  1362. goto got_exception;
  1363. }
  1364. #else
  1365. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  1366. if (!func_obj) {
  1367. wasm_set_exception(module, "uninitialized element");
  1368. goto got_exception;
  1369. }
  1370. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1371. #endif
  1372. /* clang-format on */
  1373. /*
  1374. * we might be using a table injected by host or
  1375. * another module. in that case, we don't validate
  1376. * the elem value while loading
  1377. */
  1378. if (fidx >= module->e->function_count) {
  1379. wasm_set_exception(module, "unknown function");
  1380. goto got_exception;
  1381. }
  1382. /* always call module own functions */
  1383. cur_func = module->e->functions + fidx;
  1384. if (cur_func->is_import_func)
  1385. cur_func_type = cur_func->u.func_import->func_type;
  1386. else
  1387. cur_func_type = cur_func->u.func->func_type;
  1388. /* clang-format off */
  1389. #if WASM_ENABLE_GC == 0
  1390. if (cur_type != cur_func_type) {
  1391. wasm_set_exception(module, "indirect call type mismatch");
  1392. goto got_exception;
  1393. }
  1394. #else
  1395. if (cur_type->min_type_idx_normalized
  1396. != cur_func_type->min_type_idx_normalized) {
  1397. wasm_set_exception(module, "indirect call type mismatch");
  1398. goto got_exception;
  1399. }
  1400. #endif
  1401. /* clang-format on */
  1402. #if WASM_ENABLE_TAIL_CALL != 0
  1403. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1404. goto call_func_from_return_call;
  1405. #endif
  1406. goto call_func_from_interp;
  1407. }
  1408. /* parametric instructions */
  1409. HANDLE_OP(WASM_OP_SELECT)
  1410. {
  1411. cond = frame_lp[GET_OFFSET()];
  1412. addr1 = GET_OFFSET();
  1413. addr2 = GET_OFFSET();
  1414. addr_ret = GET_OFFSET();
  1415. if (!cond) {
  1416. if (addr_ret != addr1)
  1417. frame_lp[addr_ret] = frame_lp[addr1];
  1418. }
  1419. else {
  1420. if (addr_ret != addr2)
  1421. frame_lp[addr_ret] = frame_lp[addr2];
  1422. }
  1423. HANDLE_OP_END();
  1424. }
  1425. HANDLE_OP(WASM_OP_SELECT_64)
  1426. {
  1427. cond = frame_lp[GET_OFFSET()];
  1428. addr1 = GET_OFFSET();
  1429. addr2 = GET_OFFSET();
  1430. addr_ret = GET_OFFSET();
  1431. if (!cond) {
  1432. if (addr_ret != addr1)
  1433. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1434. GET_I64_FROM_ADDR(frame_lp + addr1));
  1435. }
  1436. else {
  1437. if (addr_ret != addr2)
  1438. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1439. GET_I64_FROM_ADDR(frame_lp + addr2));
  1440. }
  1441. HANDLE_OP_END();
  1442. }
  1443. #if WASM_ENABLE_GC != 0
  1444. HANDLE_OP(WASM_OP_SELECT_T)
  1445. {
  1446. cond = frame_lp[GET_OFFSET()];
  1447. addr1 = GET_OFFSET();
  1448. addr2 = GET_OFFSET();
  1449. addr_ret = GET_OFFSET();
  1450. if (!cond) {
  1451. if (addr_ret != addr1)
  1452. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1453. GET_REF_FROM_ADDR(frame_lp + addr1));
  1454. }
  1455. else {
  1456. if (addr_ret != addr2)
  1457. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1458. GET_REF_FROM_ADDR(frame_lp + addr2));
  1459. }
  1460. CLEAR_FRAME_REF(addr1);
  1461. CLEAR_FRAME_REF(addr2);
  1462. SET_FRAME_REF(addr_ret);
  1463. HANDLE_OP_END();
  1464. }
  1465. #endif
  1466. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1467. HANDLE_OP(WASM_OP_TABLE_GET)
  1468. {
  1469. uint32 tbl_idx, elem_idx;
  1470. WASMTableInstance *tbl_inst;
  1471. tbl_idx = read_uint32(frame_ip);
  1472. bh_assert(tbl_idx < module->table_count);
  1473. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1474. elem_idx = POP_I32();
  1475. if (elem_idx >= tbl_inst->cur_size) {
  1476. wasm_set_exception(module, "out of bounds table access");
  1477. goto got_exception;
  1478. }
  1479. #if WASM_ENABLE_GC == 0
  1480. PUSH_I32(tbl_inst->elems[elem_idx]);
  1481. #else
  1482. PUSH_REF(tbl_inst->elems[elem_idx]);
  1483. #endif
  1484. HANDLE_OP_END();
  1485. }
  1486. HANDLE_OP(WASM_OP_TABLE_SET)
  1487. {
  1488. uint32 tbl_idx, elem_idx;
  1489. WASMTableInstance *tbl_inst;
  1490. table_elem_type_t elem_val;
  1491. tbl_idx = read_uint32(frame_ip);
  1492. bh_assert(tbl_idx < module->table_count);
  1493. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1494. #if WASM_ENABLE_GC == 0
  1495. elem_val = POP_I32();
  1496. #else
  1497. elem_val = POP_REF();
  1498. #endif
  1499. elem_idx = POP_I32();
  1500. if (elem_idx >= tbl_inst->cur_size) {
  1501. wasm_set_exception(module, "out of bounds table access");
  1502. goto got_exception;
  1503. }
  1504. tbl_inst->elems[elem_idx] = elem_val;
  1505. HANDLE_OP_END();
  1506. }
  1507. HANDLE_OP(WASM_OP_REF_NULL)
  1508. {
  1509. #if WASM_ENABLE_GC == 0
  1510. PUSH_I32(NULL_REF);
  1511. #else
  1512. PUSH_REF(NULL_REF);
  1513. #endif
  1514. HANDLE_OP_END();
  1515. }
  1516. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1517. {
  1518. #if WASM_ENABLE_GC == 0
  1519. uint32 ref_val;
  1520. ref_val = POP_I32();
  1521. #else
  1522. void *ref_val;
  1523. ref_val = POP_REF();
  1524. #endif
  1525. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1526. HANDLE_OP_END();
  1527. }
  1528. HANDLE_OP(WASM_OP_REF_FUNC)
  1529. {
  1530. uint32 func_idx = read_uint32(frame_ip);
  1531. #if WASM_ENABLE_GC == 0
  1532. PUSH_I32(func_idx);
  1533. #else
  1534. SYNC_ALL_TO_FRAME();
  1535. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  1536. NULL, 0))) {
  1537. goto got_exception;
  1538. }
  1539. PUSH_REF(gc_obj);
  1540. #endif
  1541. HANDLE_OP_END();
  1542. }
  1543. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  1544. #if WASM_ENABLE_GC != 0
  1545. HANDLE_OP(WASM_OP_CALL_REF)
  1546. {
  1547. #if WASM_ENABLE_THREAD_MGR != 0
  1548. CHECK_SUSPEND_FLAGS();
  1549. #endif
  1550. func_obj = POP_REF();
  1551. if (!func_obj) {
  1552. wasm_set_exception(module, "null function object");
  1553. goto got_exception;
  1554. }
  1555. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1556. cur_func = module->e->functions + fidx;
  1557. goto call_func_from_interp;
  1558. }
  1559. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  1560. {
  1561. #if WASM_ENABLE_THREAD_MGR != 0
  1562. CHECK_SUSPEND_FLAGS();
  1563. #endif
  1564. func_obj = POP_REF();
  1565. if (!func_obj) {
  1566. wasm_set_exception(module, "null function object");
  1567. goto got_exception;
  1568. }
  1569. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1570. cur_func = module->e->functions + fidx;
  1571. goto call_func_from_return_call;
  1572. }
  1573. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  1574. {
  1575. opnd_off = GET_OFFSET();
  1576. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1577. if (gc_obj == NULL_REF) {
  1578. wasm_set_exception(module, "null reference");
  1579. goto got_exception;
  1580. }
  1581. HANDLE_OP_END();
  1582. }
  1583. HANDLE_OP(WASM_OP_REF_EQ)
  1584. {
  1585. WASMObjectRef gc_obj1, gc_obj2;
  1586. gc_obj2 = POP_REF();
  1587. gc_obj1 = POP_REF();
  1588. val = wasm_obj_equal(gc_obj1, gc_obj2);
  1589. PUSH_I32(val);
  1590. HANDLE_OP_END();
  1591. }
  1592. HANDLE_OP(WASM_OP_BR_ON_NULL)
  1593. {
  1594. #if WASM_ENABLE_THREAD_MGR != 0
  1595. CHECK_SUSPEND_FLAGS();
  1596. #endif
  1597. opnd_off = GET_OFFSET();
  1598. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1599. if (gc_obj == NULL_REF) {
  1600. CLEAR_FRAME_REF(opnd_off);
  1601. goto recover_br_info;
  1602. }
  1603. else {
  1604. SKIP_BR_INFO();
  1605. }
  1606. HANDLE_OP_END();
  1607. }
  1608. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  1609. {
  1610. #if WASM_ENABLE_THREAD_MGR != 0
  1611. CHECK_SUSPEND_FLAGS();
  1612. #endif
  1613. opnd_off = GET_OFFSET();
  1614. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1615. if (gc_obj != NULL_REF) {
  1616. goto recover_br_info;
  1617. }
  1618. else {
  1619. CLEAR_FRAME_REF(opnd_off);
  1620. SKIP_BR_INFO();
  1621. }
  1622. HANDLE_OP_END();
  1623. }
  1624. HANDLE_OP(WASM_OP_GC_PREFIX)
  1625. {
  1626. GET_OPCODE();
  1627. switch (opcode) {
  1628. case WASM_OP_STRUCT_NEW_CANON:
  1629. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  1630. {
  1631. WASMModule *wasm_module = module->module;
  1632. WASMStructType *struct_type;
  1633. WASMRttType *rtt_type;
  1634. WASMValue field_value = { 0 };
  1635. type_idx = read_uint32(frame_ip);
  1636. struct_type =
  1637. (WASMStructType *)module->module->types[type_idx];
  1638. if (!(rtt_type = wasm_rtt_type_new(
  1639. (WASMType *)struct_type, type_idx,
  1640. wasm_module->rtt_types,
  1641. wasm_module->type_count,
  1642. &wasm_module->rtt_type_lock))) {
  1643. wasm_set_exception(module,
  1644. "create rtt type failed");
  1645. goto got_exception;
  1646. }
  1647. SYNC_ALL_TO_FRAME();
  1648. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  1649. if (!struct_obj) {
  1650. wasm_set_exception(module,
  1651. "create struct object failed");
  1652. goto got_exception;
  1653. }
  1654. if (opcode == WASM_OP_STRUCT_NEW_CANON) {
  1655. WASMStructFieldType *fields = struct_type->fields;
  1656. int32 field_count = (int32)struct_type->field_count;
  1657. int32 field_idx;
  1658. uint8 field_type;
  1659. for (field_idx = field_count - 1; field_idx >= 0;
  1660. field_idx--) {
  1661. field_type = fields[field_idx].field_type;
  1662. if (wasm_is_type_reftype(field_type)) {
  1663. field_value.gc_obj = POP_REF();
  1664. }
  1665. else if (field_type == VALUE_TYPE_I32
  1666. || field_type == VALUE_TYPE_F32
  1667. || field_type == PACKED_TYPE_I8
  1668. || field_type == PACKED_TYPE_I16) {
  1669. field_value.i32 = POP_I32();
  1670. }
  1671. else {
  1672. field_value.i64 = POP_I64();
  1673. }
  1674. wasm_struct_obj_set_field(struct_obj, field_idx,
  1675. &field_value);
  1676. }
  1677. }
  1678. PUSH_REF(struct_obj);
  1679. HANDLE_OP_END();
  1680. }
  1681. case WASM_OP_STRUCT_GET:
  1682. case WASM_OP_STRUCT_GET_S:
  1683. case WASM_OP_STRUCT_GET_U:
  1684. {
  1685. WASMStructType *struct_type;
  1686. WASMValue field_value = { 0 };
  1687. uint32 field_idx;
  1688. uint8 field_type;
  1689. type_idx = read_uint32(frame_ip);
  1690. field_idx = read_uint32(frame_ip);
  1691. struct_type =
  1692. (WASMStructType *)module->module->types[type_idx];
  1693. struct_obj = POP_REF();
  1694. if (!struct_obj) {
  1695. wasm_set_exception(module, "null structure object");
  1696. goto got_exception;
  1697. }
  1698. if (field_idx >= struct_type->field_count) {
  1699. wasm_set_exception(
  1700. module, "struct field index out of bounds");
  1701. goto got_exception;
  1702. }
  1703. wasm_struct_obj_get_field(
  1704. struct_obj, field_idx,
  1705. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  1706. &field_value);
  1707. field_type = struct_type->fields[field_idx].field_type;
  1708. if (wasm_is_type_reftype(field_type)) {
  1709. PUSH_REF(field_value.gc_obj);
  1710. }
  1711. else if (field_type == VALUE_TYPE_I32
  1712. || field_type == VALUE_TYPE_F32
  1713. || field_type == PACKED_TYPE_I8
  1714. || field_type == PACKED_TYPE_I16) {
  1715. PUSH_I32(field_value.i32);
  1716. }
  1717. else {
  1718. PUSH_I64(field_value.i64);
  1719. }
  1720. HANDLE_OP_END();
  1721. }
  1722. case WASM_OP_STRUCT_SET:
  1723. {
  1724. WASMStructType *struct_type;
  1725. WASMValue field_value = { 0 };
  1726. uint32 field_idx;
  1727. uint8 field_type;
  1728. type_idx = read_uint32(frame_ip);
  1729. field_idx = read_uint32(frame_ip);
  1730. struct_type =
  1731. (WASMStructType *)module->module->types[type_idx];
  1732. field_type = struct_type->fields[field_idx].field_type;
  1733. if (wasm_is_type_reftype(field_type)) {
  1734. field_value.gc_obj = POP_REF();
  1735. }
  1736. else if (field_type == VALUE_TYPE_I32
  1737. || field_type == VALUE_TYPE_F32
  1738. || field_type == PACKED_TYPE_I8
  1739. || field_type == PACKED_TYPE_I16) {
  1740. field_value.i32 = POP_I32();
  1741. }
  1742. else {
  1743. field_value.i64 = POP_I64();
  1744. }
  1745. struct_obj = POP_REF();
  1746. if (!struct_obj) {
  1747. wasm_set_exception(module, "null structure object");
  1748. goto got_exception;
  1749. }
  1750. if (field_idx >= struct_type->field_count) {
  1751. wasm_set_exception(
  1752. module, "struct field index out of bounds");
  1753. goto got_exception;
  1754. }
  1755. wasm_struct_obj_set_field(struct_obj, field_idx,
  1756. &field_value);
  1757. HANDLE_OP_END();
  1758. }
  1759. case WASM_OP_ARRAY_NEW_CANON:
  1760. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  1761. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  1762. {
  1763. WASMModule *wasm_module = module->module;
  1764. WASMArrayType *array_type;
  1765. WASMRttType *rtt_type;
  1766. WASMValue array_elem = { 0 };
  1767. uint32 array_len, i;
  1768. type_idx = read_uint32(frame_ip);
  1769. array_type =
  1770. (WASMArrayType *)wasm_module->types[type_idx];
  1771. if (!(rtt_type = wasm_rtt_type_new(
  1772. (WASMType *)array_type, type_idx,
  1773. wasm_module->rtt_types,
  1774. wasm_module->type_count,
  1775. &wasm_module->rtt_type_lock))) {
  1776. wasm_set_exception(module,
  1777. "create rtt type failed");
  1778. goto got_exception;
  1779. }
  1780. if (opcode != WASM_OP_ARRAY_NEW_CANON_FIXED)
  1781. array_len = POP_I32();
  1782. else
  1783. array_len = read_uint32(frame_ip);
  1784. if (opcode == WASM_OP_ARRAY_NEW_CANON) {
  1785. if (wasm_is_type_reftype(array_type->elem_type)) {
  1786. array_elem.gc_obj = POP_REF();
  1787. }
  1788. else if (array_type->elem_type == VALUE_TYPE_I32
  1789. || array_type->elem_type == VALUE_TYPE_F32
  1790. || array_type->elem_type == PACKED_TYPE_I8
  1791. || array_type->elem_type
  1792. == PACKED_TYPE_I16) {
  1793. array_elem.i32 = POP_I32();
  1794. }
  1795. else {
  1796. array_elem.i64 = POP_I64();
  1797. }
  1798. }
  1799. SYNC_ALL_TO_FRAME();
  1800. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  1801. array_len, &array_elem);
  1802. if (!array_obj) {
  1803. wasm_set_exception(module,
  1804. "create array object failed");
  1805. goto got_exception;
  1806. }
  1807. if (opcode == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  1808. for (i = 0; i < array_len; i++) {
  1809. if (wasm_is_type_reftype(
  1810. array_type->elem_type)) {
  1811. array_elem.gc_obj = POP_REF();
  1812. }
  1813. else if (array_type->elem_type == VALUE_TYPE_I32
  1814. || array_type->elem_type
  1815. == VALUE_TYPE_F32
  1816. || array_type->elem_type
  1817. == PACKED_TYPE_I8
  1818. || array_type->elem_type
  1819. == PACKED_TYPE_I16) {
  1820. array_elem.i32 = POP_I32();
  1821. }
  1822. else {
  1823. array_elem.i64 = POP_I64();
  1824. }
  1825. wasm_array_obj_set_elem(
  1826. array_obj, array_len - 1 - i, &array_elem);
  1827. }
  1828. }
  1829. PUSH_REF(array_obj);
  1830. HANDLE_OP_END();
  1831. }
  1832. case WASM_OP_ARRAY_NEW_CANON_DATA:
  1833. {
  1834. WASMModule *wasm_module = module->module;
  1835. WASMArrayType *array_type;
  1836. WASMRttType *rtt_type;
  1837. WASMValue array_elem = { 0 };
  1838. WASMDataSeg *data_seg;
  1839. uint8 *array_elem_base;
  1840. uint32 array_len, data_seg_idx, data_seg_offset;
  1841. uint32 elem_size = 0;
  1842. uint64 total_size;
  1843. type_idx = read_uint32(frame_ip);
  1844. data_seg_idx = read_uint32(frame_ip);
  1845. data_seg = wasm_module->data_segments[data_seg_idx];
  1846. array_type =
  1847. (WASMArrayType *)wasm_module->types[type_idx];
  1848. if (!(rtt_type = wasm_rtt_type_new(
  1849. (WASMType *)array_type, type_idx,
  1850. wasm_module->rtt_types,
  1851. wasm_module->type_count,
  1852. &wasm_module->rtt_type_lock))) {
  1853. wasm_set_exception(module,
  1854. "create rtt type failed");
  1855. goto got_exception;
  1856. }
  1857. array_len = POP_I32();
  1858. data_seg_offset = POP_I32();
  1859. switch (array_type->elem_type) {
  1860. case PACKED_TYPE_I8:
  1861. elem_size = 1;
  1862. break;
  1863. case PACKED_TYPE_I16:
  1864. elem_size = 2;
  1865. break;
  1866. case VALUE_TYPE_I32:
  1867. case VALUE_TYPE_F32:
  1868. elem_size = 4;
  1869. break;
  1870. case VALUE_TYPE_I64:
  1871. case VALUE_TYPE_F64:
  1872. elem_size = 8;
  1873. break;
  1874. default:
  1875. bh_assert(0);
  1876. }
  1877. total_size = (uint64)elem_size * array_len;
  1878. if (data_seg_offset >= data_seg->data_length
  1879. || total_size
  1880. > data_seg->data_length - data_seg_offset) {
  1881. wasm_set_exception(module,
  1882. "data segment out of bounds");
  1883. goto got_exception;
  1884. }
  1885. SYNC_ALL_TO_FRAME();
  1886. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  1887. array_len, &array_elem);
  1888. if (!array_obj) {
  1889. wasm_set_exception(module,
  1890. "create array object failed");
  1891. goto got_exception;
  1892. }
  1893. array_elem_base =
  1894. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  1895. bh_memcpy_s(array_elem_base, (uint32)total_size,
  1896. data_seg->data + data_seg_offset,
  1897. (uint32)total_size);
  1898. PUSH_REF(array_obj);
  1899. HANDLE_OP_END();
  1900. }
  1901. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  1902. {
  1903. /* TODO */
  1904. wasm_set_exception(module, "unsupported opcode");
  1905. goto got_exception;
  1906. }
  1907. case WASM_OP_ARRAY_GET:
  1908. case WASM_OP_ARRAY_GET_S:
  1909. case WASM_OP_ARRAY_GET_U:
  1910. {
  1911. WASMArrayType *array_type;
  1912. WASMValue array_elem = { 0 };
  1913. uint32 elem_idx, elem_size_log;
  1914. type_idx = read_uint32(frame_ip);
  1915. array_type =
  1916. (WASMArrayType *)module->module->types[type_idx];
  1917. elem_idx = POP_I32();
  1918. array_obj = POP_REF();
  1919. if (!array_obj) {
  1920. wasm_set_exception(module, "null array object");
  1921. goto got_exception;
  1922. }
  1923. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  1924. wasm_set_exception(module,
  1925. "array index out of bounds");
  1926. goto got_exception;
  1927. }
  1928. wasm_array_obj_get_elem(
  1929. array_obj, elem_idx,
  1930. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  1931. &array_elem);
  1932. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  1933. if (wasm_is_type_reftype(array_type->elem_type)) {
  1934. PUSH_REF(array_elem.gc_obj);
  1935. }
  1936. else if (elem_size_log < 3) {
  1937. PUSH_I32(array_elem.i32);
  1938. }
  1939. else {
  1940. PUSH_I64(array_elem.i64);
  1941. }
  1942. HANDLE_OP_END();
  1943. }
  1944. case WASM_OP_ARRAY_SET:
  1945. {
  1946. WASMArrayType *array_type;
  1947. WASMValue array_elem = { 0 };
  1948. uint32 elem_idx;
  1949. type_idx = read_uint32(frame_ip);
  1950. array_type =
  1951. (WASMArrayType *)module->module->types[type_idx];
  1952. if (wasm_is_type_reftype(array_type->elem_type)) {
  1953. array_elem.gc_obj = POP_REF();
  1954. }
  1955. else if (array_type->elem_type == VALUE_TYPE_I32
  1956. || array_type->elem_type == VALUE_TYPE_F32
  1957. || array_type->elem_type == PACKED_TYPE_I8
  1958. || array_type->elem_type == PACKED_TYPE_I16) {
  1959. array_elem.i32 = POP_I32();
  1960. }
  1961. else {
  1962. array_elem.i64 = POP_I64();
  1963. }
  1964. elem_idx = POP_I32();
  1965. array_obj = POP_REF();
  1966. if (!array_obj) {
  1967. wasm_set_exception(module, "null array object");
  1968. goto got_exception;
  1969. }
  1970. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  1971. wasm_set_exception(module,
  1972. "array index out of bounds");
  1973. goto got_exception;
  1974. }
  1975. wasm_array_obj_set_elem(array_obj, elem_idx,
  1976. &array_elem);
  1977. HANDLE_OP_END();
  1978. }
  1979. #if WASM_ENABLE_GC_BINARYEN != 0
  1980. case WASM_OP_ARRAY_COPY:
  1981. {
  1982. uint32 dst_offset, src_offset, len, src_type_index;
  1983. WASMArrayObjectRef src_obj, dst_obj;
  1984. type_idx = read_uint32(frame_ip);
  1985. src_type_index = read_uint32(frame_ip);
  1986. len = POP_I32();
  1987. src_offset = POP_I32();
  1988. src_obj = POP_REF();
  1989. dst_offset = POP_I32();
  1990. dst_obj = POP_REF();
  1991. if (!src_obj || !dst_obj) {
  1992. wasm_set_exception(module, "null array object");
  1993. goto got_exception;
  1994. }
  1995. if (len > 0) {
  1996. if ((dst_offset > UINT32_MAX - len)
  1997. || (dst_offset + len
  1998. > wasm_array_obj_length(dst_obj))
  1999. || (src_offset > UINT32_MAX - len)
  2000. || (src_offset + len
  2001. > wasm_array_obj_length(src_obj))) {
  2002. wasm_set_exception(module,
  2003. "array index out of bounds");
  2004. goto got_exception;
  2005. }
  2006. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2007. src_offset, len);
  2008. }
  2009. (void)src_type_index;
  2010. HANDLE_OP_END();
  2011. }
  2012. #endif
  2013. case WASM_OP_ARRAY_LEN:
  2014. {
  2015. uint32 array_len;
  2016. array_obj = POP_REF();
  2017. if (!array_obj) {
  2018. wasm_set_exception(module, "null array object");
  2019. goto got_exception;
  2020. }
  2021. array_len = wasm_array_obj_length(array_obj);
  2022. PUSH_I32(array_len);
  2023. HANDLE_OP_END();
  2024. }
  2025. case WASM_OP_I31_NEW:
  2026. {
  2027. uint32 i31_val;
  2028. i31_val = POP_I32();
  2029. i31_obj = wasm_i31_obj_new(i31_val);
  2030. PUSH_REF(i31_obj);
  2031. HANDLE_OP_END();
  2032. }
  2033. case WASM_OP_I31_GET_S:
  2034. case WASM_OP_I31_GET_U:
  2035. {
  2036. uint32 i31_val;
  2037. i31_obj = (WASMI31ObjectRef)POP_REF();
  2038. if (!i31_obj) {
  2039. wasm_set_exception(module, "null i31 reference");
  2040. goto got_exception;
  2041. }
  2042. i31_val = wasm_i31_obj_get_value(
  2043. i31_obj,
  2044. opcode == WASM_OP_I31_GET_S ? true : false);
  2045. PUSH_I32(i31_val);
  2046. HANDLE_OP_END();
  2047. }
  2048. case WASM_OP_REF_TEST:
  2049. case WASM_OP_REF_CAST:
  2050. case WASM_OP_REF_TEST_NULLABLE:
  2051. case WASM_OP_REF_CAST_NULLABLE:
  2052. {
  2053. int32 heap_type;
  2054. heap_type = (int32)read_uint32(frame_ip);
  2055. gc_obj = POP_REF();
  2056. if (!gc_obj) {
  2057. if (opcode == WASM_OP_REF_TEST
  2058. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2059. if (opcode == WASM_OP_REF_TEST)
  2060. PUSH_I32(0);
  2061. else
  2062. PUSH_I32(1);
  2063. }
  2064. else if (opcode == WASM_OP_REF_CAST) {
  2065. wasm_set_exception(module, "cast failure");
  2066. goto got_exception;
  2067. }
  2068. else {
  2069. PUSH_REF(gc_obj);
  2070. }
  2071. }
  2072. else {
  2073. bool castable = false;
  2074. if (heap_type >= 0) {
  2075. WASMModule *wasm_module = module->module;
  2076. castable = wasm_obj_is_instance_of(
  2077. gc_obj, (uint32)heap_type,
  2078. wasm_module->types,
  2079. wasm_module->type_count);
  2080. }
  2081. else {
  2082. castable =
  2083. wasm_obj_is_type_of(gc_obj, heap_type);
  2084. }
  2085. if (opcode == WASM_OP_REF_TEST
  2086. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2087. if (castable)
  2088. PUSH_I32(1);
  2089. else
  2090. PUSH_I32(0);
  2091. }
  2092. else if (!castable) {
  2093. wasm_set_exception(module, "cast failure");
  2094. goto got_exception;
  2095. }
  2096. else {
  2097. PUSH_REF(gc_obj);
  2098. }
  2099. }
  2100. HANDLE_OP_END();
  2101. }
  2102. case WASM_OP_BR_ON_CAST:
  2103. case WASM_OP_BR_ON_CAST_NULLABLE:
  2104. case WASM_OP_BR_ON_CAST_FAIL:
  2105. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  2106. {
  2107. int32 heap_type;
  2108. uint16 opnd_off_br;
  2109. #if WASM_ENABLE_THREAD_MGR != 0
  2110. CHECK_SUSPEND_FLAGS();
  2111. #endif
  2112. heap_type = (int32)read_uint32(frame_ip);
  2113. opnd_off = GET_OFFSET();
  2114. opnd_off_br = GET_OFFSET();
  2115. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  2116. PUT_REF_TO_ADDR(frame_lp + opnd_off_br, gc_obj);
  2117. if (!gc_obj) {
  2118. if (opcode == WASM_OP_BR_ON_CAST_NULLABLE
  2119. || opcode == WASM_OP_BR_ON_CAST_FAIL) {
  2120. CLEAR_FRAME_REF(opnd_off);
  2121. CLEAR_FRAME_REF(opnd_off_br);
  2122. goto recover_br_info;
  2123. }
  2124. }
  2125. else {
  2126. bool castable = false;
  2127. if (heap_type >= 0) {
  2128. WASMModule *wasm_module = module->module;
  2129. castable = wasm_obj_is_instance_of(
  2130. gc_obj, (uint32)heap_type,
  2131. wasm_module->types,
  2132. wasm_module->type_count);
  2133. }
  2134. else {
  2135. castable =
  2136. wasm_obj_is_type_of(gc_obj, heap_type);
  2137. }
  2138. if ((castable
  2139. && (opcode == WASM_OP_BR_ON_CAST
  2140. || opcode == WASM_OP_BR_ON_CAST_NULLABLE))
  2141. || (!castable
  2142. && (opcode == WASM_OP_BR_ON_CAST_FAIL
  2143. || opcode
  2144. == WASM_OP_BR_ON_CAST_FAIL_NULLABLE))) {
  2145. CLEAR_FRAME_REF(opnd_off);
  2146. CLEAR_FRAME_REF(opnd_off_br);
  2147. goto recover_br_info;
  2148. }
  2149. }
  2150. SKIP_BR_INFO();
  2151. HANDLE_OP_END();
  2152. }
  2153. case WASM_OP_EXTERN_INTERNALIZE:
  2154. {
  2155. externref_obj = POP_REF();
  2156. if (externref_obj == NULL_REF)
  2157. PUSH_REF(NULL_REF);
  2158. else {
  2159. gc_obj = wasm_externref_obj_to_internal_obj(
  2160. externref_obj);
  2161. PUSH_REF(gc_obj);
  2162. }
  2163. HANDLE_OP_END();
  2164. }
  2165. case WASM_OP_EXTERN_EXTERNALIZE:
  2166. {
  2167. gc_obj = POP_REF();
  2168. if (gc_obj == NULL_REF)
  2169. PUSH_REF(NULL_REF);
  2170. else {
  2171. if (!(externref_obj =
  2172. wasm_internal_obj_to_externref_obj(
  2173. exec_env, gc_obj))) {
  2174. wasm_set_exception(
  2175. module, "create externref object failed");
  2176. goto got_exception;
  2177. }
  2178. PUSH_REF(externref_obj);
  2179. }
  2180. HANDLE_OP_END();
  2181. }
  2182. default:
  2183. {
  2184. wasm_set_exception(module, "unsupported opcode");
  2185. goto got_exception;
  2186. }
  2187. }
  2188. }
  2189. #endif /* end of WASM_ENABLE_GC != 0 */
  2190. /* variable instructions */
  2191. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  2192. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  2193. {
  2194. /* clang-format off */
  2195. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  2196. local_offset = *frame_ip++;
  2197. #else
  2198. local_offset = *frame_ip;
  2199. frame_ip += 2;
  2200. #endif
  2201. /* clang-format on */
  2202. *(uint32 *)(frame_lp + local_offset) =
  2203. GET_OPERAND(uint32, I32, 0);
  2204. frame_ip += 2;
  2205. HANDLE_OP_END();
  2206. }
  2207. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  2208. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  2209. {
  2210. /* clang-format off */
  2211. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  2212. local_offset = *frame_ip++;
  2213. #else
  2214. local_offset = *frame_ip;
  2215. frame_ip += 2;
  2216. #endif
  2217. /* clang-format on */
  2218. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  2219. GET_OPERAND(uint64, I64, 0));
  2220. frame_ip += 2;
  2221. HANDLE_OP_END();
  2222. }
  2223. HANDLE_OP(WASM_OP_GET_GLOBAL)
  2224. {
  2225. global_idx = read_uint32(frame_ip);
  2226. bh_assert(global_idx < module->e->global_count);
  2227. global = globals + global_idx;
  2228. global_addr = get_global_addr(global_data, global);
  2229. addr_ret = GET_OFFSET();
  2230. /* clang-format off */
  2231. #if WASM_ENABLE_GC == 0
  2232. frame_lp[addr_ret] = *(uint32 *)global_addr;
  2233. #else
  2234. if (!wasm_is_type_reftype(global->type))
  2235. frame_lp[addr_ret] = *(uint32 *)global_addr;
  2236. else
  2237. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  2238. GET_REF_FROM_ADDR((uint32 *)global_addr));
  2239. #endif
  2240. /* clang-format on */
  2241. HANDLE_OP_END();
  2242. }
  2243. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  2244. {
  2245. global_idx = read_uint32(frame_ip);
  2246. bh_assert(global_idx < module->e->global_count);
  2247. global = globals + global_idx;
  2248. global_addr = get_global_addr(global_data, global);
  2249. addr_ret = GET_OFFSET();
  2250. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  2251. GET_I64_FROM_ADDR((uint32 *)global_addr));
  2252. HANDLE_OP_END();
  2253. }
  2254. HANDLE_OP(WASM_OP_SET_GLOBAL)
  2255. {
  2256. global_idx = read_uint32(frame_ip);
  2257. bh_assert(global_idx < module->e->global_count);
  2258. global = globals + global_idx;
  2259. global_addr = get_global_addr(global_data, global);
  2260. addr1 = GET_OFFSET();
  2261. /* clang-format off */
  2262. #if WASM_ENABLE_GC == 0
  2263. *(int32 *)global_addr = frame_lp[addr1];
  2264. #else
  2265. if (!wasm_is_type_reftype(global->type))
  2266. *(int32 *)global_addr = frame_lp[addr1];
  2267. else
  2268. PUT_REF_TO_ADDR((uint32 *)global_addr,
  2269. GET_REF_FROM_ADDR(frame_lp + addr1));
  2270. #endif
  2271. /* clang-format on */
  2272. HANDLE_OP_END();
  2273. }
  2274. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  2275. {
  2276. uint32 aux_stack_top;
  2277. global_idx = read_uint32(frame_ip);
  2278. bh_assert(global_idx < module->e->global_count);
  2279. global = globals + global_idx;
  2280. global_addr = get_global_addr(global_data, global);
  2281. aux_stack_top = frame_lp[GET_OFFSET()];
  2282. if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
  2283. wasm_set_exception(module, "wasm auxiliary stack overflow");
  2284. goto got_exception;
  2285. }
  2286. if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
  2287. wasm_set_exception(module,
  2288. "wasm auxiliary stack underflow");
  2289. goto got_exception;
  2290. }
  2291. *(int32 *)global_addr = aux_stack_top;
  2292. #if WASM_ENABLE_MEMORY_PROFILING != 0
  2293. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  2294. uint32 aux_stack_used = module->module->aux_stack_bottom
  2295. - *(uint32 *)global_addr;
  2296. if (aux_stack_used > module->e->max_aux_stack_used)
  2297. module->e->max_aux_stack_used = aux_stack_used;
  2298. }
  2299. #endif
  2300. HANDLE_OP_END();
  2301. }
  2302. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  2303. {
  2304. global_idx = read_uint32(frame_ip);
  2305. bh_assert(global_idx < module->e->global_count);
  2306. global = globals + global_idx;
  2307. global_addr = get_global_addr(global_data, global);
  2308. addr1 = GET_OFFSET();
  2309. PUT_I64_TO_ADDR((uint32 *)global_addr,
  2310. GET_I64_FROM_ADDR(frame_lp + addr1));
  2311. HANDLE_OP_END();
  2312. }
  2313. /* memory load instructions */
  2314. HANDLE_OP(WASM_OP_I32_LOAD)
  2315. {
  2316. uint32 offset, addr;
  2317. offset = read_uint32(frame_ip);
  2318. addr = GET_OPERAND(uint32, I32, 0);
  2319. frame_ip += 2;
  2320. addr_ret = GET_OFFSET();
  2321. CHECK_MEMORY_OVERFLOW(4);
  2322. frame_lp[addr_ret] = LOAD_I32(maddr);
  2323. HANDLE_OP_END();
  2324. }
  2325. HANDLE_OP(WASM_OP_I64_LOAD)
  2326. {
  2327. uint32 offset, addr;
  2328. offset = read_uint32(frame_ip);
  2329. addr = GET_OPERAND(uint32, I32, 0);
  2330. frame_ip += 2;
  2331. addr_ret = GET_OFFSET();
  2332. CHECK_MEMORY_OVERFLOW(8);
  2333. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  2334. HANDLE_OP_END();
  2335. }
  2336. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  2337. {
  2338. uint32 offset, addr;
  2339. offset = read_uint32(frame_ip);
  2340. addr = GET_OPERAND(uint32, I32, 0);
  2341. frame_ip += 2;
  2342. addr_ret = GET_OFFSET();
  2343. CHECK_MEMORY_OVERFLOW(1);
  2344. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  2345. HANDLE_OP_END();
  2346. }
  2347. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  2348. {
  2349. uint32 offset, addr;
  2350. offset = read_uint32(frame_ip);
  2351. addr = GET_OPERAND(uint32, I32, 0);
  2352. frame_ip += 2;
  2353. addr_ret = GET_OFFSET();
  2354. CHECK_MEMORY_OVERFLOW(1);
  2355. frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr);
  2356. HANDLE_OP_END();
  2357. }
  2358. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  2359. {
  2360. uint32 offset, addr;
  2361. offset = read_uint32(frame_ip);
  2362. addr = GET_OPERAND(uint32, I32, 0);
  2363. frame_ip += 2;
  2364. addr_ret = GET_OFFSET();
  2365. CHECK_MEMORY_OVERFLOW(2);
  2366. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  2367. HANDLE_OP_END();
  2368. }
  2369. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  2370. {
  2371. uint32 offset, addr;
  2372. offset = read_uint32(frame_ip);
  2373. addr = GET_OPERAND(uint32, I32, 0);
  2374. frame_ip += 2;
  2375. addr_ret = GET_OFFSET();
  2376. CHECK_MEMORY_OVERFLOW(2);
  2377. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  2378. HANDLE_OP_END();
  2379. }
  2380. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  2381. {
  2382. uint32 offset, addr;
  2383. offset = read_uint32(frame_ip);
  2384. addr = GET_OPERAND(uint32, I32, 0);
  2385. frame_ip += 2;
  2386. addr_ret = GET_OFFSET();
  2387. CHECK_MEMORY_OVERFLOW(1);
  2388. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  2389. sign_ext_8_64(*(int8 *)maddr));
  2390. HANDLE_OP_END();
  2391. }
  2392. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  2393. {
  2394. uint32 offset, addr;
  2395. offset = read_uint32(frame_ip);
  2396. addr = GET_OPERAND(uint32, I32, 0);
  2397. frame_ip += 2;
  2398. addr_ret = GET_OFFSET();
  2399. CHECK_MEMORY_OVERFLOW(1);
  2400. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  2401. HANDLE_OP_END();
  2402. }
  2403. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  2404. {
  2405. uint32 offset, addr;
  2406. offset = read_uint32(frame_ip);
  2407. addr = GET_OPERAND(uint32, I32, 0);
  2408. frame_ip += 2;
  2409. addr_ret = GET_OFFSET();
  2410. CHECK_MEMORY_OVERFLOW(2);
  2411. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  2412. sign_ext_16_64(LOAD_I16(maddr)));
  2413. HANDLE_OP_END();
  2414. }
  2415. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  2416. {
  2417. uint32 offset, addr;
  2418. offset = read_uint32(frame_ip);
  2419. addr = GET_OPERAND(uint32, I32, 0);
  2420. frame_ip += 2;
  2421. addr_ret = GET_OFFSET();
  2422. CHECK_MEMORY_OVERFLOW(2);
  2423. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  2424. HANDLE_OP_END();
  2425. }
  2426. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  2427. {
  2428. uint32 offset, addr;
  2429. offset = read_uint32(frame_ip);
  2430. addr = GET_OPERAND(uint32, I32, 0);
  2431. frame_ip += 2;
  2432. addr_ret = GET_OFFSET();
  2433. CHECK_MEMORY_OVERFLOW(4);
  2434. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  2435. sign_ext_32_64(LOAD_I32(maddr)));
  2436. HANDLE_OP_END();
  2437. }
  2438. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  2439. {
  2440. uint32 offset, addr;
  2441. offset = read_uint32(frame_ip);
  2442. addr = GET_OPERAND(uint32, I32, 0);
  2443. frame_ip += 2;
  2444. addr_ret = GET_OFFSET();
  2445. CHECK_MEMORY_OVERFLOW(4);
  2446. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  2447. HANDLE_OP_END();
  2448. }
  2449. HANDLE_OP(WASM_OP_I32_STORE)
  2450. {
  2451. uint32 offset, addr;
  2452. uint32 sval;
  2453. offset = read_uint32(frame_ip);
  2454. sval = GET_OPERAND(uint32, I32, 0);
  2455. addr = GET_OPERAND(uint32, I32, 2);
  2456. frame_ip += 4;
  2457. CHECK_MEMORY_OVERFLOW(4);
  2458. STORE_U32(maddr, sval);
  2459. HANDLE_OP_END();
  2460. }
  2461. HANDLE_OP(WASM_OP_I32_STORE8)
  2462. {
  2463. uint32 offset, addr;
  2464. uint32 sval;
  2465. offset = read_uint32(frame_ip);
  2466. sval = GET_OPERAND(uint32, I32, 0);
  2467. addr = GET_OPERAND(uint32, I32, 2);
  2468. frame_ip += 4;
  2469. CHECK_MEMORY_OVERFLOW(1);
  2470. *(uint8 *)maddr = (uint8)sval;
  2471. HANDLE_OP_END();
  2472. }
  2473. HANDLE_OP(WASM_OP_I32_STORE16)
  2474. {
  2475. uint32 offset, addr;
  2476. uint32 sval;
  2477. offset = read_uint32(frame_ip);
  2478. sval = GET_OPERAND(uint32, I32, 0);
  2479. addr = GET_OPERAND(uint32, I32, 2);
  2480. frame_ip += 4;
  2481. CHECK_MEMORY_OVERFLOW(2);
  2482. STORE_U16(maddr, (uint16)sval);
  2483. HANDLE_OP_END();
  2484. }
  2485. HANDLE_OP(WASM_OP_I64_STORE)
  2486. {
  2487. uint32 offset, addr;
  2488. uint64 sval;
  2489. offset = read_uint32(frame_ip);
  2490. sval = GET_OPERAND(uint64, I64, 0);
  2491. addr = GET_OPERAND(uint32, I32, 2);
  2492. frame_ip += 4;
  2493. CHECK_MEMORY_OVERFLOW(8);
  2494. STORE_I64(maddr, sval);
  2495. HANDLE_OP_END();
  2496. }
  2497. HANDLE_OP(WASM_OP_I64_STORE8)
  2498. {
  2499. uint32 offset, addr;
  2500. uint64 sval;
  2501. offset = read_uint32(frame_ip);
  2502. sval = GET_OPERAND(uint64, I64, 0);
  2503. addr = GET_OPERAND(uint32, I32, 2);
  2504. frame_ip += 4;
  2505. CHECK_MEMORY_OVERFLOW(1);
  2506. *(uint8 *)maddr = (uint8)sval;
  2507. HANDLE_OP_END();
  2508. }
  2509. HANDLE_OP(WASM_OP_I64_STORE16)
  2510. {
  2511. uint32 offset, addr;
  2512. uint64 sval;
  2513. offset = read_uint32(frame_ip);
  2514. sval = GET_OPERAND(uint64, I64, 0);
  2515. addr = GET_OPERAND(uint32, I32, 2);
  2516. frame_ip += 4;
  2517. CHECK_MEMORY_OVERFLOW(2);
  2518. STORE_U16(maddr, (uint16)sval);
  2519. HANDLE_OP_END();
  2520. }
  2521. HANDLE_OP(WASM_OP_I64_STORE32)
  2522. {
  2523. uint32 offset, addr;
  2524. uint64 sval;
  2525. offset = read_uint32(frame_ip);
  2526. sval = GET_OPERAND(uint64, I64, 0);
  2527. addr = GET_OPERAND(uint32, I32, 2);
  2528. frame_ip += 4;
  2529. CHECK_MEMORY_OVERFLOW(4);
  2530. STORE_U32(maddr, (uint32)sval);
  2531. HANDLE_OP_END();
  2532. }
  2533. /* memory size and memory grow instructions */
  2534. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  2535. {
  2536. uint32 reserved;
  2537. addr_ret = GET_OFFSET();
  2538. frame_lp[addr_ret] = memory->cur_page_count;
  2539. (void)reserved;
  2540. HANDLE_OP_END();
  2541. }
  2542. HANDLE_OP(WASM_OP_MEMORY_GROW)
  2543. {
  2544. uint32 reserved, delta,
  2545. prev_page_count = memory->cur_page_count;
  2546. addr1 = GET_OFFSET();
  2547. addr_ret = GET_OFFSET();
  2548. delta = (uint32)frame_lp[addr1];
  2549. if (!wasm_enlarge_memory(module, delta)) {
  2550. /* failed to memory.grow, return -1 */
  2551. frame_lp[addr_ret] = -1;
  2552. }
  2553. else {
  2554. /* success, return previous page count */
  2555. frame_lp[addr_ret] = prev_page_count;
  2556. /* update memory size, no need to update memory ptr as
  2557. it isn't changed in wasm_enlarge_memory */
  2558. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  2559. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  2560. || WASM_ENABLE_BULK_MEMORY != 0
  2561. linear_mem_size = memory->memory_data_size;
  2562. #endif
  2563. }
  2564. (void)reserved;
  2565. HANDLE_OP_END();
  2566. }
  2567. /* constant instructions */
  2568. HANDLE_OP(WASM_OP_F64_CONST)
  2569. HANDLE_OP(WASM_OP_I64_CONST)
  2570. {
  2571. uint8 *orig_ip = frame_ip;
  2572. frame_ip += sizeof(uint64);
  2573. addr_ret = GET_OFFSET();
  2574. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  2575. sizeof(uint64));
  2576. HANDLE_OP_END();
  2577. }
  2578. HANDLE_OP(WASM_OP_F32_CONST)
  2579. HANDLE_OP(WASM_OP_I32_CONST)
  2580. {
  2581. uint8 *orig_ip = frame_ip;
  2582. frame_ip += sizeof(uint32);
  2583. addr_ret = GET_OFFSET();
  2584. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  2585. sizeof(uint32));
  2586. HANDLE_OP_END();
  2587. }
  2588. /* comparison instructions of i32 */
  2589. HANDLE_OP(WASM_OP_I32_EQZ)
  2590. {
  2591. DEF_OP_EQZ(int32, I32);
  2592. HANDLE_OP_END();
  2593. }
  2594. HANDLE_OP(WASM_OP_I32_EQ)
  2595. {
  2596. DEF_OP_CMP(uint32, I32, ==);
  2597. HANDLE_OP_END();
  2598. }
  2599. HANDLE_OP(WASM_OP_I32_NE)
  2600. {
  2601. DEF_OP_CMP(uint32, I32, !=);
  2602. HANDLE_OP_END();
  2603. }
  2604. HANDLE_OP(WASM_OP_I32_LT_S)
  2605. {
  2606. DEF_OP_CMP(int32, I32, <);
  2607. HANDLE_OP_END();
  2608. }
  2609. HANDLE_OP(WASM_OP_I32_LT_U)
  2610. {
  2611. DEF_OP_CMP(uint32, I32, <);
  2612. HANDLE_OP_END();
  2613. }
  2614. HANDLE_OP(WASM_OP_I32_GT_S)
  2615. {
  2616. DEF_OP_CMP(int32, I32, >);
  2617. HANDLE_OP_END();
  2618. }
  2619. HANDLE_OP(WASM_OP_I32_GT_U)
  2620. {
  2621. DEF_OP_CMP(uint32, I32, >);
  2622. HANDLE_OP_END();
  2623. }
  2624. HANDLE_OP(WASM_OP_I32_LE_S)
  2625. {
  2626. DEF_OP_CMP(int32, I32, <=);
  2627. HANDLE_OP_END();
  2628. }
  2629. HANDLE_OP(WASM_OP_I32_LE_U)
  2630. {
  2631. DEF_OP_CMP(uint32, I32, <=);
  2632. HANDLE_OP_END();
  2633. }
  2634. HANDLE_OP(WASM_OP_I32_GE_S)
  2635. {
  2636. DEF_OP_CMP(int32, I32, >=);
  2637. HANDLE_OP_END();
  2638. }
  2639. HANDLE_OP(WASM_OP_I32_GE_U)
  2640. {
  2641. DEF_OP_CMP(uint32, I32, >=);
  2642. HANDLE_OP_END();
  2643. }
  2644. /* comparison instructions of i64 */
  2645. HANDLE_OP(WASM_OP_I64_EQZ)
  2646. {
  2647. DEF_OP_EQZ(int64, I64);
  2648. HANDLE_OP_END();
  2649. }
  2650. HANDLE_OP(WASM_OP_I64_EQ)
  2651. {
  2652. DEF_OP_CMP(uint64, I64, ==);
  2653. HANDLE_OP_END();
  2654. }
  2655. HANDLE_OP(WASM_OP_I64_NE)
  2656. {
  2657. DEF_OP_CMP(uint64, I64, !=);
  2658. HANDLE_OP_END();
  2659. }
  2660. HANDLE_OP(WASM_OP_I64_LT_S)
  2661. {
  2662. DEF_OP_CMP(int64, I64, <);
  2663. HANDLE_OP_END();
  2664. }
  2665. HANDLE_OP(WASM_OP_I64_LT_U)
  2666. {
  2667. DEF_OP_CMP(uint64, I64, <);
  2668. HANDLE_OP_END();
  2669. }
  2670. HANDLE_OP(WASM_OP_I64_GT_S)
  2671. {
  2672. DEF_OP_CMP(int64, I64, >);
  2673. HANDLE_OP_END();
  2674. }
  2675. HANDLE_OP(WASM_OP_I64_GT_U)
  2676. {
  2677. DEF_OP_CMP(uint64, I64, >);
  2678. HANDLE_OP_END();
  2679. }
  2680. HANDLE_OP(WASM_OP_I64_LE_S)
  2681. {
  2682. DEF_OP_CMP(int64, I64, <=);
  2683. HANDLE_OP_END();
  2684. }
  2685. HANDLE_OP(WASM_OP_I64_LE_U)
  2686. {
  2687. DEF_OP_CMP(uint64, I64, <=);
  2688. HANDLE_OP_END();
  2689. }
  2690. HANDLE_OP(WASM_OP_I64_GE_S)
  2691. {
  2692. DEF_OP_CMP(int64, I64, >=);
  2693. HANDLE_OP_END();
  2694. }
  2695. HANDLE_OP(WASM_OP_I64_GE_U)
  2696. {
  2697. DEF_OP_CMP(uint64, I64, >=);
  2698. HANDLE_OP_END();
  2699. }
  2700. /* comparison instructions of f32 */
  2701. HANDLE_OP(WASM_OP_F32_EQ)
  2702. {
  2703. DEF_OP_CMP(float32, F32, ==);
  2704. HANDLE_OP_END();
  2705. }
  2706. HANDLE_OP(WASM_OP_F32_NE)
  2707. {
  2708. DEF_OP_CMP(float32, F32, !=);
  2709. HANDLE_OP_END();
  2710. }
  2711. HANDLE_OP(WASM_OP_F32_LT)
  2712. {
  2713. DEF_OP_CMP(float32, F32, <);
  2714. HANDLE_OP_END();
  2715. }
  2716. HANDLE_OP(WASM_OP_F32_GT)
  2717. {
  2718. DEF_OP_CMP(float32, F32, >);
  2719. HANDLE_OP_END();
  2720. }
  2721. HANDLE_OP(WASM_OP_F32_LE)
  2722. {
  2723. DEF_OP_CMP(float32, F32, <=);
  2724. HANDLE_OP_END();
  2725. }
  2726. HANDLE_OP(WASM_OP_F32_GE)
  2727. {
  2728. DEF_OP_CMP(float32, F32, >=);
  2729. HANDLE_OP_END();
  2730. }
  2731. /* comparison instructions of f64 */
  2732. HANDLE_OP(WASM_OP_F64_EQ)
  2733. {
  2734. DEF_OP_CMP(float64, F64, ==);
  2735. HANDLE_OP_END();
  2736. }
  2737. HANDLE_OP(WASM_OP_F64_NE)
  2738. {
  2739. DEF_OP_CMP(float64, F64, !=);
  2740. HANDLE_OP_END();
  2741. }
  2742. HANDLE_OP(WASM_OP_F64_LT)
  2743. {
  2744. DEF_OP_CMP(float64, F64, <);
  2745. HANDLE_OP_END();
  2746. }
  2747. HANDLE_OP(WASM_OP_F64_GT)
  2748. {
  2749. DEF_OP_CMP(float64, F64, >);
  2750. HANDLE_OP_END();
  2751. }
  2752. HANDLE_OP(WASM_OP_F64_LE)
  2753. {
  2754. DEF_OP_CMP(float64, F64, <=);
  2755. HANDLE_OP_END();
  2756. }
  2757. HANDLE_OP(WASM_OP_F64_GE)
  2758. {
  2759. DEF_OP_CMP(float64, F64, >=);
  2760. HANDLE_OP_END();
  2761. }
  2762. /* numberic instructions of i32 */
  2763. HANDLE_OP(WASM_OP_I32_CLZ)
  2764. {
  2765. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  2766. HANDLE_OP_END();
  2767. }
  2768. HANDLE_OP(WASM_OP_I32_CTZ)
  2769. {
  2770. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  2771. HANDLE_OP_END();
  2772. }
  2773. HANDLE_OP(WASM_OP_I32_POPCNT)
  2774. {
  2775. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  2776. HANDLE_OP_END();
  2777. }
  2778. HANDLE_OP(WASM_OP_I32_ADD)
  2779. {
  2780. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  2781. HANDLE_OP_END();
  2782. }
  2783. HANDLE_OP(WASM_OP_I32_SUB)
  2784. {
  2785. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  2786. HANDLE_OP_END();
  2787. }
  2788. HANDLE_OP(WASM_OP_I32_MUL)
  2789. {
  2790. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  2791. HANDLE_OP_END();
  2792. }
  2793. HANDLE_OP(WASM_OP_I32_DIV_S)
  2794. {
  2795. int32 a, b;
  2796. b = frame_lp[GET_OFFSET()];
  2797. a = frame_lp[GET_OFFSET()];
  2798. addr_ret = GET_OFFSET();
  2799. if (a == (int32)0x80000000 && b == -1) {
  2800. wasm_set_exception(module, "integer overflow");
  2801. goto got_exception;
  2802. }
  2803. if (b == 0) {
  2804. wasm_set_exception(module, "integer divide by zero");
  2805. goto got_exception;
  2806. }
  2807. frame_lp[addr_ret] = (a / b);
  2808. HANDLE_OP_END();
  2809. }
  2810. HANDLE_OP(WASM_OP_I32_DIV_U)
  2811. {
  2812. uint32 a, b;
  2813. addr1 = GET_OFFSET();
  2814. addr2 = GET_OFFSET();
  2815. addr_ret = GET_OFFSET();
  2816. b = (uint32)frame_lp[addr1];
  2817. a = (uint32)frame_lp[addr2];
  2818. if (b == 0) {
  2819. wasm_set_exception(module, "integer divide by zero");
  2820. goto got_exception;
  2821. }
  2822. frame_lp[addr_ret] = (a / b);
  2823. HANDLE_OP_END();
  2824. }
  2825. HANDLE_OP(WASM_OP_I32_REM_S)
  2826. {
  2827. int32 a, b;
  2828. addr1 = GET_OFFSET();
  2829. addr2 = GET_OFFSET();
  2830. addr_ret = GET_OFFSET();
  2831. b = frame_lp[addr1];
  2832. a = frame_lp[addr2];
  2833. if (a == (int32)0x80000000 && b == -1) {
  2834. frame_lp[addr_ret] = 0;
  2835. HANDLE_OP_END();
  2836. }
  2837. if (b == 0) {
  2838. wasm_set_exception(module, "integer divide by zero");
  2839. goto got_exception;
  2840. }
  2841. frame_lp[addr_ret] = (a % b);
  2842. HANDLE_OP_END();
  2843. }
  2844. HANDLE_OP(WASM_OP_I32_REM_U)
  2845. {
  2846. uint32 a, b;
  2847. addr1 = GET_OFFSET();
  2848. addr2 = GET_OFFSET();
  2849. addr_ret = GET_OFFSET();
  2850. b = (uint32)frame_lp[addr1];
  2851. a = (uint32)frame_lp[addr2];
  2852. if (b == 0) {
  2853. wasm_set_exception(module, "integer divide by zero");
  2854. goto got_exception;
  2855. }
  2856. frame_lp[addr_ret] = (a % b);
  2857. HANDLE_OP_END();
  2858. }
  2859. HANDLE_OP(WASM_OP_I32_AND)
  2860. {
  2861. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  2862. HANDLE_OP_END();
  2863. }
  2864. HANDLE_OP(WASM_OP_I32_OR)
  2865. {
  2866. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  2867. HANDLE_OP_END();
  2868. }
  2869. HANDLE_OP(WASM_OP_I32_XOR)
  2870. {
  2871. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  2872. HANDLE_OP_END();
  2873. }
  2874. HANDLE_OP(WASM_OP_I32_SHL)
  2875. {
  2876. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  2877. HANDLE_OP_END();
  2878. }
  2879. HANDLE_OP(WASM_OP_I32_SHR_S)
  2880. {
  2881. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  2882. HANDLE_OP_END();
  2883. }
  2884. HANDLE_OP(WASM_OP_I32_SHR_U)
  2885. {
  2886. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  2887. HANDLE_OP_END();
  2888. }
  2889. HANDLE_OP(WASM_OP_I32_ROTL)
  2890. {
  2891. uint32 a, b;
  2892. b = (uint32)frame_lp[GET_OFFSET()];
  2893. a = (uint32)frame_lp[GET_OFFSET()];
  2894. frame_lp[GET_OFFSET()] = rotl32(a, b);
  2895. HANDLE_OP_END();
  2896. }
  2897. HANDLE_OP(WASM_OP_I32_ROTR)
  2898. {
  2899. uint32 a, b;
  2900. b = (uint32)frame_lp[GET_OFFSET()];
  2901. a = (uint32)frame_lp[GET_OFFSET()];
  2902. frame_lp[GET_OFFSET()] = rotr32(a, b);
  2903. HANDLE_OP_END();
  2904. }
  2905. /* numberic instructions of i64 */
  2906. HANDLE_OP(WASM_OP_I64_CLZ)
  2907. {
  2908. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  2909. HANDLE_OP_END();
  2910. }
  2911. HANDLE_OP(WASM_OP_I64_CTZ)
  2912. {
  2913. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  2914. HANDLE_OP_END();
  2915. }
  2916. HANDLE_OP(WASM_OP_I64_POPCNT)
  2917. {
  2918. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  2919. HANDLE_OP_END();
  2920. }
  2921. HANDLE_OP(WASM_OP_I64_ADD)
  2922. {
  2923. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  2924. HANDLE_OP_END();
  2925. }
  2926. HANDLE_OP(WASM_OP_I64_SUB)
  2927. {
  2928. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  2929. HANDLE_OP_END();
  2930. }
  2931. HANDLE_OP(WASM_OP_I64_MUL)
  2932. {
  2933. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  2934. HANDLE_OP_END();
  2935. }
  2936. HANDLE_OP(WASM_OP_I64_DIV_S)
  2937. {
  2938. int64 a, b;
  2939. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2940. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2941. if (a == (int64)0x8000000000000000LL && b == -1) {
  2942. wasm_set_exception(module, "integer overflow");
  2943. goto got_exception;
  2944. }
  2945. if (b == 0) {
  2946. wasm_set_exception(module, "integer divide by zero");
  2947. goto got_exception;
  2948. }
  2949. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2950. HANDLE_OP_END();
  2951. }
  2952. HANDLE_OP(WASM_OP_I64_DIV_U)
  2953. {
  2954. uint64 a, b;
  2955. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2956. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2957. if (b == 0) {
  2958. wasm_set_exception(module, "integer divide by zero");
  2959. goto got_exception;
  2960. }
  2961. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2962. HANDLE_OP_END();
  2963. }
  2964. HANDLE_OP(WASM_OP_I64_REM_S)
  2965. {
  2966. int64 a, b;
  2967. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2968. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2969. if (a == (int64)0x8000000000000000LL && b == -1) {
  2970. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  2971. HANDLE_OP_END();
  2972. }
  2973. if (b == 0) {
  2974. wasm_set_exception(module, "integer divide by zero");
  2975. goto got_exception;
  2976. }
  2977. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2978. HANDLE_OP_END();
  2979. }
  2980. HANDLE_OP(WASM_OP_I64_REM_U)
  2981. {
  2982. uint64 a, b;
  2983. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2984. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2985. if (b == 0) {
  2986. wasm_set_exception(module, "integer divide by zero");
  2987. goto got_exception;
  2988. }
  2989. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2990. HANDLE_OP_END();
  2991. }
  2992. HANDLE_OP(WASM_OP_I64_AND)
  2993. {
  2994. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  2995. HANDLE_OP_END();
  2996. }
  2997. HANDLE_OP(WASM_OP_I64_OR)
  2998. {
  2999. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  3000. HANDLE_OP_END();
  3001. }
  3002. HANDLE_OP(WASM_OP_I64_XOR)
  3003. {
  3004. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  3005. HANDLE_OP_END();
  3006. }
  3007. HANDLE_OP(WASM_OP_I64_SHL)
  3008. {
  3009. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  3010. HANDLE_OP_END();
  3011. }
  3012. HANDLE_OP(WASM_OP_I64_SHR_S)
  3013. {
  3014. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  3015. HANDLE_OP_END();
  3016. }
  3017. HANDLE_OP(WASM_OP_I64_SHR_U)
  3018. {
  3019. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  3020. HANDLE_OP_END();
  3021. }
  3022. HANDLE_OP(WASM_OP_I64_ROTL)
  3023. {
  3024. uint64 a, b;
  3025. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3026. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3027. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  3028. HANDLE_OP_END();
  3029. }
  3030. HANDLE_OP(WASM_OP_I64_ROTR)
  3031. {
  3032. uint64 a, b;
  3033. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3034. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3035. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  3036. HANDLE_OP_END();
  3037. }
  3038. /* numberic instructions of f32 */
  3039. HANDLE_OP(WASM_OP_F32_ABS)
  3040. {
  3041. DEF_OP_MATH(float32, F32, fabsf);
  3042. HANDLE_OP_END();
  3043. }
  3044. HANDLE_OP(WASM_OP_F32_NEG)
  3045. {
  3046. uint32 u32 = frame_lp[GET_OFFSET()];
  3047. uint32 sign_bit = u32 & ((uint32)1 << 31);
  3048. addr_ret = GET_OFFSET();
  3049. if (sign_bit)
  3050. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  3051. else
  3052. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  3053. HANDLE_OP_END();
  3054. }
  3055. HANDLE_OP(WASM_OP_F32_CEIL)
  3056. {
  3057. DEF_OP_MATH(float32, F32, ceilf);
  3058. HANDLE_OP_END();
  3059. }
  3060. HANDLE_OP(WASM_OP_F32_FLOOR)
  3061. {
  3062. DEF_OP_MATH(float32, F32, floorf);
  3063. HANDLE_OP_END();
  3064. }
  3065. HANDLE_OP(WASM_OP_F32_TRUNC)
  3066. {
  3067. DEF_OP_MATH(float32, F32, truncf);
  3068. HANDLE_OP_END();
  3069. }
  3070. HANDLE_OP(WASM_OP_F32_NEAREST)
  3071. {
  3072. DEF_OP_MATH(float32, F32, rintf);
  3073. HANDLE_OP_END();
  3074. }
  3075. HANDLE_OP(WASM_OP_F32_SQRT)
  3076. {
  3077. DEF_OP_MATH(float32, F32, sqrtf);
  3078. HANDLE_OP_END();
  3079. }
  3080. HANDLE_OP(WASM_OP_F32_ADD)
  3081. {
  3082. DEF_OP_NUMERIC(float32, float32, F32, +);
  3083. HANDLE_OP_END();
  3084. }
  3085. HANDLE_OP(WASM_OP_F32_SUB)
  3086. {
  3087. DEF_OP_NUMERIC(float32, float32, F32, -);
  3088. HANDLE_OP_END();
  3089. }
  3090. HANDLE_OP(WASM_OP_F32_MUL)
  3091. {
  3092. DEF_OP_NUMERIC(float32, float32, F32, *);
  3093. HANDLE_OP_END();
  3094. }
  3095. HANDLE_OP(WASM_OP_F32_DIV)
  3096. {
  3097. DEF_OP_NUMERIC(float32, float32, F32, /);
  3098. HANDLE_OP_END();
  3099. }
  3100. HANDLE_OP(WASM_OP_F32_MIN)
  3101. {
  3102. float32 a, b;
  3103. b = *(float32 *)(frame_lp + GET_OFFSET());
  3104. a = *(float32 *)(frame_lp + GET_OFFSET());
  3105. *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
  3106. HANDLE_OP_END();
  3107. }
  3108. HANDLE_OP(WASM_OP_F32_MAX)
  3109. {
  3110. float32 a, b;
  3111. b = *(float32 *)(frame_lp + GET_OFFSET());
  3112. a = *(float32 *)(frame_lp + GET_OFFSET());
  3113. *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
  3114. HANDLE_OP_END();
  3115. }
  3116. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  3117. {
  3118. float32 a, b;
  3119. b = *(float32 *)(frame_lp + GET_OFFSET());
  3120. a = *(float32 *)(frame_lp + GET_OFFSET());
  3121. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  3122. HANDLE_OP_END();
  3123. }
  3124. /* numberic instructions of f64 */
  3125. HANDLE_OP(WASM_OP_F64_ABS)
  3126. {
  3127. DEF_OP_MATH(float64, F64, fabs);
  3128. HANDLE_OP_END();
  3129. }
  3130. HANDLE_OP(WASM_OP_F64_NEG)
  3131. {
  3132. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3133. uint64 sign_bit = u64 & (((uint64)1) << 63);
  3134. if (sign_bit)
  3135. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  3136. (u64 & ~(((uint64)1) << 63)));
  3137. else
  3138. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  3139. (u64 | (((uint64)1) << 63)));
  3140. HANDLE_OP_END();
  3141. }
  3142. HANDLE_OP(WASM_OP_F64_CEIL)
  3143. {
  3144. DEF_OP_MATH(float64, F64, ceil);
  3145. HANDLE_OP_END();
  3146. }
  3147. HANDLE_OP(WASM_OP_F64_FLOOR)
  3148. {
  3149. DEF_OP_MATH(float64, F64, floor);
  3150. HANDLE_OP_END();
  3151. }
  3152. HANDLE_OP(WASM_OP_F64_TRUNC)
  3153. {
  3154. DEF_OP_MATH(float64, F64, trunc);
  3155. HANDLE_OP_END();
  3156. }
  3157. HANDLE_OP(WASM_OP_F64_NEAREST)
  3158. {
  3159. DEF_OP_MATH(float64, F64, rint);
  3160. HANDLE_OP_END();
  3161. }
  3162. HANDLE_OP(WASM_OP_F64_SQRT)
  3163. {
  3164. DEF_OP_MATH(float64, F64, sqrt);
  3165. HANDLE_OP_END();
  3166. }
  3167. HANDLE_OP(WASM_OP_F64_ADD)
  3168. {
  3169. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  3170. HANDLE_OP_END();
  3171. }
  3172. HANDLE_OP(WASM_OP_F64_SUB)
  3173. {
  3174. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  3175. HANDLE_OP_END();
  3176. }
  3177. HANDLE_OP(WASM_OP_F64_MUL)
  3178. {
  3179. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  3180. HANDLE_OP_END();
  3181. }
  3182. HANDLE_OP(WASM_OP_F64_DIV)
  3183. {
  3184. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  3185. HANDLE_OP_END();
  3186. }
  3187. HANDLE_OP(WASM_OP_F64_MIN)
  3188. {
  3189. float64 a, b;
  3190. b = POP_F64();
  3191. a = POP_F64();
  3192. PUSH_F64(f64_min(a, b));
  3193. HANDLE_OP_END();
  3194. }
  3195. HANDLE_OP(WASM_OP_F64_MAX)
  3196. {
  3197. float64 a, b;
  3198. b = POP_F64();
  3199. a = POP_F64();
  3200. PUSH_F64(f64_max(a, b));
  3201. HANDLE_OP_END();
  3202. }
  3203. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  3204. {
  3205. float64 a, b;
  3206. b = POP_F64();
  3207. a = POP_F64();
  3208. PUSH_F64(local_copysign(a, b));
  3209. HANDLE_OP_END();
  3210. }
  3211. /* conversions of i32 */
  3212. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  3213. {
  3214. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  3215. PUSH_I32(value);
  3216. HANDLE_OP_END();
  3217. }
  3218. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  3219. {
  3220. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  3221. since float/double values of ieee754 cannot precisely
  3222. represent all int32/uint32/int64/uint64 values, e.g.:
  3223. UINT32_MAX is 4294967295, but (float32)4294967295 is
  3224. 4294967296.0f, but not 4294967295.0f. */
  3225. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  3226. HANDLE_OP_END();
  3227. }
  3228. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  3229. {
  3230. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  3231. HANDLE_OP_END();
  3232. }
  3233. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  3234. {
  3235. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  3236. HANDLE_OP_END();
  3237. }
  3238. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  3239. {
  3240. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  3241. HANDLE_OP_END();
  3242. }
  3243. /* conversions of i64 */
  3244. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  3245. {
  3246. DEF_OP_CONVERT(int64, I64, int32, I32);
  3247. HANDLE_OP_END();
  3248. }
  3249. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  3250. {
  3251. DEF_OP_CONVERT(int64, I64, uint32, I32);
  3252. HANDLE_OP_END();
  3253. }
  3254. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  3255. {
  3256. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  3257. 9223372036854775808.0f, false, true);
  3258. HANDLE_OP_END();
  3259. }
  3260. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  3261. {
  3262. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  3263. HANDLE_OP_END();
  3264. }
  3265. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  3266. {
  3267. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  3268. false, true);
  3269. HANDLE_OP_END();
  3270. }
  3271. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  3272. {
  3273. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  3274. HANDLE_OP_END();
  3275. }
  3276. /* conversions of f32 */
  3277. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  3278. {
  3279. DEF_OP_CONVERT(float32, F32, int32, I32);
  3280. HANDLE_OP_END();
  3281. }
  3282. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  3283. {
  3284. DEF_OP_CONVERT(float32, F32, uint32, I32);
  3285. HANDLE_OP_END();
  3286. }
  3287. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  3288. {
  3289. DEF_OP_CONVERT(float32, F32, int64, I64);
  3290. HANDLE_OP_END();
  3291. }
  3292. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  3293. {
  3294. DEF_OP_CONVERT(float32, F32, uint64, I64);
  3295. HANDLE_OP_END();
  3296. }
  3297. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  3298. {
  3299. DEF_OP_CONVERT(float32, F32, float64, F64);
  3300. HANDLE_OP_END();
  3301. }
  3302. /* conversions of f64 */
  3303. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  3304. {
  3305. DEF_OP_CONVERT(float64, F64, int32, I32);
  3306. HANDLE_OP_END();
  3307. }
  3308. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  3309. {
  3310. DEF_OP_CONVERT(float64, F64, uint32, I32);
  3311. HANDLE_OP_END();
  3312. }
  3313. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  3314. {
  3315. DEF_OP_CONVERT(float64, F64, int64, I64);
  3316. HANDLE_OP_END();
  3317. }
  3318. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  3319. {
  3320. DEF_OP_CONVERT(float64, F64, uint64, I64);
  3321. HANDLE_OP_END();
  3322. }
  3323. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  3324. {
  3325. DEF_OP_CONVERT(float64, F64, float32, F32);
  3326. HANDLE_OP_END();
  3327. }
  3328. /* reinterpretations */
  3329. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  3330. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  3331. {
  3332. DEF_OP_REINTERPRET(uint32, I32);
  3333. HANDLE_OP_END();
  3334. }
  3335. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  3336. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  3337. {
  3338. DEF_OP_REINTERPRET(int64, I64);
  3339. HANDLE_OP_END();
  3340. }
  3341. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  3342. {
  3343. addr1 = GET_OFFSET();
  3344. addr2 = GET_OFFSET();
  3345. frame_lp[addr2] = frame_lp[addr1];
  3346. HANDLE_OP_END();
  3347. }
  3348. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  3349. {
  3350. addr1 = GET_OFFSET();
  3351. addr2 = GET_OFFSET();
  3352. frame_lp[addr2] = frame_lp[addr1];
  3353. frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
  3354. HANDLE_OP_END();
  3355. }
  3356. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  3357. {
  3358. uint32 values_count, total_cell;
  3359. uint8 *cells;
  3360. int16 *src_offsets = NULL;
  3361. uint16 *dst_offsets = NULL;
  3362. /* read values_count */
  3363. values_count = read_uint32(frame_ip);
  3364. /* read total cell num */
  3365. total_cell = read_uint32(frame_ip);
  3366. /* cells */
  3367. cells = (uint8 *)frame_ip;
  3368. frame_ip += values_count * CELL_SIZE;
  3369. /* src offsets */
  3370. src_offsets = (int16 *)frame_ip;
  3371. frame_ip += values_count * sizeof(int16);
  3372. /* dst offsets */
  3373. dst_offsets = (uint16 *)frame_ip;
  3374. frame_ip += values_count * sizeof(uint16);
  3375. if (!copy_stack_values(module, frame_lp, values_count,
  3376. total_cell, cells, src_offsets,
  3377. dst_offsets))
  3378. goto got_exception;
  3379. HANDLE_OP_END();
  3380. }
  3381. HANDLE_OP(WASM_OP_SET_LOCAL)
  3382. HANDLE_OP(WASM_OP_TEE_LOCAL)
  3383. {
  3384. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3385. addr1 = GET_OFFSET();
  3386. if (local_type == VALUE_TYPE_I32
  3387. || local_type == VALUE_TYPE_F32) {
  3388. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  3389. }
  3390. else if (local_type == VALUE_TYPE_I64
  3391. || local_type == VALUE_TYPE_F64) {
  3392. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3393. GET_I64_FROM_ADDR(frame_lp + addr1));
  3394. }
  3395. #if WASM_ENABLE_GC != 0
  3396. else if (wasm_is_type_reftype(local_type)) {
  3397. PUT_REF_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3398. GET_REF_FROM_ADDR(frame_lp + addr1));
  3399. }
  3400. #endif
  3401. else {
  3402. wasm_set_exception(module, "invalid local type");
  3403. goto got_exception;
  3404. }
  3405. HANDLE_OP_END();
  3406. }
  3407. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  3408. {
  3409. DEF_OP_CONVERT(int32, I32, int8, I32);
  3410. HANDLE_OP_END();
  3411. }
  3412. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  3413. {
  3414. DEF_OP_CONVERT(int32, I32, int16, I32);
  3415. HANDLE_OP_END();
  3416. }
  3417. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  3418. {
  3419. DEF_OP_CONVERT(int64, I64, int8, I64);
  3420. HANDLE_OP_END();
  3421. }
  3422. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  3423. {
  3424. DEF_OP_CONVERT(int64, I64, int16, I64);
  3425. HANDLE_OP_END();
  3426. }
  3427. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  3428. {
  3429. DEF_OP_CONVERT(int64, I64, int32, I64);
  3430. HANDLE_OP_END();
  3431. }
  3432. HANDLE_OP(WASM_OP_MISC_PREFIX)
  3433. {
  3434. GET_OPCODE();
  3435. switch (opcode) {
  3436. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3437. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  3438. true, true);
  3439. break;
  3440. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3441. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  3442. break;
  3443. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3444. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  3445. true);
  3446. break;
  3447. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3448. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  3449. break;
  3450. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3451. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  3452. 9223372036854775808.0f, false,
  3453. true);
  3454. break;
  3455. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3456. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  3457. false, false);
  3458. break;
  3459. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3460. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  3461. 9223372036854775808.0, false,
  3462. true);
  3463. break;
  3464. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3465. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  3466. false, false);
  3467. break;
  3468. #if WASM_ENABLE_BULK_MEMORY != 0
  3469. case WASM_OP_MEMORY_INIT:
  3470. {
  3471. uint32 addr, segment;
  3472. uint64 bytes, offset, seg_len;
  3473. uint8 *data;
  3474. segment = read_uint32(frame_ip);
  3475. bytes = (uint64)POP_I32();
  3476. offset = (uint64)POP_I32();
  3477. addr = POP_I32();
  3478. #if WASM_ENABLE_THREAD_MGR
  3479. linear_mem_size = memory->memory_data_size;
  3480. #endif
  3481. #ifndef OS_ENABLE_HW_BOUND_CHECK
  3482. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  3483. #else
  3484. if ((uint64)(uint32)addr + bytes
  3485. > (uint64)linear_mem_size)
  3486. goto out_of_bounds;
  3487. maddr = memory->memory_data + (uint32)addr;
  3488. #endif
  3489. seg_len = (uint64)module->module->data_segments[segment]
  3490. ->data_length;
  3491. data = module->module->data_segments[segment]->data;
  3492. if (offset + bytes > seg_len)
  3493. goto out_of_bounds;
  3494. bh_memcpy_s(maddr, linear_mem_size - addr,
  3495. data + offset, (uint32)bytes);
  3496. break;
  3497. }
  3498. case WASM_OP_DATA_DROP:
  3499. {
  3500. uint32 segment;
  3501. segment = read_uint32(frame_ip);
  3502. module->module->data_segments[segment]->data_length = 0;
  3503. break;
  3504. }
  3505. case WASM_OP_MEMORY_COPY:
  3506. {
  3507. uint32 dst, src, len;
  3508. uint8 *mdst, *msrc;
  3509. len = POP_I32();
  3510. src = POP_I32();
  3511. dst = POP_I32();
  3512. #if WASM_ENABLE_THREAD_MGR
  3513. linear_mem_size = memory->memory_data_size;
  3514. #endif
  3515. #ifndef OS_ENABLE_HW_BOUND_CHECK
  3516. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  3517. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  3518. #else
  3519. if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
  3520. goto out_of_bounds;
  3521. msrc = memory->memory_data + (uint32)src;
  3522. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  3523. goto out_of_bounds;
  3524. mdst = memory->memory_data + (uint32)dst;
  3525. #endif
  3526. /* allowing the destination and source to overlap */
  3527. bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
  3528. break;
  3529. }
  3530. case WASM_OP_MEMORY_FILL:
  3531. {
  3532. uint32 dst, len;
  3533. uint8 fill_val, *mdst;
  3534. len = POP_I32();
  3535. fill_val = POP_I32();
  3536. dst = POP_I32();
  3537. #if WASM_ENABLE_THREAD_MGR
  3538. linear_mem_size = memory->memory_data_size;
  3539. #endif
  3540. #ifndef OS_ENABLE_HW_BOUND_CHECK
  3541. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  3542. #else
  3543. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  3544. goto out_of_bounds;
  3545. mdst = memory->memory_data + (uint32)dst;
  3546. #endif
  3547. memset(mdst, fill_val, len);
  3548. break;
  3549. }
  3550. #endif /* WASM_ENABLE_BULK_MEMORY */
  3551. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3552. case WASM_OP_TABLE_INIT:
  3553. {
  3554. uint32 tbl_idx, elem_idx;
  3555. uint64 n, s, d;
  3556. WASMTableInstance *tbl_inst;
  3557. #if WASM_ENABLE_GC != 0
  3558. void **table_elems;
  3559. uint32 *func_indexes;
  3560. uint64 i;
  3561. #endif
  3562. elem_idx = read_uint32(frame_ip);
  3563. bh_assert(elem_idx < module->module->table_seg_count);
  3564. tbl_idx = read_uint32(frame_ip);
  3565. bh_assert(tbl_idx < module->module->table_count);
  3566. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  3567. n = (uint32)POP_I32();
  3568. s = (uint32)POP_I32();
  3569. d = (uint32)POP_I32();
  3570. if (!n) {
  3571. break;
  3572. }
  3573. if (n + s > module->module->table_segments[elem_idx]
  3574. .function_count
  3575. || d + n > tbl_inst->cur_size) {
  3576. wasm_set_exception(module,
  3577. "out of bounds table access");
  3578. goto got_exception;
  3579. }
  3580. if (module->module->table_segments[elem_idx]
  3581. .is_dropped) {
  3582. wasm_set_exception(module,
  3583. "out of bounds table access");
  3584. goto got_exception;
  3585. }
  3586. if (!wasm_elem_is_passive(
  3587. module->module->table_segments[elem_idx]
  3588. .mode)) {
  3589. wasm_set_exception(module,
  3590. "out of bounds table access");
  3591. goto got_exception;
  3592. }
  3593. #if WASM_ENABLE_GC == 0
  3594. bh_memcpy_s(
  3595. (uint8 *)tbl_inst
  3596. + offsetof(WASMTableInstance, elems)
  3597. + d * sizeof(uint32),
  3598. (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
  3599. module->module->table_segments[elem_idx]
  3600. .func_indexes
  3601. + s,
  3602. (uint32)(n * sizeof(uint32)));
  3603. #else
  3604. SYNC_ALL_TO_FRAME();
  3605. table_elems = tbl_inst->elems + d;
  3606. func_indexes = module->module->table_segments[elem_idx]
  3607. .func_indexes
  3608. + s;
  3609. for (i = 0; i < n; i++) {
  3610. /* UINT32_MAX indicates that it is an null ref */
  3611. if (func_indexes[i] != UINT32_MAX) {
  3612. if (!(func_obj = wasm_create_func_obj(
  3613. module, func_indexes[i], true, NULL,
  3614. 0))) {
  3615. goto got_exception;
  3616. }
  3617. table_elems[i] = func_obj;
  3618. }
  3619. else {
  3620. table_elems[i] = NULL_REF;
  3621. }
  3622. }
  3623. #endif
  3624. break;
  3625. }
  3626. case WASM_OP_ELEM_DROP:
  3627. {
  3628. uint32 elem_idx = read_uint32(frame_ip);
  3629. bh_assert(elem_idx < module->module->table_seg_count);
  3630. module->module->table_segments[elem_idx].is_dropped =
  3631. true;
  3632. break;
  3633. }
  3634. case WASM_OP_TABLE_COPY:
  3635. {
  3636. uint32 src_tbl_idx, dst_tbl_idx;
  3637. uint64 n, s, d;
  3638. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  3639. dst_tbl_idx = read_uint32(frame_ip);
  3640. bh_assert(dst_tbl_idx < module->table_count);
  3641. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  3642. src_tbl_idx = read_uint32(frame_ip);
  3643. bh_assert(src_tbl_idx < module->table_count);
  3644. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  3645. n = (uint32)POP_I32();
  3646. s = (uint32)POP_I32();
  3647. d = (uint32)POP_I32();
  3648. if (d + n > dst_tbl_inst->cur_size
  3649. || s + n > src_tbl_inst->cur_size) {
  3650. wasm_set_exception(module,
  3651. "out of bounds table access");
  3652. goto got_exception;
  3653. }
  3654. /* if s >= d, copy from front to back */
  3655. /* if s < d, copy from back to front */
  3656. /* merge all together */
  3657. bh_memmove_s((uint8 *)dst_tbl_inst
  3658. + offsetof(WASMTableInstance, elems)
  3659. + d * sizeof(table_elem_type_t),
  3660. (uint32)((dst_tbl_inst->cur_size - d)
  3661. * sizeof(table_elem_type_t)),
  3662. (uint8 *)src_tbl_inst
  3663. + offsetof(WASMTableInstance, elems)
  3664. + s * sizeof(table_elem_type_t),
  3665. (uint32)(n * sizeof(table_elem_type_t)));
  3666. break;
  3667. }
  3668. case WASM_OP_TABLE_GROW:
  3669. {
  3670. uint32 tbl_idx, n, orig_tbl_sz;
  3671. WASMTableInstance *tbl_inst;
  3672. table_elem_type_t init_val;
  3673. tbl_idx = read_uint32(frame_ip);
  3674. bh_assert(tbl_idx < module->table_count);
  3675. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  3676. orig_tbl_sz = tbl_inst->cur_size;
  3677. n = POP_I32();
  3678. #if WASM_ENABLE_GC == 0
  3679. init_val = POP_I32();
  3680. #else
  3681. init_val = POP_REF();
  3682. #endif
  3683. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  3684. PUSH_I32(-1);
  3685. }
  3686. else {
  3687. PUSH_I32(orig_tbl_sz);
  3688. }
  3689. break;
  3690. }
  3691. case WASM_OP_TABLE_SIZE:
  3692. {
  3693. uint32 tbl_idx;
  3694. WASMTableInstance *tbl_inst;
  3695. tbl_idx = read_uint32(frame_ip);
  3696. bh_assert(tbl_idx < module->table_count);
  3697. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  3698. PUSH_I32(tbl_inst->cur_size);
  3699. break;
  3700. }
  3701. case WASM_OP_TABLE_FILL:
  3702. {
  3703. uint32 tbl_idx, n, i;
  3704. WASMTableInstance *tbl_inst;
  3705. table_elem_type_t fill_val;
  3706. tbl_idx = read_uint32(frame_ip);
  3707. bh_assert(tbl_idx < module->table_count);
  3708. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  3709. n = POP_I32();
  3710. #if WASM_ENABLE_GC == 0
  3711. fill_val = POP_I32();
  3712. #else
  3713. fill_val = POP_REF();
  3714. #endif
  3715. i = POP_I32();
  3716. if (i + n > tbl_inst->cur_size) {
  3717. wasm_set_exception(module,
  3718. "out of bounds table access");
  3719. goto got_exception;
  3720. }
  3721. for (; n != 0; i++, n--) {
  3722. tbl_inst->elems[i] = fill_val;
  3723. }
  3724. break;
  3725. }
  3726. #endif /* WASM_ENABLE_REF_TYPES */
  3727. default:
  3728. wasm_set_exception(module, "unsupported opcode");
  3729. goto got_exception;
  3730. }
  3731. HANDLE_OP_END();
  3732. }
  3733. #if WASM_ENABLE_SHARED_MEMORY != 0
  3734. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  3735. {
  3736. uint32 offset = 0, addr;
  3737. GET_OPCODE();
  3738. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3739. offset = read_uint32(frame_ip);
  3740. }
  3741. switch (opcode) {
  3742. case WASM_OP_ATOMIC_NOTIFY:
  3743. {
  3744. uint32 notify_count, ret;
  3745. notify_count = POP_I32();
  3746. addr = POP_I32();
  3747. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3748. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3749. ret = wasm_runtime_atomic_notify(
  3750. (WASMModuleInstanceCommon *)module, maddr,
  3751. notify_count);
  3752. if (ret == (uint32)-1)
  3753. goto got_exception;
  3754. PUSH_I32(ret);
  3755. break;
  3756. }
  3757. case WASM_OP_ATOMIC_WAIT32:
  3758. {
  3759. uint64 timeout;
  3760. uint32 expect, ret;
  3761. timeout = POP_I64();
  3762. expect = POP_I32();
  3763. addr = POP_I32();
  3764. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3765. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3766. ret = wasm_runtime_atomic_wait(
  3767. (WASMModuleInstanceCommon *)module, maddr,
  3768. (uint64)expect, timeout, false);
  3769. if (ret == (uint32)-1)
  3770. goto got_exception;
  3771. #if WASM_ENABLE_THREAD_MGR != 0
  3772. CHECK_SUSPEND_FLAGS();
  3773. #endif
  3774. PUSH_I32(ret);
  3775. break;
  3776. }
  3777. case WASM_OP_ATOMIC_WAIT64:
  3778. {
  3779. uint64 timeout, expect;
  3780. uint32 ret;
  3781. timeout = POP_I64();
  3782. expect = POP_I64();
  3783. addr = POP_I32();
  3784. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3785. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3786. ret = wasm_runtime_atomic_wait(
  3787. (WASMModuleInstanceCommon *)module, maddr, expect,
  3788. timeout, true);
  3789. if (ret == (uint32)-1)
  3790. goto got_exception;
  3791. #if WASM_ENABLE_THREAD_MGR != 0
  3792. CHECK_SUSPEND_FLAGS();
  3793. #endif
  3794. PUSH_I32(ret);
  3795. break;
  3796. }
  3797. case WASM_OP_ATOMIC_FENCE:
  3798. {
  3799. os_atomic_thread_fence(os_memory_order_seq_cst);
  3800. break;
  3801. }
  3802. case WASM_OP_ATOMIC_I32_LOAD:
  3803. case WASM_OP_ATOMIC_I32_LOAD8_U:
  3804. case WASM_OP_ATOMIC_I32_LOAD16_U:
  3805. {
  3806. uint32 readv;
  3807. addr = POP_I32();
  3808. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  3809. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3810. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3811. os_mutex_lock(&node->shared_mem_lock);
  3812. readv = (uint32)(*(uint8 *)maddr);
  3813. os_mutex_unlock(&node->shared_mem_lock);
  3814. }
  3815. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  3816. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3817. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3818. os_mutex_lock(&node->shared_mem_lock);
  3819. readv = (uint32)LOAD_U16(maddr);
  3820. os_mutex_unlock(&node->shared_mem_lock);
  3821. }
  3822. else {
  3823. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3824. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3825. os_mutex_lock(&node->shared_mem_lock);
  3826. readv = LOAD_I32(maddr);
  3827. os_mutex_unlock(&node->shared_mem_lock);
  3828. }
  3829. PUSH_I32(readv);
  3830. break;
  3831. }
  3832. case WASM_OP_ATOMIC_I64_LOAD:
  3833. case WASM_OP_ATOMIC_I64_LOAD8_U:
  3834. case WASM_OP_ATOMIC_I64_LOAD16_U:
  3835. case WASM_OP_ATOMIC_I64_LOAD32_U:
  3836. {
  3837. uint64 readv;
  3838. addr = POP_I32();
  3839. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  3840. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3841. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3842. os_mutex_lock(&node->shared_mem_lock);
  3843. readv = (uint64)(*(uint8 *)maddr);
  3844. os_mutex_unlock(&node->shared_mem_lock);
  3845. }
  3846. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  3847. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3848. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3849. os_mutex_lock(&node->shared_mem_lock);
  3850. readv = (uint64)LOAD_U16(maddr);
  3851. os_mutex_unlock(&node->shared_mem_lock);
  3852. }
  3853. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  3854. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3855. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3856. os_mutex_lock(&node->shared_mem_lock);
  3857. readv = (uint64)LOAD_U32(maddr);
  3858. os_mutex_unlock(&node->shared_mem_lock);
  3859. }
  3860. else {
  3861. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3862. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3863. os_mutex_lock(&node->shared_mem_lock);
  3864. readv = LOAD_I64(maddr);
  3865. os_mutex_unlock(&node->shared_mem_lock);
  3866. }
  3867. PUSH_I64(readv);
  3868. break;
  3869. }
  3870. case WASM_OP_ATOMIC_I32_STORE:
  3871. case WASM_OP_ATOMIC_I32_STORE8:
  3872. case WASM_OP_ATOMIC_I32_STORE16:
  3873. {
  3874. uint32 sval;
  3875. sval = (uint32)POP_I32();
  3876. addr = POP_I32();
  3877. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  3878. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3879. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3880. os_mutex_lock(&node->shared_mem_lock);
  3881. *(uint8 *)maddr = (uint8)sval;
  3882. os_mutex_unlock(&node->shared_mem_lock);
  3883. }
  3884. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  3885. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3886. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3887. os_mutex_lock(&node->shared_mem_lock);
  3888. STORE_U16(maddr, (uint16)sval);
  3889. os_mutex_unlock(&node->shared_mem_lock);
  3890. }
  3891. else {
  3892. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3893. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3894. os_mutex_lock(&node->shared_mem_lock);
  3895. STORE_U32(maddr, sval);
  3896. os_mutex_unlock(&node->shared_mem_lock);
  3897. }
  3898. break;
  3899. }
  3900. case WASM_OP_ATOMIC_I64_STORE:
  3901. case WASM_OP_ATOMIC_I64_STORE8:
  3902. case WASM_OP_ATOMIC_I64_STORE16:
  3903. case WASM_OP_ATOMIC_I64_STORE32:
  3904. {
  3905. uint64 sval;
  3906. sval = (uint64)POP_I64();
  3907. addr = POP_I32();
  3908. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  3909. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3910. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3911. os_mutex_lock(&node->shared_mem_lock);
  3912. *(uint8 *)maddr = (uint8)sval;
  3913. os_mutex_unlock(&node->shared_mem_lock);
  3914. }
  3915. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  3916. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3917. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3918. os_mutex_lock(&node->shared_mem_lock);
  3919. STORE_U16(maddr, (uint16)sval);
  3920. os_mutex_unlock(&node->shared_mem_lock);
  3921. }
  3922. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  3923. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3924. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3925. os_mutex_lock(&node->shared_mem_lock);
  3926. STORE_U32(maddr, (uint32)sval);
  3927. os_mutex_unlock(&node->shared_mem_lock);
  3928. }
  3929. else {
  3930. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3931. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3932. os_mutex_lock(&node->shared_mem_lock);
  3933. STORE_I64(maddr, sval);
  3934. os_mutex_unlock(&node->shared_mem_lock);
  3935. }
  3936. break;
  3937. }
  3938. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  3939. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  3940. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  3941. {
  3942. uint32 readv, sval, expect;
  3943. sval = POP_I32();
  3944. expect = POP_I32();
  3945. addr = POP_I32();
  3946. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  3947. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3948. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3949. expect = (uint8)expect;
  3950. os_mutex_lock(&node->shared_mem_lock);
  3951. readv = (uint32)(*(uint8 *)maddr);
  3952. if (readv == expect)
  3953. *(uint8 *)maddr = (uint8)(sval);
  3954. os_mutex_unlock(&node->shared_mem_lock);
  3955. }
  3956. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  3957. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3958. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3959. expect = (uint16)expect;
  3960. os_mutex_lock(&node->shared_mem_lock);
  3961. readv = (uint32)LOAD_U16(maddr);
  3962. if (readv == expect)
  3963. STORE_U16(maddr, (uint16)(sval));
  3964. os_mutex_unlock(&node->shared_mem_lock);
  3965. }
  3966. else {
  3967. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3968. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3969. os_mutex_lock(&node->shared_mem_lock);
  3970. readv = LOAD_I32(maddr);
  3971. if (readv == expect)
  3972. STORE_U32(maddr, sval);
  3973. os_mutex_unlock(&node->shared_mem_lock);
  3974. }
  3975. PUSH_I32(readv);
  3976. break;
  3977. }
  3978. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  3979. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  3980. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  3981. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  3982. {
  3983. uint64 readv, sval, expect;
  3984. sval = (uint64)POP_I64();
  3985. expect = (uint64)POP_I64();
  3986. addr = POP_I32();
  3987. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  3988. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3989. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3990. expect = (uint8)expect;
  3991. os_mutex_lock(&node->shared_mem_lock);
  3992. readv = (uint64)(*(uint8 *)maddr);
  3993. if (readv == expect)
  3994. *(uint8 *)maddr = (uint8)(sval);
  3995. os_mutex_unlock(&node->shared_mem_lock);
  3996. }
  3997. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  3998. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3999. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4000. expect = (uint16)expect;
  4001. os_mutex_lock(&node->shared_mem_lock);
  4002. readv = (uint64)LOAD_U16(maddr);
  4003. if (readv == expect)
  4004. STORE_U16(maddr, (uint16)(sval));
  4005. os_mutex_unlock(&node->shared_mem_lock);
  4006. }
  4007. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  4008. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  4009. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4010. expect = (uint32)expect;
  4011. os_mutex_lock(&node->shared_mem_lock);
  4012. readv = (uint64)LOAD_U32(maddr);
  4013. if (readv == expect)
  4014. STORE_U32(maddr, (uint32)(sval));
  4015. os_mutex_unlock(&node->shared_mem_lock);
  4016. }
  4017. else {
  4018. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  4019. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4020. os_mutex_lock(&node->shared_mem_lock);
  4021. readv = (uint64)LOAD_I64(maddr);
  4022. if (readv == expect)
  4023. STORE_I64(maddr, sval);
  4024. os_mutex_unlock(&node->shared_mem_lock);
  4025. }
  4026. PUSH_I64(readv);
  4027. break;
  4028. }
  4029. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  4030. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  4031. DEF_ATOMIC_RMW_OPCODE(AND, &);
  4032. DEF_ATOMIC_RMW_OPCODE(OR, |);
  4033. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  4034. /* xchg, ignore the read value, and store the given
  4035. value: readv * 0 + sval */
  4036. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  4037. }
  4038. HANDLE_OP_END();
  4039. }
  4040. #endif
  4041. HANDLE_OP(WASM_OP_IMPDEP)
  4042. {
  4043. frame = prev_frame;
  4044. frame_ip = frame->ip;
  4045. goto call_func_from_entry;
  4046. }
  4047. HANDLE_OP(WASM_OP_CALL)
  4048. {
  4049. #if WASM_ENABLE_THREAD_MGR != 0
  4050. CHECK_SUSPEND_FLAGS();
  4051. #endif
  4052. fidx = read_uint32(frame_ip);
  4053. #if WASM_ENABLE_MULTI_MODULE != 0
  4054. if (fidx >= module->e->function_count) {
  4055. wasm_set_exception(module, "unknown function");
  4056. goto got_exception;
  4057. }
  4058. #endif
  4059. cur_func = module->e->functions + fidx;
  4060. goto call_func_from_interp;
  4061. }
  4062. #if WASM_ENABLE_TAIL_CALL != 0
  4063. HANDLE_OP(WASM_OP_RETURN_CALL)
  4064. {
  4065. #if WASM_ENABLE_THREAD_MGR != 0
  4066. CHECK_SUSPEND_FLAGS();
  4067. #endif
  4068. fidx = read_uint32(frame_ip);
  4069. #if WASM_ENABLE_MULTI_MODULE != 0
  4070. if (fidx >= module->e->function_count) {
  4071. wasm_set_exception(module, "unknown function");
  4072. goto got_exception;
  4073. }
  4074. #endif
  4075. cur_func = module->e->functions + fidx;
  4076. goto call_func_from_return_call;
  4077. }
  4078. #endif /* WASM_ENABLE_TAIL_CALL */
  4079. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  4080. default:
  4081. wasm_set_exception(module, "unsupported opcode");
  4082. goto got_exception;
  4083. }
  4084. #endif
  4085. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4086. HANDLE_OP(WASM_OP_UNUSED_0x06)
  4087. HANDLE_OP(WASM_OP_UNUSED_0x07)
  4088. HANDLE_OP(WASM_OP_UNUSED_0x08)
  4089. HANDLE_OP(WASM_OP_UNUSED_0x09)
  4090. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  4091. #if WASM_ENABLE_TAIL_CALL == 0
  4092. HANDLE_OP(WASM_OP_RETURN_CALL)
  4093. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  4094. #endif
  4095. #if WASM_ENABLE_SHARED_MEMORY == 0
  4096. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  4097. #endif
  4098. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  4099. HANDLE_OP(WASM_OP_TABLE_GET)
  4100. HANDLE_OP(WASM_OP_TABLE_SET)
  4101. HANDLE_OP(WASM_OP_REF_NULL)
  4102. HANDLE_OP(WASM_OP_REF_IS_NULL)
  4103. HANDLE_OP(WASM_OP_REF_FUNC)
  4104. #endif
  4105. #if WASM_ENABLE_GC == 0
  4106. /* SELECT_T is converted to SELECT or SELECT_64 */
  4107. HANDLE_OP(WASM_OP_SELECT_T)
  4108. #endif
  4109. #if WASM_ENABLE_GC == 0
  4110. HANDLE_OP(WASM_OP_CALL_REF)
  4111. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  4112. HANDLE_OP(WASM_OP_REF_EQ)
  4113. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  4114. HANDLE_OP(WASM_OP_BR_ON_NULL)
  4115. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  4116. HANDLE_OP(WASM_OP_GC_PREFIX)
  4117. #endif
  4118. HANDLE_OP(WASM_OP_UNUSED_0x16)
  4119. HANDLE_OP(WASM_OP_UNUSED_0x17)
  4120. HANDLE_OP(WASM_OP_UNUSED_0x18)
  4121. HANDLE_OP(WASM_OP_UNUSED_0x19)
  4122. HANDLE_OP(WASM_OP_UNUSED_0x27)
  4123. /* optimized op code */
  4124. HANDLE_OP(WASM_OP_F32_STORE)
  4125. HANDLE_OP(WASM_OP_F64_STORE)
  4126. HANDLE_OP(WASM_OP_F32_LOAD)
  4127. HANDLE_OP(WASM_OP_F64_LOAD)
  4128. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  4129. HANDLE_OP(WASM_OP_GET_LOCAL)
  4130. HANDLE_OP(WASM_OP_DROP)
  4131. HANDLE_OP(WASM_OP_DROP_64)
  4132. HANDLE_OP(WASM_OP_BLOCK)
  4133. HANDLE_OP(WASM_OP_LOOP)
  4134. HANDLE_OP(WASM_OP_END)
  4135. HANDLE_OP(WASM_OP_NOP)
  4136. HANDLE_OP(EXT_OP_BLOCK)
  4137. HANDLE_OP(EXT_OP_LOOP)
  4138. HANDLE_OP(EXT_OP_IF)
  4139. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  4140. {
  4141. wasm_set_exception(module, "unsupported opcode");
  4142. goto got_exception;
  4143. }
  4144. #endif
  4145. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  4146. continue;
  4147. #else
  4148. FETCH_OPCODE_AND_DISPATCH();
  4149. #endif
  4150. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  4151. call_func_from_return_call:
  4152. {
  4153. uint32 *lp_base = NULL, *lp = NULL;
  4154. int i;
  4155. if (cur_func->param_cell_num > 0
  4156. && !(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  4157. * sizeof(uint32)))) {
  4158. wasm_set_exception(module, "allocate memory failed");
  4159. goto got_exception;
  4160. }
  4161. for (i = 0; i < cur_func->param_count; i++) {
  4162. if (cur_func->param_types[i] == VALUE_TYPE_I64
  4163. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  4164. PUT_I64_TO_ADDR(
  4165. lp, GET_OPERAND(uint64, I64,
  4166. 2 * (cur_func->param_count - i - 1)));
  4167. lp += 2;
  4168. }
  4169. else {
  4170. *lp = GET_OPERAND(uint32, I32,
  4171. (2 * (cur_func->param_count - i - 1)));
  4172. lp++;
  4173. }
  4174. }
  4175. frame->lp = frame->operand + cur_func->const_cell_num;
  4176. if (lp - lp_base > 0) {
  4177. word_copy(frame->lp, lp_base, lp - lp_base);
  4178. }
  4179. if (lp_base)
  4180. wasm_runtime_free(lp_base);
  4181. FREE_FRAME(exec_env, frame);
  4182. frame_ip += cur_func->param_count * sizeof(int16);
  4183. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  4184. goto call_func_from_entry;
  4185. }
  4186. #endif /* WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 */
  4187. call_func_from_interp:
  4188. {
  4189. /* Only do the copy when it's called from interpreter. */
  4190. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  4191. int i;
  4192. #if WASM_ENABLE_MULTI_MODULE != 0
  4193. if (cur_func->is_import_func) {
  4194. outs_area->lp = outs_area->operand
  4195. + (cur_func->import_func_inst
  4196. ? cur_func->import_func_inst->const_cell_num
  4197. : 0);
  4198. }
  4199. else
  4200. #endif
  4201. {
  4202. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  4203. }
  4204. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  4205. > exec_env->wasm_stack.s.top_boundary) {
  4206. wasm_set_exception(module, "wasm operand stack overflow");
  4207. goto got_exception;
  4208. }
  4209. for (i = 0; i < cur_func->param_count; i++) {
  4210. if (cur_func->param_types[i] == VALUE_TYPE_I64
  4211. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  4212. PUT_I64_TO_ADDR(
  4213. outs_area->lp,
  4214. GET_OPERAND(uint64, I64,
  4215. 2 * (cur_func->param_count - i - 1)));
  4216. outs_area->lp += 2;
  4217. }
  4218. #if WASM_ENABLE_GC != 0
  4219. else if (wasm_is_type_reftype(cur_func->param_types[i])) {
  4220. PUT_REF_TO_ADDR(
  4221. outs_area->lp,
  4222. GET_OPERAND(void *, REF,
  4223. 2 * (cur_func->param_count - i - 1)));
  4224. outs_area->lp += REF_CELL_NUM;
  4225. }
  4226. #endif
  4227. else {
  4228. *outs_area->lp = GET_OPERAND(
  4229. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  4230. outs_area->lp++;
  4231. }
  4232. }
  4233. frame_ip += cur_func->param_count * sizeof(int16);
  4234. if (cur_func->ret_cell_num != 0) {
  4235. /* Get the first return value's offset. Since loader emit
  4236. * all return values' offset so we must skip remain return
  4237. * values' offsets.
  4238. */
  4239. WASMFuncType *func_type;
  4240. if (cur_func->is_import_func)
  4241. func_type = cur_func->u.func_import->func_type;
  4242. else
  4243. func_type = cur_func->u.func->func_type;
  4244. frame->ret_offset = GET_OFFSET();
  4245. frame_ip += 2 * (func_type->result_count - 1);
  4246. }
  4247. SYNC_ALL_TO_FRAME();
  4248. prev_frame = frame;
  4249. }
  4250. call_func_from_entry:
  4251. {
  4252. if (cur_func->is_import_func) {
  4253. #if WASM_ENABLE_MULTI_MODULE != 0
  4254. if (cur_func->import_func_inst) {
  4255. wasm_interp_call_func_import(module, exec_env, cur_func,
  4256. prev_frame);
  4257. }
  4258. else
  4259. #endif
  4260. {
  4261. wasm_interp_call_func_native(module, exec_env, cur_func,
  4262. prev_frame);
  4263. }
  4264. prev_frame = frame->prev_frame;
  4265. cur_func = frame->function;
  4266. UPDATE_ALL_FROM_FRAME();
  4267. /* update memory size, no need to update memory ptr as
  4268. it isn't changed in wasm_enlarge_memory */
  4269. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  4270. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  4271. || WASM_ENABLE_BULK_MEMORY != 0
  4272. if (memory)
  4273. linear_mem_size = memory->memory_data_size;
  4274. #endif
  4275. if (wasm_copy_exception(module, NULL))
  4276. goto got_exception;
  4277. }
  4278. else {
  4279. WASMFunction *cur_wasm_func = cur_func->u.func;
  4280. uint32 cell_num_of_local_stack;
  4281. cell_num_of_local_stack = cur_func->param_cell_num
  4282. + cur_func->local_cell_num
  4283. + cur_wasm_func->max_stack_cell_num;
  4284. all_cell_num = cur_func->const_cell_num + cell_num_of_local_stack;
  4285. #if WASM_ENABLE_GC != 0
  4286. /* area of frame_ref */
  4287. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  4288. #endif
  4289. /* param_cell_num, local_cell_num, const_cell_num and
  4290. max_stack_cell_num are all no larger than UINT16_MAX (checked
  4291. in loader), all_cell_num must be smaller than 1MB */
  4292. bh_assert(all_cell_num < 1 * BH_MB);
  4293. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  4294. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  4295. frame = prev_frame;
  4296. goto got_exception;
  4297. }
  4298. /* Initialize the interpreter context. */
  4299. frame->function = cur_func;
  4300. frame_ip = wasm_get_func_code(cur_func);
  4301. frame_ip_end = wasm_get_func_code_end(cur_func);
  4302. frame_lp = frame->lp =
  4303. frame->operand + cur_wasm_func->const_cell_num;
  4304. /* Initialize the consts */
  4305. if (cur_wasm_func->const_cell_num > 0) {
  4306. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  4307. cur_wasm_func->const_cell_num);
  4308. }
  4309. /* Initialize the local variables */
  4310. memset(frame_lp + cur_func->param_cell_num, 0,
  4311. (uint32)(cur_func->local_cell_num * 4));
  4312. #if WASM_ENABLE_GC != 0
  4313. frame_ref = frame->frame_ref =
  4314. (uint8 *)(frame->lp + (uint32)cell_num_of_local_stack);
  4315. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  4316. cur_func);
  4317. #endif
  4318. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  4319. }
  4320. #if WASM_ENABLE_THREAD_MGR != 0
  4321. CHECK_SUSPEND_FLAGS();
  4322. #endif
  4323. HANDLE_OP_END();
  4324. }
  4325. return_func:
  4326. {
  4327. FREE_FRAME(exec_env, frame);
  4328. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  4329. if (!prev_frame->ip)
  4330. /* Called from native. */
  4331. return;
  4332. RECOVER_CONTEXT(prev_frame);
  4333. HANDLE_OP_END();
  4334. }
  4335. (void)frame_ip_end;
  4336. #if WASM_ENABLE_SHARED_MEMORY != 0
  4337. unaligned_atomic:
  4338. wasm_set_exception(module, "unaligned atomic");
  4339. goto got_exception;
  4340. #endif
  4341. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  4342. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  4343. || WASM_ENABLE_BULK_MEMORY != 0
  4344. out_of_bounds:
  4345. wasm_set_exception(module, "out of bounds memory access");
  4346. #endif
  4347. got_exception:
  4348. SYNC_ALL_TO_FRAME();
  4349. return;
  4350. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  4351. }
  4352. #else
  4353. FETCH_OPCODE_AND_DISPATCH();
  4354. #endif
  4355. }
  4356. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4357. void **
  4358. wasm_interp_get_handle_table()
  4359. {
  4360. WASMModuleInstance module;
  4361. memset(&module, 0, sizeof(WASMModuleInstance));
  4362. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  4363. return global_handle_table;
  4364. }
  4365. #endif
  4366. #if WASM_ENABLE_GC != 0
  4367. bool
  4368. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  4369. {
  4370. WASMInterpFrame *frame;
  4371. WASMObjectRef gc_obj;
  4372. WASMFunctionInstance *cur_func;
  4373. uint8 *frame_ref;
  4374. uint32 local_cell_num, i;
  4375. frame = wasm_exec_env_get_cur_frame(exec_env);
  4376. for (; frame; frame = frame->prev_frame) {
  4377. frame_ref = frame->frame_ref;
  4378. cur_func = frame->function;
  4379. if (!cur_func)
  4380. continue;
  4381. local_cell_num = cur_func->param_cell_num;
  4382. if (frame->ip)
  4383. local_cell_num +=
  4384. cur_func->local_cell_num + cur_func->u.func->max_stack_cell_num;
  4385. for (i = 0; i < local_cell_num; i++) {
  4386. if (frame_ref[i]) {
  4387. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  4388. if (wasm_obj_is_created_from_heap(gc_obj)) {
  4389. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  4390. return false;
  4391. }
  4392. }
  4393. #if UINTPTR_MAX == UINT64_MAX
  4394. bh_assert(frame_ref[i + 1]);
  4395. i++;
  4396. #endif
  4397. }
  4398. }
  4399. }
  4400. return true;
  4401. }
  4402. #endif
  4403. void
  4404. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  4405. WASMFunctionInstance *function, uint32 argc,
  4406. uint32 argv[])
  4407. {
  4408. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  4409. WASMInterpFrame *frame, *outs_area;
  4410. /* Allocate sufficient cells for all kinds of return values. */
  4411. unsigned all_cell_num =
  4412. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  4413. i;
  4414. /* This frame won't be used by JITed code, so only allocate interp
  4415. frame here. */
  4416. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  4417. char exception[EXCEPTION_BUF_LEN];
  4418. if (argc < function->param_cell_num) {
  4419. char buf[128];
  4420. snprintf(buf, sizeof(buf),
  4421. "invalid argument count %" PRIu32
  4422. ", must be no smaller than %" PRIu32,
  4423. argc, (uint32)function->param_cell_num);
  4424. wasm_set_exception(module_inst, buf);
  4425. return;
  4426. }
  4427. argc = function->param_cell_num;
  4428. RECORD_STACK_USAGE(exec_env, (uint8 *)&prev_frame);
  4429. #if !(defined(OS_ENABLE_HW_BOUND_CHECK) \
  4430. && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0)
  4431. if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
  4432. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  4433. "native stack overflow");
  4434. return;
  4435. }
  4436. #endif
  4437. if (!(frame =
  4438. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  4439. return;
  4440. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  4441. frame->function = NULL;
  4442. frame->ip = NULL;
  4443. /* There is no local variable. */
  4444. frame->lp = frame->operand + 0;
  4445. #if WASM_ENABLE_GC != 0
  4446. frame->frame_ref = (uint8 *)frame->lp;
  4447. #endif
  4448. frame->ret_offset = 0;
  4449. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  4450. > exec_env->wasm_stack.s.top_boundary) {
  4451. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  4452. "wasm operand stack overflow");
  4453. return;
  4454. }
  4455. if (argc > 0)
  4456. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  4457. wasm_exec_env_set_cur_frame(exec_env, frame);
  4458. if (function->is_import_func) {
  4459. #if WASM_ENABLE_MULTI_MODULE != 0
  4460. if (function->import_module_inst) {
  4461. LOG_DEBUG("it is a function of a sub module");
  4462. wasm_interp_call_func_import(module_inst, exec_env, function,
  4463. frame);
  4464. }
  4465. else
  4466. #endif
  4467. {
  4468. LOG_DEBUG("it is an native function");
  4469. wasm_interp_call_func_native(module_inst, exec_env, function,
  4470. frame);
  4471. }
  4472. }
  4473. else {
  4474. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  4475. }
  4476. /* Output the return value to the caller */
  4477. if (!wasm_copy_exception(module_inst, NULL)) {
  4478. for (i = 0; i < function->ret_cell_num; i++)
  4479. argv[i] = *(frame->lp + i);
  4480. }
  4481. else {
  4482. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  4483. if (wasm_interp_create_call_stack(exec_env)) {
  4484. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  4485. }
  4486. #endif
  4487. wasm_copy_exception(module_inst, exception);
  4488. LOG_DEBUG("meet an exception %s", exception);
  4489. }
  4490. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  4491. FREE_FRAME(exec_env, frame);
  4492. #if WASM_ENABLE_OPCODE_COUNTER != 0
  4493. wasm_interp_dump_op_count();
  4494. #endif
  4495. }