wasm_mini_loader.c 199 KB

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