| 1 |
/* We need this, otherwise the warnings are emitted inside the macros, which |
| 2 |
makes it hard to write the DejaGnu directives. */ |
| 3 |
/* { dg-additional-options " -ftrack-macro-expansion=0" } */ |
| 4 |
|
| 5 |
/* Adapted from code in the Linux kernel, which has this: */ |
| 6 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 7 |
|
| 8 |
#define __noreturn __attribute__ ((__noreturn__)) |
| 9 |
|
| 10 |
void panic(const char *fmt, ...) __noreturn; |
| 11 |
|
| 12 |
int _printk(const char *fmt, ...); |
| 13 |
#define __printk_index_emit(...) do {} while (0) |
| 14 |
#define printk_index_wrap(_p_func, _fmt, ...) \ |
| 15 |
({ \ |
| 16 |
__printk_index_emit(_fmt, NULL, NULL); \ |
| 17 |
_p_func(_fmt, ##__VA_ARGS__); \ |
| 18 |
}) |
| 19 |
#define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__) |
| 20 |
#define barrier_before_unreachable() do { } while (0) |
| 21 |
|
| 22 |
#define BUG() do { \ |
| 23 |
printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ |
| 24 |
barrier_before_unreachable(); \ |
| 25 |
panic("BUG!"); \ |
| 26 |
} while (0) |
| 27 |
|
| 28 |
#define BUG_ON(condition) do { if (condition) BUG(); } while (0) |
| 29 |
|
| 30 |
void __attribute__((tainted_args)) |
| 31 |
test_BUG(int n) |
| 32 |
{ |
| 33 |
if (n > 100) /* { dg-message "use of attacker-controlled value for control flow" } */ |
| 34 |
BUG(); /* { dg-warning "-Wanalyzer-tainted-assertion" "warning" } */ |
| 35 |
/* { dg-message "treating 'panic' as an assertion failure handler due to '__attribute__\\(\\(__noreturn__\\)\\)'" "final event" { target *-*-* } .-1 } */ |
| 36 |
} |
| 37 |
|
| 38 |
void __attribute__((tainted_args)) |
| 39 |
test_BUG_ON(int n) |
| 40 |
{ |
| 41 |
BUG_ON(n > 100); /* { dg-warning "-Wanalyzer-tainted-assertion" "warning" } */ |
| 42 |
/* { dg-message "treating 'panic' as an assertion failure handler due to '__attribute__\\(\\(__noreturn__\\)\\)'" "final event" { target *-*-* } .-1 } */ |
| 43 |
} |
| 44 |
|
| 45 |
int __attribute__((tainted_args)) |
| 46 |
test_switch_BUG_1(int n) |
| 47 |
{ |
| 48 |
switch (n) { /* { dg-message "use of attacker-controlled value for control flow" } */ |
| 49 |
default: |
| 50 |
case 0: |
| 51 |
return 5; |
| 52 |
case 1: |
| 53 |
return 22; |
| 54 |
case 2: |
| 55 |
return -1; |
| 56 |
case 42: |
| 57 |
BUG (); /* { dg-warning "-Wanalyzer-tainted-assertion" } */ |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
int __attribute__((tainted_args)) |
| 62 |
test_switch_BUG(int n) |
| 63 |
{ |
| 64 |
switch (n) { /* { dg-message "use of attacker-controlled value for control flow" } */ |
| 65 |
case 0: |
| 66 |
return 5; |
| 67 |
case 1: |
| 68 |
return 22; |
| 69 |
case 2: |
| 70 |
return -1; |
| 71 |
} |
| 72 |
BUG (); /* { dg-warning "-Wanalyzer-tainted-assertion" } */ |
| 73 |
} |