File: init-randomized-struct.c

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (77 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcded \
// RUN:  -verify -fsyntax-only -Werror %s

// NOTE: The current seed (1234567890abcded) is specifically chosen because it
// uncovered a bug in diagnostics. With it the randomization of "t9" places the
// "a" element at the end of the record. When that happens, the clang complains
// about excessive initializers, which is confusing, because there aren't
// excessive initializers. It should instead complain about using a
// non-designated initializer on a raqndomized struct.

// Initializing a randomized structure requires a designated initializer,
// otherwise the element ordering will be off. The only exceptions to this rule
// are:
//
//    - A structure with only one element, and
//    - A structure initialized with "{0}".
//
// These are well-defined situations where the field ordering doesn't affect
// the result.

typedef void (*func_ptr)();

void foo(void);
void bar(void);
void baz(void);
void gaz(void);

struct test {
  func_ptr a;
  func_ptr b;
  func_ptr c;
  func_ptr d;
  func_ptr e;
  func_ptr f;
  func_ptr g;
} __attribute__((randomize_layout));

struct test t1 = {}; // This should be fine per WG14 N2900 (in C23) + our extension handling of it in earlier modes
struct test t2 = { 0 }; // This should also be fine per C99 6.7.8p19
struct test t3 = { .f = baz, .b = bar, .g = gaz, .a = foo }; // Okay
struct test t4 = { .a = foo, bar, baz }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}

struct other_test {
  func_ptr a;
  func_ptr b[3];
  func_ptr c;
} __attribute__((randomize_layout));

struct other_test t5 = { .a = foo, .b[0] = foo }; // Okay
struct other_test t6 = { .a = foo, .b[0] = foo, bar, baz }; // Okay
struct other_test t7 = { .a = foo, .b = { foo, bar, baz } }; // Okay
struct other_test t8 = { baz, bar, gaz, foo }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}
struct other_test t9 = { .a = foo, .b[0] = foo, bar, baz, gaz }; // expected-error {{a randomized struct can only be initialized with a designated initializer}}

struct empty_test {
} __attribute__((randomize_layout));

struct empty_test t10 = {}; // Okay

struct degen_test {
  func_ptr a;
} __attribute__((randomize_layout));

struct degen_test t11 = { foo }; // Okay

struct static_assert_test {
  int f;
  _Static_assert(sizeof(int) == 4, "oh no!");
} __attribute__((randomize_layout));

struct static_assert_test t12 = { 42 }; // Okay

struct enum_decl_test {
  enum e { BORK = 42, FORK = 9 } f;
} __attribute__((randomize_layout));

struct enum_decl_test t13 = { BORK }; // Okay