Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: util/win/process_info_test.cc

Issue 1370063005: MEM_RESERVE regions are not accessible by ReadProcessMemory() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: add real-world test Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/win/process_info.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/process_info.h" 15 #include "util/win/process_info.h"
16 16
17 #include <imagehlp.h> 17 #include <imagehlp.h>
18 #include <intrin.h> 18 #include <intrin.h>
19 #include <wchar.h> 19 #include <wchar.h>
20 20
21 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
22 #include "base/memory/scoped_ptr.h"
22 #include "build/build_config.h" 23 #include "build/build_config.h"
23 #include "gtest/gtest.h" 24 #include "gtest/gtest.h"
24 #include "test/paths.h" 25 #include "test/paths.h"
25 #include "test/win/child_launcher.h" 26 #include "test/win/child_launcher.h"
26 #include "util/file/file_io.h" 27 #include "util/file/file_io.h"
27 #include "util/misc/uuid.h" 28 #include "util/misc/uuid.h"
28 #include "util/win/scoped_handle.h" 29 #include "util/win/scoped_handle.h"
29 30
30 namespace crashpad { 31 namespace crashpad {
31 namespace test { 32 namespace test {
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 273
273 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result = 274 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result =
274 GetReadableRangesOfMemoryMap(CheckedRange<WinVMAddress, WinVMSize>(5, 10), 275 GetReadableRangesOfMemoryMap(CheckedRange<WinVMAddress, WinVMSize>(5, 10),
275 memory_info); 276 memory_info);
276 277
277 ASSERT_EQ(result.size(), 1u); 278 ASSERT_EQ(result.size(), 1u);
278 EXPECT_EQ(10, result[0].base()); 279 EXPECT_EQ(10, result[0].base());
279 EXPECT_EQ(5, result[0].size()); 280 EXPECT_EQ(5, result[0].size());
280 } 281 }
281 282
283 TEST(ProcessInfo, ReserveIsInaccessible) {
284 std::vector<ProcessInfo::MemoryInfo> memory_info;
285 MEMORY_BASIC_INFORMATION mbi = {0};
286
287 mbi.BaseAddress = 0;
288 mbi.RegionSize = 10;
289 mbi.State = MEM_RESERVE;
290 memory_info.push_back(ProcessInfo::MemoryInfo(mbi));
291
292 mbi.BaseAddress = reinterpret_cast<void*>(10);
293 mbi.RegionSize = 20;
294 mbi.State = MEM_COMMIT;
295 memory_info.push_back(ProcessInfo::MemoryInfo(mbi));
296
297 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result =
298 GetReadableRangesOfMemoryMap(CheckedRange<WinVMAddress, WinVMSize>(5, 10),
299 memory_info);
300
301 ASSERT_EQ(result.size(), 1u);
302 EXPECT_EQ(10, result[0].base());
303 EXPECT_EQ(5, result[0].size());
304 }
305
282 TEST(ProcessInfo, AccessibleRangesCoalesced) { 306 TEST(ProcessInfo, AccessibleRangesCoalesced) {
283 std::vector<ProcessInfo::MemoryInfo> memory_info; 307 std::vector<ProcessInfo::MemoryInfo> memory_info;
284 MEMORY_BASIC_INFORMATION mbi = {0}; 308 MEMORY_BASIC_INFORMATION mbi = {0};
285 309
286 mbi.BaseAddress = 0; 310 mbi.BaseAddress = 0;
287 mbi.RegionSize = 10; 311 mbi.RegionSize = 10;
288 mbi.State = MEM_FREE; 312 mbi.State = MEM_FREE;
289 memory_info.push_back(ProcessInfo::MemoryInfo(mbi)); 313 memory_info.push_back(ProcessInfo::MemoryInfo(mbi));
290 314
291 mbi.BaseAddress = reinterpret_cast<void*>(10); 315 mbi.BaseAddress = reinterpret_cast<void*>(10);
292 mbi.RegionSize = 2; 316 mbi.RegionSize = 2;
293 mbi.State = MEM_COMMIT; 317 mbi.State = MEM_COMMIT;
294 memory_info.push_back(ProcessInfo::MemoryInfo(mbi)); 318 memory_info.push_back(ProcessInfo::MemoryInfo(mbi));
295 319
296 mbi.BaseAddress = reinterpret_cast<void*>(12); 320 mbi.BaseAddress = reinterpret_cast<void*>(12);
297 mbi.RegionSize = 5; 321 mbi.RegionSize = 5;
298 mbi.State = MEM_RESERVE; 322 mbi.State = MEM_COMMIT;
299 memory_info.push_back(ProcessInfo::MemoryInfo(mbi)); 323 memory_info.push_back(ProcessInfo::MemoryInfo(mbi));
300 324
301 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result = 325 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result =
302 GetReadableRangesOfMemoryMap(CheckedRange<WinVMAddress, WinVMSize>(11, 4), 326 GetReadableRangesOfMemoryMap(CheckedRange<WinVMAddress, WinVMSize>(11, 4),
303 memory_info); 327 memory_info);
304 328
305 ASSERT_EQ(result.size(), 1u); 329 ASSERT_EQ(result.size(), 1u);
306 EXPECT_EQ(11, result[0].base()); 330 EXPECT_EQ(11, result[0].base());
307 EXPECT_EQ(4, result[0].size()); 331 EXPECT_EQ(4, result[0].size());
308 } 332 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 390
367 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result = 391 std::vector<CheckedRange<WinVMAddress, WinVMSize>> result =
368 GetReadableRangesOfMemoryMap( 392 GetReadableRangesOfMemoryMap(
369 CheckedRange<WinVMAddress, WinVMSize>(15, 100), memory_info); 393 CheckedRange<WinVMAddress, WinVMSize>(15, 100), memory_info);
370 394
371 ASSERT_EQ(result.size(), 1u); 395 ASSERT_EQ(result.size(), 1u);
372 EXPECT_EQ(15, result[0].base()); 396 EXPECT_EQ(15, result[0].base());
373 EXPECT_EQ(5, result[0].size()); 397 EXPECT_EQ(5, result[0].size());
374 } 398 }
375 399
400 TEST(ProcessInfo, ReadableRanges) {
401 ProcessInfo info;
Mark Mentovai 2015/10/01 21:43:52 Don’t declare this until you use it below.
scottmg 2015/10/01 22:03:49 Done.
402
403 SYSTEM_INFO system_info;
404 GetSystemInfo(&system_info);
405
406 const size_t kBlockSize = system_info.dwPageSize;
407
408 // Allocate 6 pages, and then commit the second, fourth, and fifth, so we have
409 // a setup like this:
410 // 0 1 2 3 4 5
411 // +-----------------------------------------------+
412 // | ????? | | ????? | | | ????? |
Mark Mentovai 2015/10/01 21:43:52 Maybe page[2] should be PAGE_NOACCESS to test that
scottmg 2015/10/01 22:03:49 Sadly, I screwed that three line function up when
Mark Mentovai 2015/10/01 22:22:28 scottmg wrote:
413 // +-----------------------------------------------+
414 void* reserve_region =
415 VirtualAlloc(nullptr, kBlockSize * 6, MEM_RESERVE, PAGE_READWRITE);
416 ASSERT_TRUE(reserve_region);
417 uintptr_t reserved_as_int = reinterpret_cast<uintptr_t>(reserve_region);
418 void* readable1 =
419 VirtualAlloc(reinterpret_cast<void*>(reserved_as_int + kBlockSize),
420 kBlockSize,
421 MEM_COMMIT,
422 PAGE_READWRITE);
423 ASSERT_TRUE(readable1);
424 void* readable2 =
425 VirtualAlloc(reinterpret_cast<void*>(reserved_as_int + (kBlockSize * 3)),
426 kBlockSize * 2,
427 MEM_COMMIT,
428 PAGE_READWRITE);
429 ASSERT_TRUE(readable2);
430
431 info.Initialize(GetCurrentProcess());
Mark Mentovai 2015/10/01 21:43:52 You call GetCurrentProcess() five times in this te
scottmg 2015/10/01 22:03:49 Sure, it's just "return -1", but I guess it means
432 auto ranges = info.GetReadableRanges(
433 CheckedRange<WinVMAddress, WinVMSize>(reserved_as_int, kBlockSize * 6));
434
435 ASSERT_EQ(ranges.size(), 2u);
Mark Mentovai 2015/10/01 21:43:52 Order as (2u, ranges.size()).
scottmg 2015/10/01 22:03:49 Done.
436 EXPECT_EQ(reserved_as_int + kBlockSize, ranges[0].base());
437 EXPECT_EQ(kBlockSize, ranges[0].size());
438 EXPECT_EQ(reserved_as_int + (kBlockSize * 3), ranges[1].base());
439 EXPECT_EQ(kBlockSize * 2, ranges[1].size());
440
441 // Also make sure what we think we can read corresponds with what we can
442 // actually read.
443 scoped_ptr<unsigned char[]> into(new unsigned char[kBlockSize * 6]);
444 SIZE_T bytes_read;
445
446 EXPECT_TRUE(ReadProcessMemory(
447 GetCurrentProcess(), readable1, into.get(), kBlockSize, &bytes_read));
448 EXPECT_EQ(kBlockSize, bytes_read);
449
450 EXPECT_TRUE(ReadProcessMemory(
451 GetCurrentProcess(), readable2, into.get(), kBlockSize * 2, &bytes_read));
452 EXPECT_EQ(kBlockSize * 2, bytes_read);
453
454 EXPECT_FALSE(ReadProcessMemory(GetCurrentProcess(),
455 reserve_region,
456 into.get(),
457 kBlockSize,
458 &bytes_read));
459 EXPECT_FALSE(ReadProcessMemory(GetCurrentProcess(),
460 reserve_region,
461 into.get(),
462 kBlockSize * 6,
463 &bytes_read));
464 }
465
376 } // namespace 466 } // namespace
377 } // namespace test 467 } // namespace test
378 } // namespace crashpad 468 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/win/process_info.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698