PR c++/68795: fix uninitialized close_paren_loc in cp_parser_postfix_expression
[gcc.git] / gcc / asan.c
index ba933c0a5e847606492e6fecdf9c6f244ad05031..2f9f92fa676748718e17f6e8f29d0b06c7a9dafa 100644 (file)
@@ -1,5 +1,5 @@
 /* AddressSanitizer, a fast memory error detector.
-   Copyright (C) 2012-2014 Free Software Foundation, Inc.
+   Copyright (C) 2012-2016 Free Software Foundation, Inc.
    Contributed by Kostya Serebryany <kcc@google.com>
 
 This file is part of GCC.
@@ -22,40 +22,41 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "backend.h"
+#include "target.h"
+#include "rtl.h"
 #include "tree.h"
-#include "hash-table.h"
-#include "basic-block.h"
-#include "tree-ssa-alias.h"
-#include "internal-fn.h"
-#include "gimple-expr.h"
-#include "is-a.h"
-#include "inchash.h"
 #include "gimple.h"
+#include "cfghooks.h"
+#include "alloc-pool.h"
+#include "tree-pass.h"
+#include "tm_p.h"
+#include "stringpool.h"
+#include "tree-ssanames.h"
+#include "optabs.h"
+#include "emit-rtl.h"
+#include "cgraph.h"
+#include "gimple-pretty-print.h"
+#include "alias.h"
+#include "fold-const.h"
+#include "cfganal.h"
 #include "gimplify.h"
 #include "gimple-iterator.h"
-#include "calls.h"
 #include "varasm.h"
 #include "stor-layout.h"
 #include "tree-iterator.h"
-#include "cgraph.h"
-#include "stringpool.h"
-#include "tree-ssanames.h"
-#include "tree-pass.h"
 #include "asan.h"
-#include "gimple-pretty-print.h"
-#include "target.h"
+#include "dojump.h"
+#include "explow.h"
 #include "expr.h"
-#include "optabs.h"
 #include "output.h"
-#include "tm_p.h"
 #include "langhooks.h"
-#include "alloc-pool.h"
 #include "cfgloop.h"
 #include "gimple-builder.h"
 #include "ubsan.h"
-#include "predict.h"
 #include "params.h"
 #include "builtins.h"
+#include "fnmatch.h"
 
 /* AddressSanitizer finds out-of-bounds and use-after-free bugs
    with <2x slowdown on average.
@@ -143,7 +144,7 @@ along with GCC; see the file COPYING3.  If not see
 
        where '(...){n}' means the content inside the parenthesis occurs 'n'
        times, with 'n' being the number of variables on the stack.
-     
+
      3/ The following 8 bytes contain the PC of the current function which
      will be used by the run-time library to print an error message.
 
@@ -238,6 +239,78 @@ along with GCC; see the file COPYING3.  If not see
    A destructor function that calls the runtime asan library function
    _asan_unregister_globals is also installed.  */
 
+static unsigned HOST_WIDE_INT asan_shadow_offset_value;
+static bool asan_shadow_offset_computed;
+static vec<char *> sanitized_sections;
+
+/* Sets shadow offset to value in string VAL.  */
+
+bool
+set_asan_shadow_offset (const char *val)
+{
+  char *endp;
+
+  errno = 0;
+#ifdef HAVE_LONG_LONG
+  asan_shadow_offset_value = strtoull (val, &endp, 0);
+#else
+  asan_shadow_offset_value = strtoul (val, &endp, 0);
+#endif
+  if (!(*val != '\0' && *endp == '\0' && errno == 0))
+    return false;
+
+  asan_shadow_offset_computed = true;
+
+  return true;
+}
+
+/* Set list of user-defined sections that need to be sanitized.  */
+
+void
+set_sanitized_sections (const char *sections)
+{
+  char *pat;
+  unsigned i;
+  FOR_EACH_VEC_ELT (sanitized_sections, i, pat)
+    free (pat);
+  sanitized_sections.truncate (0);
+
+  for (const char *s = sections; *s; )
+    {
+      const char *end;
+      for (end = s; *end && *end != ','; ++end);
+      size_t len = end - s;
+      sanitized_sections.safe_push (xstrndup (s, len));
+      s = *end ? end + 1 : end;
+    }
+}
+
+/* Checks whether section SEC should be sanitized.  */
+
+static bool
+section_sanitized_p (const char *sec)
+{
+  char *pat;
+  unsigned i;
+  FOR_EACH_VEC_ELT (sanitized_sections, i, pat)
+    if (fnmatch (pat, sec, FNM_PERIOD) == 0)
+      return true;
+  return false;
+}
+
+/* Returns Asan shadow offset.  */
+
+static unsigned HOST_WIDE_INT
+asan_shadow_offset ()
+{
+  if (!asan_shadow_offset_computed)
+    {
+      asan_shadow_offset_computed = true;
+      asan_shadow_offset_value = targetm.asan_shadow_offset ();
+    }
+  return asan_shadow_offset_value;
+}
+
 alias_set_type asan_shadow_set = -1;
 
 /* Pointer types to 1 resp. 2 byte integers in shadow memory.  A separate
@@ -253,9 +326,7 @@ enum asan_check_flags
   ASAN_CHECK_STORE = 1 << 0,
   ASAN_CHECK_SCALAR_ACCESS = 1 << 1,
   ASAN_CHECK_NON_ZERO_LEN = 1 << 2,
-  ASAN_CHECK_START_INSTRUMENTED = 1 << 3,
-  ASAN_CHECK_END_INSTRUMENTED = 1 << 4,
-  ASAN_CHECK_LAST
+  ASAN_CHECK_LAST = 1 << 3
 };
 
 /* Hashtable support for memory references used by gimple
@@ -271,21 +342,7 @@ struct asan_mem_ref
   HOST_WIDE_INT access_size;
 };
 
-static alloc_pool asan_mem_ref_alloc_pool;
-
-/* This creates the alloc pool used to store the instances of
-   asan_mem_ref that are stored in the hash table asan_mem_ref_ht.  */
-
-static alloc_pool
-asan_mem_ref_get_alloc_pool ()
-{
-  if (asan_mem_ref_alloc_pool == NULL)
-    asan_mem_ref_alloc_pool = create_alloc_pool ("asan_mem_ref",
-                                                sizeof (asan_mem_ref),
-                                                10);
-  return asan_mem_ref_alloc_pool;
-    
-}
+object_allocator <asan_mem_ref> asan_mem_ref_pool ("asan_mem_ref");
 
 /* Initializes an instance of asan_mem_ref.  */
 
@@ -305,8 +362,7 @@ asan_mem_ref_init (asan_mem_ref *ref, tree start, HOST_WIDE_INT access_size)
 static asan_mem_ref*
 asan_mem_ref_new (tree start, HOST_WIDE_INT access_size)
 {
-  asan_mem_ref *ref =
-    (asan_mem_ref *) pool_alloc (asan_mem_ref_get_alloc_pool ());
+  asan_mem_ref *ref = asan_mem_ref_pool.allocate ();
 
   asan_mem_ref_init (ref, start, access_size);
   return ref;
@@ -337,14 +393,10 @@ asan_mem_ref_get_end (const asan_mem_ref *ref, tree len)
   return asan_mem_ref_get_end (ref->start, len);
 }
 
