sparkfun / mbed-os-ambiq-apollo3

Arm Mbed OS is a platform operating system designed for the internet of things
https://mbed.com
Other
18 stars 14 forks source link

Functions that return a struct cause hardfault on the Artemis #38

Open idea--list opened 4 years ago

idea--list commented 4 years ago

Description of defect

When using functions that return struct the Artemis Thing Plus throws the following error message:

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0: 0
R1: 7FFFFFFE
R2: 7FFFFFFF
R3: 59
R4: 10000CE0
R5: 7FFFFFFE
R6: 0
R7: 26202
R8: B
R9: 100048F8
R10: C
R11: 10004938
R12: 7FFFFFFE
SP   : 100048C0
LR   : 26923
PC   : 26A86
xPSR : 81000000
PSP  : 10004858
MSP  : 1005FF80
CPUID: 410FC241
HFSR : 40000000
MMFSR: 0
BFSR : 82
UFSR : 0
DFSR : 0
AFSR : 0
BFAR : 59
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x26A86
Error Value: 0x10006150
Current Thread: main Id: 0x100064F0 Entry: 0x26D7D StackSize: 0x1000 StackMem: 0x10003958 SP: 0x100048C0 
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=SFE_ARTEMIS_THING_PLUS
-- MbedOS Error Info --

The same code runs without any errors on my MAX32630FTHR

Target(s) affected by this defect ?

Artemis Thing Plus (and i guess all the other artemis boards might be affected too)

Toolchain(s) (name and version) displaying this defect ?

ARMC 6.14

What version of Mbed-os are you using (tag or sha) ?

Mbed OS V6.3

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

Mbed Studio V1.3.0

How is this defect reproduced ?

Compile&flash this code:

#include "mbed.h"
#include <cstdio>

struct a_tag {
   char c;
   int i;
   char *w;
};

// return_struct: return a struct by value (copied)
struct a_tag return_struct(void) {
   struct a_tag a;      //create a temporary a_tag struct
   a.c = 'Y';
   a.i = 88;
   a.w = "My dog has fleas";
   return a;
}

int main()
{
    struct a_tag myTag;
    while (true) {
        myTag = return_struct();
        printf("Character: %s\tinteger: %d\tstring: %s\n", myTag.c, myTag.i, myTag.w);
        ThisThread::sleep_for(1000ms);
    }
}
idea--list commented 3 years ago

Revisited the issue again. I find this code works:

#include "mbed.h"
#include <cstdio>

struct a_tag {
   int i;
   int n;
};

// return_struct: return a struct by value (copied)
struct a_tag return_struct(void) {
   struct a_tag a;      //create a temporary a_tag struct
   a.i = 88;
   a.n = 99;
   return a;
}

int main()
{
    struct a_tag myTag;
    while (true) {
        myTag = return_struct();
        printf("integer: %d\tinteger: %d\n", myTag.i, myTag.n);
        ThisThread::sleep_for(1000ms);
    }
}

So it seems the issue is only introduced if the struct contains character or string.

idea--list commented 3 years ago

Still the same with Mbed OS 6.15 regardless of compiler (ARMC6.16 or GCC_ARM 10.3-2021.07), no improvement with recent releases.