Skip to content

Commit ac7eec6

Browse files
CamsynAnthony Tran
authored andcommitted
[TSan] Add 2 test cases related to incomplete shadow cleanup in unmap (llvm#145472)
Once part of PR llvm#144648, follow the reviewer's advice and split into this separate PR. `unmap` works at page granularity, but supports an arbitrary non-zero size as an argument, which results in possible shadow undercleaning in the existing TSan implementation when `size % kShadowCell != 0`. This change introduces two test cases to verify the shadow cleaning effect in `unmap`. - java_heap_init2.cpp: Imitating java_heap_init cpp, verify the incomplete cleaning of meta - munmap_clear_shadow.c: verify the incomplete cleaning of shadow
1 parent e762d55 commit ac7eec6

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2+
// XFAIL: *
3+
4+
#include "java.h"
5+
#include <errno.h>
6+
#include <sys/mman.h>
7+
8+
int main() {
9+
// Test a non-regular kHeapSize
10+
// Previously __tsan_java_init failed because it encountered non-zero meta
11+
// shadow for the destination.
12+
size_t const kPageSize = sysconf(_SC_PAGESIZE);
13+
int const kSize = kPageSize - 1;
14+
jptr jheap2 = (jptr)mmap(0, kSize, PROT_READ | PROT_WRITE,
15+
MAP_ANON | MAP_PRIVATE, -1, 0);
16+
if (jheap2 == (jptr)MAP_FAILED)
17+
return printf("mmap failed with %d\n", errno);
18+
__atomic_store_n((int *)(jheap2 + kSize - 3), 1, __ATOMIC_RELEASE);
19+
// Due to the previous incorrect meta-end calculation, the following munmap
20+
// did not clear the tail meta shadow.
21+
munmap((void *)jheap2, kSize);
22+
int const kHeapSize2 = kSize + 1;
23+
jheap2 = (jptr)mmap((void *)jheap2, kHeapSize2, PROT_READ | PROT_WRITE,
24+
MAP_ANON | MAP_PRIVATE, -1, 0);
25+
if (jheap2 == (jptr)MAP_FAILED)
26+
return printf("second mmap failed with %d\n", errno);
27+
__tsan_java_init(jheap2, kHeapSize2);
28+
__tsan_java_move(jheap2, jheap2 + kHeapSize2 - 8, 8);
29+
fprintf(stderr, "DONE\n");
30+
return __tsan_java_fini();
31+
}
32+
33+
// CHECK-NOT: WARNING: ThreadSanitizer: data race
34+
// CHECK: DONE
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// RUN: %clang_tsan %s -o %t && %run %t | FileCheck %s
2+
// XFAIL: *
3+
4+
#include "test.h"
5+
#include <assert.h>
6+
#include <pthread.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <sys/mman.h>
10+
#include <unistd.h>
11+
12+
void __tsan_read1(void *addr);
13+
14+
struct thread_params {
15+
char *buf;
16+
unsigned int size;
17+
};
18+
19+
static void *thread_func(void *arg) {
20+
struct thread_params *p = (struct thread_params *)arg;
21+
// Access 1
22+
p->buf[0] = 0x42;
23+
p->buf[p->size - 1] = 0x42;
24+
barrier_wait(&barrier);
25+
return 0;
26+
}
27+
28+
int main() {
29+
const unsigned int kPageSize = sysconf(_SC_PAGESIZE);
30+
// The relevant shadow memory size should be exactly multiple of kPageSize,
31+
// even if Size = kPageSize - 1.
32+
const unsigned int Size = kPageSize - 1;
33+
34+
barrier_init(&barrier, 2);
35+
char *buf = (char *)mmap(NULL, Size, PROT_READ | PROT_WRITE,
36+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37+
assert(buf != MAP_FAILED);
38+
assert(((uintptr_t)buf % kPageSize) == 0);
39+
40+
pthread_t t;
41+
struct thread_params p = {buf, Size};
42+
pthread_create(&t, 0, thread_func, &p);
43+
44+
barrier_wait(&barrier);
45+
// Should clear all the shadow memory related to the mmaped memory.
46+
munmap(buf, Size);
47+
48+
// If the shadow memory is cleared completely, the following reads should not
49+
// cause races and behave the same. However, previously, __tsan_read1(&buf[0])
50+
// would not report a race, while __tsan_read1(&buf[Size - 1]) did.
51+
// CHECK-NOT: WARNING: ThreadSanitizer: data race
52+
__tsan_read1(&buf[0]); // Access 2
53+
__tsan_read1(&buf[Size - 1]); // Access 2
54+
pthread_join(t, 0);
55+
56+
puts("DONE");
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)