18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2011-2017 by the PaX Team <pageexec@freemail.hu>
38c2ecf20Sopenharmony_ci * Modified by Alexander Popov <alex.popov@linux.com>
48c2ecf20Sopenharmony_ci * Licensed under the GPL v2
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Note: the choice of the license means that the compilation process is
78c2ecf20Sopenharmony_ci * NOT 'eligible' as defined by gcc's library exception to the GPL v3,
88c2ecf20Sopenharmony_ci * but for the kernel it doesn't matter since it doesn't link against
98c2ecf20Sopenharmony_ci * any of the gcc libraries
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This gcc plugin is needed for tracking the lowest border of the kernel stack.
128c2ecf20Sopenharmony_ci * It instruments the kernel code inserting stackleak_track_stack() calls:
138c2ecf20Sopenharmony_ci *  - after alloca();
148c2ecf20Sopenharmony_ci *  - for the functions with a stack frame size greater than or equal
158c2ecf20Sopenharmony_ci *     to the "track-min-size" plugin parameter.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * This plugin is ported from grsecurity/PaX. For more information see:
188c2ecf20Sopenharmony_ci *   https://grsecurity.net/
198c2ecf20Sopenharmony_ci *   https://pax.grsecurity.net/
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * Debugging:
228c2ecf20Sopenharmony_ci *  - use fprintf() to stderr, debug_generic_expr(), debug_gimple_stmt(),
238c2ecf20Sopenharmony_ci *     print_rtl_single() and debug_rtx();
248c2ecf20Sopenharmony_ci *  - add "-fdump-tree-all -fdump-rtl-all" to the plugin CFLAGS in
258c2ecf20Sopenharmony_ci *     Makefile.gcc-plugins to see the verbose dumps of the gcc passes;
268c2ecf20Sopenharmony_ci *  - use gcc -E to understand the preprocessing shenanigans;
278c2ecf20Sopenharmony_ci *  - use gcc with enabled CFG/GIMPLE/SSA verification (--enable-checking).
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "gcc-common.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci__visible int plugin_is_GPL_compatible;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int track_frame_size = -1;
358c2ecf20Sopenharmony_cistatic bool build_for_x86 = false;
368c2ecf20Sopenharmony_cistatic const char track_function[] = "stackleak_track_stack";
378c2ecf20Sopenharmony_cistatic bool disable = false;
388c2ecf20Sopenharmony_cistatic bool verbose = false;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Mark these global variables (roots) for gcc garbage collector since
428c2ecf20Sopenharmony_ci * they point to the garbage-collected memory.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistatic GTY(()) tree track_function_decl;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct plugin_info stackleak_plugin_info = {
478c2ecf20Sopenharmony_ci	.version = "201707101337",
488c2ecf20Sopenharmony_ci	.help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
498c2ecf20Sopenharmony_ci		"arch=target_arch\tspecify target build arch\n"
508c2ecf20Sopenharmony_ci		"disable\t\tdo not activate the plugin\n"
518c2ecf20Sopenharmony_ci		"verbose\t\tprint info about the instrumentation\n"
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void add_stack_tracking_gcall(gimple_stmt_iterator *gsi, bool after)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	gimple stmt;
578c2ecf20Sopenharmony_ci	gcall *gimple_call;
588c2ecf20Sopenharmony_ci	cgraph_node_ptr node;
598c2ecf20Sopenharmony_ci	basic_block bb;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	/* Insert calling stackleak_track_stack() */
628c2ecf20Sopenharmony_ci	stmt = gimple_build_call(track_function_decl, 0);
638c2ecf20Sopenharmony_ci	gimple_call = as_a_gcall(stmt);
648c2ecf20Sopenharmony_ci	if (after)
658c2ecf20Sopenharmony_ci		gsi_insert_after(gsi, gimple_call, GSI_CONTINUE_LINKING);
668c2ecf20Sopenharmony_ci	else
678c2ecf20Sopenharmony_ci		gsi_insert_before(gsi, gimple_call, GSI_SAME_STMT);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* Update the cgraph */
708c2ecf20Sopenharmony_ci	bb = gimple_bb(gimple_call);
718c2ecf20Sopenharmony_ci	node = cgraph_get_create_node(track_function_decl);
728c2ecf20Sopenharmony_ci	gcc_assert(node);
738c2ecf20Sopenharmony_ci	cgraph_create_edge(cgraph_get_node(current_function_decl), node,
748c2ecf20Sopenharmony_ci			gimple_call, bb->count,
758c2ecf20Sopenharmony_ci			compute_call_stmt_bb_frequency(current_function_decl, bb));
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic bool is_alloca(gimple stmt)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA))
818c2ecf20Sopenharmony_ci		return true;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION >= 4007
848c2ecf20Sopenharmony_ci	if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
858c2ecf20Sopenharmony_ci		return true;
868c2ecf20Sopenharmony_ci#endif
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return false;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic tree get_current_stack_pointer_decl(void)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	varpool_node_ptr node;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	FOR_EACH_VARIABLE(node) {
968c2ecf20Sopenharmony_ci		tree var = NODE_DECL(node);
978c2ecf20Sopenharmony_ci		tree name = DECL_NAME(var);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		if (DECL_NAME_LENGTH(var) != sizeof("current_stack_pointer") - 1)
1008c2ecf20Sopenharmony_ci			continue;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		if (strcmp(IDENTIFIER_POINTER(name), "current_stack_pointer"))
1038c2ecf20Sopenharmony_ci			continue;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		return var;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (verbose) {
1098c2ecf20Sopenharmony_ci		fprintf(stderr, "stackleak: missing current_stack_pointer in %s()\n",
1108c2ecf20Sopenharmony_ci			DECL_NAME_POINTER(current_function_decl));
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci	return NULL_TREE;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic void add_stack_tracking_gasm(gimple_stmt_iterator *gsi, bool after)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	gasm *asm_call = NULL;
1188c2ecf20Sopenharmony_ci	tree sp_decl, input;
1198c2ecf20Sopenharmony_ci	vec<tree, va_gc> *inputs = NULL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* 'no_caller_saved_registers' is currently supported only for x86 */
1228c2ecf20Sopenharmony_ci	gcc_assert(build_for_x86);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/*
1258c2ecf20Sopenharmony_ci	 * Insert calling stackleak_track_stack() in asm:
1268c2ecf20Sopenharmony_ci	 *   asm volatile("call stackleak_track_stack"
1278c2ecf20Sopenharmony_ci	 *		  :: "r" (current_stack_pointer))
1288c2ecf20Sopenharmony_ci	 * Use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h.
1298c2ecf20Sopenharmony_ci	 * This constraint is taken into account during gcc shrink-wrapping
1308c2ecf20Sopenharmony_ci	 * optimization. It is needed to be sure that stackleak_track_stack()
1318c2ecf20Sopenharmony_ci	 * call is inserted after the prologue of the containing function,
1328c2ecf20Sopenharmony_ci	 * when the stack frame is prepared.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	sp_decl = get_current_stack_pointer_decl();
1358c2ecf20Sopenharmony_ci	if (sp_decl == NULL_TREE) {
1368c2ecf20Sopenharmony_ci		add_stack_tracking_gcall(gsi, after);
1378c2ecf20Sopenharmony_ci		return;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	input = build_tree_list(NULL_TREE, build_const_char_string(2, "r"));
1408c2ecf20Sopenharmony_ci	input = chainon(NULL_TREE, build_tree_list(input, sp_decl));
1418c2ecf20Sopenharmony_ci	vec_safe_push(inputs, input);
1428c2ecf20Sopenharmony_ci	asm_call = gimple_build_asm_vec("call stackleak_track_stack",
1438c2ecf20Sopenharmony_ci					inputs, NULL, NULL, NULL);
1448c2ecf20Sopenharmony_ci	gimple_asm_set_volatile(asm_call, true);
1458c2ecf20Sopenharmony_ci	if (after)
1468c2ecf20Sopenharmony_ci		gsi_insert_after(gsi, asm_call, GSI_CONTINUE_LINKING);
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		gsi_insert_before(gsi, asm_call, GSI_SAME_STMT);
1498c2ecf20Sopenharmony_ci	update_stmt(asm_call);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void add_stack_tracking(gimple_stmt_iterator *gsi, bool after)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	/*
1558c2ecf20Sopenharmony_ci	 * The 'no_caller_saved_registers' attribute is used for
1568c2ecf20Sopenharmony_ci	 * stackleak_track_stack(). If the compiler supports this attribute for
1578c2ecf20Sopenharmony_ci	 * the target arch, we can add calling stackleak_track_stack() in asm.
1588c2ecf20Sopenharmony_ci	 * That improves performance: we avoid useless operations with the
1598c2ecf20Sopenharmony_ci	 * caller-saved registers in the functions from which we will remove
1608c2ecf20Sopenharmony_ci	 * stackleak_track_stack() call during the stackleak_cleanup pass.
1618c2ecf20Sopenharmony_ci	 */
1628c2ecf20Sopenharmony_ci	if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
1638c2ecf20Sopenharmony_ci		add_stack_tracking_gasm(gsi, after);
1648c2ecf20Sopenharmony_ci	else
1658c2ecf20Sopenharmony_ci		add_stack_tracking_gcall(gsi, after);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Work with the GIMPLE representation of the code. Insert the
1708c2ecf20Sopenharmony_ci * stackleak_track_stack() call after alloca() and into the beginning
1718c2ecf20Sopenharmony_ci * of the function if it is not instrumented.
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_cistatic unsigned int stackleak_instrument_execute(void)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	basic_block bb, entry_bb;
1768c2ecf20Sopenharmony_ci	bool prologue_instrumented = false, is_leaf = true;
1778c2ecf20Sopenharmony_ci	gimple_stmt_iterator gsi = { 0 };
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/*
1808c2ecf20Sopenharmony_ci	 * ENTRY_BLOCK_PTR is a basic block which represents possible entry
1818c2ecf20Sopenharmony_ci	 * point of a function. This block does not contain any code and
1828c2ecf20Sopenharmony_ci	 * has a CFG edge to its successor.
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
1858c2ecf20Sopenharmony_ci	entry_bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/*
1888c2ecf20Sopenharmony_ci	 * Loop through the GIMPLE statements in each of cfun basic blocks.
1898c2ecf20Sopenharmony_ci	 * cfun is a global variable which represents the function that is
1908c2ecf20Sopenharmony_ci	 * currently processed.
1918c2ecf20Sopenharmony_ci	 */
1928c2ecf20Sopenharmony_ci	FOR_EACH_BB_FN(bb, cfun) {
1938c2ecf20Sopenharmony_ci		for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
1948c2ecf20Sopenharmony_ci			gimple stmt;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci			stmt = gsi_stmt(gsi);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci			/* Leaf function is a function which makes no calls */
1998c2ecf20Sopenharmony_ci			if (is_gimple_call(stmt))
2008c2ecf20Sopenharmony_ci				is_leaf = false;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci			if (!is_alloca(stmt))
2038c2ecf20Sopenharmony_ci				continue;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci			if (verbose) {
2068c2ecf20Sopenharmony_ci				fprintf(stderr, "stackleak: be careful, alloca() in %s()\n",
2078c2ecf20Sopenharmony_ci					DECL_NAME_POINTER(current_function_decl));
2088c2ecf20Sopenharmony_ci			}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci			/* Insert stackleak_track_stack() call after alloca() */
2118c2ecf20Sopenharmony_ci			add_stack_tracking(&gsi, true);
2128c2ecf20Sopenharmony_ci			if (bb == entry_bb)
2138c2ecf20Sopenharmony_ci				prologue_instrumented = true;
2148c2ecf20Sopenharmony_ci		}
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (prologue_instrumented)
2188c2ecf20Sopenharmony_ci		return 0;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/*
2218c2ecf20Sopenharmony_ci	 * Special cases to skip the instrumentation.
2228c2ecf20Sopenharmony_ci	 *
2238c2ecf20Sopenharmony_ci	 * Taking the address of static inline functions materializes them,
2248c2ecf20Sopenharmony_ci	 * but we mustn't instrument some of them as the resulting stack
2258c2ecf20Sopenharmony_ci	 * alignment required by the function call ABI will break other
2268c2ecf20Sopenharmony_ci	 * assumptions regarding the expected (but not otherwise enforced)
2278c2ecf20Sopenharmony_ci	 * register clobbering ABI.
2288c2ecf20Sopenharmony_ci	 *
2298c2ecf20Sopenharmony_ci	 * Case in point: native_save_fl on amd64 when optimized for size
2308c2ecf20Sopenharmony_ci	 * clobbers rdx if it were instrumented here.
2318c2ecf20Sopenharmony_ci	 *
2328c2ecf20Sopenharmony_ci	 * TODO: any more special cases?
2338c2ecf20Sopenharmony_ci	 */
2348c2ecf20Sopenharmony_ci	if (is_leaf &&
2358c2ecf20Sopenharmony_ci	    !TREE_PUBLIC(current_function_decl) &&
2368c2ecf20Sopenharmony_ci	    DECL_DECLARED_INLINE_P(current_function_decl)) {
2378c2ecf20Sopenharmony_ci		return 0;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (is_leaf &&
2418c2ecf20Sopenharmony_ci	    !strncmp(IDENTIFIER_POINTER(DECL_NAME(current_function_decl)),
2428c2ecf20Sopenharmony_ci		     "_paravirt_", 10)) {
2438c2ecf20Sopenharmony_ci		return 0;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* Insert stackleak_track_stack() call at the function beginning */
2478c2ecf20Sopenharmony_ci	bb = entry_bb;
2488c2ecf20Sopenharmony_ci	if (!single_pred_p(bb)) {
2498c2ecf20Sopenharmony_ci		/* gcc_assert(bb_loop_depth(bb) ||
2508c2ecf20Sopenharmony_ci				(bb->flags & BB_IRREDUCIBLE_LOOP)); */
2518c2ecf20Sopenharmony_ci		split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
2528c2ecf20Sopenharmony_ci		gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
2538c2ecf20Sopenharmony_ci		bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci	gsi = gsi_after_labels(bb);
2568c2ecf20Sopenharmony_ci	add_stack_tracking(&gsi, false);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic bool large_stack_frame(void)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION >= 8000
2648c2ecf20Sopenharmony_ci	return maybe_ge(get_frame_size(), track_frame_size);
2658c2ecf20Sopenharmony_ci#else
2668c2ecf20Sopenharmony_ci	return (get_frame_size() >= track_frame_size);
2678c2ecf20Sopenharmony_ci#endif
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic void remove_stack_tracking_gcall(void)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	rtx_insn *insn, *next;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/*
2758c2ecf20Sopenharmony_ci	 * Find stackleak_track_stack() calls. Loop through the chain of insns,
2768c2ecf20Sopenharmony_ci	 * which is an RTL representation of the code for a function.
2778c2ecf20Sopenharmony_ci	 *
2788c2ecf20Sopenharmony_ci	 * The example of a matching insn:
2798c2ecf20Sopenharmony_ci	 *  (call_insn 8 4 10 2 (call (mem (symbol_ref ("stackleak_track_stack")
2808c2ecf20Sopenharmony_ci	 *  [flags 0x41] <function_decl 0x7f7cd3302a80 stackleak_track_stack>)
2818c2ecf20Sopenharmony_ci	 *  [0 stackleak_track_stack S1 A8]) (0)) 675 {*call} (expr_list
2828c2ecf20Sopenharmony_ci	 *  (symbol_ref ("stackleak_track_stack") [flags 0x41] <function_decl
2838c2ecf20Sopenharmony_ci	 *  0x7f7cd3302a80 stackleak_track_stack>) (expr_list (0) (nil))) (nil))
2848c2ecf20Sopenharmony_ci	 */
2858c2ecf20Sopenharmony_ci	for (insn = get_insns(); insn; insn = next) {
2868c2ecf20Sopenharmony_ci		rtx body;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		next = NEXT_INSN(insn);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		/* Check the expression code of the insn */
2918c2ecf20Sopenharmony_ci		if (!CALL_P(insn))
2928c2ecf20Sopenharmony_ci			continue;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		/*
2958c2ecf20Sopenharmony_ci		 * Check the expression code of the insn body, which is an RTL
2968c2ecf20Sopenharmony_ci		 * Expression (RTX) describing the side effect performed by
2978c2ecf20Sopenharmony_ci		 * that insn.
2988c2ecf20Sopenharmony_ci		 */
2998c2ecf20Sopenharmony_ci		body = PATTERN(insn);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		if (GET_CODE(body) == PARALLEL)
3028c2ecf20Sopenharmony_ci			body = XVECEXP(body, 0, 0);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci		if (GET_CODE(body) != CALL)
3058c2ecf20Sopenharmony_ci			continue;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		/*
3088c2ecf20Sopenharmony_ci		 * Check the first operand of the call expression. It should
3098c2ecf20Sopenharmony_ci		 * be a mem RTX describing the needed subroutine with a
3108c2ecf20Sopenharmony_ci		 * symbol_ref RTX.
3118c2ecf20Sopenharmony_ci		 */
3128c2ecf20Sopenharmony_ci		body = XEXP(body, 0);
3138c2ecf20Sopenharmony_ci		if (GET_CODE(body) != MEM)
3148c2ecf20Sopenharmony_ci			continue;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci		body = XEXP(body, 0);
3178c2ecf20Sopenharmony_ci		if (GET_CODE(body) != SYMBOL_REF)
3188c2ecf20Sopenharmony_ci			continue;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		if (SYMBOL_REF_DECL(body) != track_function_decl)
3218c2ecf20Sopenharmony_ci			continue;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		/* Delete the stackleak_track_stack() call */
3248c2ecf20Sopenharmony_ci		delete_insn_and_edges(insn);
3258c2ecf20Sopenharmony_ci#if BUILDING_GCC_VERSION >= 4007 && BUILDING_GCC_VERSION < 8000
3268c2ecf20Sopenharmony_ci		if (GET_CODE(next) == NOTE &&
3278c2ecf20Sopenharmony_ci		    NOTE_KIND(next) == NOTE_INSN_CALL_ARG_LOCATION) {
3288c2ecf20Sopenharmony_ci			insn = next;
3298c2ecf20Sopenharmony_ci			next = NEXT_INSN(insn);
3308c2ecf20Sopenharmony_ci			delete_insn_and_edges(insn);
3318c2ecf20Sopenharmony_ci		}
3328c2ecf20Sopenharmony_ci#endif
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic bool remove_stack_tracking_gasm(void)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	bool removed = false;
3398c2ecf20Sopenharmony_ci	rtx_insn *insn, *next;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* 'no_caller_saved_registers' is currently supported only for x86 */
3428c2ecf20Sopenharmony_ci	gcc_assert(build_for_x86);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/*
3458c2ecf20Sopenharmony_ci	 * Find stackleak_track_stack() asm calls. Loop through the chain of
3468c2ecf20Sopenharmony_ci	 * insns, which is an RTL representation of the code for a function.
3478c2ecf20Sopenharmony_ci	 *
3488c2ecf20Sopenharmony_ci	 * The example of a matching insn:
3498c2ecf20Sopenharmony_ci	 *  (insn 11 5 12 2 (parallel [ (asm_operands/v
3508c2ecf20Sopenharmony_ci	 *  ("call stackleak_track_stack") ("") 0
3518c2ecf20Sopenharmony_ci	 *  [ (reg/v:DI 7 sp [ current_stack_pointer ]) ]
3528c2ecf20Sopenharmony_ci	 *  [ (asm_input:DI ("r")) ] [])
3538c2ecf20Sopenharmony_ci	 *  (clobber (reg:CC 17 flags)) ]) -1 (nil))
3548c2ecf20Sopenharmony_ci	 */
3558c2ecf20Sopenharmony_ci	for (insn = get_insns(); insn; insn = next) {
3568c2ecf20Sopenharmony_ci		rtx body;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		next = NEXT_INSN(insn);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		/* Check the expression code of the insn */
3618c2ecf20Sopenharmony_ci		if (!NONJUMP_INSN_P(insn))
3628c2ecf20Sopenharmony_ci			continue;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		/*
3658c2ecf20Sopenharmony_ci		 * Check the expression code of the insn body, which is an RTL
3668c2ecf20Sopenharmony_ci		 * Expression (RTX) describing the side effect performed by
3678c2ecf20Sopenharmony_ci		 * that insn.
3688c2ecf20Sopenharmony_ci		 */
3698c2ecf20Sopenharmony_ci		body = PATTERN(insn);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		if (GET_CODE(body) != PARALLEL)
3728c2ecf20Sopenharmony_ci			continue;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci		body = XVECEXP(body, 0, 0);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci		if (GET_CODE(body) != ASM_OPERANDS)
3778c2ecf20Sopenharmony_ci			continue;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		if (strcmp(ASM_OPERANDS_TEMPLATE(body),
3808c2ecf20Sopenharmony_ci						"call stackleak_track_stack")) {
3818c2ecf20Sopenharmony_ci			continue;
3828c2ecf20Sopenharmony_ci		}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		delete_insn_and_edges(insn);
3858c2ecf20Sopenharmony_ci		gcc_assert(!removed);
3868c2ecf20Sopenharmony_ci		removed = true;
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return removed;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci/*
3938c2ecf20Sopenharmony_ci * Work with the RTL representation of the code.
3948c2ecf20Sopenharmony_ci * Remove the unneeded stackleak_track_stack() calls from the functions
3958c2ecf20Sopenharmony_ci * which don't call alloca() and don't have a large enough stack frame size.
3968c2ecf20Sopenharmony_ci */
3978c2ecf20Sopenharmony_cistatic unsigned int stackleak_cleanup_execute(void)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	const char *fn = DECL_NAME_POINTER(current_function_decl);
4008c2ecf20Sopenharmony_ci	bool removed = false;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/*
4038c2ecf20Sopenharmony_ci	 * Leave stack tracking in functions that call alloca().
4048c2ecf20Sopenharmony_ci	 * Additional case:
4058c2ecf20Sopenharmony_ci	 *   gcc before version 7 called allocate_dynamic_stack_space() from
4068c2ecf20Sopenharmony_ci	 *   expand_stack_vars() for runtime alignment of constant-sized stack
4078c2ecf20Sopenharmony_ci	 *   variables. That caused cfun->calls_alloca to be set for functions
4088c2ecf20Sopenharmony_ci	 *   that in fact don't use alloca().
4098c2ecf20Sopenharmony_ci	 *   For more info see gcc commit 7072df0aae0c59ae437e.
4108c2ecf20Sopenharmony_ci	 *   Let's leave such functions instrumented as well.
4118c2ecf20Sopenharmony_ci	 */
4128c2ecf20Sopenharmony_ci	if (cfun->calls_alloca) {
4138c2ecf20Sopenharmony_ci		if (verbose)
4148c2ecf20Sopenharmony_ci			fprintf(stderr, "stackleak: instrument %s(): calls_alloca\n", fn);
4158c2ecf20Sopenharmony_ci		return 0;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* Leave stack tracking in functions with large stack frame */
4198c2ecf20Sopenharmony_ci	if (large_stack_frame()) {
4208c2ecf20Sopenharmony_ci		if (verbose)
4218c2ecf20Sopenharmony_ci			fprintf(stderr, "stackleak: instrument %s()\n", fn);
4228c2ecf20Sopenharmony_ci		return 0;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
4268c2ecf20Sopenharmony_ci		removed = remove_stack_tracking_gasm();
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (!removed)
4298c2ecf20Sopenharmony_ci		remove_stack_tracking_gcall();
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return 0;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci/*
4358c2ecf20Sopenharmony_ci * STRING_CST may or may not be NUL terminated:
4368c2ecf20Sopenharmony_ci * https://gcc.gnu.org/onlinedocs/gccint/Constant-expressions.html
4378c2ecf20Sopenharmony_ci */
4388c2ecf20Sopenharmony_cistatic inline bool string_equal(tree node, const char *string, int length)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	if (TREE_STRING_LENGTH(node) < length)
4418c2ecf20Sopenharmony_ci		return false;
4428c2ecf20Sopenharmony_ci	if (TREE_STRING_LENGTH(node) > length + 1)
4438c2ecf20Sopenharmony_ci		return false;
4448c2ecf20Sopenharmony_ci	if (TREE_STRING_LENGTH(node) == length + 1 &&
4458c2ecf20Sopenharmony_ci	    TREE_STRING_POINTER(node)[length] != '\0')
4468c2ecf20Sopenharmony_ci		return false;
4478c2ecf20Sopenharmony_ci	return !memcmp(TREE_STRING_POINTER(node), string, length);
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci#define STRING_EQUAL(node, str)	string_equal(node, str, strlen(str))
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic bool stackleak_gate(void)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	tree section;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	section = lookup_attribute("section",
4568c2ecf20Sopenharmony_ci				   DECL_ATTRIBUTES(current_function_decl));
4578c2ecf20Sopenharmony_ci	if (section && TREE_VALUE(section)) {
4588c2ecf20Sopenharmony_ci		section = TREE_VALUE(TREE_VALUE(section));
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		if (STRING_EQUAL(section, ".init.text"))
4618c2ecf20Sopenharmony_ci			return false;
4628c2ecf20Sopenharmony_ci		if (STRING_EQUAL(section, ".devinit.text"))
4638c2ecf20Sopenharmony_ci			return false;
4648c2ecf20Sopenharmony_ci		if (STRING_EQUAL(section, ".cpuinit.text"))
4658c2ecf20Sopenharmony_ci			return false;
4668c2ecf20Sopenharmony_ci		if (STRING_EQUAL(section, ".meminit.text"))
4678c2ecf20Sopenharmony_ci			return false;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	return track_frame_size >= 0;
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci/* Build the function declaration for stackleak_track_stack() */
4748c2ecf20Sopenharmony_cistatic void stackleak_start_unit(void *gcc_data __unused,
4758c2ecf20Sopenharmony_ci				 void *user_data __unused)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	tree fntype;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/* void stackleak_track_stack(void) */
4808c2ecf20Sopenharmony_ci	fntype = build_function_type_list(void_type_node, NULL_TREE);
4818c2ecf20Sopenharmony_ci	track_function_decl = build_fn_decl(track_function, fntype);
4828c2ecf20Sopenharmony_ci	DECL_ASSEMBLER_NAME(track_function_decl); /* for LTO */
4838c2ecf20Sopenharmony_ci	TREE_PUBLIC(track_function_decl) = 1;
4848c2ecf20Sopenharmony_ci	TREE_USED(track_function_decl) = 1;
4858c2ecf20Sopenharmony_ci	DECL_EXTERNAL(track_function_decl) = 1;
4868c2ecf20Sopenharmony_ci	DECL_ARTIFICIAL(track_function_decl) = 1;
4878c2ecf20Sopenharmony_ci	DECL_PRESERVE_P(track_function_decl) = 1;
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/*
4918c2ecf20Sopenharmony_ci * Pass gate function is a predicate function that gets executed before the
4928c2ecf20Sopenharmony_ci * corresponding pass. If the return value is 'true' the pass gets executed,
4938c2ecf20Sopenharmony_ci * otherwise, it is skipped.
4948c2ecf20Sopenharmony_ci */
4958c2ecf20Sopenharmony_cistatic bool stackleak_instrument_gate(void)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	return stackleak_gate();
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci#define PASS_NAME stackleak_instrument
5018c2ecf20Sopenharmony_ci#define PROPERTIES_REQUIRED PROP_gimple_leh | PROP_cfg
5028c2ecf20Sopenharmony_ci#define TODO_FLAGS_START TODO_verify_ssa | TODO_verify_flow | TODO_verify_stmts
5038c2ecf20Sopenharmony_ci#define TODO_FLAGS_FINISH TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func \
5048c2ecf20Sopenharmony_ci			| TODO_update_ssa | TODO_rebuild_cgraph_edges
5058c2ecf20Sopenharmony_ci#include "gcc-generate-gimple-pass.h"
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_cistatic bool stackleak_cleanup_gate(void)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	return stackleak_gate();
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci#define PASS_NAME stackleak_cleanup
5138c2ecf20Sopenharmony_ci#define TODO_FLAGS_FINISH TODO_dump_func
5148c2ecf20Sopenharmony_ci#include "gcc-generate-rtl-pass.h"
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci/*
5178c2ecf20Sopenharmony_ci * Every gcc plugin exports a plugin_init() function that is called right
5188c2ecf20Sopenharmony_ci * after the plugin is loaded. This function is responsible for registering
5198c2ecf20Sopenharmony_ci * the plugin callbacks and doing other required initialization.
5208c2ecf20Sopenharmony_ci */
5218c2ecf20Sopenharmony_ci__visible int plugin_init(struct plugin_name_args *plugin_info,
5228c2ecf20Sopenharmony_ci			  struct plugin_gcc_version *version)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	const char * const plugin_name = plugin_info->base_name;
5258c2ecf20Sopenharmony_ci	const int argc = plugin_info->argc;
5268c2ecf20Sopenharmony_ci	const struct plugin_argument * const argv = plugin_info->argv;
5278c2ecf20Sopenharmony_ci	int i = 0;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* Extra GGC root tables describing our GTY-ed data */
5308c2ecf20Sopenharmony_ci	static const struct ggc_root_tab gt_ggc_r_gt_stackleak[] = {
5318c2ecf20Sopenharmony_ci		{
5328c2ecf20Sopenharmony_ci			.base = &track_function_decl,
5338c2ecf20Sopenharmony_ci			.nelt = 1,
5348c2ecf20Sopenharmony_ci			.stride = sizeof(track_function_decl),
5358c2ecf20Sopenharmony_ci			.cb = &gt_ggc_mx_tree_node,
5368c2ecf20Sopenharmony_ci			.pchw = &gt_pch_nx_tree_node
5378c2ecf20Sopenharmony_ci		},
5388c2ecf20Sopenharmony_ci		LAST_GGC_ROOT_TAB
5398c2ecf20Sopenharmony_ci	};
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/*
5428c2ecf20Sopenharmony_ci	 * The stackleak_instrument pass should be executed before the
5438c2ecf20Sopenharmony_ci	 * "optimized" pass, which is the control flow graph cleanup that is
5448c2ecf20Sopenharmony_ci	 * performed just before expanding gcc trees to the RTL. In former
5458c2ecf20Sopenharmony_ci	 * versions of the plugin this new pass was inserted before the
5468c2ecf20Sopenharmony_ci	 * "tree_profile" pass, which is currently called "profile".
5478c2ecf20Sopenharmony_ci	 */
5488c2ecf20Sopenharmony_ci	PASS_INFO(stackleak_instrument, "optimized", 1,
5498c2ecf20Sopenharmony_ci						PASS_POS_INSERT_BEFORE);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/*
5528c2ecf20Sopenharmony_ci	 * The stackleak_cleanup pass should be executed before the "*free_cfg"
5538c2ecf20Sopenharmony_ci	 * pass. It's the moment when the stack frame size is already final,
5548c2ecf20Sopenharmony_ci	 * function prologues and epilogues are generated, and the
5558c2ecf20Sopenharmony_ci	 * machine-dependent code transformations are not done.
5568c2ecf20Sopenharmony_ci	 */
5578c2ecf20Sopenharmony_ci	PASS_INFO(stackleak_cleanup, "*free_cfg", 1, PASS_POS_INSERT_BEFORE);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	if (!plugin_default_version_check(version, &gcc_version)) {
5608c2ecf20Sopenharmony_ci		error(G_("incompatible gcc/plugin versions"));
5618c2ecf20Sopenharmony_ci		return 1;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* Parse the plugin arguments */
5658c2ecf20Sopenharmony_ci	for (i = 0; i < argc; i++) {
5668c2ecf20Sopenharmony_ci		if (!strcmp(argv[i].key, "track-min-size")) {
5678c2ecf20Sopenharmony_ci			if (!argv[i].value) {
5688c2ecf20Sopenharmony_ci				error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
5698c2ecf20Sopenharmony_ci					plugin_name, argv[i].key);
5708c2ecf20Sopenharmony_ci				return 1;
5718c2ecf20Sopenharmony_ci			}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci			track_frame_size = atoi(argv[i].value);
5748c2ecf20Sopenharmony_ci			if (track_frame_size < 0) {
5758c2ecf20Sopenharmony_ci				error(G_("invalid option argument '-fplugin-arg-%s-%s=%s'"),
5768c2ecf20Sopenharmony_ci					plugin_name, argv[i].key, argv[i].value);
5778c2ecf20Sopenharmony_ci				return 1;
5788c2ecf20Sopenharmony_ci			}
5798c2ecf20Sopenharmony_ci		} else if (!strcmp(argv[i].key, "arch")) {
5808c2ecf20Sopenharmony_ci			if (!argv[i].value) {
5818c2ecf20Sopenharmony_ci				error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
5828c2ecf20Sopenharmony_ci					plugin_name, argv[i].key);
5838c2ecf20Sopenharmony_ci				return 1;
5848c2ecf20Sopenharmony_ci			}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci			if (!strcmp(argv[i].value, "x86"))
5878c2ecf20Sopenharmony_ci				build_for_x86 = true;
5888c2ecf20Sopenharmony_ci		} else if (!strcmp(argv[i].key, "disable")) {
5898c2ecf20Sopenharmony_ci			disable = true;
5908c2ecf20Sopenharmony_ci		} else if (!strcmp(argv[i].key, "verbose")) {
5918c2ecf20Sopenharmony_ci			verbose = true;
5928c2ecf20Sopenharmony_ci		} else {
5938c2ecf20Sopenharmony_ci			error(G_("unknown option '-fplugin-arg-%s-%s'"),
5948c2ecf20Sopenharmony_ci					plugin_name, argv[i].key);
5958c2ecf20Sopenharmony_ci			return 1;
5968c2ecf20Sopenharmony_ci		}
5978c2ecf20Sopenharmony_ci	}
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	if (disable) {
6008c2ecf20Sopenharmony_ci		if (verbose)
6018c2ecf20Sopenharmony_ci			fprintf(stderr, "stackleak: disabled for this translation unit\n");
6028c2ecf20Sopenharmony_ci		return 0;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Give the information about the plugin */
6068c2ecf20Sopenharmony_ci	register_callback(plugin_name, PLUGIN_INFO, NULL,
6078c2ecf20Sopenharmony_ci						&stackleak_plugin_info);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* Register to be called before processing a translation unit */
6108c2ecf20Sopenharmony_ci	register_callback(plugin_name, PLUGIN_START_UNIT,
6118c2ecf20Sopenharmony_ci					&stackleak_start_unit, NULL);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/* Register an extra GCC garbage collector (GGC) root table */
6148c2ecf20Sopenharmony_ci	register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL,
6158c2ecf20Sopenharmony_ci					(void *)&gt_ggc_r_gt_stackleak);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/*
6188c2ecf20Sopenharmony_ci	 * Hook into the Pass Manager to register new gcc passes.
6198c2ecf20Sopenharmony_ci	 *
6208c2ecf20Sopenharmony_ci	 * The stack frame size info is available only at the last RTL pass,
6218c2ecf20Sopenharmony_ci	 * when it's too late to insert complex code like a function call.
6228c2ecf20Sopenharmony_ci	 * So we register two gcc passes to instrument every function at first
6238c2ecf20Sopenharmony_ci	 * and remove the unneeded instrumentation later.
6248c2ecf20Sopenharmony_ci	 */
6258c2ecf20Sopenharmony_ci	register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
6268c2ecf20Sopenharmony_ci					&stackleak_instrument_pass_info);
6278c2ecf20Sopenharmony_ci	register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
6288c2ecf20Sopenharmony_ci					&stackleak_cleanup_pass_info);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return 0;
6318c2ecf20Sopenharmony_ci}
632