uwplse / herbgrind

A Valgrind tool for Herbie
GNU General Public License v3.0
90 stars 7 forks source link

herbgrind segfaults for a sample program #55

Closed spraharsh closed 4 years ago

spraharsh commented 4 years ago

I tried compiling the following c++ program as a test case.

#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <stdio.h>

double add_numbers(double a, double b, double c) {
    double d = a+b+c;
    return d;
}

int main() {
    double a = 1e16;
    double b = 1;
    double c = -1e16;
    double d = add_numbers(a, b, c);
    // std::cout << d << "\n";
    printf("%e \n", d);
}

and compiled it with

g++ ./sample.cpp -g -lm -O0 -o demo

I ran ./demo giving me

0.000000e+00 as expected,

however, using herbgrind (I'm expanding the alias out)

alias herbgrind="$MYDIR/valgrind/herbgrind-install/bin/valgrind --tool=herbgrind"
herbgrind ./demo

gives me

==32306== Herbgrind, a valgrind tool for Herbie
==32306== Copyright (C) 2016-2017, and GNU GPL'd, by Alex Sanchez-Stern
==32306== Using Valgrind-3.15.0.GIT and LibVEX; rerun with -h for copyright info
==32306== Command: ./demo
==32306== 
0.000000e+00 
==32306== 
[1]    32306 segmentation fault (core dumped)  /home/praharsh/herbgrind//valgrind/herbgrind-install/bin/valgrind  ./demo

my herbgrind installation seems okay since running the sample code provided in diff-roots-simple.c works and I get the expected output

It might be an issue with the printf function since commenting out the printf and using std::cout fixes it partly in the sense that herbgrind gives an output. however demo.gh, the output says no marks found, which I do not expect, considering the exact result is 1 when I get 0.

HazardousPeach commented 4 years ago

Hey spraharsh,

Thanks for the bug report. I did some debugging, and it looks like gcc is unhappy with Herbgrind allocating very big text buffers on the stack in certain cases. Not sure why your code would trigger this issue and others wouldn't, since it seems pretty straightforward, but I fixed the issue by allocating the output text buffers on the heap instead of the stack.

The reason that switching to std::cout prevented the segfault is because Herbgrind doesn't support std::cout as an output method, so it sees the program as not producing any output, and therefore doesn't produce an error report (errors are reported in terms of the program outputs they influence).

You should see the fix on both master and develop now, as well as your test case added to the bench/ dir as spraharsh-test.cpp (if you would prefer I not include the test, or not mention you on it, I'm happy to change it).

Let me know if you have any further issues! Alex