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