Assert that LLVM range-attribute values don't exceed 128 bits

The underlying implementation of `LLVMCreateConstantRangeAttribute` assumes
that each of `LowerWords` and `UpperWords` points to enough u64 values to
define an integer of the specified bit-length, and will encounter UB if that is
not the case.

Our safe wrapper function always passes pointers to `[u64; 2]` arrays,
regardless of the bit-length specified. That's fine in practice, because scalar
primitives never exceed 128 bits, but it is technically a soundness hole in a
safe function.

We can close the soundness hole by explicitly asserting `size_bits <= 128`.
This is effectively just a stricter version of the existing check that the
value must be small enough to fit in `c_uint`.
This commit is contained in:
Zalathar
2025-08-26 12:31:33 +10:00
parent d327d651e2
commit fcff8f7f5a
3 changed files with 30 additions and 11 deletions

View File

@@ -1929,11 +1929,17 @@ unsafe extern "C" {
C: &Context,
effects: MemoryEffects,
) -> &Attribute;
/// ## Safety
/// - Each of `LowerWords` and `UpperWords` must point to an array that is
/// long enough to fully define an integer of size `NumBits`, i.e. each
/// pointer must point to `NumBits.div_ceil(64)` elements or more.
/// - The implementation will make its own copy of the pointed-to `u64`
/// values, so the pointers only need to outlive this function call.
pub(crate) fn LLVMRustCreateRangeAttribute(
C: &Context,
num_bits: c_uint,
lower_words: *const u64,
upper_words: *const u64,
NumBits: c_uint,
LowerWords: *const u64,
UpperWords: *const u64,
) -> &Attribute;
// Operations on functions