[gcc r15-11440] asan: Don't emit __asan_handle_no_return_call before __asan_report_* calls [PR126084]

Jakub Jelinek via Gcc-cvs <[email protected]> Sat, 1 Aug 2026 10:16:48 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:20f76596214b0cef5768bfec8b036947daeb902a

commit r15-11440-g20f76596214b0cef5768bfec8b036947daeb902a
Author: Jakub Jelinek <[email protected]>
Date:   Thu Jul 30 09:54:53 2026 +0200

    asan: Don't emit __asan_handle_no_return_call before __asan_report_* calls [PR126084]
    
    Alex reported that since my r17-2388 fix we now emit an undesirable
    __asan_handle_no_return call before the __asan_report_{load,store}*
    calls added during bitintlower pass.  Normally (when large/huge _BitInt
    is not involved), those are added by the sanopt pass which runs after
    the asan pass and so aren't instrumented.
    
    The following patch avoids instrumenting those.
    Unfortunately the first hunk isn't all that is needed.
    That is because for the bitintlower added __asan_report_* calls
    gimple_call_builtin_p (stmt, BUILT_IN_NORMAL) returns false
    due to argument type mismatch.
    THe C/C++/Fortran FEs use
    DEF_PRIMITIVE_TYPE (BT_PTRMODE, (*lang_hooks.types.type_for_mode)(ptr_mode, 0))
    and so use signed type with TYPE_MODE (ptr_mode).
    The fallback initialization in initialize_sanitizer_builtins
    (done for non-C/C++/Fortran FEs) uses for PTRMODE instead
    pointer_sized_int_node type, which is initialized to:
      pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
    where
      ptr_mode = as_a <scalar_int_mode>
        (mode_for_size (POINTER_SIZE, GET_MODE_CLASS (Pmode), 0).require ());
    so, I think both have the same precision, just one is signed and one
    unsigned.  And then asan_expand_poison_ifn uses pointer_sized_int_node.
    
    The following patch just changes initialize_sanitizer_builtins and
    asan_expand_poison_ifn to do the same thing as the C/C++/Fortran FEs here.
    Seems asan.cc is full of similar builtin argument type mismatches, but
    I've changed only what was needed for this patch.
    
    2026-07-30  Jakub Jelinek  <[email protected]>
    
            PR middle-end/126084
            * asan.cc (maybe_instrument_call): Don't instrument
            BUILT_IN_ASAN_REPORT_{LOAD,STORE}{1,2,4,8,16,_N} builtins.
            (initialize_sanitizer_builtins): Use
            (*lang_hooks.types.type_for_mode) (ptr_mode, 0) instead of
            pointer_sized_int_mode for PTRMODE arguments.
            (asan_expand_poison_ifn): Likewise.
    
    Reviewed-by: Richard Biener <[email protected]>
    (cherry picked from commit 2720a372bda550b8516a786562e0f5b705aede5b)

Diff:
---
 gcc/asan.cc | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/gcc/asan.cc b/gcc/asan.cc
index 8170372e7630..6efa8da35814 100644
--- a/gcc/asan.cc
+++ b/gcc/asan.cc
@@ -3094,6 +3094,18 @@ maybe_instrument_call (gimple_stmt_iterator *iter)
 	    case BUILT_IN_UNREACHABLE:
 	    case BUILT_IN_UNREACHABLE_TRAP:
 	    case BUILT_IN_TRAP:
+	    case BUILT_IN_ASAN_REPORT_LOAD1:
+	    case BUILT_IN_ASAN_REPORT_LOAD2:
+	    case BUILT_IN_ASAN_REPORT_LOAD4:
+	    case BUILT_IN_ASAN_REPORT_LOAD8:
+	    case BUILT_IN_ASAN_REPORT_LOAD16:
+	    case BUILT_IN_ASAN_REPORT_LOAD_N:
+	    case BUILT_IN_ASAN_REPORT_STORE1:
+	    case BUILT_IN_ASAN_REPORT_STORE2:
+	    case BUILT_IN_ASAN_REPORT_STORE4:
+	    case BUILT_IN_ASAN_REPORT_STORE8:
+	    case BUILT_IN_ASAN_REPORT_STORE16:
+	    case BUILT_IN_ASAN_REPORT_STORE_N:
 	      /* Don't instrument these.  */
 	      return false;
 	    default:
@@ -3494,6 +3506,7 @@ initialize_sanitizer_builtins (void)
   if (builtin_decl_implicit_p (BUILT_IN_ASAN_INIT))
     return;
 
+  tree ptrmode_type = (*lang_hooks.types.type_for_mode) (ptr_mode, 0);
   tree BT_FN_VOID = build_function_type_list (void_type_node, NULL_TREE);
   tree BT_FN_VOID_PTR
     = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
@@ -3507,7 +3520,7 @@ initialize_sanitizer_builtins (void)
 				ptr_type_node, ptr_type_node, NULL_TREE);
   tree BT_FN_VOID_PTR_PTRMODE
     = build_function_type_list (void_type_node, ptr_type_node,
-				pointer_sized_int_node, NULL_TREE);
+				ptrmode_type, 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
@@ -3542,7 +3555,7 @@ initialize_sanitizer_builtins (void)
   tree BT_FN_VOID_PTR_UINT8_PTRMODE
     = build_function_type_list (void_type_node, ptr_type_node,
 				unsigned_char_type_node,
-				pointer_sized_int_node, NULL_TREE);
+				ptrmode_type, NULL_TREE);
 
   tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5];
   tree BT_FN_IX_CONST_VPTR_INT[5];
@@ -4294,11 +4307,13 @@ asan_expand_poison_ifn (gimple_stmt_iterator *iter,
 	{
 	  tree fun = report_error_func (store_p, recover_p, tree_to_uhwi (size),
 					&nargs);
+	  tree ptrmode_type
+	    = (nargs == 2 ? (*lang_hooks.types.type_for_mode) (ptr_mode, 0)
+	       : NULL_TREE);
 	  call = gimple_build_call (fun, nargs,
 				    build_fold_addr_expr (shadow_var),
 				    nargs == 2
-				    ? fold_convert (pointer_sized_int_node,
-						    size)
+				    ? fold_convert (ptrmode_type, size)
 				    : NULL_TREE);
 	}
       gimple_set_location (call, gimple_location (use));