canfuzz 0.6.0

A coverage-guided fuzzing framework for Internet Computer canisters, built on `libafl` and `pocket-ic`
Documentation
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
//! This module provides functionality to instrument WebAssembly (Wasm) modules
//! for coverage-guided fuzzing, specifically implementing an AFL-style instrumentation.
//!
//! The primary goal is to inject code into a Wasm module that tracks execution paths.
//! This is achieved by:
//! 1.  Injecting global variables to maintain state, such as the previous location.
//! 2.  Injecting a helper function that contains the core instrumentation logic.
//! 3.  Instrumenting the Wasm bytecode by inserting calls to the helper function at the
//!     start of each function and before every branch, effectively covering all basic blocks.
//! 4.  Exporting a coverage function that allows the fuzzer to retrieve the
//!     coverage map from the canister.
//!
//! ## Instruction Count Instrumentation
//!
//! When [`InstrumentationArgs::instrument_instruction_count`] is enabled, the module also
//! performs instruction-count instrumentation via `ic0.performance_counter`. This allows the
//! fuzzer to maximize the number of IC instructions consumed by canister methods, without
//! requiring any changes to the target canister's source code.
//!
//! The instruction counting works by:
//! 1.  Wrapping each `canister_update` export in a new function that
//!     reads `ic0.performance_counter(1)` after the original method returns.
//! 2.  Subtracting the estimated overhead of AFL instrumentation (computed from the IC
//!     instruction cost model and `history_size`) to isolate the canister's own cost.
//! 3.  Exporting a [`INSTRUCTION_COUNT_FN_EXPORT_NAME`](crate::constants::INSTRUCTION_COUNT_FN_EXPORT_NAME)
//!     function that replies with the 8-byte little-endian instruction count.
//!
//! ### Limitations
//!
//! - **Trapped executions report 0 instructions.** The wrapper function runs *after* the
//!   original method returns. If execution never reaches that point, the instruction count
//!   global keeps its previous value (reset to 0 at the start of each wrapper call). Traps
//!   can originate from several sources:
//!   - **Explicit traps:** The canister calls `ic0.trap(...)` or `ic_cdk::trap(...)`.
//!   - **Wasm `unreachable`:** Rust's `panic!`/`unwrap()`/`expect()` compile to
//!     `unreachable` after printing the panic message via `ic0.trap`.
//!   - **Implicit wasm traps:** Integer divide-by-zero, integer overflow on `i32.trunc_f64_s`,
//!     out-of-bounds memory access, out-of-bounds table access, indirect call type mismatch,
//!     and stack overflow all cause the wasm runtime to trap immediately.
//!   - **System API traps:** Many `ic0.*` calls can trap on invalid arguments (e.g.,
//!     `ic0.msg_reply` called twice, `ic0.canister_cycle_balance` in the wrong context,
//!     `ic0.stable_read` with out-of-bounds offset). The system call never returns and the
//!     wasm execution is aborted.
//!   - **Instruction limit exceeded:** The IC halts execution when the per-message instruction
//!     limit is reached, which behaves identically to a trap.
//!
//!   In all of these cases the wrapper never executes, so the instruction count is 0. These
//!   inputs are still captured by `CrashFeedback` / `TimeoutFeedback` and saved as crashes
//!   or timeouts.
//!
//! - **The AFL overhead discount is approximate.** It assumes fixed IC instruction costs per
//!   wasm opcode (1 per opcode, 5 for `call`, 200 for `ic0.performance_counter` system API
//!   overhead). The IC's actual cost model may differ slightly. For fuzzing guidance (relative
//!   ordering of inputs), approximate is sufficient.
//!
//! - **Query and composite query methods are not wrapped.** The wrapper stores the
//!   instruction count in a wasm global, which requires state to be committed. Query calls
//!   execute against a state snapshot and discard all mutations (including global writes),
//!   so the count would be lost before it can be read back. Only `canister_update` exports
//!   are instrumented for instruction counting.
//!
//! - **`performance_counter(1)` includes inter-canister call instructions.** If the target
//!   method makes downstream calls, the counter includes instructions executed in callbacks.
//!   This is generally desirable (total cost of the message), but means the count is not
//!   purely the target canister's own instructions.

use anyhow::Result;
use rand::Rng;
use rand::RngCore;
use rand::SeedableRng;
use wirm::ir::function::FunctionBuilder;
use wirm::ir::id::{FunctionID, GlobalID, LocalID, MemoryID};
use wirm::ir::module::module_functions::FuncKind;
use wirm::ir::types::{InitExpr, Instructions, Value};
use wirm::module_builder::AddLocal;
use wirm::opcode::Inject;
use wirm::wasmparser::{ExternalKind, MemArg, Operator, Validator};
use wirm::{DataType, InitInstr, Module, Opcode};

use crate::constants::{
    AFL_COVERAGE_MAP_SIZE, API_VERSION_IC0, COVERAGE_FN_EXPORT_NAME,
    INSTRUCTION_COUNT_FN_EXPORT_NAME,
};
use std::collections::HashSet;

/// Arguments for configuring the Wasm instrumentation process.
pub struct InstrumentationArgs {
    /// The raw Wasm module to instrument.
    pub wasm_bytes: Vec<u8>,
    /// The number of previous locations to track (must be 1, 2, 4, or 8).
    pub history_size: usize,
    /// The seed to use for instrumentation.
    pub seed: Seed,
    /// Whether to instrument `canister_update` methods to track instruction counts.
    /// When enabled, wrapper functions are injected that read the IC performance counter
    /// after each method execution and an export function is added to retrieve the count.
    /// Query methods are not wrapped (see module-level limitations).
    pub instrument_instruction_count: bool,
}

/// Specifies the seed for the random number generator used during instrumentation.
///
/// Using a static seed allows for deterministic and reproducible instrumentation,
/// which is crucial for debugging and consistent testing environments.
#[derive(Copy, Clone, Debug)]
pub enum Seed {
    /// A randomly generated seed will be used.
    Random,
    /// A user-provided static seed will be used.
    Static(u32),
}
/// A global, mutable static array to hold the coverage map.
///
/// # Safety
///
/// This is a raw pointer to a mutable static memory region, which is inherently `unsafe`.
/// It is used as a shared memory region between the fuzzer and the instrumented canister.
/// The `libafl` framework is designed to work with such a mechanism, which is a highly
/// optimized approach for coverage-guided fuzzing, inspired by AFL.
pub static mut COVERAGE_MAP: &mut [u8] = &mut [0; AFL_COVERAGE_MAP_SIZE as usize];

/// Instruments the given Wasm bytes for fuzzing.
///
/// This function takes a raw Wasm module, applies AFL-style instrumentation for
/// coverage tracking, and returns the instrumented Wasm module as a vector of bytes.
/// The resulting Wasm is validated before being returned.
///
/// # Arguments
///
/// * `instrumentation_args` - A struct containing the Wasm bytes, history size, and instrumentation seed.
pub fn instrument_wasm_for_fuzzing(instrumentation_args: InstrumentationArgs) -> Vec<u8> {
    assert!(
        matches!(instrumentation_args.history_size, 1 | 2 | 4 | 8),
        "History size must be 1, 2, 4, or 8"
    );
    let mut module = Module::parse(&instrumentation_args.wasm_bytes, false, false)
        .expect("Failed to parse module with wirm");

    instrument_for_afl(&mut module, &instrumentation_args)
        .expect("Unable to instrument wasm module for AFL");

    // Sorry it has to be this way :(
    let buf = vec![0u8; AFL_COVERAGE_MAP_SIZE as usize * instrumentation_args.history_size]
        .into_boxed_slice();
    let buf: &'static mut [u8] = Box::leak(buf);
    unsafe {
        COVERAGE_MAP = buf;
    }

    let instrumented_wasm = module.encode();

    validate_wasm(&instrumented_wasm).expect("Wasm is not valid");

    instrumented_wasm
}

