gcc/libgcc
Alexandre Oliva 551935d118 Control flow redundancy hardening
This patch introduces an optional hardening pass to catch unexpected
execution flows.  Functions are transformed so that basic blocks set a
bit in an automatic array, and (non-exceptional) function exit edges
check that the bits in the array represent an expected execution path
in the CFG.

Functions with multiple exit edges, or with too many blocks, call an
out-of-line checker builtin implemented in libgcc.  For simpler
functions, the verification is performed in-line.

-fharden-control-flow-redundancy enables the pass for eligible
functions, --param hardcfr-max-blocks sets a block count limit for
functions to be eligible, and --param hardcfr-max-inline-blocks
tunes the "too many blocks" limit for in-line verification.
-fhardcfr-skip-leaf makes leaf functions non-eligible.

Additional -fhardcfr-check-* options are added to enable checking at
exception escape points, before potential sibcalls, hereby dubbed
returning calls, and before noreturn calls and exception raises.  A
notable case is the distinction between noreturn calls expected to
throw and those expected to terminate or loop forever: the default
setting for -fhardcfr-check-noreturn-calls, no-xthrow, performs
checking before the latter, but the former only gets checking in the
exception handler.  GCC can only tell between them by explicit marking
noreturn functions expected to raise with the newly-introduced
expected_throw attribute, and corresponding ECF_XTHROW flag.


for  gcc/ChangeLog

	* tree-core.h (ECF_XTHROW): New macro.
	* tree.cc (set_call_expr): Add expected_throw attribute when
	ECF_XTHROW is set.
	(build_common_builtin_node): Add ECF_XTHROW to
	__cxa_end_cleanup and _Unwind_Resume or _Unwind_SjLj_Resume.
	* calls.cc (flags_from_decl_or_type): Check for expected_throw
	attribute to set ECF_XTHROW.
	* gimple.cc (gimple_build_call_from_tree): Propagate
	ECF_XTHROW from decl flags to gimple call...
	(gimple_call_flags): ... and back.
	* gimple.h (GF_CALL_XTHROW): New gf_mask flag.
	(gimple_call_set_expected_throw): New.
	(gimple_call_expected_throw_p): New.
	* Makefile.in (OBJS): Add gimple-harden-control-flow.o.
	* builtins.def (BUILT_IN___HARDCFR_CHECK): New.
	* common.opt (fharden-control-flow-redundancy): New.
	(-fhardcfr-check-returning-calls): New.
	(-fhardcfr-check-exceptions): New.
	(-fhardcfr-check-noreturn-calls=*): New.
	(Enum hardcfr_check_noreturn_calls): New.
	(fhardcfr-skip-leaf): New.
	* doc/invoke.texi: Document them.
	(hardcfr-max-blocks, hardcfr-max-inline-blocks): New params.
	* flag-types.h (enum hardcfr_noret): New.
	* gimple-harden-control-flow.cc: New.
	* params.opt (-param=hardcfr-max-blocks=): New.
	(-param=hradcfr-max-inline-blocks=): New.
	* passes.def (pass_harden_control_flow_redundancy): Add.
	* tree-pass.h (make_pass_harden_control_flow_redundancy):
	Declare.
	* doc/extend.texi: Document expected_throw attribute.

for  gcc/ada/ChangeLog

	* gcc-interface/trans.cc (gigi): Mark __gnat_reraise_zcx with
	ECF_XTHROW.
	(build_raise_check): Likewise for all rcheck subprograms.

for  gcc/c-family/ChangeLog

	* c-attribs.cc (handle_expected_throw_attribute): New.
	(c_common_attribute_table): Add expected_throw.

for  gcc/cp/ChangeLog

	* decl.cc (push_throw_library_fn): Mark with ECF_XTHROW.
	* except.cc (build_throw): Likewise __cxa_throw,
	_ITM_cxa_throw, __cxa_rethrow.

