Skip to content

Commit 7117f16

Browse files
author
Peter Zijlstra
committed
objtool: Fix ORC vs alternatives
Jann reported that (for instance) entry_64.o:general_protection has very odd ORC data: 0000000000000f40 <general_protection>: #######sp:sp+8 bp:(und) type:iret end:0 f40: 90 nop #######sp:(und) bp:(und) type:call end:0 f41: 90 nop f42: 90 nop #######sp:sp+8 bp:(und) type:iret end:0 f43: e8 a8 01 00 00 callq 10f0 <error_entry> #######sp:sp+0 bp:(und) type:regs end:0 f48: f6 84 24 88 00 00 00 testb $0x3,0x88(%rsp) f4f: 03 f50: 74 00 je f52 <general_protection+0x12> f52: 48 89 e7 mov %rsp,%rdi f55: 48 8b 74 24 78 mov 0x78(%rsp),%rsi f5a: 48 c7 44 24 78 ff ff movq $0xffffffffffffffff,0x78(%rsp) f61: ff ff f63: e8 00 00 00 00 callq f68 <general_protection+0x28> f68: e9 73 02 00 00 jmpq 11e0 <error_exit> #######sp:(und) bp:(und) type:call end:0 f6d: 0f 1f 00 nopl (%rax) Note the entry at 0xf41. Josh found this was the result of commit: 764eef4 ("objtool: Rewrite alt->skip_orig") Due to the early return in validate_branch() we no longer set insn->cfi of the original instruction stream (the NOPs at 0xf41 and 0xf42) and we'll end up with the above weirdness. In other discussions we realized alternatives should be ORC invariant; that is, due to there being only a single ORC table, it must be valid for all alternatives. The easiest way to ensure this is to not allow any stack modifications in alternatives. When we enforce this latter observation, we get the property that the whole alternative must have the same CFI, which we can employ to fix the former report. Fixes: 764eef4 ("objtool: Rewrite alt->skip_orig") Reported-by: Jann Horn <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 13fab06 commit 7117f16

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

tools/objtool/Documentation/stack-validation.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ they mean, and suggestions for how to fix them.
315315
function tracing inserts additional calls, which is not obvious from the
316316
sources).
317317

318+
10. file.o: warning: func()+0x5c: alternative modifies stack
319+
320+
This means that an alternative includes instructions that modify the
321+
stack. The problem is that there is only one ORC unwind table, this means
322+
that the ORC unwind entries must be valid for each of the alternatives.
323+
The easiest way to enforce this is to ensure alternatives do not contain
324+
any ORC entries, which in turn implies the above constraint.
318325

319326
If the error doesn't seem to make sense, it could be a bug in objtool.
320327
Feel free to ask the objtool maintainer for help.

tools/objtool/check.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,11 @@ static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
19831983
list_for_each_entry(op, &insn->stack_ops, list) {
19841984
int res;
19851985

1986+
if (insn->alt_group) {
1987+
WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
1988+
return -1;
1989+
}
1990+
19861991
res = update_cfi_state(insn, &state->cfi, op);
19871992
if (res)
19881993
return res;
@@ -2149,6 +2154,30 @@ static int validate_return(struct symbol *func, struct instruction *insn, struct
21492154
return 0;
21502155
}
21512156

2157+
/*
2158+
* Alternatives should not contain any ORC entries, this in turn means they
2159+
* should not contain any CFI ops, which implies all instructions should have
2160+
* the same same CFI state.
2161+
*
2162+
* It is possible to constuct alternatives that have unreachable holes that go
2163+
* unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2164+
* states which then results in ORC entries, which we just said we didn't want.
2165+
*
2166+
* Avoid them by copying the CFI entry of the first instruction into the whole
2167+
* alternative.
2168+
*/
2169+
static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2170+
{
2171+
struct instruction *first_insn = insn;
2172+
int alt_group = insn->alt_group;
2173+
2174+
sec_for_each_insn_continue(file, insn) {
2175+
if (insn->alt_group != alt_group)
2176+
break;
2177+
insn->cfi = first_insn->cfi;
2178+
}
2179+
}
2180+
21522181
/*
21532182
* Follow the branch starting at the given instruction, and recursively follow
21542183
* any other branches (jumps). Meanwhile, track the frame pointer state at
@@ -2200,7 +2229,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
22002229

22012230
insn->visited |= visited;
22022231

2203-
if (!insn->ignore_alts) {
2232+
if (!insn->ignore_alts && !list_empty(&insn->alts)) {
22042233
bool skip_orig = false;
22052234

22062235
list_for_each_entry(alt, &insn->alts, list) {
@@ -2215,6 +2244,9 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
22152244
}
22162245
}
22172246

2247+
if (insn->alt_group)
2248+
fill_alternative_cfi(file, insn);
2249+
22182250
if (skip_orig)
22192251
return 0;
22202252
}

0 commit comments

Comments
 (0)