/// The main orchestration function for applying AFL instrumentation.
///
/// It performs the following steps:
/// 1. Resolves all required `ic0` imports upfront (before adding local functions).
/// 2. Injects global variables required for tracking coverage.
/// 3. Injects the [`COVERAGE_FN_EXPORT_NAME`] update function to expose the coverage map.
/// 4. Instruments all functions by inserting calls to a helper function at the
///    start of each function and before each branch instruction.
///
/// When [`InstrumentationArgs::instrument_instruction_count`] is enabled, it additionally:
/// 5. Imports `ic0.performance_counter` and injects instruction-counting globals.
/// 6. Wraps each `canister_update` export to read the instruction counter.
/// 7. Injects the [`INSTRUCTION_COUNT_FN_EXPORT_NAME`] export to retrieve the count.
fn instrument_for_afl(
    module: &mut Module<'_>,
    instrumentation_args: &InstrumentationArgs,
) -> Result<()> {
    let is_memory64 = is_memory64(module);
    let inst_count = instrumentation_args.instrument_instruction_count;

    // Ensure all ic0 imports upfront, before adding any local functions.
    // This is important because wirm's get_func returns import-list position,
    // which only matches the FunctionID before local functions shift indices.
    let (msg_reply_data_append_idx, msg_reply_idx, perf_counter_idx) =
        ensure_ic0_imports(module, is_memory64, inst_count)?;

    let (afl_prev_loc_indices, afl_mem_ptr_idx, instruction_count_globals) = inject_globals(
        module,
        instrumentation_args.history_size,
        is_memory64,
        inst_count,
    );
    println!(
        "  -> Injected globals: prev_locs @ indices {afl_prev_loc_indices:?}, mem_ptr @ index {afl_mem_ptr_idx:?}"
    );

    inject_afl_coverage_export(
        module,
        instrumentation_args.history_size,
        afl_mem_ptr_idx,
        msg_reply_data_append_idx,
        msg_reply_idx,
        is_memory64,
    )?;
    println!("  -> Injected `canister_update __export_coverage_for_afl` function.");

    // Instruction count wrapper functions (injected before branch instrumentation)
    let mut skip_function_ids = HashSet::new();

    let call_count_global = if let Some((ic_global, call_count_global)) = instruction_count_globals
    {
        let perf_counter_idx = perf_counter_idx.unwrap();
        println!(
            "  -> Injected instruction count globals: ic @ {ic_global:?}, call_count @ {call_count_global:?}"
        );
        println!("  -> Ensured ic0.performance_counter import @ {perf_counter_idx:?}");

        let cost_per_afl_call = compute_cost_per_afl_call(instrumentation_args.history_size);
        println!("  -> Computed AFL instrumentation cost per call: {cost_per_afl_call}");

        let wrapper_ids = inject_method_wrappers(
            module,
            call_count_global,
            ic_global,
            perf_counter_idx,
            cost_per_afl_call,
        );
        for id in &wrapper_ids {
            skip_function_ids.insert(*id);
        }
        println!(
            "  -> Injected {} method wrapper(s) for instruction counting.",
            wrapper_ids.len()
        );

        let export_fn_id = inject_instruction_count_export(
            module,
            instrumentation_args.history_size,
            afl_mem_ptr_idx,
            ic_global,
            msg_reply_data_append_idx,
            msg_reply_idx,
            is_memory64,
        )?;
        skip_function_ids.insert(export_fn_id);
        println!("  -> Injected `canister_query {INSTRUCTION_COUNT_FN_EXPORT_NAME}` function.");

        Some(call_count_global)
    } else {
        None
    };

    instrument_branches(
        module,
        &afl_prev_loc_indices,
        afl_mem_ptr_idx,
        instrumentation_args.seed,
        is_memory64,
        &skip_function_ids,
        call_count_global,
    );
    println!("  -> Instrumented branch instructions in all functions.");

    Ok(())
}

/// Injects the necessary global variables for AFL instrumentation.
///
/// **Always injected:**
/// - `__afl_prev_loc_N`: A set of `history_size` mutable i32 (or i64 for wasm64) globals to store the IDs
///   of the previously executed basic blocks.
/// - `__afl_mem_ptr`: An immutable i32 (or i64 for wasm64) global that holds the base address (0) of the coverage map.
///
/// **When `instrument_instruction_count` is true, also injects:**
/// - `__afl_instruction_count`: mutable i64 global storing the instruction count after method execution.
/// - `__afl_instrumentation_call_count`: mutable i64 global counting AFL helper function invocations per execution.
fn inject_globals(
    module: &mut Module<'_>,
    history_size: usize,
    is_memory64: bool,
    instrument_instruction_count: bool,
) -> (Vec<GlobalID>, GlobalID, Option<(GlobalID, GlobalID)>) {
    let mut afl_prev_loc_indices = Vec::with_capacity(history_size);

    let (ptr_type, init_val) = if is_memory64 {
        (DataType::I64, Value::I64(0))
    } else {
        (DataType::I32, Value::I32(0))
    };

    for _ in 0..history_size {
        let global_id = module.add_global(
            InitExpr::new(vec![InitInstr::Value(init_val)]),
            ptr_type,
            true,
            false,
        );
        afl_prev_loc_indices.push(global_id);
    }
    let afl_mem_ptr_idx = module.add_global(
        InitExpr::new(vec![InitInstr::Value(init_val)]),
        ptr_type,
        false,
        false,
    );

    let instruction_count_globals = if instrument_instruction_count {
        let instruction_count_global = module.add_global(
            InitExpr::new(vec![InitInstr::Value(Value::I64(0))]),
            DataType::I64,
            true,
            false,
        );
        let call_count_global = module.add_global(
            InitExpr::new(vec![InitInstr::Value(Value::I64(0))]),
            DataType::I64,
            true,
            false,
        );
        Some((instruction_count_global, call_count_global))
    } else {
        None
    };

    (
        afl_prev_loc_indices,
        afl_mem_ptr_idx,
        instruction_count_globals,
    )
}

/// Injects the `canister_update `[COVERAGE_FN_EXPORT_NAME]` function.
///
/// This exported function allows the fuzzer orchestrator to read the canister,
/// retrieve the coverage map and reset it. It uses the `ic0.msg_reply_data_append` and
/// `ic0.msg_reply` System API calls to send the contents of the coverage map
/// back to the caller.
fn inject_afl_coverage_export<'a>(
    module: &mut Module<'a>,
    history_size: usize,
    afl_mem_ptr_idx: GlobalID,
    msg_reply_data_append_idx: FunctionID,
    msg_reply_idx: FunctionID,
    is_memory64: bool,
) -> Result<()> {
    let mut func_builder = FunctionBuilder::new(&[], &[]);

    if is_memory64 {
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i64_const(AFL_COVERAGE_MAP_SIZE as i64 * history_size as i64)
            .call(msg_reply_data_append_idx)
            .call(msg_reply_idx)
            .global_get(afl_mem_ptr_idx)
            .i32_const(0)
            .i64_const(AFL_COVERAGE_MAP_SIZE as i64 * history_size as i64)
            .memory_fill(0);
    } else {
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i32_const(AFL_COVERAGE_MAP_SIZE * history_size as i32)
            .call(msg_reply_data_append_idx)
            .call(msg_reply_idx)
            .global_get(afl_mem_ptr_idx)
            .i32_const(0)
            .i32_const(AFL_COVERAGE_MAP_SIZE * history_size as i32)
            .memory_fill(0);
    }

    let coverage_function_id = func_builder.finish_module(module);
    let export_name = format!("canister_update {COVERAGE_FN_EXPORT_NAME}");
    module
        .exports
        .add_export_func(export_name, coverage_function_id.0);

    Ok(())
}

