Skip to content

[msan] Runtime false positives with iostream, msan, and -O0 #134515

Open
@steveWang

Description

@steveWang

Running clang version 19.1.7 obtained from Arch Linux repositories, with libc++ 19.1.7-1 and libstdc++ 14.2.1, although this also reproduces on Compiler Explorer with newer versions of clang.

I was exploring the conditions in which MSAN / ASAN / hardened builds detect buffer overruns (initially exploring with std::array which stores its data inline), and ran into the following issues with printing argc:

// $ clang++ -fsanitize=memory
#include <iostream>
#include <vector>

int main(int argc, char** argv) {
  std::vector<int> vec = {1, 2, 3, 4};
  std::cout << argc << std::endl;
  return 0;
}

https://godbolt.org/z/ovxsG8e9d

(I was initially modifying vec[argc], but removed that when constructing a reproducer to make sure that wasn't a confounding factor.)

In short: with libstdc++, this fails when trying to format argc, while with libc++, this fails when printing any string.

In the latter case, I've gotten this down to the following minimum reproducer on Compiler Explorer, but it doesn't reproduce locally:

// $ clang++ -stdlib=libc++ -fsanitize=memory 
#include <iostream>

int main(int argc, char** argv) {
  std::cout << "hello world\n";
  return 0;
}

https://godbolt.org/z/3Ea9coP3E

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions