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

Memory Leak wasm2c.c while building zig #21777

Closed
lefsha opened this issue Oct 23, 2024 · 6 comments
Closed

Memory Leak wasm2c.c while building zig #21777

lefsha opened this issue Oct 23, 2024 · 6 comments
Labels
question No questions on the issue tracker, please.

Comments

@lefsha
Copy link

lefsha commented Oct 23, 2024

Zig Version

latest dev as well as any

Steps to Reproduce and Observed Behavior

CARGS =
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_VERBOSE_MAKEFILE=ON
-DZIG2_NO_RTLIB=OFF
-DZIG_AR_WORKAROUND=OFF
-DZIG_EXTRA_BUILD_ARGS=OFF
-DZIG_HOST_TARGET_TRIPLE=x86_64-linux
-DZIG_NO_LIB=OFF
-DZIG_PIE=OFF
-DZIG_SHARED_LLVM=OFF
-DZIG_SINGLE_THREADED=OFF
-DZIG_STATIC=OFF
-DZIG_STATIC_CURSES=OFF
-DZIG_STATIC_LLVM=OFF
-DZIG_STATIC_ZLIB=OFF
-DZIG_STATIC_ZSTD=OFF
-DZIG_SYSTEM_LIBCXX=stdc++
-DZIG_TARGET_MCPU=x86_64
-DZIG_TARGET_TRIPLE=native
-Wno-dev \

-- The C compiler identification is Clang 19.1.2
-- The CXX compiler identification is Clang 19.1.2
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/lib/llvm/19/bin/clang-19 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/lib/llvm/19/bin/clang++-19 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.14.0-dev.1954+2d888a8e6
-- Found llvm: -lLLVM-19 (Required is at least version "19")
-- Found clang: /usr/lib/llvm/19/lib64/libclang-cpp.so.19.1 (Required is at least version "19")
-- Found lld: /usr/lib/llvm/19/lib64/liblldMinGW.so;/usr/lib/llvm/19/lib64/liblldELF.so;/usr/lib/llvm/19/lib64/liblldCOFF.so;/usr/lib/llvm/19/lib64/liblldWasm.so;/usr/lib/llvm/19/lib64/liblldMachO.so;/usr/lib/llvm/19/lib64/liblldCommon.so (Required is at least version "19")
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done (2.7s)
-- Generating done (0.0s)
-- Build files have been written to: /var/tmp/lang-zig-bld20241023110438
[1/19] Building C object CMakeFiles/zig-wasm2c.dir/stage1/wasm2c.c.o

...

Direct leak of 20460 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc790 in realloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc790)
#1 0x00000042d388 in FuncGen_localAlloc /shr/git/rep/lang/zig/zig/stage1/FuncGen.h:72:22
#2 0x000000428eb3 in FuncGen_blockBegin /shr/git/rep/lang/zig/zig/stage1/FuncGen.h:166:22
#3 0x000000424cea in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:795:29
#4 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 14956 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000420084 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:353:17
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 9920 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc790 in realloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc790)
#1 0x000000428e0c in FuncGen_blockBegin /shr/git/rep/lang/zig/zig/stage1/FuncGen.h:154:23
#2 0x000000424cea in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:795:29
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 1488 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x00000041f220 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:295:17
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 576 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x00000041f5e8 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:313:19
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 560 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc790 in realloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc790)
#1 0x00000042a641 in FuncGen_stackPop /shr/git/rep/lang/zig/zig/stage1/FuncGen.h:133:23
#2 0x000000425087 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:952:56
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 240 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc790 in realloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc790)
#1 0x00000042afd2 in FuncGen_stackPush /shr/git/rep/lang/zig/zig/stage1/FuncGen.h:116:23
#2 0x0000004244cf in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:1372:29
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 100 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000421ded in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:514:33
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 25 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000421ddd in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:513:28
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 12 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x0000004208fa in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:382:18
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000420d2a in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:407:16
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Direct leak of 2 byte(s) in 1 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000420e74 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:425:19
#2 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Indirect leak of 844 byte(s) in 93 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000428260 in InputStream_readResultType /shr/git/rep/lang/zig/zig/stage1/InputStream.h:163:38
#2 0x00000041f26b in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:299:30
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Indirect leak of 552 byte(s) in 24 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x0000004283e2 in InputStream_readName /shr/git/rep/lang/zig/zig/stage1/InputStream.h:137:18
#2 0x00000041f648 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:316:30
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Indirect leak of 422 byte(s) in 93 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x000000428260 in InputStream_readResultType /shr/git/rep/lang/zig/zig/stage1/InputStream.h:163:38
#2 0x00000041f386 in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:301:31
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