/// Instruments all local functions in the module to track code coverage.
///
/// This function iterates through every instruction in every function body.
/// It inserts a call to a helper instrumentation function at the beginning of the function
/// and before each branch-like instruction (`If`, `Else`, `Block`, `Loop`, `Br`, `BrIf`, `BrTable`).
/// This ensures that every basic block is instrumented.
fn instrument_branches(
    module: &mut Module<'_>,
    afl_prev_loc_indices: &[GlobalID],
    afl_mem_ptr_idx: GlobalID,
    seed: Seed,
    is_memory64: bool,
    skip_function_ids: &HashSet<FunctionID>,
    call_count_global: Option<GlobalID>,
) {
    let instrumentation_function = afl_instrumentation_slice(
        module,
        afl_prev_loc_indices,
        afl_mem_ptr_idx,
        is_memory64,
        call_count_global,
    );

    let seed = match seed {
        Seed::Random => rand::rng().next_u32(),
        Seed::Static(s) => s,
    };
    println!("The seed used for instrumentation is {seed}");
    let mut rng = rand::rngs::StdRng::seed_from_u64(seed as u64);

    let mut create_instrumentation_ops = |ops: &mut Vec<Operator>| {
        let curr_location =
            rng.random_range(0..AFL_COVERAGE_MAP_SIZE * afl_prev_loc_indices.len() as i32);

        if is_memory64 {
            ops.push(Operator::I64Const {
                value: curr_location as i64,
            });
        } else {
            ops.push(Operator::I32Const {
                value: curr_location as i32,
            });
        }
        ops.push(Operator::Call {
            function_index: instrumentation_function.0,
        });
    };

    for (function_index, function) in module.functions.iter_mut().enumerate() {
        let func_id = FunctionID(function_index as u32);
        if matches!(function.kind(), FuncKind::Local(_))
            && func_id != instrumentation_function
            && !skip_function_ids.contains(&func_id)
        {
            let local_function = function.unwrap_local_mut();
            let mut new_instructions = Vec::with_capacity(local_function.body.num_instructions * 2);

            create_instrumentation_ops(&mut new_instructions);

            for instruction in local_function.body.instructions.get_ops() {
                match instruction {
                    Operator::Block { .. }
                    | Operator::Loop { .. }
                    | Operator::If { .. }
                    | Operator::Else => {
                        new_instructions.push(instruction.clone());
                        create_instrumentation_ops(&mut new_instructions);
                    }
                    Operator::Br { .. }
                    | Operator::BrIf { .. }
                    | Operator::BrTable { .. }
                    | Operator::Return => {
                        create_instrumentation_ops(&mut new_instructions);
                        new_instructions.push(instruction.clone());
                    }
                    _ => new_instructions.push(instruction.clone()),
                }
            }
            // offsets are set to zero, as we are not interested in preserving them.
            local_function.body.instructions = Instructions::new(
                new_instructions.iter().map(|i| (i.clone(), 0)).collect(),
                0,
                false,
            );
        }
    }
}

/// Creates and injects a helper function that contains the core AFL instrumentation logic.
///
/// This function will be called from instrumented locations (start of functions, before branches).
/// It implements the standard AFL coverage tracking mechanism:
/// ```text
///   curr_location = <COMPILE_TIME_RANDOM>;
///   key = curr_location ^ prev_loc[0] ^ ... ^ prev_loc[history_size-1];
///   shared_mem[key]++;
///   prev_loc[history_size-1] = prev_loc[history_size-2] >> 1;
///   ...
///   prev_loc[0] = curr_location >> 1;
/// ```
/// The generated function takes the current location (`curr_location`) as an i32 (or i64 for wasm64)
/// parameter and is added to the module.
///
/// # Returns
///
/// The `FunctionID` of the newly created helper function.
fn afl_instrumentation_slice(
    module: &mut Module<'_>,
    afl_prev_loc_indices: &[GlobalID],
    afl_mem_ptr_idx: GlobalID,
    is_memory64: bool,
    call_count_global: Option<GlobalID>,
) -> FunctionID {
    if is_memory64 {
        let mut func_builder = FunctionBuilder::new(&[DataType::I64], &[]);
        let curr_location = LocalID(0);
        let afl_local_idx = func_builder.add_local(DataType::I64);

        func_builder.local_get(curr_location);
        for &prev_loc_idx in afl_prev_loc_indices {
            func_builder.global_get(prev_loc_idx).i64_xor();
        }

        func_builder
            .global_get(afl_mem_ptr_idx)
            .i64_add()
            .local_tee(afl_local_idx)
            .local_get(afl_local_idx)
            .i64_load8_u(MemArg {
                offset: 0,
                align: 0,
                memory: 0,
                max_align: 0,
            })
            .i64_const(1)
            .i64_add();

        // i64_store8 opcode trait doesn't exist
        func_builder.inject(Operator::I64Store8 {
            memarg: MemArg {
                offset: 0,
                align: 0,
                memory: 0,
                max_align: 0,
            },
        });

        // Shift the history
        for i in (1..afl_prev_loc_indices.len()).rev() {
            func_builder
                .global_get(afl_prev_loc_indices[i - 1])
                .i64_const(1)
                .i64_shr_unsigned()
                .global_set(afl_prev_loc_indices[i]);
        }

        func_builder
            .local_get(curr_location)
            .i64_const(1)
            .i64_shr_unsigned()
            .global_set(afl_prev_loc_indices[0]);

        // Increment AFL call counter if instruction counting is enabled
        if let Some(call_count_idx) = call_count_global {
            func_builder
                .global_get(call_count_idx)
                .i64_const(1)
                .i64_add()
                .global_set(call_count_idx);
        }

        func_builder.finish_module(module)
    } else {
        let mut func_builder = FunctionBuilder::new(&[DataType::I32], &[]);
        let curr_location = LocalID(0);
        let afl_local_idx = func_builder.add_local(DataType::I32);

        func_builder.local_get(curr_location);
        for &prev_loc_idx in afl_prev_loc_indices {
            func_builder.global_get(prev_loc_idx).i32_xor();
        }

        func_builder
            .global_get(afl_mem_ptr_idx)
            .i32_add()
            .local_tee(afl_local_idx)
            .local_get(afl_local_idx)
            .i32_load8_u(MemArg {
                offset: 0,
                align: 0,
                memory: 0,
                max_align: 0,
            })
            .i32_const(1)
            .i32_add()
            .i32_store8(MemArg {
                offset: 0,
                align: 0,
                memory: 0,
                max_align: 0,
            });

        // Shift the history
        for i in (1..afl_prev_loc_indices.len()).rev() {
            func_builder
                .global_get(afl_prev_loc_indices[i - 1])
                .i32_const(1)
                .i32_shr_unsigned()
                .global_set(afl_prev_loc_indices[i]);
        }

        func_builder
            .local_get(curr_location)
            .i32_const(1)
            .i32_shr_unsigned()
            .global_set(afl_prev_loc_indices[0]);

        // Increment AFL call counter if instruction counting is enabled
        if let Some(call_count_idx) = call_count_global {
            func_builder
                .global_get(call_count_idx)
                .i64_const(1)
                .i64_add()
                .global_set(call_count_idx);
        }

        func_builder.finish_module(module)
    }
}

