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

Support for COMDAT sections for COFF output #7

Closed
wants to merge 4 commits into from

Conversation

igg0
Copy link
Contributor

@igg0 igg0 commented Mar 31, 2021

Hi,

I'd like to include COMDAT support to nasm. When linking a win32/win64 module, it provides the possibility of function-level linking, and generally helps with dead code elimination (for example, when we link our x64 object files with a proper unwinding information, all object files from the library end up in the resulting module even if they are not used at all - most likely because the unwinding info introduces references to them and the linker is subsequently unable to remove them).

I'm not saying I'm any expert on COMDAT format - but I checked the specs, checked the Microsoft compiler/linker behavior on simple test files, and prepared some changes to nasm code. The output of the new nasm works fine with my code and gets rid of the dead code I've had so far, though of course I'm not suggesting I covered all possible scenarios.

The changes are restricted to outcoff.c. The new code is conditioned by the new section qualifier being used (or the subsequent coff_Section fields having been set) - so it shouldn't break any existing functionality.

The COMDAT support is enabled by comdat=... extension of the SECTION directive. The format I chose (for no special reason) is comdat=selection:name where selection is a number corresponding to one of the IMAGE_COMDAT_SELECT_* constants and name is the chosed comdat symbol/name). So it can be e.g.
SECTION .text$aa align=32 comdat=1:MyFunction1
If selection==5, the name is the comdat symbol of the section this section should be associated with. So the unwinding info for the previous example may start e.g. with
SECTION .xdata comdat=5:MyFunction1
Since the extension is ignored by older versions of nasm, a source using it successfully compiles even on older versions of nasm - just the code is emitted into the "big" sections, not split into comdat's (not saying this property is very useful, but it could be kinda convenient).

I created this pull request mainly so that you can check the code and give me your comments - regarding the chosen syntax of the extension, the code changes, and also about the interest in this feature :-) (I admit I'm not too familiar with what's happening during the multiple passes when nasm runs so maybe you'll spot some troubles.) If you find the syntax (and also the rest) OK, I can also extend the docs, prepare some samples etc.

Thanks!

@igg0
Copy link
Contributor Author

igg0 commented Apr 7, 2021

Even though the specs don't say that a specific order of sections is required for IMAGE_COMDAT_SELECT_ASSOCIATIVE, it seems it is... so I added a little hack to support defining the associated "pair" in any order. If it's too ugly, it can be removed and enforced (as in the initial commit) that the first section has to be defined before the one with selection==5 (or possibly there's a better solution, of course).
Now I'll just wait for @hpax or @cyrillos I guess ;-)

@cyrillos cyrillos self-assigned this Apr 14, 2021
@cyrillos
Copy link
Member

Thanks for patches! I'll take a look once time permit. And very sorry for long delay in answers.

@igg0
Copy link
Contributor Author

igg0 commented Dec 9, 2021

I know time is a precious commodity… I would just like to ask (if that answer itself isn’t a bigger task as a "design" decision) if you find the suggested syntax fine.
I’m OK with using a custom nasm build in our projects for the time being, however I’d rather avoid updating our asm code twice (if the COMDAT-related changes got included in nasm one day but with a different syntax).
Thanks!

@cyrillos
Copy link
Member

@igg0 I'm very very sorry for taking you so long. You know we're not big fans of pull requests. Could you be so kind and send patches to https://lists.zytor.com/nasm-devel mailing list? At lease there would be a chance the patches get reviewed and merged. Again, I'm really sorry for keeping silence since my last reply.

@cyrillos
Copy link
Member

Or you can send them to me directly to [email protected] and I'll resend them for review.

@igg0 igg0 closed this Jan 20, 2022
sorokin added a commit to sorokin/nasm that referenced this pull request 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
    netwide-assembler#1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x564dc07f606a in detoken asm/preproc.c:2029
    netwide-assembler#3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    netwide-assembler#4 0x564dc0797f3e in main asm/nasm.c:654
    netwide-assembler#5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#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
    netwide-assembler#1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x564dc07f64f9 in detoken asm/preproc.c:2029
    netwide-assembler#3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    netwide-assembler#4 0x564dc0797f3e in main asm/nasm.c:654
    netwide-assembler#5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#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 added a commit to sorokin/nasm that referenced this pull request Feb 26, 2023
When running with -fsanitize=leak enabled nasm prints these errors:

Direct leak of 25 byte(s) in 5 object(s) allocated from:
    #0 0x7f5fc494b867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a8037f10e0 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a8037f10e0 in nasm_strdup nasmlib/alloc.c:117
    netwide-assembler#3 0x55a803873172 in expand_mmacro asm/preproc.c:6905
    netwide-assembler#4 0x55a803873172 in pp_tokline asm/preproc.c:7814
    netwide-assembler#5 0x55a803873172 in pp_getline asm/preproc.c:7826
    netwide-assembler#6 0x55a8037eb5d8 in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55a8037e5761 in main asm/nasm.c:719
    netwide-assembler#8 0x7f5fc4063d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f5fc4063e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a8037e7c34 in _start (/home/ivan/d/nasm/nasm+0x2e5c34)

This is reproducible on many tests, for example on zerobyte.asm.

The problem was that MMacro::iname is only allocated but never freed.
sorokin added a commit to sorokin/nasm that referenced this pull request Feb 26, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 25 byte(s) in 5 object(s) allocated from:
    #0 0x7f5fc494b867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a8037f10e0 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a8037f10e0 in nasm_strdup nasmlib/alloc.c:117
    netwide-assembler#3 0x55a803873172 in expand_mmacro asm/preproc.c:6905
    netwide-assembler#4 0x55a803873172 in pp_tokline asm/preproc.c:7814
    netwide-assembler#5 0x55a803873172 in pp_getline asm/preproc.c:7826
    netwide-assembler#6 0x55a8037eb5d8 in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55a8037e5761 in main asm/nasm.c:719
    netwide-assembler#8 0x7f5fc4063d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f5fc4063e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a8037e7c34 in _start (/home/ivan/d/nasm/nasm+0x2e5c34)

This is reproducible on many tests, for example on zerobyte.asm.

The problem was that MMacro::iname is only allocated but never freed.
sorokin added a commit to sorokin/nasm that referenced this pull request Feb 26, 2023
When running with -fsanitize=leak enabled nasm prints this error:

==19965==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 360 byte(s) in 90 object(s) allocated from:
    #0 0x7faee9396867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5645d39a401c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5645d3a41f9d in string_transform asm/strfunc.c:356
    netwide-assembler#3 0x5645d3a37d40 in eval_strfunc asm/eval.c:761
    netwide-assembler#4 0x5645d3a37d40 in expr6 asm/eval.c:906
    netwide-assembler#5 0x5645d3a3968d in expr5 asm/eval.c:627
    netwide-assembler#6 0x5645d3a39aca in expr4 asm/eval.c:602
    netwide-assembler#7 0x5645d3a39b72 in expr3 asm/eval.c:563
    netwide-assembler#8 0x5645d3a39db8 in expr2 asm/eval.c:537
    netwide-assembler#9 0x5645d3a39f38 in expr1 asm/eval.c:511
    netwide-assembler#10 0x5645d3a3a0b8 in expr0 asm/eval.c:485
    netwide-assembler#11 0x5645d3a3a242 in rexp3 asm/eval.c:422
    netwide-assembler#12 0x5645d3a3a508 in rexp2 asm/eval.c:396
    netwide-assembler#13 0x5645d3a3a6a8 in rexp1 asm/eval.c:369
    netwide-assembler#14 0x5645d3a3a838 in rexp0 asm/eval.c:342
    netwide-assembler#15 0x5645d3a3a838 in cexpr asm/eval.c:305
    netwide-assembler#16 0x5645d3a3ad08 in bexpr asm/eval.c:298
    netwide-assembler#17 0x5645d3a3ad08 in evaluate asm/eval.c:1032
    netwide-assembler#18 0x5645d39e4f20 in parse_line asm/parser.c:959
    netwide-assembler#19 0x5645d399e243 in assemble_file asm/nasm.c:1735
    netwide-assembler#20 0x5645d3998801 in main asm/nasm.c:719
    netwide-assembler#21 0x7faee8aaed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#22 0x7faee8aaee3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#23 0x5645d399acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 360 byte(s) leaked in 90 allocation(s).

This problem is reproducible on test utf.asm.

The problem was caused by the fact that eval_strfunc doesn't free the string
allocated by string_transform.
sorokin added a commit to sorokin/nasm that referenced this pull request Feb 26, 2023
When running with -fsanitize=leak enabled nasm prints this error:

ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6 byte(s) in 1 object(s) allocated from:
    #0 0x7f17d8a60867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5613fd57401c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5613fd5be840 in dup_text asm/preproc.c:436
    netwide-assembler#3 0x5613fd5dc71d in parse_mmacro_spec asm/preproc.c:3325
    netwide-assembler#4 0x5613fd5e5f7a in do_directive asm/preproc.c:4615
    netwide-assembler#5 0x5613fd5f5e19 in pp_tokline asm/preproc.c:7766
    netwide-assembler#6 0x5613fd5f5e19 in pp_getline asm/preproc.c:7830
    netwide-assembler#7 0x5613fd56e678 in assemble_file asm/nasm.c:1722
    netwide-assembler#8 0x5613fd568801 in main asm/nasm.c:719
    netwide-assembler#9 0x7f17d8178d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#10 0x7f17d8178e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#11 0x5613fd56acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 6 byte(s) leaked in 1 allocation(s).

This error was reproducible on align13s.asm test.

The problem was caused by the fact that do_directive didn't
cleaup properly the macro name returned from parse_mmacro_spec.
sorokin added a commit to sorokin/nasm that referenced this pull request Feb 26, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 25 byte(s) in 5 object(s) allocated from:
    #0 0x7f5fc494b867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a8037f10e0 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a8037f10e0 in nasm_strdup nasmlib/alloc.c:117
    netwide-assembler#3 0x55a803873172 in expand_mmacro asm/preproc.c:6905
    netwide-assembler#4 0x55a803873172 in pp_tokline asm/preproc.c:7814
    netwide-assembler#5 0x55a803873172 in pp_getline asm/preproc.c:7826
    netwide-assembler#6 0x55a8037eb5d8 in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55a8037e5761 in main asm/nasm.c:719
    netwide-assembler#8 0x7f5fc4063d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f5fc4063e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a8037e7c34 in _start (/home/ivan/d/nasm/nasm+0x2e5c34)

This is reproducible on many tests, for example on zerobyte.asm.

The problem was that MMacro::iname is only allocated but never freed.
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

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
    netwide-assembler#1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x564dc07f606a in detoken asm/preproc.c:2029
    netwide-assembler#3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    netwide-assembler#4 0x564dc0797f3e in main asm/nasm.c:654
    netwide-assembler#5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#7 0x564dc0799c24 in _start (/home/ivan/d/nasm/nasm+0x2e5c24)

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.

git push --set-upstream origin fix-null-deref-in-count_mmac_params
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

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
    netwide-assembler#1 0x564dc07a2f6c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x564dc07f606a in detoken asm/preproc.c:2029
    netwide-assembler#3 0x564dc0828a62 in pp_getline asm/preproc.c:7835
    netwide-assembler#4 0x564dc0797f3e in main asm/nasm.c:654
    netwide-assembler#5 0x7f3031608d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#6 0x7f3031608e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#7 0x564dc0799c24 in _start (/home/ivan/d/nasm/nasm+0x2e5c24)

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 added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 25 byte(s) in 5 object(s) allocated from:
    #0 0x7f5fc494b867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a8037f10e0 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a8037f10e0 in nasm_strdup nasmlib/alloc.c:117
    netwide-assembler#3 0x55a803873172 in expand_mmacro asm/preproc.c:6905
    netwide-assembler#4 0x55a803873172 in pp_tokline asm/preproc.c:7814
    netwide-assembler#5 0x55a803873172 in pp_getline asm/preproc.c:7826
    netwide-assembler#6 0x55a8037eb5d8 in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55a8037e5761 in main asm/nasm.c:719
    netwide-assembler#8 0x7f5fc4063d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f5fc4063e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a8037e7c34 in _start (/home/ivan/d/nasm/nasm+0x2e5c34)

This is reproducible on many tests, for example on zerobyte.asm.

The problem was that MMacro::iname is only allocated but never freed.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 25 byte(s) in 5 object(s) allocated from:
    #0 0x7f5fc494b867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a8037f10e0 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a8037f10e0 in nasm_strdup nasmlib/alloc.c:117
    netwide-assembler#3 0x55a803873172 in expand_mmacro asm/preproc.c:6905
    netwide-assembler#4 0x55a803873172 in pp_tokline asm/preproc.c:7814
    netwide-assembler#5 0x55a803873172 in pp_getline asm/preproc.c:7826
    netwide-assembler#6 0x55a8037eb5d8 in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55a8037e5761 in main asm/nasm.c:719
    netwide-assembler#8 0x7f5fc4063d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f5fc4063e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a8037e7c34 in _start (/home/ivan/d/nasm/nasm+0x2e5c34)

This is reproducible on many tests, for example on zerobyte.asm.

The problem was that MMacro::iname is only allocated but never freed.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

==19965==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 360 byte(s) in 90 object(s) allocated from:
    #0 0x7faee9396867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5645d39a401c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5645d3a41f9d in string_transform asm/strfunc.c:356
    netwide-assembler#3 0x5645d3a37d40 in eval_strfunc asm/eval.c:761
    netwide-assembler#4 0x5645d3a37d40 in expr6 asm/eval.c:906
    netwide-assembler#5 0x5645d3a3968d in expr5 asm/eval.c:627
    netwide-assembler#6 0x5645d3a39aca in expr4 asm/eval.c:602
    netwide-assembler#7 0x5645d3a39b72 in expr3 asm/eval.c:563
    netwide-assembler#8 0x5645d3a39db8 in expr2 asm/eval.c:537
    netwide-assembler#9 0x5645d3a39f38 in expr1 asm/eval.c:511
    netwide-assembler#10 0x5645d3a3a0b8 in expr0 asm/eval.c:485
    netwide-assembler#11 0x5645d3a3a242 in rexp3 asm/eval.c:422
    netwide-assembler#12 0x5645d3a3a508 in rexp2 asm/eval.c:396
    netwide-assembler#13 0x5645d3a3a6a8 in rexp1 asm/eval.c:369
    netwide-assembler#14 0x5645d3a3a838 in rexp0 asm/eval.c:342
    netwide-assembler#15 0x5645d3a3a838 in cexpr asm/eval.c:305
    netwide-assembler#16 0x5645d3a3ad08 in bexpr asm/eval.c:298
    netwide-assembler#17 0x5645d3a3ad08 in evaluate asm/eval.c:1032
    netwide-assembler#18 0x5645d39e4f20 in parse_line asm/parser.c:959
    netwide-assembler#19 0x5645d399e243 in assemble_file asm/nasm.c:1735
    netwide-assembler#20 0x5645d3998801 in main asm/nasm.c:719
    netwide-assembler#21 0x7faee8aaed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#22 0x7faee8aaee3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#23 0x5645d399acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 360 byte(s) leaked in 90 allocation(s).

This problem is reproducible on test utf.asm.

The problem was caused by the fact that eval_strfunc doesn't free the string
allocated by string_transform.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6 byte(s) in 1 object(s) allocated from:
    #0 0x7f17d8a60867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5613fd57401c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5613fd5be840 in dup_text asm/preproc.c:436
    netwide-assembler#3 0x5613fd5dc71d in parse_mmacro_spec asm/preproc.c:3325
    netwide-assembler#4 0x5613fd5e5f7a in do_directive asm/preproc.c:4615
    netwide-assembler#5 0x5613fd5f5e19 in pp_tokline asm/preproc.c:7766
    netwide-assembler#6 0x5613fd5f5e19 in pp_getline asm/preproc.c:7830
    netwide-assembler#7 0x5613fd56e678 in assemble_file asm/nasm.c:1722
    netwide-assembler#8 0x5613fd568801 in main asm/nasm.c:719
    netwide-assembler#9 0x7f17d8178d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#10 0x7f17d8178e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#11 0x5613fd56acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 6 byte(s) leaked in 1 allocation(s).

This error was reproducible on align13s.asm test.

The problem was caused by the fact that do_directive didn't
cleaup properly the macro name returned from parse_mmacro_spec.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 3795 byte(s) in 5 object(s) allocated from:
    #0 0x7f1ff9313867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x56000dc80bc4 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x56000dcd526b in new_Token asm/preproc.c:1879
    netwide-assembler#3 0x56000dcd4667 in tokenize asm/preproc.c:1748
    netwide-assembler#4 0x56000dd0809f in pp_tokline asm/preproc.c:7718
    netwide-assembler#5 0x56000dd09715 in pp_getline asm/preproc.c:7834
    netwide-assembler#6 0x56000dc7c63c in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x56000dc754e4 in main asm/nasm.c:719
    netwide-assembler#8 0x7f1ff8a2bd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f1ff8a2be3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x56000dc70e04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error was reproducible on testnos3.asm test.

Signed-off-by: Ivan Sorokin <[email protected]>
@sorokin sorokin mentioned this pull request Mar 5, 2023
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 504 byte(s) in 32 object(s) allocated from:
    #0 0x7f7274bf9867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55a15e7bbbc4 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55a15e7bc069 in nasm_strcat nasmlib/alloc.c:139
    netwide-assembler#3 0x55a15e7f3968 in find_label asm/labels.c:235
    netwide-assembler#4 0x55a15e7f61f7 in define_label asm/labels.c:462
    netwide-assembler#5 0x55a15e7ff1ce in parse_line asm/parser.c:665
    netwide-assembler#6 0x55a15e7b75f8 in assemble_file asm/nasm.c:1735
    netwide-assembler#7 0x55a15e7b04e4 in main asm/nasm.c:719
    netwide-assembler#8 0x7f7274311d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f7274311e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55a15e7abe04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error was reproducible on struc.asm test.

The problem was that not all exit paths freed the allocated
string.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request 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
    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 <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
Direct leak of 960 byte(s) in 5 object(s) allocated from:
    #0 0x7f52b6464a37 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    netwide-assembler#1 0x55cf36676c46 in nasm_calloc nasmlib/alloc.c:72
    netwide-assembler#2 0x55cf36676cd1 in nasm_zalloc nasmlib/alloc.c:87
    netwide-assembler#3 0x55cf366e3980 in do_directive asm/preproc.c:4754
    netwide-assembler#4 0x55cf366fec97 in pp_tokline asm/preproc.c:7773
    netwide-assembler#5 0x55cf366ff84a in pp_getline asm/preproc.c:7837
    netwide-assembler#6 0x55cf3667263c in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55cf3666b4e4 in main asm/nasm.c:719
    netwide-assembler#8 0x7f52b5b7cd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f52b5b7ce3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55cf36666e04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error is reproducible on lnxlinux.asm test or on this
small snippet:

    %rep 8
        nop
        nop
        nop
    %endrep

The original call to free_mmacro was commented out in
91e7240 as it caused
use-after-free. https://bugzilla.nasm.us/show_bug.cgi?id=3392414

After adding free_mmacro I tested nasm with -fsanitize=address
on all four reproducers attached to the issue and none of them
causes use-after-free now. Also this commit passes all tests
without causing use-after-free.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 960 byte(s) in 5 object(s) allocated from:
    #0 0x7f52b6464a37 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    netwide-assembler#1 0x55cf36676c46 in nasm_calloc nasmlib/alloc.c:72
    netwide-assembler#2 0x55cf36676cd1 in nasm_zalloc nasmlib/alloc.c:87
    netwide-assembler#3 0x55cf366e3980 in do_directive asm/preproc.c:4754
    netwide-assembler#4 0x55cf366fec97 in pp_tokline asm/preproc.c:7773
    netwide-assembler#5 0x55cf366ff84a in pp_getline asm/preproc.c:7837
    netwide-assembler#6 0x55cf3667263c in assemble_file asm/nasm.c:1722
    netwide-assembler#7 0x55cf3666b4e4 in main asm/nasm.c:719
    netwide-assembler#8 0x7f52b5b7cd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#9 0x7f52b5b7ce3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#10 0x55cf36666e04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error is reproducible on lnxlinux.asm test or on this
small snippet:

    %rep 8
        nop
        nop
        nop
    %endrep

The original call to free_mmacro was commented out in
91e7240 as it caused
use-after-free. https://bugzilla.nasm.us/show_bug.cgi?id=3392414

After adding free_mmacro I tested nasm with -fsanitize=address
on all four reproducers attached to the issue and none of them
causes use-after-free now. Also this commit passes all tests
without causing use-after-free.

Signed-off-by: Ivan Sorokin <[email protected]>
sorokin added a commit to sorokin/nasm that referenced this pull request Mar 5, 2023
When running with -fsanitize=leak enabled nasm prints this error:

Direct leak of 10 byte(s) in 5 object(s) allocated from:
    #0 0x7f52b6464867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x55cf36676bc4 in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x55cf3667730d in nasm_vaxprintf nasmlib/asprintf.c:61
    netwide-assembler#3 0x55cf36677618 in nasm_asprintf nasmlib/asprintf.c:90
    netwide-assembler#4 0x55cf366e8b96 in expand_mmac_params asm/preproc.c:5451
    netwide-assembler#5 0x55cf366fec7a in pp_tokline asm/preproc.c:7767
    netwide-assembler#6 0x55cf366ff84a in pp_getline asm/preproc.c:7837
    netwide-assembler#7 0x55cf3667263c in assemble_file asm/nasm.c:1722
    netwide-assembler#8 0x55cf3666b4e4 in main asm/nasm.c:719
    netwide-assembler#9 0x7f52b5b7cd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#10 0x7f52b5b7ce3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#11 0x55cf36666e04 in _start (/home/ivan/d/nasm/nasm+0x2e2e04)

This error is reproducing on lnxhello.asm test.

Signed-off-by: Ivan Sorokin <[email protected]>
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