From: Pu Hu <hupu(a)transsion.com>
note_buf_t is a typedef of a u32 array in include/linux/vmcore_info.h.
It is not architecture-specific. Clang often omits the typedef from
vmlinux DWARF even when the crash_notes symbol is present.
SIZE(note_buf) then aborts session startup:
crash: invalid structure size: note_buf
have_crash_notes() and the per-architecture get_crash_notes() paths
all read the per-cpu crash note with that size. Add crash_note_size()
and use it at those call sites. Look up note_buf_t first and note_buf
second. When neither type is available, measure one NT_PRSTATUS note
from the per-cpu crash note in memory and include the terminating
empty Elf note header. The measured length stays in a local variable
and is not written into size_table. If it still cannot be determined,
warn and continue.
A vmlinux that already contains note_buf_t keeps the previous size.
No constant note length is introduced.
Signed-off-by: Pu Hu <hupu(a)transsion.com>
---
arm.c | 18 ++++++++++--
arm64.c | 22 ++++++++++++---
defs.h | 1 +
diskdump.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--
loongarch64.c | 18 ++++++++++--
mips.c | 18 ++++++++++--
mips64.c | 18 ++++++++++--
ppc.c | 10 +++++--
riscv64.c | 18 ++++++++++--
sadump.c | 15 ++++++----
symbols.c | 4 +--
task.c | 4 +--
12 files changed, 192 insertions(+), 32 deletions(-)
diff --git a/arm.c b/arm.c
index 0532469..36133a3 100644
--- a/arm.c
+++ b/arm.c
@@ -551,6 +551,7 @@ arm_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i, found;
+ long note_sz;
if (!symbol_exists("crash_notes"))
return;
@@ -577,13 +578,24 @@ arm_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = found = 0; i<kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", i);
continue;
@@ -610,7 +622,7 @@ arm_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf32_Nhdr);
+ long notesz = note_sz - sizeof(Elf32_Nhdr);
if (sizeof(Elf32_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/arm64.c b/arm64.c
index 39f745b..ff58226 100644
--- a/arm64.c
+++ b/arm64.c
@@ -2788,6 +2788,8 @@ arm64_stackframe_init(void)
struct syment *sp1, *sp1n, *sp2, *sp2n, *sp3, *sp3n;
STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (INVALID_SIZE(note_buf))
+ STRUCT_SIZE_INIT(note_buf, "note_buf");
STRUCT_SIZE_INIT(elf_prstatus, "elf_prstatus");
MEMBER_OFFSET_INIT(elf_prstatus_pr_pid, "elf_prstatus", "pr_pid");
MEMBER_OFFSET_INIT(elf_prstatus_pr_reg, "elf_prstatus", "pr_reg");
@@ -5066,6 +5068,7 @@ arm64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i, found;
+ long note_sz;
if (!symbol_exists("crash_notes")) {
if (DISKDUMP_DUMPFILE() || KDUMP_DUMPFILE()) {
@@ -5127,13 +5130,24 @@ arm64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return;
+ }
+
+ buf = GETBUF(note_sz);
if (!(ms->panic_task_regs = calloc((size_t)kt->cpus, sizeof(struct
arm64_pt_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = found = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf),
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz,
"note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", i);
continue;
@@ -5157,10 +5171,10 @@ arm64_get_crash_notes(void)
note = netdump_get_prstatus_percpu(i);
if (note) {
/*
- * SIZE(note_buf) accounts for a "final note", which is a
+ * note_sz accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/defs.h b/defs.h
index c8708d1..cce535f 100644
--- a/defs.h
+++ b/defs.h
@@ -7569,6 +7569,7 @@ void x86_process_elf_notes(void *, unsigned long);
void *diskdump_get_prstatus_percpu(int);
int diskdump_is_cpu_prstatus_valid(int cpu);
int have_crash_notes(int cpu);
+long crash_note_size(ulong);
void map_cpus_to_prstatus_kdump_cmprs(void);
void diskdump_display_regs(int, FILE *);
void process_elf32_notes(void *, ulong);
diff --git a/diskdump.c b/diskdump.c
index 7263e2c..b7d28bc 100644
--- a/diskdump.c
+++ b/diskdump.c
@@ -104,11 +104,75 @@ int dumpfile_is_split(void)
return KDUMP_SPLIT();
}
+/*
+ * Bytes in one Elf note: header, aligned name, descriptor.
+ * Returns 0 when the header is not a usable NT_PRSTATUS note.
+ * Elf32_Nhdr and Elf64_Nhdr have the same three 32-bit fields.
+ */
+static long
+crash_note_size_from_mem(ulong addr)
+{
+ Elf64_Nhdr hdr;
+ long size, limit;
+
+ if (!addr)
+ return 0;
+
+ if (!readmem(addr, KVADDR, &hdr, sizeof(hdr),
+ "Elf64_Nhdr", RETURN_ON_ERROR|QUIET))
+ return 0;
+
+ limit = PAGESIZE() ? PAGESIZE() : 4096;
+ if (hdr.n_type != NT_PRSTATUS)
+ return 0;
+ if (!hdr.n_namesz || hdr.n_namesz > 256)
+ return 0;
+ if (!hdr.n_descsz || hdr.n_descsz > limit)
+ return 0;
+
+ size = sizeof(hdr);
+ size = roundup(size + hdr.n_namesz, 4);
+ size += hdr.n_descsz;
+ size = roundup(size, 4);
+ /* note_buf_t also contains the terminating empty note header */
+ size += sizeof(hdr);
+ return size;
+}
+
+/*
+ * Bytes to read from one per-cpu crash note.
+ *
+ * note_buf_t is a typedef of a u32 array in include/linux/vmcore_info.h.
+ * It is not architecture-specific. Clang often omits the typedef from
+ * DWARF, and SIZE(note_buf) then aborts startup. Look up note_buf_t,
+ * then note_buf. If neither type exists, measure the NT_PRSTATUS note
+ * at addr. A measured length is not written into size_table.
+ *
+ * Returns a positive size, or 0 if it cannot be determined.
+ */
+long
+crash_note_size(ulong addr)
+{
+ long sz;
+
+ if (!(VALID_SIZE(note_buf) && size_table.note_buf > 0)) {
+ STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (INVALID_SIZE(note_buf))
+ STRUCT_SIZE_INIT(note_buf, "note_buf");
+ }
+ if (VALID_SIZE(note_buf) && size_table.note_buf > 0)
+ return size_table.note_buf;
+
+ sz = crash_note_size_from_mem(addr);
+ return sz > 0 ? sz : 0;
+}
+
int have_crash_notes(int cpu)
{
ulong crash_notes, notes_ptr;
char *buf, *p;
Elf64_Nhdr *note = NULL;
+ long note_sz;
if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes,
sizeof(crash_notes), "crash_notes", RETURN_ON_ERROR)) {
@@ -121,11 +185,18 @@ int have_crash_notes(int cpu)
else
notes_ptr = crash_notes;
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptr);
+ if (note_sz <= 0) {
+ error(WARNING, "cpu %d: cannot determine size of note_buf_t\n", cpu);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!readmem(notes_ptr, KVADDR, buf,
- SIZE(note_buf), "note_buf_t", RETURN_ON_ERROR)) {
+ note_sz, "note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", cpu);
+ FREEBUF(buf);
return FALSE;
}
@@ -134,14 +205,17 @@ int have_crash_notes(int cpu)
if (note->n_type != NT_PRSTATUS) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (n_type != NT_PRSTATUS)\n",
cpu);
+ FREEBUF(buf);
return FALSE;
}
if (!STRNEQ(p, "CORE")) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (name !=
\"CORE\")\n", cpu);
+ FREEBUF(buf);
return FALSE;
}
+ FREEBUF(buf);
return TRUE;
}
diff --git a/loongarch64.c b/loongarch64.c
index 6b518d4..1519de3 100644
--- a/loongarch64.c
+++ b/loongarch64.c
@@ -1559,6 +1559,7 @@ loongarch64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -1596,14 +1597,25 @@ loongarch64_get_crash_notes(void)
}
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -1631,7 +1643,7 @@ loongarch64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/mips.c b/mips.c
index d6602e3..70543eb 100644
--- a/mips.c
+++ b/mips.c
@@ -807,6 +807,7 @@ mips_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
if (!symbol_exists("crash_notes"))
return FALSE;
@@ -834,14 +835,25 @@ mips_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i=0;i<kt->cpus;i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING, "failed to read note_buf_t\n");
goto fail;
@@ -868,7 +880,7 @@ mips_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf32_Nhdr);
+ long notesz = note_sz - sizeof(Elf32_Nhdr);
if (sizeof(Elf32_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/mips64.c b/mips64.c
index b1d6acf..13f7e26 100644
--- a/mips64.c
+++ b/mips64.c
@@ -881,6 +881,7 @@ mips64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -914,14 +915,25 @@ mips64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -949,7 +961,7 @@ mips64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/ppc.c b/ppc.c
index cf5bf56..182e1d7 100644
--- a/ppc.c
+++ b/ppc.c
@@ -1984,6 +1984,7 @@ verify_crash_note_in_kernel(int cpu)
Elf32_Nhdr *note32;
ulong crash_notes_ptr;
char *buf, *name;
+ long note_sz;
ret = TRUE;
if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes_ptr,
@@ -1991,10 +1992,15 @@ verify_crash_note_in_kernel(int cpu)
!crash_notes_ptr)
goto out;
- buf = GETBUF(SIZE(note_buf));
if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF))
crash_notes_ptr += kt->__per_cpu_offset[cpu];
- if (!readmem(crash_notes_ptr, KVADDR, buf, SIZE(note_buf),
+ note_sz = crash_note_size(crash_notes_ptr);
+ if (note_sz <= 0) {
+ ret = FALSE;
+ goto out;
+ }
+ buf = GETBUF(note_sz);
+ if (!readmem(crash_notes_ptr, KVADDR, buf, note_sz,
"cpu crash_notes", QUIET|RETURN_ON_ERROR))
goto freebuf;
diff --git a/riscv64.c b/riscv64.c
index 1b6396c..1d011b6 100644
--- a/riscv64.c
+++ b/riscv64.c
@@ -1381,6 +1381,7 @@ riscv64_get_crash_notes(void)
char *buf, *p;
ulong *notes_ptrs;
ulong i;
+ long note_sz;
/*
* crash_notes contains per cpu memory for storing cpu states
@@ -1414,14 +1415,25 @@ riscv64_get_crash_notes(void)
notes_ptrs[i] = notes_ptrs[kt->cpus-1] + kt->__per_cpu_offset[i];
}
- buf = GETBUF(SIZE(note_buf));
+ note_sz = crash_note_size(notes_ptrs[0]);
+ if (note_sz <= 0) {
+ for (i = 1; i < kt->cpus && note_sz <= 0; i++)
+ note_sz = crash_note_size(notes_ptrs[i]);
+ }
+ if (note_sz <= 0) {
+ error(WARNING, "cannot determine size of note_buf_t\n");
+ FREEBUF(notes_ptrs);
+ return FALSE;
+ }
+
+ buf = GETBUF(note_sz);
if (!(panic_task_regs = calloc((size_t)kt->cpus, sizeof(*panic_task_regs))))
error(FATAL, "cannot calloc panic_task_regs space\n");
for (i = 0; i < kt->cpus; i++) {
- if (!readmem(notes_ptrs[i], KVADDR, buf, SIZE(note_buf), "note_buf_t",
+ if (!readmem(notes_ptrs[i], KVADDR, buf, note_sz, "note_buf_t",
RETURN_ON_ERROR)) {
error(WARNING,
"cannot find NT_PRSTATUS note for cpu: %d\n", i);
@@ -1449,7 +1461,7 @@ riscv64_get_crash_notes(void)
* SIZE(note_buf) accounts for a "final note", which is a
* trailing empty elf note header.
*/
- long notesz = SIZE(note_buf) - sizeof(Elf64_Nhdr);
+ long notesz = note_sz - sizeof(Elf64_Nhdr);
if (sizeof(Elf64_Nhdr) + roundup(note->n_namesz, 4) +
note->n_descsz == notesz)
diff --git a/sadump.c b/sadump.c
index cb43fdb..9fd05c9 100644
--- a/sadump.c
+++ b/sadump.c
@@ -1227,6 +1227,7 @@ get_prstatus_from_crash_notes(int cpu, char *prstatus)
ulong crash_notes, crash_notes_ptr, percpu_addr;
char *prstatus_ptr, *note_buf, *zero_buf, *name;
uint32_t *buf;
+ long note_sz;
if (cpu < 0 || kt->cpus <= cpu) {
error(INFO, "sadump: given cpu is invalid: %d\n", cpu);
@@ -1254,15 +1255,19 @@ get_prstatus_from_crash_notes(int cpu, char *prstatus)
? legacy_per_cpu_ptr(crash_notes_ptr, cpu)
: per_cpu_ptr(crash_notes_ptr, cpu);
- zero_buf = GETBUF(SIZE(note_buf));
- BZERO(zero_buf, SIZE(note_buf));
+ note_sz = crash_note_size(percpu_addr);
+ if (note_sz <= 0)
+ return FALSE;
+
+ zero_buf = GETBUF(note_sz);
+ BZERO(zero_buf, note_sz);
- note_buf = GETBUF(SIZE(note_buf));
+ note_buf = GETBUF(note_sz);
- readmem(percpu_addr, KVADDR, note_buf, SIZE(note_buf),
+ readmem(percpu_addr, KVADDR, note_buf, note_sz,
"read crash_notes", FAULT_ON_ERROR);
- if (memcmp(note_buf, zero_buf, SIZE(note_buf)) == 0)
+ if (memcmp(note_buf, zero_buf, note_sz) == 0)
return FALSE;
if (BITS64()) {
diff --git a/symbols.c b/symbols.c
index 270e14e..b1bc01e 100644
--- a/symbols.c
+++ b/symbols.c
@@ -12218,8 +12218,8 @@ dump_offset_table(char *spec, ulong makestruct)
SIZE(cpu_context_save));
fprintf(fp, " elf_prstatus: %ld\n",
SIZE(elf_prstatus));
- fprintf(fp, " note_buf: %ld\n",
- SIZE(note_buf));
+ fprintf(fp, " note_buf: %ld\n",
+ size_table.note_buf);
fprintf(fp, " unwind_idx: %ld\n",
SIZE(unwind_idx));
fprintf(fp, " s390_stack_frame: %ld\n",
diff --git a/task.c b/task.c
index d9d592e..994f137 100644
--- a/task.c
+++ b/task.c
@@ -687,8 +687,8 @@ task_init(void)
tt->this_task = pid_to_task(active_pid);
}
else {
- if (INVALID_SIZE(note_buf))
- STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+ if (!(VALID_SIZE(note_buf) && size_table.note_buf > 0))
+ crash_note_size(0);
if (KDUMP_DUMPFILE())
map_cpus_to_prstatus();
--
2.43.0