Skip to content

msan: stat() result is seen as uninitialized (st_size) #54131

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

Closed
vstinner opened this issue Mar 1, 2022 · 3 comments
Closed

msan: stat() result is seen as uninitialized (st_size) #54131

vstinner opened this issue Mar 1, 2022 · 3 comments
Labels
compiler-rt:msan Memory sanitizer

Comments

@vstinner
Copy link

vstinner commented Mar 1, 2022

The following code fails with use-of-uninitialized-value:

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    struct stat st;
    printf("sizeof(struct stat) = %zd\n", sizeof(struct stat));
    int fd = open(__FILE__, O_RDONLY);
    if (fd < 0) {
        return 1;
    }
    if (fstat(fd, &st)) {
        return 1;
    }
    if (st.st_size > 1) {
        return 2;
    }
    close(fd);
    return 0;
}

Output:

$ clang x.c -o x -fsanitize=memory && ./x; echo $?
sizeof(struct stat) = 144
==134123==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x49d9e5 in main (/home/vstinner/python/main/x+0x49d9e5)
    #1 0x7f874ea5355f in __libc_start_call_main (/lib64/libc.so.6+0x2d55f)
    #2 0x7f874ea5360b in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2d60b)
    #3 0x41c344 in _start (/home/vstinner/python/main/x+0x41c344)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/home/vstinner/python/main/x+0x49d9e5) in main
Exiting
1

I tested on Fedora 35 with versions:

$ clang --version
clang version 13.0.0 (Fedora 13.0.0-3.fc35)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

$ rpm -q clang
clang-13.0.0-3.fc35.x86_64

Issue discovered in Python: https://bugs.python.org/issue46887

@EugeneZelenko EugeneZelenko added compiler-rt:msan Memory sanitizer and removed new issue labels Mar 1, 2022
@maflcko
Copy link
Contributor

maflcko commented Mar 29, 2022

This looks like a Fedora bug? I can't reproduce with the clang from https://apt.llvm.org/ or with Debian/Ubuntu system packages.

@serge-sans-paille
Copy link
Collaborator

Same on godbolt: https://godbolt.org/z/ovz3de65z
@vstinner you should probably report on Fedora's bugzilla then.

@vstinner
Copy link
Author

vstinner commented Sep 6, 2024

No activity for 2 years, I close the issue.

@vstinner vstinner closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:msan Memory sanitizer
Projects
None yet
Development

No branches or pull requests

4 participants