From 9b80960bbe236a4cdab7e9385f1e487c715fa0b2 Mon Sep 17 00:00:00 2001 From: Eric Eide Date: Fri, 26 May 2017 11:30:55 -0600 Subject: [PATCH] Use `srand48_deterministic` if it is available. OpenBSD requires a special call to activate the standard, deterministic behavior of `lrand48'. Check for the special function and use it if it is found. (As far as I know, it is only available on OpenBSD.) In addition to the principle change above, remove some old, commented-out calls to `srand48' --- to avoid somebody uncommenting them in the future and forgetting to call `srnd48_deterministic' instead! --- CMakeLists.txt | 2 ++ cmake_config.h.in | 3 +++ config.h.in | 3 +++ configure | 15 +++++++++++++++ configure.ac | 5 +++++ src/AbsRndNumGenerator.cpp | 10 ++++++++-- src/DefaultRndNumGenerator.cpp | 15 --------------- src/SimpleDeltaRndNumGenerator.cpp | 2 -- 8 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48023344d..f83565205 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,8 @@ check_include_file("sys/types.h" HAVE_SYS_TYPES_H) check_include_file("unistd.h" HAVE_UNISTD_H) check_symbol_exists(lrand48 "stdlib.h" HAVE_LRAND48) +check_symbol_exists(srand48_deterministic + "stdlib.h" HAVE_SRAND48_DETERMINISTIC) set(csmith_PACKAGE "csmith") set(csmith_PACKAGE_BUGREPORT "csmith-bugs@flux.utah.edu") diff --git a/cmake_config.h.in b/cmake_config.h.in index b205ed595..3b218ebd4 100755 --- a/cmake_config.h.in +++ b/cmake_config.h.in @@ -10,6 +10,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_MEMORY_H 1 +/* Define to 1 if you have the `srand48_deterministic' function. */ +#cmakedefine HAVE_SRAND48_DETERMINISTIC + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_STDINT_H 1 diff --git a/config.h.in b/config.h.in index fab02040c..bfbc5fbb2 100644 --- a/config.h.in +++ b/config.h.in @@ -12,6 +12,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `srand48_deterministic' function. */ +#undef HAVE_SRAND48_DETERMINISTIC + /* Define to 1 if you have the header file. */ #undef HAVE_STDINT_H diff --git a/configure b/configure index ec58b0c67..cd04f8f72 100755 --- a/configure +++ b/configure @@ -15840,6 +15840,21 @@ else fi +# OpenBSD requires a special call to activate the standard, deterministic +# behavior of `lrand48'. +# +for ac_func in srand48_deterministic +do : + ac_fn_c_check_func "$LINENO" "srand48_deterministic" "ac_cv_func_srand48_deterministic" +if test "x$ac_cv_func_srand48_deterministic" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_SRAND48_DETERMINISTIC 1 +_ACEOF + +fi +done + + # If we're using g++, set the default `CXXFLAGS' to something more pedantic. # if test "$ac_test_CXXFLAGS" = set; then diff --git a/configure.ac b/configure.ac index 6459f0705..e6b93c24f 100644 --- a/configure.ac +++ b/configure.ac @@ -63,6 +63,11 @@ have_lrand48="no" AC_CHECK_FUNCS([lrand48], [have_lrand48="yes"], [have_lrand48="no"]) AM_CONDITIONAL([HAVE_LRAND48], [test "$have_lrand48" = "yes"]) +# OpenBSD requires a special call to activate the standard, deterministic +# behavior of `lrand48'. +# +AC_CHECK_FUNCS([srand48_deterministic]) + # If we're using g++, set the default `CXXFLAGS' to something more pedantic. # if test "$ac_test_CXXFLAGS" = set; then diff --git a/src/AbsRndNumGenerator.cpp b/src/AbsRndNumGenerator.cpp index 643bde7ae..68942c2e8 100644 --- a/src/AbsRndNumGenerator.cpp +++ b/src/AbsRndNumGenerator.cpp @@ -43,7 +43,7 @@ using namespace std; -#ifdef WIN32 +#ifndef HAVE_LRAND48 extern "C" { extern void srand48(long seed); extern long lrand48(void); @@ -94,7 +94,13 @@ AbsRndNumGenerator::make_rndnum_generator(RNDNUM_GENERATOR impl, const unsigned void AbsRndNumGenerator::seedrand(const unsigned long seed ) { - srand48 (seed); +#ifdef HAVE_SRAND48_DETERMINISTIC + // OpenBSD requires a special call to activate the standard, + // deterministic behavior of `lrand48'. + srand48_deterministic(seed); +#else + srand48(seed); +#endif } /* diff --git a/src/DefaultRndNumGenerator.cpp b/src/DefaultRndNumGenerator.cpp index 59950d1b7..44816f69d 100644 --- a/src/DefaultRndNumGenerator.cpp +++ b/src/DefaultRndNumGenerator.cpp @@ -45,13 +45,6 @@ #include "CGOptions.h" #include "DeltaMonitor.h" -#ifdef WIN32 -extern "C" { - extern void srand48(long seed); - extern long lrand48(void); -} -#endif - DefaultRndNumGenerator *DefaultRndNumGenerator::impl_ = 0; /* @@ -181,14 +174,6 @@ DefaultRndNumGenerator::rnd_flipcoin(const unsigned int p, const Filter *f, cons return rv; } -#if 0 -void -DefaultRndNumGenerator::seedrand( unsigned long seed ) -{ - srand48 (seed); -} -#endif - std::string & DefaultRndNumGenerator::trace_depth() { diff --git a/src/SimpleDeltaRndNumGenerator.cpp b/src/SimpleDeltaRndNumGenerator.cpp index 7aaa536d3..6fe3a6eac 100644 --- a/src/SimpleDeltaRndNumGenerator.cpp +++ b/src/SimpleDeltaRndNumGenerator.cpp @@ -81,8 +81,6 @@ SimpleDeltaRndNumGenerator::make_rndnum_generator(const unsigned long /*seed*/) impl_ = new SimpleDeltaRndNumGenerator(seq); - //srand48(seed); - impl_->random_point_ = SimpleDeltaRndNumGenerator::pure_rnd_upto(seq->sequence_length()); assert(impl_);