Skip to content

Conversation

@sorokin
Copy link
Contributor

@sorokin sorokin commented Feb 26, 2023

When running with -fsanitize=leak enabled nasm prints these errors:

Direct leak of 114 byte(s) in 10 object(s) allocated from:
    #0 0x7f3031ef0867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    #2 0x564dc07f606a in detoken asm/preproc.c:2029
    #3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    #4 0x564dc0797f3e in main asm/nasm.c:654
    #5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    #7 0x564dc0799c24 in _start (/home/ivan/d/nasm/nasm+0x2e5c24)

Direct leak of 10 byte(s) in 10 object(s) allocated from:
    #0 0x7f3031ef0867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    #2 0x564dc07f64f9 in detoken asm/preproc.c:2029
    #3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    #4 0x564dc0797f3e in main asm/nasm.c:654
    #5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    #7 0x564dc0799c24 in _start (/home/ivan/d/nasm/nasm+0x2e5c24)

This is reproducible on tests that do preprocessing for example weirdpaste test.

The problem is caused by the fact that the line returned by pp_getline isn't freed in main function.

@sorokin sorokin force-pushed the fix-memory-leak-in-E-mode branch 2 times, most recently from 4e3099c to 31816e7 Compare March 5, 2023 20:07
@hpax
Copy link
Member

hpax commented Nov 4, 2025

Is this still a problem?

@sorokin
Copy link
Contributor Author

sorokin commented Jan 7, 2026

I tested the latest commit 22a9118 in master and, yes, the bug is still present. The exact error message is slightly different, but I guess it is because of the newer compiler version.

In order to reproduce the problem one can run make test when nasm is configured with -fsanitize=address,undefined. Here is how I did it:

git clone [email protected]:netwide-assembler/nasm.git
cd nasm
./autogen.sh

# at first run tests normally to create the directory test/golden
./configure
make
make test # tests print a lot of errors, but this is because we have no directory test/golden yet
# tests created the directory test/testresults, rename it as test/golden
mv test/testresults test/golden

# now run the tests under the sanitizers
make clean # make clean is important here, as the build system doesn't rebuild when compiler flags are changed!
CFLAGS="-fsanitize=address,undefined -g" ./configure
make
make test
# now observe a lot of mismatches of stderr files

In this specific bug the problematic file is test/testresults/weirdpaste/preproc/stderr. On my machin the specific output is:

==996926==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 124 byte(s) in 20 object(s) allocated from:
    #0 0x7e96126fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x55a08fa0f6b5 in nasm_malloc nasmlib/alloc.c:25
    #2 0x55a08f90db3a in detoken asm/preproc.c:2219
    #3 0x55a08f949b38 in pp_getline asm/preproc.c:8815
    #4 0x55a08f8ab33e in main asm/nasm.c:602
    #5 0x7e9611a2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #6 0x7e9611a2a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #7 0x55a08f8a6e84 in _start (/mnt/c/w/projects/nasm/nasm+0x34be84) (BuildId: d3f329ecc664d9645b45338a0a54df158f5195dc)

SUMMARY: AddressSanitizer: 124 byte(s) leaked in 20 allocation(s).

So, essentially the same problem as I reported originally.

When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 124 byte(s) in 20 object(s) allocated from:
    #0 0x7e96126fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    netwide-assembler#1 0x55a08fa0f6b5 in nasm_malloc nasmlib/alloc.c:25
    netwide-assembler#2 0x55a08f90db3a in detoken asm/preproc.c:2219
    netwide-assembler#3 0x55a08f949b38 in pp_getline asm/preproc.c:8815
    netwide-assembler#4 0x55a08f8ab33e in main asm/nasm.c:602
    netwide-assembler#5 0x7e9611a2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#6 0x7e9611a2a28a in __libc_start_main_impl ../csu/libc-start.c:360
    netwide-assembler#7 0x55a08f8a6e84 in _start	(/home/ivan/d/nasm/nasm+0x34be84)

This is reproducible on tests that do preprocessing for example
weirdpaste.asm test.

The problem is caused by the fact that the line returned by pp_getline
isn't freed in main function.

Signed-off-by: Ivan Sorokin <[email protected]>
@sorokin sorokin force-pushed the fix-memory-leak-in-E-mode branch from 31816e7 to ba58085 Compare January 7, 2026 23:26
@sorokin
Copy link
Contributor Author

sorokin commented Jan 7, 2026

I rebased the patch so it applies cleanly to the latest master. I also retested the patch and I can confirm that without it the test/testresults/weirdpaste/preproc/stderr shows the memory leaks and with the patch the test passes cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants