1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/base/test/vm_test_utils.h"
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/ext/base/utils.h"
21
22 #include <memory>
23
24 #include <errno.h>
25 #include <string.h>
26
27 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
28 #include <vector>
29
30 #include <Windows.h>
31 #include <Psapi.h>
32 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
36
37 #include "perfetto/base/build_config.h"
38 #include "perfetto/base/logging.h"
39 #include "perfetto/ext/base/utils.h"
40
41 namespace perfetto {
42 namespace base {
43 namespace vm_test_utils {
44
IsMapped(void * start,size_t size)45 bool IsMapped(void* start, size_t size) {
46 const size_t page_size = GetSysPageSize();
47 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
48 int retries = 5;
49 size_t number_of_entries = 4000; // Just a guess.
50 PSAPI_WORKING_SET_INFORMATION* ws_info = nullptr;
51
52 std::vector<char> buffer;
53 for (;;) {
54 size_t buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) +
55 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
56
57 buffer.resize(buffer_size);
58 ws_info = reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>(&buffer[0]);
59
60 // On success, |buffer_| is populated with info about the working set of
61 // |process|. On ERROR_BAD_LENGTH failure, increase the size of the
62 // buffer and try again.
63 if (QueryWorkingSet(GetCurrentProcess(), &buffer[0], buffer_size))
64 break; // Success
65
66 PERFETTO_CHECK(GetLastError() == ERROR_BAD_LENGTH);
67
68 number_of_entries = ws_info->NumberOfEntries;
69
70 // Maybe some entries are being added right now. Increase the buffer to
71 // take that into account. Increasing by 10% should generally be enough.
72 number_of_entries = static_cast<size_t>(double(number_of_entries) * 1.1);
73
74 PERFETTO_CHECK(--retries > 0); // If we're looping, eventually fail.
75 }
76
77 void* end = reinterpret_cast<char*>(start) + size;
78 // Now scan the working-set information looking for the addresses.
79 unsigned pages_found = 0;
80 for (unsigned i = 0; i < ws_info->NumberOfEntries; ++i) {
81 void* address = reinterpret_cast<void*>(
82 ws_info->WorkingSetInfo[i].VirtualPage * page_size);
83 if (address >= start && address < end)
84 ++pages_found;
85 }
86
87 if (pages_found * page_size == size)
88 return true;
89 return false;
90 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
91 // Fuchsia doesn't yet support paging (b/119503290).
92 ignore_result(page_size);
93 return true;
94 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
95 // mincore isn't available on NaCL.
96 ignore_result(page_size);
97 return true;
98 #else
99 #if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
100 using PageState = char;
101 static constexpr PageState kIncoreMask = MINCORE_INCORE;
102 #else
103 using PageState = unsigned char;
104 static constexpr PageState kIncoreMask = 1;
105 #endif
106 const size_t num_pages = (size + page_size - 1) / page_size;
107 std::unique_ptr<PageState[]> page_states(new PageState[num_pages]);
108 memset(page_states.get(), 0, num_pages * sizeof(PageState));
109 int res = mincore(start, size, page_states.get());
110 // Linux returns ENOMEM when an unmapped memory range is passed.
111 // MacOS instead returns 0 but leaves the page_states empty.
112 if (res == -1 && errno == ENOMEM)
113 return false;
114 PERFETTO_CHECK(res == 0);
115 for (size_t i = 0; i < num_pages; i++) {
116 if (!(page_states[i] & kIncoreMask))
117 return false;
118 }
119 return true;
120 #endif
121 }
122
123 } // namespace vm_test_utils
124 } // namespace base
125 } // namespace perfetto
126