Bug 89124

Summary: __attribute__((no_sanitize_address)) interferes with __attribute__((target(xxx)))
Product: gcc Reporter: Thiago Macieira <thiago>
Component: sanitizerAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: dodji, jakub, kcc, wojciech.mula
Priority: P3 Keywords: rejects-valid
Version: 8.2.1   
Target Milestone: 8.3   
Host: Target:
Build: Known to work: 8.3.0, 8.4.0, 9.1.0
Known to fail: 7.5.0, 8.1.0, 8.2.0 Last reconfirmed:

Description Thiago Macieira 2019-01-30 18:49:04 UTC
$ cat test.cpp
#include <immintrin.h>

#ifdef __GNUC__
__attribute__((target("avx2"), no_sanitize_address))
#endif
void f(void *ptr)
{
    _mm256_loadu_si256((__m256i *)ptr);
}
$ gcc -c test.cpp && echo ok
ok
$ gcc -c -fsanitize=addreess test.cpp
In file included from /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/include/immintrin.h:41,
                 from <source>:1:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/include/avxintrin.h: In function 'void f(void*)':
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/include/avxintrin.h:919:1: error: inlining failed in call to always_inline '__m256i _mm256_loadu_si256(const __m256i_u*)': function attribute mismatch
 _mm256_loadu_si256 (__m256i_u const *__P)
 ^~~~~~~~~~~~~~~~~~
<source>:8:23: note: called from here
     _mm256_loadu_si256((__m256i *)ptr);
     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~

Works fine in Clang. Godbolt link: https://godbolt.org/z/rg5kUD
Comment 1 Thiago Macieira 2019-01-30 18:51:30 UTC
Worse:

$ cat test.cpp
#include <immintrin.h>

#ifdef __GNUC__
__attribute__((no_sanitize_address))
#endif
void f(void *ptr)
{
    _mm256_loadu_si256((__m256i *)ptr);
}
$ gcc -c -mavx2 test.cpp
[same errors]
Comment 2 Thiago Macieira 2019-01-30 19:06:45 UTC
-fsanitize=address missing from the command-line in the previous comment. It should be:

gcc -c -mavx2 -fsanitize=address test.cpp
Comment 3 Jakub Jelinek 2019-01-30 20:52:39 UTC
We have (for a few years already) refused to inline if the callee and caller disagree on the requested (-fsanitize=address related) sanitization flags:

static bool
sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
{
  if (!caller || !callee)
    return true;

  return ((sanitize_flags_p (SANITIZE_ADDRESS, caller)
           == sanitize_flags_p (SANITIZE_ADDRESS, callee))
          && (sanitize_flags_p (SANITIZE_POINTER_COMPARE, caller)
              == sanitize_flags_p (SANITIZE_POINTER_COMPARE, callee))
          && (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT, caller)
              == sanitize_flags_p (SANITIZE_POINTER_SUBTRACT, callee)));
}

I guess we could make an exception and allow inlining always_inline functions that do have these enabled into a function that disables that.
Comment 4 Thiago Macieira 2019-01-31 00:51:01 UTC
Or permit the inlining if the function is also __artificial__. It's documented, but I don't see anyone needing to use that besides gcc's own headers.
Comment 5 Jakub Jelinek 2019-01-31 08:21:17 UTC
Author: jakub
Date: Thu Jan 31 08:20:45 2019
New Revision: 268415

URL: https://gcc.gnu.org/viewcvs?rev=268415&root=gcc&view=rev
Log:
	PR sanitizer/89124
	* ipa-inline.c (sanitize_attrs_match_for_inline_p): Allow inlining
	always_inline callees into no_sanitize_address callers.

	* c-c++-common/asan/pr89124.c: New test.

Added:
    trunk/gcc/testsuite/c-c++-common/asan/pr89124.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ipa-inline.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Jakub Jelinek 2019-01-31 08:23:11 UTC
Fixed for GCC9+.
Comment 7 Jakub Jelinek 2019-02-07 14:49:43 UTC
Author: jakub
Date: Thu Feb  7 14:48:43 2019
New Revision: 268639

URL: https://gcc.gnu.org/viewcvs?rev=268639&root=gcc&view=rev
Log:
	Backported from mainline
	2019-01-31  Jakub Jelinek  <jakub@redhat.com>

	PR sanitizer/89124
	* ipa-inline.c (sanitize_attrs_match_for_inline_p): Allow inlining
	always_inline callees into no_sanitize_address callers.

	* c-c++-common/asan/pr89124.c: New test.

Added:
    branches/gcc-8-branch/gcc/testsuite/c-c++-common/asan/pr89124.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/ipa-inline.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
Comment 8 Andrew Pinski 2021-08-18 02:46:22 UTC
*** Bug 67368 has been marked as a duplicate of this bug. ***