| 1 |
/* AddressSanitizer, a fast memory error detector. |
| 2 |
Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Kostya Serebryany <[email protected]> |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 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/>. */ |
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "backend.h" |
| 26 |
#include "target.h" |
| 27 |
#include "rtl.h" |
| 28 |
#include "tree.h" |
| 29 |
#include "gimple.h" |
| 30 |
#include "cfghooks.h" |
| 31 |
#include "alloc-pool.h" |
| 32 |
#include "tree-pass.h" |
| 33 |
#include "memmodel.h" |
| 34 |
#include "tm_p.h" |
| 35 |
#include "ssa.h" |
| 36 |
#include "stringpool.h" |
| 37 |
#include "tree-ssanames.h" |
| 38 |
#include "optabs.h" |
| 39 |
#include "emit-rtl.h" |
| 40 |
#include "cgraph.h" |
| 41 |
#include "gimple-pretty-print.h" |
| 42 |
#include "alias.h" |
| 43 |
#include "fold-const.h" |
| 44 |
#include "cfganal.h" |
| 45 |
#include "gimplify.h" |
| 46 |
#include "gimple-iterator.h" |
| 47 |
#include "varasm.h" |
| 48 |
#include "stor-layout.h" |
| 49 |
#include "tree-iterator.h" |
| 50 |
#include "stringpool.h" |
| 51 |
#include "attribs.h" |
| 52 |
#include "asan.h" |
| 53 |
#include "dojump.h" |
| 54 |
#include "explow.h" |
| 55 |
#include "expr.h" |
| 56 |
#include "output.h" |
| 57 |
#include "langhooks.h" |
| 58 |
#include "cfgloop.h" |
| 59 |
#include "gimple-builder.h" |
| 60 |
#include "gimple-fold.h" |
| 61 |
#include "ubsan.h" |
| 62 |
#include "builtins.h" |
| 63 |
#include "fnmatch.h" |
| 64 |
#include "tree-inline.h" |
| 65 |
#include "tree-ssa.h" |
| 66 |
#include "tree-eh.h" |
| 67 |
#include "diagnostic-core.h" |
| 68 |
|
| 69 |
/* AddressSanitizer finds out-of-bounds and use-after-free bugs |
| 70 |
with <2x slowdown on average. |
| 71 |
|
| 72 |
The tool consists of two parts: |
| 73 |
instrumentation module (this file) and a run-time library. |
| 74 |
The instrumentation module adds a run-time check before every memory insn. |
| 75 |
For a 8- or 16- byte load accessing address X: |
| 76 |
ShadowAddr = (X >> 3) + Offset |
| 77 |
ShadowValue = *(char*)ShadowAddr; // *(short*) for 16-byte access. |
| 78 |
if (ShadowValue) |
| 79 |
__asan_report_load8(X); |
| 80 |
For a load of N bytes (N=1, 2 or 4) from address X: |
| 81 |
ShadowAddr = (X >> 3) + Offset |
| 82 |
ShadowValue = *(char*)ShadowAddr; |
| 83 |
if (ShadowValue) |
| 84 |
if ((X & 7) + N - 1 > ShadowValue) |
| 85 |
__asan_report_loadN(X); |
| 86 |
Stores are instrumented similarly, but using __asan_report_storeN functions. |
| 87 |
A call too __asan_init_vN() is inserted to the list of module CTORs. |
| 88 |
N is the version number of the AddressSanitizer API. The changes between the |
| 89 |
API versions are listed in libsanitizer/asan/asan_interface_internal.h. |
| 90 |
|
| 91 |
The run-time library redefines malloc (so that redzone are inserted around |
| 92 |
the allocated memory) and free (so that reuse of free-ed memory is delayed), |
| 93 |
provides __asan_report* and __asan_init_vN functions. |
| 94 |
|
| 95 |
Read more: |
| 96 |
http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm |
| 97 |
|
| 98 |
The current implementation supports detection of out-of-bounds and |
| 99 |
use-after-free in the heap, on the stack and for global variables. |
| 100 |
|
| 101 |
[Protection of stack variables] |
| 102 |
|
| 103 |
To understand how detection of out-of-bounds and use-after-free works |
| 104 |
for stack variables, lets look at this example on x86_64 where the |
| 105 |
stack grows downward: |
| 106 |
|
| 107 |
int |
| 108 |
foo () |
| 109 |
{ |
| 110 |
char a[24] = {0}; |
| 111 |
int b[2] = {0}; |
| 112 |
|
| 113 |
a[5] = 1; |
| 114 |
b[1] = 2; |
| 115 |
|
| 116 |
return a[5] + b[1]; |
| 117 |
} |
| 118 |
|
| 119 |
For this function, the stack protected by asan will be organized as |
| 120 |
follows, from the top of the stack to the bottom: |
| 121 |
|
| 122 |
Slot 1/ [red zone of 32 bytes called 'RIGHT RedZone'] |
| 123 |
|
| 124 |
Slot 2/ [8 bytes of red zone, that adds up to the space of 'a' to make |
| 125 |
the next slot be 32 bytes aligned; this one is called Partial |
| 126 |
Redzone; this 32 bytes alignment is an asan constraint] |
| 127 |
|
| 128 |
Slot 3/ [24 bytes for variable 'a'] |
| 129 |
|
| 130 |
Slot 4/ [red zone of 32 bytes called 'Middle RedZone'] |
| 131 |
|
| 132 |
Slot 5/ [24 bytes of Partial Red Zone (similar to slot 2] |
| 133 |
|
| 134 |
Slot 6/ [8 bytes for variable 'b'] |
| 135 |
|
| 136 |
Slot 7/ [32 bytes of Red Zone at the bottom of the stack, called |
| 137 |
'LEFT RedZone'] |
| 138 |
|
| 139 |
The 32 bytes of LEFT red zone at the bottom of the stack can be |
| 140 |
decomposed as such: |
| 141 |
|
| 142 |
1/ The first 8 bytes contain a magical asan number that is always |
| 143 |
0x41B58AB3. |
| 144 |
|
| 145 |
2/ The following 8 bytes contains a pointer to a string (to be |
| 146 |
parsed at runtime by the runtime asan library), which format is |
| 147 |
the following: |
| 148 |
|
| 149 |
"<function-name> <space> <num-of-variables-on-the-stack> |
| 150 |
(<32-bytes-aligned-offset-in-bytes-of-variable> <space> |
| 151 |
<length-of-var-in-bytes> ){n} " |
| 152 |
|
| 153 |
where '(...){n}' means the content inside the parenthesis occurs 'n' |
| 154 |
times, with 'n' being the number of variables on the stack. |
| 155 |
|
| 156 |
3/ The following 8 bytes contain the PC of the current function which |
| 157 |
will be used by the run-time library to print an error message. |
| 158 |
|
| 159 |
4/ The following 8 bytes are reserved for internal use by the run-time. |
| 160 |
|
| 161 |
The shadow memory for that stack layout is going to look like this: |
| 162 |
|
| 163 |
- content of shadow memory 8 bytes for slot 7: 0xF1F1F1F1. |
| 164 |
The F1 byte pattern is a magic number called |
| 165 |
ASAN_STACK_MAGIC_LEFT and is a way for the runtime to know that |
| 166 |
the memory for that shadow byte is part of a the LEFT red zone |
| 167 |
intended to seat at the bottom of the variables on the stack. |
| 168 |
|
| 169 |
- content of shadow memory 8 bytes for slots 6 and 5: |
| 170 |
0xF4F4F400. The F4 byte pattern is a magic number |
| 171 |
called ASAN_STACK_MAGIC_PARTIAL. It flags the fact that the |
| 172 |
memory region for this shadow byte is a PARTIAL red zone |
| 173 |
intended to pad a variable A, so that the slot following |
| 174 |
{A,padding} is 32 bytes aligned. |
| 175 |
|
| 176 |
Note that the fact that the least significant byte of this |
| 177 |
shadow memory content is 00 means that 8 bytes of its |
| 178 |
corresponding memory (which corresponds to the memory of |
| 179 |
variable 'b') is addressable. |
| 180 |
|
| 181 |
- content of shadow memory 8 bytes for slot 4: 0xF2F2F2F2. |
| 182 |
The F2 byte pattern is a magic number called |
| 183 |
ASAN_STACK_MAGIC_MIDDLE. It flags the fact that the memory |
| 184 |
region for this shadow byte is a MIDDLE red zone intended to |
| 185 |
seat between two 32 aligned slots of {variable,padding}. |
| 186 |
|
| 187 |
- content of shadow memory 8 bytes for slot 3 and 2: |
| 188 |
0xF4000000. This represents is the concatenation of |
| 189 |
variable 'a' and the partial red zone following it, like what we |
| 190 |
had for variable 'b'. The least significant 3 bytes being 00 |
| 191 |
means that the 3 bytes of variable 'a' are addressable. |
| 192 |
|
| 193 |
- content of shadow memory 8 bytes for slot 1: 0xF3F3F3F3. |
| 194 |
The F3 byte pattern is a magic number called |
| 195 |
ASAN_STACK_MAGIC_RIGHT. It flags the fact that the memory |
| 196 |
region for this shadow byte is a RIGHT red zone intended to seat |
| 197 |
at the top of the variables of the stack. |
| 198 |
|
| 199 |
Note that the real variable layout is done in expand_used_vars in |
| 200 |
cfgexpand.cc. As far as Address Sanitizer is concerned, it lays out |
| 201 |
stack variables as well as the different red zones, emits some |
| 202 |
prologue code to populate the shadow memory as to poison (mark as |
| 203 |
non-accessible) the regions of the red zones and mark the regions of |
| 204 |
stack variables as accessible, and emit some epilogue code to |
| 205 |
un-poison (mark as accessible) the regions of red zones right before |
| 206 |
the function exits. |
| 207 |
|
| 208 |
[Protection of global variables] |
| 209 |
|
| 210 |
The basic idea is to insert a red zone between two global variables |
| 211 |
and install a constructor function that calls the asan runtime to do |
| 212 |
the populating of the relevant shadow memory regions at load time. |
| 213 |
|
| 214 |
So the global variables are laid out as to insert a red zone between |
| 215 |
them. The size of the red zones is so that each variable starts on a |
| 216 |
32 bytes boundary. |
| 217 |
|
| 218 |
Then a constructor function is installed so that, for each global |
| 219 |
variable, it calls the runtime asan library function |
| 220 |
__asan_register_globals_with an instance of this type: |
| 221 |
|
| 222 |
struct __asan_global |
| 223 |
{ |
| 224 |
// Address of the beginning of the global variable. |
| 225 |
const void *__beg; |
| 226 |
|
| 227 |
// Initial size of the global variable. |
| 228 |
uptr __size; |
| 229 |
|
| 230 |
// Size of the global variable + size of the red zone. This |
| 231 |
// size is 32 bytes aligned. |
| 232 |
uptr __size_with_redzone; |
| 233 |
|
| 234 |
// Name of the global variable. |
| 235 |
const void *__name; |
| 236 |
|
| 237 |
// Name of the module where the global variable is declared. |
| 238 |
const void *__module_name; |
| 239 |
|
| 240 |
// 1 if it has dynamic initialization, 0 otherwise. |
| 241 |
uptr __has_dynamic_init; |
| 242 |
|
| 243 |
// A pointer to struct that contains source location, could be NULL. |
| 244 |
__asan_global_source_location *__location; |
| 245 |
} |
| 246 |
|
| 247 |
A destructor function that calls the runtime asan library function |
| 248 |
_asan_unregister_globals is also installed. */ |
| 249 |
|
| 250 |
static unsigned HOST_WIDE_INT asan_shadow_offset_value; |
| 251 |
static bool asan_shadow_offset_computed; |
| 252 |
static vec<char *> sanitized_sections; |
| 253 |
static tree last_alloca_addr; |
| 254 |
|
| 255 |
/* Set of variable declarations that are going to be guarded by |
| 256 |
use-after-scope sanitizer. */ |
| 257 |
|
| 258 |
hash_set<tree> *asan_handled_variables = NULL; |
| 259 |
|
| 260 |
hash_set <tree> *asan_used_labels = NULL; |
| 261 |
|
| 262 |
/* Global variables for HWASAN stack tagging. */ |
| 263 |
/* hwasan_frame_tag_offset records the offset from the frame base tag that the |
| 264 |
next object should have. */ |
| 265 |
static uint8_t hwasan_frame_tag_offset = 0; |
| 266 |
/* hwasan_frame_base_ptr is a pointer with the same address as |
| 267 |
`virtual_stack_vars_rtx` for the current frame, and with the frame base tag |
| 268 |
stored in it. N.b. this global RTX does not need to be marked GTY, but is |
| 269 |
done so anyway. The need is not there since all uses are in just one pass |
| 270 |
(cfgexpand) and there are no calls to ggc_collect between the uses. We mark |
| 271 |
it GTY(()) anyway to allow the use of the variable later on if needed by |
| 272 |
future features. */ |
| 273 |
static GTY(()) rtx hwasan_frame_base_ptr = NULL_RTX; |
| 274 |
/* hwasan_frame_base_init_seq is the sequence of RTL insns that will initialize |
| 275 |
the hwasan_frame_base_ptr. When the hwasan_frame_base_ptr is requested, we |
| 276 |
generate this sequence but do not emit it. If the sequence was created it |
| 277 |
is emitted once the function body has been expanded. |
| 278 |
|
| 279 |
This delay is because the frame base pointer may be needed anywhere in the |
| 280 |
function body, or needed by the expand_used_vars function. Emitting once in |
| 281 |
a known place is simpler than requiring the emission of the instructions to |
| 282 |
be know where it should go depending on the first place the hwasan frame |
| 283 |
base is needed. */ |
| 284 |
static GTY(()) rtx_insn *hwasan_frame_base_init_seq = NULL; |
| 285 |
|
| 286 |
/* Structure defining the extent of one object on the stack that HWASAN needs |
| 287 |
to tag in the corresponding shadow stack space. |
| 288 |
|
| 289 |
The range this object spans on the stack is between `untagged_base + |
| 290 |
nearest_offset` and `untagged_base + farthest_offset`. |
| 291 |
`tagged_base` is an rtx containing the same value as `untagged_base` but |
| 292 |
with a random tag stored in the top byte. We record both `untagged_base` |
| 293 |
and `tagged_base` so that `hwasan_emit_prologue` can use both without having |
| 294 |
to emit RTL into the instruction stream to re-calculate one from the other. |
| 295 |
(`hwasan_emit_prologue` needs to use both bases since the |
| 296 |
__hwasan_tag_memory call it emits uses an untagged value, and it calculates |
| 297 |
the tag to store in shadow memory based on the tag_offset plus the tag in |
| 298 |
tagged_base). */ |
| 299 |
struct hwasan_stack_var |
| 300 |
{ |
| 301 |
rtx untagged_base; |
| 302 |
rtx tagged_base; |
| 303 |
poly_int64 nearest_offset; |
| 304 |
poly_int64 farthest_offset; |
| 305 |
uint8_t tag_offset; |
| 306 |
}; |
| 307 |
|
| 308 |
/* Variable recording all stack variables that HWASAN needs to tag. |
| 309 |
Does not need to be marked as GTY(()) since every use is in the cfgexpand |
| 310 |
pass and gcc_collect is not called in the middle of that pass. */ |
| 311 |
static vec<hwasan_stack_var> hwasan_tagged_stack_vars; |
| 312 |
|
| 313 |
|
| 314 |
/* Sets shadow offset to value in string VAL. */ |
| 315 |
|
| 316 |
bool |
| 317 |
set_asan_shadow_offset (const char *val) |
| 318 |
{ |
| 319 |
char *endp; |
| 320 |
|
| 321 |
errno = 0; |
| 322 |
#ifdef HAVE_LONG_LONG |
| 323 |
asan_shadow_offset_value = strtoull (val, &endp, 0); |
| 324 |
#else |
| 325 |
asan_shadow_offset_value = strtoul (val, &endp, 0); |
| 326 |
#endif |
| 327 |
if (!(*val != '\0' && *endp == '\0' && errno == 0)) |
| 328 |
return false; |
| 329 |
|
| 330 |
asan_shadow_offset_computed = true; |
| 331 |
|
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
/* Set list of user-defined sections that need to be sanitized. */ |
| 336 |
|
| 337 |
void |
| 338 |
set_sanitized_sections (const char *sections) |
| 339 |
{ |
| 340 |
char *pat; |
| 341 |
unsigned i; |
| 342 |
FOR_EACH_VEC_ELT (sanitized_sections, i, pat) |
| 343 |
free (pat); |
| 344 |
sanitized_sections.truncate (0); |
| 345 |
|
| 346 |
for (const char *s = sections; *s; ) |
| 347 |
{ |
| 348 |
const char *end; |
| 349 |
for (end = s; *end && *end != ','; ++end); |
| 350 |
size_t len = end - s; |
| 351 |
sanitized_sections.safe_push (xstrndup (s, len)); |
| 352 |
s = *end ? end + 1 : end; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
bool |
| 357 |
asan_mark_p (gimple *stmt, enum asan_mark_flags flag) |
| 358 |
{ |
| 359 |
return (gimple_call_internal_p (stmt, IFN_ASAN_MARK) |
| 360 |
&& tree_to_uhwi (gimple_call_arg (stmt, 0)) == flag); |
| 361 |
} |
| 362 |
|
| 363 |
bool |
| 364 |
asan_sanitize_stack_p (void) |
| 365 |
{ |
| 366 |
return (sanitize_flags_p (SANITIZE_ADDRESS) && param_asan_stack); |
| 367 |
} |
| 368 |
|
| 369 |
bool |
| 370 |
asan_sanitize_allocas_p (void) |
| 371 |
{ |
| 372 |
return (asan_sanitize_stack_p () && param_asan_protect_allocas); |
| 373 |
} |
| 374 |
|
| 375 |
bool |
| 376 |
asan_instrument_reads (void) |
| 377 |
{ |
| 378 |
return (sanitize_flags_p (SANITIZE_ADDRESS) && param_asan_instrument_reads); |
| 379 |
} |
| 380 |
|
| 381 |
bool |
| 382 |
asan_instrument_writes (void) |
| 383 |
{ |
| 384 |
return (sanitize_flags_p (SANITIZE_ADDRESS) && param_asan_instrument_writes); |
| 385 |
} |
| 386 |
|
| 387 |
bool |
| 388 |
asan_memintrin (void) |
| 389 |
{ |
| 390 |
return (sanitize_flags_p (SANITIZE_ADDRESS) && param_asan_memintrin); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/* Support for --param asan-kernel-mem-intrinsic-prefix=1. */ |
| 395 |
static GTY(()) rtx asan_memfn_rtls[3]; |
| 396 |
|
| 397 |
rtx |
| 398 |
asan_memfn_rtl (tree fndecl) |
| 399 |
{ |
| 400 |
int i; |
| 401 |
const char *f, *p; |
| 402 |
char buf[sizeof ("__hwasan_memmove")]; |
| 403 |
|
| 404 |
switch (DECL_FUNCTION_CODE (fndecl)) |
| 405 |
{ |
| 406 |
case BUILT_IN_MEMCPY: i = 0; f = "memcpy"; break; |
| 407 |
case BUILT_IN_MEMSET: i = 1; f = "memset"; break; |
| 408 |
case BUILT_IN_MEMMOVE: i = 2; f = "memmove"; break; |
| 409 |
default: gcc_unreachable (); |
| 410 |
} |
| 411 |
if (asan_memfn_rtls[i] == NULL_RTX) |
| 412 |
{ |
| 413 |
tree save_name = DECL_NAME (fndecl); |
| 414 |
tree save_assembler_name = DECL_ASSEMBLER_NAME (fndecl); |
| 415 |
rtx save_rtl = DECL_RTL (fndecl); |
| 416 |
if (flag_sanitize & SANITIZE_KERNEL_HWADDRESS) |
| 417 |
p = "__hwasan_"; |
| 418 |
else |
| 419 |
p = "__asan_"; |
| 420 |
strcpy (buf, p); |
| 421 |
strcat (buf, f); |
| 422 |
DECL_NAME (fndecl) = get_identifier (buf); |
| 423 |
DECL_ASSEMBLER_NAME_RAW (fndecl) = NULL_TREE; |
| 424 |
SET_DECL_RTL (fndecl, NULL_RTX); |
| 425 |
asan_memfn_rtls[i] = DECL_RTL (fndecl); |
| 426 |
DECL_NAME (fndecl) = save_name; |
| 427 |
DECL_ASSEMBLER_NAME_RAW (fndecl) = save_assembler_name; |
| 428 |
SET_DECL_RTL (fndecl, save_rtl); |
| 429 |
} |
| 430 |
return asan_memfn_rtls[i]; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
/* Checks whether section SEC should be sanitized. */ |
| 435 |
|
| 436 |
static bool |
| 437 |
section_sanitized_p (const char *sec) |
| 438 |
{ |
| 439 |
char *pat; |
| 440 |
unsigned i; |
| 441 |
FOR_EACH_VEC_ELT (sanitized_sections, i, pat) |
| 442 |
if (fnmatch (pat, sec, FNM_PERIOD) == 0) |
| 443 |
return true; |
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
/* Returns Asan shadow offset. */ |
| 448 |
|
| 449 |
static unsigned HOST_WIDE_INT |
| 450 |
asan_shadow_offset () |
| 451 |
{ |
| 452 |
if (!asan_shadow_offset_computed) |
| 453 |
{ |
| 454 |
asan_shadow_offset_computed = true; |
| 455 |
asan_shadow_offset_value = targetm.asan_shadow_offset (); |
| 456 |
} |
| 457 |
return asan_shadow_offset_value; |
| 458 |
} |
| 459 |
|
| 460 |
static bool |
| 461 |
asan_dynamic_shadow_offset_p () |
| 462 |
{ |
| 463 |
return (asan_shadow_offset_value == 0) |
| 464 |
&& targetm.asan_dynamic_shadow_offset_p (); |
| 465 |
} |
| 466 |
|
| 467 |
/* Returns Asan shadow offset has been set. */ |
| 468 |
bool |
| 469 |
asan_shadow_offset_set_p () |
| 470 |
{ |
| 471 |
return asan_shadow_offset_computed; |
| 472 |
} |
| 473 |
|
| 474 |
alias_set_type asan_shadow_set = -1; |
| 475 |
|
| 476 |
/* Pointer types to 1, 2 or 4 byte integers in shadow memory. A separate |
| 477 |
alias set is used for all shadow memory accesses. */ |
| 478 |
static GTY(()) tree shadow_ptr_types[3]; |
| 479 |
|
| 480 |
/* Decl for __asan_option_detect_stack_use_after_return. */ |
| 481 |
static GTY(()) tree asan_detect_stack_use_after_return; |
| 482 |
|
| 483 |
static GTY (()) tree asan_shadow_memory_dynamic_address; |
| 484 |
|
| 485 |
/* Local copy for the asan_shadow_memory_dynamic_address within the |
| 486 |
function. */ |
| 487 |
static GTY (()) tree asan_local_shadow_memory_dynamic_address; |
| 488 |
|
| 489 |
static tree |
| 490 |
get_asan_shadow_memory_dynamic_address_decl () |
| 491 |
{ |
| 492 |
if (asan_shadow_memory_dynamic_address == NULL_TREE) |
| 493 |
{ |
| 494 |
tree id, decl; |
| 495 |
id = get_identifier ("__asan_shadow_memory_dynamic_address"); |
| 496 |
decl |
| 497 |
= build_decl (BUILTINS_LOCATION, VAR_DECL, id, pointer_sized_int_node); |
| 498 |
SET_DECL_ASSEMBLER_NAME (decl, id); |
| 499 |
TREE_ADDRESSABLE (decl) = 1; |
| 500 |
DECL_ARTIFICIAL (decl) = 1; |
| 501 |
DECL_IGNORED_P (decl) = 1; |
| 502 |
DECL_EXTERNAL (decl) = 1; |
| 503 |
TREE_STATIC (decl) = 1; |
| 504 |
TREE_PUBLIC (decl) = 1; |
| 505 |
TREE_USED (decl) = 1; |
| 506 |
asan_shadow_memory_dynamic_address = decl; |
| 507 |
} |
| 508 |
|
| 509 |
return asan_shadow_memory_dynamic_address; |
| 510 |
} |
| 511 |
|
| 512 |
void |
| 513 |
asan_maybe_insert_dynamic_shadow_at_function_entry (function *fun) |
| 514 |
{ |
| 515 |
asan_local_shadow_memory_dynamic_address = NULL_TREE; |
| 516 |
if (!asan_dynamic_shadow_offset_p ()) |
| 517 |
return; |
| 518 |
|
| 519 |
gimple *g; |
| 520 |
|
| 521 |
tree lhs = create_tmp_var (pointer_sized_int_node, |
| 522 |
"__local_asan_shadow_memory_dynamic_address"); |
| 523 |
|
| 524 |
g = gimple_build_assign (lhs, get_asan_shadow_memory_dynamic_address_decl ()); |
| 525 |
gimple_set_location (g, fun->function_start_locus); |
| 526 |
edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
| 527 |
gsi_insert_on_edge_immediate (e, g); |
| 528 |
|
| 529 |
asan_local_shadow_memory_dynamic_address = lhs; |
| 530 |
} |
| 531 |
|
| 532 |
/* Hashtable support for memory references used by gimple |
| 533 |
statements. */ |
| 534 |
|
| 535 |
/* This type represents a reference to a memory region. */ |
| 536 |
struct asan_mem_ref |
| 537 |
{ |
| 538 |
/* The expression of the beginning of the memory region. */ |
| 539 |
tree start; |
| 540 |
|
| 541 |
/* The size of the access. */ |
| 542 |
HOST_WIDE_INT access_size; |
| 543 |
}; |
| 544 |
|
| 545 |
object_allocator <asan_mem_ref> asan_mem_ref_pool ("asan_mem_ref"); |
| 546 |
|
| 547 |
/* Initializes an instance of asan_mem_ref. */ |
| 548 |
|
| 549 |
static void |
| 550 |
asan_mem_ref_init (asan_mem_ref *ref, tree start, HOST_WIDE_INT access_size) |
| 551 |
{ |
| 552 |
ref->start = start; |
| 553 |
ref->access_size = access_size; |
| 554 |
} |
| 555 |
|
| 556 |
/* Allocates memory for an instance of asan_mem_ref into the memory |
| 557 |
pool returned by asan_mem_ref_get_alloc_pool and initialize it. |
| 558 |
START is the address of (or the expression pointing to) the |
| 559 |
beginning of memory reference. ACCESS_SIZE is the size of the |
| 560 |
access to the referenced memory. */ |
| 561 |
|
| 562 |
static asan_mem_ref* |
| 563 |
asan_mem_ref_new (tree start, HOST_WIDE_INT access_size) |
| 564 |
{ |
| 565 |
asan_mem_ref *ref = asan_mem_ref_pool.allocate (); |
| 566 |
|
| 567 |
asan_mem_ref_init (ref, start, access_size); |
| 568 |
return ref; |
| 569 |
} |
| 570 |
|
| 571 |
/* This builds and returns a pointer to the end of the memory region |
| 572 |
that starts at START and of length LEN. */ |
| 573 |
|
| 574 |
tree |
| 575 |
asan_mem_ref_get_end (tree start, tree len) |
| 576 |
{ |
| 577 |
if (len == NULL_TREE || integer_zerop (len)) |
| 578 |
return start; |
| 579 |
|
| 580 |
if (!ptrofftype_p (len)) |
| 581 |
len = convert_to_ptrofftype (len); |
| 582 |
|
| 583 |
return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (start), start, len); |
| 584 |
} |
| 585 |
|
| 586 |
/* Return a tree expression that represents the end of the referenced |
| 587 |
memory region. Beware that this function can actually build a new |
| 588 |
tree expression. */ |
| 589 |
|
| 590 |
tree |
| 591 |
asan_mem_ref_get_end (const asan_mem_ref *ref, tree len) |
| 592 |
{ |
| 593 |
return asan_mem_ref_get_end (ref->start, len); |
| 594 |
} |
| 595 |
|
| 596 |
struct asan_mem_ref_hasher : nofree_ptr_hash <asan_mem_ref> |
| 597 |
{ |
| 598 |
static inline hashval_t hash (const asan_mem_ref *); |
| 599 |
static inline bool equal (const asan_mem_ref *, const asan_mem_ref *); |
| 600 |
}; |
| 601 |
|
| 602 |
/* Hash a memory reference. */ |
| 603 |
|
| 604 |
inline hashval_t |
| 605 |
asan_mem_ref_hasher::hash (const asan_mem_ref *mem_ref) |
| 606 |
{ |
| 607 |
return iterative_hash_expr (mem_ref->start, 0); |
| 608 |
} |
| 609 |
|
| 610 |
/* Compare two memory references. We accept the length of either |
| 611 |
memory references to be NULL_TREE. */ |
| 612 |
|
| 613 |
inline bool |
| 614 |
asan_mem_ref_hasher::equal (const asan_mem_ref *m1, |
| 615 |
const asan_mem_ref *m2) |
| 616 |
{ |
| 617 |
return operand_equal_p (m1->start, m2->start, 0); |
| 618 |
} |
| 619 |
|
| 620 |
static hash_table<asan_mem_ref_hasher> *asan_mem_ref_ht; |
| 621 |
|
| 622 |
/* Returns a reference to the hash table containing memory references. |
| 623 |
This function ensures that the hash table is created. Note that |
| 624 |
this hash table is updated by the function |
| 625 |
update_mem_ref_hash_table. */ |
| 626 |
|
| 627 |
static hash_table<asan_mem_ref_hasher> * |
| 628 |
get_mem_ref_hash_table () |
| 629 |
{ |
| 630 |
if (!asan_mem_ref_ht) |
| 631 |
asan_mem_ref_ht = new hash_table<asan_mem_ref_hasher> (10); |
| 632 |
|
| 633 |
return asan_mem_ref_ht; |
| 634 |
} |
| 635 |
|
| 636 |
/* Clear all entries from the memory references hash table. */ |
| 637 |
|
| 638 |
static void |
| 639 |
empty_mem_ref_hash_table () |
| 640 |
{ |
| 641 |
if (asan_mem_ref_ht) |
| 642 |
asan_mem_ref_ht->empty (); |
| 643 |
} |
| 644 |
|
| 645 |
/* Free the memory references hash table. */ |
| 646 |
|
| 647 |
static void |
| 648 |
free_mem_ref_resources () |
| 649 |
{ |
| 650 |
delete asan_mem_ref_ht; |
| 651 |
asan_mem_ref_ht = NULL; |
| 652 |
|
| 653 |
asan_mem_ref_pool.release (); |
| 654 |
} |
| 655 |
|
| 656 |
/* Return true iff the memory reference REF has been instrumented. */ |
| 657 |
|
| 658 |
static bool |
| 659 |
has_mem_ref_been_instrumented (tree ref, HOST_WIDE_INT access_size) |
| 660 |
{ |
| 661 |
asan_mem_ref r; |
| 662 |
asan_mem_ref_init (&r, ref, access_size); |
| 663 |
|
| 664 |
asan_mem_ref *saved_ref = get_mem_ref_hash_table ()->find (&r); |
| 665 |
return saved_ref && saved_ref->access_size >= access_size; |
| 666 |
} |
| 667 |
|
| 668 |
/* Return true iff the memory reference REF has been instrumented. */ |
| 669 |
|
| 670 |
static bool |
| 671 |
has_mem_ref_been_instrumented (const asan_mem_ref *ref) |
| 672 |
{ |
| 673 |
return has_mem_ref_been_instrumented (ref->start, ref->access_size); |
| 674 |
} |
| 675 |
|
| 676 |
/* Return true iff access to memory region starting at REF and of |
| 677 |
length LEN has been instrumented. */ |
| 678 |
|
| 679 |
static bool |
| 680 |
has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len) |
| 681 |
{ |
| 682 |
HOST_WIDE_INT size_in_bytes |
| 683 |
= tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; |
| 684 |
|
| 685 |
return size_in_bytes != -1 |
| 686 |
&& has_mem_ref_been_instrumented (ref->start, size_in_bytes); |
| 687 |
} |
| 688 |
|
| 689 |
/* Set REF to the memory reference present in a gimple assignment |
| 690 |
ASSIGNMENT. Return true upon successful completion, false |
| 691 |
otherwise. */ |
| 692 |
|
| 693 |
static bool |
| 694 |
get_mem_ref_of_assignment (const gassign *assignment, |
| 695 |
asan_mem_ref *ref, |
| 696 |
bool *ref_is_store) |
| 697 |
{ |
| 698 |
gcc_assert (gimple_assign_single_p (assignment)); |
| 699 |
|
| 700 |
if (gimple_store_p (assignment) |
| 701 |
&& !gimple_clobber_p (assignment)) |
| 702 |
{ |
| 703 |
ref->start = gimple_assign_lhs (assignment); |
| 704 |
*ref_is_store = true; |
| 705 |
} |
| 706 |
else if (gimple_assign_load_p (assignment)) |
| 707 |
{ |
| 708 |
ref->start = gimple_assign_rhs1 (assignment); |
| 709 |
*ref_is_store = false; |
| 710 |
} |
| 711 |
else |
| 712 |
return false; |
| 713 |
|
| 714 |
ref->access_size = int_size_in_bytes (TREE_TYPE (ref->start)); |
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
/* Return address of last allocated dynamic alloca. */ |
| 719 |
|
| 720 |
static tree |
| 721 |
get_last_alloca_addr () |
| 722 |
{ |
| 723 |
if (last_alloca_addr) |
| 724 |
return last_alloca_addr; |
| 725 |
|
| 726 |
last_alloca_addr = create_tmp_reg (ptr_type_node, "last_alloca_addr"); |
| 727 |
gassign *g = gimple_build_assign (last_alloca_addr, null_pointer_node); |
| 728 |
edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
| 729 |
gsi_insert_on_edge_immediate (e, g); |
| 730 |
return last_alloca_addr; |
| 731 |
} |
| 732 |
|
| 733 |
/* Insert __asan_allocas_unpoison (top, bottom) call before |
| 734 |
__builtin_stack_restore (new_sp) call. |
| 735 |
The pseudocode of this routine should look like this: |
| 736 |
top = last_alloca_addr; |
| 737 |
bot = new_sp; |
| 738 |
__asan_allocas_unpoison (top, bot); |
| 739 |
last_alloca_addr = new_sp; |
| 740 |
__builtin_stack_restore (new_sp); |
| 741 |
In general, we can't use new_sp as bot parameter because on some |
| 742 |
architectures SP has non zero offset from dynamic stack area. Moreover, on |
| 743 |
some architectures this offset (STACK_DYNAMIC_OFFSET) becomes known for each |
| 744 |
particular function only after all callees were expanded to rtl. |
| 745 |
The most noticeable example is PowerPC{,64}, see |
| 746 |
http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#DYNAM-STACK. |
| 747 |
To overcome the issue we use following trick: pass new_sp as a second |
| 748 |
parameter to __asan_allocas_unpoison and rewrite it during expansion with |
| 749 |
new_sp + (virtual_dynamic_stack_rtx - sp) later in |
| 750 |
expand_asan_emit_allocas_unpoison function. |
| 751 |
|
| 752 |
HWASAN needs to do very similar, the eventual pseudocode should be: |
| 753 |
__hwasan_tag_memory (virtual_stack_dynamic_rtx, |
| 754 |
0, |
| 755 |
new_sp - sp); |
| 756 |
__builtin_stack_restore (new_sp) |
| 757 |
|
| 758 |
Need to use the same trick to handle STACK_DYNAMIC_OFFSET as described |
| 759 |
above. */ |
| 760 |
|
| 761 |
static void |
| 762 |
handle_builtin_stack_restore (gcall *call, gimple_stmt_iterator *iter) |
| 763 |
{ |
| 764 |
if (!iter |
| 765 |
|| !(asan_sanitize_allocas_p () || hwasan_sanitize_allocas_p ())) |
| 766 |
return; |
| 767 |
|
| 768 |
tree restored_stack = gimple_call_arg (call, 0); |
| 769 |
|
| 770 |
gimple *g; |
| 771 |
|
| 772 |
if (hwasan_sanitize_allocas_p ()) |
| 773 |
{ |
| 774 |
enum internal_fn fn = IFN_HWASAN_ALLOCA_UNPOISON; |
| 775 |
/* There is only one piece of information `expand_HWASAN_ALLOCA_UNPOISON` |
| 776 |
needs to work. This is the length of the area that we're |
| 777 |
deallocating. Since the stack pointer is known at expand time, the |
| 778 |
position of the new stack pointer after deallocation is enough |
| 779 |
information to calculate this length. */ |
| 780 |
g = gimple_build_call_internal (fn, 1, restored_stack); |
| 781 |
} |
| 782 |
else |
| 783 |
{ |
| 784 |
tree last_alloca = get_last_alloca_addr (); |
| 785 |
tree fn = builtin_decl_implicit (BUILT_IN_ASAN_ALLOCAS_UNPOISON); |
| 786 |
g = gimple_build_call (fn, 2, last_alloca, restored_stack); |
| 787 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 788 |
g = gimple_build_assign (last_alloca, restored_stack); |
| 789 |
} |
| 790 |
|
| 791 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 792 |
} |
| 793 |
|
| 794 |
/* Deploy and poison redzones around __builtin_alloca call. To do this, we |
| 795 |
should replace this call with another one with changed parameters and |
| 796 |
replace all its uses with new address, so |
| 797 |
addr = __builtin_alloca (old_size, align); |
| 798 |
is replaced by |
| 799 |
left_redzone_size = max (align, ASAN_RED_ZONE_SIZE); |
| 800 |
Following two statements are optimized out if we know that |
| 801 |
old_size & (ASAN_RED_ZONE_SIZE - 1) == 0, i.e. alloca doesn't need partial |
| 802 |
redzone. |
| 803 |
misalign = old_size & (ASAN_RED_ZONE_SIZE - 1); |
| 804 |
partial_redzone_size = ASAN_RED_ZONE_SIZE - misalign; |
| 805 |
right_redzone_size = ASAN_RED_ZONE_SIZE; |
| 806 |
additional_size = left_redzone_size + partial_redzone_size + |
| 807 |
right_redzone_size; |
| 808 |
new_size = old_size + additional_size; |
| 809 |
new_alloca = __builtin_alloca (new_size, max (align, 32)) |
| 810 |
__asan_alloca_poison (new_alloca, old_size) |
| 811 |
addr = new_alloca + max (align, ASAN_RED_ZONE_SIZE); |
| 812 |
last_alloca_addr = new_alloca; |
| 813 |
ADDITIONAL_SIZE is added to make new memory allocation contain not only |
| 814 |
requested memory, but also left, partial and right redzones as well as some |
| 815 |
additional space, required by alignment. */ |
| 816 |
|
| 817 |
static void |
| 818 |
handle_builtin_alloca (gcall *call, gimple_stmt_iterator *iter) |
| 819 |
{ |
| 820 |
if (!iter |
| 821 |
|| !(asan_sanitize_allocas_p () || hwasan_sanitize_allocas_p ())) |
| 822 |
return; |
| 823 |
|
| 824 |
gassign *g; |
| 825 |
gcall *gg; |
| 826 |
tree callee = gimple_call_fndecl (call); |
| 827 |
tree lhs = gimple_call_lhs (call); |
| 828 |
tree old_size = gimple_call_arg (call, 0); |
| 829 |
tree ptr_type = lhs ? TREE_TYPE (lhs) : ptr_type_node; |
| 830 |
tree partial_size = NULL_TREE; |
| 831 |
unsigned int align |
| 832 |
= DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA |
| 833 |
? 0 : tree_to_uhwi (gimple_call_arg (call, 1)); |
| 834 |
|
| 835 |
bool throws = false; |
| 836 |
edge e = NULL; |
| 837 |
if (stmt_can_throw_internal (cfun, call)) |
| 838 |
{ |
| 839 |
if (!lhs) |
| 840 |
return; |
| 841 |
throws = true; |
| 842 |
e = find_fallthru_edge (gsi_bb (*iter)->succs); |
| 843 |
} |
| 844 |
|
| 845 |
if (hwasan_sanitize_allocas_p ()) |
| 846 |
{ |
| 847 |
gimple_seq stmts = NULL; |
| 848 |
location_t loc = gimple_location (gsi_stmt (*iter)); |
| 849 |
/* |
| 850 |
HWASAN needs a different expansion. |
| 851 |
|
| 852 |
addr = __builtin_alloca (size, align); |
| 853 |
|
| 854 |
should be replaced by |
| 855 |
|
| 856 |
new_size = size rounded up to HWASAN_TAG_GRANULE_SIZE byte alignment; |
| 857 |
untagged_addr = __builtin_alloca (new_size, align); |
| 858 |
tag = __hwasan_choose_alloca_tag (); |
| 859 |
addr = ifn_HWASAN_SET_TAG (untagged_addr, tag); |
| 860 |
__hwasan_tag_memory (untagged_addr, tag, new_size); |
| 861 |
*/ |
| 862 |
/* Ensure alignment at least HWASAN_TAG_GRANULE_SIZE bytes so we start on |
| 863 |
a tag granule. */ |
| 864 |
align = align > HWASAN_TAG_GRANULE_SIZE ? align : HWASAN_TAG_GRANULE_SIZE; |
| 865 |
|
| 866 |
tree old_size = gimple_call_arg (call, 0); |
| 867 |
tree new_size = gimple_build_round_up (&stmts, loc, size_type_node, |
| 868 |
old_size, |
| 869 |
HWASAN_TAG_GRANULE_SIZE); |
| 870 |
|
| 871 |
/* Make the alloca call */ |
| 872 |
tree untagged_addr |
| 873 |
= gimple_build (&stmts, loc, |
| 874 |
as_combined_fn (BUILT_IN_ALLOCA_WITH_ALIGN), ptr_type, |
| 875 |
new_size, build_int_cst (size_type_node, align)); |
| 876 |
|
| 877 |
/* Choose the tag. |
| 878 |
Here we use an internal function so we can choose the tag at expand |
| 879 |
time. We need the decision to be made after stack variables have been |
| 880 |
assigned their tag (i.e. once the hwasan_frame_tag_offset variable has |
| 881 |
been set to one after the last stack variables tag). */ |
| 882 |
tree tag = gimple_build (&stmts, loc, CFN_HWASAN_CHOOSE_TAG, |
| 883 |
unsigned_char_type_node); |
| 884 |
|
| 885 |
/* Add tag to pointer. */ |
| 886 |
tree addr |
| 887 |
= gimple_build (&stmts, loc, CFN_HWASAN_SET_TAG, ptr_type, |
| 888 |
untagged_addr, tag); |
| 889 |
|
| 890 |
/* Tag shadow memory. |
| 891 |
NOTE: require using `untagged_addr` here for libhwasan API. */ |
| 892 |
gimple_build (&stmts, loc, as_combined_fn (BUILT_IN_HWASAN_TAG_MEM), |
| 893 |
void_type_node, untagged_addr, tag, new_size); |
| 894 |
|
| 895 |
/* Insert the built up code sequence into the original instruction stream |
| 896 |
the iterator points to. */ |
| 897 |
gsi_insert_seq_before (iter, stmts, GSI_SAME_STMT); |
| 898 |
|
| 899 |
/* Finally, replace old alloca ptr with NEW_ALLOCA. */ |
| 900 |
replace_call_with_value (iter, addr); |
| 901 |
return; |
| 902 |
} |
| 903 |
|
| 904 |
tree last_alloca = get_last_alloca_addr (); |
| 905 |
const HOST_WIDE_INT redzone_mask = ASAN_RED_ZONE_SIZE - 1; |
| 906 |
|
| 907 |
/* If ALIGN > ASAN_RED_ZONE_SIZE, we embed left redzone into first ALIGN |
| 908 |
bytes of allocated space. Otherwise, align alloca to ASAN_RED_ZONE_SIZE |
| 909 |
manually. */ |
| 910 |
align = MAX (align, ASAN_RED_ZONE_SIZE * BITS_PER_UNIT); |
| 911 |
|
| 912 |
tree alloca_rz_mask = build_int_cst (size_type_node, redzone_mask); |
| 913 |
tree redzone_size = build_int_cst (size_type_node, ASAN_RED_ZONE_SIZE); |
| 914 |
|
| 915 |
/* Extract lower bits from old_size. */ |
| 916 |
wide_int size_nonzero_bits = get_nonzero_bits (old_size); |
| 917 |
wide_int rz_mask |
| 918 |
= wi::uhwi (redzone_mask, wi::get_precision (size_nonzero_bits)); |
| 919 |
wide_int old_size_lower_bits = wi::bit_and (size_nonzero_bits, rz_mask); |
| 920 |
|
| 921 |
/* If alloca size is aligned to ASAN_RED_ZONE_SIZE, we don't need partial |
| 922 |
redzone. Otherwise, compute its size here. */ |
| 923 |
if (wi::ne_p (old_size_lower_bits, 0)) |
| 924 |
{ |
| 925 |
/* misalign = size & (ASAN_RED_ZONE_SIZE - 1) |
| 926 |
partial_size = ASAN_RED_ZONE_SIZE - misalign. */ |
| 927 |
g = gimple_build_assign (make_ssa_name (size_type_node, NULL), |
| 928 |
BIT_AND_EXPR, old_size, alloca_rz_mask); |
| 929 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 930 |
tree misalign = gimple_assign_lhs (g); |
| 931 |
g = gimple_build_assign (make_ssa_name (size_type_node, NULL), MINUS_EXPR, |
| 932 |
redzone_size, misalign); |
| 933 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 934 |
partial_size = gimple_assign_lhs (g); |
| 935 |
} |
| 936 |
|
| 937 |
/* additional_size = align + ASAN_RED_ZONE_SIZE. */ |
| 938 |
tree additional_size = build_int_cst (size_type_node, align / BITS_PER_UNIT |
| 939 |
+ ASAN_RED_ZONE_SIZE); |
| 940 |
/* If alloca has partial redzone, include it to additional_size too. */ |
| 941 |
if (partial_size) |
| 942 |
{ |
| 943 |
/* additional_size += partial_size. */ |
| 944 |
g = gimple_build_assign (make_ssa_name (size_type_node), PLUS_EXPR, |
| 945 |
partial_size, additional_size); |
| 946 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 947 |
additional_size = gimple_assign_lhs (g); |
| 948 |
} |
| 949 |
|
| 950 |
/* new_size = old_size + additional_size. */ |
| 951 |
g = gimple_build_assign (make_ssa_name (size_type_node), PLUS_EXPR, old_size, |
| 952 |
additional_size); |
| 953 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 954 |
tree new_size = gimple_assign_lhs (g); |
| 955 |
|
| 956 |
/* Build new __builtin_alloca call: |
| 957 |
new_alloca_with_rz = __builtin_alloca (new_size, align). */ |
| 958 |
tree fn = builtin_decl_implicit (BUILT_IN_ALLOCA_WITH_ALIGN); |
| 959 |
gg = gimple_build_call (fn, 2, new_size, |
| 960 |
build_int_cst (size_type_node, align)); |
| 961 |
tree new_alloca_with_rz = make_ssa_name (ptr_type, gg); |
| 962 |
gimple_call_set_lhs (gg, new_alloca_with_rz); |
| 963 |
if (throws) |
| 964 |
{ |
| 965 |
gimple_call_set_lhs (call, NULL); |
| 966 |
gsi_replace (iter, gg, true); |
| 967 |
} |
| 968 |
else |
| 969 |
gsi_insert_before (iter, gg, GSI_SAME_STMT); |
| 970 |
|
| 971 |
/* new_alloca = new_alloca_with_rz + align. */ |
| 972 |
g = gimple_build_assign (make_ssa_name (ptr_type), POINTER_PLUS_EXPR, |
| 973 |
new_alloca_with_rz, |
| 974 |
build_int_cst (size_type_node, |
| 975 |
align / BITS_PER_UNIT)); |
| 976 |
gimple_stmt_iterator gsi = gsi_none (); |
| 977 |
if (throws) |
| 978 |
{ |
| 979 |
gsi_insert_on_edge_immediate (e, g); |
| 980 |
gsi = gsi_for_stmt (g); |
| 981 |
} |
| 982 |
else |
| 983 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 984 |
tree new_alloca = gimple_assign_lhs (g); |
| 985 |
|
| 986 |
/* Poison newly created alloca redzones: |
| 987 |
__asan_alloca_poison (new_alloca, old_size). */ |
| 988 |
fn = builtin_decl_implicit (BUILT_IN_ASAN_ALLOCA_POISON); |
| 989 |
gg = gimple_build_call (fn, 2, new_alloca, old_size); |
| 990 |
if (throws) |
| 991 |
gsi_insert_after (&gsi, gg, GSI_NEW_STMT); |
| 992 |
else |
| 993 |
gsi_insert_before (iter, gg, GSI_SAME_STMT); |
| 994 |
|
| 995 |
/* Save new_alloca_with_rz value into last_alloca to use it during |
| 996 |
allocas unpoisoning. */ |
| 997 |
g = gimple_build_assign (last_alloca, new_alloca_with_rz); |
| 998 |
if (throws) |
| 999 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 1000 |
else |
| 1001 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 1002 |
|
| 1003 |
/* Finally, replace old alloca ptr with NEW_ALLOCA. */ |
| 1004 |
if (throws) |
| 1005 |
{ |
| 1006 |
g = gimple_build_assign (lhs, new_alloca); |
| 1007 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 1008 |
} |
| 1009 |
else |
| 1010 |
replace_call_with_value (iter, new_alloca); |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* Return the memory references contained in a gimple statement |
| 1014 |
representing a builtin call that has to do with memory access. */ |
| 1015 |
|
| 1016 |
static bool |
| 1017 |
get_mem_refs_of_builtin_call (gcall *call, |
| 1018 |
asan_mem_ref *src0, |
| 1019 |
tree *src0_len, |
| 1020 |
bool *src0_is_store, |
| 1021 |
asan_mem_ref *src1, |
| 1022 |
tree *src1_len, |
| 1023 |
bool *src1_is_store, |
| 1024 |
asan_mem_ref *dst, |
| 1025 |
tree *dst_len, |
| 1026 |
bool *dst_is_store, |
| 1027 |
bool *dest_is_deref, |
| 1028 |
bool *intercepted_p, |
| 1029 |
gimple_stmt_iterator *iter = NULL) |
| 1030 |
{ |
| 1031 |
gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); |
| 1032 |
|
| 1033 |
tree callee = gimple_call_fndecl (call); |
| 1034 |
tree source0 = NULL_TREE, source1 = NULL_TREE, |
| 1035 |
dest = NULL_TREE, len = NULL_TREE; |
| 1036 |
bool is_store = true, got_reference_p = false; |
| 1037 |
HOST_WIDE_INT access_size = 1; |
| 1038 |
|
| 1039 |
*intercepted_p = asan_intercepted_p ((DECL_FUNCTION_CODE (callee))); |
| 1040 |
|
| 1041 |
switch (DECL_FUNCTION_CODE (callee)) |
| 1042 |
{ |
| 1043 |
/* (s, s, n) style memops. */ |
| 1044 |
case BUILT_IN_BCMP: |
| 1045 |
case BUILT_IN_MEMCMP: |
| 1046 |
source0 = gimple_call_arg (call, 0); |
| 1047 |
source1 = gimple_call_arg (call, 1); |
| 1048 |
len = gimple_call_arg (call, 2); |
| 1049 |
break; |
| 1050 |
|
| 1051 |
/* (src, dest, n) style memops. */ |
| 1052 |
case BUILT_IN_BCOPY: |
| 1053 |
source0 = gimple_call_arg (call, 0); |
| 1054 |
dest = gimple_call_arg (call, 1); |
| 1055 |
len = gimple_call_arg (call, 2); |
| 1056 |
break; |
| 1057 |
|
| 1058 |
/* (dest, src, n) style memops. */ |
| 1059 |
case BUILT_IN_MEMCPY: |
| 1060 |
case BUILT_IN_MEMCPY_CHK: |
| 1061 |
case BUILT_IN_MEMMOVE: |
| 1062 |
case BUILT_IN_MEMMOVE_CHK: |
| 1063 |
case BUILT_IN_MEMPCPY: |
| 1064 |
case BUILT_IN_MEMPCPY_CHK: |
| 1065 |
dest = gimple_call_arg (call, 0); |
| 1066 |
source0 = gimple_call_arg (call, 1); |
| 1067 |
len = gimple_call_arg (call, 2); |
| 1068 |
break; |
| 1069 |
|
| 1070 |
/* (dest, n) style memops. */ |
| 1071 |
case BUILT_IN_BZERO: |
| 1072 |
dest = gimple_call_arg (call, 0); |
| 1073 |
len = gimple_call_arg (call, 1); |
| 1074 |
break; |
| 1075 |
|
| 1076 |
/* (dest, x, n) style memops*/ |
| 1077 |
case BUILT_IN_MEMSET: |
| 1078 |
case BUILT_IN_MEMSET_CHK: |
| 1079 |
dest = gimple_call_arg (call, 0); |
| 1080 |
len = gimple_call_arg (call, 2); |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case BUILT_IN_STRLEN: |
| 1084 |
/* Special case strlen here since its length is taken from its return |
| 1085 |
value. |
| 1086 |
|
| 1087 |
The approach taken by the sanitizers is to check a memory access |
| 1088 |
before it's taken. For ASAN strlen is intercepted by libasan, so no |
| 1089 |
check is inserted by the compiler. |
| 1090 |
|
| 1091 |
This function still returns `true` and provides a length to the rest |
| 1092 |
of the ASAN pass in order to record what areas have been checked, |
| 1093 |
avoiding superfluous checks later on. |
| 1094 |
|
| 1095 |
HWASAN does not intercept any of these internal functions. |
| 1096 |
This means that checks for memory accesses must be inserted by the |
| 1097 |
compiler. |
| 1098 |
strlen is a special case, because we can tell the length from the |
| 1099 |
return of the function, but that is not known until after the function |
| 1100 |
has returned. |
| 1101 |
|
| 1102 |
Hence we can't check the memory access before it happens. |
| 1103 |
We could check the memory access after it has already happened, but |
| 1104 |
for now we choose to just ignore `strlen` calls. |
| 1105 |
This decision was simply made because that means the special case is |
| 1106 |
limited to this one case of this one function. */ |
| 1107 |
if (hwasan_sanitize_p ()) |
| 1108 |
return false; |
| 1109 |
source0 = gimple_call_arg (call, 0); |
| 1110 |
len = gimple_call_lhs (call); |
| 1111 |
break; |
| 1112 |
|
| 1113 |
case BUILT_IN_STACK_RESTORE: |
| 1114 |
handle_builtin_stack_restore (call, iter); |
| 1115 |
break; |
| 1116 |
|
| 1117 |
CASE_BUILT_IN_ALLOCA: |
| 1118 |
handle_builtin_alloca (call, iter); |
| 1119 |
break; |
| 1120 |
/* And now the __atomic* and __sync builtins. |
| 1121 |
These are handled differently from the classical memory |
| 1122 |
access builtins above. */ |
| 1123 |
|
| 1124 |
case BUILT_IN_ATOMIC_LOAD_1: |
| 1125 |
is_store = false; |
| 1126 |
/* FALLTHRU */ |
| 1127 |
case BUILT_IN_SYNC_FETCH_AND_ADD_1: |
| 1128 |
case BUILT_IN_SYNC_FETCH_AND_SUB_1: |
| 1129 |
case BUILT_IN_SYNC_FETCH_AND_OR_1: |
| 1130 |
case BUILT_IN_SYNC_FETCH_AND_AND_1: |
| 1131 |
case BUILT_IN_SYNC_FETCH_AND_XOR_1: |
| 1132 |
case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
| 1133 |
case BUILT_IN_SYNC_ADD_AND_FETCH_1: |
| 1134 |
case BUILT_IN_SYNC_SUB_AND_FETCH_1: |
| 1135 |
case BUILT_IN_SYNC_OR_AND_FETCH_1: |
| 1136 |
case BUILT_IN_SYNC_AND_AND_FETCH_1: |
| 1137 |
case BUILT_IN_SYNC_XOR_AND_FETCH_1: |
| 1138 |
case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
| 1139 |
case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: |
| 1140 |
case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: |
| 1141 |
case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: |
| 1142 |
case BUILT_IN_SYNC_LOCK_RELEASE_1: |
| 1143 |
case BUILT_IN_ATOMIC_EXCHANGE_1: |
| 1144 |
case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: |
| 1145 |
case BUILT_IN_ATOMIC_STORE_1: |
| 1146 |
case BUILT_IN_ATOMIC_ADD_FETCH_1: |
| 1147 |
case BUILT_IN_ATOMIC_SUB_FETCH_1: |
| 1148 |
case BUILT_IN_ATOMIC_AND_FETCH_1: |
| 1149 |
case BUILT_IN_ATOMIC_NAND_FETCH_1: |
| 1150 |
case BUILT_IN_ATOMIC_XOR_FETCH_1: |
| 1151 |
case BUILT_IN_ATOMIC_OR_FETCH_1: |
| 1152 |
case BUILT_IN_ATOMIC_FETCH_ADD_1: |
| 1153 |
case BUILT_IN_ATOMIC_FETCH_SUB_1: |
| 1154 |
case BUILT_IN_ATOMIC_FETCH_AND_1: |
| 1155 |
case BUILT_IN_ATOMIC_FETCH_NAND_1: |
| 1156 |
case BUILT_IN_ATOMIC_FETCH_XOR_1: |
| 1157 |
case BUILT_IN_ATOMIC_FETCH_OR_1: |
| 1158 |
access_size = 1; |
| 1159 |
goto do_atomic; |
| 1160 |
|
| 1161 |
case BUILT_IN_ATOMIC_LOAD_2: |
| 1162 |
is_store = false; |
| 1163 |
/* FALLTHRU */ |
| 1164 |
case BUILT_IN_SYNC_FETCH_AND_ADD_2: |
| 1165 |
case BUILT_IN_SYNC_FETCH_AND_SUB_2: |
| 1166 |
case BUILT_IN_SYNC_FETCH_AND_OR_2: |
| 1167 |
case BUILT_IN_SYNC_FETCH_AND_AND_2: |
| 1168 |
case BUILT_IN_SYNC_FETCH_AND_XOR_2: |
| 1169 |
case BUILT_IN_SYNC_FETCH_AND_NAND_2: |
| 1170 |
case BUILT_IN_SYNC_ADD_AND_FETCH_2: |
| 1171 |
case BUILT_IN_SYNC_SUB_AND_FETCH_2: |
| 1172 |
case BUILT_IN_SYNC_OR_AND_FETCH_2: |
| 1173 |
case BUILT_IN_SYNC_AND_AND_FETCH_2: |
| 1174 |
case BUILT_IN_SYNC_XOR_AND_FETCH_2: |
| 1175 |
case BUILT_IN_SYNC_NAND_AND_FETCH_2: |
| 1176 |
case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: |
| 1177 |
case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: |
| 1178 |
case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: |
| 1179 |
case BUILT_IN_SYNC_LOCK_RELEASE_2: |
| 1180 |
case BUILT_IN_ATOMIC_EXCHANGE_2: |
| 1181 |
case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: |
| 1182 |
case BUILT_IN_ATOMIC_STORE_2: |
| 1183 |
case BUILT_IN_ATOMIC_ADD_FETCH_2: |
| 1184 |
case BUILT_IN_ATOMIC_SUB_FETCH_2: |
| 1185 |
case BUILT_IN_ATOMIC_AND_FETCH_2: |
| 1186 |
case BUILT_IN_ATOMIC_NAND_FETCH_2: |
| 1187 |
case BUILT_IN_ATOMIC_XOR_FETCH_2: |
| 1188 |
case BUILT_IN_ATOMIC_OR_FETCH_2: |
| 1189 |
case BUILT_IN_ATOMIC_FETCH_ADD_2: |
| 1190 |
case BUILT_IN_ATOMIC_FETCH_SUB_2: |
| 1191 |
case BUILT_IN_ATOMIC_FETCH_AND_2: |
| 1192 |
case BUILT_IN_ATOMIC_FETCH_NAND_2: |
| 1193 |
case BUILT_IN_ATOMIC_FETCH_XOR_2: |
| 1194 |
case BUILT_IN_ATOMIC_FETCH_OR_2: |
| 1195 |
access_size = 2; |
| 1196 |
goto do_atomic; |
| 1197 |
|
| 1198 |
case BUILT_IN_ATOMIC_LOAD_4: |
| 1199 |
is_store = false; |
| 1200 |
/* FALLTHRU */ |
| 1201 |
case BUILT_IN_SYNC_FETCH_AND_ADD_4: |
| 1202 |
case BUILT_IN_SYNC_FETCH_AND_SUB_4: |
| 1203 |
case BUILT_IN_SYNC_FETCH_AND_OR_4: |
| 1204 |
case BUILT_IN_SYNC_FETCH_AND_AND_4: |
| 1205 |
case BUILT_IN_SYNC_FETCH_AND_XOR_4: |
| 1206 |
case BUILT_IN_SYNC_FETCH_AND_NAND_4: |
| 1207 |
case BUILT_IN_SYNC_ADD_AND_FETCH_4: |
| 1208 |
case BUILT_IN_SYNC_SUB_AND_FETCH_4: |
| 1209 |
case BUILT_IN_SYNC_OR_AND_FETCH_4: |
| 1210 |
case BUILT_IN_SYNC_AND_AND_FETCH_4: |
| 1211 |
case BUILT_IN_SYNC_XOR_AND_FETCH_4: |
| 1212 |
case BUILT_IN_SYNC_NAND_AND_FETCH_4: |
| 1213 |
case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: |
| 1214 |
case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: |
| 1215 |
case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: |
| 1216 |
case BUILT_IN_SYNC_LOCK_RELEASE_4: |
| 1217 |
case BUILT_IN_ATOMIC_EXCHANGE_4: |
| 1218 |
case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: |
| 1219 |
case BUILT_IN_ATOMIC_STORE_4: |
| 1220 |
case BUILT_IN_ATOMIC_ADD_FETCH_4: |
| 1221 |
case BUILT_IN_ATOMIC_SUB_FETCH_4: |
| 1222 |
case BUILT_IN_ATOMIC_AND_FETCH_4: |
| 1223 |
case BUILT_IN_ATOMIC_NAND_FETCH_4: |
| 1224 |
case BUILT_IN_ATOMIC_XOR_FETCH_4: |
| 1225 |
case BUILT_IN_ATOMIC_OR_FETCH_4: |
| 1226 |
case BUILT_IN_ATOMIC_FETCH_ADD_4: |
| 1227 |
case BUILT_IN_ATOMIC_FETCH_SUB_4: |
| 1228 |
case BUILT_IN_ATOMIC_FETCH_AND_4: |
| 1229 |
case BUILT_IN_ATOMIC_FETCH_NAND_4: |
| 1230 |
case BUILT_IN_ATOMIC_FETCH_XOR_4: |
| 1231 |
case BUILT_IN_ATOMIC_FETCH_OR_4: |
| 1232 |
access_size = 4; |
| 1233 |
goto do_atomic; |
| 1234 |
|
| 1235 |
case BUILT_IN_ATOMIC_LOAD_8: |
| 1236 |
is_store = false; |
| 1237 |
/* FALLTHRU */ |
| 1238 |
case BUILT_IN_SYNC_FETCH_AND_ADD_8: |
| 1239 |
case BUILT_IN_SYNC_FETCH_AND_SUB_8: |
| 1240 |
case BUILT_IN_SYNC_FETCH_AND_OR_8: |
| 1241 |
case BUILT_IN_SYNC_FETCH_AND_AND_8: |
| 1242 |
case BUILT_IN_SYNC_FETCH_AND_XOR_8: |
| 1243 |
case BUILT_IN_SYNC_FETCH_AND_NAND_8: |
| 1244 |
case BUILT_IN_SYNC_ADD_AND_FETCH_8: |
| 1245 |
case BUILT_IN_SYNC_SUB_AND_FETCH_8: |
| 1246 |
case BUILT_IN_SYNC_OR_AND_FETCH_8: |
| 1247 |
case BUILT_IN_SYNC_AND_AND_FETCH_8: |
| 1248 |
case BUILT_IN_SYNC_XOR_AND_FETCH_8: |
| 1249 |
case BUILT_IN_SYNC_NAND_AND_FETCH_8: |
| 1250 |
case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: |
| 1251 |
case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: |
| 1252 |
case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: |
| 1253 |
case BUILT_IN_SYNC_LOCK_RELEASE_8: |
| 1254 |
case BUILT_IN_ATOMIC_EXCHANGE_8: |
| 1255 |
case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: |
| 1256 |
case BUILT_IN_ATOMIC_STORE_8: |
| 1257 |
case BUILT_IN_ATOMIC_ADD_FETCH_8: |
| 1258 |
case BUILT_IN_ATOMIC_SUB_FETCH_8: |
| 1259 |
case BUILT_IN_ATOMIC_AND_FETCH_8: |
| 1260 |
case BUILT_IN_ATOMIC_NAND_FETCH_8: |
| 1261 |
case BUILT_IN_ATOMIC_XOR_FETCH_8: |
| 1262 |
case BUILT_IN_ATOMIC_OR_FETCH_8: |
| 1263 |
case BUILT_IN_ATOMIC_FETCH_ADD_8: |
| 1264 |
case BUILT_IN_ATOMIC_FETCH_SUB_8: |
| 1265 |
case BUILT_IN_ATOMIC_FETCH_AND_8: |
| 1266 |
case BUILT_IN_ATOMIC_FETCH_NAND_8: |
| 1267 |
case BUILT_IN_ATOMIC_FETCH_XOR_8: |
| 1268 |
case BUILT_IN_ATOMIC_FETCH_OR_8: |
| 1269 |
access_size = 8; |
| 1270 |
goto do_atomic; |
| 1271 |
|
| 1272 |
case BUILT_IN_ATOMIC_LOAD_16: |
| 1273 |
is_store = false; |
| 1274 |
/* FALLTHRU */ |
| 1275 |
case BUILT_IN_SYNC_FETCH_AND_ADD_16: |
| 1276 |
case BUILT_IN_SYNC_FETCH_AND_SUB_16: |
| 1277 |
case BUILT_IN_SYNC_FETCH_AND_OR_16: |
| 1278 |
case BUILT_IN_SYNC_FETCH_AND_AND_16: |
| 1279 |
case BUILT_IN_SYNC_FETCH_AND_XOR_16: |
| 1280 |
case BUILT_IN_SYNC_FETCH_AND_NAND_16: |
| 1281 |
case BUILT_IN_SYNC_ADD_AND_FETCH_16: |
| 1282 |
case BUILT_IN_SYNC_SUB_AND_FETCH_16: |
| 1283 |
case BUILT_IN_SYNC_OR_AND_FETCH_16: |
| 1284 |
case BUILT_IN_SYNC_AND_AND_FETCH_16: |
| 1285 |
case BUILT_IN_SYNC_XOR_AND_FETCH_16: |
| 1286 |
case BUILT_IN_SYNC_NAND_AND_FETCH_16: |
| 1287 |
case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: |
| 1288 |
case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: |
| 1289 |
case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: |
| 1290 |
case BUILT_IN_SYNC_LOCK_RELEASE_16: |
| 1291 |
case BUILT_IN_ATOMIC_EXCHANGE_16: |
| 1292 |
case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: |
| 1293 |
case BUILT_IN_ATOMIC_STORE_16: |
| 1294 |
case BUILT_IN_ATOMIC_ADD_FETCH_16: |
| 1295 |
case BUILT_IN_ATOMIC_SUB_FETCH_16: |
| 1296 |
case BUILT_IN_ATOMIC_AND_FETCH_16: |
| 1297 |
case BUILT_IN_ATOMIC_NAND_FETCH_16: |
| 1298 |
case BUILT_IN_ATOMIC_XOR_FETCH_16: |
| 1299 |
case BUILT_IN_ATOMIC_OR_FETCH_16: |
| 1300 |
case BUILT_IN_ATOMIC_FETCH_ADD_16: |
| 1301 |
case BUILT_IN_ATOMIC_FETCH_SUB_16: |
| 1302 |
case BUILT_IN_ATOMIC_FETCH_AND_16: |
| 1303 |
case BUILT_IN_ATOMIC_FETCH_NAND_16: |
| 1304 |
case BUILT_IN_ATOMIC_FETCH_XOR_16: |
| 1305 |
case BUILT_IN_ATOMIC_FETCH_OR_16: |
| 1306 |
access_size = 16; |
| 1307 |
/* FALLTHRU */ |
| 1308 |
do_atomic: |
| 1309 |
{ |
| 1310 |
dest = gimple_call_arg (call, 0); |
| 1311 |
/* DEST represents the address of a memory location. |
| 1312 |
instrument_derefs wants the memory location, so lets |
| 1313 |
dereference the address DEST before handing it to |
| 1314 |
instrument_derefs. */ |
| 1315 |
tree type = build_nonstandard_integer_type (access_size |
| 1316 |
* BITS_PER_UNIT, 1); |
| 1317 |
dest = build2 (MEM_REF, type, dest, |
| 1318 |
build_int_cst (build_pointer_type (char_type_node), 0)); |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
|
| 1322 |
default: |
| 1323 |
/* The other builtins memory access are not instrumented in this |
| 1324 |
function because they either don't have any length parameter, |
| 1325 |
or their length parameter is just a limit. */ |
| 1326 |
break; |
| 1327 |
} |
| 1328 |
|
| 1329 |
if (len != NULL_TREE) |
| 1330 |
{ |
| 1331 |
if (source0 != NULL_TREE) |
| 1332 |
{ |
| 1333 |
src0->start = source0; |
| 1334 |
src0->access_size = access_size; |
| 1335 |
*src0_len = len; |
| 1336 |
*src0_is_store = false; |
| 1337 |
} |
| 1338 |
|
| 1339 |
if (source1 != NULL_TREE) |
| 1340 |
{ |
| 1341 |
src1->start = source1; |
| 1342 |
src1->access_size = access_size; |
| 1343 |
*src1_len = len; |
| 1344 |
*src1_is_store = false; |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (dest != NULL_TREE) |
| 1348 |
{ |
| 1349 |
dst->start = dest; |
| 1350 |
dst->access_size = access_size; |
| 1351 |
*dst_len = len; |
| 1352 |
*dst_is_store = true; |
| 1353 |
} |
| 1354 |
|
| 1355 |
got_reference_p = true; |
| 1356 |
} |
| 1357 |
else if (dest) |
| 1358 |
{ |
| 1359 |
dst->start = dest; |
| 1360 |
dst->access_size = access_size; |
| 1361 |
*dst_len = NULL_TREE; |
| 1362 |
*dst_is_store = is_store; |
| 1363 |
*dest_is_deref = true; |
| 1364 |
got_reference_p = true; |
| 1365 |
} |
| 1366 |
|
| 1367 |
return got_reference_p; |
| 1368 |
} |
| 1369 |
|
| 1370 |
/* Return true iff a given gimple statement has been instrumented. |
| 1371 |
Note that the statement is "defined" by the memory references it |
| 1372 |
contains. */ |
| 1373 |
|
| 1374 |
static bool |
| 1375 |
has_stmt_been_instrumented_p (gimple *stmt) |
| 1376 |
{ |
| 1377 |
if (gimple_assign_single_p (stmt)) |
| 1378 |
{ |
| 1379 |
bool r_is_store; |
| 1380 |
asan_mem_ref r; |
| 1381 |
asan_mem_ref_init (&r, NULL, 1); |
| 1382 |
|
| 1383 |
if (get_mem_ref_of_assignment (as_a <gassign *> (stmt), &r, |
| 1384 |
&r_is_store)) |
| 1385 |
{ |
| 1386 |
if (!has_mem_ref_been_instrumented (&r)) |
| 1387 |
return false; |
| 1388 |
if (r_is_store && gimple_assign_load_p (stmt)) |
| 1389 |
{ |
| 1390 |
asan_mem_ref src; |
| 1391 |
asan_mem_ref_init (&src, NULL, 1); |
| 1392 |
src.start = gimple_assign_rhs1 (stmt); |
| 1393 |
src.access_size = int_size_in_bytes (TREE_TYPE (src.start)); |
| 1394 |
if (!has_mem_ref_been_instrumented (&src)) |
| 1395 |
return false; |
| 1396 |
} |
| 1397 |
return true; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) |
| 1401 |
{ |
| 1402 |
asan_mem_ref src0, src1, dest; |
| 1403 |
asan_mem_ref_init (&src0, NULL, 1); |
| 1404 |
asan_mem_ref_init (&src1, NULL, 1); |
| 1405 |
asan_mem_ref_init (&dest, NULL, 1); |
| 1406 |
|
| 1407 |
tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; |
| 1408 |
bool src0_is_store = false, src1_is_store = false, |
| 1409 |
dest_is_store = false, dest_is_deref = false, intercepted_p = true; |
| 1410 |
if (get_mem_refs_of_builtin_call (as_a <gcall *> (stmt), |
| 1411 |
&src0, &src0_len, &src0_is_store, |
| 1412 |
&src1, &src1_len, &src1_is_store, |
| 1413 |
&dest, &dest_len, &dest_is_store, |
| 1414 |
&dest_is_deref, &intercepted_p)) |
| 1415 |
{ |
| 1416 |
if (src0.start != NULL_TREE |
| 1417 |
&& !has_mem_ref_been_instrumented (&src0, src0_len)) |
| 1418 |
return false; |
| 1419 |
|
| 1420 |
if (src1.start != NULL_TREE |
| 1421 |
&& !has_mem_ref_been_instrumented (&src1, src1_len)) |
| 1422 |
return false; |
| 1423 |
|
| 1424 |
if (dest.start != NULL_TREE |
| 1425 |
&& !has_mem_ref_been_instrumented (&dest, dest_len)) |
| 1426 |
return false; |
| 1427 |
|
| 1428 |
return true; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
else if (is_gimple_call (stmt) |
| 1432 |
&& gimple_store_p (stmt) |
| 1433 |
&& (gimple_call_builtin_p (stmt) |
| 1434 |
|| gimple_call_internal_p (stmt) |
| 1435 |
|| !aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)), |
| 1436 |
gimple_call_fntype (stmt)))) |
| 1437 |
{ |
| 1438 |
asan_mem_ref r; |
| 1439 |
asan_mem_ref_init (&r, NULL, 1); |
| 1440 |
|
| 1441 |
r.start = gimple_call_lhs (stmt); |
| 1442 |
r.access_size = int_size_in_bytes (TREE_TYPE (r.start)); |
| 1443 |
return has_mem_ref_been_instrumented (&r); |
| 1444 |
} |
| 1445 |
|
| 1446 |
return false; |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* Insert a memory reference into the hash table. */ |
| 1450 |
|
| 1451 |
static void |
| 1452 |
update_mem_ref_hash_table (tree ref, HOST_WIDE_INT access_size) |
| 1453 |
{ |
| 1454 |
hash_table<asan_mem_ref_hasher> *ht = get_mem_ref_hash_table (); |
| 1455 |
|
| 1456 |
asan_mem_ref r; |
| 1457 |
asan_mem_ref_init (&r, ref, access_size); |
| 1458 |
|
| 1459 |
asan_mem_ref **slot = ht->find_slot (&r, INSERT); |
| 1460 |
if (*slot == NULL || (*slot)->access_size < access_size) |
| 1461 |
*slot = asan_mem_ref_new (ref, access_size); |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* Initialize shadow_ptr_types array. */ |
| 1465 |
|
| 1466 |
static void |
| 1467 |
asan_init_shadow_ptr_types (void) |
| 1468 |
{ |
| 1469 |
asan_shadow_set = new_alias_set (); |
| 1470 |
tree types[3] = { signed_char_type_node, short_integer_type_node, |
| 1471 |
integer_type_node }; |
| 1472 |
|
| 1473 |
for (unsigned i = 0; i < 3; i++) |
| 1474 |
{ |
| 1475 |
shadow_ptr_types[i] = build_distinct_type_copy (types[i]); |
| 1476 |
TYPE_ALIAS_SET (shadow_ptr_types[i]) = asan_shadow_set; |
| 1477 |
shadow_ptr_types[i] = build_pointer_type (shadow_ptr_types[i]); |
| 1478 |
} |
| 1479 |
|
| 1480 |
initialize_sanitizer_builtins (); |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* Create ADDR_EXPR of STRING_CST with the PP pretty printer text. */ |
| 1484 |
|
| 1485 |
static tree |
| 1486 |
asan_pp_string (pretty_printer *pp) |
| 1487 |
{ |
| 1488 |
const char *buf = pp_formatted_text (pp); |
| 1489 |
size_t len = strlen (buf); |
| 1490 |
tree ret = build_string (len + 1, buf); |
| 1491 |
TREE_TYPE (ret) |
| 1492 |
= build_array_type (TREE_TYPE (shadow_ptr_types[0]), |
| 1493 |
build_index_type (size_int (len))); |
| 1494 |
TREE_READONLY (ret) = 1; |
| 1495 |
TREE_STATIC (ret) = 1; |
| 1496 |
return build1 (ADDR_EXPR, shadow_ptr_types[0], ret); |
| 1497 |
} |
| 1498 |
|
| 1499 |
/* Clear shadow memory at SHADOW_MEM, LEN bytes. Can't call a library call here |
| 1500 |
though. */ |
| 1501 |
|
| 1502 |
static void |
| 1503 |
asan_clear_shadow (rtx shadow_mem, HOST_WIDE_INT len) |
| 1504 |
{ |
| 1505 |
rtx_insn *insn, *insns, *jump; |
| 1506 |
rtx_code_label *top_label; |
| 1507 |
rtx end, addr, tmp; |
| 1508 |
|
| 1509 |
gcc_assert ((len & 3) == 0); |
| 1510 |
start_sequence (); |
| 1511 |
clear_storage (shadow_mem, GEN_INT (len), BLOCK_OP_NORMAL); |
| 1512 |
insns = get_insns (); |
| 1513 |
end_sequence (); |
| 1514 |
for (insn = insns; insn; insn = NEXT_INSN (insn)) |
| 1515 |
if (CALL_P (insn)) |
| 1516 |
break; |
| 1517 |
if (insn == NULL_RTX) |
| 1518 |
{ |
| 1519 |
emit_insn (insns); |
| 1520 |
return; |
| 1521 |
} |
| 1522 |
|
| 1523 |
top_label = gen_label_rtx (); |
| 1524 |
addr = copy_to_mode_reg (Pmode, XEXP (shadow_mem, 0)); |
| 1525 |
shadow_mem = adjust_automodify_address (shadow_mem, SImode, addr, 0); |
| 1526 |
end = force_reg (Pmode, plus_constant (Pmode, addr, len)); |
| 1527 |
emit_label (top_label); |
| 1528 |
|
| 1529 |
emit_move_insn (shadow_mem, const0_rtx); |
| 1530 |
tmp = expand_simple_binop (Pmode, PLUS, addr, gen_int_mode (4, Pmode), addr, |
| 1531 |
true, OPTAB_LIB_WIDEN); |
| 1532 |
if (tmp != addr) |
| 1533 |
emit_move_insn (addr, tmp); |
| 1534 |
emit_cmp_and_jump_insns (addr, end, LT, NULL_RTX, Pmode, true, top_label); |
| 1535 |
jump = get_last_insn (); |
| 1536 |
gcc_assert (JUMP_P (jump)); |
| 1537 |
add_reg_br_prob_note (jump, |
| 1538 |
profile_probability::guessed_always () |
| 1539 |
.apply_scale (80, 100)); |
| 1540 |
} |
| 1541 |
|
| 1542 |
void |
| 1543 |
asan_function_start (void) |
| 1544 |
{ |
| 1545 |
ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LASANPC", current_function_funcdef_no); |
| 1546 |
} |
| 1547 |
|
| 1548 |
/* Return number of shadow bytes that are occupied by a local variable |
| 1549 |
of SIZE bytes. */ |
| 1550 |
|
| 1551 |
static unsigned HOST_WIDE_INT |
| 1552 |
shadow_mem_size (unsigned HOST_WIDE_INT size) |
| 1553 |
{ |
| 1554 |
/* It must be possible to align stack variables to granularity |
| 1555 |
of shadow memory. */ |
| 1556 |
gcc_assert (BITS_PER_UNIT |
| 1557 |
* ASAN_SHADOW_GRANULARITY <= MAX_SUPPORTED_STACK_ALIGNMENT); |
| 1558 |
|
| 1559 |
return ROUND_UP (size, ASAN_SHADOW_GRANULARITY) / ASAN_SHADOW_GRANULARITY; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Always emit 4 bytes at a time. */ |
| 1563 |
#define RZ_BUFFER_SIZE 4 |
| 1564 |
|
| 1565 |
/* ASAN redzone buffer container that handles emission of shadow bytes. */ |
| 1566 |
class asan_redzone_buffer |
| 1567 |
{ |
| 1568 |
public: |
| 1569 |
/* Constructor. */ |
| 1570 |
asan_redzone_buffer (rtx shadow_mem, HOST_WIDE_INT prev_offset): |
| 1571 |
m_shadow_mem (shadow_mem), m_prev_offset (prev_offset), |
| 1572 |
m_original_offset (prev_offset), m_shadow_bytes (RZ_BUFFER_SIZE) |
| 1573 |
{} |
| 1574 |
|
| 1575 |
/* Emit VALUE shadow byte at a given OFFSET. */ |
| 1576 |
void emit_redzone_byte (HOST_WIDE_INT offset, unsigned char value); |
| 1577 |
|
| 1578 |
/* Emit RTX emission of the content of the buffer. */ |
| 1579 |
void flush_redzone_payload (void); |
| 1580 |
|
| 1581 |
private: |
| 1582 |
/* Flush if the content of the buffer is full |
| 1583 |
(equal to RZ_BUFFER_SIZE). */ |
| 1584 |
void flush_if_full (void); |
| 1585 |
|
| 1586 |
/* Memory where we last emitted a redzone payload. */ |
| 1587 |
rtx m_shadow_mem; |
| 1588 |
|
| 1589 |
/* Relative offset where we last emitted a redzone payload. */ |
| 1590 |
HOST_WIDE_INT m_prev_offset; |
| 1591 |
|
| 1592 |
/* Relative original offset. Used for checking only. */ |
| 1593 |
HOST_WIDE_INT m_original_offset; |
| 1594 |
|
| 1595 |
public: |
| 1596 |
/* Buffer with redzone payload. */ |
| 1597 |
auto_vec<unsigned char> m_shadow_bytes; |
| 1598 |
}; |
| 1599 |
|
| 1600 |
/* Emit VALUE shadow byte at a given OFFSET. */ |
| 1601 |
|
| 1602 |
void |
| 1603 |
asan_redzone_buffer::emit_redzone_byte (HOST_WIDE_INT offset, |
| 1604 |
unsigned char value) |
| 1605 |
{ |
| 1606 |
gcc_assert ((offset & (ASAN_SHADOW_GRANULARITY - 1)) == 0); |
| 1607 |
gcc_assert (offset >= m_prev_offset); |
| 1608 |
|
| 1609 |
HOST_WIDE_INT off |
| 1610 |
= m_prev_offset + ASAN_SHADOW_GRANULARITY * m_shadow_bytes.length (); |
| 1611 |
if (off == offset) |
| 1612 |
/* Consecutive shadow memory byte. */; |
| 1613 |
else if (offset < m_prev_offset + (HOST_WIDE_INT) (ASAN_SHADOW_GRANULARITY |
| 1614 |
* RZ_BUFFER_SIZE) |
| 1615 |
&& !m_shadow_bytes.is_empty ()) |
| 1616 |
{ |
| 1617 |
/* Shadow memory byte with a small gap. */ |
| 1618 |
for (; off < offset; off += ASAN_SHADOW_GRANULARITY) |
| 1619 |
m_shadow_bytes.safe_push (0); |
| 1620 |
} |
| 1621 |
else |
| 1622 |
{ |
| 1623 |
if (!m_shadow_bytes.is_empty ()) |
| 1624 |
flush_redzone_payload (); |
| 1625 |
|
| 1626 |
/* Maybe start earlier in order to use aligned store. */ |
| 1627 |
HOST_WIDE_INT align = (offset - m_prev_offset) % ASAN_RED_ZONE_SIZE; |
| 1628 |
if (align) |
| 1629 |
{ |
| 1630 |
offset -= align; |
| 1631 |
for (unsigned i = 0; i < align / BITS_PER_UNIT; i++) |
| 1632 |
m_shadow_bytes.safe_push (0); |
| 1633 |
} |
| 1634 |
|
| 1635 |
/* Adjust m_prev_offset and m_shadow_mem. */ |
| 1636 |
HOST_WIDE_INT diff = offset - m_prev_offset; |
| 1637 |
m_shadow_mem = adjust_address (m_shadow_mem, VOIDmode, |
| 1638 |
diff >> ASAN_SHADOW_SHIFT); |
| 1639 |
m_prev_offset = offset; |
| 1640 |
} |
| 1641 |
m_shadow_bytes.safe_push (value); |
| 1642 |
flush_if_full (); |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* Emit RTX emission of the content of the buffer. */ |
| 1646 |
|
| 1647 |
void |
| 1648 |
asan_redzone_buffer::flush_redzone_payload (void) |
| 1649 |
{ |
| 1650 |
gcc_assert (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN); |
| 1651 |
|
| 1652 |
if (m_shadow_bytes.is_empty ()) |
| 1653 |
return; |
| 1654 |
|
| 1655 |
/* Be sure we always emit to an aligned address. */ |
| 1656 |
gcc_assert (((m_prev_offset - m_original_offset) |
| 1657 |
& (ASAN_RED_ZONE_SIZE - 1)) == 0); |
| 1658 |
|
| 1659 |
/* Fill it to RZ_BUFFER_SIZE bytes with zeros if needed. */ |
| 1660 |
unsigned l = m_shadow_bytes.length (); |
| 1661 |
for (unsigned i = 0; i <= RZ_BUFFER_SIZE - l; i++) |
| 1662 |
m_shadow_bytes.safe_push (0); |
| 1663 |
|
| 1664 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1665 |
fprintf (dump_file, |
| 1666 |
"Flushing rzbuffer at offset %" PRId64 " with: ", m_prev_offset); |
| 1667 |
|
| 1668 |
unsigned HOST_WIDE_INT val = 0; |
| 1669 |
for (unsigned i = 0; i < RZ_BUFFER_SIZE; i++) |
| 1670 |
{ |
| 1671 |
unsigned char v |
| 1672 |
= m_shadow_bytes[BYTES_BIG_ENDIAN ? RZ_BUFFER_SIZE - i - 1 : i]; |
| 1673 |
val |= (unsigned HOST_WIDE_INT)v << (BITS_PER_UNIT * i); |
| 1674 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1675 |
fprintf (dump_file, "%02x ", v); |
| 1676 |
} |
| 1677 |
|
| 1678 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1679 |
fprintf (dump_file, "\n"); |
| 1680 |
|
| 1681 |
rtx c = gen_int_mode (val, SImode); |
| 1682 |
m_shadow_mem = adjust_address (m_shadow_mem, SImode, 0); |
| 1683 |
emit_move_insn (m_shadow_mem, c); |
| 1684 |
m_shadow_bytes.truncate (0); |
| 1685 |
} |
| 1686 |
|
| 1687 |
/* Flush if the content of the buffer is full |
| 1688 |
(equal to RZ_BUFFER_SIZE). */ |
| 1689 |
|
| 1690 |
void |
| 1691 |
asan_redzone_buffer::flush_if_full (void) |
| 1692 |
{ |
| 1693 |
if (m_shadow_bytes.length () == RZ_BUFFER_SIZE) |
| 1694 |
flush_redzone_payload (); |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
/* HWAddressSanitizer (hwasan) is a probabilistic method for detecting |
| 1699 |
out-of-bounds and use-after-free bugs. |
| 1700 |
Read more: |
| 1701 |
http://code.google.com/p/address-sanitizer/ |
| 1702 |
|
| 1703 |
Similar to AddressSanitizer (asan) it consists of two parts: the |
| 1704 |
instrumentation module in this file, and a run-time library. |
| 1705 |
|
| 1706 |
The instrumentation module adds a run-time check before every memory insn in |
| 1707 |
the same manner as asan (see the block comment for AddressSanitizer above). |
| 1708 |
Currently, hwasan only adds out-of-line instrumentation, where each check is |
| 1709 |
implemented as a function call to the run-time library. Hence a check for a |
| 1710 |
load of N bytes from address X would be implemented with a function call to |
| 1711 |
__hwasan_loadN(X), and checking a store of N bytes from address X would be |
| 1712 |
implemented with a function call to __hwasan_storeN(X). |
| 1713 |
|
| 1714 |
The main difference between hwasan and asan is in the information stored to |
| 1715 |
help this checking. Both sanitizers use a shadow memory area which stores |
| 1716 |
data recording the state of main memory at a corresponding address. |
| 1717 |
|
| 1718 |
For hwasan, each 16 byte granule in main memory has a corresponding 1 byte |
| 1719 |
in shadow memory. This shadow address can be calculated with equation: |
| 1720 |
(addr >> log_2(HWASAN_TAG_GRANULE_SIZE)) |
| 1721 |
+ __hwasan_shadow_memory_dynamic_address; |
| 1722 |
The conversion between real and shadow memory for asan is given in the block |
| 1723 |
comment at the top of this file. |
| 1724 |
The description of how this shadow memory is laid out for asan is in the |
| 1725 |
block comment at the top of this file, here we describe how this shadow |
| 1726 |
memory is used for hwasan. |
| 1727 |
|
| 1728 |
For hwasan, each variable is assigned a byte-sized 'tag'. The extent of |
| 1729 |
the shadow memory for that variable is filled with the assigned tag, and |
| 1730 |
every pointer referencing that variable has its top byte set to the same |
| 1731 |
tag. The run-time library redefines malloc so that every allocation returns |
| 1732 |
a tagged pointer and tags the corresponding shadow memory with the same tag. |
| 1733 |
|
| 1734 |
On each pointer dereference the tag found in the pointer is compared to the |
| 1735 |
tag found in the shadow memory corresponding to the accessed memory address. |
| 1736 |
If these tags are found to differ then this memory access is judged to be |
| 1737 |
invalid and a report is generated. |
| 1738 |
|
| 1739 |
This method of bug detection is not perfect -- it can not catch every bad |
| 1740 |
access -- but catches them probabilistically instead. There is always the |
| 1741 |
possibility that an invalid memory access will happen to access memory |
| 1742 |
tagged with the same tag as the pointer that this access used. |
| 1743 |
The chances of this are approx. 0.4% for any two uncorrelated objects. |
| 1744 |
|
| 1745 |
Random tag generation can mitigate this problem by decreasing the |
| 1746 |
probability that an invalid access will be missed in the same manner over |
| 1747 |
multiple runs. i.e. if two objects are tagged the same in one run of the |
| 1748 |
binary they are unlikely to be tagged the same in the next run. |
| 1749 |
Both heap and stack allocated objects have random tags by default. |
| 1750 |
|
| 1751 |
[16 byte granule implications] |
| 1752 |
Since the shadow memory only has a resolution on real memory of 16 bytes, |
| 1753 |
invalid accesses that are within the same 16 byte granule as a valid |
| 1754 |
address will not be caught. |
| 1755 |
|
| 1756 |
There is a "short-granule" feature in the runtime library which does catch |
| 1757 |
such accesses, but this feature is not implemented for stack objects (since |
| 1758 |
stack objects are allocated and tagged by compiler instrumentation, and |
| 1759 |
this feature has not yet been implemented in GCC instrumentation). |
| 1760 |
|
| 1761 |
Another outcome of this 16 byte resolution is that each tagged object must |
| 1762 |
be 16 byte aligned. If two objects were to share any 16 byte granule in |
| 1763 |
memory, then they both would have to be given the same tag, and invalid |
| 1764 |
accesses to one using a pointer to the other would be undetectable. |
| 1765 |
|
| 1766 |
[Compiler instrumentation] |
| 1767 |
Compiler instrumentation ensures that two adjacent buffers on the stack are |
| 1768 |
given different tags, this means an access to one buffer using a pointer |
| 1769 |
generated from the other (e.g. through buffer overrun) will have mismatched |
| 1770 |
tags and be caught by hwasan. |
| 1771 |
|
| 1772 |
We don't randomly tag every object on the stack, since that would require |
| 1773 |
keeping many registers to record each tag. Instead we randomly generate a |
| 1774 |
tag for each function frame, and each new stack object uses a tag offset |
| 1775 |
from that frame tag. |
| 1776 |
i.e. each object is tagged as RFT + offset, where RFT is the "random frame |
| 1777 |
tag" generated for this frame. |
| 1778 |
This means that randomisation does not peturb the difference between tags |
| 1779 |
on tagged stack objects within a frame, but this is mitigated by the fact |
| 1780 |
that objects with the same tag within a frame are very far apart |
| 1781 |
(approx. 2^HWASAN_TAG_SIZE objects apart). |
| 1782 |
|
| 1783 |
As a demonstration, using the same example program as in the asan block |
| 1784 |
comment above: |
| 1785 |
|
| 1786 |
int |
| 1787 |
foo () |
| 1788 |
{ |
| 1789 |
char a[24] = {0}; |
| 1790 |
int b[2] = {0}; |
| 1791 |
|
| 1792 |
a[5] = 1; |
| 1793 |
b[1] = 2; |
| 1794 |
|
| 1795 |
return a[5] + b[1]; |
| 1796 |
} |
| 1797 |
|
| 1798 |
On AArch64 the stack will be ordered as follows for the above function: |
| 1799 |
|
| 1800 |
Slot 1/ [24 bytes for variable 'a'] |
| 1801 |
Slot 2/ [8 bytes padding for alignment] |
| 1802 |
Slot 3/ [8 bytes for variable 'b'] |
| 1803 |
Slot 4/ [8 bytes padding for alignment] |
| 1804 |
|
| 1805 |
(The padding is there to ensure 16 byte alignment as described in the 16 |
| 1806 |
byte granule implications). |
| 1807 |
|
| 1808 |
While the shadow memory will be ordered as follows: |
| 1809 |
|
| 1810 |
- 2 bytes (representing 32 bytes in real memory) tagged with RFT + 1. |
| 1811 |
- 1 byte (representing 16 bytes in real memory) tagged with RFT + 2. |
| 1812 |
|
| 1813 |
And any pointer to "a" will have the tag RFT + 1, and any pointer to "b" |
| 1814 |
will have the tag RFT + 2. |
| 1815 |
|
| 1816 |
[Top Byte Ignore requirements] |
| 1817 |
Hwasan requires the ability to store an 8 bit tag in every pointer. There |
| 1818 |
is no instrumentation done to remove this tag from pointers before |
| 1819 |
dereferencing, which means the hardware must ignore this tag during memory |
| 1820 |
accesses. |
| 1821 |
|
| 1822 |
Architectures where this feature is available should indicate this using |
| 1823 |
the TARGET_MEMTAG_CAN_TAG_ADDRESSES hook. |
| 1824 |
|
| 1825 |
[Stack requires cleanup on unwinding] |
| 1826 |
During normal operation of a hwasan sanitized program more space in the |
| 1827 |
shadow memory becomes tagged as the stack grows. As the stack shrinks this |
| 1828 |
shadow memory space must become untagged. If it is not untagged then when |
| 1829 |
the stack grows again (during other function calls later on in the program) |
| 1830 |
objects on the stack that are usually not tagged (e.g. parameters passed on |
| 1831 |
the stack) can be placed in memory whose shadow space is tagged with |
| 1832 |
something else, and accesses can cause false positive reports. |
| 1833 |
|
| 1834 |
Hence we place untagging code on every epilogue of functions which tag some |
| 1835 |
stack objects. |
| 1836 |
|
| 1837 |
Moreover, the run-time library intercepts longjmp & setjmp to untag when |
| 1838 |
the stack is unwound this way. |
| 1839 |
|
| 1840 |
C++ exceptions are not yet handled, which means this sanitizer can not |
| 1841 |
handle C++ code that throws exceptions -- it will give false positives |
| 1842 |
after an exception has been thrown. The implementation that the hwasan |
| 1843 |
library has for handling these relies on the frame pointer being after any |
| 1844 |
local variables. This is not generally the case for GCC. */ |
| 1845 |
|
| 1846 |
|
| 1847 |
/* Returns whether we are tagging pointers and checking those tags on memory |
| 1848 |
access. */ |
| 1849 |
bool |
| 1850 |
hwasan_sanitize_p () |
| 1851 |
{ |
| 1852 |
return sanitize_flags_p (SANITIZE_HWADDRESS); |
| 1853 |
} |
| 1854 |
|
| 1855 |
/* Are we tagging the stack? */ |
| 1856 |
bool |
| 1857 |
hwasan_sanitize_stack_p () |
| 1858 |
{ |
| 1859 |
return (hwasan_sanitize_p () && param_hwasan_instrument_stack); |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* Are we tagging alloca objects? */ |
| 1863 |
bool |
| 1864 |
hwasan_sanitize_allocas_p (void) |
| 1865 |
{ |
| 1866 |
return (hwasan_sanitize_stack_p () && param_hwasan_instrument_allocas); |
| 1867 |
} |
| 1868 |
|
| 1869 |
/* Should we instrument reads? */ |
| 1870 |
bool |
| 1871 |
hwasan_instrument_reads (void) |
| 1872 |
{ |
| 1873 |
return (hwasan_sanitize_p () && param_hwasan_instrument_reads); |
| 1874 |
} |
| 1875 |
|
| 1876 |
/* Should we instrument writes? */ |
| 1877 |
bool |
| 1878 |
hwasan_instrument_writes (void) |
| 1879 |
{ |
| 1880 |
return (hwasan_sanitize_p () && param_hwasan_instrument_writes); |
| 1881 |
} |
| 1882 |
|
| 1883 |
/* Should we instrument builtin calls? */ |
| 1884 |
bool |
| 1885 |
hwasan_memintrin (void) |
| 1886 |
{ |
| 1887 |
return (hwasan_sanitize_p () && param_hwasan_instrument_mem_intrinsics); |
| 1888 |
} |
| 1889 |
|
| 1890 |
/* Insert code to protect stack vars. The prologue sequence should be emitted |
| 1891 |
directly, epilogue sequence returned. BASE is the register holding the |
| 1892 |
stack base, against which OFFSETS array offsets are relative to, OFFSETS |
| 1893 |
array contains pairs of offsets in reverse order, always the end offset |
| 1894 |
of some gap that needs protection followed by starting offset, |
| 1895 |
and DECLS is an array of representative decls for each var partition. |
| 1896 |
LENGTH is the length of the OFFSETS array, DECLS array is LENGTH / 2 - 1 |
| 1897 |
elements long (OFFSETS include gap before the first variable as well |
| 1898 |
as gaps after each stack variable). PBASE is, if non-NULL, some pseudo |
| 1899 |
register which stack vars DECL_RTLs are based on. Either BASE should be |
| 1900 |
assigned to PBASE, when not doing use after return protection, or |
| 1901 |
corresponding address based on __asan_stack_malloc* return value. */ |
| 1902 |
|
| 1903 |
rtx_insn * |
| 1904 |
asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb, |
| 1905 |
HOST_WIDE_INT *offsets, tree *decls, int length) |
| 1906 |
{ |
| 1907 |
rtx shadow_base, shadow_mem, ret, mem, orig_base; |
| 1908 |
rtx_code_label *lab; |
| 1909 |
rtx_insn *insns; |
| 1910 |
char buf[32]; |
| 1911 |
HOST_WIDE_INT base_offset = offsets[length - 1]; |
| 1912 |
HOST_WIDE_INT base_align_bias = 0, offset, prev_offset; |
| 1913 |
HOST_WIDE_INT asan_frame_size = offsets[0] - base_offset; |
| 1914 |
HOST_WIDE_INT last_offset, last_size, last_size_aligned; |
| 1915 |
int l; |
| 1916 |
unsigned char cur_shadow_byte = ASAN_STACK_MAGIC_LEFT; |
| 1917 |
tree str_cst, decl, id; |
| 1918 |
int use_after_return_class = -1; |
| 1919 |
|
| 1920 |
/* Don't emit anything when doing error recovery, the assertions |
| 1921 |
might fail e.g. if a function had a frame offset overflow. */ |
| 1922 |
if (seen_error ()) |
| 1923 |
return NULL; |
| 1924 |
|
| 1925 |
if (shadow_ptr_types[0] == NULL_TREE) |
| 1926 |
asan_init_shadow_ptr_types (); |
| 1927 |
|
| 1928 |
expanded_location cfun_xloc |
| 1929 |
= expand_location (DECL_SOURCE_LOCATION (current_function_decl)); |
| 1930 |
|
| 1931 |
/* First of all, prepare the description string. */ |
| 1932 |
pretty_printer asan_pp; |
| 1933 |
|
| 1934 |
pp_decimal_int (&asan_pp, length / 2 - 1); |
| 1935 |
pp_space (&asan_pp); |
| 1936 |
for (l = length - 2; l; l -= 2) |
| 1937 |
{ |
| 1938 |
tree decl = decls[l / 2 - 1]; |
| 1939 |
pp_wide_integer (&asan_pp, offsets[l] - base_offset); |
| 1940 |
pp_space (&asan_pp); |
| 1941 |
pp_wide_integer (&asan_pp, offsets[l - 1] - offsets[l]); |
| 1942 |
pp_space (&asan_pp); |
| 1943 |
|
| 1944 |
expanded_location xloc |
| 1945 |
= expand_location (DECL_SOURCE_LOCATION (decl)); |
| 1946 |
char location[32]; |
| 1947 |
|
| 1948 |
if (xloc.file == cfun_xloc.file) |
| 1949 |
sprintf (location, ":%d", xloc.line); |
| 1950 |
else |
| 1951 |
location[0] = '\0'; |
| 1952 |
|
| 1953 |
if (DECL_P (decl) && DECL_NAME (decl)) |
| 1954 |
{ |
| 1955 |
unsigned idlen |
| 1956 |
= IDENTIFIER_LENGTH (DECL_NAME (decl)) + strlen (location); |
| 1957 |
pp_decimal_int (&asan_pp, idlen); |
| 1958 |
pp_space (&asan_pp); |
| 1959 |
pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
| 1960 |
pp_string (&asan_pp, location); |
| 1961 |
} |
| 1962 |
else |
| 1963 |
pp_string (&asan_pp, "9 <unknown>"); |
| 1964 |
|
| 1965 |
if (l > 2) |
| 1966 |
pp_space (&asan_pp); |
| 1967 |
} |
| 1968 |
str_cst = asan_pp_string (&asan_pp); |
| 1969 |
|
| 1970 |
gcc_checking_assert (offsets[0] == (crtl->stack_protect_guard |
| 1971 |
? -ASAN_RED_ZONE_SIZE : 0)); |
| 1972 |
/* Emit the prologue sequence. */ |
| 1973 |
if (asan_frame_size > 32 && asan_frame_size <= 65536 && pbase |
| 1974 |
&& param_asan_use_after_return) |
| 1975 |
{ |
| 1976 |
HOST_WIDE_INT adjusted_frame_size = asan_frame_size; |
| 1977 |
/* The stack protector guard is allocated at the top of the frame |
| 1978 |
and cfgexpand.cc then uses align_frame_offset (ASAN_RED_ZONE_SIZE); |
| 1979 |
while in that case we can still use asan_frame_size, we need to take |
| 1980 |
that into account when computing base_align_bias. */ |
| 1981 |
if (alignb > ASAN_RED_ZONE_SIZE && crtl->stack_protect_guard) |
| 1982 |
adjusted_frame_size += ASAN_RED_ZONE_SIZE; |
| 1983 |
use_after_return_class = floor_log2 (asan_frame_size - 1) - 5; |
| 1984 |
/* __asan_stack_malloc_N guarantees alignment |
| 1985 |
N < 6 ? (64 << N) : 4096 bytes. */ |
| 1986 |
if (alignb > (use_after_return_class < 6 |
| 1987 |
? (64U << use_after_return_class) : 4096U)) |
| 1988 |
use_after_return_class = -1; |
| 1989 |
else if (alignb > ASAN_RED_ZONE_SIZE |
| 1990 |
&& (adjusted_frame_size & (alignb - 1))) |
| 1991 |
{ |
| 1992 |
base_align_bias |
| 1993 |
= ((adjusted_frame_size + alignb - 1) |
| 1994 |
& ~(alignb - HOST_WIDE_INT_1)) - adjusted_frame_size; |
| 1995 |
use_after_return_class |
| 1996 |
= floor_log2 (asan_frame_size + base_align_bias - 1) - 5; |
| 1997 |
if (use_after_return_class > 10) |
| 1998 |
{ |
| 1999 |
base_align_bias = 0; |
| 2000 |
use_after_return_class = -1; |
| 2001 |
} |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
/* Align base if target is STRICT_ALIGNMENT. */ |
| 2006 |
if (STRICT_ALIGNMENT) |
| 2007 |
{ |
| 2008 |
const HOST_WIDE_INT align |
| 2009 |
= (GET_MODE_ALIGNMENT (SImode) / BITS_PER_UNIT) << ASAN_SHADOW_SHIFT; |
| 2010 |
base = expand_binop (Pmode, and_optab, base, gen_int_mode (-align, Pmode), |
| 2011 |
NULL_RTX, 1, OPTAB_DIRECT); |
| 2012 |
} |
| 2013 |
|
| 2014 |
if (use_after_return_class == -1 && pbase) |
| 2015 |
emit_move_insn (pbase, base); |
| 2016 |
|
| 2017 |
base = expand_binop (Pmode, add_optab, base, |
| 2018 |
gen_int_mode (base_offset - base_align_bias, Pmode), |
| 2019 |
NULL_RTX, 1, OPTAB_DIRECT); |
| 2020 |
orig_base = NULL_RTX; |
| 2021 |
if (use_after_return_class != -1) |
| 2022 |
{ |
| 2023 |
if (asan_detect_stack_use_after_return == NULL_TREE) |
| 2024 |
{ |
| 2025 |
id = get_identifier ("__asan_option_detect_stack_use_after_return"); |
| 2026 |
decl = build_decl (BUILTINS_LOCATION, VAR_DECL, id, |
| 2027 |
integer_type_node); |
| 2028 |
SET_DECL_ASSEMBLER_NAME (decl, id); |
| 2029 |
TREE_ADDRESSABLE (decl) = 1; |
| 2030 |
DECL_ARTIFICIAL (decl) = 1; |
| 2031 |
DECL_IGNORED_P (decl) = 1; |
| 2032 |
DECL_EXTERNAL (decl) = 1; |
| 2033 |
TREE_STATIC (decl) = 1; |
| 2034 |
TREE_PUBLIC (decl) = 1; |
| 2035 |
TREE_USED (decl) = 1; |
| 2036 |
asan_detect_stack_use_after_return = decl; |
| 2037 |
} |
| 2038 |
orig_base = gen_reg_rtx (Pmode); |
| 2039 |
emit_move_insn (orig_base, base); |
| 2040 |
ret = expand_normal (asan_detect_stack_use_after_return); |
| 2041 |
lab = gen_label_rtx (); |
| 2042 |
emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX, |
| 2043 |
VOIDmode, 0, lab, |
| 2044 |
profile_probability::very_likely ()); |
| 2045 |
snprintf (buf, sizeof buf, "__asan_stack_malloc_%d", |
| 2046 |
use_after_return_class); |
| 2047 |
ret = init_one_libfunc (buf); |
| 2048 |
ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode, |
| 2049 |
GEN_INT (asan_frame_size |
| 2050 |
+ base_align_bias), |
| 2051 |
TYPE_MODE (pointer_sized_int_node)); |
| 2052 |
/* __asan_stack_malloc_[n] returns a pointer to fake stack if succeeded |
| 2053 |
and NULL otherwise. Check RET value is NULL here and jump over the |
| 2054 |
BASE reassignment in this case. Otherwise, reassign BASE to RET. */ |
| 2055 |
emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX, |
| 2056 |
VOIDmode, 0, lab, |
| 2057 |
profile_probability:: very_unlikely ()); |
| 2058 |
ret = convert_memory_address (Pmode, ret); |
| 2059 |
emit_move_insn (base, ret); |
| 2060 |
emit_label (lab); |
| 2061 |
emit_move_insn (pbase, expand_binop (Pmode, add_optab, base, |
| 2062 |
gen_int_mode (base_align_bias |
| 2063 |
- base_offset, Pmode), |
| 2064 |
NULL_RTX, 1, OPTAB_DIRECT)); |
| 2065 |
} |
| 2066 |
mem = gen_rtx_MEM (ptr_mode, base); |
| 2067 |
mem = adjust_address (mem, VOIDmode, base_align_bias); |
| 2068 |
emit_move_insn (mem, gen_int_mode (ASAN_STACK_FRAME_MAGIC, ptr_mode)); |
| 2069 |
mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode)); |
| 2070 |
emit_move_insn (mem, expand_normal (str_cst)); |
| 2071 |
mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode)); |
| 2072 |
ASM_GENERATE_INTERNAL_LABEL (buf, "LASANPC", current_function_funcdef_no); |
| 2073 |
id = get_identifier (buf); |
| 2074 |
decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl), |
| 2075 |
VAR_DECL, id, char_type_node); |
| 2076 |
SET_DECL_ASSEMBLER_NAME (decl, id); |
| 2077 |
TREE_ADDRESSABLE (decl) = 1; |
| 2078 |
TREE_READONLY (decl) = 1; |
| 2079 |
DECL_ARTIFICIAL (decl) = 1; |
| 2080 |
DECL_IGNORED_P (decl) = 1; |
| 2081 |
TREE_STATIC (decl) = 1; |
| 2082 |
TREE_PUBLIC (decl) = 0; |
| 2083 |
TREE_USED (decl) = 1; |
| 2084 |
DECL_INITIAL (decl) = decl; |
| 2085 |
TREE_ASM_WRITTEN (decl) = 1; |
| 2086 |
TREE_ASM_WRITTEN (id) = 1; |
| 2087 |
DECL_ALIGN_RAW (decl) = DECL_ALIGN_RAW (current_function_decl); |
| 2088 |
emit_move_insn (mem, expand_normal (build_fold_addr_expr (decl))); |
| 2089 |
shadow_base = expand_binop (Pmode, lshr_optab, base, |
| 2090 |
gen_int_shift_amount (Pmode, ASAN_SHADOW_SHIFT), |
| 2091 |
NULL_RTX, 1, OPTAB_DIRECT); |
| 2092 |
if (asan_dynamic_shadow_offset_p ()) |
| 2093 |
{ |
| 2094 |
ret = expand_normal (get_asan_shadow_memory_dynamic_address_decl ()); |
| 2095 |
shadow_base |
| 2096 |
= expand_simple_binop (Pmode, PLUS, shadow_base, ret, NULL_RTX, |
| 2097 |
/* unsignedp = */ 1, OPTAB_WIDEN); |
| 2098 |
shadow_base = plus_constant (Pmode, shadow_base, |
| 2099 |
(base_align_bias >> ASAN_SHADOW_SHIFT)); |
| 2100 |
} |
| 2101 |
else |
| 2102 |
{ |
| 2103 |
shadow_base = plus_constant (Pmode, shadow_base, |
| 2104 |
asan_shadow_offset () |
| 2105 |
+ (base_align_bias >> ASAN_SHADOW_SHIFT)); |
| 2106 |
} |
| 2107 |
gcc_assert (asan_shadow_set != -1 |
| 2108 |
&& (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4); |
| 2109 |
shadow_mem = gen_rtx_MEM (SImode, shadow_base); |
| 2110 |
set_mem_alias_set (shadow_mem, asan_shadow_set); |
| 2111 |
if (STRICT_ALIGNMENT) |
| 2112 |
set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode))); |
| 2113 |
prev_offset = base_offset; |
| 2114 |
|
| 2115 |
asan_redzone_buffer rz_buffer (shadow_mem, prev_offset); |
| 2116 |
for (l = length; l; l -= 2) |
| 2117 |
{ |
| 2118 |
if (l == 2) |
| 2119 |
cur_shadow_byte = ASAN_STACK_MAGIC_RIGHT; |
| 2120 |
offset = offsets[l - 1]; |
| 2121 |
|
| 2122 |
bool extra_byte = (offset - base_offset) & (ASAN_SHADOW_GRANULARITY - 1); |
| 2123 |
/* If a red-zone is not aligned to ASAN_SHADOW_GRANULARITY then |
| 2124 |
the previous stack variable has size % ASAN_SHADOW_GRANULARITY != 0. |
| 2125 |
In that case we have to emit one extra byte that will describe |
| 2126 |
how many bytes (our of ASAN_SHADOW_GRANULARITY) can be accessed. */ |
| 2127 |
if (extra_byte) |
| 2128 |
{ |
| 2129 |
HOST_WIDE_INT aoff |
| 2130 |
= base_offset + ((offset - base_offset) |
| 2131 |
& ~(ASAN_SHADOW_GRANULARITY - HOST_WIDE_INT_1)); |
| 2132 |
rz_buffer.emit_redzone_byte (aoff, offset - aoff); |
| 2133 |
offset = aoff + ASAN_SHADOW_GRANULARITY; |
| 2134 |
} |
| 2135 |
|
| 2136 |
/* Calculate size of red zone payload. */ |
| 2137 |
while (offset < offsets[l - 2]) |
| 2138 |
{ |
| 2139 |
rz_buffer.emit_redzone_byte (offset, cur_shadow_byte); |
| 2140 |
offset += ASAN_SHADOW_GRANULARITY; |
| 2141 |
} |
| 2142 |
|
| 2143 |
cur_shadow_byte = ASAN_STACK_MAGIC_MIDDLE; |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* As the automatic variables are aligned to |
| 2147 |
ASAN_RED_ZONE_SIZE / ASAN_SHADOW_GRANULARITY, the buffer should be |
| 2148 |
flushed here. */ |
| 2149 |
gcc_assert (rz_buffer.m_shadow_bytes.is_empty ()); |
| 2150 |
|
| 2151 |
do_pending_stack_adjust (); |
| 2152 |
|
| 2153 |
/* Construct epilogue sequence. */ |
| 2154 |
start_sequence (); |
| 2155 |
|
| 2156 |
lab = NULL; |
| 2157 |
if (use_after_return_class != -1) |
| 2158 |
{ |
| 2159 |
rtx_code_label *lab2 = gen_label_rtx (); |
| 2160 |
char c = (char) ASAN_STACK_MAGIC_USE_AFTER_RET; |
| 2161 |
emit_cmp_and_jump_insns (orig_base, base, EQ, NULL_RTX, |
| 2162 |
VOIDmode, 0, lab2, |
| 2163 |
profile_probability::very_likely ()); |
| 2164 |
shadow_mem = gen_rtx_MEM (BLKmode, shadow_base); |
| 2165 |
set_mem_alias_set (shadow_mem, asan_shadow_set); |
| 2166 |
mem = gen_rtx_MEM (ptr_mode, base); |
| 2167 |
mem = adjust_address (mem, VOIDmode, base_align_bias); |
| 2168 |
emit_move_insn (mem, gen_int_mode (ASAN_STACK_RETIRED_MAGIC, ptr_mode)); |
| 2169 |
unsigned HOST_WIDE_INT sz = asan_frame_size >> ASAN_SHADOW_SHIFT; |
| 2170 |
bool asan_stack_free_emitted_p = false; |
| 2171 |
if (use_after_return_class < 5 |
| 2172 |
&& can_store_by_pieces (sz, builtin_memset_read_str, &c, |
| 2173 |
BITS_PER_UNIT, true)) |
| 2174 |
/* Emit memset (ShadowBase, kAsanStackAfterReturnMagic, ShadowSize). */ |
| 2175 |
store_by_pieces (shadow_mem, sz, builtin_memset_read_str, &c, |
| 2176 |
BITS_PER_UNIT, true, RETURN_BEGIN); |
| 2177 |
else if (use_after_return_class >= 5 |
| 2178 |
|| !set_storage_via_setmem (shadow_mem, |
| 2179 |
GEN_INT (sz), |
| 2180 |
gen_int_mode (c, QImode), |
| 2181 |
BITS_PER_UNIT, BITS_PER_UNIT, |
| 2182 |
-1, sz, sz, sz)) |
| 2183 |
{ |
| 2184 |
snprintf (buf, sizeof buf, "__asan_stack_free_%d", |
| 2185 |
use_after_return_class); |
| 2186 |
ret = init_one_libfunc (buf); |
| 2187 |
rtx addr = convert_memory_address (ptr_mode, base); |
| 2188 |
rtx orig_addr = convert_memory_address (ptr_mode, orig_base); |
| 2189 |
emit_library_call (ret, LCT_NORMAL, ptr_mode, addr, ptr_mode, |
| 2190 |
GEN_INT (asan_frame_size + base_align_bias), |
| 2191 |
TYPE_MODE (pointer_sized_int_node), |
| 2192 |
orig_addr, ptr_mode); |
| 2193 |
asan_stack_free_emitted_p = true; |
| 2194 |
} |
| 2195 |
if (!asan_stack_free_emitted_p) |
| 2196 |
{ |
| 2197 |
/* Emit **SavedFlagPtr (FakeStack, class_id) = 0. */ |
| 2198 |
unsigned HOST_WIDE_INT offset = (1 << (use_after_return_class + 6)); |
| 2199 |
offset -= GET_MODE_SIZE (ptr_mode); |
| 2200 |
mem = gen_rtx_MEM (ptr_mode, base); |
| 2201 |
mem = adjust_address (mem, ptr_mode, offset); |
| 2202 |
rtx addr = gen_reg_rtx (ptr_mode); |
| 2203 |
emit_move_insn (addr, mem); |
| 2204 |
addr = convert_memory_address (Pmode, addr); |
| 2205 |
mem = gen_rtx_MEM (QImode, addr); |
| 2206 |
emit_move_insn (mem, const0_rtx); |
| 2207 |
} |
| 2208 |
lab = gen_label_rtx (); |
| 2209 |
emit_jump (lab); |
| 2210 |
emit_label (lab2); |
| 2211 |
} |
| 2212 |
|
| 2213 |
shadow_mem = gen_rtx_MEM (BLKmode, shadow_base); |
| 2214 |
set_mem_alias_set (shadow_mem, asan_shadow_set); |
| 2215 |
|
| 2216 |
if (STRICT_ALIGNMENT) |
| 2217 |
set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode))); |
| 2218 |
|
| 2219 |
prev_offset = base_offset; |
| 2220 |
last_offset = base_offset; |
| 2221 |
last_size = 0; |
| 2222 |
last_size_aligned = 0; |
| 2223 |
for (l = length; l; l -= 2) |
| 2224 |
{ |
| 2225 |
offset = base_offset + ((offsets[l - 1] - base_offset) |
| 2226 |
& ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); |
| 2227 |
if (last_offset + last_size_aligned < offset) |
| 2228 |
{ |
| 2229 |
shadow_mem = adjust_address (shadow_mem, VOIDmode, |
| 2230 |
(last_offset - prev_offset) |
| 2231 |
>> ASAN_SHADOW_SHIFT); |
| 2232 |
prev_offset = last_offset; |
| 2233 |
asan_clear_shadow (shadow_mem, last_size_aligned >> ASAN_SHADOW_SHIFT); |
| 2234 |
last_offset = offset; |
| 2235 |
last_size = 0; |
| 2236 |
} |
| 2237 |
else |
| 2238 |
last_size = offset - last_offset; |
| 2239 |
last_size += base_offset + ((offsets[l - 2] - base_offset) |
| 2240 |
& ~(ASAN_MIN_RED_ZONE_SIZE - HOST_WIDE_INT_1)) |
| 2241 |
- offset; |
| 2242 |
|
| 2243 |
/* Unpoison shadow memory that corresponds to a variable that is |
| 2244 |
is subject of use-after-return sanitization. */ |
| 2245 |
if (l > 2) |
| 2246 |
{ |
| 2247 |
decl = decls[l / 2 - 2]; |
| 2248 |
if (asan_handled_variables != NULL |
| 2249 |
&& asan_handled_variables->contains (decl)) |
| 2250 |
{ |
| 2251 |
HOST_WIDE_INT size = offsets[l - 3] - offsets[l - 2]; |
| 2252 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2253 |
{ |
| 2254 |
const char *n = (DECL_NAME (decl) |
| 2255 |
? IDENTIFIER_POINTER (DECL_NAME (decl)) |
| 2256 |
: "<unknown>"); |
| 2257 |
fprintf (dump_file, "Unpoisoning shadow stack for variable: " |
| 2258 |
"%s (%" PRId64 " B)\n", n, size); |
| 2259 |
} |
| 2260 |
|
| 2261 |
last_size += size & ~(ASAN_MIN_RED_ZONE_SIZE - HOST_WIDE_INT_1); |
| 2262 |
} |
| 2263 |
} |
| 2264 |
last_size_aligned |
| 2265 |
= ((last_size + (ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)) |
| 2266 |
& ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); |
| 2267 |
} |
| 2268 |
if (last_size_aligned) |
| 2269 |
{ |
| 2270 |
shadow_mem = adjust_address (shadow_mem, VOIDmode, |
| 2271 |
(last_offset - prev_offset) |
| 2272 |
>> ASAN_SHADOW_SHIFT); |
| 2273 |
asan_clear_shadow (shadow_mem, last_size_aligned >> ASAN_SHADOW_SHIFT); |
| 2274 |
} |
| 2275 |
|
| 2276 |
/* Clean-up set with instrumented stack variables. */ |
| 2277 |
delete asan_handled_variables; |
| 2278 |
asan_handled_variables = NULL; |
| 2279 |
delete asan_used_labels; |
| 2280 |
asan_used_labels = NULL; |
| 2281 |
|
| 2282 |
do_pending_stack_adjust (); |
| 2283 |
if (lab) |
| 2284 |
emit_label (lab); |
| 2285 |
|
| 2286 |
insns = get_insns (); |
| 2287 |
end_sequence (); |
| 2288 |
return insns; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* Emit __asan_allocas_unpoison (top, bot) call. The BASE parameter corresponds |
| 2292 |
to BOT argument, for TOP virtual_stack_dynamic_rtx is used. NEW_SEQUENCE |
| 2293 |
indicates whether we're emitting new instructions sequence or not. */ |
| 2294 |
|
| 2295 |
rtx_insn * |
| 2296 |
asan_emit_allocas_unpoison (rtx top, rtx bot, rtx_insn *before) |
| 2297 |
{ |
| 2298 |
if (before) |
| 2299 |
push_to_sequence (before); |
| 2300 |
else |
| 2301 |
start_sequence (); |
| 2302 |
rtx ret = init_one_libfunc ("__asan_allocas_unpoison"); |
| 2303 |
top = convert_memory_address (ptr_mode, top); |
| 2304 |
bot = convert_memory_address (ptr_mode, bot); |
| 2305 |
emit_library_call (ret, LCT_NORMAL, ptr_mode, |
| 2306 |
top, ptr_mode, bot, ptr_mode); |
| 2307 |
|
| 2308 |
do_pending_stack_adjust (); |
| 2309 |
rtx_insn *insns = get_insns (); |
| 2310 |
end_sequence (); |
| 2311 |
return insns; |
| 2312 |
} |
| 2313 |
|
| 2314 |
/* Return true if DECL, a global var, might be overridden and needs |
| 2315 |
therefore a local alias. */ |
| 2316 |
|
| 2317 |
static bool |
| 2318 |
asan_needs_local_alias (tree decl) |
| 2319 |
{ |
| 2320 |
return DECL_WEAK (decl) || !targetm.binds_local_p (decl); |
| 2321 |
} |
| 2322 |
|
| 2323 |
/* Return true if DECL, a global var, is an artificial ODR indicator symbol |
| 2324 |
therefore doesn't need protection. */ |
| 2325 |
|
| 2326 |
static bool |
| 2327 |
is_odr_indicator (tree decl) |
| 2328 |
{ |
| 2329 |
return (DECL_ARTIFICIAL (decl) |
| 2330 |
&& lookup_attribute ("asan odr indicator", DECL_ATTRIBUTES (decl))); |
| 2331 |
} |
| 2332 |
|
| 2333 |
/* Return true if DECL is a VAR_DECL that should be protected |
| 2334 |
by Address Sanitizer, by appending a red zone with protected |
| 2335 |
shadow memory after it and aligning it to at least |
| 2336 |
ASAN_RED_ZONE_SIZE bytes. */ |
| 2337 |
|
| 2338 |
bool |
| 2339 |
asan_protect_global (tree decl, bool ignore_decl_rtl_set_p) |
| 2340 |
{ |
| 2341 |
if (!param_asan_globals) |
| 2342 |
return false; |
| 2343 |
|
| 2344 |
rtx rtl, symbol; |
| 2345 |
|
| 2346 |
if (TREE_CODE (decl) == STRING_CST) |
| 2347 |
{ |
| 2348 |
/* Instrument all STRING_CSTs except those created |
| 2349 |
by asan_pp_string here. */ |
| 2350 |
if (shadow_ptr_types[0] != NULL_TREE |
| 2351 |
&& TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE |
| 2352 |
&& TREE_TYPE (TREE_TYPE (decl)) == TREE_TYPE (shadow_ptr_types[0])) |
| 2353 |
return false; |
| 2354 |
return true; |
| 2355 |
} |
| 2356 |
if (!VAR_P (decl) |
| 2357 |
/* TLS vars aren't statically protectable. */ |
| 2358 |
|| DECL_THREAD_LOCAL_P (decl) |
| 2359 |
/* Externs will be protected elsewhere. */ |
| 2360 |
|| DECL_EXTERNAL (decl) |
| 2361 |
/* PR sanitizer/81697: For architectures that use section anchors first |
| 2362 |
call to asan_protect_global may occur before DECL_RTL (decl) is set. |
| 2363 |
We should ignore DECL_RTL_SET_P then, because otherwise the first call |
| 2364 |
to asan_protect_global will return FALSE and the following calls on the |
| 2365 |
same decl after setting DECL_RTL (decl) will return TRUE and we'll end |
| 2366 |
up with inconsistency at runtime. */ |
| 2367 |
|| (!DECL_RTL_SET_P (decl) && !ignore_decl_rtl_set_p) |
| 2368 |
/* Comdat vars pose an ABI problem, we can't know if |
| 2369 |
the var that is selected by the linker will have |
| 2370 |
padding or not. */ |
| 2371 |
|| DECL_ONE_ONLY (decl) |
| 2372 |
/* Similarly for common vars. People can use -fno-common. |
| 2373 |
Note: Linux kernel is built with -fno-common, so we do instrument |
| 2374 |
globals there even if it is C. */ |
| 2375 |
|| (DECL_COMMON (decl) && TREE_PUBLIC (decl)) |
| 2376 |
/* Don't protect if using user section, often vars placed |
| 2377 |
into user section from multiple TUs are then assumed |
| 2378 |
to be an array of such vars, putting padding in there |
| 2379 |
breaks this assumption. */ |
| 2380 |
|| (DECL_SECTION_NAME (decl) != NULL |
| 2381 |
&& !symtab_node::get (decl)->implicit_section |
| 2382 |
&& !section_sanitized_p (DECL_SECTION_NAME (decl))) |
| 2383 |
/* Don't protect variables in non-generic address-space. */ |
| 2384 |
|| !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (decl))) |
| 2385 |
|| DECL_SIZE (decl) == 0 |
| 2386 |
|| ASAN_RED_ZONE_SIZE * BITS_PER_UNIT > MAX_OFILE_ALIGNMENT |
| 2387 |
|| TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST |
| 2388 |
|| !valid_constant_size_p (DECL_SIZE_UNIT (decl)) |
| 2389 |
|| DECL_ALIGN_UNIT (decl) > 2 * ASAN_RED_ZONE_SIZE |
| 2390 |
|| TREE_TYPE (decl) == ubsan_get_source_location_type () |
| 2391 |
|| is_odr_indicator (decl)) |
| 2392 |
return false; |
| 2393 |
|
| 2394 |
if (!ignore_decl_rtl_set_p || DECL_RTL_SET_P (decl)) |
| 2395 |
{ |
| 2396 |
|
| 2397 |
rtl = DECL_RTL (decl); |
| 2398 |
if (!MEM_P (rtl) || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF) |
| 2399 |
return false; |
| 2400 |
symbol = XEXP (rtl, 0); |
| 2401 |
|
| 2402 |
if (CONSTANT_POOL_ADDRESS_P (symbol) |
| 2403 |
|| TREE_CONSTANT_POOL_ADDRESS_P (symbol)) |
| 2404 |
return false; |
| 2405 |
} |
| 2406 |
|
| 2407 |
if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
| 2408 |
return false; |
| 2409 |
|
| 2410 |
if (!TARGET_SUPPORTS_ALIASES && asan_needs_local_alias (decl)) |
| 2411 |
return false; |
| 2412 |
|
| 2413 |
return true; |
| 2414 |
} |
| 2415 |
|
| 2416 |
/* Construct a function tree for __asan_report_{load,store}{1,2,4,8,16,_n}. |
| 2417 |
IS_STORE is either 1 (for a store) or 0 (for a load). */ |
| 2418 |
|
| 2419 |
static tree |
| 2420 |
report_error_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes, |
| 2421 |
int *nargs) |
| 2422 |
{ |
| 2423 |
gcc_assert (!hwasan_sanitize_p ()); |
| 2424 |
|
| 2425 |
static enum built_in_function report[2][2][6] |
| 2426 |
= { { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2, |
| 2427 |
BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8, |
| 2428 |
BUILT_IN_ASAN_REPORT_LOAD16, BUILT_IN_ASAN_REPORT_LOAD_N }, |
| 2429 |
{ BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2, |
| 2430 |
BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8, |
| 2431 |
BUILT_IN_ASAN_REPORT_STORE16, BUILT_IN_ASAN_REPORT_STORE_N } }, |
| 2432 |
{ { BUILT_IN_ASAN_REPORT_LOAD1_NOABORT, |
| 2433 |
BUILT_IN_ASAN_REPORT_LOAD2_NOABORT, |
| 2434 |
BUILT_IN_ASAN_REPORT_LOAD4_NOABORT, |
| 2435 |
BUILT_IN_ASAN_REPORT_LOAD8_NOABORT, |
| 2436 |
BUILT_IN_ASAN_REPORT_LOAD16_NOABORT, |
| 2437 |
BUILT_IN_ASAN_REPORT_LOAD_N_NOABORT }, |
| 2438 |
{ BUILT_IN_ASAN_REPORT_STORE1_NOABORT, |
| 2439 |
BUILT_IN_ASAN_REPORT_STORE2_NOABORT, |
| 2440 |
BUILT_IN_ASAN_REPORT_STORE4_NOABORT, |
| 2441 |
BUILT_IN_ASAN_REPORT_STORE8_NOABORT, |
| 2442 |
BUILT_IN_ASAN_REPORT_STORE16_NOABORT, |
| 2443 |
BUILT_IN_ASAN_REPORT_STORE_N_NOABORT } } }; |
| 2444 |
if (size_in_bytes == -1) |
| 2445 |
{ |
| 2446 |
*nargs = 2; |
| 2447 |
return builtin_decl_implicit (report[recover_p][is_store][5]); |
| 2448 |
} |
| 2449 |
*nargs = 1; |
| 2450 |
int size_log2 = exact_log2 (size_in_bytes); |
| 2451 |
return builtin_decl_implicit (report[recover_p][is_store][size_log2]); |
| 2452 |
} |
| 2453 |
|
| 2454 |
/* Construct a function tree for __asan_{load,store}{1,2,4,8,16,_n}. |
| 2455 |
IS_STORE is either 1 (for a store) or 0 (for a load). */ |
| 2456 |
|
| 2457 |
static tree |
| 2458 |
check_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes, |
| 2459 |
int *nargs) |
| 2460 |
{ |
| 2461 |
static enum built_in_function check[2][2][6] |
| 2462 |
= { { { BUILT_IN_ASAN_LOAD1, BUILT_IN_ASAN_LOAD2, |
| 2463 |
BUILT_IN_ASAN_LOAD4, BUILT_IN_ASAN_LOAD8, |
| 2464 |
BUILT_IN_ASAN_LOAD16, BUILT_IN_ASAN_LOADN }, |
| 2465 |
{ BUILT_IN_ASAN_STORE1, BUILT_IN_ASAN_STORE2, |
| 2466 |
BUILT_IN_ASAN_STORE4, BUILT_IN_ASAN_STORE8, |
| 2467 |
BUILT_IN_ASAN_STORE16, BUILT_IN_ASAN_STOREN } }, |
| 2468 |
{ { BUILT_IN_ASAN_LOAD1_NOABORT, |
| 2469 |
BUILT_IN_ASAN_LOAD2_NOABORT, |
| 2470 |
BUILT_IN_ASAN_LOAD4_NOABORT, |
| 2471 |
BUILT_IN_ASAN_LOAD8_NOABORT, |
| 2472 |
BUILT_IN_ASAN_LOAD16_NOABORT, |
| 2473 |
BUILT_IN_ASAN_LOADN_NOABORT }, |
| 2474 |
{ BUILT_IN_ASAN_STORE1_NOABORT, |
| 2475 |
BUILT_IN_ASAN_STORE2_NOABORT, |
| 2476 |
BUILT_IN_ASAN_STORE4_NOABORT, |
| 2477 |
BUILT_IN_ASAN_STORE8_NOABORT, |
| 2478 |
BUILT_IN_ASAN_STORE16_NOABORT, |
| 2479 |
BUILT_IN_ASAN_STOREN_NOABORT } } }; |
| 2480 |
if (size_in_bytes == -1) |
| 2481 |
{ |
| 2482 |
*nargs = 2; |
| 2483 |
return builtin_decl_implicit (check[recover_p][is_store][5]); |
| 2484 |
} |
| 2485 |
*nargs = 1; |
| 2486 |
int size_log2 = exact_log2 (size_in_bytes); |
| 2487 |
return builtin_decl_implicit (check[recover_p][is_store][size_log2]); |
| 2488 |
} |
| 2489 |
|
| 2490 |
/* Split the current basic block and create a condition statement |
| 2491 |
insertion point right before or after the statement pointed to by |
| 2492 |
ITER. Return an iterator to the point at which the caller might |
| 2493 |
safely insert the condition statement. |
| 2494 |
|
| 2495 |
THEN_BLOCK must be set to the address of an uninitialized instance |
| 2496 |
of basic_block. The function will then set *THEN_BLOCK to the |
| 2497 |
'then block' of the condition statement to be inserted by the |
| 2498 |
caller. |
| 2499 |
|
| 2500 |
If CREATE_THEN_FALLTHRU_EDGE is false, no edge will be created from |
| 2501 |
*THEN_BLOCK to *FALLTHROUGH_BLOCK. |
| 2502 |
|
| 2503 |
Similarly, the function will set *FALLTRHOUGH_BLOCK to the 'else |
| 2504 |
block' of the condition statement to be inserted by the caller. |
| 2505 |
|
| 2506 |
Note that *FALLTHROUGH_BLOCK is a new block that contains the |
| 2507 |
statements starting from *ITER, and *THEN_BLOCK is a new empty |
| 2508 |
block. |
| 2509 |
|
| 2510 |
*ITER is adjusted to point to always point to the first statement |
| 2511 |
of the basic block * FALLTHROUGH_BLOCK. That statement is the |
| 2512 |
same as what ITER was pointing to prior to calling this function, |
| 2513 |
if BEFORE_P is true; otherwise, it is its following statement. */ |
| 2514 |
|
| 2515 |
gimple_stmt_iterator |
| 2516 |
create_cond_insert_point (gimple_stmt_iterator *iter, |
| 2517 |
bool before_p, |
| 2518 |
bool then_more_likely_p, |
| 2519 |
bool create_then_fallthru_edge, |
| 2520 |
basic_block *then_block, |
| 2521 |
basic_block *fallthrough_block) |
| 2522 |
{ |
| 2523 |
gimple_stmt_iterator gsi = *iter; |
| 2524 |
|
| 2525 |
if (!gsi_end_p (gsi) && before_p) |
| 2526 |
gsi_prev (&gsi); |
| 2527 |
|
| 2528 |
basic_block cur_bb = gsi_bb (*iter); |
| 2529 |
|
| 2530 |
edge e = split_block (cur_bb, gsi_stmt (gsi)); |
| 2531 |
|
| 2532 |
/* Get a hold on the 'condition block', the 'then block' and the |
| 2533 |
'else block'. */ |
| 2534 |
basic_block cond_bb = e->src; |
| 2535 |
basic_block fallthru_bb = e->dest; |
| 2536 |
basic_block then_bb = create_empty_bb (cond_bb); |
| 2537 |
if (current_loops) |
| 2538 |
{ |
| 2539 |
add_bb_to_loop (then_bb, cond_bb->loop_father); |
| 2540 |
loops_state_set (LOOPS_NEED_FIXUP); |
| 2541 |
} |
| 2542 |
|
| 2543 |
/* Set up the newly created 'then block'. */ |
| 2544 |
e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE); |
| 2545 |
profile_probability fallthrough_probability |
| 2546 |
= then_more_likely_p |
| 2547 |
? profile_probability::very_unlikely () |
| 2548 |
: profile_probability::very_likely (); |
| 2549 |
e->probability = fallthrough_probability.invert (); |
| 2550 |
then_bb->count = e->count (); |
| 2551 |
if (create_then_fallthru_edge) |
| 2552 |
make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); |
| 2553 |
|
| 2554 |
/* Set up the fallthrough basic block. */ |
| 2555 |
e = find_edge (cond_bb, fallthru_bb); |
| 2556 |
e->flags = EDGE_FALSE_VALUE; |
| 2557 |
e->probability = fallthrough_probability; |
| 2558 |
|
| 2559 |
/* Update dominance info for the newly created then_bb; note that |
| 2560 |
fallthru_bb's dominance info has already been updated by |
| 2561 |
split_bock. */ |
| 2562 |
if (dom_info_available_p (CDI_DOMINATORS)) |
| 2563 |
set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); |
| 2564 |
|
| 2565 |
*then_block = then_bb; |
| 2566 |
*fallthrough_block = fallthru_bb; |
| 2567 |
*iter = gsi_start_bb (fallthru_bb); |
| 2568 |
|
| 2569 |
return gsi_last_bb (cond_bb); |
| 2570 |
} |
| 2571 |
|
| 2572 |
/* Insert an if condition followed by a 'then block' right before the |
| 2573 |
statement pointed to by ITER. The fallthrough block -- which is the |
| 2574 |
else block of the condition as well as the destination of the |
| 2575 |
outcoming edge of the 'then block' -- starts with the statement |
| 2576 |
pointed to by ITER. |
| 2577 |
|
| 2578 |
COND is the condition of the if. |
| 2579 |
|
| 2580 |
If THEN_MORE_LIKELY_P is true, the probability of the edge to the |
| 2581 |
'then block' is higher than the probability of the edge to the |
| 2582 |
fallthrough block. |
| 2583 |
|
| 2584 |
Upon completion of the function, *THEN_BB is set to the newly |
| 2585 |
inserted 'then block' and similarly, *FALLTHROUGH_BB is set to the |
| 2586 |
fallthrough block. |
| 2587 |
|
| 2588 |
*ITER is adjusted to still point to the same statement it was |
| 2589 |
pointing to initially. */ |
| 2590 |
|
| 2591 |
static void |
| 2592 |
insert_if_then_before_iter (gcond *cond, |
| 2593 |
gimple_stmt_iterator *iter, |
| 2594 |
bool then_more_likely_p, |
| 2595 |
basic_block *then_bb, |
| 2596 |
basic_block *fallthrough_bb) |
| 2597 |
{ |
| 2598 |
gimple_stmt_iterator cond_insert_point = |
| 2599 |
create_cond_insert_point (iter, |
| 2600 |
/*before_p=*/true, |
| 2601 |
then_more_likely_p, |
| 2602 |
/*create_then_fallthru_edge=*/true, |
| 2603 |
then_bb, |
| 2604 |
fallthrough_bb); |
| 2605 |
gsi_insert_after (&cond_insert_point, cond, GSI_NEW_STMT); |
| 2606 |
} |
| 2607 |
|
| 2608 |
/* Build (base_addr >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). |
| 2609 |
If RETURN_ADDRESS is set to true, return memory location instread |
| 2610 |
of a value in the shadow memory. */ |
| 2611 |
|
| 2612 |
static tree |
| 2613 |
build_shadow_mem_access (gimple_stmt_iterator *gsi, location_t location, |
| 2614 |
tree base_addr, tree shadow_ptr_type, |
| 2615 |
bool return_address = false) |
| 2616 |
{ |
| 2617 |
tree t, uintptr_type = TREE_TYPE (base_addr); |
| 2618 |
tree shadow_type = TREE_TYPE (shadow_ptr_type); |
| 2619 |
gimple *g; |
| 2620 |
|
| 2621 |
t = build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT); |
| 2622 |
g = gimple_build_assign (make_ssa_name (uintptr_type), RSHIFT_EXPR, |
| 2623 |
base_addr, t); |
| 2624 |
gimple_set_location (g, location); |
| 2625 |
gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 2626 |
|
| 2627 |
if (asan_dynamic_shadow_offset_p ()) |
| 2628 |
t = asan_local_shadow_memory_dynamic_address; |
| 2629 |
else |
| 2630 |
t = build_int_cst (uintptr_type, asan_shadow_offset ()); |
| 2631 |
g = gimple_build_assign (make_ssa_name (uintptr_type), PLUS_EXPR, |
| 2632 |
gimple_assign_lhs (g), t); |
| 2633 |
gimple_set_location (g, location); |
| 2634 |
gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 2635 |
|
| 2636 |
g = gimple_build_assign (make_ssa_name (shadow_ptr_type), NOP_EXPR, |
| 2637 |
gimple_assign_lhs (g)); |
| 2638 |
gimple_set_location (g, location); |
| 2639 |
gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 2640 |
|
| 2641 |
if (!return_address) |
| 2642 |
{ |
| 2643 |
t = build2 (MEM_REF, shadow_type, gimple_assign_lhs (g), |
| 2644 |
build_int_cst (shadow_ptr_type, 0)); |
| 2645 |
g = gimple_build_assign (make_ssa_name (shadow_type), MEM_REF, t); |
| 2646 |
gimple_set_location (g, location); |
| 2647 |
gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 2648 |
} |
| 2649 |
|
| 2650 |
return gimple_assign_lhs (g); |
| 2651 |
} |
| 2652 |
|
| 2653 |
/* BASE can already be an SSA_NAME; in that case, do not create a |
| 2654 |
new SSA_NAME for it. */ |
| 2655 |
|
| 2656 |
static tree |
| 2657 |
maybe_create_ssa_name (location_t loc, tree base, gimple_stmt_iterator *iter, |
| 2658 |
bool before_p) |
| 2659 |
{ |
| 2660 |
STRIP_USELESS_TYPE_CONVERSION (base); |
| 2661 |
if (TREE_CODE (base) == SSA_NAME) |
| 2662 |
return base; |
| 2663 |
gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (base)), base); |
| 2664 |
gimple_set_location (g, loc); |
| 2665 |
if (before_p) |
| 2666 |
gsi_safe_insert_before (iter, g); |
| 2667 |
else |
| 2668 |
gsi_insert_after (iter, g, GSI_NEW_STMT); |
| 2669 |
return gimple_assign_lhs (g); |
| 2670 |
} |
| 2671 |
|
| 2672 |
/* LEN can already have necessary size and precision; |
| 2673 |
in that case, do not create a new variable. */ |
| 2674 |
|
| 2675 |
tree |
| 2676 |
maybe_cast_to_ptrmode (location_t loc, tree len, gimple_stmt_iterator *iter, |
| 2677 |
bool before_p) |
| 2678 |
{ |
| 2679 |
if (ptrofftype_p (len)) |
| 2680 |
return len; |
| 2681 |
gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 2682 |
NOP_EXPR, len); |
| 2683 |
gimple_set_location (g, loc); |
| 2684 |
if (before_p) |
| 2685 |
gsi_safe_insert_before (iter, g); |
| 2686 |
else |
| 2687 |
gsi_insert_after (iter, g, GSI_NEW_STMT); |
| 2688 |
return gimple_assign_lhs (g); |
| 2689 |
} |
| 2690 |
|
| 2691 |
/* Instrument the memory access instruction BASE. Insert new |
| 2692 |
statements before or after ITER. |
| 2693 |
|
| 2694 |
Note that the memory access represented by BASE can be either an |
| 2695 |
SSA_NAME, or a non-SSA expression. LOCATION is the source code |
| 2696 |
location. IS_STORE is TRUE for a store, FALSE for a load. |
| 2697 |
BEFORE_P is TRUE for inserting the instrumentation code before |
| 2698 |
ITER, FALSE for inserting it after ITER. IS_SCALAR_ACCESS is TRUE |
| 2699 |
for a scalar memory access and FALSE for memory region access. |
| 2700 |
NON_ZERO_P is TRUE if memory region is guaranteed to have non-zero |
| 2701 |
length. ALIGN tells alignment of accessed memory object. |
| 2702 |
|
| 2703 |
START_INSTRUMENTED and END_INSTRUMENTED are TRUE if start/end of |
| 2704 |
memory region have already been instrumented. |
| 2705 |
|
| 2706 |
If BEFORE_P is TRUE, *ITER is arranged to still point to the |
| 2707 |
statement it was pointing to prior to calling this function, |
| 2708 |
otherwise, it points to the statement logically following it. */ |
| 2709 |
|
| 2710 |
static void |
| 2711 |
build_check_stmt (location_t loc, tree base, tree len, |
| 2712 |
HOST_WIDE_INT size_in_bytes, gimple_stmt_iterator *iter, |
| 2713 |
bool is_non_zero_len, bool before_p, bool is_store, |
| 2714 |
bool is_scalar_access, unsigned int align = 0) |
| 2715 |
{ |
| 2716 |
gimple *g; |
| 2717 |
|
| 2718 |
gcc_assert (!(size_in_bytes > 0 && !is_non_zero_len)); |
| 2719 |
gcc_assert (size_in_bytes == -1 || size_in_bytes >= 1); |
| 2720 |
|
| 2721 |
base = unshare_expr (base); |
| 2722 |
base = maybe_create_ssa_name (loc, base, iter, before_p); |
| 2723 |
|
| 2724 |
if (len) |
| 2725 |
{ |
| 2726 |
len = unshare_expr (len); |
| 2727 |
len = maybe_cast_to_ptrmode (loc, len, iter, before_p); |
| 2728 |
} |
| 2729 |
else |
| 2730 |
{ |
| 2731 |
gcc_assert (size_in_bytes != -1); |
| 2732 |
len = build_int_cst (pointer_sized_int_node, size_in_bytes); |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (size_in_bytes > 1) |
| 2736 |
{ |
| 2737 |
if ((size_in_bytes & (size_in_bytes - 1)) != 0 |
| 2738 |
|| size_in_bytes > 16) |
| 2739 |
is_scalar_access = false; |
| 2740 |
else if (align && align < size_in_bytes * BITS_PER_UNIT) |
| 2741 |
{ |
| 2742 |
/* On non-strict alignment targets, if |
| 2743 |
16-byte access is just 8-byte aligned, |
| 2744 |
this will result in misaligned shadow |
| 2745 |
memory 2 byte load, but otherwise can |
| 2746 |
be handled using one read. */ |
| 2747 |
if (size_in_bytes != 16 |
| 2748 |
|| STRICT_ALIGNMENT |
| 2749 |
|| align < 8 * BITS_PER_UNIT) |
| 2750 |
is_scalar_access = false; |
| 2751 |
} |
| 2752 |
} |
| 2753 |
|
| 2754 |
HOST_WIDE_INT flags = 0; |
| 2755 |
if (is_store) |
| 2756 |
flags |= ASAN_CHECK_STORE; |
| 2757 |
if (is_non_zero_len) |
| 2758 |
flags |= ASAN_CHECK_NON_ZERO_LEN; |
| 2759 |
if (is_scalar_access) |
| 2760 |
flags |= ASAN_CHECK_SCALAR_ACCESS; |
| 2761 |
|
| 2762 |
enum internal_fn fn = hwasan_sanitize_p () |
| 2763 |
? IFN_HWASAN_CHECK |
| 2764 |
: IFN_ASAN_CHECK; |
| 2765 |
|
| 2766 |
g = gimple_build_call_internal (fn, 4, |
| 2767 |
build_int_cst (integer_type_node, flags), |
| 2768 |
base, len, |
| 2769 |
build_int_cst (integer_type_node, |
| 2770 |
align / BITS_PER_UNIT)); |
| 2771 |
gimple_set_location (g, loc); |
| 2772 |
if (before_p) |
| 2773 |
gsi_safe_insert_before (iter, g); |
| 2774 |
else |
| 2775 |
{ |
| 2776 |
gsi_insert_after (iter, g, GSI_NEW_STMT); |
| 2777 |
gsi_next (iter); |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
/* If T represents a memory access, add instrumentation code before ITER. |
| 2782 |
LOCATION is source code location. |
| 2783 |
IS_STORE is either TRUE (for a store) or FALSE (for a load). */ |
| 2784 |
|
| 2785 |
static void |
| 2786 |
instrument_derefs (gimple_stmt_iterator *iter, tree t, |
| 2787 |
location_t location, bool is_store) |
| 2788 |
{ |
| 2789 |
if (is_store && !(asan_instrument_writes () || hwasan_instrument_writes ())) |
| 2790 |
return; |
| 2791 |
if (!is_store && !(asan_instrument_reads () || hwasan_instrument_reads ())) |
| 2792 |
return; |
| 2793 |
|
| 2794 |
tree type, base; |
| 2795 |
HOST_WIDE_INT size_in_bytes; |
| 2796 |
if (location == UNKNOWN_LOCATION) |
| 2797 |
location = EXPR_LOCATION (t); |
| 2798 |
|
| 2799 |
type = TREE_TYPE (t); |
| 2800 |
switch (TREE_CODE (t)) |
| 2801 |
{ |
| 2802 |
case ARRAY_REF: |
| 2803 |
case COMPONENT_REF: |
| 2804 |
case INDIRECT_REF: |
| 2805 |
case MEM_REF: |
| 2806 |
case VAR_DECL: |
| 2807 |
case BIT_FIELD_REF: |
| 2808 |
break; |
| 2809 |
/* FALLTHRU */ |
| 2810 |
default: |
| 2811 |
return; |
| 2812 |
} |
| 2813 |
|
| 2814 |
size_in_bytes = int_size_in_bytes (type); |
| 2815 |
if (size_in_bytes <= 0) |
| 2816 |
return; |
| 2817 |
|
| 2818 |
poly_int64 bitsize, bitpos; |
| 2819 |
tree offset; |
| 2820 |
machine_mode mode; |
| 2821 |
int unsignedp, reversep, volatilep = 0; |
| 2822 |
tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode, |
| 2823 |
&unsignedp, &reversep, &volatilep); |
| 2824 |
|
| 2825 |
if (TREE_CODE (t) == COMPONENT_REF |
| 2826 |
&& DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE) |
| 2827 |
{ |
| 2828 |
tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)); |
| 2829 |
instrument_derefs (iter, build3 (COMPONENT_REF, TREE_TYPE (repr), |
| 2830 |
TREE_OPERAND (t, 0), repr, |
| 2831 |
TREE_OPERAND (t, 2)), |
| 2832 |
location, is_store); |
| 2833 |
return; |
| 2834 |
} |
| 2835 |
|
| 2836 |
if (!multiple_p (bitpos, BITS_PER_UNIT) |
| 2837 |
|| maybe_ne (bitsize, size_in_bytes * BITS_PER_UNIT)) |
| 2838 |
return; |
| 2839 |
|
| 2840 |
if (VAR_P (inner) && DECL_HARD_REGISTER (inner)) |
| 2841 |
return; |
| 2842 |
|
| 2843 |
/* Accesses to non-generic address-spaces should not be instrumented. */ |
| 2844 |
if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (inner)))) |
| 2845 |
return; |
| 2846 |
|
| 2847 |
poly_int64 decl_size; |
| 2848 |
if ((VAR_P (inner) |
| 2849 |
|| (TREE_CODE (inner) == RESULT_DECL |
| 2850 |
&& !aggregate_value_p (inner, current_function_decl))) |
| 2851 |
&& offset == NULL_TREE |
| 2852 |
&& DECL_SIZE (inner) |
| 2853 |
&& poly_int_tree_p (DECL_SIZE (inner), &decl_size) |
| 2854 |
&& known_subrange_p (bitpos, bitsize, 0, decl_size)) |
| 2855 |
{ |
| 2856 |
if (VAR_P (inner) && DECL_THREAD_LOCAL_P (inner)) |
| 2857 |
return; |
| 2858 |
/* If we're not sanitizing globals and we can tell statically that this |
| 2859 |
access is inside a global variable, then there's no point adding |
| 2860 |
instrumentation to check the access. N.b. hwasan currently never |
| 2861 |
sanitizes globals. */ |
| 2862 |
if ((hwasan_sanitize_p () || !param_asan_globals) |
| 2863 |
&& is_global_var (inner)) |
| 2864 |
return; |
| 2865 |
if (!TREE_STATIC (inner)) |
| 2866 |
{ |
| 2867 |
/* Automatic vars in the current function will be always |
| 2868 |
accessible. */ |
| 2869 |
if (decl_function_context (inner) == current_function_decl |
| 2870 |
&& (!asan_sanitize_use_after_scope () |
| 2871 |
|| !TREE_ADDRESSABLE (inner))) |
| 2872 |
return; |
| 2873 |
} |
| 2874 |
/* Always instrument external vars, they might be dynamically |
| 2875 |
initialized. */ |
| 2876 |
else if (!DECL_EXTERNAL (inner)) |
| 2877 |
{ |
| 2878 |
/* For static vars if they are known not to be dynamically |
| 2879 |
initialized, they will be always accessible. */ |
| 2880 |
varpool_node *vnode = varpool_node::get (inner); |
| 2881 |
if (vnode && !vnode->dynamically_initialized) |
| 2882 |
return; |
| 2883 |
} |
| 2884 |
} |
| 2885 |
|
| 2886 |
if (DECL_P (inner) |
| 2887 |
&& decl_function_context (inner) == current_function_decl |
| 2888 |
&& !TREE_ADDRESSABLE (inner)) |
| 2889 |
mark_addressable (inner); |
| 2890 |
|
| 2891 |
base = build_fold_addr_expr (t); |
| 2892 |
if (!has_mem_ref_been_instrumented (base, size_in_bytes)) |
| 2893 |
{ |
| 2894 |
unsigned int align = get_object_alignment (t); |
| 2895 |
build_check_stmt (location, base, NULL_TREE, size_in_bytes, iter, |
| 2896 |
/*is_non_zero_len*/size_in_bytes > 0, /*before_p=*/true, |
| 2897 |
is_store, /*is_scalar_access*/true, align); |
| 2898 |
update_mem_ref_hash_table (base, size_in_bytes); |
| 2899 |
update_mem_ref_hash_table (t, size_in_bytes); |
| 2900 |
} |
| 2901 |
|
| 2902 |
} |
| 2903 |
|
| 2904 |
/* Insert a memory reference into the hash table if access length |
| 2905 |
can be determined in compile time. */ |
| 2906 |
|
| 2907 |
static void |
| 2908 |
maybe_update_mem_ref_hash_table (tree base, tree len) |
| 2909 |
{ |
| 2910 |
if (!POINTER_TYPE_P (TREE_TYPE (base)) |
| 2911 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (len))) |
| 2912 |
return; |
| 2913 |
|
| 2914 |
HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; |
| 2915 |
|
| 2916 |
if (size_in_bytes != -1) |
| 2917 |
update_mem_ref_hash_table (base, size_in_bytes); |
| 2918 |
} |
| 2919 |
|
| 2920 |
/* Instrument an access to a contiguous memory region that starts at |
| 2921 |
the address pointed to by BASE, over a length of LEN (expressed in |
| 2922 |
the sizeof (*BASE) bytes). ITER points to the instruction before |
| 2923 |
which the instrumentation instructions must be inserted. LOCATION |
| 2924 |
is the source location that the instrumentation instructions must |
| 2925 |
have. If IS_STORE is true, then the memory access is a store; |
| 2926 |
otherwise, it's a load. */ |
| 2927 |
|
| 2928 |
static void |
| 2929 |
instrument_mem_region_access (tree base, tree len, |
| 2930 |
gimple_stmt_iterator *iter, |
| 2931 |
location_t location, bool is_store) |
| 2932 |
{ |
| 2933 |
if (!POINTER_TYPE_P (TREE_TYPE (base)) |
| 2934 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (len)) |
| 2935 |
|| integer_zerop (len)) |
| 2936 |
return; |
| 2937 |
|
| 2938 |
HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; |
| 2939 |
|
| 2940 |
if ((size_in_bytes == -1) |
| 2941 |
|| !has_mem_ref_been_instrumented (base, size_in_bytes)) |
| 2942 |
{ |
| 2943 |
build_check_stmt (location, base, len, size_in_bytes, iter, |
| 2944 |
/*is_non_zero_len*/size_in_bytes > 0, /*before_p*/true, |
| 2945 |
is_store, /*is_scalar_access*/false, /*align*/0); |
| 2946 |
} |
| 2947 |
|
| 2948 |
maybe_update_mem_ref_hash_table (base, len); |
| 2949 |
*iter = gsi_for_stmt (gsi_stmt (*iter)); |
| 2950 |
} |
| 2951 |
|
| 2952 |
/* Instrument the call to a built-in memory access function that is |
| 2953 |
pointed to by the iterator ITER. |
| 2954 |
|
| 2955 |
Upon completion, return TRUE iff *ITER has been advanced to the |
| 2956 |
statement following the one it was originally pointing to. */ |
| 2957 |
|
| 2958 |
static bool |
| 2959 |
instrument_builtin_call (gimple_stmt_iterator *iter) |
| 2960 |
{ |
| 2961 |
if (!(asan_memintrin () || hwasan_memintrin ())) |
| 2962 |
return false; |
| 2963 |
|
| 2964 |
bool iter_advanced_p = false; |
| 2965 |
gcall *call = as_a <gcall *> (gsi_stmt (*iter)); |
| 2966 |
|
| 2967 |
gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); |
| 2968 |
|
| 2969 |
location_t loc = gimple_location (call); |
| 2970 |
|
| 2971 |
asan_mem_ref src0, src1, dest; |
| 2972 |
asan_mem_ref_init (&src0, NULL, 1); |
| 2973 |
asan_mem_ref_init (&src1, NULL, 1); |
| 2974 |
asan_mem_ref_init (&dest, NULL, 1); |
| 2975 |
|
| 2976 |
tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; |
| 2977 |
bool src0_is_store = false, src1_is_store = false, dest_is_store = false, |
| 2978 |
dest_is_deref = false, intercepted_p = true; |
| 2979 |
|
| 2980 |
if (get_mem_refs_of_builtin_call (call, |
| 2981 |
&src0, &src0_len, &src0_is_store, |
| 2982 |
&src1, &src1_len, &src1_is_store, |
| 2983 |
&dest, &dest_len, &dest_is_store, |
| 2984 |
&dest_is_deref, &intercepted_p, iter)) |
| 2985 |
{ |
| 2986 |
if (dest_is_deref) |
| 2987 |
{ |
| 2988 |
instrument_derefs (iter, dest.start, loc, dest_is_store); |
| 2989 |
gsi_next (iter); |
| 2990 |
iter_advanced_p = true; |
| 2991 |
} |
| 2992 |
else if (!intercepted_p |
| 2993 |
&& (src0_len || src1_len || dest_len)) |
| 2994 |
{ |
| 2995 |
if (src0.start != NULL_TREE) |
| 2996 |
instrument_mem_region_access (src0.start, src0_len, |
| 2997 |
iter, loc, /*is_store=*/false); |
| 2998 |
if (src1.start != NULL_TREE) |
| 2999 |
instrument_mem_region_access (src1.start, src1_len, |
| 3000 |
iter, loc, /*is_store=*/false); |
| 3001 |
if (dest.start != NULL_TREE) |
| 3002 |
instrument_mem_region_access (dest.start, dest_len, |
| 3003 |
iter, loc, /*is_store=*/true); |
| 3004 |
|
| 3005 |
*iter = gsi_for_stmt (call); |
| 3006 |
gsi_next (iter); |
| 3007 |
iter_advanced_p = true; |
| 3008 |
} |
| 3009 |
else |
| 3010 |
{ |
| 3011 |
if (src0.start != NULL_TREE) |
| 3012 |
maybe_update_mem_ref_hash_table (src0.start, src0_len); |
| 3013 |
if (src1.start != NULL_TREE) |
| 3014 |
maybe_update_mem_ref_hash_table (src1.start, src1_len); |
| 3015 |
if (dest.start != NULL_TREE) |
| 3016 |
maybe_update_mem_ref_hash_table (dest.start, dest_len); |
| 3017 |
} |
| 3018 |
} |
| 3019 |
return iter_advanced_p; |
| 3020 |
} |
| 3021 |
|
| 3022 |
/* Instrument the assignment statement ITER if it is subject to |
| 3023 |
instrumentation. Return TRUE iff instrumentation actually |
| 3024 |
happened. In that case, the iterator ITER is advanced to the next |
| 3025 |
logical expression following the one initially pointed to by ITER, |
| 3026 |
and the relevant memory reference that which access has been |
| 3027 |
instrumented is added to the memory references hash table. */ |
| 3028 |
|
| 3029 |
static bool |
| 3030 |
maybe_instrument_assignment (gimple_stmt_iterator *iter) |
| 3031 |
{ |
| 3032 |
gimple *s = gsi_stmt (*iter); |
| 3033 |
|
| 3034 |
gcc_assert (gimple_assign_single_p (s)); |
| 3035 |
|
| 3036 |
tree ref_expr = NULL_TREE; |
| 3037 |
bool is_store, is_instrumented = false; |
| 3038 |
|
| 3039 |
if (gimple_store_p (s)) |
| 3040 |
{ |
| 3041 |
ref_expr = gimple_assign_lhs (s); |
| 3042 |
is_store = true; |
| 3043 |
instrument_derefs (iter, ref_expr, |
| 3044 |
gimple_location (s), |
| 3045 |
is_store); |
| 3046 |
is_instrumented = true; |
| 3047 |
} |
| 3048 |
|
| 3049 |
if (gimple_assign_load_p (s)) |
| 3050 |
{ |
| 3051 |
ref_expr = gimple_assign_rhs1 (s); |
| 3052 |
is_store = false; |
| 3053 |
instrument_derefs (iter, ref_expr, |
| 3054 |
gimple_location (s), |
| 3055 |
is_store); |
| 3056 |
is_instrumented = true; |
| 3057 |
} |
| 3058 |
|
| 3059 |
if (is_instrumented) |
| 3060 |
gsi_next (iter); |
| 3061 |
|
| 3062 |
return is_instrumented; |
| 3063 |
} |
| 3064 |
|
| 3065 |
/* Instrument the function call pointed to by the iterator ITER, if it |
| 3066 |
is subject to instrumentation. At the moment, the only function |
| 3067 |
calls that are instrumented are some built-in functions that access |
| 3068 |
memory. Look at instrument_builtin_call to learn more. |
| 3069 |
|
| 3070 |
Upon completion return TRUE iff *ITER was advanced to the statement |
| 3071 |
following the one it was originally pointing to. */ |
| 3072 |
|
| 3073 |
static bool |
| 3074 |
maybe_instrument_call (gimple_stmt_iterator *iter) |
| 3075 |
{ |
| 3076 |
gimple *stmt = gsi_stmt (*iter); |
| 3077 |
bool is_builtin = gimple_call_builtin_p (stmt, BUILT_IN_NORMAL); |
| 3078 |
|
| 3079 |
if (is_builtin && instrument_builtin_call (iter)) |
| 3080 |
return true; |
| 3081 |
|
| 3082 |
if (gimple_call_noreturn_p (stmt)) |
| 3083 |
{ |
| 3084 |
if (is_builtin) |
| 3085 |
{ |
| 3086 |
tree callee = gimple_call_fndecl (stmt); |
| 3087 |
switch (DECL_FUNCTION_CODE (callee)) |
| 3088 |
{ |
| 3089 |
case BUILT_IN_UNREACHABLE: |
| 3090 |
case BUILT_IN_UNREACHABLE_TRAP: |
| 3091 |
case BUILT_IN_TRAP: |
| 3092 |
/* Don't instrument these. */ |
| 3093 |
return false; |
| 3094 |
default: |
| 3095 |
break; |
| 3096 |
} |
| 3097 |
} |
| 3098 |
if (gimple_call_internal_p (stmt, IFN_ABNORMAL_DISPATCHER)) |
| 3099 |
/* Don't instrument this. */ |
| 3100 |
return false; |
| 3101 |
/* If a function does not return, then we must handle clearing up the |
| 3102 |
shadow stack accordingly. For ASAN we can simply set the entire stack |
| 3103 |
to "valid" for accesses by setting the shadow space to 0 and all |
| 3104 |
accesses will pass checks. That means that some bad accesses may be |
| 3105 |
missed, but we will not report any false positives. |
| 3106 |
|
| 3107 |
This is not possible for HWASAN. Since there is no "always valid" tag |
| 3108 |
we can not set any space to "always valid". If we were to clear the |
| 3109 |
entire shadow stack then code resuming from `longjmp` or a caught |
| 3110 |
exception would trigger false positives when correctly accessing |
| 3111 |
variables on the stack. Hence we need to handle things like |
| 3112 |
`longjmp`, thread exit, and exceptions in a different way. These |
| 3113 |
problems must be handled externally to the compiler, e.g. in the |
| 3114 |
language runtime. */ |
| 3115 |
if (! hwasan_sanitize_p ()) |
| 3116 |
{ |
| 3117 |
tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN); |
| 3118 |
gimple *g = gimple_build_call (decl, 0); |
| 3119 |
gimple_set_location (g, gimple_location (stmt)); |
| 3120 |
gsi_safe_insert_before (iter, g); |
| 3121 |
} |
| 3122 |
} |
| 3123 |
|
| 3124 |
bool instrumented = false; |
| 3125 |
if (gimple_store_p (stmt) |
| 3126 |
&& (gimple_call_builtin_p (stmt) |
| 3127 |
|| gimple_call_internal_p (stmt) |
| 3128 |
|| !aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)), |
| 3129 |
gimple_call_fntype (stmt)))) |
| 3130 |
{ |
| 3131 |
tree ref_expr = gimple_call_lhs (stmt); |
| 3132 |
instrument_derefs (iter, ref_expr, |
| 3133 |
gimple_location (stmt), |
| 3134 |
/*is_store=*/true); |
| 3135 |
|
| 3136 |
instrumented = true; |
| 3137 |
} |
| 3138 |
|
| 3139 |
/* Walk through gimple_call arguments and check them id needed. */ |
| 3140 |
unsigned args_num = gimple_call_num_args (stmt); |
| 3141 |
for (unsigned i = 0; i < args_num; ++i) |
| 3142 |
{ |
| 3143 |
tree arg = gimple_call_arg (stmt, i); |
| 3144 |
/* If ARG is not a non-aggregate register variable, compiler in general |
| 3145 |
creates temporary for it and pass it as argument to gimple call. |
| 3146 |
But in some cases, e.g. when we pass by value a small structure that |
| 3147 |
fits to register, compiler can avoid extra overhead by pulling out |
| 3148 |
these temporaries. In this case, we should check the argument. */ |
| 3149 |
if (!is_gimple_reg (arg) && !is_gimple_min_invariant (arg)) |
| 3150 |
{ |
| 3151 |
instrument_derefs (iter, arg, |
| 3152 |
gimple_location (stmt), |
| 3153 |
/*is_store=*/false); |
| 3154 |
instrumented = true; |
| 3155 |
} |
| 3156 |
} |
| 3157 |
if (instrumented) |
| 3158 |
gsi_next (iter); |
| 3159 |
return instrumented; |
| 3160 |
} |
| 3161 |
|
| 3162 |
/* Walk each instruction of all basic block and instrument those that |
| 3163 |
represent memory references: loads, stores, or function calls. |
| 3164 |
In a given basic block, this function avoids instrumenting memory |
| 3165 |
references that have already been instrumented. */ |
| 3166 |
|
| 3167 |
static void |
| 3168 |
transform_statements (void) |
| 3169 |
{ |
| 3170 |
basic_block bb, last_bb = NULL; |
| 3171 |
gimple_stmt_iterator i; |
| 3172 |
int saved_last_basic_block = last_basic_block_for_fn (cfun); |
| 3173 |
|
| 3174 |
FOR_EACH_BB_FN (bb, cfun) |
| 3175 |
{ |
| 3176 |
basic_block prev_bb = bb; |
| 3177 |
|
| 3178 |
if (bb->index >= saved_last_basic_block) continue; |
| 3179 |
|
| 3180 |
/* Flush the mem ref hash table, if current bb doesn't have |
| 3181 |
exactly one predecessor, or if that predecessor (skipping |
| 3182 |
over asan created basic blocks) isn't the last processed |
| 3183 |
basic block. Thus we effectively flush on extended basic |
| 3184 |
block boundaries. */ |
| 3185 |
while (single_pred_p (prev_bb)) |
| 3186 |
{ |
| 3187 |
prev_bb = single_pred (prev_bb); |
| 3188 |
if (prev_bb->index < saved_last_basic_block) |
| 3189 |
break; |
| 3190 |
} |
| 3191 |
if (prev_bb != last_bb) |
| 3192 |
empty_mem_ref_hash_table (); |
| 3193 |
last_bb = bb; |
| 3194 |
|
| 3195 |
for (i = gsi_start_bb (bb); !gsi_end_p (i);) |
| 3196 |
{ |
| 3197 |
gimple *s = gsi_stmt (i); |
| 3198 |
|
| 3199 |
if (has_stmt_been_instrumented_p (s)) |
| 3200 |
gsi_next (&i); |
| 3201 |
else if (gimple_assign_single_p (s) |
| 3202 |
&& !gimple_clobber_p (s) |
| 3203 |
&& maybe_instrument_assignment (&i)) |
| 3204 |
/* Nothing to do as maybe_instrument_assignment advanced |
| 3205 |
the iterator I. */; |
| 3206 |
else if (is_gimple_call (s) && maybe_instrument_call (&i)) |
| 3207 |
/* Nothing to do as maybe_instrument_call |
| 3208 |
advanced the iterator I. */; |
| 3209 |
else |
| 3210 |
{ |
| 3211 |
/* No instrumentation happened. |
| 3212 |
|
| 3213 |
If the current instruction is a function call that |
| 3214 |
might free something, let's forget about the memory |
| 3215 |
references that got instrumented. Otherwise we might |
| 3216 |
miss some instrumentation opportunities. Do the same |
| 3217 |
for a ASAN_MARK poisoning internal function. */ |
| 3218 |
if (is_gimple_call (s) |
| 3219 |
&& (!nonfreeing_call_p (s) |
| 3220 |
|| asan_mark_p (s, ASAN_MARK_POISON))) |
| 3221 |
empty_mem_ref_hash_table (); |
| 3222 |
|
| 3223 |
gsi_next (&i); |
| 3224 |
} |
| 3225 |
} |
| 3226 |
} |
| 3227 |
free_mem_ref_resources (); |
| 3228 |
} |
| 3229 |
|
| 3230 |
/* Build |
| 3231 |
__asan_before_dynamic_init (module_name) |
| 3232 |
or |
| 3233 |
__asan_after_dynamic_init () |
| 3234 |
call. */ |
| 3235 |
|
| 3236 |
tree |
| 3237 |
asan_dynamic_init_call (bool after_p) |
| 3238 |
{ |
| 3239 |
if (shadow_ptr_types[0] == NULL_TREE) |
| 3240 |
asan_init_shadow_ptr_types (); |
| 3241 |
|
| 3242 |
tree fn = builtin_decl_implicit (after_p |
| 3243 |
? BUILT_IN_ASAN_AFTER_DYNAMIC_INIT |
| 3244 |
: BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT); |
| 3245 |
tree module_name_cst = NULL_TREE; |
| 3246 |
if (!after_p) |
| 3247 |
{ |
| 3248 |
pretty_printer module_name_pp; |
| 3249 |
pp_string (&module_name_pp, main_input_filename); |
| 3250 |
|
| 3251 |
module_name_cst = asan_pp_string (&module_name_pp); |
| 3252 |
module_name_cst = fold_convert (const_ptr_type_node, |
| 3253 |
module_name_cst); |
| 3254 |
} |
| 3255 |
|
| 3256 |
return build_call_expr (fn, after_p ? 0 : 1, module_name_cst); |
| 3257 |
} |
| 3258 |
|
| 3259 |
/* Build |
| 3260 |
struct __asan_global |
| 3261 |
{ |
| 3262 |
const void *__beg; |
| 3263 |
uptr __size; |
| 3264 |
uptr __size_with_redzone; |
| 3265 |
const void *__name; |
| 3266 |
const void *__module_name; |
| 3267 |
uptr __has_dynamic_init; |
| 3268 |
__asan_global_source_location *__location; |
| 3269 |
char *__odr_indicator; |
| 3270 |
} type. */ |
| 3271 |
|
| 3272 |
static tree |
| 3273 |
asan_global_struct (void) |
| 3274 |
{ |
| 3275 |
static const char *field_names[] |
| 3276 |
= { "__beg", "__size", "__size_with_redzone", |
| 3277 |
"__name", "__module_name", "__has_dynamic_init", "__location", |
| 3278 |
"__odr_indicator" }; |
| 3279 |
tree fields[ARRAY_SIZE (field_names)], ret; |
| 3280 |
unsigned i; |
| 3281 |
|
| 3282 |
ret = make_node (RECORD_TYPE); |
| 3283 |
for (i = 0; i < ARRAY_SIZE (field_names); i++) |
| 3284 |
{ |
| 3285 |
fields[i] |
| 3286 |
= build_decl (UNKNOWN_LOCATION, FIELD_DECL, |
| 3287 |
get_identifier (field_names[i]), |
| 3288 |
(i == 0 || i == 3) ? const_ptr_type_node |
| 3289 |
: pointer_sized_int_node); |
| 3290 |
DECL_CONTEXT (fields[i]) = ret; |
| 3291 |
if (i) |
| 3292 |
DECL_CHAIN (fields[i - 1]) = fields[i]; |
| 3293 |
} |
| 3294 |
tree type_decl = build_decl (input_location, TYPE_DECL, |
| 3295 |
get_identifier ("__asan_global"), ret); |
| 3296 |
DECL_IGNORED_P (type_decl) = 1; |
| 3297 |
DECL_ARTIFICIAL (type_decl) = 1; |
| 3298 |
TYPE_FIELDS (ret) = fields[0]; |
| 3299 |
TYPE_NAME (ret) = type_decl; |
| 3300 |
TYPE_STUB_DECL (ret) = type_decl; |
| 3301 |
TYPE_ARTIFICIAL (ret) = 1; |
| 3302 |
layout_type (ret); |
| 3303 |
return ret; |
| 3304 |
} |
| 3305 |
|
| 3306 |
/* Create and return odr indicator symbol for DECL. |
| 3307 |
TYPE is __asan_global struct type as returned by asan_global_struct. */ |
| 3308 |
|
| 3309 |
static tree |
| 3310 |
create_odr_indicator (tree decl, tree type) |
| 3311 |
{ |
| 3312 |
char *name; |
| 3313 |
tree uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))); |
| 3314 |
tree decl_name |
| 3315 |
= (HAS_DECL_ASSEMBLER_NAME_P (decl) ? DECL_ASSEMBLER_NAME (decl) |
| 3316 |
: DECL_NAME (decl)); |
| 3317 |
/* DECL_NAME theoretically might be NULL. Bail out with 0 in this case. */ |
| 3318 |
if (decl_name == NULL_TREE) |
| 3319 |
return build_int_cst (uptr, 0); |
| 3320 |
const char *dname = IDENTIFIER_POINTER (decl_name); |
| 3321 |
if (HAS_DECL_ASSEMBLER_NAME_P (decl)) |
| 3322 |
dname = targetm.strip_name_encoding (dname); |
| 3323 |
size_t len = strlen (dname) + sizeof ("__odr_asan_"); |
| 3324 |
name = XALLOCAVEC (char, len); |
| 3325 |
snprintf (name, len, "__odr_asan_%s", dname); |
| 3326 |
#ifndef NO_DOT_IN_LABEL |
| 3327 |
name[sizeof ("__odr_asan") - 1] = '.'; |
| 3328 |
#elif !defined(NO_DOLLAR_IN_LABEL) |
| 3329 |
name[sizeof ("__odr_asan") - 1] = '$'; |
| 3330 |
#endif |
| 3331 |
tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (name), |
| 3332 |
char_type_node); |
| 3333 |
TREE_ADDRESSABLE (var) = 1; |
| 3334 |
TREE_READONLY (var) = 0; |
| 3335 |
TREE_THIS_VOLATILE (var) = 1; |
| 3336 |
DECL_ARTIFICIAL (var) = 1; |
| 3337 |
DECL_IGNORED_P (var) = 1; |
| 3338 |
TREE_STATIC (var) = 1; |
| 3339 |
TREE_PUBLIC (var) = 1; |
| 3340 |
DECL_VISIBILITY (var) = DECL_VISIBILITY (decl); |
| 3341 |
DECL_VISIBILITY_SPECIFIED (var) = DECL_VISIBILITY_SPECIFIED (decl); |
| 3342 |
|
| 3343 |
TREE_USED (var) = 1; |
| 3344 |
tree ctor = build_constructor_va (TREE_TYPE (var), 1, NULL_TREE, |
| 3345 |
build_int_cst (unsigned_type_node, 0)); |
| 3346 |
TREE_CONSTANT (ctor) = 1; |
| 3347 |
TREE_STATIC (ctor) = 1; |
| 3348 |
DECL_INITIAL (var) = ctor; |
| 3349 |
DECL_ATTRIBUTES (var) = tree_cons (get_identifier ("asan odr indicator"), |
| 3350 |
NULL, DECL_ATTRIBUTES (var)); |
| 3351 |
make_decl_rtl (var); |
| 3352 |
varpool_node::finalize_decl (var); |
| 3353 |
return fold_convert (uptr, build_fold_addr_expr (var)); |
| 3354 |
} |
| 3355 |
|
| 3356 |
/* Return true if DECL, a global var, might be overridden and needs |
| 3357 |
an additional odr indicator symbol. */ |
| 3358 |
|
| 3359 |
static bool |
| 3360 |
asan_needs_odr_indicator_p (tree decl) |
| 3361 |
{ |
| 3362 |
/* Don't emit ODR indicators for kernel because: |
| 3363 |
a) Kernel is written in C thus doesn't need ODR indicators. |
| 3364 |
b) Some kernel code may have assumptions about symbols containing specific |
| 3365 |
patterns in their names. Since ODR indicators contain original names |
| 3366 |
of symbols they are emitted for, these assumptions would be broken for |
| 3367 |
ODR indicator symbols. */ |
| 3368 |
return (!(flag_sanitize & SANITIZE_KERNEL_ADDRESS) |
| 3369 |
&& !DECL_ARTIFICIAL (decl) |
| 3370 |
&& !DECL_WEAK (decl) |
| 3371 |
&& TREE_PUBLIC (decl)); |
| 3372 |
} |
| 3373 |
|
| 3374 |
/* Append description of a single global DECL into vector V. |
| 3375 |
TYPE is __asan_global struct type as returned by asan_global_struct. */ |
| 3376 |
|
| 3377 |
static void |
| 3378 |
asan_add_global (tree decl, tree type, vec<constructor_elt, va_gc> *v) |
| 3379 |
{ |
| 3380 |
tree init, uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))); |
| 3381 |
unsigned HOST_WIDE_INT size; |
| 3382 |
tree str_cst, module_name_cst, refdecl = decl; |
| 3383 |
vec<constructor_elt, va_gc> *vinner = NULL; |
| 3384 |
|
| 3385 |
pretty_printer asan_pp, module_name_pp; |
| 3386 |
|
| 3387 |
if (DECL_NAME (decl)) |
| 3388 |
pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
| 3389 |
else |
| 3390 |
pp_string (&asan_pp, "<unknown>"); |
| 3391 |
str_cst = asan_pp_string (&asan_pp); |
| 3392 |
|
| 3393 |
if (!in_lto_p) |
| 3394 |
pp_string (&module_name_pp, main_input_filename); |
| 3395 |
else |
| 3396 |
{ |
| 3397 |
const_tree tu = get_ultimate_context ((const_tree)decl); |
| 3398 |
if (tu != NULL_TREE) |
| 3399 |
pp_string (&module_name_pp, IDENTIFIER_POINTER (DECL_NAME (tu))); |
| 3400 |
else |
| 3401 |
pp_string (&module_name_pp, aux_base_name); |
| 3402 |
} |
| 3403 |
|
| 3404 |
module_name_cst = asan_pp_string (&module_name_pp); |
| 3405 |
|
| 3406 |
if (asan_needs_local_alias (decl)) |
| 3407 |
{ |
| 3408 |
char buf[20]; |
| 3409 |
ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", vec_safe_length (v) + 1); |
| 3410 |
refdecl = build_decl (DECL_SOURCE_LOCATION (decl), |
| 3411 |
VAR_DECL, get_identifier (buf), TREE_TYPE (decl)); |
| 3412 |
TREE_ADDRESSABLE (refdecl) = TREE_ADDRESSABLE (decl); |
| 3413 |
TREE_READONLY (refdecl) = TREE_READONLY (decl); |
| 3414 |
TREE_THIS_VOLATILE (refdecl) = TREE_THIS_VOLATILE (decl); |
| 3415 |
DECL_NOT_GIMPLE_REG_P (refdecl) = DECL_NOT_GIMPLE_REG_P (decl); |
| 3416 |
DECL_ARTIFICIAL (refdecl) = DECL_ARTIFICIAL (decl); |
| 3417 |
DECL_IGNORED_P (refdecl) = DECL_IGNORED_P (decl); |
| 3418 |
TREE_STATIC (refdecl) = 1; |
| 3419 |
TREE_PUBLIC (refdecl) = 0; |
| 3420 |
TREE_USED (refdecl) = 1; |
| 3421 |
assemble_alias (refdecl, DECL_ASSEMBLER_NAME (decl)); |
| 3422 |
} |
| 3423 |
|
| 3424 |
tree odr_indicator_ptr |
| 3425 |
= (asan_needs_odr_indicator_p (decl) ? create_odr_indicator (decl, type) |
| 3426 |
: build_int_cst (uptr, 0)); |
| 3427 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, |
| 3428 |
fold_convert (const_ptr_type_node, |
| 3429 |
build_fold_addr_expr (refdecl))); |
| 3430 |
size = tree_to_uhwi (DECL_SIZE_UNIT (decl)); |
| 3431 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); |
| 3432 |
size += asan_red_zone_size (size); |
| 3433 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); |
| 3434 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, |
| 3435 |
fold_convert (const_ptr_type_node, str_cst)); |
| 3436 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, |
| 3437 |
fold_convert (const_ptr_type_node, module_name_cst)); |
| 3438 |
varpool_node *vnode = varpool_node::get (decl); |
| 3439 |
int has_dynamic_init = 0; |
| 3440 |
/* FIXME: Enable initialization order fiasco detection in LTO mode once |
| 3441 |
proper fix for PR 79061 will be applied. */ |
| 3442 |
if (!in_lto_p) |
| 3443 |
has_dynamic_init = vnode ? vnode->dynamically_initialized : 0; |
| 3444 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, |
| 3445 |
build_int_cst (uptr, has_dynamic_init)); |
| 3446 |
tree locptr = NULL_TREE; |
| 3447 |
location_t loc = DECL_SOURCE_LOCATION (decl); |
| 3448 |
expanded_location xloc = expand_location (loc); |
| 3449 |
if (xloc.file != NULL) |
| 3450 |
{ |
| 3451 |
static int lasanloccnt = 0; |
| 3452 |
char buf[25]; |
| 3453 |
ASM_GENERATE_INTERNAL_LABEL (buf, "LASANLOC", ++lasanloccnt); |
| 3454 |
tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf), |
| 3455 |
ubsan_get_source_location_type ()); |
| 3456 |
TREE_STATIC (var) = 1; |
| 3457 |
TREE_PUBLIC (var) = 0; |
| 3458 |
DECL_ARTIFICIAL (var) = 1; |
| 3459 |
DECL_IGNORED_P (var) = 1; |
| 3460 |
pretty_printer filename_pp; |
| 3461 |
pp_string (&filename_pp, xloc.file); |
| 3462 |
tree str = asan_pp_string (&filename_pp); |
| 3463 |
tree ctor = build_constructor_va (TREE_TYPE (var), 3, |
| 3464 |
NULL_TREE, str, NULL_TREE, |
| 3465 |
build_int_cst (unsigned_type_node, |
| 3466 |
xloc.line), NULL_TREE, |
| 3467 |
build_int_cst (unsigned_type_node, |
| 3468 |
xloc.column)); |
| 3469 |
TREE_CONSTANT (ctor) = 1; |
| 3470 |
TREE_STATIC (ctor) = 1; |
| 3471 |
DECL_INITIAL (var) = ctor; |
| 3472 |
varpool_node::finalize_decl (var); |
| 3473 |
locptr = fold_convert (uptr, build_fold_addr_expr (var)); |
| 3474 |
} |
| 3475 |
else |
| 3476 |
locptr = build_int_cst (uptr, 0); |
| 3477 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, locptr); |
| 3478 |
CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, odr_indicator_ptr); |
| 3479 |
init = build_constructor (type, vinner); |
| 3480 |
CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); |
| 3481 |
} |
| 3482 |
|
| 3483 |
/* Initialize sanitizer.def builtins if the FE hasn't initialized them. */ |
| 3484 |
void |
| 3485 |
initialize_sanitizer_builtins (void) |
| 3486 |
{ |
| 3487 |
tree decl; |
| 3488 |
|
| 3489 |
if (builtin_decl_implicit_p (BUILT_IN_ASAN_INIT)) |
| 3490 |
return; |
| 3491 |
|
| 3492 |
tree BT_FN_VOID = build_function_type_list (void_type_node, NULL_TREE); |
| 3493 |
tree BT_FN_VOID_PTR |
| 3494 |
= build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); |
| 3495 |
tree BT_FN_VOID_CONST_PTR |
| 3496 |
= build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE); |
| 3497 |
tree BT_FN_VOID_PTR_PTR |
| 3498 |
= build_function_type_list (void_type_node, ptr_type_node, |
| 3499 |
ptr_type_node, NULL_TREE); |
| 3500 |
tree BT_FN_VOID_PTR_PTR_PTR |
| 3501 |
= build_function_type_list (void_type_node, ptr_type_node, |
| 3502 |
ptr_type_node, ptr_type_node, NULL_TREE); |
| 3503 |
tree BT_FN_VOID_PTR_PTRMODE |
| 3504 |
= build_function_type_list (void_type_node, ptr_type_node, |
| 3505 |
pointer_sized_int_node, NULL_TREE); |
| 3506 |
tree BT_FN_VOID_INT |
| 3507 |
= build_function_type_list (void_type_node, integer_type_node, NULL_TREE); |
| 3508 |
tree BT_FN_SIZE_CONST_PTR_INT |
| 3509 |
= build_function_type_list (size_type_node, const_ptr_type_node, |
| 3510 |
integer_type_node, NULL_TREE); |
| 3511 |
|
| 3512 |
tree BT_FN_VOID_UINT8_UINT8 |
| 3513 |
= build_function_type_list (void_type_node, unsigned_char_type_node, |
| 3514 |
unsigned_char_type_node, NULL_TREE); |
| 3515 |
tree BT_FN_VOID_UINT16_UINT16 |
| 3516 |
= build_function_type_list (void_type_node, uint16_type_node, |
| 3517 |
uint16_type_node, NULL_TREE); |
| 3518 |
tree BT_FN_VOID_UINT32_UINT32 |
| 3519 |
= build_function_type_list (void_type_node, uint32_type_node, |
| 3520 |
uint32_type_node, NULL_TREE); |
| 3521 |
tree BT_FN_VOID_UINT64_UINT64 |
| 3522 |
= build_function_type_list (void_type_node, uint64_type_node, |
| 3523 |
uint64_type_node, NULL_TREE); |
| 3524 |
tree BT_FN_VOID_FLOAT_FLOAT |
| 3525 |
= build_function_type_list (void_type_node, float_type_node, |
| 3526 |
float_type_node, NULL_TREE); |
| 3527 |
tree BT_FN_VOID_DOUBLE_DOUBLE |
| 3528 |
= build_function_type_list (void_type_node, double_type_node, |
| 3529 |
double_type_node, NULL_TREE); |
| 3530 |
tree BT_FN_VOID_UINT64_PTR |
| 3531 |
= build_function_type_list (void_type_node, uint64_type_node, |
| 3532 |
ptr_type_node, NULL_TREE); |
| 3533 |
|
| 3534 |
tree BT_FN_PTR_CONST_PTR_UINT8 |
| 3535 |
= build_function_type_list (ptr_type_node, const_ptr_type_node, |
| 3536 |
unsigned_char_type_node, NULL_TREE); |
| 3537 |
tree BT_FN_VOID_PTR_UINT8_PTRMODE |
| 3538 |
= build_function_type_list (void_type_node, ptr_type_node, |
| 3539 |
unsigned_char_type_node, |
| 3540 |
pointer_sized_int_node, NULL_TREE); |
| 3541 |
|
| 3542 |
tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5]; |
| 3543 |
tree BT_FN_IX_CONST_VPTR_INT[5]; |
| 3544 |
tree BT_FN_IX_VPTR_IX_INT[5]; |
| 3545 |
tree BT_FN_VOID_VPTR_IX_INT[5]; |
| 3546 |
tree vptr |
| 3547 |
= build_pointer_type (build_qualified_type (void_type_node, |
| 3548 |
TYPE_QUAL_VOLATILE)); |
| 3549 |
tree cvptr |
| 3550 |
= build_pointer_type (build_qualified_type (void_type_node, |
| 3551 |
TYPE_QUAL_VOLATILE |
| 3552 |
|TYPE_QUAL_CONST)); |
| 3553 |
tree boolt |
| 3554 |
= lang_hooks.types.type_for_size (BOOL_TYPE_SIZE, 1); |
| 3555 |
int i; |
| 3556 |
for (i = 0; i < 5; i++) |
| 3557 |
{ |
| 3558 |
tree ix = build_nonstandard_integer_type (BITS_PER_UNIT * (1 << i), 1); |
| 3559 |
BT_FN_BOOL_VPTR_PTR_IX_INT_INT[i] |
| 3560 |
= build_function_type_list (boolt, vptr, ptr_type_node, ix, |
| 3561 |
integer_type_node, integer_type_node, |
| 3562 |
NULL_TREE); |
| 3563 |
BT_FN_IX_CONST_VPTR_INT[i] |
| 3564 |
= build_function_type_list (ix, cvptr, integer_type_node, NULL_TREE); |
| 3565 |
BT_FN_IX_VPTR_IX_INT[i] |
| 3566 |
= build_function_type_list (ix, vptr, ix, integer_type_node, |
| 3567 |
NULL_TREE); |
| 3568 |
BT_FN_VOID_VPTR_IX_INT[i] |
| 3569 |
= build_function_type_list (void_type_node, vptr, ix, |
| 3570 |
integer_type_node, NULL_TREE); |
| 3571 |
} |
| 3572 |
#define BT_FN_BOOL_VPTR_PTR_I1_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[0] |
| 3573 |
#define BT_FN_I1_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[0] |
| 3574 |
#define BT_FN_I1_VPTR_I1_INT BT_FN_IX_VPTR_IX_INT[0] |
| 3575 |
#define BT_FN_VOID_VPTR_I1_INT BT_FN_VOID_VPTR_IX_INT[0] |
| 3576 |
#define BT_FN_BOOL_VPTR_PTR_I2_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[1] |
| 3577 |
#define BT_FN_I2_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[1] |
| 3578 |
#define BT_FN_I2_VPTR_I2_INT BT_FN_IX_VPTR_IX_INT[1] |
| 3579 |
#define BT_FN_VOID_VPTR_I2_INT BT_FN_VOID_VPTR_IX_INT[1] |
| 3580 |
#define BT_FN_BOOL_VPTR_PTR_I4_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[2] |
| 3581 |
#define BT_FN_I4_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[2] |
| 3582 |
#define BT_FN_I4_VPTR_I4_INT BT_FN_IX_VPTR_IX_INT[2] |
| 3583 |
#define BT_FN_VOID_VPTR_I4_INT BT_FN_VOID_VPTR_IX_INT[2] |
| 3584 |
#define BT_FN_BOOL_VPTR_PTR_I8_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[3] |
| 3585 |
#define BT_FN_I8_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[3] |
| 3586 |
#define BT_FN_I8_VPTR_I8_INT BT_FN_IX_VPTR_IX_INT[3] |
| 3587 |
#define BT_FN_VOID_VPTR_I8_INT BT_FN_VOID_VPTR_IX_INT[3] |
| 3588 |
#define BT_FN_BOOL_VPTR_PTR_I16_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[4] |
| 3589 |
#define BT_FN_I16_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[4] |
| 3590 |
#define BT_FN_I16_VPTR_I16_INT BT_FN_IX_VPTR_IX_INT[4] |
| 3591 |
#define BT_FN_VOID_VPTR_I16_INT BT_FN_VOID_VPTR_IX_INT[4] |
| 3592 |
#undef ATTR_NOTHROW_LIST |
| 3593 |
#define ATTR_NOTHROW_LIST ECF_NOTHROW |
| 3594 |
#undef ATTR_NOTHROW_LEAF_LIST |
| 3595 |
#define ATTR_NOTHROW_LEAF_LIST ECF_NOTHROW | ECF_LEAF |
| 3596 |
#undef ATTR_TMPURE_NOTHROW_LEAF_LIST |
| 3597 |
#define ATTR_TMPURE_NOTHROW_LEAF_LIST ECF_TM_PURE | ATTR_NOTHROW_LEAF_LIST |
| 3598 |
#undef ATTR_NORETURN_NOTHROW_LEAF_LIST |
| 3599 |
#define ATTR_NORETURN_NOTHROW_LEAF_LIST ECF_NORETURN | ATTR_NOTHROW_LEAF_LIST |
| 3600 |
#undef ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST |
| 3601 |
#define ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST \ |
| 3602 |
ECF_CONST | ATTR_NORETURN_NOTHROW_LEAF_LIST |
| 3603 |
#undef ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST |
| 3604 |
#define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST \ |
| 3605 |
ECF_TM_PURE | ATTR_NORETURN_NOTHROW_LEAF_LIST |
| 3606 |
#undef ATTR_COLD_NOTHROW_LEAF_LIST |
| 3607 |
#define ATTR_COLD_NOTHROW_LEAF_LIST \ |
| 3608 |
/* ECF_COLD missing */ ATTR_NOTHROW_LEAF_LIST |
| 3609 |
#undef ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST |
| 3610 |
#define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \ |
| 3611 |
/* ECF_COLD missing */ ATTR_NORETURN_NOTHROW_LEAF_LIST |
| 3612 |
#undef ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST |
| 3613 |
#define ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST \ |
| 3614 |
/* ECF_COLD missing */ ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST |
| 3615 |
#undef ATTR_PURE_NOTHROW_LEAF_LIST |
| 3616 |
#define ATTR_PURE_NOTHROW_LEAF_LIST ECF_PURE | ATTR_NOTHROW_LEAF_LIST |
| 3617 |
#undef DEF_BUILTIN_STUB |
| 3618 |
#define DEF_BUILTIN_STUB(ENUM, NAME) |
| 3619 |
#undef DEF_SANITIZER_BUILTIN_1 |
| 3620 |
#define DEF_SANITIZER_BUILTIN_1(ENUM, NAME, TYPE, ATTRS) \ |
| 3621 |
do { \ |
| 3622 |
decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM, \ |
| 3623 |
BUILT_IN_NORMAL, NAME, NULL_TREE); \ |
| 3624 |
set_call_expr_flags (decl, ATTRS); \ |
| 3625 |
set_builtin_decl (ENUM, decl, true); \ |
| 3626 |
} while (0) |
| 3627 |
#undef DEF_SANITIZER_BUILTIN |
| 3628 |
#define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \ |
| 3629 |
DEF_SANITIZER_BUILTIN_1 (ENUM, NAME, TYPE, ATTRS); |
| 3630 |
|
| 3631 |
#include "sanitizer.def" |
| 3632 |
|
| 3633 |
/* -fsanitize=object-size uses __builtin_dynamic_object_size and |
| 3634 |
__builtin_object_size, but they might not be available for e.g. Fortran at |
| 3635 |
this point. We use DEF_SANITIZER_BUILTIN here only as a convenience |
| 3636 |
macro. */ |
| 3637 |
if (flag_sanitize & SANITIZE_OBJECT_SIZE) |
| 3638 |
{ |
| 3639 |
if (!builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE)) |
| 3640 |
DEF_SANITIZER_BUILTIN_1 (BUILT_IN_OBJECT_SIZE, "object_size", |
| 3641 |
BT_FN_SIZE_CONST_PTR_INT, |
| 3642 |
ATTR_PURE_NOTHROW_LEAF_LIST); |
| 3643 |
if (!builtin_decl_implicit_p (BUILT_IN_DYNAMIC_OBJECT_SIZE)) |
| 3644 |
DEF_SANITIZER_BUILTIN_1 (BUILT_IN_DYNAMIC_OBJECT_SIZE, |
| 3645 |
"dynamic_object_size", |
| 3646 |
BT_FN_SIZE_CONST_PTR_INT, |
| 3647 |
ATTR_PURE_NOTHROW_LEAF_LIST); |
| 3648 |
} |
| 3649 |
|
| 3650 |
#undef DEF_SANITIZER_BUILTIN_1 |
| 3651 |
#undef DEF_SANITIZER_BUILTIN |
| 3652 |
#undef DEF_BUILTIN_STUB |
| 3653 |
} |
| 3654 |
|
| 3655 |
/* Called via htab_traverse. Count number of emitted |
| 3656 |
STRING_CSTs in the constant hash table. */ |
| 3657 |
|
| 3658 |
int |
| 3659 |
count_string_csts (constant_descriptor_tree **slot, |
| 3660 |
unsigned HOST_WIDE_INT *data) |
| 3661 |
{ |
| 3662 |
struct constant_descriptor_tree *desc = *slot; |
| 3663 |
if (TREE_CODE (desc->value) == STRING_CST |
| 3664 |
&& TREE_ASM_WRITTEN (desc->value) |
| 3665 |
&& asan_protect_global (desc->value)) |
| 3666 |
++*data; |
| 3667 |
return 1; |
| 3668 |
} |
| 3669 |
|
| 3670 |
/* Helper structure to pass two parameters to |
| 3671 |
add_string_csts. */ |
| 3672 |
|
| 3673 |
struct asan_add_string_csts_data |
| 3674 |
{ |
| 3675 |
tree type; |
| 3676 |
vec<constructor_elt, va_gc> *v; |
| 3677 |
}; |
| 3678 |
|
| 3679 |
/* Called via hash_table::traverse. Call asan_add_global |
| 3680 |
on emitted STRING_CSTs from the constant hash table. */ |
| 3681 |
|
| 3682 |
int |
| 3683 |
add_string_csts (constant_descriptor_tree **slot, |
| 3684 |
asan_add_string_csts_data *aascd) |
| 3685 |
{ |
| 3686 |
struct constant_descriptor_tree *desc = *slot; |
| 3687 |
if (TREE_CODE (desc->value) == STRING_CST |
| 3688 |
&& TREE_ASM_WRITTEN (desc->value) |
| 3689 |
&& asan_protect_global (desc->value)) |
| 3690 |
{ |
| 3691 |
asan_add_global (SYMBOL_REF_DECL (XEXP (desc->rtl, 0)), |
| 3692 |
aascd->type, aascd->v); |
| 3693 |
} |
| 3694 |
return 1; |
| 3695 |
} |
| 3696 |
|
| 3697 |
/* Needs to be GTY(()), because cgraph_build_static_cdtor may |
| 3698 |
invoke ggc_collect. */ |
| 3699 |
static GTY(()) tree asan_ctor_statements; |
| 3700 |
|
| 3701 |
/* Module-level instrumentation. |
| 3702 |
- Insert __asan_init_vN() into the list of CTORs. |
| 3703 |
- TODO: insert redzones around globals. |
| 3704 |
*/ |
| 3705 |
|
| 3706 |
void |
| 3707 |
asan_finish_file (void) |
| 3708 |
{ |
| 3709 |
varpool_node *vnode; |
| 3710 |
unsigned HOST_WIDE_INT gcount = 0; |
| 3711 |
|
| 3712 |
if (shadow_ptr_types[0] == NULL_TREE) |
| 3713 |
asan_init_shadow_ptr_types (); |
| 3714 |
/* Avoid instrumenting code in the asan ctors/dtors. |
| 3715 |
We don't need to insert padding after the description strings, |
| 3716 |
nor after .LASAN* array. */ |
| 3717 |
flag_sanitize &= ~SANITIZE_ADDRESS; |
| 3718 |
|
| 3719 |
/* For user-space we want asan constructors to run first. |
| 3720 |
Linux kernel does not support priorities other than default, and the only |
| 3721 |
other user of constructors is coverage. So we run with the default |
| 3722 |
priority. */ |
| 3723 |
int priority = flag_sanitize & SANITIZE_USER_ADDRESS |
| 3724 |
? MAX_RESERVED_INIT_PRIORITY - 1 : DEFAULT_INIT_PRIORITY; |
| 3725 |
|
| 3726 |
if (flag_sanitize & SANITIZE_USER_ADDRESS) |
| 3727 |
{ |
| 3728 |
tree fn = builtin_decl_implicit (BUILT_IN_ASAN_INIT); |
| 3729 |
append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements); |
| 3730 |
fn = builtin_decl_implicit (BUILT_IN_ASAN_VERSION_MISMATCH_CHECK); |
| 3731 |
append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements); |
| 3732 |
} |
| 3733 |
FOR_EACH_DEFINED_VARIABLE (vnode) |
| 3734 |
if (TREE_ASM_WRITTEN (vnode->decl) |
| 3735 |
&& asan_protect_global (vnode->decl)) |
| 3736 |
++gcount; |
| 3737 |
hash_table<tree_descriptor_hasher> *const_desc_htab = constant_pool_htab (); |
| 3738 |
const_desc_htab->traverse<unsigned HOST_WIDE_INT *, count_string_csts> |
| 3739 |
(&gcount); |
| 3740 |
if (gcount) |
| 3741 |
{ |
| 3742 |
tree type = asan_global_struct (), var, ctor; |
| 3743 |
tree dtor_statements = NULL_TREE; |
| 3744 |
vec<constructor_elt, va_gc> *v; |
| 3745 |
char buf[20]; |
| 3746 |
|
| 3747 |
type = build_array_type_nelts (type, gcount); |
| 3748 |
ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", 0); |
| 3749 |
var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf), |
| 3750 |
type); |
| 3751 |
TREE_STATIC (var) = 1; |
| 3752 |
TREE_PUBLIC (var) = 0; |
| 3753 |
DECL_ARTIFICIAL (var) = 1; |
| 3754 |
DECL_IGNORED_P (var) = 1; |
| 3755 |
vec_alloc (v, gcount); |
| 3756 |
FOR_EACH_DEFINED_VARIABLE (vnode) |
| 3757 |
if (TREE_ASM_WRITTEN (vnode->decl) |
| 3758 |
&& asan_protect_global (vnode->decl)) |
| 3759 |
asan_add_global (vnode->decl, TREE_TYPE (type), v); |
| 3760 |
struct asan_add_string_csts_data aascd; |
| 3761 |
aascd.type = TREE_TYPE (type); |
| 3762 |
aascd.v = v; |
| 3763 |
const_desc_htab->traverse<asan_add_string_csts_data *, add_string_csts> |
| 3764 |
(&aascd); |
| 3765 |
ctor = build_constructor (type, v); |
| 3766 |
TREE_CONSTANT (ctor) = 1; |
| 3767 |
TREE_STATIC (ctor) = 1; |
| 3768 |
DECL_INITIAL (var) = ctor; |
| 3769 |
SET_DECL_ALIGN (var, MAX (DECL_ALIGN (var), |
| 3770 |
ASAN_SHADOW_GRANULARITY * BITS_PER_UNIT)); |
| 3771 |
|
| 3772 |
varpool_node::finalize_decl (var); |
| 3773 |
|
| 3774 |
tree fn = builtin_decl_implicit (BUILT_IN_ASAN_REGISTER_GLOBALS); |
| 3775 |
tree gcount_tree = build_int_cst (pointer_sized_int_node, gcount); |
| 3776 |
append_to_statement_list (build_call_expr (fn, 2, |
| 3777 |
build_fold_addr_expr (var), |
| 3778 |
gcount_tree), |
| 3779 |
&asan_ctor_statements); |
| 3780 |
|
| 3781 |
fn = builtin_decl_implicit (BUILT_IN_ASAN_UNREGISTER_GLOBALS); |
| 3782 |
append_to_statement_list (build_call_expr (fn, 2, |
| 3783 |
build_fold_addr_expr (var), |
| 3784 |
gcount_tree), |
| 3785 |
&dtor_statements); |
| 3786 |
cgraph_build_static_cdtor ('D', dtor_statements, priority); |
| 3787 |
} |
| 3788 |
if (asan_ctor_statements) |
| 3789 |
cgraph_build_static_cdtor ('I', asan_ctor_statements, priority); |
| 3790 |
flag_sanitize |= SANITIZE_ADDRESS; |
| 3791 |
} |
| 3792 |
|
| 3793 |
/* Poison or unpoison (depending on IS_CLOBBER variable) shadow memory based |
| 3794 |
on SHADOW address. Newly added statements will be added to ITER with |
| 3795 |
given location LOC. We mark SIZE bytes in shadow memory, where |
| 3796 |
LAST_CHUNK_SIZE is greater than zero in situation where we are at the |
| 3797 |
end of a variable. */ |
| 3798 |
|
| 3799 |
static void |
| 3800 |
asan_store_shadow_bytes (gimple_stmt_iterator *iter, location_t loc, |
| 3801 |
tree shadow, |
| 3802 |
unsigned HOST_WIDE_INT base_addr_offset, |
| 3803 |
bool is_clobber, unsigned size, |
| 3804 |
unsigned last_chunk_size) |
| 3805 |
{ |
| 3806 |
tree shadow_ptr_type; |
| 3807 |
|
| 3808 |
switch (size) |
| 3809 |
{ |
| 3810 |
case 1: |
| 3811 |
shadow_ptr_type = shadow_ptr_types[0]; |
| 3812 |
break; |
| 3813 |
case 2: |
| 3814 |
shadow_ptr_type = shadow_ptr_types[1]; |
| 3815 |
break; |
| 3816 |
case 4: |
| 3817 |
shadow_ptr_type = shadow_ptr_types[2]; |
| 3818 |
break; |
| 3819 |
default: |
| 3820 |
gcc_unreachable (); |
| 3821 |
} |
| 3822 |
|
| 3823 |
unsigned char c = (char) is_clobber ? ASAN_STACK_MAGIC_USE_AFTER_SCOPE : 0; |
| 3824 |
unsigned HOST_WIDE_INT val = 0; |
| 3825 |
unsigned last_pos = size; |
| 3826 |
if (last_chunk_size && !is_clobber) |
| 3827 |
last_pos = BYTES_BIG_ENDIAN ? 0 : size - 1; |
| 3828 |
for (unsigned i = 0; i < size; ++i) |
| 3829 |
{ |
| 3830 |
unsigned char shadow_c = c; |
| 3831 |
if (i == last_pos) |
| 3832 |
shadow_c = last_chunk_size; |
| 3833 |
val |= (unsigned HOST_WIDE_INT) shadow_c << (BITS_PER_UNIT * i); |
| 3834 |
} |
| 3835 |
|
| 3836 |
/* Handle last chunk in unpoisoning. */ |
| 3837 |
tree magic = build_int_cst (TREE_TYPE (shadow_ptr_type), val); |
| 3838 |
|
| 3839 |
tree dest = build2 (MEM_REF, TREE_TYPE (shadow_ptr_type), shadow, |
| 3840 |
build_int_cst (shadow_ptr_type, base_addr_offset)); |
| 3841 |
|
| 3842 |
gimple *g = gimple_build_assign (dest, magic); |
| 3843 |
gimple_set_location (g, loc); |
| 3844 |
gsi_insert_after (iter, g, GSI_NEW_STMT); |
| 3845 |
} |
| 3846 |
|
| 3847 |
/* Expand the ASAN_MARK builtins. */ |
| 3848 |
|
| 3849 |
bool |
| 3850 |
asan_expand_mark_ifn (gimple_stmt_iterator *iter) |
| 3851 |
{ |
| 3852 |
gimple *g = gsi_stmt (*iter); |
| 3853 |
location_t loc = gimple_location (g); |
| 3854 |
HOST_WIDE_INT flag = tree_to_shwi (gimple_call_arg (g, 0)); |
| 3855 |
bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON; |
| 3856 |
|
| 3857 |
tree base = gimple_call_arg (g, 1); |
| 3858 |
gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR); |
| 3859 |
tree decl = TREE_OPERAND (base, 0); |
| 3860 |
|
| 3861 |
/* For a nested function, we can have: ASAN_MARK (2, &FRAME.2.fp_input, 4) */ |
| 3862 |
if (TREE_CODE (decl) == COMPONENT_REF |
| 3863 |
&& DECL_NONLOCAL_FRAME (TREE_OPERAND (decl, 0))) |
| 3864 |
decl = TREE_OPERAND (decl, 0); |
| 3865 |
|
| 3866 |
gcc_checking_assert (TREE_CODE (decl) == VAR_DECL); |
| 3867 |
|
| 3868 |
if (hwasan_sanitize_p ()) |
| 3869 |
{ |
| 3870 |
gcc_assert (param_hwasan_instrument_stack); |
| 3871 |
gimple_seq stmts = NULL; |
| 3872 |
/* Here we swap ASAN_MARK calls for HWASAN_MARK. |
| 3873 |
This is because we are using the approach of using ASAN_MARK as a |
| 3874 |
synonym until here. |
| 3875 |
That approach means we don't yet have to duplicate all the special |
| 3876 |
cases for ASAN_MARK and ASAN_POISON with the exact same handling but |
| 3877 |
called HWASAN_MARK etc. |
| 3878 |
|
| 3879 |
N.b. __asan_poison_stack_memory (which implements ASAN_MARK for ASAN) |
| 3880 |
rounds the size up to its shadow memory granularity, while |
| 3881 |
__hwasan_tag_memory (which implements the same for HWASAN) does not. |
| 3882 |
Hence we emit HWASAN_MARK with an aligned size unlike ASAN_MARK. */ |
| 3883 |
tree len = gimple_call_arg (g, 2); |
| 3884 |
tree new_len = gimple_build_round_up (&stmts, loc, size_type_node, len, |
| 3885 |
HWASAN_TAG_GRANULE_SIZE); |
| 3886 |
gimple_build (&stmts, loc, CFN_HWASAN_MARK, |
| 3887 |
void_type_node, gimple_call_arg (g, 0), |
| 3888 |
base, new_len); |
| 3889 |
gsi_replace_with_seq (iter, stmts, true); |
| 3890 |
return false; |
| 3891 |
} |
| 3892 |
|
| 3893 |
if (is_poison) |
| 3894 |
{ |
| 3895 |
if (asan_handled_variables == NULL) |
| 3896 |
asan_handled_variables = new hash_set<tree> (16); |
| 3897 |
asan_handled_variables->add (decl); |
| 3898 |
} |
| 3899 |
tree len = gimple_call_arg (g, 2); |
| 3900 |
|
| 3901 |
gcc_assert (poly_int_tree_p (len)); |
| 3902 |
|
| 3903 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 3904 |
NOP_EXPR, base); |
| 3905 |
gimple_set_location (g, loc); |
| 3906 |
gsi_replace (iter, g, false); |
| 3907 |
tree base_addr = gimple_assign_lhs (g); |
| 3908 |
|
| 3909 |
/* Generate direct emission if size_in_bytes is small. */ |
| 3910 |
unsigned threshold = param_use_after_scope_direct_emission_threshold; |
| 3911 |
if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) <= threshold) |
| 3912 |
{ |
| 3913 |
unsigned HOST_WIDE_INT size_in_bytes = tree_to_uhwi (len); |
| 3914 |
const unsigned HOST_WIDE_INT shadow_size |
| 3915 |
= shadow_mem_size (size_in_bytes); |
| 3916 |
const unsigned int shadow_align |
| 3917 |
= (get_pointer_alignment (base) / BITS_PER_UNIT) >> ASAN_SHADOW_SHIFT; |
| 3918 |
|
| 3919 |
tree shadow = build_shadow_mem_access (iter, loc, base_addr, |
| 3920 |
shadow_ptr_types[0], true); |
| 3921 |
|
| 3922 |
for (unsigned HOST_WIDE_INT offset = 0; offset < shadow_size;) |
| 3923 |
{ |
| 3924 |
unsigned size = 1; |
| 3925 |
if (shadow_size - offset >= 4 |
| 3926 |
&& (!STRICT_ALIGNMENT || shadow_align >= 4)) |
| 3927 |
size = 4; |
| 3928 |
else if (shadow_size - offset >= 2 |
| 3929 |
&& (!STRICT_ALIGNMENT || shadow_align >= 2)) |
| 3930 |
size = 2; |
| 3931 |
|
| 3932 |
unsigned HOST_WIDE_INT last_chunk_size = 0; |
| 3933 |
unsigned HOST_WIDE_INT s = (offset + size) * ASAN_SHADOW_GRANULARITY; |
| 3934 |
if (s > size_in_bytes) |
| 3935 |
last_chunk_size = ASAN_SHADOW_GRANULARITY - (s - size_in_bytes); |
| 3936 |
|
| 3937 |
asan_store_shadow_bytes (iter, loc, shadow, offset, is_poison, |
| 3938 |
size, last_chunk_size); |
| 3939 |
offset += size; |
| 3940 |
} |
| 3941 |
} |
| 3942 |
else |
| 3943 |
{ |
| 3944 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 3945 |
NOP_EXPR, len); |
| 3946 |
gimple_set_location (g, loc); |
| 3947 |
gsi_safe_insert_before (iter, g); |
| 3948 |
tree sz_arg = gimple_assign_lhs (g); |
| 3949 |
|
| 3950 |
tree fun |
| 3951 |
= builtin_decl_implicit (is_poison ? BUILT_IN_ASAN_POISON_STACK_MEMORY |
| 3952 |
: BUILT_IN_ASAN_UNPOISON_STACK_MEMORY); |
| 3953 |
g = gimple_build_call (fun, 2, base_addr, sz_arg); |
| 3954 |
gimple_set_location (g, loc); |
| 3955 |
gsi_insert_after (iter, g, GSI_NEW_STMT); |
| 3956 |
} |
| 3957 |
|
| 3958 |
return false; |
| 3959 |
} |
| 3960 |
|
| 3961 |
/* Expand the ASAN_{LOAD,STORE} builtins. */ |
| 3962 |
|
| 3963 |
bool |
| 3964 |
asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls) |
| 3965 |
{ |
| 3966 |
gcc_assert (!hwasan_sanitize_p ()); |
| 3967 |
gimple *g = gsi_stmt (*iter); |
| 3968 |
location_t loc = gimple_location (g); |
| 3969 |
bool recover_p; |
| 3970 |
if (flag_sanitize & SANITIZE_USER_ADDRESS) |
| 3971 |
recover_p = (flag_sanitize_recover & SANITIZE_USER_ADDRESS) != 0; |
| 3972 |
else |
| 3973 |
recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0; |
| 3974 |
|
| 3975 |
HOST_WIDE_INT flags = tree_to_shwi (gimple_call_arg (g, 0)); |
| 3976 |
gcc_assert (flags < ASAN_CHECK_LAST); |
| 3977 |
bool is_scalar_access = (flags & ASAN_CHECK_SCALAR_ACCESS) != 0; |
| 3978 |
bool is_store = (flags & ASAN_CHECK_STORE) != 0; |
| 3979 |
bool is_non_zero_len = (flags & ASAN_CHECK_NON_ZERO_LEN) != 0; |
| 3980 |
|
| 3981 |
tree base = gimple_call_arg (g, 1); |
| 3982 |
tree len = gimple_call_arg (g, 2); |
| 3983 |
HOST_WIDE_INT align = tree_to_shwi (gimple_call_arg (g, 3)); |
| 3984 |
|
| 3985 |
HOST_WIDE_INT size_in_bytes |
| 3986 |
= is_scalar_access && tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; |
| 3987 |
|
| 3988 |
if (use_calls) |
| 3989 |
{ |
| 3990 |
/* Instrument using callbacks. */ |
| 3991 |
gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 3992 |
NOP_EXPR, base); |
| 3993 |
gimple_set_location (g, loc); |
| 3994 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 3995 |
tree base_addr = gimple_assign_lhs (g); |
| 3996 |
|
| 3997 |
int nargs; |
| 3998 |
tree fun = check_func (is_store, recover_p, size_in_bytes, &nargs); |
| 3999 |
if (nargs == 1) |
| 4000 |
g = gimple_build_call (fun, 1, base_addr); |
| 4001 |
else |
| 4002 |
{ |
| 4003 |
gcc_assert (nargs == 2); |
| 4004 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 4005 |
NOP_EXPR, len); |
| 4006 |
gimple_set_location (g, loc); |
| 4007 |
gsi_insert_before (iter, g, GSI_SAME_STMT); |
| 4008 |
tree sz_arg = gimple_assign_lhs (g); |
| 4009 |
g = gimple_build_call (fun, nargs, base_addr, sz_arg); |
| 4010 |
} |
| 4011 |
gimple_set_location (g, loc); |
| 4012 |
gsi_replace (iter, g, false); |
| 4013 |
return false; |
| 4014 |
} |
| 4015 |
|
| 4016 |
HOST_WIDE_INT real_size_in_bytes = size_in_bytes == -1 ? 1 : size_in_bytes; |
| 4017 |
|
| 4018 |
tree shadow_ptr_type = shadow_ptr_types[real_size_in_bytes == 16 ? 1 : 0]; |
| 4019 |
tree shadow_type = TREE_TYPE (shadow_ptr_type); |
| 4020 |
|
| 4021 |
gimple_stmt_iterator gsi = *iter; |
| 4022 |
|
| 4023 |
if (!is_non_zero_len) |
| 4024 |
{ |
| 4025 |
/* So, the length of the memory area to asan-protect is |
| 4026 |
non-constant. Let's guard the generated instrumentation code |
| 4027 |
like: |
| 4028 |
|
| 4029 |
if (len != 0) |
| 4030 |
{ |
| 4031 |
//asan instrumentation code goes here. |
| 4032 |
} |
| 4033 |
// falltrough instructions, starting with *ITER. */ |
| 4034 |
|
| 4035 |
g = gimple_build_cond (NE_EXPR, |
| 4036 |
len, |
| 4037 |
build_int_cst (TREE_TYPE (len), 0), |
| 4038 |
NULL_TREE, NULL_TREE); |
| 4039 |
gimple_set_location (g, loc); |
| 4040 |
|
| 4041 |
basic_block then_bb, fallthrough_bb; |
| 4042 |
insert_if_then_before_iter (as_a <gcond *> (g), iter, |
| 4043 |
/*then_more_likely_p=*/true, |
| 4044 |
&then_bb, &fallthrough_bb); |
| 4045 |
/* Note that fallthrough_bb starts with the statement that was |
| 4046 |
pointed to by ITER. */ |
| 4047 |
|
| 4048 |
/* The 'then block' of the 'if (len != 0) condition is where |
| 4049 |
we'll generate the asan instrumentation code now. */ |
| 4050 |
gsi = gsi_last_bb (then_bb); |
| 4051 |
} |
| 4052 |
|
| 4053 |
/* Get an iterator on the point where we can add the condition |
| 4054 |
statement for the instrumentation. */ |
| 4055 |
basic_block then_bb, else_bb; |
| 4056 |
gsi = create_cond_insert_point (&gsi, /*before_p*/false, |
| 4057 |
/*then_more_likely_p=*/false, |
| 4058 |
/*create_then_fallthru_edge*/recover_p, |
| 4059 |
&then_bb, |
| 4060 |
&else_bb); |
| 4061 |
|
| 4062 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 4063 |
NOP_EXPR, base); |
| 4064 |
gimple_set_location (g, loc); |
| 4065 |
gsi_insert_before (&gsi, g, GSI_NEW_STMT); |
| 4066 |
tree base_addr = gimple_assign_lhs (g); |
| 4067 |
|
| 4068 |
tree t = NULL_TREE; |
| 4069 |
if (real_size_in_bytes >= 8) |
| 4070 |
{ |
| 4071 |
tree shadow = build_shadow_mem_access (&gsi, loc, base_addr, |
| 4072 |
shadow_ptr_type); |
| 4073 |
t = shadow; |
| 4074 |
} |
| 4075 |
else |
| 4076 |
{ |
| 4077 |
/* Slow path for 1, 2 and 4 byte accesses. */ |
| 4078 |
/* Test (shadow != 0) |
| 4079 |
& ((base_addr & 7) + (real_size_in_bytes - 1)) >= shadow). */ |
| 4080 |
tree shadow = build_shadow_mem_access (&gsi, loc, base_addr, |
| 4081 |
shadow_ptr_type); |
| 4082 |
gimple *shadow_test = build_assign (NE_EXPR, shadow, 0); |
| 4083 |
gimple_seq seq = NULL; |
| 4084 |
gimple_seq_add_stmt (&seq, shadow_test); |
| 4085 |
/* Aligned (>= 8 bytes) can test just |
| 4086 |
(real_size_in_bytes - 1 >= shadow), as base_addr & 7 is known |
| 4087 |
to be 0. */ |
| 4088 |
if (align < 8) |
| 4089 |
{ |
| 4090 |
gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, |
| 4091 |
base_addr, 7)); |
| 4092 |
gimple_seq_add_stmt (&seq, |
| 4093 |
build_type_cast (shadow_type, |
| 4094 |
gimple_seq_last (seq))); |
| 4095 |
if (real_size_in_bytes > 1) |
| 4096 |
gimple_seq_add_stmt (&seq, |
| 4097 |
build_assign (PLUS_EXPR, |
| 4098 |
gimple_seq_last (seq), |
| 4099 |
real_size_in_bytes - 1)); |
| 4100 |
t = gimple_assign_lhs (gimple_seq_last_stmt (seq)); |
| 4101 |
} |
| 4102 |
else |
| 4103 |
t = build_int_cst (shadow_type, real_size_in_bytes - 1); |
| 4104 |
gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, t, shadow)); |
| 4105 |
gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, |
| 4106 |
gimple_seq_last (seq))); |
| 4107 |
t = gimple_assign_lhs (gimple_seq_last (seq)); |
| 4108 |
gimple_seq_set_location (seq, loc); |
| 4109 |
gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); |
| 4110 |
|
| 4111 |
/* For non-constant, misaligned or otherwise weird access sizes, |
| 4112 |
check first and last byte. */ |
| 4113 |
if (size_in_bytes == -1) |
| 4114 |
{ |
| 4115 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 4116 |
MINUS_EXPR, len, |
| 4117 |
build_int_cst (pointer_sized_int_node, 1)); |
| 4118 |
gimple_set_location (g, loc); |
| 4119 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 4120 |
tree last = gimple_assign_lhs (g); |
| 4121 |
g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 4122 |
PLUS_EXPR, base_addr, last); |
| 4123 |
gimple_set_location (g, loc); |
| 4124 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 4125 |
tree base_end_addr = gimple_assign_lhs (g); |
| 4126 |
|
| 4127 |
tree shadow = build_shadow_mem_access (&gsi, loc, base_end_addr, |
| 4128 |
shadow_ptr_type); |
| 4129 |
gimple *shadow_test = build_assign (NE_EXPR, shadow, 0); |
| 4130 |
gimple_seq seq = NULL; |
| 4131 |
gimple_seq_add_stmt (&seq, shadow_test); |
| 4132 |
gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, |
| 4133 |
base_end_addr, 7)); |
| 4134 |
gimple_seq_add_stmt (&seq, build_type_cast (shadow_type, |
| 4135 |
gimple_seq_last (seq))); |
| 4136 |
gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, |
| 4137 |
gimple_seq_last (seq), |
| 4138 |
shadow)); |
| 4139 |
gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, |
| 4140 |
gimple_seq_last (seq))); |
| 4141 |
gimple_seq_add_stmt (&seq, build_assign (BIT_IOR_EXPR, t, |
| 4142 |
gimple_seq_last (seq))); |
| 4143 |
t = gimple_assign_lhs (gimple_seq_last (seq)); |
| 4144 |
gimple_seq_set_location (seq, loc); |
| 4145 |
gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); |
| 4146 |
} |
| 4147 |
} |
| 4148 |
|
| 4149 |
g = gimple_build_cond (NE_EXPR, t, build_int_cst (TREE_TYPE (t), 0), |
| 4150 |
NULL_TREE, NULL_TREE); |
| 4151 |
gimple_set_location (g, loc); |
| 4152 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 4153 |
|
| 4154 |
/* Generate call to the run-time library (e.g. __asan_report_load8). */ |
| 4155 |
gsi = gsi_start_bb (then_bb); |
| 4156 |
int nargs; |
| 4157 |
tree fun = report_error_func (is_store, recover_p, size_in_bytes, &nargs); |
| 4158 |
g = gimple_build_call (fun, nargs, base_addr, len); |
| 4159 |
gimple_set_location (g, loc); |
| 4160 |
gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
| 4161 |
|
| 4162 |
gsi_remove (iter, true); |
| 4163 |
*iter = gsi_start_bb (else_bb); |
| 4164 |
|
| 4165 |
return true; |
| 4166 |
} |
| 4167 |
|
| 4168 |
/* Create ASAN shadow variable for a VAR_DECL which has been rewritten |
| 4169 |
into SSA. Already seen VAR_DECLs are stored in SHADOW_VARS_MAPPING. */ |
| 4170 |
|
| 4171 |
static tree |
| 4172 |
create_asan_shadow_var (tree var_decl, |
| 4173 |
hash_map<tree, tree> &shadow_vars_mapping) |
| 4174 |
{ |
| 4175 |
tree *slot = shadow_vars_mapping.get (var_decl); |
| 4176 |
if (slot == NULL) |
| 4177 |
{ |
| 4178 |
tree shadow_var = copy_node (var_decl); |
| 4179 |
|
| 4180 |
copy_body_data id; |
| 4181 |
memset (&id, 0, sizeof (copy_body_data)); |
| 4182 |
id.src_fn = id.dst_fn = current_function_decl; |
| 4183 |
copy_decl_for_dup_finish (&id, var_decl, shadow_var); |
| 4184 |
|
| 4185 |
DECL_ARTIFICIAL (shadow_var) = 1; |
| 4186 |
DECL_IGNORED_P (shadow_var) = 1; |
| 4187 |
DECL_SEEN_IN_BIND_EXPR_P (shadow_var) = 0; |
| 4188 |
gimple_add_tmp_var (shadow_var); |
| 4189 |
|
| 4190 |
shadow_vars_mapping.put (var_decl, shadow_var); |
| 4191 |
return shadow_var; |
| 4192 |
} |
| 4193 |
else |
| 4194 |
return *slot; |
| 4195 |
} |
| 4196 |
|
| 4197 |
/* Expand ASAN_POISON ifn. */ |
| 4198 |
|
| 4199 |
bool |
| 4200 |
asan_expand_poison_ifn (gimple_stmt_iterator *iter, |
| 4201 |
bool *need_commit_edge_insert, |
| 4202 |
hash_map<tree, tree> &shadow_vars_mapping) |
| 4203 |
{ |
| 4204 |
gimple *g = gsi_stmt (*iter); |
| 4205 |
tree poisoned_var = gimple_call_lhs (g); |
| 4206 |
if (!poisoned_var || has_zero_uses (poisoned_var)) |
| 4207 |
{ |
| 4208 |
gsi_remove (iter, true); |
| 4209 |
return true; |
| 4210 |
} |
| 4211 |
|
| 4212 |
if (SSA_NAME_VAR (poisoned_var) == NULL_TREE) |
| 4213 |
SET_SSA_NAME_VAR_OR_IDENTIFIER (poisoned_var, |
| 4214 |
create_tmp_var (TREE_TYPE (poisoned_var))); |
| 4215 |
|
| 4216 |
tree shadow_var = create_asan_shadow_var (SSA_NAME_VAR (poisoned_var), |
| 4217 |
shadow_vars_mapping); |
| 4218 |
|
| 4219 |
bool recover_p; |
| 4220 |
if (flag_sanitize & SANITIZE_USER_ADDRESS) |
| 4221 |
recover_p = (flag_sanitize_recover & SANITIZE_USER_ADDRESS) != 0; |
| 4222 |
else |
| 4223 |
recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0; |
| 4224 |
tree size = DECL_SIZE_UNIT (shadow_var); |
| 4225 |
gimple *poison_call |
| 4226 |
= gimple_build_call_internal (IFN_ASAN_MARK, 3, |
| 4227 |
build_int_cst (integer_type_node, |
| 4228 |
ASAN_MARK_POISON), |
| 4229 |
build_fold_addr_expr (shadow_var), size); |
| 4230 |
|
| 4231 |
gimple *use; |
| 4232 |
imm_use_iterator imm_iter; |
| 4233 |
FOR_EACH_IMM_USE_STMT (use, imm_iter, poisoned_var) |
| 4234 |
{ |
| 4235 |
if (is_gimple_debug (use)) |
| 4236 |
continue; |
| 4237 |
|
| 4238 |
int nargs; |
| 4239 |
bool store_p = gimple_call_internal_p (use, IFN_ASAN_POISON_USE); |
| 4240 |
gcall *call; |
| 4241 |
if (hwasan_sanitize_p ()) |
| 4242 |
{ |
| 4243 |
tree fun = builtin_decl_implicit (BUILT_IN_HWASAN_TAG_MISMATCH4); |
| 4244 |
/* NOTE: hwasan has no __hwasan_report_* functions like asan does. |
| 4245 |
We use __hwasan_tag_mismatch4 with arguments that tell it the |
| 4246 |
size of access and load to report all tag mismatches. |
| 4247 |
|
| 4248 |
The arguments to this function are: |
| 4249 |
Address of invalid access. |
| 4250 |
Bitfield containing information about the access |
| 4251 |
(access_info) |
| 4252 |
Pointer to a frame of registers |
| 4253 |
(for use in printing the contents of registers in a dump) |
| 4254 |
Not used yet -- to be used by inline instrumentation. |
| 4255 |
Size of access. |
| 4256 |
|
| 4257 |
The access_info bitfield encodes the following pieces of |
| 4258 |
information: |
| 4259 |
- Is this a store or load? |
| 4260 |
access_info & 0x10 => store |
| 4261 |
- Should the program continue after reporting the error? |
| 4262 |
access_info & 0x20 => recover |
| 4263 |
- What size access is this (not used here since we can always |
| 4264 |
pass the size in the last argument) |
| 4265 |
|
| 4266 |
if (access_info & 0xf == 0xf) |
| 4267 |
size is taken from last argument. |
| 4268 |
else |
| 4269 |
size == 1 << (access_info & 0xf) |
| 4270 |
|
| 4271 |
The last argument contains the size of the access iff the |
| 4272 |
access_info size indicator is 0xf (we always use this argument |
| 4273 |
rather than storing the size in the access_info bitfield). |
| 4274 |
|
| 4275 |
See the function definition `__hwasan_tag_mismatch4` in |
| 4276 |
libsanitizer/hwasan for the full definition. |
| 4277 |
*/ |
| 4278 |
unsigned access_info = (0x20 * recover_p) |
| 4279 |
+ (0x10 * store_p) |
| 4280 |
+ (0xf); |
| 4281 |
call = gimple_build_call (fun, 4, |
| 4282 |
build_fold_addr_expr (shadow_var), |
| 4283 |
build_int_cst (pointer_sized_int_node, |
| 4284 |
access_info), |
| 4285 |
build_int_cst (pointer_sized_int_node, 0), |
| 4286 |
size); |
| 4287 |
} |
| 4288 |
else |
| 4289 |
{ |
| 4290 |
tree fun = report_error_func (store_p, recover_p, tree_to_uhwi (size), |
| 4291 |
&nargs); |
| 4292 |
call = gimple_build_call (fun, 1, |
| 4293 |
build_fold_addr_expr (shadow_var)); |
| 4294 |
} |
| 4295 |
gimple_set_location (call, gimple_location (use)); |
| 4296 |
gimple *call_to_insert = call; |
| 4297 |
|
| 4298 |
/* The USE can be a gimple PHI node. If so, insert the call on |
| 4299 |
all edges leading to the PHI node. */ |
| 4300 |
if (is_a <gphi *> (use)) |
| 4301 |
{ |
| 4302 |
gphi *phi = dyn_cast<gphi *> (use); |
| 4303 |
for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) |
| 4304 |
if (gimple_phi_arg_def (phi, i) == poisoned_var) |
| 4305 |
{ |
| 4306 |
edge e = gimple_phi_arg_edge (phi, i); |
| 4307 |
|
| 4308 |
/* Do not insert on an edge we can't split. */ |
| 4309 |
if (e->flags & EDGE_ABNORMAL) |
| 4310 |
continue; |
| 4311 |
|
| 4312 |
if (call_to_insert == NULL) |
| 4313 |
call_to_insert = gimple_copy (call); |
| 4314 |
|
| 4315 |
gsi_insert_seq_on_edge (e, call_to_insert); |
| 4316 |
*need_commit_edge_insert = true; |
| 4317 |
call_to_insert = NULL; |
| 4318 |
} |
| 4319 |
} |
| 4320 |
else |
| 4321 |
{ |
| 4322 |
gimple_stmt_iterator gsi = gsi_for_stmt (use); |
| 4323 |
if (store_p) |
| 4324 |
gsi_replace (&gsi, call, true); |
| 4325 |
else |
| 4326 |
gsi_insert_before (&gsi, call, GSI_NEW_STMT); |
| 4327 |
} |
| 4328 |
} |
| 4329 |
|
| 4330 |
SSA_NAME_IS_DEFAULT_DEF (poisoned_var) = true; |
| 4331 |
SSA_NAME_DEF_STMT (poisoned_var) = gimple_build_nop (); |
| 4332 |
gsi_replace (iter, poison_call, false); |
| 4333 |
|
| 4334 |
return true; |
| 4335 |
} |
| 4336 |
|
| 4337 |
/* Instrument the current function. */ |
| 4338 |
|
| 4339 |
static unsigned int |
| 4340 |
asan_instrument (void) |
| 4341 |
{ |
| 4342 |
if (hwasan_sanitize_p ()) |
| 4343 |
{ |
| 4344 |
initialize_sanitizer_builtins (); |
| 4345 |
transform_statements (); |
| 4346 |
return 0; |
| 4347 |
} |
| 4348 |
|
| 4349 |
if (shadow_ptr_types[0] == NULL_TREE) |
| 4350 |
asan_init_shadow_ptr_types (); |
| 4351 |
transform_statements (); |
| 4352 |
last_alloca_addr = NULL_TREE; |
| 4353 |
return 0; |
| 4354 |
} |
| 4355 |
|
| 4356 |
static bool |
| 4357 |
gate_asan (void) |
| 4358 |
{ |
| 4359 |
return sanitize_flags_p (SANITIZE_ADDRESS); |
| 4360 |
} |
| 4361 |
|
| 4362 |
namespace { |
| 4363 |
|
| 4364 |
const pass_data pass_data_asan = |
| 4365 |
{ |
| 4366 |
GIMPLE_PASS, /* type */ |
| 4367 |
"asan", /* name */ |
| 4368 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 4369 |
TV_NONE, /* tv_id */ |
| 4370 |
( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ |
| 4371 |
0, /* properties_provided */ |
| 4372 |
0, /* properties_destroyed */ |
| 4373 |
0, /* todo_flags_start */ |
| 4374 |
TODO_update_ssa, /* todo_flags_finish */ |
| 4375 |
}; |
| 4376 |
|
| 4377 |
class pass_asan : public gimple_opt_pass |
| 4378 |
{ |
| 4379 |
public: |
| 4380 |
pass_asan (gcc::context *ctxt) |
| 4381 |
: gimple_opt_pass (pass_data_asan, ctxt) |
| 4382 |
{} |
| 4383 |
|
| 4384 |
/* opt_pass methods: */ |
| 4385 |
opt_pass * clone () final override { return new pass_asan (m_ctxt); } |
| 4386 |
bool gate (function *) final override |
| 4387 |
{ |
| 4388 |
return gate_asan () || gate_hwasan (); |
| 4389 |
} |
| 4390 |
unsigned int execute (function *) final override |
| 4391 |
{ |
| 4392 |
return asan_instrument (); |
| 4393 |
} |
| 4394 |
|
| 4395 |
}; // class pass_asan |
| 4396 |
|
| 4397 |
} // anon namespace |
| 4398 |
|
| 4399 |
gimple_opt_pass * |
| 4400 |
make_pass_asan (gcc::context *ctxt) |
| 4401 |
{ |
| 4402 |
return new pass_asan (ctxt); |
| 4403 |
} |
| 4404 |
|
| 4405 |
namespace { |
| 4406 |
|
| 4407 |
const pass_data pass_data_asan_O0 = |
| 4408 |
{ |
| 4409 |
GIMPLE_PASS, /* type */ |
| 4410 |
"asan0", /* name */ |
| 4411 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 4412 |
TV_NONE, /* tv_id */ |
| 4413 |
( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ |
| 4414 |
0, /* properties_provided */ |
| 4415 |
0, /* properties_destroyed */ |
| 4416 |
0, /* todo_flags_start */ |
| 4417 |
TODO_update_ssa, /* todo_flags_finish */ |
| 4418 |
}; |
| 4419 |
|
| 4420 |
class pass_asan_O0 : public gimple_opt_pass |
| 4421 |
{ |
| 4422 |
public: |
| 4423 |
pass_asan_O0 (gcc::context *ctxt) |
| 4424 |
: gimple_opt_pass (pass_data_asan_O0, ctxt) |
| 4425 |
{} |
| 4426 |
|
| 4427 |
/* opt_pass methods: */ |
| 4428 |
bool gate (function *) final override |
| 4429 |
{ |
| 4430 |
return !optimize && (gate_asan () || gate_hwasan ()); |
| 4431 |
} |
| 4432 |
unsigned int execute (function *) final override |
| 4433 |
{ |
| 4434 |
return asan_instrument (); |
| 4435 |
} |
| 4436 |
|
| 4437 |
}; // class pass_asan_O0 |
| 4438 |
|
| 4439 |
} // anon namespace |
| 4440 |
|
| 4441 |
gimple_opt_pass * |
| 4442 |
make_pass_asan_O0 (gcc::context *ctxt) |
| 4443 |
{ |
| 4444 |
return new pass_asan_O0 (ctxt); |
| 4445 |
} |
| 4446 |
|
| 4447 |
/* HWASAN */ |
| 4448 |
|
| 4449 |
/* For stack tagging: |
| 4450 |
|
| 4451 |
Return the offset from the frame base tag that the "next" expanded object |
| 4452 |
should have. */ |
| 4453 |
uint8_t |
| 4454 |
hwasan_current_frame_tag () |
| 4455 |
{ |
| 4456 |
return hwasan_frame_tag_offset; |
| 4457 |
} |
| 4458 |
|
| 4459 |
/* For stack tagging: |
| 4460 |
|
| 4461 |
Return the 'base pointer' for this function. If that base pointer has not |
| 4462 |
yet been created then we create a register to hold it and record the insns |
| 4463 |
to initialize the register in `hwasan_frame_base_init_seq` for later |
| 4464 |
emission. */ |
| 4465 |
rtx |
| 4466 |
hwasan_frame_base () |
| 4467 |
{ |
| 4468 |
if (! hwasan_frame_base_ptr) |
| 4469 |
{ |
| 4470 |
start_sequence (); |
| 4471 |
hwasan_frame_base_ptr |
| 4472 |
= force_reg (Pmode, |
| 4473 |
targetm.memtag.insert_random_tag (virtual_stack_vars_rtx, |
| 4474 |
NULL_RTX)); |
| 4475 |
hwasan_frame_base_init_seq = get_insns (); |
| 4476 |
end_sequence (); |
| 4477 |
} |
| 4478 |
|
| 4479 |
return hwasan_frame_base_ptr; |
| 4480 |
} |
| 4481 |
|
| 4482 |
/* For stack tagging: |
| 4483 |
|
| 4484 |
Check whether this RTX is a standard pointer addressing the base of the |
| 4485 |
stack variables for this frame. Returns true if the RTX is either |
| 4486 |
virtual_stack_vars_rtx or hwasan_frame_base_ptr. */ |
| 4487 |
bool |
| 4488 |
stack_vars_base_reg_p (rtx base) |
| 4489 |
{ |
| 4490 |
return base == virtual_stack_vars_rtx || base == hwasan_frame_base_ptr; |
| 4491 |
} |
| 4492 |
|
| 4493 |
/* For stack tagging: |
| 4494 |
|
| 4495 |
Emit frame base initialisation. |
| 4496 |
If hwasan_frame_base has been used before here then |
| 4497 |
hwasan_frame_base_init_seq contains the sequence of instructions to |
| 4498 |
initialize it. This must be put just before the hwasan prologue, so we emit |
| 4499 |
the insns before parm_birth_insn (which will point to the first instruction |
| 4500 |
of the hwasan prologue if it exists). |
| 4501 |
|
| 4502 |
We update `parm_birth_insn` to point to the start of this initialisation |
| 4503 |
since that represents the end of the initialisation done by |
| 4504 |
expand_function_{start,end} functions and we want to maintain that. */ |
| 4505 |
void |
| 4506 |
hwasan_maybe_emit_frame_base_init () |
| 4507 |
{ |
| 4508 |
if (! hwasan_frame_base_init_seq) |
| 4509 |
return; |
| 4510 |
emit_insn_before (hwasan_frame_base_init_seq, parm_birth_insn); |
| 4511 |
parm_birth_insn = hwasan_frame_base_init_seq; |
| 4512 |
} |
| 4513 |
|
| 4514 |
/* Record a compile-time constant size stack variable that HWASAN will need to |
| 4515 |
tag. This record of the range of a stack variable will be used by |
| 4516 |
`hwasan_emit_prologue` to emit the RTL at the start of each frame which will |
| 4517 |
set tags in the shadow memory according to the assigned tag for each object. |
| 4518 |
|
| 4519 |
The range that the object spans in stack space should be described by the |
| 4520 |
bounds `untagged_base + nearest_offset` and |
| 4521 |
`untagged_base + farthest_offset`. |
| 4522 |
`tagged_base` is the base address which contains the "base frame tag" for |
| 4523 |
this frame, and from which the value to address this object with will be |
| 4524 |
calculated. |
| 4525 |
|
| 4526 |
We record the `untagged_base` since the functions in the hwasan library we |
| 4527 |
use to tag memory take pointers without a tag. */ |
| 4528 |
void |
| 4529 |
hwasan_record_stack_var (rtx untagged_base, rtx tagged_base, |
| 4530 |
poly_int64 nearest_offset, poly_int64 farthest_offset) |
| 4531 |
{ |
| 4532 |
hwasan_stack_var cur_var; |
| 4533 |
cur_var.untagged_base = untagged_base; |
| 4534 |
cur_var.tagged_base = tagged_base; |
| 4535 |
cur_var.nearest_offset = nearest_offset; |
| 4536 |
cur_var.farthest_offset = farthest_offset; |
| 4537 |
cur_var.tag_offset = hwasan_current_frame_tag (); |
| 4538 |
|
| 4539 |
hwasan_tagged_stack_vars.safe_push (cur_var); |
| 4540 |
} |
| 4541 |
|
| 4542 |
/* Return the RTX representing the farthest extent of the statically allocated |
| 4543 |
stack objects for this frame. If hwasan_frame_base_ptr has not been |
| 4544 |
initialized then we are not storing any static variables on the stack in |
| 4545 |
this frame. In this case we return NULL_RTX to represent that. |
| 4546 |
|
| 4547 |
Otherwise simply return virtual_stack_vars_rtx + frame_offset. */ |
| 4548 |
rtx |
| 4549 |
hwasan_get_frame_extent () |
| 4550 |
{ |
| 4551 |
return (hwasan_frame_base_ptr |
| 4552 |
? plus_constant (Pmode, virtual_stack_vars_rtx, frame_offset) |
| 4553 |
: NULL_RTX); |
| 4554 |
} |
| 4555 |
|
| 4556 |
/* For stack tagging: |
| 4557 |
|
| 4558 |
Increment the frame tag offset modulo the size a tag can represent. */ |
| 4559 |
void |
| 4560 |
hwasan_increment_frame_tag () |
| 4561 |
{ |
| 4562 |
uint8_t tag_bits = HWASAN_TAG_SIZE; |
| 4563 |
gcc_assert (HWASAN_TAG_SIZE |
| 4564 |
<= sizeof (hwasan_frame_tag_offset) * CHAR_BIT); |
| 4565 |
hwasan_frame_tag_offset = (hwasan_frame_tag_offset + 1) % (1 << tag_bits); |
| 4566 |
/* The "background tag" of the stack is zero by definition. |
| 4567 |
This is the tag that objects like parameters passed on the stack and |
| 4568 |
spilled registers are given. It is handy to avoid this tag for objects |
| 4569 |
whose tags we decide ourselves, partly to ensure that buffer overruns |
| 4570 |
can't affect these important variables (e.g. saved link register, saved |
| 4571 |
stack pointer etc) and partly to make debugging easier (everything with a |
| 4572 |
tag of zero is space allocated automatically by the compiler). |
| 4573 |
|
| 4574 |
This is not feasible when using random frame tags (the default |
| 4575 |
configuration for hwasan) since the tag for the given frame is randomly |
| 4576 |
chosen at runtime. In order to avoid any tags matching the stack |
| 4577 |
background we would need to decide tag offsets at runtime instead of |
| 4578 |
compile time (and pay the resulting performance cost). |
| 4579 |
|
| 4580 |
When not using random base tags for each frame (i.e. when compiled with |
| 4581 |
`--param hwasan-random-frame-tag=0`) the base tag for each frame is zero. |
| 4582 |
This means the tag that each object gets is equal to the |
| 4583 |
hwasan_frame_tag_offset used in determining it. |
| 4584 |
When this is the case we *can* ensure no object gets the tag of zero by |
| 4585 |
simply ensuring no object has the hwasan_frame_tag_offset of zero. |
| 4586 |
|
| 4587 |
There is the extra complication that we only record the |
| 4588 |
hwasan_frame_tag_offset here (which is the offset from the tag stored in |
| 4589 |
the stack pointer). In the kernel, the tag in the stack pointer is 0xff |
| 4590 |
rather than zero. This does not cause problems since tags of 0xff are |
| 4591 |
never checked in the kernel. As mentioned at the beginning of this |
| 4592 |
comment the background tag of the stack is zero by definition, which means |
| 4593 |
that for the kernel we should skip offsets of both 0 and 1 from the stack |
| 4594 |
pointer. Avoiding the offset of 0 ensures we use a tag which will be |
| 4595 |
checked, avoiding the offset of 1 ensures we use a tag that is not the |
| 4596 |
same as the background. */ |
| 4597 |
if (hwasan_frame_tag_offset == 0 && ! param_hwasan_random_frame_tag) |
| 4598 |
hwasan_frame_tag_offset += 1; |
| 4599 |
if (hwasan_frame_tag_offset == 1 && ! param_hwasan_random_frame_tag |
| 4600 |
&& sanitize_flags_p (SANITIZE_KERNEL_HWADDRESS)) |
| 4601 |
hwasan_frame_tag_offset += 1; |
| 4602 |
} |
| 4603 |
|
| 4604 |
/* Clear internal state for the next function. |
| 4605 |
This function is called before variables on the stack get expanded, in |
| 4606 |
`init_vars_expansion`. */ |
| 4607 |
void |
| 4608 |
hwasan_record_frame_init () |
| 4609 |
{ |
| 4610 |
delete asan_used_labels; |
| 4611 |
asan_used_labels = NULL; |
| 4612 |
|
| 4613 |
/* If this isn't the case then some stack variable was recorded *before* |
| 4614 |
hwasan_record_frame_init is called, yet *after* the hwasan prologue for |
| 4615 |
the previous frame was emitted. Such stack variables would not have |
| 4616 |
their shadow stack filled in. */ |
| 4617 |
gcc_assert (hwasan_tagged_stack_vars.is_empty ()); |
| 4618 |
hwasan_frame_base_ptr = NULL_RTX; |
| 4619 |
hwasan_frame_base_init_seq = NULL; |
| 4620 |
|
| 4621 |
/* When not using a random frame tag we can avoid the background stack |
| 4622 |
color which gives the user a little better debug output upon a crash. |
| 4623 |
Meanwhile, when using a random frame tag it will be nice to avoid adding |
| 4624 |
tags for the first object since that is unnecessary extra work. |
| 4625 |
Hence set the initial hwasan_frame_tag_offset to be 0 if using a random |
| 4626 |
frame tag and 1 otherwise. |
| 4627 |
|
| 4628 |
As described in hwasan_increment_frame_tag, in the kernel the stack |
| 4629 |
pointer has the tag 0xff. That means that to avoid 0xff and 0 (the tag |
| 4630 |
which the kernel does not check and the background tag respectively) we |
| 4631 |
start with a tag offset of 2. */ |
| 4632 |
hwasan_frame_tag_offset = param_hwasan_random_frame_tag |
| 4633 |
? 0 |
| 4634 |
: sanitize_flags_p (SANITIZE_KERNEL_HWADDRESS) ? 2 : 1; |
| 4635 |
} |
| 4636 |
|
| 4637 |
/* For stack tagging: |
| 4638 |
(Emits HWASAN equivalent of what is emitted by |
| 4639 |
`asan_emit_stack_protection`). |
| 4640 |
|
| 4641 |
Emits the extra prologue code to set the shadow stack as required for HWASAN |
| 4642 |
stack instrumentation. |
| 4643 |
|
| 4644 |
Uses the vector of recorded stack variables hwasan_tagged_stack_vars. When |
| 4645 |
this function has completed hwasan_tagged_stack_vars is empty and all |
| 4646 |
objects it had pointed to are deallocated. */ |
| 4647 |
void |
| 4648 |
hwasan_emit_prologue () |
| 4649 |
{ |
| 4650 |
/* We need untagged base pointers since libhwasan only accepts untagged |
| 4651 |
pointers in __hwasan_tag_memory. We need the tagged base pointer to obtain |
| 4652 |
the base tag for an offset. */ |
| 4653 |
|
| 4654 |
if (hwasan_tagged_stack_vars.is_empty ()) |
| 4655 |
return; |
| 4656 |
|
| 4657 |
poly_int64 bot = 0, top = 0; |
| 4658 |
for (hwasan_stack_var &cur : hwasan_tagged_stack_vars) |
| 4659 |
{ |
| 4660 |
poly_int64 nearest = cur.nearest_offset; |
| 4661 |
poly_int64 farthest = cur.farthest_offset; |
| 4662 |
|
| 4663 |
if (known_ge (nearest, farthest)) |
| 4664 |
{ |
| 4665 |
top = nearest; |
| 4666 |
bot = farthest; |
| 4667 |
} |
| 4668 |
else |
| 4669 |
{ |
| 4670 |
/* Given how these values are calculated, one must be known greater |
| 4671 |
than the other. */ |
| 4672 |
gcc_assert (known_le (nearest, farthest)); |
| 4673 |
top = farthest; |
| 4674 |
bot = nearest; |
| 4675 |
} |
| 4676 |
poly_int64 size = (top - bot); |
| 4677 |
|
| 4678 |
/* Assert the edge of each variable is aligned to the HWASAN tag granule |
| 4679 |
size. */ |
| 4680 |
gcc_assert (multiple_p (top, HWASAN_TAG_GRANULE_SIZE)); |
| 4681 |
gcc_assert (multiple_p (bot, HWASAN_TAG_GRANULE_SIZE)); |
| 4682 |
gcc_assert (multiple_p (size, HWASAN_TAG_GRANULE_SIZE)); |
| 4683 |
|
| 4684 |
rtx fn = init_one_libfunc ("__hwasan_tag_memory"); |
| 4685 |
rtx base_tag = targetm.memtag.extract_tag (cur.tagged_base, NULL_RTX); |
| 4686 |
rtx tag = plus_constant (QImode, base_tag, cur.tag_offset); |
| 4687 |
tag = hwasan_truncate_to_tag_size (tag, NULL_RTX); |
| 4688 |
|
| 4689 |
rtx bottom = convert_memory_address (ptr_mode, |
| 4690 |
plus_constant (Pmode, |
| 4691 |
cur.untagged_base, |
| 4692 |
bot)); |
| 4693 |
emit_library_call (fn, LCT_NORMAL, VOIDmode, |
| 4694 |
bottom, ptr_mode, |
| 4695 |
tag, QImode, |
| 4696 |
gen_int_mode (size, ptr_mode), ptr_mode); |
| 4697 |
} |
| 4698 |
/* Clear the stack vars, we've emitted the prologue for them all now. */ |
| 4699 |
hwasan_tagged_stack_vars.truncate (0); |
| 4700 |
} |
| 4701 |
|
| 4702 |
/* For stack tagging: |
| 4703 |
|
| 4704 |
Return RTL insns to clear the tags between DYNAMIC and VARS pointers |
| 4705 |
into the stack. These instructions should be emitted at the end of |
| 4706 |
every function. |
| 4707 |
|
| 4708 |
If `dynamic` is NULL_RTX then no insns are returned. */ |
| 4709 |
rtx_insn * |
| 4710 |
hwasan_emit_untag_frame (rtx dynamic, rtx vars) |
| 4711 |
{ |
| 4712 |
if (! dynamic) |
| 4713 |
return NULL; |
| 4714 |
|
| 4715 |
start_sequence (); |
| 4716 |
|
| 4717 |
dynamic = convert_memory_address (ptr_mode, dynamic); |
| 4718 |
vars = convert_memory_address (ptr_mode, vars); |
| 4719 |
|
| 4720 |
rtx top_rtx; |
| 4721 |
rtx bot_rtx; |
| 4722 |
if (FRAME_GROWS_DOWNWARD) |
| 4723 |
{ |
| 4724 |
top_rtx = vars; |
| 4725 |
bot_rtx = dynamic; |
| 4726 |
} |
| 4727 |
else |
| 4728 |
{ |
| 4729 |
top_rtx = dynamic; |
| 4730 |
bot_rtx = vars; |
| 4731 |
} |
| 4732 |
|
| 4733 |
rtx size_rtx = expand_simple_binop (ptr_mode, MINUS, top_rtx, bot_rtx, |
| 4734 |
NULL_RTX, /* unsignedp = */0, |
| 4735 |
OPTAB_DIRECT); |
| 4736 |
|
| 4737 |
rtx fn = init_one_libfunc ("__hwasan_tag_memory"); |
| 4738 |
emit_library_call (fn, LCT_NORMAL, VOIDmode, |
| 4739 |
bot_rtx, ptr_mode, |
| 4740 |
HWASAN_STACK_BACKGROUND, QImode, |
| 4741 |
size_rtx, ptr_mode); |
| 4742 |
|
| 4743 |
do_pending_stack_adjust (); |
| 4744 |
rtx_insn *insns = get_insns (); |
| 4745 |
end_sequence (); |
| 4746 |
return insns; |
| 4747 |
} |
| 4748 |
|
| 4749 |
/* Needs to be GTY(()), because cgraph_build_static_cdtor may |
| 4750 |
invoke ggc_collect. */ |
| 4751 |
static GTY(()) tree hwasan_ctor_statements; |
| 4752 |
|
| 4753 |
/* Insert module initialization into this TU. This initialization calls the |
| 4754 |
initialization code for libhwasan. */ |
| 4755 |
void |
| 4756 |
hwasan_finish_file (void) |
| 4757 |
{ |
| 4758 |
/* Do not emit constructor initialization for the kernel. |
| 4759 |
(the kernel has its own initialization already). */ |
| 4760 |
if (flag_sanitize & SANITIZE_KERNEL_HWADDRESS) |
| 4761 |
return; |
| 4762 |
|
| 4763 |
initialize_sanitizer_builtins (); |
| 4764 |
|
| 4765 |
/* Avoid instrumenting code in the hwasan constructors/destructors. */ |
| 4766 |
flag_sanitize &= ~SANITIZE_HWADDRESS; |
| 4767 |
int priority = MAX_RESERVED_INIT_PRIORITY - 1; |
| 4768 |
tree fn = builtin_decl_implicit (BUILT_IN_HWASAN_INIT); |
| 4769 |
append_to_statement_list (build_call_expr (fn, 0), &hwasan_ctor_statements); |
| 4770 |
cgraph_build_static_cdtor ('I', hwasan_ctor_statements, priority); |
| 4771 |
flag_sanitize |= SANITIZE_HWADDRESS; |
| 4772 |
} |
| 4773 |
|
| 4774 |
/* For stack tagging: |
| 4775 |
|
| 4776 |
Truncate `tag` to the number of bits that a tag uses (i.e. to |
| 4777 |
HWASAN_TAG_SIZE). Store the result in `target` if it's convenient. */ |
| 4778 |
rtx |
| 4779 |
hwasan_truncate_to_tag_size (rtx tag, rtx target) |
| 4780 |
{ |
| 4781 |
gcc_assert (GET_MODE (tag) == QImode); |
| 4782 |
if (HWASAN_TAG_SIZE != GET_MODE_PRECISION (QImode)) |
| 4783 |
{ |
| 4784 |
gcc_assert (GET_MODE_PRECISION (QImode) > HWASAN_TAG_SIZE); |
| 4785 |
rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << HWASAN_TAG_SIZE) - 1, |
| 4786 |
QImode); |
| 4787 |
tag = expand_simple_binop (QImode, AND, tag, mask, target, |
| 4788 |
/* unsignedp = */1, OPTAB_WIDEN); |
| 4789 |
gcc_assert (tag); |
| 4790 |
} |
| 4791 |
return tag; |
| 4792 |
} |
| 4793 |
|
| 4794 |
/* Construct a function tree for __hwasan_{load,store}{1,2,4,8,16,_n}. |
| 4795 |
IS_STORE is either 1 (for a store) or 0 (for a load). */ |
| 4796 |
static combined_fn |
| 4797 |
hwasan_check_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes, |
| 4798 |
int *nargs) |
| 4799 |
{ |
| 4800 |
static enum built_in_function check[2][2][6] |
| 4801 |
= { { { BUILT_IN_HWASAN_LOAD1, BUILT_IN_HWASAN_LOAD2, |
| 4802 |
BUILT_IN_HWASAN_LOAD4, BUILT_IN_HWASAN_LOAD8, |
| 4803 |
BUILT_IN_HWASAN_LOAD16, BUILT_IN_HWASAN_LOADN }, |
| 4804 |
{ BUILT_IN_HWASAN_STORE1, BUILT_IN_HWASAN_STORE2, |
| 4805 |
BUILT_IN_HWASAN_STORE4, BUILT_IN_HWASAN_STORE8, |
| 4806 |
BUILT_IN_HWASAN_STORE16, BUILT_IN_HWASAN_STOREN } }, |
| 4807 |
{ { BUILT_IN_HWASAN_LOAD1_NOABORT, |
| 4808 |
BUILT_IN_HWASAN_LOAD2_NOABORT, |
| 4809 |
BUILT_IN_HWASAN_LOAD4_NOABORT, |
| 4810 |
BUILT_IN_HWASAN_LOAD8_NOABORT, |
| 4811 |
BUILT_IN_HWASAN_LOAD16_NOABORT, |
| 4812 |
BUILT_IN_HWASAN_LOADN_NOABORT }, |
| 4813 |
{ BUILT_IN_HWASAN_STORE1_NOABORT, |
| 4814 |
BUILT_IN_HWASAN_STORE2_NOABORT, |
| 4815 |
BUILT_IN_HWASAN_STORE4_NOABORT, |
| 4816 |
BUILT_IN_HWASAN_STORE8_NOABORT, |
| 4817 |
BUILT_IN_HWASAN_STORE16_NOABORT, |
| 4818 |
BUILT_IN_HWASAN_STOREN_NOABORT } } }; |
| 4819 |
if (size_in_bytes == -1) |
| 4820 |
{ |
| 4821 |
*nargs = 2; |
| 4822 |
return as_combined_fn (check[recover_p][is_store][5]); |
| 4823 |
} |
| 4824 |
*nargs = 1; |
| 4825 |
int size_log2 = exact_log2 (size_in_bytes); |
| 4826 |
gcc_assert (size_log2 >= 0 && size_log2 <= 5); |
| 4827 |
return as_combined_fn (check[recover_p][is_store][size_log2]); |
| 4828 |
} |
| 4829 |
|
| 4830 |
/* Expand the HWASAN_{LOAD,STORE} builtins. */ |
| 4831 |
bool |
| 4832 |
hwasan_expand_check_ifn (gimple_stmt_iterator *iter, bool) |
| 4833 |
{ |
| 4834 |
gimple *g = gsi_stmt (*iter); |
| 4835 |
location_t loc = gimple_location (g); |
| 4836 |
bool recover_p; |
| 4837 |
if (flag_sanitize & SANITIZE_USER_HWADDRESS) |
| 4838 |
recover_p = (flag_sanitize_recover & SANITIZE_USER_HWADDRESS) != 0; |
| 4839 |
else |
| 4840 |
recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_HWADDRESS) != 0; |
| 4841 |
|
| 4842 |
HOST_WIDE_INT flags = tree_to_shwi (gimple_call_arg (g, 0)); |
| 4843 |
gcc_assert (flags < ASAN_CHECK_LAST); |
| 4844 |
bool is_scalar_access = (flags & ASAN_CHECK_SCALAR_ACCESS) != 0; |
| 4845 |
bool is_store = (flags & ASAN_CHECK_STORE) != 0; |
| 4846 |
bool is_non_zero_len = (flags & ASAN_CHECK_NON_ZERO_LEN) != 0; |
| 4847 |
|
| 4848 |
tree base = gimple_call_arg (g, 1); |
| 4849 |
tree len = gimple_call_arg (g, 2); |
| 4850 |
|
| 4851 |
/* `align` is unused for HWASAN_CHECK, but we pass the argument anyway |
| 4852 |
since that way the arguments match ASAN_CHECK. */ |
| 4853 |
/* HOST_WIDE_INT align = tree_to_shwi (gimple_call_arg (g, 3)); */ |
| 4854 |
|
| 4855 |
unsigned HOST_WIDE_INT size_in_bytes |
| 4856 |
= is_scalar_access ? tree_to_shwi (len) : -1; |
| 4857 |
|
| 4858 |
gimple_stmt_iterator gsi = *iter; |
| 4859 |
|
| 4860 |
if (!is_non_zero_len) |
| 4861 |
{ |
| 4862 |
/* So, the length of the memory area to hwasan-protect is |
| 4863 |
non-constant. Let's guard the generated instrumentation code |
| 4864 |
like: |
| 4865 |
|
| 4866 |
if (len != 0) |
| 4867 |
{ |
| 4868 |
// hwasan instrumentation code goes here. |
| 4869 |
} |
| 4870 |
// falltrough instructions, starting with *ITER. */ |
| 4871 |
|
| 4872 |
g = gimple_build_cond (NE_EXPR, |
| 4873 |
len, |
| 4874 |
build_int_cst (TREE_TYPE (len), 0), |
| 4875 |
NULL_TREE, NULL_TREE); |
| 4876 |
gimple_set_location (g, loc); |
| 4877 |
|
| 4878 |
basic_block then_bb, fallthrough_bb; |
| 4879 |
insert_if_then_before_iter (as_a <gcond *> (g), iter, |
| 4880 |
/*then_more_likely_p=*/true, |
| 4881 |
&then_bb, &fallthrough_bb); |
| 4882 |
/* Note that fallthrough_bb starts with the statement that was |
| 4883 |
pointed to by ITER. */ |
| 4884 |
|
| 4885 |
/* The 'then block' of the 'if (len != 0) condition is where |
| 4886 |
we'll generate the hwasan instrumentation code now. */ |
| 4887 |
gsi = gsi_last_bb (then_bb); |
| 4888 |
} |
| 4889 |
|
| 4890 |
gimple_seq stmts = NULL; |
| 4891 |
tree base_addr = gimple_build (&stmts, loc, NOP_EXPR, |
| 4892 |
pointer_sized_int_node, base); |
| 4893 |
|
| 4894 |
int nargs = 0; |
| 4895 |
combined_fn fn |
| 4896 |
= hwasan_check_func (is_store, recover_p, size_in_bytes, &nargs); |
| 4897 |
if (nargs == 1) |
| 4898 |
gimple_build (&stmts, loc, fn, void_type_node, base_addr); |
| 4899 |
else |
| 4900 |
{ |
| 4901 |
gcc_assert (nargs == 2); |
| 4902 |
tree sz_arg = gimple_build (&stmts, loc, NOP_EXPR, |
| 4903 |
pointer_sized_int_node, len); |
| 4904 |
gimple_build (&stmts, loc, fn, void_type_node, base_addr, sz_arg); |
| 4905 |
} |
| 4906 |
|
| 4907 |
gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT); |
| 4908 |
gsi_remove (iter, true); |
| 4909 |
*iter = gsi; |
| 4910 |
return false; |
| 4911 |
} |
| 4912 |
|
| 4913 |
/* For stack tagging: |
| 4914 |
|
| 4915 |
Dummy: the HWASAN_MARK internal function should only ever be in the code |
| 4916 |
after the sanopt pass. */ |
| 4917 |
bool |
| 4918 |
hwasan_expand_mark_ifn (gimple_stmt_iterator *) |
| 4919 |
{ |
| 4920 |
gcc_unreachable (); |
| 4921 |
} |
| 4922 |
|
| 4923 |
bool |
| 4924 |
gate_hwasan () |
| 4925 |
{ |
| 4926 |
return hwasan_sanitize_p (); |
| 4927 |
} |
| 4928 |
|
| 4929 |
#include "gt-asan.h" |