OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (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 | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/win/scoped_process_suspend.h" | |
16 | |
17 #include <tlhelp32.h> | |
18 | |
19 #include <algorithm> | |
20 #include <vector> | |
21 | |
22 #include "gtest/gtest.h" | |
23 #include "test/win/win_child_process.h" | |
24 | |
25 namespace crashpad { | |
26 namespace test { | |
27 namespace { | |
28 | |
29 // 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 // verify that all thread's suspend counts match the desired suspend count. | |
32 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { | |
33 DWORD process_id = GetProcessId(process); | |
34 | |
35 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); | |
36 if (!snapshot.is_valid()) | |
37 return false; | |
38 | |
39 THREADENTRY32 te; | |
40 te.dwSize = sizeof(te); | |
41 if (!Thread32First(snapshot.get(), &te)) | |
42 return false; | |
43 do { | |
44 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + | |
45 sizeof(te.th32OwnerProcessID) && | |
46 te.th32OwnerProcessID == process_id) { | |
47 ScopedKernelHANDLE thread( | |
48 OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID)); | |
49 DWORD result = SuspendThread(thread.get()); | |
50 EXPECT_NE(result, 0xffffffff); | |
51 if (result >= 0) | |
Mark Mentovai
2015/09/05 00:15:43
result is a DWORD which is an unsigned long, right
scottmg
2015/09/08 16:06:43
Good catch, thanks. Done.
| |
52 ResumeThread(thread.get()); | |
53 if (result != desired_suspend_count) | |
54 return false; | |
55 } | |
56 te.dwSize = sizeof(te); | |
57 } while (Thread32Next(snapshot.get(), &te)); | |
58 | |
59 return true; | |
60 } | |
61 | |
62 class ScopedProcessSuspendTest final : public WinChildProcess { | |
63 public: | |
64 ScopedProcessSuspendTest() : WinChildProcess() {} | |
65 ~ScopedProcessSuspendTest() {} | |
66 | |
67 private: | |
68 int Run() override { | |
69 char c; | |
70 // Wait for notification from parent. | |
71 EXPECT_TRUE(LoggingReadFile(ReadPipeHandle(), &c, sizeof(c))); | |
72 EXPECT_EQ(' ', c); | |
73 return EXIT_SUCCESS; | |
74 } | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(ScopedProcessSuspendTest); | |
77 }; | |
78 | |
79 TEST(ScopedProcessSuspend, ScopedProcessSuspend) { | |
80 WinChildProcess::EntryPoint<ScopedProcessSuspendTest>(); | |
81 scoped_ptr<WinChildProcess::Handles> handles = WinChildProcess::Launch(); | |
82 | |
83 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); | |
84 | |
85 { | |
86 ScopedProcessSuspend suspend(handles->process.get()); | |
87 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 1)); | |
88 | |
89 { | |
90 ScopedProcessSuspend suspend(handles->process.get()); | |
91 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 2)); | |
92 } | |
93 | |
94 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 1)); | |
95 } | |
96 | |
97 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); | |
98 | |
99 // Tell the child it's OK to terminate. | |
100 char c = ' '; | |
101 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); | |
102 } | |
103 | |
104 } // namespace | |
105 } // namespace test | |
106 } // namespace crashpad | |
OLD | NEW |