yara-x 1.15.0

A pure Rust implementation of YARA.
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
use std::cell::RefCell;
use std::collections::VecDeque;
#[cfg(feature = "rules-profiling")]
use std::iter;
use std::mem::{MaybeUninit, transmute};
#[cfg(feature = "rules-profiling")]
use std::ops::AddAssign;
use std::ops::{Deref, Range};
use std::pin::Pin;
use std::ptr::NonNull;
use std::rc::Rc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::{cmp, mem, thread};

use base64::Engine;
use bitvec::order::Lsb0;
use bitvec::slice::BitSlice;
use bstr::{BString, ByteSlice};
use indexmap::IndexMap;
use protobuf::{MessageDyn, MessageFull};
use regex_automata::meta::Regex;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::compiler::{
    NamespaceId, PatternId, RegexpId, RuleId, Rules, SubPattern,
    SubPatternAtom, SubPatternFlags, SubPatternId,
};
use crate::errors::VariableError;
use crate::re::Action;
use crate::re::fast::FastVM;
use crate::re::hir::ChainedPatternGap;
use crate::re::thompson::PikeVM;
#[cfg(feature = "rules-profiling")]
use crate::scanner::ProfilingData;
use crate::scanner::matches::{Match, PatternMatches, UnconfirmedMatch};
use crate::scanner::{DataSnippets, ScanError, ScannedData};
use crate::scanner::{HEARTBEAT_COUNTER, INIT_HEARTBEAT};
use crate::types::{Array, Map, Struct, TypeValue};
use crate::wasm::MATCHING_RULES_BITMAP_BASE;
use crate::wasm::runtime::{
    AsContext, AsContextMut, Global, GlobalType, Instance, MemoryType,
    Mutability, Store, TypedFunc, Val, ValType,
};
use crate::{Variable, wasm};

