emil-e/rapidcheck

Segmentation fault when program is falsified, when compiling with optimizations.

Xvdgeest opened this issue · 5 comments

The following example of a falsifiable function gives a segfault when the RC_ASSERT receives FALSE:

#include<iostream>
#include<rapidcheck.h>

int myadd(int x,int y){
    if(x==7){return 1;}
    else{
        return x+y;
    }
}
void issame(int x,int y){


    bool res = myadd(x,y)==myadd(y,x); 
    
    std::cout<<x<<" , "<<y<<" ---> "<<myadd(x,y)<<" , "<<myadd(y,x)<<" res: "<<res<<std::endl;
    
    RC_ASSERT(res);

}

int main(){

    rc::check("issame",issame);
}

Sometimes, 100 tests run without error (I.E. x hasn't been 7 in these 100 tests). When the function is falsified, it returns a segfault. Note that it doesn't stop the program right away.

Output:

Using configuration: seed=13664549568846329301

- issame
0 , 0 ---> 0 , 0 res: 1
0 , 0 ---> 0 , 0 res: 1
0 , 0 ---> 0 , 0 res: 1
-1 , 0 ---> -1 , -1 res: 1
0 , -1 ---> -1 , -1 res: 1
-1 , -2 ---> -3 , -3 res: 1
-1 , 1 ---> 0 , 0 res: 1
0 , 1 ---> 1 , 1 res: 1
0 , 2 ---> 2 , 2 res: 1
-1 , 1 ---> 0 , 0 res: 1
-1 , 3 ---> 2 , 2 res: 1
-1 , 7 ---> 6 , 1 res: 0
-1 , 7 ---> 6 , 1 res: 0
0 , 7 ---> 7 , 1 res: 0
0 , 7 ---> 7 , 1 res: 0
0 , 0 ---> 0 , 0 res: 1
0 , 4 ---> 4 , 4 res: 1
0 , 6 ---> 6 , 6 res: 1
0 , 7 ---> 7 , 1 res: 0
Segmentation fault (core dumped)

This bug has to do with compiling with optimizing settings. I was compiling in release mode (-O3).
The crash happens in GCC 8 with any optimization, I.E. -O1 also creates this crash. I didn't try -O0.
For now, I have disabled the release mode flags in CMakeLists to make it work.

Same issue for me (with gcc 7.4.0): any failure seems to result in a segfault when the program is built in release mode (with -O3).

Is it possible that you've built rapidcheck with RTTI support enabled, but are not setting the RC_USE_RTTI compiler definition when building your tests? Because that was the issue when I tried to fix this bug just now.

Is it possible that you've built rapidcheck with RTTI support enabled, but are not setting the RC_USE_RTTI compiler definition when building your tests?

Yes! Thank you. It seems that was the issue.

Yes, same here. Thanks @Corristo !