aboutsummaryrefslogtreecommitdiff
path: root/gcc/asan.h
AgeCommit message (Collapse)AuthorFilesLines
2023-02-16don't declare header-defined functions both static and inlinePatrick Palka1-7/+7
Many functions defined in our headers are declared 'static inline' which is a C idiom whose use predates our move to C++ as the implementation language. But in C++ the inline keyword is more than just a compiler hint, and is sufficient to give the function the intended semantics. In fact declaring a function both static and inline is a pessimization since static effectively disables the desired definition merging behavior enabled by inline, and is also a source of (harmless) ODR violations when a static inline function gets called from a non-static inline one (such as tree_operand_check calling tree_operand_length). This patch mechanically fixes the vast majority of occurrences of this anti-pattern throughout the compiler's headers via the command line sed -i 's/^static inline/inline/g' gcc/*.h gcc/*/*.h There's also a manual change to remove the redundant declarations of is_ivar and lookup_category in gcc/objc/objc-act.cc which would otherwise conflict with their modified definitions in objc-act.h (due to the difference in staticness). Besides fixing some ODR violations, this speeds up stage1 cc1plus by about 2% and reduces the size of its text segment by 1.5MB. gcc/ChangeLog: * addresses.h: Mechanically drop 'static' from 'static inline' functions via s/^static inline/inline/g. * asan.h: Likewise. * attribs.h: Likewise. * basic-block.h: Likewise. * bitmap.h: Likewise. * cfghooks.h: Likewise. * cfgloop.h: Likewise. * cgraph.h: Likewise. * cselib.h: Likewise. * data-streamer.h: Likewise. * debug.h: Likewise. * df.h: Likewise. * diagnostic.h: Likewise. * dominance.h: Likewise. * dumpfile.h: Likewise. * emit-rtl.h: Likewise. * except.h: Likewise. * expmed.h: Likewise. * expr.h: Likewise. * fixed-value.h: Likewise. * gengtype.h: Likewise. * gimple-expr.h: Likewise. * gimple-iterator.h: Likewise. * gimple-predict.h: Likewise. * gimple-range-fold.h: Likewise. * gimple-ssa.h: Likewise. * gimple.h: Likewise. * graphite.h: Likewise. * hard-reg-set.h: Likewise. * hash-map.h: Likewise. * hash-set.h: Likewise. * hash-table.h: Likewise. * hwint.h: Likewise. * input.h: Likewise. * insn-addr.h: Likewise. * internal-fn.h: Likewise. * ipa-fnsummary.h: Likewise. * ipa-icf-gimple.h: Likewise. * ipa-inline.h: Likewise. * ipa-modref.h: Likewise. * ipa-prop.h: Likewise. * ira-int.h: Likewise. * ira.h: Likewise. * lra-int.h: Likewise. * lra.h: Likewise. * lto-streamer.h: Likewise. * memmodel.h: Likewise. * omp-general.h: Likewise. * optabs-query.h: Likewise. * optabs.h: Likewise. * plugin.h: Likewise. * pretty-print.h: Likewise. * range.h: Likewise. * read-md.h: Likewise. * recog.h: Likewise. * regs.h: Likewise. * rtl-iter.h: Likewise. * rtl.h: Likewise. * sbitmap.h: Likewise. * sched-int.h: Likewise. * sel-sched-ir.h: Likewise. * sese.h: Likewise. * sparseset.h: Likewise. * ssa-iterators.h: Likewise. * system.h: Likewise. * target-globals.h: Likewise. * target.h: Likewise. * timevar.h: Likewise. * tree-chrec.h: Likewise. * tree-data-ref.h: Likewise. * tree-iterator.h: Likewise. * tree-outof-ssa.h: Likewise. * tree-phinodes.h: Likewise. * tree-scalar-evolution.h: Likewise. * tree-sra.h: Likewise. * tree-ssa-alias.h: Likewise. * tree-ssa-live.h: Likewise. * tree-ssa-loop-manip.h: Likewise. * tree-ssa-loop.h: Likewise. * tree-ssa-operands.h: Likewise. * tree-ssa-propagate.h: Likewise. * tree-ssa-sccvn.h: Likewise. * tree-ssa.h: Likewise. * tree-ssanames.h: Likewise. * tree-streamer.h: Likewise. * tree-switch-conversion.h: Likewise. * tree-vectorizer.h: Likewise. * tree.h: Likewise. * wide-int.h: Likewise. gcc/c-family/ChangeLog: * c-common.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. gcc/c/ChangeLog: * c-parser.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. gcc/cp/ChangeLog: * cp-tree.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. gcc/fortran/ChangeLog: * gfortran.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. gcc/jit/ChangeLog: * jit-dejagnu.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. * jit-recording.h: Likewise. gcc/objc/ChangeLog: * objc-act.h: Mechanically drop static from static inline functions via s/^static inline/inline/g. * objc-map.h: Likewise. * objc-act.cc: Remove the redundant redeclarations of is_ivar and lookup_category.
2023-02-14asan: Add --param=asan-kernel-mem-intrinsic-prefix= [PR108777]Jakub Jelinek1-0/+1
While in the -fsanitize=address case libasan overloads memcpy, memset, memmove and many other builtins, such that they are always instrumented, Linux kernel for -fsanitize=kernel-address recently changed or is changing, such that memcpy, memset and memmove actually aren't instrumented because they are often used also from no_sanitize ("kernel-address") functions and wants __{,hw,}asaN_{memcpy,memset,memmove} to be used instead for the instrumented calls. See e.g. the https://lkml.org/lkml/2023/2/9/1182 thread. Without appropriate support on the compiler side, that will mean any time a kernel-address instrumented function (most of them) calls memcpy/memset/memmove, they will not be instrumented and thus won't catch kernel bugs. Apparently clang 15 has a param for this. The following patch implements the same (except it is a usual GCC --param, not -mllvm argument) on the GCC side. I know this isn't a regression bugfix, but given that -fsanitize=kernel-address has a single project that uses it which badly wants this I think it would be worthwhile to make an exception and get this into GCC 13 rather than waiting another year, it won't affect non-kernel code, nor even the kernel unless the new parameter is used. 2023-02-14 Jakub Jelinek <jakub@redhat.com> PR sanitizer/108777 * params.opt (-param=asan-kernel-mem-intrinsic-prefix=): New param. * asan.h (asan_memfn_rtl): Declare. * asan.cc (asan_memfn_rtls): New variable. (asan_memfn_rtl): New function. * builtins.cc (expand_builtin): If param_asan_kernel_mem_intrinsic_prefix and function is kernel-{,hw}address sanitized, emit calls to __{,hw}asan_{memcpy,memmove,memset} rather than {memcpy,memmove,memset}. Use sanitize_flags_p (SANITIZE_ADDRESS) instead of flag_sanitize & SANITIZE_ADDRESS to check if asan_intercepted_p functions shouldn't be expanded inline. * gcc.dg/asan/pr108777-1.c: New test. * gcc.dg/asan/pr108777-2.c: New test. * gcc.dg/asan/pr108777-3.c: New test. * gcc.dg/asan/pr108777-4.c: New test. * gcc.dg/asan/pr108777-5.c: New test. * gcc.dg/asan/pr108777-6.c: New test. * gcc.dg/completion-3.c: Adjust expected multiline output.
2023-01-02Update copyright years.Jakub Jelinek1-1/+1
2022-01-03Update copyright years.Jakub Jelinek1-1/+1
2021-09-13c++: fix -fsanitize-coverage=trace-pc ICE [PR101331]Martin Liska1-2/+3
PR c++/101331 gcc/ChangeLog: * asan.h (sanitize_coverage_p): Handle when fn == NULL. gcc/testsuite/ChangeLog: * g++.dg/pr101331.C: New test.
2021-05-25Add no_sanitize_coverage attribute.Martin Liska1-0/+10
gcc/ChangeLog: * asan.h (sanitize_coverage_p): New function. * doc/extend.texi: Document it. * fold-const.c (fold_range_test): Use sanitize_flags_p instead of flag_sanitize_coverage. (fold_truth_andor): Likewise. * sancov.c: Likewise. * tree-ssa-ifcombine.c (ifcombine_ifandif): Likewise. * ipa-inline.c (sanitize_attrs_match_for_inline_p): Handle -fsanitize-coverage when inlining. gcc/c-family/ChangeLog: * c-attribs.c (handle_no_sanitize_coverage_attribute): New. gcc/testsuite/ChangeLog: * gcc.dg/sancov/attribute.c: New test.
2021-01-04Update copyright years.Jakub Jelinek1-1/+1
2020-11-25libsanitizer: Add hwasan pass and associated gimple changesMatthew Malcomson1-1/+8
There are four main features to this change: 1) Check pointer tags match address tags. When sanitizing for hwasan we now put HWASAN_CHECK internal functions before memory accesses in the `asan` pass. This checks that a tag in the pointer being used match the tag stored in shadow memory for the memory region being used. These internal functions are expanded into actual checks in the sanopt pass that happens just before expansion into RTL. We use the same mechanism that currently inserts ASAN_CHECK internal functions to insert the new HWASAN_CHECK functions. 2) Instrument known builtin function calls. Handle all builtin functions that we know use memory accesses. This commit uses the machinery added for ASAN to identify builtin functions that access memory. The main differences between the approaches for HWASAN and ASAN are: - libhwasan intercepts much less builtin functions. - Alloca needs to be transformed differently (instead of adding redzones it needs to tag shadow memory and return a tagged pointer). - stack_restore needs to untag the shadow stack between the current position and where it's going. - `noreturn` functions can not be handled by simply unpoisoning the entire shadow stack -- there is no "always valid" tag. (exceptions and things such as longjmp need to be handled in a different way, usually in the runtime). For hardware implemented checking (such as AArch64's memory tagging extension) alloca and stack_restore will need to be handled by hooks in the backend rather than transformation at the gimple level. This will allow architecture specific handling of such stack modifications. 3) Introduce HWASAN block-scope poisoning Here we use exactly the same mechanism as ASAN_MARK to poison/unpoison variables on entry/exit of a block. In order to simply use the exact same machinery we're using the same internal functions until the SANOPT pass. This means that all handling of ASAN_MARK is the same. This has the negative that the naming may be a little confusing, but a positive that handling of the internal function doesn't have to be duplicated for a function that behaves exactly the same but has a different name. gcc/ChangeLog: * asan.c (asan_instrument_reads): New. (asan_instrument_writes): New. (asan_memintrin): New. (handle_builtin_stack_restore): Account for HWASAN. (handle_builtin_alloca): Account for HWASAN. (get_mem_refs_of_builtin_call): Special case strlen for HWASAN. (hwasan_instrument_reads): New. (hwasan_instrument_writes): New. (hwasan_memintrin): New. (report_error_func): Assert not HWASAN. (build_check_stmt): Make HWASAN_CHECK instead of ASAN_CHECK. (instrument_derefs): HWASAN does not tag globals. (instrument_builtin_call): Use new helper functions. (maybe_instrument_call): Don't instrument `noreturn` functions. (initialize_sanitizer_builtins): Add new type. (asan_expand_mark_ifn): Account for HWASAN. (asan_expand_check_ifn): Assert never called by HWASAN. (asan_expand_poison_ifn): Account for HWASAN. (asan_instrument): Branch based on whether using HWASAN or ASAN. (pass_asan::gate): Return true if sanitizing HWASAN. (pass_asan_O0::gate): Return true if sanitizing HWASAN. (hwasan_check_func): New. (hwasan_expand_check_ifn): New. (hwasan_expand_mark_ifn): New. (gate_hwasan): New. * asan.h (hwasan_expand_check_ifn): New decl. (hwasan_expand_mark_ifn): New decl. (gate_hwasan): New decl. (asan_intercepted_p): Always false for hwasan. (asan_sanitize_use_after_scope): Account for HWASAN. * builtin-types.def (BT_FN_PTR_CONST_PTR_UINT8): New. * gimple-fold.c (gimple_build): New overload for building function calls without arguments. (gimple_build_round_up): New. * gimple-fold.h (gimple_build): New decl. (gimple_build): New inline function. (gimple_build_round_up): New decl. (gimple_build_round_up): New inline function. * gimple-pretty-print.c (dump_gimple_call_args): Account for HWASAN. * gimplify.c (asan_poison_variable): Account for HWASAN. (gimplify_function_tree): Remove requirement of SANITIZE_ADDRESS, requiring asan or hwasan is accounted for in `asan_sanitize_use_after_scope`. * internal-fn.c (expand_HWASAN_CHECK): New. (expand_HWASAN_ALLOCA_UNPOISON): New. (expand_HWASAN_CHOOSE_TAG): New. (expand_HWASAN_MARK): New. (expand_HWASAN_SET_TAG): New. * internal-fn.def (HWASAN_ALLOCA_UNPOISON): New. (HWASAN_CHOOSE_TAG): New. (HWASAN_CHECK): New. (HWASAN_MARK): New. (HWASAN_SET_TAG): New. * sanitizer.def (BUILT_IN_HWASAN_LOAD1): New. (BUILT_IN_HWASAN_LOAD2): New. (BUILT_IN_HWASAN_LOAD4): New. (BUILT_IN_HWASAN_LOAD8): New. (BUILT_IN_HWASAN_LOAD16): New. (BUILT_IN_HWASAN_LOADN): New. (BUILT_IN_HWASAN_STORE1): New. (BUILT_IN_HWASAN_STORE2): New. (BUILT_IN_HWASAN_STORE4): New. (BUILT_IN_HWASAN_STORE8): New. (BUILT_IN_HWASAN_STORE16): New. (BUILT_IN_HWASAN_STOREN): New. (BUILT_IN_HWASAN_LOAD1_NOABORT): New. (BUILT_IN_HWASAN_LOAD2_NOABORT): New. (BUILT_IN_HWASAN_LOAD4_NOABORT): New. (BUILT_IN_HWASAN_LOAD8_NOABORT): New. (BUILT_IN_HWASAN_LOAD16_NOABORT): New. (BUILT_IN_HWASAN_LOADN_NOABORT): New. (BUILT_IN_HWASAN_STORE1_NOABORT): New. (BUILT_IN_HWASAN_STORE2_NOABORT): New. (BUILT_IN_HWASAN_STORE4_NOABORT): New. (BUILT_IN_HWASAN_STORE8_NOABORT): New. (BUILT_IN_HWASAN_STORE16_NOABORT): New. (BUILT_IN_HWASAN_STOREN_NOABORT): New. (BUILT_IN_HWASAN_TAG_MISMATCH4): New. (BUILT_IN_HWASAN_HANDLE_LONGJMP): New. (BUILT_IN_HWASAN_TAG_PTR): New. * sanopt.c (sanopt_optimize_walker): Act for hwasan. (pass_sanopt::execute): Act for hwasan. * toplev.c (compile_file): Use `gate_hwasan` function.
2020-11-25libsanitizer: mid-end: Introduce stack variable handling for HWASANMatthew Malcomson1-0/+36
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-07-23PR target/96260 - KASAN should work even back-end not porting anything.Kito Cheng1-0/+2
- Most KASAN function don't need any porting anything in back-end except asan stack protection. - However kernel will given shadow offset when enable asan stack protection, so eveything in KASAN can work if shadow offset is given. - Verified with x86 and risc-v. - Verified with RISC-V linux kernel. gcc/ChangeLog: PR target/96260 * asan.c (asan_shadow_offset_set_p): New. * asan.h (asan_shadow_offset_set_p): Ditto. * toplev.c (process_options): Allow -fsanitize=kernel-address even TARGET_ASAN_SHADOW_OFFSET not implemented, only check when asan stack protection is enabled. gcc/testsuite/ChangeLog: PR target/96260 * gcc.target/riscv/pr91441.c: Update warning message. * gcc.target/riscv/pr96260.c: New.
2020-01-01Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r279813
2019-01-01Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r267494
2018-11-30Make red zone size more flexible for stack variables (PR sanitizer/81715).Martin Liska1-0/+25
2018-11-30 Martin Liska <mliska@suse.cz> PR sanitizer/81715 * asan.c (asan_shadow_cst): Remove, partially transform into flush_redzone_payload. (RZ_BUFFER_SIZE): New. (struct asan_redzone_buffer): New. (asan_redzone_buffer::emit_redzone_byte): Likewise. (asan_redzone_buffer::flush_redzone_payload): Likewise. (asan_redzone_buffer::flush_if_full): Likewise. (asan_emit_stack_protection): Use asan_redzone_buffer class that is responsible for proper aligned stores and flushing of shadow memory payload. * asan.h (ASAN_MIN_RED_ZONE_SIZE): New. (asan_var_and_redzone_size): Likewise. * cfgexpand.c (expand_stack_vars): Use smaller alignment (ASAN_MIN_RED_ZONE_SIZE) in order to make shadow memory for automatic variables more compact. 2018-11-30 Martin Liska <mliska@suse.cz> PR sanitizer/81715 * c-c++-common/asan/asan-stack-small.c: New test. From-SVN: r266664
2018-09-24Unpoison variable partition properly (PR sanitizer/85774).Martin Liska1-0/+2
2018-09-24 Martin Liska <mliska@suse.cz> PR sanitizer/85774 * asan.c: Make asan_handled_variables extern. * asan.h: Likewise. * cfgexpand.c (expand_stack_vars): Make sure a representative is unpoison if another variable in the partition is handled by use-after-scope sanitization. 2018-09-24 Martin Liska <mliska@suse.cz> PR sanitizer/85774 * g++.dg/asan/pr85774.C: New test. From-SVN: r264528
2018-01-03Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r256169
2017-11-30re PR sanitizer/81697 (Incorrect ASan global variables alignment on arm)Maxim Ostapenko1-1/+1
gcc/ 2017-11-30 Maxim Ostapenko <m.ostapenko@samsung.com> PR sanitizer/81697 * asan.c (asan_protect_global): Add new ignore_decl_rtl_set_p parameter. Return true if ignore_decl_rtl_set_p is true and other conditions are satisfied. * asan.h (asan_protect_global): Add new parameter. * varasm.c (categorize_decl_for_section): Pass true as second parameter to asan_protect_global calls. gcc/testsuite/ 2017-11-30 Maxim Ostapenko <m.ostapenko@samsung.com> PR sanitizer/81697 * c-c++-common/asan/pr81697.c: New test. From-SVN: r255283
2017-07-06asan.h (asan_sanitize_allocas_p): Declare.Maxim Ostapenko1-0/+2
gcc/ * asan.h (asan_sanitize_allocas_p): Declare. * asan.c (asan_sanitize_allocas_p): New function. (handle_builtin_stack_restore): Bail out if !asan_sanitize_allocas_p. (handle_builtin_alloca): Likewise. * cfgexpand.c (expand_used_vars): Do not add allocas unpoisoning stuff if !asan_sanitize_allocas_p. * params.def (asan-instrument-allocas): Add new option. * params.h (ASAN_PROTECT_ALLOCAS): Define. * opts.c (common_handle_option): Disable allocas sanitization for KASan by default. gcc/testsuite/ * c-c++-common/asan/kasan-alloca-1.c: New test. * c-c++-common/asan/kasan-alloca-2.c: Likewise. From-SVN: r250032
2017-07-06ASAN: Implement dynamic allocas/VLAs sanitization.Maxim Ostapenko1-0/+1
gcc/ * asan.c: Include gimple-fold.h. (get_last_alloca_addr): New function. (handle_builtin_stackrestore): Likewise. (handle_builtin_alloca): Likewise. (asan_emit_allocas_unpoison): Likewise. (get_mem_refs_of_builtin_call): Add new parameter, remove const quallifier from first paramerer. Handle BUILT_IN_ALLOCA, BUILT_IN_ALLOCA_WITH_ALIGN and BUILT_IN_STACK_RESTORE builtins. (instrument_builtin_call): Pass gimple iterator to get_mem_refs_of_builtin_call. (last_alloca_addr): New global. * asan.h (asan_emit_allocas_unpoison): Declare. * builtins.c (expand_asan_emit_allocas_unpoison): New function. (expand_builtin): Handle BUILT_IN_ASAN_ALLOCAS_UNPOISON. * cfgexpand.c (expand_used_vars): Call asan_emit_allocas_unpoison if function calls alloca. * gimple-fold.c (replace_call_with_value): Remove static keyword. * gimple-fold.h (replace_call_with_value): Declare. * internal-fn.c: Include asan.h. * sanitizer.def (BUILT_IN_ASAN_ALLOCA_POISON, BUILT_IN_ASAN_ALLOCAS_UNPOISON): New builtins. gcc/testsuite/ * c-c++-common/asan/alloca_big_alignment.c: New test. * c-c++-common/asan/alloca_detect_custom_size.c: Likewise. * c-c++-common/asan/alloca_instruments_all_paddings.c: Likewise. * c-c++-common/asan/alloca_loop_unpoisoning.c: Likewise. * c-c++-common/asan/alloca_overflow_partial.c: Likewise. * c-c++-common/asan/alloca_overflow_right.c: Likewise. * c-c++-common/asan/alloca_safe_access.c: Likewise. * c-c++-common/asan/alloca_underflow_left.c: Likewise. From-SVN: r250031
2017-06-13Implement no_sanitize function attributeMartin Liska1-7/+20
2017-06-13 Martin Liska <mliska@suse.cz> PR sanitize/78204 * c-c++-common/ubsan/attrib-2.c (float_cast2): Enhance the test by adding no_sanitize attribute. * gcc.dg/asan/use-after-scope-4.c: Likewise. 2017-06-13 Martin Liska <mliska@suse.cz> PR sanitize/78204 * c-attribs.c (add_no_sanitize_value): New function. (handle_no_sanitize_attribute): Likewise. (handle_no_sanitize_address_attribute): Use the function. (handle_no_sanitize_thread_attribute): New function. (handle_no_address_safety_analysis_attribute): Use add_no_sanitize_value. (handle_no_sanitize_undefined_attribute): Likewise. * c-common.h: Declare new functions. * c-ubsan.c (ubsan_instrument_division): Use sanitize_flags_p. (ubsan_instrument_shift): Likewise. (ubsan_instrument_bounds): Likewise. (ubsan_maybe_instrument_array_ref): Likewise. (ubsan_maybe_instrument_reference_or_call): Likewise. 2017-06-13 Martin Liska <mliska@suse.cz> PR sanitize/78204 * asan.c (asan_sanitize_stack_p): Use sanitize_flags_p. (gate_asan): Likewise. * asan.h (asan_no_sanitize_address_p): Remove the function. (sanitize_flags_p): New function. * builtins.def: Fix coding style. * common.opt: Use renamed enum value. * convert.c (convert_to_integer_1): Use sanitize_flags_p. * doc/extend.texi: Document no_sanitize attribute. * flag-types.h (enum sanitize_code): Rename SANITIZE_NONDEFAULT to SANITIZE_UNDEFINED_NONDEFAULT. * gcc.c (sanitize_spec_function): Use the renamed enum value. * gimple-fold.c (optimize_atomic_compare_exchange_p): Use sanitize_flags_p. * gimplify.c (gimplify_function_tree): Likewise. * ipa-inline.c (sanitize_attrs_match_for_inline_p): Likewise. * opts.c (parse_no_sanitize_attribute): New function. (common_handle_option): Use renamed enum value. * opts.h (parse_no_sanitize_attribute): Declare. * tree.c (sanitize_flags_p): New function. * tree.h: Declared here. * tsan.c: Use sanitize_flags_p. * ubsan.c (ubsan_expand_null_ifn): Likewise. (instrument_mem_ref): Likewise. (instrument_bool_enum_load): Likewise. (do_ubsan_in_current_function): Remove the function. (pass_ubsan::execute): Use sanitize_flags_p. * ubsan.h: Remove do_ubsan_in_current_function * tree-cfg.c (print_no_sanitize_attr_value): New function. (dump_function_to_file): Use it here. 2017-06-13 Martin Liska <mliska@suse.cz> PR sanitize/78204 * class.c (build_base_path): Use sanitize_flags_p. * cp-gimplify.c (cp_genericize_r): Likewise. (cp_genericize_tree): Likewise. (cp_genericize): Likewise. * cp-ubsan.c (cp_ubsan_instrument_vptr_p): Likewise. * decl.c (compute_array_index_type): Likewise. (start_preparsed_function): Likewise. * decl2.c (one_static_initialization_or_destruction): Likewise. * init.c (finish_length_check): Likewise. * lambda.c (maybe_add_lambda_conv_op): Likewise. * typeck.c (cp_build_binary_op): Likewise. (build_static_cast_1): Likewise. 2017-06-13 Martin Liska <mliska@suse.cz> PR sanitize/78204 * c-convert.c (convert): Use sanitize_flags_p. * c-decl.c (grokdeclarator): Likewise. * c-typeck.c (convert_for_assignment): Likewise. (c_finish_return): Likewise. (build_binary_op): Likewise. From-SVN: r249158
2017-01-23use-after-scope: handle writes to a poisoned variableMartin Liska1-0/+2
2017-01-23 Martin Liska <mliska@suse.cz> * gcc.dg/asan/use-after-scope-10.c: New test. * gcc.dg/asan/use-after-scope-11.c: New test. * g++.dg/asan/use-after-scope-5.C: New test. 2017-01-23 Jakub Jelinek <jakub@redhat.com> Martin Liska <mliska@suse.cz> * asan.h: Define ASAN_USE_AFTER_SCOPE_ATTRIBUTE. * asan.c (asan_expand_poison_ifn): Support stores and use appropriate ASAN report function. * internal-fn.c (expand_ASAN_POISON_USE): New function. * internal-fn.def (ASAN_POISON_USE): Declare. * tree-into-ssa.c (maybe_add_asan_poison_write): New function. (maybe_register_def): Create ASAN_POISON_USE when sanitizing. * tree-ssa-dce.c (eliminate_unnecessary_stmts): Remove ASAN_POISON calls w/o LHS. * tree-ssa.c (execute_update_addresses_taken): Create clobber for ASAN_MARK (UNPOISON, &x, ...) in order to prevent usage of a LHS from ASAN_MARK (POISON, &x, ...) coming to a PHI node. * gimplify.c (asan_poison_variables): Add attribute use_after_scope_memory to variables that really needs to live in memory. * tree-ssa.c (is_asan_mark_p): Do not rewrite into SSA when having the attribute. From-SVN: r244793
2017-01-23Speed up use-after-scope (v2): rewrite into SSAMartin Liska1-0/+2
2017-01-23 Martin Liska <mliska@suse.cz> * asan.c (create_asan_shadow_var): New function. (asan_expand_poison_ifn): Likewise. * asan.h (asan_expand_poison_ifn): New declaration. * internal-fn.c (expand_ASAN_POISON): Likewise. * internal-fn.def (ASAN_POISON): New builtin. * sanopt.c (pass_sanopt::execute): Expand asan_expand_poison_ifn. * tree-inline.c (copy_decl_for_dup_finish): Make function external. * tree-inline.h (copy_decl_for_dup_finish): Likewise. * tree-ssa.c (is_asan_mark_p): New function. (execute_update_addresses_taken): Rewrite local variables (identified just by use-after-scope as addressable) into SSA. 2017-01-23 Martin Liska <mliska@suse.cz> * gcc.dg/asan/use-after-scope-3.c: Add additional flags. * gcc.dg/asan/use-after-scope-9.c: Likewise and grep for sanopt optimization for ASAN_POISON. From-SVN: r244791
2017-01-01Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r243994
2016-12-13Add pretty printer for ASAN_MARK and add a helper fnMartin Liska1-3/+8
* asan.c (asan_mark_poison_p): Remove. (asan_mark_p): New function. (transform_statements): Use the function. (asan_expand_mark_ifn): Do not use masked enum. * asan.h (enum asan_mark_flags): Declare it via a macro. * gimple-pretty-print.c (dump_gimple_call_args): Dump first argument of ASAN_MARK. * gimplify.c (build_asan_poison_call_expr): Use new enum values. (asan_poison_variable): Likewise. * gcc.dg/asan/use-after-scope-goto-1.c: Update first argument of scanned pattern ASAN_MARK. * gcc.dg/asan/use-after-scope-goto-2.c: Likewise. * gcc.dg/asan/use-after-scope-switch-1.c: Likewise. * gcc.dg/asan/use-after-scope-switch-2.c: Likewise. * gcc.dg/asan/use-after-scope-switch-3.c: Likewise. From-SVN: r243597
2016-11-09asan.h (asan_intercepted_p): Handle BUILT_IN_STRCSPN, BUILT_IN_STRPBRK, ↵Maxim Ostapenko1-0/+4
BUILT_IN_STRSPN and BUILT_IN_STRSTR. gcc/ * asan.h (asan_intercepted_p): Handle BUILT_IN_STRCSPN, BUILT_IN_STRPBRK, BUILT_IN_STRSPN and BUILT_IN_STRSTR. gcc/testsuite/ * c-c++-common/asan/default_options.h: New file. * c-c++-common/asan/strcasestr-1.c: New test. * c-c++-common/asan/strcasestr-2.c: Likewise. * c-c++-common/asan/strcspn-1.c: Likewise. * c-c++-common/asan/strcspn-2.c: Likewise. * c-c++-common/asan/strpbrk-1.c: Likewise. * c-c++-common/asan/strpbrk-2.c: Likewise. * c-c++-common/asan/strspn-1.c: Likewise. * c-c++-common/asan/strspn-2.c: Likewise. * c-c++-common/asan/strstr-1.c: Likewise. * c-c++-common/asan/strstr-2.c: Likewise. * c-c++-common/asan/halt_on_error_suppress_equal_pcs-1.c: Likewise. From-SVN: r241984
2016-11-09asan.h (ASAN_STACK_MAGIC_PARTIAL): Remove.Maxim Ostapenko1-1/+0
gcc/ * asan.h (ASAN_STACK_MAGIC_PARTIAL): Remove. * asan.c (ASAN_STACK_MAGIC_PARTIAL): Replace with ASAN_STACK_MAGIC_MIDDLE. (asan_global_struct): Increase the size of fields. (asan_add_global): Add new field constructor. * sanitizer.def (__asan_version_mismatch_check_v6): Replace with __asan_version_mismatch_check_v8. gcc/testsuite/ * c-c++-common/asan/null-deref-1.c: Adjust testcase. From-SVN: r241983
2016-11-07Introduce -fsanitize-address-use-after-scopeMartin Liska1-11/+55
* c-warn.c (warn_for_unused_label): Save all labels used in goto or in &label. * asan.c (enum asan_check_flags): Move the enum to header file. (asan_init_shadow_ptr_types): Make type creation more generic. (shadow_mem_size): New function. (asan_emit_stack_protection): Use newly added ASAN_SHADOW_GRANULARITY. Rewritten stack unpoisoning code. (build_shadow_mem_access): Add new argument return_address. (instrument_derefs): Instrument local variables if use after scope sanitization is enabled. (asan_store_shadow_bytes): New function. (asan_expand_mark_ifn): Likewise. (asan_sanitize_stack_p): Moved from asan_sanitize_stack_p. * asan.h (enum asan_mark_flags): Moved here from asan.c (asan_protect_stack_decl): Protect all declaration that need to live in memory. (asan_sanitize_use_after_scope): New function. (asan_no_sanitize_address_p): Likewise. * cfgexpand.c (partition_stack_vars): Consider asan_sanitize_use_after_scope in condition. (expand_stack_vars): Likewise. * common.opt (-fsanitize-address-use-after-scope): New option. * doc/invoke.texi (use-after-scope-direct-emission-threshold): Explain the parameter. * flag-types.h (enum sanitize_code): Define SANITIZE_USE_AFTER_SCOPE. * gimplify.c (build_asan_poison_call_expr): New function. (asan_poison_variable): Likewise. (gimplify_bind_expr): Generate poisoning/unpoisoning for local variables that have address taken. (gimplify_decl_expr): Likewise. (gimplify_target_expr): Likewise for C++ temporaries. (sort_by_decl_uid): New function. (gimplify_expr): Unpoison all variables for a label we can jump from outside of a scope. (gimplify_switch_expr): Unpoison variables defined in the switch context. (gimplify_function_tree): Clear asan_poisoned_variables. (asan_poison_variables): New function. (warn_switch_unreachable_r): Handle IFN_ASAN_MARK. * internal-fn.c (expand_ASAN_MARK): New function. * internal-fn.def (ASAN_MARK): Declare. * opts.c (finish_options): Handle -fstack-reuse if -fsanitize-address-use-after-scope is enabled. (common_handle_option): Enable address sanitization if -fsanitize-address-use-after-scope is enabled. * params.def (PARAM_USE_AFTER_SCOPE_DIRECT_EMISSION_THRESHOLD): New parameter. * params.h: Likewise. * sancov.c (pass_sanopt::execute): Handle IFN_ASAN_MARK. * sanitizer.def: Define __asan_poison_stack_memory and __asan_unpoison_stack_memory functions. * asan.c (asan_mark_poison_p): New function. (transform_statements): Handle asan_mark_poison_p calls. * gimple.c (nonfreeing_call_p): Handle IFN_ASAN_MARK. From-SVN: r241896
2016-01-04Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r232055
2015-04-17asan.c (set_sanitized_sections): New function.Yury Gribov1-0/+2
2015-04-17 Yury Gribov <y.gribov@samsung.com> gcc/ * asan.c (set_sanitized_sections): New function. (section_sanitized_p): Ditto. (asan_protect_global): Optionally sanitize user-defined sections. * asan.h (set_sanitized_sections): Declare new function. * common.opt (fsanitize-sections): New option. * doc/invoke.texi (-fsanitize-sections): Document new option. * opts-global.c (handle_common_deferred_options): Handle new option. gcc/testsuite/ * c-c++-common/asan/user-section-1.c: New test. From-SVN: r222168
2015-01-05Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r219188
2014-11-04Makefile.in (OBJS): Add sanopt.o.Marek Polacek1-0/+1
* Makefile.in (OBJS): Add sanopt.o. (GTFILES): Add sanopt.c. * asan.h (asan_expand_check_ifn): Declare. * asan.c (asan_expand_check_ifn): No longer static. (class pass_sanopt, pass_sanopt::execute, make_pass_sanopt): Move... * sanopt.c: ...here. New file. testsuite/ * c-c++-common/ubsan/align-2.c: Remove dg-output. * c-c++-common/ubsan/align-4.c: Likewise. * g++.dg/ubsan/null-1.C: Likewise. * g++.dg/ubsan/null-2.C: Likewise. From-SVN: r217099
2014-10-28Don't inline GCC memory builtins if ASan is enabled.Max Ostapenko1-0/+24
gcc/ 2014-10-28 Max Ostapenko <m.ostapenko@partner.samsung.com> * asan.h (asan_intercepted_p): New function. * asan.c (asan_mem_ref_hasher::hash): Remove MEM_REF access size from hash value construction. Call iterative_hash_expr instead of explicit hash building. (asan_mem_ref_hasher::equal): Change condition. (has_mem_ref_been_instrumented): Likewise. (update_mem_ref_hash_table): Likewise. (maybe_update_mem_ref_hash_table): New function. (instrument_strlen_call): Removed. (get_mem_refs_of_builtin_call): Handle new parameter. (instrument_builtin_call): Call maybe_update_mem_ref_hash_table instead of instrument_mem_region_access if intercepted_p is true. (instrument_mem_region_access): Instrument only base with len instead of base and end with 1. (build_check_stmt): Remove start_instrumented and end_instrumented parameters. (enum asan_check_flags): Remove ASAN_CHECK_START_INSTRUMENTED and ASAN_CHECK_END_INSTRUMENTED. Change ASAN_CHECK_LAST. (asan_expand_check_ifn): Remove start_instrumented and end_instrumented. * builtins.c (expand_builtin): Include asan.h. Don't expand string/memory builtin functions that have interceptors if ASan is enabled. gcc/testsuite/ * c-c++-common/asan/no-redundant-instrumentation-1.c: Updated test. * c-c++-common/asan/no-redundant-instrumentation-4.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-5.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-6.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-7.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-8.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-2.c: Removed. * c-c++-common/asan/no-redundant-instrumentation-9.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-10.c: New test. * c-c++-common/asan/no-redundant-instrumentation-11.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-12.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-13.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-14.c: Likewise. * c-c++-common/asan/no-redundant-instrumentation-15.c: Likewise. * c-c++-common/asan/pr63638.c: Likewise. From-SVN: r216783
2014-10-28Allow to override Asan shadow offset.Yury Gribov1-1/+3
2014-10-28 Yury Gribov <y.gribov@samsung.com> gcc/ * asan.c (set_asan_shadow_offset): New function. (asan_shadow_offset): Likewise. (asan_emit_stack_protection): Call asan_shadow_offset. (build_shadow_mem_access): Likewise. * asan.h (set_asan_shadow_offset): Declare. * common.opt (fasan-shadow-offset): New option. (frandom-seed): Fixed parameter name. * doc/invoke.texi (fasan-shadow-offset): Describe new option. (frandom-seed): Fixed parameter name. * opts-global.c (handle_common_deferred_options): Handle -fasan-shadow-offset. * opts.c (common_handle_option): Likewise. gcc/testsuite/ * c-c++-common/asan/shadow-offset-1.c: New test. From-SVN: r216773
2014-08-19asan_emit_stack_protection returns an insnDavid Malcolm1-2/+2
2014-08-19 David Malcolm <dmalcolm@redhat.com> * asan.h (asan_emit_stack_protection): Strengthen return type from rtx to rtx_insn *. * asan.c (asan_emit_stack_protection): Likewise. Add local "insns" to hold the return value. From-SVN: r214189
2014-01-02Update copyright years in gcc/Richard Sandiford1-1/+1
From-SVN: r206289
2013-12-20ubsan.c: Include tree-ssanames.h, asan.h and gimplify-me.h.Jakub Jelinek1-0/+3
* ubsan.c: Include tree-ssanames.h, asan.h and gimplify-me.h. (ubsan_type_descriptor): Handle BOOLEAN_TYPE and ENUMERAL_TYPE like INTEGER_TYPE. (instrument_bool_enum_load): New function. (ubsan_pass): Call it. (gate_ubsan): Also enable for SANITIZE_BOOL or SANITIZE_ENUM. * asan.c (create_cond_insert_point): No longer static. * asan.h (create_cond_insert_point): Declare. * sanitizer.def (BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE): New built-in. * opts.c (common_handle_option): Handle -fsanitize=bool and -fsanitize=enum. * builtins.c (fold_builtin_memory_op): When sanitizing bool and enum loads, don't use enum or bool types for memcpy folding. * flag-types.h (SANITIZE_BOOL, SANITIZE_ENUM): New. (SANITIZE_UNDEFINED): Or these in. * c-c++-common/ubsan/load-bool-enum.c: New test. From-SVN: r206143
2013-11-28cfgexpand.c (struct stack_vars_data): Add asan_base and asan_alignb fields.Jakub Jelinek1-2/+5
* cfgexpand.c (struct stack_vars_data): Add asan_base and asan_alignb fields. (expand_stack_vars): For -fsanitize=address, use (and set initially) data->asan_base as base for vars and update asan_alignb. (expand_used_vars): Initialize data.asan_base and data.asan_alignb. Pass them to asan_emit_stack_protection. * asan.c (asan_detect_stack_use_after_return): New variable. (asan_emit_stack_protection): Add pbase and alignb arguments. Implement use after return sanitization. * asan.h (asan_emit_stack_protection): Adjust prototype. (ASAN_STACK_MAGIC_USE_AFTER_RET, ASAN_STACK_RETIRED_MAGIC): Define. From-SVN: r205476
2013-11-22sanitizer.def (BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT, [...]): New.Jakub Jelinek1-0/+1
* sanitizer.def (BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT, BUILT_IN_ASAN_AFTER_DYNAMIC_INIT): New. * asan.c (instrument_derefs): Handle also VAR_DECL loads/stores. Don't instrument accesses to VAR_DECLs which are known to fit into their bounds and the vars are known to have shadow bytes indicating allowed access. (asan_dynamic_init_call): New function. (asan_add_global): If vnode->dynamically_initialized, set __has_dynamic_init to 1 instead of 0. (initialize_sanitizer_builtins): Add BT_FN_VOID_CONST_PTR var. * asan.h (asan_dynamic_init_call): New prototype. * cgraph.h (varpool_node): Add dynamically_initialized bitfield. cp/ * decl2.c: Include asan.h. (one_static_initialization_or_destruction): If -fsanitize=address, init is non-NULL and guard is NULL, set vnode->dynamically_initialized. (do_static_initialization_or_destruction): Call __asan_{before,after}_dynamic_init around the static initialization. testsuite/ * c-c++-common/asan/no-redundant-instrumentation-1.c: Tweak to avoid optimizing away some __asan_report* calls. From-SVN: r205282
2013-11-04libsanitizer merge from upstream r191666Kostya Serebryany1-0/+1
This may break gcc-asan on Mac, will follow up separately. From-SVN: r204368
2013-01-10Update copyright years in gcc/Richard Sandiford1-1/+1
From-SVN: r195098
2012-12-03sanitizer.def: Add Address Sanitizer builtins.Jakub Jelinek1-0/+1
* sanitizer.def: Add Address Sanitizer builtins. Rename BUILT_IN_TSAN_READ_* to BUILT_IN_TSAN_READ* and BUILT_IN_TSAN_WRITE_* to BUILT_IN_TSAN_WRITE*. * Makefile.in (asan.o): Depend on langhooks.h. (tsan.o): Depend on asan.h. * asan.h (initialize_sanitizer_builtins): New prototype. * asan.c: Include langhooks.h. (report_error_func): Use builtin_decl_implicit of corresponding BUILT_IN_ASAN_REPORT_{LOAD,STORE}*. (asan_init_func): Removed. (initialize_sanitizer_builtins): New function. (asan_finish_file): Call it. Use builtin_decl_implicit on BUILT_IN_ASAN_{INIT,{,UN}REGISTER_GLOBALS}. (asan_instrument): Call initialize_sanitizer_builtins. * builtins.def (DEF_SANITIZER_BUILTIN): Change condition to (flag_asan || flag_tsan). * tsan.c: Include asan.h and tsan.h. (get_memory_access_decl): Rename BUILT_IN_TSAN_{READ,WRITE}_* to BUILT_IN_TSAN_{READ,WRITE}*. (tsan_pass): Call initialize_sanitizer_builtins. (tsan_gate, tsan_gate_O0): Don't check if builtin_decl_implicit_p (BUILT_IN_TSAN_INIT) is true. (tsan_finish_file): Call initialize_sanitizer_builtins. * builtin-types.def (BT_FN_VOID_PTR_PTRMODE): New fn type. From-SVN: r194103
2012-11-12Implement protection of global variablesJakub Jelinek1-0/+11
This patch implements the protection of global variables. See the comments appended to the beginning of the asan.c file. * varasm.c: Include asan.h. (assemble_noswitch_variable): Grow size by asan_red_zone_size if decl is asan protected. (place_block_symbol): Likewise. (assemble_variable): If decl is asan protected, increase DECL_ALIGN if needed, and for decls emitted using assemble_variable_contents append padding zeros after it. * Makefile.in (varasm.o): Depend on asan.h. * asan.c: Include output.h. (asan_pp, asan_pp_initialized, asan_ctor_statements): New variables. (asan_pp_initialize, asan_pp_string): New functions. (asan_emit_stack_protection): Use asan_pp{,_initialized} instead of local pp{,_initialized} vars, use asan_pp_initialize and asan_pp_string helpers. (asan_needs_local_alias, asan_protect_global, asan_global_struct, asan_add_global): New functions. (asan_finish_file): Protect global vars that can be protected. Use asan_ctor_statements instead of ctor_statements * asan.h (asan_protect_global): New prototype. (asan_red_zone_size): New inline function. Co-Authored-By: Wei Mi <wmi@google.com> From-SVN: r193437
2012-11-12Implement protection of stack variablesJakub Jelinek1-1/+30
This patch implements the protection of stack variables. It lays out stack variables as well as the different red zones, emits some prologue code to populate the shadow memory as to poison (mark as non-accessible) the regions of the red zones and mark the regions of stack variables as accessible, and emit some epilogue code to un-poison (mark as accessible) the regions of red zones right before the function exits. * Makefile.in (asan.o): Depend on $(EXPR_H) $(OPTABS_H). (cfgexpand.o): Depend on asan.h. * asan.c: Include expr.h and optabs.h. (asan_shadow_set): New variable. (asan_shadow_cst, asan_emit_stack_protection): New functions. (asan_init_shadow_ptr_types): Initialize also asan_shadow_set. * cfgexpand.c: Include asan.h. Define HOST_WIDE_INT heap vector. (partition_stack_vars): If i is large alignment and j small alignment or vice versa, break out of the loop instead of continue, and put the test earlier. If flag_asan, break out of the loop if for small alignment size is different. (struct stack_vars_data): New type. (expand_stack_vars): Add DATA argument. Change PRED type to function taking size_t argument instead of tree. Adjust pred calls. Fill DATA in and add needed padding in between variables if -faddress-sanitizer. (defer_stack_allocation): Defer everything for flag_asan. (stack_protect_decl_phase_1, stack_protect_decl_phase_2): Take size_t index into stack_vars array instead of the decl directly. (asan_decl_phase_3): New function. (expand_used_vars): Return var destruction sequence. Adjust expand_stack_vars calls, add another one for flag_asan. Call asan_emit_stack_protection if expand_stack_vars added anything to the vectors. (expand_gimple_basic_block): Add disable_tail_calls argument. (gimple_expand_cfg): Pass true to it if expand_used_vars returned non-NULL. Emit the sequence returned by expand_used_vars after return_label. * asan.h (asan_emit_stack_protection): New prototype. (asan_shadow_set): New decl. (ASAN_RED_ZONE_SIZE, ASAN_STACK_MAGIC_LEFT, ASAN_STACK_MAGIC_MIDDLE, ASAN_STACK_MAGIC_RIGHT, ASAN_STACK_FRAME_MAGIC): Define. (asan_protect_stack_decl): New inline. * toplev.c (process_options): Also disable -faddress-sanitizer on !FRAME_GROWS_DOWNWARDS targets. From-SVN: r193436
2012-11-12Emit GIMPLE directly instead of gimplifying GENERIC.Jakub Jelinek1-1/+1
This patch cleanups the instrumentation code generation by emitting GIMPLE directly, as opposed to emitting GENERIC tree and then gimplifying them. It also does some cleanups here and there * Makefile.in (GTFILES): Add $(srcdir)/asan.c. (asan.o): Update the dependencies of asan.o. * asan.c (tm.h, tree.h, tm_p.h, basic-block.h, flags.h function.h, tree-inline.h, tree-dump.h, diagnostic.h, demangle.h, langhooks.h, ggc.h, cgraph.h, gimple.h): Remove these unused but included headers. (shadow_ptr_types): New variable. (report_error_func): Change is_store argument to bool, don't append newline to function name. (PROB_VERY_UNLIKELY, PROB_ALWAYS): Define. (build_check_stmt): Change is_store argument to bool. Emit GIMPLE directly instead of creating trees and gimplifying them. Mark the error reporting function as very unlikely. (instrument_derefs): Change is_store argument to bool. Use int_size_in_bytes to compute size_in_bytes, simplify size check. Use build_fold_addr_expr instead of build_addr. (transform_statements): Adjust instrument_derefs caller. Use gimple_assign_single_p as stmt test. Don't look at MEM refs in rhs2. (asan_init_shadow_ptr_types): New function. (asan_instrument): Don't push/pop gimplify context. Call asan_init_shadow_ptr_types if not yet initialized. * asan.h (ASAN_SHADOW_SHIFT): Adjust comment. Co-Authored-By: Dodji Seketeli <dodji@redhat.com> Co-Authored-By: Xinliang David Li <davidxl@google.com> From-SVN: r193434
2012-11-12Initial asan cleanupsJakub Jelinek1-1/+5
This patch defines a new asan_shadow_offset target macro, instead of having a mere macro in the asan.c file. It becomes thus cleaner to define the target macro for targets that supports asan, namely x86 for now. The ASAN_SHADOW_SHIFT (which, along with the asan_shadow_offset constant, is used to compute the address of the shadow memory byte for a given memory address) is defined in asan.h. gcc/ChangeLog * toplev.c (process_options): Warn and turn off -faddress-sanitizer if not supported by target. * asan.c: Include target.h. (asan_scale, asan_offset_log_32, asan_offset_log_64, asan_offset_log): Removed. (build_check_stmt): Use ASAN_SHADOW_SHIFT and targetm.asan_shadow_offset (). (asan_instrument): Don't initialize asan_offset_log. * asan.h (ASAN_SHADOW_SHIFT): Define. * target.def (TARGET_ASAN_SHADOW_OFFSET): New hook. * doc/tm.texi.in (TARGET_ASAN_SHADOW_OFFSET): Add it. * doc/tm.texi: Regenerated. * Makefile.in (asan.o): Depend on $(TARGET_H). * config/i386/i386.c (ix86_asan_shadow_offset): New function. (TARGET_ASAN_SHADOW_OFFSET): Define. From-SVN: r193433
2012-11-12Initial import of asan from the Google branchWei Mi1-0/+26
This patch imports the initial state of asan as it was in the Google branch. It provides basic infrastructure for asan to instrument memory accesses on the heap, at -O3. Note that it supports neither stack nor global variable protection. The rest of the patches of the set is intended to further improve this base. gcc/ChangeLog * Makefile.in: Add asan.c and its dependencies. * common.opt: Add -faddress-sanitizer option. * invoke.texi: Document the new flag. * passes.c: Add the asan pass. * toplev.c (compile_file): Call asan_finish_file. * asan.c: New file. * asan.h: New file. * tree-pass.h: Declare pass_asan. Co-Authored-By: Diego Novillo <dnovillo@google.com> Co-Authored-By: Dodji Seketeli <dodji@redhat.com> From-SVN: r193432