]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_openbsd.cc
Copy googletest 1.8.1 from ^/vendor/google/googletest/1.8.1 to .../contrib/googletest
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_openbsd.cc
1 //===-- sanitizer_openbsd.cc ----------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between various sanitizers' runtime libraries and
9 // implements Solaris-specific functions.
10 //===----------------------------------------------------------------------===//
11
12 #include "sanitizer_platform.h"
13 #if SANITIZER_OPENBSD
14
15 #include <stdio.h>
16
17 #include "sanitizer_common.h"
18 #include "sanitizer_flags.h"
19 #include "sanitizer_internal_defs.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_placement_new.h"
22 #include "sanitizer_platform_limits_posix.h"
23 #include "sanitizer_procmaps.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <pthread.h>
29 #include <sched.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/mman.h>
34 #include <sys/shm.h>
35 #include <sys/sysctl.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38
39 extern char **environ;
40
41 namespace __sanitizer {
42
43 uptr internal_mmap(void *addr, size_t length, int prot, int flags, int fd,
44                    u64 offset) {
45   return (uptr)mmap(addr, length, prot, flags, fd, offset);
46 }
47
48 uptr internal_munmap(void *addr, uptr length) { return munmap(addr, length); }
49
50 int internal_mprotect(void *addr, uptr length, int prot) {
51   return mprotect(addr, length, prot);
52 }
53
54 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
55   // On OpenBSD we cannot get the full path
56   struct kinfo_proc kp;
57   size_t kl;
58   const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
59   if (sysctl(Mib, ARRAY_SIZE(Mib), &kp, &kl, NULL, 0) != -1)
60     return internal_snprintf(buf,
61                              (KI_MAXCOMLEN < buf_len ? KI_MAXCOMLEN : buf_len),
62                              "%s", kp.p_comm);
63   return (uptr)0;
64 }
65
66 static void GetArgsAndEnv(char ***argv, char ***envp) {
67   size_t nargv;
68   size_t nenv;
69   int argvmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
70   int envmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ENV};
71   if (sysctl(argvmib, 4, NULL, &nargv, NULL, 0) == -1) {
72     Printf("sysctl KERN_PROC_NARGV failed\n");
73     Die();
74   }
75   if (sysctl(envmib, 4, NULL, &nenv, NULL, 0) == -1) {
76     Printf("sysctl KERN_PROC_NENV failed\n");
77     Die();
78   }
79   if (sysctl(argvmib, 4, &argv, &nargv, NULL, 0) == -1) {
80     Printf("sysctl KERN_PROC_ARGV failed\n");
81     Die();
82   }
83   if (sysctl(envmib, 4, &envp, &nenv, NULL, 0) == -1) {
84     Printf("sysctl KERN_PROC_ENV failed\n");
85     Die();
86   }
87 }
88
89 char **GetArgv() {
90   char **argv, **envp;
91   GetArgsAndEnv(&argv, &envp);
92   return argv;
93 }
94
95 void ReExec() {
96   UNIMPLEMENTED();
97 }
98
99 }  // namespace __sanitizer
100
101 #endif  // SANITIZER_OPENBSD