/// Represents the states in which a scanner can be.
pub(crate) enum ScanState<'a> {
    Idle,
    Timeout,
    ScanningData(ScannedData<'a>),
    ScanningBlock((usize, &'a [u8])),
    Finished(DataSnippets<'a>),
}

impl<'a> ScanState<'a> {
    /// Returns changes the current state to [`ScanState::Idle`] and returns
    /// the previous state.
    pub fn take(&mut self) -> ScanState<'a> {
        mem::replace(self, Self::Idle)
    }
}

/// Structure that holds information about the current scan.
pub(crate) struct ScanContext<'r, 'd> {
    /// Pointer to the WASM store.
    pub wasm_store: NonNull<Store<ScanContext<'static, 'static>>>,
    /// The WASM module.
    pub wasm_module: MaybeUninit<Instance>,
    /// Main function in the WASM module. This is the entrypoint from where
    /// the execution of rule conditions starts.
    pub wasm_main_func: Option<TypedFunc<(), i32>>,
    /// Module's main memory.
    pub wasm_main_memory: Option<wasm::runtime::Memory>,
    /// WASM global variable that contains the value of `filesize`.
    pub wasm_filesize: Option<Global>,
    /// WASM global variable that contains a boolean that indicates if
    /// pattern search was done.
    pub wasm_pattern_search_done: Option<Global>,
    /// Map where keys are object handles and values are objects used during
    /// the evaluation of rule conditions. Handles are opaque integer values
    /// that can be passed to and received from WASM code. Each handle identify
    /// an object (string, struct, array or map).
    pub runtime_objects: IndexMap<RuntimeObjectHandle, RuntimeObject>,
    /// The time that can be spent in a scan operation, including the
    /// execution of the rule conditions.
    pub scan_timeout: Option<Duration>,
    /// The current state of the scanner.
    pub scan_state: ScanState<'d>,
    /// Vector containing the IDs of the rules that matched, including both
    /// global and non-global ones. The rules are added first to the
    /// `matching_rules_per_ns` map, and then moved to this vector
    /// once the scan finishes.
    pub matching_rules: Vec<RuleId>,
    /// Map containing the IDs of rules that matched. Using an `IndexMap`
    /// because we want to keep the insertion order, so that rules in
    /// namespaces that were declared first, appear first in scan results.
    pub matching_rules_per_ns: IndexMap<NamespaceId, Vec<RuleId>>,
    /// Number of private rules that have matched. This will be equal to or
    /// less than the length of `matching_rules`.
    pub num_matching_private_rules: usize,
    /// Number of private rules that did not match.
    pub num_non_matching_private_rules: usize,
    /// Compiled rules for this scan.
    pub compiled_rules: &'r Rules,
    /// Structure that contains top-level symbols, like module names
    /// and external variables. Symbols are normally looked up in this
    /// structure, except if `current_struct` is set to some other
    /// structure that overrides `root_struct`.
    pub root_struct: Struct,
    /// Currently active structure that overrides the `root_struct` if
    /// set.
    pub current_struct: Option<Rc<Struct>>,
    /// Hash map that contains the protobuf messages returned by YARA modules.
    /// Keys are the fully qualified protobuf message name, and values are
    /// the message returned by the main function of the corresponding module.
    pub module_outputs: FxHashMap<String, Box<dyn MessageDyn>>,
    /// Hash map that contains the protobuf messages that has been explicitly
    /// provided by the user to be used as module outputs during the next scan
    /// operation. Keys are the fully qualified protobuf message names, and
    /// values are the protobuf messages set with [`Scanner::set_module_output`].
    pub user_provided_module_outputs: FxHashMap<String, Box<dyn MessageDyn>>,
    /// Hash map that tracks the matches occurred during a scan. The keys
    /// are the PatternId of the matching pattern, and values are a list
    /// of matches.
    pub pattern_matches: PatternMatches,
    /// Hash map that tracks the unconfirmed matches for chained patterns. When
    /// a pattern is split into multiple chained pieces, each piece is handled
    /// as an individual pattern, but the match of one of the pieces doesn't
    /// imply that the whole pattern matches. This partial matches are stored
    /// here until they can be confirmed or discarded. There's no guarantee
    /// that matches stored in `Vec<UnconfirmedMatch>` are sorted by matching
    /// offset.
    pub unconfirmed_matches: FxHashMap<SubPatternId, Vec<UnconfirmedMatch>>,
    /// Set that contains the PatternId for those patterns that have reached
    /// the maximum number of matches indicated by `max_matches_per_pattern`.
    pub limit_reached: FxHashSet<PatternId>,
    /// When [`HEARTBEAT_COUNTER`] is larger than this value, the scan is
    /// aborted due to a timeout.
    pub deadline: u64,
    /// Hash map that serves as a cache for regexps used in expressions like
    /// `some_var matches /foobar/`. Compiling a regexp is a expensive
    /// operation. Instead of compiling the regexp each time the expression
    /// is evaluated, it is compiled the first time and stored in this hash
    /// map.
    pub regexp_cache: RefCell<FxHashMap<RegexpId, Regex>>,
    /// Callback invoked every time a YARA rule calls `console.log`.
    pub console_log: Option<Box<dyn FnMut(String) + 'r>>,
    /// Hash map that tracks the time spend on each pattern. Keys are pattern
    /// PatternIds and values are the cumulative time spent on verifying each
    /// pattern.
    #[cfg(feature = "rules-profiling")]
    pub time_spent_in_pattern: FxHashMap<PatternId, u64>,
    /// Time spent evaluating each rule. This vector has one entry per rule,
    /// which is the number of nanoseconds spent evaluating the rule.
    #[cfg(feature = "rules-profiling")]
    pub time_spent_in_rule: Vec<u64>,
    /// The time at which the evaluation of the current rule started.
    #[cfg(feature = "rules-profiling")]
    pub rule_execution_start_time: u64,
    /// The ID of the last rule whose condition was executed.
    #[cfg(feature = "rules-profiling")]
    pub last_executed_rule: Option<RuleId>,
    /// Clock used for measuring the time spend on each pattern.
    #[cfg(any(feature = "rules-profiling", feature = "logging"))]
    pub clock: quanta::Clock,
}

#[cfg(feature = "rules-profiling")]
impl ScanContext<'_, '_> {
    /// Returns the slowest N rules.
    ///
    /// Profiling has an accumulative effect. When the scanner is used for
    /// scanning multiple files the times add up.
    pub fn slowest_rules(&self, n: usize) -> Vec<ProfilingData<'_>> {
        debug_assert_eq!(
            self.compiled_rules.num_rules(),
            self.time_spent_in_rule.len()
        );

        let mut result = Vec::with_capacity(self.compiled_rules.num_rules());

        for (rule, condition_exec_time) in iter::zip(
            self.compiled_rules.rules().iter(),
            self.time_spent_in_rule.iter(),
        ) {
            let mut pattern_matching_time = 0;
            for p in rule.patterns.iter() {
                if let Some(d) = self.time_spent_in_pattern.get(&p.pattern_id)
                {
                    pattern_matching_time += *d;
                }
            }

            // Don't track rules that took less 100ms.
            if condition_exec_time + pattern_matching_time > 100_000_000 {
                let namespace = self
                    .compiled_rules
                    .ident_pool()
                    .get(rule.namespace_ident_id)
                    .unwrap();

                let rule = self
                    .compiled_rules
                    .ident_pool()
                    .get(rule.ident_id)
                    .unwrap();

                result.push(ProfilingData {
                    namespace,
                    rule,
                    condition_exec_time: Duration::from_nanos(
                        *condition_exec_time,
                    ),
                    pattern_matching_time: Duration::from_nanos(
                        pattern_matching_time,
                    ),
                });
            }
        }

        // Sort the results by the time spent on each rule, in descending
        // order.
        result.sort_by(|a, b| {
            let a_time = a.pattern_matching_time + a.condition_exec_time;
            let b_time = b.pattern_matching_time + b.condition_exec_time;

            b_time.cmp(&a_time)
        });
        result.truncate(n);
        result
    }

    /// Clears profiling information.
    pub fn clear_profiling_data(&mut self) {
        self.time_spent_in_rule.fill(0);
        self.time_spent_in_pattern.clear();
    }
}

impl ScanContext<'_, '_> {
    const DEFAULT_SCAN_TIMEOUT: u64 = 315_360_000;

    /// Returns a slice with the data being scanned.
    ///
    /// Returns `None` if the current scan state is not [`ScanState::ScanningData`].
    /// Particularly, if the state is [`ScanState::ScanningBlock`] the result is
    /// `None`.
    pub(crate) fn scanned_data(&self) -> Option<&[u8]> {
        match &self.scan_state {
            ScanState::ScanningData(data) => Some(data.as_ref()),
            _ => None,
        }
    }

    #[inline]
    pub(crate) fn wasm_store_mut<'a>(
        &mut self,
    ) -> &'a mut Store<ScanContext<'static, 'static>> {
        unsafe { self.wasm_store.as_mut() }
    }

    /// Returns true of the regexp identified by the given [`RegexpId`]
    /// matches `haystack`.
    pub(crate) fn regexp_matches(
        &self,
        regexp_id: RegexpId,
        haystack: &[u8],
    ) -> bool {
        self.regexp_cache
            .borrow_mut()
            .entry(regexp_id)
            .or_insert_with(|| self.compiled_rules.get_regexp(regexp_id))
            .is_match(haystack)
    }

    /// Returns the protobuf struct produced by a module.
    ///
    /// The main function of a module returns a protobuf message with data
    /// produced by the module for the current scan. Accessing this data
    /// from some other function exported by the module is useful in certain
    /// cases, and that's the purpose of this function.
    ///
    /// This function is generic over `T`, where `T` is some protobuf message
    /// type.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use crate::modules::protos::my_module::MyModuleProto;
    /// let module_data: MyModuleProto = ctx.module_data::<MyModuleProto>()
    /// ```
    pub(crate) fn module_output<T: MessageFull>(&self) -> Option<&T> {
        let m = self.module_outputs.get(T::descriptor().full_name())?.as_ref();
        <dyn MessageDyn>::downcast_ref(m)
    }

    pub(crate) fn console_log(&mut self, message: String) {
        if let Some(console_log) = &mut self.console_log {
            console_log(message)
        }
    }

    /// Sets the value of a global variable.
    pub fn set_global<T: TryInto<Variable>>(
        &mut self,
        ident: &str,
        value: T,
    ) -> Result<&mut Self, VariableError>
    where
        VariableError: From<<T as TryInto<Variable>>::Error>,
    {
        if let Some(field) = self.root_struct.field_by_name_mut(ident) {
            let variable: Variable = value.try_into()?;
            let type_value: TypeValue = variable.into();
            // The new type must match the old one.
            if type_value.eq_type(&field.type_value) {
                field.type_value = type_value;
            } else {
                return Err(VariableError::InvalidType {
                    variable: ident.to_string(),
                    expected_type: field.type_value.ty().to_string(),
                    actual_type: type_value.ty().to_string(),
                });
            }
        } else {
            return Err(VariableError::Undefined(ident.to_string()));
        }

        Ok(self)
    }

    pub(crate) fn store_struct(
        &mut self,
        s: Rc<Struct>,
    ) -> RuntimeObjectHandle {
        let obj_ref = RuntimeObjectHandle(Rc::<Struct>::as_ptr(&s) as i64);
        self.runtime_objects.insert_full(obj_ref, RuntimeObject::Struct(s));
        obj_ref
    }

    pub(crate) fn store_array(&mut self, a: Rc<Array>) -> RuntimeObjectHandle {
        let obj_ref = RuntimeObjectHandle(Rc::<Array>::as_ptr(&a) as i64);
        self.runtime_objects.insert_full(obj_ref, RuntimeObject::Array(a));
        obj_ref
    }

    pub(crate) fn store_map(&mut self, m: Rc<Map>) -> RuntimeObjectHandle {
        let obj_ref = RuntimeObjectHandle(Rc::<Map>::as_ptr(&m) as i64);
        self.runtime_objects.insert_full(obj_ref, RuntimeObject::Map(m));
        obj_ref
    }

    pub(crate) fn store_string(
        &mut self,
        s: Rc<BString>,
    ) -> RuntimeObjectHandle {
        let obj_ref = RuntimeObjectHandle(Rc::<BString>::as_ptr(&s) as i64);
        self.runtime_objects.insert_full(obj_ref, RuntimeObject::String(s));
        obj_ref
    }

    /// Gets the value of the global variable `filesize`.
    pub(crate) fn get_filesize(&mut self) -> i64 {
        self.wasm_filesize.unwrap().get(self.wasm_store_mut()).i64().unwrap()
    }

    /// Set the value of the global variable `filesize`.
    pub(crate) fn set_filesize(&mut self, filesize: i64) {
        self.wasm_filesize
            .unwrap()
            .set(self.wasm_store_mut(), Val::I64(filesize))
            .unwrap();
    }

    /// Sets the value of the flag that indicates if the pattern search
    /// phase was already executed.
    pub(crate) fn set_pattern_search_done(&mut self, done: bool) {
        self.wasm_pattern_search_done
            .unwrap()
            .set(self.wasm_store_mut(), Val::I32(done as i32))
            .unwrap();
    }

    /// Sets a timeout for scan operations.
    pub(crate) fn set_timeout(&mut self, timeout: Duration) -> &mut Self {
        self.scan_timeout = Some(timeout);
        self
    }

    /// Invokes the main function, which evaluates the rules' conditions. It
    /// calls ScanContext::search_for_patterns (which does the Aho-Corasick
    /// scanning) only if necessary.
    ///
    /// This will return [ScanError::Timeout], if a timeout occurs while
    /// searching for patterns or evaluating the conditions.
    pub(crate) fn eval_conditions(&mut self) -> Result<(), ScanError> {
        // Save the time in which the evaluation started.
        #[cfg(feature = "rules-profiling")]
        {
            self.rule_execution_start_time = self.clock.raw();
        }
        // Invoke the main function, which evaluates the rules' conditions. It
        // calls ScanContext::search_for_patterns (which does the Aho-Corasick
        // scanning) only if necessary.
        //
        // This will return Err(ScanError::Timeout), when the scan timeout is
        // reached while WASM code is being executed.
        let store = self.wasm_store_mut();
        let eval_result =
            self.wasm_main_func.as_ref().unwrap().call(store, ());

        #[cfg(feature = "rules-profiling")]
        if eval_result.is_err() {
            // If a timeout occurs, the methods `ctx.track_rule_no_match` or
            // `ctx.track_rule_match` may not be invoked for the currently
            // executing rule. This means that the time spent within that rule
            // has not been recorded yet, so we need to update it here.
            //
            // The ID of the rule that was running during the timeout can be
            // determined as the one immediately following the last executed
            // rule, based on the assumption that rules are processed in a
            // strictly ascending ID order.
            //
            // Additionally, if the timeout happens after `ctx.last_executed_rule`
            // has been updated with the last rule ID, we might end up calling
            // `update_time_spent_in_rule` with an ID that is off by one.
            // However, this function is designed to handle such cases
            // gracefully.
            self.update_time_spent_in_rule(
                self.last_executed_rule
                    .map_or(RuleId::from(0), |rule_id| rule_id.next()),
            );
        }

        #[cfg(all(feature = "rules-profiling", feature = "logging"))]
        {
            let most_expensive_rules = self.slowest_rules(10);
            if !most_expensive_rules.is_empty() {
                log::info!("Most expensive rules:");
                for profiling_data in most_expensive_rules {
                    log::info!("+ namespace: {}", profiling_data.namespace);
                    log::info!("  rule: {}", profiling_data.rule);
                    log::info!(
                        "  pattern matching time: {:?}",
                        profiling_data.pattern_matching_time
                    );
                    log::info!(
                        "  condition execution time: {:?}",
                        profiling_data.condition_exec_time
                    );
                }
            }
        }

        // `matching_rules` must be empty at this point. Matching rules were
        // being tracked by the `matching_rules_per_ns` map, but we are about
        // to move them to `matching_rules` while leaving the map empty.
        assert!(self.matching_rules.is_empty());

        // Move the matching rules to the `matching_rules` vector, leaving the
        // `matching_rules_per_ns` map empty.
        for rules in self.matching_rules_per_ns.values_mut() {
            for rule_id in rules.drain(0..) {
                self.matching_rules.push(rule_id);
            }
        }

        // The WASM code that evaluates the conditions returns
        // `ScanError::Timeout` if a timeout occurs during its execution.
        // However, a timeout may also happen while `search_for_patterns`
        // is running. In that case, the function returns `Ok(0)` but the
        // scan state is updated to `ScanState::Timeout`.
        match eval_result {
            Ok(0) => match self.scan_state {
                ScanState::Timeout => Err(ScanError::Timeout),
                _ => Ok(()),
            },
            Ok(v) => panic!("WASM main returned: {v}"),
            Err(err) if err.is::<ScanError>() => {
                Err(err.downcast::<ScanError>().unwrap())
            }
            Err(err) => panic!(
                "unexpected error while executing WASM main function: {err}"
            ),
        }
    }

    /// Resets the scan context to its initial state, making it ready for
    /// another scan.
    ///
    /// This clears all the information generated during the previous scan and
    /// resets the deadline for timeouts.
    pub(crate) fn reset(&mut self) {
        let num_rules = self.compiled_rules.num_rules();
        let num_patterns = self.compiled_rules.num_patterns();

        self.scan_state = ScanState::Idle;

        // Free all runtime objects left around by previous scans.
        self.runtime_objects.clear();

        // Clear the array that tracks the patterns that reached the maximum
        // number of patterns.
        self.limit_reached.clear();

        self.unconfirmed_matches.clear();
        self.num_matching_private_rules = 0;
        self.num_non_matching_private_rules = 0;

        // Clear the value of `current_struct` as it may contain a reference
        // to some struct.
        self.current_struct = None;

        // Clear module outputs from previous scans.
        self.module_outputs.clear();

        // Move the matching rules to the `matching_rules` vector, leaving the
        // `matching_rules_per_ns` map empty.
        for rules in self.matching_rules_per_ns.values_mut() {
            for rule_id in rules.drain(0..) {
                self.matching_rules.push(rule_id);
            }
        }

        // If some pattern or rule matched, clear the matches. Notice that a
        // rule may match without any pattern being matched, because there
        // are rules without patterns, or that match if the pattern is not
        // found.
        if !self.pattern_matches.is_empty() || !self.matching_rules.is_empty()
        {
            self.pattern_matches.clear();
            self.matching_rules.clear();

            let store = self.wasm_store_mut();
            let mem = self.wasm_main_memory.unwrap().data_mut(store);

            // Starting at MATCHING_RULES_BITMAP in main memory there's a
            // bitmap were the N-th bit indicates if the rule with ID = N
            // matched or not, If some rule matched in a previous call the
            // bitmap will contain some bits set to 1 and need to be cleared.
            let base = MATCHING_RULES_BITMAP_BASE as usize;
            let bitmap = BitSlice::<_, Lsb0>::from_slice_mut(
                &mut mem[base..base
                    + num_rules.div_ceil(8)
                    + num_patterns.div_ceil(8)],
            );

            // Set to zero all bits in the bitmap.
            bitmap.fill(false);
        }

        // Timeout in seconds. This is either the value provided by the user or
        // 315.360.000 which is the number of seconds in a year. Using u64::MAX
        // doesn't work because this value is added to the current epoch, and
        // will cause an overflow. We need an integer large enough, but that
        // has room before the u64 limit is reached. For this same reason if
        // the user specifies a value larger than 315.360.000 we limit it to
        // 315.360.000 anyway. One year should be enough, I hope you don't plan
        // to run a YARA scan that takes longer.
        let timeout_secs =
            self.scan_timeout.map_or(Self::DEFAULT_SCAN_TIMEOUT, |t| {
                cmp::min(
                    t.as_secs_f32().ceil() as u64,
                    Self::DEFAULT_SCAN_TIMEOUT,
                )
            });

        self.deadline =
            HEARTBEAT_COUNTER.load(Ordering::Relaxed) + timeout_secs;

        let wasm_store = self.wasm_store_mut();

        // Sets the deadline for the WASM store. The WASM main function
        // will abort if the deadline is reached while the function is being
        // executed.
        wasm_store.set_epoch_deadline(timeout_secs);
        wasm_store.epoch_deadline_callback(|_| Err(ScanError::Timeout.into()));

        // If some timeout was specified, start the heartbeat thread, if
        // not previously started. The heartbeat thread increments the WASM
        // engine epoch and HEARTBEAT_COUNTER every second. There's a single
        // instance of this thread, independently of the number of concurrent
        // scans.
        if self.scan_timeout.is_some() {
            INIT_HEARTBEAT.call_once(|| {
                thread::spawn(|| {
                    loop {
                        thread::sleep(Duration::from_secs(1));
                        wasm::get_engine().increment_epoch();
                        HEARTBEAT_COUNTER
                            .fetch_update(
                                Ordering::SeqCst,
                                Ordering::SeqCst,
                                |x| Some(x + 1),
                            )
                            .unwrap();
                    }
                });
            });
        }
    }

    /// Update the time spent in the rule with the given ID, the time is
    /// increased by the time elapsed since `rule_execution_start_time`.
    #[cfg(feature = "rules-profiling")]
    pub(crate) fn update_time_spent_in_rule(&mut self, rule_id: RuleId) {
        // The RuleId is not guaranteed to be a valid one. It may be larger
        // than the last RuleId, so we can't assume that the `get_mut` will
        // be successful.
        if let Some(time_spend_in_rule) =
            self.time_spent_in_rule.get_mut::<usize>(rule_id.into())
        {
            time_spend_in_rule.add_assign(self.clock.delta_as_nanos(
                self.rule_execution_start_time,
                self.clock.raw(),
            ));
        }
    }

    /// Called during the scan process when a rule didn't match.
    pub(crate) fn track_rule_no_match(&mut self, rule_id: RuleId) {
        #[cfg(feature = "rules-profiling")]
        {
            self.last_executed_rule = Some(rule_id);
            self.update_time_spent_in_rule(rule_id);
        }

        let rule = self.compiled_rules.get(rule_id);

        if rule.is_private {
            self.num_non_matching_private_rules += 1;
        }

        // If the rule is global, all the rules in the same namespace that
        // matched previously must be removed from the `matching_rules_per_ns`
        // map. Also, their corresponding bits in the matching rules bitmap must
        // be cleared, and `num_matching_private_rules` must be decremented if
        // the rule was private and `num_non_matching_private_rules` incremented.
        if rule.is_global
            && let Some(rules) =
                self.matching_rules_per_ns.get_mut(&rule.namespace_id)
        {
            let store = unsafe { self.wasm_store.as_mut() };
            let main_mem = self.wasm_main_memory.unwrap().data_mut(store);

            let base = MATCHING_RULES_BITMAP_BASE as usize;
            let num_rules = self.compiled_rules.num_rules();

            let bits = BitSlice::<u8, Lsb0>::from_slice_mut(
                &mut main_mem[base..base + num_rules.div_ceil(8)],
            );

            for rule_id in rules.drain(0..) {
                if self.compiled_rules.get(rule_id).is_private {
                    self.num_matching_private_rules -= 1;
                    self.num_non_matching_private_rules += 1;
                }
                bits.set(rule_id.into(), false);
            }
        }

        // Save the time in which the evaluation of the next rule started.
        #[cfg(feature = "rules-profiling")]
        {
            self.rule_execution_start_time = self.clock.raw();
        }
    }

    /// Called during the scan process when a rule has matched for tracking
    /// the matching rules.
    pub(crate) fn track_rule_match(&mut self, rule_id: RuleId) {
        #[cfg(feature = "rules-profiling")]
        {
            self.last_executed_rule = Some(rule_id);
            self.update_time_spent_in_rule(rule_id);
        }

        let rule = self.compiled_rules.get(rule_id);

        #[cfg(feature = "logging")]
        log::info!(
            "Rule match: {}:{}  {:?}",
            self.compiled_rules
                .ident_pool()
                .get(rule.namespace_ident_id)
                .unwrap(),
            self.compiled_rules.ident_pool().get(rule.ident_id).unwrap(),
            rule_id,
        );

        self.matching_rules_per_ns
            .entry(rule.namespace_id)
            .or_default()
            .push(rule_id);

        if rule.is_private {
            self.num_matching_private_rules += 1;
        }

        let wasm_store = self.wasm_store_mut();
        let mem = self.wasm_main_memory.unwrap().data_mut(wasm_store);
        let num_rules = self.compiled_rules.num_rules();

        let base = MATCHING_RULES_BITMAP_BASE as usize;
        let bits = BitSlice::<u8, Lsb0>::from_slice_mut(
            &mut mem[base..base + num_rules.div_ceil(8)],
        );

        // The RuleId-th bit in the `rule_matches` bit vector is set to 1.
        bits.set(rule_id.into(), true);

        // Save the time in which the evaluation of the next rule started.
        #[cfg(feature = "rules-profiling")]
        {
            self.rule_execution_start_time = self.clock.raw();
        }
    }

    /// Called during the scan process when a pattern match has been found.
    ///
    /// `pattern_id` is the ID of the matching pattern, `match_` contains
    /// details about the match (range and xor key), and `replace_if_longer`
    /// indicates whether existing matches for the same pattern at the same
    /// offset should be replaced by the current match if the current is
    /// longer.
    pub(crate) fn track_pattern_match(
        &mut self,
        pattern_id: PatternId,
        match_: Match,
        replace_if_longer: bool,
    ) {
        let wasm_store = self.wasm_store_mut();
        let mem = self.wasm_main_memory.unwrap().data_mut(wasm_store);
        let num_rules = self.compiled_rules.num_rules();
        let num_patterns = self.compiled_rules.num_patterns();

        let base = MATCHING_RULES_BITMAP_BASE as usize + num_rules.div_ceil(8);
        let bits = BitSlice::<u8, Lsb0>::from_slice_mut(
            &mut mem[base..base + num_patterns.div_ceil(8)],
        );

        bits.set(pattern_id.into(), true);

        if !self.pattern_matches.add(pattern_id, match_, replace_if_longer) {
            self.limit_reached.insert(pattern_id);
        }
    }

    /// The Aho-Corasick search loop.
    pub(crate) fn ac_search_loop(
        &mut self,
        base: usize,
        data: &[u8],
        block_scanning_mode: bool,
    ) -> Result<(), ScanError> {
        let mut vm = VM {
            pike_vm: PikeVM::new(self.compiled_rules.re_code()),
            fast_vm: FastVM::new(self.compiled_rules.re_code()),
        };

        let ac = self.compiled_rules.ac_automaton();
        let atoms = self.compiled_rules.atoms();
        let filesize = self.get_filesize();

        #[cfg(feature = "logging")]
        let mut atom_matches = 0_usize;

        for ac_match in ac.find_overlapping_iter(data) {
            #[cfg(feature = "logging")]
            {
                atom_matches += 1;
            }

            if HEARTBEAT_COUNTER.load(Ordering::Relaxed) >= self.deadline {
                return Err(ScanError::Timeout);
            }

            let atom =
                unsafe { atoms.get_unchecked(ac_match.pattern().as_usize()) };

            // Subtract the backtrack value from the offset where the atom
            // matched. If the result is negative the atom can't be inside
            // the scanned data and therefore is not a possible match.
            let atom_pos = if let Some(atom_pos) =
                ac_match.start().checked_sub(atom.backtrack())
            {
                atom_pos
            } else {
                continue;
            };

            // Each atom belongs to a sub-pattern.
            let sub_pattern_id = atom.sub_pattern_id();

            // Each sub-pattern belongs to a pattern.
            let (pattern_id, sub_pattern) =
                &self.compiled_rules.get_sub_pattern(sub_pattern_id);

            // Check if the potentially matching pattern has reached the
            // maximum number of allowed matches. In that case continue without
            // verifying the match.
            if self.limit_reached.contains(pattern_id) {
                continue;
            }

            // If there are file size bounds associated to the pattern, but
            // the currently scanned file does not satisfy them, no further
            // confirmation is needed. The rule won't match regardless of
            // whether the pattern matches or not. This is not done in block
            // scanning mode as `filesize` is undefined in that mode.
            if !block_scanning_mode
                && let Some(bounds) =
                    self.compiled_rules.filesize_bounds(*pattern_id)
                && !bounds.contains(filesize)
            {
                continue;
            }

            #[cfg(feature = "rules-profiling")]
            let verification_start = self.clock.raw();

            // If the atom is exact no further verification is needed, except
            // for making sure that the fullword requirements are met. An exact
            // atom is enough to guarantee that the whole sub-pattern matched.
            #[cfg(feature = "exact-atoms")]
            if atom.is_exact() {
                let flags = match sub_pattern {
                    SubPattern::Literal { flags, .. }
                    | SubPattern::LiteralChainHead { flags, .. }
                    | SubPattern::LiteralChainTail { flags, .. }
                    | SubPattern::Regexp { flags, .. }
                    | SubPattern::RegexpChainHead { flags, .. }
                    | SubPattern::RegexpChainTail { flags, .. } => flags,
                    _ => unreachable!(),
                };

                let match_range = atom_pos..atom_pos + atom.len();

                if verify_full_word(data, &match_range, *flags, None) {
                    self.handle_sub_pattern_match(
                        sub_pattern_id,
                        sub_pattern,
                        *pattern_id,
                        Match::new(match_range).rebase(base),
                    );
                }

                continue;
            }

            match sub_pattern {
                SubPattern::Literal { pattern, flags, .. }
                | SubPattern::LiteralChainHead { pattern, flags, .. }
                | SubPattern::LiteralChainTail { pattern, flags, .. } => {
                    let pattern = self
                        .compiled_rules
                        .lit_pool()
                        .get_bytes(*pattern)
                        .unwrap();

                    if verify_literal_match(pattern, data, atom_pos, *flags) {
                        self.handle_sub_pattern_match(
                            sub_pattern_id,
                            sub_pattern,
                            *pattern_id,
                            Match::new(atom_pos..atom_pos + pattern.len())
                                .rebase(base),
                        );
                    }
                }
                SubPattern::Regexp { flags, .. }
                | SubPattern::RegexpChainHead { flags, .. }
                | SubPattern::RegexpChainTail { flags, .. } => {
                    verify_regexp_match(
                        &mut vm,
                        data,
                        atom_pos,
                        atom,
                        *flags,
                        |match_range| {
                            self.handle_sub_pattern_match(
                                sub_pattern_id,
                                sub_pattern,
                                *pattern_id,
                                Match::new(match_range).rebase(base),
                            );
                        },
                    )
                }

                SubPattern::Xor { pattern, flags } => {
                    let pattern = self
                        .compiled_rules
                        .lit_pool()
                        .get_bytes(*pattern)
                        .unwrap();

                    if let Some(key) =
                        verify_xor_match(pattern, data, atom_pos, atom, *flags)
                    {
                        self.handle_sub_pattern_match(
                            sub_pattern_id,
                            sub_pattern,
                            *pattern_id,
                            Match::new(atom_pos..atom_pos + pattern.len())
                                .rebase(base)
                                .xor_key(key),
                        );
                    }
                }

                SubPattern::Base64 { pattern, padding }
                | SubPattern::Base64Wide { pattern, padding } => {
                    if let Some(match_range) = verify_base64_match(
                        self.compiled_rules
                            .lit_pool()
                            .get_bytes(*pattern)
                            .unwrap(),
                        data,
                        (*padding).into(),
                        atom_pos,
                        None,
                        matches!(sub_pattern, SubPattern::Base64Wide { .. }),
                    ) {
                        self.handle_sub_pattern_match(
                            sub_pattern_id,
                            sub_pattern,
                            *pattern_id,
                            Match::new(match_range).rebase(base),
                        );
                    }
                }

                SubPattern::CustomBase64 { pattern, alphabet, padding }
                | SubPattern::CustomBase64Wide {
                    pattern,
                    alphabet,
                    padding,
                } => {
                    let alphabet = self
                        .compiled_rules
                        .lit_pool()
                        .get_str(*alphabet)
                        .map(|alphabet| {
                            // `Alphabet::new` validates the string again. This
                            // is not really necessary as we already know that
                            // the string represents a valid alphabet, it would
                            // be better if we could use the private function
                            // `Alphabet::from_str_unchecked`
                            base64::alphabet::Alphabet::new(alphabet).unwrap()
                        });

                    assert!(alphabet.is_some());

                    if let Some(match_range) = verify_base64_match(
                        self.compiled_rules
                            .lit_pool()
                            .get_bytes(*pattern)
                            .unwrap(),
                        data,
                        (*padding).into(),
                        atom_pos,
                        alphabet,
                        matches!(
                            sub_pattern,
                            SubPattern::CustomBase64Wide { .. }
                        ),
                    ) {
                        self.handle_sub_pattern_match(
                            sub_pattern_id,
                            sub_pattern,
                            *pattern_id,
                            Match::new(match_range).rebase(base),
                        );
                    }
                }
            };

            #[cfg(feature = "rules-profiling")]
            {
                let time_spent = self
                    .clock
                    .delta_as_nanos(verification_start, self.clock.raw());

                self.time_spent_in_pattern
                    .entry(*pattern_id)
                    .and_modify(|t| {
                        t.add_assign(time_spent);
                    })
                    .or_insert(time_spent);
            }
        }

        #[cfg(feature = "logging")]
        log::info!("Atom matches: {}", atom_matches);

        Ok(())
    }

    /// Search for patterns in the scanned data.
    ///
    /// The pattern search phase is when YARA scans the data looking for the
    /// patterns declared in rules. All the patterns are searched simultaneously
    /// using the Aho-Corasick algorithm. This phase is triggered lazily during
    /// the evaluation of the rule conditions, when some of the conditions need
    /// to know if a pattern matched or not.
    ///
    /// This function won't be called if the conditions can be fully evaluated
    /// without looking for any of the patterns. If it must be called, it will be
    /// called only once.
    ///
    /// In case of timeout, this function returns [ScanError::Timeout] and sets
    /// the scan state to [ScanState::Timeout].
    pub(crate) fn search_for_patterns(&mut self) -> Result<(), ScanError> {
        // Take ownership of the scan state, while searching for
        // the patterns, `self.scan_state` is left as `Idle`.
        let state = self.scan_state.take();

        let (base, data, block_scanning_mode) = match &state {
            ScanState::ScanningData(data) => (0, data.as_ref(), false),
            ScanState::ScanningBlock((base, data)) => (*base, *data, true),
            _ => panic!(),
        };

        #[cfg(any(feature = "rules-profiling", feature = "logging"))]
        let scan_start = self.clock.raw();

        // Verify the anchored pattern first. These are patterns that can
        // match at a single known offset within the data.
        self.verify_anchored_patterns(base, data);

        let result = match self.ac_search_loop(base, data, block_scanning_mode)
        {
            Ok(_) => {
                self.scan_state = state;
                Ok(())
            }
            Err(ScanError::Timeout) => {
                self.scan_state = ScanState::Timeout;
                Err(ScanError::Timeout)
            }
            _ => unreachable!(),
        };

        // Indicate that the pattern search phase was already done.
        self.set_pattern_search_done(true);

        #[cfg(any(feature = "rules-profiling", feature = "logging"))]
        let scan_end = self.clock.raw();

        // Adjust the rule evaluation start time to exclude the time spent
        // searching for patterns. Since the `search_for_pattern` function
        // is invoked lazily during the evaluation of some rule, the overall
        // evaluation time for that rule may appear longer. To ensure that
        // search time is not attributed to the rule, we need to adjust
        // `rule_evaluation_start_time` accordingly.
        #[cfg(feature = "rules-profiling")]
        {
            self.rule_execution_start_time +=
                scan_end.saturating_sub(scan_start);
        }

        #[cfg(feature = "logging")]
        {
            log::info!(
                "Scan time: {:?}",
                self.clock.delta(scan_start, scan_end)
            );
        }

        result
    }

    fn verify_anchored_patterns(&mut self, base: usize, data: &[u8]) {
        for (sub_pattern_id, (pattern_id, sub_pattern)) in self
            .compiled_rules
            .anchored_sub_patterns()
            .iter()
            .map(|id| (id, self.compiled_rules.get_sub_pattern(*id)))
        {
            match sub_pattern {
                SubPattern::Literal {
                    pattern,
                    flags,
                    anchored_at: Some(offset),
                    ..
                } => {
                    // Make the offset relative to the block's base. If an
                    // overflow occurs is because the block's base is larger
                    // than the offset, in such cases the match can't occur.
                    if let (offset, false) = offset.overflowing_sub(base) {
                        let pattern = self
                            .compiled_rules
                            .lit_pool()
                            .get_bytes(*pattern)
                            .unwrap();

                        if verify_literal_match(pattern, data, offset, *flags)
                        {
                            self.handle_sub_pattern_match(
                                *sub_pattern_id,
                                sub_pattern,
                                *pattern_id,
                                Match::new(offset..offset + pattern.len())
                                    .rebase(base),
                            );
                        }
                    }
                }
                _ => unreachable!(),
            }
        }
    }

    fn handle_sub_pattern_match(
        &mut self,
        sub_pattern_id: SubPatternId,
        sub_pattern: &SubPattern,
        pattern_id: PatternId,
        match_: Match,
    ) {
        match sub_pattern {
            SubPattern::Literal { .. }
            | SubPattern::Xor { .. }
            | SubPattern::Base64 { .. }
            | SubPattern::Base64Wide { .. }
            | SubPattern::CustomBase64 { .. }
            | SubPattern::CustomBase64Wide { .. } => {
                self.track_pattern_match(pattern_id, match_, false);
            }
            SubPattern::Regexp { flags, .. } => {
                self.track_pattern_match(
                    pattern_id,
                    match_,
                    flags.contains(SubPatternFlags::GreedyRegexp),
                );
            }
            SubPattern::LiteralChainHead { .. }
            | SubPattern::RegexpChainHead { .. } => {
                // This is the head of a set of chained sub-patterns.
                // Verifying that the head matches doesn't mean that
                // the whole sub-pattern matches, the rest of the chain
                // must be found as well. For the time being this is
                // just an unconfirmed match.
                self.unconfirmed_matches
                    .entry(sub_pattern_id)
                    .or_default()
                    .push(UnconfirmedMatch {
                        range: match_.range,
                        chain_length: 0,
                    })
            }
            SubPattern::LiteralChainTail {
                chained_to, gap, flags, ..
            }
            | SubPattern::RegexpChainTail { chained_to, gap, flags, .. } => {
                if self.within_valid_distance(
                    *chained_to,
                    match_.range.start,
                    gap,
                ) {
                    if flags.contains(SubPatternFlags::LastInChain) {
                        // This sub-pattern is the last one in the
                        // chain. We can proceed to verify the whole
                        // chain and determine if it matched or not.
                        self.verify_chain_of_matches(
                            pattern_id,
                            sub_pattern_id,
                            match_,
                        );
                    } else {
                        // This sub-pattern is in the middle of the
                        // chain. We need to find the sub-patterns that
                        // follow, so for the time being this only an
                        // unconfirmed match.
                        self.unconfirmed_matches
                            .entry(sub_pattern_id)
                            .or_default()
                            .push(UnconfirmedMatch {
                                range: match_.range,
                                chain_length: 0,
                            });
                    }
                }
            }
        }
    }

    fn within_valid_distance(
        &mut self,
        chained_to: SubPatternId,
        match_start: usize,
        gap: &ChainedPatternGap,
    ) -> bool {
        if let Some(unconfirmed_matches) =
            self.unconfirmed_matches.get_mut(&chained_to)
        {
            for m in unconfirmed_matches {
                match gap {
                    ChainedPatternGap::Bounded(gap) => {
                        let min_gap = *gap.start() as usize;
                        let max_gap = *gap.end() as usize;
                        if (m.range.end + min_gap..=m.range.end + max_gap)
                            .contains(&match_start)
                        {
                            return true;
                        }
                    }
                    ChainedPatternGap::Unbounded(gap) => {
                        let min_gap = gap.start as usize;
                        if (m.range.start + min_gap..).contains(&match_start) {
                            return true;
                        }
                    }
                };
            }
        }

        false
    }

    /// Given the [`SubPatternId`] associated to the last sub-pattern in a
    /// chain, and a range where this sub-pattern matched, verifies that the
    /// whole chain actually matches.
    ///
    /// The `tail_sub_pattern_id` argument must identify the last sub-pattern
    /// in a chain. For example, if the chain is `S1 <- S2 <- S3`, this function
    /// must receive the [`SubPatternId`] for `S3`, and a range where `S3`
    /// matched. Then the function traverses the chain from the tail to the
    /// head, making sure that each intermediate sub-pattern has unconfirmed
    /// matches that have the correct distance between them, so that the whole
    /// chain matches from head to tail. If the whole chain matches, the
    /// corresponding match is added to the list of confirmed matches for
    /// pattern identified by `pattern_id`.
    fn verify_chain_of_matches(
        &mut self,
        pattern_id: PatternId,
        tail_sub_pattern_id: SubPatternId,
        tail_match: Match,
    ) {
        let mut queue = VecDeque::new();

        queue.push_back((
            tail_sub_pattern_id,
            UnconfirmedMatch {
                range: tail_match.range.clone(),
                chain_length: 1,
            },
        ));

        let mut tail_chained_to: Option<SubPatternId> = None;

        while let Some((id, current_match)) = queue.pop_front() {
            match &self.compiled_rules.get_sub_pattern(id).1 {
                SubPattern::LiteralChainHead { flags, .. }
                | SubPattern::RegexpChainHead { flags, .. } => {
                    // The chain head is reached. This indicates that the whole
                    // chain is valid, and we have a full match.
                    self.track_pattern_match(
                        pattern_id,
                        Match {
                            base: tail_match.base,
                            range: current_match.range.start
                                ..tail_match.range.end,
                            xor_key: None,
                        },
                        flags.contains(SubPatternFlags::GreedyRegexp),
                    );

                    let mut next_pattern_id = tail_chained_to;

                    while let Some(id) = next_pattern_id {
                        if let Some(unconfirmed_matches) =
                            self.unconfirmed_matches.get_mut(&id)
                        {
                            unconfirmed_matches
                                .iter_mut()
                                .for_each(|m| m.chain_length = 0);
                        }
                        let (_, sub_pattern) =
                            self.compiled_rules.get_sub_pattern(id);
                        next_pattern_id = sub_pattern.chained_to();
                    }
                }
                SubPattern::LiteralChainTail {
                    chained_to,
                    gap,
                    flags,
                    ..
                }
                | SubPattern::RegexpChainTail {
                    chained_to, gap, flags, ..
                } => {
                    // Iterate over the list of unconfirmed matches of the
                    // sub-pattern that comes before in the chain. For example,
                    // if the chain is P1 <- P2, and we just found a match for
                    // P2, iterate over the unconfirmed matches for P1.
                    let unconfirmed_matches =
                        match self.unconfirmed_matches.get_mut(chained_to) {
                            Some(m) => m,
                            None => continue,
                        };

                    // Check whether the current match is at a correct distance
                    // from each of the unconfirmed matches.
                    for m in unconfirmed_matches {
                        let in_range = match gap {
                            ChainedPatternGap::Bounded(gap) => {
                                let min_gap = *gap.start() as usize;
                                let max_gap = *gap.end() as usize;
                                (m.range.end + min_gap..=m.range.end + max_gap)
                                    .contains(&current_match.range.start)
                            }
                            ChainedPatternGap::Unbounded(gap) => {
                                let min_gap = gap.start as usize;
                                (m.range.end + min_gap..)
                                    .contains(&current_match.range.start)
                            }
                        };
                        // Besides checking that the unconfirmed match lays at
                        // a correct distance from the current match, we also
                        // check that the chain length associated to the
                        // unconfirmed match doesn't exceed the current chain
                        // length.
                        if in_range
                            && m.chain_length <= current_match.chain_length
                        {
                            m.chain_length = current_match.chain_length + 1;
                            queue.push_back((*chained_to, m.clone()))
                        }
                    }

                    if flags.contains(SubPatternFlags::LastInChain)
                        && flags.contains(SubPatternFlags::GreedyRegexp)
                    {
                        tail_chained_to = Some(*chained_to);
                    }
                }
                _ => unreachable!(),
            };
        }
    }
}

