Closed Bug 1888581 Opened 1 year ago Closed 1 year ago

MOZ_CRASH(unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null

Categories

(Core :: Graphics: WebRender, defect, P2)

defect

Tracking

()

RESOLVED FIXED
127 Branch
Tracking Status
firefox127 --- fixed

People

(Reporter: bradwerth, Assigned: bradwerth, NeedInfo)

References

(Blocks 1 open bug)

Details

Attachments

(1 file, 3 obsolete files)

On my macOS local Nightly build, I get this crash when viewing most any web content. Debugging indicates that this is crashing here in FontInstanceKey::add_key when the instance has no variations. In such a case, the instance variations Vec has a raw pointer that is not memory-aligned. This Vec is created from WrVecU8::convert_into_vec which has the possibility of creating non-aligned empty Vecs.

I have no idea why this isn't a bigger deal, hitting other users, nor why it just started now for me. It is, however, easy enough to fix.

This is probably happening to me because I ran a rustup update before my most recent build, and I'm on the nightly branch. Even if that's the reason, and I could avoid this problem by going back to stable, this is a future problem that needs solving.

:bradwerth: Perhaps propagating the assertion from std to constructors of WrVecU8 could be helpful in tracking down culprit code?

Attachment #9393902 - Attachment description: Bug 1888581: Make WrVecU8::convert_into_vec return memory-aligned empty Vecs. → Bug 1888581: Make WrVecU8::convert_into_vec return memory-aligned in all cases.

(In reply to Erich Gubler [:ErichDonGubler] from comment #3)

:bradwerth: Perhaps propagating the assertion from std to constructors of WrVecU8 could be helpful in tracking down culprit code?

Maybe... but that would require constructors to specify the alignment value we intend to convert to -- even if only to check against it. And once we've provided it, we might as well construct our u8 data on the alignment boundary, but then we're just solving the problem a different way that is less semantically clear.

(In reply to Erich Gubler [:ErichDonGubler] from comment #3)

Perhaps propagating the assertion from std to constructors of WrVecU8 could be helpful in tracking down culprit code?

There's only one caller of convert_into_vec<T>, namely wr_resource_updates_add_font_instance. And there's no checking to be done in the constructor of WrVecU8 because WrVecU8 contains u8s which have an alignment of 1. It's only the convert_into_vec<T> method that needs to ensure alignment.

Another way to fix this bug would be to only allow convert_into_vec<T> to be called with types T which have an alignment of 1. We can turn FontVariation into such a type by replacing its u32 and f32 members with [u8; 4] arrays and by adding accessor functions which do the conversion by calling u32::from_ne_bytes and f32::from_ne_bytes.

Attachment #9393902 - Attachment is obsolete: true

This ensures that the eventual call in Rust to WrVecU8::convert_into_vec
will succeed, as long as no intervening calls to ::reserve or
::push_bytes cause the Vec to be reallocated. In an ideal world, we
would be able to templatize the FFI functions that call those methods,
but that requires name mangling, making the functions unusable for FFI.

Attachment #9393902 - Attachment is obsolete: false
Attachment #9394676 - Attachment description: Bug 1888581: Make C-side WrVecU8 callers responsible for aligning buffer to intended type. → Bug 1888581 Part 1: Make C-side WrVecU8 callers responsible for aligning buffer to intended type.
Attachment #9393902 - Attachment description: Bug 1888581: Make WrVecU8::convert_into_vec return memory-aligned in all cases. → Bug 1888581 Part 2: Make WrVecU8::convert_into_vec return memory-aligned in all cases.

I'm going to attach two alternative patches. The first avoids the memcpy, the second makes us always memcpy FontVariations.
How big are the lists of FontVariations usually? I'm suspecting it won't be in the megabytes range.

This is now blocking the update to rustc 1.78.

Flags: needinfo?(bwerth)

Jeff, time to pick the approach you'd prefer to solve this Bug.

Flags: needinfo?(bwerth) → needinfo?(jmuizelaar)

I've been meaning to talk this over with mstange.

Pushed by mstange@themasta.com: https://hg.mozilla.org/integration/autoland/rev/d8bf7ce5ab91 Copy FontVariation data instead of doing an unsound cast. r=jrmuizel

Backed out for causing webrender related build bustages.

Oh hello!

#[cfg(target_os = "windows")]
fn read_font_descriptor(bytes: &mut WrVecU8, index: u32) -> NativeFontHandle {
    let wchars = bytes.convert_into_vec::<u16>();

Somehow I missed this caller of convert_into_vec in all my searches.

Pushed by mstange@themasta.com: https://hg.mozilla.org/integration/autoland/rev/a33f8c53d37c Copy FontVariation data instead of doing an unsound cast. r=jrmuizel
Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 127 Branch
Attachment #9394903 - Attachment is obsolete: true
Attachment #9394676 - Attachment is obsolete: true
Attachment #9393902 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: