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