2 * Copyright 2014-2016 by Open Source Security, Inc., Brad Spengler <spender@grsecurity.net>
3 * and PaX Team <pageexec@freemail.hu>
4 * Licensed under the GPL v2
6 * Note: the choice of the license means that the compilation process is
7 * NOT 'eligible' as defined by gcc's library exception to the GPL v3,
8 * but for the kernel it doesn't matter since it doesn't link against
9 * any of the gcc libraries
12 * $ # for 4.5/4.6/C based 4.7
13 * $ 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
14 * $ # for C++ based 4.7/4.8+
15 * $ 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
16 * $ gcc -fplugin=./randomize_layout_plugin.so test.c -O2
19 #include "gcc-common.h"
20 #include "randomize_layout_seed.h"
22 #if BUILDING_GCC_MAJOR < 4 || (BUILDING_GCC_MAJOR == 4 && BUILDING_GCC_MINOR < 7)
23 #error "The RANDSTRUCT plugin requires GCC 4.7 or newer."
26 #define ORIG_TYPE_NAME(node) \
27 (TYPE_NAME(TYPE_MAIN_VARIANT(node)) != NULL_TREE ? ((const unsigned char *)IDENTIFIER_POINTER(TYPE_NAME(TYPE_MAIN_VARIANT(node)))) : (const unsigned char *)"anonymous")
29 #define INFORM(loc, msg, ...) inform(loc, "randstruct: " msg, ##__VA_ARGS__)
30 #define MISMATCH(loc, how, ...) INFORM(loc, "casting between randomized structure pointer types (" how "): %qT and %qT\n", __VA_ARGS__)
32 __visible int plugin_is_GPL_compatible;
34 static int performance_mode;
36 static struct plugin_info randomize_layout_plugin_info = {
37 .version = "201402201816vanilla",
38 .help = "disable\t\t\tdo not activate plugin\n"
39 "performance-mode\tenable cacheline-aware layout randomization\n"
42 struct whitelist_entry {
48 static const struct whitelist_entry whitelist[] = {
49 /* NIU overloads mapping with page struct */
50 { "drivers/net/ethernet/sun/niu.c", "page", "address_space" },
51 /* unix_skb_parms via UNIXCB() buffer */
52 { "net/unix/af_unix.c", "unix_skb_parms", "char" },
53 /* big_key payload.data struct splashing */
54 { "security/keys/big_key.c", "path", "void *" },
55 /* walk struct security_hook_heads as an array of struct hlist_head */
56 { "security/security.c", "hlist_head", "security_hook_heads" },
60 /* from old Linux dcache.h */
61 static inline unsigned long
62 partial_name_hash(unsigned long c, unsigned long prevhash)
64 return (prevhash + (c << 4) + (c >> 4)) * 11;
66 static inline unsigned int
67 name_hash(const unsigned char *name)
69 unsigned long hash = 0;
70 unsigned int len = strlen((const char *)name);
72 hash = partial_name_hash(*name++, hash);
73 return (unsigned int)hash;
76 static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
81 if (TREE_CODE(*node) == FUNCTION_DECL) {
82 error("%qE attribute does not apply to functions (%qF)", name, *node);
86 if (TREE_CODE(*node) == PARM_DECL) {
87 error("%qE attribute does not apply to function parameters (%qD)", name, *node);
91 if (TREE_CODE(*node) == VAR_DECL) {
92 error("%qE attribute does not apply to variables (%qD)", name, *node);
99 gcc_assert(TREE_CODE(*node) == TYPE_DECL);
100 type = TREE_TYPE(*node);
103 if (TREE_CODE(type) != RECORD_TYPE) {
104 error("%qE attribute used on %qT applies to struct types only", name, type);
108 if (lookup_attribute(IDENTIFIER_POINTER(name), TYPE_ATTRIBUTES(type))) {
109 error("%qE attribute is already applied to the type %qT", name, type);
113 *no_add_attrs = false;
118 /* set on complete types that we don't need to inspect further at all */
119 static tree handle_randomize_considered_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
121 *no_add_attrs = false;
126 * set on types that we've performed a shuffle on, to prevent re-shuffling
127 * this does not preclude us from inspecting its fields for potential shuffles
129 static tree handle_randomize_performed_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
131 *no_add_attrs = false;
136 * 64bit variant of Bob Jenkins' public domain PRNG
137 * 256 bits of internal state
140 typedef unsigned long long u64;
142 typedef struct ranctx { u64 a; u64 b; u64 c; u64 d; } ranctx;
144 #define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
145 static u64 ranval(ranctx *x) {
146 u64 e = x->a - rot(x->b, 7);
147 x->a = x->b ^ rot(x->c, 13);
148 x->b = x->c + rot(x->d, 37);
154 static void raninit(ranctx *x, u64 *seed) {
162 for (i=0; i < 30; ++i)
166 static u64 shuffle_seed[4];
168 struct partition_group {
171 unsigned long length;
174 static void partition_struct(tree *fields, unsigned long length, struct partition_group *size_groups, unsigned long *num_groups)
177 unsigned long accum_size = 0;
178 unsigned long accum_length = 0;
179 unsigned long group_idx = 0;
181 gcc_assert(length < INT_MAX);
183 memset(size_groups, 0, sizeof(struct partition_group) * length);
185 for (i = 0; i < length; i++) {
186 if (size_groups[group_idx].tree_start == NULL_TREE) {
187 size_groups[group_idx].tree_start = fields[i];
188 size_groups[group_idx].start = i;
192 accum_size += (unsigned long)int_size_in_bytes(TREE_TYPE(fields[i]));
194 if (accum_size >= 64) {
195 size_groups[group_idx].length = accum_length;
201 if (size_groups[group_idx].tree_start != NULL_TREE &&
202 !size_groups[group_idx].length) {
203 size_groups[group_idx].length = accum_length;
207 *num_groups = group_idx;
210 static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
212 unsigned long i, x, index;
213 struct partition_group size_group[length];
214 unsigned long num_groups = 0;
215 unsigned long randnum;
217 partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
219 /* FIXME: this group shuffle is currently a no-op. */
220 for (i = num_groups - 1; i > 0; i--) {
221 struct partition_group tmp;
222 randnum = ranval(prng_state) % (i + 1);
224 size_group[i] = size_group[randnum];
225 size_group[randnum] = tmp;
228 for (x = 0; x < num_groups; x++) {
229 for (index = size_group[x].length - 1; index > 0; index--) {
232 i = size_group[x].start + index;
233 if (DECL_BIT_FIELD_TYPE(newtree[i]))
235 randnum = ranval(prng_state) % (index + 1);
236 randnum += size_group[x].start;
237 // we could handle this case differently if desired
238 if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
241 newtree[i] = newtree[randnum];
242 newtree[randnum] = tmp;
247 static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
249 unsigned long i, randnum;
251 for (i = length - 1; i > 0; i--) {
253 randnum = ranval(prng_state) % (i + 1);
255 newtree[i] = newtree[randnum];
256 newtree[randnum] = tmp;
260 /* modern in-place Fisher-Yates shuffle */
261 static void shuffle(const_tree type, tree *newtree, unsigned long length)
266 const unsigned char *structname;
271 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
273 structname = ORIG_TYPE_NAME(type);
275 #ifdef __DEBUG_PLUGIN
276 fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type);
277 #ifdef __DEBUG_VERBOSE
278 debug_tree((tree)type);
282 for (i = 0; i < 4; i++) {
283 seed[i] = shuffle_seed[i];
284 seed[i] ^= name_hash(structname);
287 raninit(&prng_state, (u64 *)&seed);
289 if (performance_mode)
290 performance_shuffle(newtree, length, &prng_state);
292 full_shuffle(newtree, length, &prng_state);
295 static bool is_flexible_array(const_tree field)
297 const_tree fieldtype;
302 fieldtype = TREE_TYPE(field);
303 typesize = TYPE_SIZE(fieldtype);
305 if (TREE_CODE(fieldtype) != ARRAY_TYPE)
308 elemtype = TREE_TYPE(fieldtype);
309 elemsize = TYPE_SIZE(elemtype);
311 /* size of type is represented in bits */
313 if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
314 TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
317 if (typesize != NULL_TREE &&
318 (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
319 tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
325 static int relayout_struct(tree type)
327 unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type));
328 unsigned long shuffle_length = num_fields;
330 tree newtree[num_fields];
335 expanded_location xloc;
336 bool has_flexarray = false;
338 if (TYPE_FIELDS(type) == NULL_TREE)
344 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
346 gcc_assert(num_fields < INT_MAX);
348 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) ||
349 lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))))
352 /* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */
353 if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") ||
354 !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY"))
357 /* throw out any structs in uapi */
358 xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type)));
360 if (strstr(xloc.file, "/uapi/"))
361 error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type));
363 for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) {
364 gcc_assert(TREE_CODE(field) == FIELD_DECL);
369 * enforce that we don't randomize the layout of the last
370 * element of a struct if it's a 0 or 1-length array
371 * or a proper flexible array
373 if (is_flexible_array(newtree[num_fields - 1])) {
374 has_flexarray = true;
378 shuffle(type, (tree *)newtree, shuffle_length);
381 * set up a bogus anonymous struct field designed to error out on unnamed struct initializers
382 * as gcc provides no other way to detect such code
384 list = make_node(FIELD_DECL);
385 TREE_CHAIN(list) = newtree[0];
386 TREE_TYPE(list) = void_type_node;
387 DECL_SIZE(list) = bitsize_zero_node;
388 DECL_NONADDRESSABLE_P(list) = 1;
389 DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node;
390 DECL_SIZE_UNIT(list) = size_zero_node;
391 DECL_FIELD_OFFSET(list) = size_zero_node;
392 DECL_CONTEXT(list) = type;
393 // to satisfy the constify plugin
394 TREE_READONLY(list) = 1;
396 for (i = 0; i < num_fields - 1; i++)
397 TREE_CHAIN(newtree[i]) = newtree[i+1];
398 TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
400 main_variant = TYPE_MAIN_VARIANT(type);
401 for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
402 TYPE_FIELDS(variant) = list;
403 TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
404 TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
405 TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
407 TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
411 * force a re-layout of the main variant
412 * the TYPE_SIZE for all variants will be recomputed
413 * by finalize_type_size()
415 TYPE_SIZE(main_variant) = NULL_TREE;
416 layout_type(main_variant);
417 gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE);
422 /* from constify plugin */
423 static const_tree get_field_type(const_tree field)
425 return strip_array_types(TREE_TYPE(field));
428 /* from constify plugin */
429 static bool is_fptr(const_tree fieldtype)
431 if (TREE_CODE(fieldtype) != POINTER_TYPE)
434 return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE;
437 /* derived from constify plugin */
438 static int is_pure_ops_struct(const_tree node)
442 gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE);
444 for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) {
445 const_tree fieldtype = get_field_type(field);
446 enum tree_code code = TREE_CODE(fieldtype);
448 if (node == fieldtype)
451 if (code == RECORD_TYPE || code == UNION_TYPE) {
452 if (!is_pure_ops_struct(fieldtype))
457 if (!is_fptr(fieldtype))
464 static void randomize_type(tree type)
468 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
470 if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
473 if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
474 relayout_struct(type);
476 for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
477 TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
478 TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
480 #ifdef __DEBUG_PLUGIN
481 fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
482 #ifdef __DEBUG_VERBOSE
488 static void update_decl_size(tree decl)
490 tree lastval, lastidx, field, init, type, flexsize;
491 unsigned HOST_WIDE_INT len;
493 type = TREE_TYPE(decl);
495 if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type)))
498 init = DECL_INITIAL(decl);
499 if (init == NULL_TREE || init == error_mark_node)
502 if (TREE_CODE(init) != CONSTRUCTOR)
505 len = CONSTRUCTOR_NELTS(init);
509 lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value;
510 lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index;
512 for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field))
515 if (lastidx != field)
518 if (TREE_CODE(lastval) != STRING_CST) {
519 error("Only string constants are supported as initializers "
520 "for randomized structures with flexible arrays");
524 flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) *
525 tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval)))));
527 DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize);
533 static void randomize_layout_finish_decl(void *event_data, void *data)
535 tree decl = (tree)event_data;
538 if (decl == NULL_TREE || decl == error_mark_node)
541 type = TREE_TYPE(decl);
543 if (TREE_CODE(decl) != VAR_DECL)
546 if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
549 if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)))
553 DECL_SIZE_UNIT(decl) = 0;
554 SET_DECL_ALIGN(decl, 0);
555 SET_DECL_MODE (decl, VOIDmode);
556 SET_DECL_RTL(decl, 0);
557 update_decl_size(decl);
558 layout_decl(decl, 0);
561 static void finish_type(void *event_data, void *data)
563 tree type = (tree)event_data;
565 if (type == NULL_TREE || type == error_mark_node)
568 if (TREE_CODE(type) != RECORD_TYPE)
571 if (TYPE_FIELDS(type) == NULL_TREE)
574 if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
577 #ifdef __DEBUG_PLUGIN
578 fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type));
580 #ifdef __DEBUG_VERBOSE
583 randomize_type(type);
588 static struct attribute_spec randomize_layout_attr = { };
589 static struct attribute_spec no_randomize_layout_attr = { };
590 static struct attribute_spec randomize_considered_attr = { };
591 static struct attribute_spec randomize_performed_attr = { };
593 static void register_attributes(void *event_data, void *data)
595 randomize_layout_attr.name = "randomize_layout";
596 randomize_layout_attr.type_required = true;
597 randomize_layout_attr.handler = handle_randomize_layout_attr;
598 #if BUILDING_GCC_VERSION >= 4007
599 randomize_layout_attr.affects_type_identity = true;
602 no_randomize_layout_attr.name = "no_randomize_layout";
603 no_randomize_layout_attr.type_required = true;
604 no_randomize_layout_attr.handler = handle_randomize_layout_attr;
605 #if BUILDING_GCC_VERSION >= 4007
606 no_randomize_layout_attr.affects_type_identity = true;
609 randomize_considered_attr.name = "randomize_considered";
610 randomize_considered_attr.type_required = true;
611 randomize_considered_attr.handler = handle_randomize_considered_attr;
613 randomize_performed_attr.name = "randomize_performed";
614 randomize_performed_attr.type_required = true;
615 randomize_performed_attr.handler = handle_randomize_performed_attr;
617 register_attribute(&randomize_layout_attr);
618 register_attribute(&no_randomize_layout_attr);
619 register_attribute(&randomize_considered_attr);
620 register_attribute(&randomize_performed_attr);
623 static void check_bad_casts_in_constructor(tree var, tree init)
625 unsigned HOST_WIDE_INT idx;
627 tree field_type, val_type;
629 FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) {
630 if (TREE_CODE(val) == CONSTRUCTOR) {
631 check_bad_casts_in_constructor(var, val);
635 /* pipacs' plugin creates franken-arrays that differ from those produced by
636 normal code which all have valid 'field' trees. work around this */
637 if (field == NULL_TREE)
639 field_type = TREE_TYPE(field);
640 val_type = TREE_TYPE(val);
642 if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE)
645 if (field_type == val_type)
648 field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type))));
649 val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type))));
651 if (field_type == void_type_node)
653 if (field_type == val_type)
655 if (TREE_CODE(val_type) != RECORD_TYPE)
658 if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type)))
660 MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type));
664 /* derived from the constify plugin */
665 static void check_global_variables(void *event_data, void *data)
667 struct varpool_node *node;
670 FOR_EACH_VARIABLE(node) {
671 tree var = NODE_DECL(node);
672 init = DECL_INITIAL(var);
673 if (init == NULL_TREE)
676 if (TREE_CODE(init) != CONSTRUCTOR)
679 check_bad_casts_in_constructor(var, init);
683 static bool dominated_by_is_err(const_tree rhs, basic_block bb)
689 const_tree poss_is_err_cond;
690 const_tree poss_is_err_func;
691 const_tree is_err_arg;
693 dom = get_immediate_dominator(CDI_DOMINATORS, bb);
697 dom_stmt = last_stmt(dom);
701 if (gimple_code(dom_stmt) != GIMPLE_COND)
704 if (gimple_cond_code(dom_stmt) != NE_EXPR)
707 if (!integer_zerop(gimple_cond_rhs(dom_stmt)))
710 poss_is_err_cond = gimple_cond_lhs(dom_stmt);
712 if (TREE_CODE(poss_is_err_cond) != SSA_NAME)
715 call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond);
717 if (gimple_code(call_stmt) != GIMPLE_CALL)
720 dom_lhs = gimple_get_lhs(call_stmt);
721 poss_is_err_func = gimple_call_fndecl(call_stmt);
722 if (!poss_is_err_func)
724 if (dom_lhs != poss_is_err_cond)
726 if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR"))
729 is_err_arg = gimple_call_arg(call_stmt, 0);
733 if (is_err_arg != rhs)
739 static void handle_local_var_initializers(void)
744 FOR_EACH_LOCAL_DECL(cfun, i, var) {
745 tree init = DECL_INITIAL(var);
748 if (TREE_CODE(init) != CONSTRUCTOR)
750 check_bad_casts_in_constructor(var, init);
754 static bool type_name_eq(gimple stmt, const_tree type_tree, const char *wanted_name)
756 const char *type_name;
758 if (type_tree == NULL_TREE)
761 switch (TREE_CODE(type_tree)) {
763 type_name = TYPE_NAME_POINTER(type_tree);
766 if (TYPE_PRECISION(type_tree) == CHAR_TYPE_SIZE)
769 INFORM(gimple_location(stmt), "found non-char INTEGER_TYPE cast comparison: %qT\n", type_tree);
770 debug_tree(type_tree);
775 if (TREE_CODE(TREE_TYPE(type_tree)) == VOID_TYPE) {
776 type_name = "void *";
779 INFORM(gimple_location(stmt), "found non-void POINTER_TYPE cast comparison %qT\n", type_tree);
780 debug_tree(type_tree);
784 INFORM(gimple_location(stmt), "unhandled cast comparison: %qT\n", type_tree);
785 debug_tree(type_tree);
789 return strcmp(type_name, wanted_name) == 0;
792 static bool whitelisted_cast(gimple stmt, const_tree lhs_tree, const_tree rhs_tree)
794 const struct whitelist_entry *entry;
795 expanded_location xloc = expand_location(gimple_location(stmt));
797 for (entry = whitelist; entry->pathname; entry++) {
798 if (!strstr(xloc.file, entry->pathname))
801 if (type_name_eq(stmt, lhs_tree, entry->lhs) && type_name_eq(stmt, rhs_tree, entry->rhs))
809 * iterate over all statements to find "bad" casts:
810 * those where the address of the start of a structure is cast
811 * to a pointer of a structure of a different type, or a
812 * structure pointer type is cast to a different structure pointer type
814 static unsigned int find_bad_casts_execute(void)
818 handle_local_var_initializers();
820 FOR_EACH_BB_FN(bb, cfun) {
821 gimple_stmt_iterator gsi;
823 for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
829 const_tree ptr_lhs_type;
830 const_tree ptr_rhs_type;
833 enum tree_code rhs_code;
835 stmt = gsi_stmt(gsi);
837 #ifdef __DEBUG_PLUGIN
838 #ifdef __DEBUG_VERBOSE
839 debug_gimple_stmt(stmt);
840 debug_tree(gimple_get_lhs(stmt));
844 if (gimple_code(stmt) != GIMPLE_ASSIGN)
847 #ifdef __DEBUG_PLUGIN
848 #ifdef __DEBUG_VERBOSE
849 debug_tree(gimple_assign_rhs1(stmt));
854 rhs_code = gimple_assign_rhs_code(stmt);
856 if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME)
859 lhs = gimple_get_lhs(stmt);
860 lhs_type = TREE_TYPE(lhs);
861 rhs1 = gimple_assign_rhs1(stmt);
862 rhs_type = TREE_TYPE(rhs1);
864 if (TREE_CODE(rhs_type) != POINTER_TYPE ||
865 TREE_CODE(lhs_type) != POINTER_TYPE)
868 ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type))));
869 ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type))));
871 if (ptr_rhs_type == void_type_node)
874 if (ptr_lhs_type == void_type_node)
877 if (dominated_by_is_err(rhs1, bb))
880 if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) {
881 #ifndef __DEBUG_PLUGIN
882 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type)))
885 if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type))
886 MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type);
891 if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type)
894 if (rhs_code == ADDR_EXPR) {
895 op0 = TREE_OPERAND(rhs1, 0);
897 if (op0 == NULL_TREE)
900 if (TREE_CODE(op0) != VAR_DECL)
903 op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0))));
904 if (op0_type == ptr_lhs_type)
907 #ifndef __DEBUG_PLUGIN
908 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type)))
911 if (!whitelisted_cast(stmt, ptr_lhs_type, op0_type))
912 MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type);
915 const_tree ssa_name_var = SSA_NAME_VAR(rhs1);
916 /* skip bogus type casts introduced by container_of */
917 if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) &&
918 !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr"))
920 #ifndef __DEBUG_PLUGIN
921 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type)))
924 if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type))
925 MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type);
934 #define PASS_NAME find_bad_casts
936 #define TODO_FLAGS_FINISH TODO_dump_func
937 #include "gcc-generate-gimple-pass.h"
939 __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
942 const char * const plugin_name = plugin_info->base_name;
943 const int argc = plugin_info->argc;
944 const struct plugin_argument * const argv = plugin_info->argv;
946 int obtained_seed = 0;
947 struct register_pass_info find_bad_casts_pass_info;
949 find_bad_casts_pass_info.pass = make_find_bad_casts_pass();
950 find_bad_casts_pass_info.reference_pass_name = "ssa";
951 find_bad_casts_pass_info.ref_pass_instance_number = 1;
952 find_bad_casts_pass_info.pos_op = PASS_POS_INSERT_AFTER;
954 if (!plugin_default_version_check(version, &gcc_version)) {
955 error(G_("incompatible gcc/plugin versions"));
959 if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
960 inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
964 for (i = 0; i < argc; ++i) {
965 if (!strcmp(argv[i].key, "disable")) {
969 if (!strcmp(argv[i].key, "performance-mode")) {
970 performance_mode = 1;
973 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
976 if (strlen(randstruct_seed) != 64) {
977 error(G_("invalid seed value supplied for %s plugin"), plugin_name);
980 obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx",
981 &shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]);
982 if (obtained_seed != 4) {
983 error(G_("Invalid seed supplied for %s plugin"), plugin_name);
987 register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info);
989 register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL);
990 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info);
991 register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
992 register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL);
994 register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);