Chromium Code Reviews| 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/win/win_child_process.h" | 23 #include "test/win/win_child_process.h" |
| 24 #include "util/win/xp_compat.h" | |
| 24 | 25 |
| 25 namespace crashpad { | 26 namespace crashpad { |
| 26 namespace test { | 27 namespace test { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // There is no per-process suspend count on Windows, only a per-thread suspend | 30 // 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, | 31 // count. NtSuspendProcess just suspends all threads of a given process. So, |
| 31 // verify that all thread's suspend counts match the desired suspend count. | 32 // verify that all thread's suspend counts match the desired suspend count. |
| 32 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { | 33 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { |
| 33 DWORD process_id = GetProcessId(process); | 34 DWORD process_id = GetProcessId(process); |
| 34 | 35 |
| 35 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); | 36 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); |
| 36 if (!snapshot.is_valid()) | 37 if (!snapshot.is_valid()) |
| 37 return false; | 38 return false; |
| 38 | 39 |
| 39 THREADENTRY32 te; | 40 THREADENTRY32 te; |
| 40 te.dwSize = sizeof(te); | 41 te.dwSize = sizeof(te); |
| 41 if (!Thread32First(snapshot.get(), &te)) | 42 if (!Thread32First(snapshot.get(), &te)) |
| 42 return false; | 43 return false; |
| 43 do { | 44 do { |
| 44 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + | 45 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + |
| 45 sizeof(te.th32OwnerProcessID) && | 46 sizeof(te.th32OwnerProcessID) && |
| 46 te.th32OwnerProcessID == process_id) { | 47 te.th32OwnerProcessID == process_id) { |
| 47 ScopedKernelHANDLE thread( | 48 ScopedKernelHANDLE thread( |
| 48 OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID)); | 49 OpenThread(kXPThreadAllAccess, false, te.th32ThreadID)); |
| 50 EXPECT_TRUE(thread.is_valid()); | |
|
Mark Mentovai
2015/09/11 23:03:33
ASSERT_TRUE()? Otherwise everything that follows i
scottmg
2015/09/11 23:32:52
Yeah, I just hate ASSERT because there's no "retur
Mark Mentovai
2015/09/11 23:36:28
scottmg wrote:
| |
| 49 DWORD result = SuspendThread(thread.get()); | 51 DWORD result = SuspendThread(thread.get()); |
| 50 EXPECT_NE(result, static_cast<DWORD>(-1)); | 52 EXPECT_NE(result, static_cast<DWORD>(-1)); |
| 51 if (result != static_cast<DWORD>(-1)) | 53 if (result != static_cast<DWORD>(-1)) |
| 52 ResumeThread(thread.get()); | 54 ResumeThread(thread.get()); |
|
Mark Mentovai
2015/09/11 23:03:33
Let’s harden this a bit more by EXPECT_TRUE()ing t
scottmg
2015/09/11 23:32:52
Done (_NE -1).
Mark Mentovai
2015/09/11 23:36:28
scottmg wrote:
scottmg
2015/09/11 23:43:46
Done.
| |
| 53 if (result != desired_suspend_count) | 55 if (result != desired_suspend_count) |
| 54 return false; | 56 return false; |
| 55 } | 57 } |
| 56 te.dwSize = sizeof(te); | 58 te.dwSize = sizeof(te); |
| 57 } while (Thread32Next(snapshot.get(), &te)); | 59 } while (Thread32Next(snapshot.get(), &te)); |
| 58 | 60 |
| 59 return true; | 61 return true; |
| 60 } | 62 } |
| 61 | 63 |
| 62 class ScopedProcessSuspendTest final : public WinChildProcess { | 64 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)); | 99 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); |
| 98 | 100 |
| 99 // Tell the child it's OK to terminate. | 101 // Tell the child it's OK to terminate. |
| 100 char c = ' '; | 102 char c = ' '; |
| 101 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); | 103 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); |
| 102 } | 104 } |
| 103 | 105 |
| 104 } // namespace | 106 } // namespace |
| 105 } // namespace test | 107 } // namespace test |
| 106 } // namespace crashpad | 108 } // namespace crashpad |
| OLD | NEW |