xref: /llvm-project/compiler-rt/test/msan/Linux/file.cpp (revision 3b947cc8ce08f3f97436f375837ea35e646d9688)
1 // RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t
2 // RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins -O0 %s -o %t && %run %t
3 
4 #include <assert.h>
5 #include <stdio.h>
6 
7 #include <sanitizer/msan_interface.h>
8 
main(int argc,char * argv[])9 int main(int argc, char *argv[]) {
10   FILE *f = fopen(argv[0], "r");
11   assert(f);
12   char buf[50];
13   fread(buf, 1, 1, f);
14   fflush(f);
15 
16   assert(f->_IO_read_end > f->_IO_read_base);
17   __msan_check_mem_is_initialized(f->_IO_read_end, f->_IO_read_end - f->_IO_read_base);
18 
19   char tmp_file[1000];
20   sprintf(tmp_file, "%s.write.tmp", argv[0]);
21 
22   f = fopen(tmp_file, "w+");
23   assert(f);
24   fwrite(buf, 1, 1, f);
25   fflush(f);
26 
27   assert(f->_IO_write_end > f->_IO_write_base);
28   __msan_check_mem_is_initialized(f->_IO_write_end, f->_IO_write_end - f->_IO_write_base);
29 }
30