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 =
55       sizeof(PSAPI_WORKING_SET_INFORMATION) +
56       (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
57 
58     buffer.resize(buffer_size);
59     ws_info = reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>(&buffer[0]);
60 
61     // On success, |buffer_| is populated with info about the working set of
62     // |process|. On ERROR_BAD_LENGTH failure, increase the size of the
63     // buffer and try again.
64     if (QueryWorkingSet(GetCurrentProcess(), &buffer[0], buffer_size))
65       break;  // Success
66 
67     PERFETTO_CHECK(GetLastError() == ERROR_BAD_LENGTH);
68 
69     number_of_entries = ws_info->NumberOfEntries;
70 
71     // Maybe some entries are being added right now. Increase the buffer to
72     // take that into account. Increasing by 10% should generally be enough.
73     number_of_entries = static_cast<size_t>(double(number_of_entries) * 1.1);
74 
75     PERFETTO_CHECK(--retries > 0);  // If we're looping, eventually fail.
76   }
77 
78   void* end = reinterpret_cast<char*>(start) + size;
79   // Now scan the working-set information looking for the addresses.
80   unsigned pages_found = 0;
81   for (unsigned i = 0; i < ws_info->NumberOfEntries; ++i) {
82     void* address = reinterpret_cast<void*>(
83         ws_info->WorkingSetInfo[i].VirtualPage * page_size);
84     if (address >= start && address < end)
85       ++pages_found;
86   }
87 
88   if (pages_found * page_size == size)
89     return true;
90   return false;
91 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
92   // Fuchsia doesn't yet support paging (b/119503290).
93   ignore_result(page_size);
94   return true;
95 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
96   // mincore isn't available on NaCL.
97   ignore_result(page_size);
98   return true;
99 #else
100 #if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
101   using PageState = char;
102   static constexpr PageState kIncoreMask = MINCORE_INCORE;
103 #else
104   using PageState = unsigned char;
105   static constexpr PageState kIncoreMask = 1;
106 #endif
107   const size_t num_pages = (size + page_size - 1) / page_size;
108   std::unique_ptr<PageState[]> page_states(new PageState[num_pages]);
109   memset(page_states.get(), 0, num_pages * sizeof(PageState));
110   int res = mincore(start, size, page_states.get());
111   // Linux returns ENOMEM when an unmapped memory range is passed.
112   // MacOS instead returns 0 but leaves the page_states empty.
113   if (res == -1 && errno == ENOMEM)
114     return false;
115   PERFETTO_CHECK(res == 0);
116   for (size_t i = 0; i < num_pages; i++) {
117     if (!(page_states[i] & kIncoreMask))
118       return false;
119   }
120   return true;
121 #endif
122 }
123 
124 }  // namespace vm_test_utils
125 }  // namespace base
126 }  // namespace perfetto
127