dfajit 0.1.1

JIT compilation of DFA transition tables to native x86_64 jump tables
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
use crate::codegen;
use crate::error::{Error, Result};
use crate::table::TransitionTable;
use matchkit::Match;
#[cfg(feature = "regex")]
use regex_automata::{
    dfa::{dense, Automaton},
    Input, MatchKind,
};
#[cfg(feature = "regex")]
use std::collections::{HashMap, VecDeque};

/// A JIT-compiled DFA that executes pattern matching as native code.
///
/// On non-x86_64 platforms, falls back to an interpreted table-driven scan.
pub struct JitDfa {
    #[cfg(target_arch = "x86_64")]
    code: codegen::ExecutableBuffer,
    #[cfg(not(target_arch = "x86_64"))]
    table: TransitionTable,
    state_count: usize,
    pattern_count: usize,
    /// Aho-Corasick output links for multi-pattern accept chains.
    /// Used by the interpreted fallback on non-x86_64; stored on x86_64 for
    /// potential runtime inspection.
    #[allow(dead_code)]
    output_links: Vec<u32>,
}

impl std::fmt::Debug for JitDfa {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JitDfa")
            .field("state_count", &self.state_count)
            .field("pattern_count", &self.pattern_count)
            .finish_non_exhaustive()
    }
}

impl JitDfa {
    /// Compile a DFA transition table to native code.
    ///
    /// # Errors
    ///
    /// Returns an error if the transition table is invalid or if
    /// the executable memory allocation fails.
    /// Example: `Error::EmptyDfa` when `table.state_count == 0`
    /// Example: `Error::TooManyStates` when state_count > 4096.
    pub fn compile(table: &TransitionTable) -> Result<Self> {
        Self::compile_with_output_links(table, &[])
    }

    fn compile_with_output_links(table: &TransitionTable, output_links: &[u32]) -> Result<Self> {
        if table.state_count() == 0 {
            return Err(Error::EmptyDfa);
        }

        if table.class_count() != 256 {
            return Err(Error::InvalidTable {
                reason: format!(
                    "class_count must be 256 for JIT, got {}",
                    table.class_count()
                ),
            });
        }

        let expected_len = table
            .state_count()
            .checked_mul(table.class_count())
            .ok_or_else(|| Error::InvalidTable {
                reason: format!(
                    "state_count={} * class_count={} overflows",
                    table.state_count(),
                    table.class_count(),
                ),
            })?;

        if table.transitions().len() != expected_len {
            return Err(Error::InvalidTable {
                reason: format!(
                    "transition table has {} entries but state_count={} * class_count={} = {}",
                    table.transitions().len(),
                    table.state_count(),
                    table.class_count(),
                    expected_len,
                ),
            });
        }

        if !output_links.is_empty() && output_links.len() != table.state_count() {
            return Err(Error::InvalidTable {
                reason: format!(
                    "output_links has {} entries but state_count is {}",
                    output_links.len(),
                    table.state_count()
                ),
            });
        }

        // Validate every output_link points to a valid state or sentinel.
        for (i, &link) in output_links.iter().enumerate() {
            if link != 0xFFFF_FFFF && (link as usize) >= table.state_count() {
                return Err(Error::InvalidTable {
                    reason: format!(
                        "output_links[{i}] = {link} exceeds state_count {}",
                        table.state_count()
                    ),
                });
            }
        }

        // Build accept-state set for bit-31 validation.
        let accept_set: std::collections::HashSet<u32> =
            table.accept_states().iter().map(|&(s, _)| s).collect();

        for &t in table.transitions() {
            let state = t & 0x7FFF_FFFF;
            if state as usize >= table.state_count() {
                return Err(Error::InvalidTable {
                    reason: format!(
                        "transition target state {state} exceeds state count {}",
                        table.state_count()
                    ),
                });
            }
            if (t & 0x8000_0000) != 0 && !accept_set.contains(&state) {
                return Err(Error::InvalidTable {
                    reason: format!(
                        "transition target state {state} has bit 31 set but is not an accept state"
                    ),
                });
            }
        }

        let mut seen_states = vec![false; table.state_count()];
        let pat_len = table.pattern_lengths().len();
        for &(state, pid) in table.accept_states() {
            if state as usize >= table.state_count() {
                return Err(Error::InvalidTable {
                    reason: format!(
                        "accept state {state} exceeds state count {}",
                        table.state_count()
                    ),
                });
            }
            if seen_states[state as usize] {
                return Err(Error::InvalidTable {
                    reason: format!(
                        "state {state} has multiple accept patterns, which is not supported"
                    ),
                });
            }
            seen_states[state as usize] = true;
            if pid as usize >= pat_len {
                return Err(Error::InvalidTable {
                    reason: format!("pattern ID {pid} in accept states has no length defined"),
                });
            }
        }

        let pattern_count = table
            .accept_states()
            .iter()
            .map(|&(_, pid)| pid as usize + 1)
            .max()
            .unwrap_or(0);

        #[cfg(target_arch = "x86_64")]
        {
            let code = codegen::compile_x86_64(table, output_links)?;
            Ok(Self {
                code,
                state_count: table.state_count(),
                pattern_count,
                output_links: output_links.to_vec(),
            })
        }

        #[cfg(not(target_arch = "x86_64"))]
        {
            Ok(Self {
                table: table.clone(),
                state_count: table.state_count(),
                pattern_count,
                output_links: output_links.to_vec(),
            })
        }
    }

