wasm_loader.c 243 KB

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