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

C functions arguments wrong alignment #171

Closed
elotom opened this issue Mar 31, 2023 · 5 comments
Closed

C functions arguments wrong alignment #171

elotom opened this issue Mar 31, 2023 · 5 comments

Comments

@elotom
Copy link

elotom commented Mar 31, 2023

I'm binding a Rust function to a C function that takes 10 parameters. What I'm seeing now is some kind of misalignment where the parameters following the 7th parameter are misaligned by two bytes. We are using esp32 S3

To reproduce the issue please clone https://github.com/elotom/rust-esp32-std-demo/tree/c-parameters-align-issue
then run the following commands:
1.xtensa-esp32s3-elf-gcc -Ilibtest_parameters -o c/test_parameters.o -c c/test_parameters.c
2.xtensa-esp32s3-elf-ar csrDT c/libtest_parameters.a -c c/test_parameters.o
3. Flash your device with cargo +esp espflash --target xtensa-esp32s3-espidf --release --monitor

The C function only returns param8

uint16_t test_parameters(uint16_t param1, uint16_t param2, uint16_t param3, uint16_t param4, uint16_t param5, uint16_t param6, uint16_t param7, uint16_t param8, uint16_t param9, uint16_t param10)
{
    return param8;
}

The Rust function calls test_parameters and I'm expecting a return value of 800 but I'm getting 900 instead
image

let ret = unsafe{test_parameters(100,200,300,400,500,600,700,800,900,1000)};
    println!("Ret value should be 800 and is {}",ret);

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

@bjoernQ
Copy link

bjoernQ commented Apr 5, 2023

Maybe esp-rs/esp-wifi-sys#16 is related. My experiments initially showed it might work correctly but it probably depends on the number of arguments

@kyrias
Copy link

kyrias commented Apr 17, 2023

This is a bug in the Xtensa port of LLVM. C.f. espressif/llvm-project#66.

If you're responsible for compiling all of the code a workaround would be to compile the C portions with clang instead of GCC.

@MabezDev
Copy link
Member

image

We have a fix for this internally which fixes the issue. Hopefully will be merged shortly! I'll be sure to do a point release as of the Rust compiler as this issue is quite severe.

@MabezDev
Copy link
Member

Patch release is now available here: https://github.com/esp-rs/rust, builds are on the way.

@MabezDev
Copy link
Member

Builds are now available. Closing this issue.

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

No branches or pull requests

4 participants