for  gcc/testsuite/ChangeLog

	* c-c++-common/torture/harden-cfr.c: New.
	* c-c++-common/harden-cfr-noret-never-O0.c: New.
	* c-c++-common/torture/harden-cfr-noret-never.c: New.
	* c-c++-common/torture/harden-cfr-noret-noexcept.c: New.
	* c-c++-common/torture/harden-cfr-noret-nothrow.c: New.
	* c-c++-common/torture/harden-cfr-noret.c: New.
	* c-c++-common/torture/harden-cfr-notail.c: New.
	* c-c++-common/torture/harden-cfr-returning.c: New.
	* c-c++-common/torture/harden-cfr-tail.c: New.
	* c-c++-common/torture/harden-cfr-abrt-always.c: New.
	* c-c++-common/torture/harden-cfr-abrt-never.c: New.
	* c-c++-common/torture/harden-cfr-abrt-no-xthrow.c: New.
	* c-c++-common/torture/harden-cfr-abrt-nothrow.c: New.
	* c-c++-common/torture/harden-cfr-abrt.c: New.
	* c-c++-common/torture/harden-cfr-always.c: New.
	* c-c++-common/torture/harden-cfr-never.c: New.
	* c-c++-common/torture/harden-cfr-no-xthrow.c: New.
	* c-c++-common/torture/harden-cfr-nothrow.c: New.
	* c-c++-common/torture/harden-cfr-bret-always.c: New.
	* c-c++-common/torture/harden-cfr-bret-never.c: New.
	* c-c++-common/torture/harden-cfr-bret-noopt.c: New.
	* c-c++-common/torture/harden-cfr-bret-noret.c: New.
	* c-c++-common/torture/harden-cfr-bret-no-xthrow.c: New.
	* c-c++-common/torture/harden-cfr-bret-nothrow.c: New.
	* c-c++-common/torture/harden-cfr-bret-retcl.c: New.
	* c-c++-common/torture/harden-cfr-bret.c: New.
	* g++.dg/harden-cfr-throw-always-O0.C: New.
	* g++.dg/harden-cfr-throw-returning-O0.C: New.
	* g++.dg/torture/harden-cfr-noret-always-no-nothrow.C: New.
	* g++.dg/torture/harden-cfr-noret-never-no-nothrow.C: New.
	* g++.dg/torture/harden-cfr-noret-no-nothrow.C: New.
	* g++.dg/torture/harden-cfr-throw-always.C: New.
	* g++.dg/torture/harden-cfr-throw-never.C: New.
	* g++.dg/torture/harden-cfr-throw-no-xthrow.C: New.
	* g++.dg/torture/harden-cfr-throw-no-xthrow-expected.C: New.
	* g++.dg/torture/harden-cfr-throw-nothrow.C: New.
	* g++.dg/torture/harden-cfr-throw-nocleanup.C: New.
	* g++.dg/torture/harden-cfr-throw-returning.C: New.
	* g++.dg/torture/harden-cfr-throw.C: New.
	* gcc.dg/torture/harden-cfr-noret-no-nothrow.c: New.
	* gcc.dg/torture/harden-cfr-tail-ub.c: New.
	* gnat.dg/hardcfr.adb: New.

for  libgcc/ChangeLog

	* Makefile.in (LIB2ADD): Add hardcfr.c.
	* hardcfr.c: New.
