sanitizer_posix.cc revision f607fc1c67a613a59a1db3c80c5d1322e1978102
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
19#include <stdarg.h>
20#include <stdio.h>
21#include <sys/mman.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25namespace __sanitizer {
26
27int GetPid() {
28  return getpid();
29}
30
31void *MmapOrDie(uptr size, const char *mem_type) {
32  size = RoundUpTo(size, kPageSize);
33  void *res = internal_mmap(0, size,
34                            PROT_READ | PROT_WRITE,
35                            MAP_PRIVATE | MAP_ANON, -1, 0);
36  if (res == (void*)-1) {
37    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
38           size, size, mem_type);
39    CHECK("unable to mmap" && 0);
40  }
41  return res;
42}
43
44void UnmapOrDie(void *addr, uptr size) {
45  if (!addr || !size) return;
46  int res = internal_munmap(addr, size);
47  if (res != 0) {
48    Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
49           size, size, addr);
50    CHECK("unable to unmap" && 0);
51  }
52}
53
54void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
55  return internal_mmap((void*)fixed_addr, size,
56                      PROT_READ | PROT_WRITE,
57                      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
58                      0, 0);
59}
60
61void *Mprotect(uptr fixed_addr, uptr size) {
62  return internal_mmap((void*)fixed_addr, size,
63                       PROT_NONE,
64                       MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
65                       0, 0);
66}
67
68int internal_sscanf(const char *str, const char *format, ...) {
69  va_list args;
70  va_start(args, format);
71  int res = vsscanf(str, format, args);
72  va_end(args);
73  return res;
74}
75
76}  // namespace __sanitizer
77
78#endif  // __linux__ || __APPLE_
79