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

Bug in example 'spi_slave_hd\segment_mode\seg_slave' when -Os or -O2 is set for CC (IDFGH-5420) #7163

Closed
tslabs opened this issue Jun 17, 2021 · 1 comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@tslabs
Copy link

tslabs commented Jun 17, 2021

Environment

  • Development Kit: none
  • Module or chip used: ESP32-S2
  • IDF version: v4.4-dev-1594-g1d7068e4b
  • Build System: idf.py
  • Compiler version: xtensa-esp32-elf-gcc (crosstool-NG esp-2020r3) 8.4.0
  • Operating System: Windows
  • (Windows only) environment type: Plain Command Prompt
  • Using an IDE?: No
  • Power Supply: any

Problem Description

The '\examples\peripherals\spi_slave_hd\segment_mode\seg_slave' contains a bug, which causes the example failure, if the C compiler optimization level is -Os or -O2.

Expected Behavior

Must work normally.

Actual Behavior

Throws an exception:
'E (220) SEG_SLAVE: No enough memory!'

Steps to reproduce

  1. Go to '\examples\peripherals\spi_slave_hd\segment_mode\seg_slave' dir.
  2. Run idf.py menuconfig.
  3. Go to 'Compiler options', 'Optimisation options'.
  4. Set '-Os' or '-O2' level.
  5. Compile and flash the example.
  6. Get the 'E (220) SEG_SLAVE: Not enough memory!' error.

Code to reproduce this issue

app_main.c

void app_main(void)
{
...
    uint32_t send_buf_size = 5000;
    uint32_t recv_buf_size = 120;
...
    xTaskCreate(sender, "sendTask", 4096, &send_buf_size, 1, NULL);
    xTaskCreate(receiver, "recvTask", 4096, &recv_buf_size, 1, NULL);
}

The bug reason

Quite basic.
The variables are created on stack which is different when passed to a task. Both void receiver(void *arg) and void sender(void *arg) receive garbage in 'arg'.
What works in -O0 will fail in -Os. The variables should be set to 'static' so that they put in .bss.
This type of bug might be present in other code snippets as well, so I suggest to check them.

Code fixes

void app_main(void)
{
...
    static uint32_t send_buf_size = 5000;
    static uint32_t recv_buf_size = 120;
...
    xTaskCreate(sender, "sendTask", 4096, &send_buf_size, 1, NULL);
    xTaskCreate(receiver, "recvTask", 4096, &recv_buf_size, 1, NULL);
}
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jun 17, 2021
@github-actions github-actions bot changed the title Bug in example 'spi_slave_hd\segment_mode\seg_slave' when -Os or -O2 is set for CC Bug in example 'spi_slave_hd\segment_mode\seg_slave' when -Os or -O2 is set for CC (IDFGH-5420) Jun 17, 2021
@nopnop2002
Copy link

nopnop2002 commented Aug 1, 2021

This issue may not be resolved, but what happens with the code below?

void app_main(void)
{
...
    static uint32_t send_buf_size = 5000;
    static uint32_t recv_buf_size = 120;
...
    xTaskCreate(sender, "sendTask", 4096, &send_buf_size, 1, NULL);
    xTaskCreate(receiver, "recvTask", 4096, &recv_buf_size, 1, NULL);
    while(1) {
      vTaskDelay(1);
    }
}

@espressif-bot espressif-bot added Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Opened Issue is new Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable labels Dec 10, 2021
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 4, 2022
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

3 participants