Description
EDIT: Here's a potential workaround:
// force a .data section in the executable to ensure Valgrind debug info works,
// see https://github.com/ziglang/zig/issues/15254
export var foo: usize = 1;
pub fn main() !void {
// ...
foo += 1; // make sure the foo variable doesn't get optimized out
}
There might be a better way to go about it, but this has worked for me.
Zig Version
0.11.0-dev.2546+cb54e9a3c
Steps to Reproduce and Observed Behavior
Similar to #896 but doesn't seem to be the same cause (the --no-rosegment
workaround does not change anything).
Same test file as #896:
pub fn main() void {
foo().* += 1;
}
fn foo() *i32 {
return @intToPtr(*i32, 10000000);
}
Debug symbols do not work with Zig 0.11.0-dev.2546+cb54e9a3c
and Valgrind 3.20.0
(tested with older Valgrinds [3.17.0
, 3.13.0
] and they all work the same so this does not seem to be a Valgrind regression):
$ ~/Downloads/zig-linux-x86_64-0.11.0-dev.2546+cb54e9a3c/zig build-exe main.zig --verbose-link
LLD Link... ld.lld --error-limit=0 -O0 -z stack-size=16777216 --gc-sections -znow -m elf_x86_64 -static -o main main.o /home/ryan/.cache/zig/o/127027172500ef2ec1954339a99cad40/libc.a --as-needed /home/ryan/.cache/zig/o/56279d70bb76b973f322c635848360b7/libcompiler_rt.a
$ valgrind ./main
==2331200== Invalid read of size 4
==2331200== at 0x20B504: ??? (in /home/ryan/Programming/zig/tmp/valgrind-test/main)
==2331200== by 0x20AA75: ??? (in /home/ryan/Programming/zig/tmp/valgrind-test/main)
==2331200== by 0x20A521: ??? (in /home/ryan/Programming/zig/tmp/valgrind-test/main)
==2331200== Address 0x989680 is not stack'd, malloc'd or (recently) free'd
But debug symbols do work with Zig 0.8.0
(this seems to be the latest version that it still worked with, 0.9.0
contains the regression):
$ ~/Downloads/zig-linux-x86_64-0.8.0/zig build-exe main.zig --verbose-link
ld.lld -error-limit=0 -z stack-size=16777216 --gc-sections -m elf_x86_64 -static -o main src/zig-cache/o/a8d722ab2e9f87901a4eaa9324ec91db/main.o /home/ryan/.cache/zig/o/2f79cc6f403d84cbd92f58b396afde89/libc.a /home/ryan/.cache/zig/o/0c6fb8a0904a42c388dd6494d726db60/libcompiler_rt.a
$ valgrind ./main
==2867009== Invalid read of size 4
==2867009== at 0x22A395: main (main.zig:2)
==2867009== Address 0x989680 is not stack'd, malloc'd or (recently) free'd
However, if linking libc (statically with musl or dynamically with glibc), the debug symbols will work fine again:
$ ~/Downloads/zig-linux-x86_64-0.11.0-dev.2546+cb54e9a3c/zig build-exe main.zig --verbose-link -target x86_64-linux-musl -lc
LLD Link... ld.lld --error-limit=0 -O0 -z stack-size=16777216 --gc-sections -znow -m elf_x86_64 -static -o main /home/ryan/.cache/zig/o/7e08d8a5a02fcdc41c637dfd258dfd9b/crt1.o /home/ryan/.cache/zig/o/2471e2ac2b5e9b845351f84f88b64476/crti.o main.o --as-needed /home/ryan/.cache/zig/o/908dbd08225e4f05289d64d63425d966/libc.a /home/ryan/.cache/zig/o/0778243584f6504ffef50f624aea0590/libcompiler_rt.a /home/ryan/.cache/zig/o/6563952968b463fb667b5a0882efdf52/crtn.o --allow-shlib-undefined
$ valgrind ./main
==3801512== Invalid read of size 4
==3801512== at 0x20A081: main.main (main.zig:2)
==3801512== by 0x20A5FF: callMain (start.zig:618)
==3801512== by 0x20A5FF: initEventLoopAndCallMain (start.zig:562)
==3801512== by 0x20A5FF: callMainWithArgs (start.zig:512)
==3801512== by 0x20A5FF: main (start.zig:527)
==3801512== Address 0x989680 is not stack'd, malloc'd or (recently) free'd
Expected Behavior
Debug symbols to work with Valgrind when not linking libc.