|
50 | 50 | #include <mach/mach.h>
|
51 | 51 | #include <sys/sysctl.h>
|
52 | 52 | #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
|
| 53 | +#include <sys/types.h> |
| 54 | +#include <sys/sysctl.h> |
| 55 | +#include <sys/user.h> |
53 | 56 | #include <pthread_np.h>
|
54 | 57 | #elif KMP_OS_NETBSD
|
55 | 58 | #include <sys/types.h>
|
@@ -1979,7 +1982,7 @@ int __kmp_is_address_mapped(void *addr) {
|
1979 | 1982 | int found = 0;
|
1980 | 1983 | int rc;
|
1981 | 1984 |
|
1982 |
| -#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD |
| 1985 | +#if KMP_OS_LINUX || KMP_OS_HURD |
1983 | 1986 |
|
1984 | 1987 | /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address
|
1985 | 1988 | ranges mapped into the address space. */
|
@@ -2017,6 +2020,44 @@ int __kmp_is_address_mapped(void *addr) {
|
2017 | 2020 | // Free resources.
|
2018 | 2021 | fclose(file);
|
2019 | 2022 | KMP_INTERNAL_FREE(name);
|
| 2023 | +#elif KMP_OS_FREEBSD |
| 2024 | + char *buf; |
| 2025 | + size_t lstsz; |
| 2026 | + int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()}; |
| 2027 | + rc = sysctl(mib, 4, NULL, &lstsz, NULL, 0); |
| 2028 | + if (rc < 0) |
| 2029 | + return 0; |
| 2030 | + // We pass from number of vm entry's semantic |
| 2031 | + // to size of whole entry map list. |
| 2032 | + lstsz = lstsz * 4 / 3; |
| 2033 | + buf = reinterpret_cast<char *>(kmpc_malloc(lstsz)); |
| 2034 | + rc = sysctl(mib, 4, buf, &lstsz, NULL, 0); |
| 2035 | + if (rc < 0) { |
| 2036 | + kmpc_free(buf); |
| 2037 | + return 0; |
| 2038 | + } |
| 2039 | + |
| 2040 | + char *lw = buf; |
| 2041 | + char *up = buf + lstsz; |
| 2042 | + |
| 2043 | + while (lw < up) { |
| 2044 | + struct kinfo_vmentry *cur = reinterpret_cast<struct kinfo_vmentry *>(lw); |
| 2045 | + size_t cursz = cur->kve_structsize; |
| 2046 | + if (cursz == 0) |
| 2047 | + break; |
| 2048 | + void *start = reinterpret_cast<void *>(cur->kve_start); |
| 2049 | + void *end = reinterpret_cast<void *>(cur->kve_end); |
| 2050 | + // Readable/Writable addresses within current map entry |
| 2051 | + if ((addr >= start) && (addr < end)) { |
| 2052 | + if ((cur->kve_protection & KVME_PROT_READ) != 0 && |
| 2053 | + (cur->kve_protection & KVME_PROT_WRITE) != 0) { |
| 2054 | + found = 1; |
| 2055 | + break; |
| 2056 | + } |
| 2057 | + } |
| 2058 | + lw += cursz; |
| 2059 | + } |
| 2060 | + kmpc_free(buf); |
2020 | 2061 |
|
2021 | 2062 | #elif KMP_OS_DARWIN
|
2022 | 2063 |
|
|
0 commit comments