/// Verifies if a literal `pattern` matches at `match_start` in `scanned_data`.
fn verify_literal_match(
    pattern: &[u8],
    scanned_data: &[u8],
    match_start: usize,
    flags: SubPatternFlags,
) -> bool {
    // Offset where the match should end (exclusive).
    let match_end = match_start + pattern.len();

    // The match can not end past the end of the scanned data.
    if match_end > scanned_data.len() {
        return false;
    }

    if flags.intersects(
        SubPatternFlags::FullwordLeft | SubPatternFlags::FullwordRight,
    ) && !verify_full_word(
        scanned_data,
        &(match_start..match_end),
        flags,
        None,
    ) {
        return false;
    }

    if flags.contains(SubPatternFlags::Nocase) {
        pattern.eq_ignore_ascii_case(&scanned_data[match_start..match_end])
    } else {
        &scanned_data[match_start..match_end] == pattern.as_bytes()
    }
}

/// Returns true if the match delimited by `match_range` is a full word match.
/// This means that the bytes before the range's start and after the range's
/// end are both non-alphanumeric.
fn verify_full_word(
    scanned_data: &[u8],
    match_range: &Range<usize>,
    flags: SubPatternFlags,
    xor_key: Option<u8>,
) -> bool {
    let xor_key = xor_key.unwrap_or(0);

    if flags.contains(SubPatternFlags::Wide) {
        if flags.contains(SubPatternFlags::FullwordLeft)
            && match_range.start >= 2
            && (scanned_data[match_range.start - 1] ^ xor_key) == 0
            && (scanned_data[match_range.start - 2] ^ xor_key)
                .is_ascii_alphanumeric()
        {
            return false;
        }
        if flags.contains(SubPatternFlags::FullwordRight)
            && match_range.end + 1 < scanned_data.len()
            && (scanned_data[match_range.end + 1] ^ xor_key) == 0
            && (scanned_data[match_range.end] ^ xor_key)
                .is_ascii_alphanumeric()
        {
            return false;
        }
    } else {
        if flags.contains(SubPatternFlags::FullwordLeft)
            && match_range.start >= 1
            && (scanned_data[match_range.start - 1] ^ xor_key)
                .is_ascii_alphanumeric()
        {
            return false;
        }
        if flags.contains(SubPatternFlags::FullwordRight)
            && match_range.end < scanned_data.len()
            && (scanned_data[match_range.end] ^ xor_key)
                .is_ascii_alphanumeric()
        {
            return false;
        }
    }

    true
}

