-
Notifications
You must be signed in to change notification settings - Fork 120
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
Misaligned struct pointer from esp camera #278
Comments
OK, it is likely esp32 (xtensa) or maybe s2 or s3? |
You sure it is about the |
Where do you see this? All I see is |
Also good to mention:
|
Looking here it seems the alignment depends on what members the structure or array has. So it can't be the
Strange. It looks to me as some sort of impossible miscompilation happened, where |
Thank you for your input! The following code let fb: *mut esp_idf_sys::cam::camera_fb_t = esp_idf_sys::cam::esp_camera_fb_get();
println!("Alignment: {}", std::mem::align_of::<esp_idf_sys::cam::camera_fb_t>());
println!("Pointer: {:p}", fb);
let deref = *fb; prints
And then panics on the line In my .cargo/config.toml I have My rust version is 1.75.0. I am running on the esp32-cam, which uses an ESP32. |
Can you try with ESP IDF V4.4? I'm not suggesting a permanent downgrade, just trying to figure out if |
|
Does Rust generally require that all data types are naturally aligned (i.e. aligned to their own size) or is this just the way the Xtensa target was defined? For example, the way GCC is configured on Xtensa is that 2- and 4-byte types are naturally aligned, while larger types are aligned to 4-byte boundaries. |
Great question. I don't know but I assume rustc would generally follow the alignment specified in the target. Which would mean that there is probably something wrong here w.r.t. types > 32bit for the ESP IDF target, as well as here - for the baremetal ESP32 target (and likely the same for s2 and s3). According to docu, these are LLVM alignments, actually. Let me try to decipher this to figure out what it does say for types > 32 bits (if anything).
|
On In the document Type layout:
|
The timeval struct is defined as #[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct timeval {
pub tv_sec: time_t,
pub tv_usec: suseconds_t,
} Turns out there are a bunch of different println!("i64 alignment: {}", std::mem::align_of::<i64>());
println!("time_t alignment: {}", std::mem::align_of::<esp_idf_sys::time_t>());
println!("esp32-camera time_t alignment: {}", std::mem::align_of::<esp_idf_sys::cam::time_t>());
println!("esp OS time_t alignment: {}", std::mem::align_of::<std::os::espidf::raw::time_t>());
println!("esp_idf_svc time_t alignment: {}", std::mem::align_of::<esp_idf_svc::sys::time_t>());
println!("ffi long long alignment: {}", std::mem::align_of::<std::ffi::c_longlong>()); |
Did you re-test this with ESP IDF < 5? On ESP IDF < 5 it should give you 4, not 8. |
See here. ESP IDF < 5 does not have |
Indeed for
|
@ViktorWb Can you try compiling on ESP IDF 5+ with this JSON target instead {
"arch": "xtensa",
"cpu": "esp32",
"crt-objects-fallback": "false",
"data-layout": "e-m:e-p:32:32-i64:32-i128:32-n32",
"emit-debug-gdb-scripts": false,
"env": "newlib",
"linker": "xtensa-esp32-elf-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "xtensa-none-elf",
"max-atomic-width": 32,
"os": "espidf",
"panic-strategy": "abort",
"relocation-model": "static",
"target-family": [
"unix"
],
"target-pointer-width": "32",
"vendor": "espressif"
} Here, the relevant info w.r.t. custom targets. |
Sure! Adding the JSON data to
works! The example given earlier now prints:
println!("Alignment: {}", std::mem::align_of::<esp_idf_sys::cam::camera_fb_t>()); gives |
Alright! I'll PR the change to the Rust fork delivered with |
Fantastic, thank you! |
Closing as we now have this PR opened |
Just closed my PR because - as it turned out - there is an earlier (and more correct in that it treats the |
* Align the frame buffers to the structure alignment cc: esp-rs/esp-idf-sys#278 cc: esp-rs/rust#195 * Include stdalign.h
Hi! I activated esp32-camera using
in my Cargo.toml. This worked just fine. Then I tried to do:
Which also works fine, but only in release mode. When running in debug mode I get
misaligned pointer dereference: address must be a multiple of 0x8 but is 0x3f801fc4
The issue is that the
fb
variable is a pointer to a 4-bit aligned struct, while rust requires an 8-bit aligned struct. The struct in question is thecamera_fb_t
. My understanding is that the alignment checks are skipped in release mode, but that it can cause undefined behaviour.In the generated bindings, the
camera_fb_t
struct has an alignment of 8 and is defined asThe text was updated successfully, but these errors were encountered: