Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -6886,6 +6886,87 @@ #define INIT_GETTTYENT #endif +#if SANITIZER_INTERCEPT_GETPROTOENT +INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) { + void *ctx; + struct __sanitizer_protoent *p; + COMMON_INTERCEPTOR_ENTER(ctx, getprotoent); + p = REAL(getprotoent)(); + if (p) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); + + char **pp = p->p_aliases; + + while (*pp) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + pp++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + } + return p; +} + +INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) { + void *ctx; + struct __sanitizer_protoent *p; + COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name); + p = REAL(getprotobyname)(name); + if (p) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); + + char **pp = p->p_aliases; + + while (*pp) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + pp++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + } + return p; +} + +INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) { + void *ctx; + struct __sanitizer_protoent *p; + COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto); + p = REAL(getprotobynumber)(proto); + if (p) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); + + char **pp = p->p_aliases; + + while (*pp) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + pp++; + } + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pp, sizeof(char **)); + } + return p; +} +INTERCEPTOR(void, setprotoent, int stayopen) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, setprotoent, stayopen); + return REAL(setprotoent)(stayopen); +} + +INTERCEPTOR(void, endprotoent) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, endprotoent); + return REAL(endprotoent)(); +} +#define INIT_GETPROTOENT \ + COMMON_INTERCEPT_FUNCTION(getprotoent); \ + COMMON_INTERCEPT_FUNCTION(getprotobyname); \ + COMMON_INTERCEPT_FUNCTION(getprotobynumber); \ + COMMON_INTERCEPT_FUNCTION(setprotoent); \ + COMMON_INTERCEPT_FUNCTION(endprotoent); +#else +#define INIT_GETPROTOENT +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7117,6 +7198,7 @@ INIT_FGETLN; INIT_STRMODE; INIT_GETTTYENT; + INIT_GETPROTOENT; #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 @@ -462,5 +462,6 @@ #define SANITIZER_INTERCEPT_FGETLN SI_NETBSD #define SANITIZER_INTERCEPT_STRMODE SI_NETBSD #define SANITIZER_INTERCEPT_GETTTYENT SI_NETBSD +#define SANITIZER_INTERCEPT_GETPROTOENT SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: test/sanitizer_common/TestCases/NetBSD/getprotoent.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/getprotoent.cc @@ -0,0 +1,103 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include +#include +#include + +#define STRING_OR_NULL(x) ((x) ? (x) : "null") + +void test1() { + struct protoent *ptp; + char **cp; + + ptp = getprotoent(); + if (!ptp) + exit(1); + + for (cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +void test2() { + struct protoent *ptp; + char **cp; + + ptp = getprotobyname("icmp"); + if (!ptp) + exit(1); + + for (cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +void test3() { + struct protoent *ptp; + char **cp; + + ptp = getprotobynumber(1); + if (!ptp) + exit(1); + + for (cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +void test4() { + struct protoent *ptp; + char **cp; + + setprotoent(1); + ptp = getprotobynumber(1); + if (!ptp) + exit(1); + + ptp = getprotobynumber(2); + if (!ptp) + exit(1); + + for (cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +void test5() { + struct protoent *ptp; + char **cp; + + ptp = getprotobyname("ttp"); + if (!ptp) + exit(1); + + for (cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +int main(void) { + test1(); + test2(); + test3(); + test4(); + test5(); + + // CHECK: HOPOPT 0 + // CHECK: ICMP 1 + // CHECK: ICMP 1 + // CHECK: IGMP 2 + // CHECK: TTP iptm IPTM 84 + + return 0; +}