/// When some `atom` belonging to a regexp is found at `match_start`, verify
/// that the regexp actually matches.
///
/// This function can produce multiple matches, `f` is called for every
/// match found.
fn verify_regexp_match(
    vm: &mut VM,
    scanned_data: &[u8],
    match_start: usize,
    atom: &SubPatternAtom,
    flags: SubPatternFlags,
    mut f: impl FnMut(Range<usize>),
) {
    let mut fwd_match_len = None;

    // If the atom has some forward code, that's the code that should execute
    // the VM for matching the portion that pattern that comes after the atom.
    // The type of VM used depends on whether the pattern was compiled for the
    // faster and less general FastVM, or for the slower but more general
    // PikeVM.
    if let Some(fwd_code) = atom.fwd_code() {
        if flags.contains(SubPatternFlags::FastRegexp) {
            vm.fast_vm.try_match(
                fwd_code,
                &scanned_data[match_start..],
                flags.contains(SubPatternFlags::Wide),
                |match_len| {
                    fwd_match_len = Some(match_len);
                    if flags.contains(SubPatternFlags::GreedyRegexp) {
                        Action::Continue
                    } else {
                        Action::Stop
                    }
                },
            );
        } else {
            vm.pike_vm.try_match(
                fwd_code,
                &scanned_data[match_start..],
                &scanned_data[..match_start],
                flags.contains(SubPatternFlags::Wide),
                |match_len| {
                    fwd_match_len = Some(match_len);
                    Action::Stop
                },
            );
        }
    } else {
        fwd_match_len = Some(atom.len());
    }

    let fwd_match_len = match fwd_match_len {
        Some(len) => len,
        None => return,
    };

    if let Some(bck_code) = atom.bck_code() {
        if flags.contains(SubPatternFlags::FastRegexp) {
            vm.fast_vm.try_match(
                bck_code,
                &scanned_data[..match_start],
                flags.contains(SubPatternFlags::Wide),
                |bck_match_len| {
                    let range = match_start - bck_match_len
                        ..match_start + fwd_match_len;
                    if verify_full_word(scanned_data, &range, flags, None) {
                        f(range);
                    }
                    Action::Continue
                },
            );
        } else {
            vm.pike_vm.try_match(
                bck_code,
                &scanned_data[match_start..],
                &scanned_data[..match_start],
                flags.contains(SubPatternFlags::Wide),
                |bck_match_len| {
                    let range = match_start - bck_match_len
                        ..match_start + fwd_match_len;
                    if verify_full_word(scanned_data, &range, flags, None) {
                        f(range);
                    }
                    Action::Continue
                },
            );
        }
    } else {
        let range = match_start..match_start + fwd_match_len;
        if verify_full_word(scanned_data, &range, flags, None) {
            f(range);
        }
    }
}

