wasm_loader.c 320 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960
  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. #if WASM_ENABLE_DEBUG_INTERP != 0
  13. #include "../libraries/debug-engine/debug_engine.h"
  14. #endif
  15. /* Read a value of given type from the address pointed to by the given
  16. pointer and increase the pointer to the position just after the
  17. value being read. */
  18. #define TEMPLATE_READ_VALUE(Type, p) \
  19. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  20. static void
  21. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  22. {
  23. if (error_buf != NULL) {
  24. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  25. string);
  26. }
  27. }
  28. static void
  29. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  30. {
  31. va_list args;
  32. char buf[128];
  33. if (error_buf != NULL) {
  34. va_start(args, format);
  35. vsnprintf(buf, sizeof(buf), format, args);
  36. va_end(args);
  37. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  38. }
  39. }
  40. static bool
  41. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  42. char *error_buf, uint32 error_buf_size)
  43. {
  44. if (buf + length < buf || buf + length > buf_end) {
  45. set_error_buf(error_buf, error_buf_size,
  46. "unexpected end of section or function");
  47. return false;
  48. }
  49. return true;
  50. }
  51. static bool
  52. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  53. char *error_buf, uint32 error_buf_size)
  54. {
  55. if (buf + length < buf || buf + length > buf_end) {
  56. set_error_buf(error_buf, error_buf_size, "unexpected end");
  57. return false;
  58. }
  59. return true;
  60. }
  61. #define CHECK_BUF(buf, buf_end, length) \
  62. do { \
  63. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  64. goto fail; \
  65. } \
  66. } while (0)
  67. #define CHECK_BUF1(buf, buf_end, length) \
  68. do { \
  69. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  70. goto fail; \
  71. } \
  72. } while (0)
  73. #define skip_leb(p) while (*p++ & 0x80)
  74. #define skip_leb_int64(p, p_end) skip_leb(p)
  75. #define skip_leb_uint32(p, p_end) skip_leb(p)
  76. #define skip_leb_int32(p, p_end) skip_leb(p)
  77. static bool
  78. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  79. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  80. {
  81. const uint8 *buf = *p_buf;
  82. uint64 result = 0;
  83. uint32 shift = 0;
  84. uint32 offset = 0, bcnt = 0;
  85. uint64 byte;
  86. while (true) {
  87. /* uN or SN must not exceed ceil(N/7) bytes */
  88. if (bcnt + 1 > (maxbits + 6) / 7) {
  89. set_error_buf(error_buf, error_buf_size,
  90. "integer representation too long");
  91. return false;
  92. }
  93. CHECK_BUF(buf, buf_end, offset + 1);
  94. byte = buf[offset];
  95. offset += 1;
  96. result |= ((byte & 0x7f) << shift);
  97. shift += 7;
  98. bcnt += 1;
  99. if ((byte & 0x80) == 0) {
  100. break;
  101. }
  102. }
  103. if (!sign && maxbits == 32 && shift >= maxbits) {
  104. /* The top bits set represent values > 32 bits */
  105. if (((uint8)byte) & 0xf0)
  106. goto fail_integer_too_large;
  107. }
  108. else if (sign && maxbits == 32) {
  109. if (shift < maxbits) {
  110. /* Sign extend, second highest bit is the sign bit */
  111. if ((uint8)byte & 0x40)
  112. result |= (~((uint64)0)) << shift;
  113. }
  114. else {
  115. /* The top bits should be a sign-extension of the sign bit */
  116. bool sign_bit_set = ((uint8)byte) & 0x8;
  117. int top_bits = ((uint8)byte) & 0xf0;
  118. if ((sign_bit_set && top_bits != 0x70)
  119. || (!sign_bit_set && top_bits != 0))
  120. goto fail_integer_too_large;
  121. }
  122. }
  123. else if (sign && maxbits == 64) {
  124. if (shift < maxbits) {
  125. /* Sign extend, second highest bit is the sign bit */
  126. if ((uint8)byte & 0x40)
  127. result |= (~((uint64)0)) << shift;
  128. }
  129. else {
  130. /* The top bits should be a sign-extension of the sign bit */
  131. bool sign_bit_set = ((uint8)byte) & 0x1;
  132. int top_bits = ((uint8)byte) & 0xfe;
  133. if ((sign_bit_set && top_bits != 0x7e)
  134. || (!sign_bit_set && top_bits != 0))
  135. goto fail_integer_too_large;
  136. }
  137. }
  138. *p_buf += offset;
  139. *p_result = result;
  140. return true;
  141. fail_integer_too_large:
  142. set_error_buf(error_buf, error_buf_size, "integer too large");
  143. fail:
  144. return false;
  145. }
  146. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  147. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  148. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  149. #define read_leb_int64(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  153. error_buf_size)) \
  154. goto fail; \
  155. res = (int64)res64; \
  156. } while (0)
  157. #define read_leb_uint32(p, p_end, res) \
  158. do { \
  159. uint64 res64; \
  160. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  161. error_buf_size)) \
  162. goto fail; \
  163. res = (uint32)res64; \
  164. } while (0)
  165. #define read_leb_int32(p, p_end, res) \
  166. do { \
  167. uint64 res64; \
  168. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  169. error_buf_size)) \
  170. goto fail; \
  171. res = (int32)res64; \
  172. } while (0)
  173. static char *
  174. type2str(uint8 type)
  175. {
  176. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  177. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  178. return type_str[type - VALUE_TYPE_V128];
  179. else if (type == VALUE_TYPE_FUNCREF)
  180. return "funcref";
  181. else if (type == VALUE_TYPE_EXTERNREF)
  182. return "externref";
  183. else
  184. return "unknown type";
  185. }
  186. static bool
  187. is_32bit_type(uint8 type)
  188. {
  189. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  190. #if WASM_ENABLE_REF_TYPES != 0
  191. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  192. #endif
  193. )
  194. return true;
  195. return false;
  196. }
  197. static bool
  198. is_64bit_type(uint8 type)
  199. {
  200. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  201. return true;
  202. return false;
  203. }
  204. static bool
  205. is_value_type(uint8 type)
  206. {
  207. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  208. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  209. #if WASM_ENABLE_REF_TYPES != 0
  210. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  211. #endif
  212. #if WASM_ENABLE_SIMD != 0
  213. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  214. || type == VALUE_TYPE_V128
  215. #endif
  216. #endif
  217. )
  218. return true;
  219. return false;
  220. }
  221. static bool
  222. is_byte_a_type(uint8 type)
  223. {
  224. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  225. }
  226. #if WASM_ENABLE_SIMD != 0
  227. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  228. static V128
  229. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  230. {
  231. V128 result;
  232. uint8 i;
  233. for (i = 0; i != 16; ++i) {
  234. result.i8x16[i] = read_uint8(p_buf);
  235. }
  236. return result;
  237. }
  238. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  239. #endif /* end of WASM_ENABLE_SIMD */
  240. static void *
  241. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  242. {
  243. void *mem;
  244. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  245. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  246. return NULL;
  247. }
  248. memset(mem, 0, (uint32)size);
  249. return mem;
  250. }
  251. static bool
  252. check_utf8_str(const uint8 *str, uint32 len)
  253. {
  254. /* The valid ranges are taken from page 125, below link
  255. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  256. const uint8 *p = str, *p_end = str + len;
  257. uint8 chr;
  258. while (p < p_end) {
  259. chr = *p;
  260. if (chr < 0x80) {
  261. p++;
  262. }
  263. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  264. if (p[1] < 0x80 || p[1] > 0xBF) {
  265. return false;
  266. }
  267. p += 2;
  268. }
  269. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  270. if (chr == 0xE0) {
  271. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  272. return false;
  273. }
  274. }
  275. else if (chr == 0xED) {
  276. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  277. return false;
  278. }
  279. }
  280. else if (chr >= 0xE1 && chr <= 0xEF) {
  281. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  282. return false;
  283. }
  284. }
  285. p += 3;
  286. }
  287. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  288. if (chr == 0xF0) {
  289. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  290. || p[3] < 0x80 || p[3] > 0xBF) {
  291. return false;
  292. }
  293. }
  294. else if (chr >= 0xF1 && chr <= 0xF3) {
  295. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  296. || p[3] < 0x80 || p[3] > 0xBF) {
  297. return false;
  298. }
  299. }
  300. else if (chr == 0xF4) {
  301. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  302. || p[3] < 0x80 || p[3] > 0xBF) {
  303. return false;
  304. }
  305. }
  306. p += 4;
  307. }
  308. else {
  309. return false;
  310. }
  311. }
  312. return (p == p_end);
  313. }
  314. static char *
  315. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  316. bool is_load_from_file_buf, char *error_buf,
  317. uint32 error_buf_size)
  318. {
  319. StringNode *node, *node_next;
  320. if (!check_utf8_str(str, len)) {
  321. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  322. return NULL;
  323. }
  324. if (len == 0) {
  325. return "";
  326. }
  327. else if (is_load_from_file_buf) {
  328. /* As the file buffer can be referred to after loading, we use
  329. the previous byte of leb encoded size to adjust the string:
  330. move string 1 byte backward and then append '\0' */
  331. char *c_str = (char *)str - 1;
  332. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  333. c_str[len] = '\0';
  334. return c_str;
  335. }
  336. /* Search const str list */
  337. node = module->const_str_list;
  338. while (node) {
  339. node_next = node->next;
  340. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  341. break;
  342. node = node_next;
  343. }
  344. if (node) {
  345. return node->str;
  346. }
  347. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  348. error_buf_size))) {
  349. return NULL;
  350. }
  351. node->str = ((char *)node) + sizeof(StringNode);
  352. bh_memcpy_s(node->str, len + 1, str, len);
  353. node->str[len] = '\0';
  354. if (!module->const_str_list) {
  355. /* set as head */
  356. module->const_str_list = node;
  357. node->next = NULL;
  358. }
  359. else {
  360. /* insert it */
  361. node->next = module->const_str_list;
  362. module->const_str_list = node;
  363. }
  364. return node->str;
  365. }
  366. static bool
  367. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  368. InitializerExpression *init_expr, uint8 type, char *error_buf,
  369. uint32 error_buf_size)
  370. {
  371. const uint8 *p = *p_buf, *p_end = buf_end;
  372. uint8 flag, end_byte, *p_float;
  373. uint32 i;
  374. CHECK_BUF(p, p_end, 1);
  375. init_expr->init_expr_type = read_uint8(p);
  376. flag = init_expr->init_expr_type;
  377. switch (flag) {
  378. /* i32.const */
  379. case INIT_EXPR_TYPE_I32_CONST:
  380. if (type != VALUE_TYPE_I32)
  381. goto fail_type_mismatch;
  382. read_leb_int32(p, p_end, init_expr->u.i32);
  383. break;
  384. /* i64.const */
  385. case INIT_EXPR_TYPE_I64_CONST:
  386. if (type != VALUE_TYPE_I64)
  387. goto fail_type_mismatch;
  388. read_leb_int64(p, p_end, init_expr->u.i64);
  389. break;
  390. /* f32.const */
  391. case INIT_EXPR_TYPE_F32_CONST:
  392. if (type != VALUE_TYPE_F32)
  393. goto fail_type_mismatch;
  394. CHECK_BUF(p, p_end, 4);
  395. p_float = (uint8 *)&init_expr->u.f32;
  396. for (i = 0; i < sizeof(float32); i++)
  397. *p_float++ = *p++;
  398. break;
  399. /* f64.const */
  400. case INIT_EXPR_TYPE_F64_CONST:
  401. if (type != VALUE_TYPE_F64)
  402. goto fail_type_mismatch;
  403. CHECK_BUF(p, p_end, 8);
  404. p_float = (uint8 *)&init_expr->u.f64;
  405. for (i = 0; i < sizeof(float64); i++)
  406. *p_float++ = *p++;
  407. break;
  408. #if WASM_ENABLE_SIMD != 0
  409. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  410. case INIT_EXPR_TYPE_V128_CONST:
  411. {
  412. uint8 flag;
  413. uint64 high, low;
  414. if (type != VALUE_TYPE_V128)
  415. goto fail_type_mismatch;
  416. flag = read_uint8(p);
  417. (void)flag;
  418. CHECK_BUF(p, p_end, 16);
  419. wasm_runtime_read_v128(p, &high, &low);
  420. p += 16;
  421. init_expr->u.v128.i64x2[0] = high;
  422. init_expr->u.v128.i64x2[1] = low;
  423. break;
  424. }
  425. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  426. #endif /* end of WASM_ENABLE_SIMD */
  427. #if WASM_ENABLE_REF_TYPES != 0
  428. case INIT_EXPR_TYPE_FUNCREF_CONST:
  429. {
  430. if (type != VALUE_TYPE_FUNCREF)
  431. goto fail_type_mismatch;
  432. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  433. break;
  434. }
  435. case INIT_EXPR_TYPE_REFNULL_CONST:
  436. {
  437. uint8 reftype;
  438. CHECK_BUF(p, p_end, 1);
  439. reftype = read_uint8(p);
  440. if (reftype != type)
  441. goto fail_type_mismatch;
  442. init_expr->u.ref_index = NULL_REF;
  443. break;
  444. }
  445. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  446. /* get_global */
  447. case INIT_EXPR_TYPE_GET_GLOBAL:
  448. read_leb_uint32(p, p_end, init_expr->u.global_index);
  449. break;
  450. default:
  451. {
  452. set_error_buf(error_buf, error_buf_size,
  453. "illegal opcode "
  454. "or constant expression required "
  455. "or type mismatch");
  456. goto fail;
  457. }
  458. }
  459. CHECK_BUF(p, p_end, 1);
  460. end_byte = read_uint8(p);
  461. if (end_byte != 0x0b)
  462. goto fail_type_mismatch;
  463. *p_buf = p;
  464. return true;
  465. fail_type_mismatch:
  466. set_error_buf(error_buf, error_buf_size,
  467. "type mismatch or constant expression required");
  468. fail:
  469. return false;
  470. }
  471. static bool
  472. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  473. char *error_buf, uint32 error_buf_size)
  474. {
  475. const uint8 *p = buf, *p_end = buf_end, *p_org;
  476. uint32 type_count, param_count, result_count, i, j;
  477. uint32 param_cell_num, ret_cell_num;
  478. uint64 total_size;
  479. uint8 flag;
  480. WASMType *type;
  481. read_leb_uint32(p, p_end, type_count);
  482. if (type_count) {
  483. module->type_count = type_count;
  484. total_size = sizeof(WASMType *) * (uint64)type_count;
  485. if (!(module->types =
  486. loader_malloc(total_size, error_buf, error_buf_size))) {
  487. return false;
  488. }
  489. for (i = 0; i < type_count; i++) {
  490. CHECK_BUF(p, p_end, 1);
  491. flag = read_uint8(p);
  492. if (flag != 0x60) {
  493. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  494. return false;
  495. }
  496. read_leb_uint32(p, p_end, param_count);
  497. /* Resolve param count and result count firstly */
  498. p_org = p;
  499. CHECK_BUF(p, p_end, param_count);
  500. p += param_count;
  501. read_leb_uint32(p, p_end, result_count);
  502. CHECK_BUF(p, p_end, result_count);
  503. p = p_org;
  504. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  505. set_error_buf(error_buf, error_buf_size,
  506. "param count or result count too large");
  507. return false;
  508. }
  509. total_size = offsetof(WASMType, types)
  510. + sizeof(uint8) * (uint64)(param_count + result_count);
  511. if (!(type = module->types[i] =
  512. loader_malloc(total_size, error_buf, error_buf_size))) {
  513. return false;
  514. }
  515. /* Resolve param types and result types */
  516. type->param_count = (uint16)param_count;
  517. type->result_count = (uint16)result_count;
  518. for (j = 0; j < param_count; j++) {
  519. CHECK_BUF(p, p_end, 1);
  520. type->types[j] = read_uint8(p);
  521. }
  522. read_leb_uint32(p, p_end, result_count);
  523. for (j = 0; j < result_count; j++) {
  524. CHECK_BUF(p, p_end, 1);
  525. type->types[param_count + j] = read_uint8(p);
  526. }
  527. param_cell_num = wasm_get_cell_num(type->types, param_count);
  528. ret_cell_num =
  529. wasm_get_cell_num(type->types + param_count, result_count);
  530. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  531. set_error_buf(error_buf, error_buf_size,
  532. "param count or result count too large");
  533. return false;
  534. }
  535. type->param_cell_num = (uint16)param_cell_num;
  536. type->ret_cell_num = (uint16)ret_cell_num;
  537. }
  538. }
  539. if (p != p_end) {
  540. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  541. return false;
  542. }
  543. LOG_VERBOSE("Load type section success.\n");
  544. return true;
  545. fail:
  546. return false;
  547. }
  548. static void
  549. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  550. {
  551. uint32 default_max_size =
  552. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  553. if (max_size_flag) {
  554. /* module defines the table limitation */
  555. bh_assert(init_size <= *max_size);
  556. if (init_size < *max_size) {
  557. *max_size =
  558. *max_size < default_max_size ? *max_size : default_max_size;
  559. }
  560. }
  561. else {
  562. /* partial defined table limitation, gives a default value */
  563. *max_size = default_max_size;
  564. }
  565. }
  566. #if WASM_ENABLE_MULTI_MODULE != 0
  567. /**
  568. * Find export item of a module with export info:
  569. * module name, field name and export kind
  570. */
  571. static WASMExport *
  572. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  573. const char *field_name, uint8 export_kind,
  574. uint32 export_index_boundary, char *error_buf,
  575. uint32 error_buf_size)
  576. {
  577. WASMExport *export;
  578. uint32 i;
  579. for (i = 0, export = module->exports; i < module->export_count;
  580. ++i, ++export) {
  581. /**
  582. * need to consider a scenario that different kinds of exports
  583. * may have the same name, like
  584. * (table (export "m1" "exported") 10 funcref)
  585. * (memory (export "m1" "exported") 10)
  586. **/
  587. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  588. break;
  589. }
  590. }
  591. if (i == module->export_count) {
  592. LOG_DEBUG("can not find an export %d named %s in the module %s",
  593. export_kind, field_name, module_name);
  594. set_error_buf(error_buf, error_buf_size,
  595. "unknown import or incompatible import type");
  596. return NULL;
  597. }
  598. if (export->index >= export_index_boundary) {
  599. LOG_DEBUG("%s in the module %s is out of index (%d >= %d )", field_name,
  600. module_name, export->index, export_index_boundary);
  601. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  602. return NULL;
  603. }
  604. return export;
  605. }
  606. static WASMFunction *
  607. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  608. const WASMType *expected_function_type,
  609. char *error_buf, uint32 error_buf_size)
  610. {
  611. WASMModuleCommon *module_reg;
  612. WASMFunction *function = NULL;
  613. WASMExport *export = NULL;
  614. WASMModule *module = NULL;
  615. WASMType *target_function_type = NULL;
  616. module_reg = wasm_runtime_find_module_registered(module_name);
  617. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  618. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  619. function_name);
  620. set_error_buf(error_buf, error_buf_size, "unknown import");
  621. return NULL;
  622. }
  623. module = (WASMModule *)module_reg;
  624. export = wasm_loader_find_export(
  625. module, module_name, function_name, EXPORT_KIND_FUNC,
  626. module->import_function_count + module->function_count, error_buf,
  627. error_buf_size);
  628. if (!export) {
  629. return NULL;
  630. }
  631. /* resolve function type and function */
  632. if (export->index < module->import_function_count) {
  633. target_function_type =
  634. module->import_functions[export->index].u.function.func_type;
  635. function = module->import_functions[export->index]
  636. .u.function.import_func_linked;
  637. }
  638. else {
  639. target_function_type =
  640. module->functions[export->index - module->import_function_count]
  641. ->func_type;
  642. function =
  643. module->functions[export->index - module->import_function_count];
  644. }
  645. /* check function type */
  646. if (!wasm_type_equal(expected_function_type, target_function_type)) {
  647. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  648. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  649. return NULL;
  650. }
  651. return function;
  652. }
  653. static WASMTable *
  654. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  655. uint32 init_size, uint32 max_size, char *error_buf,
  656. uint32 error_buf_size)
  657. {
  658. WASMModuleCommon *module_reg;
  659. WASMTable *table = NULL;
  660. WASMExport *export = NULL;
  661. WASMModule *module = NULL;
  662. module_reg = wasm_runtime_find_module_registered(module_name);
  663. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  664. LOG_DEBUG("can not find a module named %s for table", module_name);
  665. set_error_buf(error_buf, error_buf_size, "unknown import");
  666. return NULL;
  667. }
  668. module = (WASMModule *)module_reg;
  669. export = wasm_loader_find_export(
  670. module, module_name, table_name, EXPORT_KIND_TABLE,
  671. module->table_count + module->import_table_count, error_buf,
  672. error_buf_size);
  673. if (!export) {
  674. return NULL;
  675. }
  676. /* resolve table and check the init/max size */
  677. if (export->index < module->import_table_count) {
  678. table =
  679. module->import_tables[export->index].u.table.import_table_linked;
  680. }
  681. else {
  682. table = &(module->tables[export->index - module->import_table_count]);
  683. }
  684. if (table->init_size < init_size || table->max_size > max_size) {
  685. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  686. module_name, table_name, table->init_size, table->max_size,
  687. init_size, max_size);
  688. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  689. return NULL;
  690. }
  691. return table;
  692. }
  693. static WASMMemory *
  694. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  695. uint32 init_page_count, uint32 max_page_count,
  696. char *error_buf, uint32 error_buf_size)
  697. {
  698. WASMModuleCommon *module_reg;
  699. WASMMemory *memory = NULL;
  700. WASMExport *export = NULL;
  701. WASMModule *module = NULL;
  702. module_reg = wasm_runtime_find_module_registered(module_name);
  703. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  704. LOG_DEBUG("can not find a module named %s for memory", module_name);
  705. set_error_buf(error_buf, error_buf_size, "unknown import");
  706. return NULL;
  707. }
  708. module = (WASMModule *)module_reg;
  709. export = wasm_loader_find_export(
  710. module, module_name, memory_name, EXPORT_KIND_MEMORY,
  711. module->import_memory_count + module->memory_count, error_buf,
  712. error_buf_size);
  713. if (!export) {
  714. return NULL;
  715. }
  716. /* resolve memory and check the init/max page count */
  717. if (export->index < module->import_memory_count) {
  718. memory = module->import_memories[export->index]
  719. .u.memory.import_memory_linked;
  720. }
  721. else {
  722. memory =
  723. &(module->memories[export->index - module->import_memory_count]);
  724. }
  725. if (memory->init_page_count < init_page_count
  726. || memory->max_page_count > max_page_count) {
  727. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  728. module_name, memory_name, memory->init_page_count,
  729. memory->max_page_count, init_page_count, max_page_count);
  730. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  731. return NULL;
  732. }
  733. return memory;
  734. }
  735. static WASMGlobal *
  736. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  737. uint8 type, bool is_mutable, char *error_buf,
  738. uint32 error_buf_size)
  739. {
  740. WASMModuleCommon *module_reg;
  741. WASMGlobal *global = NULL;
  742. WASMExport *export = NULL;
  743. WASMModule *module = NULL;
  744. module_reg = wasm_runtime_find_module_registered(module_name);
  745. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  746. LOG_DEBUG("can not find a module named %s for global", module_name);
  747. set_error_buf(error_buf, error_buf_size, "unknown import");
  748. return NULL;
  749. }
  750. module = (WASMModule *)module_reg;
  751. export = wasm_loader_find_export(
  752. module, module_name, global_name, EXPORT_KIND_GLOBAL,
  753. module->import_global_count + module->global_count, error_buf,
  754. error_buf_size);
  755. if (!export) {
  756. return NULL;
  757. }
  758. /* resolve and check the global */
  759. if (export->index < module->import_global_count) {
  760. global =
  761. module->import_globals[export->index].u.global.import_global_linked;
  762. }
  763. else {
  764. global =
  765. &(module->globals[export->index - module->import_global_count]);
  766. }
  767. if (global->type != type || global->is_mutable != is_mutable) {
  768. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  769. module_name, global_name, global->type, global->is_mutable,
  770. type, is_mutable);
  771. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  772. return NULL;
  773. }
  774. return global;
  775. }
  776. static WASMModule *
  777. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  778. {
  779. WASMRegisteredModule *node =
  780. bh_list_first_elem(parent_module->import_module_list);
  781. while (node && strcmp(sub_module_name, node->module_name)) {
  782. node = bh_list_elem_next(node);
  783. }
  784. return node ? (WASMModule *)node->module : NULL;
  785. }
  786. static bool
  787. register_sub_module(const WASMModule *parent_module,
  788. const char *sub_module_name, WASMModule *sub_module)
  789. {
  790. /* register sub_module into its parent sub module list */
  791. WASMRegisteredModule *node = NULL;
  792. bh_list_status ret;
  793. if (search_sub_module(parent_module, sub_module_name)) {
  794. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  795. return true;
  796. }
  797. node = wasm_runtime_malloc(sizeof(WASMRegisteredModule));
  798. if (!node) {
  799. return false;
  800. }
  801. node->module_name = sub_module_name;
  802. node->module = (WASMModuleCommon *)sub_module;
  803. ret = bh_list_insert(parent_module->import_module_list, node);
  804. bh_assert(BH_LIST_SUCCESS == ret);
  805. (void)ret;
  806. return true;
  807. }
  808. static WASMModule *
  809. load_depended_module(const WASMModule *parent_module,
  810. const char *sub_module_name, char *error_buf,
  811. uint32 error_buf_size)
  812. {
  813. WASMModule *sub_module = NULL;
  814. bool ret = false;
  815. uint8 *buffer = NULL;
  816. uint32 buffer_size = 0;
  817. const module_reader reader = wasm_runtime_get_module_reader();
  818. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  819. /* check the registered module list of the parent */
  820. sub_module = search_sub_module(parent_module, sub_module_name);
  821. if (sub_module) {
  822. LOG_DEBUG("%s has been loaded before", sub_module_name);
  823. return sub_module;
  824. }
  825. /* check the global registered module list */
  826. sub_module =
  827. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  828. if (sub_module) {
  829. LOG_DEBUG("%s has been loaded", sub_module_name);
  830. goto register_sub_module;
  831. }
  832. LOG_VERBOSE("loading %s", sub_module_name);
  833. if (!reader) {
  834. set_error_buf_v(error_buf, error_buf_size,
  835. "no sub module reader to load %s", sub_module_name);
  836. return NULL;
  837. }
  838. /* start to maintain a loading module list */
  839. ret = wasm_runtime_is_loading_module(sub_module_name);
  840. if (ret) {
  841. set_error_buf_v(error_buf, error_buf_size,
  842. "found circular dependency on %s", sub_module_name);
  843. return NULL;
  844. }
  845. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  846. error_buf_size);
  847. if (!ret) {
  848. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  849. return NULL;
  850. }
  851. ret = reader(sub_module_name, &buffer, &buffer_size);
  852. if (!ret) {
  853. LOG_DEBUG("read the file of %s failed", sub_module_name);
  854. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  855. sub_module_name);
  856. goto delete_loading_module;
  857. }
  858. sub_module =
  859. wasm_loader_load(buffer, buffer_size, error_buf, error_buf_size);
  860. if (!sub_module) {
  861. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  862. /* others will be destroyed in runtime_destroy() */
  863. goto destroy_file_buffer;
  864. }
  865. wasm_runtime_delete_loading_module(sub_module_name);
  866. /* register on a global list */
  867. ret = wasm_runtime_register_module_internal(
  868. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  869. error_buf, error_buf_size);
  870. if (!ret) {
  871. LOG_DEBUG("error: can not register module %s globally\n",
  872. sub_module_name);
  873. /* others will be unloaded in runtime_destroy() */
  874. goto unload_module;
  875. }
  876. /* register into its parent list */
  877. register_sub_module:
  878. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  879. if (!ret) {
  880. set_error_buf_v(error_buf, error_buf_size,
  881. "failed to register sub module %s", sub_module_name);
  882. /* since it is in the global module list, no need to
  883. * unload the module. the runtime_destroy() will do it
  884. */
  885. return NULL;
  886. }
  887. return sub_module;
  888. unload_module:
  889. wasm_loader_unload(sub_module);
  890. destroy_file_buffer:
  891. if (destroyer) {
  892. destroyer(buffer, buffer_size);
  893. }
  894. else {
  895. LOG_WARNING("need to release the reading buffer of %s manually",
  896. sub_module_name);
  897. }
  898. delete_loading_module:
  899. wasm_runtime_delete_loading_module(sub_module_name);
  900. return NULL;
  901. }
  902. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  903. static bool
  904. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  905. const WASMModule *parent_module,
  906. const char *sub_module_name, const char *function_name,
  907. WASMFunctionImport *function, char *error_buf,
  908. uint32 error_buf_size)
  909. {
  910. const uint8 *p = *p_buf, *p_end = buf_end;
  911. uint32 declare_type_index = 0;
  912. WASMType *declare_func_type = NULL;
  913. WASMFunction *linked_func = NULL;
  914. #if WASM_ENABLE_MULTI_MODULE != 0
  915. WASMModule *sub_module = NULL;
  916. #endif
  917. const char *linked_signature = NULL;
  918. void *linked_attachment = NULL;
  919. bool linked_call_conv_raw = false;
  920. bool is_native_symbol = false;
  921. read_leb_uint32(p, p_end, declare_type_index);
  922. *p_buf = p;
  923. if (declare_type_index >= parent_module->type_count) {
  924. set_error_buf(error_buf, error_buf_size, "unknown type");
  925. return false;
  926. }
  927. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  928. declare_type_index = wasm_get_smallest_type_idx(
  929. parent_module->types, parent_module->type_count, declare_type_index);
  930. #endif
  931. declare_func_type = parent_module->types[declare_type_index];
  932. /* lookup registered native symbols first */
  933. linked_func = wasm_native_resolve_symbol(
  934. sub_module_name, function_name, declare_func_type, &linked_signature,
  935. &linked_attachment, &linked_call_conv_raw);
  936. if (linked_func) {
  937. is_native_symbol = true;
  938. }
  939. #if WASM_ENABLE_MULTI_MODULE != 0
  940. else {
  941. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  942. sub_module = load_depended_module(parent_module, sub_module_name,
  943. error_buf, error_buf_size);
  944. if (!sub_module) {
  945. return false;
  946. }
  947. }
  948. linked_func = wasm_loader_resolve_function(
  949. sub_module_name, function_name, declare_func_type, error_buf,
  950. error_buf_size);
  951. }
  952. #endif
  953. function->module_name = (char *)sub_module_name;
  954. function->field_name = (char *)function_name;
  955. function->func_type = declare_func_type;
  956. /* func_ptr_linked is for native registered symbol */
  957. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  958. function->signature = linked_signature;
  959. function->attachment = linked_attachment;
  960. function->call_conv_raw = linked_call_conv_raw;
  961. #if WASM_ENABLE_MULTI_MODULE != 0
  962. function->import_module = is_native_symbol ? NULL : sub_module;
  963. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  964. #endif
  965. return true;
  966. fail:
  967. return false;
  968. }
  969. static bool
  970. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  971. uint32 error_buf_size)
  972. {
  973. if (max_size < init_size) {
  974. set_error_buf(error_buf, error_buf_size,
  975. "size minimum must not be greater than maximum");
  976. return false;
  977. }
  978. return true;
  979. }
  980. static bool
  981. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  982. WASMModule *parent_module, const char *sub_module_name,
  983. const char *table_name, WASMTableImport *table,
  984. char *error_buf, uint32 error_buf_size)
  985. {
  986. const uint8 *p = *p_buf, *p_end = buf_end;
  987. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  988. declare_init_size = 0, declare_max_size = 0;
  989. #if WASM_ENABLE_MULTI_MODULE != 0
  990. WASMModule *sub_module = NULL;
  991. WASMTable *linked_table = NULL;
  992. #endif
  993. CHECK_BUF(p, p_end, 1);
  994. /* 0x70 or 0x6F */
  995. declare_elem_type = read_uint8(p);
  996. if (VALUE_TYPE_FUNCREF != declare_elem_type
  997. #if WASM_ENABLE_REF_TYPES != 0
  998. && VALUE_TYPE_EXTERNREF != declare_elem_type
  999. #endif
  1000. ) {
  1001. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1002. return false;
  1003. }
  1004. read_leb_uint32(p, p_end, declare_max_size_flag);
  1005. if (declare_max_size_flag > 1) {
  1006. set_error_buf(error_buf, error_buf_size, "integer too large");
  1007. return false;
  1008. }
  1009. read_leb_uint32(p, p_end, declare_init_size);
  1010. if (declare_max_size_flag) {
  1011. read_leb_uint32(p, p_end, declare_max_size);
  1012. if (!check_table_max_size(declare_init_size, declare_max_size,
  1013. error_buf, error_buf_size))
  1014. return false;
  1015. }
  1016. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1017. &declare_max_size);
  1018. *p_buf = p;
  1019. #if WASM_ENABLE_MULTI_MODULE != 0
  1020. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1021. sub_module = load_depended_module(parent_module, sub_module_name,
  1022. error_buf, error_buf_size);
  1023. if (!sub_module) {
  1024. return false;
  1025. }
  1026. linked_table = wasm_loader_resolve_table(
  1027. sub_module_name, table_name, declare_init_size, declare_max_size,
  1028. error_buf, error_buf_size);
  1029. if (!linked_table) {
  1030. return false;
  1031. }
  1032. /* reset with linked table limit */
  1033. declare_elem_type = linked_table->elem_type;
  1034. declare_init_size = linked_table->init_size;
  1035. declare_max_size = linked_table->max_size;
  1036. declare_max_size_flag = linked_table->flags;
  1037. table->import_table_linked = linked_table;
  1038. table->import_module = sub_module;
  1039. }
  1040. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1041. /* (table (export "table") 10 20 funcref) */
  1042. /* we need this section working in wamrc */
  1043. if (!strcmp("spectest", sub_module_name)) {
  1044. const uint32 spectest_table_init_size = 10;
  1045. const uint32 spectest_table_max_size = 20;
  1046. if (strcmp("table", table_name)) {
  1047. set_error_buf(error_buf, error_buf_size,
  1048. "incompatible import type or unknown import");
  1049. return false;
  1050. }
  1051. if (declare_init_size > spectest_table_init_size
  1052. || declare_max_size < spectest_table_max_size) {
  1053. set_error_buf(error_buf, error_buf_size,
  1054. "incompatible import type");
  1055. return false;
  1056. }
  1057. declare_init_size = spectest_table_init_size;
  1058. declare_max_size = spectest_table_max_size;
  1059. }
  1060. /* now we believe all declaration are ok */
  1061. table->elem_type = declare_elem_type;
  1062. table->init_size = declare_init_size;
  1063. table->flags = declare_max_size_flag;
  1064. table->max_size = declare_max_size;
  1065. return true;
  1066. fail:
  1067. return false;
  1068. }
  1069. unsigned
  1070. wasm_runtime_memory_pool_size();
  1071. static bool
  1072. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1073. {
  1074. if (init_size > 65536) {
  1075. set_error_buf(error_buf, error_buf_size,
  1076. "memory size must be at most 65536 pages (4GiB)");
  1077. return false;
  1078. }
  1079. return true;
  1080. }
  1081. static bool
  1082. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1083. uint32 error_buf_size)
  1084. {
  1085. if (max_size < init_size) {
  1086. set_error_buf(error_buf, error_buf_size,
  1087. "size minimum must not be greater than maximum");
  1088. return false;
  1089. }
  1090. if (max_size > 65536) {
  1091. set_error_buf(error_buf, error_buf_size,
  1092. "memory size must be at most 65536 pages (4GiB)");
  1093. return false;
  1094. }
  1095. return true;
  1096. }
  1097. static bool
  1098. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1099. WASMModule *parent_module, const char *sub_module_name,
  1100. const char *memory_name, WASMMemoryImport *memory,
  1101. char *error_buf, uint32 error_buf_size)
  1102. {
  1103. const uint8 *p = *p_buf, *p_end = buf_end;
  1104. uint32 pool_size = wasm_runtime_memory_pool_size();
  1105. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1106. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1107. / DEFAULT_NUM_BYTES_PER_PAGE;
  1108. #else
  1109. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  1110. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1111. uint32 declare_max_page_count_flag = 0;
  1112. uint32 declare_init_page_count = 0;
  1113. uint32 declare_max_page_count = 0;
  1114. #if WASM_ENABLE_MULTI_MODULE != 0
  1115. WASMModule *sub_module = NULL;
  1116. WASMMemory *linked_memory = NULL;
  1117. #endif
  1118. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1119. read_leb_uint32(p, p_end, declare_init_page_count);
  1120. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1121. error_buf_size)) {
  1122. return false;
  1123. }
  1124. if (declare_max_page_count_flag & 1) {
  1125. read_leb_uint32(p, p_end, declare_max_page_count);
  1126. if (!check_memory_max_size(declare_init_page_count,
  1127. declare_max_page_count, error_buf,
  1128. error_buf_size)) {
  1129. return false;
  1130. }
  1131. if (declare_max_page_count > max_page_count) {
  1132. declare_max_page_count = max_page_count;
  1133. }
  1134. }
  1135. else {
  1136. /* Limit the maximum memory size to max_page_count */
  1137. declare_max_page_count = max_page_count;
  1138. }
  1139. #if WASM_ENABLE_MULTI_MODULE != 0
  1140. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1141. sub_module = load_depended_module(parent_module, sub_module_name,
  1142. error_buf, error_buf_size);
  1143. if (!sub_module) {
  1144. return false;
  1145. }
  1146. linked_memory = wasm_loader_resolve_memory(
  1147. sub_module_name, memory_name, declare_init_page_count,
  1148. declare_max_page_count, error_buf, error_buf_size);
  1149. if (!linked_memory) {
  1150. return false;
  1151. }
  1152. /**
  1153. * reset with linked memory limit
  1154. */
  1155. memory->import_module = sub_module;
  1156. memory->import_memory_linked = linked_memory;
  1157. declare_init_page_count = linked_memory->init_page_count;
  1158. declare_max_page_count = linked_memory->max_page_count;
  1159. }
  1160. #endif
  1161. /* (memory (export "memory") 1 2) */
  1162. if (!strcmp("spectest", sub_module_name)) {
  1163. uint32 spectest_memory_init_page = 1;
  1164. uint32 spectest_memory_max_page = 2;
  1165. if (strcmp("memory", memory_name)) {
  1166. set_error_buf(error_buf, error_buf_size,
  1167. "incompatible import type or unknown import");
  1168. return false;
  1169. }
  1170. if (declare_init_page_count > spectest_memory_init_page
  1171. || declare_max_page_count < spectest_memory_max_page) {
  1172. set_error_buf(error_buf, error_buf_size,
  1173. "incompatible import type");
  1174. return false;
  1175. }
  1176. declare_init_page_count = spectest_memory_init_page;
  1177. declare_max_page_count = spectest_memory_max_page;
  1178. }
  1179. /* now we believe all declaration are ok */
  1180. memory->flags = declare_max_page_count_flag;
  1181. memory->init_page_count = declare_init_page_count;
  1182. memory->max_page_count = declare_max_page_count;
  1183. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1184. *p_buf = p;
  1185. return true;
  1186. fail:
  1187. return false;
  1188. }
  1189. static bool
  1190. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  1191. const WASMModule *parent_module, char *sub_module_name,
  1192. char *global_name, WASMGlobalImport *global, char *error_buf,
  1193. uint32 error_buf_size)
  1194. {
  1195. const uint8 *p = *p_buf, *p_end = buf_end;
  1196. uint8 declare_type = 0;
  1197. uint8 declare_mutable = 0;
  1198. #if WASM_ENABLE_MULTI_MODULE != 0
  1199. WASMModule *sub_module = NULL;
  1200. WASMGlobal *linked_global = NULL;
  1201. #endif
  1202. CHECK_BUF(p, p_end, 2);
  1203. declare_type = read_uint8(p);
  1204. declare_mutable = read_uint8(p);
  1205. *p_buf = p;
  1206. if (declare_mutable >= 2) {
  1207. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1208. return false;
  1209. }
  1210. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1211. global->is_linked = wasm_native_lookup_libc_builtin_global(
  1212. sub_module_name, global_name, global);
  1213. if (global->is_linked) {
  1214. if (global->type != declare_type
  1215. || global->is_mutable != declare_mutable) {
  1216. set_error_buf(error_buf, error_buf_size,
  1217. "incompatible import type");
  1218. return false;
  1219. }
  1220. }
  1221. #endif
  1222. #if WASM_ENABLE_MULTI_MODULE != 0
  1223. if (!global->is_linked
  1224. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  1225. sub_module = load_depended_module(parent_module, sub_module_name,
  1226. error_buf, error_buf_size);
  1227. if (!sub_module) {
  1228. return false;
  1229. }
  1230. /* check sub modules */
  1231. linked_global = wasm_loader_resolve_global(
  1232. sub_module_name, global_name, declare_type, declare_mutable,
  1233. error_buf, error_buf_size);
  1234. if (linked_global) {
  1235. global->import_module = sub_module;
  1236. global->import_global_linked = linked_global;
  1237. global->is_linked = true;
  1238. }
  1239. }
  1240. #endif
  1241. global->module_name = sub_module_name;
  1242. global->field_name = global_name;
  1243. global->type = declare_type;
  1244. global->is_mutable = (declare_mutable == 1);
  1245. return true;
  1246. fail:
  1247. return false;
  1248. }
  1249. static bool
  1250. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  1251. char *error_buf, uint32 error_buf_size)
  1252. {
  1253. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1254. CHECK_BUF(p, p_end, 1);
  1255. /* 0x70 or 0x6F */
  1256. table->elem_type = read_uint8(p);
  1257. if (VALUE_TYPE_FUNCREF != table->elem_type
  1258. #if WASM_ENABLE_REF_TYPES != 0
  1259. && VALUE_TYPE_EXTERNREF != table->elem_type
  1260. #endif
  1261. ) {
  1262. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1263. return false;
  1264. }
  1265. p_org = p;
  1266. read_leb_uint32(p, p_end, table->flags);
  1267. #if WASM_ENABLE_SHARED_MEMORY == 0
  1268. if (p - p_org > 1) {
  1269. set_error_buf(error_buf, error_buf_size,
  1270. "integer representation too long");
  1271. return false;
  1272. }
  1273. if (table->flags > 1) {
  1274. set_error_buf(error_buf, error_buf_size, "integer too large");
  1275. return false;
  1276. }
  1277. #else
  1278. if (p - p_org > 1) {
  1279. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1280. return false;
  1281. }
  1282. if (table->flags == 2) {
  1283. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  1284. return false;
  1285. }
  1286. if (table->flags > 1) {
  1287. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1288. return false;
  1289. }
  1290. #endif
  1291. read_leb_uint32(p, p_end, table->init_size);
  1292. if (table->flags) {
  1293. read_leb_uint32(p, p_end, table->max_size);
  1294. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  1295. error_buf_size))
  1296. return false;
  1297. }
  1298. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  1299. *p_buf = p;
  1300. return true;
  1301. fail:
  1302. return false;
  1303. }
  1304. static bool
  1305. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  1306. char *error_buf, uint32 error_buf_size)
  1307. {
  1308. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1309. uint32 pool_size = wasm_runtime_memory_pool_size();
  1310. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1311. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1312. / DEFAULT_NUM_BYTES_PER_PAGE;
  1313. #else
  1314. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  1315. #endif
  1316. p_org = p;
  1317. read_leb_uint32(p, p_end, memory->flags);
  1318. #if WASM_ENABLE_SHARED_MEMORY == 0
  1319. if (p - p_org > 1) {
  1320. set_error_buf(error_buf, error_buf_size,
  1321. "integer representation too long");
  1322. return false;
  1323. }
  1324. if (memory->flags > 1) {
  1325. set_error_buf(error_buf, error_buf_size, "integer too large");
  1326. return false;
  1327. }
  1328. #else
  1329. if (p - p_org > 1) {
  1330. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1331. return false;
  1332. }
  1333. if (memory->flags > 3) {
  1334. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1335. return false;
  1336. }
  1337. else if (memory->flags == 2) {
  1338. set_error_buf(error_buf, error_buf_size,
  1339. "shared memory must have maximum");
  1340. return false;
  1341. }
  1342. #endif
  1343. read_leb_uint32(p, p_end, memory->init_page_count);
  1344. if (!check_memory_init_size(memory->init_page_count, error_buf,
  1345. error_buf_size))
  1346. return false;
  1347. if (memory->flags & 1) {
  1348. read_leb_uint32(p, p_end, memory->max_page_count);
  1349. if (!check_memory_max_size(memory->init_page_count,
  1350. memory->max_page_count, error_buf,
  1351. error_buf_size))
  1352. return false;
  1353. if (memory->max_page_count > max_page_count)
  1354. memory->max_page_count = max_page_count;
  1355. }
  1356. else {
  1357. /* Limit the maximum memory size to max_page_count */
  1358. memory->max_page_count = max_page_count;
  1359. }
  1360. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1361. *p_buf = p;
  1362. return true;
  1363. fail:
  1364. return false;
  1365. }
  1366. static bool
  1367. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1368. bool is_load_from_file_buf, char *error_buf,
  1369. uint32 error_buf_size)
  1370. {
  1371. const uint8 *p = buf, *p_end = buf_end, *p_old;
  1372. uint32 import_count, name_len, type_index, i, u32, flags;
  1373. uint64 total_size;
  1374. WASMImport *import;
  1375. WASMImport *import_functions = NULL, *import_tables = NULL;
  1376. WASMImport *import_memories = NULL, *import_globals = NULL;
  1377. char *sub_module_name, *field_name;
  1378. uint8 u8, kind;
  1379. read_leb_uint32(p, p_end, import_count);
  1380. if (import_count) {
  1381. module->import_count = import_count;
  1382. total_size = sizeof(WASMImport) * (uint64)import_count;
  1383. if (!(module->imports =
  1384. loader_malloc(total_size, error_buf, error_buf_size))) {
  1385. return false;
  1386. }
  1387. p_old = p;
  1388. /* Scan firstly to get import count of each type */
  1389. for (i = 0; i < import_count; i++) {
  1390. /* module name */
  1391. read_leb_uint32(p, p_end, name_len);
  1392. CHECK_BUF(p, p_end, name_len);
  1393. p += name_len;
  1394. /* field name */
  1395. read_leb_uint32(p, p_end, name_len);
  1396. CHECK_BUF(p, p_end, name_len);
  1397. p += name_len;
  1398. CHECK_BUF(p, p_end, 1);
  1399. /* 0x00/0x01/0x02/0x03 */
  1400. kind = read_uint8(p);
  1401. switch (kind) {
  1402. case IMPORT_KIND_FUNC: /* import function */
  1403. read_leb_uint32(p, p_end, type_index);
  1404. module->import_function_count++;
  1405. break;
  1406. case IMPORT_KIND_TABLE: /* import table */
  1407. CHECK_BUF(p, p_end, 1);
  1408. /* 0x70 */
  1409. u8 = read_uint8(p);
  1410. read_leb_uint32(p, p_end, flags);
  1411. read_leb_uint32(p, p_end, u32);
  1412. if (flags & 1)
  1413. read_leb_uint32(p, p_end, u32);
  1414. module->import_table_count++;
  1415. #if WASM_ENABLE_REF_TYPES == 0
  1416. if (module->import_table_count > 1) {
  1417. set_error_buf(error_buf, error_buf_size,
  1418. "multiple tables");
  1419. return false;
  1420. }
  1421. #endif
  1422. break;
  1423. case IMPORT_KIND_MEMORY: /* import memory */
  1424. read_leb_uint32(p, p_end, flags);
  1425. read_leb_uint32(p, p_end, u32);
  1426. if (flags & 1)
  1427. read_leb_uint32(p, p_end, u32);
  1428. module->import_memory_count++;
  1429. if (module->import_memory_count > 1) {
  1430. set_error_buf(error_buf, error_buf_size,
  1431. "multiple memories");
  1432. return false;
  1433. }
  1434. break;
  1435. case IMPORT_KIND_GLOBAL: /* import global */
  1436. CHECK_BUF(p, p_end, 2);
  1437. p += 2;
  1438. module->import_global_count++;
  1439. break;
  1440. default:
  1441. set_error_buf(error_buf, error_buf_size,
  1442. "invalid import kind");
  1443. return false;
  1444. }
  1445. }
  1446. if (module->import_function_count)
  1447. import_functions = module->import_functions = module->imports;
  1448. if (module->import_table_count)
  1449. import_tables = module->import_tables =
  1450. module->imports + module->import_function_count;
  1451. if (module->import_memory_count)
  1452. import_memories = module->import_memories =
  1453. module->imports + module->import_function_count
  1454. + module->import_table_count;
  1455. if (module->import_global_count)
  1456. import_globals = module->import_globals =
  1457. module->imports + module->import_function_count
  1458. + module->import_table_count + module->import_memory_count;
  1459. p = p_old;
  1460. /* Scan again to resolve the data */
  1461. for (i = 0; i < import_count; i++) {
  1462. /* load module name */
  1463. read_leb_uint32(p, p_end, name_len);
  1464. CHECK_BUF(p, p_end, name_len);
  1465. if (!(sub_module_name = const_str_list_insert(
  1466. p, name_len, module, is_load_from_file_buf, error_buf,
  1467. error_buf_size))) {
  1468. return false;
  1469. }
  1470. p += name_len;
  1471. /* load field name */
  1472. read_leb_uint32(p, p_end, name_len);
  1473. CHECK_BUF(p, p_end, name_len);
  1474. if (!(field_name = const_str_list_insert(
  1475. p, name_len, module, is_load_from_file_buf, error_buf,
  1476. error_buf_size))) {
  1477. return false;
  1478. }
  1479. p += name_len;
  1480. CHECK_BUF(p, p_end, 1);
  1481. /* 0x00/0x01/0x02/0x03 */
  1482. kind = read_uint8(p);
  1483. switch (kind) {
  1484. case IMPORT_KIND_FUNC: /* import function */
  1485. bh_assert(import_functions);
  1486. import = import_functions++;
  1487. if (!load_function_import(
  1488. &p, p_end, module, sub_module_name, field_name,
  1489. &import->u.function, error_buf, error_buf_size)) {
  1490. return false;
  1491. }
  1492. break;
  1493. case IMPORT_KIND_TABLE: /* import table */
  1494. bh_assert(import_tables);
  1495. import = import_tables++;
  1496. if (!load_table_import(&p, p_end, module, sub_module_name,
  1497. field_name, &import->u.table,
  1498. error_buf, error_buf_size)) {
  1499. LOG_DEBUG("can not import such a table (%s,%s)",
  1500. sub_module_name, field_name);
  1501. return false;
  1502. }
  1503. break;
  1504. case IMPORT_KIND_MEMORY: /* import memory */
  1505. bh_assert(import_memories);
  1506. import = import_memories++;
  1507. if (!load_memory_import(&p, p_end, module, sub_module_name,
  1508. field_name, &import->u.memory,
  1509. error_buf, error_buf_size)) {
  1510. return false;
  1511. }
  1512. break;
  1513. case IMPORT_KIND_GLOBAL: /* import global */
  1514. bh_assert(import_globals);
  1515. import = import_globals++;
  1516. if (!load_global_import(&p, p_end, module, sub_module_name,
  1517. field_name, &import->u.global,
  1518. error_buf, error_buf_size)) {
  1519. return false;
  1520. }
  1521. break;
  1522. default:
  1523. set_error_buf(error_buf, error_buf_size,
  1524. "invalid import kind");
  1525. return false;
  1526. }
  1527. import->kind = kind;
  1528. import->u.names.module_name = sub_module_name;
  1529. import->u.names.field_name = field_name;
  1530. }
  1531. #if WASM_ENABLE_LIBC_WASI != 0
  1532. import = module->import_functions;
  1533. for (i = 0; i < module->import_function_count; i++, import++) {
  1534. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1535. || !strcmp(import->u.names.module_name,
  1536. "wasi_snapshot_preview1")) {
  1537. module->is_wasi_module = true;
  1538. break;
  1539. }
  1540. }
  1541. #endif
  1542. }
  1543. if (p != p_end) {
  1544. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1545. return false;
  1546. }
  1547. LOG_VERBOSE("Load import section success.\n");
  1548. (void)u8;
  1549. (void)u32;
  1550. (void)type_index;
  1551. return true;
  1552. fail:
  1553. return false;
  1554. }
  1555. static bool
  1556. init_function_local_offsets(WASMFunction *func, char *error_buf,
  1557. uint32 error_buf_size)
  1558. {
  1559. WASMType *param_type = func->func_type;
  1560. uint32 param_count = param_type->param_count;
  1561. uint8 *param_types = param_type->types;
  1562. uint32 local_count = func->local_count;
  1563. uint8 *local_types = func->local_types;
  1564. uint32 i, local_offset = 0;
  1565. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1566. /*
  1567. * Only allocate memory when total_size is not 0,
  1568. * or the return value of malloc(0) might be NULL on some platforms,
  1569. * which causes wasm loader return false.
  1570. */
  1571. if (total_size > 0
  1572. && !(func->local_offsets =
  1573. loader_malloc(total_size, error_buf, error_buf_size))) {
  1574. return false;
  1575. }
  1576. for (i = 0; i < param_count; i++) {
  1577. func->local_offsets[i] = (uint16)local_offset;
  1578. local_offset += wasm_value_type_cell_num(param_types[i]);
  1579. }
  1580. for (i = 0; i < local_count; i++) {
  1581. func->local_offsets[param_count + i] = (uint16)local_offset;
  1582. local_offset += wasm_value_type_cell_num(local_types[i]);
  1583. }
  1584. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1585. return true;
  1586. }
  1587. static bool
  1588. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1589. const uint8 *buf_code, const uint8 *buf_code_end,
  1590. WASMModule *module, char *error_buf,
  1591. uint32 error_buf_size)
  1592. {
  1593. const uint8 *p = buf, *p_end = buf_end;
  1594. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1595. uint32 func_count;
  1596. uint64 total_size;
  1597. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1598. uint32 local_count, local_set_count, sub_local_count;
  1599. uint8 type;
  1600. WASMFunction *func;
  1601. read_leb_uint32(p, p_end, func_count);
  1602. if (buf_code)
  1603. read_leb_uint32(p_code, buf_code_end, code_count);
  1604. if (func_count != code_count) {
  1605. set_error_buf(error_buf, error_buf_size,
  1606. "function and code section have inconsistent lengths or "
  1607. "unexpected end");
  1608. return false;
  1609. }
  1610. if (func_count) {
  1611. module->function_count = func_count;
  1612. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  1613. if (!(module->functions =
  1614. loader_malloc(total_size, error_buf, error_buf_size))) {
  1615. return false;
  1616. }
  1617. for (i = 0; i < func_count; i++) {
  1618. /* Resolve function type */
  1619. read_leb_uint32(p, p_end, type_index);
  1620. if (type_index >= module->type_count) {
  1621. set_error_buf(error_buf, error_buf_size, "unknown type");
  1622. return false;
  1623. }
  1624. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1625. type_index = wasm_get_smallest_type_idx(
  1626. module->types, module->type_count, type_index);
  1627. #endif
  1628. read_leb_uint32(p_code, buf_code_end, code_size);
  1629. if (code_size == 0 || p_code + code_size > buf_code_end) {
  1630. set_error_buf(error_buf, error_buf_size,
  1631. "invalid function code size");
  1632. return false;
  1633. }
  1634. /* Resolve local set count */
  1635. p_code_end = p_code + code_size;
  1636. local_count = 0;
  1637. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1638. p_code_save = p_code;
  1639. /* Calculate total local count */
  1640. for (j = 0; j < local_set_count; j++) {
  1641. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1642. if (sub_local_count > UINT32_MAX - local_count) {
  1643. set_error_buf(error_buf, error_buf_size, "too many locals");
  1644. return false;
  1645. }
  1646. CHECK_BUF(p_code, buf_code_end, 1);
  1647. /* 0x7F/0x7E/0x7D/0x7C */
  1648. type = read_uint8(p_code);
  1649. local_count += sub_local_count;
  1650. }
  1651. /* Alloc memory, layout: function structure + local types */
  1652. code_size = (uint32)(p_code_end - p_code);
  1653. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1654. if (!(func = module->functions[i] =
  1655. loader_malloc(total_size, error_buf, error_buf_size))) {
  1656. return false;
  1657. }
  1658. /* Set function type, local count, code size and code body */
  1659. func->func_type = module->types[type_index];
  1660. func->local_count = local_count;
  1661. if (local_count > 0)
  1662. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  1663. func->code_size = code_size;
  1664. /*
  1665. * we shall make a copy of code body [p_code, p_code + code_size]
  1666. * when we are worrying about inappropriate releasing behaviour.
  1667. * all code bodies are actually in a buffer which user allocates in
  1668. * his embedding environment and we don't have power on them.
  1669. * it will be like:
  1670. * code_body_cp = malloc(code_size);
  1671. * memcpy(code_body_cp, p_code, code_size);
  1672. * func->code = code_body_cp;
  1673. */
  1674. func->code = (uint8 *)p_code;
  1675. /* Load each local type */
  1676. p_code = p_code_save;
  1677. local_type_index = 0;
  1678. for (j = 0; j < local_set_count; j++) {
  1679. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1680. if (!sub_local_count
  1681. || local_type_index > UINT32_MAX - sub_local_count
  1682. || local_type_index + sub_local_count > local_count) {
  1683. set_error_buf(error_buf, error_buf_size,
  1684. "invalid local count");
  1685. return false;
  1686. }
  1687. CHECK_BUF(p_code, buf_code_end, 1);
  1688. /* 0x7F/0x7E/0x7D/0x7C */
  1689. type = read_uint8(p_code);
  1690. if ((type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32)
  1691. #if WASM_ENABLE_SIMD != 0
  1692. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1693. && type != VALUE_TYPE_V128
  1694. #endif
  1695. #endif
  1696. #if WASM_ENABLE_REF_TYPES != 0
  1697. && type != VALUE_TYPE_FUNCREF
  1698. && type != VALUE_TYPE_EXTERNREF
  1699. #endif
  1700. ) {
  1701. if (type == VALUE_TYPE_V128)
  1702. set_error_buf(error_buf, error_buf_size,
  1703. "v128 value type requires simd feature");
  1704. else if (type == VALUE_TYPE_FUNCREF
  1705. || type == VALUE_TYPE_EXTERNREF)
  1706. set_error_buf(error_buf, error_buf_size,
  1707. "ref value type requires "
  1708. "reference types feature");
  1709. else
  1710. set_error_buf_v(error_buf, error_buf_size,
  1711. "invalid local type 0x%02X", type);
  1712. return false;
  1713. }
  1714. for (k = 0; k < sub_local_count; k++) {
  1715. func->local_types[local_type_index++] = type;
  1716. }
  1717. }
  1718. func->param_cell_num = func->func_type->param_cell_num;
  1719. func->ret_cell_num = func->func_type->ret_cell_num;
  1720. func->local_cell_num =
  1721. wasm_get_cell_num(func->local_types, func->local_count);
  1722. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1723. return false;
  1724. p_code = p_code_end;
  1725. }
  1726. }
  1727. if (p != p_end) {
  1728. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1729. return false;
  1730. }
  1731. LOG_VERBOSE("Load function section success.\n");
  1732. return true;
  1733. fail:
  1734. return false;
  1735. }
  1736. static bool
  1737. check_function_index(const WASMModule *module, uint32 function_index,
  1738. char *error_buf, uint32 error_buf_size)
  1739. {
  1740. if (function_index
  1741. >= module->import_function_count + module->function_count) {
  1742. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1743. function_index);
  1744. return false;
  1745. }
  1746. return true;
  1747. }
  1748. static bool
  1749. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1750. char *error_buf, uint32 error_buf_size)
  1751. {
  1752. const uint8 *p = buf, *p_end = buf_end;
  1753. uint32 table_count, i;
  1754. uint64 total_size;
  1755. WASMTable *table;
  1756. read_leb_uint32(p, p_end, table_count);
  1757. #if WASM_ENABLE_REF_TYPES == 0
  1758. if (module->import_table_count + table_count > 1) {
  1759. /* a total of one table is allowed */
  1760. set_error_buf(error_buf, error_buf_size, "multiple tables");
  1761. return false;
  1762. }
  1763. #endif
  1764. if (table_count) {
  1765. module->table_count = table_count;
  1766. total_size = sizeof(WASMTable) * (uint64)table_count;
  1767. if (!(module->tables =
  1768. loader_malloc(total_size, error_buf, error_buf_size))) {
  1769. return false;
  1770. }
  1771. /* load each table */
  1772. table = module->tables;
  1773. for (i = 0; i < table_count; i++, table++)
  1774. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1775. return false;
  1776. }
  1777. if (p != p_end) {
  1778. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1779. return false;
  1780. }
  1781. LOG_VERBOSE("Load table section success.\n");
  1782. return true;
  1783. fail:
  1784. return false;
  1785. }
  1786. static bool
  1787. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1788. char *error_buf, uint32 error_buf_size)
  1789. {
  1790. const uint8 *p = buf, *p_end = buf_end;
  1791. uint32 memory_count, i;
  1792. uint64 total_size;
  1793. WASMMemory *memory;
  1794. read_leb_uint32(p, p_end, memory_count);
  1795. /* a total of one memory is allowed */
  1796. if (module->import_memory_count + memory_count > 1) {
  1797. set_error_buf(error_buf, error_buf_size, "multiple memories");
  1798. return false;
  1799. }
  1800. if (memory_count) {
  1801. module->memory_count = memory_count;
  1802. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1803. if (!(module->memories =
  1804. loader_malloc(total_size, error_buf, error_buf_size))) {
  1805. return false;
  1806. }
  1807. /* load each memory */
  1808. memory = module->memories;
  1809. for (i = 0; i < memory_count; i++, memory++)
  1810. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1811. return false;
  1812. }
  1813. if (p != p_end) {
  1814. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1815. return false;
  1816. }
  1817. LOG_VERBOSE("Load memory section success.\n");
  1818. return true;
  1819. fail:
  1820. return false;
  1821. }
  1822. static bool
  1823. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1824. char *error_buf, uint32 error_buf_size)
  1825. {
  1826. const uint8 *p = buf, *p_end = buf_end;
  1827. uint32 global_count, i;
  1828. uint64 total_size;
  1829. WASMGlobal *global;
  1830. uint8 mutable;
  1831. read_leb_uint32(p, p_end, global_count);
  1832. if (global_count) {
  1833. module->global_count = global_count;
  1834. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1835. if (!(module->globals =
  1836. loader_malloc(total_size, error_buf, error_buf_size))) {
  1837. return false;
  1838. }
  1839. global = module->globals;
  1840. for (i = 0; i < global_count; i++, global++) {
  1841. CHECK_BUF(p, p_end, 2);
  1842. global->type = read_uint8(p);
  1843. mutable = read_uint8(p);
  1844. if (mutable >= 2) {
  1845. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1846. return false;
  1847. }
  1848. global->is_mutable = mutable ? true : false;
  1849. /* initialize expression */
  1850. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  1851. error_buf, error_buf_size))
  1852. return false;
  1853. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1854. /**
  1855. * Currently, constant expressions occurring as initializers
  1856. * of globals are further constrained in that contained
  1857. * global.get instructions are
  1858. * only allowed to refer to imported globals.
  1859. */
  1860. uint32 target_global_index = global->init_expr.u.global_index;
  1861. if (target_global_index >= module->import_global_count) {
  1862. set_error_buf(error_buf, error_buf_size, "unknown global");
  1863. return false;
  1864. }
  1865. }
  1866. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1867. == global->init_expr.init_expr_type) {
  1868. if (!check_function_index(module, global->init_expr.u.ref_index,
  1869. error_buf, error_buf_size)) {
  1870. return false;
  1871. }
  1872. }
  1873. }
  1874. }
  1875. if (p != p_end) {
  1876. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1877. return false;
  1878. }
  1879. LOG_VERBOSE("Load global section success.\n");
  1880. return true;
  1881. fail:
  1882. return false;
  1883. }
  1884. static bool
  1885. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1886. bool is_load_from_file_buf, char *error_buf,
  1887. uint32 error_buf_size)
  1888. {
  1889. const uint8 *p = buf, *p_end = buf_end;
  1890. uint32 export_count, i, j, index;
  1891. uint64 total_size;
  1892. uint32 str_len;
  1893. WASMExport *export;
  1894. const char *name;
  1895. read_leb_uint32(p, p_end, export_count);
  1896. if (export_count) {
  1897. module->export_count = export_count;
  1898. total_size = sizeof(WASMExport) * (uint64)export_count;
  1899. if (!(module->exports =
  1900. loader_malloc(total_size, error_buf, error_buf_size))) {
  1901. return false;
  1902. }
  1903. export = module->exports;
  1904. for (i = 0; i < export_count; i++, export ++) {
  1905. read_leb_uint32(p, p_end, str_len);
  1906. CHECK_BUF(p, p_end, str_len);
  1907. for (j = 0; j < i; j++) {
  1908. name = module->exports[j].name;
  1909. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  1910. set_error_buf(error_buf, error_buf_size,
  1911. "duplicate export name");
  1912. return false;
  1913. }
  1914. }
  1915. if (!(export->name = const_str_list_insert(
  1916. p, str_len, module, is_load_from_file_buf, error_buf,
  1917. error_buf_size))) {
  1918. return false;
  1919. }
  1920. p += str_len;
  1921. CHECK_BUF(p, p_end, 1);
  1922. export->kind = read_uint8(p);
  1923. read_leb_uint32(p, p_end, index);
  1924. export->index = index;
  1925. switch (export->kind) {
  1926. /* function index */
  1927. case EXPORT_KIND_FUNC:
  1928. if (index >= module->function_count
  1929. + module->import_function_count) {
  1930. set_error_buf(error_buf, error_buf_size,
  1931. "unknown function");
  1932. return false;
  1933. }
  1934. #if WASM_ENABLE_SIMD != 0
  1935. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1936. /* TODO: check func type, if it has v128 param or result,
  1937. report error */
  1938. #endif
  1939. #endif
  1940. break;
  1941. /* table index */
  1942. case EXPORT_KIND_TABLE:
  1943. if (index
  1944. >= module->table_count + module->import_table_count) {
  1945. set_error_buf(error_buf, error_buf_size,
  1946. "unknown table");
  1947. return false;
  1948. }
  1949. break;
  1950. /* memory index */
  1951. case EXPORT_KIND_MEMORY:
  1952. if (index
  1953. >= module->memory_count + module->import_memory_count) {
  1954. set_error_buf(error_buf, error_buf_size,
  1955. "unknown memory");
  1956. return false;
  1957. }
  1958. break;
  1959. /* global index */
  1960. case EXPORT_KIND_GLOBAL:
  1961. if (index
  1962. >= module->global_count + module->import_global_count) {
  1963. set_error_buf(error_buf, error_buf_size,
  1964. "unknown global");
  1965. return false;
  1966. }
  1967. break;
  1968. default:
  1969. set_error_buf(error_buf, error_buf_size,
  1970. "invalid export kind");
  1971. return false;
  1972. }
  1973. }
  1974. }
  1975. if (p != p_end) {
  1976. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1977. return false;
  1978. }
  1979. LOG_VERBOSE("Load export section success.\n");
  1980. return true;
  1981. fail:
  1982. return false;
  1983. }
  1984. static bool
  1985. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  1986. uint32 error_buf_size)
  1987. {
  1988. #if WASM_ENABLE_REF_TYPES == 0
  1989. if (table_index != 0) {
  1990. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  1991. return false;
  1992. }
  1993. #endif
  1994. if (table_index >= module->import_table_count + module->table_count) {
  1995. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  1996. table_index);
  1997. return false;
  1998. }
  1999. return true;
  2000. }
  2001. static bool
  2002. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2003. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  2004. {
  2005. const uint8 *p = *p_buf, *p_end = buf_end;
  2006. uint32 table_index;
  2007. read_leb_uint32(p, p_end, table_index);
  2008. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  2009. return false;
  2010. }
  2011. *p_table_index = table_index;
  2012. *p_buf = p;
  2013. return true;
  2014. fail:
  2015. return false;
  2016. }
  2017. #if WASM_ENABLE_REF_TYPES != 0
  2018. static bool
  2019. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  2020. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  2021. {
  2022. const uint8 *p = *p_buf, *p_end = buf_end;
  2023. uint8 elem_type;
  2024. CHECK_BUF(p, p_end, 1);
  2025. elem_type = read_uint8(p);
  2026. if ((elemkind_zero && elem_type != 0)
  2027. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  2028. && elem_type != VALUE_TYPE_EXTERNREF)) {
  2029. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  2030. return false;
  2031. }
  2032. if (elemkind_zero)
  2033. *p_elem_type = VALUE_TYPE_FUNCREF;
  2034. else
  2035. *p_elem_type = elem_type;
  2036. *p_buf = p;
  2037. return true;
  2038. fail:
  2039. return false;
  2040. }
  2041. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  2042. static bool
  2043. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  2044. WASMModule *module, WASMTableSeg *table_segment,
  2045. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  2046. {
  2047. const uint8 *p = *p_buf, *p_end = buf_end;
  2048. uint32 function_count, function_index = 0, i;
  2049. uint64 total_size;
  2050. read_leb_uint32(p, p_end, function_count);
  2051. table_segment->function_count = function_count;
  2052. total_size = sizeof(uint32) * (uint64)function_count;
  2053. if (total_size > 0
  2054. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  2055. total_size, error_buf, error_buf_size))) {
  2056. return false;
  2057. }
  2058. for (i = 0; i < function_count; i++) {
  2059. InitializerExpression init_expr = { 0 };
  2060. #if WASM_ENABLE_REF_TYPES != 0
  2061. if (!use_init_expr) {
  2062. read_leb_uint32(p, p_end, function_index);
  2063. }
  2064. else {
  2065. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  2066. error_buf, error_buf_size))
  2067. return false;
  2068. function_index = init_expr.u.ref_index;
  2069. }
  2070. #else
  2071. read_leb_uint32(p, p_end, function_index);
  2072. #endif
  2073. /* since we are using -1 to indicate ref.null */
  2074. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  2075. && !check_function_index(module, function_index, error_buf,
  2076. error_buf_size)) {
  2077. return false;
  2078. }
  2079. table_segment->func_indexes[i] = function_index;
  2080. }
  2081. *p_buf = p;
  2082. return true;
  2083. fail:
  2084. return false;
  2085. }
  2086. static bool
  2087. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  2088. WASMModule *module, char *error_buf,
  2089. uint32 error_buf_size)
  2090. {
  2091. const uint8 *p = buf, *p_end = buf_end;
  2092. uint32 table_segment_count, i;
  2093. uint64 total_size;
  2094. WASMTableSeg *table_segment;
  2095. read_leb_uint32(p, p_end, table_segment_count);
  2096. if (table_segment_count) {
  2097. module->table_seg_count = table_segment_count;
  2098. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  2099. if (!(module->table_segments =
  2100. loader_malloc(total_size, error_buf, error_buf_size))) {
  2101. return false;
  2102. }
  2103. table_segment = module->table_segments;
  2104. for (i = 0; i < table_segment_count; i++, table_segment++) {
  2105. if (p >= p_end) {
  2106. set_error_buf(error_buf, error_buf_size,
  2107. "invalid value type or "
  2108. "invalid elements segment kind");
  2109. return false;
  2110. }
  2111. #if WASM_ENABLE_REF_TYPES != 0
  2112. read_leb_uint32(p, p_end, table_segment->mode);
  2113. /* last three bits */
  2114. table_segment->mode = table_segment->mode & 0x07;
  2115. switch (table_segment->mode) {
  2116. /* elemkind/elemtype + active */
  2117. case 0:
  2118. case 4:
  2119. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  2120. table_segment->table_index = 0;
  2121. if (!check_table_index(module, table_segment->table_index,
  2122. error_buf, error_buf_size))
  2123. return false;
  2124. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2125. VALUE_TYPE_I32, error_buf,
  2126. error_buf_size))
  2127. return false;
  2128. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2129. table_segment->mode == 0 ? false
  2130. : true,
  2131. error_buf, error_buf_size))
  2132. return false;
  2133. break;
  2134. /* elemkind + passive/declarative */
  2135. case 1:
  2136. case 3:
  2137. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2138. true, error_buf, error_buf_size))
  2139. return false;
  2140. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2141. false, error_buf, error_buf_size))
  2142. return false;
  2143. break;
  2144. /* elemkind/elemtype + table_idx + active */
  2145. case 2:
  2146. case 6:
  2147. if (!load_table_index(&p, p_end, module,
  2148. &table_segment->table_index,
  2149. error_buf, error_buf_size))
  2150. return false;
  2151. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2152. VALUE_TYPE_I32, error_buf,
  2153. error_buf_size))
  2154. return false;
  2155. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2156. table_segment->mode == 2 ? true : false,
  2157. error_buf, error_buf_size))
  2158. return false;
  2159. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2160. table_segment->mode == 2 ? false
  2161. : true,
  2162. error_buf, error_buf_size))
  2163. return false;
  2164. break;
  2165. case 5:
  2166. case 7:
  2167. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2168. false, error_buf, error_buf_size))
  2169. return false;
  2170. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2171. true, error_buf, error_buf_size))
  2172. return false;
  2173. break;
  2174. default:
  2175. set_error_buf(error_buf, error_buf_size,
  2176. "unknown element segment kind");
  2177. return false;
  2178. }
  2179. #else
  2180. /*
  2181. * like: 00 41 05 0b 04 00 01 00 01
  2182. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  2183. */
  2184. if (!load_table_index(&p, p_end, module,
  2185. &table_segment->table_index, error_buf,
  2186. error_buf_size))
  2187. return false;
  2188. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2189. VALUE_TYPE_I32, error_buf, error_buf_size))
  2190. return false;
  2191. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  2192. error_buf, error_buf_size))
  2193. return false;
  2194. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2195. }
  2196. }
  2197. if (p != p_end) {
  2198. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2199. return false;
  2200. }
  2201. LOG_VERBOSE("Load table segment section success.\n");
  2202. return true;
  2203. fail:
  2204. return false;
  2205. }
  2206. static bool
  2207. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  2208. WASMModule *module, char *error_buf,
  2209. uint32 error_buf_size)
  2210. {
  2211. const uint8 *p = buf, *p_end = buf_end;
  2212. uint32 data_seg_count, i, mem_index, data_seg_len;
  2213. uint64 total_size;
  2214. WASMDataSeg *dataseg;
  2215. InitializerExpression init_expr;
  2216. #if WASM_ENABLE_BULK_MEMORY != 0
  2217. bool is_passive = false;
  2218. uint32 mem_flag;
  2219. #endif
  2220. read_leb_uint32(p, p_end, data_seg_count);
  2221. #if WASM_ENABLE_BULK_MEMORY != 0
  2222. if ((module->data_seg_count1 != 0)
  2223. && (data_seg_count != module->data_seg_count1)) {
  2224. set_error_buf(error_buf, error_buf_size,
  2225. "data count and data section have inconsistent lengths");
  2226. return false;
  2227. }
  2228. #endif
  2229. if (data_seg_count) {
  2230. module->data_seg_count = data_seg_count;
  2231. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  2232. if (!(module->data_segments =
  2233. loader_malloc(total_size, error_buf, error_buf_size))) {
  2234. return false;
  2235. }
  2236. for (i = 0; i < data_seg_count; i++) {
  2237. read_leb_uint32(p, p_end, mem_index);
  2238. #if WASM_ENABLE_BULK_MEMORY != 0
  2239. is_passive = false;
  2240. mem_flag = mem_index & 0x03;
  2241. switch (mem_flag) {
  2242. case 0x01:
  2243. is_passive = true;
  2244. break;
  2245. case 0x00:
  2246. /* no memory index, treat index as 0 */
  2247. mem_index = 0;
  2248. goto check_mem_index;
  2249. case 0x02:
  2250. /* read following memory index */
  2251. read_leb_uint32(p, p_end, mem_index);
  2252. check_mem_index:
  2253. if (mem_index
  2254. >= module->import_memory_count + module->memory_count) {
  2255. set_error_buf_v(error_buf, error_buf_size,
  2256. "unknown memory %d", mem_index);
  2257. return false;
  2258. }
  2259. break;
  2260. case 0x03:
  2261. default:
  2262. set_error_buf(error_buf, error_buf_size, "unknown memory");
  2263. return false;
  2264. break;
  2265. }
  2266. #else
  2267. if (mem_index
  2268. >= module->import_memory_count + module->memory_count) {
  2269. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  2270. mem_index);
  2271. return false;
  2272. }
  2273. #endif /* WASM_ENABLE_BULK_MEMORY */
  2274. #if WASM_ENABLE_BULK_MEMORY != 0
  2275. if (!is_passive)
  2276. #endif
  2277. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  2278. error_buf, error_buf_size))
  2279. return false;
  2280. read_leb_uint32(p, p_end, data_seg_len);
  2281. if (!(dataseg = module->data_segments[i] = loader_malloc(
  2282. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  2283. return false;
  2284. }
  2285. #if WASM_ENABLE_BULK_MEMORY != 0
  2286. dataseg->is_passive = is_passive;
  2287. if (!is_passive)
  2288. #endif
  2289. {
  2290. bh_memcpy_s(&dataseg->base_offset,
  2291. sizeof(InitializerExpression), &init_expr,
  2292. sizeof(InitializerExpression));
  2293. dataseg->memory_index = mem_index;
  2294. }
  2295. dataseg->data_length = data_seg_len;
  2296. CHECK_BUF(p, p_end, data_seg_len);
  2297. dataseg->data = (uint8 *)p;
  2298. p += data_seg_len;
  2299. }
  2300. }
  2301. if (p != p_end) {
  2302. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2303. return false;
  2304. }
  2305. LOG_VERBOSE("Load data segment section success.\n");
  2306. return true;
  2307. fail:
  2308. return false;
  2309. }
  2310. #if WASM_ENABLE_BULK_MEMORY != 0
  2311. static bool
  2312. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  2313. WASMModule *module, char *error_buf,
  2314. uint32 error_buf_size)
  2315. {
  2316. const uint8 *p = buf, *p_end = buf_end;
  2317. uint32 data_seg_count1 = 0;
  2318. read_leb_uint32(p, p_end, data_seg_count1);
  2319. module->data_seg_count1 = data_seg_count1;
  2320. if (p != p_end) {
  2321. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2322. return false;
  2323. }
  2324. LOG_VERBOSE("Load datacount section success.\n");
  2325. return true;
  2326. fail:
  2327. return false;
  2328. }
  2329. #endif
  2330. static bool
  2331. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  2332. const uint8 *buf_func_end, WASMModule *module,
  2333. char *error_buf, uint32 error_buf_size)
  2334. {
  2335. const uint8 *p = buf, *p_end = buf_end;
  2336. const uint8 *p_func = buf_func;
  2337. uint32 func_count = 0, code_count;
  2338. /* code has been loaded in function section, so pass it here, just check
  2339. * whether function and code section have inconsistent lengths */
  2340. read_leb_uint32(p, p_end, code_count);
  2341. if (buf_func)
  2342. read_leb_uint32(p_func, buf_func_end, func_count);
  2343. if (func_count != code_count) {
  2344. set_error_buf(error_buf, error_buf_size,
  2345. "function and code section have inconsistent lengths");
  2346. return false;
  2347. }
  2348. LOG_VERBOSE("Load code segment section success.\n");
  2349. return true;
  2350. fail:
  2351. return false;
  2352. }
  2353. static bool
  2354. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2355. char *error_buf, uint32 error_buf_size)
  2356. {
  2357. const uint8 *p = buf, *p_end = buf_end;
  2358. WASMType *type;
  2359. uint32 start_function;
  2360. read_leb_uint32(p, p_end, start_function);
  2361. if (start_function
  2362. >= module->function_count + module->import_function_count) {
  2363. set_error_buf(error_buf, error_buf_size, "unknown function");
  2364. return false;
  2365. }
  2366. if (start_function < module->import_function_count)
  2367. type = module->import_functions[start_function].u.function.func_type;
  2368. else
  2369. type = module->functions[start_function - module->import_function_count]
  2370. ->func_type;
  2371. if (type->param_count != 0 || type->result_count != 0) {
  2372. set_error_buf(error_buf, error_buf_size, "invalid start function");
  2373. return false;
  2374. }
  2375. module->start_function = start_function;
  2376. if (p != p_end) {
  2377. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2378. return false;
  2379. }
  2380. LOG_VERBOSE("Load start section success.\n");
  2381. return true;
  2382. fail:
  2383. return false;
  2384. }
  2385. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2386. static bool
  2387. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2388. bool is_load_from_file_buf, char *error_buf,
  2389. uint32 error_buf_size)
  2390. {
  2391. const uint8 *p = buf, *p_end = buf_end;
  2392. uint32 name_type, subsection_size;
  2393. uint32 previous_name_type = 0;
  2394. uint32 num_func_name;
  2395. uint32 func_index;
  2396. uint32 previous_func_index = ~0U;
  2397. uint32 func_name_len;
  2398. uint32 name_index;
  2399. int i = 0;
  2400. if (p >= p_end) {
  2401. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2402. return false;
  2403. }
  2404. while (p < p_end) {
  2405. read_leb_uint32(p, p_end, name_type);
  2406. if (i != 0) {
  2407. if (name_type == previous_name_type) {
  2408. set_error_buf(error_buf, error_buf_size,
  2409. "duplicate sub-section");
  2410. return false;
  2411. }
  2412. if (name_type < previous_name_type) {
  2413. set_error_buf(error_buf, error_buf_size,
  2414. "out-of-order sub-section");
  2415. return false;
  2416. }
  2417. }
  2418. previous_name_type = name_type;
  2419. read_leb_uint32(p, p_end, subsection_size);
  2420. CHECK_BUF(p, p_end, subsection_size);
  2421. switch (name_type) {
  2422. case SUB_SECTION_TYPE_FUNC:
  2423. if (subsection_size) {
  2424. read_leb_uint32(p, p_end, num_func_name);
  2425. for (name_index = 0; name_index < num_func_name;
  2426. name_index++) {
  2427. read_leb_uint32(p, p_end, func_index);
  2428. if (func_index == previous_func_index) {
  2429. set_error_buf(error_buf, error_buf_size,
  2430. "duplicate function name");
  2431. return false;
  2432. }
  2433. if (func_index < previous_func_index
  2434. && previous_func_index != ~0U) {
  2435. set_error_buf(error_buf, error_buf_size,
  2436. "out-of-order function index ");
  2437. return false;
  2438. }
  2439. previous_func_index = func_index;
  2440. read_leb_uint32(p, p_end, func_name_len);
  2441. CHECK_BUF(p, p_end, func_name_len);
  2442. /* Skip the import functions */
  2443. if (func_index >= module->import_count) {
  2444. func_index -= module->import_count;
  2445. if (func_index >= module->function_count) {
  2446. set_error_buf(error_buf, error_buf_size,
  2447. "out-of-range function index");
  2448. return false;
  2449. }
  2450. if (!(module->functions[func_index]->field_name =
  2451. const_str_list_insert(
  2452. p, func_name_len, module,
  2453. is_load_from_file_buf, error_buf,
  2454. error_buf_size))) {
  2455. return false;
  2456. }
  2457. }
  2458. p += func_name_len;
  2459. }
  2460. }
  2461. break;
  2462. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  2463. */
  2464. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  2465. default:
  2466. p = p + subsection_size;
  2467. break;
  2468. }
  2469. i++;
  2470. }
  2471. return true;
  2472. fail:
  2473. return false;
  2474. }
  2475. #endif
  2476. static bool
  2477. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2478. bool is_load_from_file_buf, char *error_buf,
  2479. uint32 error_buf_size)
  2480. {
  2481. const uint8 *p = buf, *p_end = buf_end;
  2482. uint32 name_len;
  2483. if (p >= p_end) {
  2484. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2485. return false;
  2486. }
  2487. read_leb_uint32(p, p_end, name_len);
  2488. if (name_len == 0 || p + name_len > p_end) {
  2489. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2490. return false;
  2491. }
  2492. if (!check_utf8_str(p, name_len)) {
  2493. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  2494. return false;
  2495. }
  2496. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2497. if (memcmp(p, "name", 4) == 0) {
  2498. module->name_section_buf = buf;
  2499. module->name_section_buf_end = buf_end;
  2500. p += name_len;
  2501. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  2502. error_buf_size);
  2503. }
  2504. #endif
  2505. LOG_VERBOSE("Load custom section success.\n");
  2506. return true;
  2507. fail:
  2508. return false;
  2509. }
  2510. static bool
  2511. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2512. uint32 cur_func_idx, char *error_buf,
  2513. uint32 error_buf_size);
  2514. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2515. void **
  2516. wasm_interp_get_handle_table();
  2517. static void **handle_table;
  2518. #endif
  2519. static bool
  2520. load_from_sections(WASMModule *module, WASMSection *sections,
  2521. bool is_load_from_file_buf, char *error_buf,
  2522. uint32 error_buf_size)
  2523. {
  2524. WASMExport *export;
  2525. WASMSection *section = sections;
  2526. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  2527. *buf_func = NULL, *buf_func_end = NULL;
  2528. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2529. WASMGlobal *aux_stack_top_global = NULL, *global;
  2530. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  2531. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  2532. uint32 aux_data_end_global_index = (uint32)-1;
  2533. uint32 aux_heap_base_global_index = (uint32)-1;
  2534. WASMType *func_type;
  2535. /* Find code and function sections if have */
  2536. while (section) {
  2537. if (section->section_type == SECTION_TYPE_CODE) {
  2538. buf_code = section->section_body;
  2539. buf_code_end = buf_code + section->section_body_size;
  2540. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  2541. module->buf_code = (uint8 *)buf_code;
  2542. module->buf_code_size = section->section_body_size;
  2543. #endif
  2544. }
  2545. else if (section->section_type == SECTION_TYPE_FUNC) {
  2546. buf_func = section->section_body;
  2547. buf_func_end = buf_func + section->section_body_size;
  2548. }
  2549. section = section->next;
  2550. }
  2551. section = sections;
  2552. while (section) {
  2553. buf = section->section_body;
  2554. buf_end = buf + section->section_body_size;
  2555. switch (section->section_type) {
  2556. case SECTION_TYPE_USER:
  2557. /* unsupported user section, ignore it. */
  2558. if (!load_user_section(buf, buf_end, module,
  2559. is_load_from_file_buf, error_buf,
  2560. error_buf_size))
  2561. return false;
  2562. break;
  2563. case SECTION_TYPE_TYPE:
  2564. if (!load_type_section(buf, buf_end, module, error_buf,
  2565. error_buf_size))
  2566. return false;
  2567. break;
  2568. case SECTION_TYPE_IMPORT:
  2569. if (!load_import_section(buf, buf_end, module,
  2570. is_load_from_file_buf, error_buf,
  2571. error_buf_size))
  2572. return false;
  2573. break;
  2574. case SECTION_TYPE_FUNC:
  2575. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2576. module, error_buf, error_buf_size))
  2577. return false;
  2578. break;
  2579. case SECTION_TYPE_TABLE:
  2580. if (!load_table_section(buf, buf_end, module, error_buf,
  2581. error_buf_size))
  2582. return false;
  2583. break;
  2584. case SECTION_TYPE_MEMORY:
  2585. if (!load_memory_section(buf, buf_end, module, error_buf,
  2586. error_buf_size))
  2587. return false;
  2588. break;
  2589. case SECTION_TYPE_GLOBAL:
  2590. if (!load_global_section(buf, buf_end, module, error_buf,
  2591. error_buf_size))
  2592. return false;
  2593. break;
  2594. case SECTION_TYPE_EXPORT:
  2595. if (!load_export_section(buf, buf_end, module,
  2596. is_load_from_file_buf, error_buf,
  2597. error_buf_size))
  2598. return false;
  2599. break;
  2600. case SECTION_TYPE_START:
  2601. if (!load_start_section(buf, buf_end, module, error_buf,
  2602. error_buf_size))
  2603. return false;
  2604. break;
  2605. case SECTION_TYPE_ELEM:
  2606. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  2607. error_buf_size))
  2608. return false;
  2609. break;
  2610. case SECTION_TYPE_CODE:
  2611. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2612. module, error_buf, error_buf_size))
  2613. return false;
  2614. break;
  2615. case SECTION_TYPE_DATA:
  2616. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  2617. error_buf_size))
  2618. return false;
  2619. break;
  2620. #if WASM_ENABLE_BULK_MEMORY != 0
  2621. case SECTION_TYPE_DATACOUNT:
  2622. if (!load_datacount_section(buf, buf_end, module, error_buf,
  2623. error_buf_size))
  2624. return false;
  2625. break;
  2626. #endif
  2627. default:
  2628. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2629. return false;
  2630. }
  2631. section = section->next;
  2632. }
  2633. module->aux_data_end_global_index = (uint32)-1;
  2634. module->aux_heap_base_global_index = (uint32)-1;
  2635. module->aux_stack_top_global_index = (uint32)-1;
  2636. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2637. export = module->exports;
  2638. for (i = 0; i < module->export_count; i++, export ++) {
  2639. if (export->kind == EXPORT_KIND_GLOBAL) {
  2640. if (!strcmp(export->name, "__heap_base")) {
  2641. global_index = export->index - module->import_global_count;
  2642. global = module->globals + global_index;
  2643. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2644. && global->init_expr.init_expr_type
  2645. == INIT_EXPR_TYPE_I32_CONST) {
  2646. aux_heap_base_global = global;
  2647. aux_heap_base = global->init_expr.u.i32;
  2648. aux_heap_base_global_index = export->index;
  2649. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2650. aux_heap_base);
  2651. }
  2652. }
  2653. else if (!strcmp(export->name, "__data_end")) {
  2654. global_index = export->index - module->import_global_count;
  2655. global = module->globals + global_index;
  2656. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2657. && global->init_expr.init_expr_type
  2658. == INIT_EXPR_TYPE_I32_CONST) {
  2659. aux_data_end_global = global;
  2660. aux_data_end = global->init_expr.u.i32;
  2661. aux_data_end_global_index = export->index;
  2662. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2663. aux_data_end);
  2664. aux_data_end = align_uint(aux_data_end, 16);
  2665. }
  2666. }
  2667. /* For module compiled with -pthread option, the global is:
  2668. [0] stack_top <-- 0
  2669. [1] tls_pointer
  2670. [2] tls_size
  2671. [3] data_end <-- 3
  2672. [4] global_base
  2673. [5] heap_base <-- 5
  2674. [6] dso_handle
  2675. For module compiled without -pthread option:
  2676. [0] stack_top <-- 0
  2677. [1] data_end <-- 1
  2678. [2] global_base
  2679. [3] heap_base <-- 3
  2680. [4] dso_handle
  2681. */
  2682. if (aux_data_end_global && aux_heap_base_global
  2683. && aux_data_end <= aux_heap_base) {
  2684. module->aux_data_end_global_index = aux_data_end_global_index;
  2685. module->aux_data_end = aux_data_end;
  2686. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2687. module->aux_heap_base = aux_heap_base;
  2688. /* Resolve aux stack top global */
  2689. for (global_index = 0; global_index < module->global_count;
  2690. global_index++) {
  2691. global = module->globals + global_index;
  2692. if (global->is_mutable /* heap_base and data_end is
  2693. not mutable */
  2694. && global->type == VALUE_TYPE_I32
  2695. && global->init_expr.init_expr_type
  2696. == INIT_EXPR_TYPE_I32_CONST
  2697. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2698. aux_stack_top_global = global;
  2699. aux_stack_top = (uint32)global->init_expr.u.i32;
  2700. module->aux_stack_top_global_index =
  2701. module->import_global_count + global_index;
  2702. module->aux_stack_bottom = aux_stack_top;
  2703. module->aux_stack_size =
  2704. aux_stack_top > aux_data_end
  2705. ? aux_stack_top - aux_data_end
  2706. : aux_stack_top;
  2707. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2708. "global index: %d, stack size: %d",
  2709. aux_stack_top, global_index,
  2710. module->aux_stack_size);
  2711. break;
  2712. }
  2713. }
  2714. break;
  2715. }
  2716. }
  2717. }
  2718. module->malloc_function = (uint32)-1;
  2719. module->free_function = (uint32)-1;
  2720. module->retain_function = (uint32)-1;
  2721. /* Resolve malloc/free function exported by wasm module */
  2722. export = module->exports;
  2723. for (i = 0; i < module->export_count; i++, export ++) {
  2724. if (export->kind == EXPORT_KIND_FUNC) {
  2725. if (!strcmp(export->name, "malloc")
  2726. && export->index >= module->import_function_count) {
  2727. func_index = export->index - module->import_function_count;
  2728. func_type = module->functions[func_index]->func_type;
  2729. if (func_type->param_count == 1 && func_type->result_count == 1
  2730. && func_type->types[0] == VALUE_TYPE_I32
  2731. && func_type->types[1] == VALUE_TYPE_I32) {
  2732. bh_assert(module->malloc_function == (uint32)-1);
  2733. module->malloc_function = export->index;
  2734. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2735. export->name, export->index);
  2736. }
  2737. }
  2738. else if (!strcmp(export->name, "__new")
  2739. && export->index >= module->import_function_count) {
  2740. /* __new && __pin for AssemblyScript */
  2741. func_index = export->index - module->import_function_count;
  2742. func_type = module->functions[func_index]->func_type;
  2743. if (func_type->param_count == 2 && func_type->result_count == 1
  2744. && func_type->types[0] == VALUE_TYPE_I32
  2745. && func_type->types[1] == VALUE_TYPE_I32
  2746. && func_type->types[2] == VALUE_TYPE_I32) {
  2747. uint32 j;
  2748. WASMExport *export_tmp;
  2749. bh_assert(module->malloc_function == (uint32)-1);
  2750. module->malloc_function = export->index;
  2751. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2752. export->name, export->index);
  2753. /* resolve retain function.
  2754. If not find, reset malloc function index */
  2755. export_tmp = module->exports;
  2756. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2757. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2758. && (!strcmp(export_tmp->name, "__retain")
  2759. || (!strcmp(export_tmp->name, "__pin")))
  2760. && (export_tmp->index
  2761. >= module->import_function_count)) {
  2762. func_index = export_tmp->index
  2763. - module->import_function_count;
  2764. func_type =
  2765. module->functions[func_index]->func_type;
  2766. if (func_type->param_count == 1
  2767. && func_type->result_count == 1
  2768. && func_type->types[0] == VALUE_TYPE_I32
  2769. && func_type->types[1] == VALUE_TYPE_I32) {
  2770. bh_assert(module->retain_function
  2771. == (uint32)-1);
  2772. module->retain_function = export_tmp->index;
  2773. LOG_VERBOSE("Found retain function, name: %s, "
  2774. "index: %u",
  2775. export_tmp->name,
  2776. export_tmp->index);
  2777. break;
  2778. }
  2779. }
  2780. }
  2781. if (j == module->export_count) {
  2782. module->malloc_function = (uint32)-1;
  2783. LOG_VERBOSE("Can't find retain function,"
  2784. "reset malloc function index to -1");
  2785. }
  2786. }
  2787. }
  2788. else if (((!strcmp(export->name, "free"))
  2789. || (!strcmp(export->name, "__release"))
  2790. || (!strcmp(export->name, "__unpin")))
  2791. && export->index >= module->import_function_count) {
  2792. func_index = export->index - module->import_function_count;
  2793. func_type = module->functions[func_index]->func_type;
  2794. if (func_type->param_count == 1 && func_type->result_count == 0
  2795. && func_type->types[0] == VALUE_TYPE_I32) {
  2796. bh_assert(module->free_function == (uint32)-1);
  2797. module->free_function = export->index;
  2798. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2799. export->name, export->index);
  2800. }
  2801. }
  2802. }
  2803. }
  2804. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2805. handle_table = wasm_interp_get_handle_table();
  2806. #endif
  2807. for (i = 0; i < module->function_count; i++) {
  2808. WASMFunction *func = module->functions[i];
  2809. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  2810. error_buf_size)) {
  2811. return false;
  2812. }
  2813. }
  2814. if (!module->possible_memory_grow) {
  2815. WASMMemoryImport *memory_import;
  2816. WASMMemory *memory;
  2817. if (aux_data_end_global && aux_heap_base_global
  2818. && aux_stack_top_global) {
  2819. uint64 init_memory_size;
  2820. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2821. if (module->import_memory_count) {
  2822. memory_import = &module->import_memories[0].u.memory;
  2823. init_memory_size = (uint64)memory_import->num_bytes_per_page
  2824. * memory_import->init_page_count;
  2825. if (shrunk_memory_size <= init_memory_size) {
  2826. /* Reset memory info to decrease memory usage */
  2827. memory_import->num_bytes_per_page = shrunk_memory_size;
  2828. memory_import->init_page_count = 1;
  2829. LOG_VERBOSE("Shrink import memory size to %d",
  2830. shrunk_memory_size);
  2831. }
  2832. }
  2833. if (module->memory_count) {
  2834. memory = &module->memories[0];
  2835. init_memory_size = (uint64)memory->num_bytes_per_page
  2836. * memory->init_page_count;
  2837. if (shrunk_memory_size <= init_memory_size) {
  2838. /* Reset memory info to decrease memory usage */
  2839. memory->num_bytes_per_page = shrunk_memory_size;
  2840. memory->init_page_count = 1;
  2841. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2842. }
  2843. }
  2844. }
  2845. #if WASM_ENABLE_MULTI_MODULE == 0
  2846. if (module->import_memory_count) {
  2847. memory_import = &module->import_memories[0].u.memory;
  2848. /* Memory init page count cannot be larger than 65536, we don't
  2849. check integer overflow again. */
  2850. memory_import->num_bytes_per_page *= memory_import->init_page_count;
  2851. memory_import->init_page_count = memory_import->max_page_count = 1;
  2852. }
  2853. if (module->memory_count) {
  2854. /* Memory init page count cannot be larger than 65536, we don't
  2855. check integer overflow again. */
  2856. memory = &module->memories[0];
  2857. memory->num_bytes_per_page *= memory->init_page_count;
  2858. memory->init_page_count = memory->max_page_count = 1;
  2859. }
  2860. #endif
  2861. }
  2862. #if WASM_ENABLE_MEMORY_TRACING != 0
  2863. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  2864. #endif
  2865. return true;
  2866. }
  2867. static WASMModule *
  2868. create_module(char *error_buf, uint32 error_buf_size)
  2869. {
  2870. WASMModule *module =
  2871. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  2872. if (!module) {
  2873. return NULL;
  2874. }
  2875. module->module_type = Wasm_Module_Bytecode;
  2876. /* Set start_function to -1, means no start function */
  2877. module->start_function = (uint32)-1;
  2878. #if WASM_ENABLE_MULTI_MODULE != 0
  2879. module->import_module_list = &module->import_module_list_head;
  2880. #endif
  2881. #if WASM_ENABLE_DEBUG_INTERP != 0
  2882. bh_list_init(&module->fast_opcode_list);
  2883. #endif
  2884. return module;
  2885. }
  2886. #if WASM_ENABLE_DEBUG_INTERP != 0
  2887. static void
  2888. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op)
  2889. {
  2890. WASMFastOPCodeNode *fast_op =
  2891. wasm_runtime_malloc(sizeof(WASMFastOPCodeNode));
  2892. if (fast_op) {
  2893. fast_op->offset = pos - module->load_addr;
  2894. fast_op->orig_op = orig_op;
  2895. bh_list_insert(&module->fast_opcode_list, fast_op);
  2896. }
  2897. }
  2898. #endif
  2899. WASMModule *
  2900. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  2901. uint32 error_buf_size)
  2902. {
  2903. WASMModule *module = create_module(error_buf, error_buf_size);
  2904. if (!module)
  2905. return NULL;
  2906. if (!load_from_sections(module, section_list, false, error_buf,
  2907. error_buf_size)) {
  2908. wasm_loader_unload(module);
  2909. return NULL;
  2910. }
  2911. LOG_VERBOSE("Load module from sections success.\n");
  2912. return module;
  2913. }
  2914. static void
  2915. destroy_sections(WASMSection *section_list)
  2916. {
  2917. WASMSection *section = section_list, *next;
  2918. while (section) {
  2919. next = section->next;
  2920. wasm_runtime_free(section);
  2921. section = next;
  2922. }
  2923. }
  2924. /* clang-format off */
  2925. static uint8 section_ids[] = {
  2926. SECTION_TYPE_USER,
  2927. SECTION_TYPE_TYPE,
  2928. SECTION_TYPE_IMPORT,
  2929. SECTION_TYPE_FUNC,
  2930. SECTION_TYPE_TABLE,
  2931. SECTION_TYPE_MEMORY,
  2932. SECTION_TYPE_GLOBAL,
  2933. SECTION_TYPE_EXPORT,
  2934. SECTION_TYPE_START,
  2935. SECTION_TYPE_ELEM,
  2936. #if WASM_ENABLE_BULK_MEMORY != 0
  2937. SECTION_TYPE_DATACOUNT,
  2938. #endif
  2939. SECTION_TYPE_CODE,
  2940. SECTION_TYPE_DATA
  2941. };
  2942. /* clang-format on */
  2943. static uint8
  2944. get_section_index(uint8 section_type)
  2945. {
  2946. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2947. for (uint8 i = 0; i < max_id; i++) {
  2948. if (section_type == section_ids[i])
  2949. return i;
  2950. }
  2951. return (uint8)-1;
  2952. }
  2953. static bool
  2954. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  2955. char *error_buf, uint32 error_buf_size)
  2956. {
  2957. WASMSection *section_list_end = NULL, *section;
  2958. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  2959. uint8 section_type, section_index, last_section_index = (uint8)-1;
  2960. uint32 section_size;
  2961. bh_assert(!*p_section_list);
  2962. p += 8;
  2963. while (p < p_end) {
  2964. CHECK_BUF(p, p_end, 1);
  2965. section_type = read_uint8(p);
  2966. section_index = get_section_index(section_type);
  2967. if (section_index != (uint8)-1) {
  2968. if (section_type != SECTION_TYPE_USER) {
  2969. /* Custom sections may be inserted at any place,
  2970. while other sections must occur at most once
  2971. and in prescribed order. */
  2972. if (last_section_index != (uint8)-1
  2973. && (section_index <= last_section_index)) {
  2974. set_error_buf(error_buf, error_buf_size,
  2975. "junk after last section");
  2976. return false;
  2977. }
  2978. last_section_index = section_index;
  2979. }
  2980. read_leb_uint32(p, p_end, section_size);
  2981. CHECK_BUF1(p, p_end, section_size);
  2982. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  2983. error_buf_size))) {
  2984. return false;
  2985. }
  2986. section->section_type = section_type;
  2987. section->section_body = (uint8 *)p;
  2988. section->section_body_size = section_size;
  2989. if (!section_list_end)
  2990. *p_section_list = section_list_end = section;
  2991. else {
  2992. section_list_end->next = section;
  2993. section_list_end = section;
  2994. }
  2995. p += section_size;
  2996. }
  2997. else {
  2998. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2999. return false;
  3000. }
  3001. }
  3002. return true;
  3003. fail:
  3004. return false;
  3005. }
  3006. static void
  3007. exchange32(uint8 *p_data)
  3008. {
  3009. uint8 value = *p_data;
  3010. *p_data = *(p_data + 3);
  3011. *(p_data + 3) = value;
  3012. value = *(p_data + 1);
  3013. *(p_data + 1) = *(p_data + 2);
  3014. *(p_data + 2) = value;
  3015. }
  3016. static union {
  3017. int a;
  3018. char b;
  3019. } __ue = { .a = 1 };
  3020. #define is_little_endian() (__ue.b == 1)
  3021. static bool
  3022. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  3023. uint32 error_buf_size)
  3024. {
  3025. const uint8 *buf_end = buf + size;
  3026. const uint8 *p = buf, *p_end = buf_end;
  3027. uint32 magic_number, version;
  3028. WASMSection *section_list = NULL;
  3029. CHECK_BUF1(p, p_end, sizeof(uint32));
  3030. magic_number = read_uint32(p);
  3031. if (!is_little_endian())
  3032. exchange32((uint8 *)&magic_number);
  3033. if (magic_number != WASM_MAGIC_NUMBER) {
  3034. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  3035. return false;
  3036. }
  3037. CHECK_BUF1(p, p_end, sizeof(uint32));
  3038. version = read_uint32(p);
  3039. if (!is_little_endian())
  3040. exchange32((uint8 *)&version);
  3041. if (version != WASM_CURRENT_VERSION) {
  3042. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3043. return false;
  3044. }
  3045. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  3046. || !load_from_sections(module, section_list, true, error_buf,
  3047. error_buf_size)) {
  3048. destroy_sections(section_list);
  3049. return false;
  3050. }
  3051. destroy_sections(section_list);
  3052. return true;
  3053. fail:
  3054. return false;
  3055. }
  3056. WASMModule *
  3057. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf,
  3058. uint32 error_buf_size)
  3059. {
  3060. WASMModule *module = create_module(error_buf, error_buf_size);
  3061. if (!module) {
  3062. return NULL;
  3063. }
  3064. #if WASM_ENABLE_DEBUG_INTERP != 0
  3065. module->load_addr = (uint8 *)buf;
  3066. module->load_size = size;
  3067. #endif
  3068. if (!load(buf, size, module, error_buf, error_buf_size)) {
  3069. goto fail;
  3070. }
  3071. LOG_VERBOSE("Load module success.\n");
  3072. return module;
  3073. fail:
  3074. wasm_loader_unload(module);
  3075. return NULL;
  3076. }
  3077. void
  3078. wasm_loader_unload(WASMModule *module)
  3079. {
  3080. uint32 i;
  3081. if (!module)
  3082. return;
  3083. if (module->types) {
  3084. for (i = 0; i < module->type_count; i++) {
  3085. if (module->types[i])
  3086. wasm_runtime_free(module->types[i]);
  3087. }
  3088. wasm_runtime_free(module->types);
  3089. }
  3090. if (module->imports)
  3091. wasm_runtime_free(module->imports);
  3092. if (module->functions) {
  3093. for (i = 0; i < module->function_count; i++) {
  3094. if (module->functions[i]) {
  3095. if (module->functions[i]->local_offsets)
  3096. wasm_runtime_free(module->functions[i]->local_offsets);
  3097. #if WASM_ENABLE_FAST_INTERP != 0
  3098. if (module->functions[i]->code_compiled)
  3099. wasm_runtime_free(module->functions[i]->code_compiled);
  3100. if (module->functions[i]->consts)
  3101. wasm_runtime_free(module->functions[i]->consts);
  3102. #endif
  3103. wasm_runtime_free(module->functions[i]);
  3104. }
  3105. }
  3106. wasm_runtime_free(module->functions);
  3107. }
  3108. if (module->tables)
  3109. wasm_runtime_free(module->tables);
  3110. if (module->memories)
  3111. wasm_runtime_free(module->memories);
  3112. if (module->globals)
  3113. wasm_runtime_free(module->globals);
  3114. if (module->exports)
  3115. wasm_runtime_free(module->exports);
  3116. if (module->table_segments) {
  3117. for (i = 0; i < module->table_seg_count; i++) {
  3118. if (module->table_segments[i].func_indexes)
  3119. wasm_runtime_free(module->table_segments[i].func_indexes);
  3120. }
  3121. wasm_runtime_free(module->table_segments);
  3122. }
  3123. if (module->data_segments) {
  3124. for (i = 0; i < module->data_seg_count; i++) {
  3125. if (module->data_segments[i])
  3126. wasm_runtime_free(module->data_segments[i]);
  3127. }
  3128. wasm_runtime_free(module->data_segments);
  3129. }
  3130. if (module->const_str_list) {
  3131. StringNode *node = module->const_str_list, *node_next;
  3132. while (node) {
  3133. node_next = node->next;
  3134. wasm_runtime_free(node);
  3135. node = node_next;
  3136. }
  3137. }
  3138. #if WASM_ENABLE_MULTI_MODULE != 0
  3139. /* just release the sub module list */
  3140. if (module->import_module_list) {
  3141. WASMRegisteredModule *node =
  3142. bh_list_first_elem(module->import_module_list);
  3143. while (node) {
  3144. WASMRegisteredModule *next = bh_list_elem_next(node);
  3145. bh_list_remove(module->import_module_list, node);
  3146. /*
  3147. * unload(sub_module) will be trigged during runtime_destroy().
  3148. * every module in the global module list will be unloaded one by
  3149. * one. so don't worry.
  3150. */
  3151. wasm_runtime_free(node);
  3152. /*
  3153. * the module file reading buffer will be released
  3154. * in runtime_destroy()
  3155. */
  3156. node = next;
  3157. }
  3158. }
  3159. #endif
  3160. #if WASM_ENABLE_DEBUG_INTERP != 0
  3161. WASMFastOPCodeNode *fast_opcode =
  3162. bh_list_first_elem(&module->fast_opcode_list);
  3163. while (fast_opcode) {
  3164. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  3165. wasm_runtime_free(fast_opcode);
  3166. fast_opcode = next;
  3167. }
  3168. #endif
  3169. wasm_runtime_free(module);
  3170. }
  3171. bool
  3172. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  3173. const uint8 *start_addr, const uint8 *code_end_addr,
  3174. uint8 label_type, uint8 **p_else_addr,
  3175. uint8 **p_end_addr)
  3176. {
  3177. const uint8 *p = start_addr, *p_end = code_end_addr;
  3178. uint8 *else_addr = NULL;
  3179. char error_buf[128];
  3180. uint32 block_nested_depth = 1, count, i, j, t;
  3181. uint32 error_buf_size = sizeof(error_buf);
  3182. uint8 opcode, u8;
  3183. BlockAddr block_stack[16] = { { 0 } }, *block;
  3184. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3185. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3186. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  3187. if (block[j].start_addr == start_addr) {
  3188. /* Cache hit */
  3189. *p_else_addr = block[j].else_addr;
  3190. *p_end_addr = block[j].end_addr;
  3191. return true;
  3192. }
  3193. }
  3194. /* Cache unhit */
  3195. block_stack[0].start_addr = start_addr;
  3196. while (p < code_end_addr) {
  3197. opcode = *p++;
  3198. #if WASM_ENABLE_DEBUG_INTERP != 0
  3199. op_break_retry:
  3200. #endif
  3201. switch (opcode) {
  3202. case WASM_OP_UNREACHABLE:
  3203. case WASM_OP_NOP:
  3204. break;
  3205. case WASM_OP_BLOCK:
  3206. case WASM_OP_LOOP:
  3207. case WASM_OP_IF:
  3208. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  3209. u8 = read_uint8(p);
  3210. if (block_nested_depth
  3211. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3212. block_stack[block_nested_depth].start_addr = p;
  3213. block_stack[block_nested_depth].else_addr = NULL;
  3214. }
  3215. block_nested_depth++;
  3216. break;
  3217. case EXT_OP_BLOCK:
  3218. case EXT_OP_LOOP:
  3219. case EXT_OP_IF:
  3220. /* block type */
  3221. skip_leb_uint32(p, p_end);
  3222. if (block_nested_depth
  3223. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3224. block_stack[block_nested_depth].start_addr = p;
  3225. block_stack[block_nested_depth].else_addr = NULL;
  3226. }
  3227. block_nested_depth++;
  3228. break;
  3229. case WASM_OP_ELSE:
  3230. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  3231. else_addr = (uint8 *)(p - 1);
  3232. if (block_nested_depth - 1
  3233. < sizeof(block_stack) / sizeof(BlockAddr))
  3234. block_stack[block_nested_depth - 1].else_addr =
  3235. (uint8 *)(p - 1);
  3236. break;
  3237. case WASM_OP_END:
  3238. if (block_nested_depth == 1) {
  3239. if (label_type == LABEL_TYPE_IF)
  3240. *p_else_addr = else_addr;
  3241. *p_end_addr = (uint8 *)(p - 1);
  3242. block_stack[0].end_addr = (uint8 *)(p - 1);
  3243. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  3244. t++) {
  3245. start_addr = block_stack[t].start_addr;
  3246. if (start_addr) {
  3247. i = ((uintptr_t)start_addr)
  3248. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3249. block =
  3250. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3251. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  3252. if (!block[j].start_addr)
  3253. break;
  3254. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  3255. memmove(block + 1, block,
  3256. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  3257. * sizeof(BlockAddr));
  3258. j = 0;
  3259. }
  3260. block[j].start_addr = block_stack[t].start_addr;
  3261. block[j].else_addr = block_stack[t].else_addr;
  3262. block[j].end_addr = block_stack[t].end_addr;
  3263. }
  3264. else
  3265. break;
  3266. }
  3267. return true;
  3268. }
  3269. else {
  3270. block_nested_depth--;
  3271. if (block_nested_depth
  3272. < sizeof(block_stack) / sizeof(BlockAddr))
  3273. block_stack[block_nested_depth].end_addr =
  3274. (uint8 *)(p - 1);
  3275. }
  3276. break;
  3277. case WASM_OP_BR:
  3278. case WASM_OP_BR_IF:
  3279. skip_leb_uint32(p, p_end); /* labelidx */
  3280. break;
  3281. case WASM_OP_BR_TABLE:
  3282. read_leb_uint32(p, p_end, count); /* lable num */
  3283. for (i = 0; i <= count; i++) /* lableidxs */
  3284. skip_leb_uint32(p, p_end);
  3285. break;
  3286. case WASM_OP_RETURN:
  3287. break;
  3288. case WASM_OP_CALL:
  3289. #if WASM_ENABLE_TAIL_CALL != 0
  3290. case WASM_OP_RETURN_CALL:
  3291. #endif
  3292. skip_leb_uint32(p, p_end); /* funcidx */
  3293. break;
  3294. case WASM_OP_CALL_INDIRECT:
  3295. #if WASM_ENABLE_TAIL_CALL != 0
  3296. case WASM_OP_RETURN_CALL_INDIRECT:
  3297. #endif
  3298. skip_leb_uint32(p, p_end); /* typeidx */
  3299. CHECK_BUF(p, p_end, 1);
  3300. u8 = read_uint8(p); /* 0x00 */
  3301. break;
  3302. case WASM_OP_DROP:
  3303. case WASM_OP_SELECT:
  3304. case WASM_OP_DROP_64:
  3305. case WASM_OP_SELECT_64:
  3306. break;
  3307. #if WASM_ENABLE_REF_TYPES != 0
  3308. case WASM_OP_SELECT_T:
  3309. skip_leb_uint32(p, p_end); /* vec length */
  3310. CHECK_BUF(p, p_end, 1);
  3311. u8 = read_uint8(p); /* typeidx */
  3312. break;
  3313. case WASM_OP_TABLE_GET:
  3314. case WASM_OP_TABLE_SET:
  3315. skip_leb_uint32(p, p_end); /* table index */
  3316. break;
  3317. case WASM_OP_REF_NULL:
  3318. CHECK_BUF(p, p_end, 1);
  3319. u8 = read_uint8(p); /* type */
  3320. break;
  3321. case WASM_OP_REF_IS_NULL:
  3322. break;
  3323. case WASM_OP_REF_FUNC:
  3324. skip_leb_uint32(p, p_end); /* func index */
  3325. break;
  3326. #endif /* WASM_ENABLE_REF_TYPES */
  3327. case WASM_OP_GET_LOCAL:
  3328. case WASM_OP_SET_LOCAL:
  3329. case WASM_OP_TEE_LOCAL:
  3330. case WASM_OP_GET_GLOBAL:
  3331. case WASM_OP_SET_GLOBAL:
  3332. case WASM_OP_GET_GLOBAL_64:
  3333. case WASM_OP_SET_GLOBAL_64:
  3334. case WASM_OP_SET_GLOBAL_AUX_STACK:
  3335. skip_leb_uint32(p, p_end); /* local index */
  3336. break;
  3337. case EXT_OP_GET_LOCAL_FAST:
  3338. case EXT_OP_SET_LOCAL_FAST:
  3339. case EXT_OP_TEE_LOCAL_FAST:
  3340. CHECK_BUF(p, p_end, 1);
  3341. p++;
  3342. break;
  3343. case WASM_OP_I32_LOAD:
  3344. case WASM_OP_I64_LOAD:
  3345. case WASM_OP_F32_LOAD:
  3346. case WASM_OP_F64_LOAD:
  3347. case WASM_OP_I32_LOAD8_S:
  3348. case WASM_OP_I32_LOAD8_U:
  3349. case WASM_OP_I32_LOAD16_S:
  3350. case WASM_OP_I32_LOAD16_U:
  3351. case WASM_OP_I64_LOAD8_S:
  3352. case WASM_OP_I64_LOAD8_U:
  3353. case WASM_OP_I64_LOAD16_S:
  3354. case WASM_OP_I64_LOAD16_U:
  3355. case WASM_OP_I64_LOAD32_S:
  3356. case WASM_OP_I64_LOAD32_U:
  3357. case WASM_OP_I32_STORE:
  3358. case WASM_OP_I64_STORE:
  3359. case WASM_OP_F32_STORE:
  3360. case WASM_OP_F64_STORE:
  3361. case WASM_OP_I32_STORE8:
  3362. case WASM_OP_I32_STORE16:
  3363. case WASM_OP_I64_STORE8:
  3364. case WASM_OP_I64_STORE16:
  3365. case WASM_OP_I64_STORE32:
  3366. skip_leb_uint32(p, p_end); /* align */
  3367. skip_leb_uint32(p, p_end); /* offset */
  3368. break;
  3369. case WASM_OP_MEMORY_SIZE:
  3370. case WASM_OP_MEMORY_GROW:
  3371. skip_leb_uint32(p, p_end); /* 0x00 */
  3372. break;
  3373. case WASM_OP_I32_CONST:
  3374. skip_leb_int32(p, p_end);
  3375. break;
  3376. case WASM_OP_I64_CONST:
  3377. skip_leb_int64(p, p_end);
  3378. break;
  3379. case WASM_OP_F32_CONST:
  3380. p += sizeof(float32);
  3381. break;
  3382. case WASM_OP_F64_CONST:
  3383. p += sizeof(float64);
  3384. break;
  3385. case WASM_OP_I32_EQZ:
  3386. case WASM_OP_I32_EQ:
  3387. case WASM_OP_I32_NE:
  3388. case WASM_OP_I32_LT_S:
  3389. case WASM_OP_I32_LT_U:
  3390. case WASM_OP_I32_GT_S:
  3391. case WASM_OP_I32_GT_U:
  3392. case WASM_OP_I32_LE_S:
  3393. case WASM_OP_I32_LE_U:
  3394. case WASM_OP_I32_GE_S:
  3395. case WASM_OP_I32_GE_U:
  3396. case WASM_OP_I64_EQZ:
  3397. case WASM_OP_I64_EQ:
  3398. case WASM_OP_I64_NE:
  3399. case WASM_OP_I64_LT_S:
  3400. case WASM_OP_I64_LT_U:
  3401. case WASM_OP_I64_GT_S:
  3402. case WASM_OP_I64_GT_U:
  3403. case WASM_OP_I64_LE_S:
  3404. case WASM_OP_I64_LE_U:
  3405. case WASM_OP_I64_GE_S:
  3406. case WASM_OP_I64_GE_U:
  3407. case WASM_OP_F32_EQ:
  3408. case WASM_OP_F32_NE:
  3409. case WASM_OP_F32_LT:
  3410. case WASM_OP_F32_GT:
  3411. case WASM_OP_F32_LE:
  3412. case WASM_OP_F32_GE:
  3413. case WASM_OP_F64_EQ:
  3414. case WASM_OP_F64_NE:
  3415. case WASM_OP_F64_LT:
  3416. case WASM_OP_F64_GT:
  3417. case WASM_OP_F64_LE:
  3418. case WASM_OP_F64_GE:
  3419. case WASM_OP_I32_CLZ:
  3420. case WASM_OP_I32_CTZ:
  3421. case WASM_OP_I32_POPCNT:
  3422. case WASM_OP_I32_ADD:
  3423. case WASM_OP_I32_SUB:
  3424. case WASM_OP_I32_MUL:
  3425. case WASM_OP_I32_DIV_S:
  3426. case WASM_OP_I32_DIV_U:
  3427. case WASM_OP_I32_REM_S:
  3428. case WASM_OP_I32_REM_U:
  3429. case WASM_OP_I32_AND:
  3430. case WASM_OP_I32_OR:
  3431. case WASM_OP_I32_XOR:
  3432. case WASM_OP_I32_SHL:
  3433. case WASM_OP_I32_SHR_S:
  3434. case WASM_OP_I32_SHR_U:
  3435. case WASM_OP_I32_ROTL:
  3436. case WASM_OP_I32_ROTR:
  3437. case WASM_OP_I64_CLZ:
  3438. case WASM_OP_I64_CTZ:
  3439. case WASM_OP_I64_POPCNT:
  3440. case WASM_OP_I64_ADD:
  3441. case WASM_OP_I64_SUB:
  3442. case WASM_OP_I64_MUL:
  3443. case WASM_OP_I64_DIV_S:
  3444. case WASM_OP_I64_DIV_U:
  3445. case WASM_OP_I64_REM_S:
  3446. case WASM_OP_I64_REM_U:
  3447. case WASM_OP_I64_AND:
  3448. case WASM_OP_I64_OR:
  3449. case WASM_OP_I64_XOR:
  3450. case WASM_OP_I64_SHL:
  3451. case WASM_OP_I64_SHR_S:
  3452. case WASM_OP_I64_SHR_U:
  3453. case WASM_OP_I64_ROTL:
  3454. case WASM_OP_I64_ROTR:
  3455. case WASM_OP_F32_ABS:
  3456. case WASM_OP_F32_NEG:
  3457. case WASM_OP_F32_CEIL:
  3458. case WASM_OP_F32_FLOOR:
  3459. case WASM_OP_F32_TRUNC:
  3460. case WASM_OP_F32_NEAREST:
  3461. case WASM_OP_F32_SQRT:
  3462. case WASM_OP_F32_ADD:
  3463. case WASM_OP_F32_SUB:
  3464. case WASM_OP_F32_MUL:
  3465. case WASM_OP_F32_DIV:
  3466. case WASM_OP_F32_MIN:
  3467. case WASM_OP_F32_MAX:
  3468. case WASM_OP_F32_COPYSIGN:
  3469. case WASM_OP_F64_ABS:
  3470. case WASM_OP_F64_NEG:
  3471. case WASM_OP_F64_CEIL:
  3472. case WASM_OP_F64_FLOOR:
  3473. case WASM_OP_F64_TRUNC:
  3474. case WASM_OP_F64_NEAREST:
  3475. case WASM_OP_F64_SQRT:
  3476. case WASM_OP_F64_ADD:
  3477. case WASM_OP_F64_SUB:
  3478. case WASM_OP_F64_MUL:
  3479. case WASM_OP_F64_DIV:
  3480. case WASM_OP_F64_MIN:
  3481. case WASM_OP_F64_MAX:
  3482. case WASM_OP_F64_COPYSIGN:
  3483. case WASM_OP_I32_WRAP_I64:
  3484. case WASM_OP_I32_TRUNC_S_F32:
  3485. case WASM_OP_I32_TRUNC_U_F32:
  3486. case WASM_OP_I32_TRUNC_S_F64:
  3487. case WASM_OP_I32_TRUNC_U_F64:
  3488. case WASM_OP_I64_EXTEND_S_I32:
  3489. case WASM_OP_I64_EXTEND_U_I32:
  3490. case WASM_OP_I64_TRUNC_S_F32:
  3491. case WASM_OP_I64_TRUNC_U_F32:
  3492. case WASM_OP_I64_TRUNC_S_F64:
  3493. case WASM_OP_I64_TRUNC_U_F64:
  3494. case WASM_OP_F32_CONVERT_S_I32:
  3495. case WASM_OP_F32_CONVERT_U_I32:
  3496. case WASM_OP_F32_CONVERT_S_I64:
  3497. case WASM_OP_F32_CONVERT_U_I64:
  3498. case WASM_OP_F32_DEMOTE_F64:
  3499. case WASM_OP_F64_CONVERT_S_I32:
  3500. case WASM_OP_F64_CONVERT_U_I32:
  3501. case WASM_OP_F64_CONVERT_S_I64:
  3502. case WASM_OP_F64_CONVERT_U_I64:
  3503. case WASM_OP_F64_PROMOTE_F32:
  3504. case WASM_OP_I32_REINTERPRET_F32:
  3505. case WASM_OP_I64_REINTERPRET_F64:
  3506. case WASM_OP_F32_REINTERPRET_I32:
  3507. case WASM_OP_F64_REINTERPRET_I64:
  3508. case WASM_OP_I32_EXTEND8_S:
  3509. case WASM_OP_I32_EXTEND16_S:
  3510. case WASM_OP_I64_EXTEND8_S:
  3511. case WASM_OP_I64_EXTEND16_S:
  3512. case WASM_OP_I64_EXTEND32_S:
  3513. break;
  3514. case WASM_OP_MISC_PREFIX:
  3515. {
  3516. uint32 opcode1;
  3517. read_leb_uint32(p, p_end, opcode1);
  3518. switch (opcode1) {
  3519. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3520. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3521. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3522. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3523. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3524. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3525. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3526. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3527. break;
  3528. #if WASM_ENABLE_BULK_MEMORY != 0
  3529. case WASM_OP_MEMORY_INIT:
  3530. skip_leb_uint32(p, p_end);
  3531. /* skip memory idx */
  3532. p++;
  3533. break;
  3534. case WASM_OP_DATA_DROP:
  3535. skip_leb_uint32(p, p_end);
  3536. break;
  3537. case WASM_OP_MEMORY_COPY:
  3538. /* skip two memory idx */
  3539. p += 2;
  3540. break;
  3541. case WASM_OP_MEMORY_FILL:
  3542. /* skip memory idx */
  3543. p++;
  3544. break;
  3545. #endif /* WASM_ENABLE_BULK_MEMORY */
  3546. #if WASM_ENABLE_REF_TYPES != 0
  3547. case WASM_OP_TABLE_INIT:
  3548. case WASM_OP_TABLE_COPY:
  3549. /* tableidx */
  3550. skip_leb_uint32(p, p_end);
  3551. /* elemidx */
  3552. skip_leb_uint32(p, p_end);
  3553. break;
  3554. case WASM_OP_ELEM_DROP:
  3555. /* elemidx */
  3556. skip_leb_uint32(p, p_end);
  3557. break;
  3558. case WASM_OP_TABLE_SIZE:
  3559. case WASM_OP_TABLE_GROW:
  3560. case WASM_OP_TABLE_FILL:
  3561. skip_leb_uint32(p, p_end); /* table idx */
  3562. break;
  3563. #endif /* WASM_ENABLE_REF_TYPES */
  3564. default:
  3565. return false;
  3566. }
  3567. break;
  3568. }
  3569. #if WASM_ENABLE_SIMD != 0
  3570. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3571. case WASM_OP_SIMD_PREFIX:
  3572. {
  3573. /* TODO: shall we ceate a table to be friendly to branch
  3574. * prediction */
  3575. opcode = read_uint8(p);
  3576. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  3577. */
  3578. switch (opcode) {
  3579. case SIMD_v128_load:
  3580. case SIMD_v128_load8x8_s:
  3581. case SIMD_v128_load8x8_u:
  3582. case SIMD_v128_load16x4_s:
  3583. case SIMD_v128_load16x4_u:
  3584. case SIMD_v128_load32x2_s:
  3585. case SIMD_v128_load32x2_u:
  3586. case SIMD_v128_load8_splat:
  3587. case SIMD_v128_load16_splat:
  3588. case SIMD_v128_load32_splat:
  3589. case SIMD_v128_load64_splat:
  3590. case SIMD_v128_store:
  3591. /* memarg align */
  3592. skip_leb_uint32(p, p_end);
  3593. /* memarg offset*/
  3594. skip_leb_uint32(p, p_end);
  3595. break;
  3596. case SIMD_v128_const:
  3597. case SIMD_v8x16_shuffle:
  3598. /* immByte[16] immLaneId[16] */
  3599. CHECK_BUF1(p, p_end, 16);
  3600. p += 16;
  3601. break;
  3602. case SIMD_i8x16_extract_lane_s:
  3603. case SIMD_i8x16_extract_lane_u:
  3604. case SIMD_i8x16_replace_lane:
  3605. case SIMD_i16x8_extract_lane_s:
  3606. case SIMD_i16x8_extract_lane_u:
  3607. case SIMD_i16x8_replace_lane:
  3608. case SIMD_i32x4_extract_lane:
  3609. case SIMD_i32x4_replace_lane:
  3610. case SIMD_i64x2_extract_lane:
  3611. case SIMD_i64x2_replace_lane:
  3612. case SIMD_f32x4_extract_lane:
  3613. case SIMD_f32x4_replace_lane:
  3614. case SIMD_f64x2_extract_lane:
  3615. case SIMD_f64x2_replace_lane:
  3616. /* ImmLaneId */
  3617. CHECK_BUF(p, p_end, 1);
  3618. p++;
  3619. break;
  3620. case SIMD_v128_load8_lane:
  3621. case SIMD_v128_load16_lane:
  3622. case SIMD_v128_load32_lane:
  3623. case SIMD_v128_load64_lane:
  3624. case SIMD_v128_store8_lane:
  3625. case SIMD_v128_store16_lane:
  3626. case SIMD_v128_store32_lane:
  3627. case SIMD_v128_store64_lane:
  3628. /* memarg align */
  3629. skip_leb_uint32(p, p_end);
  3630. /* memarg offset*/
  3631. skip_leb_uint32(p, p_end);
  3632. /* ImmLaneId */
  3633. CHECK_BUF(p, p_end, 1);
  3634. p++;
  3635. break;
  3636. case SIMD_v128_load32_zero:
  3637. case SIMD_v128_load64_zero:
  3638. /* memarg align */
  3639. skip_leb_uint32(p, p_end);
  3640. /* memarg offset*/
  3641. skip_leb_uint32(p, p_end);
  3642. break;
  3643. default:
  3644. /*
  3645. * since latest SIMD specific used almost every value
  3646. * from 0x00 to 0xff, the default branch will present
  3647. * all opcodes without imm
  3648. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  3649. */
  3650. break;
  3651. }
  3652. break;
  3653. }
  3654. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  3655. #endif /* end of WASM_ENABLE_SIMD */
  3656. #if WASM_ENABLE_SHARED_MEMORY != 0
  3657. case WASM_OP_ATOMIC_PREFIX:
  3658. {
  3659. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3660. opcode = read_uint8(p);
  3661. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3662. skip_leb_uint32(p, p_end); /* align */
  3663. skip_leb_uint32(p, p_end); /* offset */
  3664. }
  3665. else {
  3666. /* atomic.fence doesn't have memarg */
  3667. p++;
  3668. }
  3669. break;
  3670. }
  3671. #endif
  3672. #if WASM_ENABLE_DEBUG_INTERP != 0
  3673. case DEBUG_OP_BREAK:
  3674. {
  3675. WASMDebugInstance *debug_instance =
  3676. wasm_exec_env_get_instance(exec_env);
  3677. char orignal_opcode[1];
  3678. uint64 size = 1;
  3679. WASMModuleInstance *module_inst =
  3680. (WASMModuleInstance *)exec_env->module_inst;
  3681. uint64 offset = (p - 1) >= module_inst->module->load_addr
  3682. ? (p - 1) - module_inst->module->load_addr
  3683. : ~0;
  3684. if (debug_instance) {
  3685. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  3686. orignal_opcode, &size)
  3687. && size == 1) {
  3688. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  3689. "with %02x: ",
  3690. orignal_opcode[0]);
  3691. opcode = orignal_opcode[0];
  3692. goto op_break_retry;
  3693. }
  3694. }
  3695. break;
  3696. }
  3697. #endif
  3698. default:
  3699. return false;
  3700. }
  3701. }
  3702. (void)u8;
  3703. return false;
  3704. fail:
  3705. return false;
  3706. }
  3707. #define REF_ANY VALUE_TYPE_ANY
  3708. #define REF_I32 VALUE_TYPE_I32
  3709. #define REF_F32 VALUE_TYPE_F32
  3710. #define REF_I64_1 VALUE_TYPE_I64
  3711. #define REF_I64_2 VALUE_TYPE_I64
  3712. #define REF_F64_1 VALUE_TYPE_F64
  3713. #define REF_F64_2 VALUE_TYPE_F64
  3714. #define REF_V128_1 VALUE_TYPE_V128
  3715. #define REF_V128_2 VALUE_TYPE_V128
  3716. #define REF_V128_3 VALUE_TYPE_V128
  3717. #define REF_V128_4 VALUE_TYPE_V128
  3718. #define REF_FUNCREF VALUE_TYPE_FUNCREF
  3719. #define REF_EXTERNREF VALUE_TYPE_EXTERNREF
  3720. #if WASM_ENABLE_FAST_INTERP != 0
  3721. #if WASM_DEBUG_PREPROCESSOR != 0
  3722. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3723. #else
  3724. #define LOG_OP(...) (void)0
  3725. #endif
  3726. #define PATCH_ELSE 0
  3727. #define PATCH_END 1
  3728. typedef struct BranchBlockPatch {
  3729. struct BranchBlockPatch *next;
  3730. uint8 patch_type;
  3731. uint8 *code_compiled;
  3732. } BranchBlockPatch;
  3733. #endif
  3734. typedef struct BranchBlock {
  3735. uint8 label_type;
  3736. BlockType block_type;
  3737. uint8 *start_addr;
  3738. uint8 *else_addr;
  3739. uint8 *end_addr;
  3740. uint32 stack_cell_num;
  3741. #if WASM_ENABLE_FAST_INTERP != 0
  3742. uint16 dynamic_offset;
  3743. uint8 *code_compiled;
  3744. BranchBlockPatch *patch_list;
  3745. /* This is used to save params frame_offset of of if block */
  3746. int16 *param_frame_offsets;
  3747. #endif
  3748. /* Indicate the operand stack is in polymorphic state.
  3749. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3750. * to polymorphic state until the block's 'end' opcode is processed.
  3751. * If stack is in polymorphic state and stack is empty, instruction can
  3752. * pop any type of value directly without decreasing stack top pointer
  3753. * and stack cell num. */
  3754. bool is_stack_polymorphic;
  3755. } BranchBlock;
  3756. typedef struct WASMLoaderContext {
  3757. /* frame ref stack */
  3758. uint8 *frame_ref;
  3759. uint8 *frame_ref_bottom;
  3760. uint8 *frame_ref_boundary;
  3761. uint32 frame_ref_size;
  3762. uint32 stack_cell_num;
  3763. uint32 max_stack_cell_num;
  3764. /* frame csp stack */
  3765. BranchBlock *frame_csp;
  3766. BranchBlock *frame_csp_bottom;
  3767. BranchBlock *frame_csp_boundary;
  3768. uint32 frame_csp_size;
  3769. uint32 csp_num;
  3770. uint32 max_csp_num;
  3771. #if WASM_ENABLE_FAST_INTERP != 0
  3772. /* frame offset stack */
  3773. int16 *frame_offset;
  3774. int16 *frame_offset_bottom;
  3775. int16 *frame_offset_boundary;
  3776. uint32 frame_offset_size;
  3777. int16 dynamic_offset;
  3778. int16 start_dynamic_offset;
  3779. int16 max_dynamic_offset;
  3780. /* preserved local offset */
  3781. int16 preserved_local_offset;
  3782. /* const buffer */
  3783. uint8 *const_buf;
  3784. uint16 num_const;
  3785. uint16 const_buf_size;
  3786. uint16 const_cell_num;
  3787. /* processed code */
  3788. uint8 *p_code_compiled;
  3789. uint8 *p_code_compiled_end;
  3790. uint32 code_compiled_size;
  3791. #endif
  3792. } WASMLoaderContext;
  3793. typedef struct Const {
  3794. WASMValue value;
  3795. uint16 slot_index;
  3796. uint8 value_type;
  3797. } Const;
  3798. static void *
  3799. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  3800. uint32 error_buf_size)
  3801. {
  3802. uint8 *mem_new;
  3803. bh_assert(size_new > size_old);
  3804. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  3805. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  3806. memset(mem_new + size_old, 0, size_new - size_old);
  3807. wasm_runtime_free(mem_old);
  3808. }
  3809. return mem_new;
  3810. }
  3811. #define MEM_REALLOC(mem, size_old, size_new) \
  3812. do { \
  3813. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  3814. error_buf_size); \
  3815. if (!mem_new) \
  3816. goto fail; \
  3817. mem = mem_new; \
  3818. } while (0)
  3819. #define CHECK_CSP_PUSH() \
  3820. do { \
  3821. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3822. MEM_REALLOC( \
  3823. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3824. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  3825. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3826. ctx->frame_csp_boundary = \
  3827. ctx->frame_csp_bottom \
  3828. + ctx->frame_csp_size / sizeof(BranchBlock); \
  3829. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3830. } \
  3831. } while (0)
  3832. #define CHECK_CSP_POP() \
  3833. do { \
  3834. if (ctx->csp_num < 1) { \
  3835. set_error_buf(error_buf, error_buf_size, \
  3836. "type mismatch: " \
  3837. "expect data but block stack was empty"); \
  3838. goto fail; \
  3839. } \
  3840. } while (0)
  3841. #if WASM_ENABLE_FAST_INTERP != 0
  3842. static bool
  3843. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  3844. uint32 error_buf_size)
  3845. {
  3846. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  3847. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3848. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3849. ctx->frame_offset_size + 16);
  3850. ctx->frame_offset_size += 16;
  3851. ctx->frame_offset_boundary =
  3852. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  3853. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3854. }
  3855. return true;
  3856. fail:
  3857. return false;
  3858. }
  3859. static bool
  3860. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3861. {
  3862. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3863. return false;
  3864. return true;
  3865. }
  3866. static void
  3867. free_label_patch_list(BranchBlock *frame_csp)
  3868. {
  3869. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3870. BranchBlockPatch *next;
  3871. while (label_patch != NULL) {
  3872. next = label_patch->next;
  3873. wasm_runtime_free(label_patch);
  3874. label_patch = next;
  3875. }
  3876. frame_csp->patch_list = NULL;
  3877. }
  3878. static void
  3879. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3880. {
  3881. BranchBlock *tmp_csp = frame_csp;
  3882. for (uint32 i = 0; i < csp_num; i++) {
  3883. free_label_patch_list(tmp_csp);
  3884. tmp_csp++;
  3885. }
  3886. }
  3887. #endif /* end of WASM_ENABLE_FAST_INTERP */
  3888. static bool
  3889. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  3890. {
  3891. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3892. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3893. ctx->frame_ref_size + 16);
  3894. ctx->frame_ref_size += 16;
  3895. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3896. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3897. }
  3898. return true;
  3899. fail:
  3900. return false;
  3901. }
  3902. static bool
  3903. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3904. char *error_buf, uint32 error_buf_size)
  3905. {
  3906. if ((is_32bit_type(type) && stack_cell_num < 1)
  3907. || (is_64bit_type(type) && stack_cell_num < 2)
  3908. #if WASM_ENABLE_SIMD != 0
  3909. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3910. || (type == VALUE_TYPE_V128 && stack_cell_num < 4)
  3911. #endif
  3912. #endif
  3913. ) {
  3914. set_error_buf(error_buf, error_buf_size,
  3915. "type mismatch: expect data but stack was empty");
  3916. return false;
  3917. }
  3918. if ((is_32bit_type(type) && *(frame_ref - 1) != type)
  3919. || (is_64bit_type(type)
  3920. && (*(frame_ref - 2) != type || *(frame_ref - 1) != type))
  3921. #if WASM_ENABLE_SIMD != 0
  3922. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3923. || (type == VALUE_TYPE_V128
  3924. && (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2
  3925. || *(frame_ref - 2) != REF_V128_3
  3926. || *(frame_ref - 1) != REF_V128_4))
  3927. #endif
  3928. #endif
  3929. ) {
  3930. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  3931. "type mismatch: expect ", type2str(type),
  3932. " but got other");
  3933. return false;
  3934. }
  3935. return true;
  3936. }
  3937. static bool
  3938. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3939. uint32 error_buf_size)
  3940. {
  3941. int32 block_stack_cell_num =
  3942. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3943. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3944. /* the stack top is a value of any type, return success */
  3945. return true;
  3946. }
  3947. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  3948. error_buf, error_buf_size))
  3949. return false;
  3950. return true;
  3951. }
  3952. static void
  3953. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3954. {
  3955. if (ctx) {
  3956. if (ctx->frame_ref_bottom)
  3957. wasm_runtime_free(ctx->frame_ref_bottom);
  3958. if (ctx->frame_csp_bottom) {
  3959. #if WASM_ENABLE_FAST_INTERP != 0
  3960. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3961. #endif
  3962. wasm_runtime_free(ctx->frame_csp_bottom);
  3963. }
  3964. #if WASM_ENABLE_FAST_INTERP != 0
  3965. if (ctx->frame_offset_bottom)
  3966. wasm_runtime_free(ctx->frame_offset_bottom);
  3967. if (ctx->const_buf)
  3968. wasm_runtime_free(ctx->const_buf);
  3969. #endif
  3970. wasm_runtime_free(ctx);
  3971. }
  3972. }
  3973. static WASMLoaderContext *
  3974. wasm_loader_ctx_init(WASMFunction *func)
  3975. {
  3976. WASMLoaderContext *loader_ctx =
  3977. wasm_runtime_malloc(sizeof(WASMLoaderContext));
  3978. if (!loader_ctx)
  3979. return NULL;
  3980. memset(loader_ctx, 0, sizeof(WASMLoaderContext));
  3981. loader_ctx->frame_ref_size = 32;
  3982. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
  3983. wasm_runtime_malloc(loader_ctx->frame_ref_size)))
  3984. goto fail;
  3985. memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
  3986. loader_ctx->frame_ref_boundary =
  3987. loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size;
  3988. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3989. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
  3990. wasm_runtime_malloc(loader_ctx->frame_csp_size)))
  3991. goto fail;
  3992. memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
  3993. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3994. #if WASM_ENABLE_FAST_INTERP != 0
  3995. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3996. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3997. wasm_runtime_malloc(loader_ctx->frame_offset_size)))
  3998. goto fail;
  3999. memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size);
  4000. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  4001. loader_ctx->num_const = 0;
  4002. loader_ctx->const_buf_size = sizeof(Const) * 8;
  4003. if (!(loader_ctx->const_buf =
  4004. wasm_runtime_malloc(loader_ctx->const_buf_size)))
  4005. goto fail;
  4006. memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
  4007. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  4008. loader_ctx->max_dynamic_offset =
  4009. func->param_cell_num + func->local_cell_num;
  4010. #endif
  4011. return loader_ctx;
  4012. fail:
  4013. wasm_loader_ctx_destroy(loader_ctx);
  4014. return NULL;
  4015. }
  4016. static bool
  4017. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4018. uint32 error_buf_size)
  4019. {
  4020. if (type == VALUE_TYPE_VOID)
  4021. return true;
  4022. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4023. return false;
  4024. *ctx->frame_ref++ = type;
  4025. ctx->stack_cell_num++;
  4026. if (is_32bit_type(type) || type == VALUE_TYPE_ANY)
  4027. goto check_stack_and_return;
  4028. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4029. return false;
  4030. *ctx->frame_ref++ = type;
  4031. ctx->stack_cell_num++;
  4032. #if WASM_ENABLE_SIMD != 0
  4033. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4034. if (type == VALUE_TYPE_V128) {
  4035. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4036. return false;
  4037. *ctx->frame_ref++ = type;
  4038. ctx->stack_cell_num++;
  4039. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4040. return false;
  4041. *ctx->frame_ref++ = type;
  4042. ctx->stack_cell_num++;
  4043. }
  4044. #endif
  4045. #endif
  4046. check_stack_and_return:
  4047. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  4048. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4049. return true;
  4050. }
  4051. static bool
  4052. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4053. uint32 error_buf_size)
  4054. {
  4055. BranchBlock *cur_block = ctx->frame_csp - 1;
  4056. int32 available_stack_cell =
  4057. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4058. /* Directly return success if current block is in stack
  4059. * polymorphic state while stack is empty. */
  4060. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4061. return true;
  4062. if (type == VALUE_TYPE_VOID)
  4063. return true;
  4064. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  4065. return false;
  4066. ctx->frame_ref--;
  4067. ctx->stack_cell_num--;
  4068. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  4069. return true;
  4070. ctx->frame_ref--;
  4071. ctx->stack_cell_num--;
  4072. #if WASM_ENABLE_SIMD != 0
  4073. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4074. if (type == VALUE_TYPE_V128) {
  4075. ctx->frame_ref -= 2;
  4076. ctx->stack_cell_num -= 2;
  4077. }
  4078. #endif
  4079. #endif
  4080. return true;
  4081. }
  4082. static bool
  4083. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  4084. uint8 type_push, uint8 type_pop, char *error_buf,
  4085. uint32 error_buf_size)
  4086. {
  4087. for (int i = 0; i < pop_cnt; i++) {
  4088. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4089. error_buf_size))
  4090. return false;
  4091. }
  4092. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4093. return false;
  4094. return true;
  4095. }
  4096. static bool
  4097. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  4098. BlockType block_type, uint8 *start_addr,
  4099. char *error_buf, uint32 error_buf_size)
  4100. {
  4101. CHECK_CSP_PUSH();
  4102. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  4103. ctx->frame_csp->label_type = label_type;
  4104. ctx->frame_csp->block_type = block_type;
  4105. ctx->frame_csp->start_addr = start_addr;
  4106. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  4107. #if WASM_ENABLE_FAST_INTERP != 0
  4108. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  4109. ctx->frame_csp->patch_list = NULL;
  4110. #endif
  4111. ctx->frame_csp++;
  4112. ctx->csp_num++;
  4113. if (ctx->csp_num > ctx->max_csp_num)
  4114. ctx->max_csp_num = ctx->csp_num;
  4115. return true;
  4116. fail:
  4117. return false;
  4118. }
  4119. static bool
  4120. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  4121. uint32 error_buf_size)
  4122. {
  4123. CHECK_CSP_POP();
  4124. #if WASM_ENABLE_FAST_INTERP != 0
  4125. if ((ctx->frame_csp - 1)->param_frame_offsets)
  4126. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  4127. #endif
  4128. ctx->frame_csp--;
  4129. ctx->csp_num--;
  4130. return true;
  4131. fail:
  4132. return false;
  4133. }
  4134. #if WASM_ENABLE_FAST_INTERP != 0
  4135. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4136. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4137. #define emit_label(opcode) \
  4138. do { \
  4139. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  4140. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4141. } while (0)
  4142. #define skip_label() \
  4143. do { \
  4144. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  4145. LOG_OP("\ndelete last op\n"); \
  4146. } while (0)
  4147. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4148. #define emit_label(opcode) \
  4149. do { \
  4150. int32 offset = \
  4151. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  4152. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  4153. set_error_buf(error_buf, error_buf_size, \
  4154. "pre-compiled label offset out of range"); \
  4155. goto fail; \
  4156. } \
  4157. wasm_loader_emit_int16(loader_ctx, offset); \
  4158. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4159. } while (0)
  4160. #define skip_label() \
  4161. do { \
  4162. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  4163. LOG_OP("\ndelete last op\n"); \
  4164. } while (0)
  4165. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4166. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4167. #define emit_label(opcode) \
  4168. do { \
  4169. wasm_loader_emit_uint8(loader_ctx, opcode); \
  4170. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4171. } while (0)
  4172. #define skip_label() \
  4173. do { \
  4174. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  4175. LOG_OP("\ndelete last op\n"); \
  4176. } while (0)
  4177. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4178. #define emit_empty_label_addr_and_frame_ip(type) \
  4179. do { \
  4180. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  4181. loader_ctx->p_code_compiled, error_buf, \
  4182. error_buf_size)) \
  4183. goto fail; \
  4184. /* label address, to be patched */ \
  4185. wasm_loader_emit_ptr(loader_ctx, NULL); \
  4186. } while (0)
  4187. #define emit_br_info(frame_csp) \
  4188. do { \
  4189. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  4190. error_buf_size)) \
  4191. goto fail; \
  4192. } while (0)
  4193. #define LAST_OP_OUTPUT_I32() \
  4194. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  4195. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  4196. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  4197. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  4198. || (last_op >= WASM_OP_I32_WRAP_I64 \
  4199. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  4200. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  4201. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  4202. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  4203. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  4204. || (last_op == EXT_OP_COPY_STACK_TOP)
  4205. #define LAST_OP_OUTPUT_I64() \
  4206. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  4207. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  4208. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  4209. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  4210. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  4211. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  4212. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  4213. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  4214. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  4215. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  4216. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  4217. #define GET_CONST_OFFSET(type, val) \
  4218. do { \
  4219. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  4220. &operand_offset, error_buf, \
  4221. error_buf_size))) \
  4222. goto fail; \
  4223. } while (0)
  4224. #define GET_CONST_F32_OFFSET(type, fval) \
  4225. do { \
  4226. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4227. &operand_offset, error_buf, \
  4228. error_buf_size))) \
  4229. goto fail; \
  4230. } while (0)
  4231. #define GET_CONST_F64_OFFSET(type, fval) \
  4232. do { \
  4233. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4234. &operand_offset, error_buf, \
  4235. error_buf_size))) \
  4236. goto fail; \
  4237. } while (0)
  4238. #define emit_operand(ctx, offset) \
  4239. do { \
  4240. wasm_loader_emit_int16(ctx, offset); \
  4241. LOG_OP("%d\t", offset); \
  4242. } while (0)
  4243. #define emit_byte(ctx, byte) \
  4244. do { \
  4245. wasm_loader_emit_uint8(ctx, byte); \
  4246. LOG_OP("%d\t", byte); \
  4247. } while (0)
  4248. #define emit_uint32(ctx, value) \
  4249. do { \
  4250. wasm_loader_emit_uint32(ctx, value); \
  4251. LOG_OP("%d\t", value); \
  4252. } while (0)
  4253. static bool
  4254. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  4255. {
  4256. if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
  4257. return false;
  4258. memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
  4259. ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size;
  4260. /* clean up frame ref */
  4261. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  4262. ctx->frame_ref = ctx->frame_ref_bottom;
  4263. ctx->stack_cell_num = 0;
  4264. /* clean up frame csp */
  4265. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  4266. ctx->frame_csp = ctx->frame_csp_bottom;
  4267. ctx->csp_num = 0;
  4268. ctx->max_csp_num = 0;
  4269. /* clean up frame offset */
  4270. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  4271. ctx->frame_offset = ctx->frame_offset_bottom;
  4272. ctx->dynamic_offset = ctx->start_dynamic_offset;
  4273. /* init preserved local offsets */
  4274. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  4275. /* const buf is reserved */
  4276. return true;
  4277. }
  4278. static void
  4279. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  4280. {
  4281. if (ctx->p_code_compiled) {
  4282. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4283. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4284. #endif
  4285. STORE_U32(ctx->p_code_compiled, value);
  4286. ctx->p_code_compiled += sizeof(uint32);
  4287. }
  4288. else {
  4289. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4290. bh_assert((ctx->code_compiled_size & 1) == 0);
  4291. #endif
  4292. ctx->code_compiled_size += sizeof(uint32);
  4293. }
  4294. }
  4295. static void
  4296. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  4297. {
  4298. if (ctx->p_code_compiled) {
  4299. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4300. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4301. #endif
  4302. STORE_U16(ctx->p_code_compiled, (uint16)value);
  4303. ctx->p_code_compiled += sizeof(int16);
  4304. }
  4305. else {
  4306. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4307. bh_assert((ctx->code_compiled_size & 1) == 0);
  4308. #endif
  4309. ctx->code_compiled_size += sizeof(int16);
  4310. }
  4311. }
  4312. static void
  4313. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  4314. {
  4315. if (ctx->p_code_compiled) {
  4316. *(ctx->p_code_compiled) = value;
  4317. ctx->p_code_compiled += sizeof(uint8);
  4318. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4319. ctx->p_code_compiled++;
  4320. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4321. #endif
  4322. }
  4323. else {
  4324. ctx->code_compiled_size += sizeof(uint8);
  4325. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4326. ctx->code_compiled_size++;
  4327. bh_assert((ctx->code_compiled_size & 1) == 0);
  4328. #endif
  4329. }
  4330. }
  4331. static void
  4332. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  4333. {
  4334. if (ctx->p_code_compiled) {
  4335. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4336. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4337. #endif
  4338. STORE_PTR(ctx->p_code_compiled, value);
  4339. ctx->p_code_compiled += sizeof(void *);
  4340. }
  4341. else {
  4342. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4343. bh_assert((ctx->code_compiled_size & 1) == 0);
  4344. #endif
  4345. ctx->code_compiled_size += sizeof(void *);
  4346. }
  4347. }
  4348. static void
  4349. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  4350. {
  4351. if (ctx->p_code_compiled) {
  4352. ctx->p_code_compiled -= size;
  4353. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4354. if (size == sizeof(uint8)) {
  4355. ctx->p_code_compiled--;
  4356. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4357. }
  4358. #endif
  4359. }
  4360. else {
  4361. ctx->code_compiled_size -= size;
  4362. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4363. if (size == sizeof(uint8)) {
  4364. ctx->code_compiled_size--;
  4365. bh_assert((ctx->code_compiled_size & 1) == 0);
  4366. }
  4367. #endif
  4368. }
  4369. }
  4370. static bool
  4371. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  4372. uint32 local_index, uint32 local_type,
  4373. bool *preserved, char *error_buf,
  4374. uint32 error_buf_size)
  4375. {
  4376. uint32 i = 0;
  4377. int16 preserved_offset = (int16)local_index;
  4378. *preserved = false;
  4379. while (i < loader_ctx->stack_cell_num) {
  4380. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4381. /* move previous local into dynamic space before a set/tee_local opcode
  4382. */
  4383. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  4384. if (!(*preserved)) {
  4385. *preserved = true;
  4386. skip_label();
  4387. preserved_offset = loader_ctx->preserved_local_offset;
  4388. if (loader_ctx->p_code_compiled) {
  4389. bh_assert(preserved_offset != (int16)local_index);
  4390. }
  4391. if (is_32bit_type(local_type)) {
  4392. /* Only increase preserve offset in the second traversal */
  4393. if (loader_ctx->p_code_compiled)
  4394. loader_ctx->preserved_local_offset++;
  4395. emit_label(EXT_OP_COPY_STACK_TOP);
  4396. }
  4397. else {
  4398. if (loader_ctx->p_code_compiled)
  4399. loader_ctx->preserved_local_offset += 2;
  4400. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  4401. }
  4402. emit_operand(loader_ctx, local_index);
  4403. emit_operand(loader_ctx, preserved_offset);
  4404. emit_label(opcode);
  4405. }
  4406. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  4407. }
  4408. if (is_32bit_type(cur_type))
  4409. i++;
  4410. else
  4411. i += 2;
  4412. }
  4413. return true;
  4414. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4415. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4416. fail:
  4417. return false;
  4418. #endif
  4419. #endif
  4420. }
  4421. static bool
  4422. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  4423. char *error_buf, uint32 error_buf_size)
  4424. {
  4425. uint32 i = 0;
  4426. bool preserve_local;
  4427. /* preserve locals before blocks to ensure that "tee/set_local" inside
  4428. blocks will not influence the value of these locals */
  4429. while (i < loader_ctx->stack_cell_num) {
  4430. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  4431. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4432. if ((cur_offset < loader_ctx->start_dynamic_offset)
  4433. && (cur_offset >= 0)) {
  4434. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  4435. cur_type, &preserve_local,
  4436. error_buf, error_buf_size)))
  4437. return false;
  4438. }
  4439. if (is_32bit_type(cur_type)) {
  4440. i++;
  4441. }
  4442. else {
  4443. i += 2;
  4444. }
  4445. }
  4446. return true;
  4447. }
  4448. static bool
  4449. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  4450. uint8 *p_code_compiled, char *error_buf,
  4451. uint32 error_buf_size)
  4452. {
  4453. BranchBlockPatch *patch =
  4454. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  4455. if (!patch) {
  4456. return false;
  4457. }
  4458. patch->patch_type = patch_type;
  4459. patch->code_compiled = p_code_compiled;
  4460. if (!frame_csp->patch_list) {
  4461. frame_csp->patch_list = patch;
  4462. patch->next = NULL;
  4463. }
  4464. else {
  4465. patch->next = frame_csp->patch_list;
  4466. frame_csp->patch_list = patch;
  4467. }
  4468. return true;
  4469. }
  4470. static void
  4471. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  4472. {
  4473. BranchBlock *frame_csp = ctx->frame_csp - depth;
  4474. BranchBlockPatch *node = frame_csp->patch_list;
  4475. BranchBlockPatch *node_prev = NULL, *node_next;
  4476. if (!ctx->p_code_compiled)
  4477. return;
  4478. while (node) {
  4479. node_next = node->next;
  4480. if (node->patch_type == patch_type) {
  4481. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  4482. if (node_prev == NULL) {
  4483. frame_csp->patch_list = node_next;
  4484. }
  4485. else {
  4486. node_prev->next = node_next;
  4487. }
  4488. wasm_runtime_free(node);
  4489. }
  4490. else {
  4491. node_prev = node;
  4492. }
  4493. node = node_next;
  4494. }
  4495. }
  4496. static bool
  4497. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  4498. char *error_buf, uint32 error_buf_size)
  4499. {
  4500. /* br info layout:
  4501. * a) arity of target block
  4502. * b) total cell num of arity values
  4503. * c) each arity value's cell num
  4504. * d) each arity value's src frame offset
  4505. * e) each arity values's dst dynamic offset
  4506. * f) branch target address
  4507. *
  4508. * Note: b-e are omitted when arity is 0 so that
  4509. * interpreter can recover the br info quickly.
  4510. */
  4511. BlockType *block_type = &frame_csp->block_type;
  4512. uint8 *types = NULL, cell;
  4513. uint32 arity = 0;
  4514. int32 i;
  4515. int16 *frame_offset = ctx->frame_offset;
  4516. uint16 dynamic_offset;
  4517. /* Note: loop's arity is different from if and block. loop's arity is
  4518. * its parameter count while if and block arity is result count.
  4519. */
  4520. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4521. arity = block_type_get_param_types(block_type, &types);
  4522. else
  4523. arity = block_type_get_result_types(block_type, &types);
  4524. /* Part a */
  4525. emit_uint32(ctx, arity);
  4526. if (arity) {
  4527. /* Part b */
  4528. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  4529. /* Part c */
  4530. for (i = (int32)arity - 1; i >= 0; i--) {
  4531. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4532. emit_byte(ctx, cell);
  4533. }
  4534. /* Part d */
  4535. for (i = (int32)arity - 1; i >= 0; i--) {
  4536. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4537. frame_offset -= cell;
  4538. emit_operand(ctx, *(int16 *)(frame_offset));
  4539. }
  4540. /* Part e */
  4541. dynamic_offset =
  4542. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  4543. for (i = (int32)arity - 1; i >= 0; i--) {
  4544. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4545. dynamic_offset -= cell;
  4546. emit_operand(ctx, dynamic_offset);
  4547. }
  4548. }
  4549. /* Part f */
  4550. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  4551. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  4552. }
  4553. else {
  4554. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  4555. error_buf, error_buf_size))
  4556. return false;
  4557. /* label address, to be patched */
  4558. wasm_loader_emit_ptr(ctx, NULL);
  4559. }
  4560. return true;
  4561. }
  4562. static bool
  4563. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4564. bool disable_emit, int16 operand_offset,
  4565. char *error_buf, uint32 error_buf_size)
  4566. {
  4567. if (type == VALUE_TYPE_VOID)
  4568. return true;
  4569. /* only check memory overflow in first traverse */
  4570. if (ctx->p_code_compiled == NULL) {
  4571. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4572. return false;
  4573. }
  4574. if (disable_emit)
  4575. *(ctx->frame_offset)++ = operand_offset;
  4576. else {
  4577. emit_operand(ctx, ctx->dynamic_offset);
  4578. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4579. ctx->dynamic_offset++;
  4580. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  4581. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4582. }
  4583. if (is_32bit_type(type))
  4584. return true;
  4585. if (ctx->p_code_compiled == NULL) {
  4586. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4587. return false;
  4588. }
  4589. ctx->frame_offset++;
  4590. if (!disable_emit) {
  4591. ctx->dynamic_offset++;
  4592. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  4593. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4594. }
  4595. return true;
  4596. }
  4597. /* This function should be in front of wasm_loader_pop_frame_ref
  4598. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4599. will be modified by wasm_loader_pop_frame_ref */
  4600. static bool
  4601. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4602. char *error_buf, uint32 error_buf_size)
  4603. {
  4604. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4605. then current block is the function block */
  4606. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4607. BranchBlock *cur_block = ctx->frame_csp - depth;
  4608. int32 available_stack_cell =
  4609. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4610. /* Directly return success if current block is in stack
  4611. * polymorphic state while stack is empty. */
  4612. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4613. return true;
  4614. if (type == VALUE_TYPE_VOID)
  4615. return true;
  4616. if (is_32bit_type(type)) {
  4617. /* Check the offset stack bottom to ensure the frame offset
  4618. stack will not go underflow. But we don't thrown error
  4619. and return true here, because the error msg should be
  4620. given in wasm_loader_pop_frame_ref */
  4621. if (!check_offset_pop(ctx, 1))
  4622. return true;
  4623. ctx->frame_offset -= 1;
  4624. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4625. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4626. ctx->dynamic_offset -= 1;
  4627. }
  4628. else {
  4629. if (!check_offset_pop(ctx, 2))
  4630. return true;
  4631. ctx->frame_offset -= 2;
  4632. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4633. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4634. ctx->dynamic_offset -= 2;
  4635. }
  4636. emit_operand(ctx, *(ctx->frame_offset));
  4637. return true;
  4638. }
  4639. static bool
  4640. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4641. uint8 type_push, uint8 type_pop,
  4642. bool disable_emit, int16 operand_offset,
  4643. char *error_buf, uint32 error_buf_size)
  4644. {
  4645. uint8 i;
  4646. for (i = 0; i < pop_cnt; i++) {
  4647. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  4648. error_buf_size))
  4649. return false;
  4650. }
  4651. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  4652. operand_offset, error_buf,
  4653. error_buf_size))
  4654. return false;
  4655. return true;
  4656. }
  4657. static bool
  4658. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4659. bool disable_emit, int16 operand_offset,
  4660. char *error_buf, uint32 error_buf_size)
  4661. {
  4662. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4663. error_buf, error_buf_size)))
  4664. return false;
  4665. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4666. return false;
  4667. return true;
  4668. }
  4669. static bool
  4670. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4671. char *error_buf, uint32 error_buf_size)
  4672. {
  4673. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4674. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4675. return false;
  4676. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4677. return false;
  4678. return true;
  4679. }
  4680. static bool
  4681. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4682. uint8 type_push, uint8 type_pop,
  4683. bool disable_emit, int16 operand_offset,
  4684. char *error_buf, uint32 error_buf_size)
  4685. {
  4686. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  4687. disable_emit, operand_offset,
  4688. error_buf, error_buf_size))
  4689. return false;
  4690. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  4691. error_buf, error_buf_size))
  4692. return false;
  4693. return true;
  4694. }
  4695. static bool
  4696. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  4697. int16 *offset, char *error_buf,
  4698. uint32 error_buf_size)
  4699. {
  4700. int16 operand_offset = 0;
  4701. Const *c;
  4702. for (c = (Const *)ctx->const_buf;
  4703. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  4704. /* TODO: handle v128 type? */
  4705. if ((type == c->value_type)
  4706. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  4707. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  4708. #if WASM_ENABLE_REF_TYPES != 0
  4709. || (type == VALUE_TYPE_FUNCREF
  4710. && *(int32 *)value == c->value.i32)
  4711. || (type == VALUE_TYPE_EXTERNREF
  4712. && *(int32 *)value == c->value.i32)
  4713. #endif
  4714. || (type == VALUE_TYPE_F64
  4715. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  4716. || (type == VALUE_TYPE_F32
  4717. && (0
  4718. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  4719. operand_offset = c->slot_index;
  4720. break;
  4721. }
  4722. if (is_32bit_type(c->value_type))
  4723. operand_offset += 1;
  4724. else
  4725. operand_offset += 2;
  4726. }
  4727. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  4728. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  4729. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  4730. ctx->const_buf_size + 4 * sizeof(Const));
  4731. ctx->const_buf_size += 4 * sizeof(Const);
  4732. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  4733. }
  4734. c->value_type = type;
  4735. switch (type) {
  4736. case VALUE_TYPE_F64:
  4737. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  4738. sizeof(float64));
  4739. ctx->const_cell_num += 2;
  4740. /* The const buf will be reversed, we use the second cell */
  4741. /* of the i64/f64 const so the finnal offset is corrent */
  4742. operand_offset++;
  4743. break;
  4744. case VALUE_TYPE_I64:
  4745. c->value.i64 = *(int64 *)value;
  4746. ctx->const_cell_num += 2;
  4747. operand_offset++;
  4748. break;
  4749. case VALUE_TYPE_F32:
  4750. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  4751. sizeof(float32));
  4752. ctx->const_cell_num++;
  4753. break;
  4754. case VALUE_TYPE_I32:
  4755. c->value.i32 = *(int32 *)value;
  4756. ctx->const_cell_num++;
  4757. break;
  4758. #if WASM_ENABLE_REF_TYPES != 0
  4759. case VALUE_TYPE_EXTERNREF:
  4760. case VALUE_TYPE_FUNCREF:
  4761. c->value.i32 = *(int32 *)value;
  4762. ctx->const_cell_num++;
  4763. break;
  4764. #endif
  4765. default:
  4766. break;
  4767. }
  4768. c->slot_index = operand_offset;
  4769. ctx->num_const++;
  4770. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  4771. (int64)c->value.i64);
  4772. }
  4773. /* use negetive index for const */
  4774. operand_offset = -(operand_offset + 1);
  4775. *offset = operand_offset;
  4776. return true;
  4777. fail:
  4778. return false;
  4779. }
  4780. /*
  4781. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4782. -- Mostly used for the binary / compare operation
  4783. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4784. -- Mostly used in block / control instructions
  4785. The POP will always emit the offset on the top of the frame_offset stack
  4786. PUSH can be used in two ways:
  4787. 1. directly PUSH:
  4788. PUSH_XXX();
  4789. will allocate a dynamic space and emit
  4790. 2. silent PUSH:
  4791. operand_offset = xxx; disable_emit = true;
  4792. PUSH_XXX();
  4793. only push the frame_offset stack, no emit
  4794. */
  4795. #define TEMPLATE_PUSH(Type) \
  4796. do { \
  4797. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  4798. disable_emit, operand_offset, \
  4799. error_buf, error_buf_size)) \
  4800. goto fail; \
  4801. } while (0)
  4802. #define TEMPLATE_POP(Type) \
  4803. do { \
  4804. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  4805. error_buf, error_buf_size)) \
  4806. goto fail; \
  4807. } while (0)
  4808. #define PUSH_OFFSET_TYPE(type) \
  4809. do { \
  4810. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  4811. operand_offset, error_buf, \
  4812. error_buf_size))) \
  4813. goto fail; \
  4814. } while (0)
  4815. #define POP_OFFSET_TYPE(type) \
  4816. do { \
  4817. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  4818. error_buf_size))) \
  4819. goto fail; \
  4820. } while (0)
  4821. #define POP_AND_PUSH(type_pop, type_push) \
  4822. do { \
  4823. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4824. loader_ctx, 1, type_push, type_pop, disable_emit, \
  4825. operand_offset, error_buf, error_buf_size))) \
  4826. goto fail; \
  4827. } while (0)
  4828. /* type of POPs should be the same */
  4829. #define POP2_AND_PUSH(type_pop, type_push) \
  4830. do { \
  4831. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4832. loader_ctx, 2, type_push, type_pop, disable_emit, \
  4833. operand_offset, error_buf, error_buf_size))) \
  4834. goto fail; \
  4835. } while (0)
  4836. #else /* WASM_ENABLE_FAST_INTERP */
  4837. #define TEMPLATE_PUSH(Type) \
  4838. do { \
  4839. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  4840. error_buf, error_buf_size))) \
  4841. goto fail; \
  4842. } while (0)
  4843. #define TEMPLATE_POP(Type) \
  4844. do { \
  4845. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  4846. error_buf, error_buf_size))) \
  4847. goto fail; \
  4848. } while (0)
  4849. #define POP_AND_PUSH(type_pop, type_push) \
  4850. do { \
  4851. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  4852. type_pop, error_buf, \
  4853. error_buf_size))) \
  4854. goto fail; \
  4855. } while (0)
  4856. /* type of POPs should be the same */
  4857. #define POP2_AND_PUSH(type_pop, type_push) \
  4858. do { \
  4859. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  4860. type_pop, error_buf, \
  4861. error_buf_size))) \
  4862. goto fail; \
  4863. } while (0)
  4864. #endif /* WASM_ENABLE_FAST_INTERP */
  4865. #define PUSH_I32() TEMPLATE_PUSH(I32)
  4866. #define PUSH_F32() TEMPLATE_PUSH(F32)
  4867. #define PUSH_I64() TEMPLATE_PUSH(I64)
  4868. #define PUSH_F64() TEMPLATE_PUSH(F64)
  4869. #define PUSH_V128() TEMPLATE_PUSH(V128)
  4870. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  4871. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  4872. #define POP_I32() TEMPLATE_POP(I32)
  4873. #define POP_F32() TEMPLATE_POP(F32)
  4874. #define POP_I64() TEMPLATE_POP(I64)
  4875. #define POP_F64() TEMPLATE_POP(F64)
  4876. #define POP_V128() TEMPLATE_POP(V128)
  4877. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  4878. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  4879. #if WASM_ENABLE_FAST_INTERP != 0
  4880. static bool
  4881. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  4882. bool disable_emit, char *error_buf, uint32 error_buf_size)
  4883. {
  4884. int16 operand_offset = 0;
  4885. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  4886. : loader_ctx->frame_csp;
  4887. BlockType *block_type = &block->block_type;
  4888. uint8 *return_types = NULL;
  4889. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4890. int32 i = 0;
  4891. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  4892. *frame_offset_org = NULL;
  4893. return_count = block_type_get_result_types(block_type, &return_types);
  4894. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4895. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4896. if (return_count == 1) {
  4897. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  4898. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4899. /* insert op_copy before else opcode */
  4900. if (opcode == WASM_OP_ELSE)
  4901. skip_label();
  4902. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  4903. : EXT_OP_COPY_STACK_TOP_I64);
  4904. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4905. emit_operand(loader_ctx, block->dynamic_offset);
  4906. if (opcode == WASM_OP_ELSE) {
  4907. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4908. }
  4909. else {
  4910. loader_ctx->frame_offset -= cell;
  4911. loader_ctx->dynamic_offset = block->dynamic_offset;
  4912. PUSH_OFFSET_TYPE(return_types[0]);
  4913. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4914. }
  4915. if (opcode == WASM_OP_ELSE)
  4916. emit_label(opcode);
  4917. }
  4918. return true;
  4919. }
  4920. /* Copy stack top values to block's results which are in dynamic space.
  4921. * The instruction format:
  4922. * Part a: values count
  4923. * Part b: all values total cell num
  4924. * Part c: each value's cell_num, src offset and dst offset
  4925. * Part d: each value's src offset and dst offset
  4926. * Part e: each value's dst offset
  4927. */
  4928. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4929. dynamic_offset = dynamic_offset_org =
  4930. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  4931. /* First traversal to get the count of values needed to be copied. */
  4932. for (i = (int32)return_count - 1; i >= 0; i--) {
  4933. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  4934. frame_offset -= cells;
  4935. dynamic_offset -= cells;
  4936. if (dynamic_offset != *frame_offset) {
  4937. value_count++;
  4938. total_cel_num += cells;
  4939. }
  4940. }
  4941. if (value_count) {
  4942. uint32 j = 0;
  4943. uint8 *emit_data = NULL, *cells = NULL;
  4944. int16 *src_offsets = NULL;
  4945. uint16 *dst_offsets = NULL;
  4946. uint64 size =
  4947. (uint64)value_count
  4948. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  4949. /* Allocate memory for the emit data */
  4950. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4951. return false;
  4952. cells = emit_data;
  4953. src_offsets = (int16 *)(cells + value_count);
  4954. dst_offsets = (uint16 *)(src_offsets + value_count);
  4955. /* insert op_copy before else opcode */
  4956. if (opcode == WASM_OP_ELSE)
  4957. skip_label();
  4958. emit_label(EXT_OP_COPY_STACK_VALUES);
  4959. /* Part a) */
  4960. emit_uint32(loader_ctx, value_count);
  4961. /* Part b) */
  4962. emit_uint32(loader_ctx, total_cel_num);
  4963. /* Second traversal to get each value's cell num, src offset and dst
  4964. * offset. */
  4965. frame_offset = frame_offset_org;
  4966. dynamic_offset = dynamic_offset_org;
  4967. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4968. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  4969. frame_offset -= cell;
  4970. dynamic_offset -= cell;
  4971. if (dynamic_offset != *frame_offset) {
  4972. /* cell num */
  4973. cells[j] = cell;
  4974. /* src offset */
  4975. src_offsets[j] = *frame_offset;
  4976. /* dst offset */
  4977. dst_offsets[j] = dynamic_offset;
  4978. j++;
  4979. }
  4980. if (opcode == WASM_OP_ELSE) {
  4981. *frame_offset = dynamic_offset;
  4982. }
  4983. else {
  4984. loader_ctx->frame_offset = frame_offset;
  4985. loader_ctx->dynamic_offset = dynamic_offset;
  4986. PUSH_OFFSET_TYPE(return_types[i]);
  4987. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4988. loader_ctx->frame_offset = frame_offset_org;
  4989. loader_ctx->dynamic_offset = dynamic_offset_org;
  4990. }
  4991. }
  4992. bh_assert(j == value_count);
  4993. /* Emit the cells, src_offsets and dst_offsets */
  4994. for (j = 0; j < value_count; j++)
  4995. emit_byte(loader_ctx, cells[j]);
  4996. for (j = 0; j < value_count; j++)
  4997. emit_operand(loader_ctx, src_offsets[j]);
  4998. for (j = 0; j < value_count; j++)
  4999. emit_operand(loader_ctx, dst_offsets[j]);
  5000. if (opcode == WASM_OP_ELSE)
  5001. emit_label(opcode);
  5002. wasm_runtime_free(emit_data);
  5003. }
  5004. return true;
  5005. fail:
  5006. return false;
  5007. }
  5008. #endif /* WASM_ENABLE_FAST_INTERP */
  5009. #define RESERVE_BLOCK_RET() \
  5010. do { \
  5011. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  5012. error_buf_size)) \
  5013. goto fail; \
  5014. } while (0)
  5015. #define PUSH_TYPE(type) \
  5016. do { \
  5017. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  5018. error_buf_size))) \
  5019. goto fail; \
  5020. } while (0)
  5021. #define POP_TYPE(type) \
  5022. do { \
  5023. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  5024. error_buf_size))) \
  5025. goto fail; \
  5026. } while (0)
  5027. #define PUSH_CSP(label_type, block_type, _start_addr) \
  5028. do { \
  5029. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  5030. _start_addr, error_buf, \
  5031. error_buf_size)) \
  5032. goto fail; \
  5033. } while (0)
  5034. #define POP_CSP() \
  5035. do { \
  5036. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  5037. goto fail; \
  5038. } while (0)
  5039. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  5040. do { \
  5041. read_leb_uint32(p, p_end, local_idx); \
  5042. if (local_idx >= param_count + local_count) { \
  5043. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  5044. goto fail; \
  5045. } \
  5046. local_type = local_idx < param_count \
  5047. ? param_types[local_idx] \
  5048. : local_types[local_idx - param_count]; \
  5049. local_offset = local_offsets[local_idx]; \
  5050. } while (0)
  5051. #define CHECK_BR(depth) \
  5052. do { \
  5053. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  5054. error_buf_size)) \
  5055. goto fail; \
  5056. } while (0)
  5057. static bool
  5058. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  5059. {
  5060. if (module->memory_count == 0 && module->import_memory_count == 0) {
  5061. set_error_buf(error_buf, error_buf_size, "unknown memory");
  5062. return false;
  5063. }
  5064. return true;
  5065. }
  5066. #define CHECK_MEMORY() \
  5067. do { \
  5068. if (!check_memory(module, error_buf, error_buf_size)) \
  5069. goto fail; \
  5070. } while (0)
  5071. static bool
  5072. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5073. uint32 error_buf_size)
  5074. {
  5075. uint8 mem_access_aligns[] = {
  5076. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  5077. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  5078. };
  5079. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  5080. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  5081. set_error_buf(error_buf, error_buf_size,
  5082. "alignment must not be larger than natural");
  5083. return false;
  5084. }
  5085. return true;
  5086. }
  5087. #if WASM_ENABLE_SIMD != 0
  5088. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5089. static bool
  5090. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5091. uint32 error_buf_size)
  5092. {
  5093. uint8 mem_access_aligns[] = {
  5094. 4, /* load */
  5095. 3, 3, 3, 3, 3, 3, /* load and extend */
  5096. 0, 1, 2, 3, /* load and splat */
  5097. 4, /* store */
  5098. };
  5099. uint8 mem_access_aligns_load_lane[] = {
  5100. 0, 1, 2, 3, /* load lane */
  5101. 0, 1, 2, 3, /* store lane */
  5102. 2, 3 /* store zero */
  5103. };
  5104. if (!((opcode <= SIMD_v128_store)
  5105. || (SIMD_v128_load8_lane <= opcode
  5106. && opcode <= SIMD_v128_load64_zero))) {
  5107. set_error_buf(error_buf, error_buf_size,
  5108. "the opcode doesn't include memarg");
  5109. return false;
  5110. }
  5111. if ((opcode <= SIMD_v128_store
  5112. && align > mem_access_aligns[opcode - SIMD_v128_load])
  5113. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  5114. && align > mem_access_aligns_load_lane[opcode
  5115. - SIMD_v128_load8_lane])) {
  5116. set_error_buf(error_buf, error_buf_size,
  5117. "alignment must not be larger than natural");
  5118. return false;
  5119. }
  5120. return true;
  5121. }
  5122. static bool
  5123. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  5124. uint32 error_buf_size)
  5125. {
  5126. switch (opcode) {
  5127. case SIMD_i8x16_extract_lane_s:
  5128. case SIMD_i8x16_extract_lane_u:
  5129. case SIMD_i8x16_replace_lane:
  5130. if (lane >= 16) {
  5131. goto fail;
  5132. }
  5133. break;
  5134. case SIMD_i16x8_extract_lane_s:
  5135. case SIMD_i16x8_extract_lane_u:
  5136. case SIMD_i16x8_replace_lane:
  5137. if (lane >= 8) {
  5138. goto fail;
  5139. }
  5140. break;
  5141. case SIMD_i32x4_extract_lane:
  5142. case SIMD_i32x4_replace_lane:
  5143. case SIMD_f32x4_extract_lane:
  5144. case SIMD_f32x4_replace_lane:
  5145. if (lane >= 4) {
  5146. goto fail;
  5147. }
  5148. break;
  5149. case SIMD_i64x2_extract_lane:
  5150. case SIMD_i64x2_replace_lane:
  5151. case SIMD_f64x2_extract_lane:
  5152. case SIMD_f64x2_replace_lane:
  5153. if (lane >= 2) {
  5154. goto fail;
  5155. }
  5156. break;
  5157. case SIMD_v128_load8_lane:
  5158. case SIMD_v128_load16_lane:
  5159. case SIMD_v128_load32_lane:
  5160. case SIMD_v128_load64_lane:
  5161. case SIMD_v128_store8_lane:
  5162. case SIMD_v128_store16_lane:
  5163. case SIMD_v128_store32_lane:
  5164. case SIMD_v128_store64_lane:
  5165. case SIMD_v128_load32_zero:
  5166. case SIMD_v128_load64_zero:
  5167. {
  5168. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  5169. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  5170. goto fail;
  5171. }
  5172. break;
  5173. }
  5174. default:
  5175. goto fail;
  5176. }
  5177. return true;
  5178. fail:
  5179. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  5180. return false;
  5181. }
  5182. static bool
  5183. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  5184. {
  5185. uint8 i;
  5186. for (i = 0; i != 16; ++i) {
  5187. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  5188. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  5189. return false;
  5190. }
  5191. }
  5192. return true;
  5193. }
  5194. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  5195. #endif /* end of WASM_ENABLE_SIMD */
  5196. #if WASM_ENABLE_SHARED_MEMORY != 0
  5197. static bool
  5198. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  5199. uint32 error_buf_size)
  5200. {
  5201. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  5202. uint8 mem_access_aligns[] = {
  5203. 2, 3, 0, 1, 0, 1, 2,
  5204. };
  5205. uint8 expect;
  5206. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  5207. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  5208. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  5209. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  5210. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  5211. }
  5212. else {
  5213. /* 7 opcodes in every group */
  5214. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  5215. }
  5216. if (align != expect) {
  5217. set_error_buf(error_buf, error_buf_size,
  5218. "alignment isn't equal to natural");
  5219. return false;
  5220. }
  5221. return true;
  5222. }
  5223. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  5224. static bool
  5225. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  5226. char *error_buf, uint32 error_buf_size)
  5227. {
  5228. BranchBlock *target_block, *cur_block;
  5229. BlockType *target_block_type;
  5230. uint8 *types = NULL, *frame_ref;
  5231. uint32 arity = 0;
  5232. int32 i, available_stack_cell;
  5233. uint16 cell_num;
  5234. if (loader_ctx->csp_num < depth + 1) {
  5235. set_error_buf(error_buf, error_buf_size,
  5236. "unknown label, "
  5237. "unexpected end of section or function");
  5238. return false;
  5239. }
  5240. cur_block = loader_ctx->frame_csp - 1;
  5241. target_block = loader_ctx->frame_csp - (depth + 1);
  5242. target_block_type = &target_block->block_type;
  5243. frame_ref = loader_ctx->frame_ref;
  5244. /* Note: loop's arity is different from if and block. loop's arity is
  5245. * its parameter count while if and block arity is result count.
  5246. */
  5247. if (target_block->label_type == LABEL_TYPE_LOOP)
  5248. arity = block_type_get_param_types(target_block_type, &types);
  5249. else
  5250. arity = block_type_get_result_types(target_block_type, &types);
  5251. /* If the stack is in polymorphic state, just clear the stack
  5252. * and then re-push the values to make the stack top values
  5253. * match block type. */
  5254. if (cur_block->is_stack_polymorphic) {
  5255. for (i = (int32)arity - 1; i >= 0; i--) {
  5256. #if WASM_ENABLE_FAST_INTERP != 0
  5257. POP_OFFSET_TYPE(types[i]);
  5258. #endif
  5259. POP_TYPE(types[i]);
  5260. }
  5261. for (i = 0; i < (int32)arity; i++) {
  5262. #if WASM_ENABLE_FAST_INTERP != 0
  5263. bool disable_emit = true;
  5264. int16 operand_offset = 0;
  5265. PUSH_OFFSET_TYPE(types[i]);
  5266. #endif
  5267. PUSH_TYPE(types[i]);
  5268. }
  5269. return true;
  5270. }
  5271. available_stack_cell =
  5272. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5273. /* Check stack top values match target block type */
  5274. for (i = (int32)arity - 1; i >= 0; i--) {
  5275. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  5276. error_buf, error_buf_size))
  5277. return false;
  5278. cell_num = wasm_value_type_cell_num(types[i]);
  5279. frame_ref -= cell_num;
  5280. available_stack_cell -= cell_num;
  5281. }
  5282. return true;
  5283. fail:
  5284. return false;
  5285. }
  5286. static BranchBlock *
  5287. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  5288. char *error_buf, uint32 error_buf_size)
  5289. {
  5290. uint8 *p = *p_buf, *p_end = buf_end;
  5291. BranchBlock *frame_csp_tmp;
  5292. uint32 depth;
  5293. read_leb_uint32(p, p_end, depth);
  5294. CHECK_BR(depth);
  5295. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  5296. #if WASM_ENABLE_FAST_INTERP != 0
  5297. emit_br_info(frame_csp_tmp);
  5298. #endif
  5299. *p_buf = p;
  5300. return frame_csp_tmp;
  5301. fail:
  5302. return NULL;
  5303. }
  5304. static bool
  5305. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  5306. char *error_buf, uint32 error_buf_size)
  5307. {
  5308. BlockType *block_type = &block->block_type;
  5309. uint8 *return_types = NULL;
  5310. uint32 return_count = 0;
  5311. int32 available_stack_cell, return_cell_num, i;
  5312. uint8 *frame_ref = NULL;
  5313. available_stack_cell =
  5314. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  5315. return_count = block_type_get_result_types(block_type, &return_types);
  5316. return_cell_num =
  5317. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  5318. /* If the stack is in polymorphic state, just clear the stack
  5319. * and then re-push the values to make the stack top values
  5320. * match block type. */
  5321. if (block->is_stack_polymorphic) {
  5322. for (i = (int32)return_count - 1; i >= 0; i--) {
  5323. #if WASM_ENABLE_FAST_INTERP != 0
  5324. POP_OFFSET_TYPE(return_types[i]);
  5325. #endif
  5326. POP_TYPE(return_types[i]);
  5327. }
  5328. /* Check stack is empty */
  5329. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  5330. set_error_buf(
  5331. error_buf, error_buf_size,
  5332. "type mismatch: stack size does not match block type");
  5333. goto fail;
  5334. }
  5335. for (i = 0; i < (int32)return_count; i++) {
  5336. #if WASM_ENABLE_FAST_INTERP != 0
  5337. bool disable_emit = true;
  5338. int16 operand_offset = 0;
  5339. PUSH_OFFSET_TYPE(return_types[i]);
  5340. #endif
  5341. PUSH_TYPE(return_types[i]);
  5342. }
  5343. return true;
  5344. }
  5345. /* Check stack cell num equals return cell num */
  5346. if (available_stack_cell != return_cell_num) {
  5347. set_error_buf(error_buf, error_buf_size,
  5348. "type mismatch: stack size does not match block type");
  5349. goto fail;
  5350. }
  5351. /* Check stack values match return types */
  5352. frame_ref = loader_ctx->frame_ref;
  5353. for (i = (int32)return_count - 1; i >= 0; i--) {
  5354. if (!check_stack_top_values(frame_ref, available_stack_cell,
  5355. return_types[i], error_buf, error_buf_size))
  5356. return false;
  5357. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  5358. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  5359. }
  5360. return true;
  5361. fail:
  5362. return false;
  5363. }
  5364. #if WASM_ENABLE_FAST_INTERP != 0
  5365. /* Copy parameters to dynamic space.
  5366. * 1) POP original parameter out;
  5367. * 2) Push and copy original values to dynamic space.
  5368. * The copy instruction format:
  5369. * Part a: param count
  5370. * Part b: all param total cell num
  5371. * Part c: each param's cell_num, src offset and dst offset
  5372. * Part d: each param's src offset
  5373. * Part e: each param's dst offset
  5374. */
  5375. static bool
  5376. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  5377. char *error_buf, uint32 error_buf_size)
  5378. {
  5379. int16 *frame_offset = NULL;
  5380. uint8 *cells = NULL, cell;
  5381. int16 *src_offsets = NULL;
  5382. uint8 *emit_data = NULL;
  5383. uint32 i;
  5384. BranchBlock *block = loader_ctx->frame_csp - 1;
  5385. BlockType *block_type = &block->block_type;
  5386. WASMType *wasm_type = block_type->u.type;
  5387. uint32 param_count = block_type->u.type->param_count;
  5388. int16 condition_offset = 0;
  5389. bool disable_emit = false;
  5390. int16 operand_offset = 0;
  5391. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  5392. /* For if block, we also need copy the condition operand offset. */
  5393. if (is_if_block)
  5394. size += sizeof(*cells) + sizeof(*src_offsets);
  5395. /* Allocate memory for the emit data */
  5396. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5397. return false;
  5398. cells = emit_data;
  5399. src_offsets = (int16 *)(cells + param_count);
  5400. if (is_if_block)
  5401. condition_offset = *loader_ctx->frame_offset;
  5402. /* POP original parameter out */
  5403. for (i = 0; i < param_count; i++) {
  5404. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  5405. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5406. }
  5407. frame_offset = loader_ctx->frame_offset;
  5408. /* Get each param's cell num and src offset */
  5409. for (i = 0; i < param_count; i++) {
  5410. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  5411. cells[i] = cell;
  5412. src_offsets[i] = *frame_offset;
  5413. frame_offset += cell;
  5414. }
  5415. /* emit copy instruction */
  5416. emit_label(EXT_OP_COPY_STACK_VALUES);
  5417. /* Part a) */
  5418. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  5419. /* Part b) */
  5420. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  5421. : wasm_type->param_cell_num);
  5422. /* Part c) */
  5423. for (i = 0; i < param_count; i++)
  5424. emit_byte(loader_ctx, cells[i]);
  5425. if (is_if_block)
  5426. emit_byte(loader_ctx, 1);
  5427. /* Part d) */
  5428. for (i = 0; i < param_count; i++)
  5429. emit_operand(loader_ctx, src_offsets[i]);
  5430. if (is_if_block)
  5431. emit_operand(loader_ctx, condition_offset);
  5432. /* Part e) */
  5433. /* Push to dynamic space. The push will emit the dst offset. */
  5434. for (i = 0; i < param_count; i++)
  5435. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  5436. if (is_if_block)
  5437. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  5438. /* Free the emit data */
  5439. wasm_runtime_free(emit_data);
  5440. return true;
  5441. fail:
  5442. return false;
  5443. }
  5444. #endif
  5445. /* reset the stack to the state of before entering the last block */
  5446. #if WASM_ENABLE_FAST_INTERP != 0
  5447. #define RESET_STACK() \
  5448. do { \
  5449. loader_ctx->stack_cell_num = \
  5450. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5451. loader_ctx->frame_ref = \
  5452. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5453. loader_ctx->frame_offset = \
  5454. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  5455. } while (0)
  5456. #else
  5457. #define RESET_STACK() \
  5458. do { \
  5459. loader_ctx->stack_cell_num = \
  5460. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5461. loader_ctx->frame_ref = \
  5462. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5463. } while (0)
  5464. #endif
  5465. /* set current block's stack polymorphic state */
  5466. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  5467. do { \
  5468. BranchBlock *cur_block = loader_ctx->frame_csp - 1; \
  5469. cur_block->is_stack_polymorphic = flag; \
  5470. } while (0)
  5471. #define BLOCK_HAS_PARAM(block_type) \
  5472. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  5473. #define PRESERVE_LOCAL_FOR_BLOCK() \
  5474. do { \
  5475. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  5476. error_buf_size))) { \
  5477. goto fail; \
  5478. } \
  5479. } while (0)
  5480. #if WASM_ENABLE_REF_TYPES != 0
  5481. static bool
  5482. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  5483. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  5484. {
  5485. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  5486. return false;
  5487. }
  5488. if (p_elem_type) {
  5489. if (table_idx < module->import_table_count)
  5490. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  5491. else
  5492. *p_elem_type =
  5493. module->tables[module->import_table_count + table_idx]
  5494. .elem_type;
  5495. }
  5496. return true;
  5497. }
  5498. static bool
  5499. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  5500. uint8 *p_elem_type, char *error_buf,
  5501. uint32 error_buf_size)
  5502. {
  5503. if (table_seg_idx >= module->table_seg_count) {
  5504. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  5505. table_seg_idx);
  5506. return false;
  5507. }
  5508. if (p_elem_type) {
  5509. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  5510. }
  5511. return true;
  5512. }
  5513. #endif
  5514. static bool
  5515. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5516. uint32 cur_func_idx, char *error_buf,
  5517. uint32 error_buf_size)
  5518. {
  5519. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  5520. uint32 param_count, local_count, global_count;
  5521. uint8 *param_types, *local_types, local_type, global_type;
  5522. BlockType func_type;
  5523. uint16 *local_offsets, local_offset;
  5524. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  5525. uint32 table_seg_idx, data_seg_idx, count, i, align, mem_offset;
  5526. int32 i32_const = 0;
  5527. int64 i64;
  5528. uint8 opcode;
  5529. bool return_value = false;
  5530. WASMLoaderContext *loader_ctx;
  5531. BranchBlock *frame_csp_tmp;
  5532. #if WASM_ENABLE_FAST_INTERP != 0
  5533. uint8 *func_const_end, *func_const = NULL;
  5534. int16 operand_offset = 0;
  5535. uint8 last_op = 0;
  5536. bool disable_emit, preserve_local = false;
  5537. float32 f32;
  5538. float64 f64;
  5539. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  5540. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  5541. #endif
  5542. global_count = module->import_global_count + module->global_count;
  5543. param_count = func->func_type->param_count;
  5544. param_types = func->func_type->types;
  5545. func_type.is_value_type = false;
  5546. func_type.u.type = func->func_type;
  5547. local_count = func->local_count;
  5548. local_types = func->local_types;
  5549. local_offsets = func->local_offsets;
  5550. if (!(loader_ctx = wasm_loader_ctx_init(func))) {
  5551. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  5552. goto fail;
  5553. }
  5554. #if WASM_ENABLE_FAST_INTERP != 0
  5555. re_scan:
  5556. if (loader_ctx->code_compiled_size > 0) {
  5557. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  5558. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  5559. goto fail;
  5560. }
  5561. p = func->code;
  5562. func->code_compiled = loader_ctx->p_code_compiled;
  5563. func->code_compiled_size = loader_ctx->code_compiled_size;
  5564. }
  5565. #endif
  5566. PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p);
  5567. while (p < p_end) {
  5568. opcode = *p++;
  5569. #if WASM_ENABLE_FAST_INTERP != 0
  5570. p_org = p;
  5571. disable_emit = false;
  5572. emit_label(opcode);
  5573. #endif
  5574. switch (opcode) {
  5575. case WASM_OP_UNREACHABLE:
  5576. RESET_STACK();
  5577. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5578. break;
  5579. case WASM_OP_NOP:
  5580. #if WASM_ENABLE_FAST_INTERP != 0
  5581. skip_label();
  5582. #endif
  5583. break;
  5584. case WASM_OP_IF:
  5585. #if WASM_ENABLE_FAST_INTERP != 0
  5586. PRESERVE_LOCAL_FOR_BLOCK();
  5587. #endif
  5588. POP_I32();
  5589. goto handle_op_block_and_loop;
  5590. case WASM_OP_BLOCK:
  5591. case WASM_OP_LOOP:
  5592. #if WASM_ENABLE_FAST_INTERP != 0
  5593. PRESERVE_LOCAL_FOR_BLOCK();
  5594. #endif
  5595. handle_op_block_and_loop:
  5596. {
  5597. uint8 value_type;
  5598. BlockType block_type;
  5599. value_type = read_uint8(p);
  5600. if (is_byte_a_type(value_type)) {
  5601. /* If the first byte is one of these special values:
  5602. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  5603. * the single return value. */
  5604. block_type.is_value_type = true;
  5605. block_type.u.value_type = value_type;
  5606. }
  5607. else {
  5608. uint32 type_index;
  5609. /* Resolve the leb128 encoded type index as block type */
  5610. p--;
  5611. read_leb_uint32(p, p_end, type_index);
  5612. if (type_index >= module->type_count) {
  5613. set_error_buf(error_buf, error_buf_size,
  5614. "unknown type");
  5615. goto fail;
  5616. }
  5617. block_type.is_value_type = false;
  5618. block_type.u.type = module->types[type_index];
  5619. #if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \
  5620. && WASM_ENABLE_JIT == 0
  5621. /* If block use type index as block type, change the opcode
  5622. * to new extended opcode so that interpreter can resolve
  5623. * the block quickly.
  5624. */
  5625. #if WASM_ENABLE_DEBUG_INTERP != 0
  5626. record_fast_op(module, p - 2, *(p - 2));
  5627. #endif
  5628. *(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  5629. #endif
  5630. }
  5631. /* Pop block parameters from stack */
  5632. if (BLOCK_HAS_PARAM(block_type)) {
  5633. WASMType *wasm_type = block_type.u.type;
  5634. for (i = 0; i < block_type.u.type->param_count; i++)
  5635. POP_TYPE(
  5636. wasm_type->types[wasm_type->param_count - i - 1]);
  5637. }
  5638. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  5639. block_type, p);
  5640. /* Pass parameters to block */
  5641. if (BLOCK_HAS_PARAM(block_type)) {
  5642. for (i = 0; i < block_type.u.type->param_count; i++)
  5643. PUSH_TYPE(block_type.u.type->types[i]);
  5644. }
  5645. #if WASM_ENABLE_FAST_INTERP != 0
  5646. if (opcode == WASM_OP_BLOCK) {
  5647. skip_label();
  5648. }
  5649. else if (opcode == WASM_OP_LOOP) {
  5650. skip_label();
  5651. if (BLOCK_HAS_PARAM(block_type)) {
  5652. /* Make sure params are in dynamic space */
  5653. if (!copy_params_to_dynamic_space(
  5654. loader_ctx, false, error_buf, error_buf_size))
  5655. goto fail;
  5656. }
  5657. (loader_ctx->frame_csp - 1)->code_compiled =
  5658. loader_ctx->p_code_compiled;
  5659. }
  5660. else if (opcode == WASM_OP_IF) {
  5661. /* If block has parameters, we should make sure they are in
  5662. * dynamic space. Otherwise, when else branch is missing,
  5663. * the later opcode may consume incorrect operand offset.
  5664. * Spec case:
  5665. * (func (export "params-id") (param i32) (result i32)
  5666. * (i32.const 1)
  5667. * (i32.const 2)
  5668. * (if (param i32 i32) (result i32 i32) (local.get 0)
  5669. * (then)) (i32.add)
  5670. * )
  5671. *
  5672. * So we should emit a copy instruction before the if.
  5673. *
  5674. * And we also need to save the parameter offsets and
  5675. * recover them before entering else branch.
  5676. *
  5677. */
  5678. if (BLOCK_HAS_PARAM(block_type)) {
  5679. BranchBlock *block = loader_ctx->frame_csp - 1;
  5680. uint64 size;
  5681. /* skip the if condition operand offset */
  5682. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5683. /* skip the if label */
  5684. skip_label();
  5685. /* Emit a copy instruction */
  5686. if (!copy_params_to_dynamic_space(
  5687. loader_ctx, true, error_buf, error_buf_size))
  5688. goto fail;
  5689. /* Emit the if instruction */
  5690. emit_label(opcode);
  5691. /* Emit the new condition operand offset */
  5692. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5693. /* Save top param_count values of frame_offset stack, so
  5694. * that we can recover it before executing else branch
  5695. */
  5696. size = sizeof(int16)
  5697. * (uint64)block_type.u.type->param_cell_num;
  5698. if (!(block->param_frame_offsets = loader_malloc(
  5699. size, error_buf, error_buf_size)))
  5700. goto fail;
  5701. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  5702. loader_ctx->frame_offset
  5703. - size / sizeof(int16),
  5704. (uint32)size);
  5705. }
  5706. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5707. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5708. }
  5709. #endif
  5710. break;
  5711. }
  5712. case WASM_OP_ELSE:
  5713. {
  5714. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5715. if (loader_ctx->csp_num < 2
  5716. || (loader_ctx->frame_csp - 1)->label_type
  5717. != LABEL_TYPE_IF) {
  5718. set_error_buf(
  5719. error_buf, error_buf_size,
  5720. "opcode else found without matched opcode if");
  5721. goto fail;
  5722. }
  5723. /* check whether if branch's stack matches its result type */
  5724. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  5725. error_buf, error_buf_size))
  5726. goto fail;
  5727. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  5728. #if WASM_ENABLE_FAST_INTERP != 0
  5729. /* if the result of if branch is in local or const area, add a
  5730. * copy op */
  5731. RESERVE_BLOCK_RET();
  5732. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5733. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5734. #endif
  5735. RESET_STACK();
  5736. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5737. /* Pass parameters to if-false branch */
  5738. if (BLOCK_HAS_PARAM(block_type)) {
  5739. for (i = 0; i < block_type.u.type->param_count; i++)
  5740. PUSH_TYPE(block_type.u.type->types[i]);
  5741. }
  5742. #if WASM_ENABLE_FAST_INTERP != 0
  5743. /* Recover top param_count values of frame_offset stack */
  5744. if (BLOCK_HAS_PARAM((block_type))) {
  5745. uint32 size;
  5746. BranchBlock *block = loader_ctx->frame_csp - 1;
  5747. size = sizeof(int16) * block_type.u.type->param_cell_num;
  5748. bh_memcpy_s(loader_ctx->frame_offset, size,
  5749. block->param_frame_offsets, size);
  5750. loader_ctx->frame_offset += (size / sizeof(int16));
  5751. }
  5752. #endif
  5753. break;
  5754. }
  5755. case WASM_OP_END:
  5756. {
  5757. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5758. /* check whether block stack matches its result type */
  5759. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  5760. error_buf_size))
  5761. goto fail;
  5762. /* if no else branch, and return types do not match param types,
  5763. * fail */
  5764. if (cur_block->label_type == LABEL_TYPE_IF
  5765. && !cur_block->else_addr) {
  5766. uint32 param_count = 0, ret_count = 0;
  5767. uint8 *param_types = NULL, *ret_types = NULL;
  5768. BlockType *block_type = &cur_block->block_type;
  5769. if (block_type->is_value_type) {
  5770. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  5771. ret_count = 1;
  5772. ret_types = &block_type->u.value_type;
  5773. }
  5774. }
  5775. else {
  5776. param_count = block_type->u.type->param_count;
  5777. ret_count = block_type->u.type->result_count;
  5778. param_types = block_type->u.type->types;
  5779. ret_types = block_type->u.type->types + param_count;
  5780. }
  5781. if (param_count != ret_count
  5782. || (param_count
  5783. && memcmp(param_types, ret_types, param_count))) {
  5784. set_error_buf(error_buf, error_buf_size,
  5785. "type mismatch: else branch missing");
  5786. goto fail;
  5787. }
  5788. }
  5789. POP_CSP();
  5790. #if WASM_ENABLE_FAST_INTERP != 0
  5791. skip_label();
  5792. /* copy the result to the block return address */
  5793. RESERVE_BLOCK_RET();
  5794. apply_label_patch(loader_ctx, 0, PATCH_END);
  5795. free_label_patch_list(loader_ctx->frame_csp);
  5796. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  5797. int32 idx;
  5798. uint8 ret_type;
  5799. emit_label(WASM_OP_RETURN);
  5800. for (idx = (int32)func->func_type->result_count - 1;
  5801. idx >= 0; idx--) {
  5802. ret_type = *(func->func_type->types
  5803. + func->func_type->param_count + idx);
  5804. POP_OFFSET_TYPE(ret_type);
  5805. }
  5806. }
  5807. #endif
  5808. if (loader_ctx->csp_num > 0) {
  5809. loader_ctx->frame_csp->end_addr = p - 1;
  5810. }
  5811. else {
  5812. /* end of function block, function will return,
  5813. ignore the following bytecodes */
  5814. p = p_end;
  5815. continue;
  5816. }
  5817. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5818. break;
  5819. }
  5820. case WASM_OP_BR:
  5821. {
  5822. if (!(frame_csp_tmp = check_branch_block(
  5823. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5824. goto fail;
  5825. RESET_STACK();
  5826. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5827. break;
  5828. }
  5829. case WASM_OP_BR_IF:
  5830. {
  5831. POP_I32();
  5832. if (!(frame_csp_tmp = check_branch_block(
  5833. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5834. goto fail;
  5835. break;
  5836. }
  5837. case WASM_OP_BR_TABLE:
  5838. {
  5839. uint8 *ret_types = NULL;
  5840. uint32 ret_count = 0;
  5841. read_leb_uint32(p, p_end, count);
  5842. #if WASM_ENABLE_FAST_INTERP != 0
  5843. emit_uint32(loader_ctx, count);
  5844. #endif
  5845. POP_I32();
  5846. for (i = 0; i <= count; i++) {
  5847. if (!(frame_csp_tmp =
  5848. check_branch_block(loader_ctx, &p, p_end,
  5849. error_buf, error_buf_size)))
  5850. goto fail;
  5851. if (i == 0) {
  5852. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  5853. ret_count = block_type_get_result_types(
  5854. &frame_csp_tmp->block_type, &ret_types);
  5855. }
  5856. else {
  5857. uint8 *tmp_ret_types = NULL;
  5858. uint32 tmp_ret_count = 0;
  5859. /* Check whether all table items have the same return
  5860. * type */
  5861. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  5862. tmp_ret_count = block_type_get_result_types(
  5863. &frame_csp_tmp->block_type, &tmp_ret_types);
  5864. if (ret_count != tmp_ret_count
  5865. || (ret_count
  5866. && 0
  5867. != memcmp(ret_types, tmp_ret_types,
  5868. ret_count))) {
  5869. set_error_buf(
  5870. error_buf, error_buf_size,
  5871. "type mismatch: br_table targets must "
  5872. "all use same result type");
  5873. goto fail;
  5874. }
  5875. }
  5876. }
  5877. RESET_STACK();
  5878. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5879. break;
  5880. }
  5881. case WASM_OP_RETURN:
  5882. {
  5883. int32 idx;
  5884. uint8 ret_type;
  5885. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  5886. idx--) {
  5887. ret_type = *(func->func_type->types
  5888. + func->func_type->param_count + idx);
  5889. POP_TYPE(ret_type);
  5890. #if WASM_ENABLE_FAST_INTERP != 0
  5891. /* emit the offset after return opcode */
  5892. POP_OFFSET_TYPE(ret_type);
  5893. #endif
  5894. }
  5895. RESET_STACK();
  5896. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5897. break;
  5898. }
  5899. case WASM_OP_CALL:
  5900. #if WASM_ENABLE_TAIL_CALL != 0
  5901. case WASM_OP_RETURN_CALL:
  5902. #endif
  5903. {
  5904. WASMType *func_type;
  5905. int32 idx;
  5906. read_leb_uint32(p, p_end, func_idx);
  5907. #if WASM_ENABLE_FAST_INTERP != 0
  5908. /* we need to emit func_idx before arguments */
  5909. emit_uint32(loader_ctx, func_idx);
  5910. #endif
  5911. if (!check_function_index(module, func_idx, error_buf,
  5912. error_buf_size)) {
  5913. goto fail;
  5914. }
  5915. if (func_idx < module->import_function_count)
  5916. func_type =
  5917. module->import_functions[func_idx].u.function.func_type;
  5918. else
  5919. func_type = module
  5920. ->functions[func_idx
  5921. - module->import_function_count]
  5922. ->func_type;
  5923. if (func_type->param_count > 0) {
  5924. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5925. idx--) {
  5926. POP_TYPE(func_type->types[idx]);
  5927. #if WASM_ENABLE_FAST_INTERP != 0
  5928. POP_OFFSET_TYPE(func_type->types[idx]);
  5929. #endif
  5930. }
  5931. }
  5932. #if WASM_ENABLE_TAIL_CALL != 0
  5933. if (opcode == WASM_OP_CALL) {
  5934. #endif
  5935. for (i = 0; i < func_type->result_count; i++) {
  5936. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5937. #if WASM_ENABLE_FAST_INTERP != 0
  5938. /* Here we emit each return value's dynamic_offset. But
  5939. * in fact these offsets are continuous, so interpreter
  5940. * only need to get the first return value's offset.
  5941. */
  5942. PUSH_OFFSET_TYPE(
  5943. func_type->types[func_type->param_count + i]);
  5944. #endif
  5945. }
  5946. #if WASM_ENABLE_TAIL_CALL != 0
  5947. }
  5948. else {
  5949. uint8 type;
  5950. if (func_type->result_count
  5951. != func->func_type->result_count) {
  5952. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  5953. "type mismatch: expect ",
  5954. func->func_type->result_count,
  5955. " return values but got other");
  5956. goto fail;
  5957. }
  5958. for (i = 0; i < func_type->result_count; i++) {
  5959. type = func->func_type
  5960. ->types[func->func_type->param_count + i];
  5961. if (func_type->types[func_type->param_count + i]
  5962. != type) {
  5963. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  5964. "type mismatch: expect ",
  5965. type2str(type), " but got other");
  5966. goto fail;
  5967. }
  5968. }
  5969. RESET_STACK();
  5970. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5971. }
  5972. #endif
  5973. func->has_op_func_call = true;
  5974. break;
  5975. }
  5976. /*
  5977. * if disable reference type: call_indirect typeidx, 0x00
  5978. * if enable reference type: call_indirect typeidx, tableidx
  5979. */
  5980. case WASM_OP_CALL_INDIRECT:
  5981. #if WASM_ENABLE_TAIL_CALL != 0
  5982. case WASM_OP_RETURN_CALL_INDIRECT:
  5983. #endif
  5984. {
  5985. int32 idx;
  5986. WASMType *func_type;
  5987. read_leb_uint32(p, p_end, type_idx);
  5988. #if WASM_ENABLE_REF_TYPES != 0
  5989. read_leb_uint32(p, p_end, table_idx);
  5990. #else
  5991. CHECK_BUF(p, p_end, 1);
  5992. table_idx = read_uint8(p);
  5993. #endif
  5994. if (!check_table_index(module, table_idx, error_buf,
  5995. error_buf_size)) {
  5996. goto fail;
  5997. }
  5998. #if WASM_ENABLE_FAST_INTERP != 0
  5999. /* we need to emit before arguments */
  6000. #if WASM_ENABLE_TAIL_CALL != 0
  6001. emit_byte(loader_ctx, opcode);
  6002. #endif
  6003. emit_uint32(loader_ctx, type_idx);
  6004. emit_uint32(loader_ctx, table_idx);
  6005. #endif
  6006. /* skip elem idx */
  6007. POP_I32();
  6008. if (type_idx >= module->type_count) {
  6009. set_error_buf(error_buf, error_buf_size, "unknown type");
  6010. goto fail;
  6011. }
  6012. func_type = module->types[type_idx];
  6013. if (func_type->param_count > 0) {
  6014. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6015. idx--) {
  6016. POP_TYPE(func_type->types[idx]);
  6017. #if WASM_ENABLE_FAST_INTERP != 0
  6018. POP_OFFSET_TYPE(func_type->types[idx]);
  6019. #endif
  6020. }
  6021. }
  6022. #if WASM_ENABLE_TAIL_CALL != 0
  6023. if (opcode == WASM_OP_CALL_INDIRECT) {
  6024. #endif
  6025. for (i = 0; i < func_type->result_count; i++) {
  6026. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6027. #if WASM_ENABLE_FAST_INTERP != 0
  6028. PUSH_OFFSET_TYPE(
  6029. func_type->types[func_type->param_count + i]);
  6030. #endif
  6031. }
  6032. #if WASM_ENABLE_TAIL_CALL != 0
  6033. }
  6034. else {
  6035. uint8 type;
  6036. if (func_type->result_count
  6037. != func->func_type->result_count) {
  6038. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  6039. "type mismatch: expect ",
  6040. func->func_type->result_count,
  6041. " return values but got other");
  6042. goto fail;
  6043. }
  6044. for (i = 0; i < func_type->result_count; i++) {
  6045. type = func->func_type
  6046. ->types[func->func_type->param_count + i];
  6047. if (func_type->types[func_type->param_count + i]
  6048. != type) {
  6049. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6050. "type mismatch: expect ",
  6051. type2str(type), " but got other");
  6052. goto fail;
  6053. }
  6054. }
  6055. RESET_STACK();
  6056. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6057. }
  6058. #endif
  6059. func->has_op_func_call = true;
  6060. break;
  6061. }
  6062. case WASM_OP_DROP:
  6063. case WASM_OP_DROP_64:
  6064. {
  6065. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6066. int32 available_stack_cell =
  6067. (int32)(loader_ctx->stack_cell_num
  6068. - cur_block->stack_cell_num);
  6069. if (available_stack_cell <= 0
  6070. && !cur_block->is_stack_polymorphic) {
  6071. set_error_buf(error_buf, error_buf_size,
  6072. "type mismatch, opcode drop was found "
  6073. "but stack was empty");
  6074. goto fail;
  6075. }
  6076. if (available_stack_cell > 0) {
  6077. if (*(loader_ctx->frame_ref - 1) == REF_I32
  6078. || *(loader_ctx->frame_ref - 1) == REF_F32
  6079. #if WASM_ENABLE_REF_TYPES != 0
  6080. || *(loader_ctx->frame_ref - 1) == REF_FUNCREF
  6081. || *(loader_ctx->frame_ref - 1) == REF_EXTERNREF
  6082. #endif
  6083. ) {
  6084. loader_ctx->frame_ref--;
  6085. loader_ctx->stack_cell_num--;
  6086. #if WASM_ENABLE_FAST_INTERP != 0
  6087. skip_label();
  6088. loader_ctx->frame_offset--;
  6089. if (*(loader_ctx->frame_offset)
  6090. > loader_ctx->start_dynamic_offset)
  6091. loader_ctx->dynamic_offset--;
  6092. #endif
  6093. }
  6094. else if (*(loader_ctx->frame_ref - 1) == REF_I64_1
  6095. || *(loader_ctx->frame_ref - 1) == REF_F64_1) {
  6096. loader_ctx->frame_ref -= 2;
  6097. loader_ctx->stack_cell_num -= 2;
  6098. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  6099. *(p - 1) = WASM_OP_DROP_64;
  6100. #endif
  6101. #if WASM_ENABLE_FAST_INTERP != 0
  6102. skip_label();
  6103. loader_ctx->frame_offset -= 2;
  6104. if (*(loader_ctx->frame_offset)
  6105. > loader_ctx->start_dynamic_offset)
  6106. loader_ctx->dynamic_offset -= 2;
  6107. #endif
  6108. }
  6109. #if WASM_ENABLE_SIMD != 0
  6110. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6111. else if (*(loader_ctx->frame_ref - 1) == REF_V128_1) {
  6112. loader_ctx->frame_ref -= 4;
  6113. loader_ctx->stack_cell_num -= 4;
  6114. }
  6115. #endif
  6116. #endif
  6117. else {
  6118. set_error_buf(error_buf, error_buf_size,
  6119. "type mismatch");
  6120. goto fail;
  6121. }
  6122. }
  6123. else {
  6124. #if WASM_ENABLE_FAST_INTERP != 0
  6125. skip_label();
  6126. #endif
  6127. }
  6128. break;
  6129. }
  6130. case WASM_OP_SELECT:
  6131. case WASM_OP_SELECT_64:
  6132. {
  6133. uint8 ref_type;
  6134. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6135. int32 available_stack_cell;
  6136. POP_I32();
  6137. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  6138. - cur_block->stack_cell_num);
  6139. if (available_stack_cell <= 0
  6140. && !cur_block->is_stack_polymorphic) {
  6141. set_error_buf(error_buf, error_buf_size,
  6142. "type mismatch or invalid result arity, "
  6143. "opcode select was found "
  6144. "but stack was empty");
  6145. goto fail;
  6146. }
  6147. if (available_stack_cell > 0) {
  6148. switch (*(loader_ctx->frame_ref - 1)) {
  6149. case REF_I32:
  6150. case REF_F32:
  6151. break;
  6152. case REF_I64_2:
  6153. case REF_F64_2:
  6154. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  6155. *(p - 1) = WASM_OP_SELECT_64;
  6156. #endif
  6157. #if WASM_ENABLE_FAST_INTERP != 0
  6158. if (loader_ctx->p_code_compiled) {
  6159. uint8 opcode_tmp = WASM_OP_SELECT_64;
  6160. uint8 *p_code_compiled_tmp =
  6161. loader_ctx->p_code_compiled - 2;
  6162. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6163. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6164. *(void **)(p_code_compiled_tmp
  6165. - sizeof(void *)) =
  6166. handle_table[opcode_tmp];
  6167. #else
  6168. int32 offset =
  6169. (int32)((uint8 *)handle_table[opcode_tmp]
  6170. - (uint8 *)handle_table[0]);
  6171. if (!(offset >= INT16_MIN
  6172. && offset < INT16_MAX)) {
  6173. set_error_buf(error_buf, error_buf_size,
  6174. "pre-compiled label offset "
  6175. "out of range");
  6176. goto fail;
  6177. }
  6178. *(int16 *)(p_code_compiled_tmp
  6179. - sizeof(int16)) = (int16)offset;
  6180. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6181. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6182. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6183. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6184. #else
  6185. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6186. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6187. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6188. }
  6189. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6190. break;
  6191. #if WASM_ENABLE_SIMD != 0
  6192. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6193. case REF_V128_4:
  6194. break;
  6195. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6196. #endif /* WASM_ENABLE_SIMD != 0 */
  6197. default:
  6198. {
  6199. set_error_buf(error_buf, error_buf_size,
  6200. "type mismatch");
  6201. goto fail;
  6202. }
  6203. }
  6204. ref_type = *(loader_ctx->frame_ref - 1);
  6205. #if WASM_ENABLE_FAST_INTERP != 0
  6206. POP_OFFSET_TYPE(ref_type);
  6207. POP_TYPE(ref_type);
  6208. POP_OFFSET_TYPE(ref_type);
  6209. POP_TYPE(ref_type);
  6210. PUSH_OFFSET_TYPE(ref_type);
  6211. PUSH_TYPE(ref_type);
  6212. #else
  6213. POP2_AND_PUSH(ref_type, ref_type);
  6214. #endif
  6215. }
  6216. else {
  6217. #if WASM_ENABLE_FAST_INTERP != 0
  6218. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  6219. #endif
  6220. PUSH_TYPE(VALUE_TYPE_ANY);
  6221. }
  6222. break;
  6223. }
  6224. #if WASM_ENABLE_REF_TYPES != 0
  6225. case WASM_OP_SELECT_T:
  6226. {
  6227. uint8 vec_len, ref_type;
  6228. read_leb_uint32(p, p_end, vec_len);
  6229. if (!vec_len) {
  6230. set_error_buf(error_buf, error_buf_size,
  6231. "invalid result arity");
  6232. goto fail;
  6233. }
  6234. CHECK_BUF(p, p_end, 1);
  6235. ref_type = read_uint8(p);
  6236. if (!is_value_type(ref_type)) {
  6237. set_error_buf(error_buf, error_buf_size,
  6238. "unknown value type");
  6239. goto fail;
  6240. }
  6241. POP_I32();
  6242. #if WASM_ENABLE_FAST_INTERP != 0
  6243. if (loader_ctx->p_code_compiled) {
  6244. uint8 opcode_tmp = WASM_OP_SELECT;
  6245. uint8 *p_code_compiled_tmp =
  6246. loader_ctx->p_code_compiled - 2;
  6247. if (ref_type == VALUE_TYPE_V128) {
  6248. #if (WASM_ENABLE_SIMD == 0) \
  6249. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  6250. set_error_buf(error_buf, error_buf_size,
  6251. "SIMD v128 type isn't supported");
  6252. goto fail;
  6253. #endif
  6254. }
  6255. else {
  6256. if (ref_type == VALUE_TYPE_F64
  6257. || ref_type == VALUE_TYPE_I64)
  6258. opcode_tmp = WASM_OP_SELECT_64;
  6259. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6260. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6261. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  6262. handle_table[opcode_tmp];
  6263. #else
  6264. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  6265. - (uint8 *)handle_table[0]);
  6266. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  6267. set_error_buf(
  6268. error_buf, error_buf_size,
  6269. "pre-compiled label offset out of range");
  6270. goto fail;
  6271. }
  6272. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  6273. (int16)offset;
  6274. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6275. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6276. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6277. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6278. #else
  6279. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6280. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6281. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6282. }
  6283. }
  6284. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6285. #if WASM_ENABLE_FAST_INTERP != 0
  6286. POP_OFFSET_TYPE(ref_type);
  6287. POP_TYPE(ref_type);
  6288. POP_OFFSET_TYPE(ref_type);
  6289. POP_TYPE(ref_type);
  6290. PUSH_OFFSET_TYPE(ref_type);
  6291. PUSH_TYPE(ref_type);
  6292. #else
  6293. POP2_AND_PUSH(ref_type, ref_type);
  6294. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6295. (void)vec_len;
  6296. break;
  6297. }
  6298. /* table.get x. tables[x]. [i32] -> [t] */
  6299. /* table.set x. tables[x]. [i32 t] -> [] */
  6300. case WASM_OP_TABLE_GET:
  6301. case WASM_OP_TABLE_SET:
  6302. {
  6303. uint8 decl_ref_type;
  6304. read_leb_uint32(p, p_end, table_idx);
  6305. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  6306. error_buf, error_buf_size))
  6307. goto fail;
  6308. #if WASM_ENABLE_FAST_INTERP != 0
  6309. emit_uint32(loader_ctx, table_idx);
  6310. #endif
  6311. if (opcode == WASM_OP_TABLE_GET) {
  6312. POP_I32();
  6313. #if WASM_ENABLE_FAST_INTERP != 0
  6314. PUSH_OFFSET_TYPE(decl_ref_type);
  6315. #endif
  6316. PUSH_TYPE(decl_ref_type);
  6317. }
  6318. else {
  6319. #if WASM_ENABLE_FAST_INTERP != 0
  6320. POP_OFFSET_TYPE(decl_ref_type);
  6321. #endif
  6322. POP_TYPE(decl_ref_type);
  6323. POP_I32();
  6324. }
  6325. break;
  6326. }
  6327. case WASM_OP_REF_NULL:
  6328. {
  6329. uint8 ref_type;
  6330. CHECK_BUF(p, p_end, 1);
  6331. ref_type = read_uint8(p);
  6332. if (ref_type != VALUE_TYPE_FUNCREF
  6333. && ref_type != VALUE_TYPE_EXTERNREF) {
  6334. set_error_buf(error_buf, error_buf_size,
  6335. "unknown value type");
  6336. goto fail;
  6337. }
  6338. #if WASM_ENABLE_FAST_INTERP != 0
  6339. PUSH_OFFSET_TYPE(ref_type);
  6340. #endif
  6341. PUSH_TYPE(ref_type);
  6342. break;
  6343. }
  6344. case WASM_OP_REF_IS_NULL:
  6345. {
  6346. #if WASM_ENABLE_FAST_INTERP != 0
  6347. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  6348. VALUE_TYPE_FUNCREF,
  6349. error_buf, error_buf_size)
  6350. && !wasm_loader_pop_frame_ref_offset(
  6351. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  6352. error_buf_size)) {
  6353. goto fail;
  6354. }
  6355. #else
  6356. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  6357. error_buf, error_buf_size)
  6358. && !wasm_loader_pop_frame_ref(loader_ctx,
  6359. VALUE_TYPE_EXTERNREF,
  6360. error_buf, error_buf_size)) {
  6361. goto fail;
  6362. }
  6363. #endif
  6364. PUSH_I32();
  6365. break;
  6366. }
  6367. case WASM_OP_REF_FUNC:
  6368. {
  6369. read_leb_uint32(p, p_end, func_idx);
  6370. if (!check_function_index(module, func_idx, error_buf,
  6371. error_buf_size)) {
  6372. goto fail;
  6373. }
  6374. if (func_idx == cur_func_idx) {
  6375. WASMTableSeg *table_seg = module->table_segments;
  6376. bool func_declared = false;
  6377. uint32 j;
  6378. /* Check whether current function is declared */
  6379. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  6380. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  6381. && wasm_elem_is_declarative(table_seg->mode)) {
  6382. for (j = 0; j < table_seg->function_count; j++) {
  6383. if (table_seg->func_indexes[j]
  6384. == cur_func_idx) {
  6385. func_declared = true;
  6386. break;
  6387. }
  6388. }
  6389. }
  6390. }
  6391. if (!func_declared) {
  6392. set_error_buf(error_buf, error_buf_size,
  6393. "undeclared function reference");
  6394. goto fail;
  6395. }
  6396. }
  6397. #if WASM_ENABLE_FAST_INTERP != 0
  6398. emit_uint32(loader_ctx, func_idx);
  6399. #endif
  6400. PUSH_FUNCREF();
  6401. break;
  6402. }
  6403. #endif /* WASM_ENABLE_REF_TYPES */
  6404. case WASM_OP_GET_LOCAL:
  6405. {
  6406. p_org = p - 1;
  6407. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6408. PUSH_TYPE(local_type);
  6409. #if WASM_ENABLE_FAST_INTERP != 0
  6410. /* Get Local is optimized out */
  6411. skip_label();
  6412. disable_emit = true;
  6413. operand_offset = local_offset;
  6414. PUSH_OFFSET_TYPE(local_type);
  6415. #else
  6416. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  6417. if (local_offset < 0x80) {
  6418. #if WASM_ENABLE_DEBUG_INTERP != 0
  6419. record_fast_op(module, p_org, *p_org);
  6420. #endif
  6421. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  6422. if (is_32bit_type(local_type)) {
  6423. #if WASM_ENABLE_DEBUG_INTERP != 0
  6424. record_fast_op(module, p_org, *p_org);
  6425. #endif
  6426. *p_org++ = (uint8)local_offset;
  6427. }
  6428. else {
  6429. #if WASM_ENABLE_DEBUG_INTERP != 0
  6430. record_fast_op(module, p_org, *p_org);
  6431. #endif
  6432. *p_org++ = (uint8)(local_offset | 0x80);
  6433. }
  6434. while (p_org < p) {
  6435. #if WASM_ENABLE_DEBUG_INTERP != 0
  6436. record_fast_op(module, p_org, *p_org);
  6437. #endif
  6438. *p_org++ = WASM_OP_NOP;
  6439. }
  6440. }
  6441. #endif
  6442. #endif
  6443. break;
  6444. }
  6445. case WASM_OP_SET_LOCAL:
  6446. {
  6447. p_org = p - 1;
  6448. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6449. POP_TYPE(local_type);
  6450. #if WASM_ENABLE_FAST_INTERP != 0
  6451. if (!(preserve_referenced_local(
  6452. loader_ctx, opcode, local_offset, local_type,
  6453. &preserve_local, error_buf, error_buf_size)))
  6454. goto fail;
  6455. if (local_offset < 256) {
  6456. skip_label();
  6457. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  6458. if (loader_ctx->p_code_compiled)
  6459. STORE_U16(loader_ctx->p_code_compiled - 2,
  6460. local_offset);
  6461. loader_ctx->frame_offset--;
  6462. loader_ctx->dynamic_offset--;
  6463. }
  6464. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  6465. if (loader_ctx->p_code_compiled)
  6466. STORE_U16(loader_ctx->p_code_compiled - 2,
  6467. local_offset);
  6468. loader_ctx->frame_offset -= 2;
  6469. loader_ctx->dynamic_offset -= 2;
  6470. }
  6471. else {
  6472. if (is_32bit_type(local_type)) {
  6473. emit_label(EXT_OP_SET_LOCAL_FAST);
  6474. emit_byte(loader_ctx, (uint8)local_offset);
  6475. }
  6476. else {
  6477. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  6478. emit_byte(loader_ctx, (uint8)local_offset);
  6479. }
  6480. POP_OFFSET_TYPE(local_type);
  6481. }
  6482. }
  6483. else { /* local index larger than 255, reserve leb */
  6484. emit_uint32(loader_ctx, local_idx);
  6485. POP_OFFSET_TYPE(local_type);
  6486. }
  6487. #else
  6488. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  6489. if (local_offset < 0x80) {
  6490. #if WASM_ENABLE_DEBUG_INTERP != 0
  6491. record_fast_op(module, p_org, *p_org);
  6492. #endif
  6493. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  6494. if (is_32bit_type(local_type)) {
  6495. #if WASM_ENABLE_DEBUG_INTERP != 0
  6496. record_fast_op(module, p_org, *p_org);
  6497. #endif
  6498. *p_org++ = (uint8)local_offset;
  6499. }
  6500. else {
  6501. #if WASM_ENABLE_DEBUG_INTERP != 0
  6502. record_fast_op(module, p_org, *p_org);
  6503. #endif
  6504. *p_org++ = (uint8)(local_offset | 0x80);
  6505. }
  6506. while (p_org < p) {
  6507. #if WASM_ENABLE_DEBUG_INTERP != 0
  6508. record_fast_op(module, p_org, *p_org);
  6509. #endif
  6510. *p_org++ = WASM_OP_NOP;
  6511. }
  6512. }
  6513. #endif
  6514. #endif
  6515. break;
  6516. }
  6517. case WASM_OP_TEE_LOCAL:
  6518. {
  6519. p_org = p - 1;
  6520. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6521. #if WASM_ENABLE_FAST_INTERP != 0
  6522. /* If the stack is in polymorphic state, do fake pop and push on
  6523. offset stack to keep the depth of offset stack to be the
  6524. same with ref stack */
  6525. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6526. if (cur_block->is_stack_polymorphic) {
  6527. POP_OFFSET_TYPE(local_type);
  6528. PUSH_OFFSET_TYPE(local_type);
  6529. }
  6530. #endif
  6531. POP_TYPE(local_type);
  6532. PUSH_TYPE(local_type);
  6533. #if WASM_ENABLE_FAST_INTERP != 0
  6534. if (!(preserve_referenced_local(
  6535. loader_ctx, opcode, local_offset, local_type,
  6536. &preserve_local, error_buf, error_buf_size)))
  6537. goto fail;
  6538. if (local_offset < 256) {
  6539. skip_label();
  6540. if (is_32bit_type(local_type)) {
  6541. emit_label(EXT_OP_TEE_LOCAL_FAST);
  6542. emit_byte(loader_ctx, (uint8)local_offset);
  6543. }
  6544. else {
  6545. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  6546. emit_byte(loader_ctx, (uint8)local_offset);
  6547. }
  6548. }
  6549. else { /* local index larger than 255, reserve leb */
  6550. emit_uint32(loader_ctx, local_idx);
  6551. }
  6552. emit_operand(loader_ctx,
  6553. *(loader_ctx->frame_offset
  6554. - wasm_value_type_cell_num(local_type)));
  6555. #else
  6556. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  6557. if (local_offset < 0x80) {
  6558. #if WASM_ENABLE_DEBUG_INTERP != 0
  6559. record_fast_op(module, p_org, *p_org);
  6560. #endif
  6561. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  6562. if (is_32bit_type(local_type)) {
  6563. #if WASM_ENABLE_DEBUG_INTERP != 0
  6564. record_fast_op(module, p_org, *p_org);
  6565. #endif
  6566. *p_org++ = (uint8)local_offset;
  6567. }
  6568. else {
  6569. #if WASM_ENABLE_DEBUG_INTERP != 0
  6570. record_fast_op(module, p_org, *p_org);
  6571. #endif
  6572. *p_org++ = (uint8)(local_offset | 0x80);
  6573. }
  6574. while (p_org < p) {
  6575. #if WASM_ENABLE_DEBUG_INTERP != 0
  6576. record_fast_op(module, p_org, *p_org);
  6577. #endif
  6578. *p_org++ = WASM_OP_NOP;
  6579. }
  6580. }
  6581. #endif
  6582. #endif
  6583. break;
  6584. }
  6585. case WASM_OP_GET_GLOBAL:
  6586. {
  6587. p_org = p - 1;
  6588. read_leb_uint32(p, p_end, global_idx);
  6589. if (global_idx >= global_count) {
  6590. set_error_buf(error_buf, error_buf_size, "unknown global");
  6591. goto fail;
  6592. }
  6593. global_type =
  6594. global_idx < module->import_global_count
  6595. ? module->import_globals[global_idx].u.global.type
  6596. : module
  6597. ->globals[global_idx
  6598. - module->import_global_count]
  6599. .type;
  6600. PUSH_TYPE(global_type);
  6601. #if WASM_ENABLE_FAST_INTERP == 0
  6602. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  6603. if (global_type == VALUE_TYPE_I64
  6604. || global_type == VALUE_TYPE_F64) {
  6605. #if WASM_ENABLE_DEBUG_INTERP != 0
  6606. record_fast_op(module, p_org, *p_org);
  6607. #endif
  6608. *p_org = WASM_OP_GET_GLOBAL_64;
  6609. }
  6610. #endif
  6611. #else /* else of WASM_ENABLE_FAST_INTERP */
  6612. if (global_type == VALUE_TYPE_I64
  6613. || global_type == VALUE_TYPE_F64) {
  6614. skip_label();
  6615. emit_label(WASM_OP_GET_GLOBAL_64);
  6616. }
  6617. emit_uint32(loader_ctx, global_idx);
  6618. PUSH_OFFSET_TYPE(global_type);
  6619. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6620. break;
  6621. }
  6622. case WASM_OP_SET_GLOBAL:
  6623. {
  6624. bool is_mutable = false;
  6625. p_org = p - 1;
  6626. read_leb_uint32(p, p_end, global_idx);
  6627. if (global_idx >= global_count) {
  6628. set_error_buf(error_buf, error_buf_size, "unknown global");
  6629. goto fail;
  6630. }
  6631. is_mutable =
  6632. global_idx < module->import_global_count
  6633. ? module->import_globals[global_idx].u.global.is_mutable
  6634. : module
  6635. ->globals[global_idx
  6636. - module->import_global_count]
  6637. .is_mutable;
  6638. if (!is_mutable) {
  6639. set_error_buf(error_buf, error_buf_size,
  6640. "global is immutable");
  6641. goto fail;
  6642. }
  6643. global_type =
  6644. global_idx < module->import_global_count
  6645. ? module->import_globals[global_idx].u.global.type
  6646. : module
  6647. ->globals[global_idx
  6648. - module->import_global_count]
  6649. .type;
  6650. POP_TYPE(global_type);
  6651. #if WASM_ENABLE_FAST_INTERP == 0
  6652. if (global_type == VALUE_TYPE_I64
  6653. || global_type == VALUE_TYPE_F64) {
  6654. #if WASM_ENABLE_DEBUG_INTERP != 0
  6655. record_fast_op(module, p_org, *p_org);
  6656. #endif
  6657. *p_org = WASM_OP_SET_GLOBAL_64;
  6658. }
  6659. else if (module->aux_stack_size > 0
  6660. && global_idx == module->aux_stack_top_global_index) {
  6661. #if WASM_ENABLE_DEBUG_INTERP != 0
  6662. record_fast_op(module, p_org, *p_org);
  6663. #endif
  6664. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  6665. }
  6666. #else /* else of WASM_ENABLE_FAST_INTERP */
  6667. if (global_type == VALUE_TYPE_I64
  6668. || global_type == VALUE_TYPE_F64) {
  6669. skip_label();
  6670. emit_label(WASM_OP_SET_GLOBAL_64);
  6671. }
  6672. else if (module->aux_stack_size > 0
  6673. && global_idx == module->aux_stack_top_global_index) {
  6674. skip_label();
  6675. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  6676. }
  6677. emit_uint32(loader_ctx, global_idx);
  6678. POP_OFFSET_TYPE(global_type);
  6679. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6680. break;
  6681. }
  6682. /* load */
  6683. case WASM_OP_I32_LOAD:
  6684. case WASM_OP_I32_LOAD8_S:
  6685. case WASM_OP_I32_LOAD8_U:
  6686. case WASM_OP_I32_LOAD16_S:
  6687. case WASM_OP_I32_LOAD16_U:
  6688. case WASM_OP_I64_LOAD:
  6689. case WASM_OP_I64_LOAD8_S:
  6690. case WASM_OP_I64_LOAD8_U:
  6691. case WASM_OP_I64_LOAD16_S:
  6692. case WASM_OP_I64_LOAD16_U:
  6693. case WASM_OP_I64_LOAD32_S:
  6694. case WASM_OP_I64_LOAD32_U:
  6695. case WASM_OP_F32_LOAD:
  6696. case WASM_OP_F64_LOAD:
  6697. /* store */
  6698. case WASM_OP_I32_STORE:
  6699. case WASM_OP_I32_STORE8:
  6700. case WASM_OP_I32_STORE16:
  6701. case WASM_OP_I64_STORE:
  6702. case WASM_OP_I64_STORE8:
  6703. case WASM_OP_I64_STORE16:
  6704. case WASM_OP_I64_STORE32:
  6705. case WASM_OP_F32_STORE:
  6706. case WASM_OP_F64_STORE:
  6707. {
  6708. #if WASM_ENABLE_FAST_INTERP != 0
  6709. /* change F32/F64 into I32/I64 */
  6710. if (opcode == WASM_OP_F32_LOAD) {
  6711. skip_label();
  6712. emit_label(WASM_OP_I32_LOAD);
  6713. }
  6714. else if (opcode == WASM_OP_F64_LOAD) {
  6715. skip_label();
  6716. emit_label(WASM_OP_I64_LOAD);
  6717. }
  6718. else if (opcode == WASM_OP_F32_STORE) {
  6719. skip_label();
  6720. emit_label(WASM_OP_I32_STORE);
  6721. }
  6722. else if (opcode == WASM_OP_F64_STORE) {
  6723. skip_label();
  6724. emit_label(WASM_OP_I64_STORE);
  6725. }
  6726. #endif
  6727. CHECK_MEMORY();
  6728. read_leb_uint32(p, p_end, align); /* align */
  6729. read_leb_uint32(p, p_end, mem_offset); /* offset */
  6730. if (!check_memory_access_align(opcode, align, error_buf,
  6731. error_buf_size)) {
  6732. goto fail;
  6733. }
  6734. #if WASM_ENABLE_FAST_INTERP != 0
  6735. emit_uint32(loader_ctx, mem_offset);
  6736. #endif
  6737. switch (opcode) {
  6738. /* load */
  6739. case WASM_OP_I32_LOAD:
  6740. case WASM_OP_I32_LOAD8_S:
  6741. case WASM_OP_I32_LOAD8_U:
  6742. case WASM_OP_I32_LOAD16_S:
  6743. case WASM_OP_I32_LOAD16_U:
  6744. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6745. break;
  6746. case WASM_OP_I64_LOAD:
  6747. case WASM_OP_I64_LOAD8_S:
  6748. case WASM_OP_I64_LOAD8_U:
  6749. case WASM_OP_I64_LOAD16_S:
  6750. case WASM_OP_I64_LOAD16_U:
  6751. case WASM_OP_I64_LOAD32_S:
  6752. case WASM_OP_I64_LOAD32_U:
  6753. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6754. break;
  6755. case WASM_OP_F32_LOAD:
  6756. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6757. break;
  6758. case WASM_OP_F64_LOAD:
  6759. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  6760. break;
  6761. /* store */
  6762. case WASM_OP_I32_STORE:
  6763. case WASM_OP_I32_STORE8:
  6764. case WASM_OP_I32_STORE16:
  6765. POP_I32();
  6766. POP_I32();
  6767. break;
  6768. case WASM_OP_I64_STORE:
  6769. case WASM_OP_I64_STORE8:
  6770. case WASM_OP_I64_STORE16:
  6771. case WASM_OP_I64_STORE32:
  6772. POP_I64();
  6773. POP_I32();
  6774. break;
  6775. case WASM_OP_F32_STORE:
  6776. POP_F32();
  6777. POP_I32();
  6778. break;
  6779. case WASM_OP_F64_STORE:
  6780. POP_F64();
  6781. POP_I32();
  6782. break;
  6783. default:
  6784. break;
  6785. }
  6786. break;
  6787. }
  6788. case WASM_OP_MEMORY_SIZE:
  6789. CHECK_MEMORY();
  6790. /* reserved byte 0x00 */
  6791. if (*p++ != 0x00) {
  6792. set_error_buf(error_buf, error_buf_size,
  6793. "zero byte expected");
  6794. goto fail;
  6795. }
  6796. PUSH_I32();
  6797. module->possible_memory_grow = true;
  6798. break;
  6799. case WASM_OP_MEMORY_GROW:
  6800. CHECK_MEMORY();
  6801. /* reserved byte 0x00 */
  6802. if (*p++ != 0x00) {
  6803. set_error_buf(error_buf, error_buf_size,
  6804. "zero byte expected");
  6805. goto fail;
  6806. }
  6807. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6808. func->has_op_memory_grow = true;
  6809. module->possible_memory_grow = true;
  6810. break;
  6811. case WASM_OP_I32_CONST:
  6812. read_leb_int32(p, p_end, i32_const);
  6813. #if WASM_ENABLE_FAST_INTERP != 0
  6814. skip_label();
  6815. disable_emit = true;
  6816. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  6817. #else
  6818. (void)i32_const;
  6819. #endif
  6820. PUSH_I32();
  6821. break;
  6822. case WASM_OP_I64_CONST:
  6823. read_leb_int64(p, p_end, i64);
  6824. #if WASM_ENABLE_FAST_INTERP != 0
  6825. skip_label();
  6826. disable_emit = true;
  6827. GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
  6828. #endif
  6829. PUSH_I64();
  6830. break;
  6831. case WASM_OP_F32_CONST:
  6832. p += sizeof(float32);
  6833. #if WASM_ENABLE_FAST_INTERP != 0
  6834. skip_label();
  6835. disable_emit = true;
  6836. bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org,
  6837. sizeof(float32));
  6838. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
  6839. #endif
  6840. PUSH_F32();
  6841. break;
  6842. case WASM_OP_F64_CONST:
  6843. p += sizeof(float64);
  6844. #if WASM_ENABLE_FAST_INTERP != 0
  6845. skip_label();
  6846. disable_emit = true;
  6847. /* Some MCU may require 8-byte align */
  6848. bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org,
  6849. sizeof(float64));
  6850. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
  6851. #endif
  6852. PUSH_F64();
  6853. break;
  6854. case WASM_OP_I32_EQZ:
  6855. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6856. break;
  6857. case WASM_OP_I32_EQ:
  6858. case WASM_OP_I32_NE:
  6859. case WASM_OP_I32_LT_S:
  6860. case WASM_OP_I32_LT_U:
  6861. case WASM_OP_I32_GT_S:
  6862. case WASM_OP_I32_GT_U:
  6863. case WASM_OP_I32_LE_S:
  6864. case WASM_OP_I32_LE_U:
  6865. case WASM_OP_I32_GE_S:
  6866. case WASM_OP_I32_GE_U:
  6867. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6868. break;
  6869. case WASM_OP_I64_EQZ:
  6870. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6871. break;
  6872. case WASM_OP_I64_EQ:
  6873. case WASM_OP_I64_NE:
  6874. case WASM_OP_I64_LT_S:
  6875. case WASM_OP_I64_LT_U:
  6876. case WASM_OP_I64_GT_S:
  6877. case WASM_OP_I64_GT_U:
  6878. case WASM_OP_I64_LE_S:
  6879. case WASM_OP_I64_LE_U:
  6880. case WASM_OP_I64_GE_S:
  6881. case WASM_OP_I64_GE_U:
  6882. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6883. break;
  6884. case WASM_OP_F32_EQ:
  6885. case WASM_OP_F32_NE:
  6886. case WASM_OP_F32_LT:
  6887. case WASM_OP_F32_GT:
  6888. case WASM_OP_F32_LE:
  6889. case WASM_OP_F32_GE:
  6890. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6891. break;
  6892. case WASM_OP_F64_EQ:
  6893. case WASM_OP_F64_NE:
  6894. case WASM_OP_F64_LT:
  6895. case WASM_OP_F64_GT:
  6896. case WASM_OP_F64_LE:
  6897. case WASM_OP_F64_GE:
  6898. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6899. break;
  6900. case WASM_OP_I32_CLZ:
  6901. case WASM_OP_I32_CTZ:
  6902. case WASM_OP_I32_POPCNT:
  6903. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6904. break;
  6905. case WASM_OP_I32_ADD:
  6906. case WASM_OP_I32_SUB:
  6907. case WASM_OP_I32_MUL:
  6908. case WASM_OP_I32_DIV_S:
  6909. case WASM_OP_I32_DIV_U:
  6910. case WASM_OP_I32_REM_S:
  6911. case WASM_OP_I32_REM_U:
  6912. case WASM_OP_I32_AND:
  6913. case WASM_OP_I32_OR:
  6914. case WASM_OP_I32_XOR:
  6915. case WASM_OP_I32_SHL:
  6916. case WASM_OP_I32_SHR_S:
  6917. case WASM_OP_I32_SHR_U:
  6918. case WASM_OP_I32_ROTL:
  6919. case WASM_OP_I32_ROTR:
  6920. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6921. break;
  6922. case WASM_OP_I64_CLZ:
  6923. case WASM_OP_I64_CTZ:
  6924. case WASM_OP_I64_POPCNT:
  6925. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6926. break;
  6927. case WASM_OP_I64_ADD:
  6928. case WASM_OP_I64_SUB:
  6929. case WASM_OP_I64_MUL:
  6930. case WASM_OP_I64_DIV_S:
  6931. case WASM_OP_I64_DIV_U:
  6932. case WASM_OP_I64_REM_S:
  6933. case WASM_OP_I64_REM_U:
  6934. case WASM_OP_I64_AND:
  6935. case WASM_OP_I64_OR:
  6936. case WASM_OP_I64_XOR:
  6937. case WASM_OP_I64_SHL:
  6938. case WASM_OP_I64_SHR_S:
  6939. case WASM_OP_I64_SHR_U:
  6940. case WASM_OP_I64_ROTL:
  6941. case WASM_OP_I64_ROTR:
  6942. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6943. break;
  6944. case WASM_OP_F32_ABS:
  6945. case WASM_OP_F32_NEG:
  6946. case WASM_OP_F32_CEIL:
  6947. case WASM_OP_F32_FLOOR:
  6948. case WASM_OP_F32_TRUNC:
  6949. case WASM_OP_F32_NEAREST:
  6950. case WASM_OP_F32_SQRT:
  6951. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6952. break;
  6953. case WASM_OP_F32_ADD:
  6954. case WASM_OP_F32_SUB:
  6955. case WASM_OP_F32_MUL:
  6956. case WASM_OP_F32_DIV:
  6957. case WASM_OP_F32_MIN:
  6958. case WASM_OP_F32_MAX:
  6959. case WASM_OP_F32_COPYSIGN:
  6960. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6961. break;
  6962. case WASM_OP_F64_ABS:
  6963. case WASM_OP_F64_NEG:
  6964. case WASM_OP_F64_CEIL:
  6965. case WASM_OP_F64_FLOOR:
  6966. case WASM_OP_F64_TRUNC:
  6967. case WASM_OP_F64_NEAREST:
  6968. case WASM_OP_F64_SQRT:
  6969. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6970. break;
  6971. case WASM_OP_F64_ADD:
  6972. case WASM_OP_F64_SUB:
  6973. case WASM_OP_F64_MUL:
  6974. case WASM_OP_F64_DIV:
  6975. case WASM_OP_F64_MIN:
  6976. case WASM_OP_F64_MAX:
  6977. case WASM_OP_F64_COPYSIGN:
  6978. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6979. break;
  6980. case WASM_OP_I32_WRAP_I64:
  6981. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6982. break;
  6983. case WASM_OP_I32_TRUNC_S_F32:
  6984. case WASM_OP_I32_TRUNC_U_F32:
  6985. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6986. break;
  6987. case WASM_OP_I32_TRUNC_S_F64:
  6988. case WASM_OP_I32_TRUNC_U_F64:
  6989. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6990. break;
  6991. case WASM_OP_I64_EXTEND_S_I32:
  6992. case WASM_OP_I64_EXTEND_U_I32:
  6993. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6994. break;
  6995. case WASM_OP_I64_TRUNC_S_F32:
  6996. case WASM_OP_I64_TRUNC_U_F32:
  6997. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6998. break;
  6999. case WASM_OP_I64_TRUNC_S_F64:
  7000. case WASM_OP_I64_TRUNC_U_F64:
  7001. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7002. break;
  7003. case WASM_OP_F32_CONVERT_S_I32:
  7004. case WASM_OP_F32_CONVERT_U_I32:
  7005. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7006. break;
  7007. case WASM_OP_F32_CONVERT_S_I64:
  7008. case WASM_OP_F32_CONVERT_U_I64:
  7009. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  7010. break;
  7011. case WASM_OP_F32_DEMOTE_F64:
  7012. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  7013. break;
  7014. case WASM_OP_F64_CONVERT_S_I32:
  7015. case WASM_OP_F64_CONVERT_U_I32:
  7016. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7017. break;
  7018. case WASM_OP_F64_CONVERT_S_I64:
  7019. case WASM_OP_F64_CONVERT_U_I64:
  7020. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7021. break;
  7022. case WASM_OP_F64_PROMOTE_F32:
  7023. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  7024. break;
  7025. case WASM_OP_I32_REINTERPRET_F32:
  7026. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7027. break;
  7028. case WASM_OP_I64_REINTERPRET_F64:
  7029. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7030. break;
  7031. case WASM_OP_F32_REINTERPRET_I32:
  7032. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7033. break;
  7034. case WASM_OP_F64_REINTERPRET_I64:
  7035. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7036. break;
  7037. case WASM_OP_I32_EXTEND8_S:
  7038. case WASM_OP_I32_EXTEND16_S:
  7039. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7040. break;
  7041. case WASM_OP_I64_EXTEND8_S:
  7042. case WASM_OP_I64_EXTEND16_S:
  7043. case WASM_OP_I64_EXTEND32_S:
  7044. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7045. break;
  7046. case WASM_OP_MISC_PREFIX:
  7047. {
  7048. uint32 opcode1;
  7049. read_leb_uint32(p, p_end, opcode1);
  7050. #if WASM_ENABLE_FAST_INTERP != 0
  7051. emit_byte(loader_ctx, ((uint8)opcode1));
  7052. #endif
  7053. switch (opcode1) {
  7054. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7055. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7056. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7057. break;
  7058. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7059. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7060. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7061. break;
  7062. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7063. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7064. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7065. break;
  7066. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7067. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7068. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7069. break;
  7070. #if WASM_ENABLE_BULK_MEMORY != 0
  7071. case WASM_OP_MEMORY_INIT:
  7072. {
  7073. read_leb_uint32(p, p_end, data_seg_idx);
  7074. #if WASM_ENABLE_FAST_INTERP != 0
  7075. emit_uint32(loader_ctx, data_seg_idx);
  7076. #endif
  7077. if (module->import_memory_count == 0
  7078. && module->memory_count == 0)
  7079. goto fail_unknown_memory;
  7080. if (*p++ != 0x00)
  7081. goto fail_zero_byte_expected;
  7082. if (data_seg_idx >= module->data_seg_count) {
  7083. set_error_buf_v(error_buf, error_buf_size,
  7084. "unknown data segment %d",
  7085. data_seg_idx);
  7086. goto fail;
  7087. }
  7088. if (module->data_seg_count1 == 0)
  7089. goto fail_data_cnt_sec_require;
  7090. POP_I32();
  7091. POP_I32();
  7092. POP_I32();
  7093. break;
  7094. }
  7095. case WASM_OP_DATA_DROP:
  7096. {
  7097. read_leb_uint32(p, p_end, data_seg_idx);
  7098. #if WASM_ENABLE_FAST_INTERP != 0
  7099. emit_uint32(loader_ctx, data_seg_idx);
  7100. #endif
  7101. if (data_seg_idx >= module->data_seg_count) {
  7102. set_error_buf(error_buf, error_buf_size,
  7103. "unknown data segment");
  7104. goto fail;
  7105. }
  7106. if (module->data_seg_count1 == 0)
  7107. goto fail_data_cnt_sec_require;
  7108. break;
  7109. }
  7110. case WASM_OP_MEMORY_COPY:
  7111. {
  7112. /* both src and dst memory index should be 0 */
  7113. if (*(int16 *)p != 0x0000)
  7114. goto fail_zero_byte_expected;
  7115. p += 2;
  7116. if (module->import_memory_count == 0
  7117. && module->memory_count == 0)
  7118. goto fail_unknown_memory;
  7119. POP_I32();
  7120. POP_I32();
  7121. POP_I32();
  7122. break;
  7123. }
  7124. case WASM_OP_MEMORY_FILL:
  7125. {
  7126. if (*p++ != 0x00) {
  7127. goto fail_zero_byte_expected;
  7128. }
  7129. if (module->import_memory_count == 0
  7130. && module->memory_count == 0) {
  7131. goto fail_unknown_memory;
  7132. }
  7133. POP_I32();
  7134. POP_I32();
  7135. POP_I32();
  7136. break;
  7137. fail_zero_byte_expected:
  7138. set_error_buf(error_buf, error_buf_size,
  7139. "zero byte expected");
  7140. goto fail;
  7141. fail_unknown_memory:
  7142. set_error_buf(error_buf, error_buf_size,
  7143. "unknown memory 0");
  7144. goto fail;
  7145. fail_data_cnt_sec_require:
  7146. set_error_buf(error_buf, error_buf_size,
  7147. "data count section required");
  7148. goto fail;
  7149. }
  7150. #endif /* WASM_ENABLE_BULK_MEMORY */
  7151. #if WASM_ENABLE_REF_TYPES != 0
  7152. case WASM_OP_TABLE_INIT:
  7153. {
  7154. uint8 seg_ref_type = 0, tbl_ref_type = 0;
  7155. read_leb_uint32(p, p_end, table_seg_idx);
  7156. read_leb_uint32(p, p_end, table_idx);
  7157. if (!get_table_elem_type(module, table_idx,
  7158. &tbl_ref_type, error_buf,
  7159. error_buf_size))
  7160. goto fail;
  7161. if (!get_table_seg_elem_type(module, table_seg_idx,
  7162. &seg_ref_type, error_buf,
  7163. error_buf_size))
  7164. goto fail;
  7165. if (seg_ref_type != tbl_ref_type) {
  7166. set_error_buf(error_buf, error_buf_size,
  7167. "type mismatch");
  7168. goto fail;
  7169. }
  7170. #if WASM_ENABLE_FAST_INTERP != 0
  7171. emit_uint32(loader_ctx, table_seg_idx);
  7172. emit_uint32(loader_ctx, table_idx);
  7173. #endif
  7174. POP_I32();
  7175. POP_I32();
  7176. POP_I32();
  7177. break;
  7178. }
  7179. case WASM_OP_ELEM_DROP:
  7180. {
  7181. read_leb_uint32(p, p_end, table_seg_idx);
  7182. if (!get_table_seg_elem_type(module, table_seg_idx,
  7183. NULL, error_buf,
  7184. error_buf_size))
  7185. goto fail;
  7186. #if WASM_ENABLE_FAST_INTERP != 0
  7187. emit_uint32(loader_ctx, table_seg_idx);
  7188. #endif
  7189. break;
  7190. }
  7191. case WASM_OP_TABLE_COPY:
  7192. {
  7193. uint8 src_ref_type, dst_ref_type;
  7194. uint32 src_tbl_idx, dst_tbl_idx;
  7195. read_leb_uint32(p, p_end, src_tbl_idx);
  7196. if (!get_table_elem_type(module, src_tbl_idx,
  7197. &src_ref_type, error_buf,
  7198. error_buf_size))
  7199. goto fail;
  7200. read_leb_uint32(p, p_end, dst_tbl_idx);
  7201. if (!get_table_elem_type(module, dst_tbl_idx,
  7202. &dst_ref_type, error_buf,
  7203. error_buf_size))
  7204. goto fail;
  7205. if (src_ref_type != dst_ref_type) {
  7206. set_error_buf(error_buf, error_buf_size,
  7207. "type mismatch");
  7208. goto fail;
  7209. }
  7210. #if WASM_ENABLE_FAST_INTERP != 0
  7211. emit_uint32(loader_ctx, src_tbl_idx);
  7212. emit_uint32(loader_ctx, dst_tbl_idx);
  7213. #endif
  7214. POP_I32();
  7215. POP_I32();
  7216. POP_I32();
  7217. break;
  7218. }
  7219. case WASM_OP_TABLE_SIZE:
  7220. {
  7221. read_leb_uint32(p, p_end, table_idx);
  7222. /* TODO: shall we create a new function to check
  7223. table idx instead of using below function? */
  7224. if (!get_table_elem_type(module, table_idx, NULL,
  7225. error_buf, error_buf_size))
  7226. goto fail;
  7227. #if WASM_ENABLE_FAST_INTERP != 0
  7228. emit_uint32(loader_ctx, table_idx);
  7229. #endif
  7230. PUSH_I32();
  7231. break;
  7232. }
  7233. case WASM_OP_TABLE_GROW:
  7234. case WASM_OP_TABLE_FILL:
  7235. {
  7236. uint8 decl_ref_type;
  7237. read_leb_uint32(p, p_end, table_idx);
  7238. if (!get_table_elem_type(module, table_idx,
  7239. &decl_ref_type, error_buf,
  7240. error_buf_size))
  7241. goto fail;
  7242. if (opcode1 == WASM_OP_TABLE_GROW) {
  7243. if (table_idx < module->import_table_count) {
  7244. module->import_tables[table_idx]
  7245. .u.table.possible_grow = true;
  7246. }
  7247. else {
  7248. module
  7249. ->tables[table_idx
  7250. - module->import_table_count]
  7251. .possible_grow = true;
  7252. }
  7253. }
  7254. #if WASM_ENABLE_FAST_INTERP != 0
  7255. emit_uint32(loader_ctx, table_idx);
  7256. #endif
  7257. POP_I32();
  7258. #if WASM_ENABLE_FAST_INTERP != 0
  7259. POP_OFFSET_TYPE(decl_ref_type);
  7260. #endif
  7261. POP_TYPE(decl_ref_type);
  7262. if (opcode1 == WASM_OP_TABLE_GROW)
  7263. PUSH_I32();
  7264. else
  7265. POP_I32();
  7266. break;
  7267. }
  7268. #endif /* WASM_ENABLE_REF_TYPES */
  7269. default:
  7270. set_error_buf_v(error_buf, error_buf_size,
  7271. "%s %02x %02x", "unsupported opcode",
  7272. 0xfc, opcode1);
  7273. goto fail;
  7274. }
  7275. break;
  7276. }
  7277. #if WASM_ENABLE_SIMD != 0
  7278. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7279. case WASM_OP_SIMD_PREFIX:
  7280. {
  7281. opcode = read_uint8(p);
  7282. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7283. */
  7284. switch (opcode) {
  7285. /* memory instruction */
  7286. case SIMD_v128_load:
  7287. case SIMD_v128_load8x8_s:
  7288. case SIMD_v128_load8x8_u:
  7289. case SIMD_v128_load16x4_s:
  7290. case SIMD_v128_load16x4_u:
  7291. case SIMD_v128_load32x2_s:
  7292. case SIMD_v128_load32x2_u:
  7293. case SIMD_v128_load8_splat:
  7294. case SIMD_v128_load16_splat:
  7295. case SIMD_v128_load32_splat:
  7296. case SIMD_v128_load64_splat:
  7297. {
  7298. CHECK_MEMORY();
  7299. read_leb_uint32(p, p_end, align); /* align */
  7300. if (!check_simd_memory_access_align(
  7301. opcode, align, error_buf, error_buf_size)) {
  7302. goto fail;
  7303. }
  7304. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7305. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  7306. break;
  7307. }
  7308. case SIMD_v128_store:
  7309. {
  7310. CHECK_MEMORY();
  7311. read_leb_uint32(p, p_end, align); /* align */
  7312. if (!check_simd_memory_access_align(
  7313. opcode, align, error_buf, error_buf_size)) {
  7314. goto fail;
  7315. }
  7316. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7317. POP_V128();
  7318. POP_I32();
  7319. break;
  7320. }
  7321. /* basic operation */
  7322. case SIMD_v128_const:
  7323. {
  7324. CHECK_BUF1(p, p_end, 16);
  7325. p += 16;
  7326. PUSH_V128();
  7327. break;
  7328. }
  7329. case SIMD_v8x16_shuffle:
  7330. {
  7331. V128 mask;
  7332. CHECK_BUF1(p, p_end, 16);
  7333. mask = read_i8x16(p, error_buf, error_buf_size);
  7334. p += 16;
  7335. if (!check_simd_shuffle_mask(mask, error_buf,
  7336. error_buf_size)) {
  7337. goto fail;
  7338. }
  7339. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7340. break;
  7341. }
  7342. case SIMD_v8x16_swizzle:
  7343. {
  7344. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7345. break;
  7346. }
  7347. /* splat operation */
  7348. case SIMD_i8x16_splat:
  7349. case SIMD_i16x8_splat:
  7350. case SIMD_i32x4_splat:
  7351. case SIMD_i64x2_splat:
  7352. case SIMD_f32x4_splat:
  7353. case SIMD_f64x2_splat:
  7354. {
  7355. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  7356. VALUE_TYPE_I32, VALUE_TYPE_I64,
  7357. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  7358. POP_AND_PUSH(pop_type[opcode - SIMD_i8x16_splat],
  7359. VALUE_TYPE_V128);
  7360. break;
  7361. }
  7362. /* lane operation */
  7363. case SIMD_i8x16_extract_lane_s:
  7364. case SIMD_i8x16_extract_lane_u:
  7365. case SIMD_i8x16_replace_lane:
  7366. case SIMD_i16x8_extract_lane_s:
  7367. case SIMD_i16x8_extract_lane_u:
  7368. case SIMD_i16x8_replace_lane:
  7369. case SIMD_i32x4_extract_lane:
  7370. case SIMD_i32x4_replace_lane:
  7371. case SIMD_i64x2_extract_lane:
  7372. case SIMD_i64x2_replace_lane:
  7373. case SIMD_f32x4_extract_lane:
  7374. case SIMD_f32x4_replace_lane:
  7375. case SIMD_f64x2_extract_lane:
  7376. case SIMD_f64x2_replace_lane:
  7377. {
  7378. uint8 lane;
  7379. /* clang-format off */
  7380. uint8 replace[] = {
  7381. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  7382. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  7383. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  7384. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  7385. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  7386. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  7387. };
  7388. uint8 push_type[] = {
  7389. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  7390. VALUE_TYPE_V128,
  7391. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  7392. VALUE_TYPE_V128,
  7393. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  7394. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  7395. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  7396. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  7397. };
  7398. /* clang-format on */
  7399. CHECK_BUF(p, p_end, 1);
  7400. lane = read_uint8(p);
  7401. if (!check_simd_access_lane(opcode, lane, error_buf,
  7402. error_buf_size)) {
  7403. goto fail;
  7404. }
  7405. if (replace[opcode - SIMD_i8x16_extract_lane_s]) {
  7406. if (!(wasm_loader_pop_frame_ref(
  7407. loader_ctx,
  7408. replace[opcode - SIMD_i8x16_extract_lane_s],
  7409. error_buf, error_buf_size)))
  7410. goto fail;
  7411. }
  7412. POP_AND_PUSH(
  7413. VALUE_TYPE_V128,
  7414. push_type[opcode - SIMD_i8x16_extract_lane_s]);
  7415. break;
  7416. }
  7417. /* i8x16 compare operation */
  7418. case SIMD_i8x16_eq:
  7419. case SIMD_i8x16_ne:
  7420. case SIMD_i8x16_lt_s:
  7421. case SIMD_i8x16_lt_u:
  7422. case SIMD_i8x16_gt_s:
  7423. case SIMD_i8x16_gt_u:
  7424. case SIMD_i8x16_le_s:
  7425. case SIMD_i8x16_le_u:
  7426. case SIMD_i8x16_ge_s:
  7427. case SIMD_i8x16_ge_u:
  7428. /* i16x8 compare operation */
  7429. case SIMD_i16x8_eq:
  7430. case SIMD_i16x8_ne:
  7431. case SIMD_i16x8_lt_s:
  7432. case SIMD_i16x8_lt_u:
  7433. case SIMD_i16x8_gt_s:
  7434. case SIMD_i16x8_gt_u:
  7435. case SIMD_i16x8_le_s:
  7436. case SIMD_i16x8_le_u:
  7437. case SIMD_i16x8_ge_s:
  7438. case SIMD_i16x8_ge_u:
  7439. /* i32x4 compare operation */
  7440. case SIMD_i32x4_eq:
  7441. case SIMD_i32x4_ne:
  7442. case SIMD_i32x4_lt_s:
  7443. case SIMD_i32x4_lt_u:
  7444. case SIMD_i32x4_gt_s:
  7445. case SIMD_i32x4_gt_u:
  7446. case SIMD_i32x4_le_s:
  7447. case SIMD_i32x4_le_u:
  7448. case SIMD_i32x4_ge_s:
  7449. case SIMD_i32x4_ge_u:
  7450. /* f32x4 compare operation */
  7451. case SIMD_f32x4_eq:
  7452. case SIMD_f32x4_ne:
  7453. case SIMD_f32x4_lt:
  7454. case SIMD_f32x4_gt:
  7455. case SIMD_f32x4_le:
  7456. case SIMD_f32x4_ge:
  7457. /* f64x2 compare operation */
  7458. case SIMD_f64x2_eq:
  7459. case SIMD_f64x2_ne:
  7460. case SIMD_f64x2_lt:
  7461. case SIMD_f64x2_gt:
  7462. case SIMD_f64x2_le:
  7463. case SIMD_f64x2_ge:
  7464. {
  7465. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7466. break;
  7467. }
  7468. /* v128 operation */
  7469. case SIMD_v128_not:
  7470. {
  7471. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7472. break;
  7473. }
  7474. case SIMD_v128_and:
  7475. case SIMD_v128_andnot:
  7476. case SIMD_v128_or:
  7477. case SIMD_v128_xor:
  7478. {
  7479. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7480. break;
  7481. }
  7482. case SIMD_v128_bitselect:
  7483. {
  7484. POP_V128();
  7485. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7486. break;
  7487. }
  7488. case SIMD_v128_any_true:
  7489. {
  7490. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7491. break;
  7492. }
  7493. /* Load Lane Operation */
  7494. case SIMD_v128_load8_lane:
  7495. case SIMD_v128_load16_lane:
  7496. case SIMD_v128_load32_lane:
  7497. case SIMD_v128_load64_lane:
  7498. case SIMD_v128_store8_lane:
  7499. case SIMD_v128_store16_lane:
  7500. case SIMD_v128_store32_lane:
  7501. case SIMD_v128_store64_lane:
  7502. {
  7503. uint8 lane;
  7504. CHECK_MEMORY();
  7505. read_leb_uint32(p, p_end, align); /* align */
  7506. if (!check_simd_memory_access_align(
  7507. opcode, align, error_buf, error_buf_size)) {
  7508. goto fail;
  7509. }
  7510. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7511. CHECK_BUF(p, p_end, 1);
  7512. lane = read_uint8(p);
  7513. if (!check_simd_access_lane(opcode, lane, error_buf,
  7514. error_buf_size)) {
  7515. goto fail;
  7516. }
  7517. POP_V128();
  7518. POP_I32();
  7519. if (opcode < SIMD_v128_store8_lane) {
  7520. PUSH_V128();
  7521. }
  7522. break;
  7523. }
  7524. case SIMD_v128_load32_zero:
  7525. case SIMD_v128_load64_zero:
  7526. {
  7527. CHECK_MEMORY();
  7528. read_leb_uint32(p, p_end, align); /* align */
  7529. if (!check_simd_memory_access_align(
  7530. opcode, align, error_buf, error_buf_size)) {
  7531. goto fail;
  7532. }
  7533. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7534. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  7535. break;
  7536. }
  7537. /* Float conversion */
  7538. case SIMD_f32x4_demote_f64x2_zero:
  7539. case SIMD_f64x2_promote_low_f32x4_zero:
  7540. {
  7541. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7542. break;
  7543. }
  7544. /* i8x16 Operation */
  7545. case SIMD_i8x16_abs:
  7546. case SIMD_i8x16_neg:
  7547. case SIMD_i8x16_popcnt:
  7548. {
  7549. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7550. break;
  7551. }
  7552. case SIMD_i8x16_all_true:
  7553. case SIMD_i8x16_bitmask:
  7554. {
  7555. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7556. break;
  7557. }
  7558. case SIMD_i8x16_narrow_i16x8_s:
  7559. case SIMD_i8x16_narrow_i16x8_u:
  7560. {
  7561. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7562. break;
  7563. }
  7564. case SIMD_f32x4_ceil:
  7565. case SIMD_f32x4_floor:
  7566. case SIMD_f32x4_trunc:
  7567. case SIMD_f32x4_nearest:
  7568. {
  7569. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7570. break;
  7571. }
  7572. case SIMD_i8x16_shl:
  7573. case SIMD_i8x16_shr_s:
  7574. case SIMD_i8x16_shr_u:
  7575. {
  7576. POP_I32();
  7577. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7578. break;
  7579. }
  7580. case SIMD_i8x16_add:
  7581. case SIMD_i8x16_add_sat_s:
  7582. case SIMD_i8x16_add_sat_u:
  7583. case SIMD_i8x16_sub:
  7584. case SIMD_i8x16_sub_sat_s:
  7585. case SIMD_i8x16_sub_sat_u:
  7586. {
  7587. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7588. break;
  7589. }
  7590. case SIMD_f64x2_ceil:
  7591. case SIMD_f64x2_floor:
  7592. {
  7593. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7594. break;
  7595. }
  7596. case SIMD_i8x16_min_s:
  7597. case SIMD_i8x16_min_u:
  7598. case SIMD_i8x16_max_s:
  7599. case SIMD_i8x16_max_u:
  7600. {
  7601. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7602. break;
  7603. }
  7604. case SIMD_f64x2_trunc:
  7605. {
  7606. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7607. break;
  7608. }
  7609. case SIMD_i8x16_avgr_u:
  7610. {
  7611. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7612. break;
  7613. }
  7614. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  7615. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  7616. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  7617. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  7618. /* i16x8 operation */
  7619. case SIMD_i16x8_abs:
  7620. case SIMD_i16x8_neg:
  7621. {
  7622. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7623. break;
  7624. }
  7625. case SIMD_i16x8_q15mulr_sat_s:
  7626. {
  7627. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7628. break;
  7629. }
  7630. case SIMD_i16x8_all_true:
  7631. case SIMD_i16x8_bitmask:
  7632. {
  7633. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7634. break;
  7635. }
  7636. case SIMD_i16x8_narrow_i32x4_s:
  7637. case SIMD_i16x8_narrow_i32x4_u:
  7638. {
  7639. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7640. break;
  7641. }
  7642. case SIMD_i16x8_extend_low_i8x16_s:
  7643. case SIMD_i16x8_extend_high_i8x16_s:
  7644. case SIMD_i16x8_extend_low_i8x16_u:
  7645. case SIMD_i16x8_extend_high_i8x16_u:
  7646. {
  7647. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7648. break;
  7649. }
  7650. case SIMD_i16x8_shl:
  7651. case SIMD_i16x8_shr_s:
  7652. case SIMD_i16x8_shr_u:
  7653. {
  7654. POP_I32();
  7655. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7656. break;
  7657. }
  7658. case SIMD_i16x8_add:
  7659. case SIMD_i16x8_add_sat_s:
  7660. case SIMD_i16x8_add_sat_u:
  7661. case SIMD_i16x8_sub:
  7662. case SIMD_i16x8_sub_sat_s:
  7663. case SIMD_i16x8_sub_sat_u:
  7664. {
  7665. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7666. break;
  7667. }
  7668. case SIMD_f64x2_nearest:
  7669. {
  7670. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7671. break;
  7672. }
  7673. case SIMD_i16x8_mul:
  7674. case SIMD_i16x8_min_s:
  7675. case SIMD_i16x8_min_u:
  7676. case SIMD_i16x8_max_s:
  7677. case SIMD_i16x8_max_u:
  7678. case SIMD_i16x8_avgr_u:
  7679. case SIMD_i16x8_extmul_low_i8x16_s:
  7680. case SIMD_i16x8_extmul_high_i8x16_s:
  7681. case SIMD_i16x8_extmul_low_i8x16_u:
  7682. case SIMD_i16x8_extmul_high_i8x16_u:
  7683. {
  7684. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7685. break;
  7686. }
  7687. /* i32x4 operation */
  7688. case SIMD_i32x4_abs:
  7689. case SIMD_i32x4_neg:
  7690. {
  7691. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7692. break;
  7693. }
  7694. case SIMD_i32x4_all_true:
  7695. case SIMD_i32x4_bitmask:
  7696. {
  7697. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7698. break;
  7699. }
  7700. case SIMD_i32x4_narrow_i64x2_s:
  7701. case SIMD_i32x4_narrow_i64x2_u:
  7702. {
  7703. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7704. break;
  7705. }
  7706. case SIMD_i32x4_extend_low_i16x8_s:
  7707. case SIMD_i32x4_extend_high_i16x8_s:
  7708. case SIMD_i32x4_extend_low_i16x8_u:
  7709. case SIMD_i32x4_extend_high_i16x8_u:
  7710. {
  7711. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7712. break;
  7713. }
  7714. case SIMD_i32x4_shl:
  7715. case SIMD_i32x4_shr_s:
  7716. case SIMD_i32x4_shr_u:
  7717. {
  7718. POP_I32();
  7719. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7720. break;
  7721. }
  7722. case SIMD_i32x4_add:
  7723. case SIMD_i32x4_sub:
  7724. case SIMD_i32x4_mul:
  7725. case SIMD_i32x4_min_s:
  7726. case SIMD_i32x4_min_u:
  7727. case SIMD_i32x4_max_s:
  7728. case SIMD_i32x4_max_u:
  7729. case SIMD_i32x4_dot_i16x8_s:
  7730. case SIMD_i32x4_avgr_u:
  7731. case SIMD_i32x4_extmul_low_i16x8_s:
  7732. case SIMD_i32x4_extmul_high_i16x8_s:
  7733. case SIMD_i32x4_extmul_low_i16x8_u:
  7734. case SIMD_i32x4_extmul_high_i16x8_u:
  7735. {
  7736. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7737. break;
  7738. }
  7739. /* i64x2 operation */
  7740. case SIMD_i64x2_abs:
  7741. case SIMD_i64x2_neg:
  7742. {
  7743. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7744. break;
  7745. }
  7746. case SIMD_i64x2_all_true:
  7747. case SIMD_i64x2_bitmask:
  7748. {
  7749. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7750. break;
  7751. }
  7752. case SIMD_i64x2_extend_low_i32x4_s:
  7753. case SIMD_i64x2_extend_high_i32x4_s:
  7754. case SIMD_i64x2_extend_low_i32x4_u:
  7755. case SIMD_i64x2_extend_high_i32x4_u:
  7756. {
  7757. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7758. break;
  7759. }
  7760. case SIMD_i64x2_shl:
  7761. case SIMD_i64x2_shr_s:
  7762. case SIMD_i64x2_shr_u:
  7763. {
  7764. POP_I32();
  7765. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7766. break;
  7767. }
  7768. case SIMD_i64x2_add:
  7769. case SIMD_i64x2_sub:
  7770. case SIMD_i64x2_mul:
  7771. case SIMD_i64x2_eq:
  7772. case SIMD_i64x2_ne:
  7773. case SIMD_i64x2_lt_s:
  7774. case SIMD_i64x2_gt_s:
  7775. case SIMD_i64x2_le_s:
  7776. case SIMD_i64x2_ge_s:
  7777. case SIMD_i64x2_extmul_low_i32x4_s:
  7778. case SIMD_i64x2_extmul_high_i32x4_s:
  7779. case SIMD_i64x2_extmul_low_i32x4_u:
  7780. case SIMD_i64x2_extmul_high_i32x4_u:
  7781. {
  7782. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7783. break;
  7784. }
  7785. /* f32x4 operation */
  7786. case SIMD_f32x4_abs:
  7787. case SIMD_f32x4_neg:
  7788. case SIMD_f32x4_round:
  7789. case SIMD_f32x4_sqrt:
  7790. {
  7791. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7792. break;
  7793. }
  7794. case SIMD_f32x4_add:
  7795. case SIMD_f32x4_sub:
  7796. case SIMD_f32x4_mul:
  7797. case SIMD_f32x4_div:
  7798. case SIMD_f32x4_min:
  7799. case SIMD_f32x4_max:
  7800. case SIMD_f32x4_pmin:
  7801. case SIMD_f32x4_pmax:
  7802. {
  7803. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7804. break;
  7805. }
  7806. /* f64x2 operation */
  7807. case SIMD_f64x2_abs:
  7808. case SIMD_f64x2_neg:
  7809. case SIMD_f64x2_round:
  7810. case SIMD_f64x2_sqrt:
  7811. {
  7812. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7813. break;
  7814. }
  7815. case SIMD_f64x2_add:
  7816. case SIMD_f64x2_sub:
  7817. case SIMD_f64x2_mul:
  7818. case SIMD_f64x2_div:
  7819. case SIMD_f64x2_min:
  7820. case SIMD_f64x2_max:
  7821. case SIMD_f64x2_pmin:
  7822. case SIMD_f64x2_pmax:
  7823. {
  7824. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7825. break;
  7826. }
  7827. case SIMD_i32x4_trunc_sat_f32x4_s:
  7828. case SIMD_i32x4_trunc_sat_f32x4_u:
  7829. case SIMD_f32x4_convert_i32x4_s:
  7830. case SIMD_f32x4_convert_i32x4_u:
  7831. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  7832. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  7833. case SIMD_f64x2_convert_low_i32x4_s:
  7834. case SIMD_f64x2_convert_low_i32x4_u:
  7835. {
  7836. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7837. break;
  7838. }
  7839. default:
  7840. {
  7841. if (error_buf != NULL) {
  7842. snprintf(error_buf, error_buf_size,
  7843. "WASM module load failed: "
  7844. "invalid opcode 0xfd %02x.",
  7845. opcode);
  7846. }
  7847. goto fail;
  7848. }
  7849. }
  7850. break;
  7851. }
  7852. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7853. #endif /* end of WASM_ENABLE_SIMD */
  7854. #if WASM_ENABLE_SHARED_MEMORY != 0
  7855. case WASM_OP_ATOMIC_PREFIX:
  7856. {
  7857. opcode = read_uint8(p);
  7858. #if WASM_ENABLE_FAST_INTERP != 0
  7859. emit_byte(loader_ctx, opcode);
  7860. #endif
  7861. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7862. CHECK_MEMORY();
  7863. read_leb_uint32(p, p_end, align); /* align */
  7864. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7865. if (!check_memory_align_equal(opcode, align, error_buf,
  7866. error_buf_size)) {
  7867. goto fail;
  7868. }
  7869. #if WASM_ENABLE_FAST_INTERP != 0
  7870. emit_uint32(loader_ctx, mem_offset);
  7871. #endif
  7872. }
  7873. switch (opcode) {
  7874. case WASM_OP_ATOMIC_NOTIFY:
  7875. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7876. break;
  7877. case WASM_OP_ATOMIC_WAIT32:
  7878. POP_I64();
  7879. POP_I32();
  7880. POP_I32();
  7881. PUSH_I32();
  7882. break;
  7883. case WASM_OP_ATOMIC_WAIT64:
  7884. POP_I64();
  7885. POP_I64();
  7886. POP_I32();
  7887. PUSH_I32();
  7888. break;
  7889. case WASM_OP_ATOMIC_FENCE:
  7890. /* reserved byte 0x00 */
  7891. if (*p++ != 0x00) {
  7892. set_error_buf(error_buf, error_buf_size,
  7893. "zero byte expected");
  7894. goto fail;
  7895. }
  7896. break;
  7897. case WASM_OP_ATOMIC_I32_LOAD:
  7898. case WASM_OP_ATOMIC_I32_LOAD8_U:
  7899. case WASM_OP_ATOMIC_I32_LOAD16_U:
  7900. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7901. break;
  7902. case WASM_OP_ATOMIC_I32_STORE:
  7903. case WASM_OP_ATOMIC_I32_STORE8:
  7904. case WASM_OP_ATOMIC_I32_STORE16:
  7905. POP_I32();
  7906. POP_I32();
  7907. break;
  7908. case WASM_OP_ATOMIC_I64_LOAD:
  7909. case WASM_OP_ATOMIC_I64_LOAD8_U:
  7910. case WASM_OP_ATOMIC_I64_LOAD16_U:
  7911. case WASM_OP_ATOMIC_I64_LOAD32_U:
  7912. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7913. break;
  7914. case WASM_OP_ATOMIC_I64_STORE:
  7915. case WASM_OP_ATOMIC_I64_STORE8:
  7916. case WASM_OP_ATOMIC_I64_STORE16:
  7917. case WASM_OP_ATOMIC_I64_STORE32:
  7918. POP_I64();
  7919. POP_I32();
  7920. break;
  7921. case WASM_OP_ATOMIC_RMW_I32_ADD:
  7922. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  7923. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  7924. case WASM_OP_ATOMIC_RMW_I32_SUB:
  7925. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  7926. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  7927. case WASM_OP_ATOMIC_RMW_I32_AND:
  7928. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  7929. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  7930. case WASM_OP_ATOMIC_RMW_I32_OR:
  7931. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  7932. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  7933. case WASM_OP_ATOMIC_RMW_I32_XOR:
  7934. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  7935. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  7936. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  7937. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  7938. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  7939. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7940. break;
  7941. case WASM_OP_ATOMIC_RMW_I64_ADD:
  7942. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  7943. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  7944. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  7945. case WASM_OP_ATOMIC_RMW_I64_SUB:
  7946. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  7947. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  7948. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  7949. case WASM_OP_ATOMIC_RMW_I64_AND:
  7950. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  7951. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  7952. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  7953. case WASM_OP_ATOMIC_RMW_I64_OR:
  7954. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  7955. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  7956. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  7957. case WASM_OP_ATOMIC_RMW_I64_XOR:
  7958. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  7959. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  7960. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  7961. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  7962. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  7963. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  7964. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  7965. POP_I64();
  7966. POP_I32();
  7967. PUSH_I64();
  7968. break;
  7969. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  7970. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  7971. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  7972. POP_I32();
  7973. POP_I32();
  7974. POP_I32();
  7975. PUSH_I32();
  7976. break;
  7977. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  7978. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  7979. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  7980. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  7981. POP_I64();
  7982. POP_I64();
  7983. POP_I32();
  7984. PUSH_I64();
  7985. break;
  7986. default:
  7987. set_error_buf_v(error_buf, error_buf_size,
  7988. "%s %02x %02x", "unsupported opcode",
  7989. 0xfe, opcode);
  7990. goto fail;
  7991. }
  7992. break;
  7993. }
  7994. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7995. default:
  7996. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  7997. "unsupported opcode", opcode);
  7998. goto fail;
  7999. }
  8000. #if WASM_ENABLE_FAST_INTERP != 0
  8001. last_op = opcode;
  8002. #endif
  8003. }
  8004. if (loader_ctx->csp_num > 0) {
  8005. set_error_buf(error_buf, error_buf_size,
  8006. "function body must end with END opcode");
  8007. goto fail;
  8008. }
  8009. #if WASM_ENABLE_FAST_INTERP != 0
  8010. if (loader_ctx->p_code_compiled == NULL)
  8011. goto re_scan;
  8012. func->const_cell_num = loader_ctx->const_cell_num;
  8013. if (func->const_cell_num > 0) {
  8014. if (!(func->consts = func_const = loader_malloc(
  8015. func->const_cell_num * 4, error_buf, error_buf_size)))
  8016. goto fail;
  8017. func_const_end = func->consts + func->const_cell_num * 4;
  8018. /* reverse the const buf */
  8019. for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
  8020. Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const));
  8021. if (c->value_type == VALUE_TYPE_F64
  8022. || c->value_type == VALUE_TYPE_I64) {
  8023. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  8024. &(c->value.f64), (uint32)sizeof(int64));
  8025. func_const += sizeof(int64);
  8026. }
  8027. else {
  8028. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  8029. &(c->value.f32), (uint32)sizeof(int32));
  8030. func_const += sizeof(int32);
  8031. }
  8032. }
  8033. }
  8034. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  8035. - loader_ctx->start_dynamic_offset + 1;
  8036. #else
  8037. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  8038. #endif
  8039. func->max_block_num = loader_ctx->max_csp_num;
  8040. return_value = true;
  8041. fail:
  8042. wasm_loader_ctx_destroy(loader_ctx);
  8043. (void)table_idx;
  8044. (void)table_seg_idx;
  8045. (void)data_seg_idx;
  8046. (void)i64;
  8047. (void)local_offset;
  8048. (void)p_org;
  8049. (void)mem_offset;
  8050. (void)align;
  8051. return return_value;
  8052. }