diff options
author | B. Watson <urchlay@slackware.uk> | 2023-06-17 07:52:18 +0700 |
---|---|---|
committer | Willy Sudiarto Raharjo <willysr@slackbuilds.org> | 2023-06-17 07:52:18 +0700 |
commit | 49853566bb6ac10e9003d6b703050437e2fd1511 (patch) | |
tree | 149e6a20e6556d5bc1154c56fcd599c84f29f0ef /development/csmith | |
parent | bbbedab4e2f99511ba667d32e74a78c27dd991f4 (diff) | |
download | slackbuilds-49853566bb6ac10e9003d6b703050437e2fd1511.tar.gz slackbuilds-49853566bb6ac10e9003d6b703050437e2fd1511.tar.xz |
development/csmith: Added (generates random C programs).
Signed-off-by: Willy Sudiarto Raharjo <willysr@slackbuilds.org>
Diffstat (limited to 'development/csmith')
-rw-r--r-- | development/csmith/README | 12 | ||||
-rw-r--r-- | development/csmith/compiler_test.diff | 320 | ||||
-rw-r--r-- | development/csmith/csmith.SlackBuild | 120 | ||||
-rw-r--r-- | development/csmith/csmith.info | 10 | ||||
-rw-r--r-- | development/csmith/launchn.diff | 42 | ||||
-rw-r--r-- | development/csmith/slack-desc | 19 |
6 files changed, 523 insertions, 0 deletions
diff --git a/development/csmith/README b/development/csmith/README new file mode 100644 index 0000000000..c244b3c26b --- /dev/null +++ b/development/csmith/README @@ -0,0 +1,12 @@ +csmith (generates random C programs) + +Csmith is a random generator of C programs. Its primary purpose is to +find compiler bugs with random programs, using differential testing as +the test oracle. Csmith outputs C programs free of undefined behaviors +(believe us, that's not trivial), and the statistics of each generated +program (as comments at the end of the code). + +It is useful for stress-testing compilers, static analyzers, and other +tools that process C code. Csmith has found bugs in every tool that +it has tested, and has been used to find and report more than 400 +previously unknown compiler bugs. diff --git a/development/csmith/compiler_test.diff b/development/csmith/compiler_test.diff new file mode 100644 index 0000000000..5351e2f4c3 --- /dev/null +++ b/development/csmith/compiler_test.diff @@ -0,0 +1,320 @@ +diff -Naur csmith-csmith-2.3.0/scripts/compiler_test.pl csmith-csmith-2.3.0.patched/scripts/compiler_test.pl +--- csmith-csmith-2.3.0/scripts/compiler_test.pl 2017-06-21 16:50:24.000000000 -0400 ++++ csmith-csmith-2.3.0.patched/scripts/compiler_test.pl 2023-06-10 17:08:54.342868470 -0400 +@@ -29,7 +29,7 @@ + + ################################################################# + +-use strict; ++use strict; + use File::stat; + + ################################################################# +@@ -39,7 +39,7 @@ + my $MIN_PROGRAM_SIZE = 8000; + + # kill Csmith after this many seconds +-my $CSMITH_TIMEOUT = 90; ++my $CSMITH_TIMEOUT = 90; + + # kill a compiler after this many seconds + my $COMPILER_TIMEOUT = 120; +@@ -48,7 +48,7 @@ + my $PROG_TIMEOUT = 8; + + # extra options here +-my $CSMITH_USER_OPTIONS = " --bitfields --packed-struct"; ++my $CSMITH_USER_OPTIONS = " --bitfields --packed-struct"; + + ################# end user-configurable stuff ################### + ################################################################# +@@ -56,7 +56,7 @@ + ################################################################# + # TODO + # +-# - make it easy to plugin an emulator for testing embedded compilers ++# - make it easy to plugin an emulator for testing embedded compilers + # - automatically fire up a reducer when a bug is found + # - support "reference compilers" that supply checksums but that we're + # not testing +@@ -66,14 +66,13 @@ + + my $RUN_PROGRAM = 0; + +-my $CSMITH_HOME = $ENV{"CSMITH_HOME"}; +-my $good = 0; ++my $good = 0; + my $crash_bug = 0; + my $wrongcode_bug = 0; + my $csmith_bug = 0; + +-my $HEADER = "-I$CSMITH_HOME/runtime"; +-my $CYGWIN_HEADER = "-I`cygpath -d ${CSMITH_HOME}/runtime`"; ++my $HEADER = "-I/usr/include/csmith"; ++my $CYGWIN_HEADER = "-I`cygpath -d /usr/include/csmith`"; + my $COMPILE_OPTIONS = ""; + my @COMPILERS; + +@@ -81,11 +80,11 @@ + my ($fn, $match) = @_; + open INF, "<$fn" or die; + while (my $line = <INF>) { +- $line =~ s/\r?\n?$//; # get rid of LF/CR ++ $line =~ s/\r?\n?$//; # get rid of LF/CR + if ($line =~ /$match/) { + close INF; + return $1; +- } ++ } + } + close INF; + return ""; +@@ -100,14 +99,14 @@ + + # properly parse the return value from system() + sub runit ($$$) { +- my ($cmd, $timeout, $out) = @_; ++ my ($cmd, $timeout, $out) = @_; + my $res; + if ($RUN_PROGRAM) { + $res = system "timeout $timeout $cmd > $out 2>&1"; + } else { + $res = system "$cmd > $out 2>&1"; + } +- my $success = 0; ++ my $success = 0; + if ($? == -1) { + print "can't execute $cmd\n"; + } +@@ -129,36 +128,36 @@ + } + + # compile a program and execute +-# return code 0: normal; +-# 1: compiler crashes; +-# 2: compiler hangs; +-# 3: executable crashes; ++# return code 0: normal; ++# 1: compiler crashes; ++# 2: compiler hangs; ++# 3: executable crashes; + # 4: executable hangs + sub compile_and_run($$$$) { +- my ($compiler, $src_file, $exe, $out) = @_; +- my $command = "$compiler $src_file $COMPILE_OPTIONS $HEADER -o $exe"; ++ my ($compiler, $src_file, $exe, $out) = @_; ++ my $command = "$compiler $src_file $COMPILE_OPTIONS $HEADER -o $exe"; + + my @a = split(" ", $compiler); + # special treatment of MS compiler: convert header path to unix-style + if ($a[0] =~ /cl$/) { +- $command = "$compiler $src_file $COMPILE_OPTIONS $CYGWIN_HEADER -o $exe"; +- } ++ $command = "$compiler $src_file $COMPILE_OPTIONS $CYGWIN_HEADER -o $exe"; ++ } + + # compile random program +- my ($res, $exit_value) = runit($command, $COMPILER_TIMEOUT, "compiler.out"); ++ my ($res, $exit_value) = runit($command, $COMPILER_TIMEOUT, "compiler.out"); + # print "after run compiler: $res, $exit_value\n"; + if (($res == 0) || (!(-e $exe))) { + # exit code 124 means time out +- return ($exit_value == 124 ? 2 : 1); ++ return ($exit_value == 124 ? 2 : 1); + } + +- # run random program ++ # run random program + if ($RUN_PROGRAM) { + ($res, $exit_value) = runit("./$exe", $PROG_TIMEOUT, $out); + # print "after run program: $res, $exit_value\n"; + if (($res == 0) || (!(-e $out))) { + # exit code 124 means time out +- return ($exit_value == 124 ? 4 : 3); ++ return ($exit_value == 124 ? 4 : 3); + } + } + return 0; +@@ -169,35 +168,35 @@ + # -1: hangs (not interesting) + # 0: normal, but found no compiler error (not interesting) + # 1: found compiler crash error(s) +-# 2: found compiler wrong code error(s) ++# 2: found compiler wrong code error(s) + sub evaluate_program ($) { +- my ($test_file) = @_; ++ my ($test_file) = @_; + my @checksums; +- my @tested_compilers; ++ my @tested_compilers; + my $interesting = 0; +- my $i = 0; +- foreach my $compiler (@COMPILERS) { ++ my $i = 0; ++ foreach my $compiler (@COMPILERS) { + my $out = "out$i.log"; + my $exe = "a.out$i"; +- $i++; ++ $i++; + my $res = compile_and_run($compiler, $test_file, $exe, $out); + + if ($res) { +- if ($res == 1 || $res == 2) { +- write_bug_desc_to_file($test_file, +- "Compiler error! Can't compile with $compiler $COMPILE_OPTIONS $HEADER"); ++ if ($res == 1 || $res == 2) { ++ write_bug_desc_to_file($test_file, ++ "Compiler error! Can't compile with $compiler $COMPILE_OPTIONS $HEADER"); + $interesting = 1; + } +- elsif ($res == 3) { +- write_bug_desc_to_file($test_file, "random program crashed!"); ++ elsif ($res == 3) { ++ write_bug_desc_to_file($test_file, "random program crashed!"); + # random program crashes, a likely wrong-code bug, but + # can't rule out the probablity of a Csmith bug +- $interesting = -2; ++ $interesting = -2; + last; + } else { +- print "random program hangs!\n"; ++ print "random program hangs!\n"; + # program hangs, not interesting +- $interesting = -1; ++ $interesting = -1; + last; + } + } +@@ -205,23 +204,23 @@ + if ($RUN_PROGRAM) { + die "cannot find $out.\n" if (!(-e $out)); + my $sum = read_value_from_file($out, "checksum = (.*)"); +- $interesting = 2 if +- (scalar(@checksums) > 0 && $sum ne $checksums[0]); ++ $interesting = 2 if ++ (scalar(@checksums) > 0 && $sum ne $checksums[0]); + push @checksums, $sum; + push @tested_compilers, "$compiler $COMPILE_OPTIONS"; +- } ++ } + } +- } ++ } + if ($interesting >= 1) { +- if ($interesting == 2) { +- write_bug_desc_to_file ($test_file, +- "Found checksum difference between compiler implementations"); ++ if ($interesting == 2) { ++ write_bug_desc_to_file ($test_file, ++ "Found checksum difference between compiler implementations"); + for (my $i=0; $i < scalar (@checksums); $i++) { +- write_bug_desc_to_file ($test_file, ++ write_bug_desc_to_file ($test_file, + "$tested_compilers[$i]: $checksums[$i]"); + } + } +- write_bug_desc_to_file($test_file, ++ write_bug_desc_to_file($test_file, + "please refer to http://embed.cs.utah.edu/csmith/using.html on how to report a bug"); + } + system "rm -f out*.log a.out* test*.obj compiler.out csmith.out"; +@@ -237,30 +236,30 @@ + # run Csmith until generate a big enough program + while (1) { + unlink $cfile; +- my $cmd = "$CSMITH_HOME/src/csmith $CSMITH_USER_OPTIONS --output $cfile"; +- my ($res, $exitcode) = runit($cmd, $CSMITH_TIMEOUT, "csmith.out"); ++ my $cmd = "csmith $CSMITH_USER_OPTIONS --output $cfile"; ++ my ($res, $exitcode) = runit($cmd, $CSMITH_TIMEOUT, "csmith.out"); + # print "after run csmith: $res, $exitcode\n"; +- +- $seed = read_value_from_file($cfile, "Seed:\\s+([0-9]+)"); +- die "Random program $cfile has no seed information!\n" if (!$seed); ++ ++ $seed = read_value_from_file($cfile, "Seed:\\s+([0-9]+)"); ++ die "Random program $cfile has no seed information!\n" if (!$seed); + + if ($res == 0) { + print "CSMITH BUG FOUND: number $csmith_bug\n"; + $csmith_bug++; +- system "cp $cfile csmith_bug_${csmith_bug}.c"; +- next; ++ system "cp $cfile csmith_bug_${csmith_bug}.c"; ++ next; + } +- else { ++ else { + $filesize = stat("$cfile")->size; +- # print "$cfile is $filesize bytes\n"; ++ # print "$cfile is $filesize bytes\n"; + last if ($filesize >= $MIN_PROGRAM_SIZE); + } + } + + print "seed= $seed, size= $filesize\n"; +- ++ + # test if the random program is interesting +- my $ret = evaluate_program($cfile); ++ my $ret = evaluate_program($cfile); + if ($ret >= 0) { + $good++; + print "GOOD PROGRAM: number $good\n"; +@@ -273,27 +272,21 @@ + print "LIKELY WRONG CODE ERROR FOUND: number $wrongcode_bug\n"; + $wrongcode_bug++; + system "cp $cfile wrong${wrongcode_bug}.c"; +- } +- } else { ++ } ++ } else { + print "BAD PROGRAM: doesn't count towards goal.\n"; +- } ++ } + unlink $cfile; + return $ret; + } + + sub usage () { +- print "usage: compiler_test.pl <test_case_count>(0 for unlimited) <config-file>\n"; ++ print "usage: compiler_test <test_case_count>(0 for unlimited) <config-file>\n"; + exit -1; + } + + ########################### main ################################## + +-if (!(-f "$CSMITH_HOME/runtime/csmith.h")) { +- print "Please point the environment variable CSMITH_HOME to the top-level\n"; +- print "directory of your Csmith tree before running this script.\n"; +- exit(-1); +-} +- + my $nargs = scalar(@ARGV); + + if ($nargs == 2) { +@@ -323,12 +316,12 @@ + open INF, "<$infile" or die "Cannot read configuration file ${infile}.\n"; + while (my $line = <INF>) { + chomp $line; +- if ($line && !($line =~ /^\s*#/)) { +- my $res = system ("echo \"int main() { return 0;}\" > foo.c ; $line foo.c > /dev/null 2>&1"); ++ if ($line && !($line =~ /^\s*#/)) { ++ my $res = system ("echo \"int main() { return 0;}\" > foo.c ; $line foo.c > /dev/null 2>&1"); + unlink "foo.c", "a.out"; +- die "cannot execute compiler $line\n" if ($res); ++ die "cannot execute compiler $line\n" if ($res); + push @COMPILERS, $line; +- } ++ } + } + close INF; + +@@ -339,7 +332,7 @@ + $i++; + } + print "\n"; +-} ++} + + print "Total csmith errors found: $csmith_bug\n"; + print "Total crash errors found: $crash_bug\n"; diff --git a/development/csmith/csmith.SlackBuild b/development/csmith/csmith.SlackBuild new file mode 100644 index 0000000000..9655514b23 --- /dev/null +++ b/development/csmith/csmith.SlackBuild @@ -0,0 +1,120 @@ +#!/bin/bash + +# Slackware build script for csmith + +# Written by B. Watson (urchlay@slackware.uk) + +# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details. + +# Package layout based on (but not identical to) the Debian packages. As +# usual, they break it up into 3 packages: +# https://packages.debian.org/sid/csmith +# https://packages.debian.org/sid/libcsmith0 +# https://packages.debian.org/sid/libcsmith-dev + +## TODO: man pages. or not... + +cd $(dirname $0) ; CWD=$(pwd) + +PRGNAM=csmith +VERSION=${VERSION:-2.3.0} +BUILD=${BUILD:-1} +TAG=${TAG:-_SBo} +PKGTYPE=${PKGTYPE:-tgz} + +if [ -z "$ARCH" ]; then + case "$( uname -m )" in + i?86) ARCH=i586 ;; + arm*) ARCH=arm ;; + *) ARCH=$( uname -m ) ;; + esac +fi + +if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then + echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE" + exit 0 +fi + +TMP=${TMP:-/tmp/SBo} +PKG=$TMP/package-$PRGNAM +OUTPUT=${OUTPUT:-/tmp} + +if [ "$ARCH" = "i586" ]; then + SLKCFLAGS="-O2 -march=i586 -mtune=i686" + LIBDIRSUFFIX="" +elif [ "$ARCH" = "i686" ]; then + SLKCFLAGS="-O2 -march=i686 -mtune=i686" + LIBDIRSUFFIX="" +elif [ "$ARCH" = "x86_64" ]; then + SLKCFLAGS="-O2 -fPIC" + LIBDIRSUFFIX="64" +else + SLKCFLAGS="-O2" + LIBDIRSUFFIX="" +fi + +set -e + +rm -rf $PKG +mkdir -p $TMP $PKG $OUTPUT +cd $TMP +rm -rf $PRGNAM-$PRGNAM-$VERSION +tar xvf $CWD/$PRGNAM-$PRGNAM-$VERSION.tar.gz +cd $PRGNAM-$PRGNAM-$VERSION +chown -R root:root . +find -L . -perm /111 -a \! -perm 755 -a -exec chmod 755 {} + -o \ + \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} + + +# Fix the compiler_test and launchn scripts to use a system-wide install. +# Also remove the perl-Sys-CPU dependency in launchn (just call `nproc` +# to get the # of cores). +patch -p1 < $CWD/compiler_test.diff +patch -p1 < $CWD/launchn.diff + +# Don't see a way to override this on the cmake command line: +sed -i "/(LIB_DIR/s,/lib,/lib$LIBDIRSUFFIX," runtime/CMakeLists.txt + +mkdir -p build +cd build + cmake \ + -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \ + -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_BUILD_TYPE=Release .. + make + make install/strip DESTDIR=$PKG +cd .. + +# Cleanup on aisle six! +# Nothing in $PKG/usr/share except doc (we make our own doc dir, below). +# No way to disable the static lib, so rm it. +# Somehow the binary's immune to 'install/strip' (though the shared lib ain't). +strip $PKG/usr/bin/$PRGNAM +rm -rf $PKG/usr/lib*/*.a $PKG/usr/share + +# Rename this stuff to match the Debian package. +rename .pl '' $PKG/usr/bin/*.pl +mv $PKG/usr/include/$PRGNAM-$VERSION $PKG/usr/include/$PRGNAM + +# The original site is down currently, use a working URL here: +( source $CWD/$PRGNAM.info + sed -i "s,http://embed\.cs.*,$HOMEPAGE," README ) + +PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION +mkdir -p $PKGDOC +cp -a doc/{pa,probabilities}.txt \ + AUTHORS BUGS_REPORTED.TXT COPYING \ + ChangeLog NEWS README TODO \ + $PKGDOC +cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild + +# Debian doesn't package the sample config, but it seems like a good idea. +CONF=$PKGDOC/compiler_test.conf.sample +mv $PKG/usr/bin/compiler_test.in $CONF +chmod 644 $CONF + +mkdir -p $PKG/install +cat $CWD/slack-desc > $PKG/install/slack-desc + +cd $PKG +/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE diff --git a/development/csmith/csmith.info b/development/csmith/csmith.info new file mode 100644 index 0000000000..298fcb017f --- /dev/null +++ b/development/csmith/csmith.info @@ -0,0 +1,10 @@ +PRGNAM="csmith" +VERSION="2.3.0" +HOMEPAGE="http://web.archive.org/web/20230323203147/http://embed.cs.utah.edu/csmith/" +DOWNLOAD="https://github.com/csmith-project/csmith/archive/csmith-2.3.0/csmith-csmith-2.3.0.tar.gz" +MD5SUM="1749ba1d7938b6dc1ba984bf61e7679f" +DOWNLOAD_x86_64="" +MD5SUM_x86_64="" +REQUIRES="" +MAINTAINER="B. Watson" +EMAIL="urchlay@slackware.uk" diff --git a/development/csmith/launchn.diff b/development/csmith/launchn.diff new file mode 100644 index 0000000000..e7781b212a --- /dev/null +++ b/development/csmith/launchn.diff @@ -0,0 +1,42 @@ +diff -Naur csmith-csmith-2.3.0/scripts/launchn.pl csmith-csmith-2.3.0.patched/scripts/launchn.pl +--- csmith-csmith-2.3.0/scripts/launchn.pl 2017-06-21 16:50:24.000000000 -0400 ++++ csmith-csmith-2.3.0.patched/scripts/launchn.pl 2023-06-10 17:14:02.157839502 -0400 +@@ -30,32 +30,31 @@ + ############################################################################### + + # This script illustrates how to run compiler_test.pl in parallel on multi-core machines. Please make sure +-# these processes are terminated at the end of your testing ++# these processes are terminated at the end of your testing + + #!/usr/bin/perl -w + use strict; + use warnings; +-use Sys::CPU; + + sub usage () { +- die "usage: launchn.pl <config-file>\n"; ++ die "usage: launchn <config-file>\n"; + } + + my $CONFIG_FILE = ""; +-my $CPUS = Sys::CPU::cpu_count(); ++chomp (my $CPUS = `nproc`); + + if (scalar (@ARGV) != 1 || !(-e $ARGV[0]) ) { + usage(); +-} ++} + print "looks like we have $CPUS cpus\n"; + +-$CONFIG_FILE = $ARGV[0]; ++chomp ($CONFIG_FILE = `realpath $ARGV[0]`); + for (my $i=0; $i<$CPUS; $i++) { + my $dir = "work$i"; + system "rm -rf $dir"; + system "mkdir $dir"; + chdir $dir; +- system "nice -19 nohup ../compiler_test.pl 0 ../$CONFIG_FILE > output.txt 2>&1 &"; ++ system "nice -19 nohup compiler_test 0 $CONFIG_FILE > output.txt 2>&1 &"; + print "start working in $dir\n"; + chdir ".."; + } diff --git a/development/csmith/slack-desc b/development/csmith/slack-desc new file mode 100644 index 0000000000..88c9b7d51e --- /dev/null +++ b/development/csmith/slack-desc @@ -0,0 +1,19 @@ +# HOW TO EDIT THIS FILE: +# The "handy ruler" below makes it easier to edit a package description. +# Line up the first '|' above the ':' following the base package name, and +# the '|' on the right side marks the last column you can put a character in. +# You must make exactly 11 lines for the formatting to be correct. It's also +# customary to leave one space after the ':' except on otherwise blank lines. + + |-----handy-ruler------------------------------------------------------| +csmith: csmith (generates random C programs) +csmith: +csmith: Csmith is a random generator of C programs. Its primary purpose is to +csmith: find compiler bugs with random programs, using differential testing as +csmith: the test oracle. Csmith outputs C programs free of undefined behaviors +csmith: (believe us, that's not trivial), and the statistics of each generated +csmith: program. +csmith: +csmith: +csmith: +csmith: |