Bug 61693

Summary: [asan] is not intercepting aligned_alloc
Product: gcc Reporter: Lars Gullik Bjønnes <larsbj>
Component: sanitizerAssignee: Kostya Serebryany <kcc>
Status: RESOLVED FIXED    
Severity: normal CC: dodji, dvyukov, eugeni.stepanov, jakub, kcc, tavianator
Priority: P3    
Version: 5.0   
Target Milestone: 5.0   
Host: Target:
Build: Known to work: 5.0
Known to fail: Last reconfirmed: 2014-07-04 00:00:00

Description Lars Gullik Bjønnes 2014-07-03 23:00:48 UTC
cat aligned_alloc.c
#include <stdlib.h>

int main(void)
{
  void * p = aligned_alloc(128, 1024);
  free(p);
}

$ gcc -std=c11 -fsanitize=address aligned_alloc.c && ./a.out 
=================================================================
==28341==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x000000728080 in thread T0
    #0 0x7fa78060d2d7 in __interceptor_free ../../../../gcc/libsanitizer/asan/asan_malloc_linux.cc:62
    #1 0x40077e in main (/home/lgb/Development/test/a.out+0x40077e)
    #2 0x31c0821d64 in __libc_start_main (/lib64/libc.so.6+0x31c0821d64)
    #3 0x400688 (/home/lgb/Development/test/a.out+0x400688)

AddressSanitizer can not describe address in more detail (wild memory access suspected).
SUMMARY: AddressSanitizer: bad-free ../../../../gcc/libsanitizer/asan/asan_malloc_linux.cc:62 __interceptor_free
==28341==ABORTING


AFAICS the asan interceptor for aligned_alloc is missing.
Comment 1 Kostya Serebryany 2014-07-04 06:55:30 UTC
Interesting. This is a relatively new c11 feature. 
On older Linux distros this does not compile nor link:

aligned_alloc.c: In function ‘main’:
aligned_alloc.c:5:3: warning: implicit declaration of function ‘aligned_alloc’ [-Wimplicit-function-declaration]
   void * p = aligned_alloc(128, 1024);
...
/tmp/ccJJcsqx.o: In function `main':
aligned_alloc.c:(.text+0x13): undefined reference to `aligned_alloc'

On Ubuntu 14.04 it behaves as you describe and indeed we need to add an interceptor. The tricky part is to add a test that will work on older distros
Comment 2 Jakub Jelinek 2014-07-04 07:17:21 UTC
Just declare it yourself?  If libasan always contains the aligned_alloc symbol, then the testcase will link even when system glibc does not contain it...
Comment 3 Kostya Serebryany 2014-07-04 07:23:07 UTC
yep, that's the plan...
Comment 4 Kostya Serebryany 2014-07-04 07:43:31 UTC
http://llvm.org/viewvc/llvm-project?view=revision&revision=212322 implements 
aligned_alloc support on Linux.
I will let others implement Android/Windows/Mac support if relevant.

This will reach GCC with the next libsanitizer merge (not sure when), 
or feel free to cherry pick in a couple of days (to make sure it works well)
Comment 5 Yuri Gribov 2017-07-07 07:55:26 UTC
Has been fixed in r215527. Close?
Comment 6 Yury Gribov 2017-07-18 19:58:46 UTC
Fixed.