huggingface / huggingface/candle

quantized/ggml_file.rs: from_raw_parts on a 1-byte-aligned pointer without an alignment check

Open Beginner friendly
#3,815 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
21.1k
Forks
1.8k
Avg merge
16h 42m
Merged PRs (30d)
25

Description

`candle-core/src/quantized/ggml_file.rs:128` builds a typed slice from a byte pointer with no alignment check:

```rust
std::slice::from_raw_parts(raw_data_ptr as *const T, n_blocks)
```

`raw_data_ptr` is a `*const u8` derived from the file buffer, so it carries 1-byte alignment. Creating a `&[T]` where `T` has stricter alignment is undefined behaviour regardless of whether the access happens to work.

Miri flags it directly:

```
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference
(required 4 byte alignment but found 1)
```

This is unconditional on that code path — it is not triggered by any malformed input, it happens on ordinary GGUF loads. In practice it is benign on the usual allocators (malloc returns ≥16-byte-aligned blocks) and on x86/ARM64 where unaligned loads work, which is presumably why it hasn't caused visible problems.

The reason I think it's worth fixing rather than ignoring: **candle's own sibling helper already does the check.** `quantized/mod.rs::as_t_slice` asserts both the length-multiple and the alignment. `from_raw_data` doesn't. That inconsistency is the actual defect — one of the two raw-slice constructors validates and the other doesn't.

Suggested fix: mirror the `as_t_slice` assertions in `from_raw_data`, or route it through the same helper.

Reported as a soundness/UB issue, not a security vulnerability — I found no way to turn it into an out-of-bounds access.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in candle-core/src/quantized/ggml_file.rs at line 128 and compare from_raw_data with quantized/mod.rs::as_t_slice, which already checks alignment and length. The work is done when the raw-slice construction applies equivalent validation and the reported Miri unaligned-reference error no longer occurs on the affected GGUF load path.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.