llvm / llvm/llvm-project

[LeakSanitizer] incomplete trace of allocation

Open
#188,431 0 comments 0 reactions 0 assignees View on GitHub
compiler-rt:lsan
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```cpp
#include

#include

int main()
{
const char *pcreCompileErrorStr = NULL;
int erroffset = 0;
pcre * re = pcre_compile("begin.*end", 0, &pcreCompileErrorStr, &erroffset, NULL);
if (!re) {
fputs(pcreCompileErrorStr, stdout);
return 1;
}

const char *pcreStudyErrorStr = NULL;
pcre_extra * extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
if (!extra) {
pcre_free(re);
fputs(pcreStudyErrorStr, stdout);
return 2;
}

int ovector[3];
pcre_exec(re, extra, "pattern", 7, 0, 0, ovector, 3);

pcre_free(extra); // leak: use pcre_free_study() instead
pcre_free(re);
return 0;
}
```

`$ clang -o pcre a.c -g -lpcre -fsanitize=leak`

This reports the following which makes it harder to determine where the leak is actually coming from:
```
=================================================================
==77637==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 96 byte(s) in 1 object(s) allocated from:
#0 0x5591ed4c3d10 in malloc (/home/user/CLionProjects/cppcheck/pcre+0x46d10) (BuildId: cc6cad6230b276f6bc22f3d3a5dbd945b96edc7c)
#1 0x7f29300627b9 in _pcre_jit_compile /usr/src/debug/pcre/pcre-8.45/pcre_jit_compile.c:11558:15

Indirect leak of 264 byte(s) in 1 object(s) allocated from:
#0 0x5591ed4c3d10 in malloc (/home/user/CLionProjects/cppcheck/pcre+0x46d10) (BuildId: cc6cad6230b276f6bc22f3d3a5dbd945b96edc7c)
#1 0x7f293003be73 in allocate_read_only_data /usr/src/debug/pcre/pcre-8.45/pcre_jit_compile.c:2356:22

SUMMARY: LeakSanitizer: 360 byte(s) leaked in 2 allocation(s).
```

Running it in valgrind provides more information:
```
==77711== Memcheck, a memory error detector
==77711== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==77711== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==77711== Command: ./pcre
==77711==
==77711==
==77711== HEAP SUMMARY:
==77711== in use at exit: 360 bytes in 2 blocks
==77711== total heap usage: 9 allocs, 7 frees, 8,998 bytes allocated
==77711==
==77711== 360 (96 direct, 264 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==77711== at 0x484B7A8: malloc (vg_replace_malloc.c:446)
==77711== by 0x48E67B9: _pcre_jit_compile (pcre_jit_compile.c:11558)
==77711== by 0x48E8C51: pcre_study (pcre_study.c:1630)
==77711== by 0x4001209: main (a.c:16)
==77711==
==77711== LEAK SUMMARY:
==77711== definitely lost: 96 bytes in 1 blocks
==77711== indirectly lost: 264 bytes in 1 blocks
==77711== possibly lost: 0 bytes in 0 blocks
==77711== still reachable: 0 bytes in 0 blocks
==77711== suppressed: 0 bytes in 0 blocks
==77711==
==77711== For lists of detected and suppressed errors, rerun with: -s
==77711== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
```

I am using LLVM 22 on Manjaro Linux. I have seen the same behavior with older versions on Ubuntu.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided C reproducer with Clang and LeakSanitizer, then compare its allocation trace with the shown Valgrind output. Trace the LeakSanitizer report for the PCRE JIT allocations and determine why the call chain omits pcre_study; done means the report identifies the relevant allocation path more completely.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.