From b69bd9e723db53ce95c6ebb911d623b8f396b6ac Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 26 Jun 2021 23:03:53 -0400 Subject: [PATCH] sim: cgen: add asserts to fix unused engine warnings If the user passed in values outside the range of [0, MAX_NR_PROCESSORS), it would cause the code to access out-of-bind engine function pointers. Add some asserts to catch that and to fix the related compiler warnings. --- sim/common/ChangeLog | 4 ++++ sim/common/cgen-run.c | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sim/common/ChangeLog b/sim/common/ChangeLog index 91e254c20a3..8472b2bdf12 100644 --- a/sim/common/ChangeLog +++ b/sim/common/ChangeLog @@ -1,3 +1,7 @@ +2021-06-27 Mike Frysinger + + * cgen-run.c (engine_run_n): Assert cpu arguments are valid. + 2021-06-27 Mike Frysinger * cgen-trace.h (cgen_trace_printf): Add ATTRIBUTE_PRINTF_2. diff --git a/sim/common/cgen-run.c b/sim/common/cgen-run.c index 1b097e1f1ec..0951c924385 100644 --- a/sim/common/cgen-run.c +++ b/sim/common/cgen-run.c @@ -231,6 +231,9 @@ engine_run_n (SIM_DESC sd, int next_cpu_nr, int nr_cpus, int max_insns, int fast int i; ENGINE_FN *engine_fns[MAX_NR_PROCESSORS]; + SIM_ASSERT (nr_cpus <= MAX_NR_PROCESSORS); + SIM_ASSERT (next_cpu_nr >= 0 && next_cpu_nr < nr_cpus); + for (i = 0; i < nr_cpus; ++i) { SIM_CPU *cpu = STATE_CPU (sd, i); @@ -244,7 +247,7 @@ engine_run_n (SIM_DESC sd, int next_cpu_nr, int nr_cpus, int max_insns, int fast SIM_ENGINE_PREFIX_HOOK (sd); /* FIXME: proper cycling of all of them, blah blah blah. */ - while (next_cpu_nr != nr_cpus) + while (next_cpu_nr < nr_cpus) { SIM_CPU *cpu = STATE_CPU (sd, next_cpu_nr); -- 2.30.2