/// Verifies that `pattern` actually matches in XORed form at `match_start`
/// within `scanned_data`.
///
/// Returns the XOR key if the match was confirmed, or [`None`] if otherwise.
fn verify_xor_match(
    pattern: &[u8],
    scanned_data: &[u8],
    match_start: usize,
    atom: &SubPatternAtom,
    flags: SubPatternFlags,
) -> Option<u8> {
    // Offset where the match should end (exclusive).
    let match_end = match_start + pattern.len();

    // The match can not end past the end of the scanned data.
    if match_end > scanned_data.len() {
        return None;
    }

    // The atom that matched is the result of XORing the pattern with some
    // key. The key can be obtained by XORing some byte in the atom with
    // the corresponding byte in the pattern.
    let key = atom.as_slice()[0] ^ pattern[atom.backtrack()];

    let match_range = match_start..match_end;

    if !verify_full_word(scanned_data, &match_range, flags, Some(key)) {
        return None;
    }

    if key == 0 {
        if pattern != &scanned_data[match_start..match_end] {
            return None;
        }
    } else {
        for (i, b) in scanned_data[match_start..match_end].iter().enumerate() {
            if pattern[i] != b ^ key {
                return None;
            }
        }
    }

    Some(key)
}

/// Verifies that `pattern` actually matches in base64 form at `match_start`
/// within `scanned_data`.
///
/// Returns the range where the match was found or [`None`] if otherwise.
fn verify_base64_match(
    pattern: &[u8],
    scanned_data: &[u8],
    padding: usize,
    match_start: usize,
    alphabet: Option<base64::alphabet::Alphabet>,
    wide: bool,
) -> Option<Range<usize>> {
    // The pattern is passed to this function in its original form, before
    // being encoded as base64. Compute the size of the pattern once it is
    // encoded as base64.
    let len = base64::encoded_len(pattern.len(), false)?;

    // A portion of the pattern in base64 form was found at `match_start`,
    // but decoding the base64 string starting at that offset is not ok
    // because some characters may have been removed from both the left
    // and the right sides of the base64 string that has been found. For
    // example, for the pattern "foobar" one of the base64 patterns that
    // are searched for is "Zvb2Jhc", but once this pattern is found in
    // the scanned data we can't simply decode that string and expect
    // to find "foobar" in the decoded data.
    //
    // What we must do is use two more characters from the left, and one
    // more from the right of "Zvb2Jhc", like for example "eGZvb2Jhcg",
    // which is decoded as "xfoobar". The actual number of additional
    // characters that must be used from the left and right of the pattern
    // found depends on the padding and the rest of dividing the pattern's
    // length by 4. The table below covers all possible cases using the
    // plain text patterns "foobar", "fooba" and "foob".
    //
    // padding          base64                      len (mod 4)
    //    0   foobar    Zm9vYmFy    len(b64(foobar))  8 (0)  [Zm9vYmFy]
    //    0   fooba     Zm9vYmE     len(b64(fooba))   7 (3)  [Zm9vYm]E
    //    0   foob      Zm9vYg      len(b64(foob))    6 (2)  [Zm9vY]g
    //
    //    1   xfoobar   eGZvb2Jhcg  len(b64(foobar))  8 (0)  eG[Zvb2Jhc]g
    //    1   xfooba    eGZvb2Jh    len(b64(fooba))   7 (3)  eG[Zvb2Jh]
    //    1   xfoob     eGZvb2I     len(b64(foob))    6 (2)  eG[Zvb2]I
    //
    //    2   xxfoobar  eHhmb29iYXI len(b64(foobar))  8 (0)  eHh[mb29iYX]I
    //    2   xxfooba   eHhmb29iYQ  len(b64(fooba))   7 (3)  eHh[mb29iY]Q
    //    2   xxfoob    eHhmb29i    len(b64(foob))    6 (2)  eHh[mb29i]
    //
    // In the rightmost column the portion of the base64 pattern that is
    // searched using the Aho-Corasick algorithm is enclosed in [].
    let (mut decode_start_delta, mut decode_len, mut match_len) = match padding
    {
        0 => match len % 4 {
            0 => (0, len, len),
            2 => (0, len + 2, len - 1),
            3 => (0, len + 1, len - 1),
            _ => unreachable!(),
        },
        1 => match len % 4 {
            0 => (2, len + 4, len - 1),
            2 => (2, len + 2, len - 2),
            3 => (2, len + 1, len - 1),
            _ => unreachable!(),
        },
        2 => match len % 4 {
            0 => (3, len + 4, len - 1),
            2 => (3, len + 2, len - 1),
            3 => (3, len + 5, len - 1),
            _ => unreachable!(),
        },
        _ => unreachable!(),
    };

    // In wide mode each base64 character is two bytes long, adjust
    // decode_start_delta and lengths accordingly.
    if wide {
        decode_start_delta *= 2;
        decode_len *= 2;
        match_len *= 2;
    }

    // decode_range is the range within the scanned data that we are going
    // to decode as base64. It starts at match_start - decode_start_delta,
    // but if match_start < decode_start_delta this is not a real match.
    let mut decode_range = if let (decode_start, false) =
        match_start.overflowing_sub(decode_start_delta)
    {
        decode_start..decode_start + decode_len
    } else {
        return None;
    };

    // If the end of decode_range is past the end of the scanned data
    // truncate it to scanned_data_len.
    if decode_range.end > scanned_data.len() {
        decode_range.end = scanned_data.len();
    }

    let base64_engine = base64::engine::GeneralPurpose::new(
        alphabet.as_ref().unwrap_or(&base64::alphabet::STANDARD),
        base64::engine::general_purpose::NO_PAD,
    );

    let decoded = if wide {
        // Collect the ASCII characters at even positions and make sure
        // that bytes at odd positions are zeroes.
        let mut ascii = Vec::with_capacity(decode_range.len() / 2);
        for (i, b) in scanned_data[decode_range].iter().enumerate() {
            if i.is_multiple_of(2) {
                // Padding (=) is not added to ASCII string.
                if *b != b'=' {
                    ascii.push(*b)
                }
            } else if *b != 0 {
                return None;
            }
        }
        base64_engine.decode(ascii)
    } else {
        let s = &scanned_data[decode_range];

        // Strip padding if present.
        let s = if s.ends_with_str(b"==") {
            &s[0..s.len().saturating_sub(2)]
        } else if s.ends_with_str(b"=") {
            &s[0..s.len().saturating_sub(1)]
        } else {
            s
        };

        base64_engine.decode(s)
    };

    // If the decoding was successful, ignore the padding and compare to the
    // expected pattern.
    let decoded_pattern =
        decoded.as_ref().ok()?.get(padding..padding + pattern.len())?;

    if pattern.eq(decoded_pattern) {
        Some(match_start..match_start + match_len)
    } else {
        None
    }
}

