Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miri; fix unsafe ub #915

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

maksimryndin
Copy link

@maksimryndin maksimryndin commented Nov 29, 2024

  • adding miri to ci to detect undefined behaviour (UB) as the codebase has quite a lot of unsafe code
  • refactoring some tests along the way (to deduplicate the code)
  • fixing UB behaviour (memory alignment violation) by removing the method into_cpu_vec as it violates the invariant of the same element alignment. We use a slice instead. We are forced to allocate in new_canonical_ordered but it seems not to be on the critical path so it should be no issue.

Screenshot 2024-11-29 at 09 16 25

The alignment invariant violation can be reproduced with the program (cargo +nightly miri run)

#![feature(portable_simd)]
#![feature(vec_into_raw_parts)]

use std::mem;
use std::simd::Simd;

fn main() {
    let array: [u32; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    let packed: Simd<u32, 16> = array.into();

    println!("PackedBaseField alignment: {} bytes", std::mem::align_of::<Simd<u32, 16>>());
    println!("BaseField alignment: {} bytes", std::mem::align_of::<u32>());

    let mut col = vec![packed];
    let capacity = col.capacity() * 16;
    let length = 8;
    let ptr = col.as_mut_ptr() as *mut u32; // aka BaseField

    let res = unsafe { Vec::from_raw_parts(ptr, length, capacity) };
    mem::forget(col);

    println!("{:?}", res)
}

@reviewable-StarkWare
Copy link

This change is Reviewable

@maksimryndin maksimryndin marked this pull request as ready for review November 29, 2024 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants