| 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 "util/win/scoped_process_suspend.h" | 15 #include "util/win/scoped_process_suspend.h" |
| 16 | 16 |
| 17 #include <tlhelp32.h> | 17 #include <tlhelp32.h> |
| 18 | 18 |
| 19 #include <algorithm> | 19 #include <algorithm> |
| 20 #include <vector> | 20 #include <vector> |
| 21 | 21 |
| 22 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
| 23 #include "test/errors.h" |
| 23 #include "test/win/win_child_process.h" | 24 #include "test/win/win_child_process.h" |
| 25 #include "util/win/xp_compat.h" |
| 24 | 26 |
| 25 namespace crashpad { | 27 namespace crashpad { |
| 26 namespace test { | 28 namespace test { |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 // There is no per-process suspend count on Windows, only a per-thread suspend | 31 // There is no per-process suspend count on Windows, only a per-thread suspend |
| 30 // count. NtSuspendProcess just suspends all threads of a given process. So, | 32 // count. NtSuspendProcess just suspends all threads of a given process. So, |
| 31 // verify that all thread's suspend counts match the desired suspend count. | 33 // verify that all thread's suspend counts match the desired suspend count. |
| 32 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { | 34 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { |
| 33 DWORD process_id = GetProcessId(process); | 35 DWORD process_id = GetProcessId(process); |
| 34 | 36 |
| 35 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); | 37 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); |
| 36 if (!snapshot.is_valid()) | 38 if (!snapshot.is_valid()) { |
| 39 ADD_FAILURE() << ErrorMessage("CreateToolhelp32Snapshot"); |
| 37 return false; | 40 return false; |
| 41 } |
| 38 | 42 |
| 39 THREADENTRY32 te; | 43 THREADENTRY32 te; |
| 40 te.dwSize = sizeof(te); | 44 te.dwSize = sizeof(te); |
| 41 if (!Thread32First(snapshot.get(), &te)) | 45 |
| 46 BOOL ret = Thread32First(snapshot.get(), &te); |
| 47 if (!ret) { |
| 48 ADD_FAILURE() << ErrorMessage("Thread32First"); |
| 42 return false; | 49 return false; |
| 50 } |
| 43 do { | 51 do { |
| 44 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + | 52 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + |
| 45 sizeof(te.th32OwnerProcessID) && | 53 sizeof(te.th32OwnerProcessID) && |
| 46 te.th32OwnerProcessID == process_id) { | 54 te.th32OwnerProcessID == process_id) { |
| 47 ScopedKernelHANDLE thread( | 55 ScopedKernelHANDLE thread( |
| 48 OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID)); | 56 OpenThread(kXPThreadAllAccess, false, te.th32ThreadID)); |
| 57 EXPECT_TRUE(thread.is_valid()) << ErrorMessage("OpenThread"); |
| 49 DWORD result = SuspendThread(thread.get()); | 58 DWORD result = SuspendThread(thread.get()); |
| 50 EXPECT_NE(result, static_cast<DWORD>(-1)); | 59 EXPECT_NE(result, static_cast<DWORD>(-1)) |
| 51 if (result != static_cast<DWORD>(-1)) | 60 << ErrorMessage("SuspendThread"); |
| 52 ResumeThread(thread.get()); | 61 if (result != static_cast<DWORD>(-1)) { |
| 62 EXPECT_NE(ResumeThread(thread.get()), static_cast<DWORD>(-1)) |
| 63 << ErrorMessage("ResumeThread"); |
| 64 } |
| 53 if (result != desired_suspend_count) | 65 if (result != desired_suspend_count) |
| 54 return false; | 66 return false; |
| 55 } | 67 } |
| 56 te.dwSize = sizeof(te); | 68 te.dwSize = sizeof(te); |
| 57 } while (Thread32Next(snapshot.get(), &te)); | 69 } while (Thread32Next(snapshot.get(), &te)); |
| 58 | 70 |
| 59 return true; | 71 return true; |
| 60 } | 72 } |
| 61 | 73 |
| 62 class ScopedProcessSuspendTest final : public WinChildProcess { | 74 class ScopedProcessSuspendTest final : public WinChildProcess { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); | 109 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); |
| 98 | 110 |
| 99 // Tell the child it's OK to terminate. | 111 // Tell the child it's OK to terminate. |
| 100 char c = ' '; | 112 char c = ' '; |
| 101 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); | 113 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); |
| 102 } | 114 } |
| 103 | 115 |
| 104 } // namespace | 116 } // namespace |
| 105 } // namespace test | 117 } // namespace test |
| 106 } // namespace crashpad | 118 } // namespace crashpad |
| OLD | NEW |