-struct asan_mem_ref_hasher
-  : typed_noop_remove <asan_mem_ref>
+struct asan_mem_ref_hasher : nofree_ptr_hash <asan_mem_ref>
 {
-  typedef asan_mem_ref value_type;
-  typedef asan_mem_ref compare_type;
-
-  static inline hashval_t hash (const value_type *);
-  static inline bool equal (const value_type *, const compare_type *);
+  static inline hashval_t hash (const asan_mem_ref *);
+  static inline bool equal (const asan_mem_ref *, const asan_mem_ref *);
 };
 
 /* Hash a memory reference.  */
@@ -352,10 +404,7 @@ struct asan_mem_ref_hasher
 inline hashval_t
 asan_mem_ref_hasher::hash (const asan_mem_ref *mem_ref)
 {
-  inchash::hash hstate;
-  inchash::add_expr (mem_ref->start, hstate);
-  hstate.add_wide_int (mem_ref->access_size);
-  return hstate.end ();
+  return iterative_hash_expr (mem_ref->start, 0);
 }
 
 /* Compare two memory references.  We accept the length of either
@@ -365,8 +414,7 @@ inline bool
 asan_mem_ref_hasher::equal (const asan_mem_ref *m1,
                            const asan_mem_ref *m2)
 {
-  return (m1->access_size == m2->access_size
-         && operand_equal_p (m1->start, m2->start, 0));
+  return operand_equal_p (m1->start, m2->start, 0);
 }
 
 static hash_table<asan_mem_ref_hasher> *asan_mem_ref_ht;
@@ -402,11 +450,7 @@ free_mem_ref_resources ()
   delete asan_mem_ref_ht;
   asan_mem_ref_ht = NULL;
 
-  if (asan_mem_ref_alloc_pool)
-    {
-      free_alloc_pool (asan_mem_ref_alloc_pool);
-      asan_mem_ref_alloc_pool = NULL;
-    }
+  asan_mem_ref_pool.release ();
 }
 
 /* Return true iff the memory reference REF has been instrumented.  */
@@ -417,7 +461,8 @@ has_mem_ref_been_instrumented (tree ref, HOST_WIDE_INT access_size)
   asan_mem_ref r;
   asan_mem_ref_init (&r, ref, access_size);
 
-  return (get_mem_ref_hash_table ()->find (&r) != NULL);
+  asan_mem_ref *saved_ref = get_mem_ref_hash_table ()->find (&r);
+  return saved_ref && saved_ref->access_size >= access_size;
 }
 
 /* Return true iff the memory reference REF has been instrumented.  */
@@ -434,19 +479,11 @@ has_mem_ref_been_instrumented (const asan_mem_ref *ref)
 static bool
 has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len)
 {
-  /* First let's see if the address of the beginning of REF has been
-     instrumented.  */
-  if (!has_mem_ref_been_instrumented (ref))
-    return false;
+  HOST_WIDE_INT size_in_bytes
+    = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
 
-  if (len != 0)
-    {
-      /* Let's see if the end of the region has been instrumented.  */
-      if (!has_mem_ref_been_instrumented (asan_mem_ref_get_end (ref, len),
-                                         ref->access_size))
-       return false;
-    }
-  return true;
+  return size_in_bytes != -1
+    && has_mem_ref_been_instrumented (ref->start, size_in_bytes);
 }
 
 /* Set REF to the memory reference present in a gimple assignment
@@ -454,7 +491,7 @@ has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len)
    otherwise.  */
 
 static bool
-get_mem_ref_of_assignment (const gimple assignment,
+get_mem_ref_of_assignment (const gassign *assignment,
                           asan_mem_ref *ref,
                           bool *ref_is_store)
 {
@@ -482,7 +519,7 @@ get_mem_ref_of_assignment (const gimple assignment,
    representing a builtin call that has to do with memory access.  */
 
 static bool
-get_mem_refs_of_builtin_call (const gimple call,
+get_mem_refs_of_builtin_call (const gcall *call,
                              asan_mem_ref *src0,
                              tree *src0_len,
                              bool *src0_is_store,
@@ -492,7 +529,8 @@ get_mem_refs_of_builtin_call (const gimple call,
                              asan_mem_ref *dst,
                              tree *dst_len,
                              bool *dst_is_store,
-                             bool *dest_is_deref)
+                             bool *dest_is_deref,
+                             bool *intercepted_p)
 {
   gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL));
 
@@ -502,6 +540,8 @@ get_mem_refs_of_builtin_call (const gimple call,
   bool is_store = true, got_reference_p = false;
   HOST_WIDE_INT access_size = 1;
 
+  *intercepted_p = asan_intercepted_p ((DECL_FUNCTION_CODE (callee)));
+
   switch (DECL_FUNCTION_CODE (callee))
     {
       /* (s, s, n) style memops.  */
@@ -814,7 +854,7 @@ get_mem_refs_of_builtin_call (const gimple call,
    contains.  */
 
 static bool
-has_stmt_been_instrumented_p (gimple stmt)
+has_stmt_been_instrumented_p (gimple *stmt)
 {
   if (gimple_assign_single_p (stmt))
     {
@@ -822,7 +862,8 @@ has_stmt_been_instrumented_p (gimple stmt)
       asan_mem_ref r;
       asan_mem_ref_init (&r, NULL, 1);
 
-      if (get_mem_ref_of_assignment (stmt, &r, &r_is_store))
+      if (get_mem_ref_of_assignment (as_a <gassign *> (stmt), &r,
+                                    &r_is_store))
        return has_mem_ref_been_instrumented (&r);
     }
   else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
@@ -834,12 +875,12 @@ has_stmt_been_instrumented_p (gimple stmt)
 
       tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE;
       bool src0_is_store = false, src1_is_store = false,
-       dest_is_store = false, dest_is_deref = false;
-      if (get_mem_refs_of_builtin_call (stmt,
+       dest_is_store = false, dest_is_deref = false, intercepted_p = true;
+      if (get_mem_refs_of_builtin_call (as_a <gcall *> (stmt),
                                        &src0, &src0_len, &src0_is_store,
                                        &src1, &src1_len, &src1_is_store,
                                        &dest, &dest_len, &dest_is_store,
-                                       &dest_is_deref))
+                                       &dest_is_deref, &intercepted_p))
        {
          if (src0.start != NULL_TREE
              && !has_mem_ref_been_instrumented (&src0, src0_len))
@@ -870,7 +911,7 @@ update_mem_ref_hash_table (tree ref, HOST_WIDE_INT access_size)
   asan_mem_ref_init (&r, ref, access_size);
 
   asan_mem_ref **slot = ht->find_slot (&r, INSERT);
-  if (*slot == NULL)
+  if (*slot == NULL || (*slot)->access_size < access_size)
     *slot = asan_mem_ref_new (ref, access_size);
 }
 
@@ -1083,12 +1124,16 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb,
       snprintf (buf, sizeof buf, "__asan_stack_malloc_%d",
                use_after_return_class);
       ret = init_one_libfunc (buf);
-      rtx addr = convert_memory_address (ptr_mode, base);
-      ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode, 2,
+      ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode, 1,
                                     GEN_INT (asan_frame_size
                                              + base_align_bias),
-                                    TYPE_MODE (pointer_sized_int_node),
-                                    addr, ptr_mode);
+                                    TYPE_MODE (pointer_sized_int_node));
+      /* __asan_stack_malloc_[n] returns a pointer to fake stack if succeeded
+        and NULL otherwise.  Check RET value is NULL here and jump over the
+        BASE reassignment in this case.  Otherwise, reassign BASE to RET.  */
+      int very_unlikely = REG_BR_PROB_BASE / 2000 - 1;
+      emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX,
+                              VOIDmode, 0, lab, very_unlikely);
       ret = convert_memory_address (Pmode, ret);
       emit_move_insn (base, ret);
       emit_label (lab);
@@ -1124,7 +1169,7 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb,
                              NULL_RTX, 1, OPTAB_DIRECT);
   shadow_base
     = plus_constant (Pmode, shadow_base,
-                    targetm.asan_shadow_offset ()
+                    asan_shadow_offset ()
                     + (base_align_bias >> ASAN_SHADOW_SHIFT));
   gcc_assert (asan_shadow_set != -1
              && (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4);
@@ -1305,14 +1350,17 @@ asan_protect_global (tree decl)
         the var that is selected by the linker will have
         padding or not.  */
       || DECL_ONE_ONLY (decl)
-      /* Similarly for common vars.  People can use -fno-common.  */
+      /* Similarly for common vars.  People can use -fno-common.
+        Note: Linux kernel is built with -fno-common, so we do instrument
+        globals there even if it is C.  */
       || (DECL_COMMON (decl) && TREE_PUBLIC (decl))
       /* Don't protect if using user section, often vars placed
         into user section from multiple TUs are then assumed
         to be an array of such vars, putting padding in there
         breaks this assumption.  */
       || (DECL_SECTION_NAME (decl) != NULL
-         && !symtab_node::get (decl)->implicit_section)
+         && !symtab_node::get (decl)->implicit_section
+         && !section_sanitized_p (DECL_SECTION_NAME (decl)))
       || DECL_SIZE (decl) == 0
       || ASAN_RED_ZONE_SIZE * BITS_PER_UNIT > MAX_OFILE_ALIGNMENT
       || !valid_constant_size_p (DECL_SIZE_UNIT (decl))
@@ -1344,44 +1392,72 @@ asan_protect_global (tree decl)
    IS_STORE is either 1 (for a store) or 0 (for a load).  */
 
 static tree
-report_error_func (bool is_store, HOST_WIDE_INT size_in_bytes, int *nargs)
-{
-  static enum built_in_function report[2][6]
-    = { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2,
-         BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8,
-         BUILT_IN_ASAN_REPORT_LOAD16, BUILT_IN_ASAN_REPORT_LOAD_N },
-       { BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2,
-         BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8,
-         BUILT_IN_ASAN_REPORT_STORE16, BUILT_IN_ASAN_REPORT_STORE_N } };
+report_error_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes,
+                  int *nargs)
+{
+  static enum built_in_function report[2][2][6]
+    = { { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2,
+           BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8,
+           BUILT_IN_ASAN_REPORT_LOAD16, BUILT_IN_ASAN_REPORT_LOAD_N },
+         { BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2,
+           BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8,
+           BUILT_IN_ASAN_REPORT_STORE16, BUILT_IN_ASAN_REPORT_STORE_N } },
+       { { BUILT_IN_ASAN_REPORT_LOAD1_NOABORT,
+           BUILT_IN_ASAN_REPORT_LOAD2_NOABORT,
+           BUILT_IN_ASAN_REPORT_LOAD4_NOABORT,
+           BUILT_IN_ASAN_REPORT_LOAD8_NOABORT,
+           BUILT_IN_ASAN_REPORT_LOAD16_NOABORT,
+           BUILT_IN_ASAN_REPORT_LOAD_N_NOABORT },
+         { BUILT_IN_ASAN_REPORT_STORE1_NOABORT,
+           BUILT_IN_ASAN_REPORT_STORE2_NOABORT,
+           BUILT_IN_ASAN_REPORT_STORE4_NOABORT,
+           BUILT_IN_ASAN_REPORT_STORE8_NOABORT,
+           BUILT_IN_ASAN_REPORT_STORE16_NOABORT,
+           BUILT_IN_ASAN_REPORT_STORE_N_NOABORT } } };
   if (size_in_bytes == -1)
     {
       *nargs = 2;
-      return builtin_decl_implicit (report[is_store][5]);
+      return builtin_decl_implicit (report[recover_p][is_store][5]);
     }
   *nargs = 1;
-  return builtin_decl_implicit (report[is_store][exact_log2 (size_in_bytes)]);
+  int size_log2 = exact_log2 (size_in_bytes);
+  return builtin_decl_implicit (report[recover_p][is_store][size_log2]);
 }
 
 /* Construct a function tree for __asan_{load,store}{1,2,4,8,16,_n}.
    IS_STORE is either 1 (for a store) or 0 (for a load).  */
 
 static tree
-check_func (bool is_store, int size_in_bytes, int *nargs)
-{
-  static enum built_in_function check[2][6]
-    = { { BUILT_IN_ASAN_LOAD1, BUILT_IN_ASAN_LOAD2,
-      BUILT_IN_ASAN_LOAD4, BUILT_IN_ASAN_LOAD8,
-      BUILT_IN_ASAN_LOAD16, BUILT_IN_ASAN_LOADN },
-       { BUILT_IN_ASAN_STORE1, BUILT_IN_ASAN_STORE2,
-      BUILT_IN_ASAN_STORE4, BUILT_IN_ASAN_STORE8,
-      BUILT_IN_ASAN_STORE16, BUILT_IN_ASAN_STOREN } };
+check_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes,
+           int *nargs)
+{
+  static enum built_in_function check[2][2][6]
+    = { { { BUILT_IN_ASAN_LOAD1, BUILT_IN_ASAN_LOAD2,
+           BUILT_IN_ASAN_LOAD4, BUILT_IN_ASAN_LOAD8,
+           BUILT_IN_ASAN_LOAD16, BUILT_IN_ASAN_LOADN },
+         { BUILT_IN_ASAN_STORE1, BUILT_IN_ASAN_STORE2,
+           BUILT_IN_ASAN_STORE4, BUILT_IN_ASAN_STORE8,
+           BUILT_IN_ASAN_STORE16, BUILT_IN_ASAN_STOREN } },
+       { { BUILT_IN_ASAN_LOAD1_NOABORT,
+           BUILT_IN_ASAN_LOAD2_NOABORT,
+           BUILT_IN_ASAN_LOAD4_NOABORT,
+           BUILT_IN_ASAN_LOAD8_NOABORT,
+           BUILT_IN_ASAN_LOAD16_NOABORT,
+           BUILT_IN_ASAN_LOADN_NOABORT },
+         { BUILT_IN_ASAN_STORE1_NOABORT,
+           BUILT_IN_ASAN_STORE2_NOABORT,
+           BUILT_IN_ASAN_STORE4_NOABORT,
+           BUILT_IN_ASAN_STORE8_NOABORT,
+           BUILT_IN_ASAN_STORE16_NOABORT,
+           BUILT_IN_ASAN_STOREN_NOABORT } } };
   if (size_in_bytes == -1)
     {
       *nargs = 2;
-      return builtin_decl_implicit (check[is_store][5]);
+      return builtin_decl_implicit (check[recover_p][is_store][5]);
     }
   *nargs = 1;
-  return builtin_decl_implicit (check[is_store][exact_log2 (size_in_bytes)]);
+  int size_log2 = exact_log2 (size_in_bytes);
+  return builtin_decl_implicit (check[recover_p][is_store][size_log2]);
 }
 
 /* Split the current basic block and create a condition statement
@@ -1486,7 +1562,7 @@ create_cond_insert_point (gimple_stmt_iterator *iter,
    pointing to initially.  */
 
 static void
-insert_if_then_before_iter (gimple cond,
+insert_if_then_before_iter (gcond *cond,
                            gimple_stmt_iterator *iter,
                            bool then_more_likely_p,
                            basic_block *then_bb,
@@ -1503,7 +1579,7 @@ insert_if_then_before_iter (gimple cond,
 }
 
 /* Build
-   (base_addr >> ASAN_SHADOW_SHIFT) + targetm.asan_shadow_offset ().  */
+   (base_addr >> ASAN_SHADOW_SHIFT) + asan_shadow_offset ().  */
 
 static tree
 build_shadow_mem_access (gimple_stmt_iterator *gsi, location_t location,
@@ -1511,33 +1587,28 @@ build_shadow_mem_access (gimple_stmt_iterator *gsi, location_t location,
 {
   tree t, uintptr_type = TREE_TYPE (base_addr);
   tree shadow_type = TREE_TYPE (shadow_ptr_type);
-  gimple g;
+  gimple *g;
 
   t = build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT);
-  g = gimple_build_assign_with_ops (RSHIFT_EXPR,
-                                   make_ssa_name (uintptr_type, NULL),
-                                   base_addr, t);
+  g = gimple_build_assign (make_ssa_name (uintptr_type), RSHIFT_EXPR,
+                          base_addr, t);
   gimple_set_location (g, location);
   gsi_insert_after (gsi, g, GSI_NEW_STMT);
 
-  t = build_int_cst (uintptr_type, targetm.asan_shadow_offset ());
-  g = gimple_build_assign_with_ops (PLUS_EXPR,
-                                   make_ssa_name (uintptr_type, NULL),
-                                   gimple_assign_lhs (g), t);
+  t = build_int_cst (uintptr_type, asan_shadow_offset ());
+  g = gimple_build_assign (make_ssa_name (uintptr_type), PLUS_EXPR,
+                          gimple_assign_lhs (g), t);
   gimple_set_location (g, location);
   gsi_insert_after (gsi, g, GSI_NEW_STMT);
 
-  g = gimple_build_assign_with_ops (NOP_EXPR,
-                                   make_ssa_name (shadow_ptr_type, NULL),
-                                   gimple_assign_lhs (g), NULL_TREE);
+  g = gimple_build_assign (make_ssa_name (shadow_ptr_type), NOP_EXPR,
+                          gimple_assign_lhs (g));
   gimple_set_location (g, location);
   gsi_insert_after (gsi, g, GSI_NEW_STMT);
 
   t = build2 (MEM_REF, shadow_type, gimple_assign_lhs (g),
              build_int_cst (shadow_ptr_type, 0));
-  g = gimple_build_assign_with_ops (MEM_REF,
-                                   make_ssa_name (shadow_type, NULL),
-                                   t, NULL_TREE);
+  g = gimple_build_assign (make_ssa_name (shadow_type), MEM_REF, t);
   gimple_set_location (g, location);
   gsi_insert_after (gsi, g, GSI_NEW_STMT);
   return gimple_assign_lhs (g);
@@ -1552,10 +1623,8 @@ maybe_create_ssa_name (location_t loc, tree base, gimple_stmt_iterator *iter,
 {
   if (TREE_CODE (base) == SSA_NAME)
     return base;
-  gimple g
-    = gimple_build_assign_with_ops (TREE_CODE (base),
-                                   make_ssa_name (TREE_TYPE (base), NULL),
-                                   base, NULL_TREE);
+  gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (base)),
+                                 TREE_CODE (base), base);
   gimple_set_location (g, loc);
   if (before_p)
     gsi_insert_before (iter, g, GSI_SAME_STMT);
@@ -1573,10 +1642,8 @@ maybe_cast_to_ptrmode (location_t loc, tree len, gimple_stmt_iterator *iter,
 {
   if (ptrofftype_p (len))
     return len;
-  gimple g
-    = gimple_build_assign_with_ops (NOP_EXPR,
-                                   make_ssa_name (pointer_sized_int_node, NULL),
-                                   len, NULL);
+  gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                                 NOP_EXPR, len);
   gimple_set_location (g, loc);
   if (before_p)
     gsi_insert_before (iter, g, GSI_SAME_STMT);
@@ -1608,22 +1675,13 @@ static void
 build_check_stmt (location_t loc, tree base, tree len,
                  HOST_WIDE_INT size_in_bytes, gimple_stmt_iterator *iter,
                  bool is_non_zero_len, bool before_p, bool is_store,
-                 bool is_scalar_access, unsigned int align = 0,
-                 bool start_instrumented = false,
-                 bool end_instrumented = false)
+                 bool is_scalar_access, unsigned int align = 0)
 {
   gimple_stmt_iterator gsi = *iter;
-  gimple g;
+  gimple *g;
 
   gcc_assert (!(size_in_bytes > 0 && !is_non_zero_len));
 
-  if (start_instrumented && end_instrumented)
-    {
-      if (!before_p)
-       gsi_next (iter);
-      return;
-    }
-
   gsi = *iter;
 
   base = unshare_expr (base);
@@ -1666,10 +1724,6 @@ build_check_stmt (location_t loc, tree base, tree len,
     flags |= ASAN_CHECK_NON_ZERO_LEN;
   if (is_scalar_access)
     flags |= ASAN_CHECK_SCALAR_ACCESS;
-  if (start_instrumented)
-    flags |= ASAN_CHECK_START_INSTRUMENTED;
-  if (end_instrumented)
-    flags |= ASAN_CHECK_END_INSTRUMENTED;
 
   g = gimple_build_call_internal (IFN_ASAN_CHECK, 4,
                                  build_int_cst (integer_type_node, flags),
@@ -1711,6 +1765,7 @@ instrument_derefs (gimple_stmt_iterator *iter, tree t,
     case INDIRECT_REF:
     case MEM_REF:
     case VAR_DECL:
+    case BIT_FIELD_REF:
       break;
       /* FALLTHRU */
     default:
@@ -1723,10 +1778,10 @@ instrument_derefs (gimple_stmt_iterator *iter, tree t,
 
   HOST_WIDE_INT bitsize, bitpos;
   tree offset;
-  enum machine_mode mode;
-  int volatilep = 0, unsignedp = 0;
-  tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset,
-                                   &mode, &unsignedp, &volatilep, false);
+  machine_mode mode;
+  int unsignedp, reversep, volatilep = 0;
+  tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
+                                   &unsignedp, &reversep, &volatilep, false);
 
   if (TREE_CODE (t) == COMPONENT_REF
       && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE)
@@ -1751,6 +1806,8 @@ instrument_derefs (gimple_stmt_iterator *iter, tree t,
     {
       if (DECL_THREAD_LOCAL_P (inner))
        return;
+      if (!ASAN_GLOBALS && is_global_var (inner))
+        return;
       if (!TREE_STATIC (inner))
        {
          /* Automatic vars in the current function will be always
@@ -1783,6 +1840,22 @@ instrument_derefs (gimple_stmt_iterator *iter, tree t,
 
 }
 
+/*  Insert a memory reference into the hash table if access length
+    can be determined in compile time.  */
+
+static void
+maybe_update_mem_ref_hash_table (tree base, tree len)
+{
+  if (!POINTER_TYPE_P (TREE_TYPE (base))
+      || !INTEGRAL_TYPE_P (TREE_TYPE (len)))
+    return;
+
+  HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
+
+  if (size_in_bytes != -1)
+    update_mem_ref_hash_table (base, size_in_bytes);
+}
+
 /* Instrument an access to a contiguous memory region that starts at
    the address pointed to by BASE, over a length of LEN (expressed in
    the sizeof (*BASE) bytes).  ITER points to the instruction before
@@ -1801,97 +1874,20 @@ instrument_mem_region_access (tree base, tree len,
       || integer_zerop (len))
     return;
 
-  /* If the beginning of the memory region has already been
-     instrumented, do not instrument it.  */
-  bool start_instrumented = has_mem_ref_been_instrumented (base, 1);
-
-  /* If the end of the memory region has already been instrumented, do
-     not instrument it.  */
-  tree end = asan_mem_ref_get_end (base, len);
-  bool end_instrumented = has_mem_ref_been_instrumented (end, 1);
-
   HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
 
-  build_check_stmt (location, base, len, size_in_bytes, iter,
-                   /*is_non_zero_len*/size_in_bytes > 0, /*before_p*/true,
-                   is_store, /*is_scalar_access*/false, /*align*/0,
-                   start_instrumented, end_instrumented);
-
-  update_mem_ref_hash_table (base, 1);
-  if (size_in_bytes != -1)
-    update_mem_ref_hash_table (end, 1);
+  if ((size_in_bytes == -1)
+      || !has_mem_ref_been_instrumented (base, size_in_bytes))
+    {
+      build_check_stmt (location, base, len, size_in_bytes, iter,
+                       /*is_non_zero_len*/size_in_bytes > 0, /*before_p*/true,
+                       is_store, /*is_scalar_access*/false, /*align*/0);
+    }
 
+  maybe_update_mem_ref_hash_table (base, len);
   *iter = gsi_for_stmt (gsi_stmt (*iter));
 }
 
-/* Instrument the call (to the builtin strlen function) pointed to by
-   ITER.
-
-   This function instruments the access to the first byte of the
-   argument, right before the call.  After the call it instruments the
-   access to the last byte of the argument; it uses the result of the
-   call to deduce the offset of that last byte.
-
-   Upon completion, iff the call has actually been instrumented, this
-   function returns TRUE and *ITER points to the statement logically
-   following the built-in strlen function call *ITER was initially
-   pointing to.  Otherwise, the function returns FALSE and *ITER
-   remains unchanged.  */
-
-static bool
-instrument_strlen_call (gimple_stmt_iterator *iter)
-{
-  gimple g;
-  gimple call = gsi_stmt (*iter);
-  gcc_assert (is_gimple_call (call));
-
-  tree callee = gimple_call_fndecl (call);
-  gcc_assert (is_builtin_fn (callee)
-             && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
-             && DECL_FUNCTION_CODE (callee) == BUILT_IN_STRLEN);
-
-  location_t loc = gimple_location (call);
-
-  tree len = gimple_call_lhs (call);
-  if (len == NULL)
-    /* Some passes might clear the return value of the strlen call;
-       bail out in that case.  Return FALSE as we are not advancing
-       *ITER.  */
-    return false;
-  gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (len)));
-
-  len = maybe_cast_to_ptrmode (loc, len, iter, /*before_p*/false);
-
-  tree str_arg = gimple_call_arg (call, 0);
-  bool start_instrumented = has_mem_ref_been_instrumented (str_arg, 1);
-
-  tree cptr_type = build_pointer_type (char_type_node);
-  g = gimple_build_assign_with_ops (NOP_EXPR,
-                                   make_ssa_name (cptr_type, NULL),
-                                   str_arg, NULL);
-  gimple_set_location (g, loc);
-  gsi_insert_before (iter, g, GSI_SAME_STMT);
-  str_arg = gimple_assign_lhs (g);
-
-  build_check_stmt (loc, str_arg, NULL_TREE, 1, iter,
-                   /*is_non_zero_len*/true, /*before_p=*/true,
-                   /*is_store=*/false, /*is_scalar_access*/true, /*align*/0,
-                   start_instrumented, start_instrumented);
-
-  g = gimple_build_assign_with_ops (POINTER_PLUS_EXPR,
-                                   make_ssa_name (cptr_type, NULL),
-                                   str_arg,
-                                   len);
-  gimple_set_location (g, loc);
-  gsi_insert_after (iter, g, GSI_NEW_STMT);
-
-  build_check_stmt (loc, gimple_assign_lhs (g), NULL_TREE, 1, iter,
-                   /*is_non_zero_len*/true, /*before_p=*/false,
-                   /*is_store=*/false, /*is_scalar_access*/true, /*align*/0);
-
-  return true;
-}
-
 /* Instrument the call to a built-in memory access function that is
    pointed to by the iterator ITER.
 
@@ -1905,53 +1901,58 @@ instrument_builtin_call (gimple_stmt_iterator *iter)
     return false;
 
   bool iter_advanced_p = false;
-  gimple call = gsi_stmt (*iter);
+  gcall *call = as_a <gcall *> (gsi_stmt (*iter));
 
   gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL));
 
-  tree callee = gimple_call_fndecl (call);
   location_t loc = gimple_location (call);
 
-  if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STRLEN)
-    iter_advanced_p = instrument_strlen_call (iter);
-  else
-    {
-      asan_mem_ref src0, src1, dest;
-      asan_mem_ref_init (&src0, NULL, 1);
-      asan_mem_ref_init (&src1, NULL, 1);
-      asan_mem_ref_init (&dest, NULL, 1);
+  asan_mem_ref src0, src1, dest;
+  asan_mem_ref_init (&src0, NULL, 1);
+  asan_mem_ref_init (&src1, NULL, 1);
+  asan_mem_ref_init (&dest, NULL, 1);
 
-      tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE;
-      bool src0_is_store = false, src1_is_store = false,
-       dest_is_store = false, dest_is_deref = false;
+  tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE;
+  bool src0_is_store = false, src1_is_store = false, dest_is_store = false,
+    dest_is_deref = false, intercepted_p = true;
 
-      if (get_mem_refs_of_builtin_call (call,
-                                       &src0, &src0_len, &src0_is_store,
-                                       &src1, &src1_len, &src1_is_store,
-                                       &dest, &dest_len, &dest_is_store,
-                                       &dest_is_deref))
+  if (get_mem_refs_of_builtin_call (call,
+                                   &src0, &src0_len, &src0_is_store,
+                                   &src1, &src1_len, &src1_is_store,
+                                   &dest, &dest_len, &dest_is_store,
+                                   &dest_is_deref, &intercepted_p))
+    {
+      if (dest_is_deref)
        {
-         if (dest_is_deref)
-           {
-             instrument_derefs (iter, dest.start, loc, dest_is_store);
-             gsi_next (iter);
-             iter_advanced_p = true;
-           }
-         else if (src0_len || src1_len || dest_len)
-           {
-             if (src0.start != NULL_TREE)
-               instrument_mem_region_access (src0.start, src0_len,
-                                             iter, loc, /*is_store=*/false);
-             if (src1.start != NULL_TREE)
-               instrument_mem_region_access (src1.start, src1_len,
-                                             iter, loc, /*is_store=*/false);
-             if (dest.start != NULL_TREE)
-               instrument_mem_region_access (dest.start, dest_len,
-                                             iter, loc, /*is_store=*/true);
-             *iter = gsi_for_stmt (call);
-             gsi_next (iter);
-             iter_advanced_p = true;
-           }
+         instrument_derefs (iter, dest.start, loc, dest_is_store);
+         gsi_next (iter);
+         iter_advanced_p = true;
+       }
+      else if (!intercepted_p
+              && (src0_len || src1_len || dest_len))
+       {
+         if (src0.start != NULL_TREE)
+           instrument_mem_region_access (src0.start, src0_len,
+                                         iter, loc, /*is_store=*/false);
+         if (src1.start != NULL_TREE)
+           instrument_mem_region_access (src1.start, src1_len,
+                                         iter, loc, /*is_store=*/false);
+         if (dest.start != NULL_TREE)
+           instrument_mem_region_access (dest.start, dest_len,
+                                         iter, loc, /*is_store=*/true);
+
+         *iter = gsi_for_stmt (call);
+         gsi_next (iter);
+         iter_advanced_p = true;
+       }
+      else
+       {
+         if (src0.start != NULL_TREE)
+           maybe_update_mem_ref_hash_table (src0.start, src0_len);
+         if (src1.start != NULL_TREE)
+           maybe_update_mem_ref_hash_table (src1.start, src1_len);
+         if (dest.start != NULL_TREE)
+           maybe_update_mem_ref_hash_table (dest.start, dest_len);
        }
     }
   return iter_advanced_p;
@@ -1967,7 +1968,7 @@ instrument_builtin_call (gimple_stmt_iterator *iter)
 static bool
 maybe_instrument_assignment (gimple_stmt_iterator *iter)
 {
-  gimple s = gsi_stmt (*iter);
+  gimple *s = gsi_stmt (*iter);
 
   gcc_assert (gimple_assign_single_p (s));
 
@@ -1983,7 +1984,7 @@ maybe_instrument_assignment (gimple_stmt_iterator *iter)
                         is_store);
       is_instrumented = true;
     }
+
   if (gimple_assign_load_p (s))
     {
       ref_expr = gimple_assign_rhs1 (s);
@@ -2011,7 +2012,7 @@ maybe_instrument_assignment (gimple_stmt_iterator *iter)
 static bool
 maybe_instrument_call (gimple_stmt_iterator *iter)
 {
-  gimple stmt = gsi_stmt (*iter);
+  gimple *stmt = gsi_stmt (*iter);
   bool is_builtin = gimple_call_builtin_p (stmt, BUILT_IN_NORMAL);
 
   if (is_builtin && instrument_builtin_call (iter))
@@ -2033,7 +2034,7 @@ maybe_instrument_call (gimple_stmt_iterator *iter)
            }
        }
       tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN);
-      gimple g = gimple_build_call (decl, 0);
+      gimple *g = gimple_build_call (decl, 0);
       gimple_set_location (g, gimple_location (stmt));
       gsi_insert_before (iter, g, GSI_SAME_STMT);
     }
@@ -2075,7 +2076,7 @@ transform_statements (void)
 
       for (i = gsi_start_bb (bb); !gsi_end_p (i);)
        {
-         gimple s = gsi_stmt (i);
+         gimple *s = gsi_stmt (i);
 
          if (has_stmt_been_instrumented_p (s))
            gsi_next (&i);
@@ -2166,8 +2167,13 @@ asan_global_struct (void)
       if (i)
        DECL_CHAIN (fields[i - 1]) = fields[i];
     }
+  tree type_decl = build_decl (input_location, TYPE_DECL,
+                              get_identifier ("__asan_global"), ret);
+  DECL_IGNORED_P (type_decl) = 1;
+  DECL_ARTIFICIAL (type_decl) = 1;
   TYPE_FIELDS (ret) = fields[0];
-  TYPE_NAME (ret) = get_identifier ("__asan_global");
+  TYPE_NAME (ret) = type_decl;
+  TYPE_STUB_DECL (ret) = type_decl;
   layout_type (ret);
   return ret;
 }
