Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -6897,6 +6897,84 @@ #define INIT_TTYENT #endif +#if SANITIZER_INTERCEPT_PROTOENT +INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, getprotoent); + struct __sanitizer_protoent *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; + COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name); + struct __sanitizer_protoent *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; + COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto); + struct __sanitizer_protoent *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_PROTOENT \ + COMMON_INTERCEPT_FUNCTION(getprotoent); \ + COMMON_INTERCEPT_FUNCTION(getprotobyname); \ + COMMON_INTERCEPT_FUNCTION(getprotobynumber); \ + COMMON_INTERCEPT_FUNCTION(setprotoent); \ + COMMON_INTERCEPT_FUNCTION(endprotoent) +#else +#define INIT_PROTOENT +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7128,6 +7206,7 @@ INIT_FGETLN; INIT_STRMODE; INIT_TTYENT; + INIT_PROTOENT; #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_TTYENT SI_NETBSD +#define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: lib/sanitizer_common/sanitizer_platform_limits_netbsd.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_limits_netbsd.h +++ lib/sanitizer_common/sanitizer_platform_limits_netbsd.h @@ -113,6 +113,12 @@ void *_shm_internal; }; +struct __sanitizer_protoent { + char *p_name; + char **p_aliases; + int p_proto; +}; + extern unsigned struct_msqid_ds_sz; extern unsigned struct_mq_attr_sz; extern unsigned struct_timex_sz; Index: test/sanitizer_common/TestCases/NetBSD/protoent.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/protoent.cc @@ -0,0 +1,86 @@ +// 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 = getprotoent(); + + printf("%s ", STRING_OR_NULL(ptp->p_name)); + + for (char **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 = getprotobyname("icmp"); + + printf("%s ", STRING_OR_NULL(ptp->p_name)); + + for (char **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 = getprotobynumber(1); + + printf("%s ", STRING_OR_NULL(ptp->p_name)); + + for (char **cp = ptp->p_aliases; *cp != NULL; cp++) + printf("%s ", STRING_OR_NULL(*cp)); + + printf("%d\n", ptp->p_proto); + endprotoent(); +} + +void test4() { + setprotoent(1); + struct protoent *ptp = getprotobynumber(1); + + ptp = getprotobynumber(2); + + printf("%s ", STRING_OR_NULL(ptp->p_name)); + + for (char **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 = getprotobyname("ttp"); + + printf("%s ", STRING_OR_NULL(ptp->p_name)); + + for (char **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 HOPOPT 0 + // CHECK: icmp ICMP 1 + // CHECK: icmp ICMP 1 + // CHECK: igmp IGMP 2 + // CHECK: ttp TTP iptm IPTM 84 + + return 0; +}