1313dd1b6SKees Cook /*
2313dd1b6SKees Cook  * Copyright 2014-2016 by Open Source Security, Inc., Brad Spengler <[email protected]>
3313dd1b6SKees Cook  *                   and PaX Team <[email protected]>
4313dd1b6SKees Cook  * Licensed under the GPL v2
5313dd1b6SKees Cook  *
6313dd1b6SKees Cook  * Note: the choice of the license means that the compilation process is
7313dd1b6SKees Cook  *       NOT 'eligible' as defined by gcc's library exception to the GPL v3,
8313dd1b6SKees Cook  *       but for the kernel it doesn't matter since it doesn't link against
9313dd1b6SKees Cook  *       any of the gcc libraries
10313dd1b6SKees Cook  *
11313dd1b6SKees Cook  * Usage:
12313dd1b6SKees Cook  * $ # for 4.5/4.6/C based 4.7
13313dd1b6SKees Cook  * $ gcc -I`gcc -print-file-name=plugin`/include -I`gcc -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o randomize_layout_plugin.so randomize_layout_plugin.c
14313dd1b6SKees Cook  * $ # for C++ based 4.7/4.8+
15313dd1b6SKees Cook  * $ g++ -I`g++ -print-file-name=plugin`/include -I`g++ -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o randomize_layout_plugin.so randomize_layout_plugin.c
16313dd1b6SKees Cook  * $ gcc -fplugin=./randomize_layout_plugin.so test.c -O2
17313dd1b6SKees Cook  */
18313dd1b6SKees Cook 
19313dd1b6SKees Cook #include "gcc-common.h"
20313dd1b6SKees Cook #include "randomize_layout_seed.h"
21313dd1b6SKees Cook 
22313dd1b6SKees Cook #if BUILDING_GCC_MAJOR < 4 || (BUILDING_GCC_MAJOR == 4 && BUILDING_GCC_MINOR < 7)
23313dd1b6SKees Cook #error "The RANDSTRUCT plugin requires GCC 4.7 or newer."
24313dd1b6SKees Cook #endif
25313dd1b6SKees Cook 
26313dd1b6SKees Cook #define ORIG_TYPE_NAME(node) \
27313dd1b6SKees Cook 	(TYPE_NAME(TYPE_MAIN_VARIANT(node)) != NULL_TREE ? ((const unsigned char *)IDENTIFIER_POINTER(TYPE_NAME(TYPE_MAIN_VARIANT(node)))) : (const unsigned char *)"anonymous")
28313dd1b6SKees Cook 
29313dd1b6SKees Cook #define INFORM(loc, msg, ...)	inform(loc, "randstruct: " msg, ##__VA_ARGS__)
30313dd1b6SKees Cook #define MISMATCH(loc, how, ...)	INFORM(loc, "casting between randomized structure pointer types (" how "): %qT and %qT\n", __VA_ARGS__)
31313dd1b6SKees Cook 
32313dd1b6SKees Cook __visible int plugin_is_GPL_compatible;
33313dd1b6SKees Cook 
34313dd1b6SKees Cook static int performance_mode;
35313dd1b6SKees Cook 
36313dd1b6SKees Cook static struct plugin_info randomize_layout_plugin_info = {
37*d37aa2efSMasahiro Yamada 	.version	= PLUGIN_VERSION,
38313dd1b6SKees Cook 	.help		= "disable\t\t\tdo not activate plugin\n"
39313dd1b6SKees Cook 			  "performance-mode\tenable cacheline-aware layout randomization\n"
40313dd1b6SKees Cook };
41313dd1b6SKees Cook 
42313dd1b6SKees Cook /* from old Linux dcache.h */
43313dd1b6SKees Cook static inline unsigned long
44313dd1b6SKees Cook partial_name_hash(unsigned long c, unsigned long prevhash)
45313dd1b6SKees Cook {
46313dd1b6SKees Cook 	return (prevhash + (c << 4) + (c >> 4)) * 11;
47313dd1b6SKees Cook }
48313dd1b6SKees Cook static inline unsigned int
49313dd1b6SKees Cook name_hash(const unsigned char *name)
50313dd1b6SKees Cook {
51313dd1b6SKees Cook 	unsigned long hash = 0;
52313dd1b6SKees Cook 	unsigned int len = strlen((const char *)name);
53313dd1b6SKees Cook 	while (len--)
54313dd1b6SKees Cook 		hash = partial_name_hash(*name++, hash);
55313dd1b6SKees Cook 	return (unsigned int)hash;
56313dd1b6SKees Cook }
57313dd1b6SKees Cook 
58313dd1b6SKees Cook static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
59313dd1b6SKees Cook {
60313dd1b6SKees Cook 	tree type;
61313dd1b6SKees Cook 
62313dd1b6SKees Cook 	*no_add_attrs = true;
63313dd1b6SKees Cook 	if (TREE_CODE(*node) == FUNCTION_DECL) {
64313dd1b6SKees Cook 		error("%qE attribute does not apply to functions (%qF)", name, *node);
65313dd1b6SKees Cook 		return NULL_TREE;
66313dd1b6SKees Cook 	}
67313dd1b6SKees Cook 
68313dd1b6SKees Cook 	if (TREE_CODE(*node) == PARM_DECL) {
69313dd1b6SKees Cook 		error("%qE attribute does not apply to function parameters (%qD)", name, *node);
70313dd1b6SKees Cook 		return NULL_TREE;
71313dd1b6SKees Cook 	}
72313dd1b6SKees Cook 
73313dd1b6SKees Cook 	if (TREE_CODE(*node) == VAR_DECL) {
74313dd1b6SKees Cook 		error("%qE attribute does not apply to variables (%qD)", name, *node);
75313dd1b6SKees Cook 		return NULL_TREE;
76313dd1b6SKees Cook 	}
77313dd1b6SKees Cook 
78313dd1b6SKees Cook 	if (TYPE_P(*node)) {
79313dd1b6SKees Cook 		type = *node;
80313dd1b6SKees Cook 	} else {
81313dd1b6SKees Cook 		gcc_assert(TREE_CODE(*node) == TYPE_DECL);
82313dd1b6SKees Cook 		type = TREE_TYPE(*node);
83313dd1b6SKees Cook 	}
84313dd1b6SKees Cook 
85313dd1b6SKees Cook 	if (TREE_CODE(type) != RECORD_TYPE) {
86313dd1b6SKees Cook 		error("%qE attribute used on %qT applies to struct types only", name, type);
87313dd1b6SKees Cook 		return NULL_TREE;
88313dd1b6SKees Cook 	}
89313dd1b6SKees Cook 
90313dd1b6SKees Cook 	if (lookup_attribute(IDENTIFIER_POINTER(name), TYPE_ATTRIBUTES(type))) {
91313dd1b6SKees Cook 		error("%qE attribute is already applied to the type %qT", name, type);
92313dd1b6SKees Cook 		return NULL_TREE;
93313dd1b6SKees Cook 	}
94313dd1b6SKees Cook 
95313dd1b6SKees Cook 	*no_add_attrs = false;
96313dd1b6SKees Cook 
97313dd1b6SKees Cook 	return NULL_TREE;
98313dd1b6SKees Cook }
99313dd1b6SKees Cook 
100313dd1b6SKees Cook /* set on complete types that we don't need to inspect further at all */
101313dd1b6SKees Cook static tree handle_randomize_considered_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
102313dd1b6SKees Cook {
103313dd1b6SKees Cook 	*no_add_attrs = false;
104313dd1b6SKees Cook 	return NULL_TREE;
105313dd1b6SKees Cook }
106313dd1b6SKees Cook 
107313dd1b6SKees Cook /*
108313dd1b6SKees Cook  * set on types that we've performed a shuffle on, to prevent re-shuffling
109313dd1b6SKees Cook  * this does not preclude us from inspecting its fields for potential shuffles
110313dd1b6SKees Cook  */
111313dd1b6SKees Cook static tree handle_randomize_performed_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
112313dd1b6SKees Cook {
113313dd1b6SKees Cook 	*no_add_attrs = false;
114313dd1b6SKees Cook 	return NULL_TREE;
115313dd1b6SKees Cook }
116313dd1b6SKees Cook 
117313dd1b6SKees Cook /*
118313dd1b6SKees Cook  * 64bit variant of Bob Jenkins' public domain PRNG
119313dd1b6SKees Cook  * 256 bits of internal state
120313dd1b6SKees Cook  */
121313dd1b6SKees Cook 
122313dd1b6SKees Cook typedef unsigned long long u64;
123313dd1b6SKees Cook 
124313dd1b6SKees Cook typedef struct ranctx { u64 a; u64 b; u64 c; u64 d; } ranctx;
125313dd1b6SKees Cook 
126313dd1b6SKees Cook #define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
127313dd1b6SKees Cook static u64 ranval(ranctx *x) {
128313dd1b6SKees Cook 	u64 e = x->a - rot(x->b, 7);
129313dd1b6SKees Cook 	x->a = x->b ^ rot(x->c, 13);
130313dd1b6SKees Cook 	x->b = x->c + rot(x->d, 37);
131313dd1b6SKees Cook 	x->c = x->d + e;
132313dd1b6SKees Cook 	x->d = e + x->a;
133313dd1b6SKees Cook 	return x->d;
134313dd1b6SKees Cook }
135313dd1b6SKees Cook 
136313dd1b6SKees Cook static void raninit(ranctx *x, u64 *seed) {
137313dd1b6SKees Cook 	int i;
138313dd1b6SKees Cook 
139313dd1b6SKees Cook 	x->a = seed[0];
140313dd1b6SKees Cook 	x->b = seed[1];
141313dd1b6SKees Cook 	x->c = seed[2];
142313dd1b6SKees Cook 	x->d = seed[3];
143313dd1b6SKees Cook 
144313dd1b6SKees Cook 	for (i=0; i < 30; ++i)
145313dd1b6SKees Cook 		(void)ranval(x);
146313dd1b6SKees Cook }
147313dd1b6SKees Cook 
148313dd1b6SKees Cook static u64 shuffle_seed[4];
149313dd1b6SKees Cook 
150313dd1b6SKees Cook struct partition_group {
151313dd1b6SKees Cook 	tree tree_start;
152313dd1b6SKees Cook 	unsigned long start;
153313dd1b6SKees Cook 	unsigned long length;
154313dd1b6SKees Cook };
155313dd1b6SKees Cook 
156313dd1b6SKees Cook static void partition_struct(tree *fields, unsigned long length, struct partition_group *size_groups, unsigned long *num_groups)
157313dd1b6SKees Cook {
158313dd1b6SKees Cook 	unsigned long i;
159313dd1b6SKees Cook 	unsigned long accum_size = 0;
160313dd1b6SKees Cook 	unsigned long accum_length = 0;
161313dd1b6SKees Cook 	unsigned long group_idx = 0;
162313dd1b6SKees Cook 
163313dd1b6SKees Cook 	gcc_assert(length < INT_MAX);
164313dd1b6SKees Cook 
165313dd1b6SKees Cook 	memset(size_groups, 0, sizeof(struct partition_group) * length);
166313dd1b6SKees Cook 
167313dd1b6SKees Cook 	for (i = 0; i < length; i++) {
168313dd1b6SKees Cook 		if (size_groups[group_idx].tree_start == NULL_TREE) {
169313dd1b6SKees Cook 			size_groups[group_idx].tree_start = fields[i];
170313dd1b6SKees Cook 			size_groups[group_idx].start = i;
171313dd1b6SKees Cook 			accum_length = 0;
172313dd1b6SKees Cook 			accum_size = 0;
173313dd1b6SKees Cook 		}
174313dd1b6SKees Cook 		accum_size += (unsigned long)int_size_in_bytes(TREE_TYPE(fields[i]));
175313dd1b6SKees Cook 		accum_length++;
176313dd1b6SKees Cook 		if (accum_size >= 64) {
177313dd1b6SKees Cook 			size_groups[group_idx].length = accum_length;
178313dd1b6SKees Cook 			accum_length = 0;
179313dd1b6SKees Cook 			group_idx++;
180313dd1b6SKees Cook 		}
181313dd1b6SKees Cook 	}
182313dd1b6SKees Cook 
183313dd1b6SKees Cook 	if (size_groups[group_idx].tree_start != NULL_TREE &&
184313dd1b6SKees Cook 	    !size_groups[group_idx].length) {
185313dd1b6SKees Cook 		size_groups[group_idx].length = accum_length;
186313dd1b6SKees Cook 		group_idx++;
187313dd1b6SKees Cook 	}
188313dd1b6SKees Cook 
189313dd1b6SKees Cook 	*num_groups = group_idx;
190313dd1b6SKees Cook }
191313dd1b6SKees Cook 
192313dd1b6SKees Cook static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
193313dd1b6SKees Cook {
194313dd1b6SKees Cook 	unsigned long i, x;
195313dd1b6SKees Cook 	struct partition_group size_group[length];
196313dd1b6SKees Cook 	unsigned long num_groups = 0;
197313dd1b6SKees Cook 	unsigned long randnum;
198313dd1b6SKees Cook 
199313dd1b6SKees Cook 	partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
200313dd1b6SKees Cook 	for (i = num_groups - 1; i > 0; i--) {
201313dd1b6SKees Cook 		struct partition_group tmp;
202313dd1b6SKees Cook 		randnum = ranval(prng_state) % (i + 1);
203313dd1b6SKees Cook 		tmp = size_group[i];
204313dd1b6SKees Cook 		size_group[i] = size_group[randnum];
205313dd1b6SKees Cook 		size_group[randnum] = tmp;
206313dd1b6SKees Cook 	}
207313dd1b6SKees Cook 
208313dd1b6SKees Cook 	for (x = 0; x < num_groups; x++) {
209313dd1b6SKees Cook 		for (i = size_group[x].start + size_group[x].length - 1; i > size_group[x].start; i--) {
210313dd1b6SKees Cook 			tree tmp;
211313dd1b6SKees Cook 			if (DECL_BIT_FIELD_TYPE(newtree[i]))
212313dd1b6SKees Cook 				continue;
213313dd1b6SKees Cook 			randnum = ranval(prng_state) % (i + 1);
214313dd1b6SKees Cook 			// we could handle this case differently if desired
215313dd1b6SKees Cook 			if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
216313dd1b6SKees Cook 				continue;
217313dd1b6SKees Cook 			tmp = newtree[i];
218313dd1b6SKees Cook 			newtree[i] = newtree[randnum];
219313dd1b6SKees Cook 			newtree[randnum] = tmp;
220313dd1b6SKees Cook 		}
221313dd1b6SKees Cook 	}
222313dd1b6SKees Cook }
223313dd1b6SKees Cook 
224313dd1b6SKees Cook static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
225313dd1b6SKees Cook {
226313dd1b6SKees Cook 	unsigned long i, randnum;
227313dd1b6SKees Cook 
228313dd1b6SKees Cook 	for (i = length - 1; i > 0; i--) {
229313dd1b6SKees Cook 		tree tmp;
230313dd1b6SKees Cook 		randnum = ranval(prng_state) % (i + 1);
231313dd1b6SKees Cook 		tmp = newtree[i];
232313dd1b6SKees Cook 		newtree[i] = newtree[randnum];
233313dd1b6SKees Cook 		newtree[randnum] = tmp;
234313dd1b6SKees Cook 	}
235313dd1b6SKees Cook }
236313dd1b6SKees Cook 
237313dd1b6SKees Cook /* modern in-place Fisher-Yates shuffle */
238313dd1b6SKees Cook static void shuffle(const_tree type, tree *newtree, unsigned long length)
239313dd1b6SKees Cook {
240313dd1b6SKees Cook 	unsigned long i;
241313dd1b6SKees Cook 	u64 seed[4];
242313dd1b6SKees Cook 	ranctx prng_state;
243313dd1b6SKees Cook 	const unsigned char *structname;
244313dd1b6SKees Cook 
245313dd1b6SKees Cook 	if (length == 0)
246313dd1b6SKees Cook 		return;
247313dd1b6SKees Cook 
248313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
249313dd1b6SKees Cook 
250313dd1b6SKees Cook 	structname = ORIG_TYPE_NAME(type);
251313dd1b6SKees Cook 
252313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
253313dd1b6SKees Cook 	fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type);
254313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
255313dd1b6SKees Cook 	debug_tree((tree)type);
256313dd1b6SKees Cook #endif
257313dd1b6SKees Cook #endif
258313dd1b6SKees Cook 
259313dd1b6SKees Cook 	for (i = 0; i < 4; i++) {
260313dd1b6SKees Cook 		seed[i] = shuffle_seed[i];
261313dd1b6SKees Cook 		seed[i] ^= name_hash(structname);
262313dd1b6SKees Cook 	}
263313dd1b6SKees Cook 
264313dd1b6SKees Cook 	raninit(&prng_state, (u64 *)&seed);
265313dd1b6SKees Cook 
266313dd1b6SKees Cook 	if (performance_mode)
267313dd1b6SKees Cook 		performance_shuffle(newtree, length, &prng_state);
268313dd1b6SKees Cook 	else
269313dd1b6SKees Cook 		full_shuffle(newtree, length, &prng_state);
270313dd1b6SKees Cook }
271313dd1b6SKees Cook 
272313dd1b6SKees Cook static bool is_flexible_array(const_tree field)
273313dd1b6SKees Cook {
274313dd1b6SKees Cook 	const_tree fieldtype;
275313dd1b6SKees Cook 	const_tree typesize;
276313dd1b6SKees Cook 	const_tree elemtype;
277313dd1b6SKees Cook 	const_tree elemsize;
278313dd1b6SKees Cook 
279313dd1b6SKees Cook 	fieldtype = TREE_TYPE(field);
280313dd1b6SKees Cook 	typesize = TYPE_SIZE(fieldtype);
281313dd1b6SKees Cook 
282313dd1b6SKees Cook 	if (TREE_CODE(fieldtype) != ARRAY_TYPE)
283313dd1b6SKees Cook 		return false;
284313dd1b6SKees Cook 
285313dd1b6SKees Cook 	elemtype = TREE_TYPE(fieldtype);
286313dd1b6SKees Cook 	elemsize = TYPE_SIZE(elemtype);
287313dd1b6SKees Cook 
288313dd1b6SKees Cook 	/* size of type is represented in bits */
289313dd1b6SKees Cook 
290313dd1b6SKees Cook 	if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
291313dd1b6SKees Cook 	    TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
292313dd1b6SKees Cook 		return true;
293313dd1b6SKees Cook 
294313dd1b6SKees Cook 	if (typesize != NULL_TREE &&
295313dd1b6SKees Cook 	    (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
296313dd1b6SKees Cook 	     tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
297313dd1b6SKees Cook 		return true;
298313dd1b6SKees Cook 
299313dd1b6SKees Cook 	return false;
300313dd1b6SKees Cook }
301313dd1b6SKees Cook 
302313dd1b6SKees Cook static int relayout_struct(tree type)
303313dd1b6SKees Cook {
304313dd1b6SKees Cook 	unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type));
305313dd1b6SKees Cook 	unsigned long shuffle_length = num_fields;
306313dd1b6SKees Cook 	tree field;
307313dd1b6SKees Cook 	tree newtree[num_fields];
308313dd1b6SKees Cook 	unsigned long i;
309313dd1b6SKees Cook 	tree list;
310313dd1b6SKees Cook 	tree variant;
311313dd1b6SKees Cook 	tree main_variant;
312313dd1b6SKees Cook 	expanded_location xloc;
313313dd1b6SKees Cook 	bool has_flexarray = false;
314313dd1b6SKees Cook 
315313dd1b6SKees Cook 	if (TYPE_FIELDS(type) == NULL_TREE)
316313dd1b6SKees Cook 		return 0;
317313dd1b6SKees Cook 
318313dd1b6SKees Cook 	if (num_fields < 2)
319313dd1b6SKees Cook 		return 0;
320313dd1b6SKees Cook 
321313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
322313dd1b6SKees Cook 
323313dd1b6SKees Cook 	gcc_assert(num_fields < INT_MAX);
324313dd1b6SKees Cook 
325313dd1b6SKees Cook 	if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) ||
326313dd1b6SKees Cook 	    lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))))
327313dd1b6SKees Cook 		return 0;
328313dd1b6SKees Cook 
329313dd1b6SKees Cook 	/* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */
330313dd1b6SKees Cook 	if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") ||
331313dd1b6SKees Cook 	    !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY"))
332313dd1b6SKees Cook 		return 0;
333313dd1b6SKees Cook 
334313dd1b6SKees Cook 	/* throw out any structs in uapi */
335313dd1b6SKees Cook 	xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type)));
336313dd1b6SKees Cook 
337313dd1b6SKees Cook 	if (strstr(xloc.file, "/uapi/"))
338313dd1b6SKees Cook 		error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type));
339313dd1b6SKees Cook 
340313dd1b6SKees Cook 	for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) {
341313dd1b6SKees Cook 		gcc_assert(TREE_CODE(field) == FIELD_DECL);
342313dd1b6SKees Cook 		newtree[i] = field;
343313dd1b6SKees Cook 	}
344313dd1b6SKees Cook 
345313dd1b6SKees Cook 	/*
346313dd1b6SKees Cook 	 * enforce that we don't randomize the layout of the last
347313dd1b6SKees Cook 	 * element of a struct if it's a 0 or 1-length array
348313dd1b6SKees Cook 	 * or a proper flexible array
349313dd1b6SKees Cook 	 */
350313dd1b6SKees Cook 	if (is_flexible_array(newtree[num_fields - 1])) {
351313dd1b6SKees Cook 		has_flexarray = true;
352313dd1b6SKees Cook 		shuffle_length--;
353313dd1b6SKees Cook 	}
354313dd1b6SKees Cook 
355313dd1b6SKees Cook 	shuffle(type, (tree *)newtree, shuffle_length);
356313dd1b6SKees Cook 
357313dd1b6SKees Cook 	/*
358313dd1b6SKees Cook 	 * set up a bogus anonymous struct field designed to error out on unnamed struct initializers
359313dd1b6SKees Cook 	 * as gcc provides no other way to detect such code
360313dd1b6SKees Cook 	 */
361313dd1b6SKees Cook 	list = make_node(FIELD_DECL);
362313dd1b6SKees Cook 	TREE_CHAIN(list) = newtree[0];
363313dd1b6SKees Cook 	TREE_TYPE(list) = void_type_node;
364313dd1b6SKees Cook 	DECL_SIZE(list) = bitsize_zero_node;
365313dd1b6SKees Cook 	DECL_NONADDRESSABLE_P(list) = 1;
366313dd1b6SKees Cook 	DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node;
367313dd1b6SKees Cook 	DECL_SIZE_UNIT(list) = size_zero_node;
368313dd1b6SKees Cook 	DECL_FIELD_OFFSET(list) = size_zero_node;
369313dd1b6SKees Cook 	DECL_CONTEXT(list) = type;
370313dd1b6SKees Cook 	// to satisfy the constify plugin
371313dd1b6SKees Cook 	TREE_READONLY(list) = 1;
372313dd1b6SKees Cook 
373313dd1b6SKees Cook 	for (i = 0; i < num_fields - 1; i++)
374313dd1b6SKees Cook 		TREE_CHAIN(newtree[i]) = newtree[i+1];
375313dd1b6SKees Cook 	TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
376313dd1b6SKees Cook 
377313dd1b6SKees Cook 	main_variant = TYPE_MAIN_VARIANT(type);
378313dd1b6SKees Cook 	for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
379313dd1b6SKees Cook 		TYPE_FIELDS(variant) = list;
380313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
381313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
382313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
383313dd1b6SKees Cook 		if (has_flexarray)
384313dd1b6SKees Cook 			TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
385313dd1b6SKees Cook 	}
386313dd1b6SKees Cook 
387313dd1b6SKees Cook 	/*
388313dd1b6SKees Cook 	 * force a re-layout of the main variant
389313dd1b6SKees Cook 	 * the TYPE_SIZE for all variants will be recomputed
390313dd1b6SKees Cook 	 * by finalize_type_size()
391313dd1b6SKees Cook 	 */
392313dd1b6SKees Cook 	TYPE_SIZE(main_variant) = NULL_TREE;
393313dd1b6SKees Cook 	layout_type(main_variant);
394313dd1b6SKees Cook 	gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE);
395313dd1b6SKees Cook 
396313dd1b6SKees Cook 	return 1;
397313dd1b6SKees Cook }
398313dd1b6SKees Cook 
399313dd1b6SKees Cook /* from constify plugin */
400313dd1b6SKees Cook static const_tree get_field_type(const_tree field)
401313dd1b6SKees Cook {
402313dd1b6SKees Cook 	return strip_array_types(TREE_TYPE(field));
403313dd1b6SKees Cook }
404313dd1b6SKees Cook 
405313dd1b6SKees Cook /* from constify plugin */
406313dd1b6SKees Cook static bool is_fptr(const_tree fieldtype)
407313dd1b6SKees Cook {
408313dd1b6SKees Cook 	if (TREE_CODE(fieldtype) != POINTER_TYPE)
409313dd1b6SKees Cook 		return false;
410313dd1b6SKees Cook 
411313dd1b6SKees Cook 	return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE;
412313dd1b6SKees Cook }
413313dd1b6SKees Cook 
414313dd1b6SKees Cook /* derived from constify plugin */
415313dd1b6SKees Cook static int is_pure_ops_struct(const_tree node)
416313dd1b6SKees Cook {
417313dd1b6SKees Cook 	const_tree field;
418313dd1b6SKees Cook 
419313dd1b6SKees Cook 	gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE);
420313dd1b6SKees Cook 
421313dd1b6SKees Cook 	for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) {
422313dd1b6SKees Cook 		const_tree fieldtype = get_field_type(field);
423313dd1b6SKees Cook 		enum tree_code code = TREE_CODE(fieldtype);
424313dd1b6SKees Cook 
425313dd1b6SKees Cook 		if (node == fieldtype)
426313dd1b6SKees Cook 			continue;
427313dd1b6SKees Cook 
42860f2c82eSJoonwon Kang 		if (code == RECORD_TYPE || code == UNION_TYPE) {
429313dd1b6SKees Cook 			if (!is_pure_ops_struct(fieldtype))
430313dd1b6SKees Cook 				return 0;
43160f2c82eSJoonwon Kang 			continue;
43260f2c82eSJoonwon Kang 		}
43360f2c82eSJoonwon Kang 
43460f2c82eSJoonwon Kang 		if (!is_fptr(fieldtype))
43560f2c82eSJoonwon Kang 			return 0;
436313dd1b6SKees Cook 	}
437313dd1b6SKees Cook 
438313dd1b6SKees Cook 	return 1;
439313dd1b6SKees Cook }
440313dd1b6SKees Cook 
441313dd1b6SKees Cook static void randomize_type(tree type)
442313dd1b6SKees Cook {
443313dd1b6SKees Cook 	tree variant;
444313dd1b6SKees Cook 
445313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
446313dd1b6SKees Cook 
447313dd1b6SKees Cook 	if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
448313dd1b6SKees Cook 		return;
449313dd1b6SKees Cook 
450313dd1b6SKees Cook 	if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
451313dd1b6SKees Cook 		relayout_struct(type);
452313dd1b6SKees Cook 
453313dd1b6SKees Cook 	for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
454313dd1b6SKees Cook 		TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
455313dd1b6SKees Cook 		TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
456313dd1b6SKees Cook 	}
457313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
458313dd1b6SKees Cook 	fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
459313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
460313dd1b6SKees Cook 	debug_tree(type);
461313dd1b6SKees Cook #endif
462313dd1b6SKees Cook #endif
463313dd1b6SKees Cook }
464313dd1b6SKees Cook 
465313dd1b6SKees Cook static void update_decl_size(tree decl)
466313dd1b6SKees Cook {
467313dd1b6SKees Cook 	tree lastval, lastidx, field, init, type, flexsize;
468313dd1b6SKees Cook 	unsigned HOST_WIDE_INT len;
469313dd1b6SKees Cook 
470313dd1b6SKees Cook 	type = TREE_TYPE(decl);
471313dd1b6SKees Cook 
472313dd1b6SKees Cook 	if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type)))
473313dd1b6SKees Cook 		return;
474313dd1b6SKees Cook 
475313dd1b6SKees Cook 	init = DECL_INITIAL(decl);
476313dd1b6SKees Cook 	if (init == NULL_TREE || init == error_mark_node)
477313dd1b6SKees Cook 		return;
478313dd1b6SKees Cook 
479313dd1b6SKees Cook 	if (TREE_CODE(init) != CONSTRUCTOR)
480313dd1b6SKees Cook 		return;
481313dd1b6SKees Cook 
482313dd1b6SKees Cook 	len = CONSTRUCTOR_NELTS(init);
483313dd1b6SKees Cook         if (!len)
484313dd1b6SKees Cook 		return;
485313dd1b6SKees Cook 
486313dd1b6SKees Cook 	lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value;
487313dd1b6SKees Cook 	lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index;
488313dd1b6SKees Cook 
489313dd1b6SKees Cook 	for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field))
490313dd1b6SKees Cook 		;
491313dd1b6SKees Cook 
492313dd1b6SKees Cook 	if (lastidx != field)
493313dd1b6SKees Cook 		return;
494313dd1b6SKees Cook 
495313dd1b6SKees Cook 	if (TREE_CODE(lastval) != STRING_CST) {
496313dd1b6SKees Cook 		error("Only string constants are supported as initializers "
497313dd1b6SKees Cook 		      "for randomized structures with flexible arrays");
498313dd1b6SKees Cook 		return;
499313dd1b6SKees Cook 	}
500313dd1b6SKees Cook 
501313dd1b6SKees Cook 	flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) *
502313dd1b6SKees Cook 		tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval)))));
503313dd1b6SKees Cook 
504313dd1b6SKees Cook 	DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize);
505313dd1b6SKees Cook 
506313dd1b6SKees Cook 	return;
507313dd1b6SKees Cook }
508313dd1b6SKees Cook 
509313dd1b6SKees Cook 
510313dd1b6SKees Cook static void randomize_layout_finish_decl(void *event_data, void *data)
511313dd1b6SKees Cook {
512313dd1b6SKees Cook 	tree decl = (tree)event_data;
513313dd1b6SKees Cook 	tree type;
514313dd1b6SKees Cook 
515313dd1b6SKees Cook 	if (decl == NULL_TREE || decl == error_mark_node)
516313dd1b6SKees Cook 		return;
517313dd1b6SKees Cook 
518313dd1b6SKees Cook 	type = TREE_TYPE(decl);
519313dd1b6SKees Cook 
520313dd1b6SKees Cook 	if (TREE_CODE(decl) != VAR_DECL)
521313dd1b6SKees Cook 		return;
522313dd1b6SKees Cook 
523313dd1b6SKees Cook 	if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
524313dd1b6SKees Cook 		return;
525313dd1b6SKees Cook 
526313dd1b6SKees Cook 	if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)))
527313dd1b6SKees Cook 		return;
528313dd1b6SKees Cook 
529313dd1b6SKees Cook 	DECL_SIZE(decl) = 0;
530313dd1b6SKees Cook 	DECL_SIZE_UNIT(decl) = 0;
531313dd1b6SKees Cook 	SET_DECL_ALIGN(decl, 0);
532313dd1b6SKees Cook 	SET_DECL_MODE (decl, VOIDmode);
533313dd1b6SKees Cook 	SET_DECL_RTL(decl, 0);
534313dd1b6SKees Cook 	update_decl_size(decl);
535313dd1b6SKees Cook 	layout_decl(decl, 0);
536313dd1b6SKees Cook }
537313dd1b6SKees Cook 
538313dd1b6SKees Cook static void finish_type(void *event_data, void *data)
539313dd1b6SKees Cook {
540313dd1b6SKees Cook 	tree type = (tree)event_data;
541313dd1b6SKees Cook 
542313dd1b6SKees Cook 	if (type == NULL_TREE || type == error_mark_node)
543313dd1b6SKees Cook 		return;
544313dd1b6SKees Cook 
545313dd1b6SKees Cook 	if (TREE_CODE(type) != RECORD_TYPE)
546313dd1b6SKees Cook 		return;
547313dd1b6SKees Cook 
548313dd1b6SKees Cook 	if (TYPE_FIELDS(type) == NULL_TREE)
549313dd1b6SKees Cook 		return;
550313dd1b6SKees Cook 
551313dd1b6SKees Cook 	if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
552313dd1b6SKees Cook 		return;
553313dd1b6SKees Cook 
554313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
555313dd1b6SKees Cook 	fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type));
556313dd1b6SKees Cook #endif
557313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
558313dd1b6SKees Cook 	debug_tree(type);
559313dd1b6SKees Cook #endif
560313dd1b6SKees Cook 	randomize_type(type);
561313dd1b6SKees Cook 
562313dd1b6SKees Cook 	return;
563313dd1b6SKees Cook }
564313dd1b6SKees Cook 
565b8672910SKees Cook static struct attribute_spec randomize_layout_attr = { };
566b8672910SKees Cook static struct attribute_spec no_randomize_layout_attr = { };
567b8672910SKees Cook static struct attribute_spec randomize_considered_attr = { };
568b8672910SKees Cook static struct attribute_spec randomize_performed_attr = { };
569313dd1b6SKees Cook 
570313dd1b6SKees Cook static void register_attributes(void *event_data, void *data)
571313dd1b6SKees Cook {
572b8672910SKees Cook 	randomize_layout_attr.name		= "randomize_layout";
573b8672910SKees Cook 	randomize_layout_attr.type_required	= true;
574b8672910SKees Cook 	randomize_layout_attr.handler		= handle_randomize_layout_attr;
575b8672910SKees Cook 	randomize_layout_attr.affects_type_identity = true;
576b8672910SKees Cook 
577b8672910SKees Cook 	no_randomize_layout_attr.name		= "no_randomize_layout";
578b8672910SKees Cook 	no_randomize_layout_attr.type_required	= true;
579b8672910SKees Cook 	no_randomize_layout_attr.handler	= handle_randomize_layout_attr;
580b8672910SKees Cook 	no_randomize_layout_attr.affects_type_identity = true;
581b8672910SKees Cook 
582b8672910SKees Cook 	randomize_considered_attr.name		= "randomize_considered";
583b8672910SKees Cook 	randomize_considered_attr.type_required	= true;
584b8672910SKees Cook 	randomize_considered_attr.handler	= handle_randomize_considered_attr;
585b8672910SKees Cook 
586b8672910SKees Cook 	randomize_performed_attr.name		= "randomize_performed";
587b8672910SKees Cook 	randomize_performed_attr.type_required	= true;
588b8672910SKees Cook 	randomize_performed_attr.handler	= handle_randomize_performed_attr;
589b8672910SKees Cook 
590313dd1b6SKees Cook 	register_attribute(&randomize_layout_attr);
591313dd1b6SKees Cook 	register_attribute(&no_randomize_layout_attr);
592313dd1b6SKees Cook 	register_attribute(&randomize_considered_attr);
593313dd1b6SKees Cook 	register_attribute(&randomize_performed_attr);
594313dd1b6SKees Cook }
595313dd1b6SKees Cook 
596313dd1b6SKees Cook static void check_bad_casts_in_constructor(tree var, tree init)
597313dd1b6SKees Cook {
598313dd1b6SKees Cook 	unsigned HOST_WIDE_INT idx;
599313dd1b6SKees Cook 	tree field, val;
600313dd1b6SKees Cook 	tree field_type, val_type;
601313dd1b6SKees Cook 
602313dd1b6SKees Cook 	FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) {
603313dd1b6SKees Cook 		if (TREE_CODE(val) == CONSTRUCTOR) {
604313dd1b6SKees Cook 			check_bad_casts_in_constructor(var, val);
605313dd1b6SKees Cook 			continue;
606313dd1b6SKees Cook 		}
607313dd1b6SKees Cook 
608313dd1b6SKees Cook 		/* pipacs' plugin creates franken-arrays that differ from those produced by
609313dd1b6SKees Cook 		   normal code which all have valid 'field' trees. work around this */
610313dd1b6SKees Cook 		if (field == NULL_TREE)
611313dd1b6SKees Cook 			continue;
612313dd1b6SKees Cook 		field_type = TREE_TYPE(field);
613313dd1b6SKees Cook 		val_type = TREE_TYPE(val);
614313dd1b6SKees Cook 
615313dd1b6SKees Cook 		if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE)
616313dd1b6SKees Cook 			continue;
617313dd1b6SKees Cook 
618313dd1b6SKees Cook 		if (field_type == val_type)
619313dd1b6SKees Cook 			continue;
620313dd1b6SKees Cook 
621313dd1b6SKees Cook 		field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type))));
622313dd1b6SKees Cook 		val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type))));
623313dd1b6SKees Cook 
624313dd1b6SKees Cook 		if (field_type == void_type_node)
625313dd1b6SKees Cook 			continue;
626313dd1b6SKees Cook 		if (field_type == val_type)
627313dd1b6SKees Cook 			continue;
628313dd1b6SKees Cook 		if (TREE_CODE(val_type) != RECORD_TYPE)
629313dd1b6SKees Cook 			continue;
630313dd1b6SKees Cook 
631313dd1b6SKees Cook 		if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type)))
632313dd1b6SKees Cook 			continue;
633313dd1b6SKees Cook 		MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type));
634313dd1b6SKees Cook 	}
635313dd1b6SKees Cook }
636313dd1b6SKees Cook 
637313dd1b6SKees Cook /* derived from the constify plugin */
638313dd1b6SKees Cook static void check_global_variables(void *event_data, void *data)
639313dd1b6SKees Cook {
640313dd1b6SKees Cook 	struct varpool_node *node;
641313dd1b6SKees Cook 	tree init;
642313dd1b6SKees Cook 
643313dd1b6SKees Cook 	FOR_EACH_VARIABLE(node) {
644313dd1b6SKees Cook 		tree var = NODE_DECL(node);
645313dd1b6SKees Cook 		init = DECL_INITIAL(var);
646313dd1b6SKees Cook 		if (init == NULL_TREE)
647313dd1b6SKees Cook 			continue;
648313dd1b6SKees Cook 
649313dd1b6SKees Cook 		if (TREE_CODE(init) != CONSTRUCTOR)
650313dd1b6SKees Cook 			continue;
651313dd1b6SKees Cook 
652313dd1b6SKees Cook 		check_bad_casts_in_constructor(var, init);
653313dd1b6SKees Cook 	}
654313dd1b6SKees Cook }
655313dd1b6SKees Cook 
656313dd1b6SKees Cook static bool dominated_by_is_err(const_tree rhs, basic_block bb)
657313dd1b6SKees Cook {
658313dd1b6SKees Cook 	basic_block dom;
659313dd1b6SKees Cook 	gimple dom_stmt;
660313dd1b6SKees Cook 	gimple call_stmt;
661313dd1b6SKees Cook 	const_tree dom_lhs;
662313dd1b6SKees Cook 	const_tree poss_is_err_cond;
663313dd1b6SKees Cook 	const_tree poss_is_err_func;
664313dd1b6SKees Cook 	const_tree is_err_arg;
665313dd1b6SKees Cook 
666313dd1b6SKees Cook 	dom = get_immediate_dominator(CDI_DOMINATORS, bb);
667313dd1b6SKees Cook 	if (!dom)
668313dd1b6SKees Cook 		return false;
669313dd1b6SKees Cook 
670313dd1b6SKees Cook 	dom_stmt = last_stmt(dom);
671313dd1b6SKees Cook 	if (!dom_stmt)
672313dd1b6SKees Cook 		return false;
673313dd1b6SKees Cook 
674313dd1b6SKees Cook 	if (gimple_code(dom_stmt) != GIMPLE_COND)
675313dd1b6SKees Cook 		return false;
676313dd1b6SKees Cook 
677313dd1b6SKees Cook 	if (gimple_cond_code(dom_stmt) != NE_EXPR)
678313dd1b6SKees Cook 		return false;
679313dd1b6SKees Cook 
680313dd1b6SKees Cook 	if (!integer_zerop(gimple_cond_rhs(dom_stmt)))
681313dd1b6SKees Cook 		return false;
682313dd1b6SKees Cook 
683313dd1b6SKees Cook 	poss_is_err_cond = gimple_cond_lhs(dom_stmt);
684313dd1b6SKees Cook 
685313dd1b6SKees Cook 	if (TREE_CODE(poss_is_err_cond) != SSA_NAME)
686313dd1b6SKees Cook 		return false;
687313dd1b6SKees Cook 
688313dd1b6SKees Cook 	call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond);
689313dd1b6SKees Cook 
690313dd1b6SKees Cook 	if (gimple_code(call_stmt) != GIMPLE_CALL)
691313dd1b6SKees Cook 		return false;
692313dd1b6SKees Cook 
693313dd1b6SKees Cook 	dom_lhs = gimple_get_lhs(call_stmt);
694313dd1b6SKees Cook 	poss_is_err_func = gimple_call_fndecl(call_stmt);
695313dd1b6SKees Cook 	if (!poss_is_err_func)
696313dd1b6SKees Cook 		return false;
697313dd1b6SKees Cook 	if (dom_lhs != poss_is_err_cond)
698313dd1b6SKees Cook 		return false;
699313dd1b6SKees Cook 	if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR"))
700313dd1b6SKees Cook 		return false;
701313dd1b6SKees Cook 
702313dd1b6SKees Cook 	is_err_arg = gimple_call_arg(call_stmt, 0);
703313dd1b6SKees Cook 	if (!is_err_arg)
704313dd1b6SKees Cook 		return false;
705313dd1b6SKees Cook 
706313dd1b6SKees Cook 	if (is_err_arg != rhs)
707313dd1b6SKees Cook 		return false;
708313dd1b6SKees Cook 
709313dd1b6SKees Cook 	return true;
710313dd1b6SKees Cook }
711313dd1b6SKees Cook 
712313dd1b6SKees Cook static void handle_local_var_initializers(void)
713313dd1b6SKees Cook {
714313dd1b6SKees Cook 	tree var;
715313dd1b6SKees Cook 	unsigned int i;
716313dd1b6SKees Cook 
717313dd1b6SKees Cook 	FOR_EACH_LOCAL_DECL(cfun, i, var) {
718313dd1b6SKees Cook 		tree init = DECL_INITIAL(var);
719313dd1b6SKees Cook 		if (!init)
720313dd1b6SKees Cook 			continue;
721313dd1b6SKees Cook 		if (TREE_CODE(init) != CONSTRUCTOR)
722313dd1b6SKees Cook 			continue;
723313dd1b6SKees Cook 		check_bad_casts_in_constructor(var, init);
724313dd1b6SKees Cook 	}
725313dd1b6SKees Cook }
726313dd1b6SKees Cook 
727313dd1b6SKees Cook /*
728313dd1b6SKees Cook  * iterate over all statements to find "bad" casts:
729313dd1b6SKees Cook  * those where the address of the start of a structure is cast
730313dd1b6SKees Cook  * to a pointer of a structure of a different type, or a
731313dd1b6SKees Cook  * structure pointer type is cast to a different structure pointer type
732313dd1b6SKees Cook  */
733313dd1b6SKees Cook static unsigned int find_bad_casts_execute(void)
734313dd1b6SKees Cook {
735313dd1b6SKees Cook 	basic_block bb;
736313dd1b6SKees Cook 
737313dd1b6SKees Cook 	handle_local_var_initializers();
738313dd1b6SKees Cook 
739313dd1b6SKees Cook 	FOR_EACH_BB_FN(bb, cfun) {
740313dd1b6SKees Cook 		gimple_stmt_iterator gsi;
741313dd1b6SKees Cook 
742313dd1b6SKees Cook 		for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
743313dd1b6SKees Cook 			gimple stmt;
744313dd1b6SKees Cook 			const_tree lhs;
745313dd1b6SKees Cook 			const_tree lhs_type;
746313dd1b6SKees Cook 			const_tree rhs1;
747313dd1b6SKees Cook 			const_tree rhs_type;
748313dd1b6SKees Cook 			const_tree ptr_lhs_type;
749313dd1b6SKees Cook 			const_tree ptr_rhs_type;
750313dd1b6SKees Cook 			const_tree op0;
751313dd1b6SKees Cook 			const_tree op0_type;
752313dd1b6SKees Cook 			enum tree_code rhs_code;
753313dd1b6SKees Cook 
754313dd1b6SKees Cook 			stmt = gsi_stmt(gsi);
755313dd1b6SKees Cook 
756313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
757313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
758313dd1b6SKees Cook 			debug_gimple_stmt(stmt);
759313dd1b6SKees Cook 			debug_tree(gimple_get_lhs(stmt));
760313dd1b6SKees Cook #endif
761313dd1b6SKees Cook #endif
762313dd1b6SKees Cook 
763313dd1b6SKees Cook 			if (gimple_code(stmt) != GIMPLE_ASSIGN)
764313dd1b6SKees Cook 				continue;
765313dd1b6SKees Cook 
766313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
767313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
768313dd1b6SKees Cook 			debug_tree(gimple_assign_rhs1(stmt));
769313dd1b6SKees Cook #endif
770313dd1b6SKees Cook #endif
771313dd1b6SKees Cook 
772313dd1b6SKees Cook 
773313dd1b6SKees Cook 			rhs_code = gimple_assign_rhs_code(stmt);
774313dd1b6SKees Cook 
775313dd1b6SKees Cook 			if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME)
776313dd1b6SKees Cook 				continue;
777313dd1b6SKees Cook 
778313dd1b6SKees Cook 			lhs = gimple_get_lhs(stmt);
779313dd1b6SKees Cook 			lhs_type = TREE_TYPE(lhs);
780313dd1b6SKees Cook 			rhs1 = gimple_assign_rhs1(stmt);
781313dd1b6SKees Cook 			rhs_type = TREE_TYPE(rhs1);
782313dd1b6SKees Cook 
783313dd1b6SKees Cook 			if (TREE_CODE(rhs_type) != POINTER_TYPE ||
784313dd1b6SKees Cook 			    TREE_CODE(lhs_type) != POINTER_TYPE)
785313dd1b6SKees Cook 				continue;
786313dd1b6SKees Cook 
787313dd1b6SKees Cook 			ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type))));
788313dd1b6SKees Cook 			ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type))));
789313dd1b6SKees Cook 
790313dd1b6SKees Cook 			if (ptr_rhs_type == void_type_node)
791313dd1b6SKees Cook 				continue;
792313dd1b6SKees Cook 
793313dd1b6SKees Cook 			if (ptr_lhs_type == void_type_node)
794313dd1b6SKees Cook 				continue;
795313dd1b6SKees Cook 
796313dd1b6SKees Cook 			if (dominated_by_is_err(rhs1, bb))
797313dd1b6SKees Cook 				continue;
798313dd1b6SKees Cook 
799313dd1b6SKees Cook 			if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) {
800313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
801313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type)))
802313dd1b6SKees Cook #endif
803313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type);
804313dd1b6SKees Cook 				continue;
805313dd1b6SKees Cook 			}
806313dd1b6SKees Cook 
807313dd1b6SKees Cook 			if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type)
808313dd1b6SKees Cook 				continue;
809313dd1b6SKees Cook 
810313dd1b6SKees Cook 			if (rhs_code == ADDR_EXPR) {
811313dd1b6SKees Cook 				op0 = TREE_OPERAND(rhs1, 0);
812313dd1b6SKees Cook 
813313dd1b6SKees Cook 				if (op0 == NULL_TREE)
814313dd1b6SKees Cook 					continue;
815313dd1b6SKees Cook 
816313dd1b6SKees Cook 				if (TREE_CODE(op0) != VAR_DECL)
817313dd1b6SKees Cook 					continue;
818313dd1b6SKees Cook 
819313dd1b6SKees Cook 				op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0))));
820313dd1b6SKees Cook 				if (op0_type == ptr_lhs_type)
821313dd1b6SKees Cook 					continue;
822313dd1b6SKees Cook 
823313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
824313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type)))
825313dd1b6SKees Cook #endif
826313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type);
827313dd1b6SKees Cook 			} else {
828313dd1b6SKees Cook 				const_tree ssa_name_var = SSA_NAME_VAR(rhs1);
829313dd1b6SKees Cook 				/* skip bogus type casts introduced by container_of */
830313dd1b6SKees Cook 				if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) &&
831313dd1b6SKees Cook 				    !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr"))
832313dd1b6SKees Cook 					continue;
833313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
834313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type)))
835313dd1b6SKees Cook #endif
836313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type);
837313dd1b6SKees Cook 			}
838313dd1b6SKees Cook 
839313dd1b6SKees Cook 		}
840313dd1b6SKees Cook 	}
841313dd1b6SKees Cook 	return 0;
842313dd1b6SKees Cook }
843313dd1b6SKees Cook 
844313dd1b6SKees Cook #define PASS_NAME find_bad_casts
845313dd1b6SKees Cook #define NO_GATE
846313dd1b6SKees Cook #define TODO_FLAGS_FINISH TODO_dump_func
847313dd1b6SKees Cook #include "gcc-generate-gimple-pass.h"
848313dd1b6SKees Cook 
849313dd1b6SKees Cook __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
850313dd1b6SKees Cook {
851313dd1b6SKees Cook 	int i;
852313dd1b6SKees Cook 	const char * const plugin_name = plugin_info->base_name;
853313dd1b6SKees Cook 	const int argc = plugin_info->argc;
854313dd1b6SKees Cook 	const struct plugin_argument * const argv = plugin_info->argv;
855313dd1b6SKees Cook 	bool enable = true;
856313dd1b6SKees Cook 	int obtained_seed = 0;
857313dd1b6SKees Cook 	struct register_pass_info find_bad_casts_pass_info;
858313dd1b6SKees Cook 
859313dd1b6SKees Cook 	find_bad_casts_pass_info.pass			= make_find_bad_casts_pass();
860313dd1b6SKees Cook 	find_bad_casts_pass_info.reference_pass_name	= "ssa";
861313dd1b6SKees Cook 	find_bad_casts_pass_info.ref_pass_instance_number	= 1;
862313dd1b6SKees Cook 	find_bad_casts_pass_info.pos_op			= PASS_POS_INSERT_AFTER;
863313dd1b6SKees Cook 
864313dd1b6SKees Cook 	if (!plugin_default_version_check(version, &gcc_version)) {
865313dd1b6SKees Cook 		error(G_("incompatible gcc/plugin versions"));
866313dd1b6SKees Cook 		return 1;
867313dd1b6SKees Cook 	}
868313dd1b6SKees Cook 
869313dd1b6SKees Cook 	if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
870313dd1b6SKees Cook 		inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
871313dd1b6SKees Cook 		enable = false;
872313dd1b6SKees Cook 	}
873313dd1b6SKees Cook 
874313dd1b6SKees Cook 	for (i = 0; i < argc; ++i) {
875313dd1b6SKees Cook 		if (!strcmp(argv[i].key, "disable")) {
876313dd1b6SKees Cook 			enable = false;
877313dd1b6SKees Cook 			continue;
878313dd1b6SKees Cook 		}
879313dd1b6SKees Cook 		if (!strcmp(argv[i].key, "performance-mode")) {
880313dd1b6SKees Cook 			performance_mode = 1;
881313dd1b6SKees Cook 			continue;
882313dd1b6SKees Cook 		}
883313dd1b6SKees Cook 		error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
884313dd1b6SKees Cook 	}
885313dd1b6SKees Cook 
886313dd1b6SKees Cook 	if (strlen(randstruct_seed) != 64) {
887313dd1b6SKees Cook 		error(G_("invalid seed value supplied for %s plugin"), plugin_name);
888313dd1b6SKees Cook 		return 1;
889313dd1b6SKees Cook 	}
890313dd1b6SKees Cook 	obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx",
891313dd1b6SKees Cook 		&shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]);
892313dd1b6SKees Cook 	if (obtained_seed != 4) {
893313dd1b6SKees Cook 		error(G_("Invalid seed supplied for %s plugin"), plugin_name);
894313dd1b6SKees Cook 		return 1;
895313dd1b6SKees Cook 	}
896313dd1b6SKees Cook 
897313dd1b6SKees Cook 	register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info);
898313dd1b6SKees Cook 	if (enable) {
899313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL);
900313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info);
901313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
902313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL);
903313dd1b6SKees Cook 	}
904313dd1b6SKees Cook 	register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
905313dd1b6SKees Cook 
906313dd1b6SKees Cook 	return 0;
907313dd1b6SKees Cook }
908