@@ -2288,6 +2294,9 @@ initialize_sanitizer_builtins (void)
                                pointer_sized_int_node, NULL_TREE);
   tree BT_FN_VOID_INT
     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
+  tree BT_FN_SIZE_CONST_PTR_INT
+    = build_function_type_list (size_type_node, const_ptr_type_node,
+                               integer_type_node, NULL_TREE);
   tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5];
   tree BT_FN_IX_CONST_VPTR_INT[5];
   tree BT_FN_IX_VPTR_IX_INT[5];
@@ -2344,6 +2353,9 @@ initialize_sanitizer_builtins (void)
 #define ATTR_TMPURE_NOTHROW_LEAF_LIST ECF_TM_PURE | ATTR_NOTHROW_LEAF_LIST
 #undef ATTR_NORETURN_NOTHROW_LEAF_LIST
 #define ATTR_NORETURN_NOTHROW_LEAF_LIST ECF_NORETURN | ATTR_NOTHROW_LEAF_LIST
+#undef ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST
+#define ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST \
+  ECF_CONST | ATTR_NORETURN_NOTHROW_LEAF_LIST
 #undef ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST
 #define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST \
   ECF_TM_PURE | ATTR_NORETURN_NOTHROW_LEAF_LIST
@@ -2353,6 +2365,11 @@ initialize_sanitizer_builtins (void)
 #undef ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST
 #define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \
   /* ECF_COLD missing */ ATTR_NORETURN_NOTHROW_LEAF_LIST
