stackleak_plugin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2011-2017 by the PaX Team <[email protected]>
  4. * Modified by Alexander Popov <[email protected]>
  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. * This gcc plugin is needed for tracking the lowest border of the kernel stack.
  12. * It instruments the kernel code inserting stackleak_track_stack() calls:
  13. * - after alloca();
  14. * - for the functions with a stack frame size greater than or equal
  15. * to the "track-min-size" plugin parameter.
  16. *
  17. * This plugin is ported from grsecurity/PaX. For more information see:
  18. * https://grsecurity.net/
  19. * https://pax.grsecurity.net/
  20. *
  21. * Debugging:
  22. * - use fprintf() to stderr, debug_generic_expr(), debug_gimple_stmt(),
  23. * print_rtl_single() and debug_rtx();
  24. * - add "-fdump-tree-all -fdump-rtl-all" to the plugin CFLAGS in
  25. * Makefile.gcc-plugins to see the verbose dumps of the gcc passes;
  26. * - use gcc -E to understand the preprocessing shenanigans;
  27. * - use gcc with enabled CFG/GIMPLE/SSA verification (--enable-checking).
  28. */
  29. #include "gcc-common.h"
  30. __visible int plugin_is_GPL_compatible;
  31. static int track_frame_size = -1;
  32. static bool build_for_x86 = false;
  33. static const char track_function[] = "stackleak_track_stack";
  34. static bool disable = false;
  35. static bool verbose = false;
  36. /*
  37. * Mark these global variables (roots) for gcc garbage collector since
  38. * they point to the garbage-collected memory.
  39. */
  40. static GTY(()) tree track_function_decl;
  41. static struct plugin_info stackleak_plugin_info = {
  42. .version = PLUGIN_VERSION,
  43. .help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
  44. "arch=target_arch\tspecify target build arch\n"
  45. "disable\t\tdo not activate the plugin\n"
  46. "verbose\t\tprint info about the instrumentation\n"
  47. };
  48. static void add_stack_tracking_gcall(gimple_stmt_iterator *gsi, bool after)
  49. {
  50. gimple stmt;
  51. gcall *gimple_call;
  52. cgraph_node_ptr node;
  53. basic_block bb;
  54. /* Insert calling stackleak_track_stack() */
  55. stmt = gimple_build_call(track_function_decl, 0);
  56. gimple_call = as_a_gcall(stmt);
  57. if (after)
  58. gsi_insert_after(gsi, gimple_call, GSI_CONTINUE_LINKING);
  59. else
  60. gsi_insert_before(gsi, gimple_call, GSI_SAME_STMT);
  61. /* Update the cgraph */
  62. bb = gimple_bb(gimple_call);
  63. node = cgraph_get_create_node(track_function_decl);
  64. gcc_assert(node);
  65. cgraph_create_edge(cgraph_get_node(current_function_decl), node,
  66. gimple_call, bb->count,
  67. compute_call_stmt_bb_frequency(current_function_decl, bb));
  68. }
  69. static bool is_alloca(gimple stmt)
  70. {
  71. if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA))
  72. return true;
  73. if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
  74. return true;
  75. return false;
  76. }
  77. static tree get_current_stack_pointer_decl(void)
  78. {
  79. varpool_node_ptr node;
  80. FOR_EACH_VARIABLE(node) {
  81. tree var = NODE_DECL(node);
  82. tree name = DECL_NAME(var);
  83. if (DECL_NAME_LENGTH(var) != sizeof("current_stack_pointer") - 1)
  84. continue;
  85. if (strcmp(IDENTIFIER_POINTER(name), "current_stack_pointer"))
  86. continue;
  87. return var;
  88. }
  89. if (verbose) {
  90. fprintf(stderr, "stackleak: missing current_stack_pointer in %s()\n",
  91. DECL_NAME_POINTER(current_function_decl));
  92. }
  93. return NULL_TREE;
  94. }
  95. static void add_stack_tracking_gasm(gimple_stmt_iterator *gsi, bool after)
  96. {
  97. gasm *asm_call = NULL;
  98. tree sp_decl, input;
  99. vec<tree, va_gc> *inputs = NULL;
  100. /* 'no_caller_saved_registers' is currently supported only for x86 */
  101. gcc_assert(build_for_x86);
  102. /*
  103. * Insert calling stackleak_track_stack() in asm:
  104. * asm volatile("call stackleak_track_stack"
  105. * :: "r" (current_stack_pointer))
  106. * Use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h.
  107. * This constraint is taken into account during gcc shrink-wrapping
  108. * optimization. It is needed to be sure that stackleak_track_stack()
  109. * call is inserted after the prologue of the containing function,
  110. * when the stack frame is prepared.
  111. */
  112. sp_decl = get_current_stack_pointer_decl();
  113. if (sp_decl == NULL_TREE) {
  114. add_stack_tracking_gcall(gsi, after);
  115. return;
  116. }
  117. input = build_tree_list(NULL_TREE, build_const_char_string(2, "r"));
  118. input = chainon(NULL_TREE, build_tree_list(input, sp_decl));
  119. vec_safe_push(inputs, input);
  120. asm_call = gimple_build_asm_vec("call stackleak_track_stack",
  121. inputs, NULL, NULL, NULL);
  122. gimple_asm_set_volatile(asm_call, true);
  123. if (after)
  124. gsi_insert_after(gsi, asm_call, GSI_CONTINUE_LINKING);
  125. else
  126. gsi_insert_before(gsi, asm_call, GSI_SAME_STMT);
  127. update_stmt(asm_call);
  128. }
  129. static void add_stack_tracking(gimple_stmt_iterator *gsi, bool after)
  130. {
  131. /*
  132. * The 'no_caller_saved_registers' attribute is used for
  133. * stackleak_track_stack(). If the compiler supports this attribute for
  134. * the target arch, we can add calling stackleak_track_stack() in asm.
  135. * That improves performance: we avoid useless operations with the
  136. * caller-saved registers in the functions from which we will remove
  137. * stackleak_track_stack() call during the stackleak_cleanup pass.
  138. */
  139. if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
  140. add_stack_tracking_gasm(gsi, after);
  141. else
  142. add_stack_tracking_gcall(gsi, after);
  143. }
  144. /*
  145. * Work with the GIMPLE representation of the code. Insert the
  146. * stackleak_track_stack() call after alloca() and into the beginning
  147. * of the function if it is not instrumented.
  148. */
  149. static unsigned int stackleak_instrument_execute(void)
  150. {
  151. basic_block bb, entry_bb;
  152. bool prologue_instrumented = false, is_leaf = true;
  153. gimple_stmt_iterator gsi = { 0 };
  154. /*
  155. * ENTRY_BLOCK_PTR is a basic block which represents possible entry
  156. * point of a function. This block does not contain any code and
  157. * has a CFG edge to its successor.
  158. */
  159. gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  160. entry_bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
  161. /*
  162. * Loop through the GIMPLE statements in each of cfun basic blocks.
  163. * cfun is a global variable which represents the function that is
  164. * currently processed.
  165. */
  166. FOR_EACH_BB_FN(bb, cfun) {
  167. for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
  168. gimple stmt;
  169. stmt = gsi_stmt(gsi);
  170. /* Leaf function is a function which makes no calls */
  171. if (is_gimple_call(stmt))
  172. is_leaf = false;
  173. if (!is_alloca(stmt))
  174. continue;
  175. if (verbose) {
  176. fprintf(stderr, "stackleak: be careful, alloca() in %s()\n",
  177. DECL_NAME_POINTER(current_function_decl));
  178. }
  179. /* Insert stackleak_track_stack() call after alloca() */
  180. add_stack_tracking(&gsi, true);
  181. if (bb == entry_bb)
  182. prologue_instrumented = true;
  183. }
  184. }
  185. if (prologue_instrumented)
  186. return 0;
  187. /*
  188. * Special cases to skip the instrumentation.
  189. *
  190. * Taking the address of static inline functions materializes them,
  191. * but we mustn't instrument some of them as the resulting stack
  192. * alignment required by the function call ABI will break other
  193. * assumptions regarding the expected (but not otherwise enforced)
  194. * register clobbering ABI.
  195. *
  196. * Case in point: native_save_fl on amd64 when optimized for size
  197. * clobbers rdx if it were instrumented here.
  198. *
  199. * TODO: any more special cases?
  200. */
  201. if (is_leaf &&
  202. !TREE_PUBLIC(current_function_decl) &&
  203. DECL_DECLARED_INLINE_P(current_function_decl)) {
  204. return 0;
  205. }
  206. if (is_leaf &&
  207. !strncmp(IDENTIFIER_POINTER(DECL_NAME(current_function_decl)),
  208. "_paravirt_", 10)) {
  209. return 0;
  210. }
  211. /* Insert stackleak_track_stack() call at the function beginning */
  212. bb = entry_bb;
  213. if (!single_pred_p(bb)) {
  214. /* gcc_assert(bb_loop_depth(bb) ||
  215. (bb->flags & BB_IRREDUCIBLE_LOOP)); */
  216. split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  217. gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  218. bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
  219. }
  220. gsi = gsi_after_labels(bb);
  221. add_stack_tracking(&gsi, false);
  222. return 0;
  223. }
  224. static bool large_stack_frame(void)
  225. {
  226. #if BUILDING_GCC_VERSION >= 8000
  227. return maybe_ge(get_frame_size(), track_frame_size);
  228. #else
  229. return (get_frame_size() >= track_frame_size);
  230. #endif
  231. }
  232. static void remove_stack_tracking_gcall(void)
  233. {
  234. rtx_insn *insn, *next;
  235. /*
  236. * Find stackleak_track_stack() calls. Loop through the chain of insns,
  237. * which is an RTL representation of the code for a function.
  238. *
  239. * The example of a matching insn:
  240. * (call_insn 8 4 10 2 (call (mem (symbol_ref ("stackleak_track_stack")
  241. * [flags 0x41] <function_decl 0x7f7cd3302a80 stackleak_track_stack>)
  242. * [0 stackleak_track_stack S1 A8]) (0)) 675 {*call} (expr_list
  243. * (symbol_ref ("stackleak_track_stack") [flags 0x41] <function_decl
  244. * 0x7f7cd3302a80 stackleak_track_stack>) (expr_list (0) (nil))) (nil))
  245. */
  246. for (insn = get_insns(); insn; insn = next) {
  247. rtx body;
  248. next = NEXT_INSN(insn);
  249. /* Check the expression code of the insn */
  250. if (!CALL_P(insn))
  251. continue;
  252. /*
  253. * Check the expression code of the insn body, which is an RTL
  254. * Expression (RTX) describing the side effect performed by
  255. * that insn.
  256. */
  257. body = PATTERN(insn);
  258. if (GET_CODE(body) == PARALLEL)
  259. body = XVECEXP(body, 0, 0);
  260. if (GET_CODE(body) != CALL)
  261. continue;
  262. /*
  263. * Check the first operand of the call expression. It should
  264. * be a mem RTX describing the needed subroutine with a
  265. * symbol_ref RTX.
  266. */
  267. body = XEXP(body, 0);
  268. if (GET_CODE(body) != MEM)
  269. continue;
  270. body = XEXP(body, 0);
  271. if (GET_CODE(body) != SYMBOL_REF)
  272. continue;
  273. if (SYMBOL_REF_DECL(body) != track_function_decl)
  274. continue;
  275. /* Delete the stackleak_track_stack() call */
  276. delete_insn_and_edges(insn);
  277. #if BUILDING_GCC_VERSION < 8000
  278. if (GET_CODE(next) == NOTE &&
  279. NOTE_KIND(next) == NOTE_INSN_CALL_ARG_LOCATION) {
  280. insn = next;
  281. next = NEXT_INSN(insn);
  282. delete_insn_and_edges(insn);
  283. }
  284. #endif
  285. }
  286. }
  287. static bool remove_stack_tracking_gasm(void)
  288. {
  289. bool removed = false;
  290. rtx_insn *insn, *next;
  291. /* 'no_caller_saved_registers' is currently supported only for x86 */
  292. gcc_assert(build_for_x86);
  293. /*
  294. * Find stackleak_track_stack() asm calls. Loop through the chain of
  295. * insns, which is an RTL representation of the code for a function.
  296. *
  297. * The example of a matching insn:
  298. * (insn 11 5 12 2 (parallel [ (asm_operands/v
  299. * ("call stackleak_track_stack") ("") 0
  300. * [ (reg/v:DI 7 sp [ current_stack_pointer ]) ]
  301. * [ (asm_input:DI ("r")) ] [])
  302. * (clobber (reg:CC 17 flags)) ]) -1 (nil))
  303. */
  304. for (insn = get_insns(); insn; insn = next) {
  305. rtx body;
  306. next = NEXT_INSN(insn);
  307. /* Check the expression code of the insn */
  308. if (!NONJUMP_INSN_P(insn))
  309. continue;
  310. /*
  311. * Check the expression code of the insn body, which is an RTL
  312. * Expression (RTX) describing the side effect performed by
  313. * that insn.
  314. */
  315. body = PATTERN(insn);
  316. if (GET_CODE(body) != PARALLEL)
  317. continue;
  318. body = XVECEXP(body, 0, 0);
  319. if (GET_CODE(body) != ASM_OPERANDS)
  320. continue;
  321. if (strcmp(ASM_OPERANDS_TEMPLATE(body),
  322. "call stackleak_track_stack")) {
  323. continue;
  324. }
  325. delete_insn_and_edges(insn);
  326. gcc_assert(!removed);
  327. removed = true;
  328. }
  329. return removed;
  330. }
  331. /*
  332. * Work with the RTL representation of the code.
  333. * Remove the unneeded stackleak_track_stack() calls from the functions
  334. * which don't call alloca() and don't have a large enough stack frame size.
  335. */
  336. static unsigned int stackleak_cleanup_execute(void)
  337. {
  338. const char *fn = DECL_NAME_POINTER(current_function_decl);
  339. bool removed = false;
  340. /*
  341. * Leave stack tracking in functions that call alloca().
  342. * Additional case:
  343. * gcc before version 7 called allocate_dynamic_stack_space() from
  344. * expand_stack_vars() for runtime alignment of constant-sized stack
  345. * variables. That caused cfun->calls_alloca to be set for functions
  346. * that in fact don't use alloca().
  347. * For more info see gcc commit 7072df0aae0c59ae437e.
  348. * Let's leave such functions instrumented as well.
  349. */
  350. if (cfun->calls_alloca) {
  351. if (verbose)
  352. fprintf(stderr, "stackleak: instrument %s(): calls_alloca\n", fn);
  353. return 0;
  354. }
  355. /* Leave stack tracking in functions with large stack frame */
  356. if (large_stack_frame()) {
  357. if (verbose)
  358. fprintf(stderr, "stackleak: instrument %s()\n", fn);
  359. return 0;
  360. }
  361. if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
  362. removed = remove_stack_tracking_gasm();
  363. if (!removed)
  364. remove_stack_tracking_gcall();
  365. return 0;
  366. }
  367. /*
  368. * STRING_CST may or may not be NUL terminated:
  369. * https://gcc.gnu.org/onlinedocs/gccint/Constant-expressions.html
  370. */
  371. static inline bool string_equal(tree node, const char *string, int length)
  372. {
  373. if (TREE_STRING_LENGTH(node) < length)
  374. return false;
  375. if (TREE_STRING_LENGTH(node) > length + 1)
  376. return false;
  377. if (TREE_STRING_LENGTH(node) == length + 1 &&
  378. TREE_STRING_POINTER(node)[length] != '\0')
  379. return false;
  380. return !memcmp(TREE_STRING_POINTER(node), string, length);
  381. }
  382. #define STRING_EQUAL(node, str) string_equal(node, str, strlen(str))
  383. static bool stackleak_gate(void)
  384. {
  385. tree section;
  386. section = lookup_attribute("section",
  387. DECL_ATTRIBUTES(current_function_decl));
  388. if (section && TREE_VALUE(section)) {
  389. section = TREE_VALUE(TREE_VALUE(section));
  390. if (STRING_EQUAL(section, ".init.text"))
  391. return false;
  392. if (STRING_EQUAL(section, ".devinit.text"))
  393. return false;
  394. if (STRING_EQUAL(section, ".cpuinit.text"))
  395. return false;
  396. if (STRING_EQUAL(section, ".meminit.text"))
  397. return false;
  398. if (STRING_EQUAL(section, ".noinstr.text"))
  399. return false;
  400. if (STRING_EQUAL(section, ".entry.text"))
  401. return false;
  402. }
  403. return track_frame_size >= 0;
  404. }
  405. /* Build the function declaration for stackleak_track_stack() */
  406. static void stackleak_start_unit(void *gcc_data __unused,
  407. void *user_data __unused)
  408. {
  409. tree fntype;
  410. /* void stackleak_track_stack(void) */
  411. fntype = build_function_type_list(void_type_node, NULL_TREE);
  412. track_function_decl = build_fn_decl(track_function, fntype);
  413. DECL_ASSEMBLER_NAME(track_function_decl); /* for LTO */
  414. TREE_PUBLIC(track_function_decl) = 1;
  415. TREE_USED(track_function_decl) = 1;
  416. DECL_EXTERNAL(track_function_decl) = 1;
  417. DECL_ARTIFICIAL(track_function_decl) = 1;
  418. DECL_PRESERVE_P(track_function_decl) = 1;
  419. }
  420. /*
  421. * Pass gate function is a predicate function that gets executed before the
  422. * corresponding pass. If the return value is 'true' the pass gets executed,
  423. * otherwise, it is skipped.
  424. */
  425. static bool stackleak_instrument_gate(void)
  426. {
  427. return stackleak_gate();
  428. }
  429. #define PASS_NAME stackleak_instrument
  430. #define PROPERTIES_REQUIRED PROP_gimple_leh | PROP_cfg
  431. #define TODO_FLAGS_START TODO_verify_ssa | TODO_verify_flow | TODO_verify_stmts
  432. #define TODO_FLAGS_FINISH TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func \
  433. | TODO_update_ssa | TODO_rebuild_cgraph_edges
  434. #include "gcc-generate-gimple-pass.h"
  435. static bool stackleak_cleanup_gate(void)
  436. {
  437. return stackleak_gate();
  438. }
  439. #define PASS_NAME stackleak_cleanup
  440. #define TODO_FLAGS_FINISH TODO_dump_func
  441. #include "gcc-generate-rtl-pass.h"
  442. /*
  443. * Every gcc plugin exports a plugin_init() function that is called right
  444. * after the plugin is loaded. This function is responsible for registering
  445. * the plugin callbacks and doing other required initialization.
  446. */
  447. __visible int plugin_init(struct plugin_name_args *plugin_info,
  448. struct plugin_gcc_version *version)
  449. {
  450. const char * const plugin_name = plugin_info->base_name;
  451. const int argc = plugin_info->argc;
  452. const struct plugin_argument * const argv = plugin_info->argv;
  453. int i = 0;
  454. /* Extra GGC root tables describing our GTY-ed data */
  455. static const struct ggc_root_tab gt_ggc_r_gt_stackleak[] = {
  456. {
  457. .base = &track_function_decl,
  458. .nelt = 1,
  459. .stride = sizeof(track_function_decl),
  460. .cb = &gt_ggc_mx_tree_node,
  461. .pchw = &gt_pch_nx_tree_node
  462. },
  463. LAST_GGC_ROOT_TAB
  464. };
  465. /*
  466. * The stackleak_instrument pass should be executed before the
  467. * "optimized" pass, which is the control flow graph cleanup that is
  468. * performed just before expanding gcc trees to the RTL. In former
  469. * versions of the plugin this new pass was inserted before the
  470. * "tree_profile" pass, which is currently called "profile".
  471. */
  472. PASS_INFO(stackleak_instrument, "optimized", 1,
  473. PASS_POS_INSERT_BEFORE);
  474. /*
  475. * The stackleak_cleanup pass should be executed before the "*free_cfg"
  476. * pass. It's the moment when the stack frame size is already final,
  477. * function prologues and epilogues are generated, and the
  478. * machine-dependent code transformations are not done.
  479. */
  480. PASS_INFO(stackleak_cleanup, "*free_cfg", 1, PASS_POS_INSERT_BEFORE);
  481. if (!plugin_default_version_check(version, &gcc_version)) {
  482. error(G_("incompatible gcc/plugin versions"));
  483. return 1;
  484. }
  485. /* Parse the plugin arguments */
  486. for (i = 0; i < argc; i++) {
  487. if (!strcmp(argv[i].key, "track-min-size")) {
  488. if (!argv[i].value) {
  489. error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
  490. plugin_name, argv[i].key);
  491. return 1;
  492. }
  493. track_frame_size = atoi(argv[i].value);
  494. if (track_frame_size < 0) {
  495. error(G_("invalid option argument '-fplugin-arg-%s-%s=%s'"),
  496. plugin_name, argv[i].key, argv[i].value);
  497. return 1;
  498. }
  499. } else if (!strcmp(argv[i].key, "arch")) {
  500. if (!argv[i].value) {
  501. error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
  502. plugin_name, argv[i].key);
  503. return 1;
  504. }
  505. if (!strcmp(argv[i].value, "x86"))
  506. build_for_x86 = true;
  507. } else if (!strcmp(argv[i].key, "disable")) {
  508. disable = true;
  509. } else if (!strcmp(argv[i].key, "verbose")) {
  510. verbose = true;
  511. } else {
  512. error(G_("unknown option '-fplugin-arg-%s-%s'"),
  513. plugin_name, argv[i].key);
  514. return 1;
  515. }
  516. }
  517. if (disable) {
  518. if (verbose)
  519. fprintf(stderr, "stackleak: disabled for this translation unit\n");
  520. return 0;
  521. }
  522. /* Give the information about the plugin */
  523. register_callback(plugin_name, PLUGIN_INFO, NULL,
  524. &stackleak_plugin_info);
  525. /* Register to be called before processing a translation unit */
  526. register_callback(plugin_name, PLUGIN_START_UNIT,
  527. &stackleak_start_unit, NULL);
  528. /* Register an extra GCC garbage collector (GGC) root table */
  529. register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL,
  530. (void *)&gt_ggc_r_gt_stackleak);
  531. /*
  532. * Hook into the Pass Manager to register new gcc passes.
  533. *
  534. * The stack frame size info is available only at the last RTL pass,
  535. * when it's too late to insert complex code like a function call.
  536. * So we register two gcc passes to instrument every function at first
  537. * and remove the unneeded instrumentation later.
  538. */
  539. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
  540. &stackleak_instrument_pass_info);
  541. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
  542. &stackleak_cleanup_pass_info);
  543. return 0;
  544. }