Skip to content

Commit 1260a15

Browse files
committed
[tsan] Fix the open and open64 interceptors to have correct declarations (variadic functions)
Not matching the (real) variadic declaration makes the interceptor take garbage inputs on Darwin/AArch64. Differential Revision: https://reviews.llvm.org/D84570
1 parent 0a00a7d commit 1260a15

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "tsan_mman.h"
3232
#include "tsan_fd.h"
3333

34+
#include <stdarg.h>
35+
3436
using namespace __tsan;
3537

3638
#if SANITIZER_FREEBSD || SANITIZER_MAC
@@ -135,6 +137,7 @@ const int PTHREAD_BARRIER_SERIAL_THREAD = -1;
135137
#endif
136138
const int MAP_FIXED = 0x10;
137139
typedef long long_t;
140+
typedef __sanitizer::u16 mode_t;
138141

139142
// From /usr/include/unistd.h
140143
# define F_ULOCK 0 /* Unlock a previously locked region. */
@@ -1508,20 +1511,28 @@ TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
15081511
#define TSAN_MAYBE_INTERCEPT_FSTAT64
15091512
#endif
15101513

1511-
TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) {
1512-
SCOPED_TSAN_INTERCEPTOR(open, name, flags, mode);
1514+
TSAN_INTERCEPTOR(int, open, const char *name, int oflag, ...) {
1515+
va_list ap;
1516+
va_start(ap, oflag);
1517+
mode_t mode = va_arg(ap, int);
1518+
va_end(ap);
1519+
SCOPED_TSAN_INTERCEPTOR(open, name, oflag, mode);
15131520
READ_STRING(thr, pc, name, 0);
1514-
int fd = REAL(open)(name, flags, mode);
1521+
int fd = REAL(open)(name, oflag, mode);
15151522
if (fd >= 0)
15161523
FdFileCreate(thr, pc, fd);
15171524
return fd;
15181525
}
15191526

15201527
#if SANITIZER_LINUX
1521-
TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) {
1522-
SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode);
1528+
TSAN_INTERCEPTOR(int, open64, const char *name, int oflag, ...) {
1529+
va_list ap;
1530+
va_start(ap, oflag);
1531+
mode_t mode = va_arg(ap, int);
1532+
va_end(ap);
1533+
SCOPED_TSAN_INTERCEPTOR(open64, name, oflag, mode);
15231534
READ_STRING(thr, pc, name, 0);
1524-
int fd = REAL(open64)(name, flags, mode);
1535+
int fd = REAL(open64)(name, oflag, mode);
15251536
if (fd >= 0)
15261537
FdFileCreate(thr, pc, fd);
15271538
return fd;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t %t.tmp 2>&1 | FileCheck %s
2+
#include <stdio.h>
3+
#include <assert.h>
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
#include <sys/stat.h>
7+
8+
int main(int argc, char *argv[]) {
9+
fprintf(stderr, "Hello world.\n");
10+
assert(argv[1]);
11+
unlink(argv[1]);
12+
int fd = open(argv[1], O_RDWR | O_CREAT, 0600);
13+
assert(fd != -1);
14+
struct stat info;
15+
int result = fstat(fd, &info);
16+
fprintf(stderr, "permissions = 0%o\n", info.st_mode & ~S_IFMT);
17+
assert(result == 0);
18+
close(fd);
19+
fprintf(stderr, "Done.\n");
20+
}
21+
22+
// CHECK: Hello world.
23+
// CHECK: permissions = 0600
24+
// CHECK: Done.

0 commit comments

Comments
 (0)