wasm_mini_loader.c 248 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_FAST_JIT != 0
  14. #include "../fast-jit/jit_compiler.h"
  15. #include "../fast-jit/jit_codecache.h"
  16. #endif
  17. /* Read a value of given type from the address pointed to by the given
  18. pointer and increase the pointer to the position just after the
  19. value being read. */
  20. #define TEMPLATE_READ_VALUE(Type, p) \
  21. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  22. static void
  23. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  24. {
  25. if (error_buf != NULL)
  26. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  27. string);
  28. }
  29. #define CHECK_BUF(buf, buf_end, length) \
  30. do { \
  31. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  32. } while (0)
  33. #define CHECK_BUF1(buf, buf_end, length) \
  34. do { \
  35. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  36. } while (0)
  37. #define skip_leb(p) while (*p++ & 0x80)
  38. #define skip_leb_int64(p, p_end) skip_leb(p)
  39. #define skip_leb_uint32(p, p_end) skip_leb(p)
  40. #define skip_leb_int32(p, p_end) skip_leb(p)
  41. static bool
  42. is_32bit_type(uint8 type)
  43. {
  44. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  45. #if WASM_ENABLE_REF_TYPES != 0
  46. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  47. #endif
  48. )
  49. return true;
  50. return false;
  51. }
  52. static bool
  53. is_64bit_type(uint8 type)
  54. {
  55. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  56. return true;
  57. return false;
  58. }
  59. static bool
  60. is_value_type(uint8 type)
  61. {
  62. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  63. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  64. #if WASM_ENABLE_REF_TYPES != 0
  65. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  66. #endif
  67. )
  68. return true;
  69. return false;
  70. }
  71. static bool
  72. is_byte_a_type(uint8 type)
  73. {
  74. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  75. }
  76. static void
  77. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  78. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  79. {
  80. const uint8 *buf = *p_buf;
  81. uint64 result = 0;
  82. uint32 shift = 0;
  83. uint32 offset = 0, bcnt = 0;
  84. uint64 byte;
  85. while (true) {
  86. bh_assert(bcnt + 1 <= (maxbits + 6) / 7);
  87. CHECK_BUF(buf, buf_end, offset + 1);
  88. byte = buf[offset];
  89. offset += 1;
  90. result |= ((byte & 0x7f) << shift);
  91. shift += 7;
  92. bcnt += 1;
  93. if ((byte & 0x80) == 0) {
  94. break;
  95. }
  96. }
  97. if (!sign && maxbits == 32 && shift >= maxbits) {
  98. /* The top bits set represent values > 32 bits */
  99. bh_assert(!(((uint8)byte) & 0xf0));
  100. }
  101. else if (sign && maxbits == 32) {
  102. if (shift < maxbits) {
  103. /* Sign extend, second highest bit is the sign bit */
  104. if ((uint8)byte & 0x40)
  105. result |= (~((uint64)0)) << shift;
  106. }
  107. else {
  108. /* The top bits should be a sign-extension of the sign bit */
  109. bool sign_bit_set = ((uint8)byte) & 0x8;
  110. int top_bits = ((uint8)byte) & 0xf0;
  111. bh_assert(!((sign_bit_set && top_bits != 0x70)
  112. || (!sign_bit_set && top_bits != 0)));
  113. (void)top_bits;
  114. (void)sign_bit_set;
  115. }
  116. }
  117. else if (sign && maxbits == 64) {
  118. if (shift < maxbits) {
  119. /* Sign extend, second highest bit is the sign bit */
  120. if ((uint8)byte & 0x40)
  121. result |= (~((uint64)0)) << shift;
  122. }
  123. else {
  124. /* The top bits should be a sign-extension of the sign bit */
  125. bool sign_bit_set = ((uint8)byte) & 0x1;
  126. int top_bits = ((uint8)byte) & 0xfe;
  127. bh_assert(!((sign_bit_set && top_bits != 0x7e)
  128. || (!sign_bit_set && top_bits != 0)));
  129. (void)top_bits;
  130. (void)sign_bit_set;
  131. }
  132. }
  133. *p_buf += offset;
  134. *p_result = result;
  135. }
  136. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  137. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  138. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  139. #define read_leb_int64(p, p_end, res) \
  140. do { \
  141. uint64 res64; \
  142. read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  143. error_buf_size); \
  144. res = (int64)res64; \
  145. } while (0)
  146. #define read_leb_uint32(p, p_end, res) \
  147. do { \
  148. uint64 res64; \
  149. read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  150. error_buf_size); \
  151. res = (uint32)res64; \
  152. } while (0)
  153. #define read_leb_int32(p, p_end, res) \
  154. do { \
  155. uint64 res64; \
  156. read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  157. error_buf_size); \
  158. res = (int32)res64; \
  159. } while (0)
  160. static void *
  161. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  162. {
  163. void *mem;
  164. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  165. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  166. return NULL;
  167. }
  168. memset(mem, 0, (uint32)size);
  169. return mem;
  170. }
  171. static char *
  172. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  173. bool is_load_from_file_buf, char *error_buf,
  174. uint32 error_buf_size)
  175. {
  176. StringNode *node, *node_next;
  177. if (len == 0) {
  178. return "";
  179. }
  180. else if (is_load_from_file_buf) {
  181. /* As the file buffer can be referred to after loading, we use
  182. the previous byte of leb encoded size to adjust the string:
  183. move string 1 byte backward and then append '\0' */
  184. char *c_str = (char *)str - 1;
  185. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  186. c_str[len] = '\0';
  187. return c_str;
  188. }
  189. /* Search const str list */
  190. node = module->const_str_list;
  191. while (node) {
  192. node_next = node->next;
  193. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  194. break;
  195. node = node_next;
  196. }
  197. if (node) {
  198. LOG_DEBUG("reuse %s", node->str);
  199. return node->str;
  200. }
  201. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  202. error_buf_size))) {
  203. return NULL;
  204. }
  205. node->str = ((char *)node) + sizeof(StringNode);
  206. bh_memcpy_s(node->str, len + 1, str, len);
  207. node->str[len] = '\0';
  208. if (!module->const_str_list) {
  209. /* set as head */
  210. module->const_str_list = node;
  211. node->next = NULL;
  212. }
  213. else {
  214. /* insert it */
  215. node->next = module->const_str_list;
  216. module->const_str_list = node;
  217. }
  218. return node->str;
  219. }
  220. static bool
  221. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  222. InitializerExpression *init_expr, uint8 type, char *error_buf,
  223. uint32 error_buf_size)
  224. {
  225. const uint8 *p = *p_buf, *p_end = buf_end;
  226. uint8 flag, end_byte, *p_float;
  227. uint32 i;
  228. CHECK_BUF(p, p_end, 1);
  229. init_expr->init_expr_type = read_uint8(p);
  230. flag = init_expr->init_expr_type;
  231. switch (flag) {
  232. /* i32.const */
  233. case INIT_EXPR_TYPE_I32_CONST:
  234. bh_assert(type == VALUE_TYPE_I32);
  235. read_leb_int32(p, p_end, init_expr->u.i32);
  236. break;
  237. /* i64.const */
  238. case INIT_EXPR_TYPE_I64_CONST:
  239. bh_assert(type == VALUE_TYPE_I64);
  240. read_leb_int64(p, p_end, init_expr->u.i64);
  241. break;
  242. /* f32.const */
  243. case INIT_EXPR_TYPE_F32_CONST:
  244. bh_assert(type == VALUE_TYPE_F32);
  245. CHECK_BUF(p, p_end, 4);
  246. p_float = (uint8 *)&init_expr->u.f32;
  247. for (i = 0; i < sizeof(float32); i++)
  248. *p_float++ = *p++;
  249. break;
  250. /* f64.const */
  251. case INIT_EXPR_TYPE_F64_CONST:
  252. bh_assert(type == VALUE_TYPE_F64);
  253. CHECK_BUF(p, p_end, 8);
  254. p_float = (uint8 *)&init_expr->u.f64;
  255. for (i = 0; i < sizeof(float64); i++)
  256. *p_float++ = *p++;
  257. break;
  258. #if WASM_ENABLE_REF_TYPES != 0
  259. case INIT_EXPR_TYPE_FUNCREF_CONST:
  260. {
  261. bh_assert(type == VALUE_TYPE_FUNCREF);
  262. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  263. break;
  264. }
  265. case INIT_EXPR_TYPE_REFNULL_CONST:
  266. {
  267. uint8 reftype;
  268. CHECK_BUF(p, p_end, 1);
  269. reftype = read_uint8(p);
  270. bh_assert(type == reftype);
  271. init_expr->u.ref_index = NULL_REF;
  272. (void)reftype;
  273. break;
  274. }
  275. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  276. /* get_global */
  277. case INIT_EXPR_TYPE_GET_GLOBAL:
  278. read_leb_uint32(p, p_end, init_expr->u.global_index);
  279. break;
  280. default:
  281. bh_assert(0);
  282. break;
  283. }
  284. CHECK_BUF(p, p_end, 1);
  285. end_byte = read_uint8(p);
  286. bh_assert(end_byte == 0x0b);
  287. *p_buf = p;
  288. (void)end_byte;
  289. return true;
  290. }
  291. static bool
  292. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  293. char *error_buf, uint32 error_buf_size)
  294. {
  295. const uint8 *p = buf, *p_end = buf_end, *p_org;
  296. uint32 type_count, param_count, result_count, i, j;
  297. uint32 param_cell_num, ret_cell_num;
  298. uint64 total_size;
  299. uint8 flag;
  300. WASMType *type;
  301. read_leb_uint32(p, p_end, type_count);
  302. if (type_count) {
  303. module->type_count = type_count;
  304. total_size = sizeof(WASMType *) * (uint64)type_count;
  305. if (!(module->types =
  306. loader_malloc(total_size, error_buf, error_buf_size))) {
  307. return false;
  308. }
  309. for (i = 0; i < type_count; i++) {
  310. CHECK_BUF(p, p_end, 1);
  311. flag = read_uint8(p);
  312. bh_assert(flag == 0x60);
  313. read_leb_uint32(p, p_end, param_count);
  314. /* Resolve param count and result count firstly */
  315. p_org = p;
  316. CHECK_BUF(p, p_end, param_count);
  317. p += param_count;
  318. read_leb_uint32(p, p_end, result_count);
  319. CHECK_BUF(p, p_end, result_count);
  320. p = p_org;
  321. bh_assert(param_count <= UINT16_MAX && result_count <= UINT16_MAX);
  322. total_size = offsetof(WASMType, types)
  323. + sizeof(uint8) * (uint64)(param_count + result_count);
  324. if (!(type = module->types[i] =
  325. loader_malloc(total_size, error_buf, error_buf_size))) {
  326. return false;
  327. }
  328. /* Resolve param types and result types */
  329. type->param_count = (uint16)param_count;
  330. type->result_count = (uint16)result_count;
  331. for (j = 0; j < param_count; j++) {
  332. CHECK_BUF(p, p_end, 1);
  333. type->types[j] = read_uint8(p);
  334. }
  335. read_leb_uint32(p, p_end, result_count);
  336. for (j = 0; j < result_count; j++) {
  337. CHECK_BUF(p, p_end, 1);
  338. type->types[param_count + j] = read_uint8(p);
  339. }
  340. for (j = 0; j < param_count + result_count; j++) {
  341. bh_assert(is_value_type(type->types[j]));
  342. }
  343. param_cell_num = wasm_get_cell_num(type->types, param_count);
  344. ret_cell_num =
  345. wasm_get_cell_num(type->types + param_count, result_count);
  346. bh_assert(param_cell_num <= UINT16_MAX
  347. && ret_cell_num <= UINT16_MAX);
  348. type->param_cell_num = (uint16)param_cell_num;
  349. type->ret_cell_num = (uint16)ret_cell_num;
  350. }
  351. }
  352. bh_assert(p == p_end);
  353. LOG_VERBOSE("Load type section success.\n");
  354. (void)flag;
  355. return true;
  356. }
  357. static void
  358. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  359. {
  360. uint32 default_max_size =
  361. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  362. if (max_size_flag) {
  363. /* module defines the table limitation */
  364. bh_assert(init_size <= *max_size);
  365. if (init_size < *max_size) {
  366. *max_size =
  367. *max_size < default_max_size ? *max_size : default_max_size;
  368. }
  369. }
  370. else {
  371. /* partial defined table limitation, gives a default value */
  372. *max_size = default_max_size;
  373. }
  374. }
  375. static bool
  376. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  377. const WASMModule *parent_module,
  378. const char *sub_module_name, const char *function_name,
  379. WASMFunctionImport *function, char *error_buf,
  380. uint32 error_buf_size)
  381. {
  382. const uint8 *p = *p_buf, *p_end = buf_end;
  383. uint32 declare_type_index = 0;
  384. WASMType *declare_func_type = NULL;
  385. WASMFunction *linked_func = NULL;
  386. const char *linked_signature = NULL;
  387. void *linked_attachment = NULL;
  388. bool linked_call_conv_raw = false;
  389. read_leb_uint32(p, p_end, declare_type_index);
  390. *p_buf = p;
  391. bh_assert(declare_type_index < parent_module->type_count);
  392. declare_func_type = parent_module->types[declare_type_index];
  393. /* check built-in modules */
  394. linked_func = wasm_native_resolve_symbol(
  395. sub_module_name, function_name, declare_func_type, &linked_signature,
  396. &linked_attachment, &linked_call_conv_raw);
  397. function->module_name = (char *)sub_module_name;
  398. function->field_name = (char *)function_name;
  399. function->func_type = declare_func_type;
  400. function->func_ptr_linked = linked_func;
  401. function->signature = linked_signature;
  402. function->attachment = linked_attachment;
  403. function->call_conv_raw = linked_call_conv_raw;
  404. return true;
  405. }
  406. static bool
  407. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  408. WASMModule *parent_module, const char *sub_module_name,
  409. const char *table_name, WASMTableImport *table,
  410. char *error_buf, uint32 error_buf_size)
  411. {
  412. const uint8 *p = *p_buf, *p_end = buf_end;
  413. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  414. declare_init_size = 0, declare_max_size = 0;
  415. CHECK_BUF(p, p_end, 1);
  416. /* 0x70 or 0x6F */
  417. declare_elem_type = read_uint8(p);
  418. bh_assert(VALUE_TYPE_FUNCREF == declare_elem_type
  419. #if WASM_ENABLE_REF_TYPES != 0
  420. || VALUE_TYPE_EXTERNREF == declare_elem_type
  421. #endif
  422. );
  423. read_leb_uint32(p, p_end, declare_max_size_flag);
  424. read_leb_uint32(p, p_end, declare_init_size);
  425. if (declare_max_size_flag & 1) {
  426. read_leb_uint32(p, p_end, declare_max_size);
  427. bh_assert(table->init_size <= table->max_size);
  428. }
  429. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  430. &declare_max_size);
  431. *p_buf = p;
  432. bh_assert(
  433. !((declare_max_size_flag & 1) && declare_init_size > declare_max_size));
  434. /* now we believe all declaration are ok */
  435. table->elem_type = declare_elem_type;
  436. table->init_size = declare_init_size;
  437. table->flags = declare_max_size_flag;
  438. table->max_size = declare_max_size;
  439. return true;
  440. }
  441. static bool
  442. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  443. WASMModule *parent_module, const char *sub_module_name,
  444. const char *memory_name, WASMMemoryImport *memory,
  445. char *error_buf, uint32 error_buf_size)
  446. {
  447. const uint8 *p = *p_buf, *p_end = buf_end;
  448. uint32 pool_size = wasm_runtime_memory_pool_size();
  449. #if WASM_ENABLE_APP_FRAMEWORK != 0
  450. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  451. / DEFAULT_NUM_BYTES_PER_PAGE;
  452. #else
  453. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  454. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  455. uint32 declare_max_page_count_flag = 0;
  456. uint32 declare_init_page_count = 0;
  457. uint32 declare_max_page_count = 0;
  458. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  459. read_leb_uint32(p, p_end, declare_init_page_count);
  460. bh_assert(declare_init_page_count <= 65536);
  461. if (declare_max_page_count_flag & 1) {
  462. read_leb_uint32(p, p_end, declare_max_page_count);
  463. bh_assert(declare_init_page_count <= declare_max_page_count);
  464. bh_assert(declare_max_page_count <= 65536);
  465. if (declare_max_page_count > max_page_count) {
  466. declare_max_page_count = max_page_count;
  467. }
  468. }
  469. else {
  470. /* Limit the maximum memory size to max_page_count */
  471. declare_max_page_count = max_page_count;
  472. }
  473. /* now we believe all declaration are ok */
  474. memory->flags = declare_max_page_count_flag;
  475. memory->init_page_count = declare_init_page_count;
  476. memory->max_page_count = declare_max_page_count;
  477. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  478. *p_buf = p;
  479. return true;
  480. }
  481. static bool
  482. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  483. const WASMModule *parent_module, char *sub_module_name,
  484. char *global_name, WASMGlobalImport *global, char *error_buf,
  485. uint32 error_buf_size)
  486. {
  487. const uint8 *p = *p_buf, *p_end = buf_end;
  488. uint8 declare_type = 0;
  489. uint8 declare_mutable = 0;
  490. bool is_mutable = false;
  491. bool ret = false;
  492. CHECK_BUF(p, p_end, 2);
  493. declare_type = read_uint8(p);
  494. declare_mutable = read_uint8(p);
  495. *p_buf = p;
  496. bh_assert(declare_mutable < 2);
  497. is_mutable = declare_mutable & 1 ? true : false;
  498. #if WASM_ENABLE_LIBC_BUILTIN != 0
  499. /* check built-in modules */
  500. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  501. global);
  502. if (ret) {
  503. bh_assert(global->type == declare_type
  504. && global->is_mutable != declare_mutable);
  505. }
  506. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  507. global->is_linked = ret;
  508. global->module_name = sub_module_name;
  509. global->field_name = global_name;
  510. global->type = declare_type;
  511. global->is_mutable = is_mutable;
  512. (void)p_end;
  513. return true;
  514. }
  515. static bool
  516. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  517. char *error_buf, uint32 error_buf_size)
  518. {
  519. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  520. CHECK_BUF(p, p_end, 1);
  521. /* 0x70 or 0x6F */
  522. table->elem_type = read_uint8(p);
  523. bh_assert((VALUE_TYPE_FUNCREF == table->elem_type)
  524. #if WASM_ENABLE_REF_TYPES != 0
  525. || VALUE_TYPE_EXTERNREF == table->elem_type
  526. #endif
  527. );
  528. p_org = p;
  529. read_leb_uint32(p, p_end, table->flags);
  530. bh_assert(p - p_org <= 1);
  531. bh_assert(table->flags <= 1);
  532. (void)p_org;
  533. read_leb_uint32(p, p_end, table->init_size);
  534. if (table->flags == 1) {
  535. read_leb_uint32(p, p_end, table->max_size);
  536. bh_assert(table->init_size <= table->max_size);
  537. }
  538. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  539. *p_buf = p;
  540. return true;
  541. }
  542. static bool
  543. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  544. char *error_buf, uint32 error_buf_size)
  545. {
  546. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  547. uint32 pool_size = wasm_runtime_memory_pool_size();
  548. #if WASM_ENABLE_APP_FRAMEWORK != 0
  549. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  550. / DEFAULT_NUM_BYTES_PER_PAGE;
  551. #else
  552. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  553. #endif
  554. p_org = p;
  555. read_leb_uint32(p, p_end, memory->flags);
  556. bh_assert(p - p_org <= 1);
  557. (void)p_org;
  558. #if WASM_ENABLE_SHARED_MEMORY == 0
  559. bh_assert(memory->flags <= 1);
  560. #else
  561. bh_assert(memory->flags <= 3 && memory->flags != 2);
  562. #endif
  563. read_leb_uint32(p, p_end, memory->init_page_count);
  564. bh_assert(memory->init_page_count <= 65536);
  565. if (memory->flags & 1) {
  566. read_leb_uint32(p, p_end, memory->max_page_count);
  567. bh_assert(memory->init_page_count <= memory->max_page_count);
  568. bh_assert(memory->max_page_count <= 65536);
  569. if (memory->max_page_count > max_page_count)
  570. memory->max_page_count = max_page_count;
  571. }
  572. else {
  573. /* Limit the maximum memory size to max_page_count */
  574. memory->max_page_count = max_page_count;
  575. }
  576. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  577. *p_buf = p;
  578. return true;
  579. }
  580. static bool
  581. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  582. bool is_load_from_file_buf, char *error_buf,
  583. uint32 error_buf_size)
  584. {
  585. const uint8 *p = buf, *p_end = buf_end, *p_old;
  586. uint32 import_count, name_len, type_index, i, u32, flags;
  587. uint64 total_size;
  588. WASMImport *import;
  589. WASMImport *import_functions = NULL, *import_tables = NULL;
  590. WASMImport *import_memories = NULL, *import_globals = NULL;
  591. char *sub_module_name, *field_name;
  592. uint8 u8, kind;
  593. read_leb_uint32(p, p_end, import_count);
  594. if (import_count) {
  595. module->import_count = import_count;
  596. total_size = sizeof(WASMImport) * (uint64)import_count;
  597. if (!(module->imports =
  598. loader_malloc(total_size, error_buf, error_buf_size))) {
  599. return false;
  600. }
  601. p_old = p;
  602. /* Scan firstly to get import count of each type */
  603. for (i = 0; i < import_count; i++) {
  604. /* module name */
  605. read_leb_uint32(p, p_end, name_len);
  606. CHECK_BUF(p, p_end, name_len);
  607. p += name_len;
  608. /* field name */
  609. read_leb_uint32(p, p_end, name_len);
  610. CHECK_BUF(p, p_end, name_len);
  611. p += name_len;
  612. CHECK_BUF(p, p_end, 1);
  613. /* 0x00/0x01/0x02/0x03 */
  614. kind = read_uint8(p);
  615. switch (kind) {
  616. case IMPORT_KIND_FUNC: /* import function */
  617. read_leb_uint32(p, p_end, type_index);
  618. module->import_function_count++;
  619. break;
  620. case IMPORT_KIND_TABLE: /* import table */
  621. CHECK_BUF(p, p_end, 1);
  622. /* 0x70 */
  623. u8 = read_uint8(p);
  624. read_leb_uint32(p, p_end, flags);
  625. read_leb_uint32(p, p_end, u32);
  626. if (flags & 1)
  627. read_leb_uint32(p, p_end, u32);
  628. module->import_table_count++;
  629. #if WASM_ENABLE_REF_TYPES == 0
  630. bh_assert(module->import_table_count <= 1);
  631. #endif
  632. break;
  633. case IMPORT_KIND_MEMORY: /* import memory */
  634. read_leb_uint32(p, p_end, flags);
  635. read_leb_uint32(p, p_end, u32);
  636. if (flags & 1)
  637. read_leb_uint32(p, p_end, u32);
  638. module->import_memory_count++;
  639. bh_assert(module->import_memory_count <= 1);
  640. break;
  641. case IMPORT_KIND_GLOBAL: /* import global */
  642. CHECK_BUF(p, p_end, 2);
  643. p += 2;
  644. module->import_global_count++;
  645. break;
  646. default:
  647. bh_assert(0);
  648. break;
  649. }
  650. }
  651. if (module->import_function_count)
  652. import_functions = module->import_functions = module->imports;
  653. if (module->import_table_count)
  654. import_tables = module->import_tables =
  655. module->imports + module->import_function_count;
  656. if (module->import_memory_count)
  657. import_memories = module->import_memories =
  658. module->imports + module->import_function_count
  659. + module->import_table_count;
  660. if (module->import_global_count)
  661. import_globals = module->import_globals =
  662. module->imports + module->import_function_count
  663. + module->import_table_count + module->import_memory_count;
  664. p = p_old;
  665. /* Scan again to resolve the data */
  666. for (i = 0; i < import_count; i++) {
  667. WASMModule *sub_module = NULL;
  668. /* load module name */
  669. read_leb_uint32(p, p_end, name_len);
  670. CHECK_BUF(p, p_end, name_len);
  671. if (!(sub_module_name = const_str_list_insert(
  672. p, name_len, module, is_load_from_file_buf, error_buf,
  673. error_buf_size))) {
  674. return false;
  675. }
  676. p += name_len;
  677. /* load field name */
  678. read_leb_uint32(p, p_end, name_len);
  679. CHECK_BUF(p, p_end, name_len);
  680. if (!(field_name = const_str_list_insert(
  681. p, name_len, module, is_load_from_file_buf, error_buf,
  682. error_buf_size))) {
  683. return false;
  684. }
  685. p += name_len;
  686. CHECK_BUF(p, p_end, 1);
  687. /* 0x00/0x01/0x02/0x03 */
  688. kind = read_uint8(p);
  689. LOG_DEBUG("import #%d: (%s, %s), kind: %d", i, sub_module_name,
  690. field_name, kind);
  691. switch (kind) {
  692. case IMPORT_KIND_FUNC: /* import function */
  693. bh_assert(import_functions);
  694. import = import_functions++;
  695. if (!load_function_import(
  696. &p, p_end, module, sub_module_name, field_name,
  697. &import->u.function, error_buf, error_buf_size)) {
  698. return false;
  699. }
  700. break;
  701. case IMPORT_KIND_TABLE: /* import table */
  702. bh_assert(import_tables);
  703. import = import_tables++;
  704. if (!load_table_import(&p, p_end, module, sub_module_name,
  705. field_name, &import->u.table,
  706. error_buf, error_buf_size)) {
  707. LOG_DEBUG("can not import such a table (%s,%s)",
  708. sub_module_name, field_name);
  709. return false;
  710. }
  711. break;
  712. case IMPORT_KIND_MEMORY: /* import memory */
  713. bh_assert(import_memories);
  714. import = import_memories++;
  715. if (!load_memory_import(&p, p_end, module, sub_module_name,
  716. field_name, &import->u.memory,
  717. error_buf, error_buf_size)) {
  718. return false;
  719. }
  720. break;
  721. case IMPORT_KIND_GLOBAL: /* import global */
  722. bh_assert(import_globals);
  723. import = import_globals++;
  724. if (!load_global_import(&p, p_end, module, sub_module_name,
  725. field_name, &import->u.global,
  726. error_buf, error_buf_size)) {
  727. return false;
  728. }
  729. break;
  730. default:
  731. bh_assert(0);
  732. import = NULL;
  733. break;
  734. }
  735. import->kind = kind;
  736. import->u.names.module_name = sub_module_name;
  737. import->u.names.field_name = field_name;
  738. (void)sub_module;
  739. }
  740. #if WASM_ENABLE_LIBC_WASI != 0
  741. import = module->import_functions;
  742. for (i = 0; i < module->import_function_count; i++, import++) {
  743. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  744. || !strcmp(import->u.names.module_name,
  745. "wasi_snapshot_preview1")) {
  746. module->import_wasi_api = true;
  747. break;
  748. }
  749. }
  750. #endif
  751. }
  752. bh_assert(p == p_end);
  753. LOG_VERBOSE("Load import section success.\n");
  754. (void)u8;
  755. (void)u32;
  756. (void)type_index;
  757. return true;
  758. }
  759. static bool
  760. init_function_local_offsets(WASMFunction *func, char *error_buf,
  761. uint32 error_buf_size)
  762. {
  763. WASMType *param_type = func->func_type;
  764. uint32 param_count = param_type->param_count;
  765. uint8 *param_types = param_type->types;
  766. uint32 local_count = func->local_count;
  767. uint8 *local_types = func->local_types;
  768. uint32 i, local_offset = 0;
  769. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  770. if (total_size > 0
  771. && !(func->local_offsets =
  772. loader_malloc(total_size, error_buf, error_buf_size))) {
  773. return false;
  774. }
  775. for (i = 0; i < param_count; i++) {
  776. func->local_offsets[i] = (uint16)local_offset;
  777. local_offset += wasm_value_type_cell_num(param_types[i]);
  778. }
  779. for (i = 0; i < local_count; i++) {
  780. func->local_offsets[param_count + i] = (uint16)local_offset;
  781. local_offset += wasm_value_type_cell_num(local_types[i]);
  782. }
  783. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  784. return true;
  785. }
  786. static bool
  787. load_function_section(const uint8 *buf, const uint8 *buf_end,
  788. const uint8 *buf_code, const uint8 *buf_code_end,
  789. WASMModule *module, char *error_buf,
  790. uint32 error_buf_size)
  791. {
  792. const uint8 *p = buf, *p_end = buf_end;
  793. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  794. uint32 func_count;
  795. uint64 total_size;
  796. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  797. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  798. uint8 type;
  799. WASMFunction *func;
  800. read_leb_uint32(p, p_end, func_count);
  801. if (buf_code)
  802. read_leb_uint32(p_code, buf_code_end, code_count);
  803. bh_assert(func_count == code_count);
  804. if (func_count) {
  805. module->function_count = func_count;
  806. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  807. if (!(module->functions =
  808. loader_malloc(total_size, error_buf, error_buf_size))) {
  809. return false;
  810. }
  811. for (i = 0; i < func_count; i++) {
  812. /* Resolve function type */
  813. read_leb_uint32(p, p_end, type_index);
  814. bh_assert(type_index < module->type_count);
  815. read_leb_uint32(p_code, buf_code_end, code_size);
  816. bh_assert(code_size > 0 && p_code + code_size <= buf_code_end);
  817. /* Resolve local set count */
  818. p_code_end = p_code + code_size;
  819. local_count = 0;
  820. read_leb_uint32(p_code, buf_code_end, local_set_count);
  821. p_code_save = p_code;
  822. /* Calculate total local count */
  823. for (j = 0; j < local_set_count; j++) {
  824. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  825. bh_assert(sub_local_count <= UINT32_MAX - local_count);
  826. CHECK_BUF(p_code, buf_code_end, 1);
  827. /* 0x7F/0x7E/0x7D/0x7C */
  828. type = read_uint8(p_code);
  829. local_count += sub_local_count;
  830. }
  831. /* Alloc memory, layout: function structure + local types */
  832. code_size = (uint32)(p_code_end - p_code);
  833. total_size = sizeof(WASMFunction) + (uint64)local_count;
  834. if (!(func = module->functions[i] =
  835. loader_malloc(total_size, error_buf, error_buf_size))) {
  836. return false;
  837. }
  838. /* Set function type, local count, code size and code body */
  839. func->func_type = module->types[type_index];
  840. func->local_count = local_count;
  841. if (local_count > 0)
  842. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  843. func->code_size = code_size;
  844. /*
  845. * we shall make a copy of code body [p_code, p_code + code_size]
  846. * when we are worrying about inappropriate releasing behaviour.
  847. * all code bodies are actually in a buffer which user allocates in
  848. * his embedding environment and we don't have power on them.
  849. * it will be like:
  850. * code_body_cp = malloc(code_size);
  851. * memcpy(code_body_cp, p_code, code_size);
  852. * func->code = code_body_cp;
  853. */
  854. func->code = (uint8 *)p_code;
  855. /* Load each local type */
  856. p_code = p_code_save;
  857. local_type_index = 0;
  858. for (j = 0; j < local_set_count; j++) {
  859. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  860. bh_assert(sub_local_count
  861. && local_type_index <= UINT32_MAX - sub_local_count
  862. && local_type_index + sub_local_count <= local_count);
  863. CHECK_BUF(p_code, buf_code_end, 1);
  864. /* 0x7F/0x7E/0x7D/0x7C */
  865. type = read_uint8(p_code);
  866. bh_assert(is_value_type(type));
  867. for (k = 0; k < sub_local_count; k++) {
  868. func->local_types[local_type_index++] = type;
  869. }
  870. }
  871. func->param_cell_num = func->func_type->param_cell_num;
  872. func->ret_cell_num = func->func_type->ret_cell_num;
  873. local_cell_num =
  874. wasm_get_cell_num(func->local_types, func->local_count);
  875. bh_assert(local_cell_num <= UINT16_MAX);
  876. func->local_cell_num = (uint16)local_cell_num;
  877. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  878. return false;
  879. p_code = p_code_end;
  880. }
  881. }
  882. bh_assert(p == p_end);
  883. LOG_VERBOSE("Load function section success.\n");
  884. (void)code_count;
  885. return true;
  886. }
  887. static bool
  888. check_function_index(const WASMModule *module, uint32 function_index,
  889. char *error_buf, uint32 error_buf_size)
  890. {
  891. return (function_index
  892. < module->import_function_count + module->function_count);
  893. }
  894. static bool
  895. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  896. char *error_buf, uint32 error_buf_size)
  897. {
  898. const uint8 *p = buf, *p_end = buf_end;
  899. uint32 table_count, i;
  900. uint64 total_size;
  901. WASMTable *table;
  902. read_leb_uint32(p, p_end, table_count);
  903. #if WASM_ENABLE_REF_TYPES == 0
  904. bh_assert(module->import_table_count + table_count <= 1);
  905. #endif
  906. if (table_count) {
  907. module->table_count = table_count;
  908. total_size = sizeof(WASMTable) * (uint64)table_count;
  909. if (!(module->tables =
  910. loader_malloc(total_size, error_buf, error_buf_size))) {
  911. return false;
  912. }
  913. /* load each table */
  914. table = module->tables;
  915. for (i = 0; i < table_count; i++, table++)
  916. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  917. return false;
  918. }
  919. bh_assert(p == p_end);
  920. LOG_VERBOSE("Load table section success.\n");
  921. return true;
  922. }
  923. static bool
  924. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  925. char *error_buf, uint32 error_buf_size)
  926. {
  927. const uint8 *p = buf, *p_end = buf_end;
  928. uint32 memory_count, i;
  929. uint64 total_size;
  930. WASMMemory *memory;
  931. read_leb_uint32(p, p_end, memory_count);
  932. bh_assert(module->import_memory_count + memory_count <= 1);
  933. if (memory_count) {
  934. module->memory_count = memory_count;
  935. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  936. if (!(module->memories =
  937. loader_malloc(total_size, error_buf, error_buf_size))) {
  938. return false;
  939. }
  940. /* load each memory */
  941. memory = module->memories;
  942. for (i = 0; i < memory_count; i++, memory++)
  943. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  944. return false;
  945. }
  946. bh_assert(p == p_end);
  947. LOG_VERBOSE("Load memory section success.\n");
  948. return true;
  949. }
  950. static bool
  951. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  952. char *error_buf, uint32 error_buf_size)
  953. {
  954. const uint8 *p = buf, *p_end = buf_end;
  955. uint32 global_count, i;
  956. uint64 total_size;
  957. WASMGlobal *global;
  958. uint8 mutable;
  959. read_leb_uint32(p, p_end, global_count);
  960. if (global_count) {
  961. module->global_count = global_count;
  962. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  963. if (!(module->globals =
  964. loader_malloc(total_size, error_buf, error_buf_size))) {
  965. return false;
  966. }
  967. global = module->globals;
  968. for (i = 0; i < global_count; i++, global++) {
  969. CHECK_BUF(p, p_end, 2);
  970. global->type = read_uint8(p);
  971. mutable = read_uint8(p);
  972. bh_assert(mutable < 2);
  973. global->is_mutable = mutable ? true : false;
  974. /* initialize expression */
  975. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  976. error_buf, error_buf_size))
  977. return false;
  978. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  979. /**
  980. * Currently, constant expressions occurring as initializers
  981. * of globals are further constrained in that contained
  982. * global.get instructions are
  983. * only allowed to refer to imported globals.
  984. */
  985. uint32 target_global_index = global->init_expr.u.global_index;
  986. bh_assert(target_global_index < module->import_global_count);
  987. (void)target_global_index;
  988. }
  989. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  990. == global->init_expr.init_expr_type) {
  991. bh_assert(global->init_expr.u.ref_index
  992. < module->import_function_count
  993. + module->function_count);
  994. }
  995. }
  996. }
  997. bh_assert(p == p_end);
  998. LOG_VERBOSE("Load global section success.\n");
  999. return true;
  1000. }
  1001. static bool
  1002. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1003. bool is_load_from_file_buf, char *error_buf,
  1004. uint32 error_buf_size)
  1005. {
  1006. const uint8 *p = buf, *p_end = buf_end;
  1007. uint32 export_count, i, j, index;
  1008. uint64 total_size;
  1009. uint32 str_len;
  1010. WASMExport *export;
  1011. const char *name;
  1012. read_leb_uint32(p, p_end, export_count);
  1013. if (export_count) {
  1014. module->export_count = export_count;
  1015. total_size = sizeof(WASMExport) * (uint64)export_count;
  1016. if (!(module->exports =
  1017. loader_malloc(total_size, error_buf, error_buf_size))) {
  1018. return false;
  1019. }
  1020. export = module->exports;
  1021. for (i = 0; i < export_count; i++, export ++) {
  1022. read_leb_uint32(p, p_end, str_len);
  1023. CHECK_BUF(p, p_end, str_len);
  1024. for (j = 0; j < i; j++) {
  1025. name = module->exports[j].name;
  1026. bh_assert(!(strlen(name) == str_len
  1027. && memcmp(name, p, str_len) == 0));
  1028. }
  1029. if (!(export->name = const_str_list_insert(
  1030. p, str_len, module, is_load_from_file_buf, error_buf,
  1031. error_buf_size))) {
  1032. return false;
  1033. }
  1034. p += str_len;
  1035. CHECK_BUF(p, p_end, 1);
  1036. export->kind = read_uint8(p);
  1037. read_leb_uint32(p, p_end, index);
  1038. export->index = index;
  1039. switch (export->kind) {
  1040. /* function index */
  1041. case EXPORT_KIND_FUNC:
  1042. bh_assert(index < module->function_count
  1043. + module->import_function_count);
  1044. break;
  1045. /* table index */
  1046. case EXPORT_KIND_TABLE:
  1047. bh_assert(index < module->table_count
  1048. + module->import_table_count);
  1049. break;
  1050. /* memory index */
  1051. case EXPORT_KIND_MEMORY:
  1052. bh_assert(index < module->memory_count
  1053. + module->import_memory_count);
  1054. break;
  1055. /* global index */
  1056. case EXPORT_KIND_GLOBAL:
  1057. bh_assert(index < module->global_count
  1058. + module->import_global_count);
  1059. break;
  1060. default:
  1061. bh_assert(0);
  1062. break;
  1063. }
  1064. }
  1065. }
  1066. bh_assert(p == p_end);
  1067. LOG_VERBOSE("Load export section success.\n");
  1068. (void)name;
  1069. return true;
  1070. }
  1071. static bool
  1072. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  1073. uint32 error_buf_size)
  1074. {
  1075. #if WASM_ENABLE_REF_TYPES == 0
  1076. if (table_index != 0) {
  1077. return false;
  1078. }
  1079. #endif
  1080. if (table_index >= module->import_table_count + module->table_count) {
  1081. return false;
  1082. }
  1083. return true;
  1084. }
  1085. static bool
  1086. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1087. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  1088. {
  1089. const uint8 *p = *p_buf, *p_end = buf_end;
  1090. uint32 table_index;
  1091. read_leb_uint32(p, p_end, table_index);
  1092. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  1093. return false;
  1094. }
  1095. *p_table_index = table_index;
  1096. *p_buf = p;
  1097. return true;
  1098. }
  1099. #if WASM_ENABLE_REF_TYPES != 0
  1100. static bool
  1101. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  1102. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  1103. {
  1104. const uint8 *p = *p_buf, *p_end = buf_end;
  1105. uint8 elem_type;
  1106. CHECK_BUF(p, p_end, 1);
  1107. elem_type = read_uint8(p);
  1108. if ((elemkind_zero && elem_type != 0)
  1109. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  1110. && elem_type != VALUE_TYPE_EXTERNREF)) {
  1111. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  1112. return false;
  1113. }
  1114. if (elemkind_zero)
  1115. *p_elem_type = VALUE_TYPE_FUNCREF;
  1116. else
  1117. *p_elem_type = elem_type;
  1118. *p_buf = p;
  1119. (void)p_end;
  1120. return true;
  1121. }
  1122. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  1123. static bool
  1124. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  1125. WASMModule *module, WASMTableSeg *table_segment,
  1126. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  1127. {
  1128. const uint8 *p = *p_buf, *p_end = buf_end;
  1129. uint32 function_count, function_index = 0, i;
  1130. uint64 total_size;
  1131. read_leb_uint32(p, p_end, function_count);
  1132. table_segment->function_count = function_count;
  1133. total_size = sizeof(uint32) * (uint64)function_count;
  1134. if (total_size > 0
  1135. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  1136. total_size, error_buf, error_buf_size))) {
  1137. return false;
  1138. }
  1139. for (i = 0; i < function_count; i++) {
  1140. InitializerExpression init_expr = { 0 };
  1141. #if WASM_ENABLE_REF_TYPES != 0
  1142. if (!use_init_expr) {
  1143. read_leb_uint32(p, p_end, function_index);
  1144. }
  1145. else {
  1146. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  1147. error_buf, error_buf_size))
  1148. return false;
  1149. function_index = init_expr.u.ref_index;
  1150. }
  1151. #else
  1152. read_leb_uint32(p, p_end, function_index);
  1153. #endif
  1154. /* since we are using -1 to indicate ref.null */
  1155. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  1156. && !check_function_index(module, function_index, error_buf,
  1157. error_buf_size)) {
  1158. return false;
  1159. }
  1160. table_segment->func_indexes[i] = function_index;
  1161. }
  1162. *p_buf = p;
  1163. return true;
  1164. }
  1165. static bool
  1166. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  1167. WASMModule *module, char *error_buf,
  1168. uint32 error_buf_size)
  1169. {
  1170. const uint8 *p = buf, *p_end = buf_end;
  1171. uint32 table_segment_count, i, table_index, function_count;
  1172. uint64 total_size;
  1173. WASMTableSeg *table_segment;
  1174. read_leb_uint32(p, p_end, table_segment_count);
  1175. if (table_segment_count) {
  1176. module->table_seg_count = table_segment_count;
  1177. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1178. if (!(module->table_segments =
  1179. loader_malloc(total_size, error_buf, error_buf_size))) {
  1180. return false;
  1181. }
  1182. table_segment = module->table_segments;
  1183. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1184. bh_assert(p < p_end);
  1185. #if WASM_ENABLE_REF_TYPES != 0
  1186. read_leb_uint32(p, p_end, table_segment->mode);
  1187. /* last three bits */
  1188. table_segment->mode = table_segment->mode & 0x07;
  1189. switch (table_segment->mode) {
  1190. /* elemkind/elemtype + active */
  1191. case 0:
  1192. case 4:
  1193. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  1194. table_segment->table_index = 0;
  1195. if (!check_table_index(module, table_segment->table_index,
  1196. error_buf, error_buf_size))
  1197. return false;
  1198. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1199. VALUE_TYPE_I32, error_buf,
  1200. error_buf_size))
  1201. return false;
  1202. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1203. table_segment->mode == 0 ? false
  1204. : true,
  1205. error_buf, error_buf_size))
  1206. return false;
  1207. break;
  1208. /* elemkind + passive/declarative */
  1209. case 1:
  1210. case 3:
  1211. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1212. true, error_buf, error_buf_size))
  1213. return false;
  1214. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1215. false, error_buf, error_buf_size))
  1216. return false;
  1217. break;
  1218. /* elemkind/elemtype + table_idx + active */
  1219. case 2:
  1220. case 6:
  1221. if (!load_table_index(&p, p_end, module,
  1222. &table_segment->table_index,
  1223. error_buf, error_buf_size))
  1224. return false;
  1225. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1226. VALUE_TYPE_I32, error_buf,
  1227. error_buf_size))
  1228. return false;
  1229. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1230. table_segment->mode == 2 ? true : false,
  1231. error_buf, error_buf_size))
  1232. return false;
  1233. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1234. table_segment->mode == 2 ? false
  1235. : true,
  1236. error_buf, error_buf_size))
  1237. return false;
  1238. break;
  1239. case 5:
  1240. case 7:
  1241. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1242. false, error_buf, error_buf_size))
  1243. return false;
  1244. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1245. true, error_buf, error_buf_size))
  1246. return false;
  1247. break;
  1248. default:
  1249. return false;
  1250. }
  1251. #else
  1252. /*
  1253. * like: 00 41 05 0b 04 00 01 00 01
  1254. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  1255. */
  1256. if (!load_table_index(&p, p_end, module,
  1257. &table_segment->table_index, error_buf,
  1258. error_buf_size))
  1259. return false;
  1260. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1261. VALUE_TYPE_I32, error_buf, error_buf_size))
  1262. return false;
  1263. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  1264. error_buf, error_buf_size))
  1265. return false;
  1266. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1267. }
  1268. }
  1269. (void)table_index;
  1270. (void)function_count;
  1271. bh_assert(p == p_end);
  1272. LOG_VERBOSE("Load table segment section success.\n");
  1273. return true;
  1274. }
  1275. static bool
  1276. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1277. WASMModule *module, char *error_buf,
  1278. uint32 error_buf_size)
  1279. {
  1280. const uint8 *p = buf, *p_end = buf_end;
  1281. uint32 data_seg_count, i, mem_index, data_seg_len;
  1282. uint64 total_size;
  1283. WASMDataSeg *dataseg;
  1284. InitializerExpression init_expr;
  1285. #if WASM_ENABLE_BULK_MEMORY != 0
  1286. bool is_passive = false;
  1287. uint32 mem_flag;
  1288. #endif
  1289. read_leb_uint32(p, p_end, data_seg_count);
  1290. #if WASM_ENABLE_BULK_MEMORY != 0
  1291. bh_assert(module->data_seg_count1 == 0
  1292. || data_seg_count == module->data_seg_count1);
  1293. #endif
  1294. if (data_seg_count) {
  1295. module->data_seg_count = data_seg_count;
  1296. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  1297. if (!(module->data_segments =
  1298. loader_malloc(total_size, error_buf, error_buf_size))) {
  1299. return false;
  1300. }
  1301. for (i = 0; i < data_seg_count; i++) {
  1302. read_leb_uint32(p, p_end, mem_index);
  1303. #if WASM_ENABLE_BULK_MEMORY != 0
  1304. is_passive = false;
  1305. mem_flag = mem_index & 0x03;
  1306. switch (mem_flag) {
  1307. case 0x01:
  1308. is_passive = true;
  1309. break;
  1310. case 0x00:
  1311. /* no memory index, treat index as 0 */
  1312. mem_index = 0;
  1313. goto check_mem_index;
  1314. case 0x02:
  1315. /* read following memory index */
  1316. read_leb_uint32(p, p_end, mem_index);
  1317. check_mem_index:
  1318. bh_assert(mem_index < module->import_memory_count
  1319. + module->memory_count);
  1320. break;
  1321. case 0x03:
  1322. default:
  1323. bh_assert(0);
  1324. break;
  1325. }
  1326. #else
  1327. bh_assert(mem_index
  1328. < module->import_memory_count + module->memory_count);
  1329. #endif /* WASM_ENABLE_BULK_MEMORY */
  1330. #if WASM_ENABLE_BULK_MEMORY != 0
  1331. if (!is_passive)
  1332. #endif
  1333. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  1334. error_buf, error_buf_size))
  1335. return false;
  1336. read_leb_uint32(p, p_end, data_seg_len);
  1337. if (!(dataseg = module->data_segments[i] = loader_malloc(
  1338. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  1339. return false;
  1340. }
  1341. #if WASM_ENABLE_BULK_MEMORY != 0
  1342. dataseg->is_passive = is_passive;
  1343. if (!is_passive)
  1344. #endif
  1345. {
  1346. bh_memcpy_s(&dataseg->base_offset,
  1347. sizeof(InitializerExpression), &init_expr,
  1348. sizeof(InitializerExpression));
  1349. dataseg->memory_index = mem_index;
  1350. }
  1351. dataseg->data_length = data_seg_len;
  1352. CHECK_BUF(p, p_end, data_seg_len);
  1353. dataseg->data = (uint8 *)p;
  1354. p += data_seg_len;
  1355. }
  1356. }
  1357. bh_assert(p == p_end);
  1358. LOG_VERBOSE("Load data segment section success.\n");
  1359. return true;
  1360. }
  1361. #if WASM_ENABLE_BULK_MEMORY != 0
  1362. static bool
  1363. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  1364. WASMModule *module, char *error_buf,
  1365. uint32 error_buf_size)
  1366. {
  1367. const uint8 *p = buf, *p_end = buf_end;
  1368. uint32 data_seg_count1 = 0;
  1369. read_leb_uint32(p, p_end, data_seg_count1);
  1370. module->data_seg_count1 = data_seg_count1;
  1371. bh_assert(p == p_end);
  1372. LOG_VERBOSE("Load datacount section success.\n");
  1373. return true;
  1374. }
  1375. #endif
  1376. static bool
  1377. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  1378. const uint8 *buf_func_end, WASMModule *module,
  1379. char *error_buf, uint32 error_buf_size)
  1380. {
  1381. const uint8 *p = buf, *p_end = buf_end;
  1382. const uint8 *p_func = buf_func;
  1383. uint32 func_count = 0, code_count;
  1384. /* code has been loaded in function section, so pass it here, just check
  1385. * whether function and code section have inconsistent lengths */
  1386. read_leb_uint32(p, p_end, code_count);
  1387. if (buf_func)
  1388. read_leb_uint32(p_func, buf_func_end, func_count);
  1389. bh_assert(func_count == code_count);
  1390. LOG_VERBOSE("Load code segment section success.\n");
  1391. (void)code_count;
  1392. (void)func_count;
  1393. return true;
  1394. }
  1395. static bool
  1396. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1397. char *error_buf, uint32 error_buf_size)
  1398. {
  1399. const uint8 *p = buf, *p_end = buf_end;
  1400. WASMType *type;
  1401. uint32 start_function;
  1402. read_leb_uint32(p, p_end, start_function);
  1403. bh_assert(start_function
  1404. < module->function_count + module->import_function_count);
  1405. if (start_function < module->import_function_count)
  1406. type = module->import_functions[start_function].u.function.func_type;
  1407. else
  1408. type = module->functions[start_function - module->import_function_count]
  1409. ->func_type;
  1410. bh_assert(type->param_count == 0 && type->result_count == 0);
  1411. module->start_function = start_function;
  1412. bh_assert(p == p_end);
  1413. LOG_VERBOSE("Load start section success.\n");
  1414. (void)type;
  1415. return true;
  1416. }
  1417. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1418. static bool
  1419. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1420. bool is_load_from_file_buf, char *error_buf,
  1421. uint32 error_buf_size)
  1422. {
  1423. const uint8 *p = buf, *p_end = buf_end;
  1424. uint32 name_type, subsection_size;
  1425. uint32 previous_name_type = 0;
  1426. uint32 num_func_name;
  1427. uint32 func_index;
  1428. uint32 previous_func_index = ~0U;
  1429. uint32 func_name_len;
  1430. uint32 name_index;
  1431. int i = 0;
  1432. bh_assert(p < p_end);
  1433. while (p < p_end) {
  1434. read_leb_uint32(p, p_end, name_type);
  1435. if (i != 0) {
  1436. bh_assert(name_type > previous_name_type);
  1437. }
  1438. previous_name_type = name_type;
  1439. read_leb_uint32(p, p_end, subsection_size);
  1440. CHECK_BUF(p, p_end, subsection_size);
  1441. switch (name_type) {
  1442. case SUB_SECTION_TYPE_FUNC:
  1443. if (subsection_size) {
  1444. read_leb_uint32(p, p_end, num_func_name);
  1445. for (name_index = 0; name_index < num_func_name;
  1446. name_index++) {
  1447. read_leb_uint32(p, p_end, func_index);
  1448. bh_assert(func_index > previous_func_index);
  1449. previous_func_index = func_index;
  1450. read_leb_uint32(p, p_end, func_name_len);
  1451. CHECK_BUF(p, p_end, func_name_len);
  1452. /* Skip the import functions */
  1453. if (func_index >= module->import_count) {
  1454. func_index -= module->import_count;
  1455. bh_assert(func_index < module->function_count);
  1456. if (!(module->functions[func_index]->field_name =
  1457. const_str_list_insert(
  1458. p, func_name_len, module,
  1459. is_load_from_file_buf, error_buf,
  1460. error_buf_size))) {
  1461. return false;
  1462. }
  1463. }
  1464. p += func_name_len;
  1465. }
  1466. }
  1467. break;
  1468. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  1469. */
  1470. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  1471. default:
  1472. p = p + subsection_size;
  1473. break;
  1474. }
  1475. i++;
  1476. }
  1477. return true;
  1478. }
  1479. #endif
  1480. static bool
  1481. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1482. bool is_load_from_file_buf, char *error_buf,
  1483. uint32 error_buf_size)
  1484. {
  1485. const uint8 *p = buf, *p_end = buf_end;
  1486. uint32 name_len;
  1487. bh_assert(p < p_end);
  1488. read_leb_uint32(p, p_end, name_len);
  1489. bh_assert(name_len > 0 && p + name_len <= p_end);
  1490. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1491. if (memcmp(p, "name", 4) == 0) {
  1492. p += name_len;
  1493. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  1494. error_buf_size);
  1495. }
  1496. #endif
  1497. LOG_VERBOSE("Load custom section success.\n");
  1498. (void)name_len;
  1499. return true;
  1500. }
  1501. #if WASM_ENABLE_REF_TYPES != 0
  1502. static bool
  1503. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  1504. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  1505. {
  1506. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  1507. return false;
  1508. }
  1509. if (p_elem_type) {
  1510. if (table_idx < module->import_table_count)
  1511. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  1512. else
  1513. *p_elem_type =
  1514. module->tables[module->import_table_count + table_idx]
  1515. .elem_type;
  1516. }
  1517. return true;
  1518. }
  1519. static bool
  1520. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  1521. uint8 *p_elem_type, char *error_buf,
  1522. uint32 error_buf_size)
  1523. {
  1524. if (table_seg_idx >= module->table_seg_count) {
  1525. return false;
  1526. }
  1527. if (p_elem_type) {
  1528. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  1529. }
  1530. return true;
  1531. }
  1532. #endif
  1533. static bool
  1534. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1535. uint32 cur_func_idx, char *error_buf,
  1536. uint32 error_buf_size);
  1537. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  1538. void **
  1539. wasm_interp_get_handle_table();
  1540. static void **handle_table;
  1541. #endif
  1542. static bool
  1543. load_from_sections(WASMModule *module, WASMSection *sections,
  1544. bool is_load_from_file_buf, char *error_buf,
  1545. uint32 error_buf_size)
  1546. {
  1547. WASMExport *export;
  1548. WASMSection *section = sections;
  1549. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1550. *buf_func = NULL, *buf_func_end = NULL;
  1551. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  1552. WASMGlobal *aux_stack_top_global = NULL, *global;
  1553. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  1554. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  1555. uint32 aux_data_end_global_index = (uint32)-1;
  1556. uint32 aux_heap_base_global_index = (uint32)-1;
  1557. WASMType *func_type;
  1558. /* Find code and function sections if have */
  1559. while (section) {
  1560. if (section->section_type == SECTION_TYPE_CODE) {
  1561. buf_code = section->section_body;
  1562. buf_code_end = buf_code + section->section_body_size;
  1563. }
  1564. else if (section->section_type == SECTION_TYPE_FUNC) {
  1565. buf_func = section->section_body;
  1566. buf_func_end = buf_func + section->section_body_size;
  1567. }
  1568. section = section->next;
  1569. }
  1570. section = sections;
  1571. while (section) {
  1572. buf = section->section_body;
  1573. buf_end = buf + section->section_body_size;
  1574. LOG_DEBUG("load section, type: %d", section->section_type);
  1575. switch (section->section_type) {
  1576. case SECTION_TYPE_USER:
  1577. /* unsupported user section, ignore it. */
  1578. if (!load_user_section(buf, buf_end, module,
  1579. is_load_from_file_buf, error_buf,
  1580. error_buf_size))
  1581. return false;
  1582. break;
  1583. case SECTION_TYPE_TYPE:
  1584. if (!load_type_section(buf, buf_end, module, error_buf,
  1585. error_buf_size))
  1586. return false;
  1587. break;
  1588. case SECTION_TYPE_IMPORT:
  1589. if (!load_import_section(buf, buf_end, module,
  1590. is_load_from_file_buf, error_buf,
  1591. error_buf_size))
  1592. return false;
  1593. break;
  1594. case SECTION_TYPE_FUNC:
  1595. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  1596. module, error_buf, error_buf_size))
  1597. return false;
  1598. break;
  1599. case SECTION_TYPE_TABLE:
  1600. if (!load_table_section(buf, buf_end, module, error_buf,
  1601. error_buf_size))
  1602. return false;
  1603. break;
  1604. case SECTION_TYPE_MEMORY:
  1605. if (!load_memory_section(buf, buf_end, module, error_buf,
  1606. error_buf_size))
  1607. return false;
  1608. break;
  1609. case SECTION_TYPE_GLOBAL:
  1610. if (!load_global_section(buf, buf_end, module, error_buf,
  1611. error_buf_size))
  1612. return false;
  1613. break;
  1614. case SECTION_TYPE_EXPORT:
  1615. if (!load_export_section(buf, buf_end, module,
  1616. is_load_from_file_buf, error_buf,
  1617. error_buf_size))
  1618. return false;
  1619. break;
  1620. case SECTION_TYPE_START:
  1621. if (!load_start_section(buf, buf_end, module, error_buf,
  1622. error_buf_size))
  1623. return false;
  1624. break;
  1625. case SECTION_TYPE_ELEM:
  1626. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  1627. error_buf_size))
  1628. return false;
  1629. break;
  1630. case SECTION_TYPE_CODE:
  1631. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  1632. module, error_buf, error_buf_size))
  1633. return false;
  1634. break;
  1635. case SECTION_TYPE_DATA:
  1636. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  1637. error_buf_size))
  1638. return false;
  1639. break;
  1640. #if WASM_ENABLE_BULK_MEMORY != 0
  1641. case SECTION_TYPE_DATACOUNT:
  1642. if (!load_datacount_section(buf, buf_end, module, error_buf,
  1643. error_buf_size))
  1644. return false;
  1645. break;
  1646. #endif
  1647. default:
  1648. set_error_buf(error_buf, error_buf_size, "invalid section id");
  1649. return false;
  1650. }
  1651. section = section->next;
  1652. }
  1653. module->aux_data_end_global_index = (uint32)-1;
  1654. module->aux_heap_base_global_index = (uint32)-1;
  1655. module->aux_stack_top_global_index = (uint32)-1;
  1656. /* Resolve auxiliary data/stack/heap info and reset memory info */
  1657. export = module->exports;
  1658. for (i = 0; i < module->export_count; i++, export ++) {
  1659. if (export->kind == EXPORT_KIND_GLOBAL) {
  1660. if (!strcmp(export->name, "__heap_base")) {
  1661. global_index = export->index - module->import_global_count;
  1662. global = module->globals + global_index;
  1663. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  1664. && global->init_expr.init_expr_type
  1665. == INIT_EXPR_TYPE_I32_CONST) {
  1666. aux_heap_base_global = global;
  1667. aux_heap_base = global->init_expr.u.i32;
  1668. aux_heap_base_global_index = export->index;
  1669. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  1670. aux_heap_base);
  1671. }
  1672. }
  1673. else if (!strcmp(export->name, "__data_end")) {
  1674. global_index = export->index - module->import_global_count;
  1675. global = module->globals + global_index;
  1676. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  1677. && global->init_expr.init_expr_type
  1678. == INIT_EXPR_TYPE_I32_CONST) {
  1679. aux_data_end_global = global;
  1680. aux_data_end = global->init_expr.u.i32;
  1681. aux_data_end_global_index = export->index;
  1682. LOG_VERBOSE("Found aux __data_end global, value: %d",
  1683. aux_data_end);
  1684. aux_data_end = align_uint(aux_data_end, 16);
  1685. }
  1686. }
  1687. /* For module compiled with -pthread option, the global is:
  1688. [0] stack_top <-- 0
  1689. [1] tls_pointer
  1690. [2] tls_size
  1691. [3] data_end <-- 3
  1692. [4] global_base
  1693. [5] heap_base <-- 5
  1694. [6] dso_handle
  1695. For module compiled without -pthread option:
  1696. [0] stack_top <-- 0
  1697. [1] data_end <-- 1
  1698. [2] global_base
  1699. [3] heap_base <-- 3
  1700. [4] dso_handle
  1701. */
  1702. if (aux_data_end_global && aux_heap_base_global
  1703. && aux_data_end <= aux_heap_base) {
  1704. module->aux_data_end_global_index = aux_data_end_global_index;
  1705. module->aux_data_end = aux_data_end;
  1706. module->aux_heap_base_global_index = aux_heap_base_global_index;
  1707. module->aux_heap_base = aux_heap_base;
  1708. /* Resolve aux stack top global */
  1709. for (global_index = 0; global_index < module->global_count;
  1710. global_index++) {
  1711. global = module->globals + global_index;
  1712. if (global->is_mutable /* heap_base and data_end is
  1713. not mutable */
  1714. && global->type == VALUE_TYPE_I32
  1715. && global->init_expr.init_expr_type
  1716. == INIT_EXPR_TYPE_I32_CONST
  1717. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  1718. aux_stack_top_global = global;
  1719. aux_stack_top = (uint32)global->init_expr.u.i32;
  1720. module->aux_stack_top_global_index =
  1721. module->import_global_count + global_index;
  1722. module->aux_stack_bottom = aux_stack_top;
  1723. module->aux_stack_size =
  1724. aux_stack_top > aux_data_end
  1725. ? aux_stack_top - aux_data_end
  1726. : aux_stack_top;
  1727. LOG_VERBOSE("Found aux stack top global, value: %d, "
  1728. "global index: %d, stack size: %d",
  1729. aux_stack_top, global_index,
  1730. module->aux_stack_size);
  1731. break;
  1732. }
  1733. }
  1734. if (!aux_stack_top_global) {
  1735. /* Auxiliary stack global isn't found, it must be unused
  1736. in the wasm app, as if it is used, the global must be
  1737. defined. Here we set it to __heap_base global and set
  1738. its size to 0. */
  1739. aux_stack_top_global = aux_heap_base_global;
  1740. aux_stack_top = aux_heap_base;
  1741. module->aux_stack_top_global_index =
  1742. module->aux_heap_base_global_index;
  1743. module->aux_stack_bottom = aux_stack_top;
  1744. module->aux_stack_size = 0;
  1745. }
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. module->malloc_function = (uint32)-1;
  1751. module->free_function = (uint32)-1;
  1752. module->retain_function = (uint32)-1;
  1753. /* Resolve malloc/free function exported by wasm module */
  1754. export = module->exports;
  1755. for (i = 0; i < module->export_count; i++, export ++) {
  1756. if (export->kind == EXPORT_KIND_FUNC) {
  1757. if (!strcmp(export->name, "malloc")
  1758. && export->index >= module->import_function_count) {
  1759. func_index = export->index - module->import_function_count;
  1760. func_type = module->functions[func_index]->func_type;
  1761. if (func_type->param_count == 1 && func_type->result_count == 1
  1762. && func_type->types[0] == VALUE_TYPE_I32
  1763. && func_type->types[1] == VALUE_TYPE_I32) {
  1764. bh_assert(module->malloc_function == (uint32)-1);
  1765. module->malloc_function = export->index;
  1766. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  1767. export->name, export->index);
  1768. }
  1769. }
  1770. else if (!strcmp(export->name, "__new")
  1771. && export->index >= module->import_function_count) {
  1772. /* __new && __pin for AssemblyScript */
  1773. func_index = export->index - module->import_function_count;
  1774. func_type = module->functions[func_index]->func_type;
  1775. if (func_type->param_count == 2 && func_type->result_count == 1
  1776. && func_type->types[0] == VALUE_TYPE_I32
  1777. && func_type->types[1] == VALUE_TYPE_I32
  1778. && func_type->types[2] == VALUE_TYPE_I32) {
  1779. uint32 j;
  1780. WASMExport *export_tmp;
  1781. bh_assert(module->malloc_function == (uint32)-1);
  1782. module->malloc_function = export->index;
  1783. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  1784. export->name, export->index);
  1785. /* resolve retain function.
  1786. If not found, reset malloc function index */
  1787. export_tmp = module->exports;
  1788. for (j = 0; j < module->export_count; j++, export_tmp++) {
  1789. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  1790. && (!strcmp(export_tmp->name, "__retain")
  1791. || !strcmp(export_tmp->name, "__pin"))
  1792. && (export_tmp->index
  1793. >= module->import_function_count)) {
  1794. func_index = export_tmp->index
  1795. - module->import_function_count;
  1796. func_type =
  1797. module->functions[func_index]->func_type;
  1798. if (func_type->param_count == 1
  1799. && func_type->result_count == 1
  1800. && func_type->types[0] == VALUE_TYPE_I32
  1801. && func_type->types[1] == VALUE_TYPE_I32) {
  1802. bh_assert(module->retain_function
  1803. == (uint32)-1);
  1804. module->retain_function = export_tmp->index;
  1805. LOG_VERBOSE("Found retain function, name: %s, "
  1806. "index: %u",
  1807. export_tmp->name,
  1808. export_tmp->index);
  1809. break;
  1810. }
  1811. }
  1812. }
  1813. if (j == module->export_count) {
  1814. module->malloc_function = (uint32)-1;
  1815. LOG_VERBOSE("Can't find retain function,"
  1816. "reset malloc function index to -1");
  1817. }
  1818. }
  1819. }
  1820. else if (((!strcmp(export->name, "free"))
  1821. || (!strcmp(export->name, "__release"))
  1822. || (!strcmp(export->name, "__unpin")))
  1823. && export->index >= module->import_function_count) {
  1824. func_index = export->index - module->import_function_count;
  1825. func_type = module->functions[func_index]->func_type;
  1826. if (func_type->param_count == 1 && func_type->result_count == 0
  1827. && func_type->types[0] == VALUE_TYPE_I32) {
  1828. bh_assert(module->free_function == (uint32)-1);
  1829. module->free_function = export->index;
  1830. LOG_VERBOSE("Found free function, name: %s, index: %u",
  1831. export->name, export->index);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  1837. handle_table = wasm_interp_get_handle_table();
  1838. #endif
  1839. for (i = 0; i < module->function_count; i++) {
  1840. WASMFunction *func = module->functions[i];
  1841. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  1842. error_buf_size)) {
  1843. return false;
  1844. }
  1845. }
  1846. if (!module->possible_memory_grow) {
  1847. WASMMemoryImport *memory_import;
  1848. WASMMemory *memory;
  1849. if (aux_data_end_global && aux_heap_base_global
  1850. && aux_stack_top_global) {
  1851. uint64 init_memory_size;
  1852. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  1853. if (module->import_memory_count) {
  1854. memory_import = &module->import_memories[0].u.memory;
  1855. init_memory_size = (uint64)memory_import->num_bytes_per_page
  1856. * memory_import->init_page_count;
  1857. if (shrunk_memory_size <= init_memory_size) {
  1858. /* Reset memory info to decrease memory usage */
  1859. memory_import->num_bytes_per_page = shrunk_memory_size;
  1860. memory_import->init_page_count = 1;
  1861. LOG_VERBOSE("Shrink import memory size to %d",
  1862. shrunk_memory_size);
  1863. }
  1864. }
  1865. if (module->memory_count) {
  1866. memory = &module->memories[0];
  1867. init_memory_size = (uint64)memory->num_bytes_per_page
  1868. * memory->init_page_count;
  1869. if (shrunk_memory_size <= init_memory_size) {
  1870. /* Reset memory info to decrease memory usage */
  1871. memory->num_bytes_per_page = shrunk_memory_size;
  1872. memory->init_page_count = 1;
  1873. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  1874. }
  1875. }
  1876. }
  1877. if (module->import_memory_count) {
  1878. memory_import = &module->import_memories[0].u.memory;
  1879. /* Memory init page count cannot be larger than 65536, we don't
  1880. check integer overflow again. */
  1881. memory_import->num_bytes_per_page *= memory_import->init_page_count;
  1882. memory_import->init_page_count = memory_import->max_page_count = 1;
  1883. }
  1884. if (module->memory_count) {
  1885. /* Memory init page count cannot be larger than 65536, we don't
  1886. check integer overflow again. */
  1887. memory = &module->memories[0];
  1888. memory->num_bytes_per_page *= memory->init_page_count;
  1889. memory->init_page_count = memory->max_page_count = 1;
  1890. }
  1891. }
  1892. #if WASM_ENABLE_FAST_JIT != 0
  1893. if (!(module->fast_jit_func_ptrs =
  1894. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  1895. error_buf_size))) {
  1896. return false;
  1897. }
  1898. if (!jit_compiler_compile_all(module)) {
  1899. set_error_buf(error_buf, error_buf_size, "fast jit compilation failed");
  1900. return false;
  1901. }
  1902. #endif
  1903. #if WASM_ENABLE_MEMORY_TRACING != 0
  1904. wasm_runtime_dump_module_mem_consumption(module);
  1905. #endif
  1906. return true;
  1907. }
  1908. static WASMModule *
  1909. create_module(char *error_buf, uint32 error_buf_size)
  1910. {
  1911. WASMModule *module =
  1912. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  1913. #if WASM_ENABLE_FAST_INTERP == 0
  1914. bh_list_status ret;
  1915. #endif
  1916. if (!module) {
  1917. return NULL;
  1918. }
  1919. module->module_type = Wasm_Module_Bytecode;
  1920. /* Set start_function to -1, means no start function */
  1921. module->start_function = (uint32)-1;
  1922. #if WASM_ENABLE_FAST_INTERP == 0
  1923. module->br_table_cache_list = &module->br_table_cache_list_head;
  1924. ret = bh_list_init(module->br_table_cache_list);
  1925. bh_assert(ret == BH_LIST_SUCCESS);
  1926. (void)ret;
  1927. #endif
  1928. return module;
  1929. }
  1930. WASMModule *
  1931. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  1932. uint32 error_buf_size)
  1933. {
  1934. WASMModule *module = create_module(error_buf, error_buf_size);
  1935. if (!module)
  1936. return NULL;
  1937. if (!load_from_sections(module, section_list, false, error_buf,
  1938. error_buf_size)) {
  1939. wasm_loader_unload(module);
  1940. return NULL;
  1941. }
  1942. LOG_VERBOSE("Load module from sections success.\n");
  1943. return module;
  1944. }
  1945. static void
  1946. destroy_sections(WASMSection *section_list)
  1947. {
  1948. WASMSection *section = section_list, *next;
  1949. while (section) {
  1950. next = section->next;
  1951. wasm_runtime_free(section);
  1952. section = next;
  1953. }
  1954. }
  1955. /* clang-format off */
  1956. static uint8 section_ids[] = {
  1957. SECTION_TYPE_USER,
  1958. SECTION_TYPE_TYPE,
  1959. SECTION_TYPE_IMPORT,
  1960. SECTION_TYPE_FUNC,
  1961. SECTION_TYPE_TABLE,
  1962. SECTION_TYPE_MEMORY,
  1963. SECTION_TYPE_GLOBAL,
  1964. SECTION_TYPE_EXPORT,
  1965. SECTION_TYPE_START,
  1966. SECTION_TYPE_ELEM,
  1967. #if WASM_ENABLE_BULK_MEMORY != 0
  1968. SECTION_TYPE_DATACOUNT,
  1969. #endif
  1970. SECTION_TYPE_CODE,
  1971. SECTION_TYPE_DATA
  1972. };
  1973. /* clang-format on */
  1974. static uint8
  1975. get_section_index(uint8 section_type)
  1976. {
  1977. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  1978. for (uint8 i = 0; i < max_id; i++) {
  1979. if (section_type == section_ids[i])
  1980. return i;
  1981. }
  1982. return (uint8)-1;
  1983. }
  1984. static bool
  1985. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  1986. char *error_buf, uint32 error_buf_size)
  1987. {
  1988. WASMSection *section_list_end = NULL, *section;
  1989. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  1990. uint8 section_type, section_index, last_section_index = (uint8)-1;
  1991. uint32 section_size;
  1992. bh_assert(!*p_section_list);
  1993. p += 8;
  1994. while (p < p_end) {
  1995. CHECK_BUF(p, p_end, 1);
  1996. section_type = read_uint8(p);
  1997. section_index = get_section_index(section_type);
  1998. if (section_index != (uint8)-1) {
  1999. if (section_type != SECTION_TYPE_USER) {
  2000. /* Custom sections may be inserted at any place,
  2001. while other sections must occur at most once
  2002. and in prescribed order. */
  2003. bh_assert(last_section_index == (uint8)-1
  2004. || last_section_index < section_index);
  2005. last_section_index = section_index;
  2006. }
  2007. read_leb_uint32(p, p_end, section_size);
  2008. CHECK_BUF1(p, p_end, section_size);
  2009. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  2010. error_buf_size))) {
  2011. return false;
  2012. }
  2013. section->section_type = section_type;
  2014. section->section_body = (uint8 *)p;
  2015. section->section_body_size = section_size;
  2016. if (!*p_section_list)
  2017. *p_section_list = section_list_end = section;
  2018. else {
  2019. section_list_end->next = section;
  2020. section_list_end = section;
  2021. }
  2022. p += section_size;
  2023. }
  2024. else {
  2025. bh_assert(0);
  2026. }
  2027. }
  2028. (void)last_section_index;
  2029. return true;
  2030. }
  2031. static void
  2032. exchange32(uint8 *p_data)
  2033. {
  2034. uint8 value = *p_data;
  2035. *p_data = *(p_data + 3);
  2036. *(p_data + 3) = value;
  2037. value = *(p_data + 1);
  2038. *(p_data + 1) = *(p_data + 2);
  2039. *(p_data + 2) = value;
  2040. }
  2041. static union {
  2042. int a;
  2043. char b;
  2044. } __ue = { .a = 1 };
  2045. #define is_little_endian() (__ue.b == 1)
  2046. static bool
  2047. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  2048. uint32 error_buf_size)
  2049. {
  2050. const uint8 *buf_end = buf + size;
  2051. const uint8 *p = buf, *p_end = buf_end;
  2052. uint32 magic_number, version;
  2053. WASMSection *section_list = NULL;
  2054. CHECK_BUF1(p, p_end, sizeof(uint32));
  2055. magic_number = read_uint32(p);
  2056. if (!is_little_endian())
  2057. exchange32((uint8 *)&magic_number);
  2058. bh_assert(magic_number == WASM_MAGIC_NUMBER);
  2059. CHECK_BUF1(p, p_end, sizeof(uint32));
  2060. version = read_uint32(p);
  2061. if (!is_little_endian())
  2062. exchange32((uint8 *)&version);
  2063. if (version != WASM_CURRENT_VERSION) {
  2064. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2065. return false;
  2066. }
  2067. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  2068. || !load_from_sections(module, section_list, true, error_buf,
  2069. error_buf_size)) {
  2070. destroy_sections(section_list);
  2071. return false;
  2072. }
  2073. destroy_sections(section_list);
  2074. (void)p_end;
  2075. return true;
  2076. }
  2077. WASMModule *
  2078. wasm_loader_load(uint8 *buf, uint32 size, char *error_buf,
  2079. uint32 error_buf_size)
  2080. {
  2081. WASMModule *module = create_module(error_buf, error_buf_size);
  2082. if (!module) {
  2083. return NULL;
  2084. }
  2085. #if WASM_ENABLE_FAST_JIT != 0
  2086. module->load_addr = (uint8 *)buf;
  2087. module->load_size = size;
  2088. #endif
  2089. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2090. goto fail;
  2091. }
  2092. LOG_VERBOSE("Load module success.\n");
  2093. return module;
  2094. fail:
  2095. wasm_loader_unload(module);
  2096. return NULL;
  2097. }
  2098. void
  2099. wasm_loader_unload(WASMModule *module)
  2100. {
  2101. uint32 i;
  2102. if (!module)
  2103. return;
  2104. if (module->types) {
  2105. for (i = 0; i < module->type_count; i++) {
  2106. if (module->types[i])
  2107. wasm_runtime_free(module->types[i]);
  2108. }
  2109. wasm_runtime_free(module->types);
  2110. }
  2111. if (module->imports)
  2112. wasm_runtime_free(module->imports);
  2113. if (module->functions) {
  2114. for (i = 0; i < module->function_count; i++) {
  2115. if (module->functions[i]) {
  2116. if (module->functions[i]->local_offsets)
  2117. wasm_runtime_free(module->functions[i]->local_offsets);
  2118. #if WASM_ENABLE_FAST_INTERP != 0
  2119. if (module->functions[i]->code_compiled)
  2120. wasm_runtime_free(module->functions[i]->code_compiled);
  2121. if (module->functions[i]->consts)
  2122. wasm_runtime_free(module->functions[i]->consts);
  2123. #endif
  2124. wasm_runtime_free(module->functions[i]);
  2125. }
  2126. }
  2127. wasm_runtime_free(module->functions);
  2128. }
  2129. if (module->tables)
  2130. wasm_runtime_free(module->tables);
  2131. if (module->memories)
  2132. wasm_runtime_free(module->memories);
  2133. if (module->globals)
  2134. wasm_runtime_free(module->globals);
  2135. if (module->exports)
  2136. wasm_runtime_free(module->exports);
  2137. if (module->table_segments) {
  2138. for (i = 0; i < module->table_seg_count; i++) {
  2139. if (module->table_segments[i].func_indexes)
  2140. wasm_runtime_free(module->table_segments[i].func_indexes);
  2141. }
  2142. wasm_runtime_free(module->table_segments);
  2143. }
  2144. if (module->data_segments) {
  2145. for (i = 0; i < module->data_seg_count; i++) {
  2146. if (module->data_segments[i])
  2147. wasm_runtime_free(module->data_segments[i]);
  2148. }
  2149. wasm_runtime_free(module->data_segments);
  2150. }
  2151. if (module->const_str_list) {
  2152. StringNode *node = module->const_str_list, *node_next;
  2153. while (node) {
  2154. node_next = node->next;
  2155. wasm_runtime_free(node);
  2156. node = node_next;
  2157. }
  2158. }
  2159. #if WASM_ENABLE_FAST_INTERP == 0
  2160. if (module->br_table_cache_list) {
  2161. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  2162. BrTableCache *node_next;
  2163. while (node) {
  2164. node_next = bh_list_elem_next(node);
  2165. wasm_runtime_free(node);
  2166. node = node_next;
  2167. }
  2168. }
  2169. #endif
  2170. #if WASM_ENABLE_FAST_JIT != 0
  2171. if (module->fast_jit_func_ptrs) {
  2172. for (i = 0; i < module->function_count; i++) {
  2173. if (module->fast_jit_func_ptrs[i])
  2174. jit_code_cache_free(module->fast_jit_func_ptrs[i]);
  2175. }
  2176. wasm_runtime_free(module->fast_jit_func_ptrs);
  2177. }
  2178. #endif
  2179. wasm_runtime_free(module);
  2180. }
  2181. bool
  2182. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  2183. const uint8 *start_addr, const uint8 *code_end_addr,
  2184. uint8 label_type, uint8 **p_else_addr,
  2185. uint8 **p_end_addr)
  2186. {
  2187. const uint8 *p = start_addr, *p_end = code_end_addr;
  2188. uint8 *else_addr = NULL;
  2189. char error_buf[128];
  2190. uint32 block_nested_depth = 1, count, i, j, t;
  2191. uint32 error_buf_size = sizeof(error_buf);
  2192. uint8 opcode, u8;
  2193. BlockAddr block_stack[16] = { 0 }, *block;
  2194. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2195. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2196. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  2197. if (block[j].start_addr == start_addr) {
  2198. /* Cache hit */
  2199. *p_else_addr = block[j].else_addr;
  2200. *p_end_addr = block[j].end_addr;
  2201. return true;
  2202. }
  2203. }
  2204. /* Cache unhit */
  2205. block_stack[0].start_addr = start_addr;
  2206. while (p < code_end_addr) {
  2207. opcode = *p++;
  2208. switch (opcode) {
  2209. case WASM_OP_UNREACHABLE:
  2210. case WASM_OP_NOP:
  2211. break;
  2212. case WASM_OP_BLOCK:
  2213. case WASM_OP_LOOP:
  2214. case WASM_OP_IF:
  2215. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  2216. u8 = read_uint8(p);
  2217. if (block_nested_depth
  2218. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2219. block_stack[block_nested_depth].start_addr = p;
  2220. block_stack[block_nested_depth].else_addr = NULL;
  2221. }
  2222. block_nested_depth++;
  2223. break;
  2224. case EXT_OP_BLOCK:
  2225. case EXT_OP_LOOP:
  2226. case EXT_OP_IF:
  2227. /* block type */
  2228. skip_leb_uint32(p, p_end);
  2229. if (block_nested_depth
  2230. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2231. block_stack[block_nested_depth].start_addr = p;
  2232. block_stack[block_nested_depth].else_addr = NULL;
  2233. }
  2234. block_nested_depth++;
  2235. break;
  2236. case WASM_OP_ELSE:
  2237. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  2238. else_addr = (uint8 *)(p - 1);
  2239. if (block_nested_depth - 1
  2240. < sizeof(block_stack) / sizeof(BlockAddr))
  2241. block_stack[block_nested_depth - 1].else_addr =
  2242. (uint8 *)(p - 1);
  2243. break;
  2244. case WASM_OP_END:
  2245. if (block_nested_depth == 1) {
  2246. if (label_type == LABEL_TYPE_IF)
  2247. *p_else_addr = else_addr;
  2248. *p_end_addr = (uint8 *)(p - 1);
  2249. block_stack[0].end_addr = (uint8 *)(p - 1);
  2250. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  2251. t++) {
  2252. start_addr = block_stack[t].start_addr;
  2253. if (start_addr) {
  2254. i = ((uintptr_t)start_addr)
  2255. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2256. block =
  2257. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2258. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  2259. if (!block[j].start_addr)
  2260. break;
  2261. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  2262. memmove(block + 1, block,
  2263. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  2264. * sizeof(BlockAddr));
  2265. j = 0;
  2266. }
  2267. block[j].start_addr = block_stack[t].start_addr;
  2268. block[j].else_addr = block_stack[t].else_addr;
  2269. block[j].end_addr = block_stack[t].end_addr;
  2270. }
  2271. else
  2272. break;
  2273. }
  2274. return true;
  2275. }
  2276. else {
  2277. block_nested_depth--;
  2278. if (block_nested_depth
  2279. < sizeof(block_stack) / sizeof(BlockAddr))
  2280. block_stack[block_nested_depth].end_addr =
  2281. (uint8 *)(p - 1);
  2282. }
  2283. break;
  2284. case WASM_OP_BR:
  2285. case WASM_OP_BR_IF:
  2286. skip_leb_uint32(p, p_end); /* labelidx */
  2287. break;
  2288. case WASM_OP_BR_TABLE:
  2289. read_leb_uint32(p, p_end, count); /* lable num */
  2290. #if WASM_ENABLE_FAST_INTERP != 0
  2291. for (i = 0; i <= count; i++) /* lableidxs */
  2292. skip_leb_uint32(p, p_end);
  2293. #else
  2294. p += count + 1;
  2295. while (*p == WASM_OP_NOP)
  2296. p++;
  2297. #endif
  2298. break;
  2299. #if WASM_ENABLE_FAST_INTERP == 0
  2300. case EXT_OP_BR_TABLE_CACHE:
  2301. read_leb_uint32(p, p_end, count); /* lable num */
  2302. while (*p == WASM_OP_NOP)
  2303. p++;
  2304. break;
  2305. #endif
  2306. case WASM_OP_RETURN:
  2307. break;
  2308. case WASM_OP_CALL:
  2309. #if WASM_ENABLE_TAIL_CALL != 0
  2310. case WASM_OP_RETURN_CALL:
  2311. #endif
  2312. skip_leb_uint32(p, p_end); /* funcidx */
  2313. break;
  2314. case WASM_OP_CALL_INDIRECT:
  2315. #if WASM_ENABLE_TAIL_CALL != 0
  2316. case WASM_OP_RETURN_CALL_INDIRECT:
  2317. #endif
  2318. skip_leb_uint32(p, p_end); /* typeidx */
  2319. CHECK_BUF(p, p_end, 1);
  2320. u8 = read_uint8(p); /* 0x00 */
  2321. break;
  2322. case WASM_OP_DROP:
  2323. case WASM_OP_SELECT:
  2324. case WASM_OP_DROP_64:
  2325. case WASM_OP_SELECT_64:
  2326. break;
  2327. #if WASM_ENABLE_REF_TYPES != 0
  2328. case WASM_OP_SELECT_T:
  2329. skip_leb_uint32(p, p_end); /* vec length */
  2330. CHECK_BUF(p, p_end, 1);
  2331. u8 = read_uint8(p); /* typeidx */
  2332. break;
  2333. case WASM_OP_TABLE_GET:
  2334. case WASM_OP_TABLE_SET:
  2335. skip_leb_uint32(p, p_end); /* table index */
  2336. break;
  2337. case WASM_OP_REF_NULL:
  2338. CHECK_BUF(p, p_end, 1);
  2339. u8 = read_uint8(p); /* type */
  2340. break;
  2341. case WASM_OP_REF_IS_NULL:
  2342. break;
  2343. case WASM_OP_REF_FUNC:
  2344. skip_leb_uint32(p, p_end); /* func index */
  2345. break;
  2346. #endif /* WASM_ENABLE_REF_TYPES */
  2347. case WASM_OP_GET_LOCAL:
  2348. case WASM_OP_SET_LOCAL:
  2349. case WASM_OP_TEE_LOCAL:
  2350. case WASM_OP_GET_GLOBAL:
  2351. case WASM_OP_SET_GLOBAL:
  2352. case WASM_OP_GET_GLOBAL_64:
  2353. case WASM_OP_SET_GLOBAL_64:
  2354. case WASM_OP_SET_GLOBAL_AUX_STACK:
  2355. skip_leb_uint32(p, p_end); /* localidx */
  2356. break;
  2357. case EXT_OP_GET_LOCAL_FAST:
  2358. case EXT_OP_SET_LOCAL_FAST:
  2359. case EXT_OP_TEE_LOCAL_FAST:
  2360. CHECK_BUF(p, p_end, 1);
  2361. p++;
  2362. break;
  2363. case WASM_OP_I32_LOAD:
  2364. case WASM_OP_I64_LOAD:
  2365. case WASM_OP_F32_LOAD:
  2366. case WASM_OP_F64_LOAD:
  2367. case WASM_OP_I32_LOAD8_S:
  2368. case WASM_OP_I32_LOAD8_U:
  2369. case WASM_OP_I32_LOAD16_S:
  2370. case WASM_OP_I32_LOAD16_U:
  2371. case WASM_OP_I64_LOAD8_S:
  2372. case WASM_OP_I64_LOAD8_U:
  2373. case WASM_OP_I64_LOAD16_S:
  2374. case WASM_OP_I64_LOAD16_U:
  2375. case WASM_OP_I64_LOAD32_S:
  2376. case WASM_OP_I64_LOAD32_U:
  2377. case WASM_OP_I32_STORE:
  2378. case WASM_OP_I64_STORE:
  2379. case WASM_OP_F32_STORE:
  2380. case WASM_OP_F64_STORE:
  2381. case WASM_OP_I32_STORE8:
  2382. case WASM_OP_I32_STORE16:
  2383. case WASM_OP_I64_STORE8:
  2384. case WASM_OP_I64_STORE16:
  2385. case WASM_OP_I64_STORE32:
  2386. skip_leb_uint32(p, p_end); /* align */
  2387. skip_leb_uint32(p, p_end); /* offset */
  2388. break;
  2389. case WASM_OP_MEMORY_SIZE:
  2390. case WASM_OP_MEMORY_GROW:
  2391. skip_leb_uint32(p, p_end); /* 0x00 */
  2392. break;
  2393. case WASM_OP_I32_CONST:
  2394. skip_leb_int32(p, p_end);
  2395. break;
  2396. case WASM_OP_I64_CONST:
  2397. skip_leb_int64(p, p_end);
  2398. break;
  2399. case WASM_OP_F32_CONST:
  2400. p += sizeof(float32);
  2401. break;
  2402. case WASM_OP_F64_CONST:
  2403. p += sizeof(float64);
  2404. break;
  2405. case WASM_OP_I32_EQZ:
  2406. case WASM_OP_I32_EQ:
  2407. case WASM_OP_I32_NE:
  2408. case WASM_OP_I32_LT_S:
  2409. case WASM_OP_I32_LT_U:
  2410. case WASM_OP_I32_GT_S:
  2411. case WASM_OP_I32_GT_U:
  2412. case WASM_OP_I32_LE_S:
  2413. case WASM_OP_I32_LE_U:
  2414. case WASM_OP_I32_GE_S:
  2415. case WASM_OP_I32_GE_U:
  2416. case WASM_OP_I64_EQZ:
  2417. case WASM_OP_I64_EQ:
  2418. case WASM_OP_I64_NE:
  2419. case WASM_OP_I64_LT_S:
  2420. case WASM_OP_I64_LT_U:
  2421. case WASM_OP_I64_GT_S:
  2422. case WASM_OP_I64_GT_U:
  2423. case WASM_OP_I64_LE_S:
  2424. case WASM_OP_I64_LE_U:
  2425. case WASM_OP_I64_GE_S:
  2426. case WASM_OP_I64_GE_U:
  2427. case WASM_OP_F32_EQ:
  2428. case WASM_OP_F32_NE:
  2429. case WASM_OP_F32_LT:
  2430. case WASM_OP_F32_GT:
  2431. case WASM_OP_F32_LE:
  2432. case WASM_OP_F32_GE:
  2433. case WASM_OP_F64_EQ:
  2434. case WASM_OP_F64_NE:
  2435. case WASM_OP_F64_LT:
  2436. case WASM_OP_F64_GT:
  2437. case WASM_OP_F64_LE:
  2438. case WASM_OP_F64_GE:
  2439. case WASM_OP_I32_CLZ:
  2440. case WASM_OP_I32_CTZ:
  2441. case WASM_OP_I32_POPCNT:
  2442. case WASM_OP_I32_ADD:
  2443. case WASM_OP_I32_SUB:
  2444. case WASM_OP_I32_MUL:
  2445. case WASM_OP_I32_DIV_S:
  2446. case WASM_OP_I32_DIV_U:
  2447. case WASM_OP_I32_REM_S:
  2448. case WASM_OP_I32_REM_U:
  2449. case WASM_OP_I32_AND:
  2450. case WASM_OP_I32_OR:
  2451. case WASM_OP_I32_XOR:
  2452. case WASM_OP_I32_SHL:
  2453. case WASM_OP_I32_SHR_S:
  2454. case WASM_OP_I32_SHR_U:
  2455. case WASM_OP_I32_ROTL:
  2456. case WASM_OP_I32_ROTR:
  2457. case WASM_OP_I64_CLZ:
  2458. case WASM_OP_I64_CTZ:
  2459. case WASM_OP_I64_POPCNT:
  2460. case WASM_OP_I64_ADD:
  2461. case WASM_OP_I64_SUB:
  2462. case WASM_OP_I64_MUL:
  2463. case WASM_OP_I64_DIV_S:
  2464. case WASM_OP_I64_DIV_U:
  2465. case WASM_OP_I64_REM_S:
  2466. case WASM_OP_I64_REM_U:
  2467. case WASM_OP_I64_AND:
  2468. case WASM_OP_I64_OR:
  2469. case WASM_OP_I64_XOR:
  2470. case WASM_OP_I64_SHL:
  2471. case WASM_OP_I64_SHR_S:
  2472. case WASM_OP_I64_SHR_U:
  2473. case WASM_OP_I64_ROTL:
  2474. case WASM_OP_I64_ROTR:
  2475. case WASM_OP_F32_ABS:
  2476. case WASM_OP_F32_NEG:
  2477. case WASM_OP_F32_CEIL:
  2478. case WASM_OP_F32_FLOOR:
  2479. case WASM_OP_F32_TRUNC:
  2480. case WASM_OP_F32_NEAREST:
  2481. case WASM_OP_F32_SQRT:
  2482. case WASM_OP_F32_ADD:
  2483. case WASM_OP_F32_SUB:
  2484. case WASM_OP_F32_MUL:
  2485. case WASM_OP_F32_DIV:
  2486. case WASM_OP_F32_MIN:
  2487. case WASM_OP_F32_MAX:
  2488. case WASM_OP_F32_COPYSIGN:
  2489. case WASM_OP_F64_ABS:
  2490. case WASM_OP_F64_NEG:
  2491. case WASM_OP_F64_CEIL:
  2492. case WASM_OP_F64_FLOOR:
  2493. case WASM_OP_F64_TRUNC:
  2494. case WASM_OP_F64_NEAREST:
  2495. case WASM_OP_F64_SQRT:
  2496. case WASM_OP_F64_ADD:
  2497. case WASM_OP_F64_SUB:
  2498. case WASM_OP_F64_MUL:
  2499. case WASM_OP_F64_DIV:
  2500. case WASM_OP_F64_MIN:
  2501. case WASM_OP_F64_MAX:
  2502. case WASM_OP_F64_COPYSIGN:
  2503. case WASM_OP_I32_WRAP_I64:
  2504. case WASM_OP_I32_TRUNC_S_F32:
  2505. case WASM_OP_I32_TRUNC_U_F32:
  2506. case WASM_OP_I32_TRUNC_S_F64:
  2507. case WASM_OP_I32_TRUNC_U_F64:
  2508. case WASM_OP_I64_EXTEND_S_I32:
  2509. case WASM_OP_I64_EXTEND_U_I32:
  2510. case WASM_OP_I64_TRUNC_S_F32:
  2511. case WASM_OP_I64_TRUNC_U_F32:
  2512. case WASM_OP_I64_TRUNC_S_F64:
  2513. case WASM_OP_I64_TRUNC_U_F64:
  2514. case WASM_OP_F32_CONVERT_S_I32:
  2515. case WASM_OP_F32_CONVERT_U_I32:
  2516. case WASM_OP_F32_CONVERT_S_I64:
  2517. case WASM_OP_F32_CONVERT_U_I64:
  2518. case WASM_OP_F32_DEMOTE_F64:
  2519. case WASM_OP_F64_CONVERT_S_I32:
  2520. case WASM_OP_F64_CONVERT_U_I32:
  2521. case WASM_OP_F64_CONVERT_S_I64:
  2522. case WASM_OP_F64_CONVERT_U_I64:
  2523. case WASM_OP_F64_PROMOTE_F32:
  2524. case WASM_OP_I32_REINTERPRET_F32:
  2525. case WASM_OP_I64_REINTERPRET_F64:
  2526. case WASM_OP_F32_REINTERPRET_I32:
  2527. case WASM_OP_F64_REINTERPRET_I64:
  2528. case WASM_OP_I32_EXTEND8_S:
  2529. case WASM_OP_I32_EXTEND16_S:
  2530. case WASM_OP_I64_EXTEND8_S:
  2531. case WASM_OP_I64_EXTEND16_S:
  2532. case WASM_OP_I64_EXTEND32_S:
  2533. break;
  2534. case WASM_OP_MISC_PREFIX:
  2535. {
  2536. uint32 opcode1;
  2537. read_leb_uint32(p, p_end, opcode1);
  2538. switch (opcode1) {
  2539. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2540. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2541. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2542. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2543. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2544. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2545. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2546. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2547. break;
  2548. #if WASM_ENABLE_BULK_MEMORY != 0
  2549. case WASM_OP_MEMORY_INIT:
  2550. skip_leb_uint32(p, p_end);
  2551. /* skip memory idx */
  2552. p++;
  2553. break;
  2554. case WASM_OP_DATA_DROP:
  2555. skip_leb_uint32(p, p_end);
  2556. break;
  2557. case WASM_OP_MEMORY_COPY:
  2558. /* skip two memory idx */
  2559. p += 2;
  2560. break;
  2561. case WASM_OP_MEMORY_FILL:
  2562. /* skip memory idx */
  2563. p++;
  2564. break;
  2565. #endif
  2566. #if WASM_ENABLE_REF_TYPES != 0
  2567. case WASM_OP_TABLE_INIT:
  2568. case WASM_OP_TABLE_COPY:
  2569. /* tableidx */
  2570. skip_leb_uint32(p, p_end);
  2571. /* elemidx */
  2572. skip_leb_uint32(p, p_end);
  2573. break;
  2574. case WASM_OP_ELEM_DROP:
  2575. /* elemidx */
  2576. skip_leb_uint32(p, p_end);
  2577. break;
  2578. case WASM_OP_TABLE_SIZE:
  2579. case WASM_OP_TABLE_GROW:
  2580. case WASM_OP_TABLE_FILL:
  2581. skip_leb_uint32(p, p_end); /* table idx */
  2582. break;
  2583. #endif /* WASM_ENABLE_REF_TYPES */
  2584. default:
  2585. bh_assert(0);
  2586. break;
  2587. }
  2588. break;
  2589. }
  2590. #if WASM_ENABLE_SHARED_MEMORY != 0
  2591. case WASM_OP_ATOMIC_PREFIX:
  2592. {
  2593. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  2594. opcode = read_uint8(p);
  2595. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2596. skip_leb_uint32(p, p_end); /* align */
  2597. skip_leb_uint32(p, p_end); /* offset */
  2598. }
  2599. else {
  2600. /* atomic.fence doesn't have memarg */
  2601. p++;
  2602. }
  2603. break;
  2604. }
  2605. #endif
  2606. default:
  2607. bh_assert(0);
  2608. break;
  2609. }
  2610. }
  2611. (void)u8;
  2612. return false;
  2613. }
  2614. #define REF_I32 VALUE_TYPE_I32
  2615. #define REF_F32 VALUE_TYPE_F32
  2616. #define REF_I64_1 VALUE_TYPE_I64
  2617. #define REF_I64_2 VALUE_TYPE_I64
  2618. #define REF_F64_1 VALUE_TYPE_F64
  2619. #define REF_F64_2 VALUE_TYPE_F64
  2620. #define REF_ANY VALUE_TYPE_ANY
  2621. #if WASM_ENABLE_FAST_INTERP != 0
  2622. #if WASM_DEBUG_PREPROCESSOR != 0
  2623. #define LOG_OP(...) os_printf(__VA_ARGS__)
  2624. #else
  2625. #define LOG_OP(...) (void)0
  2626. #endif
  2627. #define PATCH_ELSE 0
  2628. #define PATCH_END 1
  2629. typedef struct BranchBlockPatch {
  2630. struct BranchBlockPatch *next;
  2631. uint8 patch_type;
  2632. uint8 *code_compiled;
  2633. } BranchBlockPatch;
  2634. #endif
  2635. typedef struct BranchBlock {
  2636. uint8 label_type;
  2637. BlockType block_type;
  2638. uint8 *start_addr;
  2639. uint8 *else_addr;
  2640. uint8 *end_addr;
  2641. uint32 stack_cell_num;
  2642. #if WASM_ENABLE_FAST_INTERP != 0
  2643. uint16 dynamic_offset;
  2644. uint8 *code_compiled;
  2645. BranchBlockPatch *patch_list;
  2646. /* This is used to save params frame_offset of of if block */
  2647. int16 *param_frame_offsets;
  2648. #endif
  2649. /* Indicate the operand stack is in polymorphic state.
  2650. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  2651. * to polymorphic state until the block's 'end' opcode is processed.
  2652. * If stack is in polymorphic state and stack is empty, instruction can
  2653. * pop any type of value directly without decreasing stack top pointer
  2654. * and stack cell num. */
  2655. bool is_stack_polymorphic;
  2656. } BranchBlock;
  2657. typedef struct WASMLoaderContext {
  2658. /* frame ref stack */
  2659. uint8 *frame_ref;
  2660. uint8 *frame_ref_bottom;
  2661. uint8 *frame_ref_boundary;
  2662. uint32 frame_ref_size;
  2663. uint32 stack_cell_num;
  2664. uint32 max_stack_cell_num;
  2665. /* frame csp stack */
  2666. BranchBlock *frame_csp;
  2667. BranchBlock *frame_csp_bottom;
  2668. BranchBlock *frame_csp_boundary;
  2669. uint32 frame_csp_size;
  2670. uint32 csp_num;
  2671. uint32 max_csp_num;
  2672. #if WASM_ENABLE_FAST_INTERP != 0
  2673. /* frame offset stack */
  2674. int16 *frame_offset;
  2675. int16 *frame_offset_bottom;
  2676. int16 *frame_offset_boundary;
  2677. uint32 frame_offset_size;
  2678. int16 dynamic_offset;
  2679. int16 start_dynamic_offset;
  2680. int16 max_dynamic_offset;
  2681. /* preserved local offset */
  2682. int16 preserved_local_offset;
  2683. /* const buffer */
  2684. uint8 *const_buf;
  2685. uint16 num_const;
  2686. uint16 const_cell_num;
  2687. uint32 const_buf_size;
  2688. /* processed code */
  2689. uint8 *p_code_compiled;
  2690. uint8 *p_code_compiled_end;
  2691. uint32 code_compiled_size;
  2692. #endif
  2693. } WASMLoaderContext;
  2694. typedef struct Const {
  2695. WASMValue value;
  2696. uint16 slot_index;
  2697. uint8 value_type;
  2698. } Const;
  2699. static void *
  2700. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  2701. uint32 error_buf_size)
  2702. {
  2703. uint8 *mem_new;
  2704. bh_assert(size_new > size_old);
  2705. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  2706. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  2707. memset(mem_new + size_old, 0, size_new - size_old);
  2708. wasm_runtime_free(mem_old);
  2709. }
  2710. return mem_new;
  2711. }
  2712. #define MEM_REALLOC(mem, size_old, size_new) \
  2713. do { \
  2714. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  2715. error_buf_size); \
  2716. if (!mem_new) \
  2717. goto fail; \
  2718. mem = mem_new; \
  2719. } while (0)
  2720. #define CHECK_CSP_PUSH() \
  2721. do { \
  2722. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  2723. MEM_REALLOC( \
  2724. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  2725. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  2726. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  2727. ctx->frame_csp_boundary = \
  2728. ctx->frame_csp_bottom \
  2729. + ctx->frame_csp_size / sizeof(BranchBlock); \
  2730. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  2731. } \
  2732. } while (0)
  2733. #define CHECK_CSP_POP() \
  2734. do { \
  2735. bh_assert(ctx->csp_num >= 1); \
  2736. } while (0)
  2737. #if WASM_ENABLE_FAST_INTERP != 0
  2738. static bool
  2739. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  2740. uint32 error_buf_size)
  2741. {
  2742. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  2743. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  2744. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  2745. ctx->frame_offset_size + 16);
  2746. ctx->frame_offset_size += 16;
  2747. ctx->frame_offset_boundary =
  2748. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  2749. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  2750. }
  2751. return true;
  2752. fail:
  2753. return false;
  2754. }
  2755. static bool
  2756. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  2757. {
  2758. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  2759. return false;
  2760. return true;
  2761. }
  2762. static void
  2763. free_label_patch_list(BranchBlock *frame_csp)
  2764. {
  2765. BranchBlockPatch *label_patch = frame_csp->patch_list;
  2766. BranchBlockPatch *next;
  2767. while (label_patch != NULL) {
  2768. next = label_patch->next;
  2769. wasm_runtime_free(label_patch);
  2770. label_patch = next;
  2771. }
  2772. frame_csp->patch_list = NULL;
  2773. }
  2774. static void
  2775. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  2776. {
  2777. BranchBlock *tmp_csp = frame_csp;
  2778. for (uint32 i = 0; i < csp_num; i++) {
  2779. free_label_patch_list(tmp_csp);
  2780. tmp_csp++;
  2781. }
  2782. }
  2783. #endif
  2784. static bool
  2785. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  2786. {
  2787. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  2788. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  2789. ctx->frame_ref_size + 16);
  2790. ctx->frame_ref_size += 16;
  2791. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  2792. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  2793. }
  2794. return true;
  2795. fail:
  2796. return false;
  2797. }
  2798. static bool
  2799. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  2800. char *error_buf, uint32 error_buf_size)
  2801. {
  2802. bh_assert(!((is_32bit_type(type) && stack_cell_num < 1)
  2803. || (is_64bit_type(type) && stack_cell_num < 2)));
  2804. bh_assert(!(
  2805. (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  2806. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  2807. || (type == VALUE_TYPE_I64
  2808. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  2809. || (type == VALUE_TYPE_F64
  2810. && (*(frame_ref - 2) != REF_F64_1
  2811. || *(frame_ref - 1) != REF_F64_2))));
  2812. return true;
  2813. }
  2814. static bool
  2815. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  2816. uint32 error_buf_size)
  2817. {
  2818. int32 block_stack_cell_num =
  2819. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  2820. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  2821. /* the stack top is a value of any type, return success */
  2822. return true;
  2823. }
  2824. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  2825. error_buf, error_buf_size))
  2826. return false;
  2827. return true;
  2828. }
  2829. static void
  2830. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  2831. {
  2832. if (ctx) {
  2833. if (ctx->frame_ref_bottom)
  2834. wasm_runtime_free(ctx->frame_ref_bottom);
  2835. if (ctx->frame_csp_bottom) {
  2836. #if WASM_ENABLE_FAST_INTERP != 0
  2837. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  2838. #endif
  2839. wasm_runtime_free(ctx->frame_csp_bottom);
  2840. }
  2841. #if WASM_ENABLE_FAST_INTERP != 0
  2842. if (ctx->frame_offset_bottom)
  2843. wasm_runtime_free(ctx->frame_offset_bottom);
  2844. if (ctx->const_buf)
  2845. wasm_runtime_free(ctx->const_buf);
  2846. #endif
  2847. wasm_runtime_free(ctx);
  2848. }
  2849. }
  2850. static WASMLoaderContext *
  2851. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  2852. {
  2853. WASMLoaderContext *loader_ctx =
  2854. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  2855. if (!loader_ctx)
  2856. return NULL;
  2857. loader_ctx->frame_ref_size = 32;
  2858. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  2859. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  2860. goto fail;
  2861. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  2862. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  2863. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  2864. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  2865. goto fail;
  2866. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  2867. #if WASM_ENABLE_FAST_INTERP != 0
  2868. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  2869. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  2870. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  2871. error_buf_size)))
  2872. goto fail;
  2873. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  2874. loader_ctx->num_const = 0;
  2875. loader_ctx->const_buf_size = sizeof(Const) * 8;
  2876. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  2877. error_buf, error_buf_size)))
  2878. goto fail;
  2879. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  2880. set_error_buf(error_buf, error_buf_size,
  2881. "fast interpreter offset overflow");
  2882. goto fail;
  2883. }
  2884. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  2885. loader_ctx->max_dynamic_offset =
  2886. func->param_cell_num + func->local_cell_num;
  2887. #endif
  2888. return loader_ctx;
  2889. fail:
  2890. wasm_loader_ctx_destroy(loader_ctx);
  2891. return NULL;
  2892. }
  2893. static bool
  2894. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  2895. uint32 error_buf_size)
  2896. {
  2897. if (type == VALUE_TYPE_VOID)
  2898. return true;
  2899. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2900. return false;
  2901. *ctx->frame_ref++ = type;
  2902. ctx->stack_cell_num++;
  2903. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2904. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2905. if (is_32bit_type(type))
  2906. return true;
  2907. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2908. return false;
  2909. *ctx->frame_ref++ = type;
  2910. ctx->stack_cell_num++;
  2911. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2912. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2913. return true;
  2914. }
  2915. static bool
  2916. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  2917. uint32 error_buf_size)
  2918. {
  2919. BranchBlock *cur_block = ctx->frame_csp - 1;
  2920. int32 available_stack_cell =
  2921. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  2922. /* Directly return success if current block is in stack
  2923. * polymorphic state while stack is empty. */
  2924. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  2925. return true;
  2926. if (type == VALUE_TYPE_VOID)
  2927. return true;
  2928. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  2929. return false;
  2930. ctx->frame_ref--;
  2931. ctx->stack_cell_num--;
  2932. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  2933. return true;
  2934. ctx->frame_ref--;
  2935. ctx->stack_cell_num--;
  2936. return true;
  2937. }
  2938. static bool
  2939. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  2940. uint8 type_push, uint8 type_pop, char *error_buf,
  2941. uint32 error_buf_size)
  2942. {
  2943. for (int i = 0; i < pop_cnt; i++) {
  2944. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  2945. error_buf_size))
  2946. return false;
  2947. }
  2948. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  2949. return false;
  2950. return true;
  2951. }
  2952. static bool
  2953. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  2954. BlockType block_type, uint8 *start_addr,
  2955. char *error_buf, uint32 error_buf_size)
  2956. {
  2957. CHECK_CSP_PUSH();
  2958. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  2959. ctx->frame_csp->label_type = label_type;
  2960. ctx->frame_csp->block_type = block_type;
  2961. ctx->frame_csp->start_addr = start_addr;
  2962. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  2963. #if WASM_ENABLE_FAST_INTERP != 0
  2964. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  2965. ctx->frame_csp->patch_list = NULL;
  2966. #endif
  2967. ctx->frame_csp++;
  2968. ctx->csp_num++;
  2969. if (ctx->csp_num > ctx->max_csp_num)
  2970. ctx->max_csp_num = ctx->csp_num;
  2971. return true;
  2972. fail:
  2973. return false;
  2974. }
  2975. static bool
  2976. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  2977. uint32 error_buf_size)
  2978. {
  2979. CHECK_CSP_POP();
  2980. #if WASM_ENABLE_FAST_INTERP != 0
  2981. if ((ctx->frame_csp - 1)->param_frame_offsets)
  2982. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  2983. #endif
  2984. ctx->frame_csp--;
  2985. ctx->csp_num--;
  2986. return true;
  2987. }
  2988. #if WASM_ENABLE_FAST_INTERP != 0
  2989. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  2990. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  2991. #define emit_label(opcode) \
  2992. do { \
  2993. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  2994. LOG_OP("\nemit_op [%02x]\t", opcode); \
  2995. } while (0)
  2996. #define skip_label() \
  2997. do { \
  2998. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  2999. LOG_OP("\ndelete last op\n"); \
  3000. } while (0)
  3001. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3002. #define emit_label(opcode) \
  3003. do { \
  3004. int32 offset = \
  3005. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  3006. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  3007. set_error_buf(error_buf, error_buf_size, \
  3008. "pre-compiled label offset out of range"); \
  3009. goto fail; \
  3010. } \
  3011. wasm_loader_emit_int16(loader_ctx, offset); \
  3012. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3013. } while (0)
  3014. #define skip_label() \
  3015. do { \
  3016. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  3017. LOG_OP("\ndelete last op\n"); \
  3018. } while (0)
  3019. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3020. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  3021. #define emit_label(opcode) \
  3022. do { \
  3023. wasm_loader_emit_uint8(loader_ctx, opcode); \
  3024. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3025. } while (0)
  3026. #define skip_label() \
  3027. do { \
  3028. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  3029. LOG_OP("\ndelete last op\n"); \
  3030. } while (0)
  3031. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  3032. #define emit_empty_label_addr_and_frame_ip(type) \
  3033. do { \
  3034. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3035. loader_ctx->p_code_compiled, error_buf, \
  3036. error_buf_size)) \
  3037. goto fail; \
  3038. /* label address, to be patched */ \
  3039. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3040. } while (0)
  3041. #define emit_br_info(frame_csp) \
  3042. do { \
  3043. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  3044. error_buf_size)) \
  3045. goto fail; \
  3046. } while (0)
  3047. #define LAST_OP_OUTPUT_I32() \
  3048. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  3049. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  3050. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  3051. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  3052. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3053. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3054. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3055. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3056. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3057. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3058. || (last_op == EXT_OP_COPY_STACK_TOP)
  3059. #define LAST_OP_OUTPUT_I64() \
  3060. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  3061. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  3062. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  3063. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  3064. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3065. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3066. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3067. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3068. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3069. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3070. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3071. #define GET_CONST_OFFSET(type, val) \
  3072. do { \
  3073. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  3074. &operand_offset, error_buf, \
  3075. error_buf_size))) \
  3076. goto fail; \
  3077. } while (0)
  3078. #define GET_CONST_F32_OFFSET(type, fval) \
  3079. do { \
  3080. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3081. &operand_offset, error_buf, \
  3082. error_buf_size))) \
  3083. goto fail; \
  3084. } while (0)
  3085. #define GET_CONST_F64_OFFSET(type, fval) \
  3086. do { \
  3087. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3088. &operand_offset, error_buf, \
  3089. error_buf_size))) \
  3090. goto fail; \
  3091. } while (0)
  3092. #define emit_operand(ctx, offset) \
  3093. do { \
  3094. wasm_loader_emit_int16(ctx, offset); \
  3095. LOG_OP("%d\t", offset); \
  3096. } while (0)
  3097. #define emit_byte(ctx, byte) \
  3098. do { \
  3099. wasm_loader_emit_uint8(ctx, byte); \
  3100. LOG_OP("%d\t", byte); \
  3101. } while (0)
  3102. #define emit_uint32(ctx, value) \
  3103. do { \
  3104. wasm_loader_emit_uint32(ctx, value); \
  3105. LOG_OP("%d\t", value); \
  3106. } while (0)
  3107. #define emit_uint64(ctx, value) \
  3108. do { \
  3109. wasm_loader_emit_const(ctx, &value, false); \
  3110. LOG_OP("%lld\t", value); \
  3111. } while (0)
  3112. #define emit_float32(ctx, value) \
  3113. do { \
  3114. wasm_loader_emit_const(ctx, &value, true); \
  3115. LOG_OP("%f\t", value); \
  3116. } while (0)
  3117. #define emit_float64(ctx, value) \
  3118. do { \
  3119. wasm_loader_emit_const(ctx, &value, false); \
  3120. LOG_OP("%f\t", value); \
  3121. } while (0)
  3122. static bool
  3123. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3124. {
  3125. if (!(ctx->p_code_compiled =
  3126. loader_malloc(ctx->code_compiled_size, NULL, 0)))
  3127. return false;
  3128. ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size;
  3129. /* clean up frame ref */
  3130. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3131. ctx->frame_ref = ctx->frame_ref_bottom;
  3132. ctx->stack_cell_num = 0;
  3133. /* clean up frame csp */
  3134. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3135. ctx->frame_csp = ctx->frame_csp_bottom;
  3136. ctx->csp_num = 0;
  3137. ctx->max_csp_num = 0;
  3138. /* clean up frame offset */
  3139. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3140. ctx->frame_offset = ctx->frame_offset_bottom;
  3141. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3142. /* init preserved local offsets */
  3143. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3144. /* const buf is reserved */
  3145. return true;
  3146. }
  3147. static void
  3148. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  3149. {
  3150. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  3151. if (ctx->p_code_compiled) {
  3152. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3153. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3154. #endif
  3155. bh_memcpy_s(ctx->p_code_compiled,
  3156. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  3157. value, size);
  3158. ctx->p_code_compiled += size;
  3159. }
  3160. else {
  3161. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3162. bh_assert((ctx->code_compiled_size & 1) == 0);
  3163. #endif
  3164. ctx->code_compiled_size += size;
  3165. }
  3166. }
  3167. static void
  3168. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  3169. {
  3170. if (ctx->p_code_compiled) {
  3171. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3172. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3173. #endif
  3174. STORE_U32(ctx->p_code_compiled, value);
  3175. ctx->p_code_compiled += sizeof(uint32);
  3176. }
  3177. else {
  3178. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3179. bh_assert((ctx->code_compiled_size & 1) == 0);
  3180. #endif
  3181. ctx->code_compiled_size += sizeof(uint32);
  3182. }
  3183. }
  3184. static void
  3185. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3186. {
  3187. if (ctx->p_code_compiled) {
  3188. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3189. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3190. #endif
  3191. STORE_U16(ctx->p_code_compiled, (uint16)value);
  3192. ctx->p_code_compiled += sizeof(int16);
  3193. }
  3194. else {
  3195. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3196. bh_assert((ctx->code_compiled_size & 1) == 0);
  3197. #endif
  3198. ctx->code_compiled_size += sizeof(int16);
  3199. }
  3200. }
  3201. static void
  3202. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3203. {
  3204. if (ctx->p_code_compiled) {
  3205. *(ctx->p_code_compiled) = value;
  3206. ctx->p_code_compiled += sizeof(uint8);
  3207. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3208. ctx->p_code_compiled++;
  3209. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3210. #endif
  3211. }
  3212. else {
  3213. ctx->code_compiled_size += sizeof(uint8);
  3214. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3215. ctx->code_compiled_size++;
  3216. bh_assert((ctx->code_compiled_size & 1) == 0);
  3217. #endif
  3218. }
  3219. }
  3220. static void
  3221. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  3222. {
  3223. if (ctx->p_code_compiled) {
  3224. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3225. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3226. #endif
  3227. STORE_PTR(ctx->p_code_compiled, value);
  3228. ctx->p_code_compiled += sizeof(void *);
  3229. }
  3230. else {
  3231. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3232. bh_assert((ctx->code_compiled_size & 1) == 0);
  3233. #endif
  3234. ctx->code_compiled_size += sizeof(void *);
  3235. }
  3236. }
  3237. static void
  3238. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  3239. {
  3240. if (ctx->p_code_compiled) {
  3241. ctx->p_code_compiled -= size;
  3242. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3243. if (size == sizeof(uint8)) {
  3244. ctx->p_code_compiled--;
  3245. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3246. }
  3247. #endif
  3248. }
  3249. else {
  3250. ctx->code_compiled_size -= size;
  3251. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3252. if (size == sizeof(uint8)) {
  3253. ctx->code_compiled_size--;
  3254. bh_assert((ctx->code_compiled_size & 1) == 0);
  3255. }
  3256. #endif
  3257. }
  3258. }
  3259. static bool
  3260. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  3261. uint32 local_index, uint32 local_type,
  3262. bool *preserved, char *error_buf,
  3263. uint32 error_buf_size)
  3264. {
  3265. uint32 i = 0;
  3266. int16 preserved_offset = (int16)local_index;
  3267. *preserved = false;
  3268. while (i < loader_ctx->stack_cell_num) {
  3269. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3270. /* move previous local into dynamic space before a set/tee_local opcode
  3271. */
  3272. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  3273. if (!(*preserved)) {
  3274. *preserved = true;
  3275. skip_label();
  3276. preserved_offset = loader_ctx->preserved_local_offset;
  3277. if (loader_ctx->p_code_compiled) {
  3278. bh_assert(preserved_offset != (int16)local_index);
  3279. }
  3280. if (is_32bit_type(local_type)) {
  3281. /* Only increase preserve offset in the second traversal */
  3282. if (loader_ctx->p_code_compiled)
  3283. loader_ctx->preserved_local_offset++;
  3284. emit_label(EXT_OP_COPY_STACK_TOP);
  3285. }
  3286. else {
  3287. if (loader_ctx->p_code_compiled)
  3288. loader_ctx->preserved_local_offset += 2;
  3289. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3290. }
  3291. emit_operand(loader_ctx, local_index);
  3292. emit_operand(loader_ctx, preserved_offset);
  3293. emit_label(opcode);
  3294. }
  3295. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  3296. }
  3297. if (is_32bit_type(cur_type))
  3298. i++;
  3299. else
  3300. i += 2;
  3301. }
  3302. return true;
  3303. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3304. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3305. fail:
  3306. return false;
  3307. #endif
  3308. #endif
  3309. }
  3310. static bool
  3311. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  3312. char *error_buf, uint32 error_buf_size)
  3313. {
  3314. uint32 i = 0;
  3315. bool preserve_local;
  3316. /* preserve locals before blocks to ensure that "tee/set_local" inside
  3317. blocks will not influence the value of these locals */
  3318. while (i < loader_ctx->stack_cell_num) {
  3319. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  3320. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3321. if ((cur_offset < loader_ctx->start_dynamic_offset)
  3322. && (cur_offset >= 0)) {
  3323. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  3324. cur_type, &preserve_local,
  3325. error_buf, error_buf_size)))
  3326. return false;
  3327. }
  3328. if (is_32bit_type(cur_type == VALUE_TYPE_I32)) {
  3329. i++;
  3330. }
  3331. else {
  3332. i += 2;
  3333. }
  3334. }
  3335. return true;
  3336. }
  3337. static bool
  3338. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  3339. uint8 *p_code_compiled, char *error_buf,
  3340. uint32 error_buf_size)
  3341. {
  3342. BranchBlockPatch *patch =
  3343. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  3344. if (!patch) {
  3345. return false;
  3346. }
  3347. patch->patch_type = patch_type;
  3348. patch->code_compiled = p_code_compiled;
  3349. if (!frame_csp->patch_list) {
  3350. frame_csp->patch_list = patch;
  3351. patch->next = NULL;
  3352. }
  3353. else {
  3354. patch->next = frame_csp->patch_list;
  3355. frame_csp->patch_list = patch;
  3356. }
  3357. return true;
  3358. }
  3359. static void
  3360. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  3361. {
  3362. BranchBlock *frame_csp = ctx->frame_csp - depth;
  3363. BranchBlockPatch *node = frame_csp->patch_list;
  3364. BranchBlockPatch *node_prev = NULL, *node_next;
  3365. if (!ctx->p_code_compiled)
  3366. return;
  3367. while (node) {
  3368. node_next = node->next;
  3369. if (node->patch_type == patch_type) {
  3370. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  3371. if (node_prev == NULL) {
  3372. frame_csp->patch_list = node_next;
  3373. }
  3374. else {
  3375. node_prev->next = node_next;
  3376. }
  3377. wasm_runtime_free(node);
  3378. }
  3379. else {
  3380. node_prev = node;
  3381. }
  3382. node = node_next;
  3383. }
  3384. }
  3385. static bool
  3386. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  3387. char *error_buf, uint32 error_buf_size)
  3388. {
  3389. /* br info layout:
  3390. * a) arity of target block
  3391. * b) total cell num of arity values
  3392. * c) each arity value's cell num
  3393. * d) each arity value's src frame offset
  3394. * e) each arity values's dst dynamic offset
  3395. * f) branch target address
  3396. *
  3397. * Note: b-e are omitted when arity is 0 so that
  3398. * interpreter can recover the br info quickly.
  3399. */
  3400. BlockType *block_type = &frame_csp->block_type;
  3401. uint8 *types = NULL, cell;
  3402. uint32 arity = 0;
  3403. int32 i;
  3404. int16 *frame_offset = ctx->frame_offset;
  3405. uint16 dynamic_offset;
  3406. /* Note: loop's arity is different from if and block. loop's arity is
  3407. * its parameter count while if and block arity is result count.
  3408. */
  3409. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  3410. arity = block_type_get_param_types(block_type, &types);
  3411. else
  3412. arity = block_type_get_result_types(block_type, &types);
  3413. /* Part a */
  3414. emit_uint32(ctx, arity);
  3415. if (arity) {
  3416. /* Part b */
  3417. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  3418. /* Part c */
  3419. for (i = (int32)arity - 1; i >= 0; i--) {
  3420. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3421. emit_byte(ctx, cell);
  3422. }
  3423. /* Part d */
  3424. for (i = (int32)arity - 1; i >= 0; i--) {
  3425. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3426. frame_offset -= cell;
  3427. emit_operand(ctx, *(int16 *)(frame_offset));
  3428. }
  3429. /* Part e */
  3430. dynamic_offset =
  3431. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  3432. for (i = (int32)arity - 1; i >= 0; i--) {
  3433. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3434. dynamic_offset -= cell;
  3435. emit_operand(ctx, dynamic_offset);
  3436. }
  3437. }
  3438. /* Part f */
  3439. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  3440. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  3441. }
  3442. else {
  3443. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  3444. error_buf, error_buf_size))
  3445. return false;
  3446. /* label address, to be patched */
  3447. wasm_loader_emit_ptr(ctx, NULL);
  3448. }
  3449. return true;
  3450. }
  3451. static bool
  3452. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3453. bool disable_emit, int16 operand_offset,
  3454. char *error_buf, uint32 error_buf_size)
  3455. {
  3456. if (type == VALUE_TYPE_VOID)
  3457. return true;
  3458. /* only check memory overflow in first traverse */
  3459. if (ctx->p_code_compiled == NULL) {
  3460. if (!check_offset_push(ctx, error_buf, error_buf_size))
  3461. return false;
  3462. }
  3463. if (disable_emit)
  3464. *(ctx->frame_offset)++ = operand_offset;
  3465. else {
  3466. emit_operand(ctx, ctx->dynamic_offset);
  3467. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  3468. ctx->dynamic_offset++;
  3469. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  3470. ctx->max_dynamic_offset = ctx->dynamic_offset;
  3471. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  3472. }
  3473. }
  3474. if (is_32bit_type(type))
  3475. return true;
  3476. if (ctx->p_code_compiled == NULL) {
  3477. if (!check_offset_push(ctx, error_buf, error_buf_size))
  3478. return false;
  3479. }
  3480. ctx->frame_offset++;
  3481. if (!disable_emit) {
  3482. ctx->dynamic_offset++;
  3483. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  3484. ctx->max_dynamic_offset = ctx->dynamic_offset;
  3485. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  3486. }
  3487. }
  3488. return true;
  3489. }
  3490. /* This function should be in front of wasm_loader_pop_frame_ref
  3491. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  3492. will be modified by wasm_loader_pop_frame_ref */
  3493. static bool
  3494. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3495. char *error_buf, uint32 error_buf_size)
  3496. {
  3497. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  3498. then current block is the function block */
  3499. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  3500. BranchBlock *cur_block = ctx->frame_csp - depth;
  3501. int32 available_stack_cell =
  3502. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  3503. /* Directly return success if current block is in stack
  3504. * polymorphic state while stack is empty. */
  3505. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3506. return true;
  3507. if (type == VALUE_TYPE_VOID)
  3508. return true;
  3509. if (is_32bit_type(type)) {
  3510. /* Check the offset stack bottom to ensure the frame offset
  3511. stack will not go underflow. But we don't thrown error
  3512. and return true here, because the error msg should be
  3513. given in wasm_loader_pop_frame_ref */
  3514. if (!check_offset_pop(ctx, 1))
  3515. return true;
  3516. ctx->frame_offset -= 1;
  3517. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  3518. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  3519. ctx->dynamic_offset -= 1;
  3520. }
  3521. else {
  3522. if (!check_offset_pop(ctx, 2))
  3523. return true;
  3524. ctx->frame_offset -= 2;
  3525. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  3526. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  3527. ctx->dynamic_offset -= 2;
  3528. }
  3529. emit_operand(ctx, *(ctx->frame_offset));
  3530. return true;
  3531. }
  3532. static bool
  3533. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  3534. uint8 type_push, uint8 type_pop,
  3535. bool disable_emit, int16 operand_offset,
  3536. char *error_buf, uint32 error_buf_size)
  3537. {
  3538. for (int i = 0; i < pop_cnt; i++) {
  3539. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  3540. error_buf_size))
  3541. return false;
  3542. }
  3543. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  3544. operand_offset, error_buf,
  3545. error_buf_size))
  3546. return false;
  3547. return true;
  3548. }
  3549. static bool
  3550. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  3551. bool disable_emit, int16 operand_offset,
  3552. char *error_buf, uint32 error_buf_size)
  3553. {
  3554. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  3555. error_buf, error_buf_size)))
  3556. return false;
  3557. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  3558. return false;
  3559. return true;
  3560. }
  3561. static bool
  3562. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  3563. char *error_buf, uint32 error_buf_size)
  3564. {
  3565. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  3566. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  3567. return false;
  3568. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  3569. return false;
  3570. return true;
  3571. }
  3572. static bool
  3573. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  3574. uint8 type_push, uint8 type_pop,
  3575. bool disable_emit, int16 operand_offset,
  3576. char *error_buf, uint32 error_buf_size)
  3577. {
  3578. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  3579. disable_emit, operand_offset,
  3580. error_buf, error_buf_size))
  3581. return false;
  3582. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  3583. error_buf, error_buf_size))
  3584. return false;
  3585. return true;
  3586. }
  3587. static bool
  3588. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  3589. int16 *offset, char *error_buf,
  3590. uint32 error_buf_size)
  3591. {
  3592. int8 bytes_to_increase;
  3593. int16 operand_offset = 0;
  3594. Const *c;
  3595. /* Search existing constant */
  3596. for (c = (Const *)ctx->const_buf;
  3597. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  3598. if ((type == c->value_type)
  3599. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  3600. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  3601. #if WASM_ENABLE_REF_TYPES != 0
  3602. || (type == VALUE_TYPE_FUNCREF
  3603. && *(int32 *)value == c->value.i32)
  3604. || (type == VALUE_TYPE_EXTERNREF
  3605. && *(int32 *)value == c->value.i32)
  3606. #endif
  3607. || (type == VALUE_TYPE_F64
  3608. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  3609. || (type == VALUE_TYPE_F32
  3610. && (0
  3611. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  3612. operand_offset = c->slot_index;
  3613. break;
  3614. }
  3615. if (c->value_type == VALUE_TYPE_I64 || c->value_type == VALUE_TYPE_F64)
  3616. operand_offset += 2;
  3617. else
  3618. operand_offset += 1;
  3619. }
  3620. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  3621. /* New constant, append to the const buffer */
  3622. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  3623. bytes_to_increase = 2;
  3624. }
  3625. else {
  3626. bytes_to_increase = 1;
  3627. }
  3628. /* The max cell num of const buffer is 32768 since the valid index range
  3629. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  3630. * full */
  3631. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  3632. *offset = 0;
  3633. return true;
  3634. }
  3635. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  3636. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  3637. ctx->const_buf_size + 4 * sizeof(Const));
  3638. ctx->const_buf_size += 4 * sizeof(Const);
  3639. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  3640. }
  3641. c->value_type = type;
  3642. switch (type) {
  3643. case VALUE_TYPE_F64:
  3644. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  3645. sizeof(float64));
  3646. ctx->const_cell_num += 2;
  3647. /* The const buf will be reversed, we use the second cell */
  3648. /* of the i64/f64 const so the finnal offset is corrent */
  3649. operand_offset++;
  3650. break;
  3651. case VALUE_TYPE_I64:
  3652. c->value.i64 = *(int64 *)value;
  3653. ctx->const_cell_num += 2;
  3654. operand_offset++;
  3655. break;
  3656. case VALUE_TYPE_F32:
  3657. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  3658. sizeof(float32));
  3659. ctx->const_cell_num++;
  3660. break;
  3661. case VALUE_TYPE_I32:
  3662. c->value.i32 = *(int32 *)value;
  3663. ctx->const_cell_num++;
  3664. break;
  3665. #if WASM_ENABLE_REF_TYPES != 0
  3666. case VALUE_TYPE_EXTERNREF:
  3667. case VALUE_TYPE_FUNCREF:
  3668. c->value.i32 = *(int32 *)value;
  3669. ctx->const_cell_num++;
  3670. break;
  3671. #endif
  3672. default:
  3673. break;
  3674. }
  3675. c->slot_index = operand_offset;
  3676. ctx->num_const++;
  3677. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  3678. (int64)c->value.i64);
  3679. }
  3680. /* use negetive index for const */
  3681. operand_offset = -(operand_offset + 1);
  3682. *offset = operand_offset;
  3683. return true;
  3684. fail:
  3685. return false;
  3686. }
  3687. /*
  3688. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  3689. -- Mostly used for the binary / compare operation
  3690. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  3691. -- Mostly used in block / control instructions
  3692. The POP will always emit the offset on the top of the frame_offset stack
  3693. PUSH can be used in two ways:
  3694. 1. directly PUSH:
  3695. PUSH_XXX();
  3696. will allocate a dynamic space and emit
  3697. 2. silent PUSH:
  3698. operand_offset = xxx; disable_emit = true;
  3699. PUSH_XXX();
  3700. only push the frame_offset stack, no emit
  3701. */
  3702. #define PUSH_I32() \
  3703. do { \
  3704. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  3705. disable_emit, operand_offset, \
  3706. error_buf, error_buf_size)) \
  3707. goto fail; \
  3708. } while (0)
  3709. #define PUSH_F32() \
  3710. do { \
  3711. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  3712. disable_emit, operand_offset, \
  3713. error_buf, error_buf_size)) \
  3714. goto fail; \
  3715. } while (0)
  3716. #define PUSH_I64() \
  3717. do { \
  3718. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  3719. disable_emit, operand_offset, \
  3720. error_buf, error_buf_size)) \
  3721. goto fail; \
  3722. } while (0)
  3723. #define PUSH_F64() \
  3724. do { \
  3725. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  3726. disable_emit, operand_offset, \
  3727. error_buf, error_buf_size)) \
  3728. goto fail; \
  3729. } while (0)
  3730. #define PUSH_FUNCREF() \
  3731. do { \
  3732. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \
  3733. disable_emit, operand_offset, \
  3734. error_buf, error_buf_size)) \
  3735. goto fail; \
  3736. } while (0)
  3737. #define POP_I32() \
  3738. do { \
  3739. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  3740. error_buf, error_buf_size)) \
  3741. goto fail; \
  3742. } while (0)
  3743. #define POP_F32() \
  3744. do { \
  3745. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  3746. error_buf, error_buf_size)) \
  3747. goto fail; \
  3748. } while (0)
  3749. #define POP_I64() \
  3750. do { \
  3751. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  3752. error_buf, error_buf_size)) \
  3753. goto fail; \
  3754. } while (0)
  3755. #define POP_F64() \
  3756. do { \
  3757. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  3758. error_buf, error_buf_size)) \
  3759. goto fail; \
  3760. } while (0)
  3761. #define PUSH_OFFSET_TYPE(type) \
  3762. do { \
  3763. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  3764. operand_offset, error_buf, \
  3765. error_buf_size))) \
  3766. goto fail; \
  3767. } while (0)
  3768. #define POP_OFFSET_TYPE(type) \
  3769. do { \
  3770. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  3771. error_buf_size))) \
  3772. goto fail; \
  3773. } while (0)
  3774. #define POP_AND_PUSH(type_pop, type_push) \
  3775. do { \
  3776. if (!(wasm_loader_push_pop_frame_ref_offset( \
  3777. loader_ctx, 1, type_push, type_pop, disable_emit, \
  3778. operand_offset, error_buf, error_buf_size))) \
  3779. goto fail; \
  3780. } while (0)
  3781. /* type of POPs should be the same */
  3782. #define POP2_AND_PUSH(type_pop, type_push) \
  3783. do { \
  3784. if (!(wasm_loader_push_pop_frame_ref_offset( \
  3785. loader_ctx, 2, type_push, type_pop, disable_emit, \
  3786. operand_offset, error_buf, error_buf_size))) \
  3787. goto fail; \
  3788. } while (0)
  3789. #else /* WASM_ENABLE_FAST_INTERP */
  3790. #define PUSH_I32() \
  3791. do { \
  3792. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  3793. error_buf, error_buf_size))) \
  3794. goto fail; \
  3795. } while (0)
  3796. #define PUSH_F32() \
  3797. do { \
  3798. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  3799. error_buf, error_buf_size))) \
  3800. goto fail; \
  3801. } while (0)
  3802. #define PUSH_I64() \
  3803. do { \
  3804. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  3805. error_buf, error_buf_size))) \
  3806. goto fail; \
  3807. } while (0)
  3808. #define PUSH_F64() \
  3809. do { \
  3810. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  3811. error_buf, error_buf_size))) \
  3812. goto fail; \
  3813. } while (0)
  3814. #define PUSH_FUNCREF() \
  3815. do { \
  3816. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  3817. error_buf, error_buf_size))) \
  3818. goto fail; \
  3819. } while (0)
  3820. #define POP_I32() \
  3821. do { \
  3822. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \
  3823. error_buf_size))) \
  3824. goto fail; \
  3825. } while (0)
  3826. #define POP_F32() \
  3827. do { \
  3828. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \
  3829. error_buf_size))) \
  3830. goto fail; \
  3831. } while (0)
  3832. #define POP_I64() \
  3833. do { \
  3834. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \
  3835. error_buf_size))) \
  3836. goto fail; \
  3837. } while (0)
  3838. #define POP_F64() \
  3839. do { \
  3840. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \
  3841. error_buf_size))) \
  3842. goto fail; \
  3843. } while (0)
  3844. #define POP_FUNCREF() \
  3845. do { \
  3846. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  3847. error_buf, error_buf_size))) \
  3848. goto fail; \
  3849. } while (0)
  3850. #define POP_AND_PUSH(type_pop, type_push) \
  3851. do { \
  3852. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  3853. type_pop, error_buf, \
  3854. error_buf_size))) \
  3855. goto fail; \
  3856. } while (0)
  3857. /* type of POPs should be the same */
  3858. #define POP2_AND_PUSH(type_pop, type_push) \
  3859. do { \
  3860. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  3861. type_pop, error_buf, \
  3862. error_buf_size))) \
  3863. goto fail; \
  3864. } while (0)
  3865. #endif /* WASM_ENABLE_FAST_INTERP */
  3866. #if WASM_ENABLE_FAST_INTERP != 0
  3867. static bool
  3868. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  3869. bool disable_emit, char *error_buf, uint32 error_buf_size)
  3870. {
  3871. int16 operand_offset = 0;
  3872. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  3873. : loader_ctx->frame_csp;
  3874. BlockType *block_type = &block->block_type;
  3875. uint8 *return_types = NULL;
  3876. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  3877. int32 i = 0;
  3878. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  3879. *frame_offset_org = NULL;
  3880. return_count = block_type_get_result_types(block_type, &return_types);
  3881. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  3882. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  3883. if (return_count == 1) {
  3884. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  3885. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  3886. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  3887. /* insert op_copy before else opcode */
  3888. if (opcode == WASM_OP_ELSE)
  3889. skip_label();
  3890. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  3891. : EXT_OP_COPY_STACK_TOP_I64);
  3892. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  3893. emit_operand(loader_ctx, block->dynamic_offset);
  3894. if (opcode == WASM_OP_ELSE) {
  3895. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  3896. }
  3897. else {
  3898. loader_ctx->frame_offset -= cell;
  3899. loader_ctx->dynamic_offset = block->dynamic_offset;
  3900. PUSH_OFFSET_TYPE(return_types[0]);
  3901. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3902. }
  3903. if (opcode == WASM_OP_ELSE)
  3904. emit_label(opcode);
  3905. }
  3906. return true;
  3907. }
  3908. /* Copy stack top values to block's results which are in dynamic space.
  3909. * The instruction format:
  3910. * Part a: values count
  3911. * Part b: all values total cell num
  3912. * Part c: each value's cell_num, src offset and dst offset
  3913. * Part d: each value's src offset and dst offset
  3914. * Part e: each value's dst offset
  3915. */
  3916. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  3917. dynamic_offset = dynamic_offset_org =
  3918. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  3919. /* First traversal to get the count of values needed to be copied. */
  3920. for (i = (int32)return_count - 1; i >= 0; i--) {
  3921. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  3922. frame_offset -= cells;
  3923. dynamic_offset -= cells;
  3924. if (dynamic_offset != *frame_offset) {
  3925. value_count++;
  3926. total_cel_num += cells;
  3927. }
  3928. }
  3929. if (value_count) {
  3930. uint32 j = 0;
  3931. uint8 *emit_data = NULL, *cells = NULL;
  3932. int16 *src_offsets = NULL;
  3933. uint16 *dst_offsets = NULL;
  3934. uint64 size =
  3935. (uint64)value_count
  3936. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  3937. /* Allocate memory for the emit data */
  3938. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  3939. return false;
  3940. cells = emit_data;
  3941. src_offsets = (int16 *)(cells + value_count);
  3942. dst_offsets = (uint16 *)(src_offsets + value_count);
  3943. /* insert op_copy before else opcode */
  3944. if (opcode == WASM_OP_ELSE)
  3945. skip_label();
  3946. emit_label(EXT_OP_COPY_STACK_VALUES);
  3947. /* Part a) */
  3948. emit_uint32(loader_ctx, value_count);
  3949. /* Part b) */
  3950. emit_uint32(loader_ctx, total_cel_num);
  3951. /* Second traversal to get each value's cell num, src offset and dst
  3952. * offset. */
  3953. frame_offset = frame_offset_org;
  3954. dynamic_offset = dynamic_offset_org;
  3955. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  3956. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  3957. frame_offset -= cell;
  3958. dynamic_offset -= cell;
  3959. if (dynamic_offset != *frame_offset) {
  3960. /* cell num */
  3961. cells[j] = cell;
  3962. /* src offset */
  3963. src_offsets[j] = *frame_offset;
  3964. /* dst offset */
  3965. dst_offsets[j] = dynamic_offset;
  3966. j++;
  3967. }
  3968. if (opcode == WASM_OP_ELSE) {
  3969. *frame_offset = dynamic_offset;
  3970. }
  3971. else {
  3972. loader_ctx->frame_offset = frame_offset;
  3973. loader_ctx->dynamic_offset = dynamic_offset;
  3974. PUSH_OFFSET_TYPE(return_types[i]);
  3975. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3976. loader_ctx->frame_offset = frame_offset_org;
  3977. loader_ctx->dynamic_offset = dynamic_offset_org;
  3978. }
  3979. }
  3980. bh_assert(j == value_count);
  3981. /* Emit the cells, src_offsets and dst_offsets */
  3982. for (j = 0; j < value_count; j++)
  3983. emit_byte(loader_ctx, cells[j]);
  3984. for (j = 0; j < value_count; j++)
  3985. emit_operand(loader_ctx, src_offsets[j]);
  3986. for (j = 0; j < value_count; j++)
  3987. emit_operand(loader_ctx, dst_offsets[j]);
  3988. if (opcode == WASM_OP_ELSE)
  3989. emit_label(opcode);
  3990. wasm_runtime_free(emit_data);
  3991. }
  3992. return true;
  3993. fail:
  3994. return false;
  3995. }
  3996. #endif /* WASM_ENABLE_FAST_INTERP */
  3997. #define RESERVE_BLOCK_RET() \
  3998. do { \
  3999. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  4000. error_buf_size)) \
  4001. goto fail; \
  4002. } while (0)
  4003. #define PUSH_TYPE(type) \
  4004. do { \
  4005. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  4006. error_buf_size))) \
  4007. goto fail; \
  4008. } while (0)
  4009. #define POP_TYPE(type) \
  4010. do { \
  4011. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  4012. error_buf_size))) \
  4013. goto fail; \
  4014. } while (0)
  4015. #define PUSH_CSP(label_type, block_type, _start_addr) \
  4016. do { \
  4017. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4018. _start_addr, error_buf, \
  4019. error_buf_size)) \
  4020. goto fail; \
  4021. } while (0)
  4022. #define POP_CSP() \
  4023. do { \
  4024. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  4025. goto fail; \
  4026. } while (0)
  4027. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  4028. do { \
  4029. read_leb_uint32(p, p_end, local_idx); \
  4030. bh_assert(local_idx < param_count + local_count); \
  4031. local_type = local_idx < param_count \
  4032. ? param_types[local_idx] \
  4033. : local_types[local_idx - param_count]; \
  4034. local_offset = local_offsets[local_idx]; \
  4035. } while (0)
  4036. #define CHECK_BR(depth) \
  4037. do { \
  4038. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  4039. error_buf_size)) \
  4040. goto fail; \
  4041. } while (0)
  4042. #define CHECK_MEMORY() \
  4043. do { \
  4044. bh_assert(module->import_memory_count + module->memory_count > 0); \
  4045. } while (0)
  4046. static bool
  4047. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  4048. char *error_buf, uint32 error_buf_size)
  4049. {
  4050. BranchBlock *target_block, *cur_block;
  4051. BlockType *target_block_type;
  4052. uint8 *types = NULL, *frame_ref;
  4053. uint32 arity = 0;
  4054. int32 i, available_stack_cell;
  4055. uint16 cell_num;
  4056. if (loader_ctx->csp_num < depth + 1) {
  4057. set_error_buf(error_buf, error_buf_size,
  4058. "unknown label, "
  4059. "unexpected end of section or function");
  4060. return false;
  4061. }
  4062. cur_block = loader_ctx->frame_csp - 1;
  4063. target_block = loader_ctx->frame_csp - (depth + 1);
  4064. target_block_type = &target_block->block_type;
  4065. frame_ref = loader_ctx->frame_ref;
  4066. /* Note: loop's arity is different from if and block. loop's arity is
  4067. * its parameter count while if and block arity is result count.
  4068. */
  4069. if (target_block->label_type == LABEL_TYPE_LOOP)
  4070. arity = block_type_get_param_types(target_block_type, &types);
  4071. else
  4072. arity = block_type_get_result_types(target_block_type, &types);
  4073. /* If the stack is in polymorphic state, just clear the stack
  4074. * and then re-push the values to make the stack top values
  4075. * match block type. */
  4076. if (cur_block->is_stack_polymorphic) {
  4077. for (i = (int32)arity - 1; i >= 0; i--) {
  4078. #if WASM_ENABLE_FAST_INTERP != 0
  4079. POP_OFFSET_TYPE(types[i]);
  4080. #endif
  4081. POP_TYPE(types[i]);
  4082. }
  4083. for (i = 0; i < (int32)arity; i++) {
  4084. #if WASM_ENABLE_FAST_INTERP != 0
  4085. bool disable_emit = true;
  4086. int16 operand_offset = 0;
  4087. PUSH_OFFSET_TYPE(types[i]);
  4088. #endif
  4089. PUSH_TYPE(types[i]);
  4090. }
  4091. return true;
  4092. }
  4093. available_stack_cell =
  4094. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4095. /* Check stack top values match target block type */
  4096. for (i = (int32)arity - 1; i >= 0; i--) {
  4097. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  4098. error_buf, error_buf_size))
  4099. return false;
  4100. cell_num = wasm_value_type_cell_num(types[i]);
  4101. frame_ref -= cell_num;
  4102. available_stack_cell -= cell_num;
  4103. }
  4104. return true;
  4105. fail:
  4106. return false;
  4107. }
  4108. static BranchBlock *
  4109. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  4110. char *error_buf, uint32 error_buf_size)
  4111. {
  4112. uint8 *p = *p_buf, *p_end = buf_end;
  4113. BranchBlock *frame_csp_tmp;
  4114. uint32 depth;
  4115. read_leb_uint32(p, p_end, depth);
  4116. CHECK_BR(depth);
  4117. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  4118. #if WASM_ENABLE_FAST_INTERP != 0
  4119. emit_br_info(frame_csp_tmp);
  4120. #endif
  4121. *p_buf = p;
  4122. return frame_csp_tmp;
  4123. fail:
  4124. return NULL;
  4125. }
  4126. static bool
  4127. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  4128. char *error_buf, uint32 error_buf_size)
  4129. {
  4130. BlockType *block_type = &block->block_type;
  4131. uint8 *return_types = NULL;
  4132. uint32 return_count = 0;
  4133. int32 available_stack_cell, return_cell_num, i;
  4134. uint8 *frame_ref = NULL;
  4135. available_stack_cell =
  4136. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  4137. return_count = block_type_get_result_types(block_type, &return_types);
  4138. return_cell_num =
  4139. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  4140. /* If the stack is in polymorphic state, just clear the stack
  4141. * and then re-push the values to make the stack top values
  4142. * match block type. */
  4143. if (block->is_stack_polymorphic) {
  4144. for (i = (int32)return_count - 1; i >= 0; i--) {
  4145. #if WASM_ENABLE_FAST_INTERP != 0
  4146. POP_OFFSET_TYPE(return_types[i]);
  4147. #endif
  4148. POP_TYPE(return_types[i]);
  4149. }
  4150. /* Check stack is empty */
  4151. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  4152. for (i = 0; i < (int32)return_count; i++) {
  4153. #if WASM_ENABLE_FAST_INTERP != 0
  4154. bool disable_emit = true;
  4155. int16 operand_offset = 0;
  4156. PUSH_OFFSET_TYPE(return_types[i]);
  4157. #endif
  4158. PUSH_TYPE(return_types[i]);
  4159. }
  4160. return true;
  4161. }
  4162. /* Check stack cell num equals return cell num */
  4163. bh_assert(available_stack_cell == return_cell_num);
  4164. /* Check stack values match return types */
  4165. frame_ref = loader_ctx->frame_ref;
  4166. for (i = (int32)return_count - 1; i >= 0; i--) {
  4167. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4168. return_types[i], error_buf, error_buf_size))
  4169. return false;
  4170. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  4171. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  4172. }
  4173. (void)return_cell_num;
  4174. return true;
  4175. fail:
  4176. return false;
  4177. }
  4178. #if WASM_ENABLE_FAST_INTERP != 0
  4179. /* Copy parameters to dynamic space.
  4180. * 1) POP original parameter out;
  4181. * 2) Push and copy original values to dynamic space.
  4182. * The copy instruction format:
  4183. * Part a: param count
  4184. * Part b: all param total cell num
  4185. * Part c: each param's cell_num, src offset and dst offset
  4186. * Part d: each param's src offset
  4187. * Part e: each param's dst offset
  4188. */
  4189. static bool
  4190. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  4191. char *error_buf, uint32 error_buf_size)
  4192. {
  4193. int16 *frame_offset = NULL;
  4194. uint8 *cells = NULL, cell;
  4195. int16 *src_offsets = NULL;
  4196. uint8 *emit_data = NULL;
  4197. uint32 i;
  4198. BranchBlock *block = loader_ctx->frame_csp - 1;
  4199. BlockType *block_type = &block->block_type;
  4200. WASMType *wasm_type = block_type->u.type;
  4201. uint32 param_count = block_type->u.type->param_count;
  4202. int16 condition_offset = 0;
  4203. bool disable_emit = false;
  4204. int16 operand_offset = 0;
  4205. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  4206. /* For if block, we also need copy the condition operand offset. */
  4207. if (is_if_block)
  4208. size += sizeof(*cells) + sizeof(*src_offsets);
  4209. /* Allocate memory for the emit data */
  4210. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4211. return false;
  4212. cells = emit_data;
  4213. src_offsets = (int16 *)(cells + param_count);
  4214. if (is_if_block)
  4215. condition_offset = *loader_ctx->frame_offset;
  4216. /* POP original parameter out */
  4217. for (i = 0; i < param_count; i++) {
  4218. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  4219. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4220. }
  4221. frame_offset = loader_ctx->frame_offset;
  4222. /* Get each param's cell num and src offset */
  4223. for (i = 0; i < param_count; i++) {
  4224. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  4225. cells[i] = cell;
  4226. src_offsets[i] = *frame_offset;
  4227. frame_offset += cell;
  4228. }
  4229. /* emit copy instruction */
  4230. emit_label(EXT_OP_COPY_STACK_VALUES);
  4231. /* Part a) */
  4232. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  4233. /* Part b) */
  4234. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  4235. : wasm_type->param_cell_num);
  4236. /* Part c) */
  4237. for (i = 0; i < param_count; i++)
  4238. emit_byte(loader_ctx, cells[i]);
  4239. if (is_if_block)
  4240. emit_byte(loader_ctx, 1);
  4241. /* Part d) */
  4242. for (i = 0; i < param_count; i++)
  4243. emit_operand(loader_ctx, src_offsets[i]);
  4244. if (is_if_block)
  4245. emit_operand(loader_ctx, condition_offset);
  4246. /* Part e) */
  4247. /* Push to dynamic space. The push will emit the dst offset. */
  4248. for (i = 0; i < param_count; i++)
  4249. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  4250. if (is_if_block)
  4251. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  4252. /* Free the emit data */
  4253. wasm_runtime_free(emit_data);
  4254. return true;
  4255. fail:
  4256. return false;
  4257. }
  4258. #endif
  4259. /* reset the stack to the state of before entering the last block */
  4260. #if WASM_ENABLE_FAST_INTERP != 0
  4261. #define RESET_STACK() \
  4262. do { \
  4263. loader_ctx->stack_cell_num = \
  4264. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4265. loader_ctx->frame_ref = \
  4266. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4267. loader_ctx->frame_offset = \
  4268. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  4269. } while (0)
  4270. #else
  4271. #define RESET_STACK() \
  4272. do { \
  4273. loader_ctx->stack_cell_num = \
  4274. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4275. loader_ctx->frame_ref = \
  4276. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4277. } while (0)
  4278. #endif
  4279. /* set current block's stack polymorphic state */
  4280. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  4281. do { \
  4282. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  4283. _cur_block->is_stack_polymorphic = flag; \
  4284. } while (0)
  4285. #define BLOCK_HAS_PARAM(block_type) \
  4286. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  4287. #define PRESERVE_LOCAL_FOR_BLOCK() \
  4288. do { \
  4289. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  4290. error_buf_size))) { \
  4291. goto fail; \
  4292. } \
  4293. } while (0)
  4294. static bool
  4295. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4296. uint32 cur_func_idx, char *error_buf,
  4297. uint32 error_buf_size)
  4298. {
  4299. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  4300. uint32 param_count, local_count, global_count;
  4301. uint8 *param_types, *local_types, local_type, global_type;
  4302. BlockType func_block_type;
  4303. uint16 *local_offsets, local_offset;
  4304. uint32 count, local_idx, global_idx, u32, align, mem_offset, i;
  4305. int32 i32, i32_const = 0;
  4306. int64 i64_const;
  4307. uint8 opcode, u8;
  4308. bool return_value = false;
  4309. WASMLoaderContext *loader_ctx;
  4310. BranchBlock *frame_csp_tmp;
  4311. #if WASM_ENABLE_BULK_MEMORY != 0
  4312. uint32 segment_index;
  4313. #endif
  4314. #if WASM_ENABLE_FAST_INTERP != 0
  4315. uint8 *func_const_end, *func_const = NULL;
  4316. int16 operand_offset = 0;
  4317. uint8 last_op = 0;
  4318. bool disable_emit, preserve_local = false;
  4319. float32 f32_const;
  4320. float64 f64_const;
  4321. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  4322. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  4323. #endif
  4324. global_count = module->import_global_count + module->global_count;
  4325. param_count = func->func_type->param_count;
  4326. param_types = func->func_type->types;
  4327. func_block_type.is_value_type = false;
  4328. func_block_type.u.type = func->func_type;
  4329. local_count = func->local_count;
  4330. local_types = func->local_types;
  4331. local_offsets = func->local_offsets;
  4332. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  4333. goto fail;
  4334. }
  4335. #if WASM_ENABLE_FAST_INTERP != 0
  4336. /* For the first traverse, the initial value of preserved_local_offset has
  4337. * not been determined, we use the INT16_MAX to represent that a slot has
  4338. * been copied to preserve space. For second traverse, this field will be
  4339. * set to the appropriate value in wasm_loader_ctx_reinit.
  4340. * This is for Issue #1230,
  4341. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  4342. * drop opcodes need to know which slots are preserved, so those slots will
  4343. * not be treated as dynamically allocated slots */
  4344. loader_ctx->preserved_local_offset = INT16_MAX;
  4345. re_scan:
  4346. if (loader_ctx->code_compiled_size > 0) {
  4347. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  4348. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  4349. goto fail;
  4350. }
  4351. p = func->code;
  4352. func->code_compiled = loader_ctx->p_code_compiled;
  4353. func->code_compiled_size = loader_ctx->code_compiled_size;
  4354. }
  4355. #endif
  4356. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  4357. while (p < p_end) {
  4358. opcode = *p++;
  4359. #if WASM_ENABLE_FAST_INTERP != 0
  4360. p_org = p;
  4361. disable_emit = false;
  4362. emit_label(opcode);
  4363. #endif
  4364. switch (opcode) {
  4365. case WASM_OP_UNREACHABLE:
  4366. RESET_STACK();
  4367. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4368. break;
  4369. case WASM_OP_NOP:
  4370. #if WASM_ENABLE_FAST_INTERP != 0
  4371. skip_label();
  4372. #endif
  4373. break;
  4374. case WASM_OP_IF:
  4375. #if WASM_ENABLE_FAST_INTERP != 0
  4376. PRESERVE_LOCAL_FOR_BLOCK();
  4377. #endif
  4378. POP_I32();
  4379. goto handle_op_block_and_loop;
  4380. case WASM_OP_BLOCK:
  4381. case WASM_OP_LOOP:
  4382. #if WASM_ENABLE_FAST_INTERP != 0
  4383. PRESERVE_LOCAL_FOR_BLOCK();
  4384. #endif
  4385. handle_op_block_and_loop:
  4386. {
  4387. uint8 value_type;
  4388. BlockType block_type;
  4389. p_org = p - 1;
  4390. value_type = read_uint8(p);
  4391. if (is_byte_a_type(value_type)) {
  4392. /* If the first byte is one of these special values:
  4393. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  4394. * the single return value. */
  4395. block_type.is_value_type = true;
  4396. block_type.u.value_type = value_type;
  4397. }
  4398. else {
  4399. uint32 type_index;
  4400. /* Resolve the leb128 encoded type index as block type */
  4401. p--;
  4402. read_leb_uint32(p, p_end, type_index);
  4403. bh_assert(type_index < module->type_count);
  4404. block_type.is_value_type = false;
  4405. block_type.u.type = module->types[type_index];
  4406. #if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \
  4407. && WASM_ENABLE_JIT == 0
  4408. /* If block use type index as block type, change the opcode
  4409. * to new extended opcode so that interpreter can resolve
  4410. * the block quickly.
  4411. */
  4412. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  4413. #endif
  4414. }
  4415. /* Pop block parameters from stack */
  4416. if (BLOCK_HAS_PARAM(block_type)) {
  4417. WASMType *wasm_type = block_type.u.type;
  4418. for (i = 0; i < block_type.u.type->param_count; i++)
  4419. POP_TYPE(
  4420. wasm_type->types[wasm_type->param_count - i - 1]);
  4421. }
  4422. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  4423. block_type, p);
  4424. /* Pass parameters to block */
  4425. if (BLOCK_HAS_PARAM(block_type)) {
  4426. for (i = 0; i < block_type.u.type->param_count; i++)
  4427. PUSH_TYPE(block_type.u.type->types[i]);
  4428. }
  4429. #if WASM_ENABLE_FAST_INTERP != 0
  4430. if (opcode == WASM_OP_BLOCK) {
  4431. skip_label();
  4432. }
  4433. else if (opcode == WASM_OP_LOOP) {
  4434. skip_label();
  4435. if (BLOCK_HAS_PARAM(block_type)) {
  4436. /* Make sure params are in dynamic space */
  4437. if (!copy_params_to_dynamic_space(
  4438. loader_ctx, false, error_buf, error_buf_size))
  4439. goto fail;
  4440. }
  4441. (loader_ctx->frame_csp - 1)->code_compiled =
  4442. loader_ctx->p_code_compiled;
  4443. }
  4444. else if (opcode == WASM_OP_IF) {
  4445. /* If block has parameters, we should make sure they are in
  4446. * dynamic space. Otherwise, when else branch is missing,
  4447. * the later opcode may consume incorrect operand offset.
  4448. * Spec case:
  4449. * (func (export "params-id") (param i32) (result i32)
  4450. * (i32.const 1)
  4451. * (i32.const 2)
  4452. * (if (param i32 i32) (result i32 i32) (local.get 0)
  4453. * (then)) (i32.add)
  4454. * )
  4455. *
  4456. * So we should emit a copy instruction before the if.
  4457. *
  4458. * And we also need to save the parameter offsets and
  4459. * recover them before entering else branch.
  4460. *
  4461. */
  4462. if (BLOCK_HAS_PARAM(block_type)) {
  4463. BranchBlock *block = loader_ctx->frame_csp - 1;
  4464. uint64 size;
  4465. /* skip the if condition operand offset */
  4466. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4467. /* skip the if label */
  4468. skip_label();
  4469. /* Emit a copy instruction */
  4470. if (!copy_params_to_dynamic_space(
  4471. loader_ctx, true, error_buf, error_buf_size))
  4472. goto fail;
  4473. /* Emit the if instruction */
  4474. emit_label(opcode);
  4475. /* Emit the new condition operand offset */
  4476. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  4477. /* Save top param_count values of frame_offset stack, so
  4478. * that we can recover it before executing else branch
  4479. */
  4480. size = sizeof(int16)
  4481. * (uint64)block_type.u.type->param_cell_num;
  4482. if (!(block->param_frame_offsets = loader_malloc(
  4483. size, error_buf, error_buf_size)))
  4484. goto fail;
  4485. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  4486. loader_ctx->frame_offset
  4487. - size / sizeof(int16),
  4488. (uint32)size);
  4489. }
  4490. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  4491. emit_empty_label_addr_and_frame_ip(PATCH_END);
  4492. }
  4493. #endif
  4494. break;
  4495. }
  4496. case WASM_OP_ELSE:
  4497. {
  4498. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  4499. bh_assert(loader_ctx->csp_num >= 2
  4500. && (loader_ctx->frame_csp - 1)->label_type
  4501. == LABEL_TYPE_IF);
  4502. /* check whether if branch's stack matches its result type */
  4503. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  4504. error_buf, error_buf_size))
  4505. goto fail;
  4506. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  4507. #if WASM_ENABLE_FAST_INTERP != 0
  4508. /* if the result of if branch is in local or const area, add a
  4509. * copy op */
  4510. RESERVE_BLOCK_RET();
  4511. emit_empty_label_addr_and_frame_ip(PATCH_END);
  4512. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  4513. #endif
  4514. RESET_STACK();
  4515. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  4516. /* Pass parameters to if-false branch */
  4517. if (BLOCK_HAS_PARAM(block_type)) {
  4518. for (i = 0; i < block_type.u.type->param_count; i++)
  4519. PUSH_TYPE(block_type.u.type->types[i]);
  4520. }
  4521. #if WASM_ENABLE_FAST_INTERP != 0
  4522. /* Recover top param_count values of frame_offset stack */
  4523. if (BLOCK_HAS_PARAM((block_type))) {
  4524. uint32 size;
  4525. BranchBlock *block = loader_ctx->frame_csp - 1;
  4526. size = sizeof(int16) * block_type.u.type->param_cell_num;
  4527. bh_memcpy_s(loader_ctx->frame_offset, size,
  4528. block->param_frame_offsets, size);
  4529. loader_ctx->frame_offset += (size / sizeof(int16));
  4530. }
  4531. #endif
  4532. break;
  4533. }
  4534. case WASM_OP_END:
  4535. {
  4536. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  4537. /* check whether block stack matches its result type */
  4538. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  4539. error_buf_size))
  4540. goto fail;
  4541. /* if no else branch, and return types do not match param types,
  4542. * fail */
  4543. if (cur_block->label_type == LABEL_TYPE_IF
  4544. && !cur_block->else_addr) {
  4545. uint32 block_param_count = 0, block_ret_count = 0;
  4546. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  4547. BlockType *cur_block_type = &cur_block->block_type;
  4548. if (cur_block_type->is_value_type) {
  4549. if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
  4550. block_ret_count = 1;
  4551. block_ret_types = &cur_block_type->u.value_type;
  4552. }
  4553. }
  4554. else {
  4555. block_param_count = cur_block_type->u.type->param_count;
  4556. block_ret_count = cur_block_type->u.type->result_count;
  4557. block_param_types = cur_block_type->u.type->types;
  4558. block_ret_types =
  4559. cur_block_type->u.type->types + block_param_count;
  4560. }
  4561. bh_assert(block_param_count == block_ret_count
  4562. && (!block_param_count
  4563. || !memcmp(block_param_types, block_ret_types,
  4564. block_param_count)));
  4565. (void)block_ret_types;
  4566. (void)block_ret_count;
  4567. (void)block_param_types;
  4568. }
  4569. POP_CSP();
  4570. #if WASM_ENABLE_FAST_INTERP != 0
  4571. skip_label();
  4572. /* copy the result to the block return address */
  4573. RESERVE_BLOCK_RET();
  4574. apply_label_patch(loader_ctx, 0, PATCH_END);
  4575. free_label_patch_list(loader_ctx->frame_csp);
  4576. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  4577. int32 idx;
  4578. uint8 ret_type;
  4579. emit_label(WASM_OP_RETURN);
  4580. for (idx = (int32)func->func_type->result_count - 1;
  4581. idx >= 0; idx--) {
  4582. ret_type = *(func->func_type->types
  4583. + func->func_type->param_count + idx);
  4584. POP_OFFSET_TYPE(ret_type);
  4585. }
  4586. }
  4587. #endif
  4588. if (loader_ctx->csp_num > 0) {
  4589. loader_ctx->frame_csp->end_addr = p - 1;
  4590. }
  4591. else {
  4592. /* end of function block, function will return,
  4593. ignore the following bytecodes */
  4594. p = p_end;
  4595. continue;
  4596. }
  4597. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  4598. break;
  4599. }
  4600. case WASM_OP_BR:
  4601. {
  4602. if (!(frame_csp_tmp = check_branch_block(
  4603. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  4604. goto fail;
  4605. RESET_STACK();
  4606. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4607. break;
  4608. }
  4609. case WASM_OP_BR_IF:
  4610. {
  4611. POP_I32();
  4612. if (!(frame_csp_tmp = check_branch_block(
  4613. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  4614. goto fail;
  4615. break;
  4616. }
  4617. case WASM_OP_BR_TABLE:
  4618. {
  4619. uint8 *ret_types = NULL;
  4620. uint32 ret_count = 0;
  4621. #if WASM_ENABLE_FAST_INTERP == 0
  4622. uint8 *p_depth_begin, *p_depth;
  4623. uint32 depth, j;
  4624. BrTableCache *br_table_cache = NULL;
  4625. p_org = p - 1;
  4626. #endif
  4627. read_leb_uint32(p, p_end, count);
  4628. #if WASM_ENABLE_FAST_INTERP != 0
  4629. emit_uint32(loader_ctx, count);
  4630. #endif
  4631. POP_I32();
  4632. #if WASM_ENABLE_FAST_INTERP == 0
  4633. p_depth_begin = p_depth = p;
  4634. #endif
  4635. for (i = 0; i <= count; i++) {
  4636. if (!(frame_csp_tmp =
  4637. check_branch_block(loader_ctx, &p, p_end,
  4638. error_buf, error_buf_size)))
  4639. goto fail;
  4640. #if WASM_ENABLE_FAST_INTERP == 0
  4641. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  4642. if (br_table_cache) {
  4643. br_table_cache->br_depths[i] = depth;
  4644. }
  4645. else {
  4646. if (depth > 255) {
  4647. /* The depth cannot be stored in one byte,
  4648. create br_table cache to store each depth */
  4649. #if WASM_ENABLE_DEBUG_INTERP != 0
  4650. if (!record_fast_op(module, p_org, *p_org,
  4651. error_buf, error_buf_size)) {
  4652. goto fail;
  4653. }
  4654. #endif
  4655. if (!(br_table_cache = loader_malloc(
  4656. offsetof(BrTableCache, br_depths)
  4657. + sizeof(uint32)
  4658. * (uint64)(count + 1),
  4659. error_buf, error_buf_size))) {
  4660. goto fail;
  4661. }
  4662. *p_org = EXT_OP_BR_TABLE_CACHE;
  4663. br_table_cache->br_table_op_addr = p_org;
  4664. br_table_cache->br_count = count;
  4665. /* Copy previous depths which are one byte */
  4666. for (j = 0; j < i; j++) {
  4667. br_table_cache->br_depths[j] = p_depth_begin[j];
  4668. }
  4669. br_table_cache->br_depths[i] = depth;
  4670. bh_list_insert(module->br_table_cache_list,
  4671. br_table_cache);
  4672. }
  4673. else {
  4674. /* The depth can be stored in one byte, use the
  4675. byte of the leb to store it */
  4676. *p_depth++ = (uint8)depth;
  4677. }
  4678. }
  4679. #endif
  4680. }
  4681. #if WASM_ENABLE_FAST_INTERP == 0
  4682. /* Set the tailing bytes to nop */
  4683. if (br_table_cache)
  4684. p_depth = p_depth_begin;
  4685. while (p_depth < p)
  4686. *p_depth++ = WASM_OP_NOP;
  4687. #endif
  4688. RESET_STACK();
  4689. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4690. (void)ret_count;
  4691. (void)ret_types;
  4692. break;
  4693. }
  4694. case WASM_OP_RETURN:
  4695. {
  4696. int32 idx;
  4697. uint8 ret_type;
  4698. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  4699. idx--) {
  4700. ret_type = *(func->func_type->types
  4701. + func->func_type->param_count + idx);
  4702. POP_TYPE(ret_type);
  4703. #if WASM_ENABLE_FAST_INTERP != 0
  4704. /* emit the offset after return opcode */
  4705. POP_OFFSET_TYPE(ret_type);
  4706. #endif
  4707. }
  4708. RESET_STACK();
  4709. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4710. break;
  4711. }
  4712. case WASM_OP_CALL:
  4713. #if WASM_ENABLE_TAIL_CALL != 0
  4714. case WASM_OP_RETURN_CALL:
  4715. #endif
  4716. {
  4717. WASMType *func_type;
  4718. uint32 func_idx;
  4719. int32 idx;
  4720. read_leb_uint32(p, p_end, func_idx);
  4721. #if WASM_ENABLE_FAST_INTERP != 0
  4722. /* we need to emit func_idx before arguments */
  4723. emit_uint32(loader_ctx, func_idx);
  4724. #endif
  4725. bh_assert(func_idx < module->import_function_count
  4726. + module->function_count);
  4727. if (func_idx < module->import_function_count)
  4728. func_type =
  4729. module->import_functions[func_idx].u.function.func_type;
  4730. else
  4731. func_type = module
  4732. ->functions[func_idx
  4733. - module->import_function_count]
  4734. ->func_type;
  4735. if (func_type->param_count > 0) {
  4736. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  4737. idx--) {
  4738. POP_TYPE(func_type->types[idx]);
  4739. #if WASM_ENABLE_FAST_INTERP != 0
  4740. POP_OFFSET_TYPE(func_type->types[idx]);
  4741. #endif
  4742. }
  4743. }
  4744. #if WASM_ENABLE_TAIL_CALL != 0
  4745. if (opcode == WASM_OP_CALL) {
  4746. #endif
  4747. for (i = 0; i < func_type->result_count; i++) {
  4748. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  4749. #if WASM_ENABLE_FAST_INTERP != 0
  4750. /* Here we emit each return value's dynamic_offset. But
  4751. * in fact these offsets are continuous, so interpreter
  4752. * only need to get the first return value's offset.
  4753. */
  4754. PUSH_OFFSET_TYPE(
  4755. func_type->types[func_type->param_count + i]);
  4756. #endif
  4757. }
  4758. #if WASM_ENABLE_TAIL_CALL != 0
  4759. }
  4760. else {
  4761. bh_assert(func_type->result_count
  4762. == func->func_type->result_count);
  4763. for (i = 0; i < func_type->result_count; i++) {
  4764. bh_assert(
  4765. func_type->types[func_type->param_count + i]
  4766. == func->func_type
  4767. ->types[func->func_type->param_count + i]);
  4768. }
  4769. }
  4770. #endif
  4771. func->has_op_func_call = true;
  4772. break;
  4773. }
  4774. case WASM_OP_CALL_INDIRECT:
  4775. #if WASM_ENABLE_TAIL_CALL != 0
  4776. case WASM_OP_RETURN_CALL_INDIRECT:
  4777. #endif
  4778. {
  4779. int32 idx;
  4780. WASMType *func_type;
  4781. uint32 type_idx, table_idx;
  4782. bh_assert(module->import_table_count + module->table_count > 0);
  4783. read_leb_uint32(p, p_end, type_idx);
  4784. #if WASM_ENABLE_REF_TYPES != 0
  4785. read_leb_uint32(p, p_end, table_idx);
  4786. #else
  4787. CHECK_BUF(p, p_end, 1);
  4788. table_idx = read_uint8(p);
  4789. #endif
  4790. if (!check_table_index(module, table_idx, error_buf,
  4791. error_buf_size)) {
  4792. goto fail;
  4793. }
  4794. #if WASM_ENABLE_FAST_INTERP != 0
  4795. /* we need to emit before arguments */
  4796. emit_uint32(loader_ctx, type_idx);
  4797. emit_uint32(loader_ctx, table_idx);
  4798. #endif
  4799. /* skip elem idx */
  4800. POP_I32();
  4801. bh_assert(type_idx < module->type_count);
  4802. func_type = module->types[type_idx];
  4803. if (func_type->param_count > 0) {
  4804. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  4805. idx--) {
  4806. POP_TYPE(func_type->types[idx]);
  4807. #if WASM_ENABLE_FAST_INTERP != 0
  4808. POP_OFFSET_TYPE(func_type->types[idx]);
  4809. #endif
  4810. }
  4811. }
  4812. #if WASM_ENABLE_TAIL_CALL != 0
  4813. if (opcode == WASM_OP_CALL) {
  4814. #endif
  4815. for (i = 0; i < func_type->result_count; i++) {
  4816. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  4817. #if WASM_ENABLE_FAST_INTERP != 0
  4818. PUSH_OFFSET_TYPE(
  4819. func_type->types[func_type->param_count + i]);
  4820. #endif
  4821. }
  4822. #if WASM_ENABLE_TAIL_CALL != 0
  4823. }
  4824. else {
  4825. bh_assert(func_type->result_count
  4826. == func->func_type->result_count);
  4827. for (i = 0; i < func_type->result_count; i++) {
  4828. bh_assert(
  4829. func_type->types[func_type->param_count + i]
  4830. == func->func_type
  4831. ->types[func->func_type->param_count + i]);
  4832. }
  4833. }
  4834. #endif
  4835. func->has_op_func_call = true;
  4836. break;
  4837. }
  4838. case WASM_OP_DROP:
  4839. case WASM_OP_DROP_64:
  4840. {
  4841. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  4842. int32 available_stack_cell =
  4843. (int32)(loader_ctx->stack_cell_num
  4844. - cur_block->stack_cell_num);
  4845. bh_assert(!(available_stack_cell <= 0
  4846. && !cur_block->is_stack_polymorphic));
  4847. if (available_stack_cell > 0) {
  4848. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  4849. loader_ctx->frame_ref--;
  4850. loader_ctx->stack_cell_num--;
  4851. #if WASM_ENABLE_FAST_INTERP != 0
  4852. skip_label();
  4853. loader_ctx->frame_offset--;
  4854. if ((*(loader_ctx->frame_offset)
  4855. > loader_ctx->start_dynamic_offset)
  4856. && (*(loader_ctx->frame_offset)
  4857. < loader_ctx->max_dynamic_offset))
  4858. loader_ctx->dynamic_offset--;
  4859. #endif
  4860. }
  4861. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  4862. loader_ctx->frame_ref -= 2;
  4863. loader_ctx->stack_cell_num -= 2;
  4864. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  4865. *(p - 1) = WASM_OP_DROP_64;
  4866. #endif
  4867. #if WASM_ENABLE_FAST_INTERP != 0
  4868. skip_label();
  4869. loader_ctx->frame_offset -= 2;
  4870. if ((*(loader_ctx->frame_offset)
  4871. > loader_ctx->start_dynamic_offset)
  4872. && (*(loader_ctx->frame_offset)
  4873. < loader_ctx->max_dynamic_offset))
  4874. loader_ctx->dynamic_offset -= 2;
  4875. #endif
  4876. }
  4877. else {
  4878. bh_assert(0);
  4879. }
  4880. }
  4881. else {
  4882. #if WASM_ENABLE_FAST_INTERP != 0
  4883. skip_label();
  4884. #endif
  4885. }
  4886. break;
  4887. }
  4888. case WASM_OP_SELECT:
  4889. case WASM_OP_SELECT_64:
  4890. {
  4891. uint8 ref_type;
  4892. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  4893. int32 available_stack_cell;
  4894. POP_I32();
  4895. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  4896. - cur_block->stack_cell_num);
  4897. bh_assert(!(available_stack_cell <= 0
  4898. && !cur_block->is_stack_polymorphic));
  4899. if (available_stack_cell > 0) {
  4900. switch (*(loader_ctx->frame_ref - 1)) {
  4901. case REF_I32:
  4902. case REF_F32:
  4903. break;
  4904. case REF_I64_2:
  4905. case REF_F64_2:
  4906. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  4907. *(p - 1) = WASM_OP_SELECT_64;
  4908. #endif
  4909. #if WASM_ENABLE_FAST_INTERP != 0
  4910. if (loader_ctx->p_code_compiled) {
  4911. uint8 opcode_tmp = WASM_OP_SELECT_64;
  4912. uint8 *p_code_compiled_tmp =
  4913. loader_ctx->p_code_compiled - 2;
  4914. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4915. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4916. *(void **)(p_code_compiled_tmp
  4917. - sizeof(void *)) =
  4918. handle_table[opcode_tmp];
  4919. #else
  4920. int32 offset =
  4921. (int32)((uint8 *)handle_table[opcode_tmp]
  4922. - (uint8 *)handle_table[0]);
  4923. if (!(offset >= INT16_MIN
  4924. && offset < INT16_MAX)) {
  4925. set_error_buf(error_buf, error_buf_size,
  4926. "pre-compiled label offset "
  4927. "out of range");
  4928. goto fail;
  4929. }
  4930. *(int16 *)(p_code_compiled_tmp
  4931. - sizeof(int16)) = (int16)offset;
  4932. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4933. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4934. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4935. *(p_code_compiled_tmp - 1) = opcode_tmp;
  4936. #else
  4937. *(p_code_compiled_tmp - 2) = opcode_tmp;
  4938. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4939. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4940. }
  4941. #endif
  4942. break;
  4943. }
  4944. ref_type = *(loader_ctx->frame_ref - 1);
  4945. #if WASM_ENABLE_FAST_INTERP != 0
  4946. POP_OFFSET_TYPE(ref_type);
  4947. #endif
  4948. POP_TYPE(ref_type);
  4949. #if WASM_ENABLE_FAST_INTERP != 0
  4950. POP_OFFSET_TYPE(ref_type);
  4951. #endif
  4952. POP_TYPE(ref_type);
  4953. #if WASM_ENABLE_FAST_INTERP != 0
  4954. PUSH_OFFSET_TYPE(ref_type);
  4955. #endif
  4956. PUSH_TYPE(ref_type);
  4957. }
  4958. else {
  4959. #if WASM_ENABLE_FAST_INTERP != 0
  4960. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  4961. #endif
  4962. PUSH_TYPE(VALUE_TYPE_ANY);
  4963. }
  4964. break;
  4965. }
  4966. #if WASM_ENABLE_REF_TYPES != 0
  4967. case WASM_OP_SELECT_T:
  4968. {
  4969. uint8 vec_len, ref_type;
  4970. read_leb_uint32(p, p_end, vec_len);
  4971. if (!vec_len) {
  4972. set_error_buf(error_buf, error_buf_size,
  4973. "invalid result arity");
  4974. goto fail;
  4975. }
  4976. CHECK_BUF(p, p_end, 1);
  4977. ref_type = read_uint8(p);
  4978. if (!is_value_type(ref_type)) {
  4979. set_error_buf(error_buf, error_buf_size,
  4980. "unknown value type");
  4981. goto fail;
  4982. }
  4983. POP_I32();
  4984. #if WASM_ENABLE_FAST_INTERP != 0
  4985. if (loader_ctx->p_code_compiled) {
  4986. uint8 opcode_tmp = WASM_OP_SELECT;
  4987. uint8 *p_code_compiled_tmp =
  4988. loader_ctx->p_code_compiled - 2;
  4989. if (ref_type == VALUE_TYPE_F64
  4990. || ref_type == VALUE_TYPE_I64)
  4991. opcode_tmp = WASM_OP_SELECT_64;
  4992. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4993. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4994. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  4995. handle_table[opcode_tmp];
  4996. #else
  4997. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  4998. - (uint8 *)handle_table[0]);
  4999. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  5000. set_error_buf(error_buf, error_buf_size,
  5001. "pre-compiled label offset out of range");
  5002. goto fail;
  5003. }
  5004. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  5005. (int16)offset;
  5006. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5007. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5008. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5009. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5010. #else
  5011. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5012. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5013. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5014. }
  5015. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5016. #if WASM_ENABLE_FAST_INTERP != 0
  5017. POP_OFFSET_TYPE(ref_type);
  5018. POP_TYPE(ref_type);
  5019. POP_OFFSET_TYPE(ref_type);
  5020. POP_TYPE(ref_type);
  5021. PUSH_OFFSET_TYPE(ref_type);
  5022. PUSH_TYPE(ref_type);
  5023. #else
  5024. POP2_AND_PUSH(ref_type, ref_type);
  5025. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5026. (void)vec_len;
  5027. break;
  5028. }
  5029. /* table.get x. tables[x]. [i32] -> [t] */
  5030. /* table.set x. tables[x]. [i32 t] -> [] */
  5031. case WASM_OP_TABLE_GET:
  5032. case WASM_OP_TABLE_SET:
  5033. {
  5034. uint8 decl_ref_type;
  5035. uint32 table_idx;
  5036. read_leb_uint32(p, p_end, table_idx);
  5037. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  5038. error_buf, error_buf_size))
  5039. goto fail;
  5040. #if WASM_ENABLE_FAST_INTERP != 0
  5041. emit_uint32(loader_ctx, table_idx);
  5042. #endif
  5043. if (opcode == WASM_OP_TABLE_GET) {
  5044. POP_I32();
  5045. #if WASM_ENABLE_FAST_INTERP != 0
  5046. PUSH_OFFSET_TYPE(decl_ref_type);
  5047. #endif
  5048. PUSH_TYPE(decl_ref_type);
  5049. }
  5050. else {
  5051. #if WASM_ENABLE_FAST_INTERP != 0
  5052. POP_OFFSET_TYPE(decl_ref_type);
  5053. #endif
  5054. POP_TYPE(decl_ref_type);
  5055. POP_I32();
  5056. }
  5057. break;
  5058. }
  5059. case WASM_OP_REF_NULL:
  5060. {
  5061. uint8 ref_type;
  5062. CHECK_BUF(p, p_end, 1);
  5063. ref_type = read_uint8(p);
  5064. if (ref_type != VALUE_TYPE_FUNCREF
  5065. && ref_type != VALUE_TYPE_EXTERNREF) {
  5066. set_error_buf(error_buf, error_buf_size,
  5067. "unknown value type");
  5068. goto fail;
  5069. }
  5070. #if WASM_ENABLE_FAST_INTERP != 0
  5071. PUSH_OFFSET_TYPE(ref_type);
  5072. #endif
  5073. PUSH_TYPE(ref_type);
  5074. break;
  5075. }
  5076. case WASM_OP_REF_IS_NULL:
  5077. {
  5078. #if WASM_ENABLE_FAST_INTERP != 0
  5079. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  5080. VALUE_TYPE_FUNCREF,
  5081. error_buf, error_buf_size)
  5082. && !wasm_loader_pop_frame_ref_offset(
  5083. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  5084. error_buf_size)) {
  5085. goto fail;
  5086. }
  5087. #else
  5088. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  5089. error_buf, error_buf_size)
  5090. && !wasm_loader_pop_frame_ref(loader_ctx,
  5091. VALUE_TYPE_EXTERNREF,
  5092. error_buf, error_buf_size)) {
  5093. goto fail;
  5094. }
  5095. #endif
  5096. PUSH_I32();
  5097. break;
  5098. }
  5099. case WASM_OP_REF_FUNC:
  5100. {
  5101. uint32 func_idx = 0;
  5102. read_leb_uint32(p, p_end, func_idx);
  5103. if (!check_function_index(module, func_idx, error_buf,
  5104. error_buf_size)) {
  5105. goto fail;
  5106. }
  5107. if (func_idx == cur_func_idx) {
  5108. WASMTableSeg *table_seg = module->table_segments;
  5109. bool func_declared = false;
  5110. uint32 j;
  5111. /* Check whether current function is declared */
  5112. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  5113. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  5114. && wasm_elem_is_declarative(table_seg->mode)) {
  5115. for (j = 0; j < table_seg->function_count; j++) {
  5116. if (table_seg->func_indexes[j]
  5117. == cur_func_idx) {
  5118. func_declared = true;
  5119. break;
  5120. }
  5121. }
  5122. }
  5123. }
  5124. if (!func_declared) {
  5125. set_error_buf(error_buf, error_buf_size,
  5126. "undeclared function reference");
  5127. goto fail;
  5128. }
  5129. }
  5130. #if WASM_ENABLE_FAST_INTERP != 0
  5131. emit_uint32(loader_ctx, func_idx);
  5132. #endif
  5133. PUSH_FUNCREF();
  5134. break;
  5135. }
  5136. #endif /* WASM_ENABLE_REF_TYPES */
  5137. case WASM_OP_GET_LOCAL:
  5138. {
  5139. p_org = p - 1;
  5140. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5141. PUSH_TYPE(local_type);
  5142. #if WASM_ENABLE_FAST_INTERP != 0
  5143. /* Get Local is optimized out */
  5144. skip_label();
  5145. disable_emit = true;
  5146. operand_offset = local_offset;
  5147. PUSH_OFFSET_TYPE(local_type);
  5148. #else
  5149. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5150. && (WASM_ENABLE_FAST_JIT == 0)
  5151. if (local_offset < 0x80) {
  5152. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  5153. if (is_32bit_type(local_type))
  5154. *p_org++ = (uint8)local_offset;
  5155. else
  5156. *p_org++ = (uint8)(local_offset | 0x80);
  5157. while (p_org < p)
  5158. *p_org++ = WASM_OP_NOP;
  5159. }
  5160. #endif
  5161. #endif
  5162. break;
  5163. }
  5164. case WASM_OP_SET_LOCAL:
  5165. {
  5166. p_org = p - 1;
  5167. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5168. POP_TYPE(local_type);
  5169. #if WASM_ENABLE_FAST_INTERP != 0
  5170. if (!(preserve_referenced_local(
  5171. loader_ctx, opcode, local_offset, local_type,
  5172. &preserve_local, error_buf, error_buf_size)))
  5173. goto fail;
  5174. if (local_offset < 256) {
  5175. skip_label();
  5176. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  5177. if (loader_ctx->p_code_compiled)
  5178. STORE_U16(loader_ctx->p_code_compiled - 2,
  5179. local_offset);
  5180. loader_ctx->frame_offset--;
  5181. loader_ctx->dynamic_offset--;
  5182. }
  5183. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  5184. if (loader_ctx->p_code_compiled)
  5185. STORE_U16(loader_ctx->p_code_compiled - 2,
  5186. local_offset);
  5187. loader_ctx->frame_offset -= 2;
  5188. loader_ctx->dynamic_offset -= 2;
  5189. }
  5190. else {
  5191. if (is_32bit_type(local_type)) {
  5192. emit_label(EXT_OP_SET_LOCAL_FAST);
  5193. emit_byte(loader_ctx, (uint8)local_offset);
  5194. }
  5195. else {
  5196. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  5197. emit_byte(loader_ctx, (uint8)local_offset);
  5198. }
  5199. POP_OFFSET_TYPE(local_type);
  5200. }
  5201. }
  5202. else { /* local index larger than 255, reserve leb */
  5203. emit_uint32(loader_ctx, local_idx);
  5204. POP_OFFSET_TYPE(local_type);
  5205. }
  5206. #else
  5207. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5208. && (WASM_ENABLE_FAST_JIT == 0)
  5209. if (local_offset < 0x80) {
  5210. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  5211. if (is_32bit_type(local_type))
  5212. *p_org++ = (uint8)local_offset;
  5213. else
  5214. *p_org++ = (uint8)(local_offset | 0x80);
  5215. while (p_org < p)
  5216. *p_org++ = WASM_OP_NOP;
  5217. }
  5218. #endif
  5219. #endif
  5220. break;
  5221. }
  5222. case WASM_OP_TEE_LOCAL:
  5223. {
  5224. p_org = p - 1;
  5225. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5226. #if WASM_ENABLE_FAST_INTERP != 0
  5227. /* If the stack is in polymorphic state, do fake pop and push on
  5228. offset stack to keep the depth of offset stack to be the
  5229. same with ref stack */
  5230. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5231. if (cur_block->is_stack_polymorphic) {
  5232. POP_OFFSET_TYPE(local_type);
  5233. PUSH_OFFSET_TYPE(local_type);
  5234. }
  5235. #endif
  5236. POP_TYPE(local_type);
  5237. PUSH_TYPE(local_type);
  5238. #if WASM_ENABLE_FAST_INTERP != 0
  5239. if (!(preserve_referenced_local(
  5240. loader_ctx, opcode, local_offset, local_type,
  5241. &preserve_local, error_buf, error_buf_size)))
  5242. goto fail;
  5243. if (local_offset < 256) {
  5244. skip_label();
  5245. if (is_32bit_type(local_type)) {
  5246. emit_label(EXT_OP_TEE_LOCAL_FAST);
  5247. emit_byte(loader_ctx, (uint8)local_offset);
  5248. }
  5249. else {
  5250. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  5251. emit_byte(loader_ctx, (uint8)local_offset);
  5252. }
  5253. }
  5254. else { /* local index larger than 255, reserve leb */
  5255. emit_uint32(loader_ctx, local_idx);
  5256. }
  5257. emit_operand(loader_ctx,
  5258. *(loader_ctx->frame_offset
  5259. - wasm_value_type_cell_num(local_type)));
  5260. #else
  5261. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5262. && (WASM_ENABLE_FAST_JIT == 0)
  5263. if (local_offset < 0x80) {
  5264. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  5265. if (is_32bit_type(local_type))
  5266. *p_org++ = (uint8)local_offset;
  5267. else
  5268. *p_org++ = (uint8)(local_offset | 0x80);
  5269. while (p_org < p)
  5270. *p_org++ = WASM_OP_NOP;
  5271. }
  5272. #endif
  5273. #endif
  5274. break;
  5275. }
  5276. case WASM_OP_GET_GLOBAL:
  5277. {
  5278. p_org = p - 1;
  5279. read_leb_uint32(p, p_end, global_idx);
  5280. bh_assert(global_idx < global_count);
  5281. global_type =
  5282. global_idx < module->import_global_count
  5283. ? module->import_globals[global_idx].u.global.type
  5284. : module
  5285. ->globals[global_idx
  5286. - module->import_global_count]
  5287. .type;
  5288. PUSH_TYPE(global_type);
  5289. #if WASM_ENABLE_FAST_INTERP == 0
  5290. if (global_type == VALUE_TYPE_I64
  5291. || global_type == VALUE_TYPE_F64) {
  5292. *p_org = WASM_OP_GET_GLOBAL_64;
  5293. }
  5294. #else /* else of WASM_ENABLE_FAST_INTERP */
  5295. if (is_64bit_type(global_type)) {
  5296. skip_label();
  5297. emit_label(WASM_OP_GET_GLOBAL_64);
  5298. }
  5299. emit_uint32(loader_ctx, global_idx);
  5300. PUSH_OFFSET_TYPE(global_type);
  5301. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5302. break;
  5303. }
  5304. case WASM_OP_SET_GLOBAL:
  5305. {
  5306. bool is_mutable = false;
  5307. p_org = p - 1;
  5308. read_leb_uint32(p, p_end, global_idx);
  5309. bh_assert(global_idx < global_count);
  5310. is_mutable =
  5311. global_idx < module->import_global_count
  5312. ? module->import_globals[global_idx].u.global.is_mutable
  5313. : module
  5314. ->globals[global_idx
  5315. - module->import_global_count]
  5316. .is_mutable;
  5317. bh_assert(is_mutable);
  5318. global_type =
  5319. global_idx < module->import_global_count
  5320. ? module->import_globals[global_idx].u.global.type
  5321. : module
  5322. ->globals[global_idx
  5323. - module->import_global_count]
  5324. .type;
  5325. POP_TYPE(global_type);
  5326. #if WASM_ENABLE_FAST_INTERP == 0
  5327. if (is_64bit_type(global_type)) {
  5328. *p_org = WASM_OP_SET_GLOBAL_64;
  5329. }
  5330. else if (module->aux_stack_size > 0
  5331. && global_idx == module->aux_stack_top_global_index) {
  5332. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  5333. }
  5334. #else /* else of WASM_ENABLE_FAST_INTERP */
  5335. if (is_64bit_type(global_type)) {
  5336. skip_label();
  5337. emit_label(WASM_OP_SET_GLOBAL_64);
  5338. }
  5339. else if (module->aux_stack_size > 0
  5340. && global_idx == module->aux_stack_top_global_index) {
  5341. skip_label();
  5342. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  5343. }
  5344. emit_uint32(loader_ctx, global_idx);
  5345. POP_OFFSET_TYPE(global_type);
  5346. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5347. (void)is_mutable;
  5348. break;
  5349. }
  5350. /* load */
  5351. case WASM_OP_I32_LOAD:
  5352. case WASM_OP_I32_LOAD8_S:
  5353. case WASM_OP_I32_LOAD8_U:
  5354. case WASM_OP_I32_LOAD16_S:
  5355. case WASM_OP_I32_LOAD16_U:
  5356. case WASM_OP_I64_LOAD:
  5357. case WASM_OP_I64_LOAD8_S:
  5358. case WASM_OP_I64_LOAD8_U:
  5359. case WASM_OP_I64_LOAD16_S:
  5360. case WASM_OP_I64_LOAD16_U:
  5361. case WASM_OP_I64_LOAD32_S:
  5362. case WASM_OP_I64_LOAD32_U:
  5363. case WASM_OP_F32_LOAD:
  5364. case WASM_OP_F64_LOAD:
  5365. /* store */
  5366. case WASM_OP_I32_STORE:
  5367. case WASM_OP_I32_STORE8:
  5368. case WASM_OP_I32_STORE16:
  5369. case WASM_OP_I64_STORE:
  5370. case WASM_OP_I64_STORE8:
  5371. case WASM_OP_I64_STORE16:
  5372. case WASM_OP_I64_STORE32:
  5373. case WASM_OP_F32_STORE:
  5374. case WASM_OP_F64_STORE:
  5375. {
  5376. #if WASM_ENABLE_FAST_INTERP != 0
  5377. /* change F32/F64 into I32/I64 */
  5378. if (opcode == WASM_OP_F32_LOAD) {
  5379. skip_label();
  5380. emit_label(WASM_OP_I32_LOAD);
  5381. }
  5382. else if (opcode == WASM_OP_F64_LOAD) {
  5383. skip_label();
  5384. emit_label(WASM_OP_I64_LOAD);
  5385. }
  5386. else if (opcode == WASM_OP_F32_STORE) {
  5387. skip_label();
  5388. emit_label(WASM_OP_I32_STORE);
  5389. }
  5390. else if (opcode == WASM_OP_F64_STORE) {
  5391. skip_label();
  5392. emit_label(WASM_OP_I64_STORE);
  5393. }
  5394. #endif
  5395. CHECK_MEMORY();
  5396. read_leb_uint32(p, p_end, align); /* align */
  5397. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5398. #if WASM_ENABLE_FAST_INTERP != 0
  5399. emit_uint32(loader_ctx, mem_offset);
  5400. #endif
  5401. switch (opcode) {
  5402. /* load */
  5403. case WASM_OP_I32_LOAD:
  5404. case WASM_OP_I32_LOAD8_S:
  5405. case WASM_OP_I32_LOAD8_U:
  5406. case WASM_OP_I32_LOAD16_S:
  5407. case WASM_OP_I32_LOAD16_U:
  5408. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5409. break;
  5410. case WASM_OP_I64_LOAD:
  5411. case WASM_OP_I64_LOAD8_S:
  5412. case WASM_OP_I64_LOAD8_U:
  5413. case WASM_OP_I64_LOAD16_S:
  5414. case WASM_OP_I64_LOAD16_U:
  5415. case WASM_OP_I64_LOAD32_S:
  5416. case WASM_OP_I64_LOAD32_U:
  5417. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5418. break;
  5419. case WASM_OP_F32_LOAD:
  5420. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5421. break;
  5422. case WASM_OP_F64_LOAD:
  5423. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5424. break;
  5425. /* store */
  5426. case WASM_OP_I32_STORE:
  5427. case WASM_OP_I32_STORE8:
  5428. case WASM_OP_I32_STORE16:
  5429. POP_I32();
  5430. POP_I32();
  5431. break;
  5432. case WASM_OP_I64_STORE:
  5433. case WASM_OP_I64_STORE8:
  5434. case WASM_OP_I64_STORE16:
  5435. case WASM_OP_I64_STORE32:
  5436. POP_I64();
  5437. POP_I32();
  5438. break;
  5439. case WASM_OP_F32_STORE:
  5440. POP_F32();
  5441. POP_I32();
  5442. break;
  5443. case WASM_OP_F64_STORE:
  5444. POP_F64();
  5445. POP_I32();
  5446. break;
  5447. default:
  5448. break;
  5449. }
  5450. break;
  5451. }
  5452. case WASM_OP_MEMORY_SIZE:
  5453. CHECK_MEMORY();
  5454. /* reserved byte 0x00 */
  5455. bh_assert(*p == 0x00);
  5456. p++;
  5457. PUSH_I32();
  5458. module->possible_memory_grow = true;
  5459. break;
  5460. case WASM_OP_MEMORY_GROW:
  5461. CHECK_MEMORY();
  5462. /* reserved byte 0x00 */
  5463. bh_assert(*p == 0x00);
  5464. p++;
  5465. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5466. func->has_op_memory_grow = true;
  5467. module->possible_memory_grow = true;
  5468. break;
  5469. case WASM_OP_I32_CONST:
  5470. read_leb_int32(p, p_end, i32_const);
  5471. #if WASM_ENABLE_FAST_INTERP != 0
  5472. skip_label();
  5473. disable_emit = true;
  5474. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  5475. if (operand_offset == 0) {
  5476. disable_emit = false;
  5477. emit_label(WASM_OP_I32_CONST);
  5478. emit_uint32(loader_ctx, i32_const);
  5479. }
  5480. #else
  5481. (void)i32_const;
  5482. #endif
  5483. PUSH_I32();
  5484. break;
  5485. case WASM_OP_I64_CONST:
  5486. read_leb_int64(p, p_end, i64_const);
  5487. #if WASM_ENABLE_FAST_INTERP != 0
  5488. skip_label();
  5489. disable_emit = true;
  5490. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  5491. if (operand_offset == 0) {
  5492. disable_emit = false;
  5493. emit_label(WASM_OP_I64_CONST);
  5494. emit_uint64(loader_ctx, i64_const);
  5495. }
  5496. #endif
  5497. PUSH_I64();
  5498. break;
  5499. case WASM_OP_F32_CONST:
  5500. p += sizeof(float32);
  5501. #if WASM_ENABLE_FAST_INTERP != 0
  5502. skip_label();
  5503. disable_emit = true;
  5504. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  5505. sizeof(float32));
  5506. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  5507. if (operand_offset == 0) {
  5508. disable_emit = false;
  5509. emit_label(WASM_OP_F32_CONST);
  5510. emit_float32(loader_ctx, f32_const);
  5511. }
  5512. #endif
  5513. PUSH_F32();
  5514. break;
  5515. case WASM_OP_F64_CONST:
  5516. p += sizeof(float64);
  5517. #if WASM_ENABLE_FAST_INTERP != 0
  5518. skip_label();
  5519. disable_emit = true;
  5520. /* Some MCU may require 8-byte align */
  5521. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  5522. sizeof(float64));
  5523. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  5524. if (operand_offset == 0) {
  5525. disable_emit = false;
  5526. emit_label(WASM_OP_F64_CONST);
  5527. emit_float64(loader_ctx, f64_const);
  5528. }
  5529. #endif
  5530. PUSH_F64();
  5531. break;
  5532. case WASM_OP_I32_EQZ:
  5533. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5534. break;
  5535. case WASM_OP_I32_EQ:
  5536. case WASM_OP_I32_NE:
  5537. case WASM_OP_I32_LT_S:
  5538. case WASM_OP_I32_LT_U:
  5539. case WASM_OP_I32_GT_S:
  5540. case WASM_OP_I32_GT_U:
  5541. case WASM_OP_I32_LE_S:
  5542. case WASM_OP_I32_LE_U:
  5543. case WASM_OP_I32_GE_S:
  5544. case WASM_OP_I32_GE_U:
  5545. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5546. break;
  5547. case WASM_OP_I64_EQZ:
  5548. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5549. break;
  5550. case WASM_OP_I64_EQ:
  5551. case WASM_OP_I64_NE:
  5552. case WASM_OP_I64_LT_S:
  5553. case WASM_OP_I64_LT_U:
  5554. case WASM_OP_I64_GT_S:
  5555. case WASM_OP_I64_GT_U:
  5556. case WASM_OP_I64_LE_S:
  5557. case WASM_OP_I64_LE_U:
  5558. case WASM_OP_I64_GE_S:
  5559. case WASM_OP_I64_GE_U:
  5560. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5561. break;
  5562. case WASM_OP_F32_EQ:
  5563. case WASM_OP_F32_NE:
  5564. case WASM_OP_F32_LT:
  5565. case WASM_OP_F32_GT:
  5566. case WASM_OP_F32_LE:
  5567. case WASM_OP_F32_GE:
  5568. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5569. break;
  5570. case WASM_OP_F64_EQ:
  5571. case WASM_OP_F64_NE:
  5572. case WASM_OP_F64_LT:
  5573. case WASM_OP_F64_GT:
  5574. case WASM_OP_F64_LE:
  5575. case WASM_OP_F64_GE:
  5576. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5577. break;
  5578. case WASM_OP_I32_CLZ:
  5579. case WASM_OP_I32_CTZ:
  5580. case WASM_OP_I32_POPCNT:
  5581. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5582. break;
  5583. case WASM_OP_I32_ADD:
  5584. case WASM_OP_I32_SUB:
  5585. case WASM_OP_I32_MUL:
  5586. case WASM_OP_I32_DIV_S:
  5587. case WASM_OP_I32_DIV_U:
  5588. case WASM_OP_I32_REM_S:
  5589. case WASM_OP_I32_REM_U:
  5590. case WASM_OP_I32_AND:
  5591. case WASM_OP_I32_OR:
  5592. case WASM_OP_I32_XOR:
  5593. case WASM_OP_I32_SHL:
  5594. case WASM_OP_I32_SHR_S:
  5595. case WASM_OP_I32_SHR_U:
  5596. case WASM_OP_I32_ROTL:
  5597. case WASM_OP_I32_ROTR:
  5598. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5599. break;
  5600. case WASM_OP_I64_CLZ:
  5601. case WASM_OP_I64_CTZ:
  5602. case WASM_OP_I64_POPCNT:
  5603. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5604. break;
  5605. case WASM_OP_I64_ADD:
  5606. case WASM_OP_I64_SUB:
  5607. case WASM_OP_I64_MUL:
  5608. case WASM_OP_I64_DIV_S:
  5609. case WASM_OP_I64_DIV_U:
  5610. case WASM_OP_I64_REM_S:
  5611. case WASM_OP_I64_REM_U:
  5612. case WASM_OP_I64_AND:
  5613. case WASM_OP_I64_OR:
  5614. case WASM_OP_I64_XOR:
  5615. case WASM_OP_I64_SHL:
  5616. case WASM_OP_I64_SHR_S:
  5617. case WASM_OP_I64_SHR_U:
  5618. case WASM_OP_I64_ROTL:
  5619. case WASM_OP_I64_ROTR:
  5620. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5621. break;
  5622. case WASM_OP_F32_ABS:
  5623. case WASM_OP_F32_NEG:
  5624. case WASM_OP_F32_CEIL:
  5625. case WASM_OP_F32_FLOOR:
  5626. case WASM_OP_F32_TRUNC:
  5627. case WASM_OP_F32_NEAREST:
  5628. case WASM_OP_F32_SQRT:
  5629. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5630. break;
  5631. case WASM_OP_F32_ADD:
  5632. case WASM_OP_F32_SUB:
  5633. case WASM_OP_F32_MUL:
  5634. case WASM_OP_F32_DIV:
  5635. case WASM_OP_F32_MIN:
  5636. case WASM_OP_F32_MAX:
  5637. case WASM_OP_F32_COPYSIGN:
  5638. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5639. break;
  5640. case WASM_OP_F64_ABS:
  5641. case WASM_OP_F64_NEG:
  5642. case WASM_OP_F64_CEIL:
  5643. case WASM_OP_F64_FLOOR:
  5644. case WASM_OP_F64_TRUNC:
  5645. case WASM_OP_F64_NEAREST:
  5646. case WASM_OP_F64_SQRT:
  5647. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5648. break;
  5649. case WASM_OP_F64_ADD:
  5650. case WASM_OP_F64_SUB:
  5651. case WASM_OP_F64_MUL:
  5652. case WASM_OP_F64_DIV:
  5653. case WASM_OP_F64_MIN:
  5654. case WASM_OP_F64_MAX:
  5655. case WASM_OP_F64_COPYSIGN:
  5656. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5657. break;
  5658. case WASM_OP_I32_WRAP_I64:
  5659. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5660. break;
  5661. case WASM_OP_I32_TRUNC_S_F32:
  5662. case WASM_OP_I32_TRUNC_U_F32:
  5663. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5664. break;
  5665. case WASM_OP_I32_TRUNC_S_F64:
  5666. case WASM_OP_I32_TRUNC_U_F64:
  5667. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5668. break;
  5669. case WASM_OP_I64_EXTEND_S_I32:
  5670. case WASM_OP_I64_EXTEND_U_I32:
  5671. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5672. break;
  5673. case WASM_OP_I64_TRUNC_S_F32:
  5674. case WASM_OP_I64_TRUNC_U_F32:
  5675. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  5676. break;
  5677. case WASM_OP_I64_TRUNC_S_F64:
  5678. case WASM_OP_I64_TRUNC_U_F64:
  5679. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5680. break;
  5681. case WASM_OP_F32_CONVERT_S_I32:
  5682. case WASM_OP_F32_CONVERT_U_I32:
  5683. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5684. break;
  5685. case WASM_OP_F32_CONVERT_S_I64:
  5686. case WASM_OP_F32_CONVERT_U_I64:
  5687. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  5688. break;
  5689. case WASM_OP_F32_DEMOTE_F64:
  5690. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  5691. break;
  5692. case WASM_OP_F64_CONVERT_S_I32:
  5693. case WASM_OP_F64_CONVERT_U_I32:
  5694. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5695. break;
  5696. case WASM_OP_F64_CONVERT_S_I64:
  5697. case WASM_OP_F64_CONVERT_U_I64:
  5698. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  5699. break;
  5700. case WASM_OP_F64_PROMOTE_F32:
  5701. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  5702. break;
  5703. case WASM_OP_I32_REINTERPRET_F32:
  5704. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5705. break;
  5706. case WASM_OP_I64_REINTERPRET_F64:
  5707. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5708. break;
  5709. case WASM_OP_F32_REINTERPRET_I32:
  5710. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5711. break;
  5712. case WASM_OP_F64_REINTERPRET_I64:
  5713. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  5714. break;
  5715. case WASM_OP_I32_EXTEND8_S:
  5716. case WASM_OP_I32_EXTEND16_S:
  5717. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5718. break;
  5719. case WASM_OP_I64_EXTEND8_S:
  5720. case WASM_OP_I64_EXTEND16_S:
  5721. case WASM_OP_I64_EXTEND32_S:
  5722. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5723. break;
  5724. case WASM_OP_MISC_PREFIX:
  5725. {
  5726. uint32 opcode1;
  5727. read_leb_uint32(p, p_end, opcode1);
  5728. #if WASM_ENABLE_FAST_INTERP != 0
  5729. emit_byte(loader_ctx, ((uint8)opcode1));
  5730. #endif
  5731. switch (opcode1) {
  5732. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5733. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5734. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5735. break;
  5736. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5737. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5738. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5739. break;
  5740. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5741. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5742. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  5743. break;
  5744. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5745. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5746. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5747. break;
  5748. #if WASM_ENABLE_BULK_MEMORY != 0
  5749. case WASM_OP_MEMORY_INIT:
  5750. read_leb_uint32(p, p_end, segment_index);
  5751. #if WASM_ENABLE_FAST_INTERP != 0
  5752. emit_uint32(loader_ctx, segment_index);
  5753. #endif
  5754. bh_assert(module->import_memory_count
  5755. + module->memory_count
  5756. > 0);
  5757. bh_assert(*p == 0x00);
  5758. p++;
  5759. bh_assert(segment_index < module->data_seg_count);
  5760. bh_assert(module->data_seg_count1 > 0);
  5761. POP_I32();
  5762. POP_I32();
  5763. POP_I32();
  5764. break;
  5765. case WASM_OP_DATA_DROP:
  5766. read_leb_uint32(p, p_end, segment_index);
  5767. #if WASM_ENABLE_FAST_INTERP != 0
  5768. emit_uint32(loader_ctx, segment_index);
  5769. #endif
  5770. bh_assert(segment_index < module->data_seg_count);
  5771. bh_assert(module->data_seg_count1 > 0);
  5772. break;
  5773. case WASM_OP_MEMORY_COPY:
  5774. /* both src and dst memory index should be 0 */
  5775. bh_assert(*(int16 *)p != 0x0000);
  5776. p += 2;
  5777. bh_assert(module->import_memory_count
  5778. + module->memory_count
  5779. > 0);
  5780. POP_I32();
  5781. POP_I32();
  5782. POP_I32();
  5783. break;
  5784. case WASM_OP_MEMORY_FILL:
  5785. bh_assert(*p == 0);
  5786. p++;
  5787. bh_assert(module->import_memory_count
  5788. + module->memory_count
  5789. > 0);
  5790. POP_I32();
  5791. POP_I32();
  5792. POP_I32();
  5793. break;
  5794. #endif /* WASM_ENABLE_BULK_MEMORY */
  5795. #if WASM_ENABLE_REF_TYPES != 0
  5796. case WASM_OP_TABLE_INIT:
  5797. {
  5798. uint8 seg_ref_type, tbl_ref_type;
  5799. uint32 table_seg_idx, table_idx;
  5800. read_leb_uint32(p, p_end, table_seg_idx);
  5801. read_leb_uint32(p, p_end, table_idx);
  5802. if (!get_table_elem_type(module, table_idx,
  5803. &tbl_ref_type, error_buf,
  5804. error_buf_size))
  5805. goto fail;
  5806. if (!get_table_seg_elem_type(module, table_seg_idx,
  5807. &seg_ref_type, error_buf,
  5808. error_buf_size))
  5809. goto fail;
  5810. if (seg_ref_type != tbl_ref_type) {
  5811. set_error_buf(error_buf, error_buf_size,
  5812. "type mismatch");
  5813. goto fail;
  5814. }
  5815. #if WASM_ENABLE_FAST_INTERP != 0
  5816. emit_uint32(loader_ctx, table_seg_idx);
  5817. emit_uint32(loader_ctx, table_idx);
  5818. #endif
  5819. POP_I32();
  5820. POP_I32();
  5821. POP_I32();
  5822. break;
  5823. }
  5824. case WASM_OP_ELEM_DROP:
  5825. {
  5826. uint32 table_seg_idx;
  5827. read_leb_uint32(p, p_end, table_seg_idx);
  5828. if (!get_table_seg_elem_type(module, table_seg_idx,
  5829. NULL, error_buf,
  5830. error_buf_size))
  5831. goto fail;
  5832. #if WASM_ENABLE_FAST_INTERP != 0
  5833. emit_uint32(loader_ctx, table_seg_idx);
  5834. #endif
  5835. break;
  5836. }
  5837. case WASM_OP_TABLE_COPY:
  5838. {
  5839. uint8 src_ref_type, dst_ref_type;
  5840. uint32 src_tbl_idx, dst_tbl_idx;
  5841. read_leb_uint32(p, p_end, src_tbl_idx);
  5842. if (!get_table_elem_type(module, src_tbl_idx,
  5843. &src_ref_type, error_buf,
  5844. error_buf_size))
  5845. goto fail;
  5846. read_leb_uint32(p, p_end, dst_tbl_idx);
  5847. if (!get_table_elem_type(module, dst_tbl_idx,
  5848. &dst_ref_type, error_buf,
  5849. error_buf_size))
  5850. goto fail;
  5851. if (src_ref_type != dst_ref_type) {
  5852. set_error_buf(error_buf, error_buf_size,
  5853. "type mismatch");
  5854. goto fail;
  5855. }
  5856. #if WASM_ENABLE_FAST_INTERP != 0
  5857. emit_uint32(loader_ctx, src_tbl_idx);
  5858. emit_uint32(loader_ctx, dst_tbl_idx);
  5859. #endif
  5860. POP_I32();
  5861. POP_I32();
  5862. POP_I32();
  5863. break;
  5864. }
  5865. case WASM_OP_TABLE_SIZE:
  5866. {
  5867. uint32 table_idx;
  5868. read_leb_uint32(p, p_end, table_idx);
  5869. /* TODO: shall we create a new function to check
  5870. table idx instead of using below function? */
  5871. if (!get_table_elem_type(module, table_idx, NULL,
  5872. error_buf, error_buf_size))
  5873. goto fail;
  5874. #if WASM_ENABLE_FAST_INTERP != 0
  5875. emit_uint32(loader_ctx, table_idx);
  5876. #endif
  5877. PUSH_I32();
  5878. break;
  5879. }
  5880. case WASM_OP_TABLE_GROW:
  5881. case WASM_OP_TABLE_FILL:
  5882. {
  5883. uint8 decl_ref_type;
  5884. uint32 table_idx;
  5885. read_leb_uint32(p, p_end, table_idx);
  5886. if (!get_table_elem_type(module, table_idx,
  5887. &decl_ref_type, error_buf,
  5888. error_buf_size))
  5889. goto fail;
  5890. if (opcode1 == WASM_OP_TABLE_GROW) {
  5891. if (table_idx < module->import_table_count) {
  5892. module->import_tables[table_idx]
  5893. .u.table.possible_grow = true;
  5894. }
  5895. else {
  5896. module
  5897. ->tables[table_idx
  5898. - module->import_table_count]
  5899. .possible_grow = true;
  5900. }
  5901. }
  5902. #if WASM_ENABLE_FAST_INTERP != 0
  5903. emit_uint32(loader_ctx, table_idx);
  5904. #endif
  5905. POP_I32();
  5906. #if WASM_ENABLE_FAST_INTERP != 0
  5907. POP_OFFSET_TYPE(decl_ref_type);
  5908. #endif
  5909. POP_TYPE(decl_ref_type);
  5910. if (opcode1 == WASM_OP_TABLE_GROW)
  5911. PUSH_I32();
  5912. else
  5913. POP_I32();
  5914. break;
  5915. }
  5916. #endif /* WASM_ENABLE_REF_TYPES */
  5917. default:
  5918. bh_assert(0);
  5919. break;
  5920. }
  5921. break;
  5922. }
  5923. #if WASM_ENABLE_SHARED_MEMORY != 0
  5924. case WASM_OP_ATOMIC_PREFIX:
  5925. {
  5926. opcode = read_uint8(p);
  5927. #if WASM_ENABLE_FAST_INTERP != 0
  5928. emit_byte(loader_ctx, opcode);
  5929. #endif
  5930. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5931. CHECK_MEMORY();
  5932. read_leb_uint32(p, p_end, align); /* align */
  5933. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5934. #if WASM_ENABLE_FAST_INTERP != 0
  5935. emit_uint32(loader_ctx, mem_offset);
  5936. #endif
  5937. }
  5938. switch (opcode) {
  5939. case WASM_OP_ATOMIC_NOTIFY:
  5940. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5941. break;
  5942. case WASM_OP_ATOMIC_WAIT32:
  5943. POP_I64();
  5944. POP_I32();
  5945. POP_I32();
  5946. PUSH_I32();
  5947. break;
  5948. case WASM_OP_ATOMIC_WAIT64:
  5949. POP_I64();
  5950. POP_I64();
  5951. POP_I32();
  5952. PUSH_I32();
  5953. break;
  5954. case WASM_OP_ATOMIC_FENCE:
  5955. /* reserved byte 0x00 */
  5956. bh_assert(*p == 0x00);
  5957. p++;
  5958. break;
  5959. case WASM_OP_ATOMIC_I32_LOAD:
  5960. case WASM_OP_ATOMIC_I32_LOAD8_U:
  5961. case WASM_OP_ATOMIC_I32_LOAD16_U:
  5962. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5963. break;
  5964. case WASM_OP_ATOMIC_I32_STORE:
  5965. case WASM_OP_ATOMIC_I32_STORE8:
  5966. case WASM_OP_ATOMIC_I32_STORE16:
  5967. POP_I32();
  5968. POP_I32();
  5969. break;
  5970. case WASM_OP_ATOMIC_I64_LOAD:
  5971. case WASM_OP_ATOMIC_I64_LOAD8_U:
  5972. case WASM_OP_ATOMIC_I64_LOAD16_U:
  5973. case WASM_OP_ATOMIC_I64_LOAD32_U:
  5974. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5975. break;
  5976. case WASM_OP_ATOMIC_I64_STORE:
  5977. case WASM_OP_ATOMIC_I64_STORE8:
  5978. case WASM_OP_ATOMIC_I64_STORE16:
  5979. case WASM_OP_ATOMIC_I64_STORE32:
  5980. POP_I64();
  5981. POP_I32();
  5982. break;
  5983. case WASM_OP_ATOMIC_RMW_I32_ADD:
  5984. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  5985. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  5986. case WASM_OP_ATOMIC_RMW_I32_SUB:
  5987. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  5988. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  5989. case WASM_OP_ATOMIC_RMW_I32_AND:
  5990. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  5991. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  5992. case WASM_OP_ATOMIC_RMW_I32_OR:
  5993. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  5994. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  5995. case WASM_OP_ATOMIC_RMW_I32_XOR:
  5996. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  5997. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  5998. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  5999. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  6000. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  6001. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6002. break;
  6003. case WASM_OP_ATOMIC_RMW_I64_ADD:
  6004. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  6005. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  6006. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  6007. case WASM_OP_ATOMIC_RMW_I64_SUB:
  6008. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  6009. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  6010. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  6011. case WASM_OP_ATOMIC_RMW_I64_AND:
  6012. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  6013. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  6014. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  6015. case WASM_OP_ATOMIC_RMW_I64_OR:
  6016. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  6017. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  6018. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  6019. case WASM_OP_ATOMIC_RMW_I64_XOR:
  6020. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  6021. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  6022. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  6023. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  6024. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  6025. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  6026. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  6027. POP_I64();
  6028. POP_I32();
  6029. PUSH_I64();
  6030. break;
  6031. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  6032. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  6033. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  6034. POP_I32();
  6035. POP_I32();
  6036. POP_I32();
  6037. PUSH_I32();
  6038. break;
  6039. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  6040. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  6041. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  6042. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  6043. POP_I64();
  6044. POP_I64();
  6045. POP_I32();
  6046. PUSH_I64();
  6047. break;
  6048. default:
  6049. bh_assert(0);
  6050. break;
  6051. }
  6052. break;
  6053. }
  6054. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6055. default:
  6056. bh_assert(0);
  6057. break;
  6058. }
  6059. #if WASM_ENABLE_FAST_INTERP != 0
  6060. last_op = opcode;
  6061. #endif
  6062. }
  6063. if (loader_ctx->csp_num > 0) {
  6064. set_error_buf(error_buf, error_buf_size,
  6065. "function body must end with END opcode");
  6066. goto fail;
  6067. }
  6068. #if WASM_ENABLE_FAST_INTERP != 0
  6069. if (loader_ctx->p_code_compiled == NULL)
  6070. goto re_scan;
  6071. func->const_cell_num = loader_ctx->const_cell_num;
  6072. if (func->const_cell_num > 0) {
  6073. int32 j;
  6074. if (!(func->consts = func_const = loader_malloc(
  6075. func->const_cell_num * 4, error_buf, error_buf_size)))
  6076. goto fail;
  6077. func_const_end = func->consts + func->const_cell_num * 4;
  6078. /* reverse the const buf */
  6079. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  6080. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  6081. if (c->value_type == VALUE_TYPE_F64
  6082. || c->value_type == VALUE_TYPE_I64) {
  6083. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6084. &(c->value.f64), (uint32)sizeof(int64));
  6085. func_const += sizeof(int64);
  6086. }
  6087. else {
  6088. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6089. &(c->value.f32), (uint32)sizeof(int32));
  6090. func_const += sizeof(int32);
  6091. }
  6092. }
  6093. }
  6094. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  6095. - loader_ctx->start_dynamic_offset + 1;
  6096. #else
  6097. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  6098. #endif
  6099. func->max_block_num = loader_ctx->max_csp_num;
  6100. return_value = true;
  6101. fail:
  6102. wasm_loader_ctx_destroy(loader_ctx);
  6103. (void)u8;
  6104. (void)u32;
  6105. (void)i32;
  6106. (void)i64_const;
  6107. (void)global_count;
  6108. (void)local_count;
  6109. (void)local_offset;
  6110. (void)p_org;
  6111. (void)mem_offset;
  6112. (void)align;
  6113. #if WASM_ENABLE_BULK_MEMORY != 0
  6114. (void)segment_index;
  6115. #endif
  6116. return return_value;
  6117. }