+#undef ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST
+#define ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST \
+  /* ECF_COLD missing */ ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST
+#undef ATTR_PURE_NOTHROW_LEAF_LIST
+#define ATTR_PURE_NOTHROW_LEAF_LIST ECF_PURE | ATTR_NOTHROW_LEAF_LIST
 #undef DEF_SANITIZER_BUILTIN
 #define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
   decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM,          \
@@ -2362,6 +2379,15 @@ initialize_sanitizer_builtins (void)
 
 #include "sanitizer.def"
 
+  /* -fsanitize=object-size uses __builtin_object_size, but that might
+     not be available for e.g. Fortran at this point.  We use
+     DEF_SANITIZER_BUILTIN here only as a convenience macro.  */
+  if ((flag_sanitize & SANITIZE_OBJECT_SIZE)
+      && !builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE))
+    DEF_SANITIZER_BUILTIN (BUILT_IN_OBJECT_SIZE, "object_size",
+                          BT_FN_SIZE_CONST_PTR_INT,
+                          ATTR_PURE_NOTHROW_LEAF_LIST)
+
 #undef DEF_SANITIZER_BUILTIN
 }
 
@@ -2429,10 +2455,19 @@ asan_finish_file (void)
      nor after .LASAN* array.  */
   flag_sanitize &= ~SANITIZE_ADDRESS;
 
