randomize_layout_plugin.c 23 KB

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