sanitizer_posix.cc revision a25b3463477d2a825df4f656001fc07c594b35ac
11f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//===-- sanitizer_posix.cc ------------------------------------------------===//
21f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//
31f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//                     The LLVM Compiler Infrastructure
41f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//
51f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// This file is distributed under the University of Illinois Open Source
61f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// License. See LICENSE.TXT for details.
71f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//
81f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//===----------------------------------------------------------------------===//
91f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//
101f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// This file is shared between AddressSanitizer and ThreadSanitizer
111f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// run-time libraries and implements POSIX-specific functions from
121f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov// sanitizer_libc.h.
131f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov//===----------------------------------------------------------------------===//
141f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov#if defined(__linux__) || defined(__APPLE__)
151f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
16230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#include "sanitizer_common.h"
171f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov#include "sanitizer_libc.h"
181f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
191f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov#include <stdarg.h>
201f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov#include <stdio.h>
21230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#include <sys/mman.h>
22230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#include <sys/types.h>
23230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#include <unistd.h>
241f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
251f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovnamespace __sanitizer {
261f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
27230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonovint GetPid() {
28230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  return getpid();
29230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov}
30230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov
31a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonovvoid *MmapOrDie(uptr size, const char *mem_type) {
32230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  size = RoundUpTo(size, kPageSize);
33230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  void *res = internal_mmap(0, size,
34230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov                            PROT_READ | PROT_WRITE,
35230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov                            MAP_PRIVATE | MAP_ANON, -1, 0);
36230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  if (res == (void*)-1) {
37a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
38a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov           size, size, mem_type);
39a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov    CHECK("unable to mmap" && 0);
40230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  }
41230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  return res;
42230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov}
43230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov
44230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonovvoid UnmapOrDie(void *addr, uptr size) {
45230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  if (!addr || !size) return;
46230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  int res = internal_munmap(addr, size);
47230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  if (res != 0) {
48a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov    Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
49a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov           size, size, addr);
50a25b3463477d2a825df4f656001fc07c594b35acAlexey Samsonov    CHECK("unable to unmap" && 0);
51230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov  }
52230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov}
53230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov
541f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovint internal_sscanf(const char *str, const char *format, ...) {
551f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  va_list args;
561f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  va_start(args, format);
571f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  int res = vsscanf(str, format, args);
581f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  va_end(args);
591f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov  return res;
601f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov}
611f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
621f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov}  // namespace __sanitizer
631f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov
641f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov#endif  // __linux__ || __APPLE_
65