+  /* For user-space we want asan constructors to run first.
+     Linux kernel does not support priorities other than default, and the only
+     other user of constructors is coverage. So we run with the default
+     priority.  */
+  int priority = flag_sanitize & SANITIZE_USER_ADDRESS
+                 ? MAX_RESERVED_INIT_PRIORITY - 1 : DEFAULT_INIT_PRIORITY;
+
   if (flag_sanitize & SANITIZE_USER_ADDRESS)
     {
       tree fn = builtin_decl_implicit (BUILT_IN_ASAN_INIT);
       append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements);
+      fn = builtin_decl_implicit (BUILT_IN_ASAN_VERSION_MISMATCH_CHECK);
+      append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements);
     }
   FOR_EACH_DEFINED_VARIABLE (vnode)
     if (TREE_ASM_WRITTEN (vnode->decl)
@@ -2484,30 +2519,31 @@ asan_finish_file (void)
                                                 build_fold_addr_expr (var),
                                                 gcount_tree),
                                &dtor_statements);
-      cgraph_build_static_cdtor ('D', dtor_statements,
-                                MAX_RESERVED_INIT_PRIORITY - 1);
+      cgraph_build_static_cdtor ('D', dtor_statements, priority);
     }
   if (asan_ctor_statements)
-    cgraph_build_static_cdtor ('I', asan_ctor_statements,
-                              MAX_RESERVED_INIT_PRIORITY - 1);
+    cgraph_build_static_cdtor ('I', asan_ctor_statements, priority);
   flag_sanitize |= SANITIZE_ADDRESS;
 }
 
 /* Expand the ASAN_{LOAD,STORE} builtins.  */
 
