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