esp-rs / rust

Rust for the xtensa architecture. Built in targets for the ESP32 and ESP8266
https://www.rust-lang.org
Other
743 stars 39 forks source link

C functions arguments wrong alignment #171

Closed elotom closed 1 year ago

elotom commented 1 year ago

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

  1. 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 commented 1 year ago

Maybe https://github.com/esp-rs/esp-wifi/issues/16 is related. My experiments initially showed it might work correctly but it probably depends on the number of arguments

kyrias commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

MabezDev commented 1 year ago

Builds are now available. Closing this issue.