Skip to content

Commit 8dd7054

Browse files
a13xp0p0vkees
authored andcommitted
gcc-plugins/stackleak: Add 'verbose' plugin parameter
Add 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation. For using it add the following to scripts/Makefile.gcc-plugins: gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-verbose Signed-off-by: Alexander Popov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent feee1b8 commit 8dd7054

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

scripts/gcc-plugins/stackleak_plugin.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ __visible int plugin_is_GPL_compatible;
3434
static int track_frame_size = -1;
3535
static bool build_for_x86 = false;
3636
static const char track_function[] = "stackleak_track_stack";
37+
static bool disable = false;
38+
static bool verbose = false;
3739

3840
/*
3941
* Mark these global variables (roots) for gcc garbage collector since
@@ -46,6 +48,7 @@ static struct plugin_info stackleak_plugin_info = {
4648
.help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
4749
"arch=target_arch\tspecify target build arch\n"
4850
"disable\t\tdo not activate the plugin\n"
51+
"verbose\t\tprint info about the instrumentation\n"
4952
};
5053

5154
static void add_stack_tracking_gcall(gimple_stmt_iterator *gsi, bool after)
@@ -102,6 +105,10 @@ static tree get_current_stack_pointer_decl(void)
102105
return var;
103106
}
104107

108+
if (verbose) {
109+
fprintf(stderr, "stackleak: missing current_stack_pointer in %s()\n",
110+
DECL_NAME_POINTER(current_function_decl));
111+
}
105112
return NULL_TREE;
106113
}
107114

@@ -195,6 +202,11 @@ static unsigned int stackleak_instrument_execute(void)
195202
if (!is_alloca(stmt))
196203
continue;
197204

205+
if (verbose) {
206+
fprintf(stderr, "stackleak: be careful, alloca() in %s()\n",
207+
DECL_NAME_POINTER(current_function_decl));
208+
}
209+
198210
/* Insert stackleak_track_stack() call after alloca() */
199211
add_stack_tracking(&gsi, true);
200212
if (bb == entry_bb)
@@ -384,13 +396,31 @@ static bool remove_stack_tracking_gasm(void)
384396
*/
385397
static unsigned int stackleak_cleanup_execute(void)
386398
{
399+
const char *fn = DECL_NAME_POINTER(current_function_decl);
387400
bool removed = false;
388401

389-
if (cfun->calls_alloca)
402+
/*
403+
* Leave stack tracking in functions that call alloca().
404+
* Additional case:
405+
* gcc before version 7 called allocate_dynamic_stack_space() from
406+
* expand_stack_vars() for runtime alignment of constant-sized stack
407+
* variables. That caused cfun->calls_alloca to be set for functions
408+
* that in fact don't use alloca().
409+
* For more info see gcc commit 7072df0aae0c59ae437e.
410+
* Let's leave such functions instrumented as well.
411+
*/
412+
if (cfun->calls_alloca) {
413+
if (verbose)
414+
fprintf(stderr, "stackleak: instrument %s(): calls_alloca\n", fn);
390415
return 0;
416+
}
391417

392-
if (large_stack_frame())
418+
/* Leave stack tracking in functions with large stack frame */
419+
if (large_stack_frame()) {
420+
if (verbose)
421+
fprintf(stderr, "stackleak: instrument %s()\n", fn);
393422
return 0;
423+
}
394424

395425
if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
396426
removed = remove_stack_tracking_gasm();
@@ -516,9 +546,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
516546

517547
/* Parse the plugin arguments */
518548
for (i = 0; i < argc; i++) {
519-
if (!strcmp(argv[i].key, "disable"))
520-
return 0;
521-
522549
if (!strcmp(argv[i].key, "track-min-size")) {
523550
if (!argv[i].value) {
524551
error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
@@ -541,13 +568,23 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
541568

542569
if (!strcmp(argv[i].value, "x86"))
543570
build_for_x86 = true;
571+
} else if (!strcmp(argv[i].key, "disable")) {
572+
disable = true;
573+
} else if (!strcmp(argv[i].key, "verbose")) {
574+
verbose = true;
544575
} else {
545576
error(G_("unknown option '-fplugin-arg-%s-%s'"),
546577
plugin_name, argv[i].key);
547578
return 1;
548579
}
549580
}
550581

582+
if (disable) {
583+
if (verbose)
584+
fprintf(stderr, "stackleak: disabled for this translation unit\n");
585+
return 0;
586+
}
587+
551588
/* Give the information about the plugin */
552589
register_callback(plugin_name, PLUGIN_INFO, NULL,
553590
&stackleak_plugin_info);

0 commit comments

Comments
 (0)