OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include <windows.h> | 15 #include <windows.h> |
16 #include <winternl.h> | 16 #include <winternl.h> |
17 | 17 |
18 // ntstatus.h conflicts with windows.h so define this locally. | 18 // ntstatus.h conflicts with windows.h so define this locally. |
19 #ifndef STATUS_NO_SUCH_FILE | 19 #ifndef STATUS_NO_SUCH_FILE |
20 #define STATUS_NO_SUCH_FILE static_cast<NTSTATUS>(0xC000000F) | 20 #define STATUS_NO_SUCH_FILE static_cast<NTSTATUS>(0xC000000F) |
21 #endif | 21 #endif |
22 | 22 |
| 23 #include "base/basictypes.h" |
23 #include "base/logging.h" | 24 #include "base/logging.h" |
24 #include "client/crashpad_client.h" | 25 #include "client/crashpad_client.h" |
25 #include "tools/tool_support.h" | 26 #include "tools/tool_support.h" |
26 | 27 |
27 namespace crashpad { | 28 namespace crashpad { |
28 namespace { | 29 namespace { |
29 | 30 |
30 ULONG RtlNtStatusToDosError(NTSTATUS status) { | 31 ULONG RtlNtStatusToDosError(NTSTATUS status) { |
31 static decltype(::RtlNtStatusToDosError)* rtl_nt_status_to_dos_error = | 32 static decltype(::RtlNtStatusToDosError)* rtl_nt_status_to_dos_error = |
32 reinterpret_cast<decltype(::RtlNtStatusToDosError)*>( | 33 reinterpret_cast<decltype(::RtlNtStatusToDosError)*>( |
33 GetProcAddress(LoadLibrary(L"ntdll.dll"), "RtlNtStatusToDosError")); | 34 GetProcAddress(LoadLibrary(L"ntdll.dll"), "RtlNtStatusToDosError")); |
34 DCHECK(rtl_nt_status_to_dos_error); | 35 DCHECK(rtl_nt_status_to_dos_error); |
35 return rtl_nt_status_to_dos_error(status); | 36 return rtl_nt_status_to_dos_error(status); |
36 } | 37 } |
37 | 38 |
| 39 void AllocateMemoryOfVariousProtections() { |
| 40 SYSTEM_INFO system_info; |
| 41 GetSystemInfo(&system_info); |
| 42 |
| 43 const size_t kPageSize = system_info.dwPageSize; |
| 44 |
| 45 const uint32_t kPageTypes[] = { |
| 46 PAGE_NOACCESS, |
| 47 PAGE_READONLY, |
| 48 PAGE_READWRITE, |
| 49 PAGE_EXECUTE, |
| 50 PAGE_EXECUTE_READ, |
| 51 PAGE_EXECUTE_READWRITE, |
| 52 |
| 53 // PAGE_NOACCESS is invalid with PAGE_GUARD. |
| 54 PAGE_READONLY | PAGE_GUARD, |
| 55 PAGE_READWRITE | PAGE_GUARD, |
| 56 PAGE_EXECUTE | PAGE_GUARD, |
| 57 PAGE_EXECUTE_READ | PAGE_GUARD, |
| 58 PAGE_EXECUTE_READWRITE | PAGE_GUARD, |
| 59 }; |
| 60 |
| 61 // All of these allocations are leaked, we want to view them in windbg via |
| 62 // !vprot. |
| 63 void* reserve = VirtualAlloc( |
| 64 nullptr, arraysize(kPageTypes) * kPageSize, MEM_RESERVE, PAGE_READWRITE); |
| 65 PCHECK(reserve) << "VirtualAlloc MEM_RESERVE"; |
| 66 uintptr_t reserve_as_int = reinterpret_cast<uintptr_t>(reserve); |
| 67 |
| 68 for (size_t i = 0; i < arraysize(kPageTypes); ++i) { |
| 69 void* result = |
| 70 VirtualAlloc(reinterpret_cast<void*>(reserve_as_int + (kPageSize * i)), |
| 71 kPageSize, |
| 72 MEM_COMMIT, |
| 73 kPageTypes[i]); |
| 74 PCHECK(result) << "VirtualAlloc MEM_COMMIT " << i; |
| 75 } |
| 76 } |
| 77 |
38 void SomeCrashyFunction() { | 78 void SomeCrashyFunction() { |
39 // SetLastError and NTSTATUS so that we have something to view in !gle in | 79 // SetLastError and NTSTATUS so that we have something to view in !gle in |
40 // windbg. RtlNtStatusToDosError() stores STATUS_NO_SUCH_FILE into the | 80 // windbg. RtlNtStatusToDosError() stores STATUS_NO_SUCH_FILE into the |
41 // LastStatusError of the TEB as a side-effect, and we'll be setting | 81 // LastStatusError of the TEB as a side-effect, and we'll be setting |
42 // ERROR_FILE_NOT_FOUND for GetLastError(). | 82 // ERROR_FILE_NOT_FOUND for GetLastError(). |
43 SetLastError(RtlNtStatusToDosError(STATUS_NO_SUCH_FILE)); | 83 SetLastError(RtlNtStatusToDosError(STATUS_NO_SUCH_FILE)); |
44 volatile int* foo = reinterpret_cast<volatile int*>(7); | 84 volatile int* foo = reinterpret_cast<volatile int*>(7); |
45 *foo = 42; | 85 *foo = 42; |
46 } | 86 } |
47 | 87 |
48 int CrashyMain(int argc, char* argv[]) { | 88 int CrashyMain(int argc, char* argv[]) { |
49 if (argc != 2) { | 89 if (argc != 2) { |
50 fprintf(stderr, "Usage: %s <server_pipe_name>\n", argv[0]); | 90 fprintf(stderr, "Usage: %s <server_pipe_name>\n", argv[0]); |
51 return 1; | 91 return 1; |
52 } | 92 } |
53 | 93 |
54 CrashpadClient client; | 94 CrashpadClient client; |
55 if (!client.SetHandler(argv[1])) { | 95 if (!client.SetHandler(argv[1])) { |
56 LOG(ERROR) << "SetHandler"; | 96 LOG(ERROR) << "SetHandler"; |
57 return 1; | 97 return 1; |
58 } | 98 } |
59 if (!client.UseHandler()) { | 99 if (!client.UseHandler()) { |
60 LOG(ERROR) << "UseHandler"; | 100 LOG(ERROR) << "UseHandler"; |
61 return 1; | 101 return 1; |
62 } | 102 } |
63 | 103 |
| 104 AllocateMemoryOfVariousProtections(); |
| 105 |
64 SomeCrashyFunction(); | 106 SomeCrashyFunction(); |
65 | 107 |
66 return 0; | 108 return 0; |
67 } | 109 } |
68 | 110 |
69 } // namespace | 111 } // namespace |
70 } // namespace crashpad | 112 } // namespace crashpad |
71 | 113 |
72 int wmain(int argc, wchar_t* argv[]) { | 114 int wmain(int argc, wchar_t* argv[]) { |
73 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::CrashyMain); | 115 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::CrashyMain); |
74 } | 116 } |
OLD | NEW |