randomize_layout_plugin.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  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
  5. *
  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
  10. *
  11. * Usage:
  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
  17. */
  18. #include "gcc-common.h"
  19. #include "randomize_layout_seed.h"
  20. #define ORIG_TYPE_NAME(node) \
  21. (TYPE_NAME(TYPE_MAIN_VARIANT(node)) != NULL_TREE ? ((const unsigned char *)IDENTIFIER_POINTER(TYPE_NAME(TYPE_MAIN_VARIANT(node)))) : (const unsigned char *)"anonymous")
  22. #define INFORM(loc, msg, ...) inform(loc, "randstruct: " msg, ##__VA_ARGS__)
  23. #define MISMATCH(loc, how, ...) INFORM(loc, "casting between randomized structure pointer types (" how "): %qT and %qT\n", __VA_ARGS__)
  24. __visible int plugin_is_GPL_compatible;
  25. static int performance_mode;
  26. static struct plugin_info randomize_layout_plugin_info = {
  27. .version = PLUGIN_VERSION,
  28. .help = "disable\t\t\tdo not activate plugin\n"
  29. "performance-mode\tenable cacheline-aware layout randomization\n"
  30. };
  31. /* from old Linux dcache.h */
  32. static inline unsigned long
  33. partial_name_hash(unsigned long c, unsigned long prevhash)
  34. {
  35. return (prevhash + (c << 4) + (c >> 4)) * 11;
  36. }
  37. static inline unsigned int
  38. name_hash(const unsigned char *name)
  39. {
  40. unsigned long hash = 0;
  41. unsigned int len = strlen((const char *)name);
  42. while (len--)
  43. hash = partial_name_hash(*name++, hash);
  44. return (unsigned int)hash;
  45. }
  46. static tree handle_randomize_layout_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
  47. {
  48. tree type;
  49. *no_add_attrs = true;
  50. if (TREE_CODE(*node) == FUNCTION_DECL) {
  51. error("%qE attribute does not apply to functions (%qF)", name, *node);
  52. return NULL_TREE;
  53. }
  54. if (TREE_CODE(*node) == PARM_DECL) {
  55. error("%qE attribute does not apply to function parameters (%qD)", name, *node);
  56. return NULL_TREE;
  57. }
  58. if (TREE_CODE(*node) == VAR_DECL) {
  59. error("%qE attribute does not apply to variables (%qD)", name, *node);
  60. return NULL_TREE;
  61. }
  62. if (TYPE_P(*node)) {
  63. type = *node;
  64. } else {
  65. gcc_assert(TREE_CODE(*node) == TYPE_DECL);
  66. type = TREE_TYPE(*node);
  67. }
  68. if (TREE_CODE(type) != RECORD_TYPE) {
  69. error("%qE attribute used on %qT applies to struct types only", name, type);
  70. return NULL_TREE;
  71. }
  72. if (lookup_attribute(IDENTIFIER_POINTER(name), TYPE_ATTRIBUTES(type))) {
  73. error("%qE attribute is already applied to the type %qT", name, type);
  74. return NULL_TREE;
  75. }
  76. *no_add_attrs = false;
  77. return NULL_TREE;
  78. }
  79. /* set on complete types that we don't need to inspect further at all */
  80. static tree handle_randomize_considered_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
  81. {
  82. *no_add_attrs = false;
  83. return NULL_TREE;
  84. }
  85. /*
  86. * set on types that we've performed a shuffle on, to prevent re-shuffling
  87. * this does not preclude us from inspecting its fields for potential shuffles
  88. */
  89. static tree handle_randomize_performed_attr(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
  90. {
  91. *no_add_attrs = false;
  92. return NULL_TREE;
  93. }
  94. /*
  95. * 64bit variant of Bob Jenkins' public domain PRNG
  96. * 256 bits of internal state
  97. */
  98. typedef unsigned long long u64;
  99. typedef struct ranctx { u64 a; u64 b; u64 c; u64 d; } ranctx;
  100. #define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
  101. static u64 ranval(ranctx *x) {
  102. u64 e = x->a - rot(x->b, 7);
  103. x->a = x->b ^ rot(x->c, 13);
  104. x->b = x->c + rot(x->d, 37);
  105. x->c = x->d + e;
  106. x->d = e + x->a;
  107. return x->d;
  108. }
  109. static void raninit(ranctx *x, u64 *seed) {
  110. int i;
  111. x->a = seed[0];
  112. x->b = seed[1];
  113. x->c = seed[2];
  114. x->d = seed[3];
  115. for (i=0; i < 30; ++i)
  116. (void)ranval(x);
  117. }
  118. static u64 shuffle_seed[4];
  119. struct partition_group {
  120. tree tree_start;
  121. unsigned long start;
  122. unsigned long length;
  123. };
  124. static void partition_struct(tree *fields, unsigned long length, struct partition_group *size_groups, unsigned long *num_groups)
  125. {
  126. unsigned long i;
  127. unsigned long accum_size = 0;
  128. unsigned long accum_length = 0;
  129. unsigned long group_idx = 0;
  130. gcc_assert(length < INT_MAX);
  131. memset(size_groups, 0, sizeof(struct partition_group) * length);
  132. for (i = 0; i < length; i++) {
  133. if (size_groups[group_idx].tree_start == NULL_TREE) {
  134. size_groups[group_idx].tree_start = fields[i];
  135. size_groups[group_idx].start = i;
  136. accum_length = 0;
  137. accum_size = 0;
  138. }
  139. accum_size += (unsigned long)int_size_in_bytes(TREE_TYPE(fields[i]));
  140. accum_length++;
  141. if (accum_size >= 64) {
  142. size_groups[group_idx].length = accum_length;
  143. accum_length = 0;
  144. group_idx++;
  145. }
  146. }
  147. if (size_groups[group_idx].tree_start != NULL_TREE &&
  148. !size_groups[group_idx].length) {
  149. size_groups[group_idx].length = accum_length;
  150. group_idx++;
  151. }
  152. *num_groups = group_idx;
  153. }
  154. static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
  155. {
  156. unsigned long i, x, index;
  157. struct partition_group size_group[length];
  158. unsigned long num_groups = 0;
  159. unsigned long randnum;
  160. partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
  161. /* FIXME: this group shuffle is currently a no-op. */
  162. for (i = num_groups - 1; i > 0; i--) {
  163. struct partition_group tmp;
  164. randnum = ranval(prng_state) % (i + 1);
  165. tmp = size_group[i];
  166. size_group[i] = size_group[randnum];
  167. size_group[randnum] = tmp;
  168. }
  169. for (x = 0; x < num_groups; x++) {
  170. for (index = size_group[x].length - 1; index > 0; index--) {
  171. tree tmp;
  172. i = size_group[x].start + index;
  173. if (DECL_BIT_FIELD_TYPE(newtree[i]))
  174. continue;
  175. randnum = ranval(prng_state) % (index + 1);
  176. randnum += size_group[x].start;
  177. // we could handle this case differently if desired
  178. if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
  179. continue;
  180. tmp = newtree[i];
  181. newtree[i] = newtree[randnum];
  182. newtree[randnum] = tmp;
  183. }
  184. }
  185. }
  186. static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
  187. {
  188. unsigned long i, randnum;
  189. for (i = length - 1; i > 0; i--) {
  190. tree tmp;
  191. randnum = ranval(prng_state) % (i + 1);
  192. tmp = newtree[i];
  193. newtree[i] = newtree[randnum];
  194. newtree[randnum] = tmp;
  195. }
  196. }
  197. /* modern in-place Fisher-Yates shuffle */
  198. static void shuffle(const_tree type, tree *newtree, unsigned long length)
  199. {
  200. unsigned long i;
  201. u64 seed[4];
  202. ranctx prng_state;
  203. const unsigned char *structname;
  204. if (length == 0)
  205. return;
  206. gcc_assert(TREE_CODE(type) == RECORD_TYPE);
  207. structname = ORIG_TYPE_NAME(type);
  208. #ifdef __DEBUG_PLUGIN
  209. fprintf(stderr, "Shuffling struct %s %p\n", (const char *)structname, type);
  210. #ifdef __DEBUG_VERBOSE
  211. debug_tree((tree)type);
  212. #endif
  213. #endif
  214. for (i = 0; i < 4; i++) {
  215. seed[i] = shuffle_seed[i];
  216. seed[i] ^= name_hash(structname);
  217. }
  218. raninit(&prng_state, (u64 *)&seed);
  219. if (performance_mode)
  220. performance_shuffle(newtree, length, &prng_state);
  221. else
  222. full_shuffle(newtree, length, &prng_state);
  223. }
  224. static bool is_flexible_array(const_tree field)
  225. {
  226. const_tree fieldtype;
  227. const_tree typesize;
  228. fieldtype = TREE_TYPE(field);
  229. typesize = TYPE_SIZE(fieldtype);
  230. if (TREE_CODE(fieldtype) != ARRAY_TYPE)
  231. return false;
  232. /* size of type is represented in bits */
  233. if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
  234. TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
  235. return true;
  236. return false;
  237. }
  238. static int relayout_struct(tree type)
  239. {
  240. unsigned long num_fields = (unsigned long)list_length(TYPE_FIELDS(type));
  241. unsigned long shuffle_length = num_fields;
  242. tree field;
  243. tree newtree[num_fields];
  244. unsigned long i;
  245. tree list;
  246. tree variant;
  247. tree main_variant;
  248. expanded_location xloc;
  249. bool has_flexarray = false;
  250. if (TYPE_FIELDS(type) == NULL_TREE)
  251. return 0;
  252. if (num_fields < 2)
  253. return 0;
  254. gcc_assert(TREE_CODE(type) == RECORD_TYPE);
  255. gcc_assert(num_fields < INT_MAX);
  256. if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)) ||
  257. lookup_attribute("no_randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))))
  258. return 0;
  259. /* Workaround for 3rd-party VirtualBox source that we can't modify ourselves */
  260. if (!strcmp((const char *)ORIG_TYPE_NAME(type), "INTNETTRUNKFACTORY") ||
  261. !strcmp((const char *)ORIG_TYPE_NAME(type), "RAWPCIFACTORY"))
  262. return 0;
  263. /* throw out any structs in uapi */
  264. xloc = expand_location(DECL_SOURCE_LOCATION(TYPE_FIELDS(type)));
  265. if (strstr(xloc.file, "/uapi/"))
  266. error(G_("attempted to randomize userland API struct %s"), ORIG_TYPE_NAME(type));
  267. for (field = TYPE_FIELDS(type), i = 0; field; field = TREE_CHAIN(field), i++) {
  268. gcc_assert(TREE_CODE(field) == FIELD_DECL);
  269. newtree[i] = field;
  270. }
  271. /*
  272. * enforce that we don't randomize the layout of the last
  273. * element of a struct if it's a proper flexible array
  274. */
  275. if (is_flexible_array(newtree[num_fields - 1])) {
  276. has_flexarray = true;
  277. shuffle_length--;
  278. }
  279. shuffle(type, (tree *)newtree, shuffle_length);
  280. /*
  281. * set up a bogus anonymous struct field designed to error out on unnamed struct initializers
  282. * as gcc provides no other way to detect such code
  283. */
  284. list = make_node(FIELD_DECL);
  285. TREE_CHAIN(list) = newtree[0];
  286. TREE_TYPE(list) = void_type_node;
  287. DECL_SIZE(list) = bitsize_zero_node;
  288. DECL_NONADDRESSABLE_P(list) = 1;
  289. DECL_FIELD_BIT_OFFSET(list) = bitsize_zero_node;
  290. DECL_SIZE_UNIT(list) = size_zero_node;
  291. DECL_FIELD_OFFSET(list) = size_zero_node;
  292. DECL_CONTEXT(list) = type;
  293. // to satisfy the constify plugin
  294. TREE_READONLY(list) = 1;
  295. for (i = 0; i < num_fields - 1; i++)
  296. TREE_CHAIN(newtree[i]) = newtree[i+1];
  297. TREE_CHAIN(newtree[num_fields - 1]) = NULL_TREE;
  298. main_variant = TYPE_MAIN_VARIANT(type);
  299. for (variant = main_variant; variant; variant = TYPE_NEXT_VARIANT(variant)) {
  300. TYPE_FIELDS(variant) = list;
  301. TYPE_ATTRIBUTES(variant) = copy_list(TYPE_ATTRIBUTES(variant));
  302. TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("randomize_performed"), NULL_TREE, TYPE_ATTRIBUTES(variant));
  303. TYPE_ATTRIBUTES(variant) = tree_cons(get_identifier("designated_init"), NULL_TREE, TYPE_ATTRIBUTES(variant));
  304. if (has_flexarray)
  305. TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("has_flexarray"), NULL_TREE, TYPE_ATTRIBUTES(type));
  306. }
  307. /*
  308. * force a re-layout of the main variant
  309. * the TYPE_SIZE for all variants will be recomputed
  310. * by finalize_type_size()
  311. */
  312. TYPE_SIZE(main_variant) = NULL_TREE;
  313. layout_type(main_variant);
  314. gcc_assert(TYPE_SIZE(main_variant) != NULL_TREE);
  315. return 1;
  316. }
  317. /* from constify plugin */
  318. static const_tree get_field_type(const_tree field)
  319. {
  320. return strip_array_types(TREE_TYPE(field));
  321. }
  322. /* from constify plugin */
  323. static bool is_fptr(const_tree fieldtype)
  324. {
  325. if (TREE_CODE(fieldtype) != POINTER_TYPE)
  326. return false;
  327. return TREE_CODE(TREE_TYPE(fieldtype)) == FUNCTION_TYPE;
  328. }
  329. /* derived from constify plugin */
  330. static int is_pure_ops_struct(const_tree node)
  331. {
  332. const_tree field;
  333. gcc_assert(TREE_CODE(node) == RECORD_TYPE || TREE_CODE(node) == UNION_TYPE);
  334. for (field = TYPE_FIELDS(node); field; field = TREE_CHAIN(field)) {
  335. const_tree fieldtype = get_field_type(field);
  336. enum tree_code code = TREE_CODE(fieldtype);
  337. if (node == fieldtype)
  338. continue;
  339. if (code == RECORD_TYPE || code == UNION_TYPE) {
  340. if (!is_pure_ops_struct(fieldtype))
  341. return 0;
  342. continue;
  343. }
  344. if (!is_fptr(fieldtype))
  345. return 0;
  346. }
  347. return 1;
  348. }
  349. static void randomize_type(tree type)
  350. {
  351. tree variant;
  352. gcc_assert(TREE_CODE(type) == RECORD_TYPE);
  353. if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
  354. return;
  355. if (lookup_attribute("randomize_layout", TYPE_ATTRIBUTES(TYPE_MAIN_VARIANT(type))) || is_pure_ops_struct(type))
  356. relayout_struct(type);
  357. for (variant = TYPE_MAIN_VARIANT(type); variant; variant = TYPE_NEXT_VARIANT(variant)) {
  358. TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
  359. TYPE_ATTRIBUTES(type) = tree_cons(get_identifier("randomize_considered"), NULL_TREE, TYPE_ATTRIBUTES(type));
  360. }
  361. #ifdef __DEBUG_PLUGIN
  362. fprintf(stderr, "Marking randomize_considered on struct %s\n", ORIG_TYPE_NAME(type));
  363. #ifdef __DEBUG_VERBOSE
  364. debug_tree(type);
  365. #endif
  366. #endif
  367. }
  368. static void update_decl_size(tree decl)
  369. {
  370. tree lastval, lastidx, field, init, type, flexsize;
  371. unsigned HOST_WIDE_INT len;
  372. type = TREE_TYPE(decl);
  373. if (!lookup_attribute("has_flexarray", TYPE_ATTRIBUTES(type)))
  374. return;
  375. init = DECL_INITIAL(decl);
  376. if (init == NULL_TREE || init == error_mark_node)
  377. return;
  378. if (TREE_CODE(init) != CONSTRUCTOR)
  379. return;
  380. len = CONSTRUCTOR_NELTS(init);
  381. if (!len)
  382. return;
  383. lastval = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->value;
  384. lastidx = CONSTRUCTOR_ELT(init, CONSTRUCTOR_NELTS(init) - 1)->index;
  385. for (field = TYPE_FIELDS(TREE_TYPE(decl)); TREE_CHAIN(field); field = TREE_CHAIN(field))
  386. ;
  387. if (lastidx != field)
  388. return;
  389. if (TREE_CODE(lastval) != STRING_CST) {
  390. error("Only string constants are supported as initializers "
  391. "for randomized structures with flexible arrays");
  392. return;
  393. }
  394. flexsize = bitsize_int(TREE_STRING_LENGTH(lastval) *
  395. tree_to_uhwi(TYPE_SIZE(TREE_TYPE(TREE_TYPE(lastval)))));
  396. DECL_SIZE(decl) = size_binop(PLUS_EXPR, TYPE_SIZE(type), flexsize);
  397. return;
  398. }
  399. static void randomize_layout_finish_decl(void *event_data, void *data)
  400. {
  401. tree decl = (tree)event_data;
  402. tree type;
  403. if (decl == NULL_TREE || decl == error_mark_node)
  404. return;
  405. type = TREE_TYPE(decl);
  406. if (TREE_CODE(decl) != VAR_DECL)
  407. return;
  408. if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
  409. return;
  410. if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(type)))
  411. return;
  412. DECL_SIZE(decl) = 0;
  413. DECL_SIZE_UNIT(decl) = 0;
  414. SET_DECL_ALIGN(decl, 0);
  415. SET_DECL_MODE (decl, VOIDmode);
  416. SET_DECL_RTL(decl, 0);
  417. update_decl_size(decl);
  418. layout_decl(decl, 0);
  419. }
  420. static void finish_type(void *event_data, void *data)
  421. {
  422. tree type = (tree)event_data;
  423. if (type == NULL_TREE || type == error_mark_node)
  424. return;
  425. if (TREE_CODE(type) != RECORD_TYPE)
  426. return;
  427. if (TYPE_FIELDS(type) == NULL_TREE)
  428. return;
  429. if (lookup_attribute("randomize_considered", TYPE_ATTRIBUTES(type)))
  430. return;
  431. #ifdef __DEBUG_PLUGIN
  432. fprintf(stderr, "Calling randomize_type on %s\n", ORIG_TYPE_NAME(type));
  433. #endif
  434. #ifdef __DEBUG_VERBOSE
  435. debug_tree(type);
  436. #endif
  437. randomize_type(type);
  438. return;
  439. }
  440. static struct attribute_spec randomize_layout_attr = { };
  441. static struct attribute_spec no_randomize_layout_attr = { };
  442. static struct attribute_spec randomize_considered_attr = { };
  443. static struct attribute_spec randomize_performed_attr = { };
  444. static void register_attributes(void *event_data, void *data)
  445. {
  446. randomize_layout_attr.name = "randomize_layout";
  447. randomize_layout_attr.type_required = true;
  448. randomize_layout_attr.handler = handle_randomize_layout_attr;
  449. randomize_layout_attr.affects_type_identity = true;
  450. no_randomize_layout_attr.name = "no_randomize_layout";
  451. no_randomize_layout_attr.type_required = true;
  452. no_randomize_layout_attr.handler = handle_randomize_layout_attr;
  453. no_randomize_layout_attr.affects_type_identity = true;
  454. randomize_considered_attr.name = "randomize_considered";
  455. randomize_considered_attr.type_required = true;
  456. randomize_considered_attr.handler = handle_randomize_considered_attr;
  457. randomize_performed_attr.name = "randomize_performed";
  458. randomize_performed_attr.type_required = true;
  459. randomize_performed_attr.handler = handle_randomize_performed_attr;
  460. register_attribute(&randomize_layout_attr);
  461. register_attribute(&no_randomize_layout_attr);
  462. register_attribute(&randomize_considered_attr);
  463. register_attribute(&randomize_performed_attr);
  464. }
  465. static void check_bad_casts_in_constructor(tree var, tree init)
  466. {
  467. unsigned HOST_WIDE_INT idx;
  468. tree field, val;
  469. tree field_type, val_type;
  470. FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(init), idx, field, val) {
  471. if (TREE_CODE(val) == CONSTRUCTOR) {
  472. check_bad_casts_in_constructor(var, val);
  473. continue;
  474. }
  475. /* pipacs' plugin creates franken-arrays that differ from those produced by
  476. normal code which all have valid 'field' trees. work around this */
  477. if (field == NULL_TREE)
  478. continue;
  479. field_type = TREE_TYPE(field);
  480. val_type = TREE_TYPE(val);
  481. if (TREE_CODE(field_type) != POINTER_TYPE || TREE_CODE(val_type) != POINTER_TYPE)
  482. continue;
  483. if (field_type == val_type)
  484. continue;
  485. field_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(field_type))));
  486. val_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(val_type))));
  487. if (field_type == void_type_node)
  488. continue;
  489. if (field_type == val_type)
  490. continue;
  491. if (TREE_CODE(val_type) != RECORD_TYPE)
  492. continue;
  493. if (!lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(val_type)))
  494. continue;
  495. MISMATCH(DECL_SOURCE_LOCATION(var), "constructor\n", TYPE_MAIN_VARIANT(field_type), TYPE_MAIN_VARIANT(val_type));
  496. }
  497. }
  498. /* derived from the constify plugin */
  499. static void check_global_variables(void *event_data, void *data)
  500. {
  501. struct varpool_node *node;
  502. tree init;
  503. FOR_EACH_VARIABLE(node) {
  504. tree var = NODE_DECL(node);
  505. init = DECL_INITIAL(var);
  506. if (init == NULL_TREE)
  507. continue;
  508. if (TREE_CODE(init) != CONSTRUCTOR)
  509. continue;
  510. check_bad_casts_in_constructor(var, init);
  511. }
  512. }
  513. static bool dominated_by_is_err(const_tree rhs, basic_block bb)
  514. {
  515. basic_block dom;
  516. gimple dom_stmt;
  517. gimple call_stmt;
  518. const_tree dom_lhs;
  519. const_tree poss_is_err_cond;
  520. const_tree poss_is_err_func;
  521. const_tree is_err_arg;
  522. dom = get_immediate_dominator(CDI_DOMINATORS, bb);
  523. if (!dom)
  524. return false;
  525. dom_stmt = last_stmt(dom);
  526. if (!dom_stmt)
  527. return false;
  528. if (gimple_code(dom_stmt) != GIMPLE_COND)
  529. return false;
  530. if (gimple_cond_code(dom_stmt) != NE_EXPR)
  531. return false;
  532. if (!integer_zerop(gimple_cond_rhs(dom_stmt)))
  533. return false;
  534. poss_is_err_cond = gimple_cond_lhs(dom_stmt);
  535. if (TREE_CODE(poss_is_err_cond) != SSA_NAME)
  536. return false;
  537. call_stmt = SSA_NAME_DEF_STMT(poss_is_err_cond);
  538. if (gimple_code(call_stmt) != GIMPLE_CALL)
  539. return false;
  540. dom_lhs = gimple_get_lhs(call_stmt);
  541. poss_is_err_func = gimple_call_fndecl(call_stmt);
  542. if (!poss_is_err_func)
  543. return false;
  544. if (dom_lhs != poss_is_err_cond)
  545. return false;
  546. if (strcmp(DECL_NAME_POINTER(poss_is_err_func), "IS_ERR"))
  547. return false;
  548. is_err_arg = gimple_call_arg(call_stmt, 0);
  549. if (!is_err_arg)
  550. return false;
  551. if (is_err_arg != rhs)
  552. return false;
  553. return true;
  554. }
  555. static void handle_local_var_initializers(void)
  556. {
  557. tree var;
  558. unsigned int i;
  559. FOR_EACH_LOCAL_DECL(cfun, i, var) {
  560. tree init = DECL_INITIAL(var);
  561. if (!init)
  562. continue;
  563. if (TREE_CODE(init) != CONSTRUCTOR)
  564. continue;
  565. check_bad_casts_in_constructor(var, init);
  566. }
  567. }
  568. /*
  569. * iterate over all statements to find "bad" casts:
  570. * those where the address of the start of a structure is cast
  571. * to a pointer of a structure of a different type, or a
  572. * structure pointer type is cast to a different structure pointer type
  573. */
  574. static unsigned int find_bad_casts_execute(void)
  575. {
  576. basic_block bb;
  577. handle_local_var_initializers();
  578. FOR_EACH_BB_FN(bb, cfun) {
  579. gimple_stmt_iterator gsi;
  580. for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
  581. gimple stmt;
  582. const_tree lhs;
  583. const_tree lhs_type;
  584. const_tree rhs1;
  585. const_tree rhs_type;
  586. const_tree ptr_lhs_type;
  587. const_tree ptr_rhs_type;
  588. const_tree op0;
  589. const_tree op0_type;
  590. enum tree_code rhs_code;
  591. stmt = gsi_stmt(gsi);
  592. #ifdef __DEBUG_PLUGIN
  593. #ifdef __DEBUG_VERBOSE
  594. debug_gimple_stmt(stmt);
  595. debug_tree(gimple_get_lhs(stmt));
  596. #endif
  597. #endif
  598. if (gimple_code(stmt) != GIMPLE_ASSIGN)
  599. continue;
  600. #ifdef __DEBUG_PLUGIN
  601. #ifdef __DEBUG_VERBOSE
  602. debug_tree(gimple_assign_rhs1(stmt));
  603. #endif
  604. #endif
  605. rhs_code = gimple_assign_rhs_code(stmt);
  606. if (rhs_code != ADDR_EXPR && rhs_code != SSA_NAME)
  607. continue;
  608. lhs = gimple_get_lhs(stmt);
  609. lhs_type = TREE_TYPE(lhs);
  610. rhs1 = gimple_assign_rhs1(stmt);
  611. rhs_type = TREE_TYPE(rhs1);
  612. if (TREE_CODE(rhs_type) != POINTER_TYPE ||
  613. TREE_CODE(lhs_type) != POINTER_TYPE)
  614. continue;
  615. ptr_lhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(lhs_type))));
  616. ptr_rhs_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(rhs_type))));
  617. if (ptr_rhs_type == void_type_node)
  618. continue;
  619. if (ptr_lhs_type == void_type_node)
  620. continue;
  621. if (dominated_by_is_err(rhs1, bb))
  622. continue;
  623. if (TREE_CODE(ptr_rhs_type) != RECORD_TYPE) {
  624. #ifndef __DEBUG_PLUGIN
  625. if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_lhs_type)))
  626. #endif
  627. MISMATCH(gimple_location(stmt), "rhs", ptr_lhs_type, ptr_rhs_type);
  628. continue;
  629. }
  630. if (rhs_code == SSA_NAME && ptr_lhs_type == ptr_rhs_type)
  631. continue;
  632. if (rhs_code == ADDR_EXPR) {
  633. op0 = TREE_OPERAND(rhs1, 0);
  634. if (op0 == NULL_TREE)
  635. continue;
  636. if (TREE_CODE(op0) != VAR_DECL)
  637. continue;
  638. op0_type = TYPE_MAIN_VARIANT(strip_array_types(TYPE_MAIN_VARIANT(TREE_TYPE(op0))));
  639. if (op0_type == ptr_lhs_type)
  640. continue;
  641. #ifndef __DEBUG_PLUGIN
  642. if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(op0_type)))
  643. #endif
  644. MISMATCH(gimple_location(stmt), "op0", ptr_lhs_type, op0_type);
  645. } else {
  646. const_tree ssa_name_var = SSA_NAME_VAR(rhs1);
  647. /* skip bogus type casts introduced by container_of */
  648. if (ssa_name_var != NULL_TREE && DECL_NAME(ssa_name_var) &&
  649. !strcmp((const char *)DECL_NAME_POINTER(ssa_name_var), "__mptr"))
  650. continue;
  651. #ifndef __DEBUG_PLUGIN
  652. if (lookup_attribute("randomize_performed", TYPE_ATTRIBUTES(ptr_rhs_type)))
  653. #endif
  654. MISMATCH(gimple_location(stmt), "ssa", ptr_lhs_type, ptr_rhs_type);
  655. }
  656. }
  657. }
  658. return 0;
  659. }
  660. #define PASS_NAME find_bad_casts
  661. #define NO_GATE
  662. #define TODO_FLAGS_FINISH TODO_dump_func
  663. #include "gcc-generate-gimple-pass.h"
  664. __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
  665. {
  666. int i;
  667. const char * const plugin_name = plugin_info->base_name;
  668. const int argc = plugin_info->argc;
  669. const struct plugin_argument * const argv = plugin_info->argv;
  670. bool enable = true;
  671. int obtained_seed = 0;
  672. struct register_pass_info find_bad_casts_pass_info;
  673. find_bad_casts_pass_info.pass = make_find_bad_casts_pass();
  674. find_bad_casts_pass_info.reference_pass_name = "ssa";
  675. find_bad_casts_pass_info.ref_pass_instance_number = 1;
  676. find_bad_casts_pass_info.pos_op = PASS_POS_INSERT_AFTER;
  677. if (!plugin_default_version_check(version, &gcc_version)) {
  678. error(G_("incompatible gcc/plugin versions"));
  679. return 1;
  680. }
  681. if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
  682. inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
  683. enable = false;
  684. }
  685. for (i = 0; i < argc; ++i) {
  686. if (!strcmp(argv[i].key, "disable")) {
  687. enable = false;
  688. continue;
  689. }
  690. if (!strcmp(argv[i].key, "performance-mode")) {
  691. performance_mode = 1;
  692. continue;
  693. }
  694. error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
  695. }
  696. if (strlen(randstruct_seed) != 64) {
  697. error(G_("invalid seed value supplied for %s plugin"), plugin_name);
  698. return 1;
  699. }
  700. obtained_seed = sscanf(randstruct_seed, "%016llx%016llx%016llx%016llx",
  701. &shuffle_seed[0], &shuffle_seed[1], &shuffle_seed[2], &shuffle_seed[3]);
  702. if (obtained_seed != 4) {
  703. error(G_("Invalid seed supplied for %s plugin"), plugin_name);
  704. return 1;
  705. }
  706. register_callback(plugin_name, PLUGIN_INFO, NULL, &randomize_layout_plugin_info);
  707. if (enable) {
  708. register_callback(plugin_name, PLUGIN_ALL_IPA_PASSES_START, check_global_variables, NULL);
  709. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &find_bad_casts_pass_info);
  710. register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
  711. register_callback(plugin_name, PLUGIN_FINISH_DECL, randomize_layout_finish_decl, NULL);
  712. }
  713. register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
  714. return 0;
  715. }