libmagic_rs/evaluator/engine/mod.rs
1// Copyright (c) 2025-2026 the libmagic-rs contributors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Core evaluation engine for magic rules.
5//!
6//! This module contains the core recursive evaluation logic for executing magic
7//! rules against file buffers. It is responsible for:
8//! - Evaluating a single rule via [`evaluate_single_rule`] (a thin wrapper
9//! around `evaluate_rules` that delegates one rule through the full
10//! context-aware pipeline)
11//! - Evaluating hierarchical rule sets with context (`evaluate_rules`)
12//! - Providing a convenience wrapper for evaluation with configuration
13//! (`evaluate_rules_with_config`)
14
15use crate::parser::ast::{MagicRule, MetaType, TypeKind};
16use crate::{EvaluationConfig, LibmagicError};
17
18use super::{EvaluationContext, RecursionGuard, RuleMatch, offset, operators, types};
19use log::{debug, warn};
20use std::sync::atomic::{AtomicBool, Ordering};
21
22/// RAII guard that saves the GNU `file` previous-match anchor **and**
23/// `base_offset` on entry and restores both on drop.
24///
25/// `MetaType::Indirect` re-evaluates the root rule list at the resolved
26/// offset. The re-entered rules are top-level-semantic (`base_offset=0`)
27/// and must start with a fresh anchor (the resolved indirect offset).
28/// When `indirect` fires inside a `MetaType::Use` subroutine, the outer
29/// subroutine's non-zero `base_offset` would otherwise leak into the
30/// root re-entry, causing every positive absolute offset in the re-entered
31/// database to be biased by the outer use-site -- producing reads at the
32/// wrong positions. Saving and restoring `base_offset` here prevents that.
33///
34/// Without an RAII wrapper, every early-return path inside the indirect
35/// branch would have to remember to restore both fields manually.
36struct AnchorScope<'a> {
37 context: &'a mut EvaluationContext,
38 saved_anchor: usize,
39 saved_base: usize,
40}
41
42impl<'a> AnchorScope<'a> {
43 /// Save the current anchor and `base_offset`, then seed the context
44 /// with `new_anchor` and reset `base_offset` to 0.
45 fn enter(context: &'a mut EvaluationContext, new_anchor: usize) -> Self {
46 let saved_anchor = context.last_match_end();
47 let saved_base = context.base_offset();
48 context.set_last_match_end(new_anchor);
49 context.set_base_offset(0);
50 Self {
51 context,
52 saved_anchor,
53 saved_base,
54 }
55 }
56
57 /// Access the underlying context for the duration of the guard.
58 fn context(&mut self) -> &mut EvaluationContext {
59 self.context
60 }
61}
62
63impl Drop for AnchorScope<'_> {
64 fn drop(&mut self) {
65 self.context.set_last_match_end(self.saved_anchor);
66 self.context.set_base_offset(self.saved_base);
67 }
68}
69
70/// RAII guard for `MetaType::Use` subroutine dispatch.
71///
72/// Saves `last_match_end` and `base_offset` on entry, seeds the context
73/// with the use-site offset (for both fields so that a subroutine's
74/// `&0` relative offset resolves to the use-site and its positive
75/// absolute offsets bias against the use-site per magic(5)), and
76/// restores both on drop.
77///
78/// This is the safety net for early-return paths inside
79/// `evaluate_use_rule`: a `RecursionGuard::enter` failure or a
80/// `Timeout`/`RecursionLimitExceeded` inside the subroutine body would
81/// otherwise leave the caller's context with corrupted anchor and
82/// base-offset state. The guard's `Drop` impl restores both fields on
83/// every exit path, error or success.
84struct SubroutineScope<'a> {
85 context: &'a mut EvaluationContext,
86 saved_anchor: usize,
87 saved_base: usize,
88}
89
90impl<'a> SubroutineScope<'a> {
91 fn enter(context: &'a mut EvaluationContext, use_site: usize) -> Self {
92 let saved_anchor = context.last_match_end();
93 let saved_base = context.base_offset();
94 context.set_last_match_end(use_site);
95 context.set_base_offset(use_site);
96 Self {
97 context,
98 saved_anchor,
99 saved_base,
100 }
101 }
102
103 fn context(&mut self) -> &mut EvaluationContext {
104 self.context
105 }
106}
107
108impl Drop for SubroutineScope<'_> {
109 fn drop(&mut self) {
110 self.context.set_last_match_end(self.saved_anchor);
111 self.context.set_base_offset(self.saved_base);
112 }
113}
114
115/// Process-local once guard for the "use directive without rule environment"
116/// warning. Ensures we surface the misconfiguration exactly once per process
117/// so low-level programmatic consumers of [`evaluate_rules`] (tests, fuzz
118/// harnesses) that intentionally run without a `MagicDatabase`-attached
119/// environment do not flood the log on every `Use` rule they encounter.
120static USE_WITHOUT_RULE_ENV_WARNED: AtomicBool = AtomicBool::new(false);
121
122/// Process-local once guard for the "`evaluate_rules_with_config` called
123/// with an `indirect` rule but without a `RuleEnvironment`" warning.
124/// Same rationale as `USE_WITHOUT_RULE_ENV_WARNED`: surface the
125/// misconfiguration exactly once per process so a large corpus of
126/// env-less `indirect` rules does not flood the log.
127static INDIRECT_WITHOUT_RULE_ENV_WARNED: AtomicBool = AtomicBool::new(false);
128
129/// Evaluate a single magic rule against a file buffer
130///
131/// This is a thin wrapper around [`evaluate_rules`] that evaluates exactly
132/// one top-level rule (and any of its children) against a buffer, using the
133/// caller-provided [`EvaluationContext`] to enforce timeout, recursion, and
134/// string-size limits. It is a BREAKING API change introduced in pre-1.0:
135/// earlier versions took no context and returned `Option<(usize, Value)>`.
136///
137/// # Arguments
138///
139/// * `rule` - The magic rule to evaluate
140/// * `buffer` - The file buffer to evaluate against
141/// * `context` - Mutable evaluation context that carries the configured
142/// safety limits (timeout, max recursion depth, max string length) and
143/// the GNU `file` previous-match anchor used for relative-offset
144/// resolution. Callers reusing a context across multiple buffers must
145/// call [`EvaluationContext::reset`](crate::evaluator::EvaluationContext::reset)
146/// between calls -- see [`evaluate_rules`] for details.
147///
148/// # Returns
149///
150/// Returns `Ok(Vec<RuleMatch>)` containing the parent match (if the rule
151/// matched) plus any child matches collected recursively. An empty vector
152/// means the rule did not match or was skipped due to a data-dependent
153/// evaluation error (buffer overrun, invalid offset, etc.). Only critical
154/// failures such as `LibmagicError::Timeout` or recursion-limit exhaustion
155/// are returned as `Err`.
156///
157/// # Examples
158///
159/// ```rust
160/// use libmagic_rs::evaluator::{evaluate_single_rule, EvaluationContext};
161/// use libmagic_rs::EvaluationConfig;
162/// use libmagic_rs::parser::ast::{MagicRule, OffsetSpec, TypeKind, Operator, Value};
163///
164/// // Create a rule to check for ELF magic bytes at offset 0
165/// let rule = MagicRule {
166/// offset: OffsetSpec::Absolute(0),
167/// typ: TypeKind::Byte { signed: true },
168/// op: Operator::Equal,
169/// value: Value::Uint(0x7f),
170/// message: "ELF magic".to_string(),
171/// children: vec![],
172/// level: 0,
173/// strength_modifier: None,
174/// value_transform: None,
175/// };
176///
177/// let mut context = EvaluationContext::new(EvaluationConfig::default());
178/// let elf_buffer = &[0x7f, 0x45, 0x4c, 0x46]; // ELF magic bytes
179/// let matches = evaluate_single_rule(&rule, elf_buffer, &mut context).unwrap();
180/// assert_eq!(matches.len(), 1); // Should match
181///
182/// context.reset();
183/// let non_elf_buffer = &[0x50, 0x4b, 0x03, 0x04]; // ZIP magic bytes
184/// let matches = evaluate_single_rule(&rule, non_elf_buffer, &mut context).unwrap();
185/// assert!(matches.is_empty()); // Should not match
186/// ```
187///
188/// # Errors
189///
190/// * `LibmagicError::Timeout` - If evaluation exceeds the configured timeout
191/// * `LibmagicError::EvaluationError` - For critical failures such as the
192/// recursion limit being exceeded. Data-dependent errors (buffer overrun,
193/// invalid offset, malformed pstring length) are handled gracefully by
194/// [`evaluate_rules`] and surface as an empty match vector rather than
195/// an error.
196pub fn evaluate_single_rule(
197 rule: &MagicRule,
198 buffer: &[u8],
199 context: &mut EvaluationContext,
200) -> Result<Vec<RuleMatch>, LibmagicError> {
201 evaluate_rules(std::slice::from_ref(rule), buffer, context)
202}
203
204/// Internal: evaluate a single rule against a buffer, supplying an explicit
205/// anchor for relative-offset resolution.
206///
207/// This is the worker behind both [`evaluate_single_rule`] (which defaults
208/// the anchor to 0) and [`evaluate_rules`] (which threads the anchor from
209/// `EvaluationContext::last_match_end()`).
210fn evaluate_single_rule_with_anchor(
211 rule: &MagicRule,
212 buffer: &[u8],
213 last_match_end: usize,
214 base_offset: usize,
215) -> Result<Option<(usize, crate::parser::ast::Value)>, LibmagicError> {
216 use crate::parser::ast::TypeKind;
217
218 // Step 1: Resolve the offset specification to an absolute position.
219 // `base_offset` is non-zero only inside a `MetaType::Use` subroutine
220 // body, where it biases positive absolute offsets to the use-site.
221 let absolute_offset =
222 offset::resolve_offset_with_base(&rule.offset, buffer, last_match_end, base_offset)?;
223
224 // Step 2 & 3: Dispatch on type category. Pattern-bearing types
225 // (Regex, Search) take a different path from fixed-width types
226 // because the rule's `value` operand is the *pattern*, not an
227 // expected matched value. Running those through `apply_operator`
228 // would compare matched text ("123") against the pattern literal
229 // ("[0-9]+") and produce false negatives on any regex with
230 // metacharacters.
231 //
232 // Meta-type directives (`default`, `clear`, `name`, `use`,
233 // `indirect`, `offset`) are dispatched by `evaluate_rules` at the
234 // outer loop level (not here) -- this single-rule helper is only
235 // invoked for non-meta rules. Short-circuiting the Meta arms here
236 // with `Ok(None)` is defense-in-depth for programmatic callers
237 // (property tests, fuzz harnesses) that hand-build a Meta rule
238 // and feed it directly to `evaluate_single_rule`; without the
239 // guard, the value/pattern paths would surface
240 // `TypeReadError::UnsupportedType`.
241 let (matched, read_value) = match &rule.typ {
242 TypeKind::Meta(MetaType::Name(name)) => {
243 // `Name` rules are normally hoisted into the name table at
244 // parse time and should not reach the evaluator. Programmatic
245 // consumers (e.g. fuzz harnesses, property tests) can still
246 // construct them directly; treat that as a no-op rather than
247 // a hard failure so the evaluator-never-panics invariant is
248 // preserved.
249 debug!(
250 "Name rule '{name}' reached evaluator (likely bypassed name-table extraction); treating as no-op"
251 );
252 return Ok(None);
253 }
254 TypeKind::Meta(MetaType::Use(_)) => {
255 // `Use` is dispatched inline by `evaluate_rules` so it can
256 // push the subroutine's matches into the caller's match
257 // vector. Reaching this arm means the rule went through the
258 // single-rule path (e.g. via `evaluate_single_rule`) which
259 // lacks that wiring; treat it as a silent no-op.
260 return Ok(None);
261 }
262 TypeKind::Meta(_) => return Ok(None),
263 TypeKind::Regex { .. } | TypeKind::Search { .. } => {
264 evaluate_pattern_rule(rule, buffer, absolute_offset)?
265 }
266 _ => evaluate_value_rule(rule, buffer, absolute_offset)?,
267 };
268 Ok(matched.then_some((absolute_offset, read_value)))
269}
270
271/// Evaluate a `TypeKind::Meta(MetaType::Use(name))` rule inline.
272///
273/// Looks up `name` in the context's rule environment, temporarily sets the
274/// GNU `file` previous-match anchor to the resolved offset, and recursively
275/// evaluates the subroutine's rules against `buffer`. Any matches produced
276/// by the subroutine are returned in document order and are intended to be
277/// pushed into the caller's match vector *before* the synthetic `Use` match
278/// itself (matching GNU `file` behavior where a `use` site is replaced by
279/// its expansion in the output).
280///
281/// Returns `Ok((Some(absolute_offset), matches))` on a successful resolution
282/// (even if the subroutine produced no matches), or `Ok((None, vec![]))`
283/// when:
284/// - the context has no rule environment attached (programmatic consumers
285/// bypassing `MagicDatabase`)
286/// - the referenced name is not in the table (logged at warn level)
287///
288/// Recursion-limit propagation is handled via [`RecursionGuard`] so that a
289/// subroutine calling `use` on itself triggers `RecursionLimitExceeded`
290/// instead of a stack overflow.
291fn evaluate_use_rule(
292 rule: &MagicRule,
293 name: &str,
294 buffer: &[u8],
295 context: &mut EvaluationContext,
296) -> Result<(Option<usize>, Vec<RuleMatch>), LibmagicError> {
297 let Some(env) = context.rule_env() else {
298 // Surface the misconfiguration once per process at warn! level so
299 // it is visible in default logging, then gate subsequent hits so a
300 // magic file with many `use` directives does not flood the log.
301 // Use `Ordering::Relaxed`: the flag is an idempotent diagnostic
302 // latch, not a synchronization primitive guarding other state.
303 if USE_WITHOUT_RULE_ENV_WARNED.swap(true, Ordering::Relaxed) {
304 debug!("use directive '{name}' evaluated without a rule environment; no-op");
305 } else {
306 warn!(
307 "use directive '{name}' evaluated without a rule environment; treating as no-op (subsequent occurrences suppressed)"
308 );
309 }
310 return Ok((None, Vec::new()));
311 };
312
313 let Some(subroutine_rules) = env.name_table.get(name) else {
314 warn!("use directive references unknown name '{name}'");
315 return Ok((None, Vec::new()));
316 };
317 // `NameTable::get` returns an `Arc<[MagicRule]>`, so this clone is a
318 // reference-count increment rather than a deep copy of the rule tree.
319 // The Arc is cloned here to release the immutable borrow of `context`
320 // (via `env`) before we mutably borrow the context below.
321
322 // Resolve the use-site offset under the *caller's* base, not the
323 // subroutine's -- the use rule itself is in the caller's scope.
324 let absolute_offset = offset::resolve_offset_with_base(
325 &rule.offset,
326 buffer,
327 context.last_match_end(),
328 context.base_offset(),
329 )?;
330
331 // `SubroutineScope` seeds `last_match_end` and `base_offset` with
332 // the use-site offset and restores both on drop. This is the
333 // safety net for early-return paths below -- if
334 // `RecursionGuard::enter` or the inner `evaluate_rules` returns
335 // `Err(Timeout)` / `Err(RecursionLimitExceeded)`, the `?` unwinds
336 // through the guard's `Drop` impl and the caller's context
337 // returns to its pre-use state. Without the RAII wrapper a manual
338 // save/restore pair would be bypassed on every error path.
339 // Capture both the subroutine's matches AND the terminal anchor
340 // where the subroutine left `last_match_end`. The terminal anchor
341 // is what GNU `file`-compatible inlining semantics require: sibling
342 // rules after the `use` site must resolve `&N` against the position
343 // the subroutine reached, not the use-site offset. Reading the
344 // anchor INSIDE the scope (before Drop restores the caller's value)
345 // preserves it for the caller.
346 let (subroutine_matches, terminal_anchor) = {
347 let mut scope = SubroutineScope::enter(context, absolute_offset);
348 let mut guard = RecursionGuard::enter(scope.context())?;
349 let matches = evaluate_rules(&subroutine_rules, buffer, guard.context())?;
350 let terminal = guard.context().last_match_end();
351 (matches, terminal)
352 };
353
354 Ok((Some(terminal_anchor), subroutine_matches))
355}
356
357/// Evaluate a pattern-bearing rule (`TypeKind::Regex` / `TypeKind::Search`).
358///
359/// `read_pattern_match` returns `Some(value)` on a successful match
360/// (possibly zero-width, e.g., `a*`) and `None` on a genuine miss; the
361/// engine translates those directly into `Equal`/`NotEqual`. Any other
362/// operator on a pattern-bearing type is a magic-file semantic bug and
363/// surfaces as [`TypeReadError::UnsupportedType`] -- the earlier
364/// fallthrough to `apply_operator` masked this by producing nonsense
365/// ordering comparisons against the pattern source text.
366///
367/// On a miss we return `Value::String(String::new())` as a display
368/// placeholder; the engine has already decided `matched = false` by
369/// then, so the placeholder only affects display and
370/// `bytes_consumed_with_pattern` (which re-derives the match position
371/// from the pattern, not this value).
372fn evaluate_pattern_rule(
373 rule: &MagicRule,
374 buffer: &[u8],
375 absolute_offset: usize,
376) -> Result<(bool, crate::parser::ast::Value), LibmagicError> {
377 let match_outcome =
378 types::read_pattern_match(buffer, absolute_offset, &rule.typ, Some(&rule.value))
379 .map_err(|e| LibmagicError::EvaluationError(e.into()))?;
380 let pattern_found = match_outcome.is_some();
381 let matched = match &rule.op {
382 crate::parser::ast::Operator::Equal => pattern_found,
383 crate::parser::ast::Operator::NotEqual => !pattern_found,
384 other => {
385 return Err(LibmagicError::EvaluationError(
386 types::TypeReadError::UnsupportedType {
387 type_name: format!(
388 "operator {other:?} is not supported for pattern-bearing type {:?}; only Equal (=) and NotEqual (!=) are allowed",
389 rule.typ
390 ),
391 }
392 .into(),
393 ));
394 }
395 };
396 let value = match_outcome.unwrap_or_else(|| crate::parser::ast::Value::String(String::new()));
397 Ok((matched, value))
398}
399
400/// Evaluate a value-based rule (all non-pattern-bearing `TypeKind` variants).
401///
402/// Reads the typed value at `absolute_offset`, coerces the rule's
403/// expected value to the target type's signedness/width (zero-copy via
404/// `Cow::Borrowed` on the hot path), and applies the operator.
405/// `BitwiseNot` needs type-aware width masking so the complement is
406/// computed at the type's natural width (e.g. byte `NOT 0x00 = 0xFF`,
407/// not `u64::MAX`).
408fn evaluate_value_rule(
409 rule: &MagicRule,
410 buffer: &[u8],
411 absolute_offset: usize,
412) -> Result<(bool, crate::parser::ast::Value), LibmagicError> {
413 let read_value =
414 types::read_typed_value_with_pattern(buffer, absolute_offset, &rule.typ, Some(&rule.value))
415 .map_err(|e| LibmagicError::EvaluationError(e.into()))?;
416
417 // Apply any pre-comparison value transform (`type+N`/`type-N`/`type*N`/
418 // `type/N`/`type%N`/`type|N`/`type^N`). The transform runs on the read
419 // value before the comparison operator and before printf-style format
420 // substitution, so `%d` in the message renders the post-transform
421 // number. `&MASK` is *not* handled here -- it lives at the operator
422 // layer via `Operator::BitwiseAndMask`.
423 let transformed_value = match rule.value_transform {
424 None => read_value,
425 Some(t) => operators::apply_value_transform(&read_value, t)
426 .map_err(LibmagicError::EvaluationError)?,
427 };
428
429 let expected_value = types::coerce_value_to_type(&rule.value, &rule.typ);
430 let expected_ref: &crate::parser::ast::Value = expected_value.as_ref();
431
432 let matched = match &rule.op {
433 crate::parser::ast::Operator::BitwiseNot => operators::apply_bitwise_not_with_width(
434 &transformed_value,
435 expected_ref,
436 rule.typ.bit_width(),
437 ),
438 op => operators::apply_operator(op, &transformed_value, expected_ref),
439 };
440 Ok((matched, transformed_value))
441}
442
443/// Evaluate a rule's children under the standard recursion-guard/graceful-skip discipline.
444///
445/// This helper centralises the `RecursionGuard` + `evaluate_rules` + error-dispatch
446/// pattern that is identical across the `Default`, `Indirect`, `Offset`, and `Use`
447/// meta-type arms in [`evaluate_rules`]. Extracting it prevents the four copies
448/// from drifting apart during future maintenance.
449///
450/// # Behaviour
451///
452/// * If `rule.children` is empty the function is a no-op (returns `Ok(())`).
453/// * Child matches are appended to `matches` in document order.
454/// * `LibmagicError::Timeout` and `LibmagicError::EvaluationError(RecursionLimitExceeded)`
455/// propagate immediately as `Err` so the caller can bail out.
456/// * Data-dependent errors (`BufferOverrun`, `InvalidOffset`,
457/// `TypeReadError::BufferOverrun`, `TypeReadError::InvalidPStringLength`,
458/// `IoError`) are logged at `warn!` and swallowed; the parent match
459/// already in `matches` is left intact. This mirrors the defensive
460/// comment in each arm: the inner `evaluate_rules` already catches and
461/// logs individual child failures, so this arm only fires if that
462/// strategy changes.
463///
464/// # Arguments
465///
466/// * `rule` – The parent rule whose children will be evaluated.
467/// * `rule_kind` – A short label for the rule kind used in the `warn!`
468/// message (e.g. `"default"`, `"indirect"`, `"offset"`, `"use"`).
469/// * `buffer` – The file buffer passed to the recursive call.
470/// * `context` – Mutable evaluation context; the recursion depth is
471/// incremented on entry and decremented on drop via [`RecursionGuard`].
472/// * `matches` – Output vector; child matches are appended here.
473fn evaluate_children_or_warn(
474 rule: &MagicRule,
475 rule_kind: &str,
476 buffer: &[u8],
477 context: &mut EvaluationContext,
478 matches: &mut Vec<RuleMatch>,
479) -> Result<(), LibmagicError> {
480 if rule.children.is_empty() {
481 return Ok(());
482 }
483 let mut guard = RecursionGuard::enter(context)?;
484 match evaluate_rules(&rule.children, buffer, guard.context()) {
485 Ok(child_matches) => {
486 matches.extend(child_matches);
487 }
488 Err(LibmagicError::Timeout { timeout_ms }) => {
489 return Err(LibmagicError::Timeout { timeout_ms });
490 }
491 // `RecursionLimitExceeded` is listed explicitly (rather than
492 // relying on the catch-all below) so a future maintainer adding
493 // another swallowed variant cannot accidentally swallow it.
494 // Both this arm and the catch-all intentionally propagate via
495 // `return Err(e)`; `match_same_arms` is suppressed because the
496 // explicit arm's purpose is documentation and future-proofing,
497 // not different behavior. See GOTCHAS S13 for the recursion-
498 // depth guard contract.
499 #[allow(clippy::match_same_arms)]
500 Err(
501 e @ LibmagicError::EvaluationError(
502 crate::error::EvaluationError::RecursionLimitExceeded { .. },
503 ),
504 ) => return Err(e),
505 Err(
506 e @ (LibmagicError::EvaluationError(
507 crate::error::EvaluationError::BufferOverrun { .. }
508 | crate::error::EvaluationError::InvalidOffset { .. }
509 | crate::error::EvaluationError::InvalidValueTransform { .. }
510 | crate::error::EvaluationError::TypeReadError(
511 crate::evaluator::types::TypeReadError::BufferOverrun { .. }
512 | crate::evaluator::types::TypeReadError::InvalidPStringLength { .. },
513 ),
514 )
515 | LibmagicError::IoError(_)),
516 ) => {
517 warn!(
518 "Discarding child evaluation under {} rule '{}' due to unexpected error: {} -- parent match is still emitted",
519 rule_kind, rule.message, e
520 );
521 }
522 Err(e) => return Err(e),
523 }
524 Ok(())
525}
526
527/// Evaluate a list of magic rules against a file buffer with hierarchical processing
528///
529/// This function implements the core hierarchical rule evaluation algorithm with graceful
530/// error handling:
531/// 1. Evaluates each top-level rule in sequence
532/// 2. If a parent rule matches, evaluates its child rules for refinement
533/// 3. Collects all matches or stops at first match based on configuration
534/// 4. Maintains evaluation context for recursion limits and state
535/// 5. Implements graceful degradation by skipping problematic rules and continuing evaluation
536///
537/// The hierarchical evaluation follows these principles:
538/// - Parent rules must match before children are evaluated
539/// - Child rules provide refinement and additional detail
540/// - Evaluation can stop at first match or continue for all matches
541/// - Recursion depth is limited to prevent infinite loops
542/// - Problematic rules are skipped to allow evaluation to continue
543///
544/// # Arguments
545///
546/// * `rules` - The list of magic rules to evaluate
547/// * `buffer` - The file buffer to evaluate against
548/// * `context` - Mutable evaluation context for state management. **Callers
549/// reusing a context across multiple buffers must call
550/// [`EvaluationContext::reset`](crate::evaluator::EvaluationContext::reset)
551/// between calls** -- the GNU `file` previous-match anchor and the
552/// recursion-depth counter both advance during evaluation and would
553/// otherwise leak across buffers. The same applies when this function
554/// returns `Err` mid-evaluation (e.g., `LibmagicError::Timeout` or
555/// `RecursionLimitExceeded`): both the anchor and (potentially) the
556/// recursion depth are left in a partially-advanced state, and a retry
557/// on the same context without `reset()` will resolve relative offsets
558/// against the stale anchor and apply the wrong recursion budget.
559/// [`evaluate_rules_with_config`] always builds a fresh context and is the
560/// safer choice when context reuse isn't required.
561///
562/// # Returns
563///
564/// Returns `Ok(Vec<RuleMatch>)` containing all matches found. Errors in individual rules
565/// are skipped to allow evaluation to continue. Only returns `Err(LibmagicError)`
566/// for critical failures like timeout or recursion limit exceeded.
567///
568/// # Examples
569///
570/// ```rust
571/// use libmagic_rs::evaluator::{evaluate_rules, EvaluationContext, RuleMatch};
572/// use libmagic_rs::parser::ast::{MagicRule, OffsetSpec, TypeKind, Operator, Value};
573/// use libmagic_rs::EvaluationConfig;
574///
575/// // Create a hierarchical rule set for ELF files
576/// let parent_rule = MagicRule {
577/// offset: OffsetSpec::Absolute(0),
578/// typ: TypeKind::Byte { signed: true },
579/// op: Operator::Equal,
580/// value: Value::Uint(0x7f),
581/// message: "ELF".to_string(),
582/// children: vec![
583/// MagicRule {
584/// offset: OffsetSpec::Absolute(4),
585/// typ: TypeKind::Byte { signed: true },
586/// op: Operator::Equal,
587/// value: Value::Uint(2),
588/// message: "64-bit".to_string(),
589/// children: vec![],
590/// level: 1,
591/// strength_modifier: None,
592/// value_transform: None,
593/// }
594/// ],
595/// level: 0,
596/// strength_modifier: None,
597/// value_transform: None,
598/// };
599///
600/// let rules = vec![parent_rule];
601/// let buffer = &[0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01]; // ELF64 header
602/// let config = EvaluationConfig::default();
603/// let mut context = EvaluationContext::new(config);
604///
605/// let matches = evaluate_rules(&rules, buffer, &mut context).unwrap();
606/// assert_eq!(matches.len(), 2); // Parent and child should both match
607/// ```
608///
609/// # Errors
610///
611/// * `LibmagicError::Timeout` - If evaluation exceeds configured timeout
612/// * `LibmagicError::EvaluationError` - Only for critical failures like recursion limit exceeded
613///
614/// Individual rule evaluation errors are handled gracefully and do not stop the overall evaluation.
615#[allow(clippy::too_many_lines)]
616pub fn evaluate_rules(
617 rules: &[MagicRule],
618 buffer: &[u8],
619 context: &mut EvaluationContext,
620) -> Result<Vec<RuleMatch>, LibmagicError> {
621 let mut matches = Vec::with_capacity(8);
622 let start_time = std::time::Instant::now();
623 let mut rule_count = 0u32;
624
625 // Per-level "did any sibling match yet?" flag for `default`/`clear`
626 // dispatch. Each recursive descent gets its own fresh flag, so child
627 // sibling chains track their own state independently of the parent.
628 let mut sibling_matched: bool = false;
629
630 // Per-level entry anchor: captured at the start of this sibling list's
631 // evaluation. For CHILD sibling lists (recursion_depth > 0), the
632 // GNU `file`/libmagic previous-match anchor is reset to this value
633 // between sibling iterations so that `&N` offsets on continuation
634 // siblings resolve against the parent-level anchor, not against
635 // whatever the *previous sibling* left the anchor at. This matches
636 // libmagic's continuation-level model (`ms->c.li[cont_level]`)
637 // where each level tracks its own anchor; a sibling at level L does
638 // not inherit the post-match anchor of another sibling at level L.
639 //
640 // TOP-LEVEL siblings (recursion_depth == 0) are independent
641 // classification attempts -- each top-level rule intentionally sees
642 // the anchor advance that prior top-level rules produced (see
643 // GOTCHAS S3.8 and the `relative_anchor_can_decrease_...`
644 // integration test). Gate the reset on recursion_depth to preserve
645 // that documented discipline while still fixing the continuation-
646 // sibling behavior that the GNU `file` `searchbug.magic` fixture
647 // relies on.
648 //
649 // Recursing into a matched rule's children still carries forward the
650 // post-match anchor (via the current value of `last_match_end()` at
651 // the point of recursion), so child sibling lists see their parent's
652 // resolved position as their own entry anchor.
653 //
654 // INDIRECT RE-ENTRY exception: `MetaType::Indirect` dispatches its
655 // sub-evaluation via `RecursionGuard::enter` (to bound the recursion
656 // cycle), which forces `recursion_depth > 0`. But an indirect
657 // re-entry semantically evaluates the root rule list with TOP-LEVEL
658 // sibling semantics -- each rule is an independent classification
659 // attempt against the re-entered sub-buffer, NOT a continuation
660 // list. The indirect dispatch sets `context.set_indirect_reentry(true)`
661 // just before this call; `take_indirect_reentry()` consumes it at
662 // entry so only this iteration treats siblings as top-level.
663 // Children of matched rules inside the re-entry still see the flag
664 // as false (consumed) and correctly fall back to continuation
665 // semantics via `recursion_depth > 0`.
666 let entry_anchor = context.last_match_end();
667 let is_indirect_reentry = context.take_indirect_reentry();
668 let is_child_sibling_list = context.recursion_depth() > 0 && !is_indirect_reentry;
669
670 // Entry-point timeout check: ensures every recursive descent is bounded
671 // and that evaluations of small rule sets (< 16 rules) are still guarded.
672 // Without this, the periodic every-16-rules check below never fires for
673 // flat rule lists with fewer than 16 rules, and recursion into children
674 // also restarts `rule_count` at 0.
675 if let Some(timeout_ms) = context.timeout_ms()
676 && start_time.elapsed().as_millis() >= u128::from(timeout_ms)
677 {
678 return Err(LibmagicError::Timeout { timeout_ms });
679 }
680
681 for rule in rules {
682 // For continuation siblings (child recursion), reset the
683 // previous-match anchor to the entry anchor so `&N` offsets
684 // resolve against the parent-level position. Top-level
685 // siblings (depth 0) keep the chaining behavior documented in
686 // GOTCHAS S3.8. See the `entry_anchor` comment above.
687 if is_child_sibling_list {
688 context.set_last_match_end(entry_anchor);
689 }
690
691 // Check timeout periodically (every 16 rules) to reduce syscall overhead
692 rule_count = rule_count.wrapping_add(1);
693 if rule_count.trailing_zeros() >= 4
694 && let Some(timeout_ms) = context.timeout_ms()
695 && start_time.elapsed().as_millis() >= u128::from(timeout_ms)
696 {
697 return Err(LibmagicError::Timeout { timeout_ms });
698 }
699
700 // `Clear` resets the per-level "sibling matched" flag so a
701 // subsequent `default` sibling can fire even if an earlier
702 // sibling matched. It does not produce a match, evaluate
703 // children, or advance the anchor.
704 if let TypeKind::Meta(MetaType::Clear) = &rule.typ {
705 sibling_matched = false;
706 continue;
707 }
708
709 // `Default` fires only when no earlier sibling at this level has
710 // matched yet. The anchor is intentionally not advanced -- the
711 // directive does not consume bytes -- but its children are
712 // evaluated and the per-level "sibling matched" flag is set so
713 // any later `default` sibling at the same level is suppressed.
714 if let TypeKind::Meta(MetaType::Default) = &rule.typ {
715 if !sibling_matched {
716 let matches_before = matches.len();
717
718 let match_result = RuleMatch {
719 message: rule.message.clone(),
720 offset: context.last_match_end(),
721 level: rule.level,
722 value: crate::parser::ast::Value::Uint(0),
723 type_kind: rule.typ.clone(),
724 confidence: RuleMatch::calculate_confidence(rule.level),
725 };
726 matches.push(match_result);
727
728 // `default` is treated as a successful match at this
729 // level, so its children are evaluated under the same
730 // recursion-guard pattern as every other successful rule.
731 evaluate_children_or_warn(rule, "default", buffer, context, &mut matches)?;
732
733 sibling_matched = true;
734
735 if matches.len() > matches_before && context.should_stop_at_first_match() {
736 break;
737 }
738 }
739 continue;
740 }
741
742 // `Indirect` re-evaluates the root rule list at the resolved
743 // offset, mirroring libmagic's indirect-type semantics. The
744 // sub-evaluation runs against `buffer[absolute_offset..]` with a
745 // fresh anchor (0) so relative offsets inside the root rules
746 // resolve correctly; the caller's anchor is restored on exit
747 // via `AnchorScope`. Without an attached `RuleEnvironment`
748 // (programmatic consumers bypassing `MagicDatabase`) the
749 // directive is a silent no-op.
750 if let TypeKind::Meta(MetaType::Indirect) = &rule.typ {
751 // Resolve the offset first so a malformed offset surfaces
752 // as a graceful skip rather than a hard error.
753 let absolute_offset = match offset::resolve_offset_with_base(
754 &rule.offset,
755 buffer,
756 context.last_match_end(),
757 context.base_offset(),
758 ) {
759 Ok(o) => o,
760 Err(
761 e @ LibmagicError::EvaluationError(
762 crate::error::EvaluationError::BufferOverrun { .. }
763 | crate::error::EvaluationError::InvalidOffset { .. },
764 ),
765 ) => {
766 debug!("Skipping indirect rule '{}': {}", rule.message, e);
767 continue;
768 }
769 Err(e) => return Err(e),
770 };
771
772 // Pull the root rules out of the rule environment. Without
773 // an environment there is nothing to re-enter, so this is a
774 // silent no-op (matching the `Use`-without-env behavior).
775 //
776 // We use `debug!` rather than `debug_assert!` here because
777 // property tests (`prop_arbitrary_rule_evaluation_never_panics`)
778 // synthesize arbitrary `TypeKind::Meta(MetaType::Indirect)`
779 // rules and run them without attaching a `RuleEnvironment`;
780 // a panic on this path would break the never-panics invariant.
781 // See GOTCHAS S2.1 for the same rationale on the leaked-Name arm.
782 let Some(root_rules) = context.rule_env().map(|e| e.root_rules.clone()) else {
783 debug!(
784 "indirect rule '{}' evaluated without a rule environment; treating as no-op",
785 rule.message
786 );
787 continue;
788 };
789
790 // Bounds-check before slicing. An indirect offset past the
791 // end of the buffer is a data-dependent skip, not an error.
792 let Some(sub_buffer) = buffer.get(absolute_offset..) else {
793 debug!(
794 "Skipping indirect rule '{}': offset {} past buffer end ({} bytes)",
795 rule.message,
796 absolute_offset,
797 buffer.len()
798 );
799 continue;
800 };
801
802 let matches_before = matches.len();
803
804 // Advance the GNU `file` previous-match anchor to the indirect's
805 // resolved offset and emit a `RuleMatch` for the indirect rule
806 // itself BEFORE descending into the root re-entry or children.
807 // This matches the shared successful-match flow used by every
808 // other rule kind: advance anchor first, record the match, then
809 // recurse. Without this, sibling rules of the `indirect` resolve
810 // their relative offsets against the stale anchor and the
811 // directive's own `message` never surfaces in the output.
812 context.set_last_match_end(absolute_offset);
813
814 let indirect_match = RuleMatch {
815 message: rule.message.clone(),
816 offset: absolute_offset,
817 level: rule.level,
818 value: crate::parser::ast::Value::String("indirect".to_string()),
819 type_kind: rule.typ.clone(),
820 confidence: RuleMatch::calculate_confidence(rule.level),
821 };
822 matches.push(indirect_match);
823
824 // Indirect counts as a match for `sibling_matched` regardless of
825 // whether the sub-evaluation produced any matches -- the directive
826 // itself successfully dispatched.
827 sibling_matched = true;
828
829 // Recursion guard + anchor scope: nested indirect / use cycles
830 // surface as `RecursionLimitExceeded` instead of a stack overflow,
831 // and the caller's anchor is restored on every exit path.
832 //
833 // Mark the upcoming `evaluate_rules` call as a top-level
834 // re-entry (consumed at entry) so sibling anchor-reset
835 // semantics do NOT fire -- root rules in the re-entered
836 // database chain their anchors across siblings like any
837 // other top-level evaluation.
838 {
839 let mut guard = RecursionGuard::enter(context)?;
840 let mut anchor_scope = AnchorScope::enter(guard.context(), 0);
841 anchor_scope.context().set_indirect_reentry(true);
842 match evaluate_rules(&root_rules, sub_buffer, anchor_scope.context()) {
843 Ok(sub_matches) => {
844 matches.extend(sub_matches);
845 }
846 Err(LibmagicError::Timeout { timeout_ms }) => {
847 return Err(LibmagicError::Timeout { timeout_ms });
848 }
849 Err(e) => return Err(e),
850 }
851 // anchor_scope drops here, restoring the saved anchor
852 // (which is now `absolute_offset`, set above before the
853 // scope was entered).
854 // guard drops next, decrementing the recursion depth.
855 }
856
857 // Evaluate the indirect rule's own children under the same
858 // recursion-guard pattern used by every other successful rule.
859 evaluate_children_or_warn(rule, "indirect", buffer, context, &mut matches)?;
860
861 if matches.len() > matches_before && context.should_stop_at_first_match() {
862 break;
863 }
864 continue;
865 }
866
867 // `Offset` reports the resolved file offset as the rule's read
868 // value, matching GNU `file`'s `FILE_OFFSET` semantics: the match
869 // emits a value-bearing `RuleMatch` whose `value` is the absolute
870 // position, which downstream message formatting substitutes into
871 // `%lld` / `%d` specifiers via `output::format::format_magic_message`.
872 //
873 // Per magic(5) the only legal operator is `x` (AnyValue); any
874 // other operator is a magic-file semantic error. Matching the
875 // evaluator's graceful-skip discipline, we `debug!`-log and skip
876 // rather than erroring -- a rogue rule shouldn't poison the rest
877 // of the evaluation.
878 if let TypeKind::Meta(MetaType::Offset) = &rule.typ {
879 if !matches!(rule.op, crate::parser::ast::Operator::AnyValue) {
880 debug!(
881 "offset rule '{}': non-`x` operator {:?} not supported; skipping",
882 rule.message, rule.op
883 );
884 continue;
885 }
886
887 // Resolve the offset first so a malformed offset surfaces as
888 // a graceful skip rather than a hard error. Mirrors the
889 // `Indirect` dispatch above.
890 let absolute_offset = match offset::resolve_offset_with_base(
891 &rule.offset,
892 buffer,
893 context.last_match_end(),
894 context.base_offset(),
895 ) {
896 Ok(o) => o,
897 Err(
898 e @ LibmagicError::EvaluationError(
899 crate::error::EvaluationError::BufferOverrun { .. }
900 | crate::error::EvaluationError::InvalidOffset { .. },
901 ),
902 ) => {
903 debug!("Skipping offset rule '{}': {}", rule.message, e);
904 continue;
905 }
906 Err(e) => return Err(e),
907 };
908
909 let matches_before = matches.len();
910
911 // Advance the anchor BEFORE emitting the match so sibling
912 // rules resolve their relative offsets against the offset
913 // directive's resolved position. Same discipline as
914 // `Indirect` and every other value-bearing rule.
915 context.set_last_match_end(absolute_offset);
916
917 let offset_match = RuleMatch {
918 message: rule.message.clone(),
919 offset: absolute_offset,
920 level: rule.level,
921 value: crate::parser::ast::Value::Uint(absolute_offset as u64),
922 type_kind: rule.typ.clone(),
923 confidence: RuleMatch::calculate_confidence(rule.level),
924 };
925 matches.push(offset_match);
926
927 sibling_matched = true;
928
929 // Evaluate children under the recursion-guard pattern used
930 // by every other successful rule.
931 evaluate_children_or_warn(rule, "offset", buffer, context, &mut matches)?;
932
933 if matches.len() > matches_before && context.should_stop_at_first_match() {
934 break;
935 }
936 continue;
937 }
938
939 // `Use` is handled inline so the subroutine's matches can be
940 // spliced into the caller's match vector in document order.
941 // Routing this through `evaluate_single_rule_with_anchor` would
942 // force the helper to return a `Vec<RuleMatch>`, which would
943 // reshape the single-rule return type for every other variant.
944 //
945 // On a successful use path we must also descend into the rule's
946 // own children, matching the flow of every other successful rule
947 // kind. libmagic chains like `>>0 use part2` often carry
948 // continuation rules (siblings and descendants of the `use` site)
949 // that depend on the anchor the subroutine left behind; skipping
950 // them produces user-visible false negatives.
951 if let TypeKind::Meta(MetaType::Use(name)) = &rule.typ {
952 let matches_before = matches.len();
953 let use_resolved = match evaluate_use_rule(rule, name, buffer, context) {
954 Ok((Some(terminal_anchor), subroutine_matches)) => {
955 matches.extend(subroutine_matches);
956
957 // A `use` rule does not produce a surface
958 // `RuleMatch` itself -- the subroutine's rules
959 // carry the visible messages. Advance the
960 // caller's anchor to the subroutine's TERMINAL
961 // anchor (where the subroutine left `last_match_end`),
962 // not the use-site offset. This makes `use`
963 // behave like inlining the subroutine: sibling
964 // rules after the `use` see `&N` resolve against
965 // the subroutine's final match position.
966 context.set_last_match_end(terminal_anchor);
967 true
968 }
969 Ok((None, _)) => {
970 // No environment, or name not found -- silent no-op.
971 false
972 }
973 Err(
974 e @ LibmagicError::EvaluationError(
975 crate::error::EvaluationError::BufferOverrun { .. }
976 | crate::error::EvaluationError::InvalidOffset { .. },
977 ),
978 ) => {
979 debug!("Skipping use rule '{name}': {e}");
980 false
981 }
982 Err(e) => return Err(e),
983 };
984
985 // Evaluate the use rule's own children exactly like any other
986 // successful rule. Subroutine matches are already appended
987 // above, so children are spliced in after them to preserve
988 // document order. The recursion guard mirrors the non-`Use`
989 // path so a `use`-site chain cannot blow past the configured
990 // recursion limit.
991 if use_resolved {
992 evaluate_children_or_warn(rule, "use", buffer, context, &mut matches)?;
993 }
994
995 // A successful `use` site is treated as a sibling match for
996 // `default`/`clear` dispatch purposes -- subsequent `default`
997 // siblings should not fire if the subroutine resolved.
998 if use_resolved {
999 sibling_matched = true;
1000 }
1001
1002 // Apply stop-at-first-match with the same semantics as every
1003 // other successful rule kind: if this `use` site contributed
1004 // any matches (either from the subroutine or from its own
1005 // children) and the caller configured first-match
1006 // short-circuiting, halt evaluation of further siblings.
1007 if matches.len() > matches_before && context.should_stop_at_first_match() {
1008 break;
1009 }
1010 continue;
1011 }
1012
1013 // Evaluate the current rule with graceful error handling.
1014 // Pass the GNU `file` anchor so OffsetSpec::Relative resolves
1015 // correctly against the previous match's end position.
1016 let match_data = match evaluate_single_rule_with_anchor(
1017 rule,
1018 buffer,
1019 context.last_match_end(),
1020 context.base_offset(),
1021 ) {
1022 Ok(data) => data,
1023 Err(
1024 e @ (LibmagicError::EvaluationError(
1025 crate::error::EvaluationError::BufferOverrun { .. }
1026 | crate::error::EvaluationError::InvalidOffset { .. }
1027 | crate::error::EvaluationError::InvalidValueTransform { .. }
1028 | crate::error::EvaluationError::TypeReadError(
1029 crate::evaluator::types::TypeReadError::BufferOverrun { .. }
1030 | crate::evaluator::types::TypeReadError::InvalidPStringLength { .. },
1031 ),
1032 )
1033 | LibmagicError::IoError(_)),
1034 ) => {
1035 // Expected data-dependent evaluation errors -- skip gracefully.
1036 // TypeReadError::UnsupportedType is intentionally NOT caught here
1037 // so that evaluator capability gaps propagate as errors.
1038 debug!("Skipping rule '{}': {}", rule.message, e);
1039 continue;
1040 }
1041 Err(e) => {
1042 // Unexpected errors (InternalError, UnsupportedType, etc.) should propagate
1043 return Err(e);
1044 }
1045 };
1046
1047 if let Some((absolute_offset, read_value)) = match_data {
1048 // Advance the GNU `file` previous-match anchor BEFORE recursing
1049 // into children, so children and their descendants see the new
1050 // anchor. The anchor is updated unconditionally to the end of
1051 // this match -- it may move forward or backward depending on
1052 // where successive rules match (it is *not* a high-watermark).
1053 let consumed = types::bytes_consumed_with_pattern(
1054 buffer,
1055 absolute_offset,
1056 &rule.typ,
1057 Some(&rule.value),
1058 );
1059 let new_anchor = absolute_offset.saturating_add(consumed);
1060 context.set_last_match_end(new_anchor);
1061
1062 // Mark this level as "matched" so any subsequent `default`
1063 // sibling at the same level is suppressed, matching libmagic's
1064 // default-after-match semantics.
1065 sibling_matched = true;
1066
1067 let match_result = RuleMatch {
1068 message: rule.message.clone(),
1069 offset: absolute_offset,
1070 level: rule.level,
1071 value: read_value,
1072 type_kind: rule.typ.clone(),
1073 confidence: RuleMatch::calculate_confidence(rule.level),
1074 };
1075 matches.push(match_result);
1076
1077 // If this rule has children, evaluate them recursively
1078 if !rule.children.is_empty() {
1079 // Check recursion depth limit - this is a critical error that should stop evaluation.
1080 // `RecursionGuard` decrements the depth on drop, so every exit path below
1081 // (Ok, graceful warn!, or early-return via `?`) restores the counter.
1082 let mut guard = RecursionGuard::enter(context)?;
1083
1084 // Recursively evaluate child rules with graceful error handling
1085 match evaluate_rules(&rule.children, buffer, guard.context()) {
1086 Ok(child_matches) => {
1087 matches.extend(child_matches);
1088 }
1089 Err(LibmagicError::Timeout { timeout_ms }) => {
1090 // Timeout is critical, propagate it up (guard drops here).
1091 return Err(LibmagicError::Timeout { timeout_ms });
1092 }
1093 Err(
1094 e @ (LibmagicError::EvaluationError(
1095 crate::error::EvaluationError::BufferOverrun { .. }
1096 | crate::error::EvaluationError::InvalidOffset { .. }
1097 | crate::error::EvaluationError::InvalidValueTransform { .. }
1098 | crate::error::EvaluationError::TypeReadError(
1099 crate::evaluator::types::TypeReadError::BufferOverrun { .. }
1100 | crate::evaluator::types::TypeReadError::InvalidPStringLength {
1101 ..
1102 },
1103 ),
1104 )
1105 | LibmagicError::IoError(_)),
1106 ) => {
1107 // Defensive: under the current implementation, individual child
1108 // failures are caught and logged inside the recursive evaluate_rules
1109 // call (they never propagate here). This arm guards against future
1110 // changes that might alter that error-handling strategy.
1111 //
1112 // If this fires, the parent match is still emitted but the entire
1113 // child subtree is silently dropped -- which means a partial,
1114 // possibly-incorrect classification is returned to the caller.
1115 // Logged at warn! (not debug!) so the asymmetry is visible.
1116 warn!(
1117 "Discarding child evaluation under rule '{}' due to unexpected error: {} -- parent match is still emitted; investigate the recursive evaluate_rules error-handling path",
1118 rule.message, e
1119 );
1120 }
1121 Err(e) => {
1122 // Unexpected errors in children (including RecursionLimitExceeded)
1123 // should propagate. The guard drops here, decrementing the depth.
1124 return Err(e);
1125 }
1126 }
1127 // `guard` drops here, decrementing the recursion depth.
1128 }
1129
1130 // Stop at first match if configured to do so
1131 if context.should_stop_at_first_match() {
1132 break;
1133 }
1134 }
1135 }
1136
1137 Ok(matches)
1138}
1139
1140/// Evaluate magic rules with a fresh context
1141///
1142/// This is a convenience function that creates a new evaluation context
1143/// and evaluates the rules. Useful for simple evaluation scenarios.
1144///
1145/// # Arguments
1146///
1147/// * `rules` - The list of magic rules to evaluate
1148/// * `buffer` - The file buffer to evaluate against
1149/// * `config` - Configuration for evaluation behavior
1150///
1151/// # Returns
1152///
1153/// Returns `Ok(Vec<RuleMatch>)` containing all matches found, or `Err(LibmagicError)`
1154/// if evaluation fails.
1155///
1156/// # Examples
1157///
1158/// ```rust
1159/// use libmagic_rs::evaluator::{evaluate_rules_with_config, RuleMatch};
1160/// use libmagic_rs::parser::ast::{MagicRule, OffsetSpec, TypeKind, Operator, Value};
1161/// use libmagic_rs::EvaluationConfig;
1162///
1163/// let rule = MagicRule {
1164/// offset: OffsetSpec::Absolute(0),
1165/// typ: TypeKind::Byte { signed: true },
1166/// op: Operator::Equal,
1167/// value: Value::Uint(0x7f),
1168/// message: "ELF magic".to_string(),
1169/// children: vec![],
1170/// level: 0,
1171/// strength_modifier: None,
1172/// value_transform: None,
1173/// };
1174///
1175/// let rules = vec![rule];
1176/// let buffer = &[0x7f, 0x45, 0x4c, 0x46];
1177/// let config = EvaluationConfig::default();
1178///
1179/// let matches = evaluate_rules_with_config(&rules, buffer, &config).unwrap();
1180/// assert_eq!(matches.len(), 1);
1181/// assert_eq!(matches[0].message, "ELF magic");
1182/// ```
1183///
1184/// # Errors
1185///
1186/// * `LibmagicError::EvaluationError` - If rule evaluation fails
1187/// * `LibmagicError::Timeout` - If evaluation exceeds configured timeout
1188pub fn evaluate_rules_with_config(
1189 rules: &[MagicRule],
1190 buffer: &[u8],
1191 config: &EvaluationConfig,
1192) -> Result<Vec<RuleMatch>, LibmagicError> {
1193 // Validate the configuration before constructing a context so that
1194 // out-of-range values (e.g. zero recursion depth, excessive timeouts)
1195 // are rejected at the API boundary rather than triggering subtle
1196 // failures during evaluation.
1197 config.validate()?;
1198 // Diagnostic guard: `evaluate_rules_with_config` builds a context
1199 // without an attached `RuleEnvironment`, which means any
1200 // `MetaType::Indirect` rule reached during evaluation is silently
1201 // no-op'd at runtime. That is the intentional behavior for low-level
1202 // callers (matching the `Use`-without-env contract), but we surface
1203 // the misconfiguration at `warn!` level (once per process) so a
1204 // consumer who wires up env-less `indirect` rules will see the
1205 // diagnostic in default logging rather than only at debug level.
1206 // The tree walk runs only in debug builds -- in release builds the
1207 // `cfg(debug_assertions)` gate prevents the O(n) scan on every
1208 // top-level evaluation. Using `debug_assert!` would panic in test
1209 // builds and break the "evaluator never panics" invariant documented
1210 // in GOTCHAS S2.4 -- a misconfigured caller should get a no-op with
1211 // a log entry, not a crash.
1212 #[cfg(debug_assertions)]
1213 if contains_indirect_rule(rules)
1214 && !INDIRECT_WITHOUT_RULE_ENV_WARNED.swap(true, Ordering::Relaxed)
1215 {
1216 warn!(
1217 "{} (subsequent occurrences suppressed)",
1218 crate::error::EvaluationError::indirect_without_environment()
1219 );
1220 }
1221 // Clear the thread-local regex compile cache so it is bounded to
1222 // the lifetime of a single top-level evaluation call. Cache
1223 // entries from a previous rule set would otherwise persist on the
1224 // current thread until process exit. See
1225 // `evaluator::types::regex::reset_regex_cache` for rationale.
1226 crate::evaluator::types::regex::reset_regex_cache();
1227 let mut context = EvaluationContext::new(config.clone());
1228 evaluate_rules(rules, buffer, &mut context)
1229}
1230
1231/// Recursively walk `rules` (including children) looking for any
1232/// [`MetaType::Indirect`] directive.
1233///
1234/// Used by the diagnostic guard in [`evaluate_rules_with_config`]: the
1235/// low-level `_with_config` entry point builds a context without a
1236/// [`crate::evaluator::RuleEnvironment`], so any `indirect` rule is
1237/// silently no-op'd at runtime. The check logs the misconfiguration at
1238/// `debug!` level so consumer tests can detect it without panicking (see
1239/// GOTCHAS S2.4 for why `debug_assert!` would be wrong here).
1240fn contains_indirect_rule(rules: &[MagicRule]) -> bool {
1241 rules.iter().any(|rule| {
1242 matches!(rule.typ, TypeKind::Meta(MetaType::Indirect))
1243 || contains_indirect_rule(&rule.children)
1244 })
1245}
1246
1247#[cfg(test)]
1248mod tests;