    /// Scan input bytes, appending matches to the output vector.
    ///
    /// Returns the number of new matches found.
    pub fn scan(&self, input: &[u8], matches: &mut [Match]) -> usize {
        #[cfg(target_arch = "x86_64")]
        {
            self.code.scan(input, matches)
        }

        #[cfg(not(target_arch = "x86_64"))]
        {
            self.scan_interpreted(input, matches)
        }
    }

    /// Interpreted fallback for non-x86_64 platforms.
    #[cfg(not(target_arch = "x86_64"))]
    fn scan_interpreted(&self, input: &[u8], matches: &mut [Match]) -> usize {
        let table = &self.table;
        let mut state = 0u32;
        let mut count = 0usize;

        let mut accept_pattern = vec![0xFFFF_FFFF; table.state_count()];
        for &(s, pid) in table.accept_states() {
            accept_pattern[s as usize] = pid;
        }

        for (pos, &byte) in input.iter().enumerate() {
            let idx = state as usize * table.class_count() + byte as usize;
            let next = table.transitions().get(idx).copied().unwrap_or(0);
            let clean_next = next & 0x7FFF_FFFF;

            if accept_pattern[clean_next as usize] != 0xFFFF_FFFF {
                let mut output_state = clean_next;
                while output_state != 0xFFFF_FFFF {
                    let pid = accept_pattern[output_state as usize];
                    if count < matches.len() {
                        let end = (pos + 1) as u32;
                        let pat_len = table
                            .pattern_lengths()
                            .get(pid as usize)
                            .copied()
                            .unwrap_or(0);
                        let start = end.saturating_sub(pat_len);
                        matches[count] = Match::from_parts(pid, start, end);
                    }
                    count += 1;
                    output_state = self
                        .output_links
                        .get(output_state as usize)
                        .copied()
                        .unwrap_or(0xFFFF_FFFF);
                }
                state = 0;
            } else {
                state = clean_next;
            }
        }
        count.min(matches.len())
    }

    /// Number of DFA states.
    #[must_use]
    pub fn state_count(&self) -> usize {
        self.state_count
    }

    /// Number of patterns recognized.
    #[must_use]
    pub fn pattern_count(&self) -> usize {
        self.pattern_count
    }

    /// Count matches without allocating a match vector.
    #[must_use]
    pub fn scan_count(&self, input: &[u8]) -> usize {
        #[cfg(target_arch = "x86_64")]
        {
            self.code.scan_count(input)
        }

        #[cfg(not(target_arch = "x86_64"))]
        {
            self.scan_count_interpreted(input)
        }
    }

    /// Interpreted fallback for non-x86_64 platforms.
    #[cfg(not(target_arch = "x86_64"))]
    fn scan_count_interpreted(&self, input: &[u8]) -> usize {
        let table = &self.table;
        let mut state = 0u32;
        let mut count = 0usize;

        let mut is_accept = vec![false; table.state_count()];
        for &(s, _) in table.accept_states() {
            is_accept[s as usize] = true;
        }

        for &byte in input {
            let idx = state as usize * table.class_count() + byte as usize;
            let next = table.transitions().get(idx).copied().unwrap_or(0);
            let clean_next = next & 0x7FFF_FFFF;

            if is_accept[clean_next as usize] {
                let mut output_state = clean_next;
                while output_state != 0xFFFF_FFFF {
                    count += 1;
                    output_state = self
                        .output_links
                        .get(output_state as usize)
                        .copied()
                        .unwrap_or(0xFFFF_FFFF);
                }
                state = 0;
            } else {
                state = clean_next;
            }
        }
        count
    }

