return res;
}
-INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
- unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
- int v) {
- void *ctx;
- COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
- csz, rnode, v);
- if (sname)
- COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
- if (namelenp)
- COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
- if (csz)
- COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
- // Skip rnode, it's rarely used and not trivial to sanitize
- // It's also used mostly internally
- int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
- if (!res) {
- if (namelenp) {
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
- if (name)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
- }
- if (csz) {
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
- if (cname)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
- }
- }
- return res;
-}
-
INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
SIZE_T *namelenp) {
void *ctx;
return res;
}
+#define INIT_SYSCTL \
+ COMMON_INTERCEPT_FUNCTION(sysctl); \
+ COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
+ COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
+#else
+#define INIT_SYSCTL
+#endif
+
+#if SANITIZER_INTERCEPT_ASYSCTL
INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
}
return res;
}
-#define INIT_SYSCTL \
- COMMON_INTERCEPT_FUNCTION(sysctl); \
- COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
- COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo); \
- COMMON_INTERCEPT_FUNCTION(sysctlnametomib); \
- COMMON_INTERCEPT_FUNCTION(asysctl); \
+#define INIT_ASYSCTL \
+ COMMON_INTERCEPT_FUNCTION(asysctl); \
COMMON_INTERCEPT_FUNCTION(asysctlbyname);
#else
-#define INIT_SYSCTL
+#define INIT_ASYSCTL
+#endif
+
+#if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
+INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
+ unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
+ int v) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
+ csz, rnode, v);
+ if (sname)
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
+ if (namelenp)
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
+ if (csz)
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
+ // Skip rnode, it's rarely used and not trivial to sanitize
+ // It's also used mostly internally
+ int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
+ if (!res) {
+ if (namelenp) {
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
+ if (name)
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
+ }
+ if (csz) {
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
+ if (cname)
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
+ }
+ }
+ return res;
+}
+#define INIT_SYSCTLGETMIBINFO \
+ COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
+#else
+#define INIT_SYSCTLGETMIBINFO
#endif
+
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
INIT_REGEX;
INIT_FTS;
INIT_SYSCTL;
+ INIT_ASYSCTL;
+ INIT_SYSCTLGETMIBINFO;
INIT___PRINTF_CHK;
}
return sysctl(name, namelen, oldp, (size_t *)oldlenp, (void *)newp,
(size_t)newlen);
#else
- return sysctl(name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
+ return internal_syscall(SYSCALL(__sysctl), name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
#endif
}
return EXEC_PAGESIZE;
#elif SANITIZER_USE_GETAUXVAL
return getauxval(AT_PAGESZ);
+#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
+// Use sysctl as sysconf can trigger interceptors internally.
+ int pz = 0;
+ uptr pzl = sizeof(pz);
+ int mib[2] = {CTL_HW, HW_PAGESIZE};
+ int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
+ CHECK_EQ(rv, 0);
+ return (uptr)pz;
#else
return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
#endif
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+//
+// UNSUPPORTED: linux darwin solaris
#include <sys/param.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
+#ifndef __arraycount
+#define __arraycount(a) (sizeof(a) / sizeof(a[0]))
+#endif
+
void test_sysctl() {
char buf[100];
size_t len = sizeof(buf);
void test_sysctlbyname() {
char buf[100];
size_t len = sizeof(buf);
- int mib[] = {CTL_KERN, KERN_OSTYPE};
int rv = sysctlbyname("kern.ostype", buf, &len, NULL, 0);
assert(!rv);
printf("sysctlbyname: '%s' size: '%zu'\n", buf, len);
}
-void test_sysctlgetmibinfo() {
- int mib[CTL_MAXNAME];
- unsigned int mib_len = __arraycount(mib);
- int rv = sysctlgetmibinfo("kern.ostype", &mib[0], &mib_len, NULL, NULL, NULL,
- SYSCTL_VERSION);
- assert(!rv);
-
- char buf[100];
- size_t len = sizeof(buf);
- rv = sysctl(mib, mib_len, buf, &len, NULL, 0);
- assert(!rv);
-
- printf("sysctlgetmibinfo: '%s' size: '%zu'\n", buf, len);
-}
-
void test_sysctlnametomib() {
int mib[CTL_MAXNAME];
size_t mib_len = __arraycount(mib);
printf("sysctlnametomib: '%s' size: '%zu'\n", buf, len);
}
+#if defined(__NetBSD__)
void test_asysctl() {
int mib[] = {CTL_KERN, KERN_OSTYPE};
size_t len;
free(buf);
}
+void test_sysctlgetmibinfo() {
+ int mib[CTL_MAXNAME];
+ unsigned int mib_len = __arraycount(mib);
+ int rv = sysctlgetmibinfo("kern.ostype", &mib[0], &mib_len, NULL, NULL, NULL,
+ SYSCTL_VERSION);
+ assert(!rv);
+
+ char buf[100];
+ size_t len = sizeof(buf);
+ rv = sysctl(mib, mib_len, buf, &len, NULL, 0);
+ assert(!rv);
+
+ printf("sysctlgetmibinfo: '%s' size: '%zu'\n", buf, len);
+}
+#endif
+
int main(void) {
printf("sysctl\n");
test_sysctl();
test_sysctlbyname();
- test_sysctlgetmibinfo();
test_sysctlnametomib();
+#if defined(__NetBSD__)
test_asysctl();
test_asysctlbyname();
+ test_sysctlgetmibinfo();
+#endif
// CHECK: sysctl
// CHECK: sysctl: '{{.*}}' size: '{{.*}}'
// CHECK: sysctlbyname: '{{.*}}' size: '{{.*}}'
- // CHECK: sysctlgetmibinfo: '{{.*}}' size: '{{.*}}'
// CHECK: sysctlnametomib: '{{.*}}' size: '{{.*}}'
+#if defined(__NetBSD__)
// CHECK: asysctl: '{{.*}}' size: '{{.*}}'
// CHECK: asysctlbyname: '{{.*}}' size: '{{.*}}'
+ // CHECK: sysctlgetmibinfo: '{{.*}}' size: '{{.*}}'
+#endif
return 0;
}