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 = {
37d37aa2efSMasahiro 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 {
194*381fdb73SKees Cook 	unsigned long i, x, index;
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);
200*381fdb73SKees Cook 
201*381fdb73SKees Cook 	/* FIXME: this group shuffle is currently a no-op. */
202313dd1b6SKees Cook 	for (i = num_groups - 1; i > 0; i--) {
203313dd1b6SKees Cook 		struct partition_group tmp;
204313dd1b6SKees Cook 		randnum = ranval(prng_state) % (i + 1);
205313dd1b6SKees Cook 		tmp = size_group[i];
206313dd1b6SKees Cook 		size_group[i] = size_group[randnum];
207313dd1b6SKees Cook 		size_group[randnum] = tmp;
208313dd1b6SKees Cook 	}
209313dd1b6SKees Cook 
210313dd1b6SKees Cook 	for (x = 0; x < num_groups; x++) {
211*381fdb73SKees Cook 		for (index = size_group[x].length - 1; index > 0; index--) {
212313dd1b6SKees Cook 			tree tmp;
213*381fdb73SKees Cook 
214*381fdb73SKees Cook 			i = size_group[x].start + index;
215313dd1b6SKees Cook 			if (DECL_BIT_FIELD_TYPE(newtree[i]))
216313dd1b6SKees Cook 				continue;
217*381fdb73SKees Cook 			randnum = ranval(prng_state) % (index + 1);
218*381fdb73SKees Cook 			randnum += size_group[x].start;
219313dd1b6SKees Cook 			// we could handle this case differently if desired
220313dd1b6SKees Cook 			if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
221313dd1b6SKees Cook 				continue;
222313dd1b6SKees Cook 			tmp = newtree[i];
223313dd1b6SKees Cook 			newtree[i] = newtree[randnum];
224313dd1b6SKees Cook 			newtree[randnum] = tmp;
225313dd1b6SKees Cook 		}
226313dd1b6SKees Cook 	}
227313dd1b6SKees Cook }
228313dd1b6SKees Cook 
229313dd1b6SKees Cook static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
230313dd1b6SKees Cook {
231313dd1b6SKees Cook 	unsigned long i, randnum;
232313dd1b6SKees Cook 
233313dd1b6SKees Cook 	for (i = length - 1; i > 0; i--) {
234313dd1b6SKees Cook 		tree tmp;
235313dd1b6SKees Cook 		randnum = ranval(prng_state) % (i + 1);
236313dd1b6SKees Cook 		tmp = newtree[i];
237313dd1b6SKees Cook 		newtree[i] = newtree[randnum];
238313dd1b6SKees Cook 		newtree[randnum] = tmp;
239313dd1b6SKees Cook 	}
240313dd1b6SKees Cook }
241313dd1b6SKees Cook 
242313dd1b6SKees Cook /* modern in-place Fisher-Yates shuffle */
243313dd1b6SKees Cook static void shuffle(const_tree type, tree *newtree, unsigned long length)
244313dd1b6SKees Cook {
245313dd1b6SKees Cook 	unsigned long i;
246313dd1b6SKees Cook 	u64 seed[4];
247313dd1b6SKees Cook 	ranctx prng_state;
248313dd1b6SKees Cook 	const unsigned char *structname;
249313dd1b6SKees Cook 
250313dd1b6SKees Cook 	if (length == 0)
251313dd1b6SKees Cook 		return;
252313dd1b6SKees Cook 
253313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
254313dd1b6SKees Cook 
255313dd1b6SKees Cook 	structname = ORIG_TYPE_NAME(type);
256313dd1b6SKees Cook 
257313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
258313dd1b6SKees Cook 	fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type);
259313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
260313dd1b6SKees Cook 	debug_tree((tree)type);
261313dd1b6SKees Cook #endif
262313dd1b6SKees Cook #endif
263313dd1b6SKees Cook 
264313dd1b6SKees Cook 	for (i = 0; i < 4; i++) {
265313dd1b6SKees Cook 		seed[i] = shuffle_seed[i];
266313dd1b6SKees Cook 		seed[i] ^= name_hash(structname);
267313dd1b6SKees Cook 	}
268313dd1b6SKees Cook 
269313dd1b6SKees Cook 	raninit(&prng_state, (u64 *)&seed);
270313dd1b6SKees Cook 
271313dd1b6SKees Cook 	if (performance_mode)
272313dd1b6SKees Cook 		performance_shuffle(newtree, length, &prng_state);
273313dd1b6SKees Cook 	else
274313dd1b6SKees Cook 		full_shuffle(newtree, length, &prng_state);
275313dd1b6SKees Cook }
276313dd1b6SKees Cook 
277313dd1b6SKees Cook static bool is_flexible_array(const_tree field)
278313dd1b6SKees Cook {
279313dd1b6SKees Cook 	const_tree fieldtype;
280313dd1b6SKees Cook 	const_tree typesize;
281313dd1b6SKees Cook 	const_tree elemtype;
282313dd1b6SKees Cook 	const_tree elemsize;
283313dd1b6SKees Cook 
284313dd1b6SKees Cook 	fieldtype = TREE_TYPE(field);
285313dd1b6SKees Cook 	typesize = TYPE_SIZE(fieldtype);
286313dd1b6SKees Cook 
287313dd1b6SKees Cook 	if (TREE_CODE(fieldtype) != ARRAY_TYPE)
288313dd1b6SKees Cook 		return false;
289313dd1b6SKees Cook 
290313dd1b6SKees Cook 	elemtype = TREE_TYPE(fieldtype);
291313dd1b6SKees Cook 	elemsize = TYPE_SIZE(elemtype);
292313dd1b6SKees Cook 
293313dd1b6SKees Cook 	/* size of type is represented in bits */
294313dd1b6SKees Cook 
295313dd1b6SKees Cook 	if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
296313dd1b6SKees Cook 	    TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
297313dd1b6SKees Cook 		return true;
298313dd1b6SKees Cook 
299313dd1b6SKees Cook 	if (typesize != NULL_TREE &&
300313dd1b6SKees Cook 	    (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
301313dd1b6SKees Cook 	     tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
302313dd1b6SKees Cook 		return true;
303313dd1b6SKees Cook 
304313dd1b6SKees Cook 	return false;
305313dd1b6SKees Cook }
306313dd1b6SKees Cook 
307313dd1b6SKees Cook static int relayout_struct(tree type)
308313dd1b6SKees Cook {
309313dd1b6SKees Cook 	unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type));
310313dd1b6SKees Cook 	unsigned long shuffle_length = num_fields;
311313dd1b6SKees Cook 	tree field;
312313dd1b6SKees Cook 	tree newtree[num_fields];
313313dd1b6SKees Cook 	unsigned long i;
314313dd1b6SKees Cook 	tree list;
315313dd1b6SKees Cook 	tree variant;
316313dd1b6SKees Cook 	tree main_variant;
317313dd1b6SKees Cook 	expanded_location xloc;
318313dd1b6SKees Cook 	bool has_flexarray = false;
319313dd1b6SKees Cook 
320313dd1b6SKees Cook 	if (TYPE_FIELDS(type) == NULL_TREE)
321313dd1b6SKees Cook 		return 0;
322313dd1b6SKees Cook 
323313dd1b6SKees Cook 	if (num_fields < 2)
324313dd1b6SKees Cook 		return 0;
325313dd1b6SKees Cook 
326313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
327313dd1b6SKees Cook 
328313dd1b6SKees Cook 	gcc_assert(num_fields < INT_MAX);
329313dd1b6SKees Cook 
330313dd1b6SKees Cook 	if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) ||
331313dd1b6SKees Cook 	    lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))))
332313dd1b6SKees Cook 		return 0;
333313dd1b6SKees Cook 
334313dd1b6SKees Cook 	/* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */
335313dd1b6SKees Cook 	if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") ||
336313dd1b6SKees Cook 	    !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY"))
337313dd1b6SKees Cook 		return 0;
338313dd1b6SKees Cook 
339313dd1b6SKees Cook 	/* throw out any structs in uapi */
340313dd1b6SKees Cook 	xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type)));
341313dd1b6SKees Cook 
342313dd1b6SKees Cook 	if (strstr(xloc.file, "/uapi/"))
343313dd1b6SKees Cook 		error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type));
344313dd1b6SKees Cook 
345313dd1b6SKees Cook 	for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) {
346313dd1b6SKees Cook 		gcc_assert(TREE_CODE(field) == FIELD_DECL);
347313dd1b6SKees Cook 		newtree[i] = field;
348313dd1b6SKees Cook 	}
349313dd1b6SKees Cook 
350313dd1b6SKees Cook 	/*
351313dd1b6SKees Cook 	 * enforce that we don't randomize the layout of the last
352313dd1b6SKees Cook 	 * element of a struct if it's a 0 or 1-length array
353313dd1b6SKees Cook 	 * or a proper flexible array
354313dd1b6SKees Cook 	 */
355313dd1b6SKees Cook 	if (is_flexible_array(newtree[num_fields - 1])) {
356313dd1b6SKees Cook 		has_flexarray = true;
357313dd1b6SKees Cook 		shuffle_length--;
358313dd1b6SKees Cook 	}
359313dd1b6SKees Cook 
360313dd1b6SKees Cook 	shuffle(type, (tree *)newtree, shuffle_length);
361313dd1b6SKees Cook 
362313dd1b6SKees Cook 	/*
363313dd1b6SKees Cook 	 * set up a bogus anonymous struct field designed to error out on unnamed struct initializers
364313dd1b6SKees Cook 	 * as gcc provides no other way to detect such code
365313dd1b6SKees Cook 	 */
366313dd1b6SKees Cook 	list = make_node(FIELD_DECL);
367313dd1b6SKees Cook 	TREE_CHAIN(list) = newtree[0];
368313dd1b6SKees Cook 	TREE_TYPE(list) = void_type_node;
369313dd1b6SKees Cook 	DECL_SIZE(list) = bitsize_zero_node;
370313dd1b6SKees Cook 	DECL_NONADDRESSABLE_P(list) = 1;
371313dd1b6SKees Cook 	DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node;
372313dd1b6SKees Cook 	DECL_SIZE_UNIT(list) = size_zero_node;
373313dd1b6SKees Cook 	DECL_FIELD_OFFSET(list) = size_zero_node;
374313dd1b6SKees Cook 	DECL_CONTEXT(list) = type;
375313dd1b6SKees Cook 	// to satisfy the constify plugin
376313dd1b6SKees Cook 	TREE_READONLY(list) = 1;
377313dd1b6SKees Cook 
378313dd1b6SKees Cook 	for (i = 0; i < num_fields - 1; i++)
379313dd1b6SKees Cook 		TREE_CHAIN(newtree[i]) = newtree[i+1];
380313dd1b6SKees Cook 	TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
381313dd1b6SKees Cook 
382313dd1b6SKees Cook 	main_variant = TYPE_MAIN_VARIANT(type);
383313dd1b6SKees Cook 	for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
384313dd1b6SKees Cook 		TYPE_FIELDS(variant) = list;
385313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
386313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
387313dd1b6SKees Cook 		TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
388313dd1b6SKees Cook 		if (has_flexarray)
389313dd1b6SKees Cook 			TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
390313dd1b6SKees Cook 	}
391313dd1b6SKees Cook 
392313dd1b6SKees Cook 	/*
393313dd1b6SKees Cook 	 * force a re-layout of the main variant
394313dd1b6SKees Cook 	 * the TYPE_SIZE for all variants will be recomputed
395313dd1b6SKees Cook 	 * by finalize_type_size()
396313dd1b6SKees Cook 	 */
397313dd1b6SKees Cook 	TYPE_SIZE(main_variant) = NULL_TREE;
398313dd1b6SKees Cook 	layout_type(main_variant);
399313dd1b6SKees Cook 	gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE);
400313dd1b6SKees Cook 
401313dd1b6SKees Cook 	return 1;
402313dd1b6SKees Cook }
403313dd1b6SKees Cook 
404313dd1b6SKees Cook /* from constify plugin */
405313dd1b6SKees Cook static const_tree get_field_type(const_tree field)
406313dd1b6SKees Cook {
407313dd1b6SKees Cook 	return strip_array_types(TREE_TYPE(field));
408313dd1b6SKees Cook }
409313dd1b6SKees Cook 
410313dd1b6SKees Cook /* from constify plugin */
411313dd1b6SKees Cook static bool is_fptr(const_tree fieldtype)
412313dd1b6SKees Cook {
413313dd1b6SKees Cook 	if (TREE_CODE(fieldtype) != POINTER_TYPE)
414313dd1b6SKees Cook 		return false;
415313dd1b6SKees Cook 
416313dd1b6SKees Cook 	return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE;
417313dd1b6SKees Cook }
418313dd1b6SKees Cook 
419313dd1b6SKees Cook /* derived from constify plugin */
420313dd1b6SKees Cook static int is_pure_ops_struct(const_tree node)
421313dd1b6SKees Cook {
422313dd1b6SKees Cook 	const_tree field;
423313dd1b6SKees Cook 
424313dd1b6SKees Cook 	gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE);
425313dd1b6SKees Cook 
426313dd1b6SKees Cook 	for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) {
427313dd1b6SKees Cook 		const_tree fieldtype = get_field_type(field);
428313dd1b6SKees Cook 		enum tree_code code = TREE_CODE(fieldtype);
429313dd1b6SKees Cook 
430313dd1b6SKees Cook 		if (node == fieldtype)
431313dd1b6SKees Cook 			continue;
432313dd1b6SKees Cook 
43360f2c82eSJoonwon Kang 		if (code == RECORD_TYPE || code == UNION_TYPE) {
434313dd1b6SKees Cook 			if (!is_pure_ops_struct(fieldtype))
435313dd1b6SKees Cook 				return 0;
43660f2c82eSJoonwon Kang 			continue;
43760f2c82eSJoonwon Kang 		}
43860f2c82eSJoonwon Kang 
43960f2c82eSJoonwon Kang 		if (!is_fptr(fieldtype))
44060f2c82eSJoonwon Kang 			return 0;
441313dd1b6SKees Cook 	}
442313dd1b6SKees Cook 
443313dd1b6SKees Cook 	return 1;
444313dd1b6SKees Cook }
445313dd1b6SKees Cook 
446313dd1b6SKees Cook static void randomize_type(tree type)
447313dd1b6SKees Cook {
448313dd1b6SKees Cook 	tree variant;
449313dd1b6SKees Cook 
450313dd1b6SKees Cook 	gcc_assert(TREE_CODE(type) == RECORD_TYPE);
451313dd1b6SKees Cook 
452313dd1b6SKees Cook 	if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
453313dd1b6SKees Cook 		return;
454313dd1b6SKees Cook 
455313dd1b6SKees Cook 	if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
456313dd1b6SKees Cook 		relayout_struct(type);
457313dd1b6SKees Cook 
458313dd1b6SKees Cook 	for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
459313dd1b6SKees Cook 		TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
460313dd1b6SKees Cook 		TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
461313dd1b6SKees Cook 	}
462313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
463313dd1b6SKees Cook 	fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
464313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
465313dd1b6SKees Cook 	debug_tree(type);
466313dd1b6SKees Cook #endif
467313dd1b6SKees Cook #endif
468313dd1b6SKees Cook }
469313dd1b6SKees Cook 
470313dd1b6SKees Cook static void update_decl_size(tree decl)
471313dd1b6SKees Cook {
472313dd1b6SKees Cook 	tree lastval, lastidx, field, init, type, flexsize;
473313dd1b6SKees Cook 	unsigned HOST_WIDE_INT len;
474313dd1b6SKees Cook 
475313dd1b6SKees Cook 	type = TREE_TYPE(decl);
476313dd1b6SKees Cook 
477313dd1b6SKees Cook 	if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type)))
478313dd1b6SKees Cook 		return;
479313dd1b6SKees Cook 
480313dd1b6SKees Cook 	init = DECL_INITIAL(decl);
481313dd1b6SKees Cook 	if (init == NULL_TREE || init == error_mark_node)
482313dd1b6SKees Cook 		return;
483313dd1b6SKees Cook 
484313dd1b6SKees Cook 	if (TREE_CODE(init) != CONSTRUCTOR)
485313dd1b6SKees Cook 		return;
486313dd1b6SKees Cook 
487313dd1b6SKees Cook 	len = CONSTRUCTOR_NELTS(init);
488313dd1b6SKees Cook         if (!len)
489313dd1b6SKees Cook 		return;
490313dd1b6SKees Cook 
491313dd1b6SKees Cook 	lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value;
492313dd1b6SKees Cook 	lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index;
493313dd1b6SKees Cook 
494313dd1b6SKees Cook 	for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field))
495313dd1b6SKees Cook 		;
496313dd1b6SKees Cook 
497313dd1b6SKees Cook 	if (lastidx != field)
498313dd1b6SKees Cook 		return;
499313dd1b6SKees Cook 
500313dd1b6SKees Cook 	if (TREE_CODE(lastval) != STRING_CST) {
501313dd1b6SKees Cook 		error("Only string constants are supported as initializers "
502313dd1b6SKees Cook 		      "for randomized structures with flexible arrays");
503313dd1b6SKees Cook 		return;
504313dd1b6SKees Cook 	}
505313dd1b6SKees Cook 
506313dd1b6SKees Cook 	flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) *
507313dd1b6SKees Cook 		tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval)))));
508313dd1b6SKees Cook 
509313dd1b6SKees Cook 	DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize);
510313dd1b6SKees Cook 
511313dd1b6SKees Cook 	return;
512313dd1b6SKees Cook }
513313dd1b6SKees Cook 
514313dd1b6SKees Cook 
515313dd1b6SKees Cook static void randomize_layout_finish_decl(void *event_data, void *data)
516313dd1b6SKees Cook {
517313dd1b6SKees Cook 	tree decl = (tree)event_data;
518313dd1b6SKees Cook 	tree type;
519313dd1b6SKees Cook 
520313dd1b6SKees Cook 	if (decl == NULL_TREE || decl == error_mark_node)
521313dd1b6SKees Cook 		return;
522313dd1b6SKees Cook 
523313dd1b6SKees Cook 	type = TREE_TYPE(decl);
524313dd1b6SKees Cook 
525313dd1b6SKees Cook 	if (TREE_CODE(decl) != VAR_DECL)
526313dd1b6SKees Cook 		return;
527313dd1b6SKees Cook 
528313dd1b6SKees Cook 	if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
529313dd1b6SKees Cook 		return;
530313dd1b6SKees Cook 
531313dd1b6SKees Cook 	if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)))
532313dd1b6SKees Cook 		return;
533313dd1b6SKees Cook 
534313dd1b6SKees Cook 	DECL_SIZE(decl) = 0;
535313dd1b6SKees Cook 	DECL_SIZE_UNIT(decl) = 0;
536313dd1b6SKees Cook 	SET_DECL_ALIGN(decl, 0);
537313dd1b6SKees Cook 	SET_DECL_MODE (decl, VOIDmode);
538313dd1b6SKees Cook 	SET_DECL_RTL(decl, 0);
539313dd1b6SKees Cook 	update_decl_size(decl);
540313dd1b6SKees Cook 	layout_decl(decl, 0);
541313dd1b6SKees Cook }
542313dd1b6SKees Cook 
543313dd1b6SKees Cook static void finish_type(void *event_data, void *data)
544313dd1b6SKees Cook {
545313dd1b6SKees Cook 	tree type = (tree)event_data;
546313dd1b6SKees Cook 
547313dd1b6SKees Cook 	if (type == NULL_TREE || type == error_mark_node)
548313dd1b6SKees Cook 		return;
549313dd1b6SKees Cook 
550313dd1b6SKees Cook 	if (TREE_CODE(type) != RECORD_TYPE)
551313dd1b6SKees Cook 		return;
552313dd1b6SKees Cook 
553313dd1b6SKees Cook 	if (TYPE_FIELDS(type) == NULL_TREE)
554313dd1b6SKees Cook 		return;
555313dd1b6SKees Cook 
556313dd1b6SKees Cook 	if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
557313dd1b6SKees Cook 		return;
558313dd1b6SKees Cook 
559313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
560313dd1b6SKees Cook 	fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type));
561313dd1b6SKees Cook #endif
562313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
563313dd1b6SKees Cook 	debug_tree(type);
564313dd1b6SKees Cook #endif
565313dd1b6SKees Cook 	randomize_type(type);
566313dd1b6SKees Cook 
567313dd1b6SKees Cook 	return;
568313dd1b6SKees Cook }
569313dd1b6SKees Cook 
570b8672910SKees Cook static struct attribute_spec randomize_layout_attr = { };
571b8672910SKees Cook static struct attribute_spec no_randomize_layout_attr = { };
572b8672910SKees Cook static struct attribute_spec randomize_considered_attr = { };
573b8672910SKees Cook static struct attribute_spec randomize_performed_attr = { };
574313dd1b6SKees Cook 
575313dd1b6SKees Cook static void register_attributes(void *event_data, void *data)
576313dd1b6SKees Cook {
577b8672910SKees Cook 	randomize_layout_attr.name		= "randomize_layout";
578b8672910SKees Cook 	randomize_layout_attr.type_required	= true;
579b8672910SKees Cook 	randomize_layout_attr.handler		= handle_randomize_layout_attr;
580b8672910SKees Cook 	randomize_layout_attr.affects_type_identity = true;
581b8672910SKees Cook 
582b8672910SKees Cook 	no_randomize_layout_attr.name		= "no_randomize_layout";
583b8672910SKees Cook 	no_randomize_layout_attr.type_required	= true;
584b8672910SKees Cook 	no_randomize_layout_attr.handler	= handle_randomize_layout_attr;
585b8672910SKees Cook 	no_randomize_layout_attr.affects_type_identity = true;
586b8672910SKees Cook 
587b8672910SKees Cook 	randomize_considered_attr.name		= "randomize_considered";
588b8672910SKees Cook 	randomize_considered_attr.type_required	= true;
589b8672910SKees Cook 	randomize_considered_attr.handler	= handle_randomize_considered_attr;
590b8672910SKees Cook 
591b8672910SKees Cook 	randomize_performed_attr.name		= "randomize_performed";
592b8672910SKees Cook 	randomize_performed_attr.type_required	= true;
593b8672910SKees Cook 	randomize_performed_attr.handler	= handle_randomize_performed_attr;
594b8672910SKees Cook 
595313dd1b6SKees Cook 	register_attribute(&randomize_layout_attr);
596313dd1b6SKees Cook 	register_attribute(&no_randomize_layout_attr);
597313dd1b6SKees Cook 	register_attribute(&randomize_considered_attr);
598313dd1b6SKees Cook 	register_attribute(&randomize_performed_attr);
599313dd1b6SKees Cook }
600313dd1b6SKees Cook 
601313dd1b6SKees Cook static void check_bad_casts_in_constructor(tree var, tree init)
602313dd1b6SKees Cook {
603313dd1b6SKees Cook 	unsigned HOST_WIDE_INT idx;
604313dd1b6SKees Cook 	tree field, val;
605313dd1b6SKees Cook 	tree field_type, val_type;
606313dd1b6SKees Cook 
607313dd1b6SKees Cook 	FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) {
608313dd1b6SKees Cook 		if (TREE_CODE(val) == CONSTRUCTOR) {
609313dd1b6SKees Cook 			check_bad_casts_in_constructor(var, val);
610313dd1b6SKees Cook 			continue;
611313dd1b6SKees Cook 		}
612313dd1b6SKees Cook 
613313dd1b6SKees Cook 		/* pipacs' plugin creates franken-arrays that differ from those produced by
614313dd1b6SKees Cook 		   normal code which all have valid 'field' trees. work around this */
615313dd1b6SKees Cook 		if (field == NULL_TREE)
616313dd1b6SKees Cook 			continue;
617313dd1b6SKees Cook 		field_type = TREE_TYPE(field);
618313dd1b6SKees Cook 		val_type = TREE_TYPE(val);
619313dd1b6SKees Cook 
620313dd1b6SKees Cook 		if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE)
621313dd1b6SKees Cook 			continue;
622313dd1b6SKees Cook 
623313dd1b6SKees Cook 		if (field_type == val_type)
624313dd1b6SKees Cook 			continue;
625313dd1b6SKees Cook 
626313dd1b6SKees Cook 		field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type))));
627313dd1b6SKees Cook 		val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type))));
628313dd1b6SKees Cook 
629313dd1b6SKees Cook 		if (field_type == void_type_node)
630313dd1b6SKees Cook 			continue;
631313dd1b6SKees Cook 		if (field_type == val_type)
632313dd1b6SKees Cook 			continue;
633313dd1b6SKees Cook 		if (TREE_CODE(val_type) != RECORD_TYPE)
634313dd1b6SKees Cook 			continue;
635313dd1b6SKees Cook 
636313dd1b6SKees Cook 		if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type)))
637313dd1b6SKees Cook 			continue;
638313dd1b6SKees Cook 		MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type));
639313dd1b6SKees Cook 	}
640313dd1b6SKees Cook }
641313dd1b6SKees Cook 
642313dd1b6SKees Cook /* derived from the constify plugin */
643313dd1b6SKees Cook static void check_global_variables(void *event_data, void *data)
644313dd1b6SKees Cook {
645313dd1b6SKees Cook 	struct varpool_node *node;
646313dd1b6SKees Cook 	tree init;
647313dd1b6SKees Cook 
648313dd1b6SKees Cook 	FOR_EACH_VARIABLE(node) {
649313dd1b6SKees Cook 		tree var = NODE_DECL(node);
650313dd1b6SKees Cook 		init = DECL_INITIAL(var);
651313dd1b6SKees Cook 		if (init == NULL_TREE)
652313dd1b6SKees Cook 			continue;
653313dd1b6SKees Cook 
654313dd1b6SKees Cook 		if (TREE_CODE(init) != CONSTRUCTOR)
655313dd1b6SKees Cook 			continue;
656313dd1b6SKees Cook 
657313dd1b6SKees Cook 		check_bad_casts_in_constructor(var, init);
658313dd1b6SKees Cook 	}
659313dd1b6SKees Cook }
660313dd1b6SKees Cook 
661313dd1b6SKees Cook static bool dominated_by_is_err(const_tree rhs, basic_block bb)
662313dd1b6SKees Cook {
663313dd1b6SKees Cook 	basic_block dom;
664313dd1b6SKees Cook 	gimple dom_stmt;
665313dd1b6SKees Cook 	gimple call_stmt;
666313dd1b6SKees Cook 	const_tree dom_lhs;
667313dd1b6SKees Cook 	const_tree poss_is_err_cond;
668313dd1b6SKees Cook 	const_tree poss_is_err_func;
669313dd1b6SKees Cook 	const_tree is_err_arg;
670313dd1b6SKees Cook 
671313dd1b6SKees Cook 	dom = get_immediate_dominator(CDI_DOMINATORS, bb);
672313dd1b6SKees Cook 	if (!dom)
673313dd1b6SKees Cook 		return false;
674313dd1b6SKees Cook 
675313dd1b6SKees Cook 	dom_stmt = last_stmt(dom);
676313dd1b6SKees Cook 	if (!dom_stmt)
677313dd1b6SKees Cook 		return false;
678313dd1b6SKees Cook 
679313dd1b6SKees Cook 	if (gimple_code(dom_stmt) != GIMPLE_COND)
680313dd1b6SKees Cook 		return false;
681313dd1b6SKees Cook 
682313dd1b6SKees Cook 	if (gimple_cond_code(dom_stmt) != NE_EXPR)
683313dd1b6SKees Cook 		return false;
684313dd1b6SKees Cook 
685313dd1b6SKees Cook 	if (!integer_zerop(gimple_cond_rhs(dom_stmt)))
686313dd1b6SKees Cook 		return false;
687313dd1b6SKees Cook 
688313dd1b6SKees Cook 	poss_is_err_cond = gimple_cond_lhs(dom_stmt);
689313dd1b6SKees Cook 
690313dd1b6SKees Cook 	if (TREE_CODE(poss_is_err_cond) != SSA_NAME)
691313dd1b6SKees Cook 		return false;
692313dd1b6SKees Cook 
693313dd1b6SKees Cook 	call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond);
694313dd1b6SKees Cook 
695313dd1b6SKees Cook 	if (gimple_code(call_stmt) != GIMPLE_CALL)
696313dd1b6SKees Cook 		return false;
697313dd1b6SKees Cook 
698313dd1b6SKees Cook 	dom_lhs = gimple_get_lhs(call_stmt);
699313dd1b6SKees Cook 	poss_is_err_func = gimple_call_fndecl(call_stmt);
700313dd1b6SKees Cook 	if (!poss_is_err_func)
701313dd1b6SKees Cook 		return false;
702313dd1b6SKees Cook 	if (dom_lhs != poss_is_err_cond)
703313dd1b6SKees Cook 		return false;
704313dd1b6SKees Cook 	if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR"))
705313dd1b6SKees Cook 		return false;
706313dd1b6SKees Cook 
707313dd1b6SKees Cook 	is_err_arg = gimple_call_arg(call_stmt, 0);
708313dd1b6SKees Cook 	if (!is_err_arg)
709313dd1b6SKees Cook 		return false;
710313dd1b6SKees Cook 
711313dd1b6SKees Cook 	if (is_err_arg != rhs)
712313dd1b6SKees Cook 		return false;
713313dd1b6SKees Cook 
714313dd1b6SKees Cook 	return true;
715313dd1b6SKees Cook }
716313dd1b6SKees Cook 
717313dd1b6SKees Cook static void handle_local_var_initializers(void)
718313dd1b6SKees Cook {
719313dd1b6SKees Cook 	tree var;
720313dd1b6SKees Cook 	unsigned int i;
721313dd1b6SKees Cook 
722313dd1b6SKees Cook 	FOR_EACH_LOCAL_DECL(cfun, i, var) {
723313dd1b6SKees Cook 		tree init = DECL_INITIAL(var);
724313dd1b6SKees Cook 		if (!init)
725313dd1b6SKees Cook 			continue;
726313dd1b6SKees Cook 		if (TREE_CODE(init) != CONSTRUCTOR)
727313dd1b6SKees Cook 			continue;
728313dd1b6SKees Cook 		check_bad_casts_in_constructor(var, init);
729313dd1b6SKees Cook 	}
730313dd1b6SKees Cook }
731313dd1b6SKees Cook 
732313dd1b6SKees Cook /*
733313dd1b6SKees Cook  * iterate over all statements to find "bad" casts:
734313dd1b6SKees Cook  * those where the address of the start of a structure is cast
735313dd1b6SKees Cook  * to a pointer of a structure of a different type, or a
736313dd1b6SKees Cook  * structure pointer type is cast to a different structure pointer type
737313dd1b6SKees Cook  */
738313dd1b6SKees Cook static unsigned int find_bad_casts_execute(void)
739313dd1b6SKees Cook {
740313dd1b6SKees Cook 	basic_block bb;
741313dd1b6SKees Cook 
742313dd1b6SKees Cook 	handle_local_var_initializers();
743313dd1b6SKees Cook 
744313dd1b6SKees Cook 	FOR_EACH_BB_FN(bb, cfun) {
745313dd1b6SKees Cook 		gimple_stmt_iterator gsi;
746313dd1b6SKees Cook 
747313dd1b6SKees Cook 		for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
748313dd1b6SKees Cook 			gimple stmt;
749313dd1b6SKees Cook 			const_tree lhs;
750313dd1b6SKees Cook 			const_tree lhs_type;
751313dd1b6SKees Cook 			const_tree rhs1;
752313dd1b6SKees Cook 			const_tree rhs_type;
753313dd1b6SKees Cook 			const_tree ptr_lhs_type;
754313dd1b6SKees Cook 			const_tree ptr_rhs_type;
755313dd1b6SKees Cook 			const_tree op0;
756313dd1b6SKees Cook 			const_tree op0_type;
757313dd1b6SKees Cook 			enum tree_code rhs_code;
758313dd1b6SKees Cook 
759313dd1b6SKees Cook 			stmt = gsi_stmt(gsi);
760313dd1b6SKees Cook 
761313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
762313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
763313dd1b6SKees Cook 			debug_gimple_stmt(stmt);
764313dd1b6SKees Cook 			debug_tree(gimple_get_lhs(stmt));
765313dd1b6SKees Cook #endif
766313dd1b6SKees Cook #endif
767313dd1b6SKees Cook 
768313dd1b6SKees Cook 			if (gimple_code(stmt) != GIMPLE_ASSIGN)
769313dd1b6SKees Cook 				continue;
770313dd1b6SKees Cook 
771313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN
772313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE
773313dd1b6SKees Cook 			debug_tree(gimple_assign_rhs1(stmt));
774313dd1b6SKees Cook #endif
775313dd1b6SKees Cook #endif
776313dd1b6SKees Cook 
777313dd1b6SKees Cook 
778313dd1b6SKees Cook 			rhs_code = gimple_assign_rhs_code(stmt);
779313dd1b6SKees Cook 
780313dd1b6SKees Cook 			if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME)
781313dd1b6SKees Cook 				continue;
782313dd1b6SKees Cook 
783313dd1b6SKees Cook 			lhs = gimple_get_lhs(stmt);
784313dd1b6SKees Cook 			lhs_type = TREE_TYPE(lhs);
785313dd1b6SKees Cook 			rhs1 = gimple_assign_rhs1(stmt);
786313dd1b6SKees Cook 			rhs_type = TREE_TYPE(rhs1);
787313dd1b6SKees Cook 
788313dd1b6SKees Cook 			if (TREE_CODE(rhs_type) != POINTER_TYPE ||
789313dd1b6SKees Cook 			    TREE_CODE(lhs_type) != POINTER_TYPE)
790313dd1b6SKees Cook 				continue;
791313dd1b6SKees Cook 
792313dd1b6SKees Cook 			ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type))));
793313dd1b6SKees Cook 			ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type))));
794313dd1b6SKees Cook 
795313dd1b6SKees Cook 			if (ptr_rhs_type == void_type_node)
796313dd1b6SKees Cook 				continue;
797313dd1b6SKees Cook 
798313dd1b6SKees Cook 			if (ptr_lhs_type == void_type_node)
799313dd1b6SKees Cook 				continue;
800313dd1b6SKees Cook 
801313dd1b6SKees Cook 			if (dominated_by_is_err(rhs1, bb))
802313dd1b6SKees Cook 				continue;
803313dd1b6SKees Cook 
804313dd1b6SKees Cook 			if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) {
805313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
806313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type)))
807313dd1b6SKees Cook #endif
808313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type);
809313dd1b6SKees Cook 				continue;
810313dd1b6SKees Cook 			}
811313dd1b6SKees Cook 
812313dd1b6SKees Cook 			if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type)
813313dd1b6SKees Cook 				continue;
814313dd1b6SKees Cook 
815313dd1b6SKees Cook 			if (rhs_code == ADDR_EXPR) {
816313dd1b6SKees Cook 				op0 = TREE_OPERAND(rhs1, 0);
817313dd1b6SKees Cook 
818313dd1b6SKees Cook 				if (op0 == NULL_TREE)
819313dd1b6SKees Cook 					continue;
820313dd1b6SKees Cook 
821313dd1b6SKees Cook 				if (TREE_CODE(op0) != VAR_DECL)
822313dd1b6SKees Cook 					continue;
823313dd1b6SKees Cook 
824313dd1b6SKees Cook 				op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0))));
825313dd1b6SKees Cook 				if (op0_type == ptr_lhs_type)
826313dd1b6SKees Cook 					continue;
827313dd1b6SKees Cook 
828313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
829313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type)))
830313dd1b6SKees Cook #endif
831313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type);
832313dd1b6SKees Cook 			} else {
833313dd1b6SKees Cook 				const_tree ssa_name_var = SSA_NAME_VAR(rhs1);
834313dd1b6SKees Cook 				/* skip bogus type casts introduced by container_of */
835313dd1b6SKees Cook 				if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) &&
836313dd1b6SKees Cook 				    !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr"))
837313dd1b6SKees Cook 					continue;
838313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN
839313dd1b6SKees Cook 				if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type)))
840313dd1b6SKees Cook #endif
841313dd1b6SKees Cook 				MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type);
842313dd1b6SKees Cook 			}
843313dd1b6SKees Cook 
844313dd1b6SKees Cook 		}
845313dd1b6SKees Cook 	}
846313dd1b6SKees Cook 	return 0;
847313dd1b6SKees Cook }
848313dd1b6SKees Cook 
849313dd1b6SKees Cook #define PASS_NAME find_bad_casts
850313dd1b6SKees Cook #define NO_GATE
851313dd1b6SKees Cook #define TODO_FLAGS_FINISH TODO_dump_func
852313dd1b6SKees Cook #include "gcc-generate-gimple-pass.h"
853313dd1b6SKees Cook 
854313dd1b6SKees Cook __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
855313dd1b6SKees Cook {
856313dd1b6SKees Cook 	int i;
857313dd1b6SKees Cook 	const char * const plugin_name = plugin_info->base_name;
858313dd1b6SKees Cook 	const int argc = plugin_info->argc;
859313dd1b6SKees Cook 	const struct plugin_argument * const argv = plugin_info->argv;
860313dd1b6SKees Cook 	bool enable = true;
861313dd1b6SKees Cook 	int obtained_seed = 0;
862313dd1b6SKees Cook 	struct register_pass_info find_bad_casts_pass_info;
863313dd1b6SKees Cook 
864313dd1b6SKees Cook 	find_bad_casts_pass_info.pass			= make_find_bad_casts_pass();
865313dd1b6SKees Cook 	find_bad_casts_pass_info.reference_pass_name	= "ssa";
866313dd1b6SKees Cook 	find_bad_casts_pass_info.ref_pass_instance_number	= 1;
867313dd1b6SKees Cook 	find_bad_casts_pass_info.pos_op			= PASS_POS_INSERT_AFTER;
868313dd1b6SKees Cook 
869313dd1b6SKees Cook 	if (!plugin_default_version_check(version, &gcc_version)) {
870313dd1b6SKees Cook 		error(G_("incompatible gcc/plugin versions"));
871313dd1b6SKees Cook 		return 1;
872313dd1b6SKees Cook 	}
873313dd1b6SKees Cook 
874313dd1b6SKees Cook 	if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
875313dd1b6SKees Cook 		inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
876313dd1b6SKees Cook 		enable = false;
877313dd1b6SKees Cook 	}
878313dd1b6SKees Cook 
879313dd1b6SKees Cook 	for (i = 0; i < argc; ++i) {
880313dd1b6SKees Cook 		if (!strcmp(argv[i].key, "disable")) {
881313dd1b6SKees Cook 			enable = false;
882313dd1b6SKees Cook 			continue;
883313dd1b6SKees Cook 		}
884313dd1b6SKees Cook 		if (!strcmp(argv[i].key, "performance-mode")) {
885313dd1b6SKees Cook 			performance_mode = 1;
886313dd1b6SKees Cook 			continue;
887313dd1b6SKees Cook 		}
888313dd1b6SKees Cook 		error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
889313dd1b6SKees Cook 	}
890313dd1b6SKees Cook 
891313dd1b6SKees Cook 	if (strlen(randstruct_seed) != 64) {
892313dd1b6SKees Cook 		error(G_("invalid seed value supplied for %s plugin"), plugin_name);
893313dd1b6SKees Cook 		return 1;
894313dd1b6SKees Cook 	}
895313dd1b6SKees Cook 	obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx",
896313dd1b6SKees Cook 		&shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]);
897313dd1b6SKees Cook 	if (obtained_seed != 4) {
898313dd1b6SKees Cook 		error(G_("Invalid seed supplied for %s plugin"), plugin_name);
899313dd1b6SKees Cook 		return 1;
900313dd1b6SKees Cook 	}
901313dd1b6SKees Cook 
902313dd1b6SKees Cook 	register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info);
903313dd1b6SKees Cook 	if (enable) {
904313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL);
905313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info);
906313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
907313dd1b6SKees Cook 		register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL);
908313dd1b6SKees Cook 	}
909313dd1b6SKees Cook 	register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
910313dd1b6SKees Cook 
911313dd1b6SKees Cook 	return 0;
912313dd1b6SKees Cook }
913