-static bool
+bool
 asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
 {
-  gimple g = gsi_stmt (*iter);
+  gimple *g = gsi_stmt (*iter);
   location_t loc = gimple_location (g);
+  bool recover_p;
+  if (flag_sanitize & SANITIZE_USER_ADDRESS)
+    recover_p = (flag_sanitize_recover & SANITIZE_USER_ADDRESS) != 0;
+  else
+    recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0;
 
   HOST_WIDE_INT flags = tree_to_shwi (gimple_call_arg (g, 0));
   gcc_assert (flags < ASAN_CHECK_LAST);
   bool is_scalar_access = (flags & ASAN_CHECK_SCALAR_ACCESS) != 0;
   bool is_store = (flags & ASAN_CHECK_STORE) != 0;
   bool is_non_zero_len = (flags & ASAN_CHECK_NON_ZERO_LEN) != 0;
-  bool start_instrumented = (flags & ASAN_CHECK_START_INSTRUMENTED) != 0;
-  bool end_instrumented = (flags & ASAN_CHECK_END_INSTRUMENTED) != 0;
 
   tree base = gimple_call_arg (g, 1);
   tree len = gimple_call_arg (g, 2);
@@ -2519,26 +2555,21 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
   if (use_calls)
     {
       /* Instrument using callbacks.  */
-      gimple g
-       = gimple_build_assign_with_ops (NOP_EXPR,
-                                       make_ssa_name (pointer_sized_int_node,
-                                       NULL),
-                                       base, NULL_TREE);
+      gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                                     NOP_EXPR, base);
       gimple_set_location (g, loc);
       gsi_insert_before (iter, g, GSI_SAME_STMT);
       tree base_addr = gimple_assign_lhs (g);
 
       int nargs;
-      tree fun = check_func (is_store, size_in_bytes, &nargs);
+      tree fun = check_func (is_store, recover_p, size_in_bytes, &nargs);
       if (nargs == 1)
        g = gimple_build_call (fun, 1, base_addr);
       else
        {
          gcc_assert (nargs == 2);
-         g = gimple_build_assign_with_ops (NOP_EXPR,
-                                           make_ssa_name (pointer_sized_int_node,
-                                                          NULL),
-                                           len, NULL_TREE);
+         g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                                  NOP_EXPR, len);
          gimple_set_location (g, loc);
          gsi_insert_before (iter, g, GSI_SAME_STMT);
          tree sz_arg = gimple_assign_lhs (g);
@@ -2575,8 +2606,9 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
       gimple_set_location (g, loc);
 
       basic_block then_bb, fallthrough_bb;
-      insert_if_then_before_iter (g, iter, /*then_more_likely_p=*/true,
-                                &then_bb, &fallthrough_bb);
+      insert_if_then_before_iter (as_a <gcond *> (g), iter,
+                                 /*then_more_likely_p=*/true,
+                                 &then_bb, &fallthrough_bb);
       /* Note that fallthrough_bb starts with the statement that was
        pointed to by ITER.  */
 
@@ -2590,14 +2622,12 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
   basic_block then_bb, else_bb;
   gsi = create_cond_insert_point (&gsi, /*before_p*/false,
                                  /*then_more_likely_p=*/false,
-                                 /*create_then_fallthru_edge=*/false,
+                                 /*create_then_fallthru_edge*/recover_p,
                                  &then_bb,
                                  &else_bb);
 
-  g = gimple_build_assign_with_ops (NOP_EXPR,
-                                   make_ssa_name (pointer_sized_int_node,
-                                                  NULL),
-                                   base, NULL_TREE);
+  g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                          NOP_EXPR, base);
   gimple_set_location (g, loc);
   gsi_insert_before (&gsi, g, GSI_NEW_STMT);
   tree base_addr = gimple_assign_lhs (g);
@@ -2612,65 +2642,58 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
   else
     {
       /* Slow path for 1, 2 and 4 byte accesses.  */
-
-      if (!start_instrumented)
+      /* Test (shadow != 0)
+        & ((base_addr & 7) + (real_size_in_bytes - 1)) >= shadow).  */
+      tree shadow = build_shadow_mem_access (&gsi, loc, base_addr,
+                                            shadow_ptr_type);
+      gimple *shadow_test = build_assign (NE_EXPR, shadow, 0);
+      gimple_seq seq = NULL;
+      gimple_seq_add_stmt (&seq, shadow_test);
+      /* Aligned (>= 8 bytes) can test just
+        (real_size_in_bytes - 1 >= shadow), as base_addr & 7 is known
+        to be 0.  */
+      if (align < 8)
        {
-         /* Test (shadow != 0)
-            & ((base_addr & 7) + (real_size_in_bytes - 1)) >= shadow).  */
-         tree shadow = build_shadow_mem_access (&gsi, loc, base_addr,
-                                                shadow_ptr_type);
-         gimple shadow_test = build_assign (NE_EXPR, shadow, 0);
-         gimple_seq seq = NULL;
-         gimple_seq_add_stmt (&seq, shadow_test);
-         /* Aligned (>= 8 bytes) can test just
-            (real_size_in_bytes - 1 >= shadow), as base_addr & 7 is known
-            to be 0.  */
-         if (align < 8)
-           {
-             gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR,
-                                                      base_addr, 7));
-             gimple_seq_add_stmt (&seq,
-                                  build_type_cast (shadow_type,
-                                                   gimple_seq_last (seq)));
-             if (real_size_in_bytes > 1)
-               gimple_seq_add_stmt (&seq,
-                                    build_assign (PLUS_EXPR,
-                                                  gimple_seq_last (seq),
-                                                  real_size_in_bytes - 1));
-             t = gimple_assign_lhs (gimple_seq_last_stmt (seq));
-           }
-         else
-           t = build_int_cst (shadow_type, real_size_in_bytes - 1);
-         gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, t, shadow));
-         gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test,
-                                                  gimple_seq_last (seq)));
-         t = gimple_assign_lhs (gimple_seq_last (seq));
-         gimple_seq_set_location (seq, loc);
-         gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
+         gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR,
+                                                  base_addr, 7));
+         gimple_seq_add_stmt (&seq,
+                              build_type_cast (shadow_type,
+                                               gimple_seq_last (seq)));
+         if (real_size_in_bytes > 1)
+           gimple_seq_add_stmt (&seq,
+                                build_assign (PLUS_EXPR,
+                                              gimple_seq_last (seq),
+                                              real_size_in_bytes - 1));
+         t = gimple_assign_lhs (gimple_seq_last_stmt (seq));
        }
