mb/google/nissa: Create pujjogatwin variant
[coreboot2.git] / util / crossgcc / patches / gcc-14.2.0_asan_shadow_offset_callback.patch
blobd446025a8add503dc3ee0851b7c9bbf2cf8608f3
1 From 41a82fb711f3637b4b7f57756492b628058f9d5f Mon Sep 17 00:00:00 2001
2 From: Harshit Sharma <harshitsharmajs@gmail.com>
3 Date: Fri, 10 Jul 2020 13:06:08 -0700
4 Subject: [PATCH] crossgcc: Enable GCC to get asan shadow offset at runtime
6 Unlike Linux kernel which has a static shadow region layout, we have multiple stages in
7 coreboot and thus require a different shadow offset address. Unfortunately, GCC currently
8 only supports adding a static shadow offset at compile time using -fasan-shadow-offset flag.
10 For this reason, we enable GCC to determine asan shadow offset address at runtime using a
11 callback function named __asan_shadow_offset(). This supersedes the need to specify this
12 address at compile time. GCC then makes use of this shadow offset to protect stack buffers
13 by inserting red zones around them.
15 Some other benefits of having this GCC patch are:
16 a. We can place the shadow region in a separate linker section with all its advantages like
17 automatic fit insurance. This ensures if a platform doesn't have enough memory space to
18 hold shadow region, the build will fail. (However, if we use a fixed shadow offset on a
19 platform that actually doesn't have enough memory, it may still build without any errors.)
20 b. We don't modify the memory layout compared to the current one, as we are placing the
21 shadow region at the end of the space already occupied by the program.
22 c. We can be much more flexible later if needed (thinking of other stages like bootblock).
23 d. Since we are appending the shadow buffer to the region already occupied, we make efficient
24 use of the limited memory available which is highly beneficial when using cache as ram.
26 Further, we have made sure that if you compile you tree with ASan enabled but missed this
27 patch, it will end up in the following compilation error:
28 "invalid --param name 'asan-use-shadow-offset-callback'"
29 So, you cannot accidentally enable the feature without having your compiler patched.
31 [pgeorgi: Updated for gcc 11.1]
33 Signed-off-by: Harshit Sharma <harshitsharmajs@gmail.com>
34 Signed-off-by: Patrick Georgi <pgeorgi@google.com>
36 diff --git a/gcc/asan.c b/gcc/asan.c
37 index 235e21947..713bf994d 100644
38 --- a/gcc/asan.cc
39 +++ b/gcc/asan.cc
40 @@ -1389,13 +1389,28 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb,
41 TREE_ASM_WRITTEN (decl) = 1;
42 TREE_ASM_WRITTEN (id) = 1;
43 emit_move_insn (mem, expand_normal (build_fold_addr_expr (decl)));
44 - shadow_base = expand_binop (Pmode, lshr_optab, base,
45 - gen_int_shift_amount (Pmode, ASAN_SHADOW_SHIFT),
46 - NULL_RTX, 1, OPTAB_DIRECT);
47 - shadow_base
48 - = plus_constant (Pmode, shadow_base,
49 - asan_shadow_offset ()
50 - + (base_align_bias >> ASAN_SHADOW_SHIFT));
51 + if (param_asan_use_shadow_offset_callback) {
52 + rtx addr, shadow_offset_rtx;
53 + ret = init_one_libfunc ("__asan_shadow_offset");
54 + addr = convert_memory_address (ptr_mode, base);
55 + ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
56 + addr, ptr_mode);
57 + shadow_offset_rtx = convert_memory_address (Pmode, ret);
58 + shadow_base = expand_binop (Pmode, lshr_optab, base,
59 + gen_int_shift_amount (Pmode, ASAN_SHADOW_SHIFT),
60 + NULL_RTX, 1, OPTAB_DIRECT);
61 + shadow_base = expand_binop (Pmode, add_optab, shadow_base,
62 + shadow_offset_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
63 + shadow_base = plus_constant (Pmode, shadow_base,
64 + (base_align_bias >> ASAN_SHADOW_SHIFT));
65 + } else {
66 + shadow_base = expand_binop (Pmode, lshr_optab, base,
67 + gen_int_shift_amount (Pmode, ASAN_SHADOW_SHIFT),
68 + NULL_RTX, 1, OPTAB_DIRECT);
69 + shadow_base = plus_constant (Pmode, shadow_base,
70 + asan_shadow_offset ()
71 + + (base_align_bias >> ASAN_SHADOW_SHIFT));
72 + }
73 gcc_assert (asan_shadow_set != -1
74 && (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4);
75 shadow_mem = gen_rtx_MEM (SImode, shadow_base);
76 --- gcc-11.1.0/gcc/params.opt~ 2021-05-11 09:02:51.897508677 +0200
77 +++ gcc-11.1.0/gcc/params.opt 2021-05-11 09:10:43.692610696 +0200
78 @@ -50,6 +50,10 @@
79 Common Joined UInteger Var(param_asan_instrumentation_with_call_threshold) Init(7000) Param Optimization
80 Use callbacks instead of inline code if number of accesses in function becomes greater or equal to this number.
82 +-param=asan-use-shadow-offset-callback=
83 +Common Joined UInteger Var(param_asan_use_shadow_offset_callback) Init(0) IntegerRange(0, 1) Param Optimization
84 +Use shadow offset callback function at runtime instead of fixed value at compile time at the cost of runtime overhead.
86 -param=asan-memintrin=
87 Common Joined UInteger Var(param_asan_memintrin) Init(1) IntegerRange(0, 1) Param Optimization
88 Enable asan builtin functions protection.