[sanitizer] Intercept tsearch.

llvm-svn: 206755
This commit is contained in:
Evgeniy Stepanov 2014-04-21 14:21:51 +00:00
parent 5e52f3a142
commit a7f9071f25
3 changed files with 54 additions and 1 deletions

View File

@ -3703,6 +3703,21 @@ INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
#define INIT_XDR
#endif // SANITIZER_INTERCEPT_XDR
#if SANITIZER_INTERCEPT_TSEARCH
INTERCEPTOR(void *, tsearch, void *key, void **rootp,
int (*compar)(const void *, const void *)) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
void *res = REAL(tsearch)(key, rootp, compar);
if (res && *(void **)res == key)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
return res;
}
#define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
#else
#define INIT_TSEARCH
#endif
#define SANITIZER_COMMON_INTERCEPTORS_INIT \
INIT_TEXTDOMAIN; \
INIT_STRCMP; \
@ -3832,5 +3847,6 @@ INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
INIT_AEABI_MEM; \
INIT___BZERO; \
INIT_FTIME; \
INIT_XDR;
INIT_XDR; \
INIT_TSEARCH;
/**/

View File

@ -195,5 +195,6 @@
#define SANITIZER_INTERCEPT___BZERO SI_MAC
#define SANITIZER_INTERCEPT_FTIME SI_NOT_WINDOWS
#define SANITIZER_INTERCEPT_XDR SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT_TSEARCH SI_LINUX_NOT_ANDROID || SI_MAC
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

View File

@ -0,0 +1,36 @@
// RUN: %clangxx_msan -O0 -g %s -o %t && %t
#include <assert.h>
#include <search.h>
#include <stdlib.h>
int compare(const void *pa, const void *pb) {
int a = *(const int *)pa;
int b = *(const int *)pb;
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
void myfreenode(void *p) {
delete (int *)p;
}
int main(void) {
void *root = NULL;
for (int i = 0; i < 5; ++i) {
int *p = new int(i);
void *q = tsearch(p, &root, compare);
if (q == NULL)
exit(1);
if (*(int **)q != p)
delete p;
}
tdestroy(root, myfreenode);
return 0;
}