1 /* Branch Target Identification for AArch64 architecture.
2 Copyright (C) 2019 Free Software Foundation, Inc.
3 Contributed by Arm Ltd.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #define IN_TARGET_CODE 1
24 #define INCLUDE_STRING
26 #include "coretypes.h"
34 #include "stringpool.h"
38 #include "gimple-iterator.h"
42 #include "tree-pass.h"
45 /* This pass enables the support for Branch Target Identification Mechanism
46 for AArch64. This is a new security feature introduced in ARMv8.5-A
47 archtitecture. A BTI instruction is used to guard against the execution
48 of instructions which are not the intended target of an indirect branch.
50 Outside of a guarded memory region, a BTI instruction executes as a NOP.
51 Within a guarded memory region any target of an indirect branch must be
52 a compatible BTI or BRK, HLT, PACIASP, PACIBASP instruction (even if the
53 branch is triggered in a non-guarded memory region). An incompatibility
54 generates a Branch Target Exception.
56 The compatibility of the BTI instruction is as follows:
57 BTI j : Can be a target of any indirect jump (BR Xn).
58 BTI c : Can be a target of any indirect call (BLR Xn and BR X16/X17).
59 BTI jc: Can be a target of any indirect call or indirect jump.
60 BTI : Can not be a target of any indirect call or indirect jump.
62 In order to enable this mechanism, this pass iterates through the
63 control flow of the code and adds appropriate BTI instructions :
64 * Add a new "BTI C" at the beginning of a function, unless its already
65 protected by a "PACIASP/PACIBSP". We exempt the functions that are only
67 * Add a new "BTI J" for every target of an indirect jump, jump table targets,
68 non-local goto targets or labels that might be referenced by variables,
69 constant pools, etc (NOTE_INSN_DELETED_LABEL)
71 Since we have already changed the use of indirect tail calls to only x16
72 and x17, we do not have to use "BTI JC".
74 This pass is triggered by the command line option -mbranch-protection=bti or
75 -mbranch-protection=standard. Since all the BTI instructions are in the HINT
76 space, this pass does not require any minimum architecture version. */
80 const pass_data pass_data_insert_bti
=
84 OPTGROUP_NONE
, /* optinfo_flags. */
85 TV_MACH_DEP
, /* tv_id. */
86 0, /* properties_required. */
87 0, /* properties_provided. */
88 0, /* properties_destroyed. */
89 0, /* todo_flags_start. */
90 0, /* todo_flags_finish. */
93 /* Check if X (or any sub-rtx of X) is a PACIASP/PACIBSP instruction. */
95 aarch64_pac_insn_p (rtx x
)
100 subrtx_var_iterator::array_type array
;
101 FOR_EACH_SUBRTX_VAR (iter
, array
, PATTERN (x
), ALL
)
104 if (sub
&& GET_CODE (sub
) == UNSPEC
)
106 int unspec_val
= XINT (sub
, 1);
117 iter
.skip_subrtxes ();
123 /* Insert the BTI instruction. */
124 /* This is implemented as a late RTL pass that runs before branch
125 shortening and does the following. */
127 rest_of_insert_bti (void)
129 timevar_push (TV_MACH_DEP
);
135 /* Since a Branch Target Exception can only be triggered by an indirect call,
136 we exempt function that are only called directly. We also exempt
137 functions that are already protected by Return Address Signing (PACIASP/
138 PACIBSP). For all other cases insert a BTI C at the beginning of the
140 if (!cgraph_node::get (cfun
->decl
)->only_called_directly_p ())
142 bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
;
144 if (!aarch64_pac_insn_p (get_first_nonnote_insn ()))
146 bti_insn
= gen_bti_c ();
147 emit_insn_before (bti_insn
, insn
);
152 FOR_EACH_BB_FN (bb
, cfun
)
154 for (insn
= BB_HEAD (bb
); insn
!= NEXT_INSN (BB_END (bb
));
155 insn
= NEXT_INSN (insn
))
157 /* If a label is marked to be preserved or can be a non-local goto
158 target, it must be protected with a BTI J. The same applies to
159 NOTE_INSN_DELETED_LABEL since they are basically labels that might
160 be referenced via variables or constant pool. */
162 && (LABEL_PRESERVE_P (insn
)
163 || bb
->flags
& BB_NON_LOCAL_GOTO_TARGET
))
165 && NOTE_KIND (insn
) == NOTE_INSN_DELETED_LABEL
))
167 bti_insn
= gen_bti_j ();
168 emit_insn_after (bti_insn
, insn
);
172 /* There could still be more labels that are valid targets of a
173 BTI J instuction. To find them we start looking through the
174 JUMP_INSN. If it jumps to a jump table, then we find all labels
175 of the jump table to protect with a BTI J. */
178 rtx_jump_table_data
*table
;
179 if (tablejump_p (insn
, NULL
, &table
))
181 rtvec vec
= table
->get_labels ();
185 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
187 label
= as_a
<rtx_insn
*> (XEXP (RTVEC_ELT (vec
, j
), 0));
188 bti_insn
= gen_bti_j ();
189 emit_insn_after (bti_insn
, label
);
194 /* Also look for calls to setjmp () which would be marked with
195 REG_SETJMP note and put a BTI J after. This is where longjump ()
197 if (CALL_P (insn
) && (find_reg_note (insn
, REG_SETJMP
, NULL
)))
199 bti_insn
= gen_bti_j ();
200 emit_insn_after (bti_insn
, insn
);
206 timevar_pop (TV_MACH_DEP
);
211 class pass_insert_bti
: public rtl_opt_pass
214 pass_insert_bti (gcc::context
*ctxt
)
215 : rtl_opt_pass (pass_data_insert_bti
, ctxt
)
218 /* opt_pass methods: */
219 virtual bool gate (function
*)
221 return aarch64_bti_enabled ();
224 virtual unsigned int execute (function
*)
226 return rest_of_insert_bti ();
229 }; // class pass_insert_bti
234 make_pass_insert_bti (gcc::context
*ctxt
)
236 return new pass_insert_bti (ctxt
);