sanitizer_posix.cc revision fa3daaf1d66314658e7c05bf63dc825d179f2faf
1//===-- sanitizer_posix.cc ------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries and implements POSIX-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14#if defined(__linux__) || defined(__APPLE__)
15
16#include "sanitizer_common.h"
17#include "sanitizer_libc.h"
18#include "sanitizer_procmaps.h"
19
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sys/mman.h>
25#include <sys/resource.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30namespace __sanitizer {
31
32// ------------- sanitizer_common.h
33
34int GetPid() {
35  return getpid();
36}
37
38uptr GetThreadSelf() {
39  return (uptr)pthread_self();
40}
41
42void *MmapOrDie(uptr size, const char *mem_type) {
43  size = RoundUpTo(size, kPageSize);
44  void *res = internal_mmap(0, size,
45                            PROT_READ | PROT_WRITE,
46                            MAP_PRIVATE | MAP_ANON, -1, 0);
47  if (res == (void*)-1) {
48    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
49           size, size, mem_type);
50    CHECK("unable to mmap" && 0);
51  }
52  return res;
53}
54
55void UnmapOrDie(void *addr, uptr size) {
56  if (!addr || !size) return;
57  int res = internal_munmap(addr, size);
58  if (res != 0) {
59    Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
60           size, size, addr);
61    CHECK("unable to unmap" && 0);
62  }
63}
64
65void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
66  return internal_mmap((void*)fixed_addr, size,
67                      PROT_READ | PROT_WRITE,
68                      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
69                      0, 0);
70}
71
72void *Mprotect(uptr fixed_addr, uptr size) {
73  return internal_mmap((void*)fixed_addr, size,
74                       PROT_NONE,
75                       MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
76                       0, 0);
77}
78
79void DumpProcessMap() {
80  ProcessMaps proc_maps;
81  uptr start, end;
82  const sptr kBufSize = 4095;
83  char filename[kBufSize];
84  Report("Process memory map follows:\n");
85  while (proc_maps.Next(&start, &end, /* file_offset */0,
86                        filename, kBufSize)) {
87    Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
88  }
89  Report("End of process memory map.\n");
90}
91
92void DisableCoreDumper() {
93  struct rlimit nocore;
94  nocore.rlim_cur = 0;
95  nocore.rlim_max = 0;
96  setrlimit(RLIMIT_CORE, &nocore);
97}
98
99void SleepForSeconds(int seconds) {
100  sleep(seconds);
101}
102
103void Exit(int exitcode) {
104  _exit(exitcode);
105}
106
107void Abort() {
108  abort();
109}
110
111int Atexit(void (*function)(void)) {
112  return atexit(function);
113}
114
115// -------------- sanitizer_libc.h
116
117int internal_sscanf(const char *str, const char *format, ...) {
118  va_list args;
119  va_start(args, format);
120  int res = vsscanf(str, format, args);
121  va_end(args);
122  return res;
123}
124
125}  // namespace __sanitizer
126
127#endif  // __linux__ || __APPLE_
128