wasm_interp_fast.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "wasm_memory.h"
  11. #include "../common/wasm_exec_env.h"
  12. #if WASM_ENABLE_SHARED_MEMORY != 0
  13. #include "../common/wasm_shared_memory.h"
  14. #endif
  15. typedef int32 CellType_I32;
  16. typedef int64 CellType_I64;
  17. typedef float32 CellType_F32;
  18. typedef float64 CellType_F64;
  19. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  20. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  21. #define CHECK_MEMORY_OVERFLOW(bytes) \
  22. do { \
  23. uint64 offset1 = (uint64)offset + (uint64)addr; \
  24. if (offset1 + bytes <= (uint64)linear_mem_size) \
  25. /* If offset1 is in valid range, maddr must also \
  26. be in valid range, no need to check it again. */ \
  27. maddr = memory->memory_data + offset1; \
  28. else \
  29. goto out_of_bounds; \
  30. } while (0)
  31. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  32. do { \
  33. uint64 offset1 = (uint32)(start); \
  34. if (offset1 + bytes <= linear_mem_size) \
  35. /* App heap space is not valid space for \
  36. bulk memory operation */ \
  37. maddr = memory->memory_data + offset1; \
  38. else \
  39. goto out_of_bounds; \
  40. } while (0)
  41. #else
  42. #define CHECK_MEMORY_OVERFLOW(bytes) \
  43. do { \
  44. uint64 offset1 = (uint64)offset + (uint64)addr; \
  45. maddr = memory->memory_data + offset1; \
  46. } while (0)
  47. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  48. do { \
  49. maddr = memory->memory_data + (uint32)(start); \
  50. } while (0)
  51. #endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
  52. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  53. #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
  54. do { \
  55. if (((uintptr_t)maddr & (align - 1)) != 0) \
  56. goto unaligned_atomic; \
  57. } while (0)
  58. static inline uint32
  59. rotl32(uint32 n, uint32 c)
  60. {
  61. const uint32 mask = (31);
  62. c = c % 32;
  63. c &= mask;
  64. return (n << c) | (n >> ((0 - c) & mask));
  65. }
  66. static inline uint32
  67. rotr32(uint32 n, uint32 c)
  68. {
  69. const uint32 mask = (31);
  70. c = c % 32;
  71. c &= mask;
  72. return (n >> c) | (n << ((0 - c) & mask));
  73. }
  74. static inline uint64
  75. rotl64(uint64 n, uint64 c)
  76. {
  77. const uint64 mask = (63);
  78. c = c % 64;
  79. c &= mask;
  80. return (n << c) | (n >> ((0 - c) & mask));
  81. }
  82. static inline uint64
  83. rotr64(uint64 n, uint64 c)
  84. {
  85. const uint64 mask = (63);
  86. c = c % 64;
  87. c &= mask;
  88. return (n >> c) | (n << ((0 - c) & mask));
  89. }
  90. static inline float32
  91. f32_min(float32 a, float32 b)
  92. {
  93. if (isnan(a) || isnan(b))
  94. return NAN;
  95. else if (a == 0 && a == b)
  96. return signbit(a) ? a : b;
  97. else
  98. return a > b ? b : a;
  99. }
  100. static inline float32
  101. f32_max(float32 a, float32 b)
  102. {
  103. if (isnan(a) || isnan(b))
  104. return NAN;
  105. else if (a == 0 && a == b)
  106. return signbit(a) ? b : a;
  107. else
  108. return a > b ? a : b;
  109. }
  110. static inline float64
  111. f64_min(float64 a, float64 b)
  112. {
  113. if (isnan(a) || isnan(b))
  114. return NAN;
  115. else if (a == 0 && a == b)
  116. return signbit(a) ? a : b;
  117. else
  118. return a > b ? b : a;
  119. }
  120. static inline float64
  121. f64_max(float64 a, float64 b)
  122. {
  123. if (isnan(a) || isnan(b))
  124. return NAN;
  125. else if (a == 0 && a == b)
  126. return signbit(a) ? b : a;
  127. else
  128. return a > b ? a : b;
  129. }
  130. static inline uint32
  131. clz32(uint32 type)
  132. {
  133. uint32 num = 0;
  134. if (type == 0)
  135. return 32;
  136. while (!(type & 0x80000000)) {
  137. num++;
  138. type <<= 1;
  139. }
  140. return num;
  141. }
  142. static inline uint32
  143. clz64(uint64 type)
  144. {
  145. uint32 num = 0;
  146. if (type == 0)
  147. return 64;
  148. while (!(type & 0x8000000000000000LL)) {
  149. num++;
  150. type <<= 1;
  151. }
  152. return num;
  153. }
  154. static inline uint32
  155. ctz32(uint32 type)
  156. {
  157. uint32 num = 0;
  158. if (type == 0)
  159. return 32;
  160. while (!(type & 1)) {
  161. num++;
  162. type >>= 1;
  163. }
  164. return num;
  165. }
  166. static inline uint32
  167. ctz64(uint64 type)
  168. {
  169. uint32 num = 0;
  170. if (type == 0)
  171. return 64;
  172. while (!(type & 1)) {
  173. num++;
  174. type >>= 1;
  175. }
  176. return num;
  177. }
  178. static inline uint32
  179. popcount32(uint32 u)
  180. {
  181. uint32 ret = 0;
  182. while (u) {
  183. u = (u & (u - 1));
  184. ret++;
  185. }
  186. return ret;
  187. }
  188. static inline uint32
  189. popcount64(uint64 u)
  190. {
  191. uint32 ret = 0;
  192. while (u) {
  193. u = (u & (u - 1));
  194. ret++;
  195. }
  196. return ret;
  197. }
  198. static float
  199. local_copysignf(float x, float y)
  200. {
  201. union {
  202. float f;
  203. uint32_t i;
  204. } ux = { x }, uy = { y };
  205. ux.i &= 0x7fffffff;
  206. ux.i |= uy.i & 0x80000000;
  207. return ux.f;
  208. }
  209. static double
  210. local_copysign(double x, double y)
  211. {
  212. union {
  213. double f;
  214. uint64_t i;
  215. } ux = { x }, uy = { y };
  216. ux.i &= -1ULL / 2;
  217. ux.i |= uy.i & 1ULL << 63;
  218. return ux.f;
  219. }
  220. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  221. #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
  222. #define LOAD_PTR(addr) (*(void **)(addr))
  223. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  224. static inline uint32
  225. LOAD_U32_WITH_2U16S(void *addr)
  226. {
  227. union {
  228. uint32 val;
  229. uint16 u16[2];
  230. } u;
  231. bh_assert(((uintptr_t)addr & 1) == 0);
  232. u.u16[0] = ((uint16 *)addr)[0];
  233. u.u16[1] = ((uint16 *)addr)[1];
  234. return u.val;
  235. }
  236. #if UINTPTR_MAX == UINT32_MAX
  237. #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
  238. #elif UINTPTR_MAX == UINT64_MAX
  239. static inline void *
  240. LOAD_PTR(void *addr)
  241. {
  242. uintptr_t addr1 = (uintptr_t)addr;
  243. union {
  244. void *val;
  245. uint32 u32[2];
  246. uint16 u16[4];
  247. } u;
  248. bh_assert(((uintptr_t)addr & 1) == 0);
  249. if ((addr1 & (uintptr_t)7) == 0)
  250. return *(void **)addr;
  251. if ((addr1 & (uintptr_t)3) == 0) {
  252. u.u32[0] = ((uint32 *)addr)[0];
  253. u.u32[1] = ((uint32 *)addr)[1];
  254. }
  255. else {
  256. u.u16[0] = ((uint16 *)addr)[0];
  257. u.u16[1] = ((uint16 *)addr)[1];
  258. u.u16[2] = ((uint16 *)addr)[2];
  259. u.u16[3] = ((uint16 *)addr)[3];
  260. }
  261. return u.val;
  262. }
  263. #endif /* end of UINTPTR_MAX */
  264. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  265. #define read_uint32(p) \
  266. (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
  267. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  268. do { \
  269. uint32 param_count = cur_func->param_count; \
  270. local_idx = read_uint32(frame_ip); \
  271. bh_assert(local_idx < param_count + cur_func->local_count); \
  272. local_offset = cur_func->local_offsets[local_idx]; \
  273. if (local_idx < param_count) \
  274. local_type = cur_func->param_types[local_idx]; \
  275. else \
  276. local_type = cur_func->local_types[local_idx - param_count]; \
  277. } while (0)
  278. #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
  279. #define SET_OPERAND_I32(off, value) \
  280. do { \
  281. *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  282. } while (0)
  283. #define SET_OPERAND_F32(off, value) \
  284. do { \
  285. *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  286. } while (0)
  287. #define SET_OPERAND_I64(off, value) \
  288. do { \
  289. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  290. PUT_I64_TO_ADDR(addr_tmp, value); \
  291. } while (0)
  292. #define SET_OPERAND_F64(off, value) \
  293. do { \
  294. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  295. PUT_F64_TO_ADDR(addr_tmp, value); \
  296. } while (0)
  297. #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
  298. #define GET_OPERAND_I32(type, off) \
  299. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  300. #define GET_OPERAND_F32(type, off) \
  301. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  302. #define GET_OPERAND_I64(type, off) \
  303. (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  304. #define GET_OPERAND_F64(type, off) \
  305. (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  306. #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
  307. #define PUSH_I32(value) \
  308. do { \
  309. *(int32 *)(frame_lp + GET_OFFSET()) = value; \
  310. } while (0)
  311. #define PUSH_F32(value) \
  312. do { \
  313. *(float32 *)(frame_lp + GET_OFFSET()) = value; \
  314. } while (0)
  315. #define PUSH_I64(value) \
  316. do { \
  317. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  318. PUT_I64_TO_ADDR(addr_tmp, value); \
  319. } while (0)
  320. #define PUSH_F64(value) \
  321. do { \
  322. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  323. PUT_F64_TO_ADDR(addr_tmp, value); \
  324. } while (0)
  325. #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
  326. #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
  327. #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
  328. #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
  329. #define SYNC_ALL_TO_FRAME() \
  330. do { \
  331. frame->ip = frame_ip; \
  332. } while (0)
  333. #define UPDATE_ALL_FROM_FRAME() \
  334. do { \
  335. frame_ip = frame->ip; \
  336. } while (0)
  337. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  338. #define UPDATE_FRAME_IP_END() (void)0
  339. #else
  340. #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  341. #endif
  342. #define RECOVER_CONTEXT(new_frame) \
  343. do { \
  344. frame = (new_frame); \
  345. cur_func = frame->function; \
  346. prev_frame = frame->prev_frame; \
  347. frame_ip = frame->ip; \
  348. UPDATE_FRAME_IP_END(); \
  349. frame_lp = frame->lp; \
  350. } while (0)
  351. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  352. #define GET_OPCODE() opcode = *frame_ip++;
  353. #else
  354. #define GET_OPCODE() \
  355. opcode = *frame_ip; \
  356. frame_ip += 2;
  357. #endif
  358. #define DEF_OP_EQZ(ctype, src_op_type) \
  359. do { \
  360. SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
  361. frame_ip += 4; \
  362. } while (0)
  363. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  364. do { \
  365. SET_OPERAND(I32, 4, \
  366. GET_OPERAND(src_type, src_op_type, 2) \
  367. cond GET_OPERAND(src_type, src_op_type, 0)); \
  368. frame_ip += 6; \
  369. } while (0)
  370. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  371. do { \
  372. SET_OPERAND( \
  373. src_op_type, 2, \
  374. (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
  375. frame_ip += 4; \
  376. } while (0)
  377. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  378. do { \
  379. SET_OPERAND(src_op_type, 4, \
  380. GET_OPERAND(src_type1, src_op_type, 2) \
  381. operation GET_OPERAND(src_type2, src_op_type, 0)); \
  382. frame_ip += 6; \
  383. } while (0)
  384. #define DEF_OP_REINTERPRET(src_type, src_op_type) \
  385. do { \
  386. SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
  387. frame_ip += 4; \
  388. } while (0)
  389. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  390. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  391. do { \
  392. SET_OPERAND(src_op_type, 4, \
  393. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  394. GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
  395. frame_ip += 6; \
  396. } while (0)
  397. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  398. do { \
  399. SET_OPERAND(src_op_type, 4, \
  400. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  401. GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
  402. frame_ip += 6; \
  403. } while (0)
  404. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  405. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  406. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  407. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  408. { \
  409. uint32 readv, sval; \
  410. \
  411. sval = POP_I32(); \
  412. addr = POP_I32(); \
  413. \
  414. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  415. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  416. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  417. \
  418. os_mutex_lock(&node->shared_mem_lock); \
  419. readv = (uint32)(*(uint8 *)maddr); \
  420. *(uint8 *)maddr = (uint8)(readv op sval); \
  421. os_mutex_unlock(&node->shared_mem_lock); \
  422. } \
  423. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  424. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  425. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  426. \
  427. os_mutex_lock(&node->shared_mem_lock); \
  428. readv = (uint32)LOAD_U16(maddr); \
  429. STORE_U16(maddr, (uint16)(readv op sval)); \
  430. os_mutex_unlock(&node->shared_mem_lock); \
  431. } \
  432. else { \
  433. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  434. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  435. \
  436. os_mutex_lock(&node->shared_mem_lock); \
  437. readv = LOAD_I32(maddr); \
  438. STORE_U32(maddr, readv op sval); \
  439. os_mutex_unlock(&node->shared_mem_lock); \
  440. } \
  441. PUSH_I32(readv); \
  442. break; \
  443. } \
  444. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  445. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  446. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  447. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  448. { \
  449. uint64 readv, sval; \
  450. \
  451. sval = (uint64)POP_I64(); \
  452. addr = POP_I32(); \
  453. \
  454. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  455. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  456. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  457. \
  458. os_mutex_lock(&node->shared_mem_lock); \
  459. readv = (uint64)(*(uint8 *)maddr); \
  460. *(uint8 *)maddr = (uint8)(readv op sval); \
  461. os_mutex_unlock(&node->shared_mem_lock); \
  462. } \
  463. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  464. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  465. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  466. \
  467. os_mutex_lock(&node->shared_mem_lock); \
  468. readv = (uint64)LOAD_U16(maddr); \
  469. STORE_U16(maddr, (uint16)(readv op sval)); \
  470. os_mutex_unlock(&node->shared_mem_lock); \
  471. } \
  472. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  473. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  474. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  475. \
  476. os_mutex_lock(&node->shared_mem_lock); \
  477. readv = (uint64)LOAD_U32(maddr); \
  478. STORE_U32(maddr, (uint32)(readv op sval)); \
  479. os_mutex_unlock(&node->shared_mem_lock); \
  480. } \
  481. else { \
  482. uint64 op_result; \
  483. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \
  484. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  485. \
  486. os_mutex_lock(&node->shared_mem_lock); \
  487. readv = (uint64)LOAD_I64(maddr); \
  488. op_result = readv op sval; \
  489. STORE_I64(maddr, op_result); \
  490. os_mutex_unlock(&node->shared_mem_lock); \
  491. } \
  492. PUSH_I64(readv); \
  493. break; \
  494. }
  495. #define DEF_OP_MATH(src_type, src_op_type, method) \
  496. do { \
  497. SET_OPERAND(src_op_type, 2, \
  498. (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
  499. frame_ip += 4; \
  500. } while (0)
  501. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  502. static dst_type func_name(src_type src_value, src_type src_min, \
  503. src_type src_max, dst_type dst_min, \
  504. dst_type dst_max, bool is_sign) \
  505. { \
  506. dst_type dst_value = 0; \
  507. if (!isnan(src_value)) { \
  508. if (src_value <= src_min) \
  509. dst_value = dst_min; \
  510. else if (src_value >= src_max) \
  511. dst_value = dst_max; \
  512. else { \
  513. if (is_sign) \
  514. dst_value = (dst_type)(signed_type)src_value; \
  515. else \
  516. dst_value = (dst_type)src_value; \
  517. } \
  518. } \
  519. return dst_value; \
  520. }
  521. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  522. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  523. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  524. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  525. static bool
  526. trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  527. float32 src_min, float32 src_max, bool saturating, bool is_i32,
  528. bool is_sign)
  529. {
  530. float32 src_value = GET_OPERAND(float32, F32, 0);
  531. uint64 dst_value_i64;
  532. uint32 dst_value_i32;
  533. if (!saturating) {
  534. if (isnan(src_value)) {
  535. wasm_set_exception(module, "invalid conversion to integer");
  536. return false;
  537. }
  538. else if (src_value <= src_min || src_value >= src_max) {
  539. wasm_set_exception(module, "integer overflow");
  540. return false;
  541. }
  542. }
  543. if (is_i32) {
  544. uint32 dst_min = is_sign ? INT32_MIN : 0;
  545. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  546. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  547. dst_max, is_sign);
  548. SET_OPERAND(I32, 2, dst_value_i32);
  549. }
  550. else {
  551. uint64 dst_min = is_sign ? INT64_MIN : 0;
  552. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  553. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  554. dst_max, is_sign);
  555. SET_OPERAND(I64, 2, dst_value_i64);
  556. }
  557. return true;
  558. }
  559. static bool
  560. trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  561. float64 src_min, float64 src_max, bool saturating, bool is_i32,
  562. bool is_sign)
  563. {
  564. float64 src_value = GET_OPERAND(float64, F64, 0);
  565. uint64 dst_value_i64;
  566. uint32 dst_value_i32;
  567. if (!saturating) {
  568. if (isnan(src_value)) {
  569. wasm_set_exception(module, "invalid conversion to integer");
  570. return false;
  571. }
  572. else if (src_value <= src_min || src_value >= src_max) {
  573. wasm_set_exception(module, "integer overflow");
  574. return false;
  575. }
  576. }
  577. if (is_i32) {
  578. uint32 dst_min = is_sign ? INT32_MIN : 0;
  579. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  580. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  581. dst_max, is_sign);
  582. SET_OPERAND(I32, 2, dst_value_i32);
  583. }
  584. else {
  585. uint64 dst_min = is_sign ? INT64_MIN : 0;
  586. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  587. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  588. dst_max, is_sign);
  589. SET_OPERAND(I64, 2, dst_value_i64);
  590. }
  591. return true;
  592. }
  593. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  594. do { \
  595. if (!trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
  596. is_i32, is_sign)) \
  597. goto got_exception; \
  598. frame_ip += 4; \
  599. } while (0)
  600. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  601. do { \
  602. if (!trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
  603. is_i32, is_sign)) \
  604. goto got_exception; \
  605. frame_ip += 4; \
  606. } while (0)
  607. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  608. do { \
  609. (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
  610. is_i32, is_sign); \
  611. frame_ip += 4; \
  612. } while (0)
  613. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  614. do { \
  615. (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
  616. is_i32, is_sign); \
  617. frame_ip += 4; \
  618. } while (0)
  619. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  620. do { \
  621. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  622. PUSH_##dst_op_type(value); \
  623. } while (0)
  624. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  625. #define CELL_SIZE sizeof(uint8)
  626. #else
  627. #define CELL_SIZE (sizeof(uint8) * 2)
  628. #endif
  629. static bool
  630. copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
  631. uint32 total_cell_num, const uint8 *cells,
  632. const int16 *src_offsets, const uint16 *dst_offsets)
  633. {
  634. /* To avoid the overlap issue between src offsets and dst offset,
  635. * we use 2 steps to do the copy. First step, copy the src values
  636. * to a tmp buf. Second step, copy the values from tmp buf to dst.
  637. */
  638. uint32 buf[16] = { 0 }, i;
  639. uint32 *tmp_buf = buf;
  640. uint8 cell;
  641. int16 src, buf_index = 0;
  642. uint16 dst;
  643. /* Allocate memory if the buf is not large enough */
  644. if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
  645. uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
  646. if (total_size >= UINT32_MAX
  647. || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
  648. wasm_set_exception(module, "allocate memory failed");
  649. return false;
  650. }
  651. }
  652. /* 1) Copy values from src to tmp buf */
  653. for (i = 0; i < arity; i++) {
  654. cell = cells[i * CELL_SIZE];
  655. src = src_offsets[i];
  656. if (cell == 1)
  657. tmp_buf[buf_index] = frame_lp[src];
  658. else {
  659. tmp_buf[buf_index] = frame_lp[src];
  660. tmp_buf[buf_index + 1] = frame_lp[src + 1];
  661. }
  662. buf_index += cell;
  663. }
  664. /* 2) Copy values from tmp buf to dest */
  665. buf_index = 0;
  666. for (i = 0; i < arity; i++) {
  667. cell = cells[i * CELL_SIZE];
  668. dst = dst_offsets[i];
  669. if (cell == 1)
  670. frame_lp[dst] = tmp_buf[buf_index];
  671. else {
  672. frame_lp[dst] = tmp_buf[buf_index];
  673. frame_lp[dst + 1] = tmp_buf[buf_index + 1];
  674. }
  675. buf_index += cell;
  676. }
  677. if (tmp_buf != buf) {
  678. wasm_runtime_free(tmp_buf);
  679. }
  680. return true;
  681. }
  682. #define RECOVER_BR_INFO() \
  683. do { \
  684. uint32 arity; \
  685. /* read arity */ \
  686. arity = read_uint32(frame_ip); \
  687. if (arity) { \
  688. uint32 total_cell; \
  689. uint16 *dst_offsets = NULL; \
  690. uint8 *cells; \
  691. int16 *src_offsets = NULL; \
  692. /* read total cell num */ \
  693. total_cell = read_uint32(frame_ip); \
  694. /* cells */ \
  695. cells = (uint8 *)frame_ip; \
  696. frame_ip += arity * CELL_SIZE; \
  697. /* src offsets */ \
  698. src_offsets = (int16 *)frame_ip; \
  699. frame_ip += arity * sizeof(int16); \
  700. /* dst offsets */ \
  701. dst_offsets = (uint16 *)frame_ip; \
  702. frame_ip += arity * sizeof(uint16); \
  703. if (arity == 1) { \
  704. if (cells[0] == 1) \
  705. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  706. else if (cells[0] == 2) { \
  707. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  708. frame_lp[dst_offsets[0] + 1] = \
  709. frame_lp[src_offsets[0] + 1]; \
  710. } \
  711. } \
  712. else { \
  713. if (!copy_stack_values(module, frame_lp, arity, total_cell, \
  714. cells, src_offsets, dst_offsets)) \
  715. goto got_exception; \
  716. } \
  717. } \
  718. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  719. } while (0)
  720. #define SKIP_BR_INFO() \
  721. do { \
  722. uint32 arity; \
  723. /* read and skip arity */ \
  724. arity = read_uint32(frame_ip); \
  725. if (arity) { \
  726. /* skip total cell num */ \
  727. frame_ip += sizeof(uint32); \
  728. /* skip cells, src offsets and dst offsets */ \
  729. frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
  730. } \
  731. /* skip target address */ \
  732. frame_ip += sizeof(uint8 *); \
  733. } while (0)
  734. static inline int32
  735. sign_ext_8_32(int8 val)
  736. {
  737. if (val & 0x80)
  738. return (int32)val | (int32)0xffffff00;
  739. return val;
  740. }
  741. static inline int32
  742. sign_ext_16_32(int16 val)
  743. {
  744. if (val & 0x8000)
  745. return (int32)val | (int32)0xffff0000;
  746. return val;
  747. }
  748. static inline int64
  749. sign_ext_8_64(int8 val)
  750. {
  751. if (val & 0x80)
  752. return (int64)val | (int64)0xffffffffffffff00LL;
  753. return val;
  754. }
  755. static inline int64
  756. sign_ext_16_64(int16 val)
  757. {
  758. if (val & 0x8000)
  759. return (int64)val | (int64)0xffffffffffff0000LL;
  760. return val;
  761. }
  762. static inline int64
  763. sign_ext_32_64(int32 val)
  764. {
  765. if (val & (int32)0x80000000)
  766. return (int64)val | (int64)0xffffffff00000000LL;
  767. return val;
  768. }
  769. static inline void
  770. word_copy(uint32 *dest, uint32 *src, unsigned num)
  771. {
  772. bh_assert(dest != NULL);
  773. bh_assert(src != NULL);
  774. bh_assert(num > 0);
  775. if (dest != src) {
  776. /* No overlap buffer */
  777. bh_assert(!((src < dest) && (dest < src + num)));
  778. for (; num > 0; num--)
  779. *dest++ = *src++;
  780. }
  781. }
  782. static inline WASMInterpFrame *
  783. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  784. {
  785. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  786. if (frame) {
  787. frame->prev_frame = prev_frame;
  788. #if WASM_ENABLE_PERF_PROFILING != 0
  789. frame->time_started = os_time_get_boot_microsecond();
  790. #endif
  791. }
  792. else {
  793. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  794. "wasm operand stack overflow");
  795. }
  796. return frame;
  797. }
  798. static inline void
  799. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  800. {
  801. #if WASM_ENABLE_PERF_PROFILING != 0
  802. if (frame->function) {
  803. frame->function->total_exec_time +=
  804. os_time_get_boot_microsecond() - frame->time_started;
  805. frame->function->total_exec_cnt++;
  806. }
  807. #endif
  808. wasm_exec_env_free_wasm_frame(exec_env, frame);
  809. }
  810. static void
  811. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  812. WASMExecEnv *exec_env,
  813. WASMFunctionInstance *cur_func,
  814. WASMInterpFrame *prev_frame)
  815. {
  816. WASMFunctionImport *func_import = cur_func->u.func_import;
  817. CApiFuncImport *c_api_func_import = NULL;
  818. unsigned local_cell_num = 2;
  819. WASMInterpFrame *frame;
  820. uint32 argv_ret[2], cur_func_index;
  821. void *native_func_pointer = NULL;
  822. bool ret;
  823. if (!(frame = ALLOC_FRAME(exec_env,
  824. wasm_interp_interp_frame_size(local_cell_num),
  825. prev_frame)))
  826. return;
  827. frame->function = cur_func;
  828. frame->ip = NULL;
  829. frame->lp = frame->operand;
  830. wasm_exec_env_set_cur_frame(exec_env, frame);
  831. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  832. bh_assert(cur_func_index < module_inst->module->import_function_count);
  833. if (!func_import->call_conv_wasm_c_api) {
  834. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  835. }
  836. else if (module_inst->e->c_api_func_imports) {
  837. c_api_func_import = module_inst->e->c_api_func_imports + cur_func_index;
  838. native_func_pointer = c_api_func_import->func_ptr_linked;
  839. }
  840. if (!native_func_pointer) {
  841. char buf[128];
  842. snprintf(buf, sizeof(buf),
  843. "failed to call unlinked import function (%s, %s)",
  844. func_import->module_name, func_import->field_name);
  845. wasm_set_exception((WASMModuleInstance *)module_inst, buf);
  846. return;
  847. }
  848. if (func_import->call_conv_wasm_c_api) {
  849. ret = wasm_runtime_invoke_c_api_native(
  850. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  851. func_import->func_type, cur_func->param_cell_num, frame->lp,
  852. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  853. if (ret) {
  854. argv_ret[0] = frame->lp[0];
  855. argv_ret[1] = frame->lp[1];
  856. }
  857. }
  858. else if (!func_import->call_conv_raw) {
  859. ret = wasm_runtime_invoke_native(
  860. exec_env, native_func_pointer, func_import->func_type,
  861. func_import->signature, func_import->attachment, frame->lp,
  862. cur_func->param_cell_num, argv_ret);
  863. }
  864. else {
  865. ret = wasm_runtime_invoke_native_raw(
  866. exec_env, native_func_pointer, func_import->func_type,
  867. func_import->signature, func_import->attachment, frame->lp,
  868. cur_func->param_cell_num, argv_ret);
  869. }
  870. if (!ret)
  871. return;
  872. if (cur_func->ret_cell_num == 1) {
  873. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  874. }
  875. else if (cur_func->ret_cell_num == 2) {
  876. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  877. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  878. }
  879. FREE_FRAME(exec_env, frame);
  880. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  881. }
  882. #if WASM_ENABLE_MULTI_MODULE != 0
  883. static void
  884. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  885. WASMExecEnv *exec_env,
  886. WASMFunctionInstance *cur_func,
  887. WASMInterpFrame *prev_frame);
  888. static void
  889. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  890. WASMExecEnv *exec_env,
  891. WASMFunctionInstance *cur_func,
  892. WASMInterpFrame *prev_frame)
  893. {
  894. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  895. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  896. WASMFunctionImport *func_import = cur_func->u.func_import;
  897. uint8 *ip = prev_frame->ip;
  898. char buf[128];
  899. WASMExecEnv *sub_module_exec_env = NULL;
  900. uint32 aux_stack_origin_boundary = 0;
  901. uint32 aux_stack_origin_bottom = 0;
  902. if (!sub_func_inst) {
  903. snprintf(buf, sizeof(buf),
  904. "failed to call unlinked import function (%s, %s)",
  905. func_import->module_name, func_import->field_name);
  906. wasm_set_exception(module_inst, buf);
  907. return;
  908. }
  909. /* Switch exec_env but keep using the same one by replacing necessary
  910. * variables */
  911. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  912. (WASMModuleInstanceCommon *)sub_module_inst);
  913. if (!sub_module_exec_env) {
  914. wasm_set_exception(module_inst, "create singleton exec_env failed");
  915. return;
  916. }
  917. /* - module_inst */
  918. exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst;
  919. /* - aux_stack_boundary */
  920. aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
  921. exec_env->aux_stack_boundary.boundary =
  922. sub_module_exec_env->aux_stack_boundary.boundary;
  923. /* - aux_stack_bottom */
  924. aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
  925. exec_env->aux_stack_bottom.bottom =
  926. sub_module_exec_env->aux_stack_bottom.bottom;
  927. /* set ip NULL to make call_func_bytecode return after executing
  928. this function */
  929. prev_frame->ip = NULL;
  930. /* call function of sub-module*/
  931. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  932. prev_frame);
  933. /* restore ip and other replaced */
  934. prev_frame->ip = ip;
  935. exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
  936. exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
  937. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  938. /* transfer exception if it is thrown */
  939. if (wasm_copy_exception(sub_module_inst, NULL)) {
  940. bh_memcpy_s(module_inst->cur_exception,
  941. sizeof(module_inst->cur_exception),
  942. sub_module_inst->cur_exception,
  943. sizeof(sub_module_inst->cur_exception));
  944. }
  945. }
  946. #endif
  947. #if WASM_ENABLE_THREAD_MGR != 0
  948. #define CHECK_SUSPEND_FLAGS() \
  949. do { \
  950. os_mutex_lock(&exec_env->wait_lock); \
  951. if (exec_env->suspend_flags.flags != 0) { \
  952. if (exec_env->suspend_flags.flags & 0x01) { \
  953. /* terminate current thread */ \
  954. os_mutex_unlock(&exec_env->wait_lock); \
  955. return; \
  956. } \
  957. /* TODO: support suspend and breakpoint */ \
  958. } \
  959. os_mutex_unlock(&exec_env->wait_lock); \
  960. } while (0)
  961. #endif
  962. #if WASM_ENABLE_OPCODE_COUNTER != 0
  963. typedef struct OpcodeInfo {
  964. char *name;
  965. uint64 count;
  966. } OpcodeInfo;
  967. /* clang-format off */
  968. #define HANDLE_OPCODE(op) \
  969. { \
  970. #op, 0 \
  971. }
  972. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  973. #undef HANDLE_OPCODE
  974. /* clang-format on */
  975. static void
  976. wasm_interp_dump_op_count()
  977. {
  978. uint32 i;
  979. uint64 total_count = 0;
  980. for (i = 0; i < WASM_OP_IMPDEP; i++)
  981. total_count += opcode_table[i].count;
  982. printf("total opcode count: %ld\n", total_count);
  983. for (i = 0; i < WASM_OP_IMPDEP; i++)
  984. if (opcode_table[i].count > 0)
  985. printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  986. opcode_table[i].count,
  987. opcode_table[i].count * 100.0f / total_count);
  988. }
  989. #endif
  990. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  991. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  992. #if WASM_ENABLE_OPCODE_COUNTER != 0
  993. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  994. #else
  995. #define HANDLE_OP(opcode) HANDLE_##opcode:
  996. #endif
  997. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  998. #define FETCH_OPCODE_AND_DISPATCH() \
  999. do { \
  1000. const void *p_label_addr = *(void **)frame_ip; \
  1001. frame_ip += sizeof(void *); \
  1002. goto *p_label_addr; \
  1003. } while (0)
  1004. #else
  1005. #define FETCH_OPCODE_AND_DISPATCH() \
  1006. do { \
  1007. const void *p_label_addr = label_base + *(int16 *)frame_ip; \
  1008. frame_ip += sizeof(int16); \
  1009. goto *p_label_addr; \
  1010. } while (0)
  1011. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  1012. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1013. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1014. #define HANDLE_OP(opcode) case opcode:
  1015. #define HANDLE_OP_END() continue
  1016. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1017. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1018. static void **global_handle_table;
  1019. #endif
  1020. static inline uint8 *
  1021. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1022. {
  1023. #if WASM_ENABLE_MULTI_MODULE == 0
  1024. return global_data + global->data_offset;
  1025. #else
  1026. return global->import_global_inst
  1027. ? global->import_module_inst->global_data
  1028. + global->import_global_inst->data_offset
  1029. : global_data + global->data_offset;
  1030. #endif
  1031. }
  1032. static void
  1033. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1034. WASMExecEnv *exec_env,
  1035. WASMFunctionInstance *cur_func,
  1036. WASMInterpFrame *prev_frame)
  1037. {
  1038. #if WASM_ENABLE_SHARED_MEMORY != 0
  1039. WASMSharedMemNode *node =
  1040. wasm_module_get_shared_memory((WASMModuleCommon *)module->module);
  1041. #else
  1042. void *node = NULL;
  1043. #endif
  1044. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1045. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1046. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1047. || WASM_ENABLE_BULK_MEMORY != 0
  1048. uint32 num_bytes_per_page =
  1049. memory ? wasm_get_num_bytes_per_page(memory, node) : 0;
  1050. uint32 linear_mem_size =
  1051. memory ? wasm_get_linear_memory_size(memory, node) : 0;
  1052. #endif
  1053. uint8 *global_data = module->global_data;
  1054. WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
  1055. WASMGlobalInstance *global;
  1056. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1057. WASMInterpFrame *frame = NULL;
  1058. /* Points to this special opcode so as to jump to the
  1059. * call_method_from_entry. */
  1060. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1061. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1062. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1063. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1064. /* cache of label base addr */
  1065. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  1066. #endif
  1067. #endif
  1068. uint8 *frame_ip_end = frame_ip + 1;
  1069. uint32 cond, count, fidx, tidx, frame_size = 0;
  1070. uint32 all_cell_num = 0;
  1071. int16 addr1, addr2, addr_ret = 0;
  1072. int32 didx, val;
  1073. uint8 *maddr = NULL;
  1074. uint32 local_idx, local_offset, global_idx;
  1075. uint8 opcode, local_type, *global_addr;
  1076. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1077. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1078. DEFINE_GOTO_TABLE(const void *, handle_table);
  1079. #undef HANDLE_OPCODE
  1080. if (exec_env == NULL) {
  1081. global_handle_table = (void **)handle_table;
  1082. return;
  1083. }
  1084. #endif
  1085. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1086. while (frame_ip < frame_ip_end) {
  1087. opcode = *frame_ip++;
  1088. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1089. frame_ip++;
  1090. #endif
  1091. switch (opcode) {
  1092. #else
  1093. goto *handle_table[WASM_OP_IMPDEP];
  1094. #endif
  1095. /* control instructions */
  1096. HANDLE_OP(WASM_OP_UNREACHABLE)
  1097. {
  1098. wasm_set_exception(module, "unreachable");
  1099. goto got_exception;
  1100. }
  1101. HANDLE_OP(WASM_OP_IF)
  1102. {
  1103. cond = (uint32)POP_I32();
  1104. if (cond == 0) {
  1105. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1106. if (else_addr == NULL) {
  1107. frame_ip =
  1108. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1109. }
  1110. else {
  1111. frame_ip = else_addr;
  1112. }
  1113. }
  1114. else {
  1115. frame_ip += sizeof(uint8 *) * 2;
  1116. }
  1117. HANDLE_OP_END();
  1118. }
  1119. HANDLE_OP(WASM_OP_ELSE)
  1120. {
  1121. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1122. HANDLE_OP_END();
  1123. }
  1124. HANDLE_OP(WASM_OP_BR)
  1125. {
  1126. #if WASM_ENABLE_THREAD_MGR != 0
  1127. CHECK_SUSPEND_FLAGS();
  1128. #endif
  1129. recover_br_info:
  1130. RECOVER_BR_INFO();
  1131. HANDLE_OP_END();
  1132. }
  1133. HANDLE_OP(WASM_OP_BR_IF)
  1134. {
  1135. #if WASM_ENABLE_THREAD_MGR != 0
  1136. CHECK_SUSPEND_FLAGS();
  1137. #endif
  1138. cond = frame_lp[GET_OFFSET()];
  1139. if (cond)
  1140. goto recover_br_info;
  1141. else
  1142. SKIP_BR_INFO();
  1143. HANDLE_OP_END();
  1144. }
  1145. HANDLE_OP(WASM_OP_BR_TABLE)
  1146. {
  1147. uint32 arity, br_item_size;
  1148. #if WASM_ENABLE_THREAD_MGR != 0
  1149. CHECK_SUSPEND_FLAGS();
  1150. #endif
  1151. count = read_uint32(frame_ip);
  1152. didx = GET_OPERAND(uint32, I32, 0);
  1153. frame_ip += 2;
  1154. if (!(didx >= 0 && (uint32)didx < count))
  1155. didx = count;
  1156. /* all br items must have the same arity and item size,
  1157. so we only calculate the first item size */
  1158. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1159. br_item_size = sizeof(uint32); /* arity */
  1160. if (arity) {
  1161. /* total cell num */
  1162. br_item_size += sizeof(uint32);
  1163. /* cells, src offsets and dst offsets */
  1164. br_item_size +=
  1165. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1166. }
  1167. /* target address */
  1168. br_item_size += sizeof(uint8 *);
  1169. frame_ip += br_item_size * didx;
  1170. goto recover_br_info;
  1171. }
  1172. HANDLE_OP(WASM_OP_RETURN)
  1173. {
  1174. uint32 ret_idx;
  1175. WASMType *func_type;
  1176. uint32 off, ret_offset;
  1177. uint8 *ret_types;
  1178. if (cur_func->is_import_func)
  1179. func_type = cur_func->u.func_import->func_type;
  1180. else
  1181. func_type = cur_func->u.func->func_type;
  1182. /* types of each return value */
  1183. ret_types = func_type->types + func_type->param_count;
  1184. ret_offset = prev_frame->ret_offset;
  1185. for (ret_idx = 0,
  1186. off = sizeof(int16) * (func_type->result_count - 1);
  1187. ret_idx < func_type->result_count;
  1188. ret_idx++, off -= sizeof(int16)) {
  1189. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1190. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1191. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1192. GET_OPERAND(uint64, I64, off));
  1193. ret_offset += 2;
  1194. }
  1195. else {
  1196. prev_frame->lp[ret_offset] =
  1197. GET_OPERAND(uint32, I32, off);
  1198. ret_offset++;
  1199. }
  1200. }
  1201. goto return_func;
  1202. }
  1203. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1204. #if WASM_ENABLE_TAIL_CALL != 0
  1205. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1206. #endif
  1207. {
  1208. WASMType *cur_type, *cur_func_type;
  1209. WASMTableInstance *tbl_inst;
  1210. uint32 tbl_idx;
  1211. #if WASM_ENABLE_TAIL_CALL != 0
  1212. GET_OPCODE();
  1213. #endif
  1214. #if WASM_ENABLE_THREAD_MGR != 0
  1215. CHECK_SUSPEND_FLAGS();
  1216. #endif
  1217. tidx = read_uint32(frame_ip);
  1218. cur_type = module->module->types[tidx];
  1219. tbl_idx = read_uint32(frame_ip);
  1220. bh_assert(tbl_idx < module->table_count);
  1221. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1222. val = GET_OPERAND(uint32, I32, 0);
  1223. frame_ip += 2;
  1224. if ((uint32)val >= tbl_inst->cur_size) {
  1225. wasm_set_exception(module, "undefined element");
  1226. goto got_exception;
  1227. }
  1228. fidx = tbl_inst->elems[val];
  1229. if (fidx == NULL_REF) {
  1230. wasm_set_exception(module, "uninitialized element");
  1231. goto got_exception;
  1232. }
  1233. /*
  1234. * we might be using a table injected by host or
  1235. * another module. in that case, we don't validate
  1236. * the elem value while loading
  1237. */
  1238. if (fidx >= module->e->function_count) {
  1239. wasm_set_exception(module, "unknown function");
  1240. goto got_exception;
  1241. }
  1242. /* always call module own functions */
  1243. cur_func = module->e->functions + fidx;
  1244. if (cur_func->is_import_func)
  1245. cur_func_type = cur_func->u.func_import->func_type;
  1246. else
  1247. cur_func_type = cur_func->u.func->func_type;
  1248. if (cur_type != cur_func_type) {
  1249. wasm_set_exception(module, "indirect call type mismatch");
  1250. goto got_exception;
  1251. }
  1252. #if WASM_ENABLE_TAIL_CALL != 0
  1253. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1254. goto call_func_from_return_call;
  1255. #endif
  1256. goto call_func_from_interp;
  1257. }
  1258. /* parametric instructions */
  1259. HANDLE_OP(WASM_OP_SELECT)
  1260. {
  1261. cond = frame_lp[GET_OFFSET()];
  1262. addr1 = GET_OFFSET();
  1263. addr2 = GET_OFFSET();
  1264. addr_ret = GET_OFFSET();
  1265. if (!cond) {
  1266. if (addr_ret != addr1)
  1267. frame_lp[addr_ret] = frame_lp[addr1];
  1268. }
  1269. else {
  1270. if (addr_ret != addr2)
  1271. frame_lp[addr_ret] = frame_lp[addr2];
  1272. }
  1273. HANDLE_OP_END();
  1274. }
  1275. HANDLE_OP(WASM_OP_SELECT_64)
  1276. {
  1277. cond = frame_lp[GET_OFFSET()];
  1278. addr1 = GET_OFFSET();
  1279. addr2 = GET_OFFSET();
  1280. addr_ret = GET_OFFSET();
  1281. if (!cond) {
  1282. if (addr_ret != addr1)
  1283. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1284. GET_I64_FROM_ADDR(frame_lp + addr1));
  1285. }
  1286. else {
  1287. if (addr_ret != addr2)
  1288. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1289. GET_I64_FROM_ADDR(frame_lp + addr2));
  1290. }
  1291. HANDLE_OP_END();
  1292. }
  1293. #if WASM_ENABLE_REF_TYPES != 0
  1294. HANDLE_OP(WASM_OP_TABLE_GET)
  1295. {
  1296. uint32 tbl_idx, elem_idx;
  1297. WASMTableInstance *tbl_inst;
  1298. tbl_idx = read_uint32(frame_ip);
  1299. bh_assert(tbl_idx < module->table_count);
  1300. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1301. elem_idx = POP_I32();
  1302. if (elem_idx >= tbl_inst->cur_size) {
  1303. wasm_set_exception(module, "out of bounds table access");
  1304. goto got_exception;
  1305. }
  1306. PUSH_I32(tbl_inst->elems[elem_idx]);
  1307. HANDLE_OP_END();
  1308. }
  1309. HANDLE_OP(WASM_OP_TABLE_SET)
  1310. {
  1311. uint32 tbl_idx, elem_idx, elem_val;
  1312. WASMTableInstance *tbl_inst;
  1313. tbl_idx = read_uint32(frame_ip);
  1314. bh_assert(tbl_idx < module->table_count);
  1315. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1316. elem_val = POP_I32();
  1317. elem_idx = POP_I32();
  1318. if (elem_idx >= tbl_inst->cur_size) {
  1319. wasm_set_exception(module, "out of bounds table access");
  1320. goto got_exception;
  1321. }
  1322. tbl_inst->elems[elem_idx] = elem_val;
  1323. HANDLE_OP_END();
  1324. }
  1325. HANDLE_OP(WASM_OP_REF_NULL)
  1326. {
  1327. PUSH_I32(NULL_REF);
  1328. HANDLE_OP_END();
  1329. }
  1330. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1331. {
  1332. uint32 ref_val;
  1333. ref_val = POP_I32();
  1334. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1335. HANDLE_OP_END();
  1336. }
  1337. HANDLE_OP(WASM_OP_REF_FUNC)
  1338. {
  1339. uint32 func_idx = read_uint32(frame_ip);
  1340. PUSH_I32(func_idx);
  1341. HANDLE_OP_END();
  1342. }
  1343. #endif /* WASM_ENABLE_REF_TYPES */
  1344. /* variable instructions */
  1345. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  1346. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  1347. {
  1348. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1349. local_offset = *frame_ip++;
  1350. #else
  1351. /* clang-format off */
  1352. local_offset = *frame_ip;
  1353. frame_ip += 2;
  1354. /* clang-format on */
  1355. #endif
  1356. *(uint32 *)(frame_lp + local_offset) =
  1357. GET_OPERAND(uint32, I32, 0);
  1358. frame_ip += 2;
  1359. HANDLE_OP_END();
  1360. }
  1361. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  1362. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  1363. {
  1364. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1365. local_offset = *frame_ip++;
  1366. #else
  1367. /* clang-format off */
  1368. local_offset = *frame_ip;
  1369. frame_ip += 2;
  1370. /* clang-format on */
  1371. #endif
  1372. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  1373. GET_OPERAND(uint64, I64, 0));
  1374. frame_ip += 2;
  1375. HANDLE_OP_END();
  1376. }
  1377. HANDLE_OP(WASM_OP_GET_GLOBAL)
  1378. {
  1379. global_idx = read_uint32(frame_ip);
  1380. bh_assert(global_idx < module->e->global_count);
  1381. global = globals + global_idx;
  1382. global_addr = get_global_addr(global_data, global);
  1383. addr_ret = GET_OFFSET();
  1384. frame_lp[addr_ret] = *(uint32 *)global_addr;
  1385. HANDLE_OP_END();
  1386. }
  1387. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  1388. {
  1389. global_idx = read_uint32(frame_ip);
  1390. bh_assert(global_idx < module->e->global_count);
  1391. global = globals + global_idx;
  1392. global_addr = get_global_addr(global_data, global);
  1393. addr_ret = GET_OFFSET();
  1394. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1395. GET_I64_FROM_ADDR((uint32 *)global_addr));
  1396. HANDLE_OP_END();
  1397. }
  1398. HANDLE_OP(WASM_OP_SET_GLOBAL)
  1399. {
  1400. global_idx = read_uint32(frame_ip);
  1401. bh_assert(global_idx < module->e->global_count);
  1402. global = globals + global_idx;
  1403. global_addr = get_global_addr(global_data, global);
  1404. addr1 = GET_OFFSET();
  1405. *(int32 *)global_addr = frame_lp[addr1];
  1406. HANDLE_OP_END();
  1407. }
  1408. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  1409. {
  1410. uint32 aux_stack_top;
  1411. global_idx = read_uint32(frame_ip);
  1412. bh_assert(global_idx < module->e->global_count);
  1413. global = globals + global_idx;
  1414. global_addr = get_global_addr(global_data, global);
  1415. aux_stack_top = frame_lp[GET_OFFSET()];
  1416. if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
  1417. wasm_set_exception(module, "wasm auxiliary stack overflow");
  1418. goto got_exception;
  1419. }
  1420. if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
  1421. wasm_set_exception(module,
  1422. "wasm auxiliary stack underflow");
  1423. goto got_exception;
  1424. }
  1425. *(int32 *)global_addr = aux_stack_top;
  1426. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1427. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  1428. uint32 aux_stack_used = module->module->aux_stack_bottom
  1429. - *(uint32 *)global_addr;
  1430. if (aux_stack_used > module->e->max_aux_stack_used)
  1431. module->e->max_aux_stack_used = aux_stack_used;
  1432. }
  1433. #endif
  1434. HANDLE_OP_END();
  1435. }
  1436. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  1437. {
  1438. global_idx = read_uint32(frame_ip);
  1439. bh_assert(global_idx < module->e->global_count);
  1440. global = globals + global_idx;
  1441. global_addr = get_global_addr(global_data, global);
  1442. addr1 = GET_OFFSET();
  1443. PUT_I64_TO_ADDR((uint32 *)global_addr,
  1444. GET_I64_FROM_ADDR(frame_lp + addr1));
  1445. HANDLE_OP_END();
  1446. }
  1447. /* memory load instructions */
  1448. HANDLE_OP(WASM_OP_I32_LOAD)
  1449. {
  1450. uint32 offset, addr;
  1451. offset = read_uint32(frame_ip);
  1452. addr = GET_OPERAND(uint32, I32, 0);
  1453. frame_ip += 2;
  1454. addr_ret = GET_OFFSET();
  1455. CHECK_MEMORY_OVERFLOW(4);
  1456. frame_lp[addr_ret] = LOAD_I32(maddr);
  1457. HANDLE_OP_END();
  1458. }
  1459. HANDLE_OP(WASM_OP_I64_LOAD)
  1460. {
  1461. uint32 offset, addr;
  1462. offset = read_uint32(frame_ip);
  1463. addr = GET_OPERAND(uint32, I32, 0);
  1464. frame_ip += 2;
  1465. addr_ret = GET_OFFSET();
  1466. CHECK_MEMORY_OVERFLOW(8);
  1467. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  1468. HANDLE_OP_END();
  1469. }
  1470. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  1471. {
  1472. uint32 offset, addr;
  1473. offset = read_uint32(frame_ip);
  1474. addr = GET_OPERAND(uint32, I32, 0);
  1475. frame_ip += 2;
  1476. addr_ret = GET_OFFSET();
  1477. CHECK_MEMORY_OVERFLOW(1);
  1478. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  1479. HANDLE_OP_END();
  1480. }
  1481. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  1482. {
  1483. uint32 offset, addr;
  1484. offset = read_uint32(frame_ip);
  1485. addr = GET_OPERAND(uint32, I32, 0);
  1486. frame_ip += 2;
  1487. addr_ret = GET_OFFSET();
  1488. CHECK_MEMORY_OVERFLOW(1);
  1489. frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr);
  1490. HANDLE_OP_END();
  1491. }
  1492. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  1493. {
  1494. uint32 offset, addr;
  1495. offset = read_uint32(frame_ip);
  1496. addr = GET_OPERAND(uint32, I32, 0);
  1497. frame_ip += 2;
  1498. addr_ret = GET_OFFSET();
  1499. CHECK_MEMORY_OVERFLOW(2);
  1500. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  1501. HANDLE_OP_END();
  1502. }
  1503. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  1504. {
  1505. uint32 offset, addr;
  1506. offset = read_uint32(frame_ip);
  1507. addr = GET_OPERAND(uint32, I32, 0);
  1508. frame_ip += 2;
  1509. addr_ret = GET_OFFSET();
  1510. CHECK_MEMORY_OVERFLOW(2);
  1511. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  1512. HANDLE_OP_END();
  1513. }
  1514. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  1515. {
  1516. uint32 offset, addr;
  1517. offset = read_uint32(frame_ip);
  1518. addr = GET_OPERAND(uint32, I32, 0);
  1519. frame_ip += 2;
  1520. addr_ret = GET_OFFSET();
  1521. CHECK_MEMORY_OVERFLOW(1);
  1522. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1523. sign_ext_8_64(*(int8 *)maddr));
  1524. HANDLE_OP_END();
  1525. }
  1526. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  1527. {
  1528. uint32 offset, addr;
  1529. offset = read_uint32(frame_ip);
  1530. addr = GET_OPERAND(uint32, I32, 0);
  1531. frame_ip += 2;
  1532. addr_ret = GET_OFFSET();
  1533. CHECK_MEMORY_OVERFLOW(1);
  1534. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  1535. HANDLE_OP_END();
  1536. }
  1537. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  1538. {
  1539. uint32 offset, addr;
  1540. offset = read_uint32(frame_ip);
  1541. addr = GET_OPERAND(uint32, I32, 0);
  1542. frame_ip += 2;
  1543. addr_ret = GET_OFFSET();
  1544. CHECK_MEMORY_OVERFLOW(2);
  1545. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1546. sign_ext_16_64(LOAD_I16(maddr)));
  1547. HANDLE_OP_END();
  1548. }
  1549. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  1550. {
  1551. uint32 offset, addr;
  1552. offset = read_uint32(frame_ip);
  1553. addr = GET_OPERAND(uint32, I32, 0);
  1554. frame_ip += 2;
  1555. addr_ret = GET_OFFSET();
  1556. CHECK_MEMORY_OVERFLOW(2);
  1557. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  1558. HANDLE_OP_END();
  1559. }
  1560. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  1561. {
  1562. uint32 offset, addr;
  1563. offset = read_uint32(frame_ip);
  1564. addr = GET_OPERAND(uint32, I32, 0);
  1565. frame_ip += 2;
  1566. addr_ret = GET_OFFSET();
  1567. CHECK_MEMORY_OVERFLOW(4);
  1568. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1569. sign_ext_32_64(LOAD_I32(maddr)));
  1570. HANDLE_OP_END();
  1571. }
  1572. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  1573. {
  1574. uint32 offset, addr;
  1575. offset = read_uint32(frame_ip);
  1576. addr = GET_OPERAND(uint32, I32, 0);
  1577. frame_ip += 2;
  1578. addr_ret = GET_OFFSET();
  1579. CHECK_MEMORY_OVERFLOW(4);
  1580. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  1581. HANDLE_OP_END();
  1582. }
  1583. HANDLE_OP(WASM_OP_I32_STORE)
  1584. {
  1585. uint32 offset, addr;
  1586. uint32 sval;
  1587. offset = read_uint32(frame_ip);
  1588. sval = GET_OPERAND(uint32, I32, 0);
  1589. addr = GET_OPERAND(uint32, I32, 2);
  1590. frame_ip += 4;
  1591. CHECK_MEMORY_OVERFLOW(4);
  1592. STORE_U32(maddr, sval);
  1593. HANDLE_OP_END();
  1594. }
  1595. HANDLE_OP(WASM_OP_I32_STORE8)
  1596. {
  1597. uint32 offset, addr;
  1598. uint32 sval;
  1599. offset = read_uint32(frame_ip);
  1600. sval = GET_OPERAND(uint32, I32, 0);
  1601. addr = GET_OPERAND(uint32, I32, 2);
  1602. frame_ip += 4;
  1603. CHECK_MEMORY_OVERFLOW(1);
  1604. *(uint8 *)maddr = (uint8)sval;
  1605. HANDLE_OP_END();
  1606. }
  1607. HANDLE_OP(WASM_OP_I32_STORE16)
  1608. {
  1609. uint32 offset, addr;
  1610. uint32 sval;
  1611. offset = read_uint32(frame_ip);
  1612. sval = GET_OPERAND(uint32, I32, 0);
  1613. addr = GET_OPERAND(uint32, I32, 2);
  1614. frame_ip += 4;
  1615. CHECK_MEMORY_OVERFLOW(2);
  1616. STORE_U16(maddr, (uint16)sval);
  1617. HANDLE_OP_END();
  1618. }
  1619. HANDLE_OP(WASM_OP_I64_STORE)
  1620. {
  1621. uint32 offset, addr;
  1622. uint64 sval;
  1623. offset = read_uint32(frame_ip);
  1624. sval = GET_OPERAND(uint64, I64, 0);
  1625. addr = GET_OPERAND(uint32, I32, 2);
  1626. frame_ip += 4;
  1627. CHECK_MEMORY_OVERFLOW(8);
  1628. STORE_I64(maddr, sval);
  1629. HANDLE_OP_END();
  1630. }
  1631. HANDLE_OP(WASM_OP_I64_STORE8)
  1632. {
  1633. uint32 offset, addr;
  1634. uint64 sval;
  1635. offset = read_uint32(frame_ip);
  1636. sval = GET_OPERAND(uint64, I64, 0);
  1637. addr = GET_OPERAND(uint32, I32, 2);
  1638. frame_ip += 4;
  1639. CHECK_MEMORY_OVERFLOW(1);
  1640. *(uint8 *)maddr = (uint8)sval;
  1641. HANDLE_OP_END();
  1642. }
  1643. HANDLE_OP(WASM_OP_I64_STORE16)
  1644. {
  1645. uint32 offset, addr;
  1646. uint64 sval;
  1647. offset = read_uint32(frame_ip);
  1648. sval = GET_OPERAND(uint64, I64, 0);
  1649. addr = GET_OPERAND(uint32, I32, 2);
  1650. frame_ip += 4;
  1651. CHECK_MEMORY_OVERFLOW(2);
  1652. STORE_U16(maddr, (uint16)sval);
  1653. HANDLE_OP_END();
  1654. }
  1655. HANDLE_OP(WASM_OP_I64_STORE32)
  1656. {
  1657. uint32 offset, addr;
  1658. uint64 sval;
  1659. offset = read_uint32(frame_ip);
  1660. sval = GET_OPERAND(uint64, I64, 0);
  1661. addr = GET_OPERAND(uint32, I32, 2);
  1662. frame_ip += 4;
  1663. CHECK_MEMORY_OVERFLOW(4);
  1664. STORE_U32(maddr, (uint32)sval);
  1665. HANDLE_OP_END();
  1666. }
  1667. /* memory size and memory grow instructions */
  1668. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  1669. {
  1670. uint32 reserved;
  1671. addr_ret = GET_OFFSET();
  1672. frame_lp[addr_ret] = memory->cur_page_count;
  1673. (void)reserved;
  1674. HANDLE_OP_END();
  1675. }
  1676. HANDLE_OP(WASM_OP_MEMORY_GROW)
  1677. {
  1678. uint32 reserved, delta,
  1679. prev_page_count = memory->cur_page_count;
  1680. addr1 = GET_OFFSET();
  1681. addr_ret = GET_OFFSET();
  1682. delta = (uint32)frame_lp[addr1];
  1683. if (!wasm_enlarge_memory(module, delta)) {
  1684. /* failed to memory.grow, return -1 */
  1685. frame_lp[addr_ret] = -1;
  1686. }
  1687. else {
  1688. /* success, return previous page count */
  1689. frame_lp[addr_ret] = prev_page_count;
  1690. /* update memory size, no need to update memory ptr as
  1691. it isn't changed in wasm_enlarge_memory */
  1692. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1693. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1694. || WASM_ENABLE_BULK_MEMORY != 0
  1695. linear_mem_size =
  1696. num_bytes_per_page * memory->cur_page_count;
  1697. #endif
  1698. }
  1699. (void)reserved;
  1700. HANDLE_OP_END();
  1701. }
  1702. /* constant instructions */
  1703. HANDLE_OP(WASM_OP_F64_CONST)
  1704. HANDLE_OP(WASM_OP_I64_CONST)
  1705. {
  1706. uint8 *orig_ip = frame_ip;
  1707. frame_ip += sizeof(uint64);
  1708. addr_ret = GET_OFFSET();
  1709. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  1710. sizeof(uint64));
  1711. HANDLE_OP_END();
  1712. }
  1713. HANDLE_OP(WASM_OP_F32_CONST)
  1714. HANDLE_OP(WASM_OP_I32_CONST)
  1715. {
  1716. uint8 *orig_ip = frame_ip;
  1717. frame_ip += sizeof(uint32);
  1718. addr_ret = GET_OFFSET();
  1719. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  1720. sizeof(uint32));
  1721. HANDLE_OP_END();
  1722. }
  1723. /* comparison instructions of i32 */
  1724. HANDLE_OP(WASM_OP_I32_EQZ)
  1725. {
  1726. DEF_OP_EQZ(int32, I32);
  1727. HANDLE_OP_END();
  1728. }
  1729. HANDLE_OP(WASM_OP_I32_EQ)
  1730. {
  1731. DEF_OP_CMP(uint32, I32, ==);
  1732. HANDLE_OP_END();
  1733. }
  1734. HANDLE_OP(WASM_OP_I32_NE)
  1735. {
  1736. DEF_OP_CMP(uint32, I32, !=);
  1737. HANDLE_OP_END();
  1738. }
  1739. HANDLE_OP(WASM_OP_I32_LT_S)
  1740. {
  1741. DEF_OP_CMP(int32, I32, <);
  1742. HANDLE_OP_END();
  1743. }
  1744. HANDLE_OP(WASM_OP_I32_LT_U)
  1745. {
  1746. DEF_OP_CMP(uint32, I32, <);
  1747. HANDLE_OP_END();
  1748. }
  1749. HANDLE_OP(WASM_OP_I32_GT_S)
  1750. {
  1751. DEF_OP_CMP(int32, I32, >);
  1752. HANDLE_OP_END();
  1753. }
  1754. HANDLE_OP(WASM_OP_I32_GT_U)
  1755. {
  1756. DEF_OP_CMP(uint32, I32, >);
  1757. HANDLE_OP_END();
  1758. }
  1759. HANDLE_OP(WASM_OP_I32_LE_S)
  1760. {
  1761. DEF_OP_CMP(int32, I32, <=);
  1762. HANDLE_OP_END();
  1763. }
  1764. HANDLE_OP(WASM_OP_I32_LE_U)
  1765. {
  1766. DEF_OP_CMP(uint32, I32, <=);
  1767. HANDLE_OP_END();
  1768. }
  1769. HANDLE_OP(WASM_OP_I32_GE_S)
  1770. {
  1771. DEF_OP_CMP(int32, I32, >=);
  1772. HANDLE_OP_END();
  1773. }
  1774. HANDLE_OP(WASM_OP_I32_GE_U)
  1775. {
  1776. DEF_OP_CMP(uint32, I32, >=);
  1777. HANDLE_OP_END();
  1778. }
  1779. /* comparison instructions of i64 */
  1780. HANDLE_OP(WASM_OP_I64_EQZ)
  1781. {
  1782. DEF_OP_EQZ(int64, I64);
  1783. HANDLE_OP_END();
  1784. }
  1785. HANDLE_OP(WASM_OP_I64_EQ)
  1786. {
  1787. DEF_OP_CMP(uint64, I64, ==);
  1788. HANDLE_OP_END();
  1789. }
  1790. HANDLE_OP(WASM_OP_I64_NE)
  1791. {
  1792. DEF_OP_CMP(uint64, I64, !=);
  1793. HANDLE_OP_END();
  1794. }
  1795. HANDLE_OP(WASM_OP_I64_LT_S)
  1796. {
  1797. DEF_OP_CMP(int64, I64, <);
  1798. HANDLE_OP_END();
  1799. }
  1800. HANDLE_OP(WASM_OP_I64_LT_U)
  1801. {
  1802. DEF_OP_CMP(uint64, I64, <);
  1803. HANDLE_OP_END();
  1804. }
  1805. HANDLE_OP(WASM_OP_I64_GT_S)
  1806. {
  1807. DEF_OP_CMP(int64, I64, >);
  1808. HANDLE_OP_END();
  1809. }
  1810. HANDLE_OP(WASM_OP_I64_GT_U)
  1811. {
  1812. DEF_OP_CMP(uint64, I64, >);
  1813. HANDLE_OP_END();
  1814. }
  1815. HANDLE_OP(WASM_OP_I64_LE_S)
  1816. {
  1817. DEF_OP_CMP(int64, I64, <=);
  1818. HANDLE_OP_END();
  1819. }
  1820. HANDLE_OP(WASM_OP_I64_LE_U)
  1821. {
  1822. DEF_OP_CMP(uint64, I64, <=);
  1823. HANDLE_OP_END();
  1824. }
  1825. HANDLE_OP(WASM_OP_I64_GE_S)
  1826. {
  1827. DEF_OP_CMP(int64, I64, >=);
  1828. HANDLE_OP_END();
  1829. }
  1830. HANDLE_OP(WASM_OP_I64_GE_U)
  1831. {
  1832. DEF_OP_CMP(uint64, I64, >=);
  1833. HANDLE_OP_END();
  1834. }
  1835. /* comparison instructions of f32 */
  1836. HANDLE_OP(WASM_OP_F32_EQ)
  1837. {
  1838. DEF_OP_CMP(float32, F32, ==);
  1839. HANDLE_OP_END();
  1840. }
  1841. HANDLE_OP(WASM_OP_F32_NE)
  1842. {
  1843. DEF_OP_CMP(float32, F32, !=);
  1844. HANDLE_OP_END();
  1845. }
  1846. HANDLE_OP(WASM_OP_F32_LT)
  1847. {
  1848. DEF_OP_CMP(float32, F32, <);
  1849. HANDLE_OP_END();
  1850. }
  1851. HANDLE_OP(WASM_OP_F32_GT)
  1852. {
  1853. DEF_OP_CMP(float32, F32, >);
  1854. HANDLE_OP_END();
  1855. }
  1856. HANDLE_OP(WASM_OP_F32_LE)
  1857. {
  1858. DEF_OP_CMP(float32, F32, <=);
  1859. HANDLE_OP_END();
  1860. }
  1861. HANDLE_OP(WASM_OP_F32_GE)
  1862. {
  1863. DEF_OP_CMP(float32, F32, >=);
  1864. HANDLE_OP_END();
  1865. }
  1866. /* comparison instructions of f64 */
  1867. HANDLE_OP(WASM_OP_F64_EQ)
  1868. {
  1869. DEF_OP_CMP(float64, F64, ==);
  1870. HANDLE_OP_END();
  1871. }
  1872. HANDLE_OP(WASM_OP_F64_NE)
  1873. {
  1874. DEF_OP_CMP(float64, F64, !=);
  1875. HANDLE_OP_END();
  1876. }
  1877. HANDLE_OP(WASM_OP_F64_LT)
  1878. {
  1879. DEF_OP_CMP(float64, F64, <);
  1880. HANDLE_OP_END();
  1881. }
  1882. HANDLE_OP(WASM_OP_F64_GT)
  1883. {
  1884. DEF_OP_CMP(float64, F64, >);
  1885. HANDLE_OP_END();
  1886. }
  1887. HANDLE_OP(WASM_OP_F64_LE)
  1888. {
  1889. DEF_OP_CMP(float64, F64, <=);
  1890. HANDLE_OP_END();
  1891. }
  1892. HANDLE_OP(WASM_OP_F64_GE)
  1893. {
  1894. DEF_OP_CMP(float64, F64, >=);
  1895. HANDLE_OP_END();
  1896. }
  1897. /* numberic instructions of i32 */
  1898. HANDLE_OP(WASM_OP_I32_CLZ)
  1899. {
  1900. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  1901. HANDLE_OP_END();
  1902. }
  1903. HANDLE_OP(WASM_OP_I32_CTZ)
  1904. {
  1905. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  1906. HANDLE_OP_END();
  1907. }
  1908. HANDLE_OP(WASM_OP_I32_POPCNT)
  1909. {
  1910. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  1911. HANDLE_OP_END();
  1912. }
  1913. HANDLE_OP(WASM_OP_I32_ADD)
  1914. {
  1915. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  1916. HANDLE_OP_END();
  1917. }
  1918. HANDLE_OP(WASM_OP_I32_SUB)
  1919. {
  1920. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  1921. HANDLE_OP_END();
  1922. }
  1923. HANDLE_OP(WASM_OP_I32_MUL)
  1924. {
  1925. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  1926. HANDLE_OP_END();
  1927. }
  1928. HANDLE_OP(WASM_OP_I32_DIV_S)
  1929. {
  1930. int32 a, b;
  1931. b = frame_lp[GET_OFFSET()];
  1932. a = frame_lp[GET_OFFSET()];
  1933. addr_ret = GET_OFFSET();
  1934. if (a == (int32)0x80000000 && b == -1) {
  1935. wasm_set_exception(module, "integer overflow");
  1936. goto got_exception;
  1937. }
  1938. if (b == 0) {
  1939. wasm_set_exception(module, "integer divide by zero");
  1940. goto got_exception;
  1941. }
  1942. frame_lp[addr_ret] = (a / b);
  1943. HANDLE_OP_END();
  1944. }
  1945. HANDLE_OP(WASM_OP_I32_DIV_U)
  1946. {
  1947. uint32 a, b;
  1948. addr1 = GET_OFFSET();
  1949. addr2 = GET_OFFSET();
  1950. addr_ret = GET_OFFSET();
  1951. b = (uint32)frame_lp[addr1];
  1952. a = (uint32)frame_lp[addr2];
  1953. if (b == 0) {
  1954. wasm_set_exception(module, "integer divide by zero");
  1955. goto got_exception;
  1956. }
  1957. frame_lp[addr_ret] = (a / b);
  1958. HANDLE_OP_END();
  1959. }
  1960. HANDLE_OP(WASM_OP_I32_REM_S)
  1961. {
  1962. int32 a, b;
  1963. addr1 = GET_OFFSET();
  1964. addr2 = GET_OFFSET();
  1965. addr_ret = GET_OFFSET();
  1966. b = frame_lp[addr1];
  1967. a = frame_lp[addr2];
  1968. if (a == (int32)0x80000000 && b == -1) {
  1969. frame_lp[addr_ret] = 0;
  1970. HANDLE_OP_END();
  1971. }
  1972. if (b == 0) {
  1973. wasm_set_exception(module, "integer divide by zero");
  1974. goto got_exception;
  1975. }
  1976. frame_lp[addr_ret] = (a % b);
  1977. HANDLE_OP_END();
  1978. }
  1979. HANDLE_OP(WASM_OP_I32_REM_U)
  1980. {
  1981. uint32 a, b;
  1982. addr1 = GET_OFFSET();
  1983. addr2 = GET_OFFSET();
  1984. addr_ret = GET_OFFSET();
  1985. b = (uint32)frame_lp[addr1];
  1986. a = (uint32)frame_lp[addr2];
  1987. if (b == 0) {
  1988. wasm_set_exception(module, "integer divide by zero");
  1989. goto got_exception;
  1990. }
  1991. frame_lp[addr_ret] = (a % b);
  1992. HANDLE_OP_END();
  1993. }
  1994. HANDLE_OP(WASM_OP_I32_AND)
  1995. {
  1996. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  1997. HANDLE_OP_END();
  1998. }
  1999. HANDLE_OP(WASM_OP_I32_OR)
  2000. {
  2001. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  2002. HANDLE_OP_END();
  2003. }
  2004. HANDLE_OP(WASM_OP_I32_XOR)
  2005. {
  2006. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  2007. HANDLE_OP_END();
  2008. }
  2009. HANDLE_OP(WASM_OP_I32_SHL)
  2010. {
  2011. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  2012. HANDLE_OP_END();
  2013. }
  2014. HANDLE_OP(WASM_OP_I32_SHR_S)
  2015. {
  2016. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  2017. HANDLE_OP_END();
  2018. }
  2019. HANDLE_OP(WASM_OP_I32_SHR_U)
  2020. {
  2021. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  2022. HANDLE_OP_END();
  2023. }
  2024. HANDLE_OP(WASM_OP_I32_ROTL)
  2025. {
  2026. uint32 a, b;
  2027. b = (uint32)frame_lp[GET_OFFSET()];
  2028. a = (uint32)frame_lp[GET_OFFSET()];
  2029. frame_lp[GET_OFFSET()] = rotl32(a, b);
  2030. HANDLE_OP_END();
  2031. }
  2032. HANDLE_OP(WASM_OP_I32_ROTR)
  2033. {
  2034. uint32 a, b;
  2035. b = (uint32)frame_lp[GET_OFFSET()];
  2036. a = (uint32)frame_lp[GET_OFFSET()];
  2037. frame_lp[GET_OFFSET()] = rotr32(a, b);
  2038. HANDLE_OP_END();
  2039. }
  2040. /* numberic instructions of i64 */
  2041. HANDLE_OP(WASM_OP_I64_CLZ)
  2042. {
  2043. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  2044. HANDLE_OP_END();
  2045. }
  2046. HANDLE_OP(WASM_OP_I64_CTZ)
  2047. {
  2048. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  2049. HANDLE_OP_END();
  2050. }
  2051. HANDLE_OP(WASM_OP_I64_POPCNT)
  2052. {
  2053. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  2054. HANDLE_OP_END();
  2055. }
  2056. HANDLE_OP(WASM_OP_I64_ADD)
  2057. {
  2058. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  2059. HANDLE_OP_END();
  2060. }
  2061. HANDLE_OP(WASM_OP_I64_SUB)
  2062. {
  2063. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  2064. HANDLE_OP_END();
  2065. }
  2066. HANDLE_OP(WASM_OP_I64_MUL)
  2067. {
  2068. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  2069. HANDLE_OP_END();
  2070. }
  2071. HANDLE_OP(WASM_OP_I64_DIV_S)
  2072. {
  2073. int64 a, b;
  2074. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2075. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2076. if (a == (int64)0x8000000000000000LL && b == -1) {
  2077. wasm_set_exception(module, "integer overflow");
  2078. goto got_exception;
  2079. }
  2080. if (b == 0) {
  2081. wasm_set_exception(module, "integer divide by zero");
  2082. goto got_exception;
  2083. }
  2084. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2085. HANDLE_OP_END();
  2086. }
  2087. HANDLE_OP(WASM_OP_I64_DIV_U)
  2088. {
  2089. uint64 a, b;
  2090. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2091. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2092. if (b == 0) {
  2093. wasm_set_exception(module, "integer divide by zero");
  2094. goto got_exception;
  2095. }
  2096. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2097. HANDLE_OP_END();
  2098. }
  2099. HANDLE_OP(WASM_OP_I64_REM_S)
  2100. {
  2101. int64 a, b;
  2102. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2103. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2104. if (a == (int64)0x8000000000000000LL && b == -1) {
  2105. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  2106. HANDLE_OP_END();
  2107. }
  2108. if (b == 0) {
  2109. wasm_set_exception(module, "integer divide by zero");
  2110. goto got_exception;
  2111. }
  2112. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2113. HANDLE_OP_END();
  2114. }
  2115. HANDLE_OP(WASM_OP_I64_REM_U)
  2116. {
  2117. uint64 a, b;
  2118. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2119. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2120. if (b == 0) {
  2121. wasm_set_exception(module, "integer divide by zero");
  2122. goto got_exception;
  2123. }
  2124. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2125. HANDLE_OP_END();
  2126. }
  2127. HANDLE_OP(WASM_OP_I64_AND)
  2128. {
  2129. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  2130. HANDLE_OP_END();
  2131. }
  2132. HANDLE_OP(WASM_OP_I64_OR)
  2133. {
  2134. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  2135. HANDLE_OP_END();
  2136. }
  2137. HANDLE_OP(WASM_OP_I64_XOR)
  2138. {
  2139. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  2140. HANDLE_OP_END();
  2141. }
  2142. HANDLE_OP(WASM_OP_I64_SHL)
  2143. {
  2144. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  2145. HANDLE_OP_END();
  2146. }
  2147. HANDLE_OP(WASM_OP_I64_SHR_S)
  2148. {
  2149. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  2150. HANDLE_OP_END();
  2151. }
  2152. HANDLE_OP(WASM_OP_I64_SHR_U)
  2153. {
  2154. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  2155. HANDLE_OP_END();
  2156. }
  2157. HANDLE_OP(WASM_OP_I64_ROTL)
  2158. {
  2159. uint64 a, b;
  2160. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2161. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2162. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  2163. HANDLE_OP_END();
  2164. }
  2165. HANDLE_OP(WASM_OP_I64_ROTR)
  2166. {
  2167. uint64 a, b;
  2168. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2169. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2170. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  2171. HANDLE_OP_END();
  2172. }
  2173. /* numberic instructions of f32 */
  2174. HANDLE_OP(WASM_OP_F32_ABS)
  2175. {
  2176. DEF_OP_MATH(float32, F32, fabsf);
  2177. HANDLE_OP_END();
  2178. }
  2179. HANDLE_OP(WASM_OP_F32_NEG)
  2180. {
  2181. uint32 u32 = frame_lp[GET_OFFSET()];
  2182. uint32 sign_bit = u32 & ((uint32)1 << 31);
  2183. addr_ret = GET_OFFSET();
  2184. if (sign_bit)
  2185. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  2186. else
  2187. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  2188. HANDLE_OP_END();
  2189. }
  2190. HANDLE_OP(WASM_OP_F32_CEIL)
  2191. {
  2192. DEF_OP_MATH(float32, F32, ceilf);
  2193. HANDLE_OP_END();
  2194. }
  2195. HANDLE_OP(WASM_OP_F32_FLOOR)
  2196. {
  2197. DEF_OP_MATH(float32, F32, floorf);
  2198. HANDLE_OP_END();
  2199. }
  2200. HANDLE_OP(WASM_OP_F32_TRUNC)
  2201. {
  2202. DEF_OP_MATH(float32, F32, truncf);
  2203. HANDLE_OP_END();
  2204. }
  2205. HANDLE_OP(WASM_OP_F32_NEAREST)
  2206. {
  2207. DEF_OP_MATH(float32, F32, rintf);
  2208. HANDLE_OP_END();
  2209. }
  2210. HANDLE_OP(WASM_OP_F32_SQRT)
  2211. {
  2212. DEF_OP_MATH(float32, F32, sqrtf);
  2213. HANDLE_OP_END();
  2214. }
  2215. HANDLE_OP(WASM_OP_F32_ADD)
  2216. {
  2217. DEF_OP_NUMERIC(float32, float32, F32, +);
  2218. HANDLE_OP_END();
  2219. }
  2220. HANDLE_OP(WASM_OP_F32_SUB)
  2221. {
  2222. DEF_OP_NUMERIC(float32, float32, F32, -);
  2223. HANDLE_OP_END();
  2224. }
  2225. HANDLE_OP(WASM_OP_F32_MUL)
  2226. {
  2227. DEF_OP_NUMERIC(float32, float32, F32, *);
  2228. HANDLE_OP_END();
  2229. }
  2230. HANDLE_OP(WASM_OP_F32_DIV)
  2231. {
  2232. DEF_OP_NUMERIC(float32, float32, F32, /);
  2233. HANDLE_OP_END();
  2234. }
  2235. HANDLE_OP(WASM_OP_F32_MIN)
  2236. {
  2237. float32 a, b;
  2238. b = *(float32 *)(frame_lp + GET_OFFSET());
  2239. a = *(float32 *)(frame_lp + GET_OFFSET());
  2240. *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
  2241. HANDLE_OP_END();
  2242. }
  2243. HANDLE_OP(WASM_OP_F32_MAX)
  2244. {
  2245. float32 a, b;
  2246. b = *(float32 *)(frame_lp + GET_OFFSET());
  2247. a = *(float32 *)(frame_lp + GET_OFFSET());
  2248. *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
  2249. HANDLE_OP_END();
  2250. }
  2251. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  2252. {
  2253. float32 a, b;
  2254. b = *(float32 *)(frame_lp + GET_OFFSET());
  2255. a = *(float32 *)(frame_lp + GET_OFFSET());
  2256. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  2257. HANDLE_OP_END();
  2258. }
  2259. /* numberic instructions of f64 */
  2260. HANDLE_OP(WASM_OP_F64_ABS)
  2261. {
  2262. DEF_OP_MATH(float64, F64, fabs);
  2263. HANDLE_OP_END();
  2264. }
  2265. HANDLE_OP(WASM_OP_F64_NEG)
  2266. {
  2267. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2268. uint64 sign_bit = u64 & (((uint64)1) << 63);
  2269. if (sign_bit)
  2270. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2271. (u64 & ~(((uint64)1) << 63)));
  2272. else
  2273. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2274. (u64 | (((uint64)1) << 63)));
  2275. HANDLE_OP_END();
  2276. }
  2277. HANDLE_OP(WASM_OP_F64_CEIL)
  2278. {
  2279. DEF_OP_MATH(float64, F64, ceil);
  2280. HANDLE_OP_END();
  2281. }
  2282. HANDLE_OP(WASM_OP_F64_FLOOR)
  2283. {
  2284. DEF_OP_MATH(float64, F64, floor);
  2285. HANDLE_OP_END();
  2286. }
  2287. HANDLE_OP(WASM_OP_F64_TRUNC)
  2288. {
  2289. DEF_OP_MATH(float64, F64, trunc);
  2290. HANDLE_OP_END();
  2291. }
  2292. HANDLE_OP(WASM_OP_F64_NEAREST)
  2293. {
  2294. DEF_OP_MATH(float64, F64, rint);
  2295. HANDLE_OP_END();
  2296. }
  2297. HANDLE_OP(WASM_OP_F64_SQRT)
  2298. {
  2299. DEF_OP_MATH(float64, F64, sqrt);
  2300. HANDLE_OP_END();
  2301. }
  2302. HANDLE_OP(WASM_OP_F64_ADD)
  2303. {
  2304. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  2305. HANDLE_OP_END();
  2306. }
  2307. HANDLE_OP(WASM_OP_F64_SUB)
  2308. {
  2309. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  2310. HANDLE_OP_END();
  2311. }
  2312. HANDLE_OP(WASM_OP_F64_MUL)
  2313. {
  2314. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  2315. HANDLE_OP_END();
  2316. }
  2317. HANDLE_OP(WASM_OP_F64_DIV)
  2318. {
  2319. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  2320. HANDLE_OP_END();
  2321. }
  2322. HANDLE_OP(WASM_OP_F64_MIN)
  2323. {
  2324. float64 a, b;
  2325. b = POP_F64();
  2326. a = POP_F64();
  2327. PUSH_F64(f64_min(a, b));
  2328. HANDLE_OP_END();
  2329. }
  2330. HANDLE_OP(WASM_OP_F64_MAX)
  2331. {
  2332. float64 a, b;
  2333. b = POP_F64();
  2334. a = POP_F64();
  2335. PUSH_F64(f64_max(a, b));
  2336. HANDLE_OP_END();
  2337. }
  2338. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  2339. {
  2340. float64 a, b;
  2341. b = POP_F64();
  2342. a = POP_F64();
  2343. PUSH_F64(local_copysign(a, b));
  2344. HANDLE_OP_END();
  2345. }
  2346. /* conversions of i32 */
  2347. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  2348. {
  2349. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  2350. PUSH_I32(value);
  2351. HANDLE_OP_END();
  2352. }
  2353. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  2354. {
  2355. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  2356. since float/double values of ieee754 cannot precisely
  2357. represent all int32/uint32/int64/uint64 values, e.g.:
  2358. UINT32_MAX is 4294967295, but (float32)4294967295 is
  2359. 4294967296.0f, but not 4294967295.0f. */
  2360. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  2361. HANDLE_OP_END();
  2362. }
  2363. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  2364. {
  2365. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  2366. HANDLE_OP_END();
  2367. }
  2368. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  2369. {
  2370. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  2371. HANDLE_OP_END();
  2372. }
  2373. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  2374. {
  2375. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  2376. HANDLE_OP_END();
  2377. }
  2378. /* conversions of i64 */
  2379. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  2380. {
  2381. DEF_OP_CONVERT(int64, I64, int32, I32);
  2382. HANDLE_OP_END();
  2383. }
  2384. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  2385. {
  2386. DEF_OP_CONVERT(int64, I64, uint32, I32);
  2387. HANDLE_OP_END();
  2388. }
  2389. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  2390. {
  2391. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  2392. 9223372036854775808.0f, false, true);
  2393. HANDLE_OP_END();
  2394. }
  2395. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  2396. {
  2397. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  2398. HANDLE_OP_END();
  2399. }
  2400. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  2401. {
  2402. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  2403. false, true);
  2404. HANDLE_OP_END();
  2405. }
  2406. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  2407. {
  2408. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  2409. HANDLE_OP_END();
  2410. }
  2411. /* conversions of f32 */
  2412. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  2413. {
  2414. DEF_OP_CONVERT(float32, F32, int32, I32);
  2415. HANDLE_OP_END();
  2416. }
  2417. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  2418. {
  2419. DEF_OP_CONVERT(float32, F32, uint32, I32);
  2420. HANDLE_OP_END();
  2421. }
  2422. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  2423. {
  2424. DEF_OP_CONVERT(float32, F32, int64, I64);
  2425. HANDLE_OP_END();
  2426. }
  2427. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  2428. {
  2429. DEF_OP_CONVERT(float32, F32, uint64, I64);
  2430. HANDLE_OP_END();
  2431. }
  2432. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  2433. {
  2434. DEF_OP_CONVERT(float32, F32, float64, F64);
  2435. HANDLE_OP_END();
  2436. }
  2437. /* conversions of f64 */
  2438. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  2439. {
  2440. DEF_OP_CONVERT(float64, F64, int32, I32);
  2441. HANDLE_OP_END();
  2442. }
  2443. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  2444. {
  2445. DEF_OP_CONVERT(float64, F64, uint32, I32);
  2446. HANDLE_OP_END();
  2447. }
  2448. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  2449. {
  2450. DEF_OP_CONVERT(float64, F64, int64, I64);
  2451. HANDLE_OP_END();
  2452. }
  2453. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  2454. {
  2455. DEF_OP_CONVERT(float64, F64, uint64, I64);
  2456. HANDLE_OP_END();
  2457. }
  2458. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  2459. {
  2460. DEF_OP_CONVERT(float64, F64, float32, F32);
  2461. HANDLE_OP_END();
  2462. }
  2463. /* reinterpretations */
  2464. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  2465. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  2466. {
  2467. DEF_OP_REINTERPRET(uint32, I32);
  2468. HANDLE_OP_END();
  2469. }
  2470. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  2471. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  2472. {
  2473. DEF_OP_REINTERPRET(int64, I64);
  2474. HANDLE_OP_END();
  2475. }
  2476. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  2477. {
  2478. addr1 = GET_OFFSET();
  2479. addr2 = GET_OFFSET();
  2480. frame_lp[addr2] = frame_lp[addr1];
  2481. HANDLE_OP_END();
  2482. }
  2483. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  2484. {
  2485. addr1 = GET_OFFSET();
  2486. addr2 = GET_OFFSET();
  2487. frame_lp[addr2] = frame_lp[addr1];
  2488. frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
  2489. HANDLE_OP_END();
  2490. }
  2491. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  2492. {
  2493. uint32 values_count, total_cell;
  2494. uint8 *cells;
  2495. int16 *src_offsets = NULL;
  2496. uint16 *dst_offsets = NULL;
  2497. /* read values_count */
  2498. values_count = read_uint32(frame_ip);
  2499. /* read total cell num */
  2500. total_cell = read_uint32(frame_ip);
  2501. /* cells */
  2502. cells = (uint8 *)frame_ip;
  2503. frame_ip += values_count * CELL_SIZE;
  2504. /* src offsets */
  2505. src_offsets = (int16 *)frame_ip;
  2506. frame_ip += values_count * sizeof(int16);
  2507. /* dst offsets */
  2508. dst_offsets = (uint16 *)frame_ip;
  2509. frame_ip += values_count * sizeof(uint16);
  2510. if (!copy_stack_values(module, frame_lp, values_count,
  2511. total_cell, cells, src_offsets,
  2512. dst_offsets))
  2513. goto got_exception;
  2514. HANDLE_OP_END();
  2515. }
  2516. HANDLE_OP(WASM_OP_SET_LOCAL)
  2517. HANDLE_OP(WASM_OP_TEE_LOCAL)
  2518. {
  2519. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2520. addr1 = GET_OFFSET();
  2521. if (local_type == VALUE_TYPE_I32
  2522. || local_type == VALUE_TYPE_F32) {
  2523. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  2524. }
  2525. else if (local_type == VALUE_TYPE_I64
  2526. || local_type == VALUE_TYPE_F64) {
  2527. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  2528. GET_I64_FROM_ADDR(frame_lp + addr1));
  2529. }
  2530. else {
  2531. wasm_set_exception(module, "invalid local type");
  2532. goto got_exception;
  2533. }
  2534. HANDLE_OP_END();
  2535. }
  2536. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  2537. {
  2538. DEF_OP_CONVERT(int32, I32, int8, I32);
  2539. HANDLE_OP_END();
  2540. }
  2541. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  2542. {
  2543. DEF_OP_CONVERT(int32, I32, int16, I32);
  2544. HANDLE_OP_END();
  2545. }
  2546. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  2547. {
  2548. DEF_OP_CONVERT(int64, I64, int8, I64);
  2549. HANDLE_OP_END();
  2550. }
  2551. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  2552. {
  2553. DEF_OP_CONVERT(int64, I64, int16, I64);
  2554. HANDLE_OP_END();
  2555. }
  2556. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  2557. {
  2558. DEF_OP_CONVERT(int64, I64, int32, I64);
  2559. HANDLE_OP_END();
  2560. }
  2561. HANDLE_OP(WASM_OP_MISC_PREFIX)
  2562. {
  2563. GET_OPCODE();
  2564. switch (opcode) {
  2565. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2566. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  2567. true, true);
  2568. break;
  2569. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2570. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  2571. break;
  2572. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2573. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  2574. true);
  2575. break;
  2576. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2577. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  2578. break;
  2579. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2580. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  2581. 9223372036854775808.0f, false,
  2582. true);
  2583. break;
  2584. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2585. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  2586. false, false);
  2587. break;
  2588. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2589. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  2590. 9223372036854775808.0, false,
  2591. true);
  2592. break;
  2593. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2594. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  2595. false, false);
  2596. break;
  2597. #if WASM_ENABLE_BULK_MEMORY != 0
  2598. case WASM_OP_MEMORY_INIT:
  2599. {
  2600. uint32 addr, segment;
  2601. uint64 bytes, offset, seg_len;
  2602. uint8 *data;
  2603. segment = read_uint32(frame_ip);
  2604. bytes = (uint64)POP_I32();
  2605. offset = (uint64)POP_I32();
  2606. addr = POP_I32();
  2607. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2608. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  2609. #else
  2610. if ((uint64)(uint32)addr + bytes
  2611. > (uint64)linear_mem_size)
  2612. goto out_of_bounds;
  2613. maddr = memory->memory_data + (uint32)addr;
  2614. #endif
  2615. seg_len = (uint64)module->module->data_segments[segment]
  2616. ->data_length;
  2617. data = module->module->data_segments[segment]->data;
  2618. if (offset + bytes > seg_len)
  2619. goto out_of_bounds;
  2620. bh_memcpy_s(maddr, linear_mem_size - addr,
  2621. data + offset, (uint32)bytes);
  2622. break;
  2623. }
  2624. case WASM_OP_DATA_DROP:
  2625. {
  2626. uint32 segment;
  2627. segment = read_uint32(frame_ip);
  2628. module->module->data_segments[segment]->data_length = 0;
  2629. break;
  2630. }
  2631. case WASM_OP_MEMORY_COPY:
  2632. {
  2633. uint32 dst, src, len;
  2634. uint8 *mdst, *msrc;
  2635. len = POP_I32();
  2636. src = POP_I32();
  2637. dst = POP_I32();
  2638. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2639. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  2640. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2641. #else
  2642. if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
  2643. goto out_of_bounds;
  2644. msrc = memory->memory_data + (uint32)src;
  2645. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2646. goto out_of_bounds;
  2647. mdst = memory->memory_data + (uint32)dst;
  2648. #endif
  2649. /* allowing the destination and source to overlap */
  2650. bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
  2651. break;
  2652. }
  2653. case WASM_OP_MEMORY_FILL:
  2654. {
  2655. uint32 dst, len;
  2656. uint8 fill_val, *mdst;
  2657. len = POP_I32();
  2658. fill_val = POP_I32();
  2659. dst = POP_I32();
  2660. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2661. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2662. #else
  2663. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2664. goto out_of_bounds;
  2665. mdst = memory->memory_data + (uint32)dst;
  2666. #endif
  2667. memset(mdst, fill_val, len);
  2668. break;
  2669. }
  2670. #endif /* WASM_ENABLE_BULK_MEMORY */
  2671. #if WASM_ENABLE_REF_TYPES != 0
  2672. case WASM_OP_TABLE_INIT:
  2673. {
  2674. uint32 tbl_idx, elem_idx;
  2675. uint64 n, s, d;
  2676. WASMTableInstance *tbl_inst;
  2677. elem_idx = read_uint32(frame_ip);
  2678. bh_assert(elem_idx < module->module->table_seg_count);
  2679. tbl_idx = read_uint32(frame_ip);
  2680. bh_assert(tbl_idx < module->module->table_count);
  2681. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2682. n = (uint32)POP_I32();
  2683. s = (uint32)POP_I32();
  2684. d = (uint32)POP_I32();
  2685. if (!n) {
  2686. break;
  2687. }
  2688. if (n + s > module->module->table_segments[elem_idx]
  2689. .function_count
  2690. || d + n > tbl_inst->cur_size) {
  2691. wasm_set_exception(module,
  2692. "out of bounds table access");
  2693. goto got_exception;
  2694. }
  2695. if (module->module->table_segments[elem_idx]
  2696. .is_dropped) {
  2697. wasm_set_exception(module,
  2698. "out of bounds table access");
  2699. goto got_exception;
  2700. }
  2701. if (!wasm_elem_is_passive(
  2702. module->module->table_segments[elem_idx]
  2703. .mode)) {
  2704. wasm_set_exception(module,
  2705. "out of bounds table access");
  2706. goto got_exception;
  2707. }
  2708. bh_memcpy_s(
  2709. (uint8 *)tbl_inst
  2710. + offsetof(WASMTableInstance, elems)
  2711. + d * sizeof(uint32),
  2712. (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
  2713. module->module->table_segments[elem_idx]
  2714. .func_indexes
  2715. + s,
  2716. (uint32)(n * sizeof(uint32)));
  2717. break;
  2718. }
  2719. case WASM_OP_ELEM_DROP:
  2720. {
  2721. uint32 elem_idx = read_uint32(frame_ip);
  2722. bh_assert(elem_idx < module->module->table_seg_count);
  2723. module->module->table_segments[elem_idx].is_dropped =
  2724. true;
  2725. break;
  2726. }
  2727. case WASM_OP_TABLE_COPY:
  2728. {
  2729. uint32 src_tbl_idx, dst_tbl_idx;
  2730. uint64 n, s, d;
  2731. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  2732. dst_tbl_idx = read_uint32(frame_ip);
  2733. bh_assert(dst_tbl_idx < module->table_count);
  2734. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  2735. src_tbl_idx = read_uint32(frame_ip);
  2736. bh_assert(src_tbl_idx < module->table_count);
  2737. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  2738. n = (uint32)POP_I32();
  2739. s = (uint32)POP_I32();
  2740. d = (uint32)POP_I32();
  2741. if (d + n > dst_tbl_inst->cur_size
  2742. || s + n > src_tbl_inst->cur_size) {
  2743. wasm_set_exception(module,
  2744. "out of bounds table access");
  2745. goto got_exception;
  2746. }
  2747. /* if s >= d, copy from front to back */
  2748. /* if s < d, copy from back to front */
  2749. /* merge all together */
  2750. bh_memmove_s((uint8 *)dst_tbl_inst
  2751. + offsetof(WASMTableInstance, elems)
  2752. + d * sizeof(uint32),
  2753. (uint32)((dst_tbl_inst->cur_size - d)
  2754. * sizeof(uint32)),
  2755. (uint8 *)src_tbl_inst
  2756. + offsetof(WASMTableInstance, elems)
  2757. + s * sizeof(uint32),
  2758. (uint32)(n * sizeof(uint32)));
  2759. break;
  2760. }
  2761. case WASM_OP_TABLE_GROW:
  2762. {
  2763. uint32 tbl_idx, n, init_val, orig_tbl_sz;
  2764. WASMTableInstance *tbl_inst;
  2765. tbl_idx = read_uint32(frame_ip);
  2766. bh_assert(tbl_idx < module->table_count);
  2767. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2768. orig_tbl_sz = tbl_inst->cur_size;
  2769. n = POP_I32();
  2770. init_val = POP_I32();
  2771. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  2772. PUSH_I32(-1);
  2773. }
  2774. else {
  2775. PUSH_I32(orig_tbl_sz);
  2776. }
  2777. break;
  2778. }
  2779. case WASM_OP_TABLE_SIZE:
  2780. {
  2781. uint32 tbl_idx;
  2782. WASMTableInstance *tbl_inst;
  2783. tbl_idx = read_uint32(frame_ip);
  2784. bh_assert(tbl_idx < module->table_count);
  2785. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2786. PUSH_I32(tbl_inst->cur_size);
  2787. break;
  2788. }
  2789. case WASM_OP_TABLE_FILL:
  2790. {
  2791. uint32 tbl_idx, n, fill_val, i;
  2792. WASMTableInstance *tbl_inst;
  2793. tbl_idx = read_uint32(frame_ip);
  2794. bh_assert(tbl_idx < module->table_count);
  2795. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2796. n = POP_I32();
  2797. fill_val = POP_I32();
  2798. i = POP_I32();
  2799. if (i + n > tbl_inst->cur_size) {
  2800. wasm_set_exception(module,
  2801. "out of bounds table access");
  2802. goto got_exception;
  2803. }
  2804. for (; n != 0; i++, n--) {
  2805. tbl_inst->elems[i] = fill_val;
  2806. }
  2807. break;
  2808. }
  2809. #endif /* WASM_ENABLE_REF_TYPES */
  2810. default:
  2811. wasm_set_exception(module, "unsupported opcode");
  2812. goto got_exception;
  2813. }
  2814. HANDLE_OP_END();
  2815. }
  2816. #if WASM_ENABLE_SHARED_MEMORY != 0
  2817. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  2818. {
  2819. uint32 offset = 0, addr;
  2820. GET_OPCODE();
  2821. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2822. offset = read_uint32(frame_ip);
  2823. }
  2824. switch (opcode) {
  2825. case WASM_OP_ATOMIC_NOTIFY:
  2826. {
  2827. uint32 notify_count, ret;
  2828. notify_count = POP_I32();
  2829. addr = POP_I32();
  2830. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2831. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2832. ret = wasm_runtime_atomic_notify(
  2833. (WASMModuleInstanceCommon *)module, maddr,
  2834. notify_count);
  2835. if (ret == (uint32)-1)
  2836. goto got_exception;
  2837. PUSH_I32(ret);
  2838. break;
  2839. }
  2840. case WASM_OP_ATOMIC_WAIT32:
  2841. {
  2842. uint64 timeout;
  2843. uint32 expect, ret;
  2844. timeout = POP_I64();
  2845. expect = POP_I32();
  2846. addr = POP_I32();
  2847. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2848. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2849. ret = wasm_runtime_atomic_wait(
  2850. (WASMModuleInstanceCommon *)module, maddr,
  2851. (uint64)expect, timeout, false);
  2852. if (ret == (uint32)-1)
  2853. goto got_exception;
  2854. #if WASM_ENABLE_THREAD_MGR != 0
  2855. CHECK_SUSPEND_FLAGS();
  2856. #endif
  2857. PUSH_I32(ret);
  2858. break;
  2859. }
  2860. case WASM_OP_ATOMIC_WAIT64:
  2861. {
  2862. uint64 timeout, expect;
  2863. uint32 ret;
  2864. timeout = POP_I64();
  2865. expect = POP_I64();
  2866. addr = POP_I32();
  2867. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2868. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2869. ret = wasm_runtime_atomic_wait(
  2870. (WASMModuleInstanceCommon *)module, maddr, expect,
  2871. timeout, true);
  2872. if (ret == (uint32)-1)
  2873. goto got_exception;
  2874. #if WASM_ENABLE_THREAD_MGR != 0
  2875. CHECK_SUSPEND_FLAGS();
  2876. #endif
  2877. PUSH_I32(ret);
  2878. break;
  2879. }
  2880. case WASM_OP_ATOMIC_FENCE:
  2881. {
  2882. os_atomic_thread_fence(os_memory_order_seq_cst);
  2883. break;
  2884. }
  2885. case WASM_OP_ATOMIC_I32_LOAD:
  2886. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2887. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2888. {
  2889. uint32 readv;
  2890. addr = POP_I32();
  2891. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  2892. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2893. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2894. os_mutex_lock(&node->shared_mem_lock);
  2895. readv = (uint32)(*(uint8 *)maddr);
  2896. os_mutex_unlock(&node->shared_mem_lock);
  2897. }
  2898. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  2899. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2900. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2901. os_mutex_lock(&node->shared_mem_lock);
  2902. readv = (uint32)LOAD_U16(maddr);
  2903. os_mutex_unlock(&node->shared_mem_lock);
  2904. }
  2905. else {
  2906. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2907. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2908. os_mutex_lock(&node->shared_mem_lock);
  2909. readv = LOAD_I32(maddr);
  2910. os_mutex_unlock(&node->shared_mem_lock);
  2911. }
  2912. PUSH_I32(readv);
  2913. break;
  2914. }
  2915. case WASM_OP_ATOMIC_I64_LOAD:
  2916. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2917. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2918. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2919. {
  2920. uint64 readv;
  2921. addr = POP_I32();
  2922. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  2923. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2924. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2925. os_mutex_lock(&node->shared_mem_lock);
  2926. readv = (uint64)(*(uint8 *)maddr);
  2927. os_mutex_unlock(&node->shared_mem_lock);
  2928. }
  2929. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  2930. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2931. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2932. os_mutex_lock(&node->shared_mem_lock);
  2933. readv = (uint64)LOAD_U16(maddr);
  2934. os_mutex_unlock(&node->shared_mem_lock);
  2935. }
  2936. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  2937. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2938. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2939. os_mutex_lock(&node->shared_mem_lock);
  2940. readv = (uint64)LOAD_U32(maddr);
  2941. os_mutex_unlock(&node->shared_mem_lock);
  2942. }
  2943. else {
  2944. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2945. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2946. os_mutex_lock(&node->shared_mem_lock);
  2947. readv = LOAD_I64(maddr);
  2948. os_mutex_unlock(&node->shared_mem_lock);
  2949. }
  2950. PUSH_I64(readv);
  2951. break;
  2952. }
  2953. case WASM_OP_ATOMIC_I32_STORE:
  2954. case WASM_OP_ATOMIC_I32_STORE8:
  2955. case WASM_OP_ATOMIC_I32_STORE16:
  2956. {
  2957. uint32 sval;
  2958. sval = (uint32)POP_I32();
  2959. addr = POP_I32();
  2960. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  2961. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2962. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2963. os_mutex_lock(&node->shared_mem_lock);
  2964. *(uint8 *)maddr = (uint8)sval;
  2965. os_mutex_unlock(&node->shared_mem_lock);
  2966. }
  2967. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  2968. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2969. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2970. os_mutex_lock(&node->shared_mem_lock);
  2971. STORE_U16(maddr, (uint16)sval);
  2972. os_mutex_unlock(&node->shared_mem_lock);
  2973. }
  2974. else {
  2975. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2976. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2977. os_mutex_lock(&node->shared_mem_lock);
  2978. STORE_U32(maddr, sval);
  2979. os_mutex_unlock(&node->shared_mem_lock);
  2980. }
  2981. break;
  2982. }
  2983. case WASM_OP_ATOMIC_I64_STORE:
  2984. case WASM_OP_ATOMIC_I64_STORE8:
  2985. case WASM_OP_ATOMIC_I64_STORE16:
  2986. case WASM_OP_ATOMIC_I64_STORE32:
  2987. {
  2988. uint64 sval;
  2989. sval = (uint64)POP_I64();
  2990. addr = POP_I32();
  2991. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  2992. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2993. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2994. os_mutex_lock(&node->shared_mem_lock);
  2995. *(uint8 *)maddr = (uint8)sval;
  2996. os_mutex_unlock(&node->shared_mem_lock);
  2997. }
  2998. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  2999. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3000. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3001. os_mutex_lock(&node->shared_mem_lock);
  3002. STORE_U16(maddr, (uint16)sval);
  3003. os_mutex_unlock(&node->shared_mem_lock);
  3004. }
  3005. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  3006. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3007. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3008. os_mutex_lock(&node->shared_mem_lock);
  3009. STORE_U32(maddr, (uint32)sval);
  3010. os_mutex_unlock(&node->shared_mem_lock);
  3011. }
  3012. else {
  3013. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3014. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3015. os_mutex_lock(&node->shared_mem_lock);
  3016. STORE_I64(maddr, sval);
  3017. os_mutex_unlock(&node->shared_mem_lock);
  3018. }
  3019. break;
  3020. }
  3021. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  3022. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  3023. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  3024. {
  3025. uint32 readv, sval, expect;
  3026. sval = POP_I32();
  3027. expect = POP_I32();
  3028. addr = POP_I32();
  3029. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  3030. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3031. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3032. expect = (uint8)expect;
  3033. os_mutex_lock(&node->shared_mem_lock);
  3034. readv = (uint32)(*(uint8 *)maddr);
  3035. if (readv == expect)
  3036. *(uint8 *)maddr = (uint8)(sval);
  3037. os_mutex_unlock(&node->shared_mem_lock);
  3038. }
  3039. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  3040. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3041. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3042. expect = (uint16)expect;
  3043. os_mutex_lock(&node->shared_mem_lock);
  3044. readv = (uint32)LOAD_U16(maddr);
  3045. if (readv == expect)
  3046. STORE_U16(maddr, (uint16)(sval));
  3047. os_mutex_unlock(&node->shared_mem_lock);
  3048. }
  3049. else {
  3050. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3051. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3052. os_mutex_lock(&node->shared_mem_lock);
  3053. readv = LOAD_I32(maddr);
  3054. if (readv == expect)
  3055. STORE_U32(maddr, sval);
  3056. os_mutex_unlock(&node->shared_mem_lock);
  3057. }
  3058. PUSH_I32(readv);
  3059. break;
  3060. }
  3061. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  3062. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  3063. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  3064. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  3065. {
  3066. uint64 readv, sval, expect;
  3067. sval = (uint64)POP_I64();
  3068. expect = (uint64)POP_I64();
  3069. addr = POP_I32();
  3070. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  3071. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3072. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3073. expect = (uint8)expect;
  3074. os_mutex_lock(&node->shared_mem_lock);
  3075. readv = (uint64)(*(uint8 *)maddr);
  3076. if (readv == expect)
  3077. *(uint8 *)maddr = (uint8)(sval);
  3078. os_mutex_unlock(&node->shared_mem_lock);
  3079. }
  3080. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  3081. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3082. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3083. expect = (uint16)expect;
  3084. os_mutex_lock(&node->shared_mem_lock);
  3085. readv = (uint64)LOAD_U16(maddr);
  3086. if (readv == expect)
  3087. STORE_U16(maddr, (uint16)(sval));
  3088. os_mutex_unlock(&node->shared_mem_lock);
  3089. }
  3090. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  3091. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3092. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3093. expect = (uint32)expect;
  3094. os_mutex_lock(&node->shared_mem_lock);
  3095. readv = (uint64)LOAD_U32(maddr);
  3096. if (readv == expect)
  3097. STORE_U32(maddr, (uint32)(sval));
  3098. os_mutex_unlock(&node->shared_mem_lock);
  3099. }
  3100. else {
  3101. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3102. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3103. os_mutex_lock(&node->shared_mem_lock);
  3104. readv = (uint64)LOAD_I64(maddr);
  3105. if (readv == expect)
  3106. STORE_I64(maddr, sval);
  3107. os_mutex_unlock(&node->shared_mem_lock);
  3108. }
  3109. PUSH_I64(readv);
  3110. break;
  3111. }
  3112. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  3113. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  3114. DEF_ATOMIC_RMW_OPCODE(AND, &);
  3115. DEF_ATOMIC_RMW_OPCODE(OR, |);
  3116. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  3117. /* xchg, ignore the read value, and store the given
  3118. value: readv * 0 + sval */
  3119. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  3120. }
  3121. HANDLE_OP_END();
  3122. }
  3123. #endif
  3124. HANDLE_OP(WASM_OP_IMPDEP)
  3125. {
  3126. frame = prev_frame;
  3127. frame_ip = frame->ip;
  3128. goto call_func_from_entry;
  3129. }
  3130. HANDLE_OP(WASM_OP_CALL)
  3131. {
  3132. #if WASM_ENABLE_THREAD_MGR != 0
  3133. CHECK_SUSPEND_FLAGS();
  3134. #endif
  3135. fidx = read_uint32(frame_ip);
  3136. #if WASM_ENABLE_MULTI_MODULE != 0
  3137. if (fidx >= module->e->function_count) {
  3138. wasm_set_exception(module, "unknown function");
  3139. goto got_exception;
  3140. }
  3141. #endif
  3142. cur_func = module->e->functions + fidx;
  3143. goto call_func_from_interp;
  3144. }
  3145. #if WASM_ENABLE_TAIL_CALL != 0
  3146. HANDLE_OP(WASM_OP_RETURN_CALL)
  3147. {
  3148. #if WASM_ENABLE_THREAD_MGR != 0
  3149. CHECK_SUSPEND_FLAGS();
  3150. #endif
  3151. fidx = read_uint32(frame_ip);
  3152. #if WASM_ENABLE_MULTI_MODULE != 0
  3153. if (fidx >= module->e->function_count) {
  3154. wasm_set_exception(module, "unknown function");
  3155. goto got_exception;
  3156. }
  3157. #endif
  3158. cur_func = module->e->functions + fidx;
  3159. goto call_func_from_return_call;
  3160. }
  3161. #endif /* WASM_ENABLE_TAIL_CALL */
  3162. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3163. default:
  3164. wasm_set_exception(module, "unsupported opcode");
  3165. goto got_exception;
  3166. }
  3167. #endif
  3168. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3169. HANDLE_OP(WASM_OP_UNUSED_0x06)
  3170. HANDLE_OP(WASM_OP_UNUSED_0x07)
  3171. HANDLE_OP(WASM_OP_UNUSED_0x08)
  3172. HANDLE_OP(WASM_OP_UNUSED_0x09)
  3173. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  3174. #if WASM_ENABLE_TAIL_CALL == 0
  3175. HANDLE_OP(WASM_OP_RETURN_CALL)
  3176. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  3177. #endif
  3178. #if WASM_ENABLE_SHARED_MEMORY == 0
  3179. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  3180. #endif
  3181. #if WASM_ENABLE_REF_TYPES == 0
  3182. HANDLE_OP(WASM_OP_TABLE_GET)
  3183. HANDLE_OP(WASM_OP_TABLE_SET)
  3184. HANDLE_OP(WASM_OP_REF_NULL)
  3185. HANDLE_OP(WASM_OP_REF_IS_NULL)
  3186. HANDLE_OP(WASM_OP_REF_FUNC)
  3187. #endif
  3188. /* SELECT_T is converted to SELECT or SELECT_64 */
  3189. HANDLE_OP(WASM_OP_SELECT_T)
  3190. HANDLE_OP(WASM_OP_UNUSED_0x14)
  3191. HANDLE_OP(WASM_OP_UNUSED_0x15)
  3192. HANDLE_OP(WASM_OP_UNUSED_0x16)
  3193. HANDLE_OP(WASM_OP_UNUSED_0x17)
  3194. HANDLE_OP(WASM_OP_UNUSED_0x18)
  3195. HANDLE_OP(WASM_OP_UNUSED_0x19)
  3196. HANDLE_OP(WASM_OP_UNUSED_0x27)
  3197. /* optimized op code */
  3198. HANDLE_OP(WASM_OP_F32_STORE)
  3199. HANDLE_OP(WASM_OP_F64_STORE)
  3200. HANDLE_OP(WASM_OP_F32_LOAD)
  3201. HANDLE_OP(WASM_OP_F64_LOAD)
  3202. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3203. HANDLE_OP(WASM_OP_GET_LOCAL)
  3204. HANDLE_OP(WASM_OP_DROP)
  3205. HANDLE_OP(WASM_OP_DROP_64)
  3206. HANDLE_OP(WASM_OP_BLOCK)
  3207. HANDLE_OP(WASM_OP_LOOP)
  3208. HANDLE_OP(WASM_OP_END)
  3209. HANDLE_OP(WASM_OP_NOP)
  3210. HANDLE_OP(EXT_OP_BLOCK)
  3211. HANDLE_OP(EXT_OP_LOOP)
  3212. HANDLE_OP(EXT_OP_IF)
  3213. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  3214. {
  3215. wasm_set_exception(module, "unsupported opcode");
  3216. goto got_exception;
  3217. }
  3218. #endif
  3219. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3220. continue;
  3221. #else
  3222. FETCH_OPCODE_AND_DISPATCH();
  3223. #endif
  3224. #if WASM_ENABLE_TAIL_CALL != 0
  3225. call_func_from_return_call:
  3226. {
  3227. uint32 *lp_base;
  3228. uint32 *lp;
  3229. int i;
  3230. if (!(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  3231. * sizeof(uint32)))) {
  3232. wasm_set_exception(module, "allocate memory failed");
  3233. goto got_exception;
  3234. }
  3235. for (i = 0; i < cur_func->param_count; i++) {
  3236. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3237. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3238. PUT_I64_TO_ADDR(
  3239. lp, GET_OPERAND(uint64, I64,
  3240. 2 * (cur_func->param_count - i - 1)));
  3241. lp += 2;
  3242. }
  3243. else {
  3244. *lp = GET_OPERAND(uint32, I32,
  3245. (2 * (cur_func->param_count - i - 1)));
  3246. lp++;
  3247. }
  3248. }
  3249. frame->lp = frame->operand + cur_func->const_cell_num;
  3250. if (lp - lp_base > 0) {
  3251. word_copy(frame->lp, lp_base, lp - lp_base);
  3252. }
  3253. wasm_runtime_free(lp_base);
  3254. FREE_FRAME(exec_env, frame);
  3255. frame_ip += cur_func->param_count * sizeof(int16);
  3256. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3257. goto call_func_from_entry;
  3258. }
  3259. #endif /* WASM_ENABLE_TAIL_CALL */
  3260. call_func_from_interp:
  3261. {
  3262. /* Only do the copy when it's called from interpreter. */
  3263. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3264. int i;
  3265. #if WASM_ENABLE_MULTI_MODULE != 0
  3266. if (cur_func->is_import_func) {
  3267. outs_area->lp = outs_area->operand
  3268. + (cur_func->import_func_inst
  3269. ? cur_func->import_func_inst->const_cell_num
  3270. : 0);
  3271. }
  3272. else
  3273. #endif
  3274. {
  3275. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  3276. }
  3277. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  3278. > exec_env->wasm_stack.s.top_boundary) {
  3279. wasm_set_exception(module, "wasm operand stack overflow");
  3280. goto got_exception;
  3281. }
  3282. for (i = 0; i < cur_func->param_count; i++) {
  3283. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3284. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3285. PUT_I64_TO_ADDR(
  3286. outs_area->lp,
  3287. GET_OPERAND(uint64, I64,
  3288. 2 * (cur_func->param_count - i - 1)));
  3289. outs_area->lp += 2;
  3290. }
  3291. else {
  3292. *outs_area->lp = GET_OPERAND(
  3293. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  3294. outs_area->lp++;
  3295. }
  3296. }
  3297. frame_ip += cur_func->param_count * sizeof(int16);
  3298. if (cur_func->ret_cell_num != 0) {
  3299. /* Get the first return value's offset. Since loader emit
  3300. * all return values' offset so we must skip remain return
  3301. * values' offsets.
  3302. */
  3303. WASMType *func_type;
  3304. if (cur_func->is_import_func)
  3305. func_type = cur_func->u.func_import->func_type;
  3306. else
  3307. func_type = cur_func->u.func->func_type;
  3308. frame->ret_offset = GET_OFFSET();
  3309. frame_ip += 2 * (func_type->result_count - 1);
  3310. }
  3311. SYNC_ALL_TO_FRAME();
  3312. prev_frame = frame;
  3313. }
  3314. call_func_from_entry:
  3315. {
  3316. if (cur_func->is_import_func) {
  3317. #if WASM_ENABLE_MULTI_MODULE != 0
  3318. if (cur_func->import_func_inst) {
  3319. wasm_interp_call_func_import(module, exec_env, cur_func,
  3320. prev_frame);
  3321. }
  3322. else
  3323. #endif
  3324. {
  3325. wasm_interp_call_func_native(module, exec_env, cur_func,
  3326. prev_frame);
  3327. }
  3328. prev_frame = frame->prev_frame;
  3329. cur_func = frame->function;
  3330. UPDATE_ALL_FROM_FRAME();
  3331. /* update memory size, no need to update memory ptr as
  3332. it isn't changed in wasm_enlarge_memory */
  3333. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3334. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3335. || WASM_ENABLE_BULK_MEMORY != 0
  3336. if (memory)
  3337. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  3338. #endif
  3339. if (wasm_copy_exception(module, NULL))
  3340. goto got_exception;
  3341. }
  3342. else {
  3343. WASMFunction *cur_wasm_func = cur_func->u.func;
  3344. all_cell_num = cur_func->param_cell_num + cur_func->local_cell_num
  3345. + cur_func->const_cell_num
  3346. + cur_wasm_func->max_stack_cell_num;
  3347. /* param_cell_num, local_cell_num, const_cell_num and
  3348. max_stack_cell_num are all no larger than UINT16_MAX (checked
  3349. in loader), all_cell_num must be smaller than 1MB */
  3350. bh_assert(all_cell_num < 1 * BH_MB);
  3351. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3352. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  3353. frame = prev_frame;
  3354. goto got_exception;
  3355. }
  3356. /* Initialize the interpreter context. */
  3357. frame->function = cur_func;
  3358. frame_ip = wasm_get_func_code(cur_func);
  3359. frame_ip_end = wasm_get_func_code_end(cur_func);
  3360. frame_lp = frame->lp =
  3361. frame->operand + cur_wasm_func->const_cell_num;
  3362. /* Initialize the consts */
  3363. if (cur_wasm_func->const_cell_num > 0) {
  3364. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  3365. cur_wasm_func->const_cell_num);
  3366. }
  3367. /* Initialize the local variables */
  3368. memset(frame_lp + cur_func->param_cell_num, 0,
  3369. (uint32)(cur_func->local_cell_num * 4));
  3370. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  3371. }
  3372. #if WASM_ENABLE_THREAD_MGR != 0
  3373. CHECK_SUSPEND_FLAGS();
  3374. #endif
  3375. HANDLE_OP_END();
  3376. }
  3377. return_func:
  3378. {
  3379. FREE_FRAME(exec_env, frame);
  3380. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3381. if (!prev_frame->ip)
  3382. /* Called from native. */
  3383. return;
  3384. RECOVER_CONTEXT(prev_frame);
  3385. HANDLE_OP_END();
  3386. }
  3387. (void)frame_ip_end;
  3388. #if WASM_ENABLE_SHARED_MEMORY != 0
  3389. unaligned_atomic:
  3390. wasm_set_exception(module, "unaligned atomic");
  3391. goto got_exception;
  3392. #endif
  3393. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3394. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3395. || WASM_ENABLE_BULK_MEMORY != 0
  3396. out_of_bounds:
  3397. wasm_set_exception(module, "out of bounds memory access");
  3398. #endif
  3399. got_exception:
  3400. SYNC_ALL_TO_FRAME();
  3401. return;
  3402. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3403. }
  3404. #else
  3405. FETCH_OPCODE_AND_DISPATCH();
  3406. #endif
  3407. }
  3408. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3409. void **
  3410. wasm_interp_get_handle_table()
  3411. {
  3412. WASMModuleInstance module;
  3413. memset(&module, 0, sizeof(WASMModuleInstance));
  3414. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  3415. return global_handle_table;
  3416. }
  3417. #endif
  3418. void
  3419. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  3420. WASMFunctionInstance *function, uint32 argc,
  3421. uint32 argv[])
  3422. {
  3423. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3424. WASMInterpFrame *frame, *outs_area;
  3425. /* Allocate sufficient cells for all kinds of return values. */
  3426. unsigned all_cell_num =
  3427. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  3428. i;
  3429. /* This frame won't be used by JITed code, so only allocate interp
  3430. frame here. */
  3431. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3432. char exception[EXCEPTION_BUF_LEN];
  3433. if (argc < function->param_cell_num) {
  3434. char buf[128];
  3435. snprintf(buf, sizeof(buf),
  3436. "invalid argument count %" PRIu32
  3437. ", must be no smaller than %" PRIu32,
  3438. argc, (uint32)function->param_cell_num);
  3439. wasm_set_exception(module_inst, buf);
  3440. return;
  3441. }
  3442. argc = function->param_cell_num;
  3443. RECORD_STACK_USAGE(exec_env, (uint8 *)&prev_frame);
  3444. #if !(defined(OS_ENABLE_HW_BOUND_CHECK) \
  3445. && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0)
  3446. if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
  3447. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3448. "native stack overflow");
  3449. return;
  3450. }
  3451. #endif
  3452. if (!(frame =
  3453. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  3454. return;
  3455. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3456. frame->function = NULL;
  3457. frame->ip = NULL;
  3458. /* There is no local variable. */
  3459. frame->lp = frame->operand + 0;
  3460. frame->ret_offset = 0;
  3461. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  3462. > exec_env->wasm_stack.s.top_boundary) {
  3463. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3464. "wasm operand stack overflow");
  3465. return;
  3466. }
  3467. if (argc > 0)
  3468. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  3469. wasm_exec_env_set_cur_frame(exec_env, frame);
  3470. if (function->is_import_func) {
  3471. #if WASM_ENABLE_MULTI_MODULE != 0
  3472. if (function->import_module_inst) {
  3473. LOG_DEBUG("it is a function of a sub module");
  3474. wasm_interp_call_func_import(module_inst, exec_env, function,
  3475. frame);
  3476. }
  3477. else
  3478. #endif
  3479. {
  3480. LOG_DEBUG("it is an native function");
  3481. wasm_interp_call_func_native(module_inst, exec_env, function,
  3482. frame);
  3483. }
  3484. }
  3485. else {
  3486. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  3487. }
  3488. /* Output the return value to the caller */
  3489. if (!wasm_copy_exception(module_inst, NULL)) {
  3490. for (i = 0; i < function->ret_cell_num; i++)
  3491. argv[i] = *(frame->lp + i);
  3492. }
  3493. else {
  3494. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3495. if (wasm_interp_create_call_stack(exec_env)) {
  3496. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  3497. }
  3498. #endif
  3499. wasm_copy_exception(module_inst, exception);
  3500. LOG_DEBUG("meet an exception %s", exception);
  3501. }
  3502. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3503. FREE_FRAME(exec_env, frame);
  3504. #if WASM_ENABLE_OPCODE_COUNTER != 0
  3505. wasm_interp_dump_op_count();
  3506. #endif
  3507. }