wasm_interp_classic.c 284 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559
  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. #if WASM_ENABLE_STRINGREF != 0
  16. #include "string_object.h"
  17. #endif
  18. #endif
  19. #if WASM_ENABLE_SHARED_MEMORY != 0
  20. #include "../common/wasm_shared_memory.h"
  21. #endif
  22. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  23. #include "../libraries/thread-mgr/thread_manager.h"
  24. #include "../libraries/debug-engine/debug_engine.h"
  25. #endif
  26. #if WASM_ENABLE_FAST_JIT != 0
  27. #include "../fast-jit/jit_compiler.h"
  28. #endif
  29. typedef int32 CellType_I32;
  30. typedef int64 CellType_I64;
  31. typedef float32 CellType_F32;
  32. typedef float64 CellType_F64;
  33. #define BR_TABLE_TMP_BUF_LEN 32
  34. #if WASM_ENABLE_THREAD_MGR == 0
  35. #define get_linear_mem_size() linear_mem_size
  36. #else
  37. /**
  38. * Load memory data size in each time boundary check in
  39. * multi-threading mode since it may be changed by other
  40. * threads in memory.grow
  41. */
  42. #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
  43. #endif
  44. #if WASM_ENABLE_MEMORY64 == 0
  45. #if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
  46. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0)
  47. #define CHECK_MEMORY_OVERFLOW(bytes) \
  48. do { \
  49. uint64 offset1 = (uint64)offset + (uint64)addr; \
  50. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  51. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  52. /* If offset1 is in valid range, maddr must also \
  53. be in valid range, no need to check it again. */ \
  54. maddr = memory->memory_data + offset1; \
  55. else \
  56. goto out_of_bounds; \
  57. } while (0)
  58. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  59. do { \
  60. uint64 offset1 = (uint32)(start); \
  61. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  62. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  63. /* App heap space is not valid space for \
  64. bulk memory operation */ \
  65. maddr = memory->memory_data + offset1; \
  66. else \
  67. goto out_of_bounds; \
  68. } while (0)
  69. #else /* else of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  70. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  71. #define CHECK_MEMORY_OVERFLOW(bytes) \
  72. do { \
  73. uint64 offset1 = (uint64)offset + (uint64)addr; \
  74. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  75. maddr = memory->memory_data + offset1; \
  76. } while (0)
  77. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  78. do { \
  79. uint64 offset1 = (uint32)(start); \
  80. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  81. maddr = memory->memory_data + offset1; \
  82. } while (0)
  83. #endif /* end of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  84. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  85. #else /* else of WASM_ENABLE_MEMORY64 == 0 */
  86. #define CHECK_MEMORY_OVERFLOW(bytes) \
  87. do { \
  88. uint64 offset1 = (uint64)offset + (uint64)addr; \
  89. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  90. /* If memory64 is enabled, offset1, offset1 + bytes can overflow */ \
  91. if (disable_bounds_checks \
  92. || (offset1 >= offset && offset1 + bytes >= offset1 \
  93. && offset1 + bytes <= get_linear_mem_size())) \
  94. maddr = memory->memory_data + offset1; \
  95. else \
  96. goto out_of_bounds; \
  97. } while (0)
  98. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  99. do { \
  100. uint64 offset1 = (uint64)(start); \
  101. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  102. /* If memory64 is enabled, offset1 + bytes can overflow */ \
  103. if (disable_bounds_checks \
  104. || (offset1 + bytes >= offset1 \
  105. && offset1 + bytes <= get_linear_mem_size())) \
  106. /* App heap space is not valid space for \
  107. bulk memory operation */ \
  108. maddr = memory->memory_data + offset1; \
  109. else \
  110. goto out_of_bounds; \
  111. } while (0)
  112. #endif /* end of WASM_ENABLE_MEMORY64 == 0 */
  113. #define CHECK_ATOMIC_MEMORY_ACCESS() \
  114. do { \
  115. if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0) \
  116. goto unaligned_atomic; \
  117. } while (0)
  118. #if WASM_ENABLE_DEBUG_INTERP != 0
  119. #define TRIGGER_WATCHPOINT_SIGTRAP() \
  120. do { \
  121. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP); \
  122. CHECK_SUSPEND_FLAGS(); \
  123. } while (0)
  124. #define CHECK_WATCHPOINT(list, current_addr) \
  125. do { \
  126. WASMDebugWatchPoint *watchpoint = bh_list_first_elem(list); \
  127. while (watchpoint) { \
  128. WASMDebugWatchPoint *next = bh_list_elem_next(watchpoint); \
  129. if (watchpoint->addr <= current_addr \
  130. && watchpoint->addr + watchpoint->length > current_addr) { \
  131. TRIGGER_WATCHPOINT_SIGTRAP(); \
  132. } \
  133. watchpoint = next; \
  134. } \
  135. } while (0)
  136. #define CHECK_READ_WATCHPOINT(addr, offset) \
  137. CHECK_WATCHPOINT(watch_point_list_read, WASM_ADDR_OFFSET(addr + offset))
  138. #define CHECK_WRITE_WATCHPOINT(addr, offset) \
  139. CHECK_WATCHPOINT(watch_point_list_write, WASM_ADDR_OFFSET(addr + offset))
  140. #else
  141. #define CHECK_READ_WATCHPOINT(addr, offset) (void)0
  142. #define CHECK_WRITE_WATCHPOINT(addr, offset) (void)0
  143. #endif
  144. static inline uint32
  145. rotl32(uint32 n, uint32 c)
  146. {
  147. const uint32 mask = (31);
  148. c = c % 32;
  149. c &= mask;
  150. return (n << c) | (n >> ((0 - c) & mask));
  151. }
  152. static inline uint32
  153. rotr32(uint32 n, uint32 c)
  154. {
  155. const uint32 mask = (31);
  156. c = c % 32;
  157. c &= mask;
  158. return (n >> c) | (n << ((0 - c) & mask));
  159. }
  160. static inline uint64
  161. rotl64(uint64 n, uint64 c)
  162. {
  163. const uint64 mask = (63);
  164. c = c % 64;
  165. c &= mask;
  166. return (n << c) | (n >> ((0 - c) & mask));
  167. }
  168. static inline uint64
  169. rotr64(uint64 n, uint64 c)
  170. {
  171. const uint64 mask = (63);
  172. c = c % 64;
  173. c &= mask;
  174. return (n >> c) | (n << ((0 - c) & mask));
  175. }
  176. static inline float32
  177. f32_min(float32 a, float32 b)
  178. {
  179. if (isnan(a) || isnan(b))
  180. return NAN;
  181. else if (a == 0 && a == b)
  182. return signbit(a) ? a : b;
  183. else
  184. return a > b ? b : a;
  185. }
  186. static inline float32
  187. f32_max(float32 a, float32 b)
  188. {
  189. if (isnan(a) || isnan(b))
  190. return NAN;
  191. else if (a == 0 && a == b)
  192. return signbit(a) ? b : a;
  193. else
  194. return a > b ? a : b;
  195. }
  196. static inline float64
  197. f64_min(float64 a, float64 b)
  198. {
  199. if (isnan(a) || isnan(b))
  200. return NAN;
  201. else if (a == 0 && a == b)
  202. return signbit(a) ? a : b;
  203. else
  204. return a > b ? b : a;
  205. }
  206. static inline float64
  207. f64_max(float64 a, float64 b)
  208. {
  209. if (isnan(a) || isnan(b))
  210. return NAN;
  211. else if (a == 0 && a == b)
  212. return signbit(a) ? b : a;
  213. else
  214. return a > b ? a : b;
  215. }
  216. static inline uint32
  217. clz32(uint32 type)
  218. {
  219. uint32 num = 0;
  220. if (type == 0)
  221. return 32;
  222. while (!(type & 0x80000000)) {
  223. num++;
  224. type <<= 1;
  225. }
  226. return num;
  227. }
  228. static inline uint32
  229. clz64(uint64 type)
  230. {
  231. uint32 num = 0;
  232. if (type == 0)
  233. return 64;
  234. while (!(type & 0x8000000000000000LL)) {
  235. num++;
  236. type <<= 1;
  237. }
  238. return num;
  239. }
  240. static inline uint32
  241. ctz32(uint32 type)
  242. {
  243. uint32 num = 0;
  244. if (type == 0)
  245. return 32;
  246. while (!(type & 1)) {
  247. num++;
  248. type >>= 1;
  249. }
  250. return num;
  251. }
  252. static inline uint32
  253. ctz64(uint64 type)
  254. {
  255. uint32 num = 0;
  256. if (type == 0)
  257. return 64;
  258. while (!(type & 1)) {
  259. num++;
  260. type >>= 1;
  261. }
  262. return num;
  263. }
  264. static inline uint32
  265. popcount32(uint32 u)
  266. {
  267. uint32 ret = 0;
  268. while (u) {
  269. u = (u & (u - 1));
  270. ret++;
  271. }
  272. return ret;
  273. }
  274. static inline uint32
  275. popcount64(uint64 u)
  276. {
  277. uint32 ret = 0;
  278. while (u) {
  279. u = (u & (u - 1));
  280. ret++;
  281. }
  282. return ret;
  283. }
  284. static float
  285. local_copysignf(float x, float y)
  286. {
  287. union {
  288. float f;
  289. uint32 i;
  290. } ux = { x }, uy = { y };
  291. ux.i &= 0x7fffffff;
  292. ux.i |= uy.i & 0x80000000;
  293. return ux.f;
  294. }
  295. static double
  296. local_copysign(double x, double y)
  297. {
  298. union {
  299. double f;
  300. uint64 i;
  301. } ux = { x }, uy = { y };
  302. ux.i &= UINT64_MAX / 2;
  303. ux.i |= uy.i & 1ULL << 63;
  304. return ux.f;
  305. }
  306. static uint64
  307. read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
  308. {
  309. uint64 result = 0, byte;
  310. uint32 offset = *p_offset;
  311. uint32 shift = 0;
  312. while (true) {
  313. byte = buf[offset++];
  314. result |= ((byte & 0x7f) << shift);
  315. shift += 7;
  316. if ((byte & 0x80) == 0) {
  317. break;
  318. }
  319. }
  320. if (sign && (shift < maxbits) && (byte & 0x40)) {
  321. /* Sign extend */
  322. result |= (~((uint64)0)) << shift;
  323. }
  324. *p_offset = offset;
  325. return result;
  326. }
  327. #if WASM_ENABLE_GC != 0
  328. static uint8 *
  329. get_frame_ref(WASMInterpFrame *frame)
  330. {
  331. WASMFunctionInstance *cur_func = frame->function;
  332. uint32 all_cell_num;
  333. if (!cur_func) {
  334. /* it's a glue frame created in wasm_interp_call_wasm,
  335. no GC object will be traversed */
  336. return (uint8 *)frame->lp;
  337. }
  338. else if (!frame->ip) {
  339. /* it's a native method frame created in
  340. wasm_interp_call_func_native */
  341. all_cell_num =
  342. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  343. return (uint8 *)(frame->lp + all_cell_num);
  344. }
  345. else {
  346. #if WASM_ENABLE_JIT == 0
  347. /* it's a wasm bytecode function frame */
  348. return (uint8 *)frame->csp_boundary;
  349. #else
  350. return (uint8 *)(frame->lp + cur_func->param_cell_num
  351. + cur_func->local_cell_num
  352. + cur_func->u.func->max_stack_cell_num);
  353. #endif
  354. }
  355. }
  356. static void
  357. init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
  358. {
  359. uint32 i, j;
  360. memset(frame_ref, 0, cell_num);
  361. for (i = 0, j = 0; i < func->param_count; i++) {
  362. if (wasm_is_type_reftype(func->param_types[i])
  363. && !wasm_is_reftype_i31ref(func->param_types[i])) {
  364. frame_ref[j++] = 1;
  365. #if UINTPTR_MAX == UINT64_MAX
  366. frame_ref[j++] = 1;
  367. #endif
  368. }
  369. else {
  370. j += wasm_value_type_cell_num(func->param_types[i]);
  371. }
  372. }
  373. for (i = 0; i < func->local_count; i++) {
  374. if (wasm_is_type_reftype(func->local_types[i])
  375. && !wasm_is_reftype_i31ref(func->local_types[i])) {
  376. frame_ref[j++] = 1;
  377. #if UINTPTR_MAX == UINT64_MAX
  378. frame_ref[j++] = 1;
  379. #endif
  380. }
  381. else {
  382. j += wasm_value_type_cell_num(func->local_types[i]);
  383. }
  384. }
  385. }
  386. uint8 *
  387. wasm_interp_get_frame_ref(WASMInterpFrame *frame)
  388. {
  389. return get_frame_ref(frame);
  390. }
  391. /* Return the corresponding ref slot of the given address of local
  392. variable or stack pointer. */
  393. #define COMPUTE_FRAME_REF(ref, lp, p) (ref + (unsigned)((uint32 *)p - lp))
  394. #define FRAME_REF(p) COMPUTE_FRAME_REF(frame_ref, frame_lp, p)
  395. #define FRAME_REF_FOR(frame, p) \
  396. COMPUTE_FRAME_REF(get_frame_ref(frame), frame->lp, p)
  397. #define CLEAR_FRAME_REF(p, n) \
  398. do { \
  399. int32 ref_i, ref_n = (int32)(n); \
  400. uint8 *ref = FRAME_REF(p); \
  401. for (ref_i = 0; ref_i < ref_n; ref_i++) \
  402. ref[ref_i] = 0; \
  403. } while (0)
  404. #else
  405. #define CLEAR_FRAME_REF(p, n) (void)0
  406. #endif /* end of WASM_ENABLE_GC != 0 */
  407. #define skip_leb(p) while (*p++ & 0x80)
  408. #define PUSH_I32(value) \
  409. do { \
  410. *(int32 *)frame_sp++ = (int32)(value); \
  411. } while (0)
  412. #define PUSH_F32(value) \
  413. do { \
  414. *(float32 *)frame_sp++ = (float32)(value); \
  415. } while (0)
  416. #define PUSH_I64(value) \
  417. do { \
  418. PUT_I64_TO_ADDR(frame_sp, value); \
  419. frame_sp += 2; \
  420. } while (0)
  421. #define PUSH_F64(value) \
  422. do { \
  423. PUT_F64_TO_ADDR(frame_sp, value); \
  424. frame_sp += 2; \
  425. } while (0)
  426. #if UINTPTR_MAX == UINT64_MAX
  427. #define PUSH_REF(value) \
  428. do { \
  429. PUT_REF_TO_ADDR(frame_sp, value); \
  430. frame_ref_tmp = FRAME_REF(frame_sp); \
  431. *frame_ref_tmp = *(frame_ref_tmp + 1) = 1; \
  432. frame_sp += 2; \
  433. } while (0)
  434. #define PUSH_I31REF(value) \
  435. do { \
  436. PUT_REF_TO_ADDR(frame_sp, value); \
  437. frame_sp += 2; \
  438. } while (0)
  439. #else
  440. #define PUSH_REF(value) \
  441. do { \
  442. PUT_REF_TO_ADDR(frame_sp, value); \
  443. frame_ref_tmp = FRAME_REF(frame_sp); \
  444. *frame_ref_tmp = 1; \
  445. frame_sp++; \
  446. } while (0)
  447. #define PUSH_I31REF(value) \
  448. do { \
  449. PUT_REF_TO_ADDR(frame_sp, value); \
  450. frame_sp++; \
  451. } while (0)
  452. #endif
  453. #if UINTPTR_MAX == UINT64_MAX
  454. #define PUSH_PTR(value) PUSH_I64(value)
  455. #else
  456. #define PUSH_PTR(value) PUSH_I32(value)
  457. #endif
  458. /* in exception handling, label_type needs to be stored to lookup exception
  459. * handlers */
  460. #if WASM_ENABLE_EXCE_HANDLING != 0
  461. #define SET_LABEL_TYPE(_label_type) frame_csp->label_type = _label_type
  462. #else
  463. #define SET_LABEL_TYPE(_label_type) (void)0
  464. #endif
  465. #if WASM_ENABLE_MEMORY64 != 0
  466. #define COND_PUSH_TEMPLATE(cond, value) \
  467. do { \
  468. if (cond) { \
  469. PUT_I64_TO_ADDR(frame_sp, value); \
  470. frame_sp += 2; \
  471. } \
  472. else { \
  473. *(int32 *)frame_sp++ = (int32)(value); \
  474. } \
  475. } while (0)
  476. #define PUSH_MEM_OFFSET(value) COND_PUSH_TEMPLATE(is_memory64, value)
  477. #define PUSH_TBL_ELEM_IDX(value) COND_PUSH_TEMPLATE(is_table64, value)
  478. #else
  479. #define PUSH_MEM_OFFSET(value) PUSH_I32(value)
  480. #define PUSH_TBL_ELEM_IDX(value) PUSH_I32(value)
  481. #endif
  482. #define PUSH_PAGE_COUNT(value) PUSH_MEM_OFFSET(value)
  483. #define PUSH_CSP(_label_type, param_cell_num, cell_num, _target_addr) \
  484. do { \
  485. bh_assert(frame_csp < frame->csp_boundary); \
  486. SET_LABEL_TYPE(_label_type); \
  487. frame_csp->cell_num = cell_num; \
  488. frame_csp->begin_addr = frame_ip; \
  489. frame_csp->target_addr = _target_addr; \
  490. frame_csp->frame_sp = frame_sp - param_cell_num; \
  491. frame_csp++; \
  492. } while (0)
  493. #define POP_I32() (--frame_sp, *(int32 *)frame_sp)
  494. #define POP_F32() (--frame_sp, *(float32 *)frame_sp)
  495. #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp))
  496. #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp))
  497. #if UINTPTR_MAX == UINT64_MAX
  498. #define POP_REF() \
  499. (frame_sp -= 2, frame_ref_tmp = FRAME_REF(frame_sp), \
  500. *frame_ref_tmp = *(frame_ref_tmp + 1) = 0, GET_REF_FROM_ADDR(frame_sp))
  501. #else
  502. #define POP_REF() \
  503. (frame_sp--, frame_ref_tmp = FRAME_REF(frame_sp), *frame_ref_tmp = 0, \
  504. GET_REF_FROM_ADDR(frame_sp))
  505. #endif
  506. #if WASM_ENABLE_MEMORY64 != 0
  507. #define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : (uint32)POP_I32())
  508. #define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : (uint32)POP_I32())
  509. #else
  510. #define POP_MEM_OFFSET() POP_I32()
  511. #define POP_TBL_ELEM_IDX() POP_I32()
  512. #endif
  513. #define POP_PAGE_COUNT() POP_MEM_OFFSET()
  514. #define POP_CSP_CHECK_OVERFLOW(n) \
  515. do { \
  516. bh_assert(frame_csp - n >= frame->csp_bottom); \
  517. } while (0)
  518. #define POP_CSP() \
  519. do { \
  520. POP_CSP_CHECK_OVERFLOW(1); \
  521. --frame_csp; \
  522. } while (0)
  523. #define POP_CSP_N(n) \
  524. do { \
  525. uint32 *frame_sp_old = frame_sp; \
  526. uint32 cell_num_to_copy; \
  527. POP_CSP_CHECK_OVERFLOW(n + 1); \
  528. frame_csp -= n; \
  529. frame_ip = (frame_csp - 1)->target_addr; \
  530. /* copy arity values of block */ \
  531. frame_sp = (frame_csp - 1)->frame_sp; \
  532. cell_num_to_copy = (frame_csp - 1)->cell_num; \
  533. if (cell_num_to_copy > 0) { \
  534. word_copy(frame_sp, frame_sp_old - cell_num_to_copy, \
  535. cell_num_to_copy); \
  536. frame_ref_copy(FRAME_REF(frame_sp), \
  537. FRAME_REF(frame_sp_old - cell_num_to_copy), \
  538. cell_num_to_copy); \
  539. } \
  540. frame_sp += cell_num_to_copy; \
  541. CLEAR_FRAME_REF(frame_sp, frame_sp_old - frame_sp); \
  542. } while (0)
  543. /* Pop the given number of elements from the given frame's stack. */
  544. #define POP(N) \
  545. do { \
  546. int n = (N); \
  547. frame_sp -= n; \
  548. CLEAR_FRAME_REF(frame_sp, n); \
  549. } while (0)
  550. #if WASM_ENABLE_EXCE_HANDLING != 0
  551. /* unwind the CSP to a given label and optionally modify the labeltype */
  552. #define UNWIND_CSP(N, T) \
  553. do { \
  554. /* unwind to function frame */ \
  555. frame_csp -= N; \
  556. /* drop handlers and values pushd in try block */ \
  557. frame_sp = (frame_csp - 1)->frame_sp; \
  558. (frame_csp - 1)->label_type = T ? T : (frame_csp - 1)->label_type; \
  559. } while (0)
  560. #endif
  561. #define SYNC_ALL_TO_FRAME() \
  562. do { \
  563. frame->sp = frame_sp; \
  564. frame->ip = frame_ip; \
  565. frame->csp = frame_csp; \
  566. } while (0)
  567. #define UPDATE_ALL_FROM_FRAME() \
  568. do { \
  569. frame_sp = frame->sp; \
  570. frame_ip = frame->ip; \
  571. frame_csp = frame->csp; \
  572. } while (0)
  573. #define read_leb_int64(p, p_end, res) \
  574. do { \
  575. uint8 _val = *p; \
  576. if (!(_val & 0x80)) { \
  577. res = (int64)_val; \
  578. if (_val & 0x40) \
  579. /* sign extend */ \
  580. res |= 0xFFFFFFFFFFFFFF80LL; \
  581. p++; \
  582. } \
  583. else { \
  584. uint32 _off = 0; \
  585. res = (int64)read_leb(p, &_off, 64, true); \
  586. p += _off; \
  587. } \
  588. } while (0)
  589. #define read_leb_uint32(p, p_end, res) \
  590. do { \
  591. uint8 _val = *p; \
  592. if (!(_val & 0x80)) { \
  593. res = _val; \
  594. p++; \
  595. } \
  596. else { \
  597. uint32 _off = 0; \
  598. res = (uint32)read_leb(p, &_off, 32, false); \
  599. p += _off; \
  600. } \
  601. } while (0)
  602. #define read_leb_int32(p, p_end, res) \
  603. do { \
  604. uint8 _val = *p; \
  605. if (!(_val & 0x80)) { \
  606. res = (int32)_val; \
  607. if (_val & 0x40) \
  608. /* sign extend */ \
  609. res |= 0xFFFFFF80; \
  610. p++; \
  611. } \
  612. else { \
  613. uint32 _off = 0; \
  614. res = (int32)read_leb(p, &_off, 32, true); \
  615. p += _off; \
  616. } \
  617. } while (0)
  618. #if WASM_ENABLE_MEMORY64 != 0
  619. #define read_leb_mem_offset(p, p_end, res) \
  620. do { \
  621. uint8 _val = *p; \
  622. if (!(_val & 0x80)) { \
  623. res = (mem_offset_t)_val; \
  624. p++; \
  625. } \
  626. else { \
  627. uint32 _off = 0; \
  628. res = (mem_offset_t)read_leb(p, &_off, is_memory64 ? 64 : 32, \
  629. false); \
  630. p += _off; \
  631. } \
  632. } while (0)
  633. #else
  634. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  635. #endif
  636. #if WASM_ENABLE_MULTI_MEMORY != 0
  637. /* If the current memidx differs than the last cached one,
  638. * update memory related information */
  639. #define read_leb_memidx(p, p_end, res) \
  640. do { \
  641. read_leb_uint32(p, p_end, res); \
  642. if (res != memidx_cached) { \
  643. memory = wasm_get_memory_with_idx(module, res); \
  644. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory); \
  645. memidx_cached = res; \
  646. } \
  647. } while (0)
  648. /* First read the alignment, then if it has flag indicating following memidx,
  649. * read and update memory related information, if it differs than the
  650. * last(cached) one. If it doesn't have flag reset the
  651. * memory instance to the default memories[0] */
  652. #define read_leb_memarg(p, p_end, res) \
  653. do { \
  654. read_leb_uint32(p, p_end, res); \
  655. if (!(res & OPT_MEMIDX_FLAG)) \
  656. memidx = 0; \
  657. else \
  658. read_leb_uint32(p, p_end, memidx); \
  659. if (memidx != memidx_cached) { \
  660. memory = wasm_get_memory_with_idx(module, memidx); \
  661. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory); \
  662. memidx_cached = memidx; \
  663. } \
  664. } while (0)
  665. #else
  666. #define read_leb_memarg(p, p_end, res) \
  667. do { \
  668. read_leb_uint32(p, p_end, res); \
  669. (void)res; \
  670. } while (0)
  671. #define read_leb_memidx(p, p_end, res) read_leb_memarg(p, p_end, res)
  672. #endif
  673. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  674. #define RECOVER_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  675. #else
  676. #define RECOVER_FRAME_IP_END() (void)0
  677. #endif
  678. #if WASM_ENABLE_GC != 0
  679. #define RECOVER_FRAME_REF() frame_ref = (uint8 *)frame->csp_boundary
  680. #else
  681. #define RECOVER_FRAME_REF() (void)0
  682. #endif
  683. #define RECOVER_CONTEXT(new_frame) \
  684. do { \
  685. frame = (new_frame); \
  686. cur_func = frame->function; \
  687. prev_frame = frame->prev_frame; \
  688. frame_ip = frame->ip; \
  689. RECOVER_FRAME_IP_END(); \
  690. frame_lp = frame->lp; \
  691. frame_sp = frame->sp; \
  692. frame_csp = frame->csp; \
  693. RECOVER_FRAME_REF(); \
  694. } while (0)
  695. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  696. #define GET_OPCODE() opcode = *(frame_ip - 1);
  697. #else
  698. #define GET_OPCODE() (void)0
  699. #endif
  700. #define DEF_OP_I_CONST(ctype, src_op_type) \
  701. do { \
  702. ctype cval; \
  703. read_leb_##ctype(frame_ip, frame_ip_end, cval); \
  704. PUSH_##src_op_type(cval); \
  705. } while (0)
  706. #define DEF_OP_EQZ(src_op_type) \
  707. do { \
  708. int32 pop_val; \
  709. pop_val = POP_##src_op_type() == 0; \
  710. PUSH_I32(pop_val); \
  711. } while (0)
  712. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  713. do { \
  714. uint32 res; \
  715. src_type val1, val2; \
  716. val2 = (src_type)POP_##src_op_type(); \
  717. val1 = (src_type)POP_##src_op_type(); \
  718. res = val1 cond val2; \
  719. PUSH_I32(res); \
  720. } while (0)
  721. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  722. do { \
  723. src_type val1, val2; \
  724. val1 = (src_type)POP_##src_op_type(); \
  725. val2 = (src_type)operation(val1); \
  726. PUSH_##src_op_type(val2); \
  727. } while (0)
  728. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  729. do { \
  730. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  731. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  732. operation## = *(src_type2 *)(frame_sp); \
  733. } while (0)
  734. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  735. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  736. #else
  737. #define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) \
  738. do { \
  739. src_type1 val1; \
  740. src_type2 val2; \
  741. frame_sp -= 2; \
  742. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  743. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  744. val1 operation## = val2; \
  745. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  746. } while (0)
  747. #endif
  748. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  749. do { \
  750. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  751. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  752. operation## = (*(src_type2 *)(frame_sp) % 32); \
  753. } while (0)
  754. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  755. do { \
  756. src_type1 val1; \
  757. src_type2 val2; \
  758. frame_sp -= 2; \
  759. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  760. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  761. val1 operation## = (val2 % 64); \
  762. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  763. } while (0)
  764. #define DEF_OP_MATH(src_type, src_op_type, method) \
  765. do { \
  766. src_type src_val; \
  767. src_val = POP_##src_op_type(); \
  768. PUSH_##src_op_type(method(src_val)); \
  769. } while (0)
  770. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  771. static dst_type func_name(src_type src_value, src_type src_min, \
  772. src_type src_max, dst_type dst_min, \
  773. dst_type dst_max, bool is_sign) \
  774. { \
  775. dst_type dst_value = 0; \
  776. if (!isnan(src_value)) { \
  777. if (src_value <= src_min) \
  778. dst_value = dst_min; \
  779. else if (src_value >= src_max) \
  780. dst_value = dst_max; \
  781. else { \
  782. if (is_sign) \
  783. dst_value = (dst_type)(signed_type)src_value; \
  784. else \
  785. dst_value = (dst_type)src_value; \
  786. } \
  787. } \
  788. return dst_value; \
  789. }
  790. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  791. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  792. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  793. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  794. static bool
  795. trunc_f32_to_int(WASMModuleInstance *module, uint32 *frame_sp, float32 src_min,
  796. float32 src_max, bool saturating, bool is_i32, bool is_sign)
  797. {
  798. float32 src_value = POP_F32();
  799. uint64 dst_value_i64;
  800. uint32 dst_value_i32;
  801. if (!saturating) {
  802. if (isnan(src_value)) {
  803. wasm_set_exception(module, "invalid conversion to integer");
  804. return false;
  805. }
  806. else if (src_value <= src_min || src_value >= src_max) {
  807. wasm_set_exception(module, "integer overflow");
  808. return false;
  809. }
  810. }
  811. if (is_i32) {
  812. uint32 dst_min = is_sign ? INT32_MIN : 0;
  813. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  814. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  815. dst_max, is_sign);
  816. PUSH_I32(dst_value_i32);
  817. }
  818. else {
  819. uint64 dst_min = is_sign ? INT64_MIN : 0;
  820. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  821. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  822. dst_max, is_sign);
  823. PUSH_I64(dst_value_i64);
  824. }
  825. return true;
  826. }
  827. static bool
  828. trunc_f64_to_int(WASMModuleInstance *module, uint32 *frame_sp, float64 src_min,
  829. float64 src_max, bool saturating, bool is_i32, bool is_sign)
  830. {
  831. float64 src_value = POP_F64();
  832. uint64 dst_value_i64;
  833. uint32 dst_value_i32;
  834. if (!saturating) {
  835. if (isnan(src_value)) {
  836. wasm_set_exception(module, "invalid conversion to integer");
  837. return false;
  838. }
  839. else if (src_value <= src_min || src_value >= src_max) {
  840. wasm_set_exception(module, "integer overflow");
  841. return false;
  842. }
  843. }
  844. if (is_i32) {
  845. uint32 dst_min = is_sign ? INT32_MIN : 0;
  846. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  847. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  848. dst_max, is_sign);
  849. PUSH_I32(dst_value_i32);
  850. }
  851. else {
  852. uint64 dst_min = is_sign ? INT64_MIN : 0;
  853. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  854. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  855. dst_max, is_sign);
  856. PUSH_I64(dst_value_i64);
  857. }
  858. return true;
  859. }
  860. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  861. do { \
  862. if (!trunc_f32_to_int(module, frame_sp, min, max, false, is_i32, \
  863. is_sign)) \
  864. goto got_exception; \
  865. } while (0)
  866. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  867. do { \
  868. if (!trunc_f64_to_int(module, frame_sp, min, max, false, is_i32, \
  869. is_sign)) \
  870. goto got_exception; \
  871. } while (0)
  872. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  873. do { \
  874. (void)trunc_f32_to_int(module, frame_sp, min, max, true, is_i32, \
  875. is_sign); \
  876. } while (0)
  877. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  878. do { \
  879. (void)trunc_f64_to_int(module, frame_sp, min, max, true, is_i32, \
  880. is_sign); \
  881. } while (0)
  882. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  883. do { \
  884. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  885. PUSH_##dst_op_type(value); \
  886. } while (0)
  887. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  888. do { \
  889. uint32 param_count = cur_func->param_count; \
  890. read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
  891. bh_assert(local_idx < param_count + cur_func->local_count); \
  892. local_offset = cur_func->local_offsets[local_idx]; \
  893. if (local_idx < param_count) \
  894. local_type = cur_func->param_types[local_idx]; \
  895. else \
  896. local_type = cur_func->local_types[local_idx - param_count]; \
  897. } while (0)
  898. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  899. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  900. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  901. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  902. { \
  903. uint32 readv, sval; \
  904. \
  905. sval = POP_I32(); \
  906. addr = POP_MEM_OFFSET(); \
  907. \
  908. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  909. CHECK_MEMORY_OVERFLOW(1); \
  910. CHECK_ATOMIC_MEMORY_ACCESS(); \
  911. \
  912. shared_memory_lock(memory); \
  913. readv = (uint32)(*(uint8 *)maddr); \
  914. *(uint8 *)maddr = (uint8)(readv op sval); \
  915. shared_memory_unlock(memory); \
  916. } \
  917. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  918. CHECK_MEMORY_OVERFLOW(2); \
  919. CHECK_ATOMIC_MEMORY_ACCESS(); \
  920. \
  921. shared_memory_lock(memory); \
  922. readv = (uint32)LOAD_U16(maddr); \
  923. STORE_U16(maddr, (uint16)(readv op sval)); \
  924. shared_memory_unlock(memory); \
  925. } \
  926. else { \
  927. CHECK_MEMORY_OVERFLOW(4); \
  928. CHECK_ATOMIC_MEMORY_ACCESS(); \
  929. \
  930. shared_memory_lock(memory); \
  931. readv = LOAD_I32(maddr); \
  932. STORE_U32(maddr, readv op sval); \
  933. shared_memory_unlock(memory); \
  934. } \
  935. PUSH_I32(readv); \
  936. break; \
  937. } \
  938. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  939. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  940. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  941. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  942. { \
  943. uint64 readv, sval; \
  944. \
  945. sval = (uint64)POP_I64(); \
  946. addr = POP_MEM_OFFSET(); \
  947. \
  948. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  949. CHECK_MEMORY_OVERFLOW(1); \
  950. CHECK_ATOMIC_MEMORY_ACCESS(); \
  951. \
  952. shared_memory_lock(memory); \
  953. readv = (uint64)(*(uint8 *)maddr); \
  954. *(uint8 *)maddr = (uint8)(readv op sval); \
  955. shared_memory_unlock(memory); \
  956. } \
  957. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  958. CHECK_MEMORY_OVERFLOW(2); \
  959. CHECK_ATOMIC_MEMORY_ACCESS(); \
  960. \
  961. shared_memory_lock(memory); \
  962. readv = (uint64)LOAD_U16(maddr); \
  963. STORE_U16(maddr, (uint16)(readv op sval)); \
  964. shared_memory_unlock(memory); \
  965. } \
  966. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  967. CHECK_MEMORY_OVERFLOW(4); \
  968. CHECK_ATOMIC_MEMORY_ACCESS(); \
  969. \
  970. shared_memory_lock(memory); \
  971. readv = (uint64)LOAD_U32(maddr); \
  972. STORE_U32(maddr, (uint32)(readv op sval)); \
  973. shared_memory_unlock(memory); \
  974. } \
  975. else { \
  976. uint64 op_result; \
  977. CHECK_MEMORY_OVERFLOW(8); \
  978. CHECK_ATOMIC_MEMORY_ACCESS(); \
  979. \
  980. shared_memory_lock(memory); \
  981. readv = (uint64)LOAD_I64(maddr); \
  982. op_result = readv op sval; \
  983. STORE_I64(maddr, op_result); \
  984. shared_memory_unlock(memory); \
  985. } \
  986. PUSH_I64(readv); \
  987. break; \
  988. }
  989. static inline int32
  990. sign_ext_8_32(int8 val)
  991. {
  992. if (val & 0x80)
  993. return (int32)val | (int32)0xffffff00;
  994. return val;
  995. }
  996. static inline int32
  997. sign_ext_16_32(int16 val)
  998. {
  999. if (val & 0x8000)
  1000. return (int32)val | (int32)0xffff0000;
  1001. return val;
  1002. }
  1003. static inline int64
  1004. sign_ext_8_64(int8 val)
  1005. {
  1006. if (val & 0x80)
  1007. return (int64)val | (int64)0xffffffffffffff00LL;
  1008. return val;
  1009. }
  1010. static inline int64
  1011. sign_ext_16_64(int16 val)
  1012. {
  1013. if (val & 0x8000)
  1014. return (int64)val | (int64)0xffffffffffff0000LL;
  1015. return val;
  1016. }
  1017. static inline int64
  1018. sign_ext_32_64(int32 val)
  1019. {
  1020. if (val & (int32)0x80000000)
  1021. return (int64)val | (int64)0xffffffff00000000LL;
  1022. return val;
  1023. }
  1024. static inline void
  1025. word_copy(uint32 *dest, uint32 *src, unsigned num)
  1026. {
  1027. bh_assert(dest != NULL);
  1028. bh_assert(src != NULL);
  1029. bh_assert(num > 0);
  1030. if (dest != src) {
  1031. /* No overlap buffer */
  1032. bh_assert(!((src < dest) && (dest < src + num)));
  1033. for (; num > 0; num--)
  1034. *dest++ = *src++;
  1035. }
  1036. }
  1037. #if WASM_ENABLE_GC != 0
  1038. static inline void
  1039. frame_ref_copy(uint8 *frame_ref_dest, uint8 *frame_ref_src, unsigned num)
  1040. {
  1041. if (frame_ref_dest != frame_ref_src)
  1042. for (; num > 0; num--)
  1043. *frame_ref_dest++ = *frame_ref_src++;
  1044. }
  1045. #else
  1046. #define frame_ref_copy(frame_ref_dst, frame_ref_src, num) (void)0
  1047. #endif
  1048. static inline WASMInterpFrame *
  1049. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  1050. {
  1051. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  1052. if (frame) {
  1053. frame->prev_frame = prev_frame;
  1054. #if WASM_ENABLE_PERF_PROFILING != 0
  1055. frame->time_started = os_time_thread_cputime_us();
  1056. #endif
  1057. }
  1058. else {
  1059. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  1060. "wasm operand stack overflow");
  1061. }
  1062. return frame;
  1063. }
  1064. static inline void
  1065. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  1066. {
  1067. #if WASM_ENABLE_PERF_PROFILING != 0
  1068. if (frame->function) {
  1069. WASMInterpFrame *prev_frame = frame->prev_frame;
  1070. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  1071. frame->function->total_exec_time += time_elapsed;
  1072. frame->function->total_exec_cnt++;
  1073. if (prev_frame && prev_frame->function)
  1074. prev_frame->function->children_exec_time += time_elapsed;
  1075. }
  1076. #endif
  1077. wasm_exec_env_free_wasm_frame(exec_env, frame);
  1078. }
  1079. static void
  1080. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  1081. WASMExecEnv *exec_env,
  1082. WASMFunctionInstance *cur_func,
  1083. WASMInterpFrame *prev_frame)
  1084. {
  1085. WASMFunctionImport *func_import = cur_func->u.func_import;
  1086. CApiFuncImport *c_api_func_import = NULL;
  1087. unsigned local_cell_num =
  1088. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  1089. unsigned all_cell_num;
  1090. WASMInterpFrame *frame;
  1091. uint32 argv_ret[2], cur_func_index;
  1092. void *native_func_pointer = NULL;
  1093. char buf[128];
  1094. bool ret;
  1095. #if WASM_ENABLE_GC != 0
  1096. WASMFuncType *func_type;
  1097. uint8 *frame_ref;
  1098. #endif
  1099. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1100. return;
  1101. }
  1102. all_cell_num = local_cell_num;
  1103. #if WASM_ENABLE_GC != 0
  1104. all_cell_num += (local_cell_num + 3) / 4;
  1105. #endif
  1106. if (!(frame =
  1107. ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
  1108. prev_frame)))
  1109. return;
  1110. frame->function = cur_func;
  1111. frame->ip = NULL;
  1112. frame->sp = frame->lp + local_cell_num;
  1113. #if WASM_ENABLE_GC != 0
  1114. /* native function doesn't have operand stack and label stack */
  1115. frame_ref = (uint8 *)frame->sp;
  1116. init_frame_refs(frame_ref, local_cell_num, cur_func);
  1117. #endif
  1118. wasm_exec_env_set_cur_frame(exec_env, frame);
  1119. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  1120. bh_assert(cur_func_index < module_inst->module->import_function_count);
  1121. if (!func_import->call_conv_wasm_c_api) {
  1122. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  1123. }
  1124. else if (module_inst->c_api_func_imports) {
  1125. c_api_func_import = module_inst->c_api_func_imports + cur_func_index;
  1126. native_func_pointer = c_api_func_import->func_ptr_linked;
  1127. }
  1128. if (!native_func_pointer) {
  1129. snprintf(buf, sizeof(buf),
  1130. "failed to call unlinked import function (%s, %s)",
  1131. func_import->module_name, func_import->field_name);
  1132. wasm_set_exception(module_inst, buf);
  1133. return;
  1134. }
  1135. if (func_import->call_conv_wasm_c_api) {
  1136. ret = wasm_runtime_invoke_c_api_native(
  1137. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  1138. func_import->func_type, cur_func->param_cell_num, frame->lp,
  1139. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  1140. if (ret) {
  1141. argv_ret[0] = frame->lp[0];
  1142. argv_ret[1] = frame->lp[1];
  1143. }
  1144. }
  1145. else if (!func_import->call_conv_raw) {
  1146. ret = wasm_runtime_invoke_native(
  1147. exec_env, native_func_pointer, func_import->func_type,
  1148. func_import->signature, func_import->attachment, frame->lp,
  1149. cur_func->param_cell_num, argv_ret);
  1150. }
  1151. else {
  1152. ret = wasm_runtime_invoke_native_raw(
  1153. exec_env, native_func_pointer, func_import->func_type,
  1154. func_import->signature, func_import->attachment, frame->lp,
  1155. cur_func->param_cell_num, argv_ret);
  1156. }
  1157. if (!ret)
  1158. return;
  1159. #if WASM_ENABLE_GC != 0
  1160. func_type = cur_func->u.func_import->func_type;
  1161. if (func_type->result_count
  1162. && wasm_is_type_reftype(func_type->types[cur_func->param_count])) {
  1163. frame_ref = (uint8 *)prev_frame->csp_boundary
  1164. + (unsigned)(uintptr_t)(prev_frame->sp - prev_frame->lp);
  1165. if (!wasm_is_reftype_i31ref(func_type->types[cur_func->param_count])) {
  1166. #if UINTPTR_MAX == UINT64_MAX
  1167. *frame_ref = *(frame_ref + 1) = 1;
  1168. #else
  1169. *frame_ref = 1;
  1170. #endif
  1171. }
  1172. }
  1173. #endif
  1174. if (cur_func->ret_cell_num == 1) {
  1175. prev_frame->sp[0] = argv_ret[0];
  1176. prev_frame->sp++;
  1177. }
  1178. else if (cur_func->ret_cell_num == 2) {
  1179. prev_frame->sp[0] = argv_ret[0];
  1180. prev_frame->sp[1] = argv_ret[1];
  1181. prev_frame->sp += 2;
  1182. }
  1183. FREE_FRAME(exec_env, frame);
  1184. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  1185. }
  1186. #if WASM_ENABLE_FAST_JIT != 0
  1187. bool
  1188. fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  1189. WASMInterpFrame *prev_frame)
  1190. {
  1191. WASMModuleInstance *module_inst =
  1192. (WASMModuleInstance *)exec_env->module_inst;
  1193. WASMFunctionInstance *cur_func = module_inst->e->functions + func_idx;
  1194. wasm_interp_call_func_native(module_inst, exec_env, cur_func, prev_frame);
  1195. return wasm_copy_exception(module_inst, NULL) ? false : true;
  1196. }
  1197. #endif
  1198. #if WASM_ENABLE_MULTI_MODULE != 0
  1199. static void
  1200. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1201. WASMExecEnv *exec_env,
  1202. WASMFunctionInstance *cur_func,
  1203. WASMInterpFrame *prev_frame);
  1204. static void
  1205. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  1206. WASMExecEnv *exec_env,
  1207. WASMFunctionInstance *cur_func,
  1208. WASMInterpFrame *prev_frame)
  1209. {
  1210. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  1211. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  1212. WASMFunctionImport *func_import = cur_func->u.func_import;
  1213. uint8 *ip = prev_frame->ip;
  1214. char buf[128];
  1215. WASMExecEnv *sub_module_exec_env = NULL;
  1216. uintptr_t aux_stack_origin_boundary = 0;
  1217. uintptr_t aux_stack_origin_bottom = 0;
  1218. /*
  1219. * perform stack overflow check before calling
  1220. * wasm_interp_call_func_bytecode recursively.
  1221. */
  1222. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1223. return;
  1224. }
  1225. if (!sub_func_inst) {
  1226. snprintf(buf, sizeof(buf),
  1227. "failed to call unlinked import function (%s, %s)",
  1228. func_import->module_name, func_import->field_name);
  1229. wasm_set_exception(module_inst, buf);
  1230. return;
  1231. }
  1232. /* Switch exec_env but keep using the same one by replacing necessary
  1233. * variables */
  1234. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  1235. (WASMModuleInstanceCommon *)sub_module_inst);
  1236. if (!sub_module_exec_env) {
  1237. wasm_set_exception(module_inst, "create singleton exec_env failed");
  1238. return;
  1239. }
  1240. /* - module_inst */
  1241. wasm_exec_env_set_module_inst(exec_env,
  1242. (WASMModuleInstanceCommon *)sub_module_inst);
  1243. /* - aux_stack_boundary */
  1244. aux_stack_origin_boundary = exec_env->aux_stack_boundary;
  1245. exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
  1246. /* - aux_stack_bottom */
  1247. aux_stack_origin_bottom = exec_env->aux_stack_bottom;
  1248. exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
  1249. /* set ip NULL to make call_func_bytecode return after executing
  1250. this function */
  1251. prev_frame->ip = NULL;
  1252. /* call function of sub-module*/
  1253. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  1254. prev_frame);
  1255. /* restore ip and other replaced */
  1256. prev_frame->ip = ip;
  1257. exec_env->aux_stack_boundary = aux_stack_origin_boundary;
  1258. exec_env->aux_stack_bottom = aux_stack_origin_bottom;
  1259. wasm_exec_env_restore_module_inst(exec_env,
  1260. (WASMModuleInstanceCommon *)module_inst);
  1261. }
  1262. #endif
  1263. #if WASM_ENABLE_THREAD_MGR != 0
  1264. #if WASM_ENABLE_DEBUG_INTERP != 0
  1265. #define CHECK_SUSPEND_FLAGS() \
  1266. do { \
  1267. os_mutex_lock(&exec_env->wait_lock); \
  1268. if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \
  1269. os_mutex_unlock(&exec_env->wait_lock); \
  1270. return; \
  1271. } \
  1272. if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \
  1273. SYNC_ALL_TO_FRAME(); \
  1274. wasm_cluster_thread_waiting_run(exec_env); \
  1275. } \
  1276. os_mutex_unlock(&exec_env->wait_lock); \
  1277. } while (0)
  1278. #else
  1279. #if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0
  1280. /* The lock is only needed when the suspend_flags is atomic; otherwise
  1281. the lock is already taken at the time when SUSPENSION_LOCK() is called. */
  1282. #define SUSPENSION_LOCK() os_mutex_lock(&exec_env->wait_lock);
  1283. #define SUSPENSION_UNLOCK() os_mutex_unlock(&exec_env->wait_lock);
  1284. #else
  1285. #define SUSPENSION_LOCK()
  1286. #define SUSPENSION_UNLOCK()
  1287. #endif
  1288. #define CHECK_SUSPEND_FLAGS() \
  1289. do { \
  1290. WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
  1291. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1292. & WASM_SUSPEND_FLAG_TERMINATE) { \
  1293. /* terminate current thread */ \
  1294. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1295. return; \
  1296. } \
  1297. while (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1298. & WASM_SUSPEND_FLAG_SUSPEND) { \
  1299. /* suspend current thread */ \
  1300. SUSPENSION_LOCK() \
  1301. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock); \
  1302. SUSPENSION_UNLOCK() \
  1303. } \
  1304. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1305. } while (0)
  1306. #endif /* WASM_ENABLE_DEBUG_INTERP */
  1307. #endif /* WASM_ENABLE_THREAD_MGR */
  1308. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1309. #if BH_ATOMIC_32_IS_ATOMIC != 0
  1310. #define GET_SIGNAL_FLAG() \
  1311. do { \
  1312. signal_flag = \
  1313. BH_ATOMIC_32_LOAD(exec_env->current_status->signal_flag); \
  1314. } while (0)
  1315. #else
  1316. #define GET_SIGNAL_FLAG() \
  1317. do { \
  1318. os_mutex_lock(&exec_env->wait_lock); \
  1319. signal_flag = exec_env->current_status->signal_flag; \
  1320. os_mutex_unlock(&exec_env->wait_lock); \
  1321. } while (0)
  1322. #endif
  1323. #endif
  1324. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1325. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1326. #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++]
  1327. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1328. #define HANDLE_OP_END() \
  1329. do { \
  1330. /* Record the current frame_ip, so when exception occurs, \
  1331. debugger can know the exact opcode who caused the exception */ \
  1332. frame_ip_orig = frame_ip; \
  1333. /* Atomic load the exec_env's signal_flag first, and then handle \
  1334. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1335. GET_SIGNAL_FLAG(); \
  1336. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1337. os_mutex_lock(&exec_env->wait_lock); \
  1338. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1339. && exec_env->current_status->step_count++ == 1) { \
  1340. exec_env->current_status->step_count = 0; \
  1341. SYNC_ALL_TO_FRAME(); \
  1342. wasm_cluster_thread_waiting_run(exec_env); \
  1343. } \
  1344. os_mutex_unlock(&exec_env->wait_lock); \
  1345. } \
  1346. CHECK_INSTRUCTION_LIMIT(); \
  1347. goto *handle_table[*frame_ip++]; \
  1348. } while (0)
  1349. #else
  1350. #define HANDLE_OP_END() \
  1351. CHECK_INSTRUCTION_LIMIT(); \
  1352. FETCH_OPCODE_AND_DISPATCH()
  1353. #endif
  1354. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1355. #define HANDLE_OP(opcode) case opcode:
  1356. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1357. #define HANDLE_OP_END() \
  1358. /* Record the current frame_ip, so when exception occurs, \
  1359. debugger can know the exact opcode who caused the exception */ \
  1360. frame_ip_orig = frame_ip; \
  1361. /* Atomic load the exec_env's signal_flag first, and then handle \
  1362. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1363. GET_SIGNAL_FLAG(); \
  1364. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1365. os_mutex_lock(&exec_env->wait_lock); \
  1366. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1367. && exec_env->current_status->step_count++ == 1) { \
  1368. exec_env->current_status->step_count = 0; \
  1369. SYNC_ALL_TO_FRAME(); \
  1370. wasm_cluster_thread_waiting_run(exec_env); \
  1371. } \
  1372. os_mutex_unlock(&exec_env->wait_lock); \
  1373. } \
  1374. CHECK_INSTRUCTION_LIMIT(); \
  1375. continue;
  1376. #else
  1377. #define HANDLE_OP_END() \
  1378. CHECK_INSTRUCTION_LIMIT(); \
  1379. continue;
  1380. #endif
  1381. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1382. static inline uint8 *
  1383. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1384. {
  1385. #if WASM_ENABLE_MULTI_MODULE == 0
  1386. return global_data + global->data_offset;
  1387. #else
  1388. return global->import_global_inst
  1389. ? global->import_module_inst->global_data
  1390. + global->import_global_inst->data_offset
  1391. : global_data + global->data_offset;
  1392. #endif
  1393. }
  1394. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  1395. #define CHECK_INSTRUCTION_LIMIT() \
  1396. if (instructions_left == 0) { \
  1397. wasm_set_exception(module, "instruction limit exceeded"); \
  1398. goto got_exception; \
  1399. } \
  1400. else if (instructions_left > 0) \
  1401. instructions_left--;
  1402. #else
  1403. #define CHECK_INSTRUCTION_LIMIT() (void)0
  1404. #endif
  1405. static void
  1406. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1407. WASMExecEnv *exec_env,
  1408. WASMFunctionInstance *cur_func,
  1409. WASMInterpFrame *prev_frame)
  1410. {
  1411. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1412. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1413. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1414. || WASM_ENABLE_BULK_MEMORY != 0
  1415. uint64 linear_mem_size = 0;
  1416. if (memory)
  1417. #if WASM_ENABLE_THREAD_MGR == 0
  1418. linear_mem_size = memory->memory_data_size;
  1419. #else
  1420. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1421. #endif
  1422. #endif
  1423. WASMFuncType **wasm_types = (WASMFuncType **)module->module->types;
  1424. WASMGlobalInstance *globals = module->e->globals, *global;
  1425. uint8 *global_data = module->global_data;
  1426. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1427. WASMInterpFrame *frame = NULL;
  1428. /* Points to this special opcode so as to jump to the
  1429. * call_method_from_entry. */
  1430. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1431. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1432. register uint32 *frame_sp = NULL; /* cache of frame->sp */
  1433. #if WASM_ENABLE_GC != 0
  1434. register uint8 *frame_ref = NULL; /* cache of frame->ref */
  1435. uint8 *frame_ref_tmp;
  1436. #endif
  1437. WASMBranchBlock *frame_csp = NULL;
  1438. BlockAddr *cache_items;
  1439. uint8 *frame_ip_end = frame_ip + 1;
  1440. uint8 opcode;
  1441. uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0;
  1442. uint32 all_cell_num = 0;
  1443. tbl_elem_idx_t val;
  1444. uint8 *else_addr, *end_addr, *maddr = NULL;
  1445. uint32 local_idx, local_offset, global_idx;
  1446. uint8 local_type, *global_addr;
  1447. uint32 cache_index, type_index, param_cell_num, cell_num;
  1448. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  1449. int instructions_left = -1;
  1450. if (exec_env) {
  1451. instructions_left = exec_env->instructions_to_execute;
  1452. }
  1453. #endif
  1454. #if WASM_ENABLE_EXCE_HANDLING != 0
  1455. int32_t exception_tag_index;
  1456. #endif
  1457. uint8 value_type;
  1458. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1459. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1460. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  1461. bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
  1462. (WASMModuleInstanceCommon *)module);
  1463. #else
  1464. bool disable_bounds_checks = false;
  1465. #endif
  1466. #endif
  1467. #if WASM_ENABLE_GC != 0
  1468. WASMObjectRef gc_obj;
  1469. WASMStructObjectRef struct_obj;
  1470. WASMArrayObjectRef array_obj;
  1471. WASMFuncObjectRef func_obj;
  1472. WASMI31ObjectRef i31_obj;
  1473. WASMExternrefObjectRef externref_obj;
  1474. #if WASM_ENABLE_STRINGREF != 0
  1475. WASMString str_obj = NULL;
  1476. WASMStringrefObjectRef stringref_obj;
  1477. WASMStringviewWTF8ObjectRef stringview_wtf8_obj;
  1478. WASMStringviewWTF16ObjectRef stringview_wtf16_obj;
  1479. WASMStringviewIterObjectRef stringview_iter_obj;
  1480. #endif
  1481. #endif
  1482. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  1483. bool is_return_call = false;
  1484. #endif
  1485. #if WASM_ENABLE_MEMORY64 != 0
  1486. /* TODO: multi-memories for now assuming the memory idx type is consistent
  1487. * across multi-memories */
  1488. bool is_memory64 = false;
  1489. bool is_table64 = false;
  1490. if (memory)
  1491. is_memory64 = memory->is_memory64;
  1492. #endif
  1493. #if WASM_ENABLE_MULTI_MEMORY != 0
  1494. uint32 memidx = 0;
  1495. uint32 memidx_cached = (uint32)-1;
  1496. #endif
  1497. #if WASM_ENABLE_DEBUG_INTERP != 0
  1498. uint8 *frame_ip_orig = NULL;
  1499. WASMDebugInstance *debug_instance = wasm_exec_env_get_instance(exec_env);
  1500. bh_list *watch_point_list_read =
  1501. debug_instance ? &debug_instance->watch_point_list_read : NULL;
  1502. bh_list *watch_point_list_write =
  1503. debug_instance ? &debug_instance->watch_point_list_write : NULL;
  1504. #if WASM_ENABLE_THREAD_MGR != 0
  1505. uint32 signal_flag;
  1506. #endif
  1507. #endif
  1508. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1509. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1510. DEFINE_GOTO_TABLE(const void *, handle_table);
  1511. #undef HANDLE_OPCODE
  1512. #endif
  1513. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1514. while (frame_ip < frame_ip_end) {
  1515. opcode = *frame_ip++;
  1516. switch (opcode) {
  1517. #else
  1518. FETCH_OPCODE_AND_DISPATCH();
  1519. #endif
  1520. /* control instructions */
  1521. HANDLE_OP(WASM_OP_UNREACHABLE)
  1522. {
  1523. wasm_set_exception(module, "unreachable");
  1524. goto got_exception;
  1525. }
  1526. HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); }
  1527. #if WASM_ENABLE_EXCE_HANDLING != 0
  1528. HANDLE_OP(WASM_OP_RETHROW)
  1529. {
  1530. int32_t relative_depth;
  1531. read_leb_int32(frame_ip, frame_ip_end, relative_depth);
  1532. /* No frame found with exception handler; validation should
  1533. * catch it */
  1534. bh_assert(frame_csp >= frame->csp_bottom + relative_depth);
  1535. /* go up the frame stack */
  1536. WASMBranchBlock *tgtframe = (frame_csp - 1) - relative_depth;
  1537. bh_assert(tgtframe->label_type == LABEL_TYPE_CATCH
  1538. || tgtframe->label_type == LABEL_TYPE_CATCH_ALL);
  1539. /* tgtframe points to the frame containing a thrown
  1540. * exception */
  1541. uint32 *tgtframe_sp = tgtframe->frame_sp;
  1542. /* frame sp of tgtframe points to caught exception */
  1543. exception_tag_index = *((uint32 *)tgtframe_sp);
  1544. tgtframe_sp++;
  1545. /* get tag type */
  1546. uint8 tag_type_index =
  1547. module->module->tags[exception_tag_index]->type;
  1548. uint32 cell_num_to_copy =
  1549. wasm_types[tag_type_index]->param_cell_num;
  1550. /* move exception parameters (if there are any) onto top
  1551. * of stack */
  1552. if (cell_num_to_copy > 0) {
  1553. word_copy(frame_sp, tgtframe_sp - cell_num_to_copy,
  1554. cell_num_to_copy);
  1555. }
  1556. frame_sp += cell_num_to_copy;
  1557. goto find_a_catch_handler;
  1558. }
  1559. HANDLE_OP(WASM_OP_THROW)
  1560. {
  1561. read_leb_int32(frame_ip, frame_ip_end, exception_tag_index);
  1562. /* landing pad for the rethrow ? */
  1563. find_a_catch_handler:
  1564. {
  1565. WASMFuncType *tag_type = NULL;
  1566. uint32 cell_num_to_copy = 0;
  1567. if (IS_INVALID_TAGINDEX(exception_tag_index)) {
  1568. /*
  1569. * invalid exception index,
  1570. * generated if a submodule throws an exception
  1571. * that has not been imported here
  1572. *
  1573. * This should result in a branch to the CATCH_ALL block,
  1574. * if there is one
  1575. */
  1576. tag_type = NULL;
  1577. cell_num_to_copy = 0;
  1578. }
  1579. else {
  1580. if (module->e->tags[exception_tag_index].is_import_tag) {
  1581. tag_type = module->e->tags[exception_tag_index]
  1582. .u.tag_import->tag_type;
  1583. }
  1584. else {
  1585. tag_type = module->e->tags[exception_tag_index]
  1586. .u.tag->tag_type;
  1587. }
  1588. cell_num_to_copy = tag_type->param_cell_num;
  1589. }
  1590. /* browse through frame stack */
  1591. uint32 relative_depth = 0;
  1592. do {
  1593. POP_CSP_CHECK_OVERFLOW(relative_depth - 1);
  1594. WASMBranchBlock *tgtframe = frame_csp - relative_depth - 1;
  1595. switch (tgtframe->label_type) {
  1596. case LABEL_TYPE_BLOCK:
  1597. case LABEL_TYPE_IF:
  1598. case LABEL_TYPE_LOOP:
  1599. case LABEL_TYPE_CATCH:
  1600. case LABEL_TYPE_CATCH_ALL:
  1601. /*
  1602. * skip that blocks in search
  1603. * BLOCK, IF and LOOP do not contain handlers and
  1604. * cannot catch exceptions.
  1605. * blocks marked as CATCH or
  1606. * CATCH_ALL did already caught an exception and can
  1607. * only be a target for RETHROW, but cannot catch an
  1608. * exception again
  1609. */
  1610. break;
  1611. case LABEL_TYPE_TRY:
  1612. {
  1613. uint32 handler_number = 0;
  1614. uint8 **handlers = (uint8 **)tgtframe->frame_sp;
  1615. uint8 *handler = NULL;
  1616. while ((handler = handlers[handler_number]) != 0) {
  1617. uint8 handler_opcode = *handler;
  1618. uint8 *target_addr =
  1619. handler
  1620. + 1; /* first instruction or leb-immediate
  1621. behind the handler opcode */
  1622. switch (handler_opcode) {
  1623. case WASM_OP_CATCH:
  1624. {
  1625. int32 lookup_index = 0;
  1626. /* read the tag_index and advance
  1627. * target_addr to the first instruction
  1628. * in the block */
  1629. read_leb_int32(target_addr, 0,
  1630. lookup_index);
  1631. if (exception_tag_index
  1632. == lookup_index) {
  1633. /* set ip */
  1634. frame_ip = target_addr;
  1635. /* save frame_sp (points to
  1636. * exception values) */
  1637. uint32 *frame_sp_old = frame_sp;
  1638. UNWIND_CSP(relative_depth,
  1639. LABEL_TYPE_CATCH);
  1640. /* push exception_tag_index and
  1641. * exception values for rethrow */
  1642. PUSH_I32(exception_tag_index);
  1643. if (cell_num_to_copy > 0) {
  1644. word_copy(
  1645. frame_sp,
  1646. frame_sp_old
  1647. - cell_num_to_copy,
  1648. cell_num_to_copy);
  1649. frame_sp += cell_num_to_copy;
  1650. /* push exception values for
  1651. * catch
  1652. */
  1653. word_copy(
  1654. frame_sp,
  1655. frame_sp_old
  1656. - cell_num_to_copy,
  1657. cell_num_to_copy);
  1658. frame_sp += cell_num_to_copy;
  1659. }
  1660. /* advance to handler */
  1661. HANDLE_OP_END();
  1662. }
  1663. break;
  1664. }
  1665. case WASM_OP_DELEGATE:
  1666. {
  1667. int32 lookup_depth = 0;
  1668. /* read the depth */
  1669. read_leb_int32(target_addr, 0,
  1670. lookup_depth);
  1671. /* save frame_sp (points to exception
  1672. * values) */
  1673. uint32 *frame_sp_old = frame_sp;
  1674. UNWIND_CSP(relative_depth,
  1675. LABEL_TYPE_CATCH);
  1676. /* leave the block (the delegate is
  1677. * technically not inside the frame) */
  1678. frame_csp--;
  1679. /* unwind to delegated frame */
  1680. frame_csp -= lookup_depth;
  1681. /* push exception values for catch */
  1682. if (cell_num_to_copy > 0) {
  1683. word_copy(frame_sp,
  1684. frame_sp_old
  1685. - cell_num_to_copy,
  1686. cell_num_to_copy);
  1687. frame_sp += cell_num_to_copy;
  1688. }
  1689. /* tag_index is already stored in
  1690. * exception_tag_index */
  1691. goto find_a_catch_handler;
  1692. }
  1693. case WASM_OP_CATCH_ALL:
  1694. {
  1695. /* no immediate */
  1696. /* save frame_sp (points to exception
  1697. * values) */
  1698. uint32 *frame_sp_old = frame_sp;
  1699. /* set ip */
  1700. frame_ip = target_addr;
  1701. UNWIND_CSP(relative_depth,
  1702. LABEL_TYPE_CATCH_ALL);
  1703. /* push exception_tag_index and
  1704. * exception values for rethrow */
  1705. PUSH_I32(exception_tag_index);
  1706. if (cell_num_to_copy > 0) {
  1707. word_copy(frame_sp,
  1708. frame_sp_old
  1709. - cell_num_to_copy,
  1710. cell_num_to_copy);
  1711. frame_sp += cell_num_to_copy;
  1712. }
  1713. /* catch_all has no exception values */
  1714. /* advance to handler */
  1715. HANDLE_OP_END();
  1716. }
  1717. default:
  1718. wasm_set_exception(
  1719. module, "WASM_OP_THROW found "
  1720. "unexpected handler type");
  1721. goto got_exception;
  1722. }
  1723. handler_number++;
  1724. }
  1725. /* exception not caught in this frame */
  1726. break;
  1727. }
  1728. case LABEL_TYPE_FUNCTION:
  1729. {
  1730. /* save frame_sp (points to exception values) */
  1731. uint32 *frame_sp_old = frame_sp;
  1732. UNWIND_CSP(relative_depth, LABEL_TYPE_FUNCTION);
  1733. /* push exception values for catch
  1734. * The values are copied to the CALLER FRAME
  1735. * (prev_frame->sp) same behavior ad WASM_OP_RETURN
  1736. */
  1737. if (cell_num_to_copy > 0) {
  1738. word_copy(prev_frame->sp,
  1739. frame_sp_old - cell_num_to_copy,
  1740. cell_num_to_copy);
  1741. prev_frame->sp += cell_num_to_copy;
  1742. }
  1743. *((int32 *)(prev_frame->sp)) = exception_tag_index;
  1744. prev_frame->sp++;
  1745. /* mark frame as raised exception */
  1746. wasm_set_exception(module,
  1747. "uncaught wasm exception");
  1748. /* end of function, treat as WASM_OP_RETURN */
  1749. goto return_func;
  1750. }
  1751. default:
  1752. wasm_set_exception(
  1753. module,
  1754. "unexpected or invalid label in THROW or "
  1755. "RETHROW when searching a catch handler");
  1756. goto got_exception;
  1757. }
  1758. relative_depth++;
  1759. } while (1);
  1760. }
  1761. /* something went wrong. normally, we should always find the
  1762. * func label. if not, stop the interpreter */
  1763. wasm_set_exception(
  1764. module, "WASM_OP_THROW hit the bottom of the frame stack");
  1765. goto got_exception;
  1766. }
  1767. HANDLE_OP(EXT_OP_TRY)
  1768. {
  1769. /* read the blocktype */
  1770. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1771. param_cell_num = wasm_types[type_index]->param_cell_num;
  1772. cell_num = wasm_types[type_index]->ret_cell_num;
  1773. goto handle_op_try;
  1774. }
  1775. HANDLE_OP(WASM_OP_TRY)
  1776. {
  1777. value_type = *frame_ip++;
  1778. param_cell_num = 0;
  1779. cell_num = wasm_value_type_cell_num(value_type);
  1780. handle_op_try:
  1781. cache_index = ((uintptr_t)frame_ip)
  1782. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1783. cache_items = exec_env->block_addr_cache[cache_index];
  1784. if (cache_items[0].start_addr == frame_ip) {
  1785. cache_items[0].start_addr = 0;
  1786. }
  1787. if (cache_items[1].start_addr == frame_ip) {
  1788. cache_items[1].start_addr = 0;
  1789. }
  1790. /* start at the first opcode following the try and its blocktype
  1791. */
  1792. uint8 *lookup_cursor = frame_ip;
  1793. uint8 handler_opcode = WASM_OP_UNREACHABLE;
  1794. /* target_addr filled in when END or DELEGATE is found */
  1795. PUSH_CSP(LABEL_TYPE_TRY, param_cell_num, cell_num, 0);
  1796. /* reset to begin of block */
  1797. lookup_cursor = frame_ip;
  1798. do {
  1799. /* lookup the next CATCH, CATCH_ALL or END for this TRY */
  1800. if (!wasm_loader_find_block_addr(
  1801. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1802. lookup_cursor, (uint8 *)-1, LABEL_TYPE_TRY,
  1803. &else_addr, &end_addr)) {
  1804. /* something went wrong */
  1805. wasm_set_exception(module, "find block address failed");
  1806. goto got_exception;
  1807. }
  1808. /* place cursor for continuation past opcode */
  1809. lookup_cursor = end_addr + 1;
  1810. /* end_addr points to CATCH, CATCH_ALL, DELEGATE or END */
  1811. handler_opcode = *end_addr;
  1812. switch (handler_opcode) {
  1813. case WASM_OP_CATCH:
  1814. skip_leb(lookup_cursor); /* skip tag_index */
  1815. PUSH_PTR(end_addr);
  1816. break;
  1817. case WASM_OP_CATCH_ALL:
  1818. PUSH_PTR(end_addr);
  1819. break;
  1820. case WASM_OP_DELEGATE:
  1821. skip_leb(lookup_cursor); /* skip depth */
  1822. PUSH_PTR(end_addr);
  1823. /* patch target_addr */
  1824. (frame_csp - 1)->target_addr = lookup_cursor;
  1825. break;
  1826. case WASM_OP_END:
  1827. PUSH_PTR(0);
  1828. /* patch target_addr */
  1829. (frame_csp - 1)->target_addr = end_addr;
  1830. break;
  1831. default:
  1832. /* something went wrong */
  1833. wasm_set_exception(module,
  1834. "find block address returned an "
  1835. "unexpected opcode");
  1836. goto got_exception;
  1837. }
  1838. /* ... search until the returned address is the END of the
  1839. * TRY block */
  1840. } while (handler_opcode != WASM_OP_END
  1841. && handler_opcode != WASM_OP_DELEGATE);
  1842. /* handler setup on stack complete */
  1843. HANDLE_OP_END();
  1844. }
  1845. HANDLE_OP(WASM_OP_CATCH)
  1846. {
  1847. /* skip the tag_index */
  1848. skip_leb(frame_ip);
  1849. /* leave the frame */
  1850. POP_CSP_N(0);
  1851. HANDLE_OP_END();
  1852. }
  1853. HANDLE_OP(WASM_OP_CATCH_ALL)
  1854. {
  1855. /* leave the frame */
  1856. POP_CSP_N(0);
  1857. HANDLE_OP_END();
  1858. }
  1859. HANDLE_OP(WASM_OP_DELEGATE)
  1860. {
  1861. /* skip the delegate depth */
  1862. skip_leb(frame_ip);
  1863. /* leave the frame like WASM_OP_END */
  1864. POP_CSP();
  1865. HANDLE_OP_END();
  1866. }
  1867. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  1868. HANDLE_OP(EXT_OP_BLOCK)
  1869. {
  1870. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1871. param_cell_num =
  1872. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1873. cell_num =
  1874. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1875. goto handle_op_block;
  1876. }
  1877. HANDLE_OP(WASM_OP_BLOCK)
  1878. {
  1879. value_type = *frame_ip++;
  1880. param_cell_num = 0;
  1881. cell_num = wasm_value_type_cell_num(value_type);
  1882. handle_op_block:
  1883. cache_index = ((uintptr_t)frame_ip)
  1884. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1885. cache_items = exec_env->block_addr_cache[cache_index];
  1886. if (cache_items[0].start_addr == frame_ip) {
  1887. end_addr = cache_items[0].end_addr;
  1888. }
  1889. else if (cache_items[1].start_addr == frame_ip) {
  1890. end_addr = cache_items[1].end_addr;
  1891. }
  1892. #if WASM_ENABLE_DEBUG_INTERP != 0
  1893. else if (!wasm_loader_find_block_addr(
  1894. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1895. frame_ip, (uint8 *)-1, LABEL_TYPE_BLOCK,
  1896. &else_addr, &end_addr)) {
  1897. wasm_set_exception(module, "find block address failed");
  1898. goto got_exception;
  1899. }
  1900. #endif
  1901. else {
  1902. end_addr = NULL;
  1903. }
  1904. PUSH_CSP(LABEL_TYPE_BLOCK, param_cell_num, cell_num, end_addr);
  1905. HANDLE_OP_END();
  1906. }
  1907. HANDLE_OP(EXT_OP_LOOP)
  1908. {
  1909. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1910. param_cell_num =
  1911. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1912. cell_num =
  1913. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1914. goto handle_op_loop;
  1915. }
  1916. HANDLE_OP(WASM_OP_LOOP)
  1917. {
  1918. value_type = *frame_ip++;
  1919. param_cell_num = 0;
  1920. cell_num = 0;
  1921. handle_op_loop:
  1922. PUSH_CSP(LABEL_TYPE_LOOP, param_cell_num, cell_num, frame_ip);
  1923. HANDLE_OP_END();
  1924. }
  1925. HANDLE_OP(EXT_OP_IF)
  1926. {
  1927. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1928. param_cell_num =
  1929. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1930. cell_num =
  1931. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1932. goto handle_op_if;
  1933. }
  1934. HANDLE_OP(WASM_OP_IF)
  1935. {
  1936. value_type = *frame_ip++;
  1937. param_cell_num = 0;
  1938. cell_num = wasm_value_type_cell_num(value_type);
  1939. handle_op_if:
  1940. cache_index = ((uintptr_t)frame_ip)
  1941. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1942. cache_items = exec_env->block_addr_cache[cache_index];
  1943. if (cache_items[0].start_addr == frame_ip) {
  1944. else_addr = cache_items[0].else_addr;
  1945. end_addr = cache_items[0].end_addr;
  1946. }
  1947. else if (cache_items[1].start_addr == frame_ip) {
  1948. else_addr = cache_items[1].else_addr;
  1949. end_addr = cache_items[1].end_addr;
  1950. }
  1951. else if (!wasm_loader_find_block_addr(
  1952. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1953. frame_ip, (uint8 *)-1, LABEL_TYPE_IF, &else_addr,
  1954. &end_addr)) {
  1955. wasm_set_exception(module, "find block address failed");
  1956. goto got_exception;
  1957. }
  1958. cond = (uint32)POP_I32();
  1959. if (cond) { /* if branch is met */
  1960. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num, end_addr);
  1961. }
  1962. else { /* if branch is not met */
  1963. /* if there is no else branch, go to the end addr */
  1964. if (else_addr == NULL) {
  1965. frame_ip = end_addr + 1;
  1966. }
  1967. /* if there is an else branch, go to the else addr */
  1968. else {
  1969. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num,
  1970. end_addr);
  1971. frame_ip = else_addr + 1;
  1972. }
  1973. }
  1974. HANDLE_OP_END();
  1975. }
  1976. HANDLE_OP(WASM_OP_ELSE)
  1977. {
  1978. /* comes from the if branch in WASM_OP_IF */
  1979. frame_ip = (frame_csp - 1)->target_addr;
  1980. HANDLE_OP_END();
  1981. }
  1982. HANDLE_OP(WASM_OP_END)
  1983. {
  1984. if (frame_csp > frame->csp_bottom + 1) {
  1985. POP_CSP();
  1986. }
  1987. else { /* end of function, treat as WASM_OP_RETURN */
  1988. frame_sp -= cur_func->ret_cell_num;
  1989. for (i = 0; i < cur_func->ret_cell_num; i++) {
  1990. #if WASM_ENABLE_GC != 0
  1991. if (prev_frame->ip) {
  1992. /* prev frame is not a glue frame and has
  1993. the frame ref area */
  1994. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  1995. *FRAME_REF(frame_sp + i);
  1996. }
  1997. #endif
  1998. *prev_frame->sp++ = frame_sp[i];
  1999. }
  2000. goto return_func;
  2001. }
  2002. HANDLE_OP_END();
  2003. }
  2004. HANDLE_OP(WASM_OP_BR)
  2005. {
  2006. #if WASM_ENABLE_THREAD_MGR != 0
  2007. CHECK_SUSPEND_FLAGS();
  2008. #endif
  2009. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2010. label_pop_csp_n:
  2011. POP_CSP_N(depth);
  2012. if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */
  2013. if (!wasm_loader_find_block_addr(
  2014. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  2015. (frame_csp - 1)->begin_addr, (uint8 *)-1,
  2016. LABEL_TYPE_BLOCK, &else_addr, &end_addr)) {
  2017. wasm_set_exception(module, "find block address failed");
  2018. goto got_exception;
  2019. }
  2020. frame_ip = end_addr;
  2021. }
  2022. HANDLE_OP_END();
  2023. }
  2024. HANDLE_OP(WASM_OP_BR_IF)
  2025. {
  2026. #if WASM_ENABLE_THREAD_MGR != 0
  2027. CHECK_SUSPEND_FLAGS();
  2028. #endif
  2029. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2030. cond = (uint32)POP_I32();
  2031. if (cond)
  2032. goto label_pop_csp_n;
  2033. HANDLE_OP_END();
  2034. }
  2035. HANDLE_OP(WASM_OP_BR_TABLE)
  2036. {
  2037. #if WASM_ENABLE_THREAD_MGR != 0
  2038. CHECK_SUSPEND_FLAGS();
  2039. #endif
  2040. read_leb_uint32(frame_ip, frame_ip_end, count);
  2041. lidx = POP_I32();
  2042. if (lidx > count)
  2043. lidx = count;
  2044. depth = frame_ip[lidx];
  2045. goto label_pop_csp_n;
  2046. }
  2047. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  2048. {
  2049. BrTableCache *node_cache =
  2050. bh_list_first_elem(module->module->br_table_cache_list);
  2051. BrTableCache *node_next;
  2052. #if WASM_ENABLE_THREAD_MGR != 0
  2053. CHECK_SUSPEND_FLAGS();
  2054. #endif
  2055. lidx = POP_I32();
  2056. while (node_cache) {
  2057. node_next = bh_list_elem_next(node_cache);
  2058. if (node_cache->br_table_op_addr == frame_ip - 1) {
  2059. if (lidx > node_cache->br_count)
  2060. lidx = node_cache->br_count;
  2061. depth = node_cache->br_depths[lidx];
  2062. goto label_pop_csp_n;
  2063. }
  2064. node_cache = node_next;
  2065. }
  2066. bh_assert(0);
  2067. HANDLE_OP_END();
  2068. }
  2069. HANDLE_OP(WASM_OP_RETURN)
  2070. {
  2071. frame_sp -= cur_func->ret_cell_num;
  2072. for (i = 0; i < cur_func->ret_cell_num; i++) {
  2073. #if WASM_ENABLE_GC != 0
  2074. if (prev_frame->ip) {
  2075. /* prev frame is not a glue frame and has
  2076. the frame ref area */
  2077. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  2078. *FRAME_REF(frame_sp + i);
  2079. }
  2080. #endif
  2081. *prev_frame->sp++ = frame_sp[i];
  2082. }
  2083. goto return_func;
  2084. }
  2085. HANDLE_OP(WASM_OP_CALL)
  2086. {
  2087. #if WASM_ENABLE_THREAD_MGR != 0
  2088. CHECK_SUSPEND_FLAGS();
  2089. #endif
  2090. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2091. #if WASM_ENABLE_MULTI_MODULE != 0
  2092. if (fidx >= module->e->function_count) {
  2093. wasm_set_exception(module, "unknown function");
  2094. goto got_exception;
  2095. }
  2096. #endif
  2097. cur_func = module->e->functions + fidx;
  2098. goto call_func_from_interp;
  2099. }
  2100. #if WASM_ENABLE_TAIL_CALL != 0
  2101. HANDLE_OP(WASM_OP_RETURN_CALL)
  2102. {
  2103. #if WASM_ENABLE_THREAD_MGR != 0
  2104. CHECK_SUSPEND_FLAGS();
  2105. #endif
  2106. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2107. #if WASM_ENABLE_MULTI_MODULE != 0
  2108. if (fidx >= module->e->function_count) {
  2109. wasm_set_exception(module, "unknown function");
  2110. goto got_exception;
  2111. }
  2112. #endif
  2113. cur_func = module->e->functions + fidx;
  2114. goto call_func_from_return_call;
  2115. }
  2116. #endif /* WASM_ENABLE_TAIL_CALL */
  2117. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  2118. #if WASM_ENABLE_TAIL_CALL != 0
  2119. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  2120. #endif
  2121. {
  2122. WASMFuncType *cur_type, *cur_func_type;
  2123. WASMTableInstance *tbl_inst;
  2124. uint32 tbl_idx;
  2125. #if WASM_ENABLE_TAIL_CALL != 0
  2126. opcode = *(frame_ip - 1);
  2127. #endif
  2128. #if WASM_ENABLE_THREAD_MGR != 0
  2129. CHECK_SUSPEND_FLAGS();
  2130. #endif
  2131. /**
  2132. * type check. compiler will make sure all like
  2133. * (call_indirect (type $x) (it.const 1))
  2134. * the function type has to be defined in the module also
  2135. * no matter it is used or not
  2136. */
  2137. read_leb_uint32(frame_ip, frame_ip_end, tidx);
  2138. bh_assert(tidx < module->module->type_count);
  2139. cur_type = wasm_types[tidx];
  2140. /* clang-format off */
  2141. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2142. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2143. #else
  2144. frame_ip++;
  2145. tbl_idx = 0;
  2146. #endif
  2147. bh_assert(tbl_idx < module->table_count);
  2148. /* clang-format on */
  2149. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2150. #if WASM_ENABLE_MEMORY64 != 0
  2151. is_table64 = tbl_inst->is_table64;
  2152. #endif
  2153. val = POP_TBL_ELEM_IDX();
  2154. if (val >= tbl_inst->cur_size) {
  2155. wasm_set_exception(module, "undefined element");
  2156. goto got_exception;
  2157. }
  2158. /* clang-format off */
  2159. #if WASM_ENABLE_GC == 0
  2160. fidx = tbl_inst->elems[val];
  2161. if (fidx == (uint32)-1) {
  2162. wasm_set_exception(module, "uninitialized element");
  2163. goto got_exception;
  2164. }
  2165. #else
  2166. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  2167. if (!func_obj) {
  2168. wasm_set_exception(module, "uninitialized element");
  2169. goto got_exception;
  2170. }
  2171. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2172. #endif
  2173. /* clang-format on */
  2174. /*
  2175. * we might be using a table injected by host or
  2176. * another module. In that case, we don't validate
  2177. * the elem value while loading
  2178. */
  2179. if (fidx >= module->e->function_count) {
  2180. wasm_set_exception(module, "unknown function");
  2181. goto got_exception;
  2182. }
  2183. /* always call module own functions */
  2184. cur_func = module->e->functions + fidx;
  2185. if (cur_func->is_import_func)
  2186. cur_func_type = cur_func->u.func_import->func_type;
  2187. else
  2188. cur_func_type = cur_func->u.func->func_type;
  2189. /* clang-format off */
  2190. #if WASM_ENABLE_GC == 0
  2191. if (cur_type != cur_func_type) {
  2192. wasm_set_exception(module, "indirect call type mismatch");
  2193. goto got_exception;
  2194. }
  2195. #else
  2196. if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
  2197. wasm_set_exception(module, "indirect call type mismatch");
  2198. goto got_exception;
  2199. }
  2200. #endif
  2201. /* clang-format on */
  2202. #if WASM_ENABLE_TAIL_CALL != 0
  2203. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  2204. goto call_func_from_return_call;
  2205. #endif
  2206. goto call_func_from_interp;
  2207. }
  2208. /* parametric instructions */
  2209. HANDLE_OP(WASM_OP_DROP)
  2210. {
  2211. frame_sp--;
  2212. #if WASM_ENABLE_GC != 0
  2213. frame_ref_tmp = FRAME_REF(frame_sp);
  2214. *frame_ref_tmp = 0;
  2215. #endif
  2216. HANDLE_OP_END();
  2217. }
  2218. HANDLE_OP(WASM_OP_DROP_64)
  2219. {
  2220. frame_sp -= 2;
  2221. #if WASM_ENABLE_GC != 0
  2222. frame_ref_tmp = FRAME_REF(frame_sp);
  2223. *frame_ref_tmp = 0;
  2224. *(frame_ref_tmp + 1) = 0;
  2225. #endif
  2226. HANDLE_OP_END();
  2227. }
  2228. HANDLE_OP(WASM_OP_SELECT)
  2229. {
  2230. cond = (uint32)POP_I32();
  2231. frame_sp--;
  2232. if (!cond)
  2233. *(frame_sp - 1) = *frame_sp;
  2234. HANDLE_OP_END();
  2235. }
  2236. HANDLE_OP(WASM_OP_SELECT_64)
  2237. {
  2238. cond = (uint32)POP_I32();
  2239. frame_sp -= 2;
  2240. if (!cond) {
  2241. *(frame_sp - 2) = *frame_sp;
  2242. *(frame_sp - 1) = *(frame_sp + 1);
  2243. }
  2244. HANDLE_OP_END();
  2245. }
  2246. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2247. HANDLE_OP(WASM_OP_SELECT_T)
  2248. {
  2249. uint32 vec_len;
  2250. uint8 type;
  2251. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  2252. type = *frame_ip++;
  2253. cond = (uint32)POP_I32();
  2254. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  2255. #if WASM_ENABLE_GC != 0 && UINTPTR_MAX == UINT64_MAX
  2256. || wasm_is_type_reftype(type)
  2257. #endif
  2258. ) {
  2259. frame_sp -= 2;
  2260. if (!cond) {
  2261. *(frame_sp - 2) = *frame_sp;
  2262. *(frame_sp - 1) = *(frame_sp + 1);
  2263. }
  2264. }
  2265. else {
  2266. frame_sp--;
  2267. if (!cond)
  2268. *(frame_sp - 1) = *frame_sp;
  2269. }
  2270. #if WASM_ENABLE_GC != 0
  2271. frame_ref_tmp = FRAME_REF(frame_sp);
  2272. *frame_ref_tmp = 0;
  2273. #if UINTPTR_MAX == UINT64_MAX
  2274. *(frame_ref_tmp + 1) = 0;
  2275. #endif
  2276. #endif
  2277. (void)vec_len;
  2278. HANDLE_OP_END();
  2279. }
  2280. HANDLE_OP(WASM_OP_TABLE_GET)
  2281. {
  2282. uint32 tbl_idx;
  2283. tbl_elem_idx_t elem_idx;
  2284. WASMTableInstance *tbl_inst;
  2285. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2286. bh_assert(tbl_idx < module->table_count);
  2287. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2288. #if WASM_ENABLE_MEMORY64 != 0
  2289. is_table64 = tbl_inst->is_table64;
  2290. #endif
  2291. elem_idx = POP_TBL_ELEM_IDX();
  2292. if (elem_idx >= tbl_inst->cur_size) {
  2293. wasm_set_exception(module, "out of bounds table access");
  2294. goto got_exception;
  2295. }
  2296. #if WASM_ENABLE_GC == 0
  2297. PUSH_I32(tbl_inst->elems[elem_idx]);
  2298. #else
  2299. PUSH_REF(tbl_inst->elems[elem_idx]);
  2300. #endif
  2301. HANDLE_OP_END();
  2302. }
  2303. HANDLE_OP(WASM_OP_TABLE_SET)
  2304. {
  2305. WASMTableInstance *tbl_inst;
  2306. uint32 tbl_idx;
  2307. tbl_elem_idx_t elem_idx;
  2308. table_elem_type_t elem_val;
  2309. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2310. bh_assert(tbl_idx < module->table_count);
  2311. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2312. #if WASM_ENABLE_MEMORY64 != 0
  2313. is_table64 = tbl_inst->is_table64;
  2314. #endif
  2315. #if WASM_ENABLE_GC == 0
  2316. elem_val = POP_I32();
  2317. #else
  2318. elem_val = POP_REF();
  2319. #endif
  2320. elem_idx = POP_TBL_ELEM_IDX();
  2321. if (elem_idx >= tbl_inst->cur_size) {
  2322. wasm_set_exception(module, "out of bounds table access");
  2323. goto got_exception;
  2324. }
  2325. tbl_inst->elems[elem_idx] = elem_val;
  2326. HANDLE_OP_END();
  2327. }
  2328. HANDLE_OP(WASM_OP_REF_NULL)
  2329. {
  2330. uint32 ref_type;
  2331. read_leb_uint32(frame_ip, frame_ip_end, ref_type);
  2332. #if WASM_ENABLE_GC == 0
  2333. PUSH_I32(NULL_REF);
  2334. #else
  2335. PUSH_REF(NULL_REF);
  2336. #endif
  2337. (void)ref_type;
  2338. HANDLE_OP_END();
  2339. }
  2340. HANDLE_OP(WASM_OP_REF_IS_NULL)
  2341. {
  2342. #if WASM_ENABLE_GC == 0
  2343. uint32 ref_val;
  2344. ref_val = POP_I32();
  2345. #else
  2346. void *ref_val;
  2347. ref_val = POP_REF();
  2348. #endif
  2349. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  2350. HANDLE_OP_END();
  2351. }
  2352. HANDLE_OP(WASM_OP_REF_FUNC)
  2353. {
  2354. uint32 func_idx;
  2355. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  2356. #if WASM_ENABLE_GC == 0
  2357. PUSH_I32(func_idx);
  2358. #else
  2359. SYNC_ALL_TO_FRAME();
  2360. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  2361. NULL, 0))) {
  2362. goto got_exception;
  2363. }
  2364. PUSH_REF(gc_obj);
  2365. #endif
  2366. HANDLE_OP_END();
  2367. }
  2368. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2369. #if WASM_ENABLE_GC != 0
  2370. HANDLE_OP(WASM_OP_CALL_REF)
  2371. {
  2372. #if WASM_ENABLE_THREAD_MGR != 0
  2373. CHECK_SUSPEND_FLAGS();
  2374. #endif
  2375. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2376. func_obj = POP_REF();
  2377. if (!func_obj) {
  2378. wasm_set_exception(module, "null function reference");
  2379. goto got_exception;
  2380. }
  2381. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2382. cur_func = module->e->functions + fidx;
  2383. goto call_func_from_interp;
  2384. }
  2385. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  2386. {
  2387. #if WASM_ENABLE_THREAD_MGR != 0
  2388. CHECK_SUSPEND_FLAGS();
  2389. #endif
  2390. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2391. func_obj = POP_REF();
  2392. if (!func_obj) {
  2393. wasm_set_exception(module, "null function reference");
  2394. goto got_exception;
  2395. }
  2396. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2397. cur_func = module->e->functions + fidx;
  2398. goto call_func_from_return_call;
  2399. }
  2400. HANDLE_OP(WASM_OP_REF_EQ)
  2401. {
  2402. WASMObjectRef gc_obj1, gc_obj2;
  2403. gc_obj2 = POP_REF();
  2404. gc_obj1 = POP_REF();
  2405. val = wasm_obj_equal(gc_obj1, gc_obj2);
  2406. PUSH_I32(val);
  2407. HANDLE_OP_END();
  2408. }
  2409. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  2410. {
  2411. gc_obj = POP_REF();
  2412. if (gc_obj == NULL_REF) {
  2413. wasm_set_exception(module, "null reference");
  2414. goto got_exception;
  2415. }
  2416. PUSH_REF(gc_obj);
  2417. HANDLE_OP_END();
  2418. }
  2419. HANDLE_OP(WASM_OP_BR_ON_NULL)
  2420. {
  2421. #if WASM_ENABLE_THREAD_MGR != 0
  2422. CHECK_SUSPEND_FLAGS();
  2423. #endif
  2424. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2425. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2426. if (gc_obj == NULL_REF) {
  2427. frame_sp -= REF_CELL_NUM;
  2428. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2429. goto label_pop_csp_n;
  2430. }
  2431. HANDLE_OP_END();
  2432. }
  2433. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  2434. {
  2435. #if WASM_ENABLE_THREAD_MGR != 0
  2436. CHECK_SUSPEND_FLAGS();
  2437. #endif
  2438. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2439. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2440. if (gc_obj != NULL_REF) {
  2441. goto label_pop_csp_n;
  2442. }
  2443. else {
  2444. frame_sp -= REF_CELL_NUM;
  2445. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2446. }
  2447. HANDLE_OP_END();
  2448. }
  2449. HANDLE_OP(WASM_OP_GC_PREFIX)
  2450. {
  2451. uint32 opcode1;
  2452. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2453. /* opcode1 was checked in loader and is no larger than
  2454. UINT8_MAX */
  2455. opcode = (uint8)opcode1;
  2456. switch (opcode) {
  2457. case WASM_OP_STRUCT_NEW:
  2458. case WASM_OP_STRUCT_NEW_DEFAULT:
  2459. {
  2460. WASMModule *wasm_module = module->module;
  2461. WASMStructType *struct_type;
  2462. WASMRttType *rtt_type;
  2463. WASMValue field_value = { 0 };
  2464. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2465. struct_type =
  2466. (WASMStructType *)module->module->types[type_index];
  2467. if (!(rtt_type = wasm_rtt_type_new(
  2468. (WASMType *)struct_type, type_index,
  2469. wasm_module->rtt_types,
  2470. wasm_module->type_count,
  2471. &wasm_module->rtt_type_lock))) {
  2472. wasm_set_exception(module,
  2473. "create rtt type failed");
  2474. goto got_exception;
  2475. }
  2476. SYNC_ALL_TO_FRAME();
  2477. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  2478. if (!struct_obj) {
  2479. wasm_set_exception(module,
  2480. "create struct object failed");
  2481. goto got_exception;
  2482. }
  2483. if (opcode == WASM_OP_STRUCT_NEW) {
  2484. WASMStructFieldType *fields = struct_type->fields;
  2485. int32 field_count = (int32)struct_type->field_count;
  2486. int32 field_idx;
  2487. uint8 field_type;
  2488. for (field_idx = field_count - 1; field_idx >= 0;
  2489. field_idx--) {
  2490. field_type = fields[field_idx].field_type;
  2491. if (wasm_is_type_reftype(field_type)) {
  2492. field_value.gc_obj = POP_REF();
  2493. }
  2494. else if (field_type == VALUE_TYPE_I32
  2495. || field_type == VALUE_TYPE_F32
  2496. || field_type == PACKED_TYPE_I8
  2497. || field_type == PACKED_TYPE_I16) {
  2498. field_value.i32 = POP_I32();
  2499. }
  2500. else {
  2501. field_value.i64 = POP_I64();
  2502. }
  2503. wasm_struct_obj_set_field(struct_obj, field_idx,
  2504. &field_value);
  2505. }
  2506. }
  2507. PUSH_REF(struct_obj);
  2508. HANDLE_OP_END();
  2509. }
  2510. case WASM_OP_STRUCT_GET:
  2511. case WASM_OP_STRUCT_GET_S:
  2512. case WASM_OP_STRUCT_GET_U:
  2513. {
  2514. WASMStructType *struct_type;
  2515. WASMValue field_value = { 0 };
  2516. uint32 field_idx;
  2517. uint8 field_type;
  2518. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2519. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2520. struct_type =
  2521. (WASMStructType *)module->module->types[type_index];
  2522. struct_obj = POP_REF();
  2523. if (!struct_obj) {
  2524. wasm_set_exception(module,
  2525. "null structure reference");
  2526. goto got_exception;
  2527. }
  2528. wasm_struct_obj_get_field(
  2529. struct_obj, field_idx,
  2530. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  2531. &field_value);
  2532. field_type = struct_type->fields[field_idx].field_type;
  2533. if (wasm_is_reftype_i31ref(field_type)) {
  2534. PUSH_I31REF(field_value.gc_obj);
  2535. }
  2536. else if (wasm_is_type_reftype(field_type)) {
  2537. PUSH_REF(field_value.gc_obj);
  2538. }
  2539. else if (field_type == VALUE_TYPE_I32
  2540. || field_type == VALUE_TYPE_F32
  2541. || field_type == PACKED_TYPE_I8
  2542. || field_type == PACKED_TYPE_I16) {
  2543. PUSH_I32(field_value.i32);
  2544. }
  2545. else {
  2546. PUSH_I64(field_value.i64);
  2547. }
  2548. HANDLE_OP_END();
  2549. }
  2550. case WASM_OP_STRUCT_SET:
  2551. {
  2552. WASMStructType *struct_type;
  2553. WASMValue field_value = { 0 };
  2554. uint32 field_idx;
  2555. uint8 field_type;
  2556. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2557. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2558. struct_type =
  2559. (WASMStructType *)module->module->types[type_index];
  2560. field_type = struct_type->fields[field_idx].field_type;
  2561. if (wasm_is_type_reftype(field_type)) {
  2562. field_value.gc_obj = POP_REF();
  2563. }
  2564. else if (field_type == VALUE_TYPE_I32
  2565. || field_type == VALUE_TYPE_F32
  2566. || field_type == PACKED_TYPE_I8
  2567. || field_type == PACKED_TYPE_I16) {
  2568. field_value.i32 = POP_I32();
  2569. }
  2570. else {
  2571. field_value.i64 = POP_I64();
  2572. }
  2573. struct_obj = POP_REF();
  2574. if (!struct_obj) {
  2575. wasm_set_exception(module,
  2576. "null structure reference");
  2577. goto got_exception;
  2578. }
  2579. wasm_struct_obj_set_field(struct_obj, field_idx,
  2580. &field_value);
  2581. HANDLE_OP_END();
  2582. }
  2583. case WASM_OP_ARRAY_NEW:
  2584. case WASM_OP_ARRAY_NEW_DEFAULT:
  2585. case WASM_OP_ARRAY_NEW_FIXED:
  2586. {
  2587. WASMModule *wasm_module = module->module;
  2588. WASMArrayType *array_type;
  2589. WASMRttType *rtt_type;
  2590. WASMValue array_elem = { 0 };
  2591. uint32 array_len;
  2592. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2593. array_type =
  2594. (WASMArrayType *)wasm_module->types[type_index];
  2595. if (!(rtt_type = wasm_rtt_type_new(
  2596. (WASMType *)array_type, type_index,
  2597. wasm_module->rtt_types,
  2598. wasm_module->type_count,
  2599. &wasm_module->rtt_type_lock))) {
  2600. wasm_set_exception(module,
  2601. "create rtt type failed");
  2602. goto got_exception;
  2603. }
  2604. if (opcode != WASM_OP_ARRAY_NEW_FIXED)
  2605. array_len = POP_I32();
  2606. else
  2607. read_leb_uint32(frame_ip, frame_ip_end, array_len);
  2608. if (opcode == WASM_OP_ARRAY_NEW) {
  2609. if (wasm_is_type_reftype(array_type->elem_type)) {
  2610. array_elem.gc_obj = POP_REF();
  2611. }
  2612. else if (array_type->elem_type == VALUE_TYPE_I32
  2613. || array_type->elem_type == VALUE_TYPE_F32
  2614. || array_type->elem_type == PACKED_TYPE_I8
  2615. || array_type->elem_type
  2616. == PACKED_TYPE_I16) {
  2617. array_elem.i32 = POP_I32();
  2618. }
  2619. else {
  2620. array_elem.i64 = POP_I64();
  2621. }
  2622. }
  2623. SYNC_ALL_TO_FRAME();
  2624. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2625. array_len, &array_elem);
  2626. if (!array_obj) {
  2627. wasm_set_exception(module,
  2628. "create array object failed");
  2629. goto got_exception;
  2630. }
  2631. if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
  2632. for (i = 0; i < array_len; i++) {
  2633. if (wasm_is_type_reftype(
  2634. array_type->elem_type)) {
  2635. array_elem.gc_obj = POP_REF();
  2636. }
  2637. else if (array_type->elem_type == VALUE_TYPE_I32
  2638. || array_type->elem_type
  2639. == VALUE_TYPE_F32
  2640. || array_type->elem_type
  2641. == PACKED_TYPE_I8
  2642. || array_type->elem_type
  2643. == PACKED_TYPE_I16) {
  2644. array_elem.i32 = POP_I32();
  2645. }
  2646. else {
  2647. array_elem.i64 = POP_I64();
  2648. }
  2649. wasm_array_obj_set_elem(
  2650. array_obj, array_len - 1 - i, &array_elem);
  2651. }
  2652. }
  2653. PUSH_REF(array_obj);
  2654. HANDLE_OP_END();
  2655. }
  2656. case WASM_OP_ARRAY_NEW_DATA:
  2657. {
  2658. WASMModule *wasm_module = module->module;
  2659. WASMArrayType *array_type;
  2660. WASMRttType *rtt_type;
  2661. WASMValue array_elem = { 0 };
  2662. WASMDataSeg *data_seg;
  2663. uint8 *array_elem_base;
  2664. uint32 array_len, data_seg_idx, data_seg_offset;
  2665. uint32 elem_size = 0;
  2666. uint64 total_size;
  2667. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2668. read_leb_uint32(frame_ip, frame_ip_end, data_seg_idx);
  2669. data_seg = wasm_module->data_segments[data_seg_idx];
  2670. array_type =
  2671. (WASMArrayType *)wasm_module->types[type_index];
  2672. if (!(rtt_type = wasm_rtt_type_new(
  2673. (WASMType *)array_type, type_index,
  2674. wasm_module->rtt_types,
  2675. wasm_module->type_count,
  2676. &wasm_module->rtt_type_lock))) {
  2677. wasm_set_exception(module,
  2678. "create rtt type failed");
  2679. goto got_exception;
  2680. }
  2681. array_len = POP_I32();
  2682. data_seg_offset = POP_I32();
  2683. switch (array_type->elem_type) {
  2684. case PACKED_TYPE_I8:
  2685. elem_size = 1;
  2686. break;
  2687. case PACKED_TYPE_I16:
  2688. elem_size = 2;
  2689. break;
  2690. case VALUE_TYPE_I32:
  2691. case VALUE_TYPE_F32:
  2692. elem_size = 4;
  2693. break;
  2694. case VALUE_TYPE_I64:
  2695. case VALUE_TYPE_F64:
  2696. elem_size = 8;
  2697. break;
  2698. default:
  2699. bh_assert(0);
  2700. }
  2701. total_size = (uint64)elem_size * array_len;
  2702. if (data_seg_offset >= data_seg->data_length
  2703. || total_size
  2704. > data_seg->data_length - data_seg_offset) {
  2705. wasm_set_exception(module,
  2706. "data segment out of bounds");
  2707. goto got_exception;
  2708. }
  2709. SYNC_ALL_TO_FRAME();
  2710. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2711. array_len, &array_elem);
  2712. if (!array_obj) {
  2713. wasm_set_exception(module,
  2714. "create array object failed");
  2715. goto got_exception;
  2716. }
  2717. array_elem_base =
  2718. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  2719. bh_memcpy_s(array_elem_base, (uint32)total_size,
  2720. data_seg->data + data_seg_offset,
  2721. (uint32)total_size);
  2722. PUSH_REF(array_obj);
  2723. HANDLE_OP_END();
  2724. }
  2725. case WASM_OP_ARRAY_NEW_ELEM:
  2726. {
  2727. /* TODO */
  2728. wasm_set_exception(module, "unsupported opcode");
  2729. goto got_exception;
  2730. }
  2731. case WASM_OP_ARRAY_GET:
  2732. case WASM_OP_ARRAY_GET_S:
  2733. case WASM_OP_ARRAY_GET_U:
  2734. {
  2735. WASMArrayType *array_type;
  2736. WASMValue array_elem = { 0 };
  2737. uint32 elem_idx, elem_size_log;
  2738. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2739. array_type =
  2740. (WASMArrayType *)module->module->types[type_index];
  2741. elem_idx = POP_I32();
  2742. array_obj = POP_REF();
  2743. if (!array_obj) {
  2744. wasm_set_exception(module, "null array reference");
  2745. goto got_exception;
  2746. }
  2747. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2748. wasm_set_exception(module,
  2749. "out of bounds array access");
  2750. goto got_exception;
  2751. }
  2752. wasm_array_obj_get_elem(
  2753. array_obj, elem_idx,
  2754. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  2755. &array_elem);
  2756. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  2757. if (wasm_is_reftype_i31ref(array_type->elem_type)) {
  2758. PUSH_I31REF(array_elem.gc_obj);
  2759. }
  2760. else if (wasm_is_type_reftype(array_type->elem_type)) {
  2761. PUSH_REF(array_elem.gc_obj);
  2762. }
  2763. else if (elem_size_log < 3) {
  2764. PUSH_I32(array_elem.i32);
  2765. }
  2766. else {
  2767. PUSH_I64(array_elem.i64);
  2768. }
  2769. HANDLE_OP_END();
  2770. }
  2771. case WASM_OP_ARRAY_SET:
  2772. {
  2773. WASMArrayType *array_type;
  2774. WASMValue array_elem = { 0 };
  2775. uint32 elem_idx;
  2776. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2777. array_type =
  2778. (WASMArrayType *)module->module->types[type_index];
  2779. if (wasm_is_type_reftype(array_type->elem_type)) {
  2780. array_elem.gc_obj = POP_REF();
  2781. }
  2782. else if (array_type->elem_type == VALUE_TYPE_I32
  2783. || array_type->elem_type == VALUE_TYPE_F32
  2784. || array_type->elem_type == PACKED_TYPE_I8
  2785. || array_type->elem_type == PACKED_TYPE_I16) {
  2786. array_elem.i32 = POP_I32();
  2787. }
  2788. else {
  2789. array_elem.i64 = POP_I64();
  2790. }
  2791. elem_idx = POP_I32();
  2792. array_obj = POP_REF();
  2793. if (!array_obj) {
  2794. wasm_set_exception(module, "null array reference");
  2795. goto got_exception;
  2796. }
  2797. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2798. wasm_set_exception(module,
  2799. "out of bounds array access");
  2800. goto got_exception;
  2801. }
  2802. wasm_array_obj_set_elem(array_obj, elem_idx,
  2803. &array_elem);
  2804. HANDLE_OP_END();
  2805. }
  2806. case WASM_OP_ARRAY_LEN:
  2807. {
  2808. uint32 array_len;
  2809. array_obj = POP_REF();
  2810. if (!array_obj) {
  2811. wasm_set_exception(module, "null array reference");
  2812. goto got_exception;
  2813. }
  2814. array_len = wasm_array_obj_length(array_obj);
  2815. PUSH_I32(array_len);
  2816. HANDLE_OP_END();
  2817. }
  2818. case WASM_OP_ARRAY_FILL:
  2819. {
  2820. WASMArrayType *array_type;
  2821. WASMValue fill_value = { 0 };
  2822. uint32 start_offset, len;
  2823. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2824. array_type =
  2825. (WASMArrayType *)module->module->types[type_index];
  2826. len = POP_I32();
  2827. if (wasm_is_type_reftype(array_type->elem_type)) {
  2828. fill_value.gc_obj = POP_REF();
  2829. }
  2830. else if (array_type->elem_type == VALUE_TYPE_I32
  2831. || array_type->elem_type == VALUE_TYPE_F32
  2832. || array_type->elem_type == PACKED_TYPE_I8
  2833. || array_type->elem_type == PACKED_TYPE_I16) {
  2834. fill_value.i32 = POP_I32();
  2835. }
  2836. else {
  2837. fill_value.i64 = POP_I64();
  2838. }
  2839. start_offset = POP_I32();
  2840. array_obj = POP_REF();
  2841. if (!array_obj) {
  2842. wasm_set_exception(module, "null array reference");
  2843. goto got_exception;
  2844. }
  2845. if (len > 0) {
  2846. if ((uint64)start_offset + len
  2847. >= wasm_array_obj_length(array_obj)) {
  2848. wasm_set_exception(
  2849. module, "out of bounds array access");
  2850. goto got_exception;
  2851. }
  2852. wasm_array_obj_fill(array_obj, start_offset, len,
  2853. &fill_value);
  2854. }
  2855. HANDLE_OP_END();
  2856. }
  2857. case WASM_OP_ARRAY_COPY:
  2858. {
  2859. uint32 dst_offset, src_offset, len, src_type_index;
  2860. WASMArrayObjectRef src_obj, dst_obj;
  2861. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2862. read_leb_uint32(frame_ip, frame_ip_end, src_type_index);
  2863. len = POP_I32();
  2864. src_offset = POP_I32();
  2865. src_obj = POP_REF();
  2866. dst_offset = POP_I32();
  2867. dst_obj = POP_REF();
  2868. if (!src_obj || !dst_obj) {
  2869. wasm_set_exception(module, "null array reference");
  2870. goto got_exception;
  2871. }
  2872. if (len > 0) {
  2873. if ((dst_offset > UINT32_MAX - len)
  2874. || (dst_offset + len
  2875. > wasm_array_obj_length(dst_obj))
  2876. || (src_offset > UINT32_MAX - len)
  2877. || (src_offset + len
  2878. > wasm_array_obj_length(src_obj))) {
  2879. wasm_set_exception(
  2880. module, "out of bounds array access");
  2881. goto got_exception;
  2882. }
  2883. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2884. src_offset, len);
  2885. }
  2886. (void)src_type_index;
  2887. HANDLE_OP_END();
  2888. }
  2889. case WASM_OP_REF_I31:
  2890. {
  2891. uint32 i31_val;
  2892. i31_val = POP_I32();
  2893. i31_obj = wasm_i31_obj_new(i31_val);
  2894. PUSH_I31REF(i31_obj);
  2895. HANDLE_OP_END();
  2896. }
  2897. case WASM_OP_I31_GET_S:
  2898. case WASM_OP_I31_GET_U:
  2899. {
  2900. uint32 i31_val;
  2901. i31_obj = (WASMI31ObjectRef)POP_REF();
  2902. if (!i31_obj) {
  2903. wasm_set_exception(module, "null i31 reference");
  2904. goto got_exception;
  2905. }
  2906. i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
  2907. if (opcode == WASM_OP_I31_GET_S
  2908. && (i31_val & 0x40000000) /* bit 30 is 1 */)
  2909. /* set bit 31 to 1 */
  2910. i31_val |= 0x80000000;
  2911. PUSH_I32(i31_val);
  2912. HANDLE_OP_END();
  2913. }
  2914. case WASM_OP_REF_TEST:
  2915. case WASM_OP_REF_CAST:
  2916. case WASM_OP_REF_TEST_NULLABLE:
  2917. case WASM_OP_REF_CAST_NULLABLE:
  2918. {
  2919. int32 heap_type;
  2920. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2921. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2922. if (!gc_obj) {
  2923. if (opcode == WASM_OP_REF_TEST
  2924. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2925. (void)POP_REF();
  2926. if (opcode == WASM_OP_REF_TEST)
  2927. PUSH_I32(0);
  2928. else
  2929. PUSH_I32(1);
  2930. }
  2931. else if (opcode == WASM_OP_REF_CAST) {
  2932. wasm_set_exception(module, "cast failure");
  2933. goto got_exception;
  2934. }
  2935. else {
  2936. /* Do nothing for WASM_OP_REF_CAST_NULLABLE */
  2937. }
  2938. }
  2939. else {
  2940. bool castable = false;
  2941. if (heap_type >= 0) {
  2942. WASMModule *wasm_module = module->module;
  2943. castable = wasm_obj_is_instance_of(
  2944. gc_obj, (uint32)heap_type,
  2945. wasm_module->types,
  2946. wasm_module->type_count);
  2947. }
  2948. else {
  2949. castable =
  2950. wasm_obj_is_type_of(gc_obj, heap_type);
  2951. }
  2952. if (opcode == WASM_OP_REF_TEST
  2953. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2954. (void)POP_REF();
  2955. if (castable)
  2956. PUSH_I32(1);
  2957. else
  2958. PUSH_I32(0);
  2959. }
  2960. else if (!castable) {
  2961. wasm_set_exception(module, "cast failure");
  2962. goto got_exception;
  2963. }
  2964. }
  2965. HANDLE_OP_END();
  2966. }
  2967. case WASM_OP_BR_ON_CAST:
  2968. case WASM_OP_BR_ON_CAST_FAIL:
  2969. {
  2970. int32 heap_type, heap_type_dst;
  2971. uint8 castflags;
  2972. #if WASM_ENABLE_THREAD_MGR != 0
  2973. CHECK_SUSPEND_FLAGS();
  2974. #endif
  2975. castflags = *frame_ip++;
  2976. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2977. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2978. read_leb_int32(frame_ip, frame_ip_end, heap_type_dst);
  2979. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2980. if (!gc_obj) {
  2981. /*
  2982. * castflags should be 0~3:
  2983. * 0: (non-null, non-null)
  2984. * 1: (null, non-null)
  2985. * 2: (non-null, null)
  2986. * 3: (null, null)
  2987. */
  2988. if (
  2989. /* op is BR_ON_CAST and dst reftype is nullable
  2990. */
  2991. ((opcode1 == WASM_OP_BR_ON_CAST)
  2992. && ((castflags == 2) || (castflags == 3)))
  2993. /* op is BR_ON_CAST_FAIL and dst reftype is
  2994. non-nullable */
  2995. || ((opcode1 == WASM_OP_BR_ON_CAST_FAIL)
  2996. && ((castflags == 0) || (castflags == 1))))
  2997. goto label_pop_csp_n;
  2998. }
  2999. else {
  3000. bool castable = false;
  3001. if (heap_type_dst >= 0) {
  3002. WASMModule *wasm_module = module->module;
  3003. castable = wasm_obj_is_instance_of(
  3004. gc_obj, (uint32)heap_type_dst,
  3005. wasm_module->types,
  3006. wasm_module->type_count);
  3007. }
  3008. else {
  3009. castable =
  3010. wasm_obj_is_type_of(gc_obj, heap_type_dst);
  3011. }
  3012. if ((castable && (opcode == WASM_OP_BR_ON_CAST))
  3013. || (!castable
  3014. && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
  3015. goto label_pop_csp_n;
  3016. }
  3017. }
  3018. (void)heap_type;
  3019. HANDLE_OP_END();
  3020. }
  3021. case WASM_OP_ANY_CONVERT_EXTERN:
  3022. {
  3023. externref_obj = POP_REF();
  3024. if (externref_obj == NULL_REF)
  3025. PUSH_REF(NULL_REF);
  3026. else {
  3027. gc_obj = wasm_externref_obj_to_internal_obj(
  3028. externref_obj);
  3029. PUSH_REF(gc_obj);
  3030. }
  3031. HANDLE_OP_END();
  3032. }
  3033. case WASM_OP_EXTERN_CONVERT_ANY:
  3034. {
  3035. gc_obj = POP_REF();
  3036. if (gc_obj == NULL_REF)
  3037. PUSH_REF(NULL_REF);
  3038. else {
  3039. if (!(externref_obj =
  3040. wasm_internal_obj_to_externref_obj(
  3041. exec_env, gc_obj))) {
  3042. wasm_set_exception(
  3043. module, "create externref object failed");
  3044. goto got_exception;
  3045. }
  3046. PUSH_REF(externref_obj);
  3047. }
  3048. HANDLE_OP_END();
  3049. }
  3050. #if WASM_ENABLE_STRINGREF != 0
  3051. case WASM_OP_STRING_NEW_UTF8:
  3052. case WASM_OP_STRING_NEW_WTF16:
  3053. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  3054. case WASM_OP_STRING_NEW_WTF8:
  3055. {
  3056. uint32 mem_idx, addr, bytes_length, offset = 0;
  3057. EncodingFlag flag = WTF8;
  3058. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3059. bytes_length = POP_I32();
  3060. addr = POP_I32();
  3061. CHECK_MEMORY_OVERFLOW(bytes_length);
  3062. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  3063. flag = WTF16;
  3064. }
  3065. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  3066. flag = UTF8;
  3067. }
  3068. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  3069. flag = LOSSY_UTF8;
  3070. }
  3071. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  3072. flag = WTF8;
  3073. }
  3074. str_obj = wasm_string_new_with_encoding(
  3075. maddr, bytes_length, flag);
  3076. if (!str_obj) {
  3077. wasm_set_exception(module,
  3078. "create string object failed");
  3079. goto got_exception;
  3080. }
  3081. SYNC_ALL_TO_FRAME();
  3082. stringref_obj =
  3083. wasm_stringref_obj_new(exec_env, str_obj);
  3084. if (!stringref_obj) {
  3085. wasm_set_exception(module,
  3086. "create stringref failed");
  3087. goto got_exception;
  3088. }
  3089. PUSH_REF(stringref_obj);
  3090. (void)mem_idx;
  3091. HANDLE_OP_END();
  3092. }
  3093. case WASM_OP_STRING_CONST:
  3094. {
  3095. WASMModule *wasm_module = module->module;
  3096. uint32 contents;
  3097. read_leb_uint32(frame_ip, frame_ip_end, contents);
  3098. str_obj = wasm_string_new_const(
  3099. (const char *)
  3100. wasm_module->string_literal_ptrs[contents],
  3101. wasm_module->string_literal_lengths[contents]);
  3102. if (!str_obj) {
  3103. wasm_set_exception(module,
  3104. "create string object failed");
  3105. goto got_exception;
  3106. }
  3107. SYNC_ALL_TO_FRAME();
  3108. stringref_obj =
  3109. wasm_stringref_obj_new(exec_env, str_obj);
  3110. if (!stringref_obj) {
  3111. wasm_set_exception(module,
  3112. "create stringref failed");
  3113. goto got_exception;
  3114. }
  3115. PUSH_REF(stringref_obj);
  3116. HANDLE_OP_END();
  3117. }
  3118. case WASM_OP_STRING_MEASURE_UTF8:
  3119. case WASM_OP_STRING_MEASURE_WTF8:
  3120. case WASM_OP_STRING_MEASURE_WTF16:
  3121. {
  3122. int32 target_bytes_length;
  3123. EncodingFlag flag = WTF8;
  3124. stringref_obj = POP_REF();
  3125. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  3126. flag = WTF16;
  3127. }
  3128. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  3129. flag = UTF8;
  3130. }
  3131. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  3132. flag = LOSSY_UTF8;
  3133. }
  3134. target_bytes_length = wasm_string_measure(
  3135. (WASMString)wasm_stringref_obj_get_value(
  3136. stringref_obj),
  3137. flag);
  3138. PUSH_I32(target_bytes_length);
  3139. HANDLE_OP_END();
  3140. }
  3141. case WASM_OP_STRING_ENCODE_UTF8:
  3142. case WASM_OP_STRING_ENCODE_WTF16:
  3143. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  3144. case WASM_OP_STRING_ENCODE_WTF8:
  3145. {
  3146. uint32 mem_idx, addr;
  3147. int32 target_bytes_length;
  3148. WASMMemoryInstance *memory_inst;
  3149. EncodingFlag flag = WTF8;
  3150. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3151. addr = POP_I32();
  3152. stringref_obj = POP_REF();
  3153. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3154. stringref_obj);
  3155. #if WASM_ENABLE_SHARED_HEAP != 0
  3156. if (app_addr_in_shared_heap((uint64)addr, 1))
  3157. shared_heap_addr_app_to_native((uint64)addr, maddr);
  3158. else
  3159. #endif
  3160. {
  3161. memory_inst = module->memories[mem_idx];
  3162. maddr = memory_inst->memory_data + addr;
  3163. }
  3164. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  3165. flag = WTF16;
  3166. count = wasm_string_measure(str_obj, flag);
  3167. target_bytes_length = wasm_string_encode(
  3168. str_obj, 0, count, maddr, NULL, flag);
  3169. }
  3170. else {
  3171. if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  3172. flag = UTF8;
  3173. }
  3174. else if (opcode
  3175. == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  3176. flag = LOSSY_UTF8;
  3177. }
  3178. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  3179. flag = WTF8;
  3180. }
  3181. count = wasm_string_measure(str_obj, flag);
  3182. target_bytes_length = wasm_string_encode(
  3183. str_obj, 0, count, maddr, NULL, flag);
  3184. if (target_bytes_length == -1) {
  3185. wasm_set_exception(
  3186. module, "isolated surrogate is seen");
  3187. goto got_exception;
  3188. }
  3189. }
  3190. if (target_bytes_length < 0) {
  3191. wasm_set_exception(module,
  3192. "stringref encode failed");
  3193. goto got_exception;
  3194. }
  3195. PUSH_I32(target_bytes_length);
  3196. HANDLE_OP_END();
  3197. }
  3198. case WASM_OP_STRING_CONCAT:
  3199. {
  3200. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3201. stringref_obj2 = POP_REF();
  3202. stringref_obj1 = POP_REF();
  3203. str_obj = wasm_string_concat(
  3204. (WASMString)wasm_stringref_obj_get_value(
  3205. stringref_obj1),
  3206. (WASMString)wasm_stringref_obj_get_value(
  3207. stringref_obj2));
  3208. if (!str_obj) {
  3209. wasm_set_exception(module,
  3210. "create string object failed");
  3211. goto got_exception;
  3212. }
  3213. SYNC_ALL_TO_FRAME();
  3214. stringref_obj =
  3215. wasm_stringref_obj_new(exec_env, str_obj);
  3216. if (!stringref_obj) {
  3217. wasm_set_exception(module,
  3218. "create stringref failed");
  3219. goto got_exception;
  3220. }
  3221. PUSH_REF(stringref_obj);
  3222. HANDLE_OP_END();
  3223. }
  3224. case WASM_OP_STRING_EQ:
  3225. {
  3226. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3227. int32 is_eq;
  3228. stringref_obj2 = POP_REF();
  3229. stringref_obj1 = POP_REF();
  3230. is_eq = wasm_string_eq(
  3231. (WASMString)wasm_stringref_obj_get_value(
  3232. stringref_obj1),
  3233. (WASMString)wasm_stringref_obj_get_value(
  3234. stringref_obj2));
  3235. PUSH_I32(is_eq);
  3236. HANDLE_OP_END();
  3237. }
  3238. case WASM_OP_STRING_IS_USV_SEQUENCE:
  3239. {
  3240. int32 is_usv_sequence;
  3241. stringref_obj = POP_REF();
  3242. is_usv_sequence = wasm_string_is_usv_sequence(
  3243. (WASMString)wasm_stringref_obj_get_value(
  3244. stringref_obj));
  3245. PUSH_I32(is_usv_sequence);
  3246. HANDLE_OP_END();
  3247. }
  3248. case WASM_OP_STRING_AS_WTF8:
  3249. {
  3250. stringref_obj = POP_REF();
  3251. str_obj = wasm_string_create_view(
  3252. (WASMString)wasm_stringref_obj_get_value(
  3253. stringref_obj),
  3254. STRING_VIEW_WTF8);
  3255. if (!str_obj) {
  3256. wasm_set_exception(module,
  3257. "create string object failed");
  3258. goto got_exception;
  3259. }
  3260. SYNC_ALL_TO_FRAME();
  3261. stringview_wtf8_obj =
  3262. wasm_stringview_wtf8_obj_new(exec_env, str_obj);
  3263. if (!stringview_wtf8_obj) {
  3264. wasm_set_exception(module,
  3265. "create stringview wtf8 failed");
  3266. goto got_exception;
  3267. }
  3268. PUSH_REF(stringview_wtf8_obj);
  3269. HANDLE_OP_END();
  3270. }
  3271. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  3272. {
  3273. uint32 next_pos, bytes, pos;
  3274. bytes = POP_I32();
  3275. pos = POP_I32();
  3276. stringview_wtf8_obj = POP_REF();
  3277. next_pos = wasm_string_advance(
  3278. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3279. stringview_wtf8_obj),
  3280. pos, bytes, NULL);
  3281. PUSH_I32(next_pos);
  3282. HANDLE_OP_END();
  3283. }
  3284. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  3285. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  3286. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  3287. {
  3288. uint32 mem_idx, addr, pos, bytes, next_pos;
  3289. int32 bytes_written;
  3290. WASMMemoryInstance *memory_inst;
  3291. EncodingFlag flag = WTF8;
  3292. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  3293. flag = UTF8;
  3294. }
  3295. else if (opcode
  3296. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  3297. flag = LOSSY_UTF8;
  3298. }
  3299. else if (opcode
  3300. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  3301. flag = WTF8;
  3302. }
  3303. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3304. bytes = POP_I32();
  3305. pos = POP_I32();
  3306. addr = POP_I32();
  3307. stringview_wtf8_obj = POP_REF();
  3308. #if WASM_ENABLE_SHARED_HEAP != 0
  3309. if (app_addr_in_shared_heap((uint64)addr, 1))
  3310. shared_heap_addr_app_to_native((uint64)addr, maddr);
  3311. else
  3312. #endif
  3313. {
  3314. memory_inst = module->memories[mem_idx];
  3315. maddr = memory_inst->memory_data + addr;
  3316. }
  3317. bytes_written = wasm_string_encode(
  3318. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3319. stringview_wtf8_obj),
  3320. pos, bytes, maddr, &next_pos, flag);
  3321. if (bytes_written < 0) {
  3322. if (bytes_written == Isolated_Surrogate) {
  3323. wasm_set_exception(
  3324. module, "isolated surrogate is seen");
  3325. }
  3326. else {
  3327. wasm_set_exception(module, "encode failed");
  3328. }
  3329. goto got_exception;
  3330. }
  3331. PUSH_I32(next_pos);
  3332. PUSH_I32(bytes_written);
  3333. HANDLE_OP_END();
  3334. }
  3335. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  3336. {
  3337. uint32 start, end;
  3338. end = POP_I32();
  3339. start = POP_I32();
  3340. stringview_wtf8_obj = POP_REF();
  3341. str_obj = wasm_string_slice(
  3342. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3343. stringview_wtf8_obj),
  3344. start, end, STRING_VIEW_WTF8);
  3345. if (!str_obj) {
  3346. wasm_set_exception(module,
  3347. "create string object failed");
  3348. goto got_exception;
  3349. }
  3350. SYNC_ALL_TO_FRAME();
  3351. stringref_obj =
  3352. wasm_stringref_obj_new(exec_env, str_obj);
  3353. if (!stringref_obj) {
  3354. wasm_set_exception(module,
  3355. "create stringref failed");
  3356. goto got_exception;
  3357. }
  3358. PUSH_REF(stringref_obj);
  3359. HANDLE_OP_END();
  3360. }
  3361. case WASM_OP_STRING_AS_WTF16:
  3362. {
  3363. stringref_obj = POP_REF();
  3364. str_obj = wasm_string_create_view(
  3365. (WASMString)wasm_stringref_obj_get_value(
  3366. stringref_obj),
  3367. STRING_VIEW_WTF16);
  3368. if (!str_obj) {
  3369. wasm_set_exception(module,
  3370. "create string object failed");
  3371. goto got_exception;
  3372. }
  3373. SYNC_ALL_TO_FRAME();
  3374. stringview_wtf16_obj =
  3375. wasm_stringview_wtf16_obj_new(exec_env, str_obj);
  3376. if (!stringview_wtf16_obj) {
  3377. wasm_set_exception(
  3378. module, "create stringview wtf16 failed");
  3379. goto got_exception;
  3380. }
  3381. PUSH_REF(stringview_wtf16_obj);
  3382. HANDLE_OP_END();
  3383. }
  3384. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  3385. {
  3386. int32 code_units_length;
  3387. stringview_wtf16_obj = POP_REF();
  3388. code_units_length = wasm_string_wtf16_get_length(
  3389. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3390. stringview_wtf16_obj));
  3391. PUSH_I32(code_units_length);
  3392. HANDLE_OP_END();
  3393. }
  3394. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  3395. {
  3396. int32 pos;
  3397. uint32 code_unit;
  3398. pos = POP_I32();
  3399. stringview_wtf16_obj = POP_REF();
  3400. code_unit = (uint32)wasm_string_get_wtf16_codeunit(
  3401. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3402. stringview_wtf16_obj),
  3403. pos);
  3404. PUSH_I32(code_unit);
  3405. HANDLE_OP_END();
  3406. }
  3407. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  3408. {
  3409. uint32 mem_idx, addr, pos, len, offset = 0;
  3410. int32 written_code_units = 0;
  3411. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3412. len = POP_I32();
  3413. pos = POP_I32();
  3414. addr = POP_I32();
  3415. stringview_wtf16_obj = POP_REF();
  3416. CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
  3417. /* check 2-byte alignment */
  3418. if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
  3419. != 0) {
  3420. wasm_set_exception(module,
  3421. "unaligned memory access");
  3422. goto got_exception;
  3423. }
  3424. written_code_units = wasm_string_encode(
  3425. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3426. stringview_wtf16_obj),
  3427. pos, len, maddr, NULL, WTF16);
  3428. if (written_code_units < 0) {
  3429. wasm_set_exception(module, "encode failed");
  3430. goto got_exception;
  3431. }
  3432. PUSH_I32(written_code_units);
  3433. (void)mem_idx;
  3434. HANDLE_OP_END();
  3435. }
  3436. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  3437. {
  3438. uint32 start, end;
  3439. end = POP_I32();
  3440. start = POP_I32();
  3441. stringview_wtf16_obj = POP_REF();
  3442. str_obj = wasm_string_slice(
  3443. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3444. stringview_wtf16_obj),
  3445. start, end, STRING_VIEW_WTF16);
  3446. if (!str_obj) {
  3447. wasm_set_exception(module,
  3448. "create string object failed");
  3449. goto got_exception;
  3450. }
  3451. SYNC_ALL_TO_FRAME();
  3452. stringref_obj =
  3453. wasm_stringref_obj_new(exec_env, str_obj);
  3454. if (!stringref_obj) {
  3455. wasm_set_exception(module,
  3456. "create stringref failed");
  3457. goto got_exception;
  3458. }
  3459. PUSH_REF(stringref_obj);
  3460. HANDLE_OP_END();
  3461. }
  3462. case WASM_OP_STRING_AS_ITER:
  3463. {
  3464. stringref_obj = POP_REF();
  3465. str_obj = wasm_string_create_view(
  3466. (WASMString)wasm_stringref_obj_get_value(
  3467. stringref_obj),
  3468. STRING_VIEW_ITER);
  3469. if (!str_obj) {
  3470. wasm_set_exception(module,
  3471. "create string object failed");
  3472. goto got_exception;
  3473. }
  3474. SYNC_ALL_TO_FRAME();
  3475. stringview_iter_obj =
  3476. wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
  3477. if (!stringview_iter_obj) {
  3478. wasm_set_exception(module,
  3479. "create stringview iter failed");
  3480. goto got_exception;
  3481. }
  3482. PUSH_REF(stringview_iter_obj);
  3483. HANDLE_OP_END();
  3484. }
  3485. case WASM_OP_STRINGVIEW_ITER_NEXT:
  3486. {
  3487. uint32 code_point;
  3488. stringview_iter_obj = POP_REF();
  3489. code_point = wasm_string_next_codepoint(
  3490. (WASMString)wasm_stringview_iter_obj_get_value(
  3491. stringview_iter_obj),
  3492. wasm_stringview_iter_obj_get_pos(
  3493. stringview_iter_obj));
  3494. PUSH_I32(code_point);
  3495. HANDLE_OP_END();
  3496. }
  3497. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  3498. case WASM_OP_STRINGVIEW_ITER_REWIND:
  3499. {
  3500. uint32 code_points_count, code_points_consumed = 0,
  3501. cur_pos, next_pos = 0;
  3502. code_points_count = POP_I32();
  3503. stringview_iter_obj = POP_REF();
  3504. str_obj =
  3505. (WASMString)wasm_stringview_iter_obj_get_value(
  3506. stringview_iter_obj);
  3507. cur_pos = wasm_stringview_iter_obj_get_pos(
  3508. stringview_iter_obj);
  3509. if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
  3510. next_pos = wasm_string_advance(
  3511. str_obj, cur_pos, code_points_count,
  3512. &code_points_consumed);
  3513. }
  3514. else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
  3515. next_pos = wasm_string_rewind(
  3516. str_obj, cur_pos, code_points_count,
  3517. &code_points_consumed);
  3518. }
  3519. wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
  3520. next_pos);
  3521. PUSH_I32(code_points_consumed);
  3522. HANDLE_OP_END();
  3523. }
  3524. case WASM_OP_STRINGVIEW_ITER_SLICE:
  3525. {
  3526. uint32 code_points_count, cur_pos;
  3527. code_points_count = POP_I32();
  3528. stringview_iter_obj = POP_REF();
  3529. cur_pos = wasm_stringview_iter_obj_get_pos(
  3530. stringview_iter_obj);
  3531. str_obj = wasm_string_slice(
  3532. (WASMString)wasm_stringview_iter_obj_get_value(
  3533. stringview_iter_obj),
  3534. cur_pos, cur_pos + code_points_count,
  3535. STRING_VIEW_ITER);
  3536. if (!str_obj) {
  3537. wasm_set_exception(module,
  3538. "create string object failed");
  3539. goto got_exception;
  3540. }
  3541. SYNC_ALL_TO_FRAME();
  3542. stringref_obj =
  3543. wasm_stringref_obj_new(exec_env, str_obj);
  3544. if (!stringref_obj) {
  3545. wasm_set_exception(module,
  3546. "create stringref failed");
  3547. goto got_exception;
  3548. }
  3549. PUSH_REF(stringref_obj);
  3550. HANDLE_OP_END();
  3551. }
  3552. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  3553. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  3554. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  3555. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  3556. {
  3557. uint32 start, end, array_len;
  3558. EncodingFlag flag = WTF8;
  3559. WASMArrayType *array_type;
  3560. void *arr_start_addr;
  3561. end = POP_I32();
  3562. start = POP_I32();
  3563. array_obj = POP_REF();
  3564. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3565. (WASMObjectRef)array_obj);
  3566. arr_start_addr =
  3567. wasm_array_obj_elem_addr(array_obj, start);
  3568. array_len = wasm_array_obj_length(array_obj);
  3569. if (start > end || end > array_len) {
  3570. wasm_set_exception(module,
  3571. "out of bounds array access");
  3572. goto got_exception;
  3573. }
  3574. if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
  3575. if (array_type->elem_type != VALUE_TYPE_I16) {
  3576. wasm_set_exception(module,
  3577. "array type mismatch");
  3578. goto got_exception;
  3579. }
  3580. flag = WTF16;
  3581. }
  3582. else {
  3583. if (array_type->elem_type != VALUE_TYPE_I8) {
  3584. wasm_set_exception(module,
  3585. "array type mismatch");
  3586. goto got_exception;
  3587. }
  3588. if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
  3589. flag = UTF8;
  3590. }
  3591. else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
  3592. flag = WTF8;
  3593. }
  3594. else if (opcode
  3595. == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
  3596. flag = LOSSY_UTF8;
  3597. }
  3598. }
  3599. str_obj = wasm_string_new_with_encoding(
  3600. arr_start_addr, (end - start), flag);
  3601. if (!str_obj) {
  3602. wasm_set_exception(module,
  3603. "create string object failed");
  3604. goto got_exception;
  3605. }
  3606. SYNC_ALL_TO_FRAME();
  3607. stringref_obj =
  3608. wasm_stringref_obj_new(exec_env, str_obj);
  3609. if (!stringref_obj) {
  3610. wasm_set_exception(module,
  3611. "create stringref failed");
  3612. goto got_exception;
  3613. }
  3614. PUSH_REF(stringref_obj);
  3615. HANDLE_OP_END();
  3616. }
  3617. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  3618. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  3619. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  3620. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  3621. {
  3622. uint32 start, array_len;
  3623. int32 bytes_written;
  3624. EncodingFlag flag = WTF8;
  3625. WASMArrayType *array_type;
  3626. void *arr_start_addr;
  3627. start = POP_I32();
  3628. array_obj = POP_REF();
  3629. stringref_obj = POP_REF();
  3630. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3631. stringref_obj);
  3632. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3633. (WASMObjectRef)array_obj);
  3634. arr_start_addr =
  3635. wasm_array_obj_elem_addr(array_obj, start);
  3636. array_len = wasm_array_obj_length(array_obj);
  3637. if (start > array_len) {
  3638. wasm_set_exception(module,
  3639. "out of bounds array access");
  3640. goto got_exception;
  3641. }
  3642. if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
  3643. if (array_type->elem_type != VALUE_TYPE_I16) {
  3644. wasm_set_exception(module,
  3645. "array type mismatch");
  3646. goto got_exception;
  3647. }
  3648. flag = WTF16;
  3649. }
  3650. else {
  3651. if (array_type->elem_type != VALUE_TYPE_I8) {
  3652. wasm_set_exception(module,
  3653. "array type mismatch");
  3654. goto got_exception;
  3655. }
  3656. if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
  3657. flag = UTF8;
  3658. }
  3659. else if (opcode
  3660. == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
  3661. flag = WTF8;
  3662. }
  3663. else if (
  3664. opcode
  3665. == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
  3666. flag = LOSSY_UTF8;
  3667. }
  3668. }
  3669. count = wasm_string_measure(str_obj, flag);
  3670. bytes_written = wasm_string_encode(
  3671. str_obj, 0, count, arr_start_addr, NULL, flag);
  3672. if (bytes_written < 0) {
  3673. if (bytes_written == Isolated_Surrogate) {
  3674. wasm_set_exception(
  3675. module, "isolated surrogate is seen");
  3676. }
  3677. else if (bytes_written == Insufficient_Space) {
  3678. wasm_set_exception(
  3679. module, "array space is insufficient");
  3680. }
  3681. else {
  3682. wasm_set_exception(module, "encode failed");
  3683. }
  3684. goto got_exception;
  3685. }
  3686. PUSH_I32(bytes_written);
  3687. HANDLE_OP_END();
  3688. }
  3689. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  3690. default:
  3691. {
  3692. wasm_set_exception(module, "unsupported opcode");
  3693. goto got_exception;
  3694. }
  3695. }
  3696. }
  3697. #endif /* end of WASM_ENABLE_GC != 0 */
  3698. /* variable instructions */
  3699. HANDLE_OP(WASM_OP_GET_LOCAL)
  3700. {
  3701. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3702. switch (local_type) {
  3703. case VALUE_TYPE_I32:
  3704. case VALUE_TYPE_F32:
  3705. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3706. case VALUE_TYPE_FUNCREF:
  3707. case VALUE_TYPE_EXTERNREF:
  3708. #endif
  3709. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3710. break;
  3711. case VALUE_TYPE_I64:
  3712. case VALUE_TYPE_F64:
  3713. PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset));
  3714. break;
  3715. default:
  3716. #if WASM_ENABLE_GC != 0
  3717. if (wasm_is_type_reftype(local_type)) {
  3718. if (wasm_is_reftype_i31ref(local_type)) {
  3719. PUSH_I31REF(
  3720. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3721. }
  3722. else {
  3723. PUSH_REF(
  3724. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3725. }
  3726. }
  3727. else
  3728. #endif
  3729. {
  3730. wasm_set_exception(module, "invalid local type");
  3731. goto got_exception;
  3732. }
  3733. }
  3734. HANDLE_OP_END();
  3735. }
  3736. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3737. {
  3738. local_offset = *frame_ip++;
  3739. if (local_offset & 0x80)
  3740. PUSH_I64(
  3741. GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F)));
  3742. else
  3743. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3744. HANDLE_OP_END();
  3745. }
  3746. HANDLE_OP(WASM_OP_SET_LOCAL)
  3747. {
  3748. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3749. switch (local_type) {
  3750. case VALUE_TYPE_I32:
  3751. case VALUE_TYPE_F32:
  3752. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3753. case VALUE_TYPE_FUNCREF:
  3754. case VALUE_TYPE_EXTERNREF:
  3755. #endif
  3756. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3757. break;
  3758. case VALUE_TYPE_I64:
  3759. case VALUE_TYPE_F64:
  3760. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3761. POP_I64());
  3762. break;
  3763. default:
  3764. #if WASM_ENABLE_GC != 0
  3765. if (wasm_is_type_reftype(local_type)) {
  3766. PUT_REF_TO_ADDR(frame_lp + local_offset, POP_REF());
  3767. }
  3768. else
  3769. #endif
  3770. {
  3771. wasm_set_exception(module, "invalid local type");
  3772. goto got_exception;
  3773. }
  3774. }
  3775. HANDLE_OP_END();
  3776. }
  3777. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  3778. {
  3779. local_offset = *frame_ip++;
  3780. if (local_offset & 0x80)
  3781. PUT_I64_TO_ADDR(
  3782. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3783. POP_I64());
  3784. else
  3785. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3786. HANDLE_OP_END();
  3787. }
  3788. HANDLE_OP(WASM_OP_TEE_LOCAL)
  3789. {
  3790. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3791. switch (local_type) {
  3792. case VALUE_TYPE_I32:
  3793. case VALUE_TYPE_F32:
  3794. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3795. case VALUE_TYPE_FUNCREF:
  3796. case VALUE_TYPE_EXTERNREF:
  3797. #endif
  3798. *(int32 *)(frame_lp + local_offset) =
  3799. *(int32 *)(frame_sp - 1);
  3800. break;
  3801. case VALUE_TYPE_I64:
  3802. case VALUE_TYPE_F64:
  3803. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3804. GET_I64_FROM_ADDR(frame_sp - 2));
  3805. break;
  3806. default:
  3807. #if WASM_ENABLE_GC != 0
  3808. if (wasm_is_type_reftype(local_type)) {
  3809. PUT_REF_TO_ADDR(
  3810. frame_lp + local_offset,
  3811. GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM));
  3812. }
  3813. else
  3814. #endif
  3815. {
  3816. wasm_set_exception(module, "invalid local type");
  3817. goto got_exception;
  3818. }
  3819. }
  3820. HANDLE_OP_END();
  3821. }
  3822. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  3823. {
  3824. local_offset = *frame_ip++;
  3825. if (local_offset & 0x80)
  3826. PUT_I64_TO_ADDR(
  3827. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3828. GET_I64_FROM_ADDR(frame_sp - 2));
  3829. else
  3830. *(int32 *)(frame_lp + local_offset) =
  3831. *(int32 *)(frame_sp - 1);
  3832. HANDLE_OP_END();
  3833. }
  3834. HANDLE_OP(WASM_OP_GET_GLOBAL)
  3835. {
  3836. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3837. bh_assert(global_idx < module->e->global_count);
  3838. global = globals + global_idx;
  3839. global_addr = get_global_addr(global_data, global);
  3840. /* clang-format off */
  3841. #if WASM_ENABLE_GC == 0
  3842. PUSH_I32(*(uint32 *)global_addr);
  3843. #else
  3844. if (!wasm_is_type_reftype(global->type)) {
  3845. PUSH_I32(*(uint32 *)global_addr);
  3846. }
  3847. else if (wasm_is_reftype_i31ref(global->type)) {
  3848. PUSH_I31REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3849. }
  3850. else {
  3851. PUSH_REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3852. }
  3853. #endif
  3854. /* clang-format on */
  3855. HANDLE_OP_END();
  3856. }
  3857. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  3858. {
  3859. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3860. bh_assert(global_idx < module->e->global_count);
  3861. global = globals + global_idx;
  3862. global_addr = get_global_addr(global_data, global);
  3863. PUSH_I64(GET_I64_FROM_ADDR((uint32 *)global_addr));
  3864. HANDLE_OP_END();
  3865. }
  3866. HANDLE_OP(WASM_OP_SET_GLOBAL)
  3867. {
  3868. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3869. bh_assert(global_idx < module->e->global_count);
  3870. global = globals + global_idx;
  3871. global_addr = get_global_addr(global_data, global);
  3872. /* clang-format off */
  3873. #if WASM_ENABLE_GC == 0
  3874. *(int32 *)global_addr = POP_I32();
  3875. #else
  3876. if (!wasm_is_type_reftype(global->type))
  3877. *(int32 *)global_addr = POP_I32();
  3878. else
  3879. PUT_REF_TO_ADDR((uint32 *)global_addr, POP_REF());
  3880. #endif
  3881. /* clang-format on */
  3882. HANDLE_OP_END();
  3883. }
  3884. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  3885. {
  3886. uint64 aux_stack_top;
  3887. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3888. bh_assert(global_idx < module->e->global_count);
  3889. global = globals + global_idx;
  3890. global_addr = get_global_addr(global_data, global);
  3891. #if WASM_ENABLE_MEMORY64 != 0
  3892. if (is_memory64) {
  3893. aux_stack_top = *(uint64 *)(frame_sp - 2);
  3894. }
  3895. else
  3896. #endif
  3897. {
  3898. aux_stack_top = (uint64)(*(uint32 *)(frame_sp - 1));
  3899. }
  3900. if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
  3901. wasm_set_exception(module, "wasm auxiliary stack overflow");
  3902. goto got_exception;
  3903. }
  3904. if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
  3905. wasm_set_exception(module,
  3906. "wasm auxiliary stack underflow");
  3907. goto got_exception;
  3908. }
  3909. #if WASM_ENABLE_MEMORY64 != 0
  3910. if (is_memory64) {
  3911. *(uint64 *)global_addr = aux_stack_top;
  3912. frame_sp -= 2;
  3913. }
  3914. else
  3915. #endif
  3916. {
  3917. *(uint32 *)global_addr = (uint32)aux_stack_top;
  3918. frame_sp--;
  3919. }
  3920. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3921. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  3922. uint32 aux_stack_used =
  3923. (uint32)(module->module->aux_stack_bottom
  3924. - *(uint32 *)global_addr);
  3925. if (aux_stack_used > module->e->max_aux_stack_used)
  3926. module->e->max_aux_stack_used = aux_stack_used;
  3927. }
  3928. #endif
  3929. HANDLE_OP_END();
  3930. }
  3931. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  3932. {
  3933. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3934. bh_assert(global_idx < module->e->global_count);
  3935. global = globals + global_idx;
  3936. global_addr = get_global_addr(global_data, global);
  3937. PUT_I64_TO_ADDR((uint32 *)global_addr, POP_I64());
  3938. HANDLE_OP_END();
  3939. }
  3940. /* memory load instructions */
  3941. HANDLE_OP(WASM_OP_I32_LOAD)
  3942. HANDLE_OP(WASM_OP_F32_LOAD)
  3943. {
  3944. uint32 flags;
  3945. mem_offset_t offset, addr;
  3946. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3947. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3948. addr = POP_MEM_OFFSET();
  3949. CHECK_MEMORY_OVERFLOW(4);
  3950. PUSH_I32(LOAD_I32(maddr));
  3951. CHECK_READ_WATCHPOINT(addr, offset);
  3952. HANDLE_OP_END();
  3953. }
  3954. HANDLE_OP(WASM_OP_I64_LOAD)
  3955. HANDLE_OP(WASM_OP_F64_LOAD)
  3956. {
  3957. uint32 flags;
  3958. mem_offset_t offset, addr;
  3959. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3960. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3961. addr = POP_MEM_OFFSET();
  3962. CHECK_MEMORY_OVERFLOW(8);
  3963. PUSH_I64(LOAD_I64(maddr));
  3964. CHECK_READ_WATCHPOINT(addr, offset);
  3965. HANDLE_OP_END();
  3966. }
  3967. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  3968. {
  3969. uint32 flags;
  3970. mem_offset_t offset, addr;
  3971. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3972. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3973. addr = POP_MEM_OFFSET();
  3974. CHECK_MEMORY_OVERFLOW(1);
  3975. PUSH_I32(sign_ext_8_32(*(int8 *)maddr));
  3976. CHECK_READ_WATCHPOINT(addr, offset);
  3977. HANDLE_OP_END();
  3978. }
  3979. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  3980. {
  3981. uint32 flags;
  3982. mem_offset_t offset, addr;
  3983. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3984. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3985. addr = POP_MEM_OFFSET();
  3986. CHECK_MEMORY_OVERFLOW(1);
  3987. PUSH_I32((uint32)(*(uint8 *)maddr));
  3988. CHECK_READ_WATCHPOINT(addr, offset);
  3989. HANDLE_OP_END();
  3990. }
  3991. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  3992. {
  3993. uint32 flags;
  3994. mem_offset_t offset, addr;
  3995. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3996. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3997. addr = POP_MEM_OFFSET();
  3998. CHECK_MEMORY_OVERFLOW(2);
  3999. PUSH_I32(sign_ext_16_32(LOAD_I16(maddr)));
  4000. CHECK_READ_WATCHPOINT(addr, offset);
  4001. HANDLE_OP_END();
  4002. }
  4003. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  4004. {
  4005. uint32 flags;
  4006. mem_offset_t offset, addr;
  4007. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4008. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4009. addr = POP_MEM_OFFSET();
  4010. CHECK_MEMORY_OVERFLOW(2);
  4011. PUSH_I32((uint32)(LOAD_U16(maddr)));
  4012. CHECK_READ_WATCHPOINT(addr, offset);
  4013. HANDLE_OP_END();
  4014. }
  4015. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  4016. {
  4017. uint32 flags;
  4018. mem_offset_t offset, addr;
  4019. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4020. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4021. addr = POP_MEM_OFFSET();
  4022. CHECK_MEMORY_OVERFLOW(1);
  4023. PUSH_I64(sign_ext_8_64(*(int8 *)maddr));
  4024. CHECK_READ_WATCHPOINT(addr, offset);
  4025. HANDLE_OP_END();
  4026. }
  4027. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  4028. {
  4029. uint32 flags;
  4030. mem_offset_t offset, addr;
  4031. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4032. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4033. addr = POP_MEM_OFFSET();
  4034. CHECK_MEMORY_OVERFLOW(1);
  4035. PUSH_I64((uint64)(*(uint8 *)maddr));
  4036. CHECK_READ_WATCHPOINT(addr, offset);
  4037. HANDLE_OP_END();
  4038. }
  4039. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  4040. {
  4041. uint32 flags;
  4042. mem_offset_t offset, addr;
  4043. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4044. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4045. addr = POP_MEM_OFFSET();
  4046. CHECK_MEMORY_OVERFLOW(2);
  4047. PUSH_I64(sign_ext_16_64(LOAD_I16(maddr)));
  4048. CHECK_READ_WATCHPOINT(addr, offset);
  4049. HANDLE_OP_END();
  4050. }
  4051. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  4052. {
  4053. uint32 flags;
  4054. mem_offset_t offset, addr;
  4055. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4056. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4057. addr = POP_MEM_OFFSET();
  4058. CHECK_MEMORY_OVERFLOW(2);
  4059. PUSH_I64((uint64)(LOAD_U16(maddr)));
  4060. CHECK_READ_WATCHPOINT(addr, offset);
  4061. HANDLE_OP_END();
  4062. }
  4063. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  4064. {
  4065. uint32 flags;
  4066. mem_offset_t offset, addr;
  4067. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4068. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4069. addr = POP_MEM_OFFSET();
  4070. CHECK_MEMORY_OVERFLOW(4);
  4071. PUSH_I64(sign_ext_32_64(LOAD_I32(maddr)));
  4072. CHECK_READ_WATCHPOINT(addr, offset);
  4073. HANDLE_OP_END();
  4074. }
  4075. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  4076. {
  4077. uint32 flags;
  4078. mem_offset_t offset, addr;
  4079. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4080. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4081. addr = POP_MEM_OFFSET();
  4082. CHECK_MEMORY_OVERFLOW(4);
  4083. PUSH_I64((uint64)(LOAD_U32(maddr)));
  4084. CHECK_READ_WATCHPOINT(addr, offset);
  4085. HANDLE_OP_END();
  4086. }
  4087. /* memory store instructions */
  4088. HANDLE_OP(WASM_OP_I32_STORE)
  4089. HANDLE_OP(WASM_OP_F32_STORE)
  4090. {
  4091. uint32 flags;
  4092. mem_offset_t offset, addr;
  4093. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4094. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4095. frame_sp--;
  4096. addr = POP_MEM_OFFSET();
  4097. CHECK_MEMORY_OVERFLOW(4);
  4098. #if WASM_ENABLE_MEMORY64 != 0
  4099. if (is_memory64) {
  4100. STORE_U32(maddr, frame_sp[2]);
  4101. }
  4102. else
  4103. #endif
  4104. {
  4105. STORE_U32(maddr, frame_sp[1]);
  4106. }
  4107. CHECK_WRITE_WATCHPOINT(addr, offset);
  4108. HANDLE_OP_END();
  4109. }
  4110. HANDLE_OP(WASM_OP_I64_STORE)
  4111. HANDLE_OP(WASM_OP_F64_STORE)
  4112. {
  4113. uint32 flags;
  4114. mem_offset_t offset, addr;
  4115. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4116. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4117. frame_sp -= 2;
  4118. addr = POP_MEM_OFFSET();
  4119. CHECK_MEMORY_OVERFLOW(8);
  4120. #if WASM_ENABLE_MEMORY64 != 0
  4121. if (is_memory64) {
  4122. PUT_I64_TO_ADDR((mem_offset_t *)maddr,
  4123. GET_I64_FROM_ADDR(frame_sp + 2));
  4124. }
  4125. else
  4126. #endif
  4127. {
  4128. PUT_I64_TO_ADDR((uint32 *)maddr,
  4129. GET_I64_FROM_ADDR(frame_sp + 1));
  4130. }
  4131. CHECK_WRITE_WATCHPOINT(addr, offset);
  4132. HANDLE_OP_END();
  4133. }
  4134. HANDLE_OP(WASM_OP_I32_STORE8)
  4135. HANDLE_OP(WASM_OP_I32_STORE16)
  4136. {
  4137. uint32 flags;
  4138. mem_offset_t offset, addr;
  4139. uint32 sval;
  4140. opcode = *(frame_ip - 1);
  4141. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4142. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4143. sval = (uint32)POP_I32();
  4144. addr = POP_MEM_OFFSET();
  4145. if (opcode == WASM_OP_I32_STORE8) {
  4146. CHECK_MEMORY_OVERFLOW(1);
  4147. *(uint8 *)maddr = (uint8)sval;
  4148. }
  4149. else {
  4150. CHECK_MEMORY_OVERFLOW(2);
  4151. STORE_U16(maddr, (uint16)sval);
  4152. }
  4153. CHECK_WRITE_WATCHPOINT(addr, offset);
  4154. HANDLE_OP_END();
  4155. }
  4156. HANDLE_OP(WASM_OP_I64_STORE8)
  4157. HANDLE_OP(WASM_OP_I64_STORE16)
  4158. HANDLE_OP(WASM_OP_I64_STORE32)
  4159. {
  4160. uint32 flags;
  4161. mem_offset_t offset, addr;
  4162. uint64 sval;
  4163. opcode = *(frame_ip - 1);
  4164. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4165. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4166. sval = (uint64)POP_I64();
  4167. addr = POP_MEM_OFFSET();
  4168. if (opcode == WASM_OP_I64_STORE8) {
  4169. CHECK_MEMORY_OVERFLOW(1);
  4170. *(uint8 *)maddr = (uint8)sval;
  4171. }
  4172. else if (opcode == WASM_OP_I64_STORE16) {
  4173. CHECK_MEMORY_OVERFLOW(2);
  4174. STORE_U16(maddr, (uint16)sval);
  4175. }
  4176. else {
  4177. CHECK_MEMORY_OVERFLOW(4);
  4178. STORE_U32(maddr, (uint32)sval);
  4179. }
  4180. CHECK_WRITE_WATCHPOINT(addr, offset);
  4181. HANDLE_OP_END();
  4182. }
  4183. /* memory size and memory grow instructions */
  4184. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  4185. {
  4186. uint32 mem_idx;
  4187. read_leb_memidx(frame_ip, frame_ip_end, mem_idx);
  4188. PUSH_PAGE_COUNT(memory->cur_page_count);
  4189. HANDLE_OP_END();
  4190. }
  4191. HANDLE_OP(WASM_OP_MEMORY_GROW)
  4192. {
  4193. uint32 mem_idx, prev_page_count;
  4194. mem_offset_t delta;
  4195. read_leb_memidx(frame_ip, frame_ip_end, mem_idx);
  4196. prev_page_count = memory->cur_page_count;
  4197. delta = POP_PAGE_COUNT();
  4198. if (
  4199. #if WASM_ENABLE_MEMORY64 != 0
  4200. delta > UINT32_MAX ||
  4201. #endif
  4202. !wasm_enlarge_memory_with_idx(module, (uint32)delta,
  4203. mem_idx)) {
  4204. /* failed to memory.grow, return -1 */
  4205. PUSH_PAGE_COUNT(-1);
  4206. }
  4207. else {
  4208. /* success, return previous page count */
  4209. PUSH_PAGE_COUNT(prev_page_count);
  4210. /* update memory size, no need to update memory ptr as
  4211. it isn't changed in wasm_enlarge_memory */
  4212. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  4213. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  4214. || WASM_ENABLE_BULK_MEMORY != 0
  4215. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  4216. #endif
  4217. }
  4218. HANDLE_OP_END();
  4219. }
  4220. /* constant instructions */
  4221. HANDLE_OP(WASM_OP_I32_CONST)
  4222. DEF_OP_I_CONST(int32, I32);
  4223. HANDLE_OP_END();
  4224. HANDLE_OP(WASM_OP_I64_CONST)
  4225. DEF_OP_I_CONST(int64, I64);
  4226. HANDLE_OP_END();
  4227. HANDLE_OP(WASM_OP_F32_CONST)
  4228. {
  4229. uint8 *p_float = (uint8 *)frame_sp++;
  4230. for (i = 0; i < sizeof(float32); i++)
  4231. *p_float++ = *frame_ip++;
  4232. HANDLE_OP_END();
  4233. }
  4234. HANDLE_OP(WASM_OP_F64_CONST)
  4235. {
  4236. uint8 *p_float = (uint8 *)frame_sp++;
  4237. frame_sp++;
  4238. for (i = 0; i < sizeof(float64); i++)
  4239. *p_float++ = *frame_ip++;
  4240. HANDLE_OP_END();
  4241. }
  4242. /* comparison instructions of i32 */
  4243. HANDLE_OP(WASM_OP_I32_EQZ)
  4244. {
  4245. DEF_OP_EQZ(I32);
  4246. HANDLE_OP_END();
  4247. }
  4248. HANDLE_OP(WASM_OP_I32_EQ)
  4249. {
  4250. DEF_OP_CMP(uint32, I32, ==);
  4251. HANDLE_OP_END();
  4252. }
  4253. HANDLE_OP(WASM_OP_I32_NE)
  4254. {
  4255. DEF_OP_CMP(uint32, I32, !=);
  4256. HANDLE_OP_END();
  4257. }
  4258. HANDLE_OP(WASM_OP_I32_LT_S)
  4259. {
  4260. DEF_OP_CMP(int32, I32, <);
  4261. HANDLE_OP_END();
  4262. }
  4263. HANDLE_OP(WASM_OP_I32_LT_U)
  4264. {
  4265. DEF_OP_CMP(uint32, I32, <);
  4266. HANDLE_OP_END();
  4267. }
  4268. HANDLE_OP(WASM_OP_I32_GT_S)
  4269. {
  4270. DEF_OP_CMP(int32, I32, >);
  4271. HANDLE_OP_END();
  4272. }
  4273. HANDLE_OP(WASM_OP_I32_GT_U)
  4274. {
  4275. DEF_OP_CMP(uint32, I32, >);
  4276. HANDLE_OP_END();
  4277. }
  4278. HANDLE_OP(WASM_OP_I32_LE_S)
  4279. {
  4280. DEF_OP_CMP(int32, I32, <=);
  4281. HANDLE_OP_END();
  4282. }
  4283. HANDLE_OP(WASM_OP_I32_LE_U)
  4284. {
  4285. DEF_OP_CMP(uint32, I32, <=);
  4286. HANDLE_OP_END();
  4287. }
  4288. HANDLE_OP(WASM_OP_I32_GE_S)
  4289. {
  4290. DEF_OP_CMP(int32, I32, >=);
  4291. HANDLE_OP_END();
  4292. }
  4293. HANDLE_OP(WASM_OP_I32_GE_U)
  4294. {
  4295. DEF_OP_CMP(uint32, I32, >=);
  4296. HANDLE_OP_END();
  4297. }
  4298. /* comparison instructions of i64 */
  4299. HANDLE_OP(WASM_OP_I64_EQZ)
  4300. {
  4301. DEF_OP_EQZ(I64);
  4302. HANDLE_OP_END();
  4303. }
  4304. HANDLE_OP(WASM_OP_I64_EQ)
  4305. {
  4306. DEF_OP_CMP(uint64, I64, ==);
  4307. HANDLE_OP_END();
  4308. }
  4309. HANDLE_OP(WASM_OP_I64_NE)
  4310. {
  4311. DEF_OP_CMP(uint64, I64, !=);
  4312. HANDLE_OP_END();
  4313. }
  4314. HANDLE_OP(WASM_OP_I64_LT_S)
  4315. {
  4316. DEF_OP_CMP(int64, I64, <);
  4317. HANDLE_OP_END();
  4318. }
  4319. HANDLE_OP(WASM_OP_I64_LT_U)
  4320. {
  4321. DEF_OP_CMP(uint64, I64, <);
  4322. HANDLE_OP_END();
  4323. }
  4324. HANDLE_OP(WASM_OP_I64_GT_S)
  4325. {
  4326. DEF_OP_CMP(int64, I64, >);
  4327. HANDLE_OP_END();
  4328. }
  4329. HANDLE_OP(WASM_OP_I64_GT_U)
  4330. {
  4331. DEF_OP_CMP(uint64, I64, >);
  4332. HANDLE_OP_END();
  4333. }
  4334. HANDLE_OP(WASM_OP_I64_LE_S)
  4335. {
  4336. DEF_OP_CMP(int64, I64, <=);
  4337. HANDLE_OP_END();
  4338. }
  4339. HANDLE_OP(WASM_OP_I64_LE_U)
  4340. {
  4341. DEF_OP_CMP(uint64, I64, <=);
  4342. HANDLE_OP_END();
  4343. }
  4344. HANDLE_OP(WASM_OP_I64_GE_S)
  4345. {
  4346. DEF_OP_CMP(int64, I64, >=);
  4347. HANDLE_OP_END();
  4348. }
  4349. HANDLE_OP(WASM_OP_I64_GE_U)
  4350. {
  4351. DEF_OP_CMP(uint64, I64, >=);
  4352. HANDLE_OP_END();
  4353. }
  4354. /* comparison instructions of f32 */
  4355. HANDLE_OP(WASM_OP_F32_EQ)
  4356. {
  4357. DEF_OP_CMP(float32, F32, ==);
  4358. HANDLE_OP_END();
  4359. }
  4360. HANDLE_OP(WASM_OP_F32_NE)
  4361. {
  4362. DEF_OP_CMP(float32, F32, !=);
  4363. HANDLE_OP_END();
  4364. }
  4365. HANDLE_OP(WASM_OP_F32_LT)
  4366. {
  4367. DEF_OP_CMP(float32, F32, <);
  4368. HANDLE_OP_END();
  4369. }
  4370. HANDLE_OP(WASM_OP_F32_GT)
  4371. {
  4372. DEF_OP_CMP(float32, F32, >);
  4373. HANDLE_OP_END();
  4374. }
  4375. HANDLE_OP(WASM_OP_F32_LE)
  4376. {
  4377. DEF_OP_CMP(float32, F32, <=);
  4378. HANDLE_OP_END();
  4379. }
  4380. HANDLE_OP(WASM_OP_F32_GE)
  4381. {
  4382. DEF_OP_CMP(float32, F32, >=);
  4383. HANDLE_OP_END();
  4384. }
  4385. /* comparison instructions of f64 */
  4386. HANDLE_OP(WASM_OP_F64_EQ)
  4387. {
  4388. DEF_OP_CMP(float64, F64, ==);
  4389. HANDLE_OP_END();
  4390. }
  4391. HANDLE_OP(WASM_OP_F64_NE)
  4392. {
  4393. DEF_OP_CMP(float64, F64, !=);
  4394. HANDLE_OP_END();
  4395. }
  4396. HANDLE_OP(WASM_OP_F64_LT)
  4397. {
  4398. DEF_OP_CMP(float64, F64, <);
  4399. HANDLE_OP_END();
  4400. }
  4401. HANDLE_OP(WASM_OP_F64_GT)
  4402. {
  4403. DEF_OP_CMP(float64, F64, >);
  4404. HANDLE_OP_END();
  4405. }
  4406. HANDLE_OP(WASM_OP_F64_LE)
  4407. {
  4408. DEF_OP_CMP(float64, F64, <=);
  4409. HANDLE_OP_END();
  4410. }
  4411. HANDLE_OP(WASM_OP_F64_GE)
  4412. {
  4413. DEF_OP_CMP(float64, F64, >=);
  4414. HANDLE_OP_END();
  4415. }
  4416. /* numeric instructions of i32 */
  4417. HANDLE_OP(WASM_OP_I32_CLZ)
  4418. {
  4419. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  4420. HANDLE_OP_END();
  4421. }
  4422. HANDLE_OP(WASM_OP_I32_CTZ)
  4423. {
  4424. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  4425. HANDLE_OP_END();
  4426. }
  4427. HANDLE_OP(WASM_OP_I32_POPCNT)
  4428. {
  4429. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  4430. HANDLE_OP_END();
  4431. }
  4432. HANDLE_OP(WASM_OP_I32_ADD)
  4433. {
  4434. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  4435. HANDLE_OP_END();
  4436. }
  4437. HANDLE_OP(WASM_OP_I32_SUB)
  4438. {
  4439. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  4440. HANDLE_OP_END();
  4441. }
  4442. HANDLE_OP(WASM_OP_I32_MUL)
  4443. {
  4444. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  4445. HANDLE_OP_END();
  4446. }
  4447. HANDLE_OP(WASM_OP_I32_DIV_S)
  4448. {
  4449. int32 a, b;
  4450. b = POP_I32();
  4451. a = POP_I32();
  4452. if (a == (int32)0x80000000 && b == -1) {
  4453. wasm_set_exception(module, "integer overflow");
  4454. goto got_exception;
  4455. }
  4456. if (b == 0) {
  4457. wasm_set_exception(module, "integer divide by zero");
  4458. goto got_exception;
  4459. }
  4460. PUSH_I32(a / b);
  4461. HANDLE_OP_END();
  4462. }
  4463. HANDLE_OP(WASM_OP_I32_DIV_U)
  4464. {
  4465. uint32 a, b;
  4466. b = (uint32)POP_I32();
  4467. a = (uint32)POP_I32();
  4468. if (b == 0) {
  4469. wasm_set_exception(module, "integer divide by zero");
  4470. goto got_exception;
  4471. }
  4472. PUSH_I32(a / b);
  4473. HANDLE_OP_END();
  4474. }
  4475. HANDLE_OP(WASM_OP_I32_REM_S)
  4476. {
  4477. int32 a, b;
  4478. b = POP_I32();
  4479. a = POP_I32();
  4480. if (a == (int32)0x80000000 && b == -1) {
  4481. PUSH_I32(0);
  4482. HANDLE_OP_END();
  4483. }
  4484. if (b == 0) {
  4485. wasm_set_exception(module, "integer divide by zero");
  4486. goto got_exception;
  4487. }
  4488. PUSH_I32(a % b);
  4489. HANDLE_OP_END();
  4490. }
  4491. HANDLE_OP(WASM_OP_I32_REM_U)
  4492. {
  4493. uint32 a, b;
  4494. b = (uint32)POP_I32();
  4495. a = (uint32)POP_I32();
  4496. if (b == 0) {
  4497. wasm_set_exception(module, "integer divide by zero");
  4498. goto got_exception;
  4499. }
  4500. PUSH_I32(a % b);
  4501. HANDLE_OP_END();
  4502. }
  4503. HANDLE_OP(WASM_OP_I32_AND)
  4504. {
  4505. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  4506. HANDLE_OP_END();
  4507. }
  4508. HANDLE_OP(WASM_OP_I32_OR)
  4509. {
  4510. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  4511. HANDLE_OP_END();
  4512. }
  4513. HANDLE_OP(WASM_OP_I32_XOR)
  4514. {
  4515. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  4516. HANDLE_OP_END();
  4517. }
  4518. HANDLE_OP(WASM_OP_I32_SHL)
  4519. {
  4520. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  4521. HANDLE_OP_END();
  4522. }
  4523. HANDLE_OP(WASM_OP_I32_SHR_S)
  4524. {
  4525. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  4526. HANDLE_OP_END();
  4527. }
  4528. HANDLE_OP(WASM_OP_I32_SHR_U)
  4529. {
  4530. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  4531. HANDLE_OP_END();
  4532. }
  4533. HANDLE_OP(WASM_OP_I32_ROTL)
  4534. {
  4535. uint32 a, b;
  4536. b = (uint32)POP_I32();
  4537. a = (uint32)POP_I32();
  4538. PUSH_I32(rotl32(a, b));
  4539. HANDLE_OP_END();
  4540. }
  4541. HANDLE_OP(WASM_OP_I32_ROTR)
  4542. {
  4543. uint32 a, b;
  4544. b = (uint32)POP_I32();
  4545. a = (uint32)POP_I32();
  4546. PUSH_I32(rotr32(a, b));
  4547. HANDLE_OP_END();
  4548. }
  4549. /* numeric instructions of i64 */
  4550. HANDLE_OP(WASM_OP_I64_CLZ)
  4551. {
  4552. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  4553. HANDLE_OP_END();
  4554. }
  4555. HANDLE_OP(WASM_OP_I64_CTZ)
  4556. {
  4557. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  4558. HANDLE_OP_END();
  4559. }
  4560. HANDLE_OP(WASM_OP_I64_POPCNT)
  4561. {
  4562. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  4563. HANDLE_OP_END();
  4564. }
  4565. HANDLE_OP(WASM_OP_I64_ADD)
  4566. {
  4567. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  4568. HANDLE_OP_END();
  4569. }
  4570. HANDLE_OP(WASM_OP_I64_SUB)
  4571. {
  4572. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  4573. HANDLE_OP_END();
  4574. }
  4575. HANDLE_OP(WASM_OP_I64_MUL)
  4576. {
  4577. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  4578. HANDLE_OP_END();
  4579. }
  4580. HANDLE_OP(WASM_OP_I64_DIV_S)
  4581. {
  4582. int64 a, b;
  4583. b = POP_I64();
  4584. a = POP_I64();
  4585. if (a == (int64)0x8000000000000000LL && b == -1) {
  4586. wasm_set_exception(module, "integer overflow");
  4587. goto got_exception;
  4588. }
  4589. if (b == 0) {
  4590. wasm_set_exception(module, "integer divide by zero");
  4591. goto got_exception;
  4592. }
  4593. PUSH_I64(a / b);
  4594. HANDLE_OP_END();
  4595. }
  4596. HANDLE_OP(WASM_OP_I64_DIV_U)
  4597. {
  4598. uint64 a, b;
  4599. b = (uint64)POP_I64();
  4600. a = (uint64)POP_I64();
  4601. if (b == 0) {
  4602. wasm_set_exception(module, "integer divide by zero");
  4603. goto got_exception;
  4604. }
  4605. PUSH_I64(a / b);
  4606. HANDLE_OP_END();
  4607. }
  4608. HANDLE_OP(WASM_OP_I64_REM_S)
  4609. {
  4610. int64 a, b;
  4611. b = POP_I64();
  4612. a = POP_I64();
  4613. if (a == (int64)0x8000000000000000LL && b == -1) {
  4614. PUSH_I64(0);
  4615. HANDLE_OP_END();
  4616. }
  4617. if (b == 0) {
  4618. wasm_set_exception(module, "integer divide by zero");
  4619. goto got_exception;
  4620. }
  4621. PUSH_I64(a % b);
  4622. HANDLE_OP_END();
  4623. }
  4624. HANDLE_OP(WASM_OP_I64_REM_U)
  4625. {
  4626. uint64 a, b;
  4627. b = (uint64)POP_I64();
  4628. a = (uint64)POP_I64();
  4629. if (b == 0) {
  4630. wasm_set_exception(module, "integer divide by zero");
  4631. goto got_exception;
  4632. }
  4633. PUSH_I64(a % b);
  4634. HANDLE_OP_END();
  4635. }
  4636. HANDLE_OP(WASM_OP_I64_AND)
  4637. {
  4638. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  4639. HANDLE_OP_END();
  4640. }
  4641. HANDLE_OP(WASM_OP_I64_OR)
  4642. {
  4643. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  4644. HANDLE_OP_END();
  4645. }
  4646. HANDLE_OP(WASM_OP_I64_XOR)
  4647. {
  4648. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  4649. HANDLE_OP_END();
  4650. }
  4651. HANDLE_OP(WASM_OP_I64_SHL)
  4652. {
  4653. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  4654. HANDLE_OP_END();
  4655. }
  4656. HANDLE_OP(WASM_OP_I64_SHR_S)
  4657. {
  4658. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  4659. HANDLE_OP_END();
  4660. }
  4661. HANDLE_OP(WASM_OP_I64_SHR_U)
  4662. {
  4663. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  4664. HANDLE_OP_END();
  4665. }
  4666. HANDLE_OP(WASM_OP_I64_ROTL)
  4667. {
  4668. uint64 a, b;
  4669. b = (uint64)POP_I64();
  4670. a = (uint64)POP_I64();
  4671. PUSH_I64(rotl64(a, b));
  4672. HANDLE_OP_END();
  4673. }
  4674. HANDLE_OP(WASM_OP_I64_ROTR)
  4675. {
  4676. uint64 a, b;
  4677. b = (uint64)POP_I64();
  4678. a = (uint64)POP_I64();
  4679. PUSH_I64(rotr64(a, b));
  4680. HANDLE_OP_END();
  4681. }
  4682. /* numeric instructions of f32 */
  4683. HANDLE_OP(WASM_OP_F32_ABS)
  4684. {
  4685. DEF_OP_MATH(float32, F32, fabsf);
  4686. HANDLE_OP_END();
  4687. }
  4688. HANDLE_OP(WASM_OP_F32_NEG)
  4689. {
  4690. uint32 u32 = frame_sp[-1];
  4691. uint32 sign_bit = u32 & ((uint32)1 << 31);
  4692. if (sign_bit)
  4693. frame_sp[-1] = u32 & ~((uint32)1 << 31);
  4694. else
  4695. frame_sp[-1] = u32 | ((uint32)1 << 31);
  4696. HANDLE_OP_END();
  4697. }
  4698. HANDLE_OP(WASM_OP_F32_CEIL)
  4699. {
  4700. DEF_OP_MATH(float32, F32, ceilf);
  4701. HANDLE_OP_END();
  4702. }
  4703. HANDLE_OP(WASM_OP_F32_FLOOR)
  4704. {
  4705. DEF_OP_MATH(float32, F32, floorf);
  4706. HANDLE_OP_END();
  4707. }
  4708. HANDLE_OP(WASM_OP_F32_TRUNC)
  4709. {
  4710. DEF_OP_MATH(float32, F32, truncf);
  4711. HANDLE_OP_END();
  4712. }
  4713. HANDLE_OP(WASM_OP_F32_NEAREST)
  4714. {
  4715. DEF_OP_MATH(float32, F32, rintf);
  4716. HANDLE_OP_END();
  4717. }
  4718. HANDLE_OP(WASM_OP_F32_SQRT)
  4719. {
  4720. DEF_OP_MATH(float32, F32, sqrtf);
  4721. HANDLE_OP_END();
  4722. }
  4723. HANDLE_OP(WASM_OP_F32_ADD)
  4724. {
  4725. DEF_OP_NUMERIC(float32, float32, F32, +);
  4726. HANDLE_OP_END();
  4727. }
  4728. HANDLE_OP(WASM_OP_F32_SUB)
  4729. {
  4730. DEF_OP_NUMERIC(float32, float32, F32, -);
  4731. HANDLE_OP_END();
  4732. }
  4733. HANDLE_OP(WASM_OP_F32_MUL)
  4734. {
  4735. DEF_OP_NUMERIC(float32, float32, F32, *);
  4736. HANDLE_OP_END();
  4737. }
  4738. HANDLE_OP(WASM_OP_F32_DIV)
  4739. {
  4740. DEF_OP_NUMERIC(float32, float32, F32, /);
  4741. HANDLE_OP_END();
  4742. }
  4743. HANDLE_OP(WASM_OP_F32_MIN)
  4744. {
  4745. float32 a, b;
  4746. b = POP_F32();
  4747. a = POP_F32();
  4748. PUSH_F32(f32_min(a, b));
  4749. HANDLE_OP_END();
  4750. }
  4751. HANDLE_OP(WASM_OP_F32_MAX)
  4752. {
  4753. float32 a, b;
  4754. b = POP_F32();
  4755. a = POP_F32();
  4756. PUSH_F32(f32_max(a, b));
  4757. HANDLE_OP_END();
  4758. }
  4759. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  4760. {
  4761. float32 a, b;
  4762. b = POP_F32();
  4763. a = POP_F32();
  4764. PUSH_F32(local_copysignf(a, b));
  4765. HANDLE_OP_END();
  4766. }
  4767. /* numeric instructions of f64 */
  4768. HANDLE_OP(WASM_OP_F64_ABS)
  4769. {
  4770. DEF_OP_MATH(float64, F64, fabs);
  4771. HANDLE_OP_END();
  4772. }
  4773. HANDLE_OP(WASM_OP_F64_NEG)
  4774. {
  4775. uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2);
  4776. uint64 sign_bit = u64 & (((uint64)1) << 63);
  4777. if (sign_bit)
  4778. PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63)));
  4779. else
  4780. PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63)));
  4781. HANDLE_OP_END();
  4782. }
  4783. HANDLE_OP(WASM_OP_F64_CEIL)
  4784. {
  4785. DEF_OP_MATH(float64, F64, ceil);
  4786. HANDLE_OP_END();
  4787. }
  4788. HANDLE_OP(WASM_OP_F64_FLOOR)
  4789. {
  4790. DEF_OP_MATH(float64, F64, floor);
  4791. HANDLE_OP_END();
  4792. }
  4793. HANDLE_OP(WASM_OP_F64_TRUNC)
  4794. {
  4795. DEF_OP_MATH(float64, F64, trunc);
  4796. HANDLE_OP_END();
  4797. }
  4798. HANDLE_OP(WASM_OP_F64_NEAREST)
  4799. {
  4800. DEF_OP_MATH(float64, F64, rint);
  4801. HANDLE_OP_END();
  4802. }
  4803. HANDLE_OP(WASM_OP_F64_SQRT)
  4804. {
  4805. DEF_OP_MATH(float64, F64, sqrt);
  4806. HANDLE_OP_END();
  4807. }
  4808. HANDLE_OP(WASM_OP_F64_ADD)
  4809. {
  4810. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  4811. HANDLE_OP_END();
  4812. }
  4813. HANDLE_OP(WASM_OP_F64_SUB)
  4814. {
  4815. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  4816. HANDLE_OP_END();
  4817. }
  4818. HANDLE_OP(WASM_OP_F64_MUL)
  4819. {
  4820. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  4821. HANDLE_OP_END();
  4822. }
  4823. HANDLE_OP(WASM_OP_F64_DIV)
  4824. {
  4825. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  4826. HANDLE_OP_END();
  4827. }
  4828. HANDLE_OP(WASM_OP_F64_MIN)
  4829. {
  4830. float64 a, b;
  4831. b = POP_F64();
  4832. a = POP_F64();
  4833. PUSH_F64(f64_min(a, b));
  4834. HANDLE_OP_END();
  4835. }
  4836. HANDLE_OP(WASM_OP_F64_MAX)
  4837. {
  4838. float64 a, b;
  4839. b = POP_F64();
  4840. a = POP_F64();
  4841. PUSH_F64(f64_max(a, b));
  4842. HANDLE_OP_END();
  4843. }
  4844. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  4845. {
  4846. float64 a, b;
  4847. b = POP_F64();
  4848. a = POP_F64();
  4849. PUSH_F64(local_copysign(a, b));
  4850. HANDLE_OP_END();
  4851. }
  4852. /* conversions of i32 */
  4853. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  4854. {
  4855. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  4856. PUSH_I32(value);
  4857. HANDLE_OP_END();
  4858. }
  4859. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  4860. {
  4861. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  4862. since float/double values of ieee754 cannot precisely
  4863. represent all int32/uint32/int64/uint64 values, e.g.
  4864. UINT32_MAX is 4294967295, but (float32)4294967295 is
  4865. 4294967296.0f, but not 4294967295.0f. */
  4866. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  4867. HANDLE_OP_END();
  4868. }
  4869. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  4870. {
  4871. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  4872. HANDLE_OP_END();
  4873. }
  4874. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  4875. {
  4876. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  4877. /* frame_sp can't be moved in trunc function, we need to
  4878. manually adjust it if src and dst op's cell num is
  4879. different */
  4880. frame_sp--;
  4881. HANDLE_OP_END();
  4882. }
  4883. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  4884. {
  4885. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  4886. frame_sp--;
  4887. HANDLE_OP_END();
  4888. }
  4889. /* conversions of i64 */
  4890. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  4891. {
  4892. DEF_OP_CONVERT(int64, I64, int32, I32);
  4893. HANDLE_OP_END();
  4894. }
  4895. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  4896. {
  4897. DEF_OP_CONVERT(int64, I64, uint32, I32);
  4898. HANDLE_OP_END();
  4899. }
  4900. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  4901. {
  4902. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  4903. 9223372036854775808.0f, false, true);
  4904. frame_sp++;
  4905. HANDLE_OP_END();
  4906. }
  4907. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  4908. {
  4909. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  4910. frame_sp++;
  4911. HANDLE_OP_END();
  4912. }
  4913. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  4914. {
  4915. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  4916. false, true);
  4917. HANDLE_OP_END();
  4918. }
  4919. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  4920. {
  4921. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  4922. HANDLE_OP_END();
  4923. }
  4924. /* conversions of f32 */
  4925. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  4926. {
  4927. DEF_OP_CONVERT(float32, F32, int32, I32);
  4928. HANDLE_OP_END();
  4929. }
  4930. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  4931. {
  4932. DEF_OP_CONVERT(float32, F32, uint32, I32);
  4933. HANDLE_OP_END();
  4934. }
  4935. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  4936. {
  4937. DEF_OP_CONVERT(float32, F32, int64, I64);
  4938. HANDLE_OP_END();
  4939. }
  4940. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  4941. {
  4942. DEF_OP_CONVERT(float32, F32, uint64, I64);
  4943. HANDLE_OP_END();
  4944. }
  4945. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  4946. {
  4947. DEF_OP_CONVERT(float32, F32, float64, F64);
  4948. HANDLE_OP_END();
  4949. }
  4950. /* conversions of f64 */
  4951. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  4952. {
  4953. DEF_OP_CONVERT(float64, F64, int32, I32);
  4954. HANDLE_OP_END();
  4955. }
  4956. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  4957. {
  4958. DEF_OP_CONVERT(float64, F64, uint32, I32);
  4959. HANDLE_OP_END();
  4960. }
  4961. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  4962. {
  4963. DEF_OP_CONVERT(float64, F64, int64, I64);
  4964. HANDLE_OP_END();
  4965. }
  4966. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  4967. {
  4968. DEF_OP_CONVERT(float64, F64, uint64, I64);
  4969. HANDLE_OP_END();
  4970. }
  4971. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  4972. {
  4973. DEF_OP_CONVERT(float64, F64, float32, F32);
  4974. HANDLE_OP_END();
  4975. }
  4976. /* reinterpretations */
  4977. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  4978. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  4979. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  4980. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); }
  4981. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4982. {
  4983. DEF_OP_CONVERT(int32, I32, int8, I32);
  4984. HANDLE_OP_END();
  4985. }
  4986. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4987. {
  4988. DEF_OP_CONVERT(int32, I32, int16, I32);
  4989. HANDLE_OP_END();
  4990. }
  4991. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  4992. {
  4993. DEF_OP_CONVERT(int64, I64, int8, I64);
  4994. HANDLE_OP_END();
  4995. }
  4996. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  4997. {
  4998. DEF_OP_CONVERT(int64, I64, int16, I64);
  4999. HANDLE_OP_END();
  5000. }
  5001. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  5002. {
  5003. DEF_OP_CONVERT(int64, I64, int32, I64);
  5004. HANDLE_OP_END();
  5005. }
  5006. HANDLE_OP(WASM_OP_MISC_PREFIX)
  5007. {
  5008. uint32 opcode1;
  5009. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  5010. /* opcode1 was checked in loader and is no larger than
  5011. UINT8_MAX */
  5012. opcode = (uint8)opcode1;
  5013. switch (opcode) {
  5014. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5015. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  5016. true, true);
  5017. break;
  5018. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5019. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  5020. break;
  5021. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5022. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  5023. true);
  5024. frame_sp--;
  5025. break;
  5026. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5027. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  5028. frame_sp--;
  5029. break;
  5030. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5031. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  5032. 9223372036854775808.0f, false,
  5033. true);
  5034. frame_sp++;
  5035. break;
  5036. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5037. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  5038. false, false);
  5039. frame_sp++;
  5040. break;
  5041. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5042. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  5043. 9223372036854775808.0, false,
  5044. true);
  5045. break;
  5046. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5047. DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0,
  5048. false, false);
  5049. break;
  5050. #if WASM_ENABLE_BULK_MEMORY != 0
  5051. case WASM_OP_MEMORY_INIT:
  5052. {
  5053. uint32 segment;
  5054. mem_offset_t addr;
  5055. uint64 bytes, offset, seg_len;
  5056. uint8 *data;
  5057. read_leb_uint32(frame_ip, frame_ip_end, segment);
  5058. #if WASM_ENABLE_MULTI_MEMORY != 0
  5059. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5060. #else
  5061. /* skip memory index */
  5062. frame_ip++;
  5063. #endif
  5064. bytes = (uint64)(uint32)POP_I32();
  5065. offset = (uint64)(uint32)POP_I32();
  5066. addr = (mem_offset_t)POP_MEM_OFFSET();
  5067. #if WASM_ENABLE_THREAD_MGR != 0
  5068. linear_mem_size = get_linear_mem_size();
  5069. #endif
  5070. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5071. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  5072. #else
  5073. #if WASM_ENABLE_SHARED_HEAP != 0
  5074. if (app_addr_in_shared_heap((uint64)(uint32)addr,
  5075. bytes))
  5076. shared_heap_addr_app_to_native((uint64)(uint32)addr,
  5077. maddr);
  5078. else
  5079. #endif
  5080. {
  5081. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  5082. goto out_of_bounds;
  5083. maddr = memory->memory_data + (uint32)addr;
  5084. }
  5085. #endif
  5086. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  5087. segment)) {
  5088. seg_len = 0;
  5089. data = NULL;
  5090. }
  5091. else {
  5092. seg_len =
  5093. (uint64)module->module->data_segments[segment]
  5094. ->data_length;
  5095. data = module->module->data_segments[segment]->data;
  5096. }
  5097. if (offset + bytes > seg_len)
  5098. goto out_of_bounds;
  5099. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  5100. data + offset, (uint32)bytes);
  5101. break;
  5102. }
  5103. case WASM_OP_DATA_DROP:
  5104. {
  5105. uint32 segment;
  5106. read_leb_uint32(frame_ip, frame_ip_end, segment);
  5107. bh_bitmap_set_bit(module->e->common.data_dropped,
  5108. segment);
  5109. break;
  5110. }
  5111. case WASM_OP_MEMORY_COPY:
  5112. {
  5113. mem_offset_t dst, src, len;
  5114. uint8 *mdst, *msrc;
  5115. len = POP_MEM_OFFSET();
  5116. src = POP_MEM_OFFSET();
  5117. dst = POP_MEM_OFFSET();
  5118. #if WASM_ENABLE_MULTI_MEMORY != 0
  5119. /* dst memidx */
  5120. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5121. #else
  5122. /* skip dst memidx */
  5123. frame_ip += 1;
  5124. #endif
  5125. // TODO: apply memidx
  5126. #if WASM_ENABLE_THREAD_MGR != 0
  5127. linear_mem_size = get_linear_mem_size();
  5128. #endif
  5129. /* dst boundary check */
  5130. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5131. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5132. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  5133. #if WASM_ENABLE_SHARED_HEAP != 0
  5134. if (app_addr_in_shared_heap((uint64)dst, len)) {
  5135. shared_heap_addr_app_to_native((uint64)dst, mdst);
  5136. }
  5137. else
  5138. #endif
  5139. {
  5140. if ((uint64)dst + len > linear_mem_size)
  5141. goto out_of_bounds;
  5142. mdst = memory->memory_data + dst;
  5143. }
  5144. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  5145. #if WASM_ENABLE_MULTI_MEMORY != 0
  5146. /* src memidx */
  5147. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5148. #else
  5149. /* skip src memidx */
  5150. frame_ip += 1;
  5151. #endif
  5152. // TODO: apply memidx
  5153. #if WASM_ENABLE_THREAD_MGR != 0
  5154. linear_mem_size = get_linear_mem_size();
  5155. #endif
  5156. /* src boundary check */
  5157. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5158. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  5159. #else
  5160. #if WASM_ENABLE_SHARED_HEAP != 0
  5161. if (app_addr_in_shared_heap((uint64)src, len))
  5162. shared_heap_addr_app_to_native((uint64)src, msrc);
  5163. else
  5164. #endif
  5165. {
  5166. if ((uint64)src + len > linear_mem_size)
  5167. goto out_of_bounds;
  5168. msrc = memory->memory_data + src;
  5169. }
  5170. #endif
  5171. /*
  5172. * avoid unnecessary operations
  5173. *
  5174. * since dst and src both are valid indexes in the
  5175. * linear memory, mdst and msrc can't be NULL
  5176. *
  5177. * The spec. converts memory.copy into i32.load8 and
  5178. * i32.store8; the following are runtime-specific
  5179. * optimizations.
  5180. *
  5181. */
  5182. if (len && mdst != msrc) {
  5183. /* allowing the destination and source to overlap */
  5184. memmove(mdst, msrc, len);
  5185. }
  5186. break;
  5187. }
  5188. case WASM_OP_MEMORY_FILL:
  5189. {
  5190. mem_offset_t dst, len;
  5191. uint8 fill_val, *mdst;
  5192. #if WASM_ENABLE_MULTI_MEMORY != 0
  5193. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5194. #else
  5195. /* skip memory index */
  5196. frame_ip++;
  5197. #endif
  5198. len = POP_MEM_OFFSET();
  5199. fill_val = POP_I32();
  5200. dst = POP_MEM_OFFSET();
  5201. #if WASM_ENABLE_THREAD_MGR != 0
  5202. linear_mem_size = get_linear_mem_size();
  5203. #endif
  5204. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5205. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5206. #else
  5207. #if WASM_ENABLE_SHARED_HEAP != 0
  5208. if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
  5209. shared_heap_addr_app_to_native((uint64)(uint32)dst,
  5210. mdst);
  5211. else
  5212. #endif
  5213. {
  5214. if ((uint64)(uint32)dst + len > linear_mem_size)
  5215. goto out_of_bounds;
  5216. mdst = memory->memory_data + (uint32)dst;
  5217. }
  5218. #endif
  5219. memset(mdst, fill_val, len);
  5220. break;
  5221. }
  5222. #endif /* WASM_ENABLE_BULK_MEMORY */
  5223. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5224. case WASM_OP_TABLE_INIT:
  5225. {
  5226. uint32 tbl_idx;
  5227. tbl_elem_idx_t elem_idx, d;
  5228. uint32 n, s;
  5229. WASMTableInstance *tbl_inst;
  5230. table_elem_type_t *table_elems;
  5231. InitializerExpression *tbl_seg_init_values = NULL,
  5232. *init_values;
  5233. uint32 tbl_seg_len = 0;
  5234. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5235. bh_assert(elem_idx < module->module->table_seg_count);
  5236. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5237. bh_assert(tbl_idx < module->module->table_count);
  5238. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5239. #if WASM_ENABLE_MEMORY64 != 0
  5240. is_table64 = tbl_inst->is_table64;
  5241. #endif
  5242. n = (uint32)POP_I32();
  5243. s = (uint32)POP_I32();
  5244. d = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5245. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  5246. elem_idx)) {
  5247. /* table segment isn't dropped */
  5248. tbl_seg_init_values =
  5249. module->module->table_segments[elem_idx]
  5250. .init_values;
  5251. tbl_seg_len =
  5252. module->module->table_segments[elem_idx]
  5253. .value_count;
  5254. }
  5255. /* TODO: memory64 current implementation of table64
  5256. * still assumes the max table size UINT32_MAX
  5257. */
  5258. if (
  5259. #if WASM_ENABLE_MEMORY64 != 0
  5260. d > UINT32_MAX ||
  5261. #endif
  5262. offset_len_out_of_bounds(s, n, tbl_seg_len)
  5263. || offset_len_out_of_bounds((uint32)d, n,
  5264. tbl_inst->cur_size)) {
  5265. wasm_set_exception(module,
  5266. "out of bounds table access");
  5267. goto got_exception;
  5268. }
  5269. if (!n) {
  5270. break;
  5271. }
  5272. table_elems = tbl_inst->elems + d;
  5273. init_values = tbl_seg_init_values + s;
  5274. #if WASM_ENABLE_GC != 0
  5275. SYNC_ALL_TO_FRAME();
  5276. #endif
  5277. for (i = 0; i < n; i++) {
  5278. /* UINT32_MAX indicates that it is a null ref */
  5279. bh_assert(init_values[i].init_expr_type
  5280. == INIT_EXPR_TYPE_REFNULL_CONST
  5281. || init_values[i].init_expr_type
  5282. == INIT_EXPR_TYPE_FUNCREF_CONST);
  5283. #if WASM_ENABLE_GC == 0
  5284. table_elems[i] = (table_elem_type_t)init_values[i]
  5285. .u.unary.v.ref_index;
  5286. #else
  5287. if (init_values[i].u.unary.v.ref_index
  5288. != UINT32_MAX) {
  5289. if (!(func_obj = wasm_create_func_obj(
  5290. module,
  5291. init_values[i].u.unary.v.ref_index,
  5292. true, NULL, 0))) {
  5293. goto got_exception;
  5294. }
  5295. table_elems[i] = func_obj;
  5296. }
  5297. else {
  5298. table_elems[i] = NULL_REF;
  5299. }
  5300. #endif
  5301. }
  5302. break;
  5303. }
  5304. case WASM_OP_ELEM_DROP:
  5305. {
  5306. uint32 elem_idx;
  5307. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5308. bh_assert(elem_idx < module->module->table_seg_count);
  5309. bh_bitmap_set_bit(module->e->common.elem_dropped,
  5310. elem_idx);
  5311. break;
  5312. }
  5313. case WASM_OP_TABLE_COPY:
  5314. {
  5315. uint32 src_tbl_idx, dst_tbl_idx;
  5316. tbl_elem_idx_t n, s, d;
  5317. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  5318. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  5319. bh_assert(dst_tbl_idx < module->table_count);
  5320. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  5321. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  5322. bh_assert(src_tbl_idx < module->table_count);
  5323. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  5324. #if WASM_ENABLE_MEMORY64 != 0
  5325. is_table64 = src_tbl_inst->is_table64
  5326. && dst_tbl_inst->is_table64;
  5327. #endif
  5328. n = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5329. #if WASM_ENABLE_MEMORY64 != 0
  5330. is_table64 = src_tbl_inst->is_table64;
  5331. #endif
  5332. s = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5333. #if WASM_ENABLE_MEMORY64 != 0
  5334. is_table64 = dst_tbl_inst->is_table64;
  5335. #endif
  5336. d = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5337. if (
  5338. #if WASM_ENABLE_MEMORY64 != 0
  5339. n > UINT32_MAX || s > UINT32_MAX || d > UINT32_MAX
  5340. ||
  5341. #endif
  5342. offset_len_out_of_bounds((uint32)d, (uint32)n,
  5343. dst_tbl_inst->cur_size)
  5344. || offset_len_out_of_bounds(
  5345. (uint32)s, (uint32)n, src_tbl_inst->cur_size)) {
  5346. wasm_set_exception(module,
  5347. "out of bounds table access");
  5348. goto got_exception;
  5349. }
  5350. /* if s >= d, copy from front to back */
  5351. /* if s < d, copy from back to front */
  5352. /* merge all together */
  5353. bh_memmove_s((uint8 *)dst_tbl_inst
  5354. + offsetof(WASMTableInstance, elems)
  5355. + d * sizeof(table_elem_type_t),
  5356. (uint32)((dst_tbl_inst->cur_size - d)
  5357. * sizeof(table_elem_type_t)),
  5358. (uint8 *)src_tbl_inst
  5359. + offsetof(WASMTableInstance, elems)
  5360. + s * sizeof(table_elem_type_t),
  5361. (uint32)(n * sizeof(table_elem_type_t)));
  5362. break;
  5363. }
  5364. case WASM_OP_TABLE_GROW:
  5365. {
  5366. WASMTableInstance *tbl_inst;
  5367. uint32 tbl_idx, orig_tbl_sz;
  5368. tbl_elem_idx_t n;
  5369. table_elem_type_t init_val;
  5370. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5371. bh_assert(tbl_idx < module->table_count);
  5372. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5373. #if WASM_ENABLE_MEMORY64 != 0
  5374. is_table64 = tbl_inst->is_table64;
  5375. #endif
  5376. orig_tbl_sz = tbl_inst->cur_size;
  5377. n = POP_TBL_ELEM_IDX();
  5378. #if WASM_ENABLE_GC == 0
  5379. init_val = POP_I32();
  5380. #else
  5381. init_val = POP_REF();
  5382. #endif
  5383. if (
  5384. #if WASM_ENABLE_MEMORY64 != 0
  5385. n > UINT32_MAX ||
  5386. #endif
  5387. !wasm_enlarge_table(module, tbl_idx, (uint32)n,
  5388. init_val)) {
  5389. PUSH_TBL_ELEM_IDX(-1);
  5390. }
  5391. else {
  5392. PUSH_TBL_ELEM_IDX(orig_tbl_sz);
  5393. }
  5394. break;
  5395. }
  5396. case WASM_OP_TABLE_SIZE:
  5397. {
  5398. uint32 tbl_idx;
  5399. WASMTableInstance *tbl_inst;
  5400. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5401. bh_assert(tbl_idx < module->table_count);
  5402. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5403. #if WASM_ENABLE_MEMORY64 != 0
  5404. is_table64 = tbl_inst->is_table64;
  5405. #endif
  5406. PUSH_TBL_ELEM_IDX(tbl_inst->cur_size);
  5407. break;
  5408. }
  5409. case WASM_OP_TABLE_FILL:
  5410. {
  5411. uint32 tbl_idx;
  5412. tbl_elem_idx_t n, elem_idx;
  5413. WASMTableInstance *tbl_inst;
  5414. table_elem_type_t fill_val;
  5415. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5416. bh_assert(tbl_idx < module->table_count);
  5417. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5418. #if WASM_ENABLE_MEMORY64 != 0
  5419. is_table64 = tbl_inst->is_table64;
  5420. #endif
  5421. n = POP_TBL_ELEM_IDX();
  5422. #if WASM_ENABLE_GC == 0
  5423. fill_val = POP_I32();
  5424. #else
  5425. fill_val = POP_REF();
  5426. #endif
  5427. elem_idx = POP_TBL_ELEM_IDX();
  5428. if (
  5429. #if WASM_ENABLE_MEMORY64 != 0
  5430. n > UINT32_MAX || elem_idx > UINT32_MAX ||
  5431. #endif
  5432. offset_len_out_of_bounds((uint32)elem_idx,
  5433. (uint32)n,
  5434. tbl_inst->cur_size)) {
  5435. wasm_set_exception(module,
  5436. "out of bounds table access");
  5437. goto got_exception;
  5438. }
  5439. for (; n != 0; elem_idx++, n--) {
  5440. tbl_inst->elems[elem_idx] = fill_val;
  5441. }
  5442. break;
  5443. }
  5444. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5445. default:
  5446. wasm_set_exception(module, "unsupported opcode");
  5447. goto got_exception;
  5448. }
  5449. HANDLE_OP_END();
  5450. }
  5451. #if WASM_ENABLE_SHARED_MEMORY != 0
  5452. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5453. {
  5454. mem_offset_t offset = 0, addr;
  5455. uint32 align = 0;
  5456. uint32 opcode1;
  5457. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  5458. /* opcode1 was checked in loader and is no larger than
  5459. UINT8_MAX */
  5460. opcode = (uint8)opcode1;
  5461. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5462. read_leb_uint32(frame_ip, frame_ip_end, align);
  5463. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  5464. }
  5465. switch (opcode) {
  5466. case WASM_OP_ATOMIC_NOTIFY:
  5467. {
  5468. uint32 notify_count, ret;
  5469. notify_count = POP_I32();
  5470. addr = POP_MEM_OFFSET();
  5471. CHECK_MEMORY_OVERFLOW(4);
  5472. CHECK_ATOMIC_MEMORY_ACCESS();
  5473. ret = wasm_runtime_atomic_notify(
  5474. (WASMModuleInstanceCommon *)module, maddr,
  5475. notify_count);
  5476. if (ret == (uint32)-1)
  5477. goto got_exception;
  5478. PUSH_I32(ret);
  5479. break;
  5480. }
  5481. case WASM_OP_ATOMIC_WAIT32:
  5482. {
  5483. uint64 timeout;
  5484. uint32 expect, ret;
  5485. timeout = POP_I64();
  5486. expect = POP_I32();
  5487. addr = POP_MEM_OFFSET();
  5488. CHECK_MEMORY_OVERFLOW(4);
  5489. CHECK_ATOMIC_MEMORY_ACCESS();
  5490. ret = wasm_runtime_atomic_wait(
  5491. (WASMModuleInstanceCommon *)module, maddr,
  5492. (uint64)expect, timeout, false);
  5493. if (ret == (uint32)-1)
  5494. goto got_exception;
  5495. #if WASM_ENABLE_THREAD_MGR != 0
  5496. CHECK_SUSPEND_FLAGS();
  5497. #endif
  5498. PUSH_I32(ret);
  5499. break;
  5500. }
  5501. case WASM_OP_ATOMIC_WAIT64:
  5502. {
  5503. uint64 timeout, expect;
  5504. uint32 ret;
  5505. timeout = POP_I64();
  5506. expect = POP_I64();
  5507. addr = POP_MEM_OFFSET();
  5508. CHECK_MEMORY_OVERFLOW(8);
  5509. CHECK_ATOMIC_MEMORY_ACCESS();
  5510. ret = wasm_runtime_atomic_wait(
  5511. (WASMModuleInstanceCommon *)module, maddr, expect,
  5512. timeout, true);
  5513. if (ret == (uint32)-1)
  5514. goto got_exception;
  5515. #if WASM_ENABLE_THREAD_MGR != 0
  5516. CHECK_SUSPEND_FLAGS();
  5517. #endif
  5518. PUSH_I32(ret);
  5519. break;
  5520. }
  5521. case WASM_OP_ATOMIC_FENCE:
  5522. {
  5523. /* Skip the memory index */
  5524. frame_ip++;
  5525. os_atomic_thread_fence(os_memory_order_seq_cst);
  5526. break;
  5527. }
  5528. case WASM_OP_ATOMIC_I32_LOAD:
  5529. case WASM_OP_ATOMIC_I32_LOAD8_U:
  5530. case WASM_OP_ATOMIC_I32_LOAD16_U:
  5531. {
  5532. uint32 readv;
  5533. addr = POP_MEM_OFFSET();
  5534. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  5535. CHECK_MEMORY_OVERFLOW(1);
  5536. CHECK_ATOMIC_MEMORY_ACCESS();
  5537. shared_memory_lock(memory);
  5538. readv = (uint32)(*(uint8 *)maddr);
  5539. shared_memory_unlock(memory);
  5540. }
  5541. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  5542. CHECK_MEMORY_OVERFLOW(2);
  5543. CHECK_ATOMIC_MEMORY_ACCESS();
  5544. shared_memory_lock(memory);
  5545. readv = (uint32)LOAD_U16(maddr);
  5546. shared_memory_unlock(memory);
  5547. }
  5548. else {
  5549. CHECK_MEMORY_OVERFLOW(4);
  5550. CHECK_ATOMIC_MEMORY_ACCESS();
  5551. shared_memory_lock(memory);
  5552. readv = LOAD_I32(maddr);
  5553. shared_memory_unlock(memory);
  5554. }
  5555. PUSH_I32(readv);
  5556. break;
  5557. }
  5558. case WASM_OP_ATOMIC_I64_LOAD:
  5559. case WASM_OP_ATOMIC_I64_LOAD8_U:
  5560. case WASM_OP_ATOMIC_I64_LOAD16_U:
  5561. case WASM_OP_ATOMIC_I64_LOAD32_U:
  5562. {
  5563. uint64 readv;
  5564. addr = POP_MEM_OFFSET();
  5565. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  5566. CHECK_MEMORY_OVERFLOW(1);
  5567. CHECK_ATOMIC_MEMORY_ACCESS();
  5568. shared_memory_lock(memory);
  5569. readv = (uint64)(*(uint8 *)maddr);
  5570. shared_memory_unlock(memory);
  5571. }
  5572. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  5573. CHECK_MEMORY_OVERFLOW(2);
  5574. CHECK_ATOMIC_MEMORY_ACCESS();
  5575. shared_memory_lock(memory);
  5576. readv = (uint64)LOAD_U16(maddr);
  5577. shared_memory_unlock(memory);
  5578. }
  5579. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  5580. CHECK_MEMORY_OVERFLOW(4);
  5581. CHECK_ATOMIC_MEMORY_ACCESS();
  5582. shared_memory_lock(memory);
  5583. readv = (uint64)LOAD_U32(maddr);
  5584. shared_memory_unlock(memory);
  5585. }
  5586. else {
  5587. CHECK_MEMORY_OVERFLOW(8);
  5588. CHECK_ATOMIC_MEMORY_ACCESS();
  5589. shared_memory_lock(memory);
  5590. readv = LOAD_I64(maddr);
  5591. shared_memory_unlock(memory);
  5592. }
  5593. PUSH_I64(readv);
  5594. break;
  5595. }
  5596. case WASM_OP_ATOMIC_I32_STORE:
  5597. case WASM_OP_ATOMIC_I32_STORE8:
  5598. case WASM_OP_ATOMIC_I32_STORE16:
  5599. {
  5600. uint32 sval;
  5601. sval = (uint32)POP_I32();
  5602. addr = POP_MEM_OFFSET();
  5603. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  5604. CHECK_MEMORY_OVERFLOW(1);
  5605. CHECK_ATOMIC_MEMORY_ACCESS();
  5606. shared_memory_lock(memory);
  5607. *(uint8 *)maddr = (uint8)sval;
  5608. shared_memory_unlock(memory);
  5609. }
  5610. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  5611. CHECK_MEMORY_OVERFLOW(2);
  5612. CHECK_ATOMIC_MEMORY_ACCESS();
  5613. shared_memory_lock(memory);
  5614. STORE_U16(maddr, (uint16)sval);
  5615. shared_memory_unlock(memory);
  5616. }
  5617. else {
  5618. CHECK_MEMORY_OVERFLOW(4);
  5619. CHECK_ATOMIC_MEMORY_ACCESS();
  5620. shared_memory_lock(memory);
  5621. STORE_U32(maddr, sval);
  5622. shared_memory_unlock(memory);
  5623. }
  5624. break;
  5625. }
  5626. case WASM_OP_ATOMIC_I64_STORE:
  5627. case WASM_OP_ATOMIC_I64_STORE8:
  5628. case WASM_OP_ATOMIC_I64_STORE16:
  5629. case WASM_OP_ATOMIC_I64_STORE32:
  5630. {
  5631. uint64 sval;
  5632. sval = (uint64)POP_I64();
  5633. addr = POP_MEM_OFFSET();
  5634. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  5635. CHECK_MEMORY_OVERFLOW(1);
  5636. CHECK_ATOMIC_MEMORY_ACCESS();
  5637. shared_memory_lock(memory);
  5638. *(uint8 *)maddr = (uint8)sval;
  5639. shared_memory_unlock(memory);
  5640. }
  5641. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  5642. CHECK_MEMORY_OVERFLOW(2);
  5643. CHECK_ATOMIC_MEMORY_ACCESS();
  5644. shared_memory_lock(memory);
  5645. STORE_U16(maddr, (uint16)sval);
  5646. shared_memory_unlock(memory);
  5647. }
  5648. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  5649. CHECK_MEMORY_OVERFLOW(4);
  5650. CHECK_ATOMIC_MEMORY_ACCESS();
  5651. shared_memory_lock(memory);
  5652. STORE_U32(maddr, (uint32)sval);
  5653. shared_memory_unlock(memory);
  5654. }
  5655. else {
  5656. CHECK_MEMORY_OVERFLOW(8);
  5657. CHECK_ATOMIC_MEMORY_ACCESS();
  5658. shared_memory_lock(memory);
  5659. STORE_I64(maddr, sval);
  5660. shared_memory_unlock(memory);
  5661. }
  5662. break;
  5663. }
  5664. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  5665. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  5666. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  5667. {
  5668. uint32 readv, sval, expect;
  5669. sval = POP_I32();
  5670. expect = POP_I32();
  5671. addr = POP_MEM_OFFSET();
  5672. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  5673. CHECK_MEMORY_OVERFLOW(1);
  5674. CHECK_ATOMIC_MEMORY_ACCESS();
  5675. expect = (uint8)expect;
  5676. shared_memory_lock(memory);
  5677. readv = (uint32)(*(uint8 *)maddr);
  5678. if (readv == expect)
  5679. *(uint8 *)maddr = (uint8)(sval);
  5680. shared_memory_unlock(memory);
  5681. }
  5682. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  5683. CHECK_MEMORY_OVERFLOW(2);
  5684. CHECK_ATOMIC_MEMORY_ACCESS();
  5685. expect = (uint16)expect;
  5686. shared_memory_lock(memory);
  5687. readv = (uint32)LOAD_U16(maddr);
  5688. if (readv == expect)
  5689. STORE_U16(maddr, (uint16)(sval));
  5690. shared_memory_unlock(memory);
  5691. }
  5692. else {
  5693. CHECK_MEMORY_OVERFLOW(4);
  5694. CHECK_ATOMIC_MEMORY_ACCESS();
  5695. shared_memory_lock(memory);
  5696. readv = LOAD_I32(maddr);
  5697. if (readv == expect)
  5698. STORE_U32(maddr, sval);
  5699. shared_memory_unlock(memory);
  5700. }
  5701. PUSH_I32(readv);
  5702. break;
  5703. }
  5704. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  5705. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  5706. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  5707. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  5708. {
  5709. uint64 readv, sval, expect;
  5710. sval = (uint64)POP_I64();
  5711. expect = (uint64)POP_I64();
  5712. addr = POP_MEM_OFFSET();
  5713. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5714. CHECK_MEMORY_OVERFLOW(1);
  5715. CHECK_ATOMIC_MEMORY_ACCESS();
  5716. expect = (uint8)expect;
  5717. shared_memory_lock(memory);
  5718. readv = (uint64)(*(uint8 *)maddr);
  5719. if (readv == expect)
  5720. *(uint8 *)maddr = (uint8)(sval);
  5721. shared_memory_unlock(memory);
  5722. }
  5723. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5724. CHECK_MEMORY_OVERFLOW(2);
  5725. CHECK_ATOMIC_MEMORY_ACCESS();
  5726. expect = (uint16)expect;
  5727. shared_memory_lock(memory);
  5728. readv = (uint64)LOAD_U16(maddr);
  5729. if (readv == expect)
  5730. STORE_U16(maddr, (uint16)(sval));
  5731. shared_memory_unlock(memory);
  5732. }
  5733. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5734. CHECK_MEMORY_OVERFLOW(4);
  5735. CHECK_ATOMIC_MEMORY_ACCESS();
  5736. expect = (uint32)expect;
  5737. shared_memory_lock(memory);
  5738. readv = (uint64)LOAD_U32(maddr);
  5739. if (readv == expect)
  5740. STORE_U32(maddr, (uint32)(sval));
  5741. shared_memory_unlock(memory);
  5742. }
  5743. else {
  5744. CHECK_MEMORY_OVERFLOW(8);
  5745. CHECK_ATOMIC_MEMORY_ACCESS();
  5746. shared_memory_lock(memory);
  5747. readv = (uint64)LOAD_I64(maddr);
  5748. if (readv == expect)
  5749. STORE_I64(maddr, sval);
  5750. shared_memory_unlock(memory);
  5751. }
  5752. PUSH_I64(readv);
  5753. break;
  5754. }
  5755. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5756. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5757. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5758. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5759. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5760. /* xchg, ignore the read value, and store the given
  5761. value: readv * 0 + sval */
  5762. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5763. }
  5764. HANDLE_OP_END();
  5765. }
  5766. #endif
  5767. HANDLE_OP(WASM_OP_IMPDEP)
  5768. {
  5769. frame = prev_frame;
  5770. frame_ip = frame->ip;
  5771. frame_sp = frame->sp;
  5772. frame_csp = frame->csp;
  5773. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5774. is_return_call = false;
  5775. #endif
  5776. goto call_func_from_entry;
  5777. }
  5778. #if WASM_ENABLE_DEBUG_INTERP != 0
  5779. HANDLE_OP(DEBUG_OP_BREAK)
  5780. {
  5781. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  5782. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  5783. WASM_SUSPEND_FLAG_SUSPEND);
  5784. frame_ip--;
  5785. SYNC_ALL_TO_FRAME();
  5786. CHECK_SUSPEND_FLAGS();
  5787. HANDLE_OP_END();
  5788. }
  5789. #endif
  5790. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5791. default:
  5792. wasm_set_exception(module, "unsupported opcode");
  5793. goto got_exception;
  5794. }
  5795. #endif
  5796. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5797. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  5798. #if WASM_ENABLE_TAIL_CALL == 0
  5799. HANDLE_OP(WASM_OP_RETURN_CALL)
  5800. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  5801. #endif
  5802. #if WASM_ENABLE_SHARED_MEMORY == 0
  5803. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5804. #endif
  5805. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5806. HANDLE_OP(WASM_OP_SELECT_T)
  5807. HANDLE_OP(WASM_OP_TABLE_GET)
  5808. HANDLE_OP(WASM_OP_TABLE_SET)
  5809. HANDLE_OP(WASM_OP_REF_NULL)
  5810. HANDLE_OP(WASM_OP_REF_IS_NULL)
  5811. HANDLE_OP(WASM_OP_REF_FUNC)
  5812. #endif
  5813. #if WASM_ENABLE_GC == 0
  5814. HANDLE_OP(WASM_OP_CALL_REF)
  5815. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  5816. HANDLE_OP(WASM_OP_REF_EQ)
  5817. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  5818. HANDLE_OP(WASM_OP_BR_ON_NULL)
  5819. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  5820. HANDLE_OP(WASM_OP_GC_PREFIX)
  5821. #endif
  5822. #if WASM_ENABLE_EXCE_HANDLING == 0
  5823. HANDLE_OP(WASM_OP_TRY)
  5824. HANDLE_OP(WASM_OP_CATCH)
  5825. HANDLE_OP(WASM_OP_THROW)
  5826. HANDLE_OP(WASM_OP_RETHROW)
  5827. HANDLE_OP(WASM_OP_DELEGATE)
  5828. HANDLE_OP(WASM_OP_CATCH_ALL)
  5829. HANDLE_OP(EXT_OP_TRY)
  5830. #endif
  5831. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0
  5832. /* SIMD isn't supported by interpreter, but when JIT is
  5833. enabled, `iwasm --interp <wasm_file>` may be run to
  5834. trigger the SIMD opcode in interpreter */
  5835. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  5836. #endif
  5837. HANDLE_OP(WASM_OP_UNUSED_0x16)
  5838. HANDLE_OP(WASM_OP_UNUSED_0x17)
  5839. HANDLE_OP(WASM_OP_UNUSED_0x27)
  5840. /* Used by fast interpreter */
  5841. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  5842. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  5843. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  5844. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  5845. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  5846. {
  5847. wasm_set_exception(module, "unsupported opcode");
  5848. goto got_exception;
  5849. }
  5850. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES != 0 */
  5851. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5852. continue;
  5853. #else
  5854. FETCH_OPCODE_AND_DISPATCH();
  5855. #endif
  5856. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5857. call_func_from_return_call:
  5858. {
  5859. POP(cur_func->param_cell_num);
  5860. if (cur_func->param_cell_num > 0) {
  5861. word_copy(frame->lp, frame_sp, cur_func->param_cell_num);
  5862. }
  5863. FREE_FRAME(exec_env, frame);
  5864. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  5865. is_return_call = true;
  5866. goto call_func_from_entry;
  5867. }
  5868. #endif
  5869. call_func_from_interp:
  5870. {
  5871. /* Only do the copy when it's called from interpreter. */
  5872. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  5873. if (cur_func->param_cell_num > 0) {
  5874. POP(cur_func->param_cell_num);
  5875. word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num);
  5876. }
  5877. SYNC_ALL_TO_FRAME();
  5878. prev_frame = frame;
  5879. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5880. is_return_call = false;
  5881. #endif
  5882. }
  5883. call_func_from_entry:
  5884. {
  5885. if (cur_func->is_import_func) {
  5886. #if WASM_ENABLE_MULTI_MODULE != 0
  5887. if (cur_func->import_func_inst) {
  5888. wasm_interp_call_func_import(module, exec_env, cur_func,
  5889. prev_frame);
  5890. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5891. if (is_return_call) {
  5892. /* the frame was freed before tail calling and
  5893. the prev_frame was set as exec_env's cur_frame,
  5894. so here we recover context from prev_frame */
  5895. RECOVER_CONTEXT(prev_frame);
  5896. }
  5897. else
  5898. #endif
  5899. {
  5900. prev_frame = frame->prev_frame;
  5901. cur_func = frame->function;
  5902. UPDATE_ALL_FROM_FRAME();
  5903. }
  5904. #if WASM_ENABLE_EXCE_HANDLING != 0
  5905. char uncaught_exception[128] = { 0 };
  5906. bool has_exception =
  5907. wasm_copy_exception(module, uncaught_exception);
  5908. if (has_exception
  5909. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5910. uint32 import_exception;
  5911. /* initialize imported exception index to be invalid */
  5912. SET_INVALID_TAGINDEX(import_exception);
  5913. /* pull external exception */
  5914. uint32 ext_exception = POP_I32();
  5915. /* external function came back with an exception or trap */
  5916. /* lookup exception in import tags */
  5917. WASMTagInstance *tag = module->e->tags;
  5918. for (uint32 t = 0; t < module->module->import_tag_count;
  5919. tag++, t++) {
  5920. /* compare the module and the external index with the
  5921. * import tag data */
  5922. if ((cur_func->u.func_import->import_module
  5923. == tag->u.tag_import->import_module)
  5924. && (ext_exception
  5925. == tag->u.tag_import
  5926. ->import_tag_index_linked)) {
  5927. /* set the import_exception to the import tag */
  5928. import_exception = t;
  5929. break;
  5930. }
  5931. }
  5932. /*
  5933. * exchange the thrown exception (index valid in submodule)
  5934. * with the imported exception index (valid in this module)
  5935. * if the module did not import the exception,
  5936. * that results in a "INVALID_TAGINDEX", that triggers
  5937. * an CATCH_ALL block, if there is one.
  5938. */
  5939. PUSH_I32(import_exception);
  5940. }
  5941. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  5942. }
  5943. else
  5944. #endif /* end of WASM_ENABLE_MULTI_MODULE != 0 */
  5945. {
  5946. wasm_interp_call_func_native(module, exec_env, cur_func,
  5947. prev_frame);
  5948. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5949. if (is_return_call) {
  5950. /* the frame was freed before tail calling and
  5951. the prev_frame was set as exec_env's cur_frame,
  5952. so here we recover context from prev_frame */
  5953. RECOVER_CONTEXT(prev_frame);
  5954. }
  5955. else
  5956. #endif
  5957. {
  5958. prev_frame = frame->prev_frame;
  5959. cur_func = frame->function;
  5960. UPDATE_ALL_FROM_FRAME();
  5961. }
  5962. }
  5963. /* update memory size, no need to update memory ptr as
  5964. it isn't changed in wasm_enlarge_memory */
  5965. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5966. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5967. || WASM_ENABLE_BULK_MEMORY != 0
  5968. if (memory)
  5969. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  5970. #endif
  5971. if (wasm_copy_exception(module, NULL)) {
  5972. #if WASM_ENABLE_EXCE_HANDLING != 0
  5973. /* the caller raised an exception */
  5974. char uncaught_exception[128] = { 0 };
  5975. bool has_exception =
  5976. wasm_copy_exception(module, uncaught_exception);
  5977. /* libc_builtin signaled a "exception thrown by stdc++" trap */
  5978. if (has_exception
  5979. && strstr(uncaught_exception,
  5980. "exception thrown by stdc++")) {
  5981. wasm_set_exception(module, NULL);
  5982. /* setup internal c++ rethrow */
  5983. exception_tag_index = 0;
  5984. goto find_a_catch_handler;
  5985. }
  5986. /* when throw hits the end of a function it signals with a
  5987. * "uncaught wasm exception" trap */
  5988. if (has_exception
  5989. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5990. wasm_set_exception(module, NULL);
  5991. exception_tag_index = POP_I32();
  5992. /* rethrow the exception into that frame */
  5993. goto find_a_catch_handler;
  5994. }
  5995. #endif /* WASM_ENABLE_EXCE_HANDLING != 0 */
  5996. goto got_exception;
  5997. }
  5998. }
  5999. else {
  6000. WASMFunction *cur_wasm_func = cur_func->u.func;
  6001. WASMFuncType *func_type = cur_wasm_func->func_type;
  6002. uint32 max_stack_cell_num = cur_wasm_func->max_stack_cell_num;
  6003. uint32 cell_num_of_local_stack;
  6004. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6005. uint32 local_cell_idx;
  6006. #endif
  6007. #if WASM_ENABLE_EXCE_HANDLING != 0
  6008. /* account for exception handlers, bundle them here */
  6009. uint32 eh_size =
  6010. cur_wasm_func->exception_handler_count * sizeof(uint8 *);
  6011. max_stack_cell_num += eh_size;
  6012. #endif
  6013. cell_num_of_local_stack = cur_func->param_cell_num
  6014. + cur_func->local_cell_num
  6015. + max_stack_cell_num;
  6016. all_cell_num = cell_num_of_local_stack
  6017. + cur_wasm_func->max_block_num
  6018. * (uint32)sizeof(WASMBranchBlock) / 4;
  6019. #if WASM_ENABLE_GC != 0
  6020. /* area of frame_ref */
  6021. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  6022. #endif
  6023. /* param_cell_num, local_cell_num, max_stack_cell_num and
  6024. max_block_num are all no larger than UINT16_MAX (checked
  6025. in loader), all_cell_num must be smaller than 1MB */
  6026. bh_assert(all_cell_num < 1 * BH_MB);
  6027. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6028. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  6029. frame = prev_frame;
  6030. goto got_exception;
  6031. }
  6032. /* Initialize the interpreter context. */
  6033. frame->function = cur_func;
  6034. frame_ip = wasm_get_func_code(cur_func);
  6035. frame_ip_end = wasm_get_func_code_end(cur_func);
  6036. frame_lp = frame->lp;
  6037. frame_sp = frame->sp_bottom =
  6038. frame_lp + cur_func->param_cell_num + cur_func->local_cell_num;
  6039. frame->sp_boundary = frame->sp_bottom + max_stack_cell_num;
  6040. frame_csp = frame->csp_bottom =
  6041. (WASMBranchBlock *)frame->sp_boundary;
  6042. frame->csp_boundary =
  6043. frame->csp_bottom + cur_wasm_func->max_block_num;
  6044. #if WASM_ENABLE_GC != 0
  6045. /* frame->sp and frame->ip are used during GC root set enumeration,
  6046. * so we must initialized these fields here */
  6047. frame->sp = frame_sp;
  6048. frame->ip = frame_ip;
  6049. frame_ref = (uint8 *)frame->csp_boundary;
  6050. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  6051. cur_func);
  6052. #endif
  6053. /* Initialize the local variables */
  6054. memset(frame_lp + cur_func->param_cell_num, 0,
  6055. (uint32)(cur_func->local_cell_num * 4));
  6056. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6057. /* externref/funcref should be NULL_REF rather than 0 */
  6058. local_cell_idx = cur_func->param_cell_num;
  6059. for (i = 0; i < cur_wasm_func->local_count; i++) {
  6060. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  6061. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  6062. *(frame_lp + local_cell_idx) = NULL_REF;
  6063. }
  6064. local_cell_idx +=
  6065. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  6066. }
  6067. #endif
  6068. /* Push function block as first block */
  6069. cell_num = func_type->ret_cell_num;
  6070. PUSH_CSP(LABEL_TYPE_FUNCTION, 0, cell_num, frame_ip_end - 1);
  6071. wasm_exec_env_set_cur_frame(exec_env, frame);
  6072. }
  6073. #if WASM_ENABLE_THREAD_MGR != 0
  6074. CHECK_SUSPEND_FLAGS();
  6075. #endif
  6076. HANDLE_OP_END();
  6077. }
  6078. return_func:
  6079. {
  6080. FREE_FRAME(exec_env, frame);
  6081. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6082. if (!prev_frame->ip) {
  6083. /* Called from native. */
  6084. return;
  6085. }
  6086. RECOVER_CONTEXT(prev_frame);
  6087. #if WASM_ENABLE_EXCE_HANDLING != 0
  6088. if (wasm_get_exception(module)) {
  6089. wasm_set_exception(module, NULL);
  6090. exception_tag_index = POP_I32();
  6091. goto find_a_catch_handler;
  6092. }
  6093. #endif
  6094. HANDLE_OP_END();
  6095. }
  6096. #if WASM_ENABLE_SHARED_MEMORY != 0
  6097. unaligned_atomic:
  6098. wasm_set_exception(module, "unaligned atomic");
  6099. goto got_exception;
  6100. #endif
  6101. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  6102. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  6103. || WASM_ENABLE_BULK_MEMORY != 0
  6104. out_of_bounds:
  6105. wasm_set_exception(module, "out of bounds memory access");
  6106. #endif
  6107. got_exception:
  6108. #if WASM_ENABLE_DEBUG_INTERP != 0
  6109. if (wasm_exec_env_get_instance(exec_env) != NULL) {
  6110. uint8 *frame_ip_temp = frame_ip;
  6111. frame_ip = frame_ip_orig;
  6112. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  6113. CHECK_SUSPEND_FLAGS();
  6114. frame_ip = frame_ip_temp;
  6115. }
  6116. #endif
  6117. SYNC_ALL_TO_FRAME();
  6118. return;
  6119. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6120. }
  6121. #else
  6122. FETCH_OPCODE_AND_DISPATCH();
  6123. #endif
  6124. }
  6125. #if WASM_ENABLE_GC != 0
  6126. bool
  6127. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  6128. {
  6129. WASMInterpFrame *frame;
  6130. WASMObjectRef gc_obj;
  6131. int i;
  6132. frame = wasm_exec_env_get_cur_frame(exec_env);
  6133. for (; frame; frame = frame->prev_frame) {
  6134. uint8 *frame_ref = get_frame_ref(frame);
  6135. for (i = 0; i < frame->sp - frame->lp; i++) {
  6136. if (frame_ref[i]) {
  6137. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  6138. if (wasm_obj_is_created_from_heap(gc_obj)) {
  6139. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  6140. return false;
  6141. }
  6142. }
  6143. #if UINTPTR_MAX == UINT64_MAX
  6144. bh_assert(frame_ref[i + 1]);
  6145. i++;
  6146. #endif
  6147. }
  6148. }
  6149. }
  6150. return true;
  6151. }
  6152. #endif
  6153. #if WASM_ENABLE_FAST_JIT != 0
  6154. /*
  6155. * ASAN is not designed to work with custom stack unwind or other low-level
  6156. * things. Ignore a function that does some low-level magic. (e.g. walking
  6157. * through the thread's stack bypassing the frame boundaries)
  6158. */
  6159. #if defined(__GNUC__) || defined(__clang__)
  6160. __attribute__((no_sanitize_address))
  6161. #endif
  6162. static void
  6163. fast_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  6164. WASMExecEnv *exec_env,
  6165. WASMFunctionInstance *function,
  6166. WASMInterpFrame *frame)
  6167. {
  6168. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  6169. JitInterpSwitchInfo info;
  6170. WASMModule *module = module_inst->module;
  6171. WASMFuncType *func_type = function->u.func->func_type;
  6172. uint8 type = func_type->result_count
  6173. ? func_type->types[func_type->param_count]
  6174. : VALUE_TYPE_VOID;
  6175. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6176. uint32 func_idx_non_import = func_idx - module->import_function_count;
  6177. int32 action;
  6178. #if WASM_ENABLE_REF_TYPES != 0
  6179. if (type == VALUE_TYPE_EXTERNREF || type == VALUE_TYPE_FUNCREF)
  6180. type = VALUE_TYPE_I32;
  6181. #endif
  6182. #if WASM_ENABLE_LAZY_JIT != 0
  6183. if (!jit_compiler_compile(module, func_idx)) {
  6184. wasm_set_exception(module_inst, "failed to compile fast jit function");
  6185. return;
  6186. }
  6187. #endif
  6188. bh_assert(jit_compiler_is_compiled(module, func_idx));
  6189. /* Switch to jitted code to call the jit function */
  6190. info.out.ret.last_return_type = type;
  6191. info.frame = frame;
  6192. frame->jitted_return_addr =
  6193. (uint8 *)jit_globals->return_to_interp_from_jitted;
  6194. action = jit_interp_switch_to_jitted(
  6195. exec_env, &info, func_idx,
  6196. module_inst->fast_jit_func_ptrs[func_idx_non_import]);
  6197. bh_assert(action == JIT_INTERP_ACTION_NORMAL
  6198. || (action == JIT_INTERP_ACTION_THROWN
  6199. && wasm_copy_exception(
  6200. (WASMModuleInstance *)exec_env->module_inst, NULL)));
  6201. /* Get the return values form info.out.ret */
  6202. if (func_type->result_count) {
  6203. switch (type) {
  6204. case VALUE_TYPE_I32:
  6205. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  6206. break;
  6207. case VALUE_TYPE_I64:
  6208. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  6209. *(frame->sp - function->ret_cell_num + 1) =
  6210. info.out.ret.ival[1];
  6211. break;
  6212. case VALUE_TYPE_F32:
  6213. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6214. break;
  6215. case VALUE_TYPE_F64:
  6216. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6217. *(frame->sp - function->ret_cell_num + 1) =
  6218. info.out.ret.fval[1];
  6219. break;
  6220. default:
  6221. bh_assert(0);
  6222. break;
  6223. }
  6224. }
  6225. (void)action;
  6226. (void)func_idx;
  6227. }
  6228. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  6229. #if WASM_ENABLE_JIT != 0
  6230. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0 \
  6231. || WASM_ENABLE_AOT_STACK_FRAME != 0
  6232. #if WASM_ENABLE_GC == 0
  6233. bool
  6234. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6235. {
  6236. WASMModuleInstance *module_inst =
  6237. (WASMModuleInstance *)exec_env->module_inst;
  6238. WASMInterpFrame *cur_frame, *frame;
  6239. uint32 size = (uint32)offsetof(WASMInterpFrame, lp);
  6240. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  6241. cur_frame = exec_env->cur_frame;
  6242. if (!cur_frame)
  6243. frame = (WASMInterpFrame *)exec_env->wasm_stack.bottom;
  6244. else
  6245. frame = (WASMInterpFrame *)((uint8 *)cur_frame + size);
  6246. if ((uint8 *)frame + size > exec_env->wasm_stack.top_boundary) {
  6247. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6248. return false;
  6249. }
  6250. frame->function = module_inst->e->functions + func_index;
  6251. /* No need to initialize ip, it will be committed in jitted code
  6252. when needed */
  6253. /* frame->ip = NULL; */
  6254. frame->prev_frame = cur_frame;
  6255. #if WASM_ENABLE_PERF_PROFILING != 0
  6256. frame->time_started = os_time_thread_cputime_us();
  6257. #endif
  6258. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6259. {
  6260. uint32 wasm_stack_used =
  6261. (uint8 *)frame + size - exec_env->wasm_stack.bottom;
  6262. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6263. exec_env->max_wasm_stack_used = wasm_stack_used;
  6264. }
  6265. #endif
  6266. exec_env->cur_frame = frame;
  6267. return true;
  6268. }
  6269. static inline void
  6270. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6271. {
  6272. WASMInterpFrame *frame = exec_env->cur_frame;
  6273. WASMInterpFrame *prev_frame = frame->prev_frame;
  6274. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6275. #if WASM_ENABLE_PERF_PROFILING != 0
  6276. if (frame->function) {
  6277. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6278. frame->function->total_exec_time += time_elapsed;
  6279. frame->function->total_exec_cnt++;
  6280. /* parent function */
  6281. if (prev_frame)
  6282. prev_frame->function->children_exec_time += time_elapsed;
  6283. }
  6284. #endif
  6285. exec_env->cur_frame = prev_frame;
  6286. }
  6287. void
  6288. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6289. {
  6290. llvm_jit_free_frame_internal(exec_env);
  6291. }
  6292. #else /* else of WASM_ENABLE_GC == 0 */
  6293. bool
  6294. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6295. {
  6296. WASMModuleInstance *module_inst;
  6297. WASMModule *module;
  6298. WASMInterpFrame *frame;
  6299. uint32 size, max_local_cell_num, max_stack_cell_num;
  6300. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6301. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  6302. module = module_inst->module;
  6303. if (func_index >= func_index - module->import_function_count) {
  6304. WASMFunction *func =
  6305. module->functions[func_index - module->import_function_count];
  6306. max_local_cell_num = func->param_cell_num + func->local_cell_num;
  6307. max_stack_cell_num = func->max_stack_cell_num;
  6308. }
  6309. else {
  6310. WASMFunctionImport *func =
  6311. &((module->import_functions + func_index)->u.function);
  6312. max_local_cell_num = func->func_type->param_cell_num > 2
  6313. ? func->func_type->param_cell_num
  6314. : 2;
  6315. max_stack_cell_num = 0;
  6316. }
  6317. size =
  6318. wasm_interp_interp_frame_size(max_local_cell_num + max_stack_cell_num);
  6319. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  6320. if (!frame) {
  6321. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6322. return false;
  6323. }
  6324. frame->function = module_inst->e->functions + func_index;
  6325. #if WASM_ENABLE_PERF_PROFILING != 0
  6326. frame->time_started = os_time_thread_cputime_us();
  6327. #endif
  6328. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6329. /* No need to initialize ip, it will be committed in jitted code
  6330. when needed */
  6331. /* frame->ip = NULL; */
  6332. #if WASM_ENABLE_GC != 0
  6333. frame->sp = frame->lp + max_local_cell_num;
  6334. /* Initialize frame ref flags for import function */
  6335. if (func_index < module->import_function_count) {
  6336. WASMFunctionImport *func =
  6337. &((module->import_functions + func_index)->u.function);
  6338. WASMFuncType *func_type = func->func_type;
  6339. /* native function doesn't have operand stack and label stack */
  6340. uint8 *frame_ref = (uint8 *)frame->sp;
  6341. uint32 i, j, k, value_type_cell_num;
  6342. for (i = 0, j = 0; i < func_type->param_count; i++) {
  6343. if (wasm_is_type_reftype(func_type->types[i])
  6344. && !wasm_is_reftype_i31ref(func_type->types[i])) {
  6345. frame_ref[j++] = 1;
  6346. #if UINTPTR_MAX == UINT64_MAX
  6347. frame_ref[j++] = 1;
  6348. #endif
  6349. }
  6350. else {
  6351. value_type_cell_num =
  6352. wasm_value_type_cell_num(func_type->types[i]);
  6353. for (k = 0; k < value_type_cell_num; k++)
  6354. frame_ref[j++] = 0;
  6355. }
  6356. }
  6357. }
  6358. #endif
  6359. wasm_exec_env_set_cur_frame(exec_env, frame);
  6360. return true;
  6361. }
  6362. static inline void
  6363. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6364. {
  6365. WASMInterpFrame *frame;
  6366. WASMInterpFrame *prev_frame;
  6367. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6368. frame = wasm_exec_env_get_cur_frame(exec_env);
  6369. prev_frame = frame->prev_frame;
  6370. #if WASM_ENABLE_PERF_PROFILING != 0
  6371. if (frame->function) {
  6372. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6373. frame->function->total_exec_time += time_elapsed;
  6374. frame->function->total_exec_cnt++;
  6375. /* parent function */
  6376. if (prev_frame)
  6377. prev_frame->function->children_exec_time += time_elapsed;
  6378. }
  6379. #endif
  6380. wasm_exec_env_free_wasm_frame(exec_env, frame);
  6381. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6382. }
  6383. void
  6384. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6385. {
  6386. llvm_jit_free_frame_internal(exec_env);
  6387. }
  6388. #endif /* end of WASM_ENABLE_GC == 0 */
  6389. void
  6390. llvm_jit_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame)
  6391. {
  6392. #if WASM_ENABLE_PERF_PROFILING != 0
  6393. WASMInterpFrame *cur_frame = exec_env->cur_frame;
  6394. if (alloc_frame) {
  6395. cur_frame->time_started = os_time_thread_cputime_us();
  6396. }
  6397. else {
  6398. if (cur_frame->function) {
  6399. WASMInterpFrame *prev_frame = cur_frame->prev_frame;
  6400. uint64 time_elapsed =
  6401. os_time_thread_cputime_us() - cur_frame->time_started;
  6402. cur_frame->function->total_exec_time += time_elapsed;
  6403. cur_frame->function->total_exec_cnt++;
  6404. /* parent function */
  6405. if (prev_frame)
  6406. prev_frame->function->children_exec_time += time_elapsed;
  6407. }
  6408. }
  6409. #endif
  6410. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6411. if (alloc_frame) {
  6412. #if WASM_ENABLE_GC == 0
  6413. uint32 wasm_stack_used = (uint8 *)exec_env->cur_frame
  6414. + (uint32)offsetof(WASMInterpFrame, lp)
  6415. - exec_env->wasm_stack.bottom;
  6416. #else
  6417. uint32 wasm_stack_used =
  6418. exec_env->wasm_stack.top - exec_env->wasm_stack.bottom;
  6419. #endif
  6420. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6421. exec_env->max_wasm_stack_used = wasm_stack_used;
  6422. }
  6423. #endif
  6424. }
  6425. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  6426. || WASM_ENABLE_PERF_PROFILING != 0 \
  6427. || WASM_ENABLE_AOT_STACK_FRAME != 0 */
  6428. static bool
  6429. llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  6430. WASMExecEnv *exec_env,
  6431. WASMFunctionInstance *function, uint32 argc,
  6432. uint32 argv[])
  6433. {
  6434. WASMFuncType *func_type = function->u.func->func_type;
  6435. uint32 result_count = func_type->result_count;
  6436. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  6437. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6438. bool ret = false;
  6439. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6440. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6441. if (!llvm_jit_alloc_frame(exec_env, function - module_inst->e->functions)) {
  6442. /* wasm operand stack overflow has been thrown,
  6443. no need to throw again */
  6444. return false;
  6445. }
  6446. #endif
  6447. if (ext_ret_count > 0) {
  6448. uint32 cell_num = 0, i;
  6449. uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;
  6450. uint32 argv1_buf[32], *argv1 = argv1_buf, *ext_rets = NULL;
  6451. uint32 *argv_ret = argv;
  6452. uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  6453. uint64 size;
  6454. /* Allocate memory all arguments */
  6455. size =
  6456. sizeof(uint32) * (uint64)argc /* original arguments */
  6457. + sizeof(void *)
  6458. * (uint64)ext_ret_count /* extra result values' addr */
  6459. + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */
  6460. if (size > sizeof(argv1_buf)) {
  6461. if (size > UINT32_MAX
  6462. || !(argv1 = wasm_runtime_malloc((uint32)size))) {
  6463. wasm_set_exception(module_inst, "allocate memory failed");
  6464. ret = false;
  6465. goto fail;
  6466. }
  6467. }
  6468. /* Copy original arguments */
  6469. bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc);
  6470. /* Get the extra result value's address */
  6471. ext_rets =
  6472. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6473. /* Append each extra result value's address to original arguments */
  6474. for (i = 0; i < ext_ret_count; i++) {
  6475. *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) =
  6476. (uintptr_t)(ext_rets + cell_num);
  6477. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  6478. }
  6479. ret = wasm_runtime_invoke_native(
  6480. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL, NULL,
  6481. argv1, argc, argv);
  6482. if (!ret) {
  6483. if (argv1 != argv1_buf)
  6484. wasm_runtime_free(argv1);
  6485. goto fail;
  6486. }
  6487. /* Get extra result values */
  6488. switch (func_type->types[func_type->param_count]) {
  6489. case VALUE_TYPE_I32:
  6490. case VALUE_TYPE_F32:
  6491. #if WASM_ENABLE_REF_TYPES != 0
  6492. case VALUE_TYPE_FUNCREF:
  6493. case VALUE_TYPE_EXTERNREF:
  6494. #endif
  6495. argv_ret++;
  6496. break;
  6497. case VALUE_TYPE_I64:
  6498. case VALUE_TYPE_F64:
  6499. argv_ret += 2;
  6500. break;
  6501. #if WASM_ENABLE_SIMD != 0
  6502. case VALUE_TYPE_V128:
  6503. argv_ret += 4;
  6504. break;
  6505. #endif
  6506. default:
  6507. bh_assert(0);
  6508. break;
  6509. }
  6510. ext_rets =
  6511. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6512. bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
  6513. sizeof(uint32) * cell_num);
  6514. if (argv1 != argv1_buf)
  6515. wasm_runtime_free(argv1);
  6516. ret = true;
  6517. }
  6518. else {
  6519. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  6520. /* Quick call if the quick jit entry is registered */
  6521. if (func_type->quick_aot_entry) {
  6522. void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
  6523. uint32 *argv_ret) =
  6524. func_type->quick_aot_entry;
  6525. invoke_native(module_inst->func_ptrs[func_idx], exec_env, argv,
  6526. argv);
  6527. ret = !wasm_copy_exception(module_inst, NULL);
  6528. }
  6529. else
  6530. #endif
  6531. {
  6532. ret = wasm_runtime_invoke_native(
  6533. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL,
  6534. NULL, argv, argc, argv);
  6535. if (ret)
  6536. ret = !wasm_copy_exception(module_inst, NULL);
  6537. }
  6538. }
  6539. fail:
  6540. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6541. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6542. llvm_jit_free_frame_internal(exec_env);
  6543. #endif
  6544. return ret;
  6545. }
  6546. #endif /* end of WASM_ENABLE_JIT != 0 */
  6547. void
  6548. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  6549. WASMFunctionInstance *function, uint32 argc,
  6550. uint32 argv[])
  6551. {
  6552. WASMRuntimeFrame *frame = NULL, *prev_frame, *outs_area;
  6553. RunningMode running_mode =
  6554. wasm_runtime_get_running_mode((WASMModuleInstanceCommon *)module_inst);
  6555. /* Allocate sufficient cells for all kinds of return values. */
  6556. bool alloc_frame = true;
  6557. if (argc < function->param_cell_num) {
  6558. char buf[128];
  6559. snprintf(buf, sizeof(buf),
  6560. "invalid argument count %" PRIu32
  6561. ", must be no smaller than %u",
  6562. argc, function->param_cell_num);
  6563. wasm_set_exception(module_inst, buf);
  6564. return;
  6565. }
  6566. argc = function->param_cell_num;
  6567. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  6568. /*
  6569. * wasm_runtime_detect_native_stack_overflow is done by
  6570. * call_wasm_with_hw_bound_check.
  6571. */
  6572. #else
  6573. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  6574. return;
  6575. }
  6576. #endif
  6577. if (!function->is_import_func) {
  6578. /* No need to alloc frame when calling LLVM JIT function */
  6579. #if WASM_ENABLE_JIT != 0
  6580. if (running_mode == Mode_LLVM_JIT) {
  6581. alloc_frame = false;
  6582. }
  6583. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0
  6584. else if (running_mode == Mode_Multi_Tier_JIT) {
  6585. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6586. if it is compiled, else call fast jit function */
  6587. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6588. if (module_inst->module->func_ptrs_compiled
  6589. [func_idx - module_inst->module->import_function_count]) {
  6590. alloc_frame = false;
  6591. }
  6592. }
  6593. #endif
  6594. #endif
  6595. }
  6596. if (alloc_frame) {
  6597. unsigned all_cell_num =
  6598. function->ret_cell_num > 2 ? function->ret_cell_num : 2;
  6599. unsigned frame_size;
  6600. prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6601. /* This frame won't be used by JITed code, so only allocate interp
  6602. frame here. */
  6603. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6604. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame)))
  6605. return;
  6606. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  6607. frame->function = NULL;
  6608. frame->ip = NULL;
  6609. /* There is no local variable. */
  6610. frame->sp = frame->lp + 0;
  6611. if ((uint8 *)(outs_area->lp + function->param_cell_num)
  6612. > exec_env->wasm_stack.top_boundary) {
  6613. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6614. return;
  6615. }
  6616. if (argc > 0)
  6617. word_copy(outs_area->lp, argv, argc);
  6618. wasm_exec_env_set_cur_frame(exec_env, frame);
  6619. }
  6620. #if defined(os_writegsbase)
  6621. {
  6622. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  6623. if (memory_inst)
  6624. /* write base addr of linear memory to GS segment register */
  6625. os_writegsbase(memory_inst->memory_data);
  6626. }
  6627. #endif
  6628. if (function->is_import_func) {
  6629. #if WASM_ENABLE_MULTI_MODULE != 0
  6630. if (function->import_module_inst) {
  6631. wasm_interp_call_func_import(module_inst, exec_env, function,
  6632. frame);
  6633. }
  6634. else
  6635. #endif
  6636. {
  6637. /* it is a native function */
  6638. wasm_interp_call_func_native(module_inst, exec_env, function,
  6639. frame);
  6640. }
  6641. }
  6642. else {
  6643. if (running_mode == Mode_Interp) {
  6644. wasm_interp_call_func_bytecode(module_inst, exec_env, function,
  6645. frame);
  6646. }
  6647. #if WASM_ENABLE_FAST_JIT != 0
  6648. else if (running_mode == Mode_Fast_JIT) {
  6649. fast_jit_call_func_bytecode(module_inst, exec_env, function, frame);
  6650. }
  6651. #endif
  6652. #if WASM_ENABLE_JIT != 0
  6653. else if (running_mode == Mode_LLVM_JIT) {
  6654. llvm_jit_call_func_bytecode(module_inst, exec_env, function, argc,
  6655. argv);
  6656. }
  6657. #endif
  6658. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  6659. && WASM_ENABLE_JIT != 0
  6660. else if (running_mode == Mode_Multi_Tier_JIT) {
  6661. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6662. if it is compiled, else call fast jit function */
  6663. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6664. if (module_inst->module->func_ptrs_compiled
  6665. [func_idx - module_inst->module->import_function_count]) {
  6666. llvm_jit_call_func_bytecode(module_inst, exec_env, function,
  6667. argc, argv);
  6668. }
  6669. else {
  6670. fast_jit_call_func_bytecode(module_inst, exec_env, function,
  6671. frame);
  6672. }
  6673. }
  6674. #endif
  6675. else {
  6676. /* There should always be a supported running mode selected */
  6677. bh_assert(0);
  6678. }
  6679. (void)wasm_interp_call_func_bytecode;
  6680. #if WASM_ENABLE_FAST_JIT != 0
  6681. (void)fast_jit_call_func_bytecode;
  6682. #endif
  6683. }
  6684. /* Output the return value to the caller */
  6685. if (!wasm_copy_exception(module_inst, NULL)) {
  6686. if (alloc_frame) {
  6687. uint32 i;
  6688. for (i = 0; i < function->ret_cell_num; i++) {
  6689. argv[i] = *(frame->sp + i - function->ret_cell_num);
  6690. }
  6691. }
  6692. }
  6693. else {
  6694. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  6695. if (wasm_interp_create_call_stack(exec_env)) {
  6696. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  6697. }
  6698. #endif
  6699. }
  6700. if (alloc_frame) {
  6701. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6702. FREE_FRAME(exec_env, frame);
  6703. }
  6704. }