Generalize sanitizer interceptors for memory and string routines

Similar to commit 3ead60236f ("Generalize bus_space(9) and atomic(9)
sanitizer interceptors"), use a more generic scheme for interposing
sanitizer implementations of routines like memcpy().

No functional change intended.

MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Mark Johnston 2021-03-24 19:43:05 -04:00
parent 5df6f7a840
commit ec8f1ea8d5
3 changed files with 43 additions and 35 deletions

View file

@ -350,12 +350,6 @@ kcsan_strlen(const char *str)
return (s - str);
}
#undef copyin
#undef copyin_nofault
#undef copyinstr
#undef copyout
#undef copyout_nofault
int
kcsan_copyin(const void *uaddr, void *kaddr, size_t len)
{

View file

@ -192,18 +192,20 @@ size_t strspn(const char *, const char *);
char *strstr(const char *, const char *);
int strvalid(const char *, size_t);
#ifdef KCSAN
char *kcsan_strcpy(char *, const char *);
int kcsan_strcmp(const char *, const char *);
size_t kcsan_strlen(const char *);
#define strcpy(d, s) kcsan_strcpy((d), (s))
#define strcmp(s1, s2) kcsan_strcmp((s1), (s2))
#define strlen(s) kcsan_strlen((s))
#ifdef SAN_PREFIX
char *SAN_INTERCEPTOR(strcpy)(char *, const char *);
int SAN_INTERCEPTOR(strcmp)(const char *, const char *);
size_t SAN_INTERCEPTOR(strlen)(const char *);
#ifndef SAN_RUNTIME
#define strcpy(d, s) SAN_INTERCEPTOR(strcpy)((d), (s))
#define strcmp(s1, s2) SAN_INTERCEPTOR(strcmp)((s1), (s2))
#define strlen(s) SAN_INTERCEPTOR(strlen)(s)
#endif /* !SAN_RUNTIME */
#else
#define strcpy(d, s) __builtin_strcpy((d), (s))
#define strcmp(s1, s2) __builtin_strcmp((s1), (s2))
#define strlen(s) __builtin_strlen((s))
#endif
#endif /* SAN_PREFIX */
static __inline char *
index(const char *p, int ch)

View file

@ -351,18 +351,28 @@ void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
int memcmp(const void *b1, const void *b2, size_t len);
#ifdef KCSAN
void *kcsan_memset(void *, int, size_t);
void *kcsan_memcpy(void *, const void *, size_t);
void *kcsan_memmove(void *, const void *, size_t);
int kcsan_memcmp(const void *, const void *, size_t);
#define bcopy(from, to, len) kcsan_memmove((to), (from), (len))
#define bzero(buf, len) kcsan_memset((buf), 0, (len))
#define bcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
#define memset(buf, c, len) kcsan_memset((buf), (c), (len))
#define memcpy(to, from, len) kcsan_memcpy((to), (from), (len))
#define memmove(dest, src, n) kcsan_memmove((dest), (src), (n))
#define memcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
#if defined(KASAN)
#define SAN_PREFIX kasan_
#elif defined(KCSAN)
#define SAN_PREFIX kcsan_
#endif
#ifdef SAN_PREFIX
#define SAN_INTERCEPTOR(func) __CONCAT(SAN_PREFIX, func)
void *SAN_INTERCEPTOR(memset)(void *, int, size_t);
void *SAN_INTERCEPTOR(memcpy)(void *, const void *, size_t);
void *SAN_INTERCEPTOR(memmove)(void *, const void *, size_t);
int SAN_INTERCEPTOR(memcmp)(const void *, const void *, size_t);
#ifndef SAN_RUNTIME
#define bcopy(from, to, len) SAN_INTERCEPTOR(memmove)((to), (from), (len))
#define bzero(buf, len) SAN_INTERCEPTOR(memset)((buf), 0, (len))
#define bcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
#define memset(buf, c, len) SAN_INTERCEPTOR(memset)((buf), (c), (len))
#define memcpy(to, from, len) SAN_INTERCEPTOR(memcpy)((to), (from), (len))
#define memmove(dest, src, n) SAN_INTERCEPTOR(memmove)((dest), (src), (n))
#define memcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
#endif /* !SAN_RUNTIME */
#else
#define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
#define bzero(buf, len) __builtin_memset((buf), 0, (len))
@ -371,7 +381,7 @@ int kcsan_memcmp(const void *, const void *, size_t);
#define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
#define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
#define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
#endif
#endif /* !SAN_PREFIX */
void *memset_early(void * _Nonnull buf, int c, size_t len);
#define bzero_early(buf, len) memset_early((buf), 0, (len))
@ -402,14 +412,16 @@ int copyout(const void * _Nonnull __restrict kaddr,
int copyout_nofault(const void * _Nonnull __restrict kaddr,
void * __restrict udaddr, size_t len);
#ifdef KCSAN
int kcsan_copyin(const void *, void *, size_t);
int kcsan_copyinstr(const void *, void *, size_t, size_t *);
int kcsan_copyout(const void *, void *, size_t);
#define copyin(u, k, l) kcsan_copyin((u), (k), (l))
#define copyinstr(u, k, l, lc) kcsan_copyinstr((u), (k), (l), (lc))
#define copyout(k, u, l) kcsan_copyout((k), (u), (l))
#endif
#ifdef SAN_PREFIX
int SAN_INTERCEPTOR(copyin)(const void *, void *, size_t);
int SAN_INTERCEPTOR(copyinstr)(const void *, void *, size_t, size_t *);
int SAN_INTERCEPTOR(copyout)(const void *, void *, size_t);
#ifndef SAN_RUNTIME
#define copyin(u, k, l) SAN_INTERCEPTOR(copyin)((u), (k), (l))
#define copyinstr(u, k, l, lc) SAN_INTERCEPTOR(copyinstr)((u), (k), (l), (lc))
#define copyout(k, u, l) SAN_INTERCEPTOR(copyout)((k), (u), (l))
#endif /* !SAN_RUNTIME */
#endif /* SAN_PREFIX */
int fubyte(volatile const void *base);
long fuword(volatile const void *base);