Skip to content

Bound constant-expression evaluation during IR verification #239

Closed

Description

@LunaStev

Reproduced on master

Revision: 4f45fdb53d07882932cc251856316206a3bb3947. A module submitted through the public Rust IR API can abort its caller during verify_module instead of returning a verification error.

The constant case below passes at depth 32 and aborts at depth 50,000 on Linux AMD64 with thread 'main' has overflowed its stack (SIGABRT / subprocess return code -6). The module is leaked before verification to exclude recursive destruction from the reproduction. No JSON decoder is involved; its recursion limit does not protect direct frontend integrations.

Source: ir/src/const_expr.rs.

Reproduction

Save as /tmp/whale-ir-depth-repro.rs:

use ir::*;
fn main() {
    let mode = std::env::args().nth(1).unwrap();
    let depth: usize = std::env::args().nth(2).unwrap().parse().unwrap();
    let mut m = Module::new("x86_64-whale-linux", DataLayout::default_64bit_le());
    if mode == "signature" {
        let mut ty = Type::I32;
        for _ in 0..depth { ty = Type::ptr_to(ty); }
        m.declarations.push(FunctionDecl {
            id: FunctionId(0), name: "external".into(),
            signature: FunctionSignature::whale(vec![ty], Type::Void),
            linkage: Linkage::External, link_name: Some("external".into()),
        });
    } else {
        let mut expr = ConstExpr::literal(Type::I32, ConstValue::I(0));
        for _ in 0..depth {
            expr = ConstExpr { ty: Type::I32, kind: ConstExprKind::Binary {
                op: ConstBinaryOp::Add, left: Box::new(expr),
                right: Box::new(ConstExpr::literal(Type::I32, ConstValue::I(0))),
            }};
        }
        m.globals.push(Global { id: GlobalId(0), name: "deep".into(), ty: Type::I32,
            init: ConstValue::I(0), init_expr: expr, align: 4 });
    }
    // Leak after construction so the reproducer only measures verification,
    // never the recursive destructor of the submitted tree.
    let m = Box::leak(Box::new(m));
    eprintln!("entering verify_module: {mode}, depth={depth}");
    eprintln!("result: {:?}", verify_module(m));
}

From the Whale repository:

cargo build --offline -p ir
rustc --edition=2021 /tmp/whale-ir-depth-repro.rs \
    --extern ir=target/debug/libir.rlib \
    -L dependency=target/debug/deps -o /tmp/whale-ir-depth-repro
ulimit -c 0
/tmp/whale-ir-depth-repro constant 32
/tmp/whale-ir-depth-repro constant 50000

Scope

Bound constant-expression evaluation by explicit depth and work/node budgets, or evaluate iteratively with a bounded work stack. Preserve original typed expressions, wrapping arithmetic, reference resolution, cycle detection and exact stored results. Apply limits to both global initializers and local ConstDecl expressions through the public Rust API.

Completion criteria

  • Excessive expression depth/work returns a structured ConstEvalError / VerifyError, never process abort.
  • Limits are explicit/configurable with safe defaults.
  • Boundary tests cover global and local expressions, left/right nested trees and references without changing arithmetic semantics.
  • A subprocess regression excludes recursive destruction and confirms graceful rejection.

Type/function-signature traversal is tracked separately. This is independent of #109 cast/checked validation and the bounded AST JSON decoder in #54. This issue is not a good first issue because it affects the constant evaluator and verification boundary.

Activity

  1. added
    bug
    ir
    priority high
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugirpriority high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions