0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include "gcc-common.h"
0031
0032 __visible int plugin_is_GPL_compatible;
0033
0034 static int track_frame_size = -1;
0035 static bool build_for_x86 = false;
0036 static const char track_function[] = "stackleak_track_stack";
0037 static bool disable = false;
0038 static bool verbose = false;
0039
0040
0041
0042
0043
0044 static GTY(()) tree track_function_decl;
0045
0046 static struct plugin_info stackleak_plugin_info = {
0047 .version = PLUGIN_VERSION,
0048 .help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
0049 "arch=target_arch\tspecify target build arch\n"
0050 "disable\t\tdo not activate the plugin\n"
0051 "verbose\t\tprint info about the instrumentation\n"
0052 };
0053
0054 static void add_stack_tracking_gcall(gimple_stmt_iterator *gsi, bool after)
0055 {
0056 gimple stmt;
0057 gcall *gimple_call;
0058 cgraph_node_ptr node;
0059 basic_block bb;
0060
0061
0062 stmt = gimple_build_call(track_function_decl, 0);
0063 gimple_call = as_a_gcall(stmt);
0064 if (after)
0065 gsi_insert_after(gsi, gimple_call, GSI_CONTINUE_LINKING);
0066 else
0067 gsi_insert_before(gsi, gimple_call, GSI_SAME_STMT);
0068
0069
0070 bb = gimple_bb(gimple_call);
0071 node = cgraph_get_create_node(track_function_decl);
0072 gcc_assert(node);
0073 cgraph_create_edge(cgraph_get_node(current_function_decl), node,
0074 gimple_call, bb->count,
0075 compute_call_stmt_bb_frequency(current_function_decl, bb));
0076 }
0077
0078 static bool is_alloca(gimple stmt)
0079 {
0080 if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA))
0081 return true;
0082
0083 if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
0084 return true;
0085
0086 return false;
0087 }
0088
0089 static tree get_current_stack_pointer_decl(void)
0090 {
0091 varpool_node_ptr node;
0092
0093 FOR_EACH_VARIABLE(node) {
0094 tree var = NODE_DECL(node);
0095 tree name = DECL_NAME(var);
0096
0097 if (DECL_NAME_LENGTH(var) != sizeof("current_stack_pointer") - 1)
0098 continue;
0099
0100 if (strcmp(IDENTIFIER_POINTER(name), "current_stack_pointer"))
0101 continue;
0102
0103 return var;
0104 }
0105
0106 if (verbose) {
0107 fprintf(stderr, "stackleak: missing current_stack_pointer in %s()\n",
0108 DECL_NAME_POINTER(current_function_decl));
0109 }
0110 return NULL_TREE;
0111 }
0112
0113 static void add_stack_tracking_gasm(gimple_stmt_iterator *gsi, bool after)
0114 {
0115 gasm *asm_call = NULL;
0116 tree sp_decl, input;
0117 vec<tree, va_gc> *inputs = NULL;
0118
0119
0120 gcc_assert(build_for_x86);
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 sp_decl = get_current_stack_pointer_decl();
0133 if (sp_decl == NULL_TREE) {
0134 add_stack_tracking_gcall(gsi, after);
0135 return;
0136 }
0137 input = build_tree_list(NULL_TREE, build_const_char_string(2, "r"));
0138 input = chainon(NULL_TREE, build_tree_list(input, sp_decl));
0139 vec_safe_push(inputs, input);
0140 asm_call = gimple_build_asm_vec("call stackleak_track_stack",
0141 inputs, NULL, NULL, NULL);
0142 gimple_asm_set_volatile(asm_call, true);
0143 if (after)
0144 gsi_insert_after(gsi, asm_call, GSI_CONTINUE_LINKING);
0145 else
0146 gsi_insert_before(gsi, asm_call, GSI_SAME_STMT);
0147 update_stmt(asm_call);
0148 }
0149
0150 static void add_stack_tracking(gimple_stmt_iterator *gsi, bool after)
0151 {
0152
0153
0154
0155
0156
0157
0158
0159
0160 if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
0161 add_stack_tracking_gasm(gsi, after);
0162 else
0163 add_stack_tracking_gcall(gsi, after);
0164 }
0165
0166
0167
0168
0169
0170
0171 static unsigned int stackleak_instrument_execute(void)
0172 {
0173 basic_block bb, entry_bb;
0174 bool prologue_instrumented = false, is_leaf = true;
0175 gimple_stmt_iterator gsi = { 0 };
0176
0177
0178
0179
0180
0181
0182 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
0183 entry_bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
0184
0185
0186
0187
0188
0189
0190 FOR_EACH_BB_FN(bb, cfun) {
0191 for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
0192 gimple stmt;
0193
0194 stmt = gsi_stmt(gsi);
0195
0196
0197 if (is_gimple_call(stmt))
0198 is_leaf = false;
0199
0200 if (!is_alloca(stmt))
0201 continue;
0202
0203 if (verbose) {
0204 fprintf(stderr, "stackleak: be careful, alloca() in %s()\n",
0205 DECL_NAME_POINTER(current_function_decl));
0206 }
0207
0208
0209 add_stack_tracking(&gsi, true);
0210 if (bb == entry_bb)
0211 prologue_instrumented = true;
0212 }
0213 }
0214
0215 if (prologue_instrumented)
0216 return 0;
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 if (is_leaf &&
0233 !TREE_PUBLIC(current_function_decl) &&
0234 DECL_DECLARED_INLINE_P(current_function_decl)) {
0235 return 0;
0236 }
0237
0238 if (is_leaf &&
0239 !strncmp(IDENTIFIER_POINTER(DECL_NAME(current_function_decl)),
0240 "_paravirt_", 10)) {
0241 return 0;
0242 }
0243
0244
0245 bb = entry_bb;
0246 if (!single_pred_p(bb)) {
0247
0248
0249 split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
0250 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
0251 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
0252 }
0253 gsi = gsi_after_labels(bb);
0254 add_stack_tracking(&gsi, false);
0255
0256 return 0;
0257 }
0258
0259 static bool large_stack_frame(void)
0260 {
0261 #if BUILDING_GCC_VERSION >= 8000
0262 return maybe_ge(get_frame_size(), track_frame_size);
0263 #else
0264 return (get_frame_size() >= track_frame_size);
0265 #endif
0266 }
0267
0268 static void remove_stack_tracking_gcall(void)
0269 {
0270 rtx_insn *insn, *next;
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 for (insn = get_insns(); insn; insn = next) {
0284 rtx body;
0285
0286 next = NEXT_INSN(insn);
0287
0288
0289 if (!CALL_P(insn))
0290 continue;
0291
0292
0293
0294
0295
0296
0297 body = PATTERN(insn);
0298
0299 if (GET_CODE(body) == PARALLEL)
0300 body = XVECEXP(body, 0, 0);
0301
0302 if (GET_CODE(body) != CALL)
0303 continue;
0304
0305
0306
0307
0308
0309
0310 body = XEXP(body, 0);
0311 if (GET_CODE(body) != MEM)
0312 continue;
0313
0314 body = XEXP(body, 0);
0315 if (GET_CODE(body) != SYMBOL_REF)
0316 continue;
0317
0318 if (SYMBOL_REF_DECL(body) != track_function_decl)
0319 continue;
0320
0321
0322 delete_insn_and_edges(insn);
0323 #if BUILDING_GCC_VERSION < 8000
0324 if (GET_CODE(next) == NOTE &&
0325 NOTE_KIND(next) == NOTE_INSN_CALL_ARG_LOCATION) {
0326 insn = next;
0327 next = NEXT_INSN(insn);
0328 delete_insn_and_edges(insn);
0329 }
0330 #endif
0331 }
0332 }
0333
0334 static bool remove_stack_tracking_gasm(void)
0335 {
0336 bool removed = false;
0337 rtx_insn *insn, *next;
0338
0339
0340 gcc_assert(build_for_x86);
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353 for (insn = get_insns(); insn; insn = next) {
0354 rtx body;
0355
0356 next = NEXT_INSN(insn);
0357
0358
0359 if (!NONJUMP_INSN_P(insn))
0360 continue;
0361
0362
0363
0364
0365
0366
0367 body = PATTERN(insn);
0368
0369 if (GET_CODE(body) != PARALLEL)
0370 continue;
0371
0372 body = XVECEXP(body, 0, 0);
0373
0374 if (GET_CODE(body) != ASM_OPERANDS)
0375 continue;
0376
0377 if (strcmp(ASM_OPERANDS_TEMPLATE(body),
0378 "call stackleak_track_stack")) {
0379 continue;
0380 }
0381
0382 delete_insn_and_edges(insn);
0383 gcc_assert(!removed);
0384 removed = true;
0385 }
0386
0387 return removed;
0388 }
0389
0390
0391
0392
0393
0394
0395 static unsigned int stackleak_cleanup_execute(void)
0396 {
0397 const char *fn = DECL_NAME_POINTER(current_function_decl);
0398 bool removed = false;
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410 if (cfun->calls_alloca) {
0411 if (verbose)
0412 fprintf(stderr, "stackleak: instrument %s(): calls_alloca\n", fn);
0413 return 0;
0414 }
0415
0416
0417 if (large_stack_frame()) {
0418 if (verbose)
0419 fprintf(stderr, "stackleak: instrument %s()\n", fn);
0420 return 0;
0421 }
0422
0423 if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
0424 removed = remove_stack_tracking_gasm();
0425
0426 if (!removed)
0427 remove_stack_tracking_gcall();
0428
0429 return 0;
0430 }
0431
0432
0433
0434
0435
0436 static inline bool string_equal(tree node, const char *string, int length)
0437 {
0438 if (TREE_STRING_LENGTH(node) < length)
0439 return false;
0440 if (TREE_STRING_LENGTH(node) > length + 1)
0441 return false;
0442 if (TREE_STRING_LENGTH(node) == length + 1 &&
0443 TREE_STRING_POINTER(node)[length] != '\0')
0444 return false;
0445 return !memcmp(TREE_STRING_POINTER(node), string, length);
0446 }
0447 #define STRING_EQUAL(node, str) string_equal(node, str, strlen(str))
0448
0449 static bool stackleak_gate(void)
0450 {
0451 tree section;
0452
0453 section = lookup_attribute("section",
0454 DECL_ATTRIBUTES(current_function_decl));
0455 if (section && TREE_VALUE(section)) {
0456 section = TREE_VALUE(TREE_VALUE(section));
0457
0458 if (STRING_EQUAL(section, ".init.text"))
0459 return false;
0460 if (STRING_EQUAL(section, ".devinit.text"))
0461 return false;
0462 if (STRING_EQUAL(section, ".cpuinit.text"))
0463 return false;
0464 if (STRING_EQUAL(section, ".meminit.text"))
0465 return false;
0466 if (STRING_EQUAL(section, ".noinstr.text"))
0467 return false;
0468 if (STRING_EQUAL(section, ".entry.text"))
0469 return false;
0470 }
0471
0472 return track_frame_size >= 0;
0473 }
0474
0475
0476 static void stackleak_start_unit(void *gcc_data __unused,
0477 void *user_data __unused)
0478 {
0479 tree fntype;
0480
0481
0482 fntype = build_function_type_list(void_type_node, NULL_TREE);
0483 track_function_decl = build_fn_decl(track_function, fntype);
0484 DECL_ASSEMBLER_NAME(track_function_decl);
0485 TREE_PUBLIC(track_function_decl) = 1;
0486 TREE_USED(track_function_decl) = 1;
0487 DECL_EXTERNAL(track_function_decl) = 1;
0488 DECL_ARTIFICIAL(track_function_decl) = 1;
0489 DECL_PRESERVE_P(track_function_decl) = 1;
0490 }
0491
0492
0493
0494
0495
0496
0497 static bool stackleak_instrument_gate(void)
0498 {
0499 return stackleak_gate();
0500 }
0501
0502 #define PASS_NAME stackleak_instrument
0503 #define PROPERTIES_REQUIRED PROP_gimple_leh | PROP_cfg
0504 #define TODO_FLAGS_START TODO_verify_ssa | TODO_verify_flow | TODO_verify_stmts
0505 #define TODO_FLAGS_FINISH TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func \
0506 | TODO_update_ssa | TODO_rebuild_cgraph_edges
0507 #include "gcc-generate-gimple-pass.h"
0508
0509 static bool stackleak_cleanup_gate(void)
0510 {
0511 return stackleak_gate();
0512 }
0513
0514 #define PASS_NAME stackleak_cleanup
0515 #define TODO_FLAGS_FINISH TODO_dump_func
0516 #include "gcc-generate-rtl-pass.h"
0517
0518
0519
0520
0521
0522
0523 __visible int plugin_init(struct plugin_name_args *plugin_info,
0524 struct plugin_gcc_version *version)
0525 {
0526 const char * const plugin_name = plugin_info->base_name;
0527 const int argc = plugin_info->argc;
0528 const struct plugin_argument * const argv = plugin_info->argv;
0529 int i = 0;
0530
0531
0532 static const struct ggc_root_tab gt_ggc_r_gt_stackleak[] = {
0533 {
0534 .base = &track_function_decl,
0535 .nelt = 1,
0536 .stride = sizeof(track_function_decl),
0537 .cb = >_ggc_mx_tree_node,
0538 .pchw = >_pch_nx_tree_node
0539 },
0540 LAST_GGC_ROOT_TAB
0541 };
0542
0543
0544
0545
0546
0547
0548
0549
0550 PASS_INFO(stackleak_instrument, "optimized", 1,
0551 PASS_POS_INSERT_BEFORE);
0552
0553
0554
0555
0556
0557
0558
0559 PASS_INFO(stackleak_cleanup, "*free_cfg", 1, PASS_POS_INSERT_BEFORE);
0560
0561 if (!plugin_default_version_check(version, &gcc_version)) {
0562 error(G_("incompatible gcc/plugin versions"));
0563 return 1;
0564 }
0565
0566
0567 for (i = 0; i < argc; i++) {
0568 if (!strcmp(argv[i].key, "track-min-size")) {
0569 if (!argv[i].value) {
0570 error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
0571 plugin_name, argv[i].key);
0572 return 1;
0573 }
0574
0575 track_frame_size = atoi(argv[i].value);
0576 if (track_frame_size < 0) {
0577 error(G_("invalid option argument '-fplugin-arg-%s-%s=%s'"),
0578 plugin_name, argv[i].key, argv[i].value);
0579 return 1;
0580 }
0581 } else if (!strcmp(argv[i].key, "arch")) {
0582 if (!argv[i].value) {
0583 error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
0584 plugin_name, argv[i].key);
0585 return 1;
0586 }
0587
0588 if (!strcmp(argv[i].value, "x86"))
0589 build_for_x86 = true;
0590 } else if (!strcmp(argv[i].key, "disable")) {
0591 disable = true;
0592 } else if (!strcmp(argv[i].key, "verbose")) {
0593 verbose = true;
0594 } else {
0595 error(G_("unknown option '-fplugin-arg-%s-%s'"),
0596 plugin_name, argv[i].key);
0597 return 1;
0598 }
0599 }
0600
0601 if (disable) {
0602 if (verbose)
0603 fprintf(stderr, "stackleak: disabled for this translation unit\n");
0604 return 0;
0605 }
0606
0607
0608 register_callback(plugin_name, PLUGIN_INFO, NULL,
0609 &stackleak_plugin_info);
0610
0611
0612 register_callback(plugin_name, PLUGIN_START_UNIT,
0613 &stackleak_start_unit, NULL);
0614
0615
0616 register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL,
0617 (void *)>_ggc_r_gt_stackleak);
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
0628 &stackleak_instrument_pass_info);
0629 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
0630 &stackleak_cleanup_pass_info);
0631
0632 return 0;
0633 }