Commit Graph

26 Commits

Author SHA1 Message Date
Martin Liska f6b9f2ffc1 use-after-scope: handle writes to a poisoned variable
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-23 12:06:13 +00:00
Martin Liska c7775327e8 Speed up use-after-scope (v2): rewrite into SSA
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-23 12:02:13 +00:00
Jakub Jelinek cbe34bb5ed Update copyright years.
From-SVN: r243994
2017-01-01 13:07:43 +01:00
Martin Liska 56b7aede91 Add pretty printer for ASAN_MARK and add a helper fn
* 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-12-13 09:14:47 +00:00
Maxim Ostapenko 0290a29200 asan.h (asan_intercepted_p): Handle BUILT_IN_STRCSPN, BUILT_IN_STRPBRK, 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-09 00:16:58 +02:00
Maxim Ostapenko fbdb92ebff asan.h (ASAN_STACK_MAGIC_PARTIAL): Remove.
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-09 00:15:35 +02:00
Martin Liska 6dc4a60450 Introduce -fsanitize-address-use-after-scope
* 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-11-07 10:23:38 +00:00
Jakub Jelinek 818ab71a41 Update copyright years.
From-SVN: r232055
2016-01-04 15:30:50 +01:00
Yury Gribov 18af8d16cf asan.c (set_sanitized_sections): New function.
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-04-17 07:51:02 +00:00
Jakub Jelinek 5624e564d2 Update copyright years.
From-SVN: r219188
2015-01-05 13:33:28 +01:00
Marek Polacek 06cefae97e Makefile.in (OBJS): Add sanopt.o.
* 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-11-04 19:43:01 +00:00
Max Ostapenko bdea98ca2e Don't inline GCC memory builtins if ASan is enabled.
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-28 14:36:54 +02:00
Yury Gribov fd960af2df Allow to override Asan shadow offset.
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-10-28 09:46:29 +00:00
David Malcolm 3a4abd2f6c asan_emit_stack_protection returns an insn
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-08-19 19:48:08 +00:00
Richard Sandiford 23a5b65a92 Update copyright years in gcc/
From-SVN: r206289
2014-01-02 22:23:26 +00:00
Jakub Jelinek ac0ff9f273 ubsan.c: Include tree-ssanames.h, asan.h and gimplify-me.h.
* 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-12-20 10:05:04 +01:00
Jakub Jelinek e361382f65 cfgexpand.c (struct stack_vars_data): Add asan_base and asan_alignb fields.
* 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-28 09:18:59 +01:00
Jakub Jelinek 59b36ecf23 sanitizer.def (BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT, [...]): New.
* 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-22 21:04:45 +01:00
Kostya Serebryany ef1b3fda32 libsanitizer merge from upstream r191666
This may break gcc-asan on Mac, will follow up separately.

From-SVN: r204368
2013-11-04 21:33:31 +00:00
Richard Sandiford d1e082c2c2 Update copyright years in gcc/
From-SVN: r195098
2013-01-10 20:38:27 +00:00
Jakub Jelinek 0e668eaf77 sanitizer.def: Add Address Sanitizer builtins.
* 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-12-03 18:28:10 +01:00
Jakub Jelinek 8240018b0c Implement protection of global variables
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-12 16:52:42 +01:00
Jakub Jelinek f3ddd6929a Implement protection of stack variables
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-12 16:52:26 +01:00
Jakub Jelinek f6d9848430 Emit GIMPLE directly instead of gimplifying GENERIC.
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-12 16:51:53 +01:00
Jakub Jelinek dfe06d3e7f Initial asan cleanups
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-12 16:51:34 +01:00
Wei Mi 37d6f666ca Initial import of asan from the Google branch
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
2012-11-12 16:51:13 +01:00