/// Computes the estimated IC instruction cost per AFL instrumentation call site.
///
/// Each instrumentation point consists of a **call site** (inlined at the branch)
/// and the **helper function body** (called from the site). The total cost is the
/// sum of both, assuming IC instruction cost = 1 per wasm opcode except `call` = 5.
///
/// ## Call site (2 wasm instructions, IC cost = 6)
///
/// ```text
/// i32/i64.const <random>   ;; cost 1
/// call $afl_helper          ;; cost 5
/// ```
///
/// ## Helper function body (IC cost = 13 + 6N, where N = history_size)
///
/// ```text
/// ;; XOR block: compute coverage map key          (1 + 2N instructions)
/// local.get(curr_location)                        ;; 1
/// N × (global.get(prev_loc[i]) + i32/i64.xor)    ;; 2N
///
/// ;; Address computation + load-increment-store   (8 instructions)
/// global.get(mem_ptr)                             ;; 1
/// i32/i64.add                                     ;; 1
/// local.tee(afl_local)                            ;; 1
/// local.get(afl_local)                            ;; 1
/// i32/i64.load8_u                                 ;; 1
/// i32/i64.const(1)                                ;; 1
/// i32/i64.add                                     ;; 1
/// i32/i64.store8                                  ;; 1
///
/// ;; History shift                                (4N instructions)
/// (N-1) × (global.get + const(1) + shr_u + global.set)  ;; 4(N-1)
/// local.get + const(1) + shr_u + global.set              ;; 4
///
/// ;; Call counter increment (only when instruction counting is enabled, +4)
/// global.get(call_count) + i64.const(1) + i64.add + global.set(call_count)
/// ```
///
/// ## Total per instrumentation point
///
/// `6 + 13 + 6N + 4 = 23 + 6N` (IC instructions, approximate)
///
/// The wasm32 and wasm64 code paths produce the same number of wasm instructions
/// (just using i32 vs i64 variants), so the instruction count is `23 + 6N` for both.
///
/// Note: the IC applies a `WASM64_INSTRUCTION_COST_OVERHEAD` multiplier (currently 2×)
/// when **charging cycles** for wasm64 execution (see `rs/config/src/subnet_config.rs`
/// in `dfinity/ic`), but this only affects the cycle fee — it does NOT affect
/// `ic0.performance_counter`, which reports the raw instruction count without any
/// multiplier. Therefore the overhead formula is the same for wasm32 and wasm64.
///
/// This is an approximation — the IC's actual cost model may assign different
/// weights to some opcodes. For fuzzing guidance (relative ordering of inputs),
/// an approximate discount is sufficient.
fn compute_cost_per_afl_call(history_size: usize) -> i64 {
    let n = history_size as i64;
    // call site (6) + body (13 + 6N) + call counter increment (4)
    23 + 6 * n
}

/// Fixed IC instruction cost of the wrapper function itself, up to and including the
/// `call ic0.performance_counter` that reads the counter. These instructions are counted
/// by performance_counter but are not part of the original canister method or AFL
/// instrumentation, so they must be subtracted separately.
///
/// System API calls have an additional overhead beyond the wasm `call` opcode cost.
/// The overhead is charged via `charge_for_cpu()` *before* the counter is read, so the
/// value returned by `performance_counter` already includes it. See
/// `rs/embedders/src/wasmtime_embedder/system_api_complexity.rs` in the IC repo for
/// the full list of overhead constants.
///
/// ```text
/// i64.const(0)          ;; 1     — reset call counter
/// global.set            ;; 1
/// call original_method  ;; 5     — call opcode (local function, no system API overhead)
/// i32.const(1)          ;; 1     — perf counter type arg
/// call perf_counter     ;; 205   — call opcode (5) + system API overhead (200)
/// Total                 = 213Th
/// ```
const WRAPPER_OVERHEAD_COST: i64 = 213;

/// Injects wrapper functions around `canister_update` exports.
///
/// Only update exports are wrapped because the wrapper stores the instruction count
/// in a wasm global, which requires state to be committed. Query calls (including
/// composite queries) execute against a state snapshot and discard mutations, so the
/// stored count would be lost before it can be read.
///
/// Each wrapper resets the AFL call counter, calls the original method, reads the
/// performance counter, subtracts estimated AFL overhead and its own fixed overhead,
/// and stores the result.
fn inject_method_wrappers(
    module: &mut Module<'_>,
    call_count_global: GlobalID,
    instruction_count_global: GlobalID,
    perf_counter_idx: FunctionID,
    cost_per_afl_call: i64,
) -> Vec<FunctionID> {
    // Only wrap canister_update exports — see doc comment for why queries are excluded.
    let exports_to_wrap: Vec<(usize, FunctionID)> = module
        .exports
        .iter()
        .enumerate()
        .filter(|(_, exp)| {
            matches!(exp.kind, ExternalKind::Func)
                && exp.name.starts_with("canister_update ")
                && !exp.name.contains(COVERAGE_FN_EXPORT_NAME)
                && !exp.name.contains(INSTRUCTION_COUNT_FN_EXPORT_NAME)
        })
        .map(|(idx, exp)| (idx, FunctionID(exp.index)))
        .collect();

    let mut wrapper_ids = Vec::new();

    for (export_idx, original_func_id) in exports_to_wrap {
        let mut func_builder = FunctionBuilder::new(&[], &[]);

        // Reset AFL call counter
        func_builder.i64_const(0).global_set(call_count_global);

        // Call the original method
        func_builder.call(original_func_id);

        // Read instruction counter (type=1: call context counter)
        func_builder.i32_const(1).call(perf_counter_idx);

        // Subtract AFL instrumentation overhead: call_count * cost_per_call
        func_builder
            .global_get(call_count_global)
            .i64_const(cost_per_afl_call)
            .i64_mul()
            .i64_sub();

        // Subtract the fixed overhead of this wrapper itself (instructions counted
        // before performance_counter returns)
        func_builder.i64_const(WRAPPER_OVERHEAD_COST).i64_sub();

        // Store result
        func_builder.global_set(instruction_count_global);

        let wrapper_id = func_builder.finish_module(module);
        wrapper_ids.push(wrapper_id);

        // Re-point the export to the wrapper
        // We need to get a mutable reference to the export and change its index
        for (idx, exp) in module.exports.iter_mut().enumerate() {
            if idx == export_idx {
                exp.index = *wrapper_id;
                break;
            }
        }
    }

    wrapper_ids
}

/// Injects the `canister_query __export_instruction_count_for_afl` function.
///
/// Exported as a query because it only reads the `__afl_instruction_count` global
/// (set during the preceding update call) and does not modify persistent state.
/// The scratch memory write is transient and discarded after the query returns.
fn inject_instruction_count_export<'a>(
    module: &mut Module<'a>,
    history_size: usize,
    afl_mem_ptr_idx: GlobalID,
    instruction_count_global: GlobalID,
    msg_reply_data_append_idx: FunctionID,
    msg_reply_idx: FunctionID,
    is_memory64: bool,
) -> Result<FunctionID> {
    let scratch_offset = AFL_COVERAGE_MAP_SIZE as i64 * history_size as i64;

    let mut func_builder = FunctionBuilder::new(&[], &[]);

    if is_memory64 {
        // Store instruction count to scratch memory
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i64_const(scratch_offset)
            .i64_add();
        func_builder
            .global_get(instruction_count_global)
            .i64_store(MemArg {
                offset: 0,
                align: 3, // 2^3 = 8 byte alignment for i64
                memory: 0,
                max_align: 0,
            });

        // Reply with 8 bytes from scratch offset
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i64_const(scratch_offset)
            .i64_add()
            .i64_const(8)
            .call(msg_reply_data_append_idx)
            .call(msg_reply_idx);
    } else {
        // Store instruction count to scratch memory
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i32_const(scratch_offset as i32)
            .i32_add();
        func_builder
            .global_get(instruction_count_global)
            .i64_store(MemArg {
                offset: 0,
                align: 3,
                memory: 0,
                max_align: 0,
            });

        // Reply with 8 bytes from scratch offset
        func_builder
            .global_get(afl_mem_ptr_idx)
            .i32_const(scratch_offset as i32)
            .i32_add()
            .i32_const(8)
            .call(msg_reply_data_append_idx)
            .call(msg_reply_idx);
    }

    let function_id = func_builder.finish_module(module);
    let export_name = format!("canister_query {INSTRUCTION_COUNT_FN_EXPORT_NAME}");
    module.exports.add_export_func(export_name, function_id.0);

    Ok(function_id)
}

