Skip to contentSkip to conversation

perf annotate: Invalidate register states for unsupported instructions

PATCH Superseded Zecheng Li 2 participants 4 messages Aug 25, 2025 – Sep 5, 2025 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Zecheng Li

author
#0 lore raw

PATCH v2 7/10 perf annotate: Invalidate register states for unsupported instructions

Invalidate register states when encountering unsupported instructions that modify pointers, to prevent propagating incorrect pointer types.

On x86, the 'xor' instruction may appear in a predecessor basic block and zero out a register that invalidates the target register state. This sometimes relates to tagged pointers and normal programs should not dereference NULL pointers, so we assume such execution paths are invalid and do not invalidate states for 'xor' instructions.

---
 tools/perf/arch/x86/annotate/instructions.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/perf/arch/x86/annotate/instructions.c +17 -0
index 540b4d0a01af..03df52a5266d 100644
@@ -413,6 +413,23 @@ static void update_insn_state_x86(struct type_state *state,
 		return;
 	}
 
+	/* Invalidate register states for other ops which may change pointers */
+	if (has_reg_type(state, dst->reg1) && !dst->mem_ref &&
+	    dwarf_tag(&state->regs[dst->reg1].type) == DW_TAG_pointer_type) {
+		if (!strncmp(dl->ins.name, "imul", 4) || !strncmp(dl->ins.name, "mul", 3) ||
+		    !strncmp(dl->ins.name, "idiv", 4) || !strncmp(dl->ins.name, "div", 3) ||
+		    !strncmp(dl->ins.name, "shl", 3)  || !strncmp(dl->ins.name, "shr", 3) ||
+		    !strncmp(dl->ins.name, "sar", 3)  || !strncmp(dl->ins.name, "and", 3) ||
+		    !strncmp(dl->ins.name, "or", 2)   || !strncmp(dl->ins.name, "neg", 3) ||
+		    !strncmp(dl->ins.name, "inc", 3)  || !strncmp(dl->ins.name, "dec", 3)) {
+			pr_debug_dtp("%s [%x] invalidate reg%d\n",
+						dl->ins.name, insn_offset, dst->reg1);
+			state->regs[dst->reg1].ok = false;
+			state->regs[dst->reg1].copied_from = -1;
+			return;
+		}
+	}
+
 	if (strncmp(dl->ins.name, "mov", 3))
 		return;
 
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
maintainer
#1 lore raw
Quoted 8 lines · Zecheng Li

Invalidate register states when encountering unsupported instructions that modify pointers, to prevent propagating incorrect pointer types.

On x86, the 'xor' instruction may appear in a predecessor basic block and zero out a register that invalidates the target register state. This sometimes relates to tagged pointers and normal programs should not dereference NULL pointers, so we assume such execution paths are invalid and do not invalidate states for 'xor' instructions.

Probably we can set it to 0 with TSR_KIND_CONST.

Thanks,
Namhyung

Quoted 37 lines
---
 tools/perf/arch/x86/annotate/instructions.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/perf/arch/x86/annotate/instructions.c +17 -0
index 540b4d0a01af..03df52a5266d 100644
@@ -413,6 +413,23 @@ static void update_insn_state_x86(struct type_state *state,
 		return;
 	}
 
+	/* Invalidate register states for other ops which may change pointers */
+	if (has_reg_type(state, dst->reg1) && !dst->mem_ref &&
+	    dwarf_tag(&state->regs[dst->reg1].type) == DW_TAG_pointer_type) {
+		if (!strncmp(dl->ins.name, "imul", 4) || !strncmp(dl->ins.name, "mul", 3) ||
+		    !strncmp(dl->ins.name, "idiv", 4) || !strncmp(dl->ins.name, "div", 3) ||
+		    !strncmp(dl->ins.name, "shl", 3)  || !strncmp(dl->ins.name, "shr", 3) ||
+		    !strncmp(dl->ins.name, "sar", 3)  || !strncmp(dl->ins.name, "and", 3) ||
+		    !strncmp(dl->ins.name, "or", 2)   || !strncmp(dl->ins.name, "neg", 3) ||
+		    !strncmp(dl->ins.name, "inc", 3)  || !strncmp(dl->ins.name, "dec", 3)) {
+			pr_debug_dtp("%s [%x] invalidate reg%d\n",
+						dl->ins.name, insn_offset, dst->reg1);
+			state->regs[dst->reg1].ok = false;
+			state->regs[dst->reg1].copied_from = -1;
+			return;
+		}
+	}
+
 	if (strncmp(dl->ins.name, "mov", 3))
 		return;
 
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
author
#2 lore raw
Quoted 5 lines · Namhyung Kim
Quoted 3 lines · Zecheng Li

Invalidate register states when encountering unsupported instructions that modify pointers, to prevent propagating incorrect pointer types.

Quoted 8 lines
Quoted 5 lines

On x86, the 'xor' instruction may appear in a predecessor basic block and zero out a register that invalidates the target register state. This sometimes relates to tagged pointers and normal programs should not dereference NULL pointers, so we assume such execution paths are invalid and do not invalidate states for 'xor' instructions.

Probably we can set it to 0 with TSR_KIND_CONST.

It seems TSR_KIND_CONST doesn't relate to a type. Although the value was set to 0, it still has the pointer type. I see regressions that has this pattern

xorl %rax, %rax
mov %rax, (%rsp)

and sometimes

xorl %rax, %rax
... (some branches)
mov (%rax), %rbx

Normally NULL pointer dereference should not happen, so I assume such execution paths are invalid.

maintainer
#3 lore raw
Quoted 21 lines · Zecheng Li
Quoted 5 lines · Namhyung Kim
Quoted 3 lines · Zecheng Li

Invalidate register states when encountering unsupported instructions that modify pointers, to prevent propagating incorrect pointer types.

Quoted 8 lines
Quoted 5 lines

On x86, the 'xor' instruction may appear in a predecessor basic block and zero out a register that invalidates the target register state. This sometimes relates to tagged pointers and normal programs should not dereference NULL pointers, so we assume such execution paths are invalid and do not invalidate states for 'xor' instructions.

Probably we can set it to 0 with TSR_KIND_CONST.

It seems TSR_KIND_CONST doesn't relate to a type. Although the value was set to 0, it still has the pointer type. I see regressions that has this pattern

xorl %rax, %rax
mov %rax, (%rsp)

Quoted 8 lines

and sometimes

xorl %rax, %rax
... (some branches)
mov (%rax), %rbx

Normally NULL pointer dereference should not happen, so I assume such execution paths are invalid.

Agreed, I think there should be a place to set the pointer to a valid value again (before use).

Thanks,
Namhyung