Skip to contentSkip to conversation

perf annotate: Fix BUG_ON in basic block processing

PATCH In review Andi Kleen 2 participants 2 messages Apr 10, 2025 – Apr 11, 2025 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 perf annotate: Fix BUG_ON in basic block processing

Hit with perf annotate --code-with-type --stdio

perf: util/annotate-data.c:1299: find_data_type_insn: Assertion `!(bb->begin->al.offset == -1 || bb->end->al.offset == -1)' failed.

This makes sure to skip non instruction disasm lines in all cases that add basic blocks. Also added some extra BUG_ONs for this case.

---
 tools/perf/util/annotate-data.c | 10 +++++++++-
 tools/perf/util/annotate.c      |  4 +++-
 2 files changed, 12 insertions(+), 2 deletions(-)
tools/perf/util/annotate-data.c +9 -1
index c3ffb8cad4fc..51ca86ef2677 100644
@@ -986,7 +986,15 @@ static void prepend_basic_blocks(struct list_head *this_blocks,
 	}
 
 	/* Point to the insn before the last when adding this block to full_blocks */
-	last_bb->end = list_prev_entry(last_bb->end, al.node);
+	do {
+		/* Nothing useful found */
+		if (last_bb->end == last_bb->begin) {
+			list_del(&last_bb->list);
+			free(last_bb);
+			goto out;
+		}
+		last_bb->end = list_prev_entry(last_bb->end, al.node);
+	} while (last_bb->end->al.offset == -1);
 
 out:
 	list_splice(this_blocks, full_blocks);
tools/perf/util/annotate.c +3 -1
index 18ce9f37053e..1c226f257245 100644
@@ -2966,6 +2966,7 @@ static int add_basic_block(struct basic_block_data *bb_data,
 
 	if (dl == NULL)
 		return -1;
+	BUG_ON(dl->al.offset == -1);
 
 	if (!is_new_basic_block(bb_data, dl))
 		return 0;
@@ -3006,7 +3007,7 @@ static bool process_basic_block(struct basic_block_data *bb_data,
 
 	last_dl = list_last_entry(&notes->src->source,
 				  struct disasm_line, al.node);
-	if (last_dl->al.offset == -1)
+	while (last_dl && last_dl->al.offset == -1)
 		last_dl = annotation__prev_asm_line(notes, last_dl);
 
 	if (last_dl == NULL)
@@ -3052,6 +3053,7 @@ static bool process_basic_block(struct basic_block_data *bb_data,
 		break;
 
 	}
+	BUG_ON(dl->al.offset == -1);
 	link->bb->end = dl;
 	return found;
 }
Signature · 1 line
2.49.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Andi Kleen

Hit with perf annotate --code-with-type --stdio

perf: util/annotate-data.c:1299: find_data_type_insn: Assertion `!(bb->begin->al.offset == -1 || bb->end->al.offset == -1)' failed.

This makes sure to skip non instruction disasm lines in all cases that add basic blocks. Also added some extra BUG_ONs for this case.

Thanks,
Namhyung

Quoted 58 lines
---
 tools/perf/util/annotate-data.c | 10 +++++++++-
 tools/perf/util/annotate.c      |  4 +++-
 2 files changed, 12 insertions(+), 2 deletions(-)
tools/perf/util/annotate-data.c +9 -1
index c3ffb8cad4fc..51ca86ef2677 100644
@@ -986,7 +986,15 @@ static void prepend_basic_blocks(struct list_head *this_blocks,
 	}
 
 	/* Point to the insn before the last when adding this block to full_blocks */
-	last_bb->end = list_prev_entry(last_bb->end, al.node);
+	do {
+		/* Nothing useful found */
+		if (last_bb->end == last_bb->begin) {
+			list_del(&last_bb->list);
+			free(last_bb);
+			goto out;
+		}
+		last_bb->end = list_prev_entry(last_bb->end, al.node);
+	} while (last_bb->end->al.offset == -1);
 
 out:
 	list_splice(this_blocks, full_blocks);
tools/perf/util/annotate.c +3 -1
index 18ce9f37053e..1c226f257245 100644
@@ -2966,6 +2966,7 @@ static int add_basic_block(struct basic_block_data *bb_data,
 
 	if (dl == NULL)
 		return -1;
+	BUG_ON(dl->al.offset == -1);
 
 	if (!is_new_basic_block(bb_data, dl))
 		return 0;
@@ -3006,7 +3007,7 @@ static bool process_basic_block(struct basic_block_data *bb_data,
 
 	last_dl = list_last_entry(&notes->src->source,
 				  struct disasm_line, al.node);
-	if (last_dl->al.offset == -1)
+	while (last_dl && last_dl->al.offset == -1)
 		last_dl = annotation__prev_asm_line(notes, last_dl);
 
 	if (last_dl == NULL)
@@ -3052,6 +3053,7 @@ static bool process_basic_block(struct basic_block_data *bb_data,
 		break;
 
 	}
+	BUG_ON(dl->al.offset == -1);
 	link->bb->end = dl;
 	return found;
 }
Signature · 1 line
2.49.0