Implement a bunch of custom glibc wrappers & corresponding tests

llvm-svn: 195749
This commit is contained in:
Lorenzo Martignoni 2013-11-26 11:45:58 +00:00
parent cffb33c53f
commit 895454e1e4
4 changed files with 929 additions and 92 deletions

View File

@ -11,26 +11,35 @@
//
// This file defines the custom functions listed in done_abilist.txt.
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_internal_defs.h"
#include "sanitizer_common/sanitizer_linux.h"
#include "dfsan/dfsan.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <dlfcn.h>
#include <link.h>
#include <poll.h>
#include <pthread.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
using namespace __dfsan;
extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_stat(const char *path, struct stat *buf, dfsan_label path_label,
dfsan_label buf_label, dfsan_label *ret_label) {
@ -338,4 +347,344 @@ SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_dl_iterate_phdr(
return dl_iterate_phdr(dl_iterate_phdr_cb, &dipi);
}
SANITIZER_INTERFACE_ATTRIBUTE
char *__dfsw_ctime_r(const time_t *timep, char *buf, dfsan_label timep_label,
dfsan_label buf_label, dfsan_label *ret_label) {
char *ret = ctime_r(timep, buf);
if (ret) {
dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), buf,
strlen(buf) + 1);
*ret_label = buf_label;
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
char *__dfsw_fgets(char *s, int size, FILE *stream, dfsan_label s_label,
dfsan_label size_label, dfsan_label stream_label,
dfsan_label *ret_label) {
char *ret = fgets(s, size, stream);
if (ret) {
dfsan_set_label(0, ret, strlen(ret) + 1);
*ret_label = s_label;
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
char *__dfsw_getcwd(char *buf, size_t size, dfsan_label buf_label,
dfsan_label size_label, dfsan_label *ret_label) {
char *ret = getcwd(buf, size);
if (ret) {
dfsan_set_label(0, ret, strlen(ret) + 1);
*ret_label = buf_label;
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
char *__dfsw_get_current_dir_name(dfsan_label *ret_label) {
char *ret = get_current_dir_name();
if (ret) {
dfsan_set_label(0, ret, strlen(ret) + 1);
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_gethostname(char *name, size_t len, dfsan_label name_label,
dfsan_label len_label, dfsan_label *ret_label) {
int ret = gethostname(name, len);
if (ret == 0) {
dfsan_set_label(0, name, strlen(name) + 1);
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_getrlimit(int resource, struct rlimit *rlim,
dfsan_label resource_label, dfsan_label rlim_label,
dfsan_label *ret_label) {
int ret = getrlimit(resource, rlim);
if (ret == 0) {
dfsan_set_label(0, rlim, sizeof(struct rlimit));
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_getrusage(int who, struct rusage *usage, dfsan_label who_label,
dfsan_label usage_label, dfsan_label *ret_label) {
int ret = getrusage(who, usage);
if (ret == 0) {
dfsan_set_label(0, usage, sizeof(struct rusage));
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
char *__dfsw_strcpy(char *dest, const char *src, dfsan_label dst_label,
dfsan_label src_label, dfsan_label *ret_label) {
char *ret = strcpy(dest, src);
if (ret) {
internal_memcpy(shadow_for(dest), shadow_for(src),
sizeof(dfsan_label) * (strlen(src) + 1));
}
*ret_label = dst_label;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
long int __dfsw_strtol(const char *nptr, char **endptr, int base,
dfsan_label nptr_label, dfsan_label endptr_label,
dfsan_label base_label, dfsan_label *ret_label) {
char *tmp_endptr;
long int ret = strtol(nptr, &tmp_endptr, base);
if (endptr) {
*endptr = tmp_endptr;
}
if (tmp_endptr > nptr) {
// If *tmp_endptr is '\0' include its label as well.
*ret_label = dfsan_union(
base_label,
dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
double __dfsw_strtod(const char *nptr, char **endptr,
dfsan_label nptr_label, dfsan_label endptr_label,
dfsan_label *ret_label) {
char *tmp_endptr;
double ret = strtod(nptr, &tmp_endptr);
if (endptr) {
*endptr = tmp_endptr;
}
if (tmp_endptr > nptr) {
// If *tmp_endptr is '\0' include its label as well.
*ret_label = dfsan_read_label(
nptr,
tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
long long int __dfsw_strtoll(const char *nptr, char **endptr, int base,
dfsan_label nptr_label, dfsan_label endptr_label,
dfsan_label base_label, dfsan_label *ret_label) {
char *tmp_endptr;
long long int ret = strtoll(nptr, &tmp_endptr, base);
if (endptr) {
*endptr = tmp_endptr;
}
if (tmp_endptr > nptr) {
// If *tmp_endptr is '\0' include its label as well.
*ret_label = dfsan_union(
base_label,
dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
unsigned long int __dfsw_strtoul(const char *nptr, char **endptr, int base,
dfsan_label nptr_label, dfsan_label endptr_label,
dfsan_label base_label, dfsan_label *ret_label) {
char *tmp_endptr;
unsigned long int ret = strtoul(nptr, &tmp_endptr, base);
if (endptr) {
*endptr = tmp_endptr;
}
if (tmp_endptr > nptr) {
// If *tmp_endptr is '\0' include its label as well.
*ret_label = dfsan_union(
base_label,
dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
long long unsigned int __dfsw_strtoull(const char *nptr, char **endptr,
dfsan_label nptr_label,
int base, dfsan_label endptr_label,
dfsan_label base_label,
dfsan_label *ret_label) {
char *tmp_endptr;
long long unsigned int ret = strtoull(nptr, &tmp_endptr, base);
if (endptr) {
*endptr = tmp_endptr;
}
if (tmp_endptr > nptr) {
// If *tmp_endptr is '\0' include its label as well.
*ret_label = dfsan_union(
base_label,
dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
time_t __dfsw_time(time_t *t, dfsan_label t_label, dfsan_label *ret_label) {
time_t ret = time(t);
if (ret != (time_t) -1 && t) {
dfsan_set_label(0, t, sizeof(time_t));
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_inet_pton(int af, const char *src, void *dst, dfsan_label af_label,
dfsan_label src_label, dfsan_label dst_label,
dfsan_label *ret_label) {
int ret = inet_pton(af, src, dst);
if (ret == 1) {
dfsan_set_label(dfsan_read_label(src, strlen(src) + 1), dst,
af == AF_INET ? sizeof(struct in_addr) : sizeof(in6_addr));
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
struct tm *__dfsw_localtime_r(const time_t *timep, struct tm *result,
dfsan_label timep_label, dfsan_label result_label,
dfsan_label *ret_label) {
struct tm *ret = localtime_r(timep, result);
if (ret) {
dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), result,
sizeof(struct tm));
*ret_label = result_label;
} else {
*ret_label = 0;
}
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_getpwuid_r(id_t uid, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result,
dfsan_label uid_label, dfsan_label pwd_label,
dfsan_label buf_label, dfsan_label buflen_label,
dfsan_label result_label, dfsan_label *ret_label) {
// Store the data in pwd, the strings referenced from pwd in buf, and the
// address of pwd in *result. On failure, NULL is stored in *result.
int ret = getpwuid_r(uid, pwd, buf, buflen, result);
if (ret == 0) {
dfsan_set_label(0, pwd, sizeof(struct passwd));
dfsan_set_label(0, buf, strlen(buf) + 1);
}
*ret_label = 0;
dfsan_set_label(0, result, sizeof(struct passwd*));
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_poll(struct pollfd *fds, nfds_t nfds, int timeout,
dfsan_label dfs_label, dfsan_label nfds_label,
dfsan_label timeout_label, dfsan_label *ret_label) {
int ret = poll(fds, nfds, timeout);
if (ret >= 0) {
for (; nfds > 0; --nfds) {
dfsan_set_label(0, &fds[nfds - 1].revents, sizeof(fds[nfds - 1].revents));
}
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout,
dfsan_label nfds_label, dfsan_label readfds_label,
dfsan_label writefds_label, dfsan_label exceptfds_label,
dfsan_label timeout_label, dfsan_label *ret_label) {
int ret = select(nfds, readfds, writefds, exceptfds, timeout);
// Clear everything (also on error) since their content is either set or
// undefined.
if (readfds) {
dfsan_set_label(0, readfds, sizeof(fd_set));
}
if (writefds) {
dfsan_set_label(0, writefds, sizeof(fd_set));
}
if (exceptfds) {
dfsan_set_label(0, exceptfds, sizeof(fd_set));
}
dfsan_set_label(0, timeout, sizeof(struct timeval));
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask,
dfsan_label pid_label,
dfsan_label cpusetsize_label,
dfsan_label mask_label, dfsan_label *ret_label) {
int ret = sched_getaffinity(pid, cpusetsize, mask);
if (ret == 0) {
dfsan_set_label(0, mask, cpusetsize);
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_sigemptyset(sigset_t *set, dfsan_label set_label,
dfsan_label *ret_label) {
int ret = sigemptyset(set);
dfsan_set_label(0, set, sizeof(sigset_t));
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_sigaction(int signum, const struct sigaction *act,
struct sigaction *oldact, dfsan_label signum_label,
dfsan_label act_label, dfsan_label oldact_label,
dfsan_label *ret_label) {
int ret = sigaction(signum, act, oldact);
if (oldact) {
dfsan_set_label(0, oldact, sizeof(struct sigaction));
}
*ret_label = 0;
return ret;
}
SANITIZER_INTERFACE_ATTRIBUTE
int __dfsw_gettimeofday(struct timeval *tv, struct timezone *tz,
dfsan_label tv_label, dfsan_label tz_label,
dfsan_label *ret_label) {
int ret = gettimeofday(tv, tz);
if (tv) {
dfsan_set_label(0, tv, sizeof(struct timeval));
}
if (tz) {
dfsan_set_label(0, tz, sizeof(struct timezone));
}
*ret_label = 0;
return ret;
}
}

View File

@ -103,23 +103,41 @@ fun:strerror=discard
fun:syscall=discard
fun:uselocale=discard
fun:write=discard
fun:dlclose=discard
fun:__assert_fail=discard
# Functions that produce output does not depend on the input (need to zero the
# shadow manually).
fun:calloc=custom
fun:clock_gettime=custom
fun:dlopen=custom
fun:fgets=custom
fun:fstat=custom
fun:getcwd=custom
fun:get_current_dir_name=custom
fun:gethostname=custom
fun:getrlimit=custom
fun:getrusage=custom
fun:pread=custom
fun:read=custom
fun:stat=custom
fun:time=custom
# Functions that produce an output that depend on the input (propagate the
# shadow manually).
fun:ctime_r=custom
fun:inet_pton=custom
fun:localtime_r=custom
fun:memcpy=custom
fun:memset=custom
fun:strcpy=custom
fun:strdup=custom
fun:strncpy=custom
fun:strtod=custom
fun:strtol=custom
fun:strtoll=custom
fun:strtoul=custom
fun:strtoull=custom
# Functions that produce an output that is computed from the input, but is not
# necessarily data dependent.
@ -134,21 +152,19 @@ fun:strncmp=custom
# Functions that take a callback (wrap the callback manually).
fun:dl_iterate_phdr=custom
fun:getpwuid_r=custom
fun:poll=custom
fun:sched_getaffinity=custom
fun:select=custom
fun:sigemptyset=custom
fun:sigaction=custom
fun:gettimeofday=custom
# TODO: custom
fun:snprintf=discard
fun:vsnprintf=discard
fun:asprintf=discard
fun:qsort=discard
fun:strtoll=discard
fun:strtoull=discard
fun:sigemptyset=discard
fun:sigaction=discard
fun:gettimeofday=discard
fun:get_current_dir_name=discard
fun:getpwuid_r=discard
fun:poll=discard
fun:sched_getaffinity=discard
fun:select=discard
###############################################################################
# pthread

View File

@ -1,154 +1,620 @@
// RUN: %clang_dfsan -m64 %s -o %t && %t
// RUN: %clang_dfsan -mllvm -dfsan-args-abi -m64 %s -o %t && %t
// Tests custom implementations of various libc functions.
// Tests custom implementations of various glibc functions.
#define _GNU_SOURCE
#include <sanitizer/dfsan_interface.h>
#include <arpa/inet.h>
#include <assert.h>
#include <link.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <link.h>
#include <poll.h>
#include <pthread.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
void *ptcb(void *p) {
assert(p == (void *)1);
assert(dfsan_get_label((uintptr_t)p) == 0);
return (void *)2;
}
dfsan_label i_label = 0;
dfsan_label j_label = 0;
dfsan_label i_j_label = 0;
int dlcb(struct dl_phdr_info *info, size_t size, void *data) {
assert(data == (void *)3);
assert(dfsan_get_label((uintptr_t)info) == 0);
assert(dfsan_get_label(size) == 0);
assert(dfsan_get_label((uintptr_t)data) == 0);
return 0;
}
#define ASSERT_ZERO_LABEL(data) \
assert(0 == dfsan_get_label((long) (data)))
int main(void) {
#define ASSERT_READ_ZERO_LABEL(ptr, size) \
assert(0 == dfsan_read_label(ptr, size))
#define ASSERT_LABEL(data, label) \
assert(label == dfsan_get_label((long) (data)))
#define ASSERT_READ_LABEL(ptr, size, label) \
assert(label == dfsan_read_label(ptr, size))
void test_stat() {
int i = 1;
dfsan_label i_label = dfsan_create_label("i", 0);
dfsan_set_label(i_label, &i, sizeof(i));
int j = 2;
dfsan_label j_label = dfsan_create_label("j", 0);
dfsan_set_label(j_label, &j, sizeof(j));
struct stat s;
s.st_dev = i;
int rv = stat("/", &s);
assert(rv == 0);
assert(dfsan_get_label(s.st_dev) == 0);
assert(0 == stat("/", &s));
ASSERT_ZERO_LABEL(s.st_dev);
s.st_dev = i;
rv = stat("/nonexistent", &s);
assert(rv == -1);
assert(dfsan_get_label(s.st_dev) == i_label);
assert(-1 == stat("/nonexistent", &s));
ASSERT_LABEL(s.st_dev, i_label);
}
void test_fstat() {
int i = 1;
dfsan_set_label(i_label, &i, sizeof(i));
struct stat s;
int fd = open("/dev/zero", O_RDONLY);
s.st_dev = i;
rv = fstat(fd, &s);
assert(rv == 0);
assert(dfsan_get_label(s.st_dev) == 0);
int rv = fstat(fd, &s);
assert(0 == rv);
ASSERT_ZERO_LABEL(s.st_dev);
}
void test_memcmp() {
char str1[] = "str1", str2[] = "str2";
dfsan_set_label(i_label, &str1[3], 1);
dfsan_set_label(j_label, &str2[3], 1);
rv = memcmp(str1, str2, sizeof(str1));
int rv = memcmp(str1, str2, sizeof(str1));
assert(rv < 0);
assert(dfsan_get_label(rv) == dfsan_union(i_label, j_label));
ASSERT_LABEL(rv, i_j_label);
}
char strc[sizeof(str1)];
memcpy(strc, str1, sizeof(str1));
assert(dfsan_get_label(strc[0]) == 0);
assert(dfsan_get_label(strc[3]) == i_label);
void test_memcpy() {
char str1[] = "str1";
char str2[sizeof(str1)];
dfsan_set_label(i_label, &str1[3], 1);
memset(strc, j, sizeof(strc));
assert(dfsan_get_label(strc[0]) == j_label);
assert(dfsan_get_label(strc[1]) == j_label);
assert(dfsan_get_label(strc[2]) == j_label);
assert(dfsan_get_label(strc[3]) == j_label);
assert(dfsan_get_label(strc[4]) == j_label);
ASSERT_ZERO_LABEL(memcpy(str2, str1, sizeof(str1)));
assert(0 == memcmp(str2, str1, sizeof(str1)));
ASSERT_ZERO_LABEL(str2[0]);
ASSERT_LABEL(str2[3], i_label);
}
rv = strcmp(str1, str2);
void test_memset() {
char buf[8];
int j = 'a';
dfsan_set_label(j_label, &j, sizeof(j));
ASSERT_ZERO_LABEL(memset(&buf, j, sizeof(buf)));
for (int i = 0; i < 8; ++i) {
ASSERT_LABEL(buf[i], j_label);
assert(buf[i] == 'a');
}
}
void test_strcmp() {
char str1[] = "str1", str2[] = "str2";
dfsan_set_label(i_label, &str1[3], 1);
dfsan_set_label(j_label, &str2[3], 1);
int rv = strcmp(str1, str2);
assert(rv < 0);
assert(dfsan_get_label(rv) == dfsan_union(i_label, j_label));
ASSERT_LABEL(rv, i_j_label);
}
void test_strlen() {
char str1[] = "str1";
dfsan_set_label(i_label, &str1[3], 1);
int rv = strlen(str1);
assert(rv == 4);
ASSERT_LABEL(rv, i_label);
}
void test_strdup() {
char str1[] = "str1";
dfsan_set_label(i_label, &str1[3], 1);
char *strd = strdup(str1);
assert(dfsan_get_label(strd[0]) == 0);
assert(dfsan_get_label(strd[3]) == i_label);
ASSERT_ZERO_LABEL(strd[0]);
ASSERT_LABEL(strd[3], i_label);
free(strd);
}
rv = strncmp(str1, str2, sizeof(str1));
void test_strncpy() {
char str1[] = "str1";
char str2[sizeof(str1)];
dfsan_set_label(i_label, &str1[3], 1);
char *strd = strncpy(str2, str1, 5);
assert(strd == str2);
assert(strcmp(str1, str2) == 0);
ASSERT_ZERO_LABEL(strd);
ASSERT_ZERO_LABEL(strd[0]);
ASSERT_ZERO_LABEL(strd[1]);
ASSERT_ZERO_LABEL(strd[2]);
ASSERT_LABEL(strd[3], i_label);
strd = strncpy(str2, str1, 3);
assert(strd == str2);
assert(strncmp(str1, str2, 3) == 0);
ASSERT_ZERO_LABEL(strd);
ASSERT_ZERO_LABEL(strd[0]);
ASSERT_ZERO_LABEL(strd[1]);
ASSERT_ZERO_LABEL(strd[2]);
}
void test_strncmp() {
char str1[] = "str1", str2[] = "str2";
dfsan_set_label(i_label, &str1[3], 1);
dfsan_set_label(j_label, &str2[3], 1);
int rv = strncmp(str1, str2, sizeof(str1));
assert(rv < 0);
assert(dfsan_get_label(rv) == dfsan_union(i_label, j_label));
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
rv = strncmp(str1, str2, 3);
assert(rv == 0);
assert(dfsan_get_label(rv) == 0);
ASSERT_ZERO_LABEL(rv);
}
str1[0] = 'S';
void test_strcasecmp() {
char str1[] = "str1", str2[] = "str2", str3[] = "Str1";
dfsan_set_label(i_label, &str1[3], 1);
dfsan_set_label(j_label, &str2[3], 1);
dfsan_set_label(j_label, &str3[2], 1);
rv = strncasecmp(str1, str2, sizeof(str1));
int rv = strcasecmp(str1, str2);
assert(rv < 0);
assert(dfsan_get_label(rv) == dfsan_union(i_label, j_label));
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
rv = strcasecmp(str1, str3);
assert(rv == 0);
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
}
void test_strncasecmp() {
char str1[] = "Str1", str2[] = "str2";
dfsan_set_label(i_label, &str1[3], 1);
dfsan_set_label(j_label, &str2[3], 1);
int rv = strncasecmp(str1, str2, sizeof(str1));
assert(rv < 0);
ASSERT_LABEL(rv, dfsan_union(i_label, j_label));
rv = strncasecmp(str1, str2, 3);
assert(rv == 0);
assert(dfsan_get_label(rv) == 0);
ASSERT_ZERO_LABEL(rv);
}
void test_strchr() {
char str1[] = "str1";
dfsan_set_label(i_label, &str1[3], 1);
char *crv = strchr(str1, 'r');
assert(crv == &str1[2]);
assert(dfsan_get_label((uintptr_t)crv) == 0);
ASSERT_ZERO_LABEL(crv);
crv = strchr(str1, '1');
assert(crv == &str1[3]);
assert(dfsan_get_label((uintptr_t)crv) == i_label);
ASSERT_LABEL(crv, i_label);
crv = strchr(str1, 'x');
assert(crv == 0);
assert(dfsan_get_label((uintptr_t)crv) == i_label);
assert(!crv);
ASSERT_LABEL(crv, i_label);
}
void test_calloc() {
// With any luck this sequence of calls will cause calloc to return the same
// pointer both times. This is probably the best we can do to test this
// function.
crv = calloc(4096, 1);
assert(dfsan_get_label(crv[0]) == 0);
char *crv = calloc(4096, 1);
ASSERT_ZERO_LABEL(crv[0]);
dfsan_set_label(i_label, crv, 100);
free(crv);
crv = calloc(4096, 1);
assert(dfsan_get_label(crv[0]) == 0);
ASSERT_ZERO_LABEL(crv[0]);
free(crv);
}
void test_read() {
char buf[16];
buf[0] = i;
buf[15] = j;
rv = read(fd, buf, sizeof(buf));
assert(rv == sizeof(buf));
assert(dfsan_get_label(buf[0]) == 0);
assert(dfsan_get_label(buf[15]) == 0);
dfsan_set_label(i_label, buf, 1);
dfsan_set_label(j_label, buf + 15, 1);
ASSERT_LABEL(buf[0], i_label);
ASSERT_LABEL(buf[15], j_label);
int fd = open("/dev/zero", O_RDONLY);
int rv = read(fd, buf, sizeof(buf));
assert(rv == sizeof(buf));
ASSERT_ZERO_LABEL(rv);
ASSERT_ZERO_LABEL(buf[0]);
ASSERT_ZERO_LABEL(buf[15]);
close(fd);
fd = open("/bin/sh", O_RDONLY);
buf[0] = i;
buf[15] = j;
rv = pread(fd, buf, sizeof(buf), 0);
assert(rv == sizeof(buf));
assert(dfsan_get_label(buf[0]) == 0);
assert(dfsan_get_label(buf[15]) == 0);
}
void test_pread() {
char buf[16];
dfsan_set_label(i_label, buf, 1);
dfsan_set_label(j_label, buf + 15, 1);
ASSERT_LABEL(buf[0], i_label);
ASSERT_LABEL(buf[15], j_label);
int fd = open("/bin/sh", O_RDONLY);
int rv = pread(fd, buf, sizeof(buf), 0);
assert(rv == sizeof(buf));
ASSERT_ZERO_LABEL(rv);
ASSERT_ZERO_LABEL(buf[0]);
ASSERT_ZERO_LABEL(buf[15]);
close(fd);
}
void test_dlopen() {
void *map = dlopen(NULL, RTLD_NOW);
assert(map);
ASSERT_ZERO_LABEL(map);
dlclose(map);
map = dlopen("/nonexistent", RTLD_NOW);
assert(!map);
ASSERT_ZERO_LABEL(map);
}
void test_clock_gettime() {
struct timespec tp;
dfsan_set_label(j_label, ((char *)&tp) + 3, 1);
int t = clock_gettime(CLOCK_REALTIME, &tp);
assert(t == 0);
ASSERT_ZERO_LABEL(t);
ASSERT_ZERO_LABEL(((char *)&tp)[3]);
}
void test_ctime_r() {
char *buf = (char*) malloc(64);
time_t t = 0;
char *ret = ctime_r(&t, buf);
ASSERT_ZERO_LABEL(ret);
assert(buf == ret);
ASSERT_READ_ZERO_LABEL(buf, strlen(buf) + 1);
dfsan_set_label(i_label, &t, sizeof(t));
ret = ctime_r(&t, buf);
ASSERT_ZERO_LABEL(ret);
ASSERT_READ_LABEL(buf, strlen(buf) + 1, i_label);
t = 0;
dfsan_set_label(j_label, &buf, sizeof(&buf));
ret = ctime_r(&t, buf);
ASSERT_LABEL(ret, j_label);
ASSERT_READ_ZERO_LABEL(buf, strlen(buf) + 1);
}
void test_fgets() {
char *buf = (char*) malloc(128);
FILE *f = fopen("/etc/passwd", "r");
dfsan_set_label(j_label, buf, 1);
char *ret = fgets(buf, sizeof(buf), f);
assert(ret == buf);
ASSERT_ZERO_LABEL(ret);
ASSERT_READ_ZERO_LABEL(buf, 128);
dfsan_set_label(j_label, &buf, sizeof(&buf));
ret = fgets(buf, sizeof(buf), f);
ASSERT_LABEL(ret, j_label);
fclose(f);
}
void test_getcwd() {
char buf[1024];
char *ptr = buf;
dfsan_set_label(i_label, buf + 2, 2);
char* ret = getcwd(buf, sizeof(buf));
assert(ret == buf);
assert(ret[0] == '/');
ASSERT_READ_ZERO_LABEL(buf + 2, 2);
dfsan_set_label(i_label, &ptr, sizeof(ptr));
ret = getcwd(ptr, sizeof(buf));
ASSERT_LABEL(ret, i_label);
}
void test_get_current_dir_name() {
char* ret = get_current_dir_name();
assert(ret);
assert(ret[0] == '/');
ASSERT_READ_ZERO_LABEL(ret, strlen(ret) + 1);
}
void test_gethostname() {
char buf[1024];
dfsan_set_label(i_label, buf + 2, 2);
assert(gethostname(buf, sizeof(buf)) == 0);
ASSERT_READ_ZERO_LABEL(buf + 2, 2);
}
void test_getrlimit() {
struct rlimit rlim;
dfsan_set_label(i_label, &rlim, sizeof(rlim));
assert(getrlimit(RLIMIT_CPU, &rlim) == 0);
ASSERT_READ_ZERO_LABEL(&rlim, sizeof(rlim));
}
void test_getrusage() {
struct rusage usage;
dfsan_set_label(i_label, &usage, sizeof(usage));
assert(getrusage(RUSAGE_SELF, &usage) == 0);
ASSERT_READ_ZERO_LABEL(&usage, sizeof(usage));
}
void test_strcpy() {
char src[] = "hello world";
char dst[sizeof(src) + 2];
dfsan_set_label(0, src, sizeof(src));
dfsan_set_label(0, dst, sizeof(dst));
dfsan_set_label(i_label, src + 2, 1);
dfsan_set_label(j_label, src + 3, 1);
dfsan_set_label(j_label, dst + 4, 1);
dfsan_set_label(i_label, dst + 12, 1);
char *ret = strcpy(dst, src);
assert(ret == dst);
assert(strcmp(src, dst) == 0);
for (int i = 0; i < strlen(src) + 1; ++i) {
assert(dfsan_get_label(dst[i]) == dfsan_get_label(src[i]));
}
// Note: if strlen(src) + 1 were used instead to compute the first untouched
// byte of dest, the label would be I|J. This is because strlen() might
// return a non-zero label, and because by default pointer labels are not
// ignored on loads.
ASSERT_LABEL(dst[12], i_label);
}
void test_strtol() {
char buf[] = "1234578910";
char *endptr = NULL;
dfsan_set_label(i_label, buf + 1, 1);
dfsan_set_label(j_label, buf + 10, 1);
long int ret = strtol(buf, &endptr, 10);
assert(ret == 1234578910);
assert(endptr == buf + 10);
ASSERT_LABEL(ret, i_j_label);
}
void test_strtoll() {
char buf[] = "1234578910 ";
char *endptr = NULL;
dfsan_set_label(i_label, buf + 1, 1);
dfsan_set_label(j_label, buf + 2, 1);
long long int ret = strtoll(buf, &endptr, 10);
assert(ret == 1234578910);
assert(endptr == buf + 10);
ASSERT_LABEL(ret, i_j_label);
}
void test_strtoul() {
char buf[] = "0xffffffffffffaa";
char *endptr = NULL;
dfsan_set_label(i_label, buf + 1, 1);
dfsan_set_label(j_label, buf + 2, 1);
long unsigned int ret = strtol(buf, &endptr, 16);
assert(ret == 72057594037927850);
assert(endptr == buf + 16);
ASSERT_LABEL(ret, i_j_label);
}
void test_strtoull() {
char buf[] = "0xffffffffffffffaa";
char *endptr = NULL;
dfsan_set_label(i_label, buf + 1, 1);
dfsan_set_label(j_label, buf + 2, 1);
long long unsigned int ret = strtoull(buf, &endptr, 16);
assert(ret == 0xffffffffffffffaa);
assert(endptr == buf + 18);
ASSERT_LABEL(ret, i_j_label);
}
void test_strtod() {
char buf[] = "12345.76 foo";
char *endptr = NULL;
dfsan_set_label(i_label, buf + 1, 1);
dfsan_set_label(j_label, buf + 6, 1);
double ret = strtod(buf, &endptr);
assert(ret == 12345.76);
assert(endptr == buf + 8);
ASSERT_LABEL(ret, i_j_label);
}
void test_time() {
time_t t = 0;
dfsan_set_label(i_label, &t, 1);
time_t ret = time(&t);
assert(ret == t);
assert(ret > 0);
ASSERT_ZERO_LABEL(t);
}
void test_inet_pton() {
char addr4[] = "127.0.0.1";
dfsan_set_label(i_label, addr4 + 3, 1);
struct in_addr in4;
int ret4 = inet_pton(AF_INET, addr4, &in4);
assert(ret4 == 1);
ASSERT_READ_LABEL(&in4, sizeof(in4), i_label);
assert(in4.s_addr == 0x0100007f);
char addr6[] = "::1";
dfsan_set_label(j_label, addr6 + 3, 1);
struct in6_addr in6;
int ret6 = inet_pton(AF_INET6, addr6, &in6);
assert(ret6 == 1);
ASSERT_READ_LABEL(((char *) &in6) + sizeof(in6) - 1, 1, j_label);
}
void test_localtime_r() {
time_t t0 = 1384800998;
struct tm t1;
dfsan_set_label(i_label, &t0, sizeof(t0));
struct tm* ret = localtime_r(&t0, &t1);
assert(ret == &t1);
assert(t1.tm_min == 56);
ASSERT_LABEL(t1.tm_mon, i_label);
}
void test_getpwuid_r() {
struct passwd pwd;
char buf[1024];
struct passwd *result;
dfsan_set_label(i_label, &pwd, 4);
int ret = getpwuid_r(0, &pwd, buf, sizeof(buf), &result);
assert(ret == 0);
assert(strcmp(pwd.pw_name, "root") == 0);
assert(result == &pwd);
ASSERT_READ_ZERO_LABEL(&pwd, 4);
}
void test_poll() {
struct pollfd fd;
fd.fd = 0;
fd.events = POLLIN;
dfsan_set_label(i_label, &fd.revents, sizeof(fd.revents));
int ret = poll(&fd, 1, 1);
ASSERT_ZERO_LABEL(fd.revents);
assert(ret >= 0);
}
void test_select() {
struct timeval t;
fd_set fds;
t.tv_sec = 2;
FD_SET(0, &fds);
dfsan_set_label(i_label, &fds, sizeof(fds));
dfsan_set_label(j_label, &t, sizeof(t));
int ret = select(1, &fds, NULL, NULL, &t);
assert(ret >= 0);
ASSERT_ZERO_LABEL(t.tv_sec);
ASSERT_READ_ZERO_LABEL(&fds, sizeof(fds));
}
void test_sched_getaffinity() {
cpu_set_t mask;
dfsan_set_label(j_label, &mask, 1);
int ret = sched_getaffinity(0, sizeof(mask), &mask);
assert(ret == 0);
ASSERT_READ_ZERO_LABEL(&mask, sizeof(mask));
}
void test_sigemptyset() {
sigset_t set;
dfsan_set_label(j_label, &set, 1);
int ret = sigemptyset(&set);
assert(ret == 0);
ASSERT_READ_ZERO_LABEL(&set, sizeof(set));
}
void test_sigaction() {
struct sigaction oldact;
dfsan_set_label(j_label, &oldact, 1);
int ret = sigaction(SIGUSR1, NULL, &oldact);
assert(ret == 0);
ASSERT_READ_ZERO_LABEL(&oldact, sizeof(oldact));
}
void test_gettimeofday() {
struct timeval tv;
struct timezone tz;
dfsan_set_label(i_label, &tv, sizeof(tv));
dfsan_set_label(j_label, &tz, sizeof(tz));
int ret = gettimeofday(&tv, &tz);
assert(ret == 0);
ASSERT_READ_ZERO_LABEL(&tv, sizeof(tv));
ASSERT_READ_ZERO_LABEL(&tz, sizeof(tz));
}
void *pthread_create_test_cb(void *p) {
assert(p == (void *)1);
ASSERT_ZERO_LABEL(p);
return (void *)2;
}
void test_pthread_create() {
pthread_t pt;
pthread_create(&pt, 0, ptcb, (void *)1);
pthread_create(&pt, 0, pthread_create_test_cb, (void *)1);
void *cbrv;
pthread_join(pt, &cbrv);
assert(cbrv == (void *)2);
}
dl_iterate_phdr(dlcb, (void *)3);
int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
void *data) {
assert(data == (void *)3);
ASSERT_ZERO_LABEL(info);
ASSERT_ZERO_LABEL(size);
ASSERT_ZERO_LABEL(data);
return 0;
}
void test_dl_iterate_phdr() {
dl_iterate_phdr(dl_iterate_phdr_test_cb, (void *)3);
}
int main(void) {
i_label = dfsan_create_label("i", 0);
j_label = dfsan_create_label("j", 0);
i_j_label = dfsan_union(i_label, j_label);
test_calloc();
test_clock_gettime();
test_dlopen();
test_fstat();
test_memcmp();
test_memcpy();
test_memset();
test_pread();
test_read();
test_stat();
test_strcasecmp();
test_strchr();
test_strcmp();
test_strdup();
test_strlen();
test_strncasecmp();
test_strncmp();
test_strncpy();
test_ctime_r();
test_fgets();
test_getcwd();
test_get_current_dir_name();
test_gethostname();
test_getrlimit();
test_getrusage();
test_strcpy();
test_strtol();
test_strtoll();
test_strtod();
test_strtoul();
test_strtoull();
test_time();
test_inet_pton();
test_localtime_r();
test_getpwuid_r();
test_poll();
test_select();
test_sched_getaffinity();
test_sigemptyset();
test_sigaction();
test_gettimeofday();
test_pthread_create();
test_dl_iterate_phdr();
}

View File

@ -27,6 +27,7 @@ TSAN_LIT_TEST_LINT_FILTER=${TSAN_TEST_LINT_FILTER},-whitespace/line_length
MSAN_RTL_LINT_FILTER=${COMMON_LINT_FILTER}
LSAN_RTL_LINT_FILTER=${COMMON_LINT_FILTER}
LSAN_LIT_TEST_LINT_FILTER=${LSAN_RTL_LINT_FILTER},-whitespace/line_length
DFSAN_RTL_LINT_FILTER=${COMMON_LINT_FILTER},-runtime/int,-runtime/printf,-runtime/references
COMMON_RTL_INC_LINT_FILTER=${COMMON_LINT_FILTER},-runtime/int,-runtime/sizeof,-runtime/printf
SANITIZER_INCLUDES_LINT_FILTER=${COMMON_LINT_FILTER},-runtime/int
MKTEMP="mktemp -q /tmp/tmp.XXXXXXXXXX"
@ -90,6 +91,11 @@ run_lint ${LSAN_RTL_LINT_FILTER} ${LSAN_RTL}/*.{cc,h} \
${LSAN_RTL}/tests/*.{cc,h} &
run_lint ${LSAN_LIT_TEST_LINT_FILTER} ${LSAN_RTL}/lit_tests/*/*.cc &
# DFSan
DFSAN_RTL=${COMPILER_RT}/lib/dfsan
run_lint ${DFSAN_RTL_LINT_FILTER} ${DFSAN_RTL}/*.{cc,h} &
${DFSAN_RTL}/scripts/check_custom_wrappers.sh >> $ERROR_LOG
# Misc files
FILES=${COMMON_RTL}/*.inc
TMPFILES=""