Skip to main content
Zanon.Luciano
Associate III
March 19, 2020
Question

Calling rand() on Cortex-M3 produces hard fault due to malloc() error (SOLVED).

  • March 19, 2020
  • 14 replies
  • 3250 views

Very strange behavior, the instruction:

uint32_t xid;

xid = rand();

cause hard fault due to unaligned address.

I am using Atollic TrueSTUDIO Version: 9.3.0 

This topic has been closed for replies.

14 replies

Uwe Bonnes
Chief
March 19, 2020

Maybe you run M3 code on a M0? Maybe the rand() implementation that TrueSTUDIO uses is broken?

Zanon.Luciano
Associate III
March 19, 2020

This problem occurs on M3 and also on M0, and seems related to the configuration of the project because on some projects it works without problems and on others it doesn't.

I just don't understand why and I still can't give more precise indications ...

TDK
Super User
March 19, 2020

Most likely the rand() library you're using is not meant for your processor instruction set.

You should step into the rand() code to find the offending instruction. It's possible rand() is fine and the hardfault is from an interrupt.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Zanon.Luciano
Associate III
March 19, 2020

Hello TDK, the library is that of atollic and cannot be an interrupt problem because moving rand () at any point of the program, always crashes during execution of the rand () instruction.

But my program places the stack (0x400 bytes) at 0x20000400 (by myprogram.ld file) and in this situation rand () generates the crash in the statement str r2, [r0, # 16] (where r2 = 1).

If I move the stack to the top of memory then rand () works correctly !!!!!

This is crazy ...

Anyone have any ideas?

TDK
Super User
March 19, 2020
It sounds like you could be overflowing the stack. Increase the size. Does the r0 register have a valid memory address?
"If you feel a post has answered your question, please click ""Accept as Solution""."
Zanon.Luciano
Associate III
March 19, 2020

R0 contains 0 (invalid address) and this causes the crash.

It's not a stack size issue because this also happens with the larger stack and also after the crash the stack area is still clean.

I'm continuing the tests ....

By the way, with the keil compiler everything works fine ...

TDK
Super User
March 19, 2020

Interesting. Might be hard to debug without knowing what rand() is doing. Good luck. Keil is probably using a different implementation of rand().

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
March 19, 2020

In mixed disasm/source view, the the offending instruction to respective source line. It's often straightforward to see, which variable maps to which register. Here, I'd say, you have a zero pointer, which was created elsewhere - maybe failed unchecked malloc()? - and just coincidentally happened to fault in rand().

JW

Zanon.Luciano
Associate III
March 19, 2020

Hi JW it seems you're right, the first call to malloc () inside rand () returns R0 = 0 and then the crash ...

Do you have any idea why malloc () fails?

waclawek.jan
Super User
March 19, 2020

No. Inadequately set heap size?

I don't use IAR, and I consider unrestricted use of dynamic memory allocation in microcontrollers to be a methodology failure.

JW

Zanon.Luciano
Associate III
March 20, 2020

I'm going crazy, ........... The heap is ok and i am working in Atollic environment.

Is it possible that having the stack at the bottom of the ram and the heap in a higher region could cause a malloc protection error?

And if so, what is the way to disable this protection?

I found the MALLOC_CHECK_ variable that could work but I don't know how to use it in an atollic environment ....