struct VM<'r> {
    pike_vm: PikeVM<'r>,
    fast_vm: FastVM<'r>,
}

/// A runtime object is a struct, array, map or string used during the
/// evaluation of a rule condition. Instances of these types can't cross the
/// WASM-Rust boundary, as integers and floats can do. Therefore, they are
/// stored in a hash map in [`ScanContext`], using a [`RuntimeObjectHandler`]
/// as the key that identifies each object. Handlers are actually 64-bits
/// integers that can cross the WASM-Rust boundary, and used to retrieve the
/// original object from [`ScanContext`].
pub(crate) enum RuntimeObject {
    Struct(Rc<Struct>),
    Array(Rc<Array>),
    Map(Rc<Map>),
    String(Rc<BString>),
}

impl RuntimeObject {
    pub fn as_struct(&self) -> Rc<Struct> {
        if let Self::Struct(s) = self {
            s.clone()
        } else {
            panic!(
                "calling `as_struct` in a RuntimeObject that is not a struct"
            )
        }
    }

    pub fn as_array(&self) -> Rc<Array> {
        if let Self::Array(a) = self {
            a.clone()
        } else {
            panic!(
                "calling `as_array` in a RuntimeObject that is not an array"
            )
        }
    }

    pub fn as_map(&self) -> Rc<Map> {
        if let Self::Map(m) = self {
            m.clone()
        } else {
            panic!("calling `as_map` in a RuntimeObject that is not a map")
        }
    }
}

/// A runtime object handle is an opaque integer value that identifies a
/// runtime object.
#[derive(Copy, Clone, Hash, Eq, PartialEq, Default)]
pub(crate) struct RuntimeObjectHandle(i64);

impl RuntimeObjectHandle {
    pub(crate) const NULL: Self = RuntimeObjectHandle(-1);
}

impl From<RuntimeObjectHandle> for i64 {
    fn from(value: RuntimeObjectHandle) -> Self {
        value.0
    }
}

impl From<i64> for RuntimeObjectHandle {
    fn from(value: i64) -> Self {
        Self(value)
    }
}

pub fn create_wasm_store_and_ctx<'r>(
    rules: &'r Rules,
) -> Pin<Box<Store<ScanContext<'static, 'static>>>> {
    let num_rules = rules.num_rules() as u32;
    let num_patterns = rules.num_patterns() as u32;

    let ctx = ScanContext {
        runtime_objects: IndexMap::new(),
        compiled_rules: rules,
        console_log: None,
        current_struct: None,
        scan_timeout: None,
        scan_state: ScanState::Idle,
        root_struct: rules.globals().make_root(),
        matching_rules: Vec::new(),
        matching_rules_per_ns: IndexMap::new(),
        num_matching_private_rules: 0,
        num_non_matching_private_rules: 0,
        wasm_store: NonNull::dangling(),
        wasm_module: MaybeUninit::uninit(),
        wasm_main_memory: None,
        wasm_main_func: None,
        wasm_filesize: None,
        wasm_pattern_search_done: None,
        module_outputs: FxHashMap::default(),
        user_provided_module_outputs: FxHashMap::default(),
        pattern_matches: PatternMatches::new(),
        unconfirmed_matches: FxHashMap::default(),
        deadline: 0,
        limit_reached: FxHashSet::default(),
        regexp_cache: RefCell::new(FxHashMap::default()),
        #[cfg(feature = "rules-profiling")]
        time_spent_in_pattern: FxHashMap::default(),
        #[cfg(feature = "rules-profiling")]
        time_spent_in_rule: vec![0; num_rules as usize],
        #[cfg(feature = "rules-profiling")]
        rule_execution_start_time: 0,
        #[cfg(feature = "rules-profiling")]
        last_executed_rule: None,
        #[cfg(any(feature = "rules-profiling", feature = "logging"))]
        clock: quanta::Clock::new(),
    };

    // The ScanContext structure belongs to the WASM store, but at the same
    // time the context must have a reference to the store because it is
    // required for accessing the WASM memory from code that only has a
    // reference to ScanContext. This kind of circular data structures are
    // not natural to Rust, and they can be achieved either by using unsafe
    // pointers, or by using Rc::Weak. In this case we are storing a pointer
    // to the store in ScanContext. The store is put into a pinned box in order
    // to make sure that it doesn't move from its original memory address and
    // the pointer remains valid.
    //
    // Also, the `Store` type requires a type T that is static, therefore
    // we are forced to transmute the ScanContext<'r> into ScanContext<'static>.
    // This is safe to do because the Store only lives for the time that
    // the scanner lives, and 'r is the lifetime for the rules passed to
    // the scanner, which are guaranteed to outlive the scanner.
    let mut wasm_store = Box::pin(Store::new(wasm::get_engine(), unsafe {
        transmute::<ScanContext<'r, '_>, ScanContext<'static, 'static>>(ctx)
    }));

    // Initialize the ScanContext.wasm_store pointer that was initially
    // dangling.
    wasm_store.data_mut().wasm_store =
        NonNull::from(wasm_store.as_ref().deref());

    // Global variable that will hold the value for `filesize`. This is
    // initialized to -1 (which means undefined) because the file size
    // is not known until some data is scanned.
    let filesize = Global::new(
        wasm_store.as_context_mut(),
        GlobalType::new(ValType::I64, Mutability::Var),
        Val::I64(-1),
    )
    .unwrap();

    // Global variable that is set to `true` when the Aho-Corasick pattern
    // search phase has been executed.
    let pattern_search_done = Global::new(
        wasm_store.as_context_mut(),
        GlobalType::new(ValType::I32, Mutability::Var),
        Val::I32(0),
    )
    .unwrap();

    // Compute the base offset for the bitmap that contains matching
    // information for patterns. This bitmap has 1 bit per pattern, the
    // N-th bit is set if pattern with PatternId = N matched. The bitmap
    // starts right after the bitmap that contains matching information
    // for rules.
    let matching_patterns_bitmap_base =
        MATCHING_RULES_BITMAP_BASE as u32 + num_rules.div_ceil(8);

    // Compute the required memory size in 64KB pages.
    let mem_size = u32::div_ceil(
        matching_patterns_bitmap_base + num_patterns.div_ceil(8),
        65536,
    );

    let matching_patterns_bitmap_base = Global::new(
        wasm_store.as_context_mut(),
        GlobalType::new(ValType::I32, Mutability::Const),
        Val::I32(matching_patterns_bitmap_base as i32),
    )
    .unwrap();

    // Create module's main memory.
    let main_memory = wasm::runtime::Memory::new(
        wasm_store.as_context_mut(),
        MemoryType::new(mem_size, Some(mem_size)),
    )
    .unwrap();

    // Instantiate the module. This takes the wasm code provided by the
    // `wasm_mod` function and links its imported functions with the
    // implementations that YARA provides.
    let wasm_instance = wasm::new_linker()
        .define(wasm_store.as_context(), "yara_x", "filesize", filesize)
        .unwrap()
        .define(
            wasm_store.as_context(),
            "yara_x",
            "pattern_search_done",
            pattern_search_done,
        )
        .unwrap()
        .define(
            wasm_store.as_context(),
            "yara_x",
            "matching_patterns_bitmap_base",
            matching_patterns_bitmap_base,
        )
        .unwrap()
        .define(wasm_store.as_context(), "yara_x", "main_memory", main_memory)
        .unwrap()
        .instantiate(wasm_store.as_context_mut(), rules.wasm_mod())
        .unwrap();

    // Obtain a reference to the "main" function exported by the module.
    let main_fn = wasm_instance
        .get_typed_func::<(), i32>(wasm_store.as_context_mut(), "main")
        .unwrap();

    let ctx = wasm_store.data_mut();

    ctx.wasm_module = MaybeUninit::new(wasm_instance);
    ctx.wasm_main_memory = Some(main_memory);
    ctx.wasm_main_func = Some(main_fn);
    ctx.wasm_filesize = Some(filesize);
    ctx.wasm_pattern_search_done = Some(pattern_search_done);

    wasm_store
}