std.stdio.writeln - MemorySanitizer: use-of-uninitialized-value
I was trying memory sanitizer, which I faced a strange error. It seems that std.stdio.writeln has an MemorySanitizer: use-of-uninitialized-value error.
import std.stdio: writeln;
void main() {
writeln("Hello");
}
ldc2 ./test.d --fsanitize=memory
./test 2>&1 | ddemangle
gives:
==11183==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x491920 in @safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(scope immutable(char)[]) (/mnt/c/amin/test+0x491920)
#1 0x491586 in @safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[]) (/mnt/c/amin/test+0x491586)
#2 0x491415 in _Dmain (/mnt/c/amin/test+0x491415)
#3 0x4c786b in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll() (/mnt/c/amin/test+0x4c786b)
#4 0x4c7767 in _d_run_main2 (/mnt/c/amin/test+0x4c7767)
#5 0x4c75bd in _d_run_main (/mnt/c/amin/test+0x4c75bd)
#6 0x491825 in main (/mnt/c/amin/test+0x491825)
#7 0x7fad3d36a0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
#8 0x41f2bd in _start (/mnt/c/amin/test+0x41f2bd)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/mnt/c/amin/test+0x491920) in @safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(scope immutable(char)[])
Exiting
Using printf doesn't have the same issue:
import core.stdc.stdio: printf;
void main() {
printf("%s\n", "Hello".ptr);
}
I think the problem here is that you are using an uninstrumented Phobos. All code must be instrumented with msan: https://clang.llvm.org/docs/MemorySanitizer.html#handling-external-code
I'm happy you are using/testing MemorySanitizer by the way! But it is very experimental with LDC, so you are going to hit bugs like this I'm quite sure. The first step is being able to build runtime/phobos with -fsanitize=memory (ldc-build-runtime), and on my PC, it hits an LLVM internal error...
Thank you! The sanitizers have been very useful to catch bugs especially when the code interfaces with C/C++ (e.g. libclang)
I didn't know that I need to build everything using MSAN. I need to try that as well!