page_allocator_internals_posix.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/allocator/partition_allocator/page_allocator.h"
  5. #include "base/allocator/partition_allocator/partition_alloc_base/cpu.h"
  6. #include "base/allocator/partition_allocator/partition_alloc_notreached.h"
  7. #include <sys/mman.h>
  8. // PA_PROT_BTI requests a page that supports BTI landing pads.
  9. #define PA_PROT_BTI 0x10
  10. // PA_PROT_MTE requests a page that's suitable for memory tagging.
  11. #define PA_PROT_MTE 0x20
  12. namespace partition_alloc::internal {
  13. int GetAccessFlags(PageAccessibilityConfiguration accessibility) {
  14. switch (accessibility) {
  15. case PageAccessibilityConfiguration::kRead:
  16. return PROT_READ;
  17. case PageAccessibilityConfiguration::kReadWriteTagged:
  18. #if defined(ARCH_CPU_ARM64)
  19. return PROT_READ | PROT_WRITE |
  20. (base::CPU::GetInstanceNoAllocation().has_mte() ? PA_PROT_MTE : 0);
  21. #else
  22. [[fallthrough]];
  23. #endif
  24. case PageAccessibilityConfiguration::kReadWrite:
  25. return PROT_READ | PROT_WRITE;
  26. case PageAccessibilityConfiguration::kReadExecuteProtected:
  27. return PROT_READ | PROT_EXEC |
  28. (base::CPU::GetInstanceNoAllocation().has_bti() ? PA_PROT_BTI : 0);
  29. case PageAccessibilityConfiguration::kReadExecute:
  30. return PROT_READ | PROT_EXEC;
  31. case PageAccessibilityConfiguration::kReadWriteExecute:
  32. return PROT_READ | PROT_WRITE | PROT_EXEC;
  33. default:
  34. PA_NOTREACHED();
  35. [[fallthrough]];
  36. case PageAccessibilityConfiguration::kInaccessible:
  37. return PROT_NONE;
  38. }
  39. }
  40. } // namespace partition_alloc::internal