Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

objtool: Assume unannotated UD2 instructions are dead ends

Arnd reported some false positive warnings with GCC 7:

drivers/hid/wacom_wac.o: warning: objtool: wacom_bpt3_touch()+0x2a5: stack state mismatch: cfa1=7+8 cfa2=6+16
drivers/iio/adc/vf610_adc.o: warning: objtool: vf610_adc_calculate_rates() falls through to next function vf610_adc_sample_set()
drivers/pwm/pwm-hibvt.o: warning: objtool: hibvt_pwm_get_state() falls through to next function hibvt_pwm_remove()
drivers/pwm/pwm-mediatek.o: warning: objtool: mtk_pwm_config() falls through to next function mtk_pwm_enable()
drivers/spi/spi-bcm2835.o: warning: objtool: .text: unexpected end of section
drivers/spi/spi-bcm2835aux.o: warning: objtool: .text: unexpected end of section
drivers/watchdog/digicolor_wdt.o: warning: objtool: dc_wdt_get_timeleft() falls through to next function dc_wdt_restart()

When GCC 7 detects a potential divide-by-zero condition, it sometimes
inserts a UD2 instruction for the case where the divisor is zero,
instead of letting the hardware trap on the divide instruction.

Objtool doesn't consider UD2 to be fatal unless it's annotated with
unreachable(). So it considers the GCC-generated UD2 to be non-fatal,
and it tries to follow the control flow past the UD2 and gets
confused.

Previously, objtool *did* assume UD2 was always a dead end. That
changed with the following commit:

d1091c7fa3d5 ("objtool: Improve detection of BUG() and other dead ends")

The motivation behind that change was that Peter was planning on using
UD2 for __WARN(), which is *not* a dead end. However, it turns out
that some emulators rely on UD2 being fatal, so he ended up using
'ud0' instead:

9a93848fe787 ("x86/debug: Implement __WARN() using UD0")

For GCC 4.5+, it should be safe to go back to the previous assumption
that UD2 is fatal, even when it's not annotated with unreachable().

But for pre-4.5 versions of GCC, the unreachable() macro isn't
supported, so such cases of UD2 need to be explicitly annotated as
reachable.

Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Fixes: d1091c7fa3d5 ("objtool: Improve detection of BUG() and other dead ends")
Link: http://lkml.kernel.org/r/e57fa9dfede25f79487da8126ee9cdf7b856db65.1501188854.git.jpoimboe@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Josh Poimboeuf and committed by
Ingo Molnar
649ea4d5 a6323757

