{"record":{"id":"650a657516d09874","repo":"BoundaryML/baml","slug":"interface-offset-fits-u32","errorCode":null,"errorMessage":"interface offset fits u32","messagePattern":"interface offset fits u32","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler2_emit/src/lib.rs","lineNumber":2445,"sourceCode":"    for (offset, kind) in obj_kind.iter().enumerate() {\n        let idx = prefix_objects + offset;\n        let u = obj_owner[idx];\n        let obj = program.objects[ObjectIndex::from_raw(idx)].clone();\n        let local_ref = match kind {\n            PoolObjKind::Class => {\n                let off = units[u].classes.len();\n                units[u].classes.push(obj);\n                LocalRef::Class(u32::try_from(off).expect(\"class offset fits u32\"))\n            }\n            PoolObjKind::Enum => {\n                let off = units[u].enums.len();\n                units[u].enums.push(obj);\n                LocalRef::Enum(u32::try_from(off).expect(\"enum offset fits u32\"))\n            }\n            PoolObjKind::Interface => {\n                let off = units[u].interfaces.len();\n                units[u].interfaces.push(obj);\n                LocalRef::Interface(u32::try_from(off).expect(\"interface offset fits u32\"))\n            }\n            PoolObjKind::TypeAlias => {\n                let off = units[u].type_alias_objects.len();\n                units[u].type_alias_objects.push(obj);\n                LocalRef::TypeAlias(u32::try_from(off).expect(\"type-alias offset fits u32\"))\n            }\n            PoolObjKind::NamedFn(_) | PoolObjKind::CodeAnon => {\n                let off = units[u].code.len();\n                units[u].code.push(obj);\n                LocalRef::Code(u32::try_from(off).expect(\"code offset fits u32\"))\n            }\n        };\n        obj_localref.push(local_ref);\n    }\n\n    // ---- Global slot -> owner + local flat index ----------------------------\n    // slot -> fq name (functions, interface bodies, and lets).\n    let mut slot_to_name: Vec<Option<String>> = vec![None; program.globals.len()];","sourceCodeStart":2427,"sourceCodeEnd":2463,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/lib.rs#L2427-L2463","documentation":"A Rust `expect(\"interface offset fits u32\")` panic in baml_compiler2_emit's local-unit packing code. While distributing pooled interface objects into per-unit local pools, the next interface offset (`units[u].interfaces.len()`) is converted to `u32`; the compiler assumes each unit holds fewer than 2^32 interfaces. Exceeding that indicates runaway or duplicated pooling, so the library panics rather than truncating offsets.","triggerScenarios":"Triggered while assigning `PoolObjKind::Interface` objects to local units when `units[u].interfaces.len()` exceeds `u32::MAX` — only reachable with an absurdly large or pathologically duplicated interface pool.","commonSituations":"Not hit by real BAML programs; appears in fuzzing or compiler-dev contexts such as a dedup failure pushing interfaces repeatedly, or synthetic inputs with billions of interface declarations.","solutions":["Audit the pooling loop for duplicate assignment of the same interface object to a unit (dedup key bug).","Add an explicit capacity check that emits a proper compile diagnostic before exceeding u32 capacity.","Widen the offset storage to `u64`/`usize` in `LocalRef` and serialized unit format if huge inputs must be supported.","Inspect the input program's interface count; a legitimate program cannot approach 2^32 interfaces."],"exampleFix":"// before\nlet off = units[u].interfaces.len();\nunits[u].interfaces.push(obj);\nLocalRef::Interface(u32::try_from(off).expect(\"interface offset fits u32\"))\n// after\nif units[u].interfaces.len() > u32::MAX as usize {\n    return Err(EmitError::UnitOverflow { unit: u, kind: \"interface\" });\n}\nlet off = units[u].interfaces.len();\nunits[u].interfaces.push(obj);\nLocalRef::Interface(off as u32)","handlingStrategy":"validation","validationCode":"// Not applicable to BAML users. Embedders can pre-check pool size before packing:\nfn interface_pool_within_u32(program: &Program) -> bool {\n    program.objects.iter().filter(|o| matches!(o, Object::Interface(_))).count() < u32::MAX as usize\n}","typeGuard":null,"tryCatchPattern":"// Panics are not catchable from BAML user code. Embedders can isolate compilation:\nlet result = std::panic::catch_unwind(|| compile(&source));\nif result.is_err() {\n    report_internal_compiler_error();\n}","preventionTips":["Keep per-unit interface counts below 2^32 by construction (real programs never approach this).","Deduplicate pooled interfaces so each object is pushed to a unit at most once.","Add a compile-time capacity check with a clear diagnostic instead of `expect`.","Fuzz with synthetic large inputs to catch overflow paths early."],"tags":["compiler","overflow","panic","rust"],"backgroundTag":"value-out-of-range","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}