/// Ensures that the necessary `ic0` System API functions are imported.
///
/// **Always imported:**
/// - `ic0.msg_reply_data_append` — needed by the coverage export function.
/// - `ic0.msg_reply` — needed by the coverage export function.
///
/// **When `instrument_instruction_count` is true, also imports:**
/// - `ic0.performance_counter : (i32) -> (i64)` — needed by method wrappers.
///
/// All imports must be resolved upfront before adding any local functions, because
/// wirm's `get_func` returns import-list positions that only match function-space IDs
/// before local functions shift indices.
fn ensure_ic0_imports(
    module: &mut Module<'_>,
    is_memory64: bool,
    instrument_instruction_count: bool,
) -> Result<(FunctionID, FunctionID, Option<FunctionID>)> {
    let mut data_append_idx = module.imports.get_func(
        API_VERSION_IC0.to_string(),
        "msg_reply_data_append".to_string(),
    );
    let mut reply_idx = module
        .imports
        .get_func(API_VERSION_IC0.to_string(), "msg_reply".to_string());

    if data_append_idx.is_none() {
        let ptr_type = if is_memory64 {
            DataType::I64
        } else {
            DataType::I32
        };
        let type_id = module.types.add_func_type(&[ptr_type, ptr_type], &[]);
        let (func_index, _) = module.add_import_func(
            API_VERSION_IC0.to_string(),
            "msg_reply_data_append".to_string(),
            type_id,
        );
        data_append_idx = Some(func_index);
    }

    if reply_idx.is_none() {
        let type_id = module.types.add_func_type(&[], &[]);
        let (func_index, _) = module.add_import_func(
            API_VERSION_IC0.to_string(),
            "msg_reply".to_string(),
            type_id,
        );
        reply_idx = Some(func_index);
    }

    let perf_counter_idx = if instrument_instruction_count {
        let existing = module.imports.get_func(
            API_VERSION_IC0.to_string(),
            "performance_counter".to_string(),
        );
        if let Some(idx) = existing {
            Some(idx)
        } else {
            let type_id = module
                .types
                .add_func_type(&[DataType::I32], &[DataType::I64]);
            let (func_index, _) = module.add_import_func(
                API_VERSION_IC0.to_string(),
                "performance_counter".to_string(),
                type_id,
            );
            Some(func_index)
        }
    } else {
        None
    };

    Ok((
        data_append_idx.unwrap(),
        reply_idx.unwrap(),
        perf_counter_idx,
    ))
}

/// Checks if the module is using 64-bit memory addressing for the purpose of instrumentation.
///
/// This function determines whether to use 64-bit or 32-bit instructions for memory operations
/// and global variables injected during instrumentation.
///
/// The logic is as follows:
/// 1. If the module's memory (index 0) is 32-bit, it returns `false`.
/// 2. If the memory is 64-bit:
///    - If `ic0.msg_reply_data_append` is NOT imported, it assumes 64-bit usage and returns `true`.
///    - If `ic0.msg_reply_data_append` IS imported, it checks the function signature.
///      - If the parameters are `i64`, it returns `true`.
///      - Otherwise (e.g., `i32`), it returns `false`.
fn is_memory64(module: &Module<'_>) -> bool {
    let memory_64 = module
        .memories
        .get_mem_by_id(MemoryID(0))
        .map(|m| m.ty.memory64)
        .unwrap_or(false);

    // The module is wasm32, no further checks needed
    if !memory_64 {
        return false;
    }

    // The module is guaranteed to be wasm64
    let data_append_idx = module.imports.get_func(
        API_VERSION_IC0.to_string(),
        "msg_reply_data_append".to_string(),
    );

    // If the systemAPI is not imported, we can safely choose i64 for wasm64
    if data_append_idx.is_none() {
        return true;
    }

    // If the systemAPI is imported, we follow the imported DataType for compatability
    // See: https://legacy.internetcomputer.org/docs/references/ic-interface-spec#responding
    let type_id = module
        .functions
        .get_fn_by_id(data_append_idx.unwrap())
        .unwrap()
        .get_type_id();
    let ty = module.types.get(type_id).unwrap();
    ty.params().iter().all(|v| matches!(v, DataType::I64))
}

/// Validates the instrumented Wasm module.
///
/// Uses `wasmparser::Validator` to ensure that the transformations have resulted in a valid Wasm module.
fn validate_wasm(wasm_bytes: &[u8]) -> Result<()> {
    let mut validator = Validator::new();
    validator.validate_all(wasm_bytes)?;
    println!("Validation of instrumented Wasm successful.");
    Ok(())
}

#[cfg(test)]
mod tests {
    use wirm::{ir::module::module_globals::GlobalKind, wasmparser::ValType};

    use super::*;
    #[test]
    fn inject_globals_empty_module() {
        let wat = wat::parse_str(
            r#"
                (module)
            "#,
        )
        .unwrap();

        let history_range: [usize; 4] = [1, 2, 4, 8];
        for history_size in history_range {
            let mut module = Module::parse(&wat, false, false).unwrap();
            let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
                inject_globals(&mut module, history_size, false, false);

            assert_eq!(afl_prev_loc_indices.len(), history_size);
            (0..history_size)
                .for_each(|index| assert_eq!(afl_prev_loc_indices[index], GlobalID(index as u32)));
            assert_eq!(afl_mem_ptr_idx, GlobalID(history_size as u32));
        }
    }

    #[test]
    fn inject_globals_two_globals() {
        let wat = wat::parse_str(
            r#"
                (module
                    (global (;0;) (mut i32) i32.const 0)
                    (global (;1;) (mut i32) i32.const 0)
                )
            "#,
        )
        .unwrap();

        let offset: u32 = 2;

        let history_range: [usize; 4] = [1, 2, 4, 8];
        for history_size in history_range {
            let mut module = Module::parse(&wat, false, false).unwrap();
            let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
                inject_globals(&mut module, history_size, false, false);

            assert_eq!(afl_prev_loc_indices.len(), history_size);
            (0..history_size).for_each(|index| {
                assert_eq!(afl_prev_loc_indices[index], GlobalID(index as u32 + offset))
            });
            assert_eq!(afl_mem_ptr_idx, GlobalID(history_size as u32 + offset));
        }
    }

    #[test]
    fn inject_globals_check_global_values() {
        let wat = wat::parse_str(
            r#"
                (module
                    (global (;0;) (mut i32) i32.const 0)
                    (global (;1;) (mut i32) i32.const 0)
                )
            "#,
        )
        .unwrap();

        let validate_global = |global: GlobalKind, mutable: bool| {
            assert_matches::assert_matches!(global, GlobalKind::Local(_));
            if let GlobalKind::Local(local) = global {
                assert_eq!(local.ty.content_type, ValType::I32);
                assert_eq!(local.ty.mutable, mutable);
                assert!(!local.ty.shared);
                assert_eq!(local.init_expr.instructions().len(), 1);
                assert_matches::assert_matches!(
                    local.init_expr.instructions()[0],
                    InitInstr::Value(Value::I32(0))
                );
            }
        };

        let history_range: [usize; 4] = [1, 2, 4, 8];
        for history_size in history_range {
            let mut module = Module::parse(&wat, false, false).unwrap();
            let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
                inject_globals(&mut module, history_size, false, false);

            for g in afl_prev_loc_indices.iter() {
                let global = module.globals.get_kind(*g);
                validate_global(global.clone(), true);
            }
            let global = module.globals.get_kind(afl_mem_ptr_idx);
            validate_global(global.clone(), false);
        }
    }

    #[test]
    fn inject_ic0_imports_empty_module() {
        let wat = wat::parse_str(
            r#"
                (module)
            "#,
        )
        .unwrap();

        let mut module = Module::parse(&wat, false, false).unwrap();
        let (data_append_idx, reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();

        assert_eq!(data_append_idx, FunctionID(0));
        assert_eq!(reply_idx, FunctionID(1));
    }

    #[test]
    fn inject_ic0_imports_one_import() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i32)))
                    (import "ic0" "dummy" (func (;0;) (type 0)))
                )
            "#,
        )
        .unwrap();

        let mut module = Module::parse(&wat, false, false).unwrap();
        let (data_append_idx, reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();

        assert_eq!(data_append_idx, FunctionID(1));
        assert_eq!(reply_idx, FunctionID(2));
    }

    #[test]
    fn inject_ic0_imports_data_append_exists() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i32 i32)))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 0)))
                )
            "#,
        )
        .unwrap();

        let mut module = Module::parse(&wat, false, false).unwrap();
        let (data_append_idx, reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();

        assert_eq!(data_append_idx, FunctionID(0));
        assert_eq!(reply_idx, FunctionID(1));
    }

    #[test]
    fn inject_ic0_imports_reply_exists() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func))
                    (import "ic0" "msg_reply" (func (;0;) (type 0)))
                )
            "#,
        )
        .unwrap();

        let mut module = Module::parse(&wat, false, false).unwrap();
        let (data_append_idx, reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();

        assert_eq!(data_append_idx, FunctionID(1));
        assert_eq!(reply_idx, FunctionID(0));
    }

    #[test]
    fn inject_ic0_imports_both_exists() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i32 i32)))
                    (type (;1;) (func))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 0)))
                    (import "ic0" "msg_reply" (func (;1;) (type 1)))
                )
            "#,
        )
        .unwrap();

        let mut module = Module::parse(&wat, false, false).unwrap();
        let (data_append_idx, reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();

        assert_eq!(data_append_idx, FunctionID(0));
        assert_eq!(reply_idx, FunctionID(1));
    }

    /// Helper function to test equality between two generated Wasm modules
    /// If they are not equal, it displays the textual difference of the WAT.
    fn wasm_equality(generated: Vec<u8>, expected: Vec<u8>) {
        // both are encoded slices
        if generated != expected {
            let generated_text = wasmprinter::print_bytes(&generated).unwrap();
            let expected_text = wasmprinter::print_bytes(&expected).unwrap();
            // It's nice to show textual diffs
            difference::assert_diff!(generated_text.as_str(), expected_text.as_str(), "\n", 0)
        }
    }

    #[test]
    fn inject_instrumentation_function_history_1() {
        let wat = wat::parse_str(
            r#"
                (module
                    (memory (;0;) 1)
                )
            "#,
        )
        .unwrap();

        let history_size = 1;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
            inject_globals(&mut module, history_size, false, false);
        let instrumentation_function = afl_instrumentation_slice(
            &mut module,
            &afl_prev_loc_indices,
            afl_mem_ptr_idx,
            false,
            None,
        );
        assert_eq!(instrumentation_function, FunctionID(0));
        let expected_wasm = wat::parse_str(
            r#"(module
                            (type (;0;) (func (param i32)))
                            (memory (;0;) 1)
                            (global (;0;) (mut i32) i32.const 0)
                            (global (;1;) i32 i32.const 0)
                            (func (;0;) (type 0) (param i32)
                                (local i32)
                                local.get 0
                                global.get 0
                                i32.xor
                                global.get 1
                                i32.add
                                local.tee 1
                                local.get 1
                                i32.load8_u
                                i32.const 1
                                i32.add
                                i32.store8
                                local.get 0
                                i32.const 1
                                i32.shr_u
                                global.set 0
                            )
                        )"#,
        )
        .unwrap();
        let mut expected_module = Module::parse(&expected_wasm, false, false).unwrap();
        wasm_equality(module.encode(), expected_module.encode());
    }

    #[test]
    fn inject_instrumentation_function_history_2() {
        let wat = wat::parse_str(
            r#"
                (module
                    (memory (;0;) 1)
                )
            "#,
        )
        .unwrap();

        let history_size = 2;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
            inject_globals(&mut module, history_size, false, false);
        let instrumentation_function = afl_instrumentation_slice(
            &mut module,
            &afl_prev_loc_indices,
            afl_mem_ptr_idx,
            false,
            None,
        );
        assert_eq!(instrumentation_function, FunctionID(0));
        let expected_wasm = wat::parse_str(
            r#"(module
                            (type (;0;) (func (param i32)))
                            (memory (;0;) 1)
                            (global (;0;) (mut i32) i32.const 0)
                            (global (;1;) (mut i32) i32.const 0)
                            (global (;2;) i32 i32.const 0)
                            (func (;0;) (type 0) (param i32)
                                (local i32)
                                local.get 0
                                global.get 0
                                i32.xor
                                global.get 1
                                i32.xor
                                global.get 2
                                i32.add
                                local.tee 1
                                local.get 1
                                i32.load8_u
                                i32.const 1
                                i32.add
                                i32.store8
                                global.get 0
                                i32.const 1
                                i32.shr_u
                                global.set 1
                                local.get 0
                                i32.const 1
                                i32.shr_u
                                global.set 0
                            )
                        )"#,
        )
        .unwrap();
        let mut expected_module = Module::parse(&expected_wasm, false, false).unwrap();
        wasm_equality(module.encode(), expected_module.encode());
    }

    #[test]
    fn inject_afl_coverage_export_history_1() {
        let wat = wat::parse_str(
            r#"
                (module
                    (memory (;0;) 1)
                )
            "#,
        )
        .unwrap();

        let history_size = 1;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (msg_reply_data_append_idx, msg_reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();
        let (_, afl_mem_ptr_idx, _) = inject_globals(&mut module, history_size, false, false);
        let coverage_function = inject_afl_coverage_export(
            &mut module,
            history_size,
            afl_mem_ptr_idx,
            msg_reply_data_append_idx,
            msg_reply_idx,
            false,
        );
        assert!(coverage_function.is_ok());
        let expected_wasm = wat::parse_str(
            r#"(module
                    (type (;0;) (func (param i32 i32)))
                    (type (;1;) (func))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 0)))
                    (import "ic0" "msg_reply" (func (;1;) (type 1)))
                    (memory (;0;) 1)
                    (global (;0;) (mut i32) i32.const 0)
                    (global (;1;) i32 i32.const 0)
                    (export "canister_update __export_coverage_for_afl" (func 2))
                    (func (;2;) (type 1)
                        global.get 1
                        i32.const 65536
                        call 0
                        call 1
                        global.get 1
                        i32.const 0
                        i32.const 65536
                        memory.fill
                    )
                )"#,
        )
        .unwrap();
        let mut expected_module = Module::parse(&expected_wasm, false, false).unwrap();
        wasm_equality(module.encode(), expected_module.encode());
    }

    #[test]
    fn inject_afl_coverage_export_history_2() {
        let wat = wat::parse_str(
            r#"
                (module
                    (memory (;0;) 1)
                )
            "#,
        )
        .unwrap();

        let history_size = 2;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (msg_reply_data_append_idx, msg_reply_idx, _) =
            ensure_ic0_imports(&mut module, false, false).unwrap();
        let (_, afl_mem_ptr_idx, _) = inject_globals(&mut module, history_size, false, false);
        let coverage_function = inject_afl_coverage_export(
            &mut module,
            history_size,
            afl_mem_ptr_idx,
            msg_reply_data_append_idx,
            msg_reply_idx,
            false,
        );
        assert!(coverage_function.is_ok());
        let expected_wasm = wat::parse_str(
            r#"(module
                    (type (;0;) (func (param i32 i32)))
                    (type (;1;) (func))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 0)))
                    (import "ic0" "msg_reply" (func (;1;) (type 1)))
                    (memory (;0;) 1)
                    (global (;0;) (mut i32) i32.const 0)
                    (global (;1;) (mut i32) i32.const 0)
                    (global (;2;) i32 i32.const 0)
                    (export "canister_update __export_coverage_for_afl" (func 2))
                    (func (;2;) (type 1)
                        global.get 2
                        i32.const 131072
                        call 0
                        call 1
                        global.get 2
                        i32.const 0
                        i32.const 131072
                        memory.fill
                    )
                )"#,
        )
        .unwrap();
        let mut expected_module = Module::parse(&expected_wasm, false, false).unwrap();
        wasm_equality(module.encode(), expected_module.encode());
    }

    /// Helper function to test branching instrumentation.
    fn instrument_branches_helper(module: &str, expected: &[Operator]) {
        let wat = wat::parse_str(module).unwrap();

        let history_size = 2;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
            inject_globals(&mut module, history_size, false, false);
        instrument_branches(
            &mut module,
            &afl_prev_loc_indices,
            afl_mem_ptr_idx,
            Seed::Static(42),
            false,
            &HashSet::new(),
            None,
        );

        let instructions = module
            .functions
            .get_fn_by_id(FunctionID(0))
            .unwrap()
            .unwrap_local()
            .body
            .instructions
            .get_ops();

        assert_eq!(instructions, expected);
    }

    #[test]
    fn inject_branch_instrumentation_func() {
        instrument_branches_helper(
            r#"
                (module
                    (type (;0;) (func))
                    (memory (;0;) 1)
                    (func (;0;) (type 0))
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_block() {
        instrument_branches_helper(
            r#"
                (module
                    (memory (;0;) 1)
                    (func
                        block
                        nop
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::Block {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::Nop,
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_loop() {
        instrument_branches_helper(
            r#"
                (module
                    (memory (;0;) 1)
                    (func
                        loop
                        nop
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::Loop {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::Nop,
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_if() {
        instrument_branches_helper(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        i32.const 0  ;; Condition
                        if
                        nop
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::I32Const { value: 0 },
                Operator::If {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::Nop,
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_if_else() {
        instrument_branches_helper(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        i32.const 0
                        if
                        nop
                        else
                        nop
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::I32Const { value: 0 },
                Operator::If {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::Nop,
                Operator::Else,
                Operator::I32Const { value: 32602 },
                Operator::Call { function_index: 1 },
                Operator::Nop,
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_br() {
        instrument_branches_helper(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        block
                        br 0
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::Block {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::I32Const { value: 32602 },
                Operator::Call { function_index: 1 },
                Operator::Br { relative_depth: 0 },
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_brif() {
        instrument_branches_helper(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        block
                        i32.const 0
                        br_if 0
                        end
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::Block {
                    blockty: wirm::wasmparser::BlockType::Empty,
                },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::I32Const { value: 0 },
                Operator::I32Const { value: 32602 },
                Operator::Call { function_index: 1 },
                Operator::BrIf { relative_depth: 0 },
                Operator::End,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_return() {
        instrument_branches_helper(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        return
                    )
                )
            "#,
            &[
                Operator::I32Const { value: 17486 },
                Operator::Call { function_index: 1 },
                Operator::I32Const { value: 69016 },
                Operator::Call { function_index: 1 },
                Operator::Return,
                Operator::End,
            ],
        );
    }

    #[test]
    fn inject_branch_instrumentation_brtable() {
        let wat = wat::parse_str(
            r#"
               (module
                    (memory (;0;) 1)
                    (func
                        block
                        i32.const 0
                        br_table 0 0
                        end
                    )
                )
            "#,
        )
        .unwrap();

        let history_size = 2;
        let mut module = Module::parse(&wat, false, false).unwrap();
        let (afl_prev_loc_indices, afl_mem_ptr_idx, _) =
            inject_globals(&mut module, history_size, false, false);
        instrument_branches(
            &mut module,
            &afl_prev_loc_indices,
            afl_mem_ptr_idx,
            Seed::Static(42),
            false,
            &HashSet::new(),
            None,
        );

        let instructions = module
            .functions
            .get_fn_by_id(FunctionID(0))
            .unwrap()
            .unwrap_local()
            .body
            .instructions
            .get_ops();

        let exptected = vec![
            Operator::I32Const { value: 17486 },
            Operator::Call { function_index: 1 },
            Operator::Block {
                blockty: wirm::wasmparser::BlockType::Empty,
            },
            Operator::I32Const { value: 69016 },
            Operator::Call { function_index: 1 },
            Operator::I32Const { value: 0 },
            Operator::I32Const { value: 32602 },
            Operator::Call { function_index: 1 },
            // BrTable fields are private, so it can't be hardcoded.
            Operator::End,
            Operator::End,
        ];

        assert_eq!(
            instructions
                .iter()
                .filter(|o| !matches!(o, Operator::BrTable { targets: _ }))
                .cloned()
                .collect::<Vec<_>>(),
            exptected
        );
    }

    #[should_panic]
    #[test]
    fn instrumentation_panic_history_size_3() {
        let wat = wat::parse_str(
            r#"
                (module)
            "#,
        )
        .unwrap();

        let history_size: usize = 3;

        let _ = instrument_wasm_for_fuzzing(InstrumentationArgs {
            wasm_bytes: wat,
            history_size,
            seed: Seed::Random,
            instrument_instruction_count: false,
        });
    }

    #[test]
    fn instrumentation_round_trip() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i32)))
                    (memory (;0;) 1)
                    (export "memory" (memory 0))
                    (export "check_even" (func 0))
                    (func (;0;) (type 0) (param $num i32)
                        local.get $num
                        i32.const 2
                        i32.rem_u
                        i32.eqz
                        if ;; label = @1
                        i32.const 0
                        i32.const 1
                        i32.store
                        else
                        i32.const 0
                        i32.const 0
                        i32.store
                        end
                    )
                )
            "#,
        )
        .unwrap();

        let history_size: usize = 2;

        let expected = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i32)))
                    (type (;1;) (func (param i32 i32)))
                    (type (;2;) (func))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 1)))
                    (import "ic0" "msg_reply" (func (;1;) (type 2)))
                    (memory (;0;) 1)
                    (global (;0;) (mut i32) i32.const 0)
                    (global (;1;) (mut i32) i32.const 0)
                    (global (;2;) i32 i32.const 0)
                    (export "memory" (memory 0))
                    (export "check_even" (func 2))
                    (export "canister_update __export_coverage_for_afl" (func 3))
                    (func (;2;) (type 0) (param i32)
                        i32.const 17486
                        call 4
                        local.get 0
                        i32.const 2
                        i32.rem_u
                        i32.eqz
                        if ;; label = @1
                        i32.const 69016
                        call 4
                        i32.const 0
                        i32.const 1
                        i32.store
                        else
                        i32.const 32602
                        call 4
                        i32.const 0
                        i32.const 0
                        i32.store
                        end
                    )
                    (func (;3;) (type 2)
                        i32.const 71136
                        call 4
                        global.get 2
                        i32.const 131072
                        call 0
                        call 1
                        global.get 2
                        i32.const 0
                        i32.const 131072
                        memory.fill
                    )
                    (func (;4;) (type 0) (param i32)
                        (local i32)
                        local.get 0
                        global.get 0
                        i32.xor
                        global.get 1
                        i32.xor
                        global.get 2
                        i32.add
                        local.tee 1
                        local.get 1
                        i32.load8_u
                        i32.const 1
                        i32.add
                        i32.store8
                        global.get 0
                        i32.const 1
                        i32.shr_u
                        global.set 1
                        local.get 0
                        i32.const 1
                        i32.shr_u
                        global.set 0
                    )
                    )
            "#,
        )
        .unwrap();

        let generated = instrument_wasm_for_fuzzing(InstrumentationArgs {
            wasm_bytes: wat,
            history_size,
            seed: Seed::Static(42),
            instrument_instruction_count: false,
        });

        wasm_equality(generated, expected);
    }

    #[test]
    fn instrumentation_round_trip_wasm64() {
        let wat = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i64)))
                    (memory (;0;) i64 1)
                    (export "memory" (memory 0))
                    (export "check_even" (func 0))
                    (func (;0;) (type 0) (param $num i64)
                        local.get $num
                        i64.const 2
                        i64.rem_u
                        i64.eqz
                        if ;; label = @1
                        i64.const 0
                        i64.const 1
                        i64.store
                        else
                        i64.const 0
                        i64.const 0
                        i64.store
                        end
                    )
                )
            "#,
        )
        .unwrap();

        let history_size: usize = 2;

        let expected = wat::parse_str(
            r#"
                (module
                    (type (;0;) (func (param i64)))
                    (type (;1;) (func (param i64 i64)))
                    (type (;2;) (func))
                    (import "ic0" "msg_reply_data_append" (func (;0;) (type 1)))
                    (import "ic0" "msg_reply" (func (;1;) (type 2)))
                    (memory (;0;) i64 1)
                    (global (;0;) (mut i64) i64.const 0)
                    (global (;1;) (mut i64) i64.const 0)
                    (global (;2;) i64 i64.const 0)
                    (export "memory" (memory 0))
                    (export "check_even" (func 2))
                    (export "canister_update __export_coverage_for_afl" (func 3))
                    (func (;2;) (type 0) (param i64)
                        i64.const 17486
                        call 4
                        local.get 0
                        i64.const 2
                        i64.rem_u
                        i64.eqz
                        if ;; label = @1
                        i64.const 69016
                        call 4
                        i64.const 0
                        i64.const 1
                        i64.store
                        else
                        i64.const 32602
                        call 4
                        i64.const 0
                        i64.const 0
                        i64.store
                        end
                    )
                    (func (;3;) (type 2)
                        i64.const 71136
                        call 4
                        global.get 2
                        i64.const 131072
                        call 0
                        call 1
                        global.get 2
                        i32.const 0
                        i64.const 131072
                        memory.fill
                    )
                    (func (;4;) (type 0) (param i64)
                        (local i64)
                        local.get 0
                        global.get 0
                        i64.xor
                        global.get 1
                        i64.xor
                        global.get 2
                        i64.add
                        local.tee 1
                        local.get 1
                        i64.load8_u
                        i64.const 1
                        i64.add
                        i64.store8
                        global.get 0
                        i64.const 1
                        i64.shr_u
                        global.set 1
                        local.get 0
                        i64.const 1
                        i64.shr_u
                        global.set 0
                    )
                    )
            "#,
        )
        .unwrap();

        let generated = instrument_wasm_for_fuzzing(InstrumentationArgs {
            wasm_bytes: wat,
            history_size,
            seed: Seed::Static(42),
            instrument_instruction_count: false,
        });

        wasm_equality(generated, expected);
    }

    #[test]
    fn test_is_memory64_wasm32() {
        let wat = wat::parse_str(r#"(module (memory 1))"#).unwrap();
        let module = Module::parse(&wat, false, false).unwrap();
        assert!(!is_memory64(&module));
    }

    #[test]
    fn test_is_memory64_wasm64_no_import() {
        let wat = wat::parse_str(r#"(module (memory i64 1))"#).unwrap();
        let module = Module::parse(&wat, false, false).unwrap();
        assert!(is_memory64(&module));
    }

    #[test]
    fn test_is_memory64_wasm64_import_i64() {
        let wat = wat::parse_str(
            r#"
            (module
                (import "ic0" "msg_reply_data_append" (func (param i64 i64)))
                (memory i64 1)
            )
            "#,
        )
        .unwrap();
        let module = Module::parse(&wat, false, false).unwrap();
        assert!(is_memory64(&module));
    }

    #[test]
    fn test_is_memory64_wasm64_import_i32() {
        let wat = wat::parse_str(
            r#"
            (module
                (import "ic0" "msg_reply_data_append" (func (param i32 i32)))
                (memory i64 1)
            )
            "#,
        )
        .unwrap();
        let module = Module::parse(&wat, false, false).unwrap();
        assert!(!is_memory64(&module));
    }

    #[test]
    fn instruction_count_instrumentation_wasm32() {
        // A module with a canister_update export — should get a wrapper and instruction count export
        let wat = wat::parse_str(
            r#"
            (module
                (type (;0;) (func))
                (import "ic0" "msg_reply" (func (;0;) (type 0)))
                (memory (;0;) 1)
                (export "memory" (memory 0))
                (export "canister_update my_method" (func 1))
                (func (;1;) (type 0)
                    call 0
                )
            )
            "#,
        )
        .unwrap();

        let generated = instrument_wasm_for_fuzzing(InstrumentationArgs {
            wasm_bytes: wat,
            history_size: 1,
            seed: Seed::Static(42),
            instrument_instruction_count: true,
        });

        // Verify the instrumented module is valid
        validate_wasm(&generated).unwrap();

        // Parse the generated module and check for expected exports
        let module = Module::parse(&generated, false, false).unwrap();

        // Should have the original export, coverage export, and instruction count export
        let has_coverage_export = module
            .exports
            .get_by_name(format!("canister_update {COVERAGE_FN_EXPORT_NAME}"))
            .is_some();
        let has_instruction_export = module
            .exports
            .get_by_name(format!("canister_query {INSTRUCTION_COUNT_FN_EXPORT_NAME}"))
            .is_some();
        let has_original_export = module
            .exports
            .get_by_name("canister_update my_method".to_string())
            .is_some();

        assert!(has_coverage_export, "Missing coverage export");
        assert!(has_instruction_export, "Missing instruction count export");
        assert!(has_original_export, "Missing original method export");

        // The original export should now point to a different function (the wrapper)
        let original_func_id = module
            .exports
            .get_func_by_name("canister_update my_method".to_string())
            .unwrap();
        // Original function was func 1 (after import at 0), wrapper should be a new function
        assert_ne!(
            original_func_id,
            FunctionID(1),
            "Export should point to wrapper, not original function"
        );

        // Check that performance_counter import exists
        let perf_counter = module
            .imports
            .get_func("ic0".to_string(), "performance_counter".to_string());
        assert!(perf_counter.is_some(), "Missing performance_counter import");
    }

    #[test]
    fn instruction_count_no_canister_exports() {
        // A module with no canister_update/query exports — should still work but no wrappers
        let wat = wat::parse_str(
            r#"
            (module
                (memory (;0;) 1)
                (export "memory" (memory 0))
                (func (;0;)
                    nop
                )
            )
            "#,
        )
        .unwrap();

        let generated = instrument_wasm_for_fuzzing(InstrumentationArgs {
            wasm_bytes: wat,
            history_size: 1,
            seed: Seed::Static(42),
            instrument_instruction_count: true,
        });

        validate_wasm(&generated).unwrap();

        let module = Module::parse(&generated, false, false).unwrap();

        // Should still have instruction count export even with no canister methods
        let has_instruction_export = module
            .exports
            .get_by_name(format!("canister_query {INSTRUCTION_COUNT_FN_EXPORT_NAME}"))
            .is_some();
        assert!(has_instruction_export, "Missing instruction count export");
    }

    #[test]
    fn compute_cost_per_afl_call_values() {
        // history_size=1: 23 + 6*1 = 29
        assert_eq!(compute_cost_per_afl_call(1), 29);
        // history_size=2: 23 + 6*2 = 35
        assert_eq!(compute_cost_per_afl_call(2), 35);
        // history_size=4: 23 + 6*4 = 47
        assert_eq!(compute_cost_per_afl_call(4), 47);
        // history_size=8: 23 + 6*8 = 71
        assert_eq!(compute_cost_per_afl_call(8), 71);
    }
}