pi_stress: Bug fixes and improvements
cover pi_stress: Bug fixes and improvements
This series addresses several issues in pi_stress:
- Fixes a critical deadlock bug in error handling where inconsistent barrier wait logic could cause the program to hang
- Improves input validation to reject malformed numeric arguments
- Enhances error messages for better user experience
- Fixes cosmetic issues in debug/error output
- Ensures proper string null-termination
The first patch is the most important as it fixes a real deadlock scenario during error conditions.
John Kacur (5):
pi_stress: Fix inconsistent barrier wait logic to prevent deadlock
pi_stress: Validate numeric command-line arguments
pi_stress: Improve error message for permission failures
pi_stress: Fix error and debug message formatting
pi_stress: Ensure null-termination of JSON filename
src/pi_tests/pi_stress.c | 44 ++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/5 pi_stress: Fix inconsistent barrier wait logic to prevent deadlock
The low_priority thread function unconditionally waited on the all_threads_done barrier, while med_priority and high_priority only waited when have_errors == 0. This inconsistency could cause deadlock during error conditions where med_priority and high_priority threads exit without waiting, leaving low_priority threads waiting forever.
Make low_priority consistent with the other thread functions by adding the same have_errors check before waiting on all_threads_done.
--- src/pi_tests/pi_stress.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
src/pi_tests/pi_stress.c +10 -7
@@ -605,13 +605,16 @@ void *low_priority(void *arg) }set_shutdown_flag ();pi_debug ("low_priority[%d]: entering done barrier\n" , p->id); -/* wait for all threads to finish */ - status =pthread_barrier_wait (&all_threads_done); -if (status && status !=PTHREAD_BARRIER_SERIAL_THREAD ) { -pi_error - ("low_priority[%d]: pthread_barrier_wait(all_threads_done): %x" , - p->id, status); -return NULL ; + +if (have_errors ==0 ) { +/* wait for all threads to finish */ + status =pthread_barrier_wait (&all_threads_done); +if (status && status !=PTHREAD_BARRIER_SERIAL_THREAD ) { +pi_error + ("low_priority[%d]: pthread_barrier_wait(all_threads_done): %x" , + p->id, status); +return NULL ; + } }pi_debug ("low_priority[%d]: exiting\n" , p->id);return NULL ;
Signature · 1 line
2.54.0
PATCH 2/5 pi_stress: Validate numeric command-line arguments
In reply to John KacurThe parse_unsigned() and parse_signed() functions were using strtoul() and strtol() but not checking if the entire string was consumed. This allowed invalid input like "123abc" to be silently accepted as "123".
Additionally, the -g (groups) and -i (inversions) options were calling strtol() directly instead of using the parse_signed() helper function, bypassing any validation entirely.
Add validation to parse_unsigned() and parse_signed() to ensure the entire input string is a valid number by checking that the end pointer points to a null terminator. Update -g and -i options to use parse_signed() for consistent error handling.
--- src/pi_tests/pi_stress.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
src/pi_tests/pi_stress.c +12 -2
@@ -1092,6 +1092,11 @@ unsigned long parse_unsigned(const char *str)exit (EXIT_FAILURE ); } +if (*p !='\0' ) { +pi_error ("invalid number format: %s\n" , str); +exit (EXIT_FAILURE ); + } +return n; }
@@ -1109,6 +1114,11 @@ long parse_signed(const char *str)exit (EXIT_FAILURE ); } +if (*p !='\0' ) { +pi_error ("invalid number format: %s\n" , str); +exit (EXIT_FAILURE ); + } +return n; }
@@ -1211,7 +1221,7 @@ void process_command_line(int argc, char **argv)break ;case OPT_GROUPS :case 'g' : - ngroups =strtol (optarg,NULL ,10 ); + ngroups =parse_signed (optarg);if (ngroups > num_processors) {pi_error ("the number of groups cannot exceed the number of online processors (%ld)\n" , num_processors);exit (-1 );
@@ -1224,7 +1234,7 @@ void process_command_line(int argc, char **argv)break ;case OPT_INVERSIONS :case 'i' : - inversions =strtol (optarg,NULL ,10 ); + inversions =parse_signed (optarg);pi_info ("doing %d inversion per group\n" , inversions);break ;case OPT_JSON :
Signature · 1 line
2.54.0
PATCH 3/5 pi_stress: Improve error message for permission failures
In reply to John KacurWhen pi_stress fails to set real-time scheduling priority, it printed a cryptic hex error code (0x1) that didn't explain what went wrong or how to fix it.
Replace the error message with a clear explanation that indicates the failure reason and suggests running with root privileges.
Before:
main: boosting to max priority: 0x1
After:
Failed to set real-time priority: Operation not permitted Please run with root privileges to set real-time priorities.
--- src/pi_tests/pi_stress.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/pi_tests/pi_stress.c +2 -2
@@ -134,8 +134,8 @@ int main(int argc, char **argv) status =pthread_setschedparam (pthread_self (), admin_sa.sched_policy, &thread_param);if (status) { -pi_error ("%s: boosting to max priority: 0x%x\n" , __func__, - status); +pi_error ("Failed to set real-time priority: %s\n" ,strerror (status)); +pi_error ("Please run with root privileges to set real-time priorities.\n" );return FAILURE ; }/* block unwanted signals */
Signature · 1 line
2.54.0
PATCH 4/5 pi_stress: Fix error and debug message formatting
In reply to John KacurFix three message formatting issues:
1. Incorrect barrier name in error message (line 212): The error referenced "all_threads_ready" but was actually checking "all_threads_done"
2. Typo in SCHED_DEADLINE error message (line 746): Fixed malformed parenthesis in "affinity*dl)" to "affinity (dl)"
3. Wrong format specifier (line 808): Variable 'count' is unsigned long but was using %d instead of %lu
These are cosmetic fixes that improve debugging clarity but don't affect program behavior.
--- src/pi_tests/pi_stress.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
src/pi_tests/pi_stress.c +3 -3
@@ -209,7 +209,7 @@ int main(int argc, char **argv)pi_info ("waiting for all threads to complete\n" ); status =pthread_barrier_wait (&all_threads_done);if (status && status !=PTHREAD_BARRIER_SERIAL_THREAD ) { -pi_error ("%s: pthread_barrier_wait(all_threads_ready): 0x%x\n" , __func__, status); +pi_error ("%s: pthread_barrier_wait(all_threads_done): 0x%x\n" , __func__, status);return FAILURE ; }pi_info ("All threads terminated!\n" );
@@ -743,7 +743,7 @@ void *high_priority(void *arg) status =sched_setaffinity (0 ,sizeof (cpu_set_t), &cpu_mask);if (status <0 ) {pi_error - ("high_priority[%d]: set cpu affinity*dl): %x\n" , + ("high_priority[%d]: set cpu affinity (dl): %x\n" , p->id, status);return NULL ; }
@@ -805,7 +805,7 @@ void *high_priority(void *arg) }pthread_mutex_unlock (&shutdown_mtx); } -pi_debug ("high_priority[%d]: entering start state (%d)\n" , p->id, +pi_debug ("high_priority[%d]: entering start state (%lu)\n" , p->id, count++); status =pthread_barrier_wait (&p->start_barrier);if (status && status !=PTHREAD_BARRIER_SERIAL_THREAD ) {
Signature · 1 line
2.54.0
PATCH 5/5 pi_stress: Ensure null-termination of JSON filename
In reply to John KacurThe --json option used strncpy() with strnlen() to copy the filename, but did not guarantee null-termination if the input string was exactly MAX_PATH-1 characters long. When strncpy() copies exactly n characters, it does not add a null terminator, potentially causing undefined behavior when the string is later used.
Replace the strnlen() call with MAX_PATH-1 as the copy limit and explicitly null-terminate the buffer to ensure the string is always properly terminated regardless of input length.
--- src/pi_tests/pi_stress.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/pi_tests/pi_stress.c +2 -1
@@ -1238,7 +1238,8 @@ void process_command_line(int argc, char **argv)pi_info ("doing %d inversion per group\n" , inversions);break ;case OPT_JSON : -strncpy (jsonfile, optarg,strnlen (optarg,MAX_PATH -1 )); +strncpy (jsonfile, optarg,MAX_PATH -1 ); + jsonfile[MAX_PATH -1 ] ='\0' ;break ;case OPT_MLOCKALL :case 'm' :
Signature · 1 line
2.54.0