+94 -26
-16
include/linux/compiler-gcc.h
··· 128 128 #define __always_unused __attribute__((unused)) 129 129 #define __mode(x) __attribute__((mode(x))) 130 130 131 - #ifdef CONFIG_STACK_VALIDATION 132 - #define annotate_unreachable() ({ \ 133 - asm("%c0:\n\t" \ 134 - ".pushsection .discard.unreachable\n\t" \ 135 - ".long %c0b - .\n\t" \ 136 - ".popsection\n\t" : : "i" (__LINE__)); \ 137 - }) 138 - #define ASM_UNREACHABLE \ 139 - "999:\n\t" \ 140 - ".pushsection .discard.unreachable\n\t" \ 141 - ".long 999b - .\n\t" \ 142 - ".popsection\n\t" 143 - #else 144 - #define annotate_unreachable() 145 - #endif 146 - 147 131 /* gcc version specific checks */ 148 132 149 133 #if GCC_VERSION < 30200
+24 -1
include/linux/compiler.h
··· 185 185 #endif 186 186 187 187 /* Unreachable code */ 188 + #ifdef CONFIG_STACK_VALIDATION 189 + #define annotate_reachable() ({ \ 190 + asm("%c0:\n\t" \ 191 + ".pushsection .discard.reachable\n\t" \ 192 + ".long %c0b - .\n\t" \ 193 + ".popsection\n\t" : : "i" (__LINE__)); \ 194 + }) 195 + #define annotate_unreachable() ({ \ 196 + asm("%c0:\n\t" \ 197 + ".pushsection .discard.unreachable\n\t" \ 198 + ".long %c0b - .\n\t" \ 199 + ".popsection\n\t" : : "i" (__LINE__)); \ 200 + }) 201 + #define ASM_UNREACHABLE \ 202 + "999:\n\t" \ 203 + ".pushsection .discard.unreachable\n\t" \ 204 + ".long 999b - .\n\t" \ 205 + ".popsection\n\t" 206 + #else 207 + #define annotate_reachable() 208 + #define annotate_unreachable() 209 + #endif 210 + 188 211 #ifndef ASM_UNREACHABLE 189 212 # define ASM_UNREACHABLE 190 213 #endif 191 214 #ifndef unreachable 192 - # define unreachable() do { } while (1) 215 + # define unreachable() do { annotate_reachable(); do { } while (1); } while (0) 193 216 #endif 194 217 195 218 /*
+3 -2
tools/objtool/arch.h
··· 31 31 #define INSN_RETURN 6 32 32 #define INSN_CONTEXT_SWITCH 7 33 33 #define INSN_STACK 8 34 - #define INSN_NOP 9 35 - #define INSN_OTHER 10 34 + #define INSN_BUG 9 35 + #define INSN_NOP 10 36 + #define INSN_OTHER 11 36 37 #define INSN_LAST INSN_OTHER 37 38 38 39 enum op_dest_type {
+12 -5
tools/objtool/arch/x86/decode.c
··· 382 382 383 383 case 0x0f: 384 384 385 - if (op2 >= 0x80 && op2 <= 0x8f) 385 + if (op2 >= 0x80 && op2 <= 0x8f) { 386 + 386 387 *type = INSN_JUMP_CONDITIONAL; 387 - else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 || 388 - op2 == 0x35) 388 + 389 + } else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 || 390 + op2 == 0x35) { 389 391 390 392 /* sysenter, sysret */ 391 393 *type = INSN_CONTEXT_SWITCH; 392 394 393 - else if (op2 == 0x0d || op2 == 0x1f) 395 + } else if (op2 == 0x0b || op2 == 0xb9) { 396 + 397 + /* ud2 */ 398 + *type = INSN_BUG; 399 + 400 + } else if (op2 == 0x0d || op2 == 0x1f) { 394 401 395 402 /* nopl/nopw */ 396 403 *type = INSN_NOP; 397 404 398 - else if (op2 == 0xa0 || op2 == 0xa8) { 405 + } else if (op2 == 0xa0 || op2 == 0xa8) { 399 406 400 407 /* push fs/gs */ 401 408 *type = INSN_STACK;
+55 -2
tools/objtool/check.c
··· 296 296 } 297 297 298 298 /* 299 - * Find all uses of the unreachable() macro, which are code path dead ends. 299 + * Mark "ud2" instructions and manually annotated dead ends. 300 300 */ 301 301 static int add_dead_ends(struct objtool_file *file) 302 302 { ··· 305 305 struct instruction *insn; 306 306 bool found; 307 307 308 + /* 309 + * By default, "ud2" is a dead end unless otherwise annotated, because 310 + * GCC 7 inserts it for certain divide-by-zero cases. 311 + */ 312 + for_each_insn(file, insn) 313 + if (insn->type == INSN_BUG) 314 + insn->dead_end = true; 315 + 316 + /* 317 + * Check for manually annotated dead ends. 318 + */ 308 319 sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); 309 320 if (!sec) 310 - return 0; 321 + goto reachable; 311 322 312 323 list_for_each_entry(rela, &sec->rela_list, list) { 313 324 if (rela->sym->type != STT_SECTION) { ··· 349 338 } 350 339 351 340 insn->dead_end = true; 341 + } 342 + 343 + reachable: 344 + /* 345 + * These manually annotated reachable checks are needed for GCC 4.4, 346 + * where the Linux unreachable() macro isn't supported. In that case 347 + * GCC doesn't know the "ud2" is fatal, so it generates code as if it's 348 + * not a dead end. 349 + */ 350 + sec = find_section_by_name(file->elf, ".rela.discard.reachable"); 351 + if (!sec) 352 + return 0; 353 + 354 + list_for_each_entry(rela, &sec->rela_list, list) { 355 + if (rela->sym->type != STT_SECTION) { 356 + WARN("unexpected relocation symbol type in %s", sec->name); 357 + return -1; 358 + } 359 + insn = find_insn(file, rela->sym->sec, rela->addend); 360 + if (insn) 361 + insn = list_prev_entry(insn, list); 362 + else if (rela->addend == rela->sym->sec->len) { 363 + found = false; 364 + list_for_each_entry_reverse(insn, &file->insn_list, list) { 365 + if (insn->sec == rela->sym->sec) { 366 + found = true; 367 + break; 368 + } 369 + } 370 + 371 + if (!found) { 372 + WARN("can't find reachable insn at %s+0x%x", 373 + rela->sym->sec->name, rela->addend); 374 + return -1; 375 + } 376 + } else { 377 + WARN("can't find reachable insn at %s+0x%x", 378 + rela->sym->sec->name, rela->addend); 379 + return -1; 380 + } 381 + 382 + insn->dead_end = false; 352 383 } 353 384 354 385 return 0;