2023-10-20 07:50:33 -03:00
..
config Daily bump. 2023-10-19 00:18:05 +00:00
soft-fp libgcc _BitInt helper documentation [PR102989] 2023-09-06 17:42:37 +02:00
ChangeLog Daily bump. 2023-10-14 00:16:40 +00:00
config.host LoongArch: support loongarch*-elf target 2023-09-05 11:48:16 +08:00
config.in gcov: use mmap pools for KVP. 2021-03-03 14:21:45 +01:00
configure Deprecate a.out support for NetBSD targets. 2023-08-07 22:59:41 +02:00
configure.ac LoongArch Port: libgcc 2022-03-29 17:43:35 +08:00
crtstuff.c Update copyright years. 2023-01-16 11:52:17 +01:00
dfp-bit.c Update copyright years. 2023-01-16 11:52:17 +01:00
dfp-bit.h Update copyright years. 2023-01-16 11:52:17 +01:00
divmod.c Update copyright years. 2023-01-16 11:52:17 +01:00
emutls.c Update copyright years. 2023-01-16 11:52:17 +01:00
enable-execute-stack-empty.c
enable-execute-stack-mprotect.c Update copyright years. 2023-01-16 11:52:17 +01:00
find-symver.awk Update copyright years. 2023-01-16 11:52:17 +01:00
fixed-bit.c Update copyright years. 2023-01-16 11:52:17 +01:00
fixed-bit.h Update copyright years. 2023-01-16 11:52:17 +01:00
fixed-obj.mk
floatunsidf.c
floatunsisf.c
floatunsitf.c
floatunsixf.c
fp-bit.c Update copyright years. 2023-01-16 11:52:17 +01:00
fp-bit.h Update copyright years. 2023-01-16 11:52:17 +01:00
gbl-ctors.h Update copyright years. 2023-01-16 11:52:17 +01:00
gcov.h Update copyright years. 2023-01-16 11:52:17 +01:00
gen-fixed.sh
generic-morestack-thread.c Update copyright years. 2023-01-16 11:52:17 +01:00
generic-morestack.c Update copyright years. 2023-01-16 11:52:17 +01:00
generic-morestack.h Update copyright years. 2023-01-16 11:52:17 +01:00
gstdint.h
gthr-posix.h Update copyright years. 2023-01-16 11:52:17 +01:00
gthr-single.h Update copyright years. 2023-01-16 11:52:17 +01:00
gthr.h Update copyright years. 2023-01-16 11:52:17 +01:00
hardcfr.c Control flow redundancy hardening 2023-10-20 07:50:33 -03:00
libgcc2.c libgcc _BitInt helper documentation [PR102989] 2023-09-06 17:42:37 +02:00
libgcc2.h libgcc _BitInt support [PR102989] 2023-09-06 17:33:05 +02:00
libgcc-std.ver.in libgcc _BitInt support [PR102989] 2023-09-06 17:33:05 +02:00
libgcov-driver-system.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov-driver.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov-interface.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov-merge.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov-profiler.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov-util.c Update copyright years. 2023-01-16 11:52:17 +01:00
libgcov.h Update copyright years. 2023-01-16 11:52:17 +01:00
Makefile.in Control flow redundancy hardening 2023-10-20 07:50:33 -03:00
memcmp.c
memcpy.c
memmove.c
memset.c
mkheader.sh Update copyright years. 2023-01-16 11:52:17 +01:00
mkmap-flat.awk Update copyright years. 2023-01-16 11:52:17 +01:00
mkmap-symver.awk Update copyright years. 2023-01-16 11:52:17 +01:00
offloadstuff.c Update copyright years. 2023-01-16 11:52:17 +01:00
shared-object.mk
siditi-object.mk
static-object.mk
sync.c Update copyright years. 2023-01-16 11:52:17 +01:00
udivhi3.c Update copyright years. 2023-01-16 11:52:17 +01:00
udivmod.c Update copyright years. 2023-01-16 11:52:17 +01:00
udivmodhi4.c Update copyright years. 2023-01-16 11:52:17 +01:00
udivmodsi4.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-arm-common.inc Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-c.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-compat.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-compat.h Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-dw2-btree.h Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-dw2-execute_cfa.h libgcc: Fix uninitialized RA signing on AArch64 [PR107678] 2023-01-18 12:20:35 +00:00
unwind-dw2-fde-compat.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-dw2-fde-dip.c libgcc: Fix -Wint-conversion warning in find_fde_tail 2023-07-11 06:19:39 +02:00
unwind-dw2-fde.c preserve base pointer for __deregister_frame [PR110956] 2023-08-11 09:20:27 -06:00
unwind-dw2-fde.h Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-dw2.c libgcc: Fix uninitialized RA signing on AArch64 [PR107678] 2023-01-18 12:20:35 +00:00
unwind-dw2.h libgcc: Fix uninitialized RA signing on AArch64 [PR107678] 2023-01-18 12:20:35 +00:00
unwind-generic.h Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-pe.h Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-seh.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind-sjlj.c Update copyright years. 2023-01-16 11:52:17 +01:00
unwind.inc Update copyright years. 2023-01-16 11:52:17 +01:00
vtv_end_preinit.c Update copyright years. 2023-01-16 11:52:17 +01:00
vtv_end.c Update copyright years. 2023-01-16 11:52:17 +01:00
vtv_start_preinit.c Update copyright years. 2023-01-16 11:52:17 +01:00
vtv_start.c Update copyright years. 2023-01-16 11:52:17 +01:00