Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -6885,6 +6885,76 @@ #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)); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1); + + SIZE_T pp_size = 1; // One handles the trailing \0 + + for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, + pp_size * sizeof(char **)); + } + return p; +} + +INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name); + if (name) + COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1); + struct __sanitizer_protoent *p = REAL(getprotobyname)(name); + if (p) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1); + + SIZE_T pp_size = 1; // One handles the trailing \0 + + for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, + pp_size * 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)); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1); + + SIZE_T pp_size = 1; // One handles the trailing \0 + + for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1); + + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, + pp_size * sizeof(char **)); + } + return p; +} +#define INIT_PROTOENT \ + COMMON_INTERCEPT_FUNCTION(getprotoent); \ + COMMON_INTERCEPT_FUNCTION(getprotobyname); \ + COMMON_INTERCEPT_FUNCTION(getprotobynumber) +#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(); @@ -7116,6 +7186,7 @@ INIT_FGETLN; INIT_STRMODE; INIT_TTYENT; + INIT_PROTOENT; #if SANITIZER_NETBSD COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock); Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ compiler-rt/trunk/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: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h +++ compiler-rt/trunk/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: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc =================================================================== --- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc +++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc @@ -0,0 +1,89 @@ +// 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) { + printf("protoent\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + + // CHECK: protoent + // CHECK: hopopt HOPOPT 0 + // CHECK: icmp ICMP 1 + // CHECK: icmp ICMP 1 + // CHECK: igmp IGMP 2 + // CHECK: ttp TTP iptm IPTM 84 + + return 0; +}