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 = { 37313dd1b6SKees Cook .version = "201402201816vanilla", 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 struct whitelist_entry { 43313dd1b6SKees Cook const char *pathname; 44313dd1b6SKees Cook const char *lhs; 45313dd1b6SKees Cook const char *rhs; 46313dd1b6SKees Cook }; 47313dd1b6SKees Cook 48313dd1b6SKees Cook static const struct whitelist_entry whitelist[] = { 491854c19cSKees Cook /* NIU overloads mapping with page struct */ 501854c19cSKees Cook { "drivers/net/ethernet/sun/niu.c", "page", "address_space" }, 51b07b6584SKees Cook /* unix_skb_parms via UNIXCB() buffer */ 52b07b6584SKees Cook { "net/unix/af_unix.c", "unix_skb_parms", "char" }, 53802762cdSKees Cook /* big_key payload.data struct splashing */ 54802762cdSKees Cook { "security/keys/big_key.c", "path", "void *" }, 55df0ce173SSargun Dhillon /* walk struct security_hook_heads as an array of struct hlist_head */ 56df0ce173SSargun Dhillon { "security/security.c", "hlist_head", "security_hook_heads" }, 57313dd1b6SKees Cook { } 58313dd1b6SKees Cook }; 59313dd1b6SKees Cook 60313dd1b6SKees Cook /* from old Linux dcache.h */ 61313dd1b6SKees Cook static inline unsigned long 62313dd1b6SKees Cook partial_name_hash(unsigned long c, unsigned long prevhash) 63313dd1b6SKees Cook { 64313dd1b6SKees Cook return (prevhash + (c << 4) + (c >> 4)) * 11; 65313dd1b6SKees Cook } 66313dd1b6SKees Cook static inline unsigned int 67313dd1b6SKees Cook name_hash(const unsigned char *name) 68313dd1b6SKees Cook { 69313dd1b6SKees Cook unsigned long hash = 0; 70313dd1b6SKees Cook unsigned int len = strlen((const char *)name); 71313dd1b6SKees Cook while (len--) 72313dd1b6SKees Cook hash = partial_name_hash(*name++, hash); 73313dd1b6SKees Cook return (unsigned int)hash; 74313dd1b6SKees Cook } 75313dd1b6SKees Cook 76313dd1b6SKees Cook static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs) 77313dd1b6SKees Cook { 78313dd1b6SKees Cook tree type; 79313dd1b6SKees Cook 80313dd1b6SKees Cook *no_add_attrs = true; 81313dd1b6SKees Cook if (TREE_CODE(*node) == FUNCTION_DECL) { 82313dd1b6SKees Cook error("%qE attribute does not apply to functions (%qF)", name, *node); 83313dd1b6SKees Cook return NULL_TREE; 84313dd1b6SKees Cook } 85313dd1b6SKees Cook 86313dd1b6SKees Cook if (TREE_CODE(*node) == PARM_DECL) { 87313dd1b6SKees Cook error("%qE attribute does not apply to function parameters (%qD)", name, *node); 88313dd1b6SKees Cook return NULL_TREE; 89313dd1b6SKees Cook } 90313dd1b6SKees Cook 91313dd1b6SKees Cook if (TREE_CODE(*node) == VAR_DECL) { 92313dd1b6SKees Cook error("%qE attribute does not apply to variables (%qD)", name, *node); 93313dd1b6SKees Cook return NULL_TREE; 94313dd1b6SKees Cook } 95313dd1b6SKees Cook 96313dd1b6SKees Cook if (TYPE_P(*node)) { 97313dd1b6SKees Cook type = *node; 98313dd1b6SKees Cook } else { 99313dd1b6SKees Cook gcc_assert(TREE_CODE(*node) == TYPE_DECL); 100313dd1b6SKees Cook type = TREE_TYPE(*node); 101313dd1b6SKees Cook } 102313dd1b6SKees Cook 103313dd1b6SKees Cook if (TREE_CODE(type) != RECORD_TYPE) { 104313dd1b6SKees Cook error("%qE attribute used on %qT applies to struct types only", name, type); 105313dd1b6SKees Cook return NULL_TREE; 106313dd1b6SKees Cook } 107313dd1b6SKees Cook 108313dd1b6SKees Cook if (lookup_attribute(IDENTIFIER_POINTER(name), TYPE_ATTRIBUTES(type))) { 109313dd1b6SKees Cook error("%qE attribute is already applied to the type %qT", name, type); 110313dd1b6SKees Cook return NULL_TREE; 111313dd1b6SKees Cook } 112313dd1b6SKees Cook 113313dd1b6SKees Cook *no_add_attrs = false; 114313dd1b6SKees Cook 115313dd1b6SKees Cook return NULL_TREE; 116313dd1b6SKees Cook } 117313dd1b6SKees Cook 118313dd1b6SKees Cook /* set on complete types that we don't need to inspect further at all */ 119313dd1b6SKees Cook static tree handle_randomize_considered_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs) 120313dd1b6SKees Cook { 121313dd1b6SKees Cook *no_add_attrs = false; 122313dd1b6SKees Cook return NULL_TREE; 123313dd1b6SKees Cook } 124313dd1b6SKees Cook 125313dd1b6SKees Cook /* 126313dd1b6SKees Cook * set on types that we've performed a shuffle on, to prevent re-shuffling 127313dd1b6SKees Cook * this does not preclude us from inspecting its fields for potential shuffles 128313dd1b6SKees Cook */ 129313dd1b6SKees Cook static tree handle_randomize_performed_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs) 130313dd1b6SKees Cook { 131313dd1b6SKees Cook *no_add_attrs = false; 132313dd1b6SKees Cook return NULL_TREE; 133313dd1b6SKees Cook } 134313dd1b6SKees Cook 135313dd1b6SKees Cook /* 136313dd1b6SKees Cook * 64bit variant of Bob Jenkins' public domain PRNG 137313dd1b6SKees Cook * 256 bits of internal state 138313dd1b6SKees Cook */ 139313dd1b6SKees Cook 140313dd1b6SKees Cook typedef unsigned long long u64; 141313dd1b6SKees Cook 142313dd1b6SKees Cook typedef struct ranctx { u64 a; u64 b; u64 c; u64 d; } ranctx; 143313dd1b6SKees Cook 144313dd1b6SKees Cook #define rot(x,k) (((x)<<(k))|((x)>>(64-(k)))) 145313dd1b6SKees Cook static u64 ranval(ranctx *x) { 146313dd1b6SKees Cook u64 e = x->a - rot(x->b, 7); 147313dd1b6SKees Cook x->a = x->b ^ rot(x->c, 13); 148313dd1b6SKees Cook x->b = x->c + rot(x->d, 37); 149313dd1b6SKees Cook x->c = x->d + e; 150313dd1b6SKees Cook x->d = e + x->a; 151313dd1b6SKees Cook return x->d; 152313dd1b6SKees Cook } 153313dd1b6SKees Cook 154313dd1b6SKees Cook static void raninit(ranctx *x, u64 *seed) { 155313dd1b6SKees Cook int i; 156313dd1b6SKees Cook 157313dd1b6SKees Cook x->a = seed[0]; 158313dd1b6SKees Cook x->b = seed[1]; 159313dd1b6SKees Cook x->c = seed[2]; 160313dd1b6SKees Cook x->d = seed[3]; 161313dd1b6SKees Cook 162313dd1b6SKees Cook for (i=0; i < 30; ++i) 163313dd1b6SKees Cook (void)ranval(x); 164313dd1b6SKees Cook } 165313dd1b6SKees Cook 166313dd1b6SKees Cook static u64 shuffle_seed[4]; 167313dd1b6SKees Cook 168313dd1b6SKees Cook struct partition_group { 169313dd1b6SKees Cook tree tree_start; 170313dd1b6SKees Cook unsigned long start; 171313dd1b6SKees Cook unsigned long length; 172313dd1b6SKees Cook }; 173313dd1b6SKees Cook 174313dd1b6SKees Cook static void partition_struct(tree *fields, unsigned long length, struct partition_group *size_groups, unsigned long *num_groups) 175313dd1b6SKees Cook { 176313dd1b6SKees Cook unsigned long i; 177313dd1b6SKees Cook unsigned long accum_size = 0; 178313dd1b6SKees Cook unsigned long accum_length = 0; 179313dd1b6SKees Cook unsigned long group_idx = 0; 180313dd1b6SKees Cook 181313dd1b6SKees Cook gcc_assert(length < INT_MAX); 182313dd1b6SKees Cook 183313dd1b6SKees Cook memset(size_groups, 0, sizeof(struct partition_group) * length); 184313dd1b6SKees Cook 185313dd1b6SKees Cook for (i = 0; i < length; i++) { 186313dd1b6SKees Cook if (size_groups[group_idx].tree_start == NULL_TREE) { 187313dd1b6SKees Cook size_groups[group_idx].tree_start = fields[i]; 188313dd1b6SKees Cook size_groups[group_idx].start = i; 189313dd1b6SKees Cook accum_length = 0; 190313dd1b6SKees Cook accum_size = 0; 191313dd1b6SKees Cook } 192313dd1b6SKees Cook accum_size += (unsigned long)int_size_in_bytes(TREE_TYPE(fields[i])); 193313dd1b6SKees Cook accum_length++; 194313dd1b6SKees Cook if (accum_size >= 64) { 195313dd1b6SKees Cook size_groups[group_idx].length = accum_length; 196313dd1b6SKees Cook accum_length = 0; 197313dd1b6SKees Cook group_idx++; 198313dd1b6SKees Cook } 199313dd1b6SKees Cook } 200313dd1b6SKees Cook 201313dd1b6SKees Cook if (size_groups[group_idx].tree_start != NULL_TREE && 202313dd1b6SKees Cook !size_groups[group_idx].length) { 203313dd1b6SKees Cook size_groups[group_idx].length = accum_length; 204313dd1b6SKees Cook group_idx++; 205313dd1b6SKees Cook } 206313dd1b6SKees Cook 207313dd1b6SKees Cook *num_groups = group_idx; 208313dd1b6SKees Cook } 209313dd1b6SKees Cook 210313dd1b6SKees Cook static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state) 211313dd1b6SKees Cook { 212313dd1b6SKees Cook unsigned long i, x; 213313dd1b6SKees Cook struct partition_group size_group[length]; 214313dd1b6SKees Cook unsigned long num_groups = 0; 215313dd1b6SKees Cook unsigned long randnum; 216313dd1b6SKees Cook 217313dd1b6SKees Cook partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups); 218313dd1b6SKees Cook for (i = num_groups - 1; i > 0; i--) { 219313dd1b6SKees Cook struct partition_group tmp; 220313dd1b6SKees Cook randnum = ranval(prng_state) % (i + 1); 221313dd1b6SKees Cook tmp = size_group[i]; 222313dd1b6SKees Cook size_group[i] = size_group[randnum]; 223313dd1b6SKees Cook size_group[randnum] = tmp; 224313dd1b6SKees Cook } 225313dd1b6SKees Cook 226313dd1b6SKees Cook for (x = 0; x < num_groups; x++) { 227313dd1b6SKees Cook for (i = size_group[x].start + size_group[x].length - 1; i > size_group[x].start; i--) { 228313dd1b6SKees Cook tree tmp; 229313dd1b6SKees Cook if (DECL_BIT_FIELD_TYPE(newtree[i])) 230313dd1b6SKees Cook continue; 231313dd1b6SKees Cook randnum = ranval(prng_state) % (i + 1); 232313dd1b6SKees Cook // we could handle this case differently if desired 233313dd1b6SKees Cook if (DECL_BIT_FIELD_TYPE(newtree[randnum])) 234313dd1b6SKees Cook continue; 235313dd1b6SKees Cook tmp = newtree[i]; 236313dd1b6SKees Cook newtree[i] = newtree[randnum]; 237313dd1b6SKees Cook newtree[randnum] = tmp; 238313dd1b6SKees Cook } 239313dd1b6SKees Cook } 240313dd1b6SKees Cook } 241313dd1b6SKees Cook 242313dd1b6SKees Cook static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state) 243313dd1b6SKees Cook { 244313dd1b6SKees Cook unsigned long i, randnum; 245313dd1b6SKees Cook 246313dd1b6SKees Cook for (i = length - 1; i > 0; i--) { 247313dd1b6SKees Cook tree tmp; 248313dd1b6SKees Cook randnum = ranval(prng_state) % (i + 1); 249313dd1b6SKees Cook tmp = newtree[i]; 250313dd1b6SKees Cook newtree[i] = newtree[randnum]; 251313dd1b6SKees Cook newtree[randnum] = tmp; 252313dd1b6SKees Cook } 253313dd1b6SKees Cook } 254313dd1b6SKees Cook 255313dd1b6SKees Cook /* modern in-place Fisher-Yates shuffle */ 256313dd1b6SKees Cook static void shuffle(const_tree type, tree *newtree, unsigned long length) 257313dd1b6SKees Cook { 258313dd1b6SKees Cook unsigned long i; 259313dd1b6SKees Cook u64 seed[4]; 260313dd1b6SKees Cook ranctx prng_state; 261313dd1b6SKees Cook const unsigned char *structname; 262313dd1b6SKees Cook 263313dd1b6SKees Cook if (length == 0) 264313dd1b6SKees Cook return; 265313dd1b6SKees Cook 266313dd1b6SKees Cook gcc_assert(TREE_CODE(type) == RECORD_TYPE); 267313dd1b6SKees Cook 268313dd1b6SKees Cook structname = ORIG_TYPE_NAME(type); 269313dd1b6SKees Cook 270313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN 271313dd1b6SKees Cook fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type); 272313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE 273313dd1b6SKees Cook debug_tree((tree)type); 274313dd1b6SKees Cook #endif 275313dd1b6SKees Cook #endif 276313dd1b6SKees Cook 277313dd1b6SKees Cook for (i = 0; i < 4; i++) { 278313dd1b6SKees Cook seed[i] = shuffle_seed[i]; 279313dd1b6SKees Cook seed[i] ^= name_hash(structname); 280313dd1b6SKees Cook } 281313dd1b6SKees Cook 282313dd1b6SKees Cook raninit(&prng_state, (u64 *)&seed); 283313dd1b6SKees Cook 284313dd1b6SKees Cook if (performance_mode) 285313dd1b6SKees Cook performance_shuffle(newtree, length, &prng_state); 286313dd1b6SKees Cook else 287313dd1b6SKees Cook full_shuffle(newtree, length, &prng_state); 288313dd1b6SKees Cook } 289313dd1b6SKees Cook 290313dd1b6SKees Cook static bool is_flexible_array(const_tree field) 291313dd1b6SKees Cook { 292313dd1b6SKees Cook const_tree fieldtype; 293313dd1b6SKees Cook const_tree typesize; 294313dd1b6SKees Cook const_tree elemtype; 295313dd1b6SKees Cook const_tree elemsize; 296313dd1b6SKees Cook 297313dd1b6SKees Cook fieldtype = TREE_TYPE(field); 298313dd1b6SKees Cook typesize = TYPE_SIZE(fieldtype); 299313dd1b6SKees Cook 300313dd1b6SKees Cook if (TREE_CODE(fieldtype) != ARRAY_TYPE) 301313dd1b6SKees Cook return false; 302313dd1b6SKees Cook 303313dd1b6SKees Cook elemtype = TREE_TYPE(fieldtype); 304313dd1b6SKees Cook elemsize = TYPE_SIZE(elemtype); 305313dd1b6SKees Cook 306313dd1b6SKees Cook /* size of type is represented in bits */ 307313dd1b6SKees Cook 308313dd1b6SKees Cook if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE && 309313dd1b6SKees Cook TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE) 310313dd1b6SKees Cook return true; 311313dd1b6SKees Cook 312313dd1b6SKees Cook if (typesize != NULL_TREE && 313313dd1b6SKees Cook (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) || 314313dd1b6SKees Cook tree_to_uhwi(typesize) == tree_to_uhwi(elemsize)))) 315313dd1b6SKees Cook return true; 316313dd1b6SKees Cook 317313dd1b6SKees Cook return false; 318313dd1b6SKees Cook } 319313dd1b6SKees Cook 320313dd1b6SKees Cook static int relayout_struct(tree type) 321313dd1b6SKees Cook { 322313dd1b6SKees Cook unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type)); 323313dd1b6SKees Cook unsigned long shuffle_length = num_fields; 324313dd1b6SKees Cook tree field; 325313dd1b6SKees Cook tree newtree[num_fields]; 326313dd1b6SKees Cook unsigned long i; 327313dd1b6SKees Cook tree list; 328313dd1b6SKees Cook tree variant; 329313dd1b6SKees Cook tree main_variant; 330313dd1b6SKees Cook expanded_location xloc; 331313dd1b6SKees Cook bool has_flexarray = false; 332313dd1b6SKees Cook 333313dd1b6SKees Cook if (TYPE_FIELDS(type) == NULL_TREE) 334313dd1b6SKees Cook return 0; 335313dd1b6SKees Cook 336313dd1b6SKees Cook if (num_fields < 2) 337313dd1b6SKees Cook return 0; 338313dd1b6SKees Cook 339313dd1b6SKees Cook gcc_assert(TREE_CODE(type) == RECORD_TYPE); 340313dd1b6SKees Cook 341313dd1b6SKees Cook gcc_assert(num_fields < INT_MAX); 342313dd1b6SKees Cook 343313dd1b6SKees Cook if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) || 344313dd1b6SKees Cook lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type)))) 345313dd1b6SKees Cook return 0; 346313dd1b6SKees Cook 347313dd1b6SKees Cook /* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */ 348313dd1b6SKees Cook if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") || 349313dd1b6SKees Cook !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY")) 350313dd1b6SKees Cook return 0; 351313dd1b6SKees Cook 352313dd1b6SKees Cook /* throw out any structs in uapi */ 353313dd1b6SKees Cook xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type))); 354313dd1b6SKees Cook 355313dd1b6SKees Cook if (strstr(xloc.file, "/uapi/")) 356313dd1b6SKees Cook error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type)); 357313dd1b6SKees Cook 358313dd1b6SKees Cook for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) { 359313dd1b6SKees Cook gcc_assert(TREE_CODE(field) == FIELD_DECL); 360313dd1b6SKees Cook newtree[i] = field; 361313dd1b6SKees Cook } 362313dd1b6SKees Cook 363313dd1b6SKees Cook /* 364313dd1b6SKees Cook * enforce that we don't randomize the layout of the last 365313dd1b6SKees Cook * element of a struct if it's a 0 or 1-length array 366313dd1b6SKees Cook * or a proper flexible array 367313dd1b6SKees Cook */ 368313dd1b6SKees Cook if (is_flexible_array(newtree[num_fields - 1])) { 369313dd1b6SKees Cook has_flexarray = true; 370313dd1b6SKees Cook shuffle_length--; 371313dd1b6SKees Cook } 372313dd1b6SKees Cook 373313dd1b6SKees Cook shuffle(type, (tree *)newtree, shuffle_length); 374313dd1b6SKees Cook 375313dd1b6SKees Cook /* 376313dd1b6SKees Cook * set up a bogus anonymous struct field designed to error out on unnamed struct initializers 377313dd1b6SKees Cook * as gcc provides no other way to detect such code 378313dd1b6SKees Cook */ 379313dd1b6SKees Cook list = make_node(FIELD_DECL); 380313dd1b6SKees Cook TREE_CHAIN(list) = newtree[0]; 381313dd1b6SKees Cook TREE_TYPE(list) = void_type_node; 382313dd1b6SKees Cook DECL_SIZE(list) = bitsize_zero_node; 383313dd1b6SKees Cook DECL_NONADDRESSABLE_P(list) = 1; 384313dd1b6SKees Cook DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node; 385313dd1b6SKees Cook DECL_SIZE_UNIT(list) = size_zero_node; 386313dd1b6SKees Cook DECL_FIELD_OFFSET(list) = size_zero_node; 387313dd1b6SKees Cook DECL_CONTEXT(list) = type; 388313dd1b6SKees Cook // to satisfy the constify plugin 389313dd1b6SKees Cook TREE_READONLY(list) = 1; 390313dd1b6SKees Cook 391313dd1b6SKees Cook for (i = 0; i < num_fields - 1; i++) 392313dd1b6SKees Cook TREE_CHAIN(newtree[i]) = newtree[i+1]; 393313dd1b6SKees Cook TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE; 394313dd1b6SKees Cook 395313dd1b6SKees Cook main_variant = TYPE_MAIN_VARIANT(type); 396313dd1b6SKees Cook for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) { 397313dd1b6SKees Cook TYPE_FIELDS(variant) = list; 398313dd1b6SKees Cook TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant)); 399313dd1b6SKees Cook TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant)); 400313dd1b6SKees Cook TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant)); 401313dd1b6SKees Cook if (has_flexarray) 402313dd1b6SKees Cook TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type)); 403313dd1b6SKees Cook } 404313dd1b6SKees Cook 405313dd1b6SKees Cook /* 406313dd1b6SKees Cook * force a re-layout of the main variant 407313dd1b6SKees Cook * the TYPE_SIZE for all variants will be recomputed 408313dd1b6SKees Cook * by finalize_type_size() 409313dd1b6SKees Cook */ 410313dd1b6SKees Cook TYPE_SIZE(main_variant) = NULL_TREE; 411313dd1b6SKees Cook layout_type(main_variant); 412313dd1b6SKees Cook gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE); 413313dd1b6SKees Cook 414313dd1b6SKees Cook return 1; 415313dd1b6SKees Cook } 416313dd1b6SKees Cook 417313dd1b6SKees Cook /* from constify plugin */ 418313dd1b6SKees Cook static const_tree get_field_type(const_tree field) 419313dd1b6SKees Cook { 420313dd1b6SKees Cook return strip_array_types(TREE_TYPE(field)); 421313dd1b6SKees Cook } 422313dd1b6SKees Cook 423313dd1b6SKees Cook /* from constify plugin */ 424313dd1b6SKees Cook static bool is_fptr(const_tree fieldtype) 425313dd1b6SKees Cook { 426313dd1b6SKees Cook if (TREE_CODE(fieldtype) != POINTER_TYPE) 427313dd1b6SKees Cook return false; 428313dd1b6SKees Cook 429313dd1b6SKees Cook return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE; 430313dd1b6SKees Cook } 431313dd1b6SKees Cook 432313dd1b6SKees Cook /* derived from constify plugin */ 433313dd1b6SKees Cook static int is_pure_ops_struct(const_tree node) 434313dd1b6SKees Cook { 435313dd1b6SKees Cook const_tree field; 436313dd1b6SKees Cook 437313dd1b6SKees Cook gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE); 438313dd1b6SKees Cook 439313dd1b6SKees Cook for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) { 440313dd1b6SKees Cook const_tree fieldtype = get_field_type(field); 441313dd1b6SKees Cook enum tree_code code = TREE_CODE(fieldtype); 442313dd1b6SKees Cook 443313dd1b6SKees Cook if (node == fieldtype) 444313dd1b6SKees Cook continue; 445313dd1b6SKees Cook 446*60f2c82eSJoonwon Kang if (code == RECORD_TYPE || code == UNION_TYPE) { 447313dd1b6SKees Cook if (!is_pure_ops_struct(fieldtype)) 448313dd1b6SKees Cook return 0; 449*60f2c82eSJoonwon Kang continue; 450*60f2c82eSJoonwon Kang } 451*60f2c82eSJoonwon Kang 452*60f2c82eSJoonwon Kang if (!is_fptr(fieldtype)) 453*60f2c82eSJoonwon Kang return 0; 454313dd1b6SKees Cook } 455313dd1b6SKees Cook 456313dd1b6SKees Cook return 1; 457313dd1b6SKees Cook } 458313dd1b6SKees Cook 459313dd1b6SKees Cook static void randomize_type(tree type) 460313dd1b6SKees Cook { 461313dd1b6SKees Cook tree variant; 462313dd1b6SKees Cook 463313dd1b6SKees Cook gcc_assert(TREE_CODE(type) == RECORD_TYPE); 464313dd1b6SKees Cook 465313dd1b6SKees Cook if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type))) 466313dd1b6SKees Cook return; 467313dd1b6SKees Cook 468313dd1b6SKees Cook if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type)) 469313dd1b6SKees Cook relayout_struct(type); 470313dd1b6SKees Cook 471313dd1b6SKees Cook for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) { 472313dd1b6SKees Cook TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type)); 473313dd1b6SKees Cook TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type)); 474313dd1b6SKees Cook } 475313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN 476313dd1b6SKees Cook fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type)); 477313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE 478313dd1b6SKees Cook debug_tree(type); 479313dd1b6SKees Cook #endif 480313dd1b6SKees Cook #endif 481313dd1b6SKees Cook } 482313dd1b6SKees Cook 483313dd1b6SKees Cook static void update_decl_size(tree decl) 484313dd1b6SKees Cook { 485313dd1b6SKees Cook tree lastval, lastidx, field, init, type, flexsize; 486313dd1b6SKees Cook unsigned HOST_WIDE_INT len; 487313dd1b6SKees Cook 488313dd1b6SKees Cook type = TREE_TYPE(decl); 489313dd1b6SKees Cook 490313dd1b6SKees Cook if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type))) 491313dd1b6SKees Cook return; 492313dd1b6SKees Cook 493313dd1b6SKees Cook init = DECL_INITIAL(decl); 494313dd1b6SKees Cook if (init == NULL_TREE || init == error_mark_node) 495313dd1b6SKees Cook return; 496313dd1b6SKees Cook 497313dd1b6SKees Cook if (TREE_CODE(init) != CONSTRUCTOR) 498313dd1b6SKees Cook return; 499313dd1b6SKees Cook 500313dd1b6SKees Cook len = CONSTRUCTOR_NELTS(init); 501313dd1b6SKees Cook if (!len) 502313dd1b6SKees Cook return; 503313dd1b6SKees Cook 504313dd1b6SKees Cook lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value; 505313dd1b6SKees Cook lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index; 506313dd1b6SKees Cook 507313dd1b6SKees Cook for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field)) 508313dd1b6SKees Cook ; 509313dd1b6SKees Cook 510313dd1b6SKees Cook if (lastidx != field) 511313dd1b6SKees Cook return; 512313dd1b6SKees Cook 513313dd1b6SKees Cook if (TREE_CODE(lastval) != STRING_CST) { 514313dd1b6SKees Cook error("Only string constants are supported as initializers " 515313dd1b6SKees Cook "for randomized structures with flexible arrays"); 516313dd1b6SKees Cook return; 517313dd1b6SKees Cook } 518313dd1b6SKees Cook 519313dd1b6SKees Cook flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) * 520313dd1b6SKees Cook tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval))))); 521313dd1b6SKees Cook 522313dd1b6SKees Cook DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize); 523313dd1b6SKees Cook 524313dd1b6SKees Cook return; 525313dd1b6SKees Cook } 526313dd1b6SKees Cook 527313dd1b6SKees Cook 528313dd1b6SKees Cook static void randomize_layout_finish_decl(void *event_data, void *data) 529313dd1b6SKees Cook { 530313dd1b6SKees Cook tree decl = (tree)event_data; 531313dd1b6SKees Cook tree type; 532313dd1b6SKees Cook 533313dd1b6SKees Cook if (decl == NULL_TREE || decl == error_mark_node) 534313dd1b6SKees Cook return; 535313dd1b6SKees Cook 536313dd1b6SKees Cook type = TREE_TYPE(decl); 537313dd1b6SKees Cook 538313dd1b6SKees Cook if (TREE_CODE(decl) != VAR_DECL) 539313dd1b6SKees Cook return; 540313dd1b6SKees Cook 541313dd1b6SKees Cook if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE) 542313dd1b6SKees Cook return; 543313dd1b6SKees Cook 544313dd1b6SKees Cook if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type))) 545313dd1b6SKees Cook return; 546313dd1b6SKees Cook 547313dd1b6SKees Cook DECL_SIZE(decl) = 0; 548313dd1b6SKees Cook DECL_SIZE_UNIT(decl) = 0; 549313dd1b6SKees Cook SET_DECL_ALIGN(decl, 0); 550313dd1b6SKees Cook SET_DECL_MODE (decl, VOIDmode); 551313dd1b6SKees Cook SET_DECL_RTL(decl, 0); 552313dd1b6SKees Cook update_decl_size(decl); 553313dd1b6SKees Cook layout_decl(decl, 0); 554313dd1b6SKees Cook } 555313dd1b6SKees Cook 556313dd1b6SKees Cook static void finish_type(void *event_data, void *data) 557313dd1b6SKees Cook { 558313dd1b6SKees Cook tree type = (tree)event_data; 559313dd1b6SKees Cook 560313dd1b6SKees Cook if (type == NULL_TREE || type == error_mark_node) 561313dd1b6SKees Cook return; 562313dd1b6SKees Cook 563313dd1b6SKees Cook if (TREE_CODE(type) != RECORD_TYPE) 564313dd1b6SKees Cook return; 565313dd1b6SKees Cook 566313dd1b6SKees Cook if (TYPE_FIELDS(type) == NULL_TREE) 567313dd1b6SKees Cook return; 568313dd1b6SKees Cook 569313dd1b6SKees Cook if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type))) 570313dd1b6SKees Cook return; 571313dd1b6SKees Cook 572313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN 573313dd1b6SKees Cook fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type)); 574313dd1b6SKees Cook #endif 575313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE 576313dd1b6SKees Cook debug_tree(type); 577313dd1b6SKees Cook #endif 578313dd1b6SKees Cook randomize_type(type); 579313dd1b6SKees Cook 580313dd1b6SKees Cook return; 581313dd1b6SKees Cook } 582313dd1b6SKees Cook 583b8672910SKees Cook static struct attribute_spec randomize_layout_attr = { }; 584b8672910SKees Cook static struct attribute_spec no_randomize_layout_attr = { }; 585b8672910SKees Cook static struct attribute_spec randomize_considered_attr = { }; 586b8672910SKees Cook static struct attribute_spec randomize_performed_attr = { }; 587313dd1b6SKees Cook 588313dd1b6SKees Cook static void register_attributes(void *event_data, void *data) 589313dd1b6SKees Cook { 590b8672910SKees Cook randomize_layout_attr.name = "randomize_layout"; 591b8672910SKees Cook randomize_layout_attr.type_required = true; 592b8672910SKees Cook randomize_layout_attr.handler = handle_randomize_layout_attr; 593b8672910SKees Cook #if BUILDING_GCC_VERSION >= 4007 594b8672910SKees Cook randomize_layout_attr.affects_type_identity = true; 595b8672910SKees Cook #endif 596b8672910SKees Cook 597b8672910SKees Cook no_randomize_layout_attr.name = "no_randomize_layout"; 598b8672910SKees Cook no_randomize_layout_attr.type_required = true; 599b8672910SKees Cook no_randomize_layout_attr.handler = handle_randomize_layout_attr; 600b8672910SKees Cook #if BUILDING_GCC_VERSION >= 4007 601b8672910SKees Cook no_randomize_layout_attr.affects_type_identity = true; 602b8672910SKees Cook #endif 603b8672910SKees Cook 604b8672910SKees Cook randomize_considered_attr.name = "randomize_considered"; 605b8672910SKees Cook randomize_considered_attr.type_required = true; 606b8672910SKees Cook randomize_considered_attr.handler = handle_randomize_considered_attr; 607b8672910SKees Cook 608b8672910SKees Cook randomize_performed_attr.name = "randomize_performed"; 609b8672910SKees Cook randomize_performed_attr.type_required = true; 610b8672910SKees Cook randomize_performed_attr.handler = handle_randomize_performed_attr; 611b8672910SKees Cook 612313dd1b6SKees Cook register_attribute(&randomize_layout_attr); 613313dd1b6SKees Cook register_attribute(&no_randomize_layout_attr); 614313dd1b6SKees Cook register_attribute(&randomize_considered_attr); 615313dd1b6SKees Cook register_attribute(&randomize_performed_attr); 616313dd1b6SKees Cook } 617313dd1b6SKees Cook 618313dd1b6SKees Cook static void check_bad_casts_in_constructor(tree var, tree init) 619313dd1b6SKees Cook { 620313dd1b6SKees Cook unsigned HOST_WIDE_INT idx; 621313dd1b6SKees Cook tree field, val; 622313dd1b6SKees Cook tree field_type, val_type; 623313dd1b6SKees Cook 624313dd1b6SKees Cook FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) { 625313dd1b6SKees Cook if (TREE_CODE(val) == CONSTRUCTOR) { 626313dd1b6SKees Cook check_bad_casts_in_constructor(var, val); 627313dd1b6SKees Cook continue; 628313dd1b6SKees Cook } 629313dd1b6SKees Cook 630313dd1b6SKees Cook /* pipacs' plugin creates franken-arrays that differ from those produced by 631313dd1b6SKees Cook normal code which all have valid 'field' trees. work around this */ 632313dd1b6SKees Cook if (field == NULL_TREE) 633313dd1b6SKees Cook continue; 634313dd1b6SKees Cook field_type = TREE_TYPE(field); 635313dd1b6SKees Cook val_type = TREE_TYPE(val); 636313dd1b6SKees Cook 637313dd1b6SKees Cook if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE) 638313dd1b6SKees Cook continue; 639313dd1b6SKees Cook 640313dd1b6SKees Cook if (field_type == val_type) 641313dd1b6SKees Cook continue; 642313dd1b6SKees Cook 643313dd1b6SKees Cook field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type)))); 644313dd1b6SKees Cook val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type)))); 645313dd1b6SKees Cook 646313dd1b6SKees Cook if (field_type == void_type_node) 647313dd1b6SKees Cook continue; 648313dd1b6SKees Cook if (field_type == val_type) 649313dd1b6SKees Cook continue; 650313dd1b6SKees Cook if (TREE_CODE(val_type) != RECORD_TYPE) 651313dd1b6SKees Cook continue; 652313dd1b6SKees Cook 653313dd1b6SKees Cook if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type))) 654313dd1b6SKees Cook continue; 655313dd1b6SKees Cook MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type)); 656313dd1b6SKees Cook } 657313dd1b6SKees Cook } 658313dd1b6SKees Cook 659313dd1b6SKees Cook /* derived from the constify plugin */ 660313dd1b6SKees Cook static void check_global_variables(void *event_data, void *data) 661313dd1b6SKees Cook { 662313dd1b6SKees Cook struct varpool_node *node; 663313dd1b6SKees Cook tree init; 664313dd1b6SKees Cook 665313dd1b6SKees Cook FOR_EACH_VARIABLE(node) { 666313dd1b6SKees Cook tree var = NODE_DECL(node); 667313dd1b6SKees Cook init = DECL_INITIAL(var); 668313dd1b6SKees Cook if (init == NULL_TREE) 669313dd1b6SKees Cook continue; 670313dd1b6SKees Cook 671313dd1b6SKees Cook if (TREE_CODE(init) != CONSTRUCTOR) 672313dd1b6SKees Cook continue; 673313dd1b6SKees Cook 674313dd1b6SKees Cook check_bad_casts_in_constructor(var, init); 675313dd1b6SKees Cook } 676313dd1b6SKees Cook } 677313dd1b6SKees Cook 678313dd1b6SKees Cook static bool dominated_by_is_err(const_tree rhs, basic_block bb) 679313dd1b6SKees Cook { 680313dd1b6SKees Cook basic_block dom; 681313dd1b6SKees Cook gimple dom_stmt; 682313dd1b6SKees Cook gimple call_stmt; 683313dd1b6SKees Cook const_tree dom_lhs; 684313dd1b6SKees Cook const_tree poss_is_err_cond; 685313dd1b6SKees Cook const_tree poss_is_err_func; 686313dd1b6SKees Cook const_tree is_err_arg; 687313dd1b6SKees Cook 688313dd1b6SKees Cook dom = get_immediate_dominator(CDI_DOMINATORS, bb); 689313dd1b6SKees Cook if (!dom) 690313dd1b6SKees Cook return false; 691313dd1b6SKees Cook 692313dd1b6SKees Cook dom_stmt = last_stmt(dom); 693313dd1b6SKees Cook if (!dom_stmt) 694313dd1b6SKees Cook return false; 695313dd1b6SKees Cook 696313dd1b6SKees Cook if (gimple_code(dom_stmt) != GIMPLE_COND) 697313dd1b6SKees Cook return false; 698313dd1b6SKees Cook 699313dd1b6SKees Cook if (gimple_cond_code(dom_stmt) != NE_EXPR) 700313dd1b6SKees Cook return false; 701313dd1b6SKees Cook 702313dd1b6SKees Cook if (!integer_zerop(gimple_cond_rhs(dom_stmt))) 703313dd1b6SKees Cook return false; 704313dd1b6SKees Cook 705313dd1b6SKees Cook poss_is_err_cond = gimple_cond_lhs(dom_stmt); 706313dd1b6SKees Cook 707313dd1b6SKees Cook if (TREE_CODE(poss_is_err_cond) != SSA_NAME) 708313dd1b6SKees Cook return false; 709313dd1b6SKees Cook 710313dd1b6SKees Cook call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond); 711313dd1b6SKees Cook 712313dd1b6SKees Cook if (gimple_code(call_stmt) != GIMPLE_CALL) 713313dd1b6SKees Cook return false; 714313dd1b6SKees Cook 715313dd1b6SKees Cook dom_lhs = gimple_get_lhs(call_stmt); 716313dd1b6SKees Cook poss_is_err_func = gimple_call_fndecl(call_stmt); 717313dd1b6SKees Cook if (!poss_is_err_func) 718313dd1b6SKees Cook return false; 719313dd1b6SKees Cook if (dom_lhs != poss_is_err_cond) 720313dd1b6SKees Cook return false; 721313dd1b6SKees Cook if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR")) 722313dd1b6SKees Cook return false; 723313dd1b6SKees Cook 724313dd1b6SKees Cook is_err_arg = gimple_call_arg(call_stmt, 0); 725313dd1b6SKees Cook if (!is_err_arg) 726313dd1b6SKees Cook return false; 727313dd1b6SKees Cook 728313dd1b6SKees Cook if (is_err_arg != rhs) 729313dd1b6SKees Cook return false; 730313dd1b6SKees Cook 731313dd1b6SKees Cook return true; 732313dd1b6SKees Cook } 733313dd1b6SKees Cook 734313dd1b6SKees Cook static void handle_local_var_initializers(void) 735313dd1b6SKees Cook { 736313dd1b6SKees Cook tree var; 737313dd1b6SKees Cook unsigned int i; 738313dd1b6SKees Cook 739313dd1b6SKees Cook FOR_EACH_LOCAL_DECL(cfun, i, var) { 740313dd1b6SKees Cook tree init = DECL_INITIAL(var); 741313dd1b6SKees Cook if (!init) 742313dd1b6SKees Cook continue; 743313dd1b6SKees Cook if (TREE_CODE(init) != CONSTRUCTOR) 744313dd1b6SKees Cook continue; 745313dd1b6SKees Cook check_bad_casts_in_constructor(var, init); 746313dd1b6SKees Cook } 747313dd1b6SKees Cook } 748313dd1b6SKees Cook 749313dd1b6SKees Cook static bool type_name_eq(gimple stmt, const_tree type_tree, const char *wanted_name) 750313dd1b6SKees Cook { 751313dd1b6SKees Cook const char *type_name; 752313dd1b6SKees Cook 753313dd1b6SKees Cook if (type_tree == NULL_TREE) 754313dd1b6SKees Cook return false; 755313dd1b6SKees Cook 756313dd1b6SKees Cook switch (TREE_CODE(type_tree)) { 757313dd1b6SKees Cook case RECORD_TYPE: 758313dd1b6SKees Cook type_name = TYPE_NAME_POINTER(type_tree); 759313dd1b6SKees Cook break; 760313dd1b6SKees Cook case INTEGER_TYPE: 761313dd1b6SKees Cook if (TYPE_PRECISION(type_tree) == CHAR_TYPE_SIZE) 762313dd1b6SKees Cook type_name = "char"; 763313dd1b6SKees Cook else { 764313dd1b6SKees Cook INFORM(gimple_location(stmt), "found non-char INTEGER_TYPE cast comparison: %qT\n", type_tree); 765313dd1b6SKees Cook debug_tree(type_tree); 766313dd1b6SKees Cook return false; 767313dd1b6SKees Cook } 768313dd1b6SKees Cook break; 769313dd1b6SKees Cook case POINTER_TYPE: 770313dd1b6SKees Cook if (TREE_CODE(TREE_TYPE(type_tree)) == VOID_TYPE) { 771313dd1b6SKees Cook type_name = "void *"; 772313dd1b6SKees Cook break; 773313dd1b6SKees Cook } else { 774313dd1b6SKees Cook INFORM(gimple_location(stmt), "found non-void POINTER_TYPE cast comparison %qT\n", type_tree); 775313dd1b6SKees Cook debug_tree(type_tree); 776313dd1b6SKees Cook return false; 777313dd1b6SKees Cook } 778313dd1b6SKees Cook default: 779313dd1b6SKees Cook INFORM(gimple_location(stmt), "unhandled cast comparison: %qT\n", type_tree); 780313dd1b6SKees Cook debug_tree(type_tree); 781313dd1b6SKees Cook return false; 782313dd1b6SKees Cook } 783313dd1b6SKees Cook 784313dd1b6SKees Cook return strcmp(type_name, wanted_name) == 0; 785313dd1b6SKees Cook } 786313dd1b6SKees Cook 787313dd1b6SKees Cook static bool whitelisted_cast(gimple stmt, const_tree lhs_tree, const_tree rhs_tree) 788313dd1b6SKees Cook { 789313dd1b6SKees Cook const struct whitelist_entry *entry; 790313dd1b6SKees Cook expanded_location xloc = expand_location(gimple_location(stmt)); 791313dd1b6SKees Cook 792313dd1b6SKees Cook for (entry = whitelist; entry->pathname; entry++) { 793313dd1b6SKees Cook if (!strstr(xloc.file, entry->pathname)) 794313dd1b6SKees Cook continue; 795313dd1b6SKees Cook 796313dd1b6SKees Cook if (type_name_eq(stmt, lhs_tree, entry->lhs) && type_name_eq(stmt, rhs_tree, entry->rhs)) 797313dd1b6SKees Cook return true; 798313dd1b6SKees Cook } 799313dd1b6SKees Cook 800313dd1b6SKees Cook return false; 801313dd1b6SKees Cook } 802313dd1b6SKees Cook 803313dd1b6SKees Cook /* 804313dd1b6SKees Cook * iterate over all statements to find "bad" casts: 805313dd1b6SKees Cook * those where the address of the start of a structure is cast 806313dd1b6SKees Cook * to a pointer of a structure of a different type, or a 807313dd1b6SKees Cook * structure pointer type is cast to a different structure pointer type 808313dd1b6SKees Cook */ 809313dd1b6SKees Cook static unsigned int find_bad_casts_execute(void) 810313dd1b6SKees Cook { 811313dd1b6SKees Cook basic_block bb; 812313dd1b6SKees Cook 813313dd1b6SKees Cook handle_local_var_initializers(); 814313dd1b6SKees Cook 815313dd1b6SKees Cook FOR_EACH_BB_FN(bb, cfun) { 816313dd1b6SKees Cook gimple_stmt_iterator gsi; 817313dd1b6SKees Cook 818313dd1b6SKees Cook for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) { 819313dd1b6SKees Cook gimple stmt; 820313dd1b6SKees Cook const_tree lhs; 821313dd1b6SKees Cook const_tree lhs_type; 822313dd1b6SKees Cook const_tree rhs1; 823313dd1b6SKees Cook const_tree rhs_type; 824313dd1b6SKees Cook const_tree ptr_lhs_type; 825313dd1b6SKees Cook const_tree ptr_rhs_type; 826313dd1b6SKees Cook const_tree op0; 827313dd1b6SKees Cook const_tree op0_type; 828313dd1b6SKees Cook enum tree_code rhs_code; 829313dd1b6SKees Cook 830313dd1b6SKees Cook stmt = gsi_stmt(gsi); 831313dd1b6SKees Cook 832313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN 833313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE 834313dd1b6SKees Cook debug_gimple_stmt(stmt); 835313dd1b6SKees Cook debug_tree(gimple_get_lhs(stmt)); 836313dd1b6SKees Cook #endif 837313dd1b6SKees Cook #endif 838313dd1b6SKees Cook 839313dd1b6SKees Cook if (gimple_code(stmt) != GIMPLE_ASSIGN) 840313dd1b6SKees Cook continue; 841313dd1b6SKees Cook 842313dd1b6SKees Cook #ifdef __DEBUG_PLUGIN 843313dd1b6SKees Cook #ifdef __DEBUG_VERBOSE 844313dd1b6SKees Cook debug_tree(gimple_assign_rhs1(stmt)); 845313dd1b6SKees Cook #endif 846313dd1b6SKees Cook #endif 847313dd1b6SKees Cook 848313dd1b6SKees Cook 849313dd1b6SKees Cook rhs_code = gimple_assign_rhs_code(stmt); 850313dd1b6SKees Cook 851313dd1b6SKees Cook if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME) 852313dd1b6SKees Cook continue; 853313dd1b6SKees Cook 854313dd1b6SKees Cook lhs = gimple_get_lhs(stmt); 855313dd1b6SKees Cook lhs_type = TREE_TYPE(lhs); 856313dd1b6SKees Cook rhs1 = gimple_assign_rhs1(stmt); 857313dd1b6SKees Cook rhs_type = TREE_TYPE(rhs1); 858313dd1b6SKees Cook 859313dd1b6SKees Cook if (TREE_CODE(rhs_type) != POINTER_TYPE || 860313dd1b6SKees Cook TREE_CODE(lhs_type) != POINTER_TYPE) 861313dd1b6SKees Cook continue; 862313dd1b6SKees Cook 863313dd1b6SKees Cook ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type)))); 864313dd1b6SKees Cook ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type)))); 865313dd1b6SKees Cook 866313dd1b6SKees Cook if (ptr_rhs_type == void_type_node) 867313dd1b6SKees Cook continue; 868313dd1b6SKees Cook 869313dd1b6SKees Cook if (ptr_lhs_type == void_type_node) 870313dd1b6SKees Cook continue; 871313dd1b6SKees Cook 872313dd1b6SKees Cook if (dominated_by_is_err(rhs1, bb)) 873313dd1b6SKees Cook continue; 874313dd1b6SKees Cook 875313dd1b6SKees Cook if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) { 876313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN 877313dd1b6SKees Cook if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type))) 878313dd1b6SKees Cook #endif 879313dd1b6SKees Cook { 880313dd1b6SKees Cook if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type)) 881313dd1b6SKees Cook MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type); 882313dd1b6SKees Cook } 883313dd1b6SKees Cook continue; 884313dd1b6SKees Cook } 885313dd1b6SKees Cook 886313dd1b6SKees Cook if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type) 887313dd1b6SKees Cook continue; 888313dd1b6SKees Cook 889313dd1b6SKees Cook if (rhs_code == ADDR_EXPR) { 890313dd1b6SKees Cook op0 = TREE_OPERAND(rhs1, 0); 891313dd1b6SKees Cook 892313dd1b6SKees Cook if (op0 == NULL_TREE) 893313dd1b6SKees Cook continue; 894313dd1b6SKees Cook 895313dd1b6SKees Cook if (TREE_CODE(op0) != VAR_DECL) 896313dd1b6SKees Cook continue; 897313dd1b6SKees Cook 898313dd1b6SKees Cook op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0)))); 899313dd1b6SKees Cook if (op0_type == ptr_lhs_type) 900313dd1b6SKees Cook continue; 901313dd1b6SKees Cook 902313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN 903313dd1b6SKees Cook if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type))) 904313dd1b6SKees Cook #endif 905313dd1b6SKees Cook { 906313dd1b6SKees Cook if (!whitelisted_cast(stmt, ptr_lhs_type, op0_type)) 907313dd1b6SKees Cook MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type); 908313dd1b6SKees Cook } 909313dd1b6SKees Cook } else { 910313dd1b6SKees Cook const_tree ssa_name_var = SSA_NAME_VAR(rhs1); 911313dd1b6SKees Cook /* skip bogus type casts introduced by container_of */ 912313dd1b6SKees Cook if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) && 913313dd1b6SKees Cook !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr")) 914313dd1b6SKees Cook continue; 915313dd1b6SKees Cook #ifndef __DEBUG_PLUGIN 916313dd1b6SKees Cook if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type))) 917313dd1b6SKees Cook #endif 918313dd1b6SKees Cook { 919313dd1b6SKees Cook if (!whitelisted_cast(stmt, ptr_lhs_type, ptr_rhs_type)) 920313dd1b6SKees Cook MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type); 921313dd1b6SKees Cook } 922313dd1b6SKees Cook } 923313dd1b6SKees Cook 924313dd1b6SKees Cook } 925313dd1b6SKees Cook } 926313dd1b6SKees Cook return 0; 927313dd1b6SKees Cook } 928313dd1b6SKees Cook 929313dd1b6SKees Cook #define PASS_NAME find_bad_casts 930313dd1b6SKees Cook #define NO_GATE 931313dd1b6SKees Cook #define TODO_FLAGS_FINISH TODO_dump_func 932313dd1b6SKees Cook #include "gcc-generate-gimple-pass.h" 933313dd1b6SKees Cook 934313dd1b6SKees Cook __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) 935313dd1b6SKees Cook { 936313dd1b6SKees Cook int i; 937313dd1b6SKees Cook const char * const plugin_name = plugin_info->base_name; 938313dd1b6SKees Cook const int argc = plugin_info->argc; 939313dd1b6SKees Cook const struct plugin_argument * const argv = plugin_info->argv; 940313dd1b6SKees Cook bool enable = true; 941313dd1b6SKees Cook int obtained_seed = 0; 942313dd1b6SKees Cook struct register_pass_info find_bad_casts_pass_info; 943313dd1b6SKees Cook 944313dd1b6SKees Cook find_bad_casts_pass_info.pass = make_find_bad_casts_pass(); 945313dd1b6SKees Cook find_bad_casts_pass_info.reference_pass_name = "ssa"; 946313dd1b6SKees Cook find_bad_casts_pass_info.ref_pass_instance_number = 1; 947313dd1b6SKees Cook find_bad_casts_pass_info.pos_op = PASS_POS_INSERT_AFTER; 948313dd1b6SKees Cook 949313dd1b6SKees Cook if (!plugin_default_version_check(version, &gcc_version)) { 950313dd1b6SKees Cook error(G_("incompatible gcc/plugin versions")); 951313dd1b6SKees Cook return 1; 952313dd1b6SKees Cook } 953313dd1b6SKees Cook 954313dd1b6SKees Cook if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) { 955313dd1b6SKees Cook inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name); 956313dd1b6SKees Cook enable = false; 957313dd1b6SKees Cook } 958313dd1b6SKees Cook 959313dd1b6SKees Cook for (i = 0; i < argc; ++i) { 960313dd1b6SKees Cook if (!strcmp(argv[i].key, "disable")) { 961313dd1b6SKees Cook enable = false; 962313dd1b6SKees Cook continue; 963313dd1b6SKees Cook } 964313dd1b6SKees Cook if (!strcmp(argv[i].key, "performance-mode")) { 965313dd1b6SKees Cook performance_mode = 1; 966313dd1b6SKees Cook continue; 967313dd1b6SKees Cook } 968313dd1b6SKees Cook error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key); 969313dd1b6SKees Cook } 970313dd1b6SKees Cook 971313dd1b6SKees Cook if (strlen(randstruct_seed) != 64) { 972313dd1b6SKees Cook error(G_("invalid seed value supplied for %s plugin"), plugin_name); 973313dd1b6SKees Cook return 1; 974313dd1b6SKees Cook } 975313dd1b6SKees Cook obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx", 976313dd1b6SKees Cook &shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]); 977313dd1b6SKees Cook if (obtained_seed != 4) { 978313dd1b6SKees Cook error(G_("Invalid seed supplied for %s plugin"), plugin_name); 979313dd1b6SKees Cook return 1; 980313dd1b6SKees Cook } 981313dd1b6SKees Cook 982313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info); 983313dd1b6SKees Cook if (enable) { 984313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL); 985313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info); 986313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL); 987313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL); 988313dd1b6SKees Cook } 989313dd1b6SKees Cook register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL); 990313dd1b6SKees Cook 991313dd1b6SKees Cook return 0; 992313dd1b6SKees Cook } 993