Page 1 of 1

float corrupted when passed into variable argument list

Posted: Thu Jan 05, 2012 8:01 pm
by Badger
I'm not sure if this problem is related to the byte alignment of chibios or just a general GCC related problem.. but here goes!

I'm trying to printf() some floats. From the main thread, everything works as expected. However, if I try to print a float from within another thread, it gets corrupted. For example, here's a little function which gets a pointer to the variable argument list as an offset from the first argument, then prints out the bytes:

Code: Select all

void float_test(int ignored, ...) {
   int *varg = (int *) (char *) (&ignored);
   varg++;
   unsigned char *adr = (unsigned char *)(varg++);
   for(int i=0; i < 8; i++)
      cPrintf("%x ", adr[i]);
   cPrint("\r\n");
}

Then I call it in a few threads (main thread is first row)

Code: Select all

float_test(10, 0.045f);

With output:

Code: Select all

38 28 0 20 0 0 0 80
0 28 0 20 0 0 0 80
30 13 0 20 0 0 0 80

This behaviour is consistent for other values. E.g 0.087:

Code: Select all

38 28 0 20 0 0 0 c0
0 28 0 20 0 0 0 c0
30 13 0 20 0 0 0 c0

So it looks as if the first two bytes get corrupted somehow. I replaced my ld script with the latest one to make sure I haven't messed something up there, but the same thing happened. Anyone got an ideas what might be causing this?

Re: float corrupted when passed into variable argument list

Posted: Thu Jan 05, 2012 8:15 pm
by Giovanni
It could be a stack alignment problem, I have to look into this. Can you create a test case?

Giovanni

Re: float corrupted when passed into variable argument list

Posted: Thu Jan 05, 2012 8:48 pm
by Badger
I wrote this quick test case which is based off of the STM32F1 discovery demo:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "math.h"

void float_test(GPIO_TypeDef *port, int pin, ...) {
   int *varg = (int *) (char *) (&pin);
   varg++;
   // http://wiki.debian.org/ArmEabiPort#Structpackingandalignment
   // Stack alignment
   //
   // The ARM EABI requires 8-byte stack alignment at public function entry points,
   // compared to the previous 4-byte alignment.
    char *cptr = (char *) varg ;  //lint !e740 !e826  convert to double pointer
   unsigned int caddr = (unsigned int) cptr ;
   if ((caddr & 0xF) != 0) {
      cptr += 4 ;
   }
   double *dblptr = (double *) cptr ;  //lint !e740 !e826  convert to double pointer
   float val = *dblptr;

   if(abs(val-0.032) < 0.000001) {
      /* looks like the value was passed in correctly */
      palSetPad(port, pin);
   }
}
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
  (void)arg;
  chRegSetThreadName("counter");

  float_test(GPIOC, GPIOC_LED3, 0.032);
  while (TRUE) {
     chThdSleepMilliseconds(100);
  }
}

int main(void) {
  halInit();
  chSysInit();

  palClearPad(GPIOC, GPIOC_LED3);
  palClearPad(GPIOC, GPIOC_LED4);

  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  float_test(GPIOC, GPIOC_LED4, 0.032);
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
}


In the demo, a float value is passed into the float_test function via a variable argument. The same value is hard coded into the function, and the absolute difference between the passed in value and the hard coded value is found. If this is < 0.000001 then it is assumed that the value was passed in correctly, and an led lights up. Otherwise the led remains unlit. There are two LEDs on the discovery board, so the test is performed from the main thread and a secondary thread. It seems to only succeed from the secondary thread.

Note that I am not using va_list() functions because I want to reduce external dependencies. But I tested using these functions as well and the problem remained.

Re: float corrupted when passed into variable argument list

Posted: Wed Feb 22, 2012 9:36 pm
by Badger
I wonder if this might be related:

Many Cortex-M3 CPU cores suffer from a design flaw that can cause data corruption when the first destination and base register of an ldrd instruction overlap (e.g. ldrd r0, [r0]). The patch below adds a new option to avoid these problematic cases, and enables it by default when -mcpu=cortex-M3 is specified.


I haven't had the time to test it yet, but I'm making a note of it here so I don't forget

Re: float corrupted when passed into variable argument list

Posted: Thu Feb 23, 2012 1:14 am
by utzig
I found that same thread when searching for float errors on cortex-m3. I didn't solve the problem for me.