From 6554e6d1fede8d20024ad41333f30c62dbb32cc1 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 21 Oct 2020 18:34:31 -0400 Subject: [PATCH] fix: correct sloppy overflow --- runtime/include/runtime.h | 1 + runtime/src/main.c | 12 +++++++----- runtime/src/module.c | 9 +++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/runtime/include/runtime.h b/runtime/include/runtime.h index 01625be..d6b6c47 100644 --- a/runtime/include/runtime.h +++ b/runtime/include/runtime.h @@ -29,6 +29,7 @@ extern FILE *runtime_sandbox_perf_log; * See runtime_get_processor_speed_MHz for further details */ extern uint32_t runtime_processor_speed_MHz; +extern uint64_t runtime_relative_deadline_us_max; /* Count of worker threads and array of their pthread identifiers */ extern pthread_t runtime_worker_threads[]; diff --git a/runtime/src/main.c b/runtime/src/main.c index 896140a..761bc36 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -20,10 +20,11 @@ /* Conditionally used by debuglog when NDEBUG is not set */ int32_t debuglog_file_descriptor = -1; -uint32_t runtime_processor_speed_MHz = 0; -uint32_t runtime_total_online_processors = 0; -uint32_t runtime_worker_threads_count = 0; -uint32_t runtime_first_worker_processor = 0; +uint32_t runtime_processor_speed_MHz = 0; +uint64_t runtime_relative_deadline_us_max = 0; /* a value higher than this will cause overflow on a uint64_t */ +uint32_t runtime_total_online_processors = 0; +uint32_t runtime_worker_threads_count = 0; +uint32_t runtime_first_worker_processor = 0; int runtime_worker_threads_argument[WORKER_THREAD_CORE_COUNT] = { 0 }; /* The worker sets its argument to -1 on error */ pthread_t runtime_worker_threads[WORKER_THREAD_CORE_COUNT]; @@ -90,7 +91,7 @@ runtime_allocate_available_cores() char *worker_count_raw = getenv("SLEDGE_NWORKERS"); if (worker_count_raw != NULL) { int worker_count = atoi(worker_count_raw); - if (worker_count < 0 || worker_count > max_possible_workers) { + if (worker_count <= 0 || worker_count > max_possible_workers) { panic("Invalid Worker Count. Was %d. Must be {1..%d}\n", worker_count, max_possible_workers); } runtime_worker_threads_count = worker_count; @@ -236,6 +237,7 @@ main(int argc, char **argv) runtime_processor_speed_MHz = runtime_get_processor_speed_MHz(); if (unlikely(runtime_processor_speed_MHz == 0)) panic("Failed to detect processor speed\n"); + runtime_relative_deadline_us_max = UINT64_MAX / runtime_processor_speed_MHz; software_interrupt_interval_duration_in_cycles = (uint64_t)SOFTWARE_INTERRUPT_INTERVAL_DURATION_IN_USEC * runtime_processor_speed_MHz; printf("Detected processor speed of %u MHz\n", runtime_processor_speed_MHz); diff --git a/runtime/src/module.c b/runtime/src/module.c index 8e9f60f..710d073 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -193,7 +193,12 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size, /* Deadlines */ module->relative_deadline_us = relative_deadline_us; - module->relative_deadline = relative_deadline_us * runtime_processor_speed_MHz; + + /* This should have been handled when a module was loaded */ + assert(relative_deadline_us < runtime_relative_deadline_us_max); + + /* This can overflow a uint32_t, so be sure to cast appropriately */ + module->relative_deadline = (uint64_t)relative_deadline_us * runtime_processor_speed_MHz; /* Admissions Control */ uint64_t expected_execution = expected_execution_us * runtime_processor_speed_MHz; @@ -389,7 +394,7 @@ module_new_from_json(char *file_name) is_active = (strcmp(val, "yes") == 0); } else if (strcmp(key, "relative-deadline-us") == 0) { unsigned long long buffer = strtoull(val, NULL, 10); - if (buffer > UINT32_MAX) + if (buffer > runtime_relative_deadline_us_max) panic("Max relative-deadline-us is %u, but entry was %llu\n", UINT32_MAX, buffer); relative_deadline_us = (uint32_t)buffer;