124 lines · c
1// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcded \2// RUN: -verify -fsyntax-only -Werror %s3 4// NOTE: The current seed (1234567890abcded) is specifically chosen because it5// uncovered a bug in diagnostics. With it the randomization of "t9" places the6// "a" element at the end of the record. When that happens, the clang complains7// about excessive initializers, which is confusing, because there aren't8// excessive initializers. It should instead complain about using a9// non-designated initializer on a raqndomized struct.10 11// Initializing a randomized structure requires a designated initializer,12// otherwise the element ordering will be off. The only exceptions to this rule13// are:14//15// - A structure with only one element, and16// - A structure initialized with "{0}".17//18// These are well-defined situations where the field ordering doesn't affect19// the result.20 21typedef void (*func_ptr)();22 23void foo(void);24void bar(void);25void baz(void);26void gaz(void);27 28struct test {29 func_ptr a;30 func_ptr b;31 func_ptr c;32 func_ptr d;33 func_ptr e;34 func_ptr f;35 func_ptr g;36} __attribute__((randomize_layout));37 38struct test t1 = {}; // This should be fine per WG14 N2900 (in C23) + our extension handling of it in earlier modes39struct test t2 = { 0 }; // This should also be fine per C99 6.7.8p1940struct test t3 = { .f = baz, .b = bar, .g = gaz, .a = foo }; // Okay41struct test t4 = { .a = foo, bar, baz }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}42 43struct other_test {44 func_ptr a;45 func_ptr b[3];46 func_ptr c;47} __attribute__((randomize_layout));48 49struct other_test t5 = { .a = foo, .b[0] = foo }; // Okay50struct other_test t6 = { .a = foo, .b[0] = foo, bar, baz }; // Okay51struct other_test t7 = { .a = foo, .b = { foo, bar, baz } }; // Okay52struct other_test t8 = { baz, bar, gaz, foo }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}53struct other_test t9 = { .a = foo, .b[0] = foo, bar, baz, gaz }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}54 55struct empty_test {56} __attribute__((randomize_layout));57 58struct empty_test t10 = {}; // Okay59 60struct degen_test {61 func_ptr a;62} __attribute__((randomize_layout));63 64struct degen_test t11 = { foo }; // Okay65 66struct static_assert_test {67 int f;68 _Static_assert(sizeof(int) == 4, "oh no!");69} __attribute__((randomize_layout));70 71struct static_assert_test t12 = { 42 }; // Okay72 73struct enum_decl_test {74 enum e { BORK = 42, FORK = 9 } f;75} __attribute__((randomize_layout));76 77struct enum_decl_test t13 = { BORK }; // Okay78 79struct mixed {80 int a;81 short b;82 unsigned c;83 char d;84} __attribute__((randomize_layout));85 86struct mixed t14 = { 7 }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}87struct mixed t15 = { .b = 8 }; // Okay88 89// This should be autodetected as randomized.90struct funcs {91 func_ptr a;92 func_ptr b;93 func_ptr c;94 func_ptr d;95};96 97struct funcs t16 = { .c = foo }; // Okay98struct funcs t17 = { foo }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}99 100// This should be forced off.101struct funcs_unshuffled {102 func_ptr a;103 func_ptr b;104 func_ptr c;105 func_ptr d;106} __attribute__((no_randomize_layout));107 108struct funcs_unshuffled t18 = { .d = foo }; // Okay109struct funcs_unshuffled t19 = { foo }; // Okay110 111// This is still all function pointers.112// https://github.com/llvm/llvm-project/issues/138355113struct funcs_composite {114 func_ptr a;115 func_ptr b;116 struct funcs inner;117 func_ptr c;118 func_ptr d;119};120 121struct funcs_composite t20 = { .a = foo }; // Okay122struct funcs_composite t21 = { .inner.c = foo }; // Okay123struct funcs_composite t22 = { foo }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}124