Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak in elf_init #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sorokin
Copy link

@sorokin sorokin commented Mar 5, 2023

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

Direct leak of 31 byte(s) in 1 object(s) allocated from:
    #0 0x7faadfc56867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7faadf3ecaac in __GI___libc_scratch_buffer_dupfree malloc/scratch_buffer_dupfree.c:32
    #2 0x7faadf395eed in scratch_buffer_dupfree ../include/scratch_buffer.h:147
    #3 0x7faadf395eed in realpath_stk stdlib/canonicalize.c:424
    #4 0x7faadf3964f5 in __GI___realpath stdlib/canonicalize.c:446
    #5 0x7faadfbe6b7c in __interceptor_canonicalize_file_name ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:3682
    #6 0x5643cfb1b058 in nasm_realpath nasmlib/realpath.c:58
    #7 0x5643cfaaaef5 in elf_init output/outelf.c:547
    #8 0x5643cfaaade7 in elf32_init output/outelf.c:496
    #9 0x5643cf9d1454 in main asm/nasm.c:716
    #10 0x7faadf36ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #11 0x7faadf36ee3f in __libc_start_main_impl ../csu/libc-start.c:392
    #12 0x5643cf9cce04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7faadfc56867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5643cf9dcbc4 in nasm_malloc nasmlib/alloc.c:55
    #2 0x5643cf9dced4 in nasm_strndup nasmlib/alloc.c:127
    #3 0x5643cfb1b4e4 in nasm_dirname nasmlib/path.c:152
    #4 0x5643cfaaaf4d in elf_init output/outelf.c:550
    #5 0x5643cfaaade7 in elf32_init output/outelf.c:496
    #6 0x5643cf9d1454 in main asm/nasm.c:716
    #7 0x7faadf36ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #8 0x7faadf36ee3f in __libc_start_main_impl ../csu/libc-start.c:392
    #9 0x5643cf9cce04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error is reproducible on any test that is run with -felf.

The problem is fixed by calling nasm_free appropriately. I had to remove const from pointers because I get -Wdiscarded-qualifiers warning otherwise, because nasm_free takes non-const pointer.

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

Direct leak of 31 byte(s) in 1 object(s) allocated from:
    #0 0x7faadfc56867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x7faadf3ecaac in __GI___libc_scratch_buffer_dupfree malloc/scratch_buffer_dupfree.c:32
    netwide-assembler#2 0x7faadf395eed in scratch_buffer_dupfree ../include/scratch_buffer.h:147
    netwide-assembler#3 0x7faadf395eed in realpath_stk stdlib/canonicalize.c:424
    netwide-assembler#4 0x7faadf3964f5 in __GI___realpath stdlib/canonicalize.c:446
    netwide-assembler#5 0x7faadfbe6b7c in __interceptor_canonicalize_file_name ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:3682
    netwide-assembler#6 0x5643cfb1b058 in nasm_realpath nasmlib/realpath.c:58
    netwide-assembler#7 0x5643cfaaaef5 in elf_init output/outelf.c:547
    netwide-assembler#8 0x5643cfaaade7 in elf32_init output/outelf.c:496
    netwide-assembler#9 0x5643cf9d1454 in main asm/nasm.c:716
    netwide-assembler#10 0x7faadf36ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#11 0x7faadf36ee3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#12 0x5643cf9cce04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7faadfc56867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5643cf9dcbc4 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5643cf9dced4 in nasm_strndup nasmlib/alloc.c:127
    netwide-assembler#3 0x5643cfb1b4e4 in nasm_dirname nasmlib/path.c:152
    netwide-assembler#4 0x5643cfaaaf4d in elf_init output/outelf.c:550
    netwide-assembler#5 0x5643cfaaade7 in elf32_init output/outelf.c:496
    netwide-assembler#6 0x5643cf9d1454 in main asm/nasm.c:716
    netwide-assembler#7 0x7faadf36ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#8 0x7faadf36ee3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#9 0x5643cf9cce04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error is reproducible on any test that is run with -felf.

The problem is fixed by calling nasm_free appropriately. I had to
remove const from pointers because I get -Wdiscarded-qualifiers
warning otherwise, because nasm_free takes non-const pointer.

Signed-off-by: Ivan Sorokin <vanyacpp@gmail.com>
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.

1 participant