Skip to content

Commit

Permalink
Add support for running Csmith output under KCC.
Browse files Browse the repository at this point in the history
  • Loading branch information
regehr committed Aug 5, 2011
1 parent a39f3c2 commit eaf9df0
Showing 1 changed file with 89 additions and 57 deletions.
146 changes: 89 additions & 57 deletions scripts/test_csmith.pl
Expand Up @@ -37,16 +37,20 @@
## TODO: run kcc and execute output

my $MIN_PROGRAM_SIZE = 8000;
my $EXTRA_OPTIONS = "";
my $CSMITH_HOME = $ENV{"CSMITH_HOME"};
my $COMPILER = "gcc -w -O0";

my $EXTRA_OPTIONS = "--no-unions";

#my $VALGRIND = "";
my $VALGRIND = "valgrind -q --error-exitcode=66 ";

#my $RUN_PROGRAM = 0;
my $RUN_PROGRAM = 1;

#my $RUN_KCC = 0;
my $RUN_KCC = 1;

#######################################################################

my $HEADER = "-I${CSMITH_HOME}/runtime";
Expand All @@ -70,70 +74,98 @@ ($)
}
}

sub run_tests ($) {
(my $n_tests) = @_;
my $n_good = 0;

my $n_good = 0;
sub run_test () {
my $cfile = "test.c";
my $ofile = "test.exe";
while ($n_tests == -1 || $n_good < $n_tests) {
system "rm -f $cfile";

my $CSMITH_OPTIONS = "";
if (rand()<0.5) { $CSMITH_OPTIONS .= " --quiet "; }
# if (rand()<0.5) { $CSMITH_OPTIONS .= " --concise "; }
$CSMITH_OPTIONS .= yesno ("math64");
$CSMITH_OPTIONS .= yesno ("paranoid");
$CSMITH_OPTIONS .= yesno ("longlong");
$CSMITH_OPTIONS .= yesno ("pointers");
$CSMITH_OPTIONS .= yesno ("arrays");
$CSMITH_OPTIONS .= yesno ("jumps");
$CSMITH_OPTIONS .= yesno ("consts");
$CSMITH_OPTIONS .= yesno ("volatiles");
$CSMITH_OPTIONS .= yesno ("volatile-pointers");
$CSMITH_OPTIONS .= yesno ("checksum");
$CSMITH_OPTIONS .= yesno ("divs");
$CSMITH_OPTIONS .= yesno ("muls");
$CSMITH_OPTIONS .= yesno ("compound-assignment");
$CSMITH_OPTIONS .= yesno ("structs");
$CSMITH_OPTIONS .= yesno ("packed-struct");
$CSMITH_OPTIONS .= yesno ("bitfields");
$CSMITH_OPTIONS .= yesno ("argc");

my $cmd = "$VALGRIND $CSMITH_HOME/src/csmith $CSMITH_OPTIONS $EXTRA_OPTIONS --output $cfile";
my $res = runit ($cmd, "csmith.out");
if ($res != 0 || !(-f $cfile) ) {
print "Failed to generate program: $cmd\n";
exit (-1);
}
my $filesize = stat($cfile)->size;
if ($filesize < $MIN_PROGRAM_SIZE) {
next;

system "rm -f $cfile";

my $CSMITH_OPTIONS = "";
if (rand()<0.5) { $CSMITH_OPTIONS .= " --quiet "; }
# if (rand()<0.5) { $CSMITH_OPTIONS .= " --concise "; }
$CSMITH_OPTIONS .= yesno ("math64");
$CSMITH_OPTIONS .= yesno ("paranoid");
$CSMITH_OPTIONS .= yesno ("longlong");
$CSMITH_OPTIONS .= yesno ("pointers");
$CSMITH_OPTIONS .= yesno ("arrays");
$CSMITH_OPTIONS .= yesno ("jumps");
$CSMITH_OPTIONS .= yesno ("consts");
$CSMITH_OPTIONS .= yesno ("volatiles");
$CSMITH_OPTIONS .= yesno ("volatile-pointers");
$CSMITH_OPTIONS .= yesno ("checksum");
$CSMITH_OPTIONS .= yesno ("divs");
$CSMITH_OPTIONS .= yesno ("muls");
$CSMITH_OPTIONS .= yesno ("compound-assignment");
$CSMITH_OPTIONS .= yesno ("structs");
$CSMITH_OPTIONS .= yesno ("packed-struct");
$CSMITH_OPTIONS .= yesno ("bitfields");
$CSMITH_OPTIONS .= yesno ("argc");

my $cmd = "$VALGRIND $CSMITH_HOME/src/csmith $CSMITH_OPTIONS $EXTRA_OPTIONS --output $cfile";
my $res = runit ($cmd, "csmith.out");
if ($res != 0 || !(-f $cfile) ) {
print "Failed to generate program: $cmd\n";
exit (-1);
}
my $filesize = stat($cfile)->size;
if ($filesize < $MIN_PROGRAM_SIZE) {
return;
}
system "grep Seed $cfile";
system "ls -l $cfile";
system "rm -f $ofile";
my $cmd2 = "$COMPILER $cfile $HEADER -o $ofile";
$res = runit ($cmd2, "csmith.out");
if ($res != 0 || !(-f $ofile)) {
print "Failed to compile program generated by $cmd\n";
exit (-1);
}

if ($RUN_PROGRAM) {
my $cmd3 = "RunSafely.sh 2 1 /dev/null out.txt $VALGRIND ./$ofile";
$res = runit ($cmd3, "out2.txt");
if ($res != 0 && $res != 137) {
print "Compiled random program died with error code $res\n";
exit (-1);
}
system "grep Seed $cfile";
system "ls -l $cfile";
system "rm -f $ofile";
my $cmd2 = "$COMPILER $cfile $HEADER -o $ofile";
$res = runit ($cmd2, "csmith.out");
if ($res == 137) {
print "Timed out.\n";
return;
}
}

if ($RUN_KCC) {

my $cmd3 = "kcc $cfile $HEADER -o $ofile";
$res = runit ($cmd3, "csmith.out");
if ($res != 0 || !(-f $ofile)) {
print "Failed to compile program generated by $cmd\n";
exit (-1);
print "KCC failed to compile program generated by $cmd\n";
exit (-1);
}

if ($RUN_PROGRAM) {
my $cmd3 = "RunSafely.sh 15 1 /dev/null out.txt $VALGRIND ./$ofile";
$res = runit ($cmd3, "out2.txt");
if ($res != 0 && $res != 137) {
print "Compiled random program died with error code $res\n";
exit (-1);
}
if ($res == 137) {
print "Timed out.\n";
}

my $cmd4 = "RunSafely.sh 600 1 /dev/null out.txt ./$ofile";
$res = runit ($cmd4, "out2.txt");
if ($res != 0 && $res != 137) {
print "KCC output died with error code $res\n";
exit (-1);
}
if ($res == 137) {
print "Timed out.\n";
return;
}
}

$n_good++;
print "finished with test case $n_good.\n\n";
}

$n_good++;
print "finished with test case $n_good.\n\n";
sub run_tests ($) {
(my $n_tests) = @_;

while ($n_tests == -1 || $n_good < $n_tests) {
run_test ();
}
}

Expand Down

0 comments on commit eaf9df0

Please sign in to comment.