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

Exceptions cause sys_exit in C++ #32

Closed
1 task done
nmaitland opened this issue Sep 25, 2020 · 0 comments · Fixed by #33
Closed
1 task done

Exceptions cause sys_exit in C++ #32

nmaitland opened this issue Sep 25, 2020 · 0 comments · Fixed by #33

Comments

@nmaitland
Copy link
Contributor

  • BUG REPORT

Background

This bug was discovered trying to use the fft library in the kendryte-freetos-sdk as it requires exceptions to find a spare dma. The example code consistently calls sys_exit when it's run on the MAIX One Dock.

Expected behavior

C++ exception should be caught by the nearest catch statement.

Actual behavior

Throwing an exception causes a fatal error and halt.

Test code

See https://gitlab.com/nemach-embedded/maix-pio-exceptions

main.c:

extern void do_exception_test();
int main(void) 
{
   do_exception_test();
   while (1)

        ;
    return 0;
}

exception_test.cpp:

#include <stdio.h>

void catch_test(bool do_throw)
{
    if (do_throw) { 
        printf("throwing exception\n");
        throw 20;
    }
    printf("not throwing exception\n");
}

extern "C" void do_exception_test()
{
    printf("Starting exception test\n");

    try {
        catch_test(false);
        printf("PASS - no exception thrown\n");
    }
    catch(...) {
        printf("ERROR - exception shouldn't be throw\n");
    }


    bool exception_caught = false;
    try {
        catch_test(true);
    }
    catch(...) {
        printf("PASS - exception should be throw\n");
        exception_caught = true;
    }

    if (!exception_caught) {
        printf("ERROR - exception should have been throw\n");
    }
}

Workaround

The problem can be fixed by manually moving crtend.o and crtn.o to the very end of the link command. I'm not sure how to make this the default though, or whether it could also be fixed in the kendryte.ld file.

Automatically generated command producing failing elf:

c:\Users\me\.platformio\packages\toolchain-kendryte210\bin\riscv64-unknown-elf-g++ -o .pio\build\sipeed-maix-one-dock\firmware.elf -T C:\Users\me\.platformio\packages\framework-kendryte-freertos-sdk\lds\kendryte.ld -nostartfiles -static -Wl,--gc-sections -Wl,-static -Wl,--start-group -Wl,--whole-archive -Wl,--no-whole-archive -Wl,--end-group -Wl,-EL -Wl,--no-relax -Wl,--start-group -lc -lgcc -lm -Wl,--end-group C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crti.o C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtbegin.o C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtend.o C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtn.o .pio\build\sipeed-maix-one-dock\src\exception_test.o .pio\build\sipeed-maix-one-dock\src\main.o -L.pio\build\sipeed-maix-one-dock -Wl,--start-group .pio\build\sipeed-maix-one-dock\libsdk-bsp.a .pio\build\sipeed-maix-one-dock\libsdk-drivers.a .pio\build\sipeed-maix-one-dock\libsdk-freertos.a .pio\build\sipeed-maix-one-dock\libsdk-hal.a .pio\build\sipeed-maix-one-dock\libsdk-posix.a .pio\build\sipeed-maix-one-dock\libthird_party-fatfs.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipcore.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipapi.a .pio\build\sipeed-maix-one-dock\libthird_party-lwiparch.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipnetif.a -lc -lgcc -lm -lstdc++ -latomic -Wl,--end-group

Modified command producing working elf:

c:\Users\me\.platformio\packages\toolchain-kendryte210\bin\riscv64-unknown-elf-g++ -o .pio\build\sipeed-maix-one-dock\firmware.elf -T C:\Users\me\.platformio\packages\framework-kendryte-freertos-sdk\lds\kendryte.ld -nostartfiles -static -Wl,--gc-sections -Wl,-static -Wl,--start-group -Wl,--whole-archive -Wl,--no-whole-archive -Wl,--end-group -Wl,-EL -Wl,--no-relax -Wl,--start-group -lc -lgcc -lm -Wl,--end-group C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crti.o C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtbegin.o .pio\build\sipeed-maix-one-dock\src\exception_test.o .pio\build\sipeed-maix-one-dock\src\main.o -L.pio\build\sipeed-maix-one-dock -Wl,--start-group .pio\build\sipeed-maix-one-dock\libsdk-bsp.a .pio\build\sipeed-maix-one-dock\libsdk-drivers.a .pio\build\sipeed-maix-one-dock\libsdk-freertos.a .pio\build\sipeed-maix-one-dock\libsdk-hal.a .pio\build\sipeed-maix-one-dock\libsdk-posix.a .pio\build\sipeed-maix-one-dock\libthird_party-fatfs.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipcore.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipapi.a .pio\build\sipeed-maix-one-dock\libthird_party-lwiparch.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipnetif.a -lc -lgcc -lm -lstdc++ -latomic -Wl,--end-group c:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtend.o C:\Users\me\.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtn.o

Tool/ Library Versions

CONFIGURATION: https://docs.platformio.org/page/boards/kendryte210/sipeed-maix-one-dock.html
PLATFORM: Kendryte K210 (1.2.2) > Sipeed MAIX ONE DOCK
HARDWARE: K210 400MHz, 6MB RAM, 16MB Flash

  • framework-kendryte-freertos-sdk 0.7.0
  • tool-kflash-kendryte210 0.9.1
  • tool-openocd-kendryte 1.203.1 (2.3)
  • toolchain-kendryte210 8.2.0

Target Hardware

MAIX One Dock

Host System

Windows 10 64-bit
VS Code 1.49.1

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

Successfully merging a pull request may close this issue.

1 participant