wasm_loader.c 251 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127
  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. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2122. static bool
  2123. handle_name_section(const uint8 *buf, const uint8 *buf_end,
  2124. WASMModule *module,
  2125. char *error_buf, uint32 error_buf_size)
  2126. {
  2127. const uint8 *p = buf, *p_end = buf_end;
  2128. uint32 name_type, subsection_size;
  2129. uint32 previous_name_type = 0;
  2130. uint32 num_func_name;
  2131. uint32 func_index;
  2132. uint32 previous_func_index = ~0U;
  2133. uint32 func_name_len;
  2134. uint32 name_index;
  2135. int i = 0;
  2136. if (p >= p_end) {
  2137. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2138. return false;
  2139. }
  2140. while (p < p_end) {
  2141. read_leb_uint32(p, p_end, name_type);
  2142. if (i != 0) {
  2143. if (name_type == previous_name_type) {
  2144. set_error_buf(error_buf, error_buf_size,
  2145. "duplicate sub-section");
  2146. return false;
  2147. }
  2148. if (name_type < previous_name_type) {
  2149. set_error_buf(error_buf, error_buf_size,
  2150. "out-of-order sub-section");
  2151. return false;
  2152. }
  2153. }
  2154. previous_name_type = name_type;
  2155. read_leb_uint32(p, p_end, subsection_size);
  2156. CHECK_BUF(p, p_end, subsection_size);
  2157. switch (name_type) {
  2158. case SUB_SECTION_TYPE_FUNC:
  2159. if (subsection_size) {
  2160. read_leb_uint32(p, p_end, num_func_name);
  2161. for (name_index = 0; name_index < num_func_name;
  2162. name_index++) {
  2163. read_leb_uint32(p, p_end, func_index);
  2164. if (func_index == previous_func_index) {
  2165. set_error_buf(error_buf, error_buf_size,
  2166. "duplicate function name");
  2167. return false;
  2168. }
  2169. if (func_index < previous_func_index
  2170. && previous_func_index != ~0U) {
  2171. set_error_buf(error_buf, error_buf_size,
  2172. "out-of-order function index ");
  2173. return false;
  2174. }
  2175. previous_func_index = func_index;
  2176. read_leb_uint32(p, p_end, func_name_len);
  2177. CHECK_BUF(p, p_end, func_name_len);
  2178. // Skip the import functions
  2179. if (func_index >= module->import_count) {
  2180. func_index -= module->import_count;
  2181. if (func_index >= module->function_count) {
  2182. set_error_buf(error_buf, error_buf_size,
  2183. "out-of-range function index");
  2184. return false;
  2185. }
  2186. if (!(module->functions[func_index]->field_name =
  2187. const_str_list_insert(p, func_name_len,
  2188. module, error_buf,
  2189. error_buf_size))) {
  2190. return false;
  2191. }
  2192. }
  2193. p += func_name_len;
  2194. }
  2195. }
  2196. break;
  2197. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection */
  2198. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  2199. default:
  2200. p = p + subsection_size;
  2201. break;
  2202. }
  2203. i++;
  2204. }
  2205. return true;
  2206. fail:
  2207. return false;
  2208. }
  2209. #endif
  2210. static bool
  2211. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2212. char *error_buf, uint32 error_buf_size)
  2213. {
  2214. const uint8 *p = buf, *p_end = buf_end;
  2215. uint32 name_len;
  2216. if (p >= p_end) {
  2217. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2218. return false;
  2219. }
  2220. read_leb_uint32(p, p_end, name_len);
  2221. if (name_len == 0
  2222. || p + name_len > p_end) {
  2223. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2224. return false;
  2225. }
  2226. if (!check_utf8_str(p, name_len)) {
  2227. set_error_buf(error_buf, error_buf_size,
  2228. "invalid UTF-8 encoding");
  2229. return false;
  2230. }
  2231. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2232. if (memcmp(p, "name", 4) == 0) {
  2233. p += name_len;
  2234. handle_name_section(p, p_end, module, error_buf, error_buf_size);
  2235. }
  2236. #endif
  2237. LOG_VERBOSE("Load custom section success.\n");
  2238. return true;
  2239. fail:
  2240. return false;
  2241. }
  2242. static bool
  2243. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2244. BlockAddr *block_addr_cache,
  2245. char *error_buf, uint32 error_buf_size);
  2246. #if WASM_ENABLE_FAST_INTERP != 0
  2247. void **
  2248. wasm_interp_get_handle_table();
  2249. static void **handle_table;
  2250. #endif
  2251. static bool
  2252. load_from_sections(WASMModule *module, WASMSection *sections,
  2253. char *error_buf, uint32 error_buf_size)
  2254. {
  2255. WASMExport *export;
  2256. WASMSection *section = sections;
  2257. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  2258. *buf_func = NULL, *buf_func_end = NULL;
  2259. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2260. WASMGlobal *aux_stack_top_global = NULL, *global;
  2261. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  2262. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  2263. uint32 aux_data_end_global_index = (uint32)-1;
  2264. uint32 aux_heap_base_global_index = (uint32)-1;
  2265. BlockAddr *block_addr_cache;
  2266. WASMType *func_type;
  2267. uint64 total_size;
  2268. /* Find code and function sections if have */
  2269. while (section) {
  2270. if (section->section_type == SECTION_TYPE_CODE) {
  2271. buf_code = section->section_body;
  2272. buf_code_end = buf_code + section->section_body_size;
  2273. }
  2274. else if (section->section_type == SECTION_TYPE_FUNC) {
  2275. buf_func = section->section_body;
  2276. buf_func_end = buf_func + section->section_body_size;
  2277. }
  2278. section = section->next;
  2279. }
  2280. section = sections;
  2281. while (section) {
  2282. buf = section->section_body;
  2283. buf_end = buf + section->section_body_size;
  2284. LOG_DEBUG("to section %d", section->section_type);
  2285. switch (section->section_type) {
  2286. case SECTION_TYPE_USER:
  2287. /* unsupported user section, ignore it. */
  2288. if (!load_user_section(buf, buf_end, module,
  2289. error_buf, error_buf_size))
  2290. return false;
  2291. break;
  2292. case SECTION_TYPE_TYPE:
  2293. if (!load_type_section(buf, buf_end, module,
  2294. error_buf, error_buf_size))
  2295. return false;
  2296. break;
  2297. case SECTION_TYPE_IMPORT:
  2298. if (!load_import_section(buf, buf_end, module,
  2299. error_buf, error_buf_size))
  2300. return false;
  2301. break;
  2302. case SECTION_TYPE_FUNC:
  2303. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2304. module, error_buf, error_buf_size))
  2305. return false;
  2306. break;
  2307. case SECTION_TYPE_TABLE:
  2308. if (!load_table_section(buf, buf_end, module,
  2309. error_buf, error_buf_size))
  2310. return false;
  2311. break;
  2312. case SECTION_TYPE_MEMORY:
  2313. if (!load_memory_section(buf, buf_end, module,
  2314. error_buf, error_buf_size))
  2315. return false;
  2316. break;
  2317. case SECTION_TYPE_GLOBAL:
  2318. if (!load_global_section(buf, buf_end, module,
  2319. error_buf, error_buf_size))
  2320. return false;
  2321. break;
  2322. case SECTION_TYPE_EXPORT:
  2323. if (!load_export_section(buf, buf_end, module,
  2324. error_buf, error_buf_size))
  2325. return false;
  2326. break;
  2327. case SECTION_TYPE_START:
  2328. if (!load_start_section(buf, buf_end, module,
  2329. error_buf, error_buf_size))
  2330. return false;
  2331. break;
  2332. case SECTION_TYPE_ELEM:
  2333. if (!load_table_segment_section(buf, buf_end, module,
  2334. error_buf, error_buf_size))
  2335. return false;
  2336. break;
  2337. case SECTION_TYPE_CODE:
  2338. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2339. module, error_buf, error_buf_size))
  2340. return false;
  2341. break;
  2342. case SECTION_TYPE_DATA:
  2343. if (!load_data_segment_section(buf, buf_end, module,
  2344. error_buf, error_buf_size))
  2345. return false;
  2346. break;
  2347. #if WASM_ENABLE_BULK_MEMORY != 0
  2348. case SECTION_TYPE_DATACOUNT:
  2349. if (!load_datacount_section(buf, buf_end, module,
  2350. error_buf, error_buf_size))
  2351. return false;
  2352. break;
  2353. #endif
  2354. default:
  2355. set_error_buf(error_buf, error_buf_size,
  2356. "invalid section id");
  2357. return false;
  2358. }
  2359. section = section->next;
  2360. }
  2361. module->aux_data_end_global_index = (uint32)-1;
  2362. module->aux_heap_base_global_index = (uint32)-1;
  2363. module->aux_stack_top_global_index = (uint32)-1;
  2364. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2365. export = module->exports;
  2366. for (i = 0; i < module->export_count; i++, export++) {
  2367. if (export->kind == EXPORT_KIND_GLOBAL) {
  2368. if (!strcmp(export->name, "__heap_base")) {
  2369. global_index = export->index - module->import_global_count;
  2370. global = module->globals + global_index;
  2371. if (global->type == VALUE_TYPE_I32
  2372. && !global->is_mutable
  2373. && global->init_expr.init_expr_type ==
  2374. INIT_EXPR_TYPE_I32_CONST) {
  2375. aux_heap_base_global = global;
  2376. aux_heap_base = global->init_expr.u.i32;
  2377. aux_heap_base_global_index = export->index;
  2378. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2379. aux_heap_base);
  2380. }
  2381. }
  2382. else if (!strcmp(export->name, "__data_end")) {
  2383. global_index = export->index - module->import_global_count;
  2384. global = module->globals + global_index;
  2385. if (global->type == VALUE_TYPE_I32
  2386. && !global->is_mutable
  2387. && global->init_expr.init_expr_type ==
  2388. INIT_EXPR_TYPE_I32_CONST) {
  2389. aux_data_end_global = global;
  2390. aux_data_end = global->init_expr.u.i32;
  2391. aux_data_end_global_index = export->index;
  2392. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2393. aux_data_end);
  2394. aux_data_end = align_uint(aux_data_end, 16);
  2395. }
  2396. }
  2397. /* For module compiled with -pthread option, the global is:
  2398. [0] stack_top <-- 0
  2399. [1] tls_pointer
  2400. [2] tls_size
  2401. [3] data_end <-- 3
  2402. [4] global_base
  2403. [5] heap_base <-- 5
  2404. [6] dso_handle
  2405. For module compiled without -pthread option:
  2406. [0] stack_top <-- 0
  2407. [1] data_end <-- 1
  2408. [2] global_base
  2409. [3] heap_base <-- 3
  2410. [4] dso_handle
  2411. */
  2412. if (aux_data_end_global && aux_heap_base_global
  2413. && aux_data_end <= aux_heap_base) {
  2414. module->aux_data_end_global_index = aux_data_end_global_index;
  2415. module->aux_data_end = aux_data_end;
  2416. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2417. module->aux_heap_base = aux_heap_base;
  2418. /* Resolve aux stack top global */
  2419. for (global_index = 0; global_index < module->global_count;
  2420. global_index++) {
  2421. global = module->globals + global_index;
  2422. if (global->is_mutable /* heap_base and data_end is
  2423. not mutable */
  2424. && global->type == VALUE_TYPE_I32
  2425. && global->init_expr.init_expr_type ==
  2426. INIT_EXPR_TYPE_I32_CONST
  2427. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2428. aux_stack_top_global = global;
  2429. aux_stack_top = (uint32)global->init_expr.u.i32;
  2430. module->aux_stack_top_global_index =
  2431. module->import_global_count + global_index;
  2432. module->aux_stack_bottom = aux_stack_top;
  2433. module->aux_stack_size = aux_stack_top > aux_data_end
  2434. ? aux_stack_top - aux_data_end
  2435. : aux_stack_top;
  2436. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2437. "global index: %d, stack size: %d",
  2438. aux_stack_top, global_index,
  2439. module->aux_stack_size);
  2440. break;
  2441. }
  2442. }
  2443. break;
  2444. }
  2445. }
  2446. }
  2447. module->malloc_function = (uint32)-1;
  2448. module->free_function = (uint32)-1;
  2449. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2450. export = module->exports;
  2451. for (i = 0; i < module->export_count; i++, export++) {
  2452. if (export->kind == EXPORT_KIND_FUNC) {
  2453. if (!strcmp(export->name, "malloc")
  2454. && export->index >= module->import_function_count) {
  2455. func_index = export->index - module->import_function_count;
  2456. func_type = module->functions[func_index]->func_type;
  2457. if (func_type->param_count == 1
  2458. && func_type->result_count == 1
  2459. && func_type->types[0] == VALUE_TYPE_I32
  2460. && func_type->types[1] == VALUE_TYPE_I32) {
  2461. module->malloc_function = export->index;
  2462. LOG_VERBOSE("Found malloc function, index: %u",
  2463. export->index);
  2464. }
  2465. }
  2466. else if (!strcmp(export->name, "free")
  2467. && export->index >= module->import_function_count) {
  2468. func_index = export->index - module->import_function_count;
  2469. func_type = module->functions[func_index]->func_type;
  2470. if (func_type->param_count == 1
  2471. && func_type->result_count == 0
  2472. && func_type->types[0] == VALUE_TYPE_I32) {
  2473. module->free_function = export->index;
  2474. LOG_VERBOSE("Found free function, index: %u",
  2475. export->index);
  2476. }
  2477. }
  2478. }
  2479. }
  2480. #if WASM_ENABLE_FAST_INTERP != 0
  2481. handle_table = wasm_interp_get_handle_table();
  2482. #endif
  2483. total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE
  2484. * BLOCK_ADDR_CONFLICT_SIZE;
  2485. if (!(block_addr_cache = loader_malloc
  2486. (total_size, error_buf, error_buf_size))) {
  2487. return false;
  2488. }
  2489. for (i = 0; i < module->function_count; i++) {
  2490. WASMFunction *func = module->functions[i];
  2491. memset(block_addr_cache, 0, (uint32)total_size);
  2492. if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache,
  2493. error_buf, error_buf_size)) {
  2494. wasm_runtime_free(block_addr_cache);
  2495. return false;
  2496. }
  2497. }
  2498. wasm_runtime_free(block_addr_cache);
  2499. if (!module->possible_memory_grow) {
  2500. WASMMemoryImport *memory_import;
  2501. WASMMemory *memory;
  2502. if (aux_data_end_global
  2503. && aux_heap_base_global
  2504. && aux_stack_top_global) {
  2505. uint64 init_memory_size;
  2506. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2507. if (module->import_memory_count) {
  2508. memory_import = &module->import_memories[0].u.memory;
  2509. init_memory_size = (uint64)memory_import->num_bytes_per_page *
  2510. memory_import->init_page_count;
  2511. if (shrunk_memory_size <= init_memory_size) {
  2512. /* Reset memory info to decrease memory usage */
  2513. memory_import->num_bytes_per_page = shrunk_memory_size;
  2514. memory_import->init_page_count = 1;
  2515. LOG_VERBOSE("Shrink import memory size to %d",
  2516. shrunk_memory_size);
  2517. }
  2518. }
  2519. if (module->memory_count) {
  2520. memory = &module->memories[0];
  2521. init_memory_size = (uint64)memory->num_bytes_per_page *
  2522. memory->init_page_count;
  2523. if (shrunk_memory_size <= init_memory_size) {
  2524. /* Reset memory info to decrease memory usage */
  2525. memory->num_bytes_per_page = shrunk_memory_size;
  2526. memory->init_page_count = 1;
  2527. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2528. }
  2529. }
  2530. }
  2531. #if WASM_ENABLE_MULTI_MODULE == 0
  2532. if (module->import_memory_count) {
  2533. memory_import = &module->import_memories[0].u.memory;
  2534. /* Memory init page count cannot be larger than 65536, we don't
  2535. check integer overflow again. */
  2536. memory_import->num_bytes_per_page *= memory_import->init_page_count;
  2537. memory_import->init_page_count = memory_import->max_page_count = 1;
  2538. }
  2539. if (module->memory_count) {
  2540. /* Memory init page count cannot be larger than 65536, we don't
  2541. check integer overflow again. */
  2542. memory = &module->memories[0];
  2543. memory->num_bytes_per_page *= memory->init_page_count;
  2544. memory->init_page_count = memory->max_page_count = 1;
  2545. }
  2546. #endif
  2547. }
  2548. #if WASM_ENABLE_MEMORY_TRACING != 0
  2549. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon*)module);
  2550. #endif
  2551. return true;
  2552. }
  2553. static WASMModule*
  2554. create_module(char *error_buf, uint32 error_buf_size)
  2555. {
  2556. WASMModule *module = loader_malloc(sizeof(WASMModule),
  2557. error_buf, error_buf_size);
  2558. if (!module) {
  2559. return NULL;
  2560. }
  2561. module->module_type = Wasm_Module_Bytecode;
  2562. /* Set start_function to -1, means no start function */
  2563. module->start_function = (uint32)-1;
  2564. #if WASM_ENABLE_MULTI_MODULE != 0
  2565. module->import_module_list = &module->import_module_list_head;
  2566. #endif
  2567. return module;
  2568. }
  2569. WASMModule *
  2570. wasm_loader_load_from_sections(WASMSection *section_list,
  2571. char *error_buf, uint32 error_buf_size)
  2572. {
  2573. WASMModule *module = create_module(error_buf, error_buf_size);
  2574. if (!module)
  2575. return NULL;
  2576. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  2577. wasm_loader_unload(module);
  2578. return NULL;
  2579. }
  2580. LOG_VERBOSE("Load module from sections success.\n");
  2581. return module;
  2582. }
  2583. static void
  2584. destroy_sections(WASMSection *section_list)
  2585. {
  2586. WASMSection *section = section_list, *next;
  2587. while (section) {
  2588. next = section->next;
  2589. wasm_runtime_free(section);
  2590. section = next;
  2591. }
  2592. }
  2593. static uint8 section_ids[] = {
  2594. SECTION_TYPE_USER,
  2595. SECTION_TYPE_TYPE,
  2596. SECTION_TYPE_IMPORT,
  2597. SECTION_TYPE_FUNC,
  2598. SECTION_TYPE_TABLE,
  2599. SECTION_TYPE_MEMORY,
  2600. SECTION_TYPE_GLOBAL,
  2601. SECTION_TYPE_EXPORT,
  2602. SECTION_TYPE_START,
  2603. SECTION_TYPE_ELEM,
  2604. #if WASM_ENABLE_BULK_MEMORY != 0
  2605. SECTION_TYPE_DATACOUNT,
  2606. #endif
  2607. SECTION_TYPE_CODE,
  2608. SECTION_TYPE_DATA
  2609. };
  2610. static uint8
  2611. get_section_index(uint8 section_type)
  2612. {
  2613. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2614. for (uint8 i = 0; i < max_id; i++) {
  2615. if (section_type == section_ids[i])
  2616. return i;
  2617. }
  2618. return (uint8)-1;
  2619. }
  2620. static bool
  2621. create_sections(const uint8 *buf, uint32 size,
  2622. WASMSection **p_section_list,
  2623. char *error_buf, uint32 error_buf_size)
  2624. {
  2625. WASMSection *section_list_end = NULL, *section;
  2626. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  2627. uint8 section_type, section_index, last_section_index = (uint8)-1;
  2628. uint32 section_size;
  2629. bh_assert(!*p_section_list);
  2630. p += 8;
  2631. while (p < p_end) {
  2632. CHECK_BUF(p, p_end, 1);
  2633. section_type = read_uint8(p);
  2634. section_index = get_section_index(section_type);
  2635. if (section_index != (uint8)-1) {
  2636. if (section_type != SECTION_TYPE_USER) {
  2637. /* Custom sections may be inserted at any place,
  2638. while other sections must occur at most once
  2639. and in prescribed order. */
  2640. if (last_section_index != (uint8)-1
  2641. && (section_index <= last_section_index)) {
  2642. set_error_buf(error_buf, error_buf_size,
  2643. "junk after last section");
  2644. return false;
  2645. }
  2646. last_section_index = section_index;
  2647. }
  2648. CHECK_BUF1(p, p_end, 1);
  2649. read_leb_uint32(p, p_end, section_size);
  2650. CHECK_BUF1(p, p_end, section_size);
  2651. if (!(section = loader_malloc(sizeof(WASMSection),
  2652. error_buf, error_buf_size))) {
  2653. return false;
  2654. }
  2655. section->section_type = section_type;
  2656. section->section_body = (uint8*)p;
  2657. section->section_body_size = section_size;
  2658. if (!*p_section_list)
  2659. *p_section_list = section_list_end = section;
  2660. else {
  2661. section_list_end->next = section;
  2662. section_list_end = section;
  2663. }
  2664. p += section_size;
  2665. }
  2666. else {
  2667. set_error_buf(error_buf, error_buf_size,
  2668. "invalid section id");
  2669. return false;
  2670. }
  2671. }
  2672. return true;
  2673. fail:
  2674. return false;
  2675. }
  2676. static void
  2677. exchange32(uint8* p_data)
  2678. {
  2679. uint8 value = *p_data;
  2680. *p_data = *(p_data + 3);
  2681. *(p_data + 3) = value;
  2682. value = *(p_data + 1);
  2683. *(p_data + 1) = *(p_data + 2);
  2684. *(p_data + 2) = value;
  2685. }
  2686. static union {
  2687. int a;
  2688. char b;
  2689. } __ue = { .a = 1 };
  2690. #define is_little_endian() (__ue.b == 1)
  2691. static bool
  2692. load(const uint8 *buf, uint32 size, WASMModule *module,
  2693. char *error_buf, uint32 error_buf_size)
  2694. {
  2695. const uint8 *buf_end = buf + size;
  2696. const uint8 *p = buf, *p_end = buf_end;
  2697. uint32 magic_number, version;
  2698. WASMSection *section_list = NULL;
  2699. CHECK_BUF1(p, p_end, sizeof(uint32));
  2700. magic_number = read_uint32(p);
  2701. if (!is_little_endian())
  2702. exchange32((uint8*)&magic_number);
  2703. if (magic_number != WASM_MAGIC_NUMBER) {
  2704. set_error_buf(error_buf, error_buf_size,
  2705. "magic header not detected");
  2706. return false;
  2707. }
  2708. CHECK_BUF1(p, p_end, sizeof(uint32));
  2709. version = read_uint32(p);
  2710. if (!is_little_endian())
  2711. exchange32((uint8*)&version);
  2712. if (version != WASM_CURRENT_VERSION) {
  2713. set_error_buf(error_buf, error_buf_size,
  2714. "unknown binary version");
  2715. return false;
  2716. }
  2717. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  2718. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  2719. destroy_sections(section_list);
  2720. return false;
  2721. }
  2722. destroy_sections(section_list);
  2723. return true;
  2724. fail:
  2725. return false;
  2726. }
  2727. WASMModule*
  2728. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  2729. {
  2730. WASMModule *module = create_module(error_buf, error_buf_size);
  2731. if (!module) {
  2732. return NULL;
  2733. }
  2734. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2735. goto fail;
  2736. }
  2737. LOG_VERBOSE("Load module success.\n");
  2738. return module;
  2739. fail:
  2740. wasm_loader_unload(module);
  2741. return NULL;
  2742. }
  2743. void
  2744. wasm_loader_unload(WASMModule *module)
  2745. {
  2746. uint32 i;
  2747. if (!module)
  2748. return;
  2749. if (module->types) {
  2750. for (i = 0; i < module->type_count; i++) {
  2751. if (module->types[i])
  2752. wasm_runtime_free(module->types[i]);
  2753. }
  2754. wasm_runtime_free(module->types);
  2755. }
  2756. if (module->imports)
  2757. wasm_runtime_free(module->imports);
  2758. if (module->functions) {
  2759. for (i = 0; i < module->function_count; i++) {
  2760. if (module->functions[i]) {
  2761. if (module->functions[i]->local_offsets)
  2762. wasm_runtime_free(module->functions[i]->local_offsets);
  2763. #if WASM_ENABLE_FAST_INTERP != 0
  2764. if (module->functions[i]->code_compiled)
  2765. wasm_runtime_free(module->functions[i]->code_compiled);
  2766. if (module->functions[i]->consts)
  2767. wasm_runtime_free(module->functions[i]->consts);
  2768. #endif
  2769. wasm_runtime_free(module->functions[i]);
  2770. }
  2771. }
  2772. wasm_runtime_free(module->functions);
  2773. }
  2774. if (module->tables)
  2775. wasm_runtime_free(module->tables);
  2776. if (module->memories)
  2777. wasm_runtime_free(module->memories);
  2778. if (module->globals)
  2779. wasm_runtime_free(module->globals);
  2780. if (module->exports)
  2781. wasm_runtime_free(module->exports);
  2782. if (module->table_segments) {
  2783. for (i = 0; i < module->table_seg_count; i++) {
  2784. if (module->table_segments[i].func_indexes)
  2785. wasm_runtime_free(module->table_segments[i].func_indexes);
  2786. }
  2787. wasm_runtime_free(module->table_segments);
  2788. }
  2789. if (module->data_segments) {
  2790. for (i = 0; i < module->data_seg_count; i++) {
  2791. if (module->data_segments[i])
  2792. wasm_runtime_free(module->data_segments[i]);
  2793. }
  2794. wasm_runtime_free(module->data_segments);
  2795. }
  2796. if (module->const_str_list) {
  2797. StringNode *node = module->const_str_list, *node_next;
  2798. while (node) {
  2799. node_next = node->next;
  2800. wasm_runtime_free(node);
  2801. node = node_next;
  2802. }
  2803. }
  2804. #if WASM_ENABLE_MULTI_MODULE != 0
  2805. /* just release the sub module list */
  2806. if (module->import_module_list) {
  2807. WASMRegisteredModule *node =
  2808. bh_list_first_elem(module->import_module_list);
  2809. while (node) {
  2810. WASMRegisteredModule *next = bh_list_elem_next(node);
  2811. bh_list_remove(module->import_module_list, node);
  2812. /*
  2813. * unload(sub_module) will be trigged during runtime_destroy().
  2814. * every module in the global module list will be unloaded one by
  2815. * one. so don't worry.
  2816. */
  2817. wasm_runtime_free(node);
  2818. /*
  2819. *
  2820. * the module file reading buffer will be released
  2821. * in runtime_destroy()
  2822. */
  2823. node = next;
  2824. }
  2825. }
  2826. #endif
  2827. wasm_runtime_free(module);
  2828. }
  2829. bool
  2830. wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
  2831. const uint8 *start_addr,
  2832. const uint8 *code_end_addr,
  2833. uint8 label_type,
  2834. uint8 **p_else_addr,
  2835. uint8 **p_end_addr,
  2836. char *error_buf,
  2837. uint32 error_buf_size)
  2838. {
  2839. const uint8 *p = start_addr, *p_end = code_end_addr;
  2840. uint8 *else_addr = NULL;
  2841. uint32 block_nested_depth = 1, count, i, j, t;
  2842. uint8 opcode, u8;
  2843. BlockAddr block_stack[16] = { 0 }, *block;
  2844. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  2845. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2846. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  2847. if (block[j].start_addr == start_addr) {
  2848. /* Cache hit */
  2849. *p_else_addr = block[j].else_addr;
  2850. *p_end_addr = block[j].end_addr;
  2851. return true;
  2852. }
  2853. }
  2854. /* Cache unhit */
  2855. block_stack[0].start_addr = start_addr;
  2856. while (p < code_end_addr) {
  2857. opcode = *p++;
  2858. switch (opcode) {
  2859. case WASM_OP_UNREACHABLE:
  2860. case WASM_OP_NOP:
  2861. break;
  2862. case WASM_OP_BLOCK:
  2863. case WASM_OP_LOOP:
  2864. case WASM_OP_IF:
  2865. CHECK_BUF(p, p_end, 1);
  2866. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  2867. u8 = read_uint8(p);
  2868. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  2869. block_stack[block_nested_depth].start_addr = p;
  2870. block_stack[block_nested_depth].else_addr = NULL;
  2871. }
  2872. block_nested_depth++;
  2873. break;
  2874. case EXT_OP_BLOCK:
  2875. case EXT_OP_LOOP:
  2876. case EXT_OP_IF:
  2877. /* block type */
  2878. skip_leb_uint32(p, p_end);
  2879. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  2880. block_stack[block_nested_depth].start_addr = p;
  2881. block_stack[block_nested_depth].else_addr = NULL;
  2882. }
  2883. block_nested_depth++;
  2884. break;
  2885. case WASM_OP_ELSE:
  2886. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  2887. else_addr = (uint8*)(p - 1);
  2888. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  2889. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  2890. break;
  2891. case WASM_OP_END:
  2892. if (block_nested_depth == 1) {
  2893. if (label_type == LABEL_TYPE_IF)
  2894. *p_else_addr = else_addr;
  2895. *p_end_addr = (uint8*)(p - 1);
  2896. block_stack[0].end_addr = (uint8*)(p - 1);
  2897. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  2898. start_addr = block_stack[t].start_addr;
  2899. if (start_addr) {
  2900. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  2901. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2902. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  2903. if (!block[j].start_addr)
  2904. break;
  2905. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  2906. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  2907. sizeof(BlockAddr));
  2908. j = 0;
  2909. }
  2910. block[j].start_addr = block_stack[t].start_addr;
  2911. block[j].else_addr = block_stack[t].else_addr;
  2912. block[j].end_addr = block_stack[t].end_addr;
  2913. }
  2914. else
  2915. break;
  2916. }
  2917. return true;
  2918. }
  2919. else {
  2920. block_nested_depth--;
  2921. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  2922. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  2923. }
  2924. break;
  2925. case WASM_OP_BR:
  2926. case WASM_OP_BR_IF:
  2927. skip_leb_uint32(p, p_end); /* labelidx */
  2928. break;
  2929. case WASM_OP_BR_TABLE:
  2930. read_leb_uint32(p, p_end, count); /* lable num */
  2931. for (i = 0; i <= count; i++) /* lableidxs */
  2932. skip_leb_uint32(p, p_end);
  2933. break;
  2934. case WASM_OP_RETURN:
  2935. break;
  2936. case WASM_OP_CALL:
  2937. #if WASM_ENABLE_TAIL_CALL != 0
  2938. case WASM_OP_RETURN_CALL:
  2939. #endif
  2940. skip_leb_uint32(p, p_end); /* funcidx */
  2941. break;
  2942. case WASM_OP_CALL_INDIRECT:
  2943. #if WASM_ENABLE_TAIL_CALL != 0
  2944. case WASM_OP_RETURN_CALL_INDIRECT:
  2945. #endif
  2946. skip_leb_uint32(p, p_end); /* typeidx */
  2947. CHECK_BUF(p, p_end, 1);
  2948. u8 = read_uint8(p); /* 0x00 */
  2949. break;
  2950. case WASM_OP_DROP:
  2951. case WASM_OP_SELECT:
  2952. case WASM_OP_DROP_64:
  2953. case WASM_OP_SELECT_64:
  2954. break;
  2955. case WASM_OP_GET_LOCAL:
  2956. case WASM_OP_SET_LOCAL:
  2957. case WASM_OP_TEE_LOCAL:
  2958. case WASM_OP_GET_GLOBAL:
  2959. case WASM_OP_SET_GLOBAL:
  2960. case WASM_OP_GET_GLOBAL_64:
  2961. case WASM_OP_SET_GLOBAL_64:
  2962. case WASM_OP_SET_GLOBAL_AUX_STACK:
  2963. skip_leb_uint32(p, p_end); /* localidx */
  2964. break;
  2965. case EXT_OP_GET_LOCAL_FAST:
  2966. case EXT_OP_SET_LOCAL_FAST:
  2967. case EXT_OP_TEE_LOCAL_FAST:
  2968. CHECK_BUF(p, p_end, 1);
  2969. p++;
  2970. break;
  2971. case WASM_OP_I32_LOAD:
  2972. case WASM_OP_I64_LOAD:
  2973. case WASM_OP_F32_LOAD:
  2974. case WASM_OP_F64_LOAD:
  2975. case WASM_OP_I32_LOAD8_S:
  2976. case WASM_OP_I32_LOAD8_U:
  2977. case WASM_OP_I32_LOAD16_S:
  2978. case WASM_OP_I32_LOAD16_U:
  2979. case WASM_OP_I64_LOAD8_S:
  2980. case WASM_OP_I64_LOAD8_U:
  2981. case WASM_OP_I64_LOAD16_S:
  2982. case WASM_OP_I64_LOAD16_U:
  2983. case WASM_OP_I64_LOAD32_S:
  2984. case WASM_OP_I64_LOAD32_U:
  2985. case WASM_OP_I32_STORE:
  2986. case WASM_OP_I64_STORE:
  2987. case WASM_OP_F32_STORE:
  2988. case WASM_OP_F64_STORE:
  2989. case WASM_OP_I32_STORE8:
  2990. case WASM_OP_I32_STORE16:
  2991. case WASM_OP_I64_STORE8:
  2992. case WASM_OP_I64_STORE16:
  2993. case WASM_OP_I64_STORE32:
  2994. skip_leb_uint32(p, p_end); /* align */
  2995. skip_leb_uint32(p, p_end); /* offset */
  2996. break;
  2997. case WASM_OP_MEMORY_SIZE:
  2998. case WASM_OP_MEMORY_GROW:
  2999. skip_leb_uint32(p, p_end); /* 0x00 */
  3000. break;
  3001. case WASM_OP_I32_CONST:
  3002. skip_leb_int32(p, p_end);
  3003. break;
  3004. case WASM_OP_I64_CONST:
  3005. skip_leb_int64(p, p_end);
  3006. break;
  3007. case WASM_OP_F32_CONST:
  3008. p += sizeof(float32);
  3009. break;
  3010. case WASM_OP_F64_CONST:
  3011. p += sizeof(float64);
  3012. break;
  3013. case WASM_OP_I32_EQZ:
  3014. case WASM_OP_I32_EQ:
  3015. case WASM_OP_I32_NE:
  3016. case WASM_OP_I32_LT_S:
  3017. case WASM_OP_I32_LT_U:
  3018. case WASM_OP_I32_GT_S:
  3019. case WASM_OP_I32_GT_U:
  3020. case WASM_OP_I32_LE_S:
  3021. case WASM_OP_I32_LE_U:
  3022. case WASM_OP_I32_GE_S:
  3023. case WASM_OP_I32_GE_U:
  3024. case WASM_OP_I64_EQZ:
  3025. case WASM_OP_I64_EQ:
  3026. case WASM_OP_I64_NE:
  3027. case WASM_OP_I64_LT_S:
  3028. case WASM_OP_I64_LT_U:
  3029. case WASM_OP_I64_GT_S:
  3030. case WASM_OP_I64_GT_U:
  3031. case WASM_OP_I64_LE_S:
  3032. case WASM_OP_I64_LE_U:
  3033. case WASM_OP_I64_GE_S:
  3034. case WASM_OP_I64_GE_U:
  3035. case WASM_OP_F32_EQ:
  3036. case WASM_OP_F32_NE:
  3037. case WASM_OP_F32_LT:
  3038. case WASM_OP_F32_GT:
  3039. case WASM_OP_F32_LE:
  3040. case WASM_OP_F32_GE:
  3041. case WASM_OP_F64_EQ:
  3042. case WASM_OP_F64_NE:
  3043. case WASM_OP_F64_LT:
  3044. case WASM_OP_F64_GT:
  3045. case WASM_OP_F64_LE:
  3046. case WASM_OP_F64_GE:
  3047. case WASM_OP_I32_CLZ:
  3048. case WASM_OP_I32_CTZ:
  3049. case WASM_OP_I32_POPCNT:
  3050. case WASM_OP_I32_ADD:
  3051. case WASM_OP_I32_SUB:
  3052. case WASM_OP_I32_MUL:
  3053. case WASM_OP_I32_DIV_S:
  3054. case WASM_OP_I32_DIV_U:
  3055. case WASM_OP_I32_REM_S:
  3056. case WASM_OP_I32_REM_U:
  3057. case WASM_OP_I32_AND:
  3058. case WASM_OP_I32_OR:
  3059. case WASM_OP_I32_XOR:
  3060. case WASM_OP_I32_SHL:
  3061. case WASM_OP_I32_SHR_S:
  3062. case WASM_OP_I32_SHR_U:
  3063. case WASM_OP_I32_ROTL:
  3064. case WASM_OP_I32_ROTR:
  3065. case WASM_OP_I64_CLZ:
  3066. case WASM_OP_I64_CTZ:
  3067. case WASM_OP_I64_POPCNT:
  3068. case WASM_OP_I64_ADD:
  3069. case WASM_OP_I64_SUB:
  3070. case WASM_OP_I64_MUL:
  3071. case WASM_OP_I64_DIV_S:
  3072. case WASM_OP_I64_DIV_U:
  3073. case WASM_OP_I64_REM_S:
  3074. case WASM_OP_I64_REM_U:
  3075. case WASM_OP_I64_AND:
  3076. case WASM_OP_I64_OR:
  3077. case WASM_OP_I64_XOR:
  3078. case WASM_OP_I64_SHL:
  3079. case WASM_OP_I64_SHR_S:
  3080. case WASM_OP_I64_SHR_U:
  3081. case WASM_OP_I64_ROTL:
  3082. case WASM_OP_I64_ROTR:
  3083. case WASM_OP_F32_ABS:
  3084. case WASM_OP_F32_NEG:
  3085. case WASM_OP_F32_CEIL:
  3086. case WASM_OP_F32_FLOOR:
  3087. case WASM_OP_F32_TRUNC:
  3088. case WASM_OP_F32_NEAREST:
  3089. case WASM_OP_F32_SQRT:
  3090. case WASM_OP_F32_ADD:
  3091. case WASM_OP_F32_SUB:
  3092. case WASM_OP_F32_MUL:
  3093. case WASM_OP_F32_DIV:
  3094. case WASM_OP_F32_MIN:
  3095. case WASM_OP_F32_MAX:
  3096. case WASM_OP_F32_COPYSIGN:
  3097. case WASM_OP_F64_ABS:
  3098. case WASM_OP_F64_NEG:
  3099. case WASM_OP_F64_CEIL:
  3100. case WASM_OP_F64_FLOOR:
  3101. case WASM_OP_F64_TRUNC:
  3102. case WASM_OP_F64_NEAREST:
  3103. case WASM_OP_F64_SQRT:
  3104. case WASM_OP_F64_ADD:
  3105. case WASM_OP_F64_SUB:
  3106. case WASM_OP_F64_MUL:
  3107. case WASM_OP_F64_DIV:
  3108. case WASM_OP_F64_MIN:
  3109. case WASM_OP_F64_MAX:
  3110. case WASM_OP_F64_COPYSIGN:
  3111. case WASM_OP_I32_WRAP_I64:
  3112. case WASM_OP_I32_TRUNC_S_F32:
  3113. case WASM_OP_I32_TRUNC_U_F32:
  3114. case WASM_OP_I32_TRUNC_S_F64:
  3115. case WASM_OP_I32_TRUNC_U_F64:
  3116. case WASM_OP_I64_EXTEND_S_I32:
  3117. case WASM_OP_I64_EXTEND_U_I32:
  3118. case WASM_OP_I64_TRUNC_S_F32:
  3119. case WASM_OP_I64_TRUNC_U_F32:
  3120. case WASM_OP_I64_TRUNC_S_F64:
  3121. case WASM_OP_I64_TRUNC_U_F64:
  3122. case WASM_OP_F32_CONVERT_S_I32:
  3123. case WASM_OP_F32_CONVERT_U_I32:
  3124. case WASM_OP_F32_CONVERT_S_I64:
  3125. case WASM_OP_F32_CONVERT_U_I64:
  3126. case WASM_OP_F32_DEMOTE_F64:
  3127. case WASM_OP_F64_CONVERT_S_I32:
  3128. case WASM_OP_F64_CONVERT_U_I32:
  3129. case WASM_OP_F64_CONVERT_S_I64:
  3130. case WASM_OP_F64_CONVERT_U_I64:
  3131. case WASM_OP_F64_PROMOTE_F32:
  3132. case WASM_OP_I32_REINTERPRET_F32:
  3133. case WASM_OP_I64_REINTERPRET_F64:
  3134. case WASM_OP_F32_REINTERPRET_I32:
  3135. case WASM_OP_F64_REINTERPRET_I64:
  3136. case WASM_OP_I32_EXTEND8_S:
  3137. case WASM_OP_I32_EXTEND16_S:
  3138. case WASM_OP_I64_EXTEND8_S:
  3139. case WASM_OP_I64_EXTEND16_S:
  3140. case WASM_OP_I64_EXTEND32_S:
  3141. break;
  3142. case WASM_OP_MISC_PREFIX:
  3143. {
  3144. uint32 opcode1;
  3145. read_leb_uint32(p, p_end, opcode1);
  3146. switch (opcode1) {
  3147. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3148. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3149. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3150. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3151. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3152. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3153. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3154. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3155. break;
  3156. #if WASM_ENABLE_BULK_MEMORY != 0
  3157. case WASM_OP_MEMORY_INIT:
  3158. skip_leb_uint32(p, p_end);
  3159. /* skip memory idx */
  3160. p++;
  3161. break;
  3162. case WASM_OP_DATA_DROP:
  3163. skip_leb_uint32(p, p_end);
  3164. break;
  3165. case WASM_OP_MEMORY_COPY:
  3166. /* skip two memory idx */
  3167. p += 2;
  3168. break;
  3169. case WASM_OP_MEMORY_FILL:
  3170. /* skip memory idx */
  3171. p++;
  3172. break;
  3173. #endif
  3174. default:
  3175. set_error_buf_v(error_buf, error_buf_size,
  3176. "%s %02x %02x",
  3177. "unsupported opcode", 0xfc, opcode);
  3178. return false;
  3179. }
  3180. break;
  3181. }
  3182. #if WASM_ENABLE_SHARED_MEMORY != 0
  3183. case WASM_OP_ATOMIC_PREFIX:
  3184. {
  3185. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3186. opcode = read_uint8(p);
  3187. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3188. skip_leb_uint32(p, p_end); /* align */
  3189. skip_leb_uint32(p, p_end); /* offset */
  3190. }
  3191. else {
  3192. /* atomic.fence doesn't have memarg */
  3193. p++;
  3194. }
  3195. break;
  3196. }
  3197. #endif
  3198. default:
  3199. set_error_buf_v(error_buf, error_buf_size,
  3200. "%s %02x",
  3201. "unsupported opcode", opcode);
  3202. return false;
  3203. }
  3204. }
  3205. (void)u8;
  3206. return false;
  3207. fail:
  3208. return false;
  3209. }
  3210. #define REF_I32 VALUE_TYPE_I32
  3211. #define REF_F32 VALUE_TYPE_F32
  3212. #define REF_I64_1 VALUE_TYPE_I64
  3213. #define REF_I64_2 VALUE_TYPE_I64
  3214. #define REF_F64_1 VALUE_TYPE_F64
  3215. #define REF_F64_2 VALUE_TYPE_F64
  3216. #define REF_ANY VALUE_TYPE_ANY
  3217. #if WASM_ENABLE_FAST_INTERP != 0
  3218. #if WASM_DEBUG_PREPROCESSOR != 0
  3219. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3220. #else
  3221. #define LOG_OP(...) (void)0
  3222. #endif
  3223. #define PATCH_ELSE 0
  3224. #define PATCH_END 1
  3225. typedef struct BranchBlockPatch {
  3226. struct BranchBlockPatch *next;
  3227. uint8 patch_type;
  3228. uint8 *code_compiled;
  3229. } BranchBlockPatch;
  3230. #endif
  3231. typedef struct BranchBlock {
  3232. uint8 label_type;
  3233. BlockType block_type;
  3234. uint8 *start_addr;
  3235. uint8 *else_addr;
  3236. uint8 *end_addr;
  3237. uint32 stack_cell_num;
  3238. #if WASM_ENABLE_FAST_INTERP != 0
  3239. uint16 dynamic_offset;
  3240. uint8 *code_compiled;
  3241. BranchBlockPatch *patch_list;
  3242. /* This is used to save params frame_offset of of if block */
  3243. int16 *param_frame_offsets;
  3244. #endif
  3245. /* Indicate the operand stack is in polymorphic state.
  3246. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3247. * to polymorphic state until the block's 'end' opcode is processed.
  3248. * If stack is in polymorphic state and stack is empty, instruction can
  3249. * pop any type of value directly without decreasing stack top pointer
  3250. * and stack cell num. */
  3251. bool is_stack_polymorphic;
  3252. } BranchBlock;
  3253. typedef struct WASMLoaderContext {
  3254. /* frame ref stack */
  3255. uint8 *frame_ref;
  3256. uint8 *frame_ref_bottom;
  3257. uint8 *frame_ref_boundary;
  3258. uint32 frame_ref_size;
  3259. uint32 stack_cell_num;
  3260. uint32 max_stack_cell_num;
  3261. /* frame csp stack */
  3262. BranchBlock *frame_csp;
  3263. BranchBlock *frame_csp_bottom;
  3264. BranchBlock *frame_csp_boundary;
  3265. uint32 frame_csp_size;
  3266. uint32 csp_num;
  3267. uint32 max_csp_num;
  3268. #if WASM_ENABLE_FAST_INTERP != 0
  3269. /* frame offset stack */
  3270. int16 *frame_offset;
  3271. int16 *frame_offset_bottom;
  3272. int16 *frame_offset_boundary;
  3273. uint32 frame_offset_size;
  3274. int16 dynamic_offset;
  3275. int16 start_dynamic_offset;
  3276. int16 max_dynamic_offset;
  3277. /* preserved local offset */
  3278. int16 preserved_local_offset;
  3279. /* const buffer */
  3280. uint8 *const_buf;
  3281. uint16 num_const;
  3282. uint16 const_buf_size;
  3283. uint16 const_cell_num;
  3284. /* processed code */
  3285. uint8 *p_code_compiled;
  3286. uint8 *p_code_compiled_end;
  3287. uint32 code_compiled_size;
  3288. #endif
  3289. } WASMLoaderContext;
  3290. typedef struct Const {
  3291. WASMValue value;
  3292. uint16 slot_index;
  3293. uint8 value_type;
  3294. } Const;
  3295. static void*
  3296. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new,
  3297. char *error_buf, uint32 error_buf_size)
  3298. {
  3299. uint8 *mem_new;
  3300. bh_assert(size_new > size_old);
  3301. if ((mem_new = loader_malloc
  3302. (size_new, error_buf, error_buf_size))) {
  3303. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  3304. memset(mem_new + size_old, 0, size_new - size_old);
  3305. wasm_runtime_free(mem_old);
  3306. }
  3307. return mem_new;
  3308. }
  3309. #define MEM_REALLOC(mem, size_old, size_new) do { \
  3310. void *mem_new = memory_realloc(mem, size_old, size_new, \
  3311. error_buf, error_buf_size); \
  3312. if (!mem_new) \
  3313. goto fail; \
  3314. mem = mem_new; \
  3315. } while (0)
  3316. #define CHECK_CSP_PUSH() do { \
  3317. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3318. MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3319. (uint32)(ctx->frame_csp_size \
  3320. + 8 * sizeof(BranchBlock))); \
  3321. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3322. ctx->frame_csp_boundary = ctx->frame_csp_bottom + \
  3323. ctx->frame_csp_size / sizeof(BranchBlock); \
  3324. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3325. } \
  3326. } while (0)
  3327. #define CHECK_CSP_POP() do { \
  3328. if (ctx->csp_num < 1) { \
  3329. set_error_buf(error_buf, error_buf_size, \
  3330. "type mismatch: " \
  3331. "expect data but block stack was empty"); \
  3332. goto fail; \
  3333. } \
  3334. } while (0)
  3335. #if WASM_ENABLE_FAST_INTERP != 0
  3336. static bool
  3337. check_offset_push(WASMLoaderContext *ctx,
  3338. char *error_buf, uint32 error_buf_size)
  3339. {
  3340. uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom);
  3341. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3342. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3343. ctx->frame_offset_size + 16);
  3344. ctx->frame_offset_size += 16;
  3345. ctx->frame_offset_boundary = ctx->frame_offset_bottom +
  3346. ctx->frame_offset_size / sizeof(int16);
  3347. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3348. }
  3349. return true;
  3350. fail:
  3351. return false;
  3352. }
  3353. static bool
  3354. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3355. {
  3356. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3357. return false;
  3358. return true;
  3359. }
  3360. static void
  3361. free_label_patch_list(BranchBlock *frame_csp)
  3362. {
  3363. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3364. BranchBlockPatch *next;
  3365. while (label_patch != NULL) {
  3366. next = label_patch->next;
  3367. wasm_runtime_free(label_patch);
  3368. label_patch = next;
  3369. }
  3370. frame_csp->patch_list = NULL;
  3371. }
  3372. static void
  3373. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3374. {
  3375. BranchBlock *tmp_csp = frame_csp;
  3376. for (uint32 i = 0; i < csp_num; i++) {
  3377. free_label_patch_list(tmp_csp);
  3378. tmp_csp ++;
  3379. }
  3380. }
  3381. #endif
  3382. static bool
  3383. check_stack_push(WASMLoaderContext *ctx,
  3384. char *error_buf, uint32 error_buf_size)
  3385. {
  3386. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3387. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3388. ctx->frame_ref_size + 16);
  3389. ctx->frame_ref_size += 16;
  3390. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3391. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3392. }
  3393. return true;
  3394. fail:
  3395. return false;
  3396. }
  3397. static bool
  3398. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3399. char *error_buf, uint32 error_buf_size)
  3400. {
  3401. char *type_str[] = { "f64", "f32", "i64", "i32" };
  3402. if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  3403. && stack_cell_num < 1)
  3404. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  3405. && stack_cell_num < 2)) {
  3406. set_error_buf(error_buf, error_buf_size,
  3407. "type mismatch: expect data but stack was empty");
  3408. return false;
  3409. }
  3410. if ((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3411. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3412. || (type == VALUE_TYPE_I64
  3413. && (*(frame_ref - 2) != REF_I64_1
  3414. || *(frame_ref - 1) != REF_I64_2))
  3415. || (type == VALUE_TYPE_F64
  3416. && (*(frame_ref - 2) != REF_F64_1
  3417. || *(frame_ref - 1) != REF_F64_2))) {
  3418. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  3419. "type mismatch: expect ",
  3420. type_str[type - VALUE_TYPE_F64],
  3421. " but got other");
  3422. return false;
  3423. }
  3424. return true;
  3425. }
  3426. static bool
  3427. check_stack_pop(WASMLoaderContext *ctx, uint8 type,
  3428. char *error_buf, uint32 error_buf_size)
  3429. {
  3430. int32 block_stack_cell_num = (int32)
  3431. (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3432. if (block_stack_cell_num > 0
  3433. && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3434. /* the stack top is a value of any type, return success */
  3435. return true;
  3436. }
  3437. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num,
  3438. type, error_buf, error_buf_size))
  3439. return false;
  3440. return true;
  3441. }
  3442. static void
  3443. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3444. {
  3445. if (ctx) {
  3446. if (ctx->frame_ref_bottom)
  3447. wasm_runtime_free(ctx->frame_ref_bottom);
  3448. if (ctx->frame_csp_bottom) {
  3449. #if WASM_ENABLE_FAST_INTERP != 0
  3450. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3451. #endif
  3452. wasm_runtime_free(ctx->frame_csp_bottom);
  3453. }
  3454. #if WASM_ENABLE_FAST_INTERP != 0
  3455. if (ctx->frame_offset_bottom)
  3456. wasm_runtime_free(ctx->frame_offset_bottom);
  3457. if (ctx->const_buf)
  3458. wasm_runtime_free(ctx->const_buf);
  3459. #endif
  3460. wasm_runtime_free(ctx);
  3461. }
  3462. }
  3463. static WASMLoaderContext*
  3464. wasm_loader_ctx_init(WASMFunction *func)
  3465. {
  3466. WASMLoaderContext *loader_ctx =
  3467. wasm_runtime_malloc(sizeof(WASMLoaderContext));
  3468. if (!loader_ctx)
  3469. return false;
  3470. memset(loader_ctx, 0, sizeof(WASMLoaderContext));
  3471. loader_ctx->frame_ref_size = 32;
  3472. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
  3473. wasm_runtime_malloc(loader_ctx->frame_ref_size)))
  3474. goto fail;
  3475. memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
  3476. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom +
  3477. loader_ctx->frame_ref_size;
  3478. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3479. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
  3480. wasm_runtime_malloc(loader_ctx->frame_csp_size)))
  3481. goto fail;
  3482. memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
  3483. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3484. #if WASM_ENABLE_FAST_INTERP != 0
  3485. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3486. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3487. wasm_runtime_malloc(loader_ctx->frame_offset_size)))
  3488. goto fail;
  3489. memset(loader_ctx->frame_offset_bottom, 0,
  3490. loader_ctx->frame_offset_size);
  3491. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3492. loader_ctx->num_const = 0;
  3493. loader_ctx->const_buf_size = sizeof(Const) * 8;
  3494. if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size)))
  3495. goto fail;
  3496. memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
  3497. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  3498. loader_ctx->max_dynamic_offset = func->param_cell_num +
  3499. func->local_cell_num;
  3500. #endif
  3501. return loader_ctx;
  3502. fail:
  3503. wasm_loader_ctx_destroy(loader_ctx);
  3504. return NULL;
  3505. }
  3506. static bool
  3507. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type,
  3508. char *error_buf, uint32 error_buf_size)
  3509. {
  3510. if (type == VALUE_TYPE_VOID)
  3511. return true;
  3512. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3513. return false;
  3514. *ctx->frame_ref++ = type;
  3515. ctx->stack_cell_num++;
  3516. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3517. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3518. if (type == VALUE_TYPE_I32
  3519. || type == VALUE_TYPE_F32
  3520. || type == VALUE_TYPE_ANY)
  3521. return true;
  3522. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3523. return false;
  3524. *ctx->frame_ref++ = type;
  3525. ctx->stack_cell_num++;
  3526. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3527. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3528. return true;
  3529. }
  3530. static bool
  3531. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type,
  3532. char *error_buf, uint32 error_buf_size)
  3533. {
  3534. BranchBlock *cur_block = ctx->frame_csp - 1;
  3535. int32 available_stack_cell = (int32)
  3536. (ctx->stack_cell_num - cur_block->stack_cell_num);
  3537. /* Directly return success if current block is in stack
  3538. * polymorphic state while stack is empty. */
  3539. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3540. return true;
  3541. if (type == VALUE_TYPE_VOID)
  3542. return true;
  3543. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  3544. return false;
  3545. ctx->frame_ref--;
  3546. ctx->stack_cell_num--;
  3547. if (type == VALUE_TYPE_I32
  3548. || type == VALUE_TYPE_F32
  3549. || *ctx->frame_ref == VALUE_TYPE_ANY)
  3550. return true;
  3551. ctx->frame_ref--;
  3552. ctx->stack_cell_num--;
  3553. return true;
  3554. }
  3555. static bool
  3556. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  3557. uint8 type_push, uint8 type_pop,
  3558. char *error_buf, uint32 error_buf_size)
  3559. {
  3560. for (int i = 0; i < pop_cnt; i++) {
  3561. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size))
  3562. return false;
  3563. }
  3564. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  3565. return false;
  3566. return true;
  3567. }
  3568. static bool
  3569. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  3570. BlockType block_type, uint8* start_addr,
  3571. char *error_buf, uint32 error_buf_size)
  3572. {
  3573. CHECK_CSP_PUSH();
  3574. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  3575. ctx->frame_csp->label_type = label_type;
  3576. ctx->frame_csp->block_type = block_type;
  3577. ctx->frame_csp->start_addr = start_addr;
  3578. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  3579. #if WASM_ENABLE_FAST_INTERP != 0
  3580. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  3581. ctx->frame_csp->patch_list = NULL;
  3582. #endif
  3583. ctx->frame_csp++;
  3584. ctx->csp_num++;
  3585. if (ctx->csp_num > ctx->max_csp_num)
  3586. ctx->max_csp_num = ctx->csp_num;
  3587. return true;
  3588. fail:
  3589. return false;
  3590. }
  3591. static bool
  3592. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
  3593. char *error_buf, uint32 error_buf_size)
  3594. {
  3595. CHECK_CSP_POP();
  3596. #if WASM_ENABLE_FAST_INTERP != 0
  3597. if ((ctx->frame_csp - 1)->param_frame_offsets)
  3598. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  3599. #endif
  3600. ctx->frame_csp--;
  3601. ctx->csp_num--;
  3602. return true;
  3603. fail:
  3604. return false;
  3605. }
  3606. #if WASM_ENABLE_FAST_INTERP != 0
  3607. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  3608. #define emit_label(opcode) do { \
  3609. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  3610. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3611. } while (0)
  3612. #define skip_label() do { \
  3613. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  3614. LOG_OP("\ndelete last op\n"); \
  3615. } while (0)
  3616. #else
  3617. #define emit_label(opcode) do { \
  3618. int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
  3619. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  3620. set_error_buf(error_buf, error_buf_size, \
  3621. "pre-compiled label offset out of range"); \
  3622. goto fail; \
  3623. } \
  3624. wasm_loader_emit_int16(loader_ctx, offset); \
  3625. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3626. } while (0)
  3627. /* drop local.get / const / block / loop / end */
  3628. #define skip_label() do { \
  3629. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  3630. LOG_OP("\ndelete last op\n"); \
  3631. } while (0)
  3632. #endif /* WASM_ENABLE_ABS_LABEL_ADDR */
  3633. #define emit_empty_label_addr_and_frame_ip(type) do { \
  3634. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3635. loader_ctx->p_code_compiled, \
  3636. error_buf, error_buf_size)) \
  3637. goto fail; \
  3638. /* label address, to be patched */ \
  3639. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3640. } while (0)
  3641. #define emit_br_info(frame_csp) do { \
  3642. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \
  3643. error_buf, error_buf_size)) \
  3644. goto fail; \
  3645. } while (0)
  3646. #define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \
  3647. && last_op <= WASM_OP_I32_ROTR) \
  3648. || (last_op == WASM_OP_I32_LOAD \
  3649. || last_op == WASM_OP_F32_LOAD) \
  3650. || (last_op >= WASM_OP_I32_LOAD8_S \
  3651. && last_op <= WASM_OP_I32_LOAD16_U) \
  3652. || (last_op >= WASM_OP_F32_ABS \
  3653. && last_op <= WASM_OP_F32_COPYSIGN) \
  3654. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3655. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3656. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3657. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3658. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3659. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3660. || (last_op == EXT_OP_COPY_STACK_TOP)
  3661. #define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \
  3662. && last_op <= WASM_OP_I64_ROTR) \
  3663. || (last_op >= WASM_OP_F64_ABS \
  3664. && last_op <= WASM_OP_F64_COPYSIGN) \
  3665. || (last_op == WASM_OP_I64_LOAD \
  3666. || last_op == WASM_OP_F64_LOAD) \
  3667. || (last_op >= WASM_OP_I64_LOAD8_S \
  3668. && last_op <= WASM_OP_I64_LOAD32_U) \
  3669. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3670. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3671. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3672. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3673. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3674. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3675. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3676. #define GET_CONST_OFFSET(type, val) do { \
  3677. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3678. &val, &operand_offset, \
  3679. error_buf, error_buf_size))) \
  3680. goto fail; \
  3681. } while (0)
  3682. #define GET_CONST_F32_OFFSET(type, fval) do { \
  3683. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3684. &fval, &operand_offset, \
  3685. error_buf, error_buf_size))) \
  3686. goto fail; \
  3687. } while (0)
  3688. #define GET_CONST_F64_OFFSET(type, fval) do { \
  3689. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3690. &fval, &operand_offset, \
  3691. error_buf, error_buf_size))) \
  3692. goto fail; \
  3693. } while (0)
  3694. #define emit_operand(ctx, offset) do { \
  3695. wasm_loader_emit_int16(ctx, offset); \
  3696. LOG_OP("%d\t", offset); \
  3697. } while (0)
  3698. #define emit_byte(ctx, byte) do { \
  3699. wasm_loader_emit_uint8(ctx, byte); \
  3700. LOG_OP("%d\t", byte); \
  3701. } while (0)
  3702. #define emit_uint32(ctx, value) do { \
  3703. wasm_loader_emit_uint32(ctx, value); \
  3704. LOG_OP("%d\t", value); \
  3705. } while (0)
  3706. #define emit_leb() do { \
  3707. wasm_loader_emit_leb(loader_ctx, p_org, p); \
  3708. } while (0)
  3709. static bool
  3710. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3711. {
  3712. if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
  3713. return false;
  3714. memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
  3715. ctx->p_code_compiled_end = ctx->p_code_compiled +
  3716. ctx->code_compiled_size;
  3717. /* clean up frame ref */
  3718. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3719. ctx->frame_ref = ctx->frame_ref_bottom;
  3720. ctx->stack_cell_num = 0;
  3721. /* clean up frame csp */
  3722. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3723. ctx->frame_csp = ctx->frame_csp_bottom;
  3724. ctx->csp_num = 0;
  3725. ctx->max_csp_num = 0;
  3726. /* clean up frame offset */
  3727. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3728. ctx->frame_offset = ctx->frame_offset_bottom;
  3729. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3730. /* init preserved local offsets */
  3731. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3732. /* const buf is reserved */
  3733. return true;
  3734. }
  3735. static void
  3736. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  3737. {
  3738. if (ctx->p_code_compiled) {
  3739. *(uint32*)(ctx->p_code_compiled) = value;
  3740. ctx->p_code_compiled += sizeof(uint32);
  3741. }
  3742. else
  3743. ctx->code_compiled_size += sizeof(uint32);
  3744. }
  3745. static void
  3746. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3747. {
  3748. if (ctx->p_code_compiled) {
  3749. *(int16*)(ctx->p_code_compiled) = value;
  3750. ctx->p_code_compiled += sizeof(int16);
  3751. }
  3752. else
  3753. ctx->code_compiled_size += sizeof(int16);
  3754. }
  3755. static void
  3756. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3757. {
  3758. if (ctx->p_code_compiled) {
  3759. *(ctx->p_code_compiled) = value;
  3760. ctx->p_code_compiled += sizeof(uint8);
  3761. }
  3762. else
  3763. ctx->code_compiled_size += sizeof(uint8);
  3764. }
  3765. static void
  3766. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  3767. {
  3768. if (ctx->p_code_compiled) {
  3769. *(uint8**)(ctx->p_code_compiled) = value;
  3770. ctx->p_code_compiled += sizeof(void *);
  3771. }
  3772. else
  3773. ctx->code_compiled_size += sizeof(void *);
  3774. }
  3775. static void
  3776. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  3777. {
  3778. if (ctx->p_code_compiled) {
  3779. ctx->p_code_compiled -= size;
  3780. }
  3781. else
  3782. ctx->code_compiled_size -= size;
  3783. }
  3784. static void
  3785. wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
  3786. {
  3787. if (ctx->p_code_compiled) {
  3788. bh_memcpy_s(ctx->p_code_compiled,
  3789. ctx->p_code_compiled_end - ctx->p_code_compiled,
  3790. start, end - start);
  3791. ctx->p_code_compiled += (end - start);
  3792. }
  3793. else {
  3794. ctx->code_compiled_size += (end - start);
  3795. }
  3796. }
  3797. static bool
  3798. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  3799. uint32 local_index, uint32 local_type, bool *preserved,
  3800. char *error_buf, uint32 error_buf_size)
  3801. {
  3802. uint32 i = 0;
  3803. int16 preserved_offset = (int16)local_index;
  3804. *preserved = false;
  3805. while (i < loader_ctx->stack_cell_num) {
  3806. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3807. /* move previous local into dynamic space before a set/tee_local opcode */
  3808. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  3809. if (!(*preserved)) {
  3810. *preserved = true;
  3811. skip_label();
  3812. preserved_offset = loader_ctx->preserved_local_offset;
  3813. if (loader_ctx->p_code_compiled) {
  3814. bh_assert(preserved_offset != (int16)local_index);
  3815. }
  3816. if (local_type == VALUE_TYPE_I32
  3817. || local_type == VALUE_TYPE_F32) {
  3818. /* Only increase preserve offset in the second traversal */
  3819. if (loader_ctx->p_code_compiled)
  3820. loader_ctx->preserved_local_offset++;
  3821. emit_label(EXT_OP_COPY_STACK_TOP);
  3822. }
  3823. else {
  3824. if (loader_ctx->p_code_compiled)
  3825. loader_ctx->preserved_local_offset += 2;
  3826. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3827. }
  3828. emit_operand(loader_ctx, local_index);
  3829. emit_operand(loader_ctx, preserved_offset);
  3830. emit_label(opcode);
  3831. }
  3832. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  3833. }
  3834. if (cur_type == VALUE_TYPE_I32 || cur_type == VALUE_TYPE_F32)
  3835. i++;
  3836. else
  3837. i += 2;
  3838. }
  3839. return true;
  3840. #if WASM_ENABLE_ABS_LABEL_ADDR == 0
  3841. fail:
  3842. return false;
  3843. #endif
  3844. }
  3845. static bool
  3846. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  3847. char *error_buf, uint32 error_buf_size)
  3848. {
  3849. uint32 i = 0;
  3850. bool preserve_local;
  3851. /* preserve locals before blocks to ensure that "tee/set_local" inside
  3852. blocks will not influence the value of these locals */
  3853. while (i < loader_ctx->stack_cell_num) {
  3854. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  3855. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3856. if ((cur_offset < loader_ctx->start_dynamic_offset)
  3857. && (cur_offset >= 0)) {
  3858. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  3859. cur_type, &preserve_local,
  3860. error_buf, error_buf_size)))
  3861. return false;
  3862. }
  3863. if (cur_type == VALUE_TYPE_I32 || cur_type == VALUE_TYPE_F32) {
  3864. i++;
  3865. }
  3866. else {
  3867. i += 2;
  3868. }
  3869. }
  3870. return true;
  3871. }
  3872. static bool
  3873. add_label_patch_to_list(BranchBlock *frame_csp,
  3874. uint8 patch_type, uint8 *p_code_compiled,
  3875. char *error_buf, uint32 error_buf_size)
  3876. {
  3877. BranchBlockPatch *patch = loader_malloc
  3878. (sizeof(BranchBlockPatch), error_buf, error_buf_size);
  3879. if (!patch) {
  3880. return false;
  3881. }
  3882. patch->patch_type = patch_type;
  3883. patch->code_compiled = p_code_compiled;
  3884. if (!frame_csp->patch_list) {
  3885. frame_csp->patch_list = patch;
  3886. patch->next = NULL;
  3887. }
  3888. else {
  3889. patch->next = frame_csp->patch_list;
  3890. frame_csp->patch_list = patch;
  3891. }
  3892. return true;
  3893. }
  3894. static void
  3895. apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
  3896. uint8 patch_type)
  3897. {
  3898. BranchBlock *frame_csp = ctx->frame_csp - depth;
  3899. BranchBlockPatch *node = frame_csp->patch_list;
  3900. BranchBlockPatch *node_prev = NULL, *node_next;
  3901. if (!ctx->p_code_compiled)
  3902. return;
  3903. while (node) {
  3904. node_next = node->next;
  3905. if (node->patch_type == patch_type) {
  3906. *((uint8**)node->code_compiled) = ctx->p_code_compiled;
  3907. if (node_prev == NULL) {
  3908. frame_csp->patch_list = node_next;
  3909. }
  3910. else {
  3911. node_prev->next = node_next;
  3912. }
  3913. wasm_runtime_free(node);
  3914. }
  3915. else {
  3916. node_prev = node;
  3917. }
  3918. node = node_next;
  3919. }
  3920. }
  3921. static bool
  3922. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  3923. char *error_buf, uint32 error_buf_size)
  3924. {
  3925. /* br info layout:
  3926. * a) arity of target block
  3927. * b) total cell num of arity values
  3928. * c) each arity value's cell num
  3929. * d) each arity value's src frame offset
  3930. * e) each arity values's dst dynamic offset
  3931. * f) branch target address
  3932. *
  3933. * Note: b-e are omitted when arity is 0 so that
  3934. * interpreter can recover the br info quickly.
  3935. */
  3936. BlockType *block_type = &frame_csp->block_type;
  3937. uint8 *types = NULL, cell;
  3938. uint32 arity = 0;
  3939. int32 i;
  3940. int16 *frame_offset = ctx->frame_offset;
  3941. uint16 dynamic_offset;
  3942. /* Note: loop's arity is different from if and block. loop's arity is
  3943. * its parameter count while if and block arity is result count.
  3944. */
  3945. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  3946. arity = block_type_get_param_types(block_type, &types);
  3947. else
  3948. arity = block_type_get_result_types(block_type, &types);
  3949. /* Part a */
  3950. emit_uint32(ctx, arity);
  3951. if (arity) {
  3952. /* Part b */
  3953. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  3954. /* Part c */
  3955. for (i = (int32)arity - 1; i >= 0; i--) {
  3956. cell = wasm_value_type_cell_num(types[i]);
  3957. emit_byte(ctx, cell);
  3958. }
  3959. /* Part d */
  3960. for (i = (int32)arity - 1; i >= 0; i--) {
  3961. cell = wasm_value_type_cell_num(types[i]);
  3962. frame_offset -= cell;
  3963. emit_operand(ctx, *(int16*)(frame_offset));
  3964. }
  3965. /* Part e */
  3966. dynamic_offset = frame_csp->dynamic_offset
  3967. + wasm_get_cell_num(types, arity);
  3968. for (i = (int32)arity - 1; i >= 0; i--) {
  3969. cell = wasm_value_type_cell_num(types[i]);
  3970. dynamic_offset -= cell;
  3971. emit_operand(ctx, dynamic_offset);
  3972. }
  3973. }
  3974. /* Part f */
  3975. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  3976. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  3977. }
  3978. else {
  3979. if (!add_label_patch_to_list(frame_csp, PATCH_END,
  3980. ctx->p_code_compiled,
  3981. error_buf, error_buf_size))
  3982. return false;
  3983. /* label address, to be patched */
  3984. wasm_loader_emit_ptr(ctx, NULL);
  3985. }
  3986. return true;
  3987. }
  3988. static bool
  3989. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3990. bool disable_emit, int16 operand_offset,
  3991. char *error_buf, uint32 error_buf_size)
  3992. {
  3993. if (type == VALUE_TYPE_VOID)
  3994. return true;
  3995. /* only check memory overflow in first traverse */
  3996. if (ctx->p_code_compiled == NULL) {
  3997. if (!check_offset_push(ctx, error_buf, error_buf_size))
  3998. return false;
  3999. }
  4000. if (disable_emit)
  4001. *(ctx->frame_offset)++ = operand_offset;
  4002. else {
  4003. emit_operand(ctx, ctx->dynamic_offset);
  4004. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4005. ctx->dynamic_offset++;
  4006. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  4007. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4008. }
  4009. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  4010. return true;
  4011. if (ctx->p_code_compiled == NULL) {
  4012. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4013. return false;
  4014. }
  4015. ctx->frame_offset++;
  4016. if (!disable_emit) {
  4017. ctx->dynamic_offset++;
  4018. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  4019. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4020. }
  4021. return true;
  4022. }
  4023. /* This function should be in front of wasm_loader_pop_frame_ref
  4024. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4025. will be modified by wasm_loader_pop_frame_ref */
  4026. static bool
  4027. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4028. char *error_buf, uint32 error_buf_size)
  4029. {
  4030. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4031. then current block is the function block */
  4032. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4033. BranchBlock *cur_block = ctx->frame_csp - depth;
  4034. int32 available_stack_cell = (int32)
  4035. (ctx->stack_cell_num - cur_block->stack_cell_num);
  4036. /* Directly return success if current block is in stack
  4037. * polymorphic state while stack is empty. */
  4038. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4039. return true;
  4040. if (type == VALUE_TYPE_VOID)
  4041. return true;
  4042. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) {
  4043. /* Check the offset stack bottom to ensure the frame offset
  4044. stack will not go underflow. But we don't thrown error
  4045. and return true here, because the error msg should be
  4046. given in wasm_loader_pop_frame_ref */
  4047. if (!check_offset_pop(ctx, 1))
  4048. return true;
  4049. ctx->frame_offset -= 1;
  4050. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4051. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4052. ctx->dynamic_offset -= 1;
  4053. }
  4054. else {
  4055. if (!check_offset_pop(ctx, 2))
  4056. return true;
  4057. ctx->frame_offset -= 2;
  4058. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4059. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4060. ctx->dynamic_offset -= 2;
  4061. }
  4062. emit_operand(ctx, *(ctx->frame_offset));
  4063. return true;
  4064. }
  4065. static bool
  4066. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4067. uint8 type_push, uint8 type_pop,
  4068. bool disable_emit, int16 operand_offset,
  4069. char *error_buf, uint32 error_buf_size)
  4070. {
  4071. for (int i = 0; i < pop_cnt; i++) {
  4072. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size))
  4073. return false;
  4074. }
  4075. if (!wasm_loader_push_frame_offset(ctx, type_push,
  4076. disable_emit, operand_offset,
  4077. error_buf, error_buf_size))
  4078. return false;
  4079. return true;
  4080. }
  4081. static bool
  4082. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4083. bool disable_emit, int16 operand_offset,
  4084. char *error_buf, uint32 error_buf_size)
  4085. {
  4086. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4087. error_buf, error_buf_size)))
  4088. return false;
  4089. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4090. return false;
  4091. return true;
  4092. }
  4093. static bool
  4094. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4095. char *error_buf, uint32 error_buf_size)
  4096. {
  4097. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4098. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4099. return false;
  4100. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4101. return false;
  4102. return true;
  4103. }
  4104. static bool
  4105. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4106. uint8 type_push, uint8 type_pop,
  4107. bool disable_emit, int16 operand_offset,
  4108. char *error_buf, uint32 error_buf_size)
  4109. {
  4110. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  4111. disable_emit, operand_offset,
  4112. error_buf, error_buf_size))
  4113. return false;
  4114. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  4115. error_buf, error_buf_size))
  4116. return false;
  4117. return true;
  4118. }
  4119. static bool
  4120. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type,
  4121. void *value, int16 *offset,
  4122. char *error_buf, uint32 error_buf_size)
  4123. {
  4124. int16 operand_offset = 0;
  4125. Const *c;
  4126. for (c = (Const *)ctx->const_buf;
  4127. (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) {
  4128. if ((type == c->value_type)
  4129. && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64)
  4130. || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32)
  4131. || (type == VALUE_TYPE_F64
  4132. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  4133. || (type == VALUE_TYPE_F32
  4134. && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  4135. operand_offset = c->slot_index;
  4136. break;
  4137. }
  4138. if (c->value_type == VALUE_TYPE_I64
  4139. || c->value_type == VALUE_TYPE_F64)
  4140. operand_offset += 2;
  4141. else
  4142. operand_offset += 1;
  4143. }
  4144. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  4145. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  4146. MEM_REALLOC(ctx->const_buf,
  4147. ctx->const_buf_size,
  4148. ctx->const_buf_size + 4 * sizeof(Const));
  4149. ctx->const_buf_size += 4 * sizeof(Const);
  4150. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  4151. }
  4152. c->value_type = type;
  4153. switch (type) {
  4154. case VALUE_TYPE_F64:
  4155. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64));
  4156. ctx->const_cell_num += 2;
  4157. /* The const buf will be reversed, we use the second cell */
  4158. /* of the i64/f64 const so the finnal offset is corrent */
  4159. operand_offset ++;
  4160. break;
  4161. case VALUE_TYPE_I64:
  4162. c->value.i64 = *(int64*)value;
  4163. ctx->const_cell_num += 2;
  4164. operand_offset ++;
  4165. break;
  4166. case VALUE_TYPE_F32:
  4167. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32));
  4168. ctx->const_cell_num ++;
  4169. break;
  4170. case VALUE_TYPE_I32:
  4171. c->value.i32 = *(int32*)value;
  4172. ctx->const_cell_num ++;
  4173. break;
  4174. default:
  4175. break;
  4176. }
  4177. c->slot_index = operand_offset;
  4178. ctx->num_const ++;
  4179. LOG_OP("#### new const [%d]: %ld\n",
  4180. ctx->num_const, (int64)c->value.i64);
  4181. }
  4182. /* use negetive index for const */
  4183. operand_offset = -(operand_offset + 1);
  4184. *offset = operand_offset;
  4185. return true;
  4186. fail:
  4187. return false;
  4188. }
  4189. /*
  4190. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4191. -- Mostly used for the binary / compare operation
  4192. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4193. -- Mostly used in block / control instructions
  4194. The POP will always emit the offset on the top of the frame_offset stack
  4195. PUSH can be used in two ways:
  4196. 1. directly PUSH:
  4197. PUSH_XXX();
  4198. will allocate a dynamic space and emit
  4199. 2. silent PUSH:
  4200. operand_offset = xxx; disable_emit = true;
  4201. PUSH_XXX();
  4202. only push the frame_offset stack, no emit
  4203. */
  4204. #define PUSH_I32() do { \
  4205. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4206. disable_emit, operand_offset,\
  4207. error_buf, error_buf_size)) \
  4208. goto fail; \
  4209. } while (0)
  4210. #define PUSH_F32() do { \
  4211. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4212. disable_emit, operand_offset,\
  4213. error_buf, error_buf_size)) \
  4214. goto fail; \
  4215. } while (0)
  4216. #define PUSH_I64() do { \
  4217. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4218. disable_emit, operand_offset,\
  4219. error_buf, error_buf_size)) \
  4220. goto fail; \
  4221. } while (0)
  4222. #define PUSH_F64() do { \
  4223. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4224. disable_emit, operand_offset,\
  4225. error_buf, error_buf_size)) \
  4226. goto fail; \
  4227. } while (0)
  4228. #define POP_I32() do { \
  4229. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4230. error_buf, error_buf_size)) \
  4231. goto fail; \
  4232. } while (0)
  4233. #define POP_F32() do { \
  4234. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4235. error_buf, error_buf_size)) \
  4236. goto fail; \
  4237. } while (0)
  4238. #define POP_I64() do { \
  4239. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4240. error_buf, error_buf_size)) \
  4241. goto fail; \
  4242. } while (0)
  4243. #define POP_F64() do { \
  4244. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4245. error_buf, error_buf_size)) \
  4246. goto fail; \
  4247. } while (0)
  4248. #define PUSH_OFFSET_TYPE(type) do { \
  4249. if (!(wasm_loader_push_frame_offset(loader_ctx, type, \
  4250. disable_emit, operand_offset, \
  4251. error_buf, error_buf_size))) \
  4252. goto fail; \
  4253. } while (0)
  4254. #define POP_OFFSET_TYPE(type) do { \
  4255. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \
  4256. error_buf, error_buf_size))) \
  4257. goto fail; \
  4258. } while (0)
  4259. #define POP_AND_PUSH(type_pop, type_push) do { \
  4260. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \
  4261. type_push, type_pop, \
  4262. disable_emit, operand_offset, \
  4263. error_buf, error_buf_size))) \
  4264. goto fail; \
  4265. } while (0)
  4266. /* type of POPs should be the same */
  4267. #define POP2_AND_PUSH(type_pop, type_push) do { \
  4268. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \
  4269. type_push, type_pop, \
  4270. disable_emit, operand_offset, \
  4271. error_buf, error_buf_size))) \
  4272. goto fail; \
  4273. } while (0)
  4274. #else /* WASM_ENABLE_FAST_INTERP */
  4275. #define PUSH_I32() do { \
  4276. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4277. error_buf, error_buf_size))) \
  4278. goto fail; \
  4279. } while (0)
  4280. #define PUSH_F32() do { \
  4281. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4282. error_buf, error_buf_size))) \
  4283. goto fail; \
  4284. } while (0)
  4285. #define PUSH_I64() do { \
  4286. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4287. error_buf, error_buf_size))) \
  4288. goto fail; \
  4289. } while (0)
  4290. #define PUSH_F64() do { \
  4291. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4292. error_buf, error_buf_size))) \
  4293. goto fail; \
  4294. } while (0)
  4295. #define POP_I32() do { \
  4296. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4297. error_buf, error_buf_size))) \
  4298. goto fail; \
  4299. } while (0)
  4300. #define POP_F32() do { \
  4301. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4302. error_buf, error_buf_size))) \
  4303. goto fail; \
  4304. } while (0)
  4305. #define POP_I64() do { \
  4306. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4307. error_buf, error_buf_size))) \
  4308. goto fail; \
  4309. } while (0)
  4310. #define POP_F64() do { \
  4311. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4312. error_buf, error_buf_size))) \
  4313. goto fail; \
  4314. } while (0)
  4315. #define POP_AND_PUSH(type_pop, type_push) do { \
  4316. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \
  4317. type_push, type_pop, \
  4318. error_buf, error_buf_size))) \
  4319. goto fail; \
  4320. } while (0)
  4321. /* type of POPs should be the same */
  4322. #define POP2_AND_PUSH(type_pop, type_push) do { \
  4323. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \
  4324. type_push, type_pop, \
  4325. error_buf, error_buf_size))) \
  4326. goto fail; \
  4327. } while (0)
  4328. #endif /* WASM_ENABLE_FAST_INTERP */
  4329. #if WASM_ENABLE_FAST_INTERP != 0
  4330. static bool
  4331. reserve_block_ret(WASMLoaderContext *loader_ctx,
  4332. uint8 opcode, bool disable_emit,
  4333. char *error_buf, uint32 error_buf_size)
  4334. {
  4335. int16 operand_offset = 0;
  4336. BranchBlock *block = (opcode == WASM_OP_ELSE) ?
  4337. loader_ctx->frame_csp - 1 : loader_ctx->frame_csp;
  4338. BlockType *block_type = &block->block_type;
  4339. uint8 *return_types = NULL;
  4340. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4341. int32 i = 0;
  4342. int16 dynamic_offset, dynamic_offset_org,
  4343. *frame_offset = NULL, *frame_offset_org = NULL;
  4344. return_count = block_type_get_result_types(block_type, &return_types);
  4345. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4346. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4347. if (return_count == 1) {
  4348. uint8 cell = wasm_value_type_cell_num(return_types[0]);
  4349. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4350. /* insert op_copy before else opcode */
  4351. if (opcode == WASM_OP_ELSE)
  4352. skip_label();
  4353. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP : EXT_OP_COPY_STACK_TOP_I64);
  4354. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4355. emit_operand(loader_ctx, block->dynamic_offset);
  4356. if (opcode == WASM_OP_ELSE) {
  4357. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4358. }
  4359. else {
  4360. loader_ctx->frame_offset -= cell;
  4361. loader_ctx->dynamic_offset = block->dynamic_offset;
  4362. PUSH_OFFSET_TYPE(return_types[0]);
  4363. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4364. }
  4365. if (opcode == WASM_OP_ELSE)
  4366. emit_label(opcode);
  4367. }
  4368. return true;
  4369. }
  4370. /* Copy stack top values to block's results which are in dynamic space.
  4371. * The instruction format:
  4372. * Part a: values count
  4373. * Part b: all values total cell num
  4374. * Part c: each value's cell_num, src offset and dst offset
  4375. * Part d: each value's src offset and dst offset
  4376. * Part e: each value's dst offset
  4377. */
  4378. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4379. dynamic_offset = dynamic_offset_org =
  4380. block->dynamic_offset
  4381. + wasm_get_cell_num(return_types, return_count);
  4382. /* First traversal to get the count of values needed to be copied. */
  4383. for (i = (int32)return_count - 1; i >= 0; i--) {
  4384. uint8 cells = wasm_value_type_cell_num(return_types[i]);
  4385. frame_offset -= cells;
  4386. dynamic_offset -= cells;
  4387. if (dynamic_offset != *frame_offset) {
  4388. value_count++;
  4389. total_cel_num += cells;
  4390. }
  4391. }
  4392. if (value_count) {
  4393. uint32 j = 0;
  4394. uint8 *emit_data = NULL, *cells = NULL;
  4395. int16 *src_offsets = NULL;
  4396. uint16 *dst_offsets = NULL;
  4397. uint64 size = (uint64)value_count * (sizeof(*cells)
  4398. + sizeof(*src_offsets)
  4399. + sizeof(*dst_offsets));
  4400. /* Allocate memory for the emit data */
  4401. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4402. return false;
  4403. cells = emit_data;
  4404. src_offsets = (int16 *)(cells + value_count);
  4405. dst_offsets = (uint16 *)(src_offsets + value_count);
  4406. /* insert op_copy before else opcode */
  4407. if (opcode == WASM_OP_ELSE)
  4408. skip_label();
  4409. emit_label(EXT_OP_COPY_STACK_VALUES);
  4410. /* Part a) */
  4411. emit_uint32(loader_ctx, value_count);
  4412. /* Part b) */
  4413. emit_uint32(loader_ctx, total_cel_num);
  4414. /* Second traversal to get each value's cell num, src offset and dst offset. */
  4415. frame_offset = frame_offset_org;
  4416. dynamic_offset = dynamic_offset_org;
  4417. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4418. uint8 cell = wasm_value_type_cell_num(return_types[i]);
  4419. frame_offset -= cell;
  4420. dynamic_offset -= cell;
  4421. if (dynamic_offset != *frame_offset) {
  4422. /* cell num */
  4423. cells[j] = cell;
  4424. /* src offset */
  4425. src_offsets[j] = *frame_offset;
  4426. /* dst offset */
  4427. dst_offsets[j] = dynamic_offset;
  4428. j++;
  4429. }
  4430. if (opcode == WASM_OP_ELSE) {
  4431. *frame_offset = dynamic_offset;
  4432. }
  4433. else {
  4434. loader_ctx->frame_offset = frame_offset;
  4435. loader_ctx->dynamic_offset = dynamic_offset;
  4436. PUSH_OFFSET_TYPE(return_types[i]);
  4437. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4438. loader_ctx->frame_offset = frame_offset_org;
  4439. loader_ctx->dynamic_offset = dynamic_offset_org;
  4440. }
  4441. }
  4442. bh_assert(j == value_count);
  4443. /* Emit the cells, src_offsets and dst_offsets */
  4444. for (j = 0; j < value_count; j++)
  4445. emit_byte(loader_ctx, cells[j]);
  4446. for (j = 0; j < value_count; j++)
  4447. emit_operand(loader_ctx, src_offsets[j]);
  4448. for (j = 0; j < value_count; j++)
  4449. emit_operand(loader_ctx, dst_offsets[j]);
  4450. if (opcode == WASM_OP_ELSE)
  4451. emit_label(opcode);
  4452. wasm_runtime_free(emit_data);
  4453. }
  4454. return true;
  4455. fail:
  4456. return false;
  4457. }
  4458. #endif /* WASM_ENABLE_FAST_INTERP */
  4459. #define RESERVE_BLOCK_RET() do { \
  4460. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \
  4461. error_buf, error_buf_size)) \
  4462. goto fail; \
  4463. } while (0)
  4464. #define PUSH_TYPE(type) do { \
  4465. if (!(wasm_loader_push_frame_ref(loader_ctx, type, \
  4466. error_buf, error_buf_size))) \
  4467. goto fail; \
  4468. } while (0)
  4469. #define POP_TYPE(type) do { \
  4470. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \
  4471. error_buf, error_buf_size))) \
  4472. goto fail; \
  4473. } while (0)
  4474. #define PUSH_CSP(label_type, block_type, _start_addr) do { \
  4475. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4476. _start_addr, error_buf, \
  4477. error_buf_size)) \
  4478. goto fail; \
  4479. } while (0)
  4480. #define POP_CSP() do { \
  4481. if (!wasm_loader_pop_frame_csp(loader_ctx, \
  4482. error_buf, error_buf_size)) \
  4483. goto fail; \
  4484. } while (0)
  4485. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
  4486. read_leb_uint32(p, p_end, local_idx); \
  4487. if (local_idx >= param_count + local_count) { \
  4488. set_error_buf(error_buf, error_buf_size, \
  4489. "unknown local"); \
  4490. goto fail; \
  4491. } \
  4492. local_type = local_idx < param_count \
  4493. ? param_types[local_idx] \
  4494. : local_types[local_idx - param_count]; \
  4495. local_offset = local_offsets[local_idx]; \
  4496. } while (0)
  4497. #define CHECK_BR(depth) do { \
  4498. if (!wasm_loader_check_br(loader_ctx, depth, \
  4499. error_buf, error_buf_size)) \
  4500. goto fail; \
  4501. } while (0)
  4502. static bool
  4503. check_memory(WASMModule *module,
  4504. char *error_buf, uint32 error_buf_size)
  4505. {
  4506. if (module->memory_count == 0
  4507. && module->import_memory_count == 0) {
  4508. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4509. return false;
  4510. }
  4511. return true;
  4512. }
  4513. #define CHECK_MEMORY() do { \
  4514. if (!check_memory(module, error_buf, error_buf_size)) \
  4515. goto fail; \
  4516. } while (0)
  4517. static bool
  4518. check_memory_access_align(uint8 opcode, uint32 align,
  4519. char *error_buf, uint32 error_buf_size)
  4520. {
  4521. uint8 mem_access_aligns[] = {
  4522. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  4523. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  4524. };
  4525. bh_assert(opcode >= WASM_OP_I32_LOAD
  4526. && opcode <= WASM_OP_I64_STORE32);
  4527. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  4528. set_error_buf(error_buf, error_buf_size,
  4529. "alignment must not be larger than natural");
  4530. return false;
  4531. }
  4532. return true;
  4533. }
  4534. #if WASM_ENABLE_SHARED_MEMORY != 0
  4535. static bool
  4536. check_memory_align_equal(uint8 opcode, uint32 align,
  4537. char *error_buf, uint32 error_buf_size)
  4538. {
  4539. uint8 wait_notify_aligns[] = {2, 2, 3};
  4540. uint8 mem_access_aligns[] = {
  4541. 2, 3, 0, 1, 0, 1, 2,
  4542. };
  4543. uint8 expect;
  4544. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  4545. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  4546. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  4547. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  4548. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  4549. }
  4550. else {
  4551. /* 7 opcodes in every group */
  4552. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  4553. }
  4554. if (align != expect) {
  4555. set_error_buf(error_buf, error_buf_size,
  4556. "alignment isn't equal to natural");
  4557. return false;
  4558. }
  4559. return true;
  4560. }
  4561. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  4562. static bool
  4563. is_value_type(uint8 type)
  4564. {
  4565. return type == VALUE_TYPE_I32 ||
  4566. type == VALUE_TYPE_I64 ||
  4567. type == VALUE_TYPE_F32 ||
  4568. type == VALUE_TYPE_F64 ||
  4569. type == VALUE_TYPE_VOID;
  4570. }
  4571. static bool
  4572. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  4573. char *error_buf, uint32 error_buf_size)
  4574. {
  4575. BranchBlock *target_block, *cur_block;
  4576. BlockType *target_block_type;
  4577. uint8 *types = NULL, *frame_ref;
  4578. uint32 arity = 0;
  4579. int32 i, available_stack_cell;
  4580. uint16 cell_num;
  4581. if (loader_ctx->csp_num < depth + 1) {
  4582. set_error_buf(error_buf, error_buf_size,
  4583. "unknown label, "
  4584. "unexpected end of section or function");
  4585. return false;
  4586. }
  4587. cur_block = loader_ctx->frame_csp - 1;
  4588. target_block = loader_ctx->frame_csp - (depth + 1);
  4589. target_block_type = &target_block->block_type;
  4590. frame_ref = loader_ctx->frame_ref;
  4591. /* Note: loop's arity is different from if and block. loop's arity is
  4592. * its parameter count while if and block arity is result count.
  4593. */
  4594. if (target_block->label_type == LABEL_TYPE_LOOP)
  4595. arity = block_type_get_param_types(target_block_type, &types);
  4596. else
  4597. arity = block_type_get_result_types(target_block_type, &types);
  4598. /* If the stack is in polymorphic state, just clear the stack
  4599. * and then re-push the values to make the stack top values
  4600. * match block type. */
  4601. if (cur_block->is_stack_polymorphic) {
  4602. for (i = (int32)arity -1; i >= 0; i--) {
  4603. #if WASM_ENABLE_FAST_INTERP != 0
  4604. POP_OFFSET_TYPE(types[i]);
  4605. #endif
  4606. POP_TYPE(types[i]);
  4607. }
  4608. for (i = 0; i < (int32)arity; i++) {
  4609. #if WASM_ENABLE_FAST_INTERP != 0
  4610. bool disable_emit = true;
  4611. int16 operand_offset = 0;
  4612. PUSH_OFFSET_TYPE(types[i]);
  4613. #endif
  4614. PUSH_TYPE(types[i]);
  4615. }
  4616. return true;
  4617. }
  4618. available_stack_cell = (int32)
  4619. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4620. /* Check stack top values match target block type */
  4621. for (i = (int32)arity -1; i >= 0; i--) {
  4622. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4623. types[i],
  4624. error_buf, error_buf_size))
  4625. return false;
  4626. cell_num = wasm_value_type_cell_num(types[i]);
  4627. frame_ref -= cell_num;
  4628. available_stack_cell -= cell_num;
  4629. }
  4630. return true;
  4631. fail:
  4632. return false;
  4633. }
  4634. static BranchBlock *
  4635. check_branch_block(WASMLoaderContext *loader_ctx,
  4636. uint8 **p_buf, uint8 *buf_end,
  4637. char *error_buf, uint32 error_buf_size)
  4638. {
  4639. uint8 *p = *p_buf, *p_end = buf_end;
  4640. BranchBlock *frame_csp_tmp;
  4641. uint32 depth;
  4642. read_leb_uint32(p, p_end, depth);
  4643. CHECK_BR(depth);
  4644. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  4645. #if WASM_ENABLE_FAST_INTERP != 0
  4646. emit_br_info(frame_csp_tmp);
  4647. #endif
  4648. *p_buf = p;
  4649. return frame_csp_tmp;
  4650. fail:
  4651. return NULL;
  4652. }
  4653. static bool
  4654. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  4655. char *error_buf, uint32 error_buf_size)
  4656. {
  4657. BlockType *block_type = &block->block_type;
  4658. uint8 *return_types = NULL;
  4659. uint32 return_count = 0;
  4660. int32 available_stack_cell, return_cell_num, i;
  4661. uint8 *frame_ref = NULL;
  4662. available_stack_cell = (int32)
  4663. (loader_ctx->stack_cell_num
  4664. - block->stack_cell_num);
  4665. return_count = block_type_get_result_types(block_type, &return_types);
  4666. return_cell_num = return_count > 0 ?
  4667. wasm_get_cell_num(return_types, return_count) : 0;
  4668. /* If the stack is in polymorphic state, just clear the stack
  4669. * and then re-push the values to make the stack top values
  4670. * match block type. */
  4671. if (block->is_stack_polymorphic) {
  4672. for (i = (int32)return_count -1; i >= 0; i--) {
  4673. #if WASM_ENABLE_FAST_INTERP != 0
  4674. POP_OFFSET_TYPE(return_types[i]);
  4675. #endif
  4676. POP_TYPE(return_types[i]);
  4677. }
  4678. /* Check stack is empty */
  4679. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  4680. set_error_buf(error_buf, error_buf_size,
  4681. "type mismatch: stack size does not match block type");
  4682. goto fail;
  4683. }
  4684. for (i = 0; i < (int32)return_count; i++) {
  4685. #if WASM_ENABLE_FAST_INTERP != 0
  4686. bool disable_emit = true;
  4687. int16 operand_offset = 0;
  4688. PUSH_OFFSET_TYPE(return_types[i]);
  4689. #endif
  4690. PUSH_TYPE(return_types[i]);
  4691. }
  4692. return true;
  4693. }
  4694. /* Check stack cell num equals return cell num */
  4695. if (available_stack_cell != return_cell_num) {
  4696. set_error_buf(error_buf, error_buf_size,
  4697. "type mismatch: stack size does not match block type");
  4698. goto fail;
  4699. }
  4700. /* Check stack values match return types */
  4701. frame_ref = loader_ctx->frame_ref;
  4702. for (i = (int32)return_count -1; i >= 0; i--) {
  4703. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4704. return_types[i],
  4705. error_buf, error_buf_size))
  4706. return false;
  4707. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  4708. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  4709. }
  4710. return true;
  4711. fail:
  4712. return false;
  4713. }
  4714. #if WASM_ENABLE_FAST_INTERP != 0
  4715. /* Copy parameters to dynamic space.
  4716. * 1) POP original parameter out;
  4717. * 2) Push and copy original values to dynamic space.
  4718. * The copy instruction format:
  4719. * Part a: param count
  4720. * Part b: all param total cell num
  4721. * Part c: each param's cell_num, src offset and dst offset
  4722. * Part d: each param's src offset
  4723. * Part e: each param's dst offset
  4724. */
  4725. static bool
  4726. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  4727. char* error_buf, uint32 error_buf_size)
  4728. {
  4729. int16 *frame_offset = NULL;
  4730. uint8 *cells = NULL, cell;
  4731. int16 *src_offsets = NULL;
  4732. uint8 *emit_data = NULL;
  4733. uint32 i;
  4734. BranchBlock *block = loader_ctx->frame_csp - 1;
  4735. BlockType *block_type = &block->block_type;
  4736. WASMType *wasm_type = block_type->u.type;
  4737. uint32 param_count = block_type->u.type->param_count;
  4738. int16 condition_offset = 0;
  4739. bool disable_emit = false;
  4740. int16 operand_offset = 0;
  4741. uint64 size = (uint64)param_count * (sizeof(*cells)
  4742. + sizeof(*src_offsets));
  4743. /* For if block, we also need copy the condition operand offset. */
  4744. if (is_if_block)
  4745. size += sizeof(*cells) + sizeof(*src_offsets);
  4746. /* Allocate memory for the emit data */
  4747. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4748. return false;
  4749. cells = emit_data;
  4750. src_offsets = (int16 *)(cells + param_count);
  4751. if (is_if_block)
  4752. condition_offset = *loader_ctx->frame_offset;
  4753. /* POP original parameter out */
  4754. for (i = 0; i < param_count; i++) {
  4755. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  4756. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4757. }
  4758. frame_offset = loader_ctx->frame_offset;
  4759. /* Get each param's cell num and src offset */
  4760. for (i = 0; i < param_count; i++) {
  4761. cell = wasm_value_type_cell_num(wasm_type->types[i]);
  4762. cells[i] = cell;
  4763. src_offsets[i] = *frame_offset;
  4764. frame_offset += cell;
  4765. }
  4766. /* emit copy instruction */
  4767. emit_label(EXT_OP_COPY_STACK_VALUES);
  4768. /* Part a) */
  4769. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  4770. /* Part b) */
  4771. emit_uint32(loader_ctx, is_if_block ?
  4772. wasm_type->param_cell_num + 1 :
  4773. wasm_type->param_cell_num);
  4774. /* Part c) */
  4775. for (i = 0; i < param_count; i++)
  4776. emit_byte(loader_ctx, cells[i]);
  4777. if (is_if_block)
  4778. emit_byte(loader_ctx, 1);
  4779. /* Part d) */
  4780. for (i = 0; i < param_count; i++)
  4781. emit_operand(loader_ctx, src_offsets[i]);
  4782. if (is_if_block)
  4783. emit_operand(loader_ctx, condition_offset);
  4784. /* Part e) */
  4785. /* Push to dynamic space. The push will emit the dst offset. */
  4786. for (i = 0; i < param_count; i++)
  4787. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  4788. if (is_if_block)
  4789. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  4790. /* Free the emit data */
  4791. wasm_runtime_free(emit_data);
  4792. return true;
  4793. fail:
  4794. return false;
  4795. }
  4796. #endif
  4797. /* reset the stack to the state of before entering the last block */
  4798. #if WASM_ENABLE_FAST_INTERP != 0
  4799. #define RESET_STACK() do { \
  4800. loader_ctx->stack_cell_num = \
  4801. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4802. loader_ctx->frame_ref = \
  4803. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4804. loader_ctx->frame_offset = \
  4805. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  4806. } while (0)
  4807. #else
  4808. #define RESET_STACK() do { \
  4809. loader_ctx->stack_cell_num = \
  4810. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4811. loader_ctx->frame_ref = \
  4812. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4813. } while (0)
  4814. #endif
  4815. /* set current block's stack polymorphic state */
  4816. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \
  4817. BranchBlock *cur_block = loader_ctx->frame_csp - 1; \
  4818. cur_block->is_stack_polymorphic = flag; \
  4819. } while (0)
  4820. #define BLOCK_HAS_PARAM(block_type) \
  4821. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  4822. #define PRESERVE_LOCAL_FOR_BLOCK() do { \
  4823. if (!(preserve_local_for_block(loader_ctx, opcode, \
  4824. error_buf, error_buf_size))) { \
  4825. goto fail; \
  4826. } \
  4827. } while (0)
  4828. static bool
  4829. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4830. BlockAddr *block_addr_cache,
  4831. char *error_buf, uint32 error_buf_size)
  4832. {
  4833. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  4834. uint32 param_count, local_count, global_count;
  4835. uint8 *param_types, *local_types, local_type, global_type;
  4836. BlockType func_type;
  4837. uint16 *local_offsets, local_offset;
  4838. uint32 count, i, local_idx, global_idx, u32, align, mem_offset;
  4839. int32 i32, i32_const = 0;
  4840. int64 i64;
  4841. uint8 opcode, u8;
  4842. bool return_value = false;
  4843. WASMLoaderContext *loader_ctx;
  4844. BranchBlock *frame_csp_tmp;
  4845. #if WASM_ENABLE_BULK_MEMORY != 0
  4846. uint32 segment_index;
  4847. #endif
  4848. #if WASM_ENABLE_FAST_INTERP != 0
  4849. uint8 *func_const_end, *func_const;
  4850. int16 operand_offset;
  4851. uint8 last_op = 0;
  4852. bool disable_emit, preserve_local = false;
  4853. float32 f32;
  4854. float64 f64;
  4855. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  4856. func->param_cell_num,
  4857. func->local_cell_num,
  4858. func->ret_cell_num);
  4859. #endif
  4860. global_count = module->import_global_count + module->global_count;
  4861. param_count = func->func_type->param_count;
  4862. param_types = func->func_type->types;
  4863. func_type.is_value_type = false;
  4864. func_type.u.type = func->func_type;
  4865. local_count = func->local_count;
  4866. local_types = func->local_types;
  4867. local_offsets = func->local_offsets;
  4868. if (!(loader_ctx = wasm_loader_ctx_init(func))) {
  4869. set_error_buf(error_buf, error_buf_size,
  4870. "allocate memory failed");
  4871. goto fail;
  4872. }
  4873. #if WASM_ENABLE_FAST_INTERP != 0
  4874. re_scan:
  4875. if (loader_ctx->code_compiled_size > 0) {
  4876. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  4877. set_error_buf(error_buf, error_buf_size,
  4878. "allocate memory failed");
  4879. goto fail;
  4880. }
  4881. p = func->code;
  4882. func->code_compiled = loader_ctx->p_code_compiled;
  4883. }
  4884. #endif
  4885. PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p);
  4886. while (p < p_end) {
  4887. opcode = *p++;
  4888. #if WASM_ENABLE_FAST_INTERP != 0
  4889. p_org = p;
  4890. disable_emit = false;
  4891. emit_label(opcode);
  4892. #endif
  4893. switch (opcode) {
  4894. case WASM_OP_UNREACHABLE:
  4895. RESET_STACK();
  4896. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4897. break;
  4898. case WASM_OP_NOP:
  4899. #if WASM_ENABLE_FAST_INTERP != 0
  4900. skip_label();
  4901. #endif
  4902. break;
  4903. case WASM_OP_IF:
  4904. #if WASM_ENABLE_FAST_INTERP != 0
  4905. PRESERVE_LOCAL_FOR_BLOCK();
  4906. #endif
  4907. POP_I32();
  4908. goto handle_op_block_and_loop;
  4909. case WASM_OP_BLOCK:
  4910. case WASM_OP_LOOP:
  4911. #if WASM_ENABLE_FAST_INTERP != 0
  4912. PRESERVE_LOCAL_FOR_BLOCK();
  4913. #endif
  4914. handle_op_block_and_loop:
  4915. {
  4916. uint8 value_type;
  4917. BlockType block_type;
  4918. value_type = read_uint8(p);
  4919. if (is_value_type(value_type)) {
  4920. /* If the first byte is one of these special values:
  4921. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  4922. * the single return value. */
  4923. block_type.is_value_type = true;
  4924. block_type.u.value_type = value_type;
  4925. }
  4926. else {
  4927. uint32 type_index;
  4928. /* Resolve the leb128 encoded type index as block type */
  4929. p--;
  4930. read_leb_uint32(p, p_end, type_index);
  4931. if (type_index >= module->type_count) {
  4932. set_error_buf(error_buf, error_buf_size,
  4933. "unknown type");
  4934. goto fail;
  4935. }
  4936. block_type.is_value_type = false;
  4937. block_type.u.type = module->types[type_index];
  4938. #if WASM_ENABLE_FAST_INTERP == 0 \
  4939. && WASM_ENABLE_WAMR_COMPILER == 0 \
  4940. && WASM_ENABLE_JIT == 0
  4941. /* If block use type index as block type, change the opcode
  4942. * to new extended opcode so that interpreter can resolve the
  4943. * block quickly.
  4944. */
  4945. *(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  4946. #endif
  4947. }
  4948. /* Pop block parameters from stack */
  4949. if (BLOCK_HAS_PARAM(block_type)) {
  4950. WASMType *wasm_type = block_type.u.type;
  4951. for (i = 0; i < block_type.u.type->param_count; i++)
  4952. POP_TYPE(wasm_type->types[wasm_type->param_count - i - 1]);
  4953. }
  4954. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), block_type, p);
  4955. /* Pass parameters to block */
  4956. if (BLOCK_HAS_PARAM(block_type)) {
  4957. for (i = 0; i < block_type.u.type->param_count; i++)
  4958. PUSH_TYPE(block_type.u.type->types[i]);
  4959. }
  4960. #if WASM_ENABLE_FAST_INTERP != 0
  4961. if (opcode == WASM_OP_BLOCK) {
  4962. skip_label();
  4963. } else if (opcode == WASM_OP_LOOP) {
  4964. skip_label();
  4965. if (BLOCK_HAS_PARAM(block_type)) {
  4966. /* Make sure params are in dynamic space */
  4967. if (!copy_params_to_dynamic_space(loader_ctx,
  4968. false,
  4969. error_buf,
  4970. error_buf_size))
  4971. goto fail;
  4972. }
  4973. (loader_ctx->frame_csp - 1)->code_compiled =
  4974. loader_ctx->p_code_compiled;
  4975. } else if (opcode == WASM_OP_IF) {
  4976. /* If block has parameters, we should make sure they are in
  4977. * dynamic space. Otherwise, when else branch is missing,
  4978. * the later opcode may consume incorrect operand offset.
  4979. * Spec case:
  4980. * (func (export "params-id") (param i32) (result i32)
  4981. * (i32.const 1)
  4982. * (i32.const 2)
  4983. * (if (param i32 i32) (result i32 i32) (local.get 0) (then))
  4984. * (i32.add)
  4985. * )
  4986. *
  4987. * So we should emit a copy instruction before the if.
  4988. *
  4989. * And we also need to save the parameter offsets and
  4990. * recover them before entering else branch.
  4991. *
  4992. */
  4993. if (BLOCK_HAS_PARAM(block_type)) {
  4994. BranchBlock *block = loader_ctx->frame_csp - 1;
  4995. uint64 size;
  4996. /* skip the if condition operand offset */
  4997. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4998. /* skip the if label */
  4999. skip_label();
  5000. /* Emit a copy instruction */
  5001. if (!copy_params_to_dynamic_space(loader_ctx,
  5002. true,
  5003. error_buf,
  5004. error_buf_size))
  5005. goto fail;
  5006. /* Emit the if instruction */
  5007. emit_label(opcode);
  5008. /* Emit the new condition operand offset */
  5009. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5010. /* Save top param_count values of frame_offset stack, so that
  5011. * we can recover it before executing else branch */
  5012. size = sizeof(int16) *
  5013. (uint64)block_type.u.type->param_cell_num;
  5014. if (!(block->param_frame_offsets =
  5015. loader_malloc(size, error_buf, error_buf_size)))
  5016. goto fail;
  5017. bh_memcpy_s(block->param_frame_offsets,
  5018. size,
  5019. loader_ctx->frame_offset - size/sizeof(int16),
  5020. size);
  5021. }
  5022. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5023. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5024. }
  5025. #endif
  5026. break;
  5027. }
  5028. case WASM_OP_ELSE:
  5029. {
  5030. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5031. if (loader_ctx->csp_num < 2
  5032. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF) {
  5033. set_error_buf(error_buf, error_buf_size,
  5034. "opcode else found without matched opcode if");
  5035. goto fail;
  5036. }
  5037. /* check whether if branch's stack matches its result type */
  5038. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  5039. error_buf, error_buf_size))
  5040. goto fail;
  5041. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  5042. #if WASM_ENABLE_FAST_INTERP != 0
  5043. /* if the result of if branch is in local or const area, add a copy op */
  5044. RESERVE_BLOCK_RET();
  5045. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5046. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5047. #endif
  5048. RESET_STACK();
  5049. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5050. /* Pass parameters to if-false branch */
  5051. if (BLOCK_HAS_PARAM(block_type)) {
  5052. for (i = 0; i < block_type.u.type->param_count; i++)
  5053. PUSH_TYPE(block_type.u.type->types[i]);
  5054. }
  5055. #if WASM_ENABLE_FAST_INTERP != 0
  5056. /* Recover top param_count values of frame_offset stack */
  5057. if (BLOCK_HAS_PARAM((block_type))) {
  5058. uint32 size;
  5059. BranchBlock *block = loader_ctx->frame_csp - 1;
  5060. size = sizeof(int16) *
  5061. block_type.u.type->param_cell_num;
  5062. bh_memcpy_s(loader_ctx->frame_offset, size,
  5063. block->param_frame_offsets, size);
  5064. loader_ctx->frame_offset += (size/sizeof(int16));
  5065. }
  5066. #endif
  5067. break;
  5068. }
  5069. case WASM_OP_END:
  5070. {
  5071. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5072. /* check whether block stack matches its result type */
  5073. if (!check_block_stack(loader_ctx, cur_block,
  5074. error_buf, error_buf_size))
  5075. goto fail;
  5076. /* if no else branch, and return types do not match param types, fail */
  5077. if (cur_block->label_type == LABEL_TYPE_IF
  5078. && !cur_block->else_addr) {
  5079. uint32 param_count = 0, ret_count = 0;
  5080. uint8 *param_types = NULL, *ret_types = NULL;
  5081. BlockType *block_type = &cur_block->block_type;
  5082. if (block_type->is_value_type) {
  5083. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  5084. ret_count = 1;
  5085. ret_types = &block_type->u.value_type;
  5086. }
  5087. }
  5088. else {
  5089. param_count = block_type->u.type->param_count;
  5090. ret_count = block_type->u.type->result_count;
  5091. param_types = block_type->u.type->types;
  5092. ret_types = block_type->u.type->types + param_count;
  5093. }
  5094. if (param_count != ret_count
  5095. || (param_count && memcmp(param_types, ret_types, param_count))) {
  5096. set_error_buf(error_buf, error_buf_size,
  5097. "type mismatch: else branch missing");
  5098. goto fail;
  5099. }
  5100. }
  5101. POP_CSP();
  5102. #if WASM_ENABLE_FAST_INTERP != 0
  5103. skip_label();
  5104. /* copy the result to the block return address */
  5105. RESERVE_BLOCK_RET();
  5106. apply_label_patch(loader_ctx, 0, PATCH_END);
  5107. free_label_patch_list(loader_ctx->frame_csp);
  5108. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  5109. int32 idx;
  5110. uint8 ret_type;
  5111. emit_label(WASM_OP_RETURN);
  5112. for (idx = (int32)func->func_type->result_count - 1;
  5113. idx >= 0; idx--) {
  5114. ret_type = *(func->func_type->types
  5115. + func->func_type->param_count + idx);
  5116. POP_OFFSET_TYPE(ret_type);
  5117. }
  5118. }
  5119. #endif
  5120. if (loader_ctx->csp_num > 0) {
  5121. loader_ctx->frame_csp->end_addr = p - 1;
  5122. }
  5123. else {
  5124. /* end of function block, function will return,
  5125. ignore the following bytecodes */
  5126. p = p_end;
  5127. continue;
  5128. }
  5129. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5130. break;
  5131. }
  5132. case WASM_OP_BR:
  5133. {
  5134. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  5135. error_buf, error_buf_size)))
  5136. goto fail;
  5137. RESET_STACK();
  5138. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5139. break;
  5140. }
  5141. case WASM_OP_BR_IF:
  5142. {
  5143. POP_I32();
  5144. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  5145. error_buf, error_buf_size)))
  5146. goto fail;
  5147. break;
  5148. }
  5149. case WASM_OP_BR_TABLE:
  5150. {
  5151. uint8 *ret_types = NULL;
  5152. uint32 ret_count = 0;
  5153. read_leb_uint32(p, p_end, count);
  5154. #if WASM_ENABLE_FAST_INTERP != 0
  5155. emit_uint32(loader_ctx, count);
  5156. #endif
  5157. POP_I32();
  5158. /* TODO: check the const */
  5159. for (i = 0; i <= count; i++) {
  5160. if (!(frame_csp_tmp =
  5161. check_branch_block(loader_ctx, &p, p_end,
  5162. error_buf, error_buf_size)))
  5163. goto fail;
  5164. if (i == 0) {
  5165. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  5166. ret_count =
  5167. block_type_get_result_types(&frame_csp_tmp->block_type,
  5168. &ret_types);
  5169. }
  5170. else {
  5171. uint8 *tmp_ret_types = NULL;
  5172. uint32 tmp_ret_count = 0;
  5173. /* Check whether all table items have the same return type */
  5174. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  5175. tmp_ret_count =
  5176. block_type_get_result_types(&frame_csp_tmp->block_type,
  5177. &tmp_ret_types);
  5178. if (ret_count != tmp_ret_count
  5179. || (ret_count
  5180. && 0 != memcmp(ret_types, tmp_ret_types, ret_count))) {
  5181. set_error_buf(error_buf, error_buf_size,
  5182. "type mismatch: br_table targets must "
  5183. "all use same result type");
  5184. goto fail;
  5185. }
  5186. }
  5187. }
  5188. RESET_STACK();
  5189. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5190. break;
  5191. }
  5192. case WASM_OP_RETURN:
  5193. {
  5194. int32 idx;
  5195. uint8 ret_type;
  5196. for (idx = (int32)func->func_type->result_count - 1; idx >= 0; idx--) {
  5197. ret_type = *(func->func_type->types
  5198. + func->func_type->param_count + idx);
  5199. POP_TYPE(ret_type);
  5200. #if WASM_ENABLE_FAST_INTERP != 0
  5201. /* emit the offset after return opcode */
  5202. POP_OFFSET_TYPE(ret_type);
  5203. #endif
  5204. }
  5205. RESET_STACK();
  5206. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5207. break;
  5208. }
  5209. case WASM_OP_CALL:
  5210. #if WASM_ENABLE_TAIL_CALL != 0
  5211. case WASM_OP_RETURN_CALL:
  5212. #endif
  5213. {
  5214. WASMType *func_type;
  5215. uint32 func_idx;
  5216. int32 idx;
  5217. read_leb_uint32(p, p_end, func_idx);
  5218. #if WASM_ENABLE_FAST_INTERP != 0
  5219. /* we need to emit func_idx before arguments */
  5220. emit_uint32(loader_ctx, func_idx);
  5221. #endif
  5222. if (func_idx >= module->import_function_count + module->function_count) {
  5223. set_error_buf(error_buf, error_buf_size,
  5224. "unknown function");
  5225. goto fail;
  5226. }
  5227. if (func_idx < module->import_function_count)
  5228. func_type = module->import_functions[func_idx].u.function.func_type;
  5229. else
  5230. func_type =
  5231. module->functions[func_idx - module->import_function_count]->func_type;
  5232. if (func_type->param_count > 0) {
  5233. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  5234. POP_TYPE(func_type->types[idx]);
  5235. #if WASM_ENABLE_FAST_INTERP != 0
  5236. POP_OFFSET_TYPE(func_type->types[idx]);
  5237. #endif
  5238. }
  5239. }
  5240. #if WASM_ENABLE_TAIL_CALL != 0
  5241. if (opcode == WASM_OP_CALL) {
  5242. #endif
  5243. for (i = 0; i < func_type->result_count; i++) {
  5244. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5245. #if WASM_ENABLE_FAST_INTERP != 0
  5246. /* Here we emit each return value's dynamic_offset. But in fact
  5247. * these offsets are continuous, so interpreter only need to get
  5248. * the first return value's offset.
  5249. */
  5250. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  5251. #endif
  5252. }
  5253. #if WASM_ENABLE_TAIL_CALL != 0
  5254. }
  5255. else {
  5256. char *type_str[] = { "f64", "f32", "i64", "i32" };
  5257. uint8 type;
  5258. if (func_type->result_count != func->func_type->result_count) {
  5259. set_error_buf_v(error_buf, error_buf_size,
  5260. "%s%u%s", "type mismatch: expect ",
  5261. func->func_type->result_count,
  5262. " return values but got other");
  5263. goto fail;
  5264. }
  5265. for (i = 0; i < func_type->result_count; i++) {
  5266. type = func->func_type->types[func->func_type->param_count + i];
  5267. if (func_type->types[func_type->param_count + i] != type) {
  5268. set_error_buf_v(error_buf, error_buf_size,
  5269. "%s%s%s", "type mismatch: expect ",
  5270. type_str[type - VALUE_TYPE_F64],
  5271. " but got other");
  5272. goto fail;
  5273. }
  5274. }
  5275. RESET_STACK();
  5276. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5277. }
  5278. #endif
  5279. func->has_op_func_call = true;
  5280. break;
  5281. }
  5282. case WASM_OP_CALL_INDIRECT:
  5283. #if WASM_ENABLE_TAIL_CALL != 0
  5284. case WASM_OP_RETURN_CALL_INDIRECT:
  5285. #endif
  5286. {
  5287. int32 idx;
  5288. WASMType *func_type;
  5289. uint32 type_idx;
  5290. if (module->table_count == 0
  5291. && module->import_table_count == 0) {
  5292. set_error_buf(error_buf, error_buf_size,
  5293. "call indirect with unknown table");
  5294. goto fail;
  5295. }
  5296. read_leb_uint32(p, p_end, type_idx);
  5297. #if WASM_ENABLE_FAST_INTERP != 0
  5298. /* we need to emit func_idx before arguments */
  5299. emit_uint32(loader_ctx, type_idx);
  5300. #endif
  5301. /* reserved byte 0x00 */
  5302. if (*p++ != 0x00) {
  5303. set_error_buf(error_buf, error_buf_size,
  5304. "zero flag expected");
  5305. goto fail;
  5306. }
  5307. POP_I32();
  5308. if (type_idx >= module->type_count) {
  5309. set_error_buf(error_buf, error_buf_size,
  5310. "unknown type");
  5311. goto fail;
  5312. }
  5313. func_type = module->types[type_idx];
  5314. if (func_type->param_count > 0) {
  5315. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  5316. POP_TYPE(func_type->types[idx]);
  5317. #if WASM_ENABLE_FAST_INTERP != 0
  5318. POP_OFFSET_TYPE(func_type->types[idx]);
  5319. #endif
  5320. }
  5321. }
  5322. #if WASM_ENABLE_TAIL_CALL != 0
  5323. if (opcode == WASM_OP_CALL_INDIRECT) {
  5324. #endif
  5325. for (i = 0; i < func_type->result_count; i++) {
  5326. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5327. #if WASM_ENABLE_FAST_INTERP != 0
  5328. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  5329. #endif
  5330. }
  5331. #if WASM_ENABLE_TAIL_CALL != 0
  5332. }
  5333. else {
  5334. char *type_str[] = { "f64", "f32", "i64", "i32" };
  5335. uint8 type;
  5336. if (func_type->result_count != func->func_type->result_count) {
  5337. set_error_buf_v(error_buf, error_buf_size,
  5338. "%s%u%s", "type mismatch: expect ",
  5339. func->func_type->result_count,
  5340. " return values but got other");
  5341. goto fail;
  5342. }
  5343. for (i = 0; i < func_type->result_count; i++) {
  5344. type = func->func_type->types[func->func_type->param_count + i];
  5345. if (func_type->types[func_type->param_count + i] != type)
  5346. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  5347. "type mismatch: expect ",
  5348. type_str[type - VALUE_TYPE_F64],
  5349. " but got other");
  5350. goto fail;
  5351. }
  5352. RESET_STACK();
  5353. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5354. }
  5355. #endif
  5356. func->has_op_func_call = true;
  5357. break;
  5358. }
  5359. case WASM_OP_DROP:
  5360. case WASM_OP_DROP_64:
  5361. {
  5362. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5363. int32 available_stack_cell = (int32)
  5364. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5365. if (available_stack_cell <= 0
  5366. && !cur_block->is_stack_polymorphic) {
  5367. set_error_buf(error_buf, error_buf_size,
  5368. "type mismatch, opcode drop was found "
  5369. "but stack was empty");
  5370. goto fail;
  5371. }
  5372. if (available_stack_cell > 0) {
  5373. if (*(loader_ctx->frame_ref - 1) == REF_I32
  5374. || *(loader_ctx->frame_ref - 1) == REF_F32) {
  5375. loader_ctx->frame_ref--;
  5376. loader_ctx->stack_cell_num--;
  5377. #if WASM_ENABLE_FAST_INTERP != 0
  5378. skip_label();
  5379. loader_ctx->frame_offset--;
  5380. if (*(loader_ctx->frame_offset) >
  5381. loader_ctx->start_dynamic_offset)
  5382. loader_ctx->dynamic_offset --;
  5383. #endif
  5384. }
  5385. else {
  5386. loader_ctx->frame_ref -= 2;
  5387. loader_ctx->stack_cell_num -= 2;
  5388. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  5389. *(p - 1) = WASM_OP_DROP_64;
  5390. #endif
  5391. #if WASM_ENABLE_FAST_INTERP != 0
  5392. skip_label();
  5393. loader_ctx->frame_offset -= 2;
  5394. if (*(loader_ctx->frame_offset) >
  5395. loader_ctx->start_dynamic_offset)
  5396. loader_ctx->dynamic_offset -= 2;
  5397. #endif
  5398. }
  5399. }
  5400. else {
  5401. #if WASM_ENABLE_FAST_INTERP != 0
  5402. skip_label();
  5403. #endif
  5404. }
  5405. break;
  5406. }
  5407. case WASM_OP_SELECT:
  5408. case WASM_OP_SELECT_64:
  5409. {
  5410. uint8 ref_type;
  5411. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5412. int32 available_stack_cell;
  5413. POP_I32();
  5414. available_stack_cell = (int32)
  5415. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5416. if (available_stack_cell <= 0
  5417. && !cur_block->is_stack_polymorphic) {
  5418. set_error_buf(error_buf, error_buf_size,
  5419. "type mismatch, opcode select was found "
  5420. "but stack was empty");
  5421. goto fail;
  5422. }
  5423. if (available_stack_cell > 0) {
  5424. switch (*(loader_ctx->frame_ref - 1)) {
  5425. case REF_I32:
  5426. case REF_F32:
  5427. break;
  5428. case REF_I64_2:
  5429. case REF_F64_2:
  5430. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  5431. *(p - 1) = WASM_OP_SELECT_64;
  5432. #endif
  5433. #if WASM_ENABLE_FAST_INTERP != 0
  5434. if (loader_ctx->p_code_compiled) {
  5435. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  5436. *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) =
  5437. handle_table[WASM_OP_SELECT_64];
  5438. #else
  5439. *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
  5440. (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
  5441. #endif
  5442. }
  5443. #endif
  5444. break;
  5445. }
  5446. ref_type = *(loader_ctx->frame_ref - 1);
  5447. #if WASM_ENABLE_FAST_INTERP != 0
  5448. POP_OFFSET_TYPE(ref_type);
  5449. #endif
  5450. POP_TYPE(ref_type);
  5451. #if WASM_ENABLE_FAST_INTERP != 0
  5452. POP_OFFSET_TYPE(ref_type);
  5453. #endif
  5454. POP_TYPE(ref_type);
  5455. #if WASM_ENABLE_FAST_INTERP != 0
  5456. PUSH_OFFSET_TYPE(ref_type);
  5457. #endif
  5458. PUSH_TYPE(ref_type);
  5459. }
  5460. else {
  5461. #if WASM_ENABLE_FAST_INTERP != 0
  5462. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  5463. #endif
  5464. PUSH_TYPE(VALUE_TYPE_ANY);
  5465. }
  5466. break;
  5467. }
  5468. case WASM_OP_GET_LOCAL:
  5469. {
  5470. p_org = p - 1;
  5471. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5472. PUSH_TYPE(local_type);
  5473. #if WASM_ENABLE_FAST_INTERP != 0
  5474. /* Get Local is optimized out */
  5475. skip_label();
  5476. disable_emit = true;
  5477. operand_offset = local_offset;
  5478. PUSH_OFFSET_TYPE(local_type);
  5479. #else
  5480. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5481. if (local_offset < 0x80) {
  5482. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  5483. if (local_type == VALUE_TYPE_I32
  5484. || local_type == VALUE_TYPE_F32)
  5485. *p_org++ = (uint8)local_offset;
  5486. else
  5487. *p_org++ = (uint8)(local_offset | 0x80);
  5488. while (p_org < p)
  5489. *p_org++ = WASM_OP_NOP;
  5490. }
  5491. #endif
  5492. #endif
  5493. break;
  5494. }
  5495. case WASM_OP_SET_LOCAL:
  5496. {
  5497. p_org = p - 1;
  5498. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5499. POP_TYPE(local_type);
  5500. #if WASM_ENABLE_FAST_INTERP != 0
  5501. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  5502. local_type, &preserve_local,
  5503. error_buf, error_buf_size)))
  5504. goto fail;
  5505. if (local_offset < 256) {
  5506. skip_label();
  5507. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  5508. if (loader_ctx->p_code_compiled)
  5509. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  5510. loader_ctx->frame_offset --;
  5511. loader_ctx->dynamic_offset --;
  5512. }
  5513. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  5514. if (loader_ctx->p_code_compiled)
  5515. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  5516. loader_ctx->frame_offset -= 2;
  5517. loader_ctx->dynamic_offset -= 2;
  5518. }
  5519. else {
  5520. if (local_type == VALUE_TYPE_I32
  5521. || local_type == VALUE_TYPE_F32) {
  5522. emit_label(EXT_OP_SET_LOCAL_FAST);
  5523. emit_byte(loader_ctx, local_offset);
  5524. }
  5525. else {
  5526. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  5527. emit_byte(loader_ctx, local_offset);
  5528. }
  5529. POP_OFFSET_TYPE(local_type);
  5530. }
  5531. }
  5532. else { /* local index larger than 255, reserve leb */
  5533. p_org ++;
  5534. emit_leb();
  5535. POP_OFFSET_TYPE(local_type);
  5536. }
  5537. #else
  5538. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5539. if (local_offset < 0x80) {
  5540. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  5541. if (local_type == VALUE_TYPE_I32
  5542. || local_type == VALUE_TYPE_F32)
  5543. *p_org++ = (uint8)local_offset;
  5544. else
  5545. *p_org++ = (uint8)(local_offset | 0x80);
  5546. while (p_org < p)
  5547. *p_org++ = WASM_OP_NOP;
  5548. }
  5549. #endif
  5550. #endif
  5551. break;
  5552. }
  5553. case WASM_OP_TEE_LOCAL:
  5554. {
  5555. p_org = p - 1;
  5556. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5557. #if WASM_ENABLE_FAST_INTERP != 0
  5558. /* If the stack is in polymorphic state, do fake pop and push on
  5559. offset stack to keep the depth of offset stack to be the same
  5560. with ref stack */
  5561. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5562. if (cur_block->is_stack_polymorphic) {
  5563. POP_OFFSET_TYPE(local_type);
  5564. PUSH_OFFSET_TYPE(local_type);
  5565. }
  5566. #endif
  5567. POP_TYPE(local_type);
  5568. PUSH_TYPE(local_type);
  5569. #if WASM_ENABLE_FAST_INTERP != 0
  5570. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  5571. local_type, &preserve_local,
  5572. error_buf, error_buf_size)))
  5573. goto fail;
  5574. if (local_offset < 256) {
  5575. skip_label();
  5576. if (local_type == VALUE_TYPE_I32
  5577. || local_type == VALUE_TYPE_F32) {
  5578. emit_label(EXT_OP_TEE_LOCAL_FAST);
  5579. emit_byte(loader_ctx, local_offset);
  5580. }
  5581. else {
  5582. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  5583. emit_byte(loader_ctx, local_offset);
  5584. }
  5585. }
  5586. else { /* local index larger than 255, reserve leb */
  5587. p_org ++;
  5588. emit_leb();
  5589. }
  5590. emit_operand(loader_ctx, *(loader_ctx->frame_offset -
  5591. wasm_value_type_cell_num(local_type)));
  5592. #else
  5593. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5594. if (local_offset < 0x80) {
  5595. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  5596. if (local_type == VALUE_TYPE_I32
  5597. || local_type == VALUE_TYPE_F32)
  5598. *p_org++ = (uint8)local_offset;
  5599. else
  5600. *p_org++ = (uint8)(local_offset | 0x80);
  5601. while (p_org < p)
  5602. *p_org++ = WASM_OP_NOP;
  5603. }
  5604. #endif
  5605. #endif
  5606. break;
  5607. }
  5608. case WASM_OP_GET_GLOBAL:
  5609. {
  5610. p_org = p - 1;
  5611. read_leb_uint32(p, p_end, global_idx);
  5612. if (global_idx >= global_count) {
  5613. set_error_buf(error_buf, error_buf_size,
  5614. "unknown global");
  5615. goto fail;
  5616. }
  5617. global_type =
  5618. global_idx < module->import_global_count
  5619. ? module->import_globals[global_idx].u.global.type
  5620. : module->globals[global_idx - module->import_global_count]
  5621. .type;
  5622. PUSH_TYPE(global_type);
  5623. #if WASM_ENABLE_FAST_INTERP == 0
  5624. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5625. if (global_type == VALUE_TYPE_I64
  5626. || global_type == VALUE_TYPE_F64) {
  5627. *p_org = WASM_OP_GET_GLOBAL_64;
  5628. }
  5629. #endif
  5630. #else /* else of WASM_ENABLE_FAST_INTERP */
  5631. if (global_type == VALUE_TYPE_I64
  5632. || global_type == VALUE_TYPE_F64) {
  5633. skip_label();
  5634. emit_label(WASM_OP_GET_GLOBAL_64);
  5635. }
  5636. emit_uint32(loader_ctx, global_idx);
  5637. PUSH_OFFSET_TYPE(global_type);
  5638. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5639. break;
  5640. }
  5641. case WASM_OP_SET_GLOBAL:
  5642. {
  5643. bool is_mutable = false;
  5644. p_org = p - 1;
  5645. read_leb_uint32(p, p_end, global_idx);
  5646. if (global_idx >= global_count) {
  5647. set_error_buf(error_buf, error_buf_size,
  5648. "unknown global");
  5649. goto fail;
  5650. }
  5651. is_mutable =
  5652. global_idx < module->import_global_count
  5653. ? module->import_globals[global_idx].u.global.is_mutable
  5654. : module->globals[global_idx - module->import_global_count]
  5655. .is_mutable;
  5656. if (!is_mutable) {
  5657. set_error_buf(error_buf, error_buf_size,
  5658. "global is immutable");
  5659. goto fail;
  5660. }
  5661. global_type =
  5662. global_idx < module->import_global_count
  5663. ? module->import_globals[global_idx].u.global.type
  5664. : module->globals[global_idx - module->import_global_count]
  5665. .type;
  5666. POP_TYPE(global_type);
  5667. #if WASM_ENABLE_FAST_INTERP == 0
  5668. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5669. if (global_type == VALUE_TYPE_I64
  5670. || global_type == VALUE_TYPE_F64) {
  5671. *p_org = WASM_OP_SET_GLOBAL_64;
  5672. }
  5673. else if (module->aux_stack_size > 0
  5674. && global_idx == module->aux_stack_top_global_index) {
  5675. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  5676. }
  5677. #endif
  5678. #else /* else of WASM_ENABLE_FAST_INTERP */
  5679. if (global_type == VALUE_TYPE_I64
  5680. || global_type == VALUE_TYPE_F64) {
  5681. skip_label();
  5682. emit_label(WASM_OP_SET_GLOBAL_64);
  5683. }
  5684. else if (module->aux_stack_size > 0
  5685. && global_idx == module->aux_stack_top_global_index) {
  5686. skip_label();
  5687. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  5688. }
  5689. emit_uint32(loader_ctx, global_idx);
  5690. POP_OFFSET_TYPE(global_type);
  5691. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5692. break;
  5693. }
  5694. /* load */
  5695. case WASM_OP_I32_LOAD:
  5696. case WASM_OP_I32_LOAD8_S:
  5697. case WASM_OP_I32_LOAD8_U:
  5698. case WASM_OP_I32_LOAD16_S:
  5699. case WASM_OP_I32_LOAD16_U:
  5700. case WASM_OP_I64_LOAD:
  5701. case WASM_OP_I64_LOAD8_S:
  5702. case WASM_OP_I64_LOAD8_U:
  5703. case WASM_OP_I64_LOAD16_S:
  5704. case WASM_OP_I64_LOAD16_U:
  5705. case WASM_OP_I64_LOAD32_S:
  5706. case WASM_OP_I64_LOAD32_U:
  5707. case WASM_OP_F32_LOAD:
  5708. case WASM_OP_F64_LOAD:
  5709. /* store */
  5710. case WASM_OP_I32_STORE:
  5711. case WASM_OP_I32_STORE8:
  5712. case WASM_OP_I32_STORE16:
  5713. case WASM_OP_I64_STORE:
  5714. case WASM_OP_I64_STORE8:
  5715. case WASM_OP_I64_STORE16:
  5716. case WASM_OP_I64_STORE32:
  5717. case WASM_OP_F32_STORE:
  5718. case WASM_OP_F64_STORE:
  5719. {
  5720. #if WASM_ENABLE_FAST_INTERP != 0
  5721. /* change F32/F64 into I32/I64 */
  5722. if (opcode == WASM_OP_F32_LOAD) {
  5723. skip_label();
  5724. emit_label(WASM_OP_I32_LOAD);
  5725. }
  5726. else if (opcode == WASM_OP_F64_LOAD) {
  5727. skip_label();
  5728. emit_label(WASM_OP_I64_LOAD);
  5729. }
  5730. else if (opcode == WASM_OP_F32_STORE) {
  5731. skip_label();
  5732. emit_label(WASM_OP_I32_STORE);
  5733. }
  5734. else if (opcode == WASM_OP_F64_STORE) {
  5735. skip_label();
  5736. emit_label(WASM_OP_I64_STORE);
  5737. }
  5738. #endif
  5739. CHECK_MEMORY();
  5740. read_leb_uint32(p, p_end, align); /* align */
  5741. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5742. if (!check_memory_access_align(opcode, align,
  5743. error_buf, error_buf_size)) {
  5744. goto fail;
  5745. }
  5746. #if WASM_ENABLE_FAST_INTERP != 0
  5747. emit_uint32(loader_ctx, mem_offset);
  5748. #endif
  5749. switch (opcode)
  5750. {
  5751. /* load */
  5752. case WASM_OP_I32_LOAD:
  5753. case WASM_OP_I32_LOAD8_S:
  5754. case WASM_OP_I32_LOAD8_U:
  5755. case WASM_OP_I32_LOAD16_S:
  5756. case WASM_OP_I32_LOAD16_U:
  5757. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5758. break;
  5759. case WASM_OP_I64_LOAD:
  5760. case WASM_OP_I64_LOAD8_S:
  5761. case WASM_OP_I64_LOAD8_U:
  5762. case WASM_OP_I64_LOAD16_S:
  5763. case WASM_OP_I64_LOAD16_U:
  5764. case WASM_OP_I64_LOAD32_S:
  5765. case WASM_OP_I64_LOAD32_U:
  5766. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5767. break;
  5768. case WASM_OP_F32_LOAD:
  5769. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5770. break;
  5771. case WASM_OP_F64_LOAD:
  5772. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5773. break;
  5774. /* store */
  5775. case WASM_OP_I32_STORE:
  5776. case WASM_OP_I32_STORE8:
  5777. case WASM_OP_I32_STORE16:
  5778. POP_I32();
  5779. POP_I32();
  5780. break;
  5781. case WASM_OP_I64_STORE:
  5782. case WASM_OP_I64_STORE8:
  5783. case WASM_OP_I64_STORE16:
  5784. case WASM_OP_I64_STORE32:
  5785. POP_I64();
  5786. POP_I32();
  5787. break;
  5788. case WASM_OP_F32_STORE:
  5789. POP_F32();
  5790. POP_I32();
  5791. break;
  5792. case WASM_OP_F64_STORE:
  5793. POP_F64();
  5794. POP_I32();
  5795. break;
  5796. default:
  5797. break;
  5798. }
  5799. break;
  5800. }
  5801. case WASM_OP_MEMORY_SIZE:
  5802. CHECK_MEMORY();
  5803. /* reserved byte 0x00 */
  5804. if (*p++ != 0x00) {
  5805. set_error_buf(error_buf, error_buf_size,
  5806. "zero flag expected");
  5807. goto fail;
  5808. }
  5809. PUSH_I32();
  5810. module->possible_memory_grow = true;
  5811. break;
  5812. case WASM_OP_MEMORY_GROW:
  5813. CHECK_MEMORY();
  5814. /* reserved byte 0x00 */
  5815. if (*p++ != 0x00) {
  5816. set_error_buf(error_buf, error_buf_size,
  5817. "zero flag expected");
  5818. goto fail;
  5819. }
  5820. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5821. func->has_op_memory_grow = true;
  5822. module->possible_memory_grow = true;
  5823. break;
  5824. case WASM_OP_I32_CONST:
  5825. read_leb_int32(p, p_end, i32_const);
  5826. #if WASM_ENABLE_FAST_INTERP != 0
  5827. skip_label();
  5828. disable_emit = true;
  5829. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  5830. #else
  5831. (void)i32_const;
  5832. #endif
  5833. PUSH_I32();
  5834. break;
  5835. case WASM_OP_I64_CONST:
  5836. read_leb_int64(p, p_end, i64);
  5837. #if WASM_ENABLE_FAST_INTERP != 0
  5838. skip_label();
  5839. disable_emit = true;
  5840. GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
  5841. #endif
  5842. PUSH_I64();
  5843. break;
  5844. case WASM_OP_F32_CONST:
  5845. p += sizeof(float32);
  5846. #if WASM_ENABLE_FAST_INTERP != 0
  5847. skip_label();
  5848. disable_emit = true;
  5849. bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32));
  5850. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
  5851. #endif
  5852. PUSH_F32();
  5853. break;
  5854. case WASM_OP_F64_CONST:
  5855. p += sizeof(float64);
  5856. #if WASM_ENABLE_FAST_INTERP != 0
  5857. skip_label();
  5858. disable_emit = true;
  5859. /* Some MCU may require 8-byte align */
  5860. bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64));
  5861. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
  5862. #endif
  5863. PUSH_F64();
  5864. break;
  5865. case WASM_OP_I32_EQZ:
  5866. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5867. break;
  5868. case WASM_OP_I32_EQ:
  5869. case WASM_OP_I32_NE:
  5870. case WASM_OP_I32_LT_S:
  5871. case WASM_OP_I32_LT_U:
  5872. case WASM_OP_I32_GT_S:
  5873. case WASM_OP_I32_GT_U:
  5874. case WASM_OP_I32_LE_S:
  5875. case WASM_OP_I32_LE_U:
  5876. case WASM_OP_I32_GE_S:
  5877. case WASM_OP_I32_GE_U:
  5878. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5879. break;
  5880. case WASM_OP_I64_EQZ:
  5881. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5882. break;
  5883. case WASM_OP_I64_EQ:
  5884. case WASM_OP_I64_NE:
  5885. case WASM_OP_I64_LT_S:
  5886. case WASM_OP_I64_LT_U:
  5887. case WASM_OP_I64_GT_S:
  5888. case WASM_OP_I64_GT_U:
  5889. case WASM_OP_I64_LE_S:
  5890. case WASM_OP_I64_LE_U:
  5891. case WASM_OP_I64_GE_S:
  5892. case WASM_OP_I64_GE_U:
  5893. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5894. break;
  5895. case WASM_OP_F32_EQ:
  5896. case WASM_OP_F32_NE:
  5897. case WASM_OP_F32_LT:
  5898. case WASM_OP_F32_GT:
  5899. case WASM_OP_F32_LE:
  5900. case WASM_OP_F32_GE:
  5901. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5902. break;
  5903. case WASM_OP_F64_EQ:
  5904. case WASM_OP_F64_NE:
  5905. case WASM_OP_F64_LT:
  5906. case WASM_OP_F64_GT:
  5907. case WASM_OP_F64_LE:
  5908. case WASM_OP_F64_GE:
  5909. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5910. break;
  5911. case WASM_OP_I32_CLZ:
  5912. case WASM_OP_I32_CTZ:
  5913. case WASM_OP_I32_POPCNT:
  5914. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5915. break;
  5916. case WASM_OP_I32_ADD:
  5917. case WASM_OP_I32_SUB:
  5918. case WASM_OP_I32_MUL:
  5919. case WASM_OP_I32_DIV_S:
  5920. case WASM_OP_I32_DIV_U:
  5921. case WASM_OP_I32_REM_S:
  5922. case WASM_OP_I32_REM_U:
  5923. case WASM_OP_I32_AND:
  5924. case WASM_OP_I32_OR:
  5925. case WASM_OP_I32_XOR:
  5926. case WASM_OP_I32_SHL:
  5927. case WASM_OP_I32_SHR_S:
  5928. case WASM_OP_I32_SHR_U:
  5929. case WASM_OP_I32_ROTL:
  5930. case WASM_OP_I32_ROTR:
  5931. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5932. break;
  5933. case WASM_OP_I64_CLZ:
  5934. case WASM_OP_I64_CTZ:
  5935. case WASM_OP_I64_POPCNT:
  5936. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5937. break;
  5938. case WASM_OP_I64_ADD:
  5939. case WASM_OP_I64_SUB:
  5940. case WASM_OP_I64_MUL:
  5941. case WASM_OP_I64_DIV_S:
  5942. case WASM_OP_I64_DIV_U:
  5943. case WASM_OP_I64_REM_S:
  5944. case WASM_OP_I64_REM_U:
  5945. case WASM_OP_I64_AND:
  5946. case WASM_OP_I64_OR:
  5947. case WASM_OP_I64_XOR:
  5948. case WASM_OP_I64_SHL:
  5949. case WASM_OP_I64_SHR_S:
  5950. case WASM_OP_I64_SHR_U:
  5951. case WASM_OP_I64_ROTL:
  5952. case WASM_OP_I64_ROTR:
  5953. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5954. break;
  5955. case WASM_OP_F32_ABS:
  5956. case WASM_OP_F32_NEG:
  5957. case WASM_OP_F32_CEIL:
  5958. case WASM_OP_F32_FLOOR:
  5959. case WASM_OP_F32_TRUNC:
  5960. case WASM_OP_F32_NEAREST:
  5961. case WASM_OP_F32_SQRT:
  5962. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5963. break;
  5964. case WASM_OP_F32_ADD:
  5965. case WASM_OP_F32_SUB:
  5966. case WASM_OP_F32_MUL:
  5967. case WASM_OP_F32_DIV:
  5968. case WASM_OP_F32_MIN:
  5969. case WASM_OP_F32_MAX:
  5970. case WASM_OP_F32_COPYSIGN:
  5971. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5972. break;
  5973. case WASM_OP_F64_ABS:
  5974. case WASM_OP_F64_NEG:
  5975. case WASM_OP_F64_CEIL:
  5976. case WASM_OP_F64_FLOOR:
  5977. case WASM_OP_F64_TRUNC:
  5978. case WASM_OP_F64_NEAREST:
  5979. case WASM_OP_F64_SQRT:
  5980. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5981. break;
  5982. case WASM_OP_F64_ADD:
  5983. case WASM_OP_F64_SUB:
  5984. case WASM_OP_F64_MUL:
  5985. case WASM_OP_F64_DIV:
  5986. case WASM_OP_F64_MIN:
  5987. case WASM_OP_F64_MAX:
  5988. case WASM_OP_F64_COPYSIGN:
  5989. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5990. break;
  5991. case WASM_OP_I32_WRAP_I64:
  5992. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5993. break;
  5994. case WASM_OP_I32_TRUNC_S_F32:
  5995. case WASM_OP_I32_TRUNC_U_F32:
  5996. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5997. break;
  5998. case WASM_OP_I32_TRUNC_S_F64:
  5999. case WASM_OP_I32_TRUNC_U_F64:
  6000. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6001. break;
  6002. case WASM_OP_I64_EXTEND_S_I32:
  6003. case WASM_OP_I64_EXTEND_U_I32:
  6004. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6005. break;
  6006. case WASM_OP_I64_TRUNC_S_F32:
  6007. case WASM_OP_I64_TRUNC_U_F32:
  6008. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6009. break;
  6010. case WASM_OP_I64_TRUNC_S_F64:
  6011. case WASM_OP_I64_TRUNC_U_F64:
  6012. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6013. break;
  6014. case WASM_OP_F32_CONVERT_S_I32:
  6015. case WASM_OP_F32_CONVERT_U_I32:
  6016. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6017. break;
  6018. case WASM_OP_F32_CONVERT_S_I64:
  6019. case WASM_OP_F32_CONVERT_U_I64:
  6020. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  6021. break;
  6022. case WASM_OP_F32_DEMOTE_F64:
  6023. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  6024. break;
  6025. case WASM_OP_F64_CONVERT_S_I32:
  6026. case WASM_OP_F64_CONVERT_U_I32:
  6027. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  6028. break;
  6029. case WASM_OP_F64_CONVERT_S_I64:
  6030. case WASM_OP_F64_CONVERT_U_I64:
  6031. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6032. break;
  6033. case WASM_OP_F64_PROMOTE_F32:
  6034. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  6035. break;
  6036. case WASM_OP_I32_REINTERPRET_F32:
  6037. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6038. break;
  6039. case WASM_OP_I64_REINTERPRET_F64:
  6040. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6041. break;
  6042. case WASM_OP_F32_REINTERPRET_I32:
  6043. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6044. break;
  6045. case WASM_OP_F64_REINTERPRET_I64:
  6046. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6047. break;
  6048. case WASM_OP_I32_EXTEND8_S:
  6049. case WASM_OP_I32_EXTEND16_S:
  6050. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6051. break;
  6052. case WASM_OP_I64_EXTEND8_S:
  6053. case WASM_OP_I64_EXTEND16_S:
  6054. case WASM_OP_I64_EXTEND32_S:
  6055. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6056. break;
  6057. case WASM_OP_MISC_PREFIX:
  6058. {
  6059. uint32 opcode1;
  6060. read_leb_uint32(p, p_end, opcode1);
  6061. #if WASM_ENABLE_FAST_INTERP != 0
  6062. emit_byte(loader_ctx, ((uint8)opcode1));
  6063. #endif
  6064. switch (opcode1)
  6065. {
  6066. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6067. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6068. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6069. break;
  6070. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6071. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6072. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6073. break;
  6074. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6075. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6076. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6077. break;
  6078. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6079. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6080. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6081. break;
  6082. #if WASM_ENABLE_BULK_MEMORY != 0
  6083. case WASM_OP_MEMORY_INIT:
  6084. read_leb_uint32(p, p_end, segment_index);
  6085. #if WASM_ENABLE_FAST_INTERP != 0
  6086. emit_uint32(loader_ctx, segment_index);
  6087. #endif
  6088. if (module->import_memory_count == 0 && module->memory_count == 0)
  6089. goto fail_unknown_memory;
  6090. if (*p++ != 0x00)
  6091. goto fail_zero_flag_expected;
  6092. if (segment_index >= module->data_seg_count) {
  6093. set_error_buf(error_buf, error_buf_size,
  6094. "unknown data segment");
  6095. goto fail;
  6096. }
  6097. if (module->data_seg_count1 == 0)
  6098. goto fail_data_cnt_sec_require;
  6099. POP_I32();
  6100. POP_I32();
  6101. POP_I32();
  6102. break;
  6103. case WASM_OP_DATA_DROP:
  6104. read_leb_uint32(p, p_end, segment_index);
  6105. #if WASM_ENABLE_FAST_INTERP != 0
  6106. emit_uint32(loader_ctx, segment_index);
  6107. #endif
  6108. if (segment_index >= module->data_seg_count) {
  6109. set_error_buf(error_buf, error_buf_size,
  6110. "unknown data segment");
  6111. goto fail;
  6112. }
  6113. if (module->data_seg_count1 == 0)
  6114. goto fail_data_cnt_sec_require;
  6115. break;
  6116. case WASM_OP_MEMORY_COPY:
  6117. /* both src and dst memory index should be 0 */
  6118. if (*(int16*)p != 0x0000)
  6119. goto fail_zero_flag_expected;
  6120. p += 2;
  6121. if (module->import_memory_count == 0 && module->memory_count == 0)
  6122. goto fail_unknown_memory;
  6123. POP_I32();
  6124. POP_I32();
  6125. POP_I32();
  6126. break;
  6127. case WASM_OP_MEMORY_FILL:
  6128. if (*p++ != 0x00) {
  6129. goto fail_zero_flag_expected;
  6130. }
  6131. if (module->import_memory_count == 0 && module->memory_count == 0) {
  6132. goto fail_unknown_memory;
  6133. }
  6134. POP_I32();
  6135. POP_I32();
  6136. POP_I32();
  6137. break;
  6138. fail_zero_flag_expected:
  6139. set_error_buf(error_buf, error_buf_size,
  6140. "zero flag expected");
  6141. goto fail;
  6142. fail_unknown_memory:
  6143. set_error_buf(error_buf, error_buf_size,
  6144. "unknown memory 0");
  6145. goto fail;
  6146. fail_data_cnt_sec_require:
  6147. set_error_buf(error_buf, error_buf_size,
  6148. "data count section required");
  6149. goto fail;
  6150. /* TODO: to support bulk table operation */
  6151. #endif /* WASM_ENABLE_BULK_MEMORY */
  6152. default:
  6153. set_error_buf_v(error_buf, error_buf_size,
  6154. "%s %02x %02x",
  6155. "unsupported opcode", 0xfc, opcode1);
  6156. goto fail;
  6157. }
  6158. break;
  6159. }
  6160. #if WASM_ENABLE_SHARED_MEMORY != 0
  6161. case WASM_OP_ATOMIC_PREFIX:
  6162. {
  6163. opcode = read_uint8(p);
  6164. #if WASM_ENABLE_FAST_INTERP != 0
  6165. emit_byte(loader_ctx, opcode);
  6166. #endif
  6167. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6168. CHECK_MEMORY();
  6169. read_leb_uint32(p, p_end, align); /* align */
  6170. read_leb_uint32(p, p_end, mem_offset); /* offset */
  6171. if (!check_memory_align_equal(opcode, align,
  6172. error_buf,
  6173. error_buf_size)) {
  6174. goto fail;
  6175. }
  6176. #if WASM_ENABLE_FAST_INTERP != 0
  6177. emit_uint32(loader_ctx, mem_offset);
  6178. #endif
  6179. }
  6180. switch (opcode) {
  6181. case WASM_OP_ATOMIC_NOTIFY:
  6182. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6183. break;
  6184. case WASM_OP_ATOMIC_WAIT32:
  6185. POP_I64();
  6186. POP_I32();
  6187. POP_I32();
  6188. PUSH_I32();
  6189. break;
  6190. case WASM_OP_ATOMIC_WAIT64:
  6191. POP_I64();
  6192. POP_I64();
  6193. POP_I32();
  6194. PUSH_I32();
  6195. break;
  6196. case WASM_OP_ATOMIC_FENCE:
  6197. /* reserved byte 0x00 */
  6198. if (*p++ != 0x00) {
  6199. set_error_buf(error_buf, error_buf_size,
  6200. "zero flag expected");
  6201. goto fail;
  6202. }
  6203. break;
  6204. case WASM_OP_ATOMIC_I32_LOAD:
  6205. case WASM_OP_ATOMIC_I32_LOAD8_U:
  6206. case WASM_OP_ATOMIC_I32_LOAD16_U:
  6207. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6208. break;
  6209. case WASM_OP_ATOMIC_I32_STORE:
  6210. case WASM_OP_ATOMIC_I32_STORE8:
  6211. case WASM_OP_ATOMIC_I32_STORE16:
  6212. POP_I32();
  6213. POP_I32();
  6214. break;
  6215. case WASM_OP_ATOMIC_I64_LOAD:
  6216. case WASM_OP_ATOMIC_I64_LOAD8_U:
  6217. case WASM_OP_ATOMIC_I64_LOAD16_U:
  6218. case WASM_OP_ATOMIC_I64_LOAD32_U:
  6219. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6220. break;
  6221. case WASM_OP_ATOMIC_I64_STORE:
  6222. case WASM_OP_ATOMIC_I64_STORE8:
  6223. case WASM_OP_ATOMIC_I64_STORE16:
  6224. case WASM_OP_ATOMIC_I64_STORE32:
  6225. POP_I64();
  6226. POP_I32();
  6227. break;
  6228. case WASM_OP_ATOMIC_RMW_I32_ADD:
  6229. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  6230. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  6231. case WASM_OP_ATOMIC_RMW_I32_SUB:
  6232. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  6233. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  6234. case WASM_OP_ATOMIC_RMW_I32_AND:
  6235. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  6236. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  6237. case WASM_OP_ATOMIC_RMW_I32_OR:
  6238. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  6239. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  6240. case WASM_OP_ATOMIC_RMW_I32_XOR:
  6241. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  6242. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  6243. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  6244. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  6245. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  6246. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6247. break;
  6248. case WASM_OP_ATOMIC_RMW_I64_ADD:
  6249. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  6250. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  6251. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  6252. case WASM_OP_ATOMIC_RMW_I64_SUB:
  6253. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  6254. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  6255. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  6256. case WASM_OP_ATOMIC_RMW_I64_AND:
  6257. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  6258. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  6259. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  6260. case WASM_OP_ATOMIC_RMW_I64_OR:
  6261. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  6262. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  6263. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  6264. case WASM_OP_ATOMIC_RMW_I64_XOR:
  6265. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  6266. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  6267. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  6268. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  6269. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  6270. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  6271. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  6272. POP_I64();
  6273. POP_I32();
  6274. PUSH_I64();
  6275. break;
  6276. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  6277. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  6278. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  6279. POP_I32();
  6280. POP_I32();
  6281. POP_I32();
  6282. PUSH_I32();
  6283. break;
  6284. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  6285. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  6286. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  6287. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  6288. POP_I64();
  6289. POP_I64();
  6290. POP_I32();
  6291. PUSH_I64();
  6292. break;
  6293. default:
  6294. set_error_buf_v(error_buf, error_buf_size,
  6295. "%s %02x %02x",
  6296. "unsupported opcode", 0xfe, opcode);
  6297. goto fail;
  6298. }
  6299. break;
  6300. }
  6301. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6302. default:
  6303. set_error_buf_v(error_buf, error_buf_size,
  6304. "%s %02x",
  6305. "unsupported opcode", opcode);
  6306. goto fail;
  6307. }
  6308. #if WASM_ENABLE_FAST_INTERP != 0
  6309. last_op = opcode;
  6310. #endif
  6311. }
  6312. if (loader_ctx->csp_num > 0) {
  6313. set_error_buf(error_buf, error_buf_size,
  6314. "function body must end with END opcode");
  6315. goto fail;
  6316. }
  6317. #if WASM_ENABLE_FAST_INTERP != 0
  6318. if (loader_ctx->p_code_compiled == NULL)
  6319. goto re_scan;
  6320. func->const_cell_num = loader_ctx->const_cell_num;
  6321. if (!(func->consts = func_const =
  6322. loader_malloc(func->const_cell_num * 4,
  6323. error_buf, error_buf_size))) {
  6324. goto fail;
  6325. }
  6326. func_const_end = func->consts + func->const_cell_num * 4;
  6327. /* reverse the const buf */
  6328. for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
  6329. Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
  6330. if (c->value_type == VALUE_TYPE_F64
  6331. || c->value_type == VALUE_TYPE_I64) {
  6332. bh_memcpy_s(func_const, func_const_end - func_const,
  6333. &(c->value.f64), sizeof(int64));
  6334. func_const += sizeof(int64);
  6335. } else {
  6336. bh_memcpy_s(func_const, func_const_end - func_const,
  6337. &(c->value.f32), sizeof(int32));
  6338. func_const += sizeof(int32);
  6339. }
  6340. }
  6341. func->max_stack_cell_num = loader_ctx->preserved_local_offset -
  6342. loader_ctx->start_dynamic_offset + 1;
  6343. #else
  6344. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  6345. #endif
  6346. func->max_block_num = loader_ctx->max_csp_num;
  6347. return_value = true;
  6348. fail:
  6349. wasm_loader_ctx_destroy(loader_ctx);
  6350. (void)u8;
  6351. (void)u32;
  6352. (void)i32;
  6353. (void)i64;
  6354. (void)local_offset;
  6355. (void)p_org;
  6356. (void)mem_offset;
  6357. (void)align;
  6358. return return_value;
  6359. }