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

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

Open
steph643 opened this issue Jul 4, 2022 · 8 comments
Open

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

steph643 opened this issue Jul 4, 2022 · 8 comments

Comments

@steph643
Copy link

steph643 commented Jul 4, 2022

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.

@ivankravets ivankravets transferred this issue from platformio/platformio-core Jul 4, 2022
@cujomalainey
Copy link

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
Copy link
Author

steph643 commented Jul 4, 2022

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

@cujomalainey
Copy link

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

@cujomalainey
Copy link

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
Copy link
Author

steph643 commented Jul 4, 2022

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
Copy link

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
Copy link
Author

steph643 commented Jul 5, 2022

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
Copy link

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

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

No branches or pull requests

2 participants