+      else
+       t = build_int_cst (shadow_type, real_size_in_bytes - 1);
+      gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, t, shadow));
+      gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test,
+                                              gimple_seq_last (seq)));
+      t = gimple_assign_lhs (gimple_seq_last (seq));
+      gimple_seq_set_location (seq, loc);
+      gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
 
       /* For non-constant, misaligned or otherwise weird access sizes,
-        check first and last byte.  */
-      if (size_in_bytes == -1 && !end_instrumented)
+       check first and last byte.  */
+      if (size_in_bytes == -1)
        {
-         g = gimple_build_assign_with_ops (MINUS_EXPR,
-                                           make_ssa_name (pointer_sized_int_node, NULL),
-                                           len,
-                                           build_int_cst (pointer_sized_int_node, 1));
+         g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                                  MINUS_EXPR, len,
+                                  build_int_cst (pointer_sized_int_node, 1));
          gimple_set_location (g, loc);
          gsi_insert_after (&gsi, g, GSI_NEW_STMT);
          tree last = gimple_assign_lhs (g);
-         g = gimple_build_assign_with_ops (PLUS_EXPR,
-                                           make_ssa_name (pointer_sized_int_node, NULL),
-                                           base_addr,
-                                           last);
+         g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
+                                  PLUS_EXPR, base_addr, last);
          gimple_set_location (g, loc);
          gsi_insert_after (&gsi, g, GSI_NEW_STMT);
          tree base_end_addr = gimple_assign_lhs (g);
 
          tree shadow = build_shadow_mem_access (&gsi, loc, base_end_addr,
                                                 shadow_ptr_type);
-         gimple shadow_test = build_assign (NE_EXPR, shadow, 0);
+         gimple *shadow_test = build_assign (NE_EXPR, shadow, 0);
          gimple_seq seq = NULL;
          gimple_seq_add_stmt (&seq, shadow_test);
          gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR,
@@ -2682,9 +2705,8 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
                                                   shadow));
          gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test,
                                                   gimple_seq_last (seq)));
-         if (!start_instrumented)
-           gimple_seq_add_stmt (&seq, build_assign (BIT_IOR_EXPR, t,
-                                                    gimple_seq_last (seq)));
+         gimple_seq_add_stmt (&seq, build_assign (BIT_IOR_EXPR, t,
+                                                  gimple_seq_last (seq)));
          t = gimple_assign_lhs (gimple_seq_last (seq));
          gimple_seq_set_location (seq, loc);
          gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
@@ -2699,7 +2721,7 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
   /* Generate call to the run-time library (e.g. __asan_report_load8).  */
   gsi = gsi_start_bb (then_bb);
   int nargs;
-  tree fun = report_error_func (is_store, size_in_bytes, &nargs);
+  tree fun = report_error_func (is_store, recover_p, size_in_bytes, &nargs);
   g = gimple_build_call (fun, nargs, base_addr, len);
   gimple_set_location (g, loc);
   gsi_insert_after (&gsi, g, GSI_NEW_STMT);
@@ -2802,116 +2824,4 @@ make_pass_asan_O0 (gcc::context *ctxt)
   return new pass_asan_O0 (ctxt);
 }
 
-/* Perform optimization of sanitize functions.  */
-
-namespace {
-
-const pass_data pass_data_sanopt =
-{
-  GIMPLE_PASS, /* type */
-  "sanopt", /* name */
-  OPTGROUP_NONE, /* optinfo_flags */
-  TV_NONE, /* tv_id */
-  ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
-  0, /* properties_provided */
-  0, /* properties_destroyed */
-  0, /* todo_flags_start */
-  TODO_update_ssa, /* todo_flags_finish */
-};
-
-class pass_sanopt : public gimple_opt_pass
-{
-public:
-  pass_sanopt (gcc::context *ctxt)
-    : gimple_opt_pass (pass_data_sanopt, ctxt)
-  {}
-
-  /* opt_pass methods: */
-  virtual bool gate (function *) { return flag_sanitize; }
-  virtual unsigned int execute (function *);
-
-}; // class pass_sanopt
-
-unsigned int
-pass_sanopt::execute (function *fun)
-{
-  basic_block bb;
-
-  int asan_num_accesses = 0;
-  if (flag_sanitize & SANITIZE_ADDRESS)
-    {
-      gimple_stmt_iterator gsi;
-      FOR_EACH_BB_FN (bb, fun)
-       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-         {
-           gimple stmt = gsi_stmt (gsi);
-           if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)
-               && gimple_call_internal_fn (stmt) == IFN_ASAN_CHECK)
-             ++asan_num_accesses;
-         }
-    }
-
-  bool use_calls = ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD < INT_MAX
-    && asan_num_accesses >= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD;
-
-  FOR_EACH_BB_FN (bb, fun)
-    {
-      gimple_stmt_iterator gsi;
-      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
-       {
-         gimple stmt = gsi_stmt (gsi);
-         bool no_next = false;
-
-         if (!is_gimple_call (stmt))
-           {
-             gsi_next (&gsi);
-             continue;
-           }
-
-         if (gimple_call_internal_p (stmt))
-           {
-             enum internal_fn ifn = gimple_call_internal_fn (stmt);
-             switch (ifn)
-               {
-               case IFN_UBSAN_NULL:
-                 no_next = ubsan_expand_null_ifn (&gsi);
-                 break;
-               case IFN_UBSAN_BOUNDS:
-                 no_next = ubsan_expand_bounds_ifn (&gsi);
-                 break;
-               case IFN_UBSAN_OBJECT_SIZE:
-                 no_next = ubsan_expand_objsize_ifn (&gsi);
-                 break;
-               case IFN_ASAN_CHECK:
-                 {
-                   no_next = asan_expand_check_ifn (&gsi, use_calls);
-                   break;
-                 }
-               default:
-                 break;
-               }
-           }
-
-         if (dump_file && (dump_flags & TDF_DETAILS))
-           {
-             fprintf (dump_file, "Optimized\n  ");
-             print_gimple_stmt (dump_file, stmt, 0, dump_flags);
-             fprintf (dump_file, "\n");
-           }
-
-         if (!no_next)
-           gsi_next (&gsi);
-       }
-    }
-  return 0;
-}
-
-} // anon namespace
-
-gimple_opt_pass *
-make_pass_sanopt (gcc::context *ctxt)
-{
-  return new pass_sanopt (ctxt);
-}
-
 #include "gt-asan.h"