gcc/config
Matthew Malcomson 0854b584bd libsanitizer: mid-end: Introduce stack variable handling for HWASAN
Handling stack variables has three features.

1) Ensure HWASAN required alignment for stack variables

When tagging shadow memory, we need to ensure that each tag granule is
only used by one variable at a time.

This is done by ensuring that each tagged variable is aligned to the tag
granule representation size and also ensure that the end of each
object is aligned to ensure the start of any other data stored on the
stack is in a different granule.

This patch ensures the above by forcing the stack pointer to be aligned
before and after allocating any stack objects. Since we are forcing
alignment we also use `align_local_variable` to ensure this new alignment
is advertised properly through SET_DECL_ALIGN.

2) Put tags into each stack variable pointer

Make sure that every pointer to a stack variable includes a tag of some
sort on it.

The way tagging works is:
  1) For every new stack frame, a random tag is generated.
  2) A base register is formed from the stack pointer value and this
     random tag.
  3) References to stack variables are now formed with RTL describing an
     offset from this base in both tag and value.

The random tag generation is handled by a backend hook.  This hook
decides whether to introduce a random tag or use the stack background
based on the parameter hwasan-random-frame-tag.  Using the stack
background is necessary for testing and bootstrap.  It is necessary
during bootstrap to avoid breaking the `configure` test program for
determining stack direction.

Using the stack background means that every stack frame has the initial
tag of zero and variables are tagged with incrementing tags from 1,
which also makes debugging a bit easier.

Backend hooks define the size of a tag, the layout of the HWASAN shadow
memory, and handle emitting the code that inserts and extracts tags from a
pointer.

3) For each stack variable, tag and untag the shadow stack on function
   prologue and epilogue.

On entry to each function we tag the relevant shadow stack region for
each stack variable. This stack region is tagged to match the tag added to
each pointer to that variable.

This is the first patch where we use the HWASAN shadow space, so we need
to add in the libhwasan initialisation code that creates this shadow
memory region into the binary we produce.  This instrumentation is done
in `compile_file`.

When exiting a function we need to ensure the shadow stack for this
function has no remaining tags.  Without clearing the shadow stack area
for this stack frame, later function calls could get false positives
when those later function calls check untagged areas (such as parameters
passed on the stack) against a shadow stack area with left-over tag.

Hence we ensure that the entire stack frame is cleared on function exit.

config/ChangeLog:

	* bootstrap-hwasan.mk: Disable random frame tags for stack-tagging
	during bootstrap.

gcc/ChangeLog:

	* asan.c (struct hwasan_stack_var): New.
	(hwasan_sanitize_p): New.
	(hwasan_sanitize_stack_p): New.
	(hwasan_sanitize_allocas_p): New.
	(initialize_sanitizer_builtins): Define new builtins.
	(ATTR_NOTHROW_LIST): New macro.
	(hwasan_current_frame_tag): New.
	(hwasan_frame_base): New.
	(stack_vars_base_reg_p): New.
	(hwasan_maybe_init_frame_base_init): New.
	(hwasan_record_stack_var): New.
	(hwasan_get_frame_extent): New.
	(hwasan_increment_frame_tag): New.
	(hwasan_record_frame_init): New.
	(hwasan_emit_prologue): New.
	(hwasan_emit_untag_frame): New.
	(hwasan_finish_file): New.
	(hwasan_truncate_to_tag_size): New.
	* asan.h (hwasan_record_frame_init): New declaration.
	(hwasan_record_stack_var): New declaration.
	(hwasan_emit_prologue): New declaration.
	(hwasan_emit_untag_frame): New declaration.
	(hwasan_get_frame_extent): New declaration.
	(hwasan_maybe_enit_frame_base_init): New declaration.
	(hwasan_frame_base): New declaration.
	(stack_vars_base_reg_p): New declaration.
	(hwasan_current_frame_tag): New declaration.
	(hwasan_increment_frame_tag): New declaration.
	(hwasan_truncate_to_tag_size): New declaration.
	(hwasan_finish_file): New declaration.
	(hwasan_sanitize_p): New declaration.
	(hwasan_sanitize_stack_p): New declaration.
	(hwasan_sanitize_allocas_p): New declaration.
	(HWASAN_TAG_SIZE): New macro.
	(HWASAN_TAG_GRANULE_SIZE): New macro.
	(HWASAN_STACK_BACKGROUND): New macro.
	* builtin-types.def (BT_FN_VOID_PTR_UINT8_PTRMODE): New.
	* builtins.def (DEF_SANITIZER_BUILTIN): Enable for HWASAN.
	* cfgexpand.c (align_local_variable): When using hwasan ensure
	alignment to tag granule.
	(align_frame_offset): New.
	(expand_one_stack_var_at): For hwasan use tag offset.
	(expand_stack_vars): Record stack objects for hwasan.
	(expand_one_stack_var_1): Record stack objects for hwasan.
	(init_vars_expansion): Initialise hwasan state.
	(expand_used_vars): Emit hwasan prologue and generate hwasan epilogue.
	(pass_expand::execute): Emit hwasan base initialization if needed.
	* doc/tm.texi (TARGET_MEMTAG_TAG_SIZE,TARGET_MEMTAG_GRANULE_SIZE,
	TARGET_MEMTAG_INSERT_RANDOM_TAG,TARGET_MEMTAG_ADD_TAG,
	TARGET_MEMTAG_SET_TAG,TARGET_MEMTAG_EXTRACT_TAG,
	TARGET_MEMTAG_UNTAGGED_POINTER): Document new hooks.
	* doc/tm.texi.in (TARGET_MEMTAG_TAG_SIZE,TARGET_MEMTAG_GRANULE_SIZE,
	TARGET_MEMTAG_INSERT_RANDOM_TAG,TARGET_MEMTAG_ADD_TAG,
	TARGET_MEMTAG_SET_TAG,TARGET_MEMTAG_EXTRACT_TAG,
	TARGET_MEMTAG_UNTAGGED_POINTER): Document new hooks.
	* explow.c (get_dynamic_stack_base): Take new `base` argument.
	* explow.h (get_dynamic_stack_base): Take new `base` argument.
	* sanitizer.def (BUILT_IN_HWASAN_INIT): New.
	(BUILT_IN_HWASAN_TAG_MEM): New.
	* target.def (target_memtag_tag_size,target_memtag_granule_size,
	target_memtag_insert_random_tag,target_memtag_add_tag,
	target_memtag_set_tag,target_memtag_extract_tag,
	target_memtag_untagged_pointer): New hooks.
	* targhooks.c (HWASAN_SHIFT): New.
	(HWASAN_SHIFT_RTX): New.
	(default_memtag_tag_size): New default hook.
	(default_memtag_granule_size): New default hook.
	(default_memtag_insert_random_tag): New default hook.
	(default_memtag_add_tag): New default hook.
	(default_memtag_set_tag): New default hook.
	(default_memtag_extract_tag): New default hook.
	(default_memtag_untagged_pointer): New default hook.
	* targhooks.h (default_memtag_tag_size): New default hook.
	(default_memtag_granule_size): New default hook.
	(default_memtag_insert_random_tag): New default hook.
	(default_memtag_add_tag): New default hook.
	(default_memtag_set_tag): New default hook.
	(default_memtag_extract_tag): New default hook.
	(default_memtag_untagged_pointer): New default hook.
	* toplev.c (compile_file): Call hwasan_finish_file when finished.
