-
Notifications
You must be signed in to change notification settings - Fork 219
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
Crash when using SpiDevice::write (Because of a hidden alignment requirement) #295
Comments
Quite interesting problem, I learned something about alignment. Seems like For now I did a workaround with a helper struct |
Thanks for bringing this up! I also think this hidden alignment requirement is very unfortunate and should get addressed |
when transferring data on esp32c3 via SPI. The alignment error originates in the call to `core::ptr::copy_nonoverlapping`, which "requires that both pointer arguments are aligned and non-null and the specified memory ranges do not overlap". Closes esp-rs#295
TL;DR: Hey was just reading through the code to figure out if QuadSPI/OctalSPI was already supported and found this issue through esp-hal/esp-hal-common/src/spi.rs Line 1422 in 23690a4
I encountered the same issue when implementing esp-hal/esp-hal-common/src/sha.rs Line 45 in 23690a4
*mut u32::volative_write in a loop like in esp-hal/esp-hal-common/src/sha.rs Lines 140 to 143 in 23690a4
|
I think that loop was what we initially had in the SPI driver - only downside IMHO is that in opt-level 0 it might be a bit slow but with opt-level >0 it's totally fine @MabezDev @jessebraham any thoughts on this? I'd say we should replace |
I think it's probably a good idea to make those changes, yeah. The only downside you listed is, IMO, not even really much of an issue in practice. |
Not really expressing much of an opinion, but in esp-idf, they return an error/panic if the buffer is not correctly aligned and doesn't have a length that is a multiple of a word. This pushes the requirements to the user of the API in a clear, albeit slightly annoying way (runtime error). |
I actually encountered a miscompilation relating to this. for byte in frame {
let b = &[byte.reverse_bits()];
spi.write(b)?;
// core::hint::black_box(b);
} Without the black box, only As far as I understand, there's actually three problems with the current code:
esp-hal/esp-hal-common/src/spi.rs Lines 1406 to 1417 in 75f7394
|
Thanks for the insights. This definitely needs to get fixed (for all the reasons you mentioned) Regarding the mis-compilation: on which targets do you see this? |
I think panicking / returning an error wouldn't be a really good option since those requirements are not requirement by the EH traits and would potentially prevent users from using existing drivers 🤷♂️ For our own APIs however, that might be okay if we document that clearly |
.cargo/config.toml: [build]
rustflags = [
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Wl,-Tlinkall.x",
]
target = "xtensa-esp32s3-none-elf"
[unstable]
build-std = ["core"] In my case the miscompilation went away after changing the code to: for i in 0..FIFO_SIZE / 4 {
unsafe {
core::ptr::write_volatile(
(fifo_ptr as *mut u32).add(i),
*(chunk.as_ptr() as *const u32).add(i),
);
}
} but this does not address the alignment or out of bounds read problem |
Hi, I am trying to write a small driver for an SPI device with registers. For that, I have the following function to write the instruction and then read the data:
However, it crashes on an ESP32-C3 with
Do you know what is happening here? I am using not a line of unsafe code (in the code written by me) before calling this method.
I tried to read the register at a different place in the lib using
spi.transfer()
and there it works without problems.Another observation: when I change the code inside the closure to
it does not crash.
Using
let cmd = [cmd;1];
however also crashes.Update 1: this observation is not always true. Sometimes it does not work...
Update 2: this also happens when using
transfer_in_place()
The text was updated successfully, but these errors were encountered: