Bug 215455

Summary: AddressSanitizer unlike libc provides memalign() confusing consumers
Product: Base System Reporter: Jan Beich <jbeich>
Component: binAssignee: Dimitry Andric <dim>
Status: Closed FIXED    
Severity: Affects Only Me CC: dim, emaste
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description Jan Beich freebsd_committer freebsd_triage 2016-12-20 21:21:20 UTC
Some build systems test symbol availability without prototypes in case of broken/incomplete headers or due to simplicity. For one, Firefox has AC_CHECK_FUNCS(memalign) which fails on FreeBSD but defines HAVE_MEMALIGN with ASan. The code can be unwrapped into the following:

  $ cat a.c
  char memalign();

  int main()
  {
      memalign();
      return 0;
  }

  $ cc a.c -fsanitize=address
  $ echo $?
  0

  $ cc a.c
  /tmp/a-294bd8.o: In function `main':
  a.c:(.text+0x12): undefined reference to `memalign'
  $ echo $?
  1
Comment 1 Dimitry Andric freebsd_committer freebsd_triage 2016-12-20 21:34:24 UTC
This is similar to bug 215125, which is for mallinfo() and mallopt().  I submitted an upstream review at https://reviews.llvm.org/D27654, still awaiting approval.  I will submit something similar for this one.
Comment 2 commit-hook freebsd_committer freebsd_triage 2017-02-01 20:42:01 UTC
A commit references this bug:

Author: dim
Date: Wed Feb  1 20:41:09 UTC 2017
New revision: 313052
URL: https://svnweb.freebsd.org/changeset/base/313052

Log:
  Pull in r293536 from upstream compiler-rt trunk:

    Recommit: Stop intercepting some malloc-related functions on FreeBSD
    and macOS

    Summary:
    In https://bugs.freebsd.org/215125 I was notified that some configure
    scripts attempt to test for the Linux-specific `mallinfo` and
    `mallopt` functions by compiling and linking small programs which
    references the functions, and observing whether that results in
    errors.

    FreeBSD and macOS do not have the `mallinfo` and `mallopt` functions,
    so normally these tests would fail, but when sanitizers are enabled,
    they incorrectly succeed, because the sanitizers define interceptors
    for these functions.  This also applies to some other malloc-related
    functions, such as `memalign`, `pvalloc` and `cfree`.

    Fix this by not intercepting `mallinfo`, `mallopt`, `memalign`,
    `pvalloc` and `cfree` for FreeBSD and macOS, in all sanitizers.

    Also delete the non-functional `cfree` wrapper for Windows, to fix the
    test cases on that platform.

    Reviewers: emaste, kcc, rnk

    Subscribers: timurrrr, eugenis, hans, joerg, llvm-commits, kubamracek

    Differential Revision: https://reviews.llvm.org/D27654

  This prevents autoconf scripts from incorrectly detecting that functions
  like mallinfo, mallopt, memalign, pvalloc and cfree are supported.

  PR:		215125, 215455
  MFC after:	1 week

Changes:
  head/contrib/compiler-rt/lib/asan/asan_malloc_linux.cc
  head/contrib/compiler-rt/lib/asan/asan_malloc_win.cc
  head/contrib/compiler-rt/lib/lsan/lsan_interceptors.cc
  head/contrib/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
Comment 3 Dimitry Andric freebsd_committer freebsd_triage 2017-02-01 20:45:15 UTC
Fix applied to head, will also MFC to stable/11 after a week.
Comment 4 commit-hook freebsd_committer freebsd_triage 2017-02-13 18:39:55 UTC
A commit references this bug:

Author: dim
Date: Mon Feb 13 18:39:22 UTC 2017
New revision: 313702
URL: https://svnweb.freebsd.org/changeset/base/313702

Log:
  MFC r313052:

  Pull in r293536 from upstream compiler-rt trunk:

    Recommit: Stop intercepting some malloc-related functions on FreeBSD
    and macOS

    Summary:
    In https://bugs.freebsd.org/215125 I was notified that some configure
    scripts attempt to test for the Linux-specific `mallinfo` and
    `mallopt` functions by compiling and linking small programs which
    references the functions, and observing whether that results in
    errors.

    FreeBSD and macOS do not have the `mallinfo` and `mallopt` functions,
    so normally these tests would fail, but when sanitizers are enabled,
    they incorrectly succeed, because the sanitizers define interceptors
    for these functions.  This also applies to some other malloc-related
    functions, such as `memalign`, `pvalloc` and `cfree`.

    Fix this by not intercepting `mallinfo`, `mallopt`, `memalign`,
    `pvalloc` and `cfree` for FreeBSD and macOS, in all sanitizers.

    Also delete the non-functional `cfree` wrapper for Windows, to fix the
    test cases on that platform.

    Reviewers: emaste, kcc, rnk

    Subscribers: timurrrr, eugenis, hans, joerg, llvm-commits, kubamracek

    Differential Revision: https://reviews.llvm.org/D27654

  This prevents autoconf scripts from incorrectly detecting that functions
  like mallinfo, mallopt, memalign, pvalloc and cfree are supported.

  PR:		215125, 215455

Changes:
_U  stable/11/
  stable/11/contrib/compiler-rt/lib/asan/asan_malloc_linux.cc
  stable/11/contrib/compiler-rt/lib/asan/asan_malloc_win.cc
  stable/11/contrib/compiler-rt/lib/lsan/lsan_interceptors.cc
  stable/11/contrib/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h