1*d21b3d34SFangrui Song // Test fopencookie interceptor.
2*d21b3d34SFangrui Song // RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t
3*d21b3d34SFangrui Song // RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins -O0 %s -o %t && %run %t
4*d21b3d34SFangrui Song
5*d21b3d34SFangrui Song #include <assert.h>
6*d21b3d34SFangrui Song #include <pthread.h>
7*d21b3d34SFangrui Song #include <stdint.h>
8*d21b3d34SFangrui Song #include <stdio.h>
9*d21b3d34SFangrui Song #include <stdlib.h>
10*d21b3d34SFangrui Song #include <string.h>
11*d21b3d34SFangrui Song
12*d21b3d34SFangrui Song #include <sanitizer/msan_interface.h>
13*d21b3d34SFangrui Song
14*d21b3d34SFangrui Song constexpr uintptr_t kMagicCookie = 0x12345678;
15*d21b3d34SFangrui Song
cookie_read(void * cookie,char * buf,size_t size)16*d21b3d34SFangrui Song static ssize_t cookie_read(void *cookie, char *buf, size_t size) {
17*d21b3d34SFangrui Song assert((uintptr_t)cookie == kMagicCookie);
18*d21b3d34SFangrui Song memset(buf, 0, size);
19*d21b3d34SFangrui Song return 0;
20*d21b3d34SFangrui Song }
21*d21b3d34SFangrui Song
cookie_write(void * cookie,const char * buf,size_t size)22*d21b3d34SFangrui Song static ssize_t cookie_write(void *cookie, const char *buf, size_t size) {
23*d21b3d34SFangrui Song assert((uintptr_t)cookie == kMagicCookie);
24*d21b3d34SFangrui Song __msan_check_mem_is_initialized(buf, size);
25*d21b3d34SFangrui Song return 0;
26*d21b3d34SFangrui Song }
27*d21b3d34SFangrui Song
cookie_seek(void * cookie,off64_t * offset,int whence)28*d21b3d34SFangrui Song static int cookie_seek(void *cookie, off64_t *offset, int whence) {
29*d21b3d34SFangrui Song assert((uintptr_t)cookie == kMagicCookie);
30*d21b3d34SFangrui Song __msan_check_mem_is_initialized(offset, sizeof(*offset));
31*d21b3d34SFangrui Song return 0;
32*d21b3d34SFangrui Song }
33*d21b3d34SFangrui Song
cookie_close(void * cookie)34*d21b3d34SFangrui Song static int cookie_close(void *cookie) {
35*d21b3d34SFangrui Song assert((uintptr_t)cookie == kMagicCookie);
36*d21b3d34SFangrui Song return 0;
37*d21b3d34SFangrui Song }
38*d21b3d34SFangrui Song
PoisonStack()39*d21b3d34SFangrui Song void PoisonStack() { char a[8192]; }
40*d21b3d34SFangrui Song
TestPoisonStack()41*d21b3d34SFangrui Song void TestPoisonStack() {
42*d21b3d34SFangrui Song // Verify that PoisonStack has poisoned the stack - otherwise this test is not
43*d21b3d34SFangrui Song // testing anything.
44*d21b3d34SFangrui Song char a;
45*d21b3d34SFangrui Song assert(__msan_test_shadow(&a - 1000, 1) == 0);
46*d21b3d34SFangrui Song }
47*d21b3d34SFangrui Song
main()48*d21b3d34SFangrui Song int main() {
49*d21b3d34SFangrui Song void *cookie = (void *)kMagicCookie;
50*d21b3d34SFangrui Song FILE *f = fopencookie(cookie, "rw",
51*d21b3d34SFangrui Song {cookie_read, cookie_write, cookie_seek, cookie_close});
52*d21b3d34SFangrui Song PoisonStack();
53*d21b3d34SFangrui Song TestPoisonStack();
54*d21b3d34SFangrui Song fseek(f, 100, SEEK_SET);
55*d21b3d34SFangrui Song char buf[50];
56*d21b3d34SFangrui Song fread(buf, 50, 1, f);
57*d21b3d34SFangrui Song fwrite(buf, 50, 1, f);
58*d21b3d34SFangrui Song fclose(f);
59*d21b3d34SFangrui Song
60*d21b3d34SFangrui Song f = fopencookie(cookie, "rw", {nullptr, nullptr, nullptr, nullptr});
61*d21b3d34SFangrui Song fseek(f, 100, SEEK_SET);
62*d21b3d34SFangrui Song fread(buf, 50, 1, f);
63*d21b3d34SFangrui Song fwrite(buf, 50, 1, f);
64*d21b3d34SFangrui Song fclose(f);
65*d21b3d34SFangrui Song }
66