[compiler-rt] r367560 - compiler-rt: Rename .cc file in lib/{interception/tests, safestack} to .cpp
Nico Weber via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 06:56:52 PDT 2019
Author: nico
Date: Thu Aug 1 06:56:52 2019
New Revision: 367560
URL: http://llvm.org/viewvc/llvm-project?rev=367560&view=rev
Log:
compiler-rt: Rename .cc file in lib/{interception/tests,safestack} to .cpp
Like r367463, but for interception/tests and safestack.
Added:
compiler-rt/trunk/lib/interception/tests/interception_linux_test.cpp
- copied, changed from r367559, compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc
compiler-rt/trunk/lib/interception/tests/interception_test_main.cpp
- copied, changed from r367559, compiler-rt/trunk/lib/interception/tests/interception_test_main.cc
compiler-rt/trunk/lib/interception/tests/interception_win_test.cpp
- copied, changed from r367559, compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
compiler-rt/trunk/lib/safestack/safestack.cpp
- copied, changed from r367559, compiler-rt/trunk/lib/safestack/safestack.cc
Removed:
compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc
compiler-rt/trunk/lib/interception/tests/interception_test_main.cc
compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
compiler-rt/trunk/lib/safestack/safestack.cc
Modified:
compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
compiler-rt/trunk/lib/safestack/CMakeLists.txt
Modified: compiler-rt/trunk/lib/interception/tests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/CMakeLists.txt?rev=367560&r1=367559&r2=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/interception/tests/CMakeLists.txt Thu Aug 1 06:56:52 2019
@@ -3,10 +3,10 @@ include(CompilerRTCompile)
filter_available_targets(INTERCEPTION_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el)
set(INTERCEPTION_UNITTESTS
- interception_linux_test.cc
- interception_test_main.cc
- interception_win_test.cc
-)
+ interception_linux_test.cpp
+ interception_test_main.cpp
+ interception_win_test.cpp
+ )
set(INTERCEPTION_TEST_HEADERS)
Removed: compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc?rev=367559&view=auto
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc (removed)
@@ -1,67 +0,0 @@
-//===-- interception_linux_test.cc ----------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
-// Tests for interception_linux.h.
-//
-//===----------------------------------------------------------------------===//
-
-// Do not declare isdigit in ctype.h.
-#define __NO_CTYPE
-
-#include "interception/interception.h"
-
-#include "gtest/gtest.h"
-
-// Too slow for debug build
-#if !SANITIZER_DEBUG
-#if SANITIZER_LINUX
-
-static int InterceptorFunctionCalled;
-
-DECLARE_REAL(int, isdigit, int);
-
-INTERCEPTOR(int, isdigit, int d) {
- ++InterceptorFunctionCalled;
- return d >= '0' && d <= '9';
-}
-
-namespace __interception {
-
-TEST(Interception, InterceptFunction) {
- uptr malloc_address = 0;
- EXPECT_TRUE(InterceptFunction("malloc", &malloc_address, 0, 0));
- EXPECT_NE(0U, malloc_address);
- EXPECT_FALSE(InterceptFunction("malloc", &malloc_address, 0, 1));
-
- uptr dummy_address = 0;
- EXPECT_FALSE(InterceptFunction("dummy_doesnt_exist__", &dummy_address, 0, 0));
- EXPECT_EQ(0U, dummy_address);
-}
-
-TEST(Interception, Basic) {
- EXPECT_TRUE(INTERCEPT_FUNCTION(isdigit));
-
- // After interception, the counter should be incremented.
- InterceptorFunctionCalled = 0;
- EXPECT_NE(0, isdigit('1'));
- EXPECT_EQ(1, InterceptorFunctionCalled);
- EXPECT_EQ(0, isdigit('a'));
- EXPECT_EQ(2, InterceptorFunctionCalled);
-
- // Calling the REAL function should not affect the counter.
- InterceptorFunctionCalled = 0;
- EXPECT_NE(0, REAL(isdigit)('1'));
- EXPECT_EQ(0, REAL(isdigit)('a'));
- EXPECT_EQ(0, InterceptorFunctionCalled);
-}
-
-} // namespace __interception
-
-#endif // SANITIZER_LINUX
-#endif // #if !SANITIZER_DEBUG
Copied: compiler-rt/trunk/lib/interception/tests/interception_linux_test.cpp (from r367559, compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_linux_test.cpp?p2=compiler-rt/trunk/lib/interception/tests/interception_linux_test.cpp&p1=compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc&r1=367559&r2=367560&rev=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_linux_test.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_linux_test.cpp Thu Aug 1 06:56:52 2019
@@ -1,4 +1,4 @@
-//===-- interception_linux_test.cc ----------------------------------------===//
+//===-- interception_linux_test.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Removed: compiler-rt/trunk/lib/interception/tests/interception_test_main.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_test_main.cc?rev=367559&view=auto
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_test_main.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_test_main.cc (removed)
@@ -1,21 +0,0 @@
-//===-- interception_test_main.cc------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file is a part of AddressSanitizer, an address sanity checker.
-//
-// Testing the machinery for providing replacements/wrappers for system
-// functions.
-//===----------------------------------------------------------------------===//
-
-#include "gtest/gtest.h"
-
-int main(int argc, char **argv) {
- testing::GTEST_FLAG(death_test_style) = "threadsafe";
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
-}
Copied: compiler-rt/trunk/lib/interception/tests/interception_test_main.cpp (from r367559, compiler-rt/trunk/lib/interception/tests/interception_test_main.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_test_main.cpp?p2=compiler-rt/trunk/lib/interception/tests/interception_test_main.cpp&p1=compiler-rt/trunk/lib/interception/tests/interception_test_main.cc&r1=367559&r2=367560&rev=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_test_main.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_test_main.cpp Thu Aug 1 06:56:52 2019
@@ -1,4 +1,4 @@
-//===-- interception_test_main.cc------------------------------------------===//
+//===-- interception_test_main.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Removed: compiler-rt/trunk/lib/interception/tests/interception_win_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_win_test.cc?rev=367559&view=auto
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_win_test.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_win_test.cc (removed)
@@ -1,657 +0,0 @@
-//===-- interception_win_test.cc ------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
-// Tests for interception_win.h.
-//
-//===----------------------------------------------------------------------===//
-#include "interception/interception.h"
-
-#include "gtest/gtest.h"
-
-// Too slow for debug build
-#if !SANITIZER_DEBUG
-#if SANITIZER_WINDOWS
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-namespace __interception {
-namespace {
-
-enum FunctionPrefixKind {
- FunctionPrefixNone,
- FunctionPrefixPadding,
- FunctionPrefixHotPatch,
- FunctionPrefixDetour,
-};
-
-typedef bool (*TestOverrideFunction)(uptr, uptr, uptr*);
-typedef int (*IdentityFunction)(int);
-
-#if SANITIZER_WINDOWS64
-
-const u8 kIdentityCodeWithPrologue[] = {
- 0x55, // push rbp
- 0x48, 0x89, 0xE5, // mov rbp,rsp
- 0x8B, 0xC1, // mov eax,ecx
- 0x5D, // pop rbp
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithPushPop[] = {
- 0x55, // push rbp
- 0x48, 0x89, 0xE5, // mov rbp,rsp
- 0x53, // push rbx
- 0x50, // push rax
- 0x58, // pop rax
- 0x8B, 0xC1, // mov rax,rcx
- 0x5B, // pop rbx
- 0x5D, // pop rbp
- 0xC3, // ret
-};
-
-const u8 kIdentityTwiceOffset = 16;
-const u8 kIdentityTwice[] = {
- 0x55, // push rbp
- 0x48, 0x89, 0xE5, // mov rbp,rsp
- 0x8B, 0xC1, // mov eax,ecx
- 0x5D, // pop rbp
- 0xC3, // ret
- 0x90, 0x90, 0x90, 0x90,
- 0x90, 0x90, 0x90, 0x90,
- 0x55, // push rbp
- 0x48, 0x89, 0xE5, // mov rbp,rsp
- 0x8B, 0xC1, // mov eax,ecx
- 0x5D, // pop rbp
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithMov[] = {
- 0x89, 0xC8, // mov eax, ecx
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithJump[] = {
- 0xE9, 0x04, 0x00, 0x00,
- 0x00, // jmp + 4
- 0xCC, 0xCC, 0xCC, 0xCC,
- 0x89, 0xC8, // mov eax, ecx
- 0xC3, // ret
-};
-
-#else
-
-const u8 kIdentityCodeWithPrologue[] = {
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8]
- 0x5D, // pop ebp
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithPushPop[] = {
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x53, // push ebx
- 0x50, // push eax
- 0x58, // pop eax
- 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8]
- 0x5B, // pop ebx
- 0x5D, // pop ebp
- 0xC3, // ret
-};
-
-const u8 kIdentityTwiceOffset = 8;
-const u8 kIdentityTwice[] = {
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8]
- 0x5D, // pop ebp
- 0xC3, // ret
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8]
- 0x5D, // pop ebp
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithMov[] = {
- 0x8B, 0x44, 0x24, 0x04, // mov eax,dword ptr [esp + 4]
- 0xC3, // ret
-};
-
-const u8 kIdentityCodeWithJump[] = {
- 0xE9, 0x04, 0x00, 0x00,
- 0x00, // jmp + 4
- 0xCC, 0xCC, 0xCC, 0xCC,
- 0x8B, 0x44, 0x24, 0x04, // mov eax,dword ptr [esp + 4]
- 0xC3, // ret
-};
-
-#endif
-
-const u8 kPatchableCode1[] = {
- 0xB8, 0x4B, 0x00, 0x00, 0x00, // mov eax,4B
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kPatchableCode2[] = {
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x33, 0xC0, // xor eax,eax
- 0x5D, // pop ebp
- 0xC3, // ret
-};
-
-const u8 kPatchableCode3[] = {
- 0x55, // push ebp
- 0x8B, 0xEC, // mov ebp,esp
- 0x6A, 0x00, // push 0
- 0xE8, 0x3D, 0xFF, 0xFF, 0xFF, // call <func>
-};
-
-const u8 kPatchableCode4[] = {
- 0xE9, 0xCC, 0xCC, 0xCC, 0xCC, // jmp <label>
- 0x90, 0x90, 0x90, 0x90,
-};
-
-const u8 kPatchableCode5[] = {
- 0x55, // push ebp
- 0x8b, 0xec, // mov ebp,esp
- 0x8d, 0xa4, 0x24, 0x30, 0xfd, 0xff, 0xff, // lea esp,[esp-2D0h]
- 0x54, // push esp
-};
-
-#if SANITIZER_WINDOWS64
-u8 kLoadGlobalCode[] = {
- 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00, // mov eax [rip + global]
- 0xC3, // ret
-};
-#endif
-
-const u8 kUnpatchableCode1[] = {
- 0xC3, // ret
-};
-
-const u8 kUnpatchableCode2[] = {
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kUnpatchableCode3[] = {
- 0x75, 0xCC, // jne <label>
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kUnpatchableCode4[] = {
- 0x74, 0xCC, // jne <label>
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kUnpatchableCode5[] = {
- 0xEB, 0x02, // jmp <label>
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kUnpatchableCode6[] = {
- 0xE8, 0xCC, 0xCC, 0xCC, 0xCC, // call <func>
- 0x90, 0x90, 0x90, 0x90,
-};
-
-const u8 kPatchableCode6[] = {
- 0x48, 0x89, 0x54, 0x24, 0xBB, // mov QWORD PTR [rsp + 0xBB], rdx
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kPatchableCode7[] = {
- 0x4c, 0x89, 0x4c, 0x24, 0xBB, // mov QWORD PTR [rsp + 0xBB], r9
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-const u8 kPatchableCode8[] = {
- 0x4c, 0x89, 0x44, 0x24, 0xBB, // mov QWORD PTR [rsp + 0xBB], r8
- 0x33, 0xC9, // xor ecx,ecx
- 0xC3, // ret
-};
-
-// A buffer holding the dynamically generated code under test.
-u8* ActiveCode;
-const size_t ActiveCodeLength = 4096;
-
-int InterceptorFunction(int x);
-
-/// Allocate code memory more than 2GB away from Base.
-u8 *AllocateCode2GBAway(u8 *Base) {
- // Find a 64K aligned location after Base plus 2GB.
- size_t TwoGB = 0x80000000;
- size_t AllocGranularity = 0x10000;
- Base = (u8 *)((((uptr)Base + TwoGB + AllocGranularity)) & ~(AllocGranularity - 1));
-
- // Check if that location is free, and if not, loop over regions until we find
- // one that is.
- MEMORY_BASIC_INFORMATION mbi = {};
- while (sizeof(mbi) == VirtualQuery(Base, &mbi, sizeof(mbi))) {
- if (mbi.State & MEM_FREE) break;
- Base += mbi.RegionSize;
- }
-
- // Allocate one RWX page at the free location.
- return (u8 *)::VirtualAlloc(Base, ActiveCodeLength, MEM_COMMIT | MEM_RESERVE,
- PAGE_EXECUTE_READWRITE);
-}
-
-template<class T>
-static void LoadActiveCode(
- const T &code,
- uptr *entry_point,
- FunctionPrefixKind prefix_kind = FunctionPrefixNone) {
- if (ActiveCode == nullptr) {
- ActiveCode = AllocateCode2GBAway((u8*)&InterceptorFunction);
- ASSERT_NE(ActiveCode, nullptr) << "failed to allocate RWX memory 2GB away";
- }
-
- size_t position = 0;
-
- // Add padding to avoid memory violation when scanning the prefix.
- for (int i = 0; i < 16; ++i)
- ActiveCode[position++] = 0xC3; // Instruction 'ret'.
-
- // Add function padding.
- size_t padding = 0;
- if (prefix_kind == FunctionPrefixPadding)
- padding = 16;
- else if (prefix_kind == FunctionPrefixDetour ||
- prefix_kind == FunctionPrefixHotPatch)
- padding = FIRST_32_SECOND_64(5, 6);
- // Insert |padding| instructions 'nop'.
- for (size_t i = 0; i < padding; ++i)
- ActiveCode[position++] = 0x90;
-
- // Keep track of the entry point.
- *entry_point = (uptr)&ActiveCode[position];
-
- // Add the detour instruction (i.e. mov edi, edi)
- if (prefix_kind == FunctionPrefixDetour) {
-#if SANITIZER_WINDOWS64
- // Note that "mov edi,edi" is NOP in 32-bit only, in 64-bit it clears
- // higher bits of RDI.
- // Use 66,90H as NOP for Windows64.
- ActiveCode[position++] = 0x66;
- ActiveCode[position++] = 0x90;
-#else
- // mov edi,edi.
- ActiveCode[position++] = 0x8B;
- ActiveCode[position++] = 0xFF;
-#endif
-
- }
-
- // Copy the function body.
- for (size_t i = 0; i < sizeof(T); ++i)
- ActiveCode[position++] = code[i];
-}
-
-int InterceptorFunctionCalled;
-IdentityFunction InterceptedRealFunction;
-
-int InterceptorFunction(int x) {
- ++InterceptorFunctionCalled;
- return InterceptedRealFunction(x);
-}
-
-} // namespace
-
-// Tests for interception_win.h
-TEST(Interception, InternalGetProcAddress) {
- HMODULE ntdll_handle = ::GetModuleHandle("ntdll");
- ASSERT_NE(nullptr, ntdll_handle);
- uptr DbgPrint_expected = (uptr)::GetProcAddress(ntdll_handle, "DbgPrint");
- uptr isdigit_expected = (uptr)::GetProcAddress(ntdll_handle, "isdigit");
- uptr DbgPrint_adddress = InternalGetProcAddress(ntdll_handle, "DbgPrint");
- uptr isdigit_address = InternalGetProcAddress(ntdll_handle, "isdigit");
-
- EXPECT_EQ(DbgPrint_expected, DbgPrint_adddress);
- EXPECT_EQ(isdigit_expected, isdigit_address);
- EXPECT_NE(DbgPrint_adddress, isdigit_address);
-}
-
-template<class T>
-static void TestIdentityFunctionPatching(
- const T &code,
- TestOverrideFunction override,
- FunctionPrefixKind prefix_kind = FunctionPrefixNone) {
- uptr identity_address;
- LoadActiveCode(code, &identity_address, prefix_kind);
- IdentityFunction identity = (IdentityFunction)identity_address;
-
- // Validate behavior before dynamic patching.
- InterceptorFunctionCalled = 0;
- EXPECT_EQ(0, identity(0));
- EXPECT_EQ(42, identity(42));
- EXPECT_EQ(0, InterceptorFunctionCalled);
-
- // Patch the function.
- uptr real_identity_address = 0;
- bool success = override(identity_address,
- (uptr)&InterceptorFunction,
- &real_identity_address);
- EXPECT_TRUE(success);
- EXPECT_NE(0U, real_identity_address);
- IdentityFunction real_identity = (IdentityFunction)real_identity_address;
- InterceptedRealFunction = real_identity;
-
- // Don't run tests if hooking failed or the real function is not valid.
- if (!success || !real_identity_address)
- return;
-
- // Calling the redirected function.
- InterceptorFunctionCalled = 0;
- EXPECT_EQ(0, identity(0));
- EXPECT_EQ(42, identity(42));
- EXPECT_EQ(2, InterceptorFunctionCalled);
-
- // Calling the real function.
- InterceptorFunctionCalled = 0;
- EXPECT_EQ(0, real_identity(0));
- EXPECT_EQ(42, real_identity(42));
- EXPECT_EQ(0, InterceptorFunctionCalled);
-
- TestOnlyReleaseTrampolineRegions();
-}
-
-#if !SANITIZER_WINDOWS64
-TEST(Interception, OverrideFunctionWithDetour) {
- TestOverrideFunction override = OverrideFunctionWithDetour;
- FunctionPrefixKind prefix = FunctionPrefixDetour;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithMov, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override, prefix);
-}
-#endif // !SANITIZER_WINDOWS64
-
-TEST(Interception, OverrideFunctionWithRedirectJump) {
- TestOverrideFunction override = OverrideFunctionWithRedirectJump;
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override);
-}
-
-TEST(Interception, OverrideFunctionWithHotPatch) {
- TestOverrideFunction override = OverrideFunctionWithHotPatch;
- FunctionPrefixKind prefix = FunctionPrefixHotPatch;
- TestIdentityFunctionPatching(kIdentityCodeWithMov, override, prefix);
-}
-
-TEST(Interception, OverrideFunctionWithTrampoline) {
- TestOverrideFunction override = OverrideFunctionWithTrampoline;
- FunctionPrefixKind prefix = FunctionPrefixNone;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
-
- prefix = FunctionPrefixPadding;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
-}
-
-TEST(Interception, OverrideFunction) {
- TestOverrideFunction override = OverrideFunction;
- FunctionPrefixKind prefix = FunctionPrefixNone;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override, prefix);
-
- prefix = FunctionPrefixPadding;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithMov, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override, prefix);
-
- prefix = FunctionPrefixHotPatch;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithMov, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override, prefix);
-
- prefix = FunctionPrefixDetour;
- TestIdentityFunctionPatching(kIdentityCodeWithPrologue, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithPushPop, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithMov, override, prefix);
- TestIdentityFunctionPatching(kIdentityCodeWithJump, override, prefix);
-}
-
-template<class T>
-static void TestIdentityFunctionMultiplePatching(
- const T &code,
- TestOverrideFunction override,
- FunctionPrefixKind prefix_kind = FunctionPrefixNone) {
- uptr identity_address;
- LoadActiveCode(code, &identity_address, prefix_kind);
-
- // Patch the function.
- uptr real_identity_address = 0;
- bool success = override(identity_address,
- (uptr)&InterceptorFunction,
- &real_identity_address);
- EXPECT_TRUE(success);
- EXPECT_NE(0U, real_identity_address);
-
- // Re-patching the function should not work.
- success = override(identity_address,
- (uptr)&InterceptorFunction,
- &real_identity_address);
- EXPECT_FALSE(success);
-
- TestOnlyReleaseTrampolineRegions();
-}
-
-TEST(Interception, OverrideFunctionMultiplePatchingIsFailing) {
-#if !SANITIZER_WINDOWS64
- TestIdentityFunctionMultiplePatching(kIdentityCodeWithPrologue,
- OverrideFunctionWithDetour,
- FunctionPrefixDetour);
-#endif
-
- TestIdentityFunctionMultiplePatching(kIdentityCodeWithMov,
- OverrideFunctionWithHotPatch,
- FunctionPrefixHotPatch);
-
- TestIdentityFunctionMultiplePatching(kIdentityCodeWithPushPop,
- OverrideFunctionWithTrampoline,
- FunctionPrefixPadding);
-}
-
-TEST(Interception, OverrideFunctionTwice) {
- uptr identity_address1;
- LoadActiveCode(kIdentityTwice, &identity_address1);
- uptr identity_address2 = identity_address1 + kIdentityTwiceOffset;
- IdentityFunction identity1 = (IdentityFunction)identity_address1;
- IdentityFunction identity2 = (IdentityFunction)identity_address2;
-
- // Patch the two functions.
- uptr real_identity_address = 0;
- EXPECT_TRUE(OverrideFunction(identity_address1,
- (uptr)&InterceptorFunction,
- &real_identity_address));
- EXPECT_TRUE(OverrideFunction(identity_address2,
- (uptr)&InterceptorFunction,
- &real_identity_address));
- IdentityFunction real_identity = (IdentityFunction)real_identity_address;
- InterceptedRealFunction = real_identity;
-
- // Calling the redirected function.
- InterceptorFunctionCalled = 0;
- EXPECT_EQ(42, identity1(42));
- EXPECT_EQ(42, identity2(42));
- EXPECT_EQ(2, InterceptorFunctionCalled);
-
- TestOnlyReleaseTrampolineRegions();
-}
-
-template<class T>
-static bool TestFunctionPatching(
- const T &code,
- TestOverrideFunction override,
- FunctionPrefixKind prefix_kind = FunctionPrefixNone) {
- uptr address;
- LoadActiveCode(code, &address, prefix_kind);
- uptr unused_real_address = 0;
- bool result = override(
- address, (uptr)&InterceptorFunction, &unused_real_address);
-
- TestOnlyReleaseTrampolineRegions();
- return result;
-}
-
-TEST(Interception, PatchableFunction) {
- TestOverrideFunction override = OverrideFunction;
- // Test without function padding.
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode1, override));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode2, override));
-#if SANITIZER_WINDOWS64
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override));
-#else
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode3, override));
-#endif
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode4, override));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode5, override));
-#if SANITIZER_WINDOWS64
- EXPECT_TRUE(TestFunctionPatching(kLoadGlobalCode, override));
-#endif
-
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode2, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override));
-}
-
-#if !SANITIZER_WINDOWS64
-TEST(Interception, PatchableFunctionWithDetour) {
- TestOverrideFunction override = OverrideFunctionWithDetour;
- // Without the prefix, no function can be detoured.
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode1, override));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode2, override));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode4, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode2, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override));
-
- // With the prefix, all functions can be detoured.
- FunctionPrefixKind prefix = FunctionPrefixDetour;
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode2, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode3, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode4, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode2, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode3, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode4, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode5, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode6, override, prefix));
-}
-#endif // !SANITIZER_WINDOWS64
-
-TEST(Interception, PatchableFunctionWithRedirectJump) {
- TestOverrideFunction override = OverrideFunctionWithRedirectJump;
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode1, override));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode2, override));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode4, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode2, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override));
-}
-
-TEST(Interception, PatchableFunctionWithHotPatch) {
- TestOverrideFunction override = OverrideFunctionWithHotPatch;
- FunctionPrefixKind prefix = FunctionPrefixHotPatch;
-
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode1, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode2, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode4, override, prefix));
-#if SANITIZER_WINDOWS64
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode6, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode7, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode8, override, prefix));
-#endif
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode2, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override, prefix));
-}
-
-TEST(Interception, PatchableFunctionWithTrampoline) {
- TestOverrideFunction override = OverrideFunctionWithTrampoline;
- FunctionPrefixKind prefix = FunctionPrefixPadding;
-
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode2, override, prefix));
-#if SANITIZER_WINDOWS64
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override, prefix));
-#else
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode3, override, prefix));
-#endif
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode4, override, prefix));
-
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode2, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override, prefix));
-}
-
-TEST(Interception, PatchableFunctionPadding) {
- TestOverrideFunction override = OverrideFunction;
- FunctionPrefixKind prefix = FunctionPrefixPadding;
-
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode2, override, prefix));
-#if SANITIZER_WINDOWS64
- EXPECT_FALSE(TestFunctionPatching(kPatchableCode3, override, prefix));
-#else
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode3, override, prefix));
-#endif
- EXPECT_TRUE(TestFunctionPatching(kPatchableCode4, override, prefix));
-
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode1, override, prefix));
- EXPECT_TRUE(TestFunctionPatching(kUnpatchableCode2, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode3, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode4, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode5, override, prefix));
- EXPECT_FALSE(TestFunctionPatching(kUnpatchableCode6, override, prefix));
-}
-
-TEST(Interception, EmptyExportTable) {
- // We try to get a pointer to a function from an executable that doesn't
- // export any symbol (empty export table).
- uptr FunPtr = InternalGetProcAddress((void *)GetModuleHandleA(0), "example");
- EXPECT_EQ(0U, FunPtr);
-}
-
-} // namespace __interception
-
-#endif // SANITIZER_WINDOWS
-#endif // #if !SANITIZER_DEBUG
Copied: compiler-rt/trunk/lib/interception/tests/interception_win_test.cpp (from r367559, compiler-rt/trunk/lib/interception/tests/interception_win_test.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/tests/interception_win_test.cpp?p2=compiler-rt/trunk/lib/interception/tests/interception_win_test.cpp&p1=compiler-rt/trunk/lib/interception/tests/interception_win_test.cc&r1=367559&r2=367560&rev=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/tests/interception_win_test.cc (original)
+++ compiler-rt/trunk/lib/interception/tests/interception_win_test.cpp Thu Aug 1 06:56:52 2019
@@ -1,4 +1,4 @@
-//===-- interception_win_test.cc ------------------------------------------===//
+//===-- interception_win_test.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Modified: compiler-rt/trunk/lib/safestack/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/CMakeLists.txt?rev=367560&r1=367559&r2=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/safestack/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/safestack/CMakeLists.txt Thu Aug 1 06:56:52 2019
@@ -1,6 +1,8 @@
add_compiler_rt_component(safestack)
-set(SAFESTACK_SOURCES safestack.cc)
+set(SAFESTACK_SOURCES
+ safestack.cpp
+ )
include_directories(..)
Removed: compiler-rt/trunk/lib/safestack/safestack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/safestack.cc?rev=367559&view=auto
==============================================================================
--- compiler-rt/trunk/lib/safestack/safestack.cc (original)
+++ compiler-rt/trunk/lib/safestack/safestack.cc (removed)
@@ -1,310 +0,0 @@
-//===-- safestack.cc ------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the runtime support for the safe stack protection
-// mechanism. The runtime manages allocation/deallocation of the unsafe stack
-// for the main thread, as well as all pthreads that are created/destroyed
-// during program execution.
-//
-//===----------------------------------------------------------------------===//
-
-#include "safestack_platform.h"
-#include "safestack_util.h"
-
-#include <errno.h>
-#include <sys/resource.h>
-
-#include "interception/interception.h"
-
-using namespace safestack;
-
-// TODO: To make accessing the unsafe stack pointer faster, we plan to
-// eventually store it directly in the thread control block data structure on
-// platforms where this structure is pointed to by %fs or %gs. This is exactly
-// the same mechanism as currently being used by the traditional stack
-// protector pass to store the stack guard (see getStackCookieLocation()
-// function above). Doing so requires changing the tcbhead_t struct in glibc
-// on Linux and tcb struct in libc on FreeBSD.
-//
-// For now, store it in a thread-local variable.
-extern "C" {
-__attribute__((visibility(
- "default"))) __thread void *__safestack_unsafe_stack_ptr = nullptr;
-}
-
-namespace {
-
-// TODO: The runtime library does not currently protect the safe stack beyond
-// relying on the system-enforced ASLR. The protection of the (safe) stack can
-// be provided by three alternative features:
-//
-// 1) Protection via hardware segmentation on x86-32 and some x86-64
-// architectures: the (safe) stack segment (implicitly accessed via the %ss
-// segment register) can be separated from the data segment (implicitly
-// accessed via the %ds segment register). Dereferencing a pointer to the safe
-// segment would result in a segmentation fault.
-//
-// 2) Protection via software fault isolation: memory writes that are not meant
-// to access the safe stack can be prevented from doing so through runtime
-// instrumentation. One way to do it is to allocate the safe stack(s) in the
-// upper half of the userspace and bitmask the corresponding upper bit of the
-// memory addresses of memory writes that are not meant to access the safe
-// stack.
-//
-// 3) Protection via information hiding on 64 bit architectures: the location
-// of the safe stack(s) can be randomized through secure mechanisms, and the
-// leakage of the stack pointer can be prevented. Currently, libc can leak the
-// stack pointer in several ways (e.g. in longjmp, signal handling, user-level
-// context switching related functions, etc.). These can be fixed in libc and
-// in other low-level libraries, by either eliminating the escaping/dumping of
-// the stack pointer (i.e., %rsp) when that's possible, or by using
-// encryption/PTR_MANGLE (XOR-ing the dumped stack pointer with another secret
-// we control and protect better, as is already done for setjmp in glibc.)
-// Furthermore, a static machine code level verifier can be ran after code
-// generation to make sure that the stack pointer is never written to memory,
-// or if it is, its written on the safe stack.
-//
-// Finally, while the Unsafe Stack pointer is currently stored in a thread
-// local variable, with libc support it could be stored in the TCB (thread
-// control block) as well, eliminating another level of indirection and making
-// such accesses faster. Alternatively, dedicating a separate register for
-// storing it would also be possible.
-
-/// Minimum stack alignment for the unsafe stack.
-const unsigned kStackAlign = 16;
-
-/// Default size of the unsafe stack. This value is only used if the stack
-/// size rlimit is set to infinity.
-const unsigned kDefaultUnsafeStackSize = 0x2800000;
-
-// Per-thread unsafe stack information. It's not frequently accessed, so there
-// it can be kept out of the tcb in normal thread-local variables.
-__thread void *unsafe_stack_start = nullptr;
-__thread size_t unsafe_stack_size = 0;
-__thread size_t unsafe_stack_guard = 0;
-
-inline void *unsafe_stack_alloc(size_t size, size_t guard) {
- SFS_CHECK(size + guard >= size);
- void *addr = Mmap(nullptr, size + guard, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, -1, 0);
- SFS_CHECK(MAP_FAILED != addr);
- Mprotect(addr, guard, PROT_NONE);
- return (char *)addr + guard;
-}
-
-inline void unsafe_stack_setup(void *start, size_t size, size_t guard) {
- SFS_CHECK((char *)start + size >= (char *)start);
- SFS_CHECK((char *)start + guard >= (char *)start);
- void *stack_ptr = (char *)start + size;
- SFS_CHECK((((size_t)stack_ptr) & (kStackAlign - 1)) == 0);
-
- __safestack_unsafe_stack_ptr = stack_ptr;
- unsafe_stack_start = start;
- unsafe_stack_size = size;
- unsafe_stack_guard = guard;
-}
-
-/// Thread data for the cleanup handler
-pthread_key_t thread_cleanup_key;
-
-/// Safe stack per-thread information passed to the thread_start function
-struct tinfo {
- void *(*start_routine)(void *);
- void *start_routine_arg;
-
- void *unsafe_stack_start;
- size_t unsafe_stack_size;
- size_t unsafe_stack_guard;
-};
-
-/// Wrap the thread function in order to deallocate the unsafe stack when the
-/// thread terminates by returning from its main function.
-void *thread_start(void *arg) {
- struct tinfo *tinfo = (struct tinfo *)arg;
-
- void *(*start_routine)(void *) = tinfo->start_routine;
- void *start_routine_arg = tinfo->start_routine_arg;
-
- // Setup the unsafe stack; this will destroy tinfo content
- unsafe_stack_setup(tinfo->unsafe_stack_start, tinfo->unsafe_stack_size,
- tinfo->unsafe_stack_guard);
-
- // Make sure out thread-specific destructor will be called
- pthread_setspecific(thread_cleanup_key, (void *)1);
-
- return start_routine(start_routine_arg);
-}
-
-/// Linked list used to store exiting threads stack/thread information.
-struct thread_stack_ll {
- struct thread_stack_ll *next;
- void *stack_base;
- size_t size;
- pid_t pid;
- ThreadId tid;
-};
-
-/// Linked list of unsafe stacks for threads that are exiting. We delay
-/// unmapping them until the thread exits.
-thread_stack_ll *thread_stacks = nullptr;
-pthread_mutex_t thread_stacks_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-/// Thread-specific data destructor. We want to free the unsafe stack only after
-/// this thread is terminated. libc can call functions in safestack-instrumented
-/// code (like free) after thread-specific data destructors have run.
-void thread_cleanup_handler(void *_iter) {
- SFS_CHECK(unsafe_stack_start != nullptr);
- pthread_setspecific(thread_cleanup_key, NULL);
-
- pthread_mutex_lock(&thread_stacks_mutex);
- // Temporary list to hold the previous threads stacks so we don't hold the
- // thread_stacks_mutex for long.
- thread_stack_ll *temp_stacks = thread_stacks;
- thread_stacks = nullptr;
- pthread_mutex_unlock(&thread_stacks_mutex);
-
- pid_t pid = getpid();
- ThreadId tid = GetTid();
-
- // Free stacks for dead threads
- thread_stack_ll **stackp = &temp_stacks;
- while (*stackp) {
- thread_stack_ll *stack = *stackp;
- if (stack->pid != pid ||
- (-1 == TgKill(stack->pid, stack->tid, 0) && errno == ESRCH)) {
- Munmap(stack->stack_base, stack->size);
- *stackp = stack->next;
- free(stack);
- } else
- stackp = &stack->next;
- }
-
- thread_stack_ll *cur_stack =
- (thread_stack_ll *)malloc(sizeof(thread_stack_ll));
- cur_stack->stack_base = (char *)unsafe_stack_start - unsafe_stack_guard;
- cur_stack->size = unsafe_stack_size + unsafe_stack_guard;
- cur_stack->pid = pid;
- cur_stack->tid = tid;
-
- pthread_mutex_lock(&thread_stacks_mutex);
- // Merge thread_stacks with the current thread's stack and any remaining
- // temp_stacks
- *stackp = thread_stacks;
- cur_stack->next = temp_stacks;
- thread_stacks = cur_stack;
- pthread_mutex_unlock(&thread_stacks_mutex);
-
- unsafe_stack_start = nullptr;
-}
-
-void EnsureInterceptorsInitialized();
-
-/// Intercept thread creation operation to allocate and setup the unsafe stack
-INTERCEPTOR(int, pthread_create, pthread_t *thread,
- const pthread_attr_t *attr,
- void *(*start_routine)(void*), void *arg) {
- EnsureInterceptorsInitialized();
- size_t size = 0;
- size_t guard = 0;
-
- if (attr) {
- pthread_attr_getstacksize(attr, &size);
- pthread_attr_getguardsize(attr, &guard);
- } else {
- // get pthread default stack size
- pthread_attr_t tmpattr;
- pthread_attr_init(&tmpattr);
- pthread_attr_getstacksize(&tmpattr, &size);
- pthread_attr_getguardsize(&tmpattr, &guard);
- pthread_attr_destroy(&tmpattr);
- }
-
- SFS_CHECK(size);
- size = RoundUpTo(size, kStackAlign);
-
- void *addr = unsafe_stack_alloc(size, guard);
- // Put tinfo at the end of the buffer. guard may be not page aligned.
- // If that is so then some bytes after addr can be mprotected.
- struct tinfo *tinfo =
- (struct tinfo *)(((char *)addr) + size - sizeof(struct tinfo));
- tinfo->start_routine = start_routine;
- tinfo->start_routine_arg = arg;
- tinfo->unsafe_stack_start = addr;
- tinfo->unsafe_stack_size = size;
- tinfo->unsafe_stack_guard = guard;
-
- return REAL(pthread_create)(thread, attr, thread_start, tinfo);
-}
-
-pthread_mutex_t interceptor_init_mutex = PTHREAD_MUTEX_INITIALIZER;
-bool interceptors_inited = false;
-
-void EnsureInterceptorsInitialized() {
- MutexLock lock(interceptor_init_mutex);
- if (interceptors_inited)
- return;
-
- // Initialize pthread interceptors for thread allocation
- INTERCEPT_FUNCTION(pthread_create);
-
- interceptors_inited = true;
-}
-
-} // namespace
-
-extern "C" __attribute__((visibility("default")))
-#if !SANITIZER_CAN_USE_PREINIT_ARRAY
-// On ELF platforms, the constructor is invoked using .preinit_array (see below)
-__attribute__((constructor(0)))
-#endif
-void __safestack_init() {
- // Determine the stack size for the main thread.
- size_t size = kDefaultUnsafeStackSize;
- size_t guard = 4096;
-
- struct rlimit limit;
- if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY)
- size = limit.rlim_cur;
-
- // Allocate unsafe stack for main thread
- void *addr = unsafe_stack_alloc(size, guard);
- unsafe_stack_setup(addr, size, guard);
-
- // Setup the cleanup handler
- pthread_key_create(&thread_cleanup_key, thread_cleanup_handler);
-}
-
-#if SANITIZER_CAN_USE_PREINIT_ARRAY
-// On ELF platforms, run safestack initialization before any other constructors.
-// On other platforms we use the constructor attribute to arrange to run our
-// initialization early.
-extern "C" {
-__attribute__((section(".preinit_array"),
- used)) void (*__safestack_preinit)(void) = __safestack_init;
-}
-#endif
-
-extern "C"
- __attribute__((visibility("default"))) void *__get_unsafe_stack_bottom() {
- return unsafe_stack_start;
-}
-
-extern "C"
- __attribute__((visibility("default"))) void *__get_unsafe_stack_top() {
- return (char*)unsafe_stack_start + unsafe_stack_size;
-}
-
-extern "C"
- __attribute__((visibility("default"))) void *__get_unsafe_stack_start() {
- return unsafe_stack_start;
-}
-
-extern "C"
- __attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {
- return __safestack_unsafe_stack_ptr;
-}
Copied: compiler-rt/trunk/lib/safestack/safestack.cpp (from r367559, compiler-rt/trunk/lib/safestack/safestack.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/safestack.cpp?p2=compiler-rt/trunk/lib/safestack/safestack.cpp&p1=compiler-rt/trunk/lib/safestack/safestack.cc&r1=367559&r2=367560&rev=367560&view=diff
==============================================================================
--- compiler-rt/trunk/lib/safestack/safestack.cc (original)
+++ compiler-rt/trunk/lib/safestack/safestack.cpp Thu Aug 1 06:56:52 2019
@@ -1,4 +1,4 @@
-//===-- safestack.cc ------------------------------------------------------===//
+//===-- safestack.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
More information about the llvm-commits
mailing list