v8_platform_page_allocator_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 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 "gin/v8_platform_page_allocator.h"
  5. #include "base/cpu.h"
  6. #include "build/build_config.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. // includes for Branch Target Instruction tests
  9. #if defined(ARCH_CPU_ARM64) && (OS_LINUX || OS_ANDROID)
  10. // BTI is only available for AArch64, relevant platform are Android and Linux
  11. #include "base/allocator/partition_allocator/arm_bti_test_functions.h"
  12. #include "base/allocator/partition_allocator/page_allocator_constants.h"
  13. #if BUILDFLAG(IS_POSIX)
  14. #include <signal.h>
  15. #include "testing/gtest/include/gtest/gtest-death-test.h"
  16. #endif
  17. #endif // defined(ARCH_CPU_ARM64) && (OS_LINUX || OS_ANDROID)
  18. namespace gin {
  19. TEST(V8PlatformPageAllocatorTest, VerifyGetPageConfig) {
  20. auto sut = gin::PageAllocator();
  21. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kNoAccess),
  22. partition_alloc::PageAccessibilityConfiguration::kInaccessible);
  23. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kRead),
  24. partition_alloc::PageAccessibilityConfiguration::kRead);
  25. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kReadWrite),
  26. partition_alloc::PageAccessibilityConfiguration::kReadWrite);
  27. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kReadWriteExecute),
  28. partition_alloc::PageAccessibilityConfiguration::kReadWriteExecute);
  29. #if defined(__ARM_FEATURE_BTI_DEFAULT)
  30. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kReadExecute),
  31. base::CPU::GetInstanceNoAllocation().has_bti()
  32. ? partition_alloc::PageAccessibilityConfiguration::
  33. kReadExecuteProtected
  34. : partition_alloc::PageAccessibilityConfiguration::kReadExecute);
  35. #else
  36. CHECK_EQ(sut.GetPageConfigForTesting(v8::PageAllocator::kReadExecute),
  37. partition_alloc::PageAccessibilityConfiguration::kReadExecute);
  38. #endif
  39. CHECK_EQ(
  40. sut.GetPageConfigForTesting(v8::PageAllocator::kNoAccessWillJitLater),
  41. partition_alloc::PageAccessibilityConfiguration::kInaccessible);
  42. }
  43. #if defined(ARCH_CPU_ARM64) && (OS_LINUX || OS_ANDROID)
  44. using BTITestFunction = int64_t (*)(int64_t);
  45. TEST(V8PlatformPageAllocatorBTITest, VerifyReadExecutePagesAreProtected) {
  46. auto page_allocator = gin::PageAllocator();
  47. auto const memory_size = base::PageAllocationGranularity();
  48. auto const memory_alignment = base::PageAllocationGranularity();
  49. // Next, map some read-write memory and copy some test helper functions there.
  50. char* const buffer = reinterpret_cast<char*>(page_allocator.AllocatePages(
  51. nullptr, memory_size, memory_alignment,
  52. v8::PageAllocator::Permission::kReadWriteExecute));
  53. ptrdiff_t const function_range =
  54. reinterpret_cast<char*>(arm_bti_test_function_end) -
  55. reinterpret_cast<char*>(arm_bti_test_function);
  56. ptrdiff_t const invalid_offset =
  57. reinterpret_cast<char*>(arm_bti_test_function_invalid_offset) -
  58. reinterpret_cast<char*>(arm_bti_test_function);
  59. // ensure alignment to 4 bytes required by function call convention
  60. EXPECT_EQ(0u, ((uint64_t)buffer) % 4);
  61. EXPECT_EQ(0u, ((uint64_t)function_range) % 4);
  62. EXPECT_EQ(0u, ((uint64_t)invalid_offset) % 4);
  63. memcpy(buffer, reinterpret_cast<void*>(arm_bti_test_function),
  64. function_range);
  65. // Next re-protect the page to the permission level to test
  66. page_allocator.SetPermissions(buffer, memory_size,
  67. v8::PageAllocator::Permission::kReadExecute);
  68. // Attempt to call a function with BTI landing pad.
  69. BTITestFunction const bti_enabled_fn =
  70. reinterpret_cast<BTITestFunction>(buffer);
  71. // bti_enabled_fn must return 18, no matter if BTI is actually enabled or not.
  72. EXPECT_EQ(bti_enabled_fn(15), 18);
  73. // Next, attempt to call a function without BTI landing pad.
  74. BTITestFunction const bti_invalid_fn =
  75. reinterpret_cast<BTITestFunction>(buffer + invalid_offset);
  76. // Expectation for behaviour of bti_invalid_fn depends on the capabilities of
  77. // the actual CPU we are running on. The code that were are trying to execute
  78. // is assembly code and always has BTI enabled.
  79. if (base::CPU::GetInstanceNoAllocation().has_bti()) {
  80. #if BUILDFLAG(IS_POSIX) // signal handling is available on POSIX compliant
  81. // systems only
  82. EXPECT_EXIT({ bti_invalid_fn(15); }, testing::KilledBySignal(SIGILL),
  83. ""); // Should crash with SIGILL.
  84. #endif // BUILDFLAG(IS_POSIX)
  85. } else {
  86. EXPECT_EQ(bti_invalid_fn(15), 17);
  87. }
  88. page_allocator.FreePages(buffer, memory_size);
  89. }
  90. TEST(V8PlatformAllocatorBTITest, VerifyReadWriteExecutePagesAreNotProtected) {
  91. auto page_allocator = gin::PageAllocator();
  92. auto const memory_size = base::PageAllocationGranularity();
  93. auto const memory_alignment = base::PageAllocationGranularity();
  94. // Next, map some read-write memory and copy some test helper functions there.
  95. char* const buffer = reinterpret_cast<char*>(page_allocator.AllocatePages(
  96. nullptr, memory_size, memory_alignment,
  97. v8::PageAllocator::Permission::kReadWriteExecute));
  98. ptrdiff_t const function_range =
  99. reinterpret_cast<char*>(arm_bti_test_function_end) -
  100. reinterpret_cast<char*>(arm_bti_test_function);
  101. ptrdiff_t const invalid_offset =
  102. reinterpret_cast<char*>(arm_bti_test_function_invalid_offset) -
  103. reinterpret_cast<char*>(arm_bti_test_function);
  104. // ensure alignment to 4 bytes required by function call convention
  105. EXPECT_EQ(0u, ((uint64_t)buffer) % 4);
  106. EXPECT_EQ(0u, ((uint64_t)function_range) % 4);
  107. EXPECT_EQ(0u, ((uint64_t)invalid_offset) % 4);
  108. memcpy(buffer, reinterpret_cast<void*>(arm_bti_test_function),
  109. function_range);
  110. // Attempt to call a function with BTI landing pad.
  111. BTITestFunction const bti_enabled_fn =
  112. reinterpret_cast<BTITestFunction>(buffer);
  113. // bti_enabled_fn must return 18, no matter if BTI is actually enabled or not.
  114. EXPECT_EQ(bti_enabled_fn(15), 18);
  115. // Next, attempt to call a function without BTI landing pad.
  116. BTITestFunction const bti_invalid_fn =
  117. reinterpret_cast<BTITestFunction>(buffer + invalid_offset);
  118. // Since permission kReadWriteExecute wont actually cause BTI to be enabled
  119. // for the allocated page, calling this function must return without error.
  120. EXPECT_EQ(bti_invalid_fn(15), 17);
  121. page_allocator.FreePages(buffer, memory_size);
  122. }
  123. #endif // if defined(ARCH_CPU_ARM64) && (OS_LINUX || OS_ANDROID)
  124. } // namespace gin