Skip to content

Default allocator doesn't guarantee allocation bigger than 4? #99

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

Closed
UnTraDe opened this issue Jan 1, 2022 · 2 comments · Fixed by rust-lang/rust#92586
Closed

Default allocator doesn't guarantee allocation bigger than 4? #99

UnTraDe opened this issue Jan 1, 2022 · 2 comments · Fixed by rust-lang/rust#92586
Assignees
Labels

Comments

@UnTraDe
Copy link

UnTraDe commented Jan 1, 2022

Allocating a type with alignment bigger than 4 bytes sometimes gets allocated on address which is not a multiple of that alignment, causing accesses to fail debug_assert! in slice::from_raw_parts_*

struct Something {
    x: f64,
    y: f32
}

fn main() -> anyhow::Result<()> {
    esp_idf_sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    log::info!("align_of::<Something>(): {}", std::mem::align_of::<Something>());
    log::info!("size_of::<Something>(): {}", std::mem::size_of::<Something>());

    loop {
        let r = rand::random::<usize>() % 100;
        let mut v = Vec::with_capacity(r);
        log::info!("pointer: 0x{:x}", v.as_ptr() as usize);
        log::info!("v.as_ptr() % std::mem::align_of::<Something>() == {}", v.as_ptr() as usize % std::mem::align_of::<Something>());
        unsafe { v.set_len(r); };

        log::info!("fill");

        for x in &mut v {
            *x = Something {
                x: 10f64,
                y: 20f32
            };
        }

        log::info!("filled");
        std::thread::sleep(std::time::Duration::from_millis(100));
    }

    Ok(())
}

Note that the alignment of Something is 8, and sometimes the pointer is not a multiple of 8, causing a panic in for x in &mut v { in debug builds.

Happens on ESP32 with Rust STD library, using https://github.com/ivmarkov/rust-esp32-std-demo

@MabezDev
Copy link
Member

MabezDev commented Jan 5, 2022

Thanks for the report, and the helpful testcase!

This should be fixed in this branch with this commit: 2e592a9.

I have also submitted the fix upstream (see the linked PR).

@MabezDev
Copy link
Member

This patch is now included in our 1.58 release. It's still not been accepted upstream, but hopefully it will soon!

Closing this now.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 21, 2022
…espidf, r=yaahc

Set the allocation MIN_ALIGN for espidf to 4.

Closes esp-rs#99.

cc: `@ivmarkov`
@tom-borcin tom-borcin moved this to Done in esp-rs Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants