Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -6967,6 +6967,88 @@ #define INIT_GETPROTOENT #endif +#if SANITIZER_INTERCEPT_GETNETENT +INTERCEPTOR(struct __sanitizer_netent *, getnetent) { + void *ctx; + struct __sanitizer_netent *n; + COMMON_INTERCEPTOR_ENTER(ctx, getnetent); + n = REAL(getnetent)(); + if (n) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); + + char **nn = n->n_aliases; + + while (*nn) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1); + nn++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + } + return n; +} + +INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) { + void *ctx; + struct __sanitizer_netent *n; + COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name); + n = REAL(getnetbyname)(name); + if (n) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); + + char **nn = n->n_aliases; + + while (*nn) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1); + nn++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + } + return n; +} + +INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) { + void *ctx; + struct __sanitizer_netent *n; + COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type); + n = REAL(getnetbyaddr)(net, type); + if (n) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); + + char **nn = n->n_aliases; + + while (*nn) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1); + nn++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, nn, sizeof(char **)); + } + return n; +} + +INTERCEPTOR(void, setnetent, int stayopen) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, setnetent, stayopen); + return REAL(setnetent)(stayopen); +} + +INTERCEPTOR(void, endnetent) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, endnetent); + return REAL(endnetent)(); +} +#define INIT_GETNETENT \ + COMMON_INTERCEPT_FUNCTION(getnetent); \ + COMMON_INTERCEPT_FUNCTION(getnetbyname); \ + COMMON_INTERCEPT_FUNCTION(getnetbyaddr); \ + COMMON_INTERCEPT_FUNCTION(setnetent); \ + COMMON_INTERCEPT_FUNCTION(endnetent); +#else +#define INIT_GETNETENT +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7199,6 +7281,7 @@ INIT_STRMODE; INIT_GETTTYENT; INIT_GETPROTOENT; + INIT_GETNETENT; #if SANITIZER_NETBSD COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock); Index: lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_interceptors.h +++ lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -463,5 +463,6 @@ #define SANITIZER_INTERCEPT_STRMODE SI_NETBSD #define SANITIZER_INTERCEPT_GETTTYENT SI_NETBSD #define SANITIZER_INTERCEPT_GETPROTOENT SI_NETBSD +#define SANITIZER_INTERCEPT_GETNETENT SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: test/sanitizer_common/TestCases/NetBSD/getnetent.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/getnetent.cc @@ -0,0 +1,101 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include +#include +#include +#include +#include + +#define STRING_OR_NULL(x) ((x) ? (x) : "null") + +void test1() { + struct netent *ntp; + char **cp; + + ntp = getnetent(); + if (!ntp) + exit(1); + + printf("%s ", ntp->n_name); + + for (cp = ntp->n_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d ", ntp->n_addrtype); + printf("%" PRIu32 "\n", ntp->n_net); + + endnetent(); +} + +void test2() { + struct netent *ntp; + char **cp; + + ntp = getnetbyname("loopback"); + if (!ntp) + exit(1); + + printf("%s ", ntp->n_name); + + for (cp = ntp->n_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d ", ntp->n_addrtype); + printf("%" PRIu32 "\n", ntp->n_net); + + endnetent(); +} + +void test3() { + struct netent *ntp; + char **cp; + + ntp = getnetbyaddr(127, 2); + if (!ntp) + exit(1); + + printf("%s ", ntp->n_name); + + for (cp = ntp->n_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d ", ntp->n_addrtype); + printf("%" PRIu32 "\n", ntp->n_net); + + endnetent(); +} + +void test4() { + struct netent *ntp; + char **cp; + + setnetent(1); + + ntp = getnetent(); + if (!ntp) + exit(1); + + printf("%s ", ntp->n_name); + + for (cp = ntp->n_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d ", ntp->n_addrtype); + printf("%" PRIu32 "\n", ntp->n_net); + + endnetent(); +} + +int main(void) { + test1(); + test2(); + test3(); + test4(); + + // CHECK: loopback 2 127 + // CHECK: loopback 2 127 + // CHECK: loopback 2 127 + // CHECK: loopback 2 127 + + return 0; +}