diff options
author | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2015-06-19 01:28:41 +0000 |
---|---|---|
committer | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2015-06-19 01:28:41 +0000 |
commit | 7b3d9036ef3ca90e32387573830c47c8f61778e1 (patch) | |
tree | 662c1a1d0e8189ba468158ccd4effbb9795e05b2 /test/msan | |
parent | e85795297a6be2c07a768074bdc3e38206f0d6e4 (diff) |
[msan] Intercept fopencookie.
https://code.google.com/p/memory-sanitizer/issues/detail?id=86
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@240107 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/msan')
-rw-r--r-- | test/msan/Linux/fopencookie.cc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/msan/Linux/fopencookie.cc b/test/msan/Linux/fopencookie.cc new file mode 100644 index 000000000..e9616d502 --- /dev/null +++ b/test/msan/Linux/fopencookie.cc @@ -0,0 +1,59 @@ +// Test fopencookie interceptor. +// RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t +// RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins -O0 %s -o %t && %run %t + +#include <assert.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <sanitizer/msan_interface.h> + +constexpr uintptr_t kMagicCookie = 0x12345678; + +static ssize_t cookie_read(void *cookie, char *buf, size_t size) { + assert((uintptr_t)cookie == kMagicCookie); + memset(buf, 0, size); + return 0; +} + +static ssize_t cookie_write(void *cookie, const char *buf, size_t size) { + assert((uintptr_t)cookie == kMagicCookie); + __msan_check_mem_is_initialized(buf, size); + return 0; +} + +static int cookie_seek(void *cookie, off64_t *offset, int whence) { + assert((uintptr_t)cookie == kMagicCookie); + __msan_check_mem_is_initialized(offset, sizeof(*offset)); + return 0; +} + +static int cookie_close(void *cookie) { + assert((uintptr_t)cookie == kMagicCookie); + return 0; +} + +void PoisonStack() { char a[8192]; } + +void TestPoisonStack() { + // Verify that PoisonStack has poisoned the stack - otherwise this test is not + // testing anything. + char a; + assert(__msan_test_shadow(&a - 1000, 1) == 0); +} + +int main() { + void *cookie = (void *)kMagicCookie; + FILE *f = fopencookie(cookie, "rw", + {cookie_read, cookie_write, cookie_seek, cookie_close}); + PoisonStack(); + TestPoisonStack(); + fseek(f, 100, SEEK_SET); + char buf[50]; + fread(buf, 50, 1, f); + fwrite(buf, 50, 1, f); + fclose(f); +} |