2020-11-25 16:38:06 +00:00
..
acinclude.m4
acx.m4 re PR jit/85384 (libgccjit does not work if --with-gcc-major-version is used) 2018-04-18 11:46:58 +02:00
asmcfi.m4
ax_check_define.m4
ax_count_cpus.m4 Generalize getconf _NPROCESSORS_ONLN 2019-05-30 09:06:48 +00:00
ax_cxx_compile_stdcxx.m4 configure: Also check C++11 (flags) for ${build} compiler not only for ${host} 2020-08-20 21:59:00 +02:00
ax_pthread.m4 Add ax_pthread.m4 for use in binutils-gdb 2018-05-09 11:25:27 -04:00
bitfields.m4
bootstrap-asan.mk
bootstrap-cet.mk x86/CET: Add -fcf-protection to STAGE4_CFLAGS 2018-04-24 15:12:46 -07:00
bootstrap-debug-big.mk
bootstrap-debug-ckovw.mk
bootstrap-debug-lean.mk
bootstrap-debug-lib.mk
bootstrap-debug.mk
bootstrap-hwasan.mk libsanitizer: mid-end: Introduce stack variable handling for HWASAN 2020-11-25 16:38:06 +00:00
bootstrap-lto-lean.mk Filter out LTO in config/bootstrap-lto-lean.mk. 2019-04-16 15:24:53 +00:00
bootstrap-lto-noplugin.mk re PR bootstrap/85571 (non-bootstrap-debug miscompare with trunk) 2018-05-08 08:50:33 +00:00
bootstrap-lto.mk re PR lto/85574 (LTO bootstapped binaries differ) 2019-05-02 13:58:47 +00:00
bootstrap-O1.mk
bootstrap-O3.mk
bootstrap-Og.mk Add a build config for bootstrapping at -Og 2019-07-08 06:21:11 +00:00
bootstrap-time.mk
bootstrap-ubsan.mk Improve boostrap-ubsan config (PR bootstrap/64914). 2018-05-10 10:15:42 +00:00
cet.m4 Require CET support only for the final GCC build 2020-07-30 05:36:24 -07:00
ChangeLog Fixup config/ChangeLog. 2020-09-10 10:17:51 +02:00
codeset.m4
depstand.m4
dfp.m4 Add missing '|' as wrong patch was applied. 2018-07-05 14:28:01 -06:00
elf.m4
enable.m4 enable.m4: New file. 2005-09-23 16:33:47 +00:00
extensions.m4
futex.m4 [ARM/FDPIC v6 02/24] [ARM] FDPIC: Handle arm*-*-uclinuxfdpiceabi in configure scripts 2019-09-10 09:37:00 +02:00
gc++filt.m4
gcc-plugin.m4
gettext-sister.m4
gettext.m4
glibc21.m4
gthr.m4 Create GCN-specific gthreads 2019-06-25 11:41:29 +00:00
gxx-include-dir.m4 re PR libgcj/28426 (libgcj header files installed in wrong location in cross build) 2006-07-21 20:49:06 +00:00
hwcaps.m4
iconv.m4 iconv.m4 (AM_ICONV_LINK): Don't overwrite CPPFLAGS. 2018-11-07 15:41:21 -07:00
intdiv0.m4
inttypes_h.m4
inttypes-pri.m4
inttypes.m4 gettext-sister.m4: Renamed from gettext.m4 2004-09-22 23:53:59 +00:00
isl.m4
largefile.m4 config: Sync largefile.m4 from binutils-gdb 2020-09-09 11:02:01 +02:00
lcmessage.m4
ld-symbolic.m4
lead-dot.m4
lib-ld.m4
lib-link.m4 Fixes after recent configure changes relating to static libraries 2020-02-01 00:34:28 +00:00
lib-prefix.m4
libstdc++-raw-cxx.m4
lthostflags.m4
math.m4 libgfortran: Provide some further math library fallbacks [PR94694] 2020-04-22 21:34:19 +02:00
mh-alpha-linux
mh-cygwin
mh-darwin
mh-djgpp
mh-mingw mh-mingw: Also set __USE_MINGW_ACCESS flag for C++ code. 2019-03-18 22:36:56 +00:00
mh-pa
mh-pa-hpux10
mh-ppc-aix
mmap.m4
mt-alphaieee
mt-android
mt-d30v
mt-gnu
mt-mips16-compat
mt-mips-elfoabi
mt-mips-gnu configure.ac (mips*-*-*linux*, [...]): Use mt-mips-gnu. 2008-08-09 19:08:15 +00:00
mt-nios2-elf
mt-ospace
mt-sde
mt-spu
multi.m4 Add D front-end, libphobos library, and D2 testsuite. 2018-10-28 19:51:47 +00:00
nls.m4
no-executables.m4 Use a non-empty test program to test ability to link. 2020-02-12 13:22:07 -08:00
override.m4 Update GCC to autoconf 2.69, automake 1.15.1 (PR bootstrap/82856). 2018-10-31 17:03:16 +00:00
picflag.m4
plugins.m4 Use dlsym to check if libdl is needed for plugin 2018-04-05 10:49:39 -07:00
po.m4
proginstall.m4
progtest.m4
sjlj.m4
stdint_h.m4
stdint.m4
target-posix
tcl.m4
tls.m4 [ARM/FDPIC v6 02/24] [ARM] FDPIC: Handle arm*-*-uclinuxfdpiceabi in configure scripts 2019-09-10 09:37:00 +02:00
toolexeclibdir.m4 Add `--with-toolexeclibdir=' configuration option 2020-01-24 11:24:25 +00:00
uintmax_t.m4
ulonglong.m4
unwind_ipinfo.m4
warnings.m4
weakref.m4
zlib.m4