1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
//! This mutator applies a random peephole transformation to the input Wasm module.
//!
//! It builds a minimal DFG (Data Flow Graph) from a random operator selected
//! from a random function inside the input Wasm. If this DFG is consistent and
//! has no side-effects, and egraph is constructed with
//! several hand-made rewriting rules. Random rewriting rules are selected and
//! the DFG is replaced by a new one. The final step assembles all together with the
//! new DFG, constructing a new equivalent Wasm binary.
//!
//!
//! To contribute with this specific mutator you can augment the defined
//! [rules][rules]. Those rewriting rules should be designed to
//! preserve the semantic of the original DFG or, in other case, should follow the filter
//! of the top config `preserve_semantics`.
//!
//! # Example
//!
//! ```ignore
//! rules.extend(rewrite!("strength-reduction"; "(i32.shl ?x i32.const.1)" <=> "(i32.mul ?x i32.const.2)"));
//! ```
//!
pub mod dfg;
pub mod eggsy;
pub mod rules;
use self::{
dfg::DFGBuilder,
eggsy::{
analysis::PeepholeMutationAnalysis,
encoder::{Encoder, expr2wasm::ResourceRequest},
expr_enumerator::lazy_expand_aux,
lang::*,
},
};
use super::{Mutator, OperatorAndByteOffset};
use crate::{
Error, ErrorKind, ModuleInfo, Result, WasmMutate,
module::{PrimitiveTypeInfo, map_type},
};
use egg::{Rewrite, Runner};
use rand::RngExt;
use wasm_encoder::reencode::{Reencode, RoundtripReencoder};
use wasm_encoder::{CodeSection, ConstExpr, Function, GlobalSection, Module, ValType};
use wasmparser::{CodeSectionReader, FunctionBody, GlobalSectionReader, LocalsReader};
/// This mutator applies a random peephole transformation to the input Wasm module
#[derive(Clone)]
pub struct PeepholeMutator {
max_tree_depth: u32,
rules: Option<Vec<Rewrite<Lang, PeepholeMutationAnalysis>>>,
}
type EG = egg::EGraph<Lang, PeepholeMutationAnalysis>;
impl PeepholeMutator {
/// Initializes a new PeepholeMutator with fuel
pub const fn new(max_depth: u32) -> Self {
PeepholeMutator {
max_tree_depth: max_depth,
rules: None,
}
}
#[cfg(test)]
pub fn new_with_rules(
max_tree_depth: u32,
rules: Vec<Rewrite<Lang, PeepholeMutationAnalysis>>,
) -> Self {
PeepholeMutator {
max_tree_depth,
rules: Some(rules),
}
}
// Collect and unfold params and locals, [x, ty, y, ty2] -> [ty....ty, ty2...ty2]
fn get_func_locals(
&self,
info: &ModuleInfo,
funcidx: u32,
localsreader: &mut LocalsReader,
) -> Result<Vec<PrimitiveTypeInfo>> {
let ftype = info.get_functype_idx(funcidx);
match ftype {
crate::module::TypeInfo::Func(tpe) => {
let mut all_locals = Vec::new();
for primitive in &tpe.params {
all_locals.push(*primitive)
}
for _ in 0..localsreader.get_count() {
let (count, ty) = localsreader.read()?;
let tymapped = PrimitiveTypeInfo::try_from(ty)?;
for _ in 0..count {
all_locals.push(tymapped);
}
}
Ok(all_locals)
}
}
}
fn random_mutate<'a>(
&self,
config: &'a mut WasmMutate,
rules: &[Rewrite<Lang, PeepholeMutationAnalysis>],
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> {
let code_section = config.info().code.unwrap();
let reader = config.info().get_binary_reader(code_section);
let sectionreader = CodeSectionReader::new(reader)?;
let function_count = sectionreader.count();
let mut function_to_mutate = config.rng().random_range(0..function_count);
let mut visited_functions = 0;
let readers = sectionreader.into_iter().collect::<Result<Vec<_>, _>>()?;
loop {
if visited_functions == function_count {
return Err(Error::no_mutations_applicable());
}
let reader = readers[function_to_mutate as usize].clone();
let operatorreader = reader.get_operators_reader()?;
let mut localsreader = reader.get_locals_reader()?;
let operators = operatorreader
.into_iter_with_offsets()
.collect::<wasmparser::Result<Vec<OperatorAndByteOffset>>>()?;
let operatorscount = operators.len();
let mut opcode_to_mutate = config.rng().random_range(0..operatorscount);
log::trace!(
"Selecting operator {opcode_to_mutate}/{operatorscount} from function {function_to_mutate}",
);
let locals = self.get_func_locals(
config.info(),
function_to_mutate + config.info().num_imported_functions(), /* the function type is shifted
by the imported functions*/
&mut localsreader,
)?;
let mut count = 0;
loop {
config.consume_fuel(1)?;
if count == operatorscount {
break;
}
let mut dfg = DFGBuilder::new(config);
let basicblock = dfg.get_bb_from_operator(opcode_to_mutate, &operators);
let basicblock = match basicblock {
None => {
log::trace!(
"Basic block cannot be constructed for opcode {:?}",
&operators[opcode_to_mutate]
);
opcode_to_mutate = (opcode_to_mutate + 1) % operatorscount;
count += 1;
continue;
}
Some(basicblock) => basicblock,
};
let minidfg = dfg.get_dfg(config.info(), &operators, &basicblock);
let minidfg = match minidfg {
None => {
log::trace!("DFG cannot be constructed for opcode {opcode_to_mutate}");
opcode_to_mutate = (opcode_to_mutate + 1) % operatorscount;
count += 1;
continue;
}
Some(minidfg) => minidfg,
};
if !minidfg.map.contains_key(&opcode_to_mutate) {
opcode_to_mutate = (opcode_to_mutate + 1) % operatorscount;
count += 1;
continue;
}
// Create an eterm expression from the basic block starting at oidx
let start = minidfg.get_expr(opcode_to_mutate);
if !minidfg.is_subtree_consistent_from_root() {
log::trace!("{start} is not consistent");
opcode_to_mutate = (opcode_to_mutate + 1) % operatorscount;
count += 1;
continue;
};
log::trace!(
"Trying to mutate\n\
{}\n\
at opcode {opcode_to_mutate} in function {function_to_mutate}",
start.pretty(30).trim(),
);
let analysis = PeepholeMutationAnalysis::new(config.info(), locals.clone());
let runner = Runner::<Lang, PeepholeMutationAnalysis, ()>::new(analysis)
.with_iter_limit(1) // FIXME, the iterations should consume fuel from the actual mutator. Be careful with inner set time limits that can lead us to non-deterministic behavior
.with_expr(&start)
.run(rules);
let mut egraph = runner.egraph;
// In theory this will return the Id of the operator eterm
let root = egraph.add_expr(&start);
let startcmp = start.clone();
// If the number of nodes in the egraph is not large, then
// continue the search
if egraph.total_number_of_nodes() <= 1 {
opcode_to_mutate = (opcode_to_mutate + 1) % operatorscount;
count += 1;
continue;
};
log::trace!(
"Egraph built, nodes count = {}",
egraph.total_number_of_nodes()
);
// At this point we spent some resource calculating basic block,
// and constructing the egraph
config.consume_fuel(1)?;
// If reduction mode is requested then yield back the smallest
// graph to start off with. For reduction cases that are
// specifically trying to find an interesting test case though
// the first reduction may not be interesting, so continue to
// chain up the lazy expansions afterwards like we always do.
let iter = if config.reduce {
let mut extractor = egg::Extractor::new(&egraph, egg::AstSize);
let (_best_cost, best_expr) = extractor.find_best(root);
Some(best_expr).into_iter()
} else {
None.into_iter()
};
let iter = iter.chain(lazy_expand_aux(
root,
egraph.clone(),
self.max_tree_depth,
config.rng().random(),
));
// Filter expression equal to the original one
let iterator = iter
.filter(move |expr| !expr.to_string().eq(&startcmp.to_string()))
.map(move |expr| {
log::trace!("Yielding expression:\n{}", expr.pretty(60));
config.consume_fuel(1)?;
let mut newfunc = copy_locals(reader.clone())?;
let needed_resources = Encoder::build_function(
config,
opcode_to_mutate,
&expr,
&operators,
&basicblock,
&mut newfunc,
&minidfg,
&egraph,
)?;
let mut codes = CodeSection::new();
let code_section = config.info().code.unwrap();
let reader = config.info().get_binary_reader(code_section);
let sectionreader = CodeSectionReader::new(reader)?;
// this mutator is applicable to internal functions, so
// it starts by randomly selecting an index between
// the imported functions and the total count, total=imported + internal
for (fidx, func) in sectionreader.into_iter().enumerate() {
let reader = func?;
if fidx as u32 == function_to_mutate {
codes.function(&newfunc);
} else {
codes.raw(reader.as_bytes());
}
}
// Process the outside function needed resources
// Needed globals
let mut new_global_section = GlobalSection::new();
// Reparse and reencode global section
if let Some(global_section) = config.info().globals {
// If the global section was already there, try to copy it to the
// new raw section
let reader = config.info().get_binary_reader(global_section);
let globalreader = GlobalSectionReader::new(reader)?;
RoundtripReencoder
.parse_global_section(&mut new_global_section, globalreader)?;
}
if needed_resources.len() > 0 {
log::trace!("Adding {} additional resources", needed_resources.len());
}
for resource in &needed_resources {
match resource {
ResourceRequest::Global {
tpe: ty,
mutable,
shared,
} => {
let (init, ty) = match ty {
PrimitiveTypeInfo::I32 => {
(ConstExpr::i32_const(0), ValType::I32)
}
PrimitiveTypeInfo::I64 => {
(ConstExpr::i64_const(0), ValType::I64)
}
PrimitiveTypeInfo::F32 => {
(ConstExpr::f32_const(0.0.into()), ValType::F32)
}
PrimitiveTypeInfo::F64 => {
(ConstExpr::f64_const(0.0.into()), ValType::F64)
}
PrimitiveTypeInfo::V128 => {
(ConstExpr::v128_const(0), ValType::V128)
}
_ => unreachable!("Not valid for globals"),
};
let ty = wasm_encoder::GlobalType {
val_type: ty,
mutable: *mutable,
shared: *shared,
};
// Add to globals
new_global_section.global(ty, &init);
}
}
}
let code_index = config.info().code;
let global_index = config.info().globals;
// This conditional placing enforces to write the global
// section by respecting its relative order in the Wasm module
let insert_globals_before = config
.info()
.globals
.or(config.info().exports)
.or(config.info().start)
.or(config.info().elements)
.or(config.info().data_count)
.or(code_index);
// If the mutator is in this staeg, then it passes the can_mutate filter,
// which checks for code section existence
let insert_globals_before = insert_globals_before.unwrap();
let module = config.info().replace_multiple_sections(
move |index, _sectionid, module: &mut wasm_encoder::Module| {
if insert_globals_before == index
// Write if needed or if it wasm in the init Wasm
&& (new_global_section.len() > 0 || global_index.is_some() )
{
// Insert the new globals here
module.section(&new_global_section);
}
if index == code_index.unwrap() {
// Replace code section
module.section(&codes);
return true;
}
if let Some(gidx) = global_index {
// return true since the global section is written by the
// conditional position writer
return gidx == index;
}
// False to say the underlying encoder to write the preexisting
// section
false
},
);
Ok(module)
})
.map_while(|module: Result<Module>| match module {
Ok(module) => Some(Ok(module)),
Err(e) if matches!(e.kind(), ErrorKind::OutOfFuel) => None,
Err(e) => Some(Err(e)),
});
return Ok(Box::new(iterator));
}
function_to_mutate = (function_to_mutate + 1) % function_count;
visited_functions += 1;
}
fn copy_locals(reader: FunctionBody) -> Result<Function> {
// Create the new function
let mut localreader = reader.get_locals_reader()?;
// Get current locals and map to encoder types
let mut local_count = 0;
let current_locals = (0..localreader.get_count())
.map(|_| {
let (count, ty) = localreader.read().unwrap();
local_count += count;
(count, map_type(ty).unwrap())
})
.collect::<Vec<(u32, ValType)>>();
Ok(Function::new(current_locals /*copy locals here*/))
}
}
/// To separate the methods will allow us to test rule by rule
fn mutate_with_rules<'a>(
&self,
config: &'a mut WasmMutate,
rules: &[Rewrite<Lang, PeepholeMutationAnalysis>],
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> {
self.random_mutate(config, rules)
}
}
/// Meta mutator for peephole
impl Mutator for PeepholeMutator {
fn mutate<'a>(
&self,
config: &'a mut crate::WasmMutate,
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> {
let rules = match self.rules.clone() {
Some(rules) => rules,
// Calculate here type related information for parameters, locals and returns
// This information could be passed to the conditions to check for type correctness rewriting
// Write the new rules in the rules.rs file
None => self.get_rules(config),
};
let modules = self.mutate_with_rules(config, &rules)?;
Ok(modules)
}
fn can_mutate<'a>(&self, config: &'a WasmMutate) -> bool {
config.info().has_code() && config.info().num_local_functions() > 0
}
}
/// This macro is meant to be used for testing deep mutators
/// It receives the original wat text variable, the expression returning the mutated function and the expected wat
/// For an example, look at SwapCommutativeOperator
#[cfg(test)]
#[macro_export]
macro_rules! match_code_mutation {
($wat: ident, $mutation:expr, $expected:ident) => {{
let original = &wat::parse_str($wat).unwrap();
let mut parser = Parser::new(0);
let config = WasmMutate::default();
let mut offset = 0;
let mut modu = Module::new();
let mut codesection = CodeSection::new();
loop {
let (payload, chunksize) = match parser.parse(&original[offset..], true).unwrap() {
Chunk::NeedMoreData(_) => {
panic!("This should not be reached");
}
Chunk::Parsed { consumed, payload } => (payload, consumed),
};
offset += chunksize;
match payload {
Payload::TypeSection(reader) => {
modu.section(&RawSection {
id: SectionId::Type.into(),
data: &original[reader.range().start..reader.range().end],
});
}
Payload::FunctionSection(reader) => {
modu.section(&RawSection {
id: SectionId::Function.into(),
data: &original[reader.range().start..reader.range().end],
});
}
Payload::ExportSection(reader) => {
modu.section(&RawSection {
id: SectionId::Export.into(),
data: &original[reader.range().start..reader.range().end],
});
}
Payload::CodeSectionEntry(reader) => {
let operatorsreader = reader.get_operators_reader().unwrap();
let range = operatorsreader.get_binary_reader().range();
let operators = operatorsreader
.into_iter_with_offsets()
.collect::<wasmparser::Result<Vec<OperatorAndByteOffset>>>()
.unwrap();
let mutated = $mutation(&config, operators, reader, range, original);
codesection.function(&mutated);
}
wasmparser::Payload::End => break,
_ => {
// do nothing
}
}
}
modu.section(&codesection);
let mutated = modu.finish();
crate::validate(&mutated);
let text = wasmprinter::print_bytes(mutated).unwrap();
// parse expected to use the same formatter
let expected_bytes = &wat::parse_str($expected).unwrap();
let expectedtext = wasmprinter::print_bytes(expected_bytes).unwrap();
assert_eq!(text, expectedtext);
}};
}
#[cfg(test)]
mod tests {
use crate::{
WasmMutate,
info::ModuleInfo,
module::PrimitiveTypeInfo,
mutators::{Mutator, peephole::PeepholeMutator},
};
use egg::{Id, Rewrite, Subst, rewrite};
use rand::{SeedableRng, rngs::SmallRng};
use super::{EG, PeepholeMutationAnalysis};
use crate::mutators::peephole::Lang;
/// Condition to apply the unfold operator
/// check that the var is a constant
#[allow(dead_code)]
fn is_const(vari: &'static str) -> impl Fn(&mut EG, Id, &Subst) -> bool {
move |egraph: &mut EG, _, subst| {
let var = vari.parse();
match var {
Ok(var) => {
let eclass = &egraph[subst[var]];
if eclass.nodes.len() == 1 {
let node = &eclass.nodes[0];
match node {
Lang::I32(_) => true,
Lang::I64(_) => true,
_ => false,
}
} else {
false
}
}
Err(_) => false,
}
}
}
fn is_type(vari: &'static str, t: PrimitiveTypeInfo) -> impl Fn(&mut EG, Id, &Subst) -> bool {
move |egraph: &mut EG, _, subst| {
let var = vari.parse();
match var {
Ok(var) => {
let eclass = &egraph[subst[var]];
match &eclass.data {
Some(d) => d.tpe == t,
None => false,
}
}
Err(_) => false,
}
}
}
// Random numbers vary by pointer-width presumably due to `usize` at some
// point factoring in, and this test is only known to pass within a
// reasonable amount of time on 64-bit platforms.
#[test]
#[cfg(target_pointer_width = "64")]
fn test_peep_unfold2() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("unfold-2"; "?x" => "(i32.unfold ?x)" if is_const("?x") if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 1731343737
i32.const -1731343681
i32.add)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_stack_neutral2() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("strength-undo"; "?x" => "(i32.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (local i32 i32)
i32.const 10
drop
)
)
"#,
rules,
r#"
(module
(type (;0;) (func ))
(func (;0;) (type 0)
(local i32 i32)
i32.const 10
i32.const 10
i32.or
drop
)
(export "exported_func" (func 0)))
"#,
4,
);
}
#[test]
fn test_peep_select() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("rule"; "(select ?x ?y ?z)" => "(select ?y ?x (i32.eqz ?z))")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result f32) (local i32 i32)
f32.const 10
f32.const 20
f32.add
f32.const 200
local.get 0
select
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result f32)))
(func (;0;) (type 0) (result f32)
(local i32 i32)
f32.const 0x1.9p+7 (;=200;)
f32.const 0x1.4p+3 (;=10;)
f32.const 0x1.4p+4 (;=20;)
f32.add
local.get 0
i32.eqz
select)
(export "exported_func" (func 0)))
"#,
4,
);
}
#[test]
fn test_peep_wrap() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("strength-undo"; "?x" => "(i32.add ?x i32.const.0)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i64)
local.get 0
i64.const 0
i64.shl
i32.wrap_i64
i32.const -441701230
i32.const 441701230
i32.add
i32.add
)
)
"#,
rules,
r#"
(module
(func (export "exported_func") (result i32) (local i64)
local.get 0
i64.const 0
i64.shl
i32.wrap_i64
i32.const -441701230
i32.const 0
i32.add
i32.const 441701230
i32.add
i32.add
)
)
"#,
0,
);
}
#[test]
fn test_peep_irelop1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("strength-undo"; "(i64.eqz ?x)" => "(i64.eq ?x i64.const.0)")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i64.const 10
i64.eqz
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32) ))
(func (;0;) (type 0)
(local i32 i32)
i64.const 10
i64.const 0
i64.eq
)
(export "exported_func" (func 0)))
"#,
2,
);
}
#[test]
fn test_peep_bug1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("strength-undo"; "?x" => "(i32.shl ?x i32.const.0)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
i32.const -14671840
i64.extend_i32_u
i32.const -1
i64.extend_i32_u
i64.rem_s
i64.const -1
i64.le_u)
(data (;0;) ""))
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
i32.const -14671840
i64.extend_i32_u
i32.const -1
i64.extend_i32_u
i64.rem_s
i64.const -1
i64.le_u
i32.const 0
i32.shl)
(data (;0;) ""))
"#,
11494877297919394048,
);
}
#[test]
fn test_peep_commutative() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("commutative-1"; "(i32.add ?x ?y)" => "(i32.add ?y ?x)")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 42
i32.const 1
i32.add
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 1
i32.const 42
i32.add
)
(export "exported_func" (func 0)))
"#,
6,
);
}
#[test]
fn test_peep_inversion() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("inversion-1"; "(i32.gt_s ?x ?y)" => "(i32.le_s ?y ?x)")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 42
i32.const 1
i32.gt_s
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 1
i32.const 42
i32.le_s)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_integrtion() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("inversion-1"; "(i32.gt_s ?x ?y)" => "(i32.le_s ?y ?x)")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 42
i32.const 1
i32.gt_s
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 1
i32.const 42
i32.le_s)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_inversion2() {
let original = r#"
(module
(type (;0;) (func (param i64 i32 f32)))
(func (;0;) (type 0) (param i64 i32 f32)
i32.const 100
i32.const 200
i32.store offset=600 align=1
)
(memory (;0;) 0)
(export "\00" (memory 0)))
"#;
let original = &wat::parse_str(original).unwrap();
let info = ModuleInfo::new(original).unwrap();
let mut wasmmutate = WasmMutate::default();
wasmmutate.fuel(3);
wasmmutate.info = Some(info);
let rnd = SmallRng::seed_from_u64(0);
wasmmutate.rng = Some(rnd);
let mutator = PeepholeMutator::new(2);
let can_mutate = mutator.can_mutate(&wasmmutate);
assert_eq!(can_mutate, true);
let rules = mutator.get_rules(&wasmmutate);
for mutated in mutator.mutate_with_rules(&mut wasmmutate, &rules).unwrap() {
let module = mutated.unwrap();
let mutated_bytes = &module.finish();
let _text = wasmprinter::print_bytes(mutated_bytes).unwrap();
crate::validate(mutated_bytes);
}
}
#[test]
fn test_mem_store1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("rule"; "(i32.store.600.0.0 ?value ?offset)" => "(i32.store.0.0.0 ?value (i32.add ?offset i32.const.600))" ),
];
test_peephole_mutator(
r#"
(module
(type (;0;) (func (param i64 i32 f32)))
(func (;0;) (type 0) (param i64 i32 f32)
i32.const 100
i32.const 200
i32.store offset=600 align=1
)
(memory (;0;) 0)
(export "\00" (memory 0)))
"#,
rules,
r#"
(module
(type (;0;) (func (param i64 i32 f32)))
(func (;0;) (type 0) (param i64 i32 f32)
i32.const 100
i32.const 200
i32.const 600
i32.add
i32.store align=1)
(memory (;0;) 0)
(export "\00" (memory 0)))
"#,
0,
);
}
#[test]
fn test_peep_shl0() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("strength-undo3"; "(i64.shr_u ?x ?y)" => "(i64.shl (i64.shr_u ?x ?y) i64.const.0)" ),
];
test_peephole_mutator(
r#"
(module
(type (;0;) (func (param i64 i32 f32)))
(func (;0;) (type 0) (param i64 i32 f32)
i64.const 89
local.get 1
i64.load align=2
local.get 1
i64.load align=1
i64.shr_u
drop
drop
)
(memory (;0;) 0)
(export "\00" (memory 0)))
"#,
rules,
r#"
(module
(type (;0;) (func (param i64 i32 f32)))
(func (;0;) (type 0) (param i64 i32 f32)
i64.const 89
local.get 1
i64.load align=2
local.get 1
i64.load align=1
i64.shr_u
i64.const 0
i64.shl
drop
drop)
(memory (;0;) 0)
(export "\00" (memory 0)))
"#,
5,
);
}
#[test]
fn test_peep_idem1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("idempotent-1"; "?x" => "(i32.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32)),
rewrite!("idempotent-12"; "?x" => "(i64.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I64)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 56
i32.const 56
i32.or)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_cv() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("idempotent-1"; "?x" => "(i32.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i64.const 56
i64.const 2
i64.mul
i32.wrap_i64
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i64.const 56
i64.const 2
i64.mul
i32.wrap_i64
i64.const 56
i64.const 2
i64.mul
i32.wrap_i64
i32.or)
(export "exported_func" (func 0)))
"#,
4,
);
}
#[test]
fn test_peep_cv4() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("idempotent-1"; "?x" => "(i32.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
i32.extend8_s
)
)
"#,
rules,
r#"
(module
(func (;0;) (result i32)
(local i32 i32)
i32.const 56
i32.extend8_s
i32.const 56
i32.extend8_s
i32.or)
(export "exported_func" (func 0)))
"#,
8,
);
}
#[test]
fn test_peep_cv5() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("cv4"; "?x" => "(i32.and ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32))];
test_peephole_mutator(
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
i32.const -1
i64.extend_i32_u
i64.const -1
i64.ge_s)
(data (;0;) ""))
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
i32.const -1
i64.extend_i32_u
i64.const -1
i64.ge_s
i32.const -1
i64.extend_i32_u
i64.const -1
i64.ge_s
i32.and)
(data (;0;) ""))
"#,
10,
);
}
#[test]
fn test_use_global() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("rule"; "?x" => "(i32.use_of_global ?x)")];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 10
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 10
global.set 0
global.get 0)
(global (;0;) (mut i32) i32.const 0)
(export "exported_func" (func 0)))
"#,
4,
);
}
#[test]
fn test_peep_idem3() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("idempotent-3"; "?x" => "(i32.add ?x i32.const.0)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 56
i32.const 0
i32.add)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_idem4() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("idempotent-4"; "?x" => "(i32.mul ?x i32.const.1)" if is_type("?x", PrimitiveTypeInfo::I32)),
rewrite!("idempotent-4"; "?x" => "(i64.mul ?x i32.const.1)" if is_type("?x", PrimitiveTypeInfo::I64)),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 56
i32.const 1
i32.mul)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_typeinfo() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("type1-1"; "?x" => "(i32.shr_u ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32) ),
];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
i32.const 56
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
i32.const 56
i32.const 56
i32.shr_u)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_locals1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("type1-1"; "(i32.add ?x ?y)" => "(i32.add ?y ?x)" )];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i32) (local i32 i32)
local.get 0
local.get 1
i32.add
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i32)))
(func (;0;) (type 0) (result i32)
(local i32 i32)
local.get 1
local.get 0
i32.add)
(export "exported_func" (func 0)))
"#,
5,
);
}
#[test]
fn test_peep_locals3() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("type1-1"; "(local.set.1 i32.const.100)" => "(local.set.1 i32.const.0)" )];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (local i32 i32)
i32.const 100
local.set 1
)
)
"#,
rules,
r#"
(module
(type (;0;) (func ))
(func (;0;) (type 0)
(local i32 i32)
i32.const 0
local.set 1
)
(export "exported_func" (func 0)))
"#,
1,
);
}
#[test]
fn test_peep_functions() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("type1-1"; "(call.0 ?x ?y)" => "(call.0 i64.const.1 i32.const.11) " )];
test_peephole_mutator(
r#"
(module
(func (export "exported_func")(param i64 i32 ) (result i64) (local i64 i32)
local.get 0
i32.const 10
call 0
)
)
"#,
rules,
r#"
(module
(func (export "exported_func") (param i64 i32) (result i64) (local i64 i32)
i64.const 1
i32.const 11
call 0
)
)
"#,
0,
);
}
#[test]
fn test_peep_functions2() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("type1-1"; "?x" => "(i32.or ?x ?x)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(type (;0;) (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i32)))
(type (;1;) (func (param i64) (result i32)))
(import "ttttttttttttuttttttttttut\09" "" (func (;0;) (type 1)))
(func (;1;) (type 0) (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i32)
(local i32)
local.get 6
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
call 0)
)
"#,
rules,
r#"
(module
(type (;0;) (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i32)))
(type (;1;) (func (param i64) (result i32)))
(import "ttttttttttttuttttttttttut\09" "" (func (;0;) (type 1)))
(func (;1;) (type 0) (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i32)
(local i32)
local.get 6
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
call 0
local.get 6
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
call 0
local.get 6
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
local.get 6
i64.div_s
call 0
i32.or
i32.or
)
)
"#,
9,
);
}
#[test]
fn test_peep_locals2() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("type1-1"; "(i64.add ?x ?y)" => "(i64.add ?y ?x)" )];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result i64) (local i64 i64)
local.get 0
local.get 1
i64.add
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result i64)))
(func (;0;) (type 0) (result i64)
(local i64 i64)
local.get 1
local.get 0
i64.add)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_floats1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] =
&[rewrite!("rule"; "f32.const.1,0" => "f32.const.0,0" )];
test_peephole_mutator(
r#"
(module
(func (export "exported_func") (result f32) (local i64 i64)
f32.const 1.0
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (result f32)))
(func (;0;) (type 0) (result f32)
(local i64 i64)
f32.const 0x0p+0 (;=0;))
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_globals1() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("mem-load-shift"; "?x" => "(i32.add ?x i32.const.0)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(memory 1)
(global $0 i32 i32.const 0)
(func (export "exported_func") (param i32) (result i32)
global.get 0
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (param i32) (result i32)))
(global $0 i32 i32.const 0)
(func (;0;) (type 0) (param i32) (result i32)
global.get $0
i32.const 0
i32.add)
(memory (;0;) 1)
(export "exported_func" (func 0)))
"#,
0,
);
}
#[test]
fn test_peep_globals2() {
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
rewrite!("rule"; "?x" => "(i32.add ?x i32.const.0)" if is_type("?x", PrimitiveTypeInfo::I32)),
];
test_peephole_mutator(
r#"
(module
(memory 1)
(global $0 (mut i32) i32.const 0)
(func (export "exported_func") (param i32) (result i32)
i32.const 10
global.set 0
i32.const 20
)
)
"#,
rules,
r#"
(module
(type (;0;) (func (param i32) (result i32)))
(func (;0;) (type 0) (param i32) (result i32)
i32.const 10
global.set $0
i32.const 20
i32.const 0
i32.add)
(memory (;0;) 1)
(global $0 (mut i32) i32.const 0)
(export "exported_func" (func 0)))
"#,
4,
);
}
#[test]
fn remove_local_set() {
test_default_peephole_mutator(
"(module (func (local i32) (local.set 0 (i32.const 0))))",
"(module (func (local i32) nop))",
4,
);
}
#[test]
fn remove_local_tee() {
test_default_peephole_mutator(
"(module (func (local i32) (local.tee 0 (i32.const 0)) drop))",
"(module (func (local i32) (i32.const 0) drop))",
4,
);
}
fn test_peephole_mutator(
original: &str,
rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>],
expected: &str,
seed: u64,
) {
let mut config = WasmMutate::default();
config.fuel(10000);
config.seed(seed);
let mutator = PeepholeMutator::new_with_rules(3, rules.to_vec());
config.match_mutation(original, mutator, expected);
}
fn test_default_peephole_mutator(original: &str, expected: &str, seed: u64) {
let original_wasm = wat::parse_str(original).unwrap();
let mut config = WasmMutate::default();
config.fuel(10000);
config.seed(seed);
config.info = Some(ModuleInfo::new(&original_wasm).unwrap());
let mut mutator = PeepholeMutator::new(3);
let rules = mutator.get_rules(&config);
mutator.rules = Some(rules);
config.match_mutation(original, mutator, expected);
}
#[test]
fn i8x16_shuffle_handled() {
test_default_peephole_mutator(
"(module (func (param v128 v128)
local.get 0
local.get 1
i8x16.shuffle 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
drop)
)",
"(module (func (param v128 v128)))",
4,
);
}
}