platformio / platform-nordicnrf52

Nordic nRF52: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/nordicnrf52
Apache License 2.0
109 stars 120 forks source link

This code hangs in PlatformIO, not in Arduino IDE #153

Open steph643 opened 2 years ago

steph643 commented 2 years ago

Configuration

Operating system: Windows 11

PlatformIO Version: Core 6.0.2 - Home 3.4.2

Description of problem :

The code below targets an Arduino 33 BLE. It runs correctly when compiled and uploaded from the Arduino IDE, but it hangs when compiled and ran from PlatformIO.

Steps to Reproduce

  1. Run the code below

Actual Results

Terminal output:

before

Expected Results

Terminal output:

before 0.00 after

Source file to reproduce issue:

#include <Arduino.h>

struct MyStruct {
  short m1[7];
  float m2[3];
} __attribute__((packed));

MyStruct s;

const void* f() {
  return (const void*)&s;
}

void setup() {
  Serial.begin(115200);
  while (!Serial);

  const MyStruct* data = (const MyStruct*)f();
  const float* pm2 = data->m2;

  Serial.println("before");
  Serial.println(3 * pm2[2]);
  Serial.println("after");
}

void loop() {}

Additional info

As far as I can tell, this is a minimum repro code: changing one single element (size of the struct, casting through function f, final multiplication) leads to a working code.

cujomalainey commented 2 years ago

Given you are not initializing the struct, is it possible you have an invalid float that is causing a trap when you do your math?

steph643 commented 2 years ago

Thanks @cujomalainey . I added a memset(&s, 0, sizeof(MyStruct)); in setup() but the behavior is the same.

cujomalainey commented 2 years ago

If you don't multiply by 3 does it work?

cujomalainey commented 2 years ago

If you don't multiply by 3 does it work?

Also might be worth dumping the pointer to see where it's at in each version to see if something is amuck

steph643 commented 2 years ago

Also might be worth dumping the pointer

Nothing strange about pm2 pointers: 536875026 on Arduino, 536873010 on PlatformIO.

If you don't multiply by 3 does it work?

Yes it does.

cujomalainey commented 2 years ago

Also might be worth dumping the pointer

Nothing strange about pm2 pointers: 536875026 on Arduino, 536873010 on PlatformIO.

And what is the location of s?

If you don't multiply by 3 does it work?

Yes it does.

Hmm if the memset doesn't work then the float being invalid is likely not the issue

steph643 commented 2 years ago

And what is the location of s?

Nothing funny: Arduino: &s = 536875012, pm2 = 536875026 PlatformIO: &s = 536872996, pm2 = 536873010

Hmm if the memset doesn't work then the float being invalid is likely not the issue

On top of that, shouldn't an invalid float be considered as NaN and propagated in every float computation?

cujomalainey commented 2 years ago

Hmm if the memset doesn't work then the float being invalid is likely not the issue

On top of that, shouldn't an invalid float be considered as NaN and propagated in every float computation?

I don't worth floats very often in C, but I wouldn't leave anything to chance here. At this point I would recommend objdumping the assembly code in the .o files and see if there is a difference in the type of instruction used for the multiplication