jeudi 29 juin 2017

"this" captured by lambda is incorrect. GCC compiler bug?

For the last few days, I have been debugging a weird issue involving lambdas in C++. I have reduced the problem down to the following symptoms:

  • The this pointer gets corrupted inside a lambda (note: this is always captured by copy, so the lambda should get it's own this pointer, which points to the App object)
  • Only occurs if a std::cout print statement is present, and called before the lambda is created. The print statement can be seemingly completely unrelated (e.g. print "Hello!"). printf() also exhibits the same behaviour.
  • Only occurs when cross-compiling
  • Compiles and runs fine with standard compiler for x86 architecture (see example at http://cpp.sh/9ru5k)
  • If I create the lambda on the heap (and save a pointer to it inside the App object), the bug does not occur

The following is the simplest, compilable code example I could come up with which causes the problem.

#include <iostream>
#include <functional>

class App {

public:

    std::function<void*()> test_;

    void Run() {

        // Enable this line, ERROR is printed
        // Disable this line, app runs o.k.
        std::cout << "This print statement causes the bug below!" << std::endl;

        test_ = [this] () {
            return this;
        };

        void* returnedThis = test_();
        if(returnedThis != this) {
            std::cout << "ERROR: 'this' returned from lambda (" << returnedThis << ") is NOT the same as 'this' (" << this << ") !?!?!?!?!" << std::endl;
        } else {
            std::cout << "Program run successfully." << std::endl;
        }

    }
};

int main(void) {
    App app;
    app.Run();
}

When running on the target device, I get the following output:

This print statement causes the bug below!
ERROR: 'this' returned from lambda (0xbec92dd4) is NOT the same as 'this' 
(0xbec92c68) !?!?!?!?!

If I try and dereference this corrupted this I usually get a seg fault, which is how I discovered the bug in the first place.

The compiler settings are:

arm-poky-linux-gnueabi-g++ -march=armv7-a -marm -mfpu=neon -std=c++14 \
-mfloat-abi=hard -mcpu=cortex-a9 --sysroot=/home/ghunter/sysroots/cortexa9hf-neon-poky-linux-gnueabi \
-O2 -pipe -g -feliminate-unused-debug-types

The linker settings are:

arm-poky-linux-gnueabi-ld --sysroot=/home/ghunter/sysroots/cortexa9hf-neon-poky-linux-gnueabi \
-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed

The version of the compiler is:

~$ arm-poky-linux-gnueabi-g++ --version

arm-poky-linux-gnueabi-g++ (GCC) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Could this a compiler bug?

Aucun commentaire:

Enregistrer un commentaire