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 /* unix_skb_parms via UNIXCB() buffer */
50 { "net/unix/af_unix.c", "unix_skb_parms", "char" },
51 /* walk struct security_hook_heads as an array of struct list_head */
52 { "security/security.c", "list_head", "security_hook_heads" },
56 /* from old Linux dcache.h */
57 static inline unsigned long
58 partial_name_hash(unsigned long c, unsigned long prevhash)
60 return (prevhash + (c << 4) + (c >> 4)) * 11;
62 static inline unsigned int
63 name_hash(const unsigned char *name)
65 unsigned long hash = 0;
66 unsigned int len = strlen((const char *)name);
68 hash = partial_name_hash(*name++, hash);
69 return (unsigned int)hash;
72 static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
77 if (TREE_CODE(*node) == FUNCTION_DECL) {
78 error("%qE attribute does not apply to functions (%qF)", name, *node);
82 if (TREE_CODE(*node) == PARM_DECL) {
83 error("%qE attribute does not apply to function parameters (%qD)", name, *node);
87 if (TREE_CODE(*node) == VAR_DECL) {
88 error("%qE attribute does not apply to variables (%qD)", name, *node);
95 gcc_assert(TREE_CODE(*node) == TYPE_DECL);
96 type = TREE_TYPE(*node);
99 if (TREE_CODE(type) != RECORD_TYPE) {
100 error("%qE attribute used on %qT applies to struct types only", name, type);
104 if (lookup_attribute(IDENTIFIER_POINTER(name), TYPE_ATTRIBUTES(type))) {
105 error("%qE attribute is already applied to the type %qT", name, type);
109 *no_add_attrs = false;
114 /* set on complete types that we don't need to inspect further at all */
115 static tree handle_randomize_considered_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
117 *no_add_attrs = false;
122 * set on types that we've performed a shuffle on, to prevent re-shuffling
123 * this does not preclude us from inspecting its fields for potential shuffles
125 static tree handle_randomize_performed_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
127 *no_add_attrs = false;
132 * 64bit variant of Bob Jenkins' public domain PRNG
133 * 256 bits of internal state
136 typedef unsigned long long u64;
138 typedef struct ranctx { u64 a; u64 b; u64 c; u64 d; } ranctx;
140 #define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
141 static u64 ranval(ranctx *x) {
142 u64 e = x->a - rot(x->b, 7);
143 x->a = x->b ^ rot(x->c, 13);
144 x->b = x->c + rot(x->d, 37);
150 static void raninit(ranctx *x, u64 *seed) {
158 for (i=0; i < 30; ++i)
162 static u64 shuffle_seed[4];
164 struct partition_group {
167 unsigned long length;
170 static void partition_struct(tree *fields, unsigned long length, struct partition_group *size_groups, unsigned long *num_groups)
173 unsigned long accum_size = 0;
174 unsigned long accum_length = 0;
175 unsigned long group_idx = 0;
177 gcc_assert(length < INT_MAX);
179 memset(size_groups, 0, sizeof(struct partition_group) * length);
181 for (i = 0; i < length; i++) {
182 if (size_groups[group_idx].tree_start == NULL_TREE) {
183 size_groups[group_idx].tree_start = fields[i];
184 size_groups[group_idx].start = i;
188 accum_size += (unsigned long)int_size_in_bytes(TREE_TYPE(fields[i]));
190 if (accum_size >= 64) {
191 size_groups[group_idx].length = accum_length;
197 if (size_groups[group_idx].tree_start != NULL_TREE &&
198 !size_groups[group_idx].length) {
199 size_groups[group_idx].length = accum_length;
203 *num_groups = group_idx;
206 static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
209 struct partition_group size_group[length];
210 unsigned long num_groups = 0;
211 unsigned long randnum;
213 partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
214 for (i = num_groups - 1; i > 0; i--) {
215 struct partition_group tmp;
216 randnum = ranval(prng_state) % (i + 1);
218 size_group[i] = size_group[randnum];
219 size_group[randnum] = tmp;
222 for (x = 0; x < num_groups; x++) {
223 for (i = size_group[x].start + size_group[x].length - 1; i > size_group[x].start; i--) {
225 if (DECL_BIT_FIELD_TYPE(newtree[i]))
227 randnum = ranval(prng_state) % (i + 1);
228 // we could handle this case differently if desired
229 if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
232 newtree[i] = newtree[randnum];
233 newtree[randnum] = tmp;
238 static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
240 unsigned long i, randnum;
242 for (i = length - 1; i > 0; i--) {
244 randnum = ranval(prng_state) % (i + 1);
246 newtree[i] = newtree[randnum];
247 newtree[randnum] = tmp;
251 /* modern in-place Fisher-Yates shuffle */
252 static void shuffle(const_tree type, tree *newtree, unsigned long length)
257 const unsigned char *structname;
262 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
264 structname = ORIG_TYPE_NAME(type);
266 #ifdef __DEBUG_PLUGIN
267 fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type);
268 #ifdef __DEBUG_VERBOSE
269 debug_tree((tree)type);
273 for (i = 0; i < 4; i++) {
274 seed[i] = shuffle_seed[i];
275 seed[i] ^= name_hash(structname);
278 raninit(&prng_state, (u64 *)&seed);
280 if (performance_mode)
281 performance_shuffle(newtree, length, &prng_state);
283 full_shuffle(newtree, length, &prng_state);
286 static bool is_flexible_array(const_tree field)
288 const_tree fieldtype;
293 fieldtype = TREE_TYPE(field);
294 typesize = TYPE_SIZE(fieldtype);
296 if (TREE_CODE(fieldtype) != ARRAY_TYPE)
299 elemtype = TREE_TYPE(fieldtype);
300 elemsize = TYPE_SIZE(elemtype);
302 /* size of type is represented in bits */
304 if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
305 TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
308 if (typesize != NULL_TREE &&
309 (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
310 tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
316 static int relayout_struct(tree type)
318 unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type));
319 unsigned long shuffle_length = num_fields;
321 tree newtree[num_fields];
326 expanded_location xloc;
327 bool has_flexarray = false;
329 if (TYPE_FIELDS(type) == NULL_TREE)
335 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
337 gcc_assert(num_fields < INT_MAX);
339 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) ||
340 lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))))
343 /* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */
344 if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") ||
345 !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY"))
348 /* throw out any structs in uapi */
349 xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type)));
351 if (strstr(xloc.file, "/uapi/"))
352 error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type));
354 for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) {
355 gcc_assert(TREE_CODE(field) == FIELD_DECL);
360 * enforce that we don't randomize the layout of the last
361 * element of a struct if it's a 0 or 1-length array
362 * or a proper flexible array
364 if (is_flexible_array(newtree[num_fields - 1])) {
365 has_flexarray = true;
369 shuffle(type, (tree *)newtree, shuffle_length);
372 * set up a bogus anonymous struct field designed to error out on unnamed struct initializers
373 * as gcc provides no other way to detect such code
375 list = make_node(FIELD_DECL);
376 TREE_CHAIN(list) = newtree[0];
377 TREE_TYPE(list) = void_type_node;
378 DECL_SIZE(list) = bitsize_zero_node;
379 DECL_NONADDRESSABLE_P(list) = 1;
380 DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node;
381 DECL_SIZE_UNIT(list) = size_zero_node;
382 DECL_FIELD_OFFSET(list) = size_zero_node;
383 DECL_CONTEXT(list) = type;
384 // to satisfy the constify plugin
385 TREE_READONLY(list) = 1;
387 for (i = 0; i < num_fields - 1; i++)
388 TREE_CHAIN(newtree[i]) = newtree[i+1];
389 TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
391 main_variant = TYPE_MAIN_VARIANT(type);
392 for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
393 TYPE_FIELDS(variant) = list;
394 TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
395 TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
396 TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
398 TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
402 * force a re-layout of the main variant
403 * the TYPE_SIZE for all variants will be recomputed
404 * by finalize_type_size()
406 TYPE_SIZE(main_variant) = NULL_TREE;
407 layout_type(main_variant);
408 gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE);
413 /* from constify plugin */
414 static const_tree get_field_type(const_tree field)
416 return strip_array_types(TREE_TYPE(field));
419 /* from constify plugin */
420 static bool is_fptr(const_tree fieldtype)
422 if (TREE_CODE(fieldtype) != POINTER_TYPE)
425 return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE;
428 /* derived from constify plugin */
429 static int is_pure_ops_struct(const_tree node)
433 gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE);
435 /* XXX: Do not apply randomization to all-ftpr structs yet. */
438 for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) {
439 const_tree fieldtype = get_field_type(field);
440 enum tree_code code = TREE_CODE(fieldtype);
442 if (node == fieldtype)
445 if (!is_fptr(fieldtype))
448 if (code != RECORD_TYPE && code != UNION_TYPE)
451 if (!is_pure_ops_struct(fieldtype))
458 static void randomize_type(tree type)
462 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
464 if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
467 if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
468 relayout_struct(type);
470 for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
471 TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
472 TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
474 #ifdef __DEBUG_PLUGIN
475 fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
476 #ifdef __DEBUG_VERBOSE
482 static void update_decl_size(tree decl)
484 tree lastval, lastidx, field, init, type, flexsize;
485 unsigned HOST_WIDE_INT len;
487 type = TREE_TYPE(decl);
489 if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type)))
492 init = DECL_INITIAL(decl);
493 if (init == NULL_TREE || init == error_mark_node)
496 if (TREE_CODE(init) != CONSTRUCTOR)
499 len = CONSTRUCTOR_NELTS(init);
503 lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value;
504 lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index;
506 for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field))
509 if (lastidx != field)
512 if (TREE_CODE(lastval) != STRING_CST) {
513 error("Only string constants are supported as initializers "
514 "for randomized structures with flexible arrays");
518 flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) *
519 tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval)))));
521 DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize);
527 static void randomize_layout_finish_decl(void *event_data, void *data)
529 tree decl = (tree)event_data;
532 if (decl == NULL_TREE || decl == error_mark_node)
535 type = TREE_TYPE(decl);
537 if (TREE_CODE(decl) != VAR_DECL)
540 if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
543 if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)))
547 DECL_SIZE_UNIT(decl) = 0;
548 SET_DECL_ALIGN(decl, 0);
549 SET_DECL_MODE (decl, VOIDmode);
550 SET_DECL_RTL(decl, 0);
551 update_decl_size(decl);
552 layout_decl(decl, 0);
555 static void finish_type(void *event_data, void *data)
557 tree type = (tree)event_data;
559 if (type == NULL_TREE || type == error_mark_node)
562 if (TREE_CODE(type) != RECORD_TYPE)
565 if (TYPE_FIELDS(type) == NULL_TREE)
568 if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
571 #ifdef __DEBUG_PLUGIN
572 fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type));
574 #ifdef __DEBUG_VERBOSE
577 randomize_type(type);
582 static struct attribute_spec randomize_layout_attr = {
583 .name = "randomize_layout",
587 .decl_required = false,
588 // need type declaration
589 .type_required = true,
590 .function_type_required = false,
591 .handler = handle_randomize_layout_attr,
592 #if BUILDING_GCC_VERSION >= 4007
593 .affects_type_identity = true
597 static struct attribute_spec no_randomize_layout_attr = {
598 .name = "no_randomize_layout",
602 .decl_required = false,
603 // need type declaration
604 .type_required = true,
605 .function_type_required = false,
606 .handler = handle_randomize_layout_attr,
607 #if BUILDING_GCC_VERSION >= 4007
608 .affects_type_identity = true
612 static struct attribute_spec randomize_considered_attr = {
613 .name = "randomize_considered",
617 .decl_required = false,
618 // need type declaration
619 .type_required = true,
620 .function_type_required = false,
621 .handler = handle_randomize_considered_attr,
622 #if BUILDING_GCC_VERSION >= 4007
623 .affects_type_identity = false
627 static struct attribute_spec randomize_performed_attr = {
628 .name = "randomize_performed",
632 .decl_required = false,
633 // need type declaration
634 .type_required = true,
635 .function_type_required = false,
636 .handler = handle_randomize_performed_attr,
637 #if BUILDING_GCC_VERSION >= 4007
638 .affects_type_identity = false
642 static void register_attributes(void *event_data, void *data)
644 register_attribute(&randomize_layout_attr);
645 register_attribute(&no_randomize_layout_attr);
646 register_attribute(&randomize_considered_attr);
647 register_attribute(&randomize_performed_attr);
650 static void check_bad_casts_in_constructor(tree var, tree init)
652 unsigned HOST_WIDE_INT idx;
654 tree field_type, val_type;
656 FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) {
657 if (TREE_CODE(val) == CONSTRUCTOR) {
658 check_bad_casts_in_constructor(var, val);
662 /* pipacs' plugin creates franken-arrays that differ from those produced by
663 normal code which all have valid 'field' trees. work around this */
664 if (field == NULL_TREE)
666 field_type = TREE_TYPE(field);
667 val_type = TREE_TYPE(val);
669 if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE)
672 if (field_type == val_type)
675 field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type))));
676 val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type))));
678 if (field_type == void_type_node)
680 if (field_type == val_type)
682 if (TREE_CODE(val_type) != RECORD_TYPE)
685 if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type)))
687 MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type));
691 /* derived from the constify plugin */
692 static void check_global_variables(void *event_data, void *data)
694 struct varpool_node *node;
697 FOR_EACH_VARIABLE(node) {
698 tree var = NODE_DECL(node);
699 init = DECL_INITIAL(var);
700 if (init == NULL_TREE)
703 if (TREE_CODE(init) != CONSTRUCTOR)
706 check_bad_casts_in_constructor(var, init);
710 static bool dominated_by_is_err(const_tree rhs, basic_block bb)
716 const_tree poss_is_err_cond;
717 const_tree poss_is_err_func;
718 const_tree is_err_arg;
720 dom = get_immediate_dominator(CDI_DOMINATORS, bb);
724 dom_stmt = last_stmt(dom);
728 if (gimple_code(dom_stmt) != GIMPLE_COND)
731 if (gimple_cond_code(dom_stmt) != NE_EXPR)
734 if (!integer_zerop(gimple_cond_rhs(dom_stmt)))
737 poss_is_err_cond = gimple_cond_lhs(dom_stmt);
739 if (TREE_CODE(poss_is_err_cond) != SSA_NAME)
742 call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond);
744 if (gimple_code(call_stmt) != GIMPLE_CALL)
747 dom_lhs = gimple_get_lhs(call_stmt);
748 poss_is_err_func = gimple_call_fndecl(call_stmt);
749 if (!poss_is_err_func)
751 if (dom_lhs != poss_is_err_cond)
753 if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR"))
756 is_err_arg = gimple_call_arg(call_stmt, 0);
760 if (is_err_arg != rhs)
766 static void handle_local_var_initializers(void)
771 FOR_EACH_LOCAL_DECL(cfun, i, var) {
772 tree init = DECL_INITIAL(var);
775 if (TREE_CODE(init) != CONSTRUCTOR)
777 check_bad_casts_in_constructor(var, init);
781 static bool type_name_eq(gimple stmt, const_tree type_tree, const char *wanted_name)
783 const char *type_name;
785 if (type_tree == NULL_TREE)
788 switch (TREE_CODE(type_tree)) {
790 type_name = TYPE_NAME_POINTER(type_tree);
793 if (TYPE_PRECISION(type_tree) == CHAR_TYPE_SIZE)
796 INFORM(gimple_location(stmt), "found non-char INTEGER_TYPE cast comparison: %qT\n", type_tree);
797 debug_tree(type_tree);
802 if (TREE_CODE(TREE_TYPE(type_tree)) == VOID_TYPE) {
803 type_name = "void *";
806 INFORM(gimple_location(stmt), "found non-void POINTER_TYPE cast comparison %qT\n", type_tree);
807 debug_tree(type_tree);
811 INFORM(gimple_location(stmt), "unhandled cast comparison: %qT\n", type_tree);
812 debug_tree(type_tree);
816 return strcmp(type_name, wanted_name) == 0;
819 static bool whitelisted_cast(gimple stmt, const_tree lhs_tree, const_tree rhs_tree)
821 const struct whitelist_entry *entry;
822 expanded_location xloc = expand_location(gimple_location(stmt));
824 for (entry = whitelist; entry->pathname; entry++) {
825 if (!strstr(xloc.file, entry->pathname))
828 if (type_name_eq(stmt, lhs_tree, entry->lhs) && type_name_eq(stmt, rhs_tree, entry->rhs))
836 * iterate over all statements to find "bad" casts:
837 * those where the address of the start of a structure is cast
838 * to a pointer of a structure of a different type, or a
839 * structure pointer type is cast to a different structure pointer type
841 static unsigned int find_bad_casts_execute(void)
845 handle_local_var_initializers();
847 FOR_EACH_BB_FN(bb, cfun) {
848 gimple_stmt_iterator gsi;
850 for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
856 const_tree ptr_lhs_type;
857 const_tree ptr_rhs_type;
860 enum tree_code rhs_code;
862 stmt = gsi_stmt(gsi);
864 #ifdef __DEBUG_PLUGIN
865 #ifdef __DEBUG_VERBOSE
866 debug_gimple_stmt(stmt);
867 debug_tree(gimple_get_lhs(stmt));
871 if (gimple_code(stmt) != GIMPLE_ASSIGN)
874 #ifdef __DEBUG_PLUGIN
875 #ifdef __DEBUG_VERBOSE
876 debug_tree(gimple_assign_rhs1(stmt));
881 rhs_code = gimple_assign_rhs_code(stmt);
883 if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME)
886 lhs = gimple_get_lhs(stmt);
887 lhs_type = TREE_TYPE(lhs);
888 rhs1 = gimple_assign_rhs1(stmt);
889 rhs_type = TREE_TYPE(rhs1);
891 if (TREE_CODE(rhs_type) != POINTER_TYPE ||
892 TREE_CODE(lhs_type) != POINTER_TYPE)
895 ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type))));
896 ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type))));
898 if (ptr_rhs_type == void_type_node)
901 if (ptr_lhs_type == void_type_node)
904 if (dominated_by_is_err(rhs1, bb))
907 if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) {
908 #ifndef __DEBUG_PLUGIN
909 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type)))
912 if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type))
913 MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type);
918 if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type)
921 if (rhs_code == ADDR_EXPR) {
922 op0 = TREE_OPERAND(rhs1, 0);
924 if (op0 == NULL_TREE)
927 if (TREE_CODE(op0) != VAR_DECL)
930 op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0))));
931 if (op0_type == ptr_lhs_type)
934 #ifndef __DEBUG_PLUGIN
935 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type)))
938 if (!whitelisted_cast(stmt, ptr_lhs_type, op0_type))
939 MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type);
942 const_tree ssa_name_var = SSA_NAME_VAR(rhs1);
943 /* skip bogus type casts introduced by container_of */
944 if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) &&
945 !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr"))
947 #ifndef __DEBUG_PLUGIN
948 if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type)))
951 if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type))
952 MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type);
961 #define PASS_NAME find_bad_casts
963 #define TODO_FLAGS_FINISH TODO_dump_func
964 #include "gcc-generate-gimple-pass.h"
966 __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
969 const char * const plugin_name = plugin_info->base_name;
970 const int argc = plugin_info->argc;
971 const struct plugin_argument * const argv = plugin_info->argv;
973 int obtained_seed = 0;
974 struct register_pass_info find_bad_casts_pass_info;
976 find_bad_casts_pass_info.pass = make_find_bad_casts_pass();
977 find_bad_casts_pass_info.reference_pass_name = "ssa";
978 find_bad_casts_pass_info.ref_pass_instance_number = 1;
979 find_bad_casts_pass_info.pos_op = PASS_POS_INSERT_AFTER;
981 if (!plugin_default_version_check(version, &gcc_version)) {
982 error(G_("incompatible gcc/plugin versions"));
986 if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
987 inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
991 for (i = 0; i < argc; ++i) {
992 if (!strcmp(argv[i].key, "disable")) {
996 if (!strcmp(argv[i].key, "performance-mode")) {
997 performance_mode = 1;
1000 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
1003 if (strlen(randstruct_seed) != 64) {
1004 error(G_("invalid seed value supplied for %s plugin"), plugin_name);
1007 obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx",
1008 &shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]);
1009 if (obtained_seed != 4) {
1010 error(G_("Invalid seed supplied for %s plugin"), plugin_name);
1014 register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info);
1016 register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL);
1017 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info);
1018 register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
1019 register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL);
1021 register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);