wasm_loader.c 327 KB

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