1 // Test fopencookie interceptor.
2 // RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t
3 // RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins -O0 %s -o %t && %run %t
12 #include <sanitizer/msan_interface.h>
14 constexpr uintptr_t kMagicCookie
= 0x12345678;
16 static ssize_t
cookie_read(void *cookie
, char *buf
, size_t size
) {
17 assert((uintptr_t)cookie
== kMagicCookie
);
22 static ssize_t
cookie_write(void *cookie
, const char *buf
, size_t size
) {
23 assert((uintptr_t)cookie
== kMagicCookie
);
24 __msan_check_mem_is_initialized(buf
, size
);
28 static int cookie_seek(void *cookie
, off64_t
*offset
, int whence
) {
29 assert((uintptr_t)cookie
== kMagicCookie
);
30 __msan_check_mem_is_initialized(offset
, sizeof(*offset
));
34 static int cookie_close(void *cookie
) {
35 assert((uintptr_t)cookie
== kMagicCookie
);
39 void PoisonStack() { char a
[8192]; }
41 void TestPoisonStack() {
42 // Verify that PoisonStack has poisoned the stack - otherwise this test is not
45 assert(__msan_test_shadow(&a
- 1000, 1) == 0);
49 void *cookie
= (void *)kMagicCookie
;
50 FILE *f
= fopencookie(cookie
, "rw",
51 {cookie_read
, cookie_write
, cookie_seek
, cookie_close
});
54 fseek(f
, 100, SEEK_SET
);
57 fwrite(buf
, 50, 1, f
);
60 f
= fopencookie(cookie
, "rw", {nullptr, nullptr, nullptr, nullptr});
61 fseek(f
, 100, SEEK_SET
);
63 fwrite(buf
, 50, 1, f
);