diff --git a/lib-platform/crypto/crypto_scrypt.c b/lib-platform/crypto/crypto_scrypt.c index a98d4306..6057f0af 100644 --- a/lib-platform/crypto/crypto_scrypt.c +++ b/lib-platform/crypto/crypto_scrypt.c @@ -45,7 +45,7 @@ #include "crypto_scrypt.h" -static void (*smix_func)(uint8_t *, size_t, uint64_t, void *, void *) = NULL; +static void (* smix_func)(uint8_t *, size_t, uint64_t, void *, void *) = NULL; /** * crypto_scrypt_internal(passwd, passwdlen, salt, saltlen, N, r, p, buf, @@ -56,7 +56,7 @@ static int crypto_scrypt_internal(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t _r, uint32_t _p, uint8_t * buf, size_t buflen, - void (*smix)(uint8_t *, size_t, uint64_t, void *, void *)) + void (* smix)(uint8_t *, size_t, uint64_t, void *, void *)) { void * B0, * V0, * XY0; uint8_t * B; @@ -192,7 +192,7 @@ static struct scrypt_test { }; static int -testsmix(void (*smix)(uint8_t *, size_t, uint64_t, void *, void *)) +testsmix(void (* smix)(uint8_t *, size_t, uint64_t, void *, void *)) { uint8_t hbuf[TESTLEN]; diff --git a/lib/scryptenc/scryptenc_cpuperf.c b/lib/scryptenc/scryptenc_cpuperf.c index 0836dc13..b7a64fb9 100644 --- a/lib/scryptenc/scryptenc_cpuperf.c +++ b/lib/scryptenc/scryptenc_cpuperf.c @@ -46,6 +46,7 @@ getclockdiff(struct timeval * st, double * diffd) return (1); *diffd = timeval_diff((*st), en); + /* Success! */ return (0); } diff --git a/libcperciva/POSIX/posix-abstract-declarator.c b/libcperciva/POSIX/posix-abstract-declarator.c index c4ec2498..40e71b2c 100644 --- a/libcperciva/POSIX/posix-abstract-declarator.c +++ b/libcperciva/POSIX/posix-abstract-declarator.c @@ -9,6 +9,8 @@ func(int arr[static restrict 1]) { (void)arr; /* UNUSED */ + + /* Success! */ return (0); } @@ -17,5 +19,7 @@ main(void) { (void)func; /* UNUSED */ + + /* Success! */ return (0); } diff --git a/libcperciva/POSIX/posix-stat-st_mtim.c b/libcperciva/POSIX/posix-stat-st_mtim.c index 7e861e22..73d5dcc9 100644 --- a/libcperciva/POSIX/posix-stat-st_mtim.c +++ b/libcperciva/POSIX/posix-stat-st_mtim.c @@ -8,5 +8,6 @@ main(void) /* Can we reference st_mtim? */ (void)sb.st_mtim.tv_sec; + /* Success! */ return (0); } diff --git a/libcperciva/POSIX/posix-trivial.c b/libcperciva/POSIX/posix-trivial.c index 9f534cdf..5036a736 100644 --- a/libcperciva/POSIX/posix-trivial.c +++ b/libcperciva/POSIX/posix-trivial.c @@ -2,5 +2,6 @@ int main(void) { + /* Success! */ return (0); } diff --git a/libcperciva/alg/sha256.c b/libcperciva/alg/sha256.c index d698eddc..1321f1f0 100644 --- a/libcperciva/alg/sha256.c +++ b/libcperciva/alg/sha256.c @@ -140,7 +140,7 @@ static int hwtest(const uint32_t state[static restrict 8], const uint8_t block[static restrict 64], uint32_t W[static restrict 64], uint32_t S[static restrict 8], - void(* func)(uint32_t state[static restrict 8], + void (* func)(uint32_t state[static restrict 8], const uint8_t block[static restrict 64], uint32_t W[static restrict 64], uint32_t S[static restrict 8])) { diff --git a/libcperciva/cpusupport/Build/cpusupport-ARM-AES.c b/libcperciva/cpusupport/Build/cpusupport-ARM-AES.c index a3f7b5b8..68900697 100644 --- a/libcperciva/cpusupport/Build/cpusupport-ARM-AES.c +++ b/libcperciva/cpusupport/Build/cpusupport-ARM-AES.c @@ -17,5 +17,6 @@ main(void) lanes = vdupq_laneq_u32(lanes, 0); + /* Success! */ return (0); } diff --git a/libcperciva/cpusupport/Build/cpusupport-ARM-SHA256.c b/libcperciva/cpusupport/Build/cpusupport-ARM-SHA256.c index 2768f618..7a04d87b 100644 --- a/libcperciva/cpusupport/Build/cpusupport-ARM-SHA256.c +++ b/libcperciva/cpusupport/Build/cpusupport-ARM-SHA256.c @@ -12,5 +12,6 @@ main(void) output = vsha256su0q_u32(w0, w4); (void)output; /* UNUSED */ + /* Success! */ return (0); } diff --git a/libcperciva/crypto/crypto_aes.c b/libcperciva/crypto/crypto_aes.c index 8d64761c..f884605b 100644 --- a/libcperciva/crypto/crypto_aes.c +++ b/libcperciva/crypto/crypto_aes.c @@ -67,7 +67,7 @@ static struct aes_test { /* Test a function against test vectors. */ static int -functest(int(* func)(const uint8_t *, size_t, const uint8_t[16], uint8_t[16])) +functest(int (* func)(const uint8_t *, size_t, const uint8_t[16], uint8_t[16])) { struct aes_test * knowngood; uint8_t ctext[16]; diff --git a/libcperciva/crypto/crypto_aesctr_shared.c b/libcperciva/crypto/crypto_aesctr_shared.c index 531bbeb8..632e419c 100644 --- a/libcperciva/crypto/crypto_aesctr_shared.c +++ b/libcperciva/crypto/crypto_aesctr_shared.c @@ -81,6 +81,7 @@ crypto_aesctr_stream_pre_wholeblock(struct crypto_aesctr * stream, buflen_p, 16 - bytemod, bytemod); } + /* Success! */ return (0); } diff --git a/libcperciva/util/monoclock.c b/libcperciva/util/monoclock.c index 5dfc0150..4cd82f4c 100644 --- a/libcperciva/util/monoclock.c +++ b/libcperciva/util/monoclock.c @@ -97,8 +97,8 @@ monoclock_get_cputime(struct timeval * tv) /** * monoclock_getres(resd): - * Store an upper limit on timer granularity in ${resd}. If CLOCK_MONOTONIC is - * available, use that clock; if CLOCK_MONOTONIC is unavailable, use + * Store an upper limit on timer granularity in ${resd}. If CLOCK_MONOTONIC + * is available, use that clock; if CLOCK_MONOTONIC is unavailable, use * CLOCK_REALTIME (if available) or gettimeofday(2). For this value to be * meaningful, we assume that clock_getres(x) succeeds iff clock_gettime(x) * succeeds. diff --git a/libcperciva/util/monoclock.h b/libcperciva/util/monoclock.h index 310c4467..ff0cded7 100644 --- a/libcperciva/util/monoclock.h +++ b/libcperciva/util/monoclock.h @@ -25,8 +25,8 @@ int monoclock_get_cputime(struct timeval *); /** * monoclock_getres(resd): - * Store an upper limit on timer granularity in ${resd}. If CLOCK_MONOTONIC is - * available, use that clock; if CLOCK_MONOTONIC is unavailable, use + * Store an upper limit on timer granularity in ${resd}. If CLOCK_MONOTONIC + * is available, use that clock; if CLOCK_MONOTONIC is unavailable, use * CLOCK_REALTIME (if available) or gettimeofday(2). For this value to be * meaningful, we assume that clock_getres(x) succeeds iff clock_gettime(x) * succeeds. diff --git a/tests/shared_test_functions.sh b/tests/shared_test_functions.sh index 99924060..ffedc094 100644 --- a/tests/shared_test_functions.sh +++ b/tests/shared_test_functions.sh @@ -12,12 +12,14 @@ # ### Design # -# The main function is scenario_runner(scenario_filename), which +# The main function is _scenario_runner(scenario_filename), which # takes a scenario file as the argument, and runs a # scenario_cmd() # function which was defined in that file. # # Functions which are available to other scripts as a "public API" are: +# - find_system(cmd, args): +# Look for ${cmd} in the ${PATH}, and ensure that it supports ${args}. # - has_pid(cmd): # Look for a ${cmd} in $(ps). # - wait_while(func): @@ -29,6 +31,8 @@ # - run_scenarios(): # Run scenarios in the test directory. # +# We adopt the convention of "private" function names beginning with an _. +# ### Variables # # Wherever possible, this suite uses local variables and @@ -73,9 +77,9 @@ bindir=$(CDPATH='' cd -- "$(dirname -- "${1-.}")" && pwd -P) NO_EXITFILE=/dev/null -## prepare_directory(): +## _prepare_directory(): # Delete the previous test output directory, and create a new one. -prepare_directory() { +_prepare_directory() { if [ -d "${out}" ]; then rm -rf "${out}" fi @@ -86,7 +90,7 @@ prepare_directory() { } ## find_system (cmd, args): -# Look for ${cmd} in the $PATH, and ensure that it supports ${args}. +# Look for ${cmd} in the ${PATH}, and ensure that it supports ${args}. find_system() { cmd=$1 cmd_with_args="$1 ${2:-}" @@ -207,14 +211,14 @@ expected_exitcode() { fi } -## notify_success_or_fail (log_basename, val_log_basename): +## _notify_success_or_fail (log_basename, val_log_basename): # Examine all "exit code" files beginning with ${log_basename} and # print "SUCCESS!", "FAILED!", "SKIP!", or "PARTIAL SUCCESS / SKIP!" # as appropriate. Check any valgrind log files associated with the # test and print "FAILED!" if appropriate, along with the valgrind # logfile. If the test failed and ${VERBOSE} is non-zero, print # the description to stderr. -notify_success_or_fail() { +_notify_success_or_fail() { log_basename=$1 val_log_basename=$2 @@ -278,9 +282,9 @@ notify_success_or_fail() { fi } -## scenario_runner (scenario_filename): +## _scenario_runner (scenario_filename): # Run a test scenario from ${scenario_filename}. -scenario_runner() { +_scenario_runner() { scenario_filename=$1 basename=$(basename "${scenario_filename}" .sh) printf " %s... " "${basename}" 1>&2 @@ -307,13 +311,14 @@ scenario_runner() { # Print PASS or FAIL, and return result. s_retval=0 - notify_success_or_fail "${s_basename}" "${s_val_basename}" + _notify_success_or_fail "${s_basename}" "${s_val_basename}" return "${s_retval}" } -## run_scenarios (scenario_filenames): -# Run all scenarios matching ${scenario_filenames}. +## run_scenarios (): +# Run all scenarios in the test directory. If the environment variable ${N} +# is specified, only run the scenario corresponding to that number. run_scenarios() { # Get the test number(s) to run. if [ "${N:-0}" -gt "0" ]; then @@ -323,7 +328,7 @@ run_scenarios() { fi # Clean up any previous directory, and create a new one. - prepare_directory + _prepare_directory # Clean up any previous valgrind directory, and prepare for new # valgrind tests (if applicable). @@ -334,7 +339,7 @@ run_scenarios() { for scenario in ${test_scenarios}; do # We can't call this function with $( ... ) because we # want to allow it to echo values to stdout. - scenario_runner "${scenario}" + _scenario_runner "${scenario}" retval=$? if [ "${retval}" -gt 0 ]; then exit "${retval}" diff --git a/tests/shared_valgrind_functions.sh b/tests/shared_valgrind_functions.sh index 0d8e5b99..83ad7f35 100644 --- a/tests/shared_valgrind_functions.sh +++ b/tests/shared_valgrind_functions.sh @@ -10,28 +10,35 @@ set -o noclobber -o nounset # - valgrind_init(): # Clear previous valgrind output, and prepare for running valgrind tests # (if applicable). -# - valgrind_setup_cmd(): -# Set up the valgrind command if $USE_VALGRIND is greater than or equal to -# ${valgrind_min}. +# - valgrind_setup_cmd(str): +# Set up the valgrind command if ${USE_VALGRIND} is greater than or equal to +# ${valgrind_min}. If ${str} is not blank, include it in the log filename. # - valgrind_check_basenames(exitfile): # Check for any memory leaks recorded in valgrind logfiles associated with a # test exitfile. Return the filename if there's a leak; otherwise return an # empty string. # - valgrind_incomplete(): # Check if any valgrind log files are incomplete. +# +# We adopt the convention of "private" function names beginning with an _. +# +### Variables +# +# Wherever possible, this suite uses local variables and +# explicitly-passed arguments, with the following exceptions: +# - valgrind_suppressions: filename of valgrind suppressions. +# - valgrind_fds_log: filename of the log of open file descriptors. # A non-zero value unlikely to be used as an exit code by the programs being # tested. valgrind_exit_code=108 -## valgrind_prepare_directory (): +## _valgrind_prepare_directory (): # Clean up a previous valgrind directory, and prepare for new valgrind tests # (if applicable). -valgrind_prepare_directory() { +_valgrind_prepare_directory() { # If we don't want to generate new suppressions files, move them. if [ "${USE_VALGRIND_NO_REGEN}" -gt 0 ]; then - valgrind_suppressions="${out_valgrind}/suppressions" - fds="${out_valgrind}/fds.log" # Bail if the file doesn't exist. if [ ! -e "${valgrind_suppressions}" ]; then echo "No valgrind suppressions file" 1>&2 @@ -42,7 +49,7 @@ valgrind_prepare_directory() { supp_tmp="$(mktemp /tmp/valgrind-suppressions.XXXXXX)" fds_tmp="$(mktemp /tmp/valgrind-fds.XXXXXX)" mv "${valgrind_suppressions}" "${supp_tmp}" - mv "${fds}" "${fds_tmp}" + mv "${valgrind_fds_log}" "${fds_tmp}" fi # Always delete any previous valgrind directory. @@ -61,19 +68,19 @@ valgrind_prepare_directory() { if [ "${USE_VALGRIND_NO_REGEN}" -gt 0 ]; then # Move the files back. mv "${supp_tmp}" "${valgrind_suppressions}" - mv "${fds_tmp}" "${fds}" + mv "${fds_tmp}" "${valgrind_fds_log}" fi # We don't want to back up this directory. [ "$(uname)" = "FreeBSD" ] && chflags nodump "${out_valgrind}" } -## valgrind_check_optional (): -# Return a $USE_VALGRIND variable defined; if it was previously defined and -# was greater than 0, then check that valgrind is available in the $PATH. -valgrind_check_optional() { +## _valgrind_check_optional (): +# If ${USE_VALGRIND} is greater than 0, check that valgrind is available in +# the ${PATH} and is at least version 3.13. +_valgrind_check_optional() { if [ "${USE_VALGRIND}" -gt 0 ]; then - # Look for valgrind in $PATH. + # Look for valgrind in ${PATH}. if ! command -v valgrind >/dev/null 2>&1; then printf "valgrind not found\n" 1>&2 exit 1 @@ -94,10 +101,10 @@ valgrind_check_optional() { fi } -## valgrind_process_suppression_file(filename): +## _valgrind_process_suppression_file(filename): # Generalize suppressions from a valgrind suppression file by omitting the # "fun:pl_*" and "fun:main" lines and anything below them. -valgrind_process_suppression_file() { +_valgrind_process_suppression_file() { filename=$1 # How many segments do we have? @@ -158,12 +165,12 @@ valgrind_process_suppression_file() { done } -## valgrind_ensure_suppression (potential_memleaks_binary): +## _valgrind_ensure_suppression (potential_memleaks_binary): # Run the ${potential_memleaks_binary} through valgrind, keeping # track of any apparent memory leak in order to suppress reporting -# those leaks when testing other binaries. Record how many file descriptors -# are open at exit in ${valgrind_fds}. -valgrind_ensure_suppression() { +# those leaks when testing other binaries. Record a log file which shows the +# open file descriptors in ${valgrind_fds_log}. +_valgrind_ensure_suppression() { potential_memleaks_binary=$1 # Quit if we're not using valgrind. @@ -171,17 +178,12 @@ valgrind_ensure_suppression() { return fi; - fds_log="${out_valgrind}/fds.log" - if [ "${USE_VALGRIND_NO_REGEN}" -gt 0 ]; then printf "Using old valgrind suppressions\n" 1>&2 - valgrind_fds=$(grep "FILE DESCRIPTORS" "${fds_log}" | \ - awk '{print $4}') return fi printf "Generating valgrind suppressions... " 1>&2 - valgrind_suppressions="${out_valgrind}/suppressions" valgrind_suppressions_log="${out_valgrind}/suppressions.pre" # Start off with an empty suppression file @@ -189,9 +191,8 @@ valgrind_ensure_suppression() { # Get list of tests and the number of open descriptors at a normal exit valgrind_suppressions_tests="${out_valgrind}/suppressions-names.txt" - valgrind --track-fds=yes --log-file="${fds_log}" \ + valgrind --track-fds=yes --log-file="${valgrind_fds_log}" \ "${potential_memleaks_binary}" > "${valgrind_suppressions_tests}" - valgrind_fds=$(grep "FILE DESCRIPTORS" "${fds_log}" | awk '{print $4}') # Generate suppressions for each test while read -r testname; do @@ -202,6 +203,7 @@ valgrind_ensure_suppression() { printf "\n" | (valgrind \ --leak-check=full --show-leak-kinds=all \ --gen-suppressions=all \ + --trace-children=yes \ --suppressions="${valgrind_suppressions}" \ --log-file="${this_valgrind_supp}" \ "${potential_memleaks_binary}" \ @@ -213,7 +215,7 @@ valgrind_ensure_suppression() { # Strip out useless parts from the log file, and allow the # suppressions to apply to other binaries. - valgrind_process_suppression_file "${this_valgrind_supp}" + _valgrind_process_suppression_file "${this_valgrind_supp}" done < "${valgrind_suppressions_tests}" # Clean up @@ -221,19 +223,29 @@ valgrind_ensure_suppression() { printf "done.\n" 1>&2 } -## valgrind_setup_cmd (): -# Set up the valgrind command if $USE_VALGRIND is greater than or equal to -# ${valgrind_min}. +## valgrind_setup_cmd (str): +# Set up the valgrind command if ${USE_VALGRIND} is greater than or equal to +# ${valgrind_min}. If ${str} is not blank, include it in the log filename. valgrind_setup_cmd() { + str=${1:-} + # Bail if we don't want to use valgrind for this check. if [ "${USE_VALGRIND}" -lt "${c_valgrind_min}" ]; then return fi - val_logfilename="${s_val_basename}-${c_count_str}-%p.log" + # Set up the log filename. + if [ -n "${str}" ]; then + val_logfilename="${s_val_basename}-${c_count_str}-${str}-%p.log" + else + val_logfilename="${s_val_basename}-${c_count_str}-%p.log" + fi + + # Set up valgrind command. c_valgrind_cmd="valgrind \ --log-file=${val_logfilename} \ --track-fds=yes \ + --trace-children=yes \ --leak-check=full --show-leak-kinds=all \ --errors-for-leak-kinds=all \ --suppressions=${valgrind_suppressions}" @@ -252,19 +264,19 @@ valgrind_incomplete() { test -n "${_valgrind_incomplete_logfiles}" } -## valgrind_get_basename (exitfile): +## _valgrind_get_basename (exitfile): # Return the filename without ".log" of the valgrind logfile corresponding to # ${exitfile}. -valgrind_get_basename() { +_valgrind_get_basename() { exitfile=$1 basename=$(basename "${exitfile}" ".exit") echo "${out_valgrind}/${basename}" } -## valgrind_check_logfile(logfile) +## _valgrind_check_logfile(logfile) # Check for any (unsuppressed) memory leaks recorded in a valgrind logfile. # Echo the filename if there's a leak; otherwise, echo nothing. -valgrind_check_logfile() { +_valgrind_check_logfile() { logfile=$1 # Bytes in use at exit. @@ -301,6 +313,8 @@ valgrind_check_logfile() { # match the simple test case (executing potential_memleaks without # running any actual tests). fds_in_use=$(grep "FILE DESCRIPTORS" "${logfile}" | awk '{print $4}') + valgrind_fds=$(grep "FILE DESCRIPTORS" "${valgrind_fds_log}" | \ + awk '{print $4}') if [ "${fds_in_use}" != "${valgrind_fds}" ] ; then # There is an unsuppressed leak. echo "${logfile}" @@ -324,7 +338,7 @@ valgrind_check_logfile() { # empty string. valgrind_check_basenames() { exitfile="$1" - val_basename=$(valgrind_get_basename "${exitfile}") + val_basename=$(_valgrind_get_basename "${exitfile}") # Get list of files to check. (Yes, the star goes outside the quotes.) logfiles=$(ls "${val_basename}"* 2>/dev/null) @@ -338,10 +352,20 @@ valgrind_check_basenames() { # Check a single file. if [ "${num_logfiles}" -eq "1" ]; then - valgrind_check_logfile "${logfiles}" + _valgrind_check_logfile "${logfiles}" return fi + # If the valgrind logfiles contain "-valgrind-parent-", then we only + # want to check the parent (the lowest pid). + for logfile in ${logfiles} ; do + if [ "${logfile#*-valgrind-parent-}" != "${logfile}" ]; then + # Only check the parent + _valgrind_check_logfile "${logfile}" + return "$?" + fi + done + # If there's two files, there's a fork() -- likely within # daemonize() -- so only pay attention to the child. if [ "${num_logfiles}" -eq "2" ]; then @@ -358,7 +382,7 @@ valgrind_check_basenames() { awk '{ print $4 }') if [ "${val_pids#*"${val_parent_pid}"}" != \ "${val_pids}" ]; then - valgrind_check_logfile "${logfile}" + _valgrind_check_logfile "${logfile}" return "$?" fi done @@ -373,14 +397,18 @@ valgrind_check_basenames() { # Clear previous valgrind output, and prepare for running valgrind tests # (if applicable). valgrind_init() { + # Set up global variables. + valgrind_suppressions="${out_valgrind}/suppressions" + valgrind_fds_log="${out_valgrind}/fds.log" + # If we want valgrind, check that the version is high enough. - valgrind_check_optional + _valgrind_check_optional # Remove any previous directory, and create a new one. - valgrind_prepare_directory + _valgrind_prepare_directory # Generate valgrind suppression file if it is required. Must be # done after preparing the directory. - valgrind_ensure_suppression \ + _valgrind_ensure_suppression \ "${bindir}/tests/valgrind/potential-memleaks" }