sanitizer_posix.cc revision 230c3be6cdd094a187f48e27ba0961dbeee70344
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) {
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    RawWrite("Failed to map!\n");
38    Die();
39  }
40  return res;
41}
42
43void UnmapOrDie(void *addr, uptr size) {
44  if (!addr || !size) return;
45  int res = internal_munmap(addr, size);
46  if (res != 0) {
47    RawWrite("Failed to unmap!\n");
48    Die();
49  }
50}
51
52int internal_sscanf(const char *str, const char *format, ...) {
53  va_list args;
54  va_start(args, format);
55  int res = vsscanf(str, format, args);
56  va_end(args);
57  return res;
58}
59
60}  // namespace __sanitizer
61
62#endif  // __linux__ || __APPLE_
63