tsan_interceptors_mach_vm.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===-- tsan_interceptors_mach_vm.cpp -------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. // Interceptors for mach_vm_* user space memory routines on Darwin.
  12. //===----------------------------------------------------------------------===//
  13. #include "interception/interception.h"
  14. #include "tsan_interceptors.h"
  15. #include "tsan_platform.h"
  16. #include <mach/mach.h>
  17. namespace __tsan {
  18. static bool intersects_with_shadow(mach_vm_address_t address,
  19. mach_vm_size_t size, int flags) {
  20. // VM_FLAGS_FIXED is 0x0, so we have to test for VM_FLAGS_ANYWHERE.
  21. if (flags & VM_FLAGS_ANYWHERE) return false;
  22. return !IsAppMem(address) || !IsAppMem(address + size - 1);
  23. }
  24. TSAN_INTERCEPTOR(kern_return_t, mach_vm_allocate, vm_map_t target,
  25. mach_vm_address_t *address, mach_vm_size_t size, int flags) {
  26. SCOPED_TSAN_INTERCEPTOR(mach_vm_allocate, target, address, size, flags);
  27. if (target != mach_task_self())
  28. return REAL(mach_vm_allocate)(target, address, size, flags);
  29. if (address && intersects_with_shadow(*address, size, flags))
  30. return KERN_NO_SPACE;
  31. kern_return_t kr = REAL(mach_vm_allocate)(target, address, size, flags);
  32. if (kr == KERN_SUCCESS)
  33. MemoryRangeImitateWriteOrResetRange(thr, pc, *address, size);
  34. return kr;
  35. }
  36. TSAN_INTERCEPTOR(kern_return_t, mach_vm_deallocate, vm_map_t target,
  37. mach_vm_address_t address, mach_vm_size_t size) {
  38. SCOPED_TSAN_INTERCEPTOR(mach_vm_deallocate, target, address, size);
  39. if (target != mach_task_self())
  40. return REAL(mach_vm_deallocate)(target, address, size);
  41. kern_return_t kr = REAL(mach_vm_deallocate)(target, address, size);
  42. if (kr == KERN_SUCCESS && address)
  43. UnmapShadow(thr, address, size);
  44. return kr;
  45. }
  46. } // namespace __tsan