Indirect leak of 326 byte(s) in 24 object(s) allocated from:
#0 0x0000003dc4e3 in malloc (/var/tmp/lang-zig-bld20241023110438/zig-wasm2c+0x3dc4e3)
#1 0x0000004283e2 in InputStream_readName /shr/git/rep/lang/zig/zig/stage1/InputStream.h:137:18
#2 0x00000041f68e in main /shr/git/rep/lang/zig/zig/stage1/wasm2c.c:317:31
#3 0x7f868f5d8a3c (/lib64/libc.so.6+0x24a3c)

SUMMARY: AddressSanitizer: 50491 byte(s) leaked in 246 allocation(s).

Expected Behavior

Every time I build ZIG I end up with Segfault by just executing zig stage3.
I have enabled all debugging options possible to track where it all comes from.
The problem has began already with wasm2c.c

Did anyone checked the code ever?

Expected no memory leak.

The full log can be attache, but I don't know where. No such option.

@lefsha lefsha added the bug Observed behavior contradicts documented or intended behavior label Oct 23, 2024
@alexrp
Copy link
Member

alexrp commented Oct 23, 2024

Did anyone checked the code ever?

~Everyone who works on Zig bootstraps with wasm2c regularly.

Expected no memory leak.

It seems unlikely that we'll fix this. The leak here is bounded and there's no real benefit to freeing the memory just before exiting; rather, doing so would just slow down the bootstrap. A total of 50491 bytes leaked should not cause an OOM on any reasonably usable machine today, anyway.


More to the point, I suggest building without Address Sanitizer and, if wasm2c still crashes, try to attach GDB to it and get a backtrace.

@linusg
Copy link
Contributor

linusg commented Oct 23, 2024

doing so would just slow down the bootstrap.

That's a ridiculous argument in favor of leaking memory. It takes a non-zero amount of time, yes, but it's not slow until you have a profile saying so :)

@rohlem
Copy link
Contributor

rohlem commented Oct 23, 2024

If there are users / use cases that want to leak memory and those who don't, the free-ing could be put behind a macro / compile flag.
I don't know whether the current maintainers see this as a worthwhile time investment, so it might be up to an external contributor that cares about this feature.

Every time I build ZIG I end up with Segfault by just executing zig stage3.

Are you stating the produced zig binary crashes due to a segfault? If so, I see no relation between that and memory leaks when executing wasm2c (a much earlier step in the build process).
Memory leaks don't imply any error in the intended output file, this is just a detail of the program's (wasm2c's) internal memory management strategy.

If the produced compiler executable segfaults, please ask in one of the community spaces for help, or file a separate issue - you'll probably have to provide more details about your system, the exact build commands used, etc.
You might also want to try one of the nightly builds, if your platform is provided there.
Since they are compiled from the same source, if they work and your own build doesn't, the error has to be about some difference between your build process/environment and the one in the CI / of other people successfully building from source.

@andrewrk andrewrk added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Oct 23, 2024
@andrewrk
Copy link
Member

working as designed

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2024
@andrewrk
Copy link
Member

That's a ridiculous argument in favor of leaking memory

no. it's ridiculous to suggest adding additional, unnecessary syscalls to reclaim 50K virtual memory, when those 50K will be reclaimed by the OS.

@linusg
Copy link
Contributor

linusg commented Oct 23, 2024

I was not suggesting anything, merely pointing out that @alexrp's "freeing this memory slows down bootstrap" doesn't match reality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

5 participants