    /// Find the first match, returning immediately without scanning the rest.
    #[must_use]
    pub fn scan_first(&self, input: &[u8]) -> Option<Match> {
        let mut matches = [Match::from_parts(0, 0, 0); 1];
        if self.scan(input, &mut matches) > 0 {
            Some(matches[0])
        } else {
            None
        }
    }

    /// Check if the input contains any match at all.
    #[must_use]
    pub fn has_match(&self, input: &[u8]) -> bool {
        self.scan_first(input).is_some()
    }

    /// Build a JIT DFA from a set of literal patterns.
    ///
    /// Constructs an Aho-Corasick-like DFA where each pattern has its own
    /// accept state, then compiles it to native code.
    ///
    /// # Errors
    ///
    /// Returns an error if compilation fails.
    pub fn from_patterns(patterns: &[&[u8]]) -> Result<Self> {
        if patterns.is_empty() {
            return Err(Error::EmptyDfa);
        }

        let mut state_count = 1usize;
        let mut trans = vec![[0u32; 256]; 1];
        let mut accepts = Vec::new();
        let mut lengths = vec![0; patterns.len()];

        for (pid, pattern) in patterns.iter().enumerate() {
            if pattern.is_empty() {
                continue;
            }
            let mut current = 0u32;
            for &byte in *pattern {
                let next = trans[current as usize][byte as usize];
                if next == 0 {
                    let new_state = state_count as u32;
                    state_count += 1;
                    trans.push([0u32; 256]);
                    trans[current as usize][byte as usize] = new_state;
                    current = new_state;
                } else {
                    current = next;
                }
            }
            accepts.push((current, pid as u32));
            lengths[pid] = pattern.len() as u32;
        }

        let (fail, accepts, output_links) = Self::build_failure_links(&trans, &accepts);
        let table = Self::build_dense_table(&trans, &fail, accepts, lengths)?;
        Self::compile_with_output_links(&table, &output_links)
    }

    fn build_failure_links(
        trans: &[[u32; 256]],
        accepts: &[(u32, u32)],
    ) -> (Vec<u32>, Vec<(u32, u32)>, Vec<u32>) {
        let state_count = trans.len();
        let mut fail = vec![0u32; state_count];
        let mut queue = std::collections::VecDeque::new();

        let mut acc_state = vec![Vec::new(); state_count];
        for &(state, pid) in accepts {
            acc_state[state as usize].push(pid);
        }

        for byte in 0..=255u8 {
            let next = trans[0][byte as usize];
            if next != 0 {
                fail[next as usize] = 0;
                queue.push_back(next);
            }
        }

        while let Some(state) = queue.pop_front() {
            for byte in 0..=255u8 {
                let next = trans[state as usize][byte as usize];
                if next != 0 {
                    queue.push_back(next);
                    let mut f = fail[state as usize];
                    while f != 0 && trans[f as usize][byte as usize] == 0 {
                        f = fail[f as usize];
                    }
                    let n_fail = trans[f as usize][byte as usize];
                    fail[next as usize] = n_fail;
                }
            }
        }

        // Build output links: for each state with a pattern, link to the nearest
        // ancestor via failure links that also has a pattern.
        let mut output_link = vec![0xFFFF_FFFF; state_count];
        for state in 0..state_count {
            if acc_state[state].is_empty() {
                continue;
            }
            let mut f = fail[state];
            while f != 0 {
                if !acc_state[f as usize].is_empty() {
                    output_link[state] = f;
                    break;
                }
                f = fail[f as usize];
            }
        }

        // Propagate failure-link patterns to states that don't have their own.
        for state in 0..state_count {
            if acc_state[state].is_empty() {
                let mut f = fail[state];
                while f != 0 {
                    if !acc_state[f as usize].is_empty() {
                        let pid = acc_state[f as usize][0];
                        acc_state[state].push(pid);
                        break;
                    }
                    f = fail[f as usize];
                }
            }
        }

        let mut final_accepts = Vec::new();
        for (state, pids) in acc_state.into_iter().enumerate() {
            if !pids.is_empty() {
                final_accepts.push((state as u32, pids[0]));
            }
        }

        (fail, final_accepts, output_link)
    }

    fn build_dense_table(
        trans: &[[u32; 256]],
        fail: &[u32],
        accepts: Vec<(u32, u32)>,
        lengths: Vec<u32>,
    ) -> Result<TransitionTable> {
        let state_count = trans.len();
        let mut table = TransitionTable::new(state_count, 256)?;
        for state in 0..state_count {
            for byte in 0..=255u8 {
                let mut current = state as u32;
                loop {
                    let next = trans[current as usize][byte as usize];
                    if next != 0 || current == 0 {
                        table.set_transition(state, byte, next);
                        break;
                    }
                    current = fail[current as usize];
                }
            }
        }
        for (state, pid) in accepts {
            table.add_accept(state, pid);
        }
        for (pid, len) in lengths.into_iter().enumerate() {
            table.set_pattern_length(pid as u32, len);
        }
        Ok(table)
    }

    /// Build a JIT DFA from a set of regex patterns.
    ///
    /// This constructor uses `regex-automata` to compile the patterns into a
    /// dense DFA, then expands its byte classes into a byte-indexed
    /// [`TransitionTable`] that `dfajit` can execute.
    ///
    /// The current engine records the first pattern ID associated with each
    /// accepting state and preserves fixed offsets by using the literal pattern
    /// length as the match width.
    ///
    /// # Errors
    ///
    /// Returns an error if the regex feature is disabled, the regexes fail to
    /// compile, or no start state can be discovered.
    #[cfg(feature = "regex")]
    pub fn from_regex_patterns(patterns: &[&str]) -> Result<Self> {
        if patterns.is_empty() {
            return Err(Error::EmptyDfa);
        }

        let config = dense::Config::new()
            .match_kind(MatchKind::All)
            .starts_for_each_pattern(true);
        let dfa = dense::Builder::new()
            .configure(config)
            .build_many(patterns)
            .map_err(|error| Error::InvalidTable {
                reason: format!("failed to compile regex patterns with regex-automata: {error}"),
            })?;

        let input = Input::new(&[][..]);
        let start_state = dfa
            .start_state_forward(&input)
            .map_err(|error| Error::InvalidTable {
                reason: format!("failed to compute regex DFA start state: {error}"),
            })?;

        let mut state_ids = Vec::new();
        let mut state_map = HashMap::new();
        let mut queue = VecDeque::new();

        state_map.insert(start_state, 0usize);
        state_ids.push(start_state);
        queue.push_back(start_state);

        while let Some(state) = queue.pop_front() {
            for byte in u8::MIN..=u8::MAX {
                let next = dfa.next_state(state, byte);
                if let std::collections::hash_map::Entry::Vacant(e) = state_map.entry(next) {
                    let next_index = state_ids.len();
                    e.insert(next_index);
                    state_ids.push(next);
                    queue.push_back(next);
                }
            }
        }

        let mut table = TransitionTable::new(state_ids.len(), 256)?;
        for (state_index, &state_id) in state_ids.iter().enumerate() {
            for byte in u8::MIN..=u8::MAX {
                let next = dfa.next_state(state_id, byte);
                let next_index =
                    state_map
                        .get(&next)
                        .copied()
                        .ok_or_else(|| Error::InvalidTable {
                            reason: format!(
                                "regex DFA transition to undiscovered state on byte {byte}"
                            ),
                        })?;
                table.set_transition(state_index, byte, next_index as u32);
            }

            let eoi_state = dfa.next_eoi_state(state_id);
            if dfa.is_match_state(eoi_state) {
                for match_index in 0..dfa.match_len(eoi_state) {
                    let pattern_id = dfa.match_pattern(eoi_state, match_index).as_usize() as u32;
                    if !table
                        .accept_states()
                        .iter()
                        .any(|&(state, pid)| state == state_index as u32 && pid == pattern_id)
                    {
                        table.add_accept(state_index as u32, pattern_id);
                    }
                }
            }
        }

        // Regex patterns have variable lengths; fixed pattern lengths cannot be derived
        // from the source string. Setting length to 0 prevents bogus start-offset underflow.
        for pattern_id in 0..patterns.len() {
            table.set_pattern_length(pattern_id as u32, 0);
        }

        Self::compile(&table)
    }

    /// Build a JIT DFA from a set of regex patterns.
    ///
    /// # Errors
    ///
    /// Returns an error when the crate is built without the `regex` feature.
    #[cfg(not(feature = "regex"))]
    pub fn from_regex_patterns(_patterns: &[&str]) -> Result<Self> {
        Err(Error::InvalidTable {
            reason: "regex support is disabled at compile time. Fix: enable the `regex` feature."
                .to_owned(),
        })
    }
}