Skip to contentSkip to conversation

objtool: Support the stack swizzle

PATCH In review Peter Zijlstra 3 participants 10 messages Feb 9, 2021 – Feb 10, 2021

Subsystems (3)
lore
Share & tools
lore
author maintainer
#7 lore raw

PATCH v2 3/3 objtool: Support stack-swizzle

In reply to Peter Zijlstra

Natively support the stack swizzle pattern:

	mov %rsp, (%[tos])
	mov %[tos], %rsp
	...
	pop %rsp

It uses the vals[] array to link the first two stack-ops, and detect the SP to SP_INDIRECT swizzle.

---
 tools/objtool/check.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
tools/objtool/check.c +45 -0
@@ -1945,6 +1945,38 @@ static int update_cfi_state(struct instr
 					cfa->offset = -cfi->vals[op->src.reg].offset;
 					cfi->stack_size = cfa->offset;
 
+				} else if (cfa->base == CFI_SP &&
+					   cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
+					   cfi->vals[op->src.reg].offset == cfa->offset) {
+
+					/*
+					 * Stack swizzle:
+					 *
+					 * 1: mov %rsp, (%[tos])
+					 * 2: mov %[tos], %rsp
+					 *    ...
+					 * 3: pop %rsp
+					 *
+					 * Where:
+					 *
+					 * 1 - places a pointer to the previous
+					 *     stack at the Top-of-Stack of the
+					 *     new stack.
+					 *
+					 * 2 - switches to the new stack.
+					 *
+					 * 3 - pops the Top-of-Stack to restore
+					 *     the original stack.
+					 *
+					 * Note: we set base to SP_INDIRECT
+					 * here and preserve offset. Therefore
+					 * when the unwinder reaches ToS it
+					 * will dereference SP and then add the
+					 * offset to find the next frame, IOW:
+					 * (%rsp) + offset.
+					 */
+					cfa->base = CFI_SP_INDIRECT;
+
 				} else {
 					cfa->base = CFI_UNDEFINED;
 					cfa->offset = 0;
@@ -2047,6 +2079,13 @@ static int update_cfi_state(struct instr
 
 		case OP_SRC_POP:
 		case OP_SRC_POPF:
+			if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
+
+				/* pop %rsp; # restore from a stack swizzle */
+				cfa->base = CFI_SP;
+				break;
+			}
+
 			if (!cfi->drap && op->dest.reg == cfa->base) {
 
 				/* pop %rbp */
@@ -2193,6 +2232,12 @@ static int update_cfi_state(struct instr
 			/* mov reg, disp(%rsp) */
 			save_reg(cfi, op->src.reg, CFI_CFA,
 				 op->dest.offset - cfi->stack_size);
+
+		} else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
+
+			/* mov %rsp, (%reg); # setup a stack swizzle. */
+			cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
+			cfi->vals[op->dest.reg].offset = cfi->cfa.offset;
 		}
 
 		break;

Peter Zijlstra

author maintainer
#0 lore raw

v2 cover objtool: Support the stack swizzle

Hi!

Implement objtool support for the x86_64 stack swizzle pattern.

This means we can use the minial stack swizzle:

mov %rsp, (%[tos])
mov %[tos], %rsp
...
pop %rsp

from inline asm, with arbitrary stack setup. The ORC data for the Top-of-Stack will use the SP_INDIRECT CFA base. In order for this to work, SP_INDIRECT needs to first dereference and then add the offset to find the next frame.

Therefore we need to change SP_INDIRECT (which is currently unused) to mean:
(%rsp) + offset.

Changes since v1 include:

- removed the !callee saved reg restriction by using the vals[] array over the regs[] array.

- per the above, removed the patches creating the regs[] scratch space.

- more comments.

- rebased to tip/objtool/core

author maintainer
#2 lore raw

PATCH v2 2/3 objtool,x86: Additionally decode: mov %rsp, (%reg)

Where we already decode: mov %rsp, %reg, also decode mov %rsp, (%reg).

Nothing should match for this new stack-op.

---
 tools/objtool/arch/x86/decode.c |   23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +18 -5
@@ -222,14 +222,24 @@ int arch_decode_instruction(const struct
 		break;
 
 	case 0x89:
-		if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
+		if (rex_w && !rex_r && modrm_reg == 4) {
 
-			/* mov %rsp, reg */
+			/* mov %rsp, */
 			ADD_OP(op) {
 				op->src.type = OP_SRC_REG;
 				op->src.reg = CFI_SP;
-				op->dest.type = OP_DEST_REG;
-				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				if (modrm_mod == 3) {
+
+					/* mov %rsp, reg */
+					op->dest.type = OP_DEST_REG;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+
+				} else if (modrm_mod == 0) {
+
+					/* mov %rsp, (%reg) */
+					op->dest.type = OP_DEST_REG_INDIRECT;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				}
 			}
 			break;
 		}
@@ -259,8 +269,10 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_BP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
+		}
 
-		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
+		if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
 
 			/* mov reg, disp(%rsp) */
 			ADD_OP(op) {
@@ -270,6 +282,7 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_SP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
 		}
 
 		break;
author maintainer
#1 lore raw

PATCH v2 1/3 x86/unwind/orc: Change REG_SP_INDIRECT

In reply to Peter Zijlstra

Currently REG_SP_INDIRECT is unused but means (%rsp + offset), change it to mean (%rsp) + offset.

The reason is that we're going to swizzle stack in the middle of a C function with non-trivial stack footprint. This means that when the unwinder finds the ToS, it needs to dereference it (%rsp) and then add the offset to the next frame, resulting in: (%rsp) + offset

This is somewhat unfortunate, since REG_BP_INDIRECT is used (by DRAP) and thus needs to retain the current (%rbp + offset).

---
 arch/x86/kernel/unwind_orc.c |    5 ++++-
 tools/objtool/orc_dump.c     |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/unwind_orc.c +4 -1
@@ -471,7 +471,7 @@ bool unwind_next_frame(struct unwind_sta
 		break;
 
 	case ORC_REG_SP_INDIRECT:
-		sp = state->sp + orc->sp_offset;
+		sp = state->sp;
 		indirect = true;
 		break;
 
@@ -521,6 +521,9 @@ bool unwind_next_frame(struct unwind_sta
 	if (indirect) {
 		if (!deref_stack_reg(state, sp, &sp))
 			goto err;
+
+		if (orc->sp_reg == ORC_REG_SP_INDIRECT)
+			sp += orc->sp_offset;
 	}
 
 	/* Find IP, SP and possibly regs: */
tools/objtool/orc_dump.c +1 -1
@@ -54,7 +54,7 @@ static void print_reg(unsigned int reg,
 	if (reg == ORC_REG_BP_INDIRECT)
 		printf("(bp%+d)", offset);
 	else if (reg == ORC_REG_SP_INDIRECT)
-		printf("(sp%+d)", offset);
+		printf("(sp)%+d", offset);
 	else if (reg == ORC_REG_UNDEFINED)
 		printf("(und)");
 	else
author maintainer
#8 lore raw

Re: [PATCH v2 3/3] objtool: Support stack-swizzle

In reply to Peter Zijlstra

tools/objtool/check.c static int update_cfi_state(struct instr · line 2240

Quoted 10 lines · Peter Zijlstra
tools/objtool/check.c +6 -0
@@ -2193,6 +2232,12 @@ static int update_cfi_state(struct instr
 			/* mov reg, disp(%rsp) */
 			save_reg(cfi, op->src.reg, CFI_CFA,
 				 op->dest.offset - cfi->stack_size);
+
+		} else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
+
+			/* mov %rsp, (%reg); # setup a stack swizzle. */
+			cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
+			cfi->vals[op->dest.reg].offset = cfi->cfa.offset;

I'll change that to:

			cfi->vals[op->dest.reg].offset = cfa->offset;

To be more consistent with the rest of the code.

Quoted 5 lines
(quoted diff) +0 -0
 		}
 
 		break;
Reviewed-by on the v2 series
#9 lore raw
In reply to Peter Zijlstra
Quoted 20 lines · Peter Zijlstra

Hi!

Implement objtool support for the x86_64 stack swizzle pattern.

This means we can use the minial stack swizzle:

mov %rsp, (%[tos])
mov %[tos], %rsp
...
pop %rsp

from inline asm, with arbitrary stack setup. The ORC data for the Top-of-Stack will use the SP_INDIRECT CFA base. In order for this to work, SP_INDIRECT needs to first dereference and then add the offset to find the next frame.

Therefore we need to change SP_INDIRECT (which is currently unused) to mean:
(%rsp) + offset.

Changes since v1 include:

Quoted 8 lines

- removed the !callee saved reg restriction by using the vals[] array over the regs[] array.

- per the above, removed the patches creating the regs[] scratch space.

- more comments.

- rebased to tip/objtool/core

I haven't tested it, but it all looks good to me.

M

#3 lore raw

Re: [PATCH v2 2/3] objtool,x86: Additionally decode: mov %rsp, (%reg)

In reply to Peter Zijlstra
Quoted 27 lines · Peter Zijlstra

Where we already decode: mov %rsp, %reg, also decode mov %rsp, (%reg).

Nothing should match for this new stack-op.

---
 tools/objtool/arch/x86/decode.c |   23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +4 -4
@@ -222,14 +222,24 @@ int arch_decode_instruction(const struct
 		break;
 
 	case 0x89:
-		if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
+		if (rex_w && !rex_r && modrm_reg == 4) {
 
-			/* mov %rsp, reg */
+			/* mov %rsp, */
 			ADD_OP(op) {
 				op->src.type = OP_SRC_REG;
 				op->src.reg = CFI_SP;
-				op->dest.type = OP_DEST_REG;
-				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				if (modrm_mod == 3) {
+

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct · line 242

Quoted 10 lines
tools/objtool/arch/x86/decode.c +10 -0
+					/* mov %rsp, reg */
+					op->dest.type = OP_DEST_REG;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+
+				} else if (modrm_mod == 0) {
+
+					/* mov %rsp, (%reg) */
+					op->dest.type = OP_DEST_REG_INDIRECT;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				}

What if modrm_mod is 1 or 2? Should 'if' below the 'case' make sure
it's 0 or 3?

Quoted 17 lines
tools/objtool/arch/x86/decode.c +3 -1
 			}
 			break;
 		}
@@ -259,8 +269,10 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_BP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
+		}
 
-		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
+		if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
 
 			/* mov reg, disp(%rsp) */
 			ADD_OP(op) {

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct · line 288

Quoted 7 lines
tools/objtool/arch/x86/decode.c +1 -0
@@ -270,6 +282,7 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_SP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
 		}
 
 		break;

Did this change have a point?

Signature · 1 line
Josh
author maintainer
#4 lore raw

Re: [PATCH v2 2/3] objtool,x86: Additionally decode: mov %rsp, (%reg)

Quoted 33 lines · Josh Poimboeuf
Quoted 32 lines · Peter Zijlstra

Where we already decode: mov %rsp, %reg, also decode mov %rsp, (%reg).

Nothing should match for this new stack-op.

---
 tools/objtool/arch/x86/decode.c |   23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +9 -4
@@ -222,14 +222,24 @@ int arch_decode_instruction(const struct
 		break;
 
 	case 0x89:
-		if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
+		if (rex_w && !rex_r && modrm_reg == 4) {
 
-			/* mov %rsp, reg */
+			/* mov %rsp, */
 			ADD_OP(op) {
 				op->src.type = OP_SRC_REG;
 				op->src.reg = CFI_SP;
-				op->dest.type = OP_DEST_REG;
-				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				if (modrm_mod == 3) {
+
+					/* mov %rsp, reg */
+					op->dest.type = OP_DEST_REG;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+
+				} else if (modrm_mod == 0) {
Quoted 8 lines

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct · line 242

Quoted 5 lines
tools/objtool/arch/x86/decode.c +5 -0
+
+					/* mov %rsp, (%reg) */
+					op->dest.type = OP_DEST_REG_INDIRECT;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				}

What if modrm_mod is 1 or 2? Should 'if' below the 'case' make sure
it's 0 or 3?

For 1,2 we need to look at the SIB byte or something. IIRC you get that encoding for stuff like: mov %rsp, off(%reg).

Didn't want to dive too deep into the instruction encoding thing again, this is all we need.

Quoted 22 lines
Quoted 22 lines
tools/objtool/arch/x86/decode.c +4 -1
 			}
 			break;
 		}
@@ -259,8 +269,10 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_BP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
+		}
 
-		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
+		if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
 
 			/* mov reg, disp(%rsp) */
 			ADD_OP(op) {
@@ -270,6 +282,7 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_SP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
 		}
 
Quoted 4 lines

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct · line 288

Quoted 2 lines
tools/objtool/arch/x86/decode.c +0 -0
 		break;

Did this change have a point?

Consistency, but yeah, I see what you mean.

author maintainer
#5 lore raw

PATCH v2 2/3 objtool,x86: Additionally decode: mov %rsp, (%reg)

In reply to Peter Zijlstra

Subject: objtool,x86: Additionally decode: mov %rsp, (%reg)
From: Peter Zijlstra <[email protected]>
Date: Wed Feb 3 12:02:18 CET 2021

Where we already decode: mov %rsp, %reg, also decode mov %rsp, (%reg).

Nothing should match for this new stack-op.

---
 tools/objtool/arch/x86/decode.c |   42 ++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)
tools/objtool/arch/x86/decode.c +34 -8
@@ -222,15 +222,38 @@ int arch_decode_instruction(const struct
 		break;
 
 	case 0x89:
-		if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
+		if (rex_w && !rex_r && modrm_reg == 4) {
 
-			/* mov %rsp, reg */
-			ADD_OP(op) {
-				op->src.type = OP_SRC_REG;
-				op->src.reg = CFI_SP;
-				op->dest.type = OP_DEST_REG;
-				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+			if (modrm_mod == 3) {
+				/* mov %rsp, reg */
+				ADD_OP(op) {
+					op->src.type = OP_SRC_REG;
+					op->src.reg = CFI_SP;
+					op->dest.type = OP_DEST_REG;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+				}
+				break;
+
+			} else {
+				/* skip nontrivial SIB */
+				if (modrm_rm == 4 && sib != 0x24)
+					break;
+
+				/* skip RIP relative displacement */
+				if (modrm_rm == 5 && modrm_mod == 0)
+					break;
+
+				/* mov %rsp, disp(%reg) */
+				ADD_OP(op) {
+					op->src.type = OP_SRC_REG;
+					op->src.reg = CFI_SP;
+					op->dest.type = OP_DEST_REG_INDIRECT;
+					op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
+					op->dest.offset = insn.displacement.value;
+				}
+				break;
 			}
+
 			break;
 		}
 
@@ -259,8 +282,10 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_BP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
+		}
 
-		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
+		if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
 
 			/* mov reg, disp(%rsp) */
 			ADD_OP(op) {
@@ -270,6 +295,7 @@ int arch_decode_instruction(const struct
 				op->dest.reg = CFI_SP;
 				op->dest.offset = insn.displacement.value;
 			}
+			break;
 		}
 
 		break;
author maintainer
#6 lore raw

Re: [PATCH v2.1 2/3] objtool,x86: Additionally decode: mov %rsp, (%reg)

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct · line 240

Quoted 3 lines · Peter Zijlstra
tools/objtool/arch/x86/decode.c +3 -0
+				/* skip nontrivial SIB */
+				if (modrm_rm == 4 && sib != 0x24)
+					break;

Hmm,, maybe that should be:

	if (modrm_rm == 4 && !(sib == 0x24 && rex_b == rex_x))

Because what we have is that once we have a SIB byte, rex_b is for sib_base and rex_x is always sib_index, and we need to ensure that sib_base == sib_index for the trivial case.

/me changes...