Skip to content

LeakSanitizer: false negative when functions stack frames overlay #937

@rusyaev-roman

Description

@rusyaev-roman

Hi!

This is a simple test demonstrating false-negative memory leak detection when two functions stack frames overlay; here the first function allocates memory and stores pointer in the stack, and the second function invokes lsan to detect memory leak

$ uname -a

Linux machine_name 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u4 (2015-09-19) x86_64 GNU/Linux

$ clang --version | head -1

clang version 5.0.1 (tags/RELEASE_501/final)

$ cat t.c

#include <sanitizer/lsan_interface.h>
#include <stdlib.h>
#include <assert.h>

#define NOINLINE __attribute__((noinline))

NOINLINE
void foo( size_t size )
{
    volatile char * a[128];

    a[0] = (volatile char *)malloc( size );
    assert( a[0] );
}

NOINLINE
void bar( void )
{
#ifdef ENABLE_LSAN_FALSE_NEGATIVE
    volatile char * a[128];
#endif

    __lsan_do_leak_check();
}

int main( int argc, char ** argv )
{
    foo( argc );
    bar();

    return 0;
}

$ clang t.c -fsanitize=leak -DENABLE_LSAN_FALSE_NEGATIVE && ./a.out
$ echo $?

0

if we unset the macro ENABLE_LSAN_FALSE_NEGATIVE then lsan will detect memory leak as expected

$ clang t.c -fsanitize=leak && ./a.out

=================================================================
==4778==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1 byte(s) in 1 object(s) allocated from
#0 0x4097c7 in __interceptor_malloc ...
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions