The generic code in ebl_set_initial_registers_sample is meant to be
used on arches with simple/small register files and a contiguous
linear mapping of perf_regs<->dwarf_regs. Make sure to guard it with a
check of ebl->perf_frame_regs_mask (the canonical sign that
sample_regs is supported for this arch).

Otherwise, a hypothetical call to ebl_set_initial_registers_sample()
on an unsupported arch with many regs falls through to the generic
code and ends up writing out of bounds to the dwarf_regs array,
(at least in builds where the assert is also compiled out).

Based on Sayed Kaif's patch and problem report cf
https://sourceware.org/pipermail/elfutils-devel/2026q2/009378.html

* libebl/eblinitreg_sample.c (ebl_set_initial_registers_sample):
  Guard the generic code on ebl->perf_frame_regs_mask != 0.

Reported-by: Sayed Kaif <[email protected]>
Signed-off-by: Serhei Makarov <[email protected]>
---
 libebl/eblinitreg_sample.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/libebl/eblinitreg_sample.c b/libebl/eblinitreg_sample.c
index feba2c64..32c1ab1c 100644
--- a/libebl/eblinitreg_sample.c
+++ b/libebl/eblinitreg_sample.c
@@ -61,10 +61,18 @@ ebl_set_initial_registers_sample (Ebl *ebl,
                                                regs_mapping, n_regs_mapping,
                                                setfunc, arg);
 
-  /* If set_initial_registers_sample is unspecified, then it is safe
-     to use the following generic code to populate a contiguous array
-     of dwarf_regs:  */
+  /* If perf_frame_regs_mask is zero, then the sample_regs
+     functionality is unsupported by this Ebl:  */
+  if (ebl->perf_frame_regs_mask == 0)
+    return false;
+
+  /* If set_initial_registers_sample is unspecified, and
+     perf_frame_regs_mask is nonzero, then it is safe to use the
+     following generic code to populate a contiguous small array of
+     dwarf_regs:  */
   Dwarf_Word dwarf_regs[64];
+  /* An Ebl that fails this assert is missing a custom
+     set_initial_registers_sample implementation:  */
   assert (ebl->frame_nregs < 64);
   size_t i;
   for (i = 0; i < ebl->frame_nregs; i++)
-- 
2.55.0

Reply via email to