sim: cgen: add asserts to fix unused engine warnings
authorMike Frysinger <vapier@gentoo.org>
Sun, 27 Jun 2021 03:03:53 +0000 (23:03 -0400)
committerMike Frysinger <vapier@gentoo.org>
Sun, 27 Jun 2021 14:48:07 +0000 (10:48 -0400)
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
sim/common/cgen-run.c

index 91e254c20a3ec9ebc0dfbb29d57e4a75b8b878c3..8472b2bdf12cae5beaffdb5a4275c01f2fc6aeaa 100644 (file)
@@ -1,3 +1,7 @@
+2021-06-27  Mike Frysinger  <vapier@gentoo.org>
+
+       * cgen-run.c (engine_run_n): Assert cpu arguments are valid.
+
 2021-06-27  Mike Frysinger  <vapier@gentoo.org>
 
        * cgen-trace.h (cgen_trace_printf): Add ATTRIBUTE_PRINTF_2.
index 1b097e1f1ec86c3f8b260cfb4a740c5d2abcc498..0951c92438579ed29f3b8c238cbd089c1ef22eba 100644 (file)
@@ -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);
 
This page took 0.036488 seconds and 4 git commands to generate.