fix: correct sloppy overflow

main
Sean McBride 4 years ago
parent 134aad35f9
commit ec627968c0

@ -29,6 +29,7 @@ extern FILE *runtime_sandbox_perf_log;
* See runtime_get_processor_speed_MHz for further details * See runtime_get_processor_speed_MHz for further details
*/ */
extern uint32_t runtime_processor_speed_MHz; 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 */ /* Count of worker threads and array of their pthread identifiers */
extern pthread_t runtime_worker_threads[]; extern pthread_t runtime_worker_threads[];

@ -21,6 +21,7 @@
int32_t debuglog_file_descriptor = -1; int32_t debuglog_file_descriptor = -1;
uint32_t runtime_processor_speed_MHz = 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_total_online_processors = 0;
uint32_t runtime_worker_threads_count = 0; uint32_t runtime_worker_threads_count = 0;
uint32_t runtime_first_worker_processor = 0; uint32_t runtime_first_worker_processor = 0;
@ -90,7 +91,7 @@ runtime_allocate_available_cores()
char *worker_count_raw = getenv("SLEDGE_NWORKERS"); char *worker_count_raw = getenv("SLEDGE_NWORKERS");
if (worker_count_raw != NULL) { if (worker_count_raw != NULL) {
int worker_count = atoi(worker_count_raw); 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); panic("Invalid Worker Count. Was %d. Must be {1..%d}\n", worker_count, max_possible_workers);
} }
runtime_worker_threads_count = worker_count; runtime_worker_threads_count = worker_count;
@ -236,6 +237,7 @@ main(int argc, char **argv)
runtime_processor_speed_MHz = runtime_get_processor_speed_MHz(); runtime_processor_speed_MHz = runtime_get_processor_speed_MHz();
if (unlikely(runtime_processor_speed_MHz == 0)) panic("Failed to detect processor speed\n"); 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 software_interrupt_interval_duration_in_cycles = (uint64_t)SOFTWARE_INTERRUPT_INTERVAL_DURATION_IN_USEC
* runtime_processor_speed_MHz; * runtime_processor_speed_MHz;
printf("Detected processor speed of %u MHz\n", runtime_processor_speed_MHz); printf("Detected processor speed of %u MHz\n", runtime_processor_speed_MHz);

@ -193,7 +193,12 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size,
/* Deadlines */ /* Deadlines */
module->relative_deadline_us = relative_deadline_us; 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 */ /* Admissions Control */
uint64_t expected_execution = expected_execution_us * runtime_processor_speed_MHz; 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); is_active = (strcmp(val, "yes") == 0);
} else if (strcmp(key, "relative-deadline-us") == 0) { } else if (strcmp(key, "relative-deadline-us") == 0) {
unsigned long long buffer = strtoull(val, NULL, 10); 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, panic("Max relative-deadline-us is %u, but entry was %llu\n", UINT32_MAX,
buffer); buffer);
relative_deadline_us = (uint32_t)buffer; relative_deadline_us = (uint32_t)buffer;

Loading…
Cancel
Save