OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/mac/process_types.h" |
| 16 |
| 17 #include <AvailabilityMacros.h> |
| 18 #include <mach/mach.h> |
| 19 #include <string.h> |
| 20 |
| 21 #include <vector> |
| 22 |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "gtest/gtest.h" |
| 25 #include "util/mac/mac_util.h" |
| 26 |
| 27 extern "C" { |
| 28 const struct dyld_all_image_infos* _dyld_get_all_image_infos(); |
| 29 |
| 30 } // extern "C" |
| 31 |
| 32 namespace { |
| 33 |
| 34 using namespace crashpad; |
| 35 |
| 36 #define TEST_STRING(process_reader, self_view, proctype_view, field) \ |
| 37 do { \ |
| 38 if (self_view->field) { \ |
| 39 std::string proctype_string; \ |
| 40 ASSERT_TRUE(process_reader.Memory()->ReadCString(proctype_view.field, \ |
| 41 &proctype_string)); \ |
| 42 EXPECT_EQ(self_view->field, proctype_string); \ |
| 43 } \ |
| 44 } while (false) |
| 45 |
| 46 TEST(ProcessTypes, DyldImagesSelf) { |
| 47 // Get the in-process view of dyld_all_image_infos, and check it for sanity. |
| 48 const struct dyld_all_image_infos* self_image_infos = |
| 49 _dyld_get_all_image_infos(); |
| 50 int mac_os_x_minor_version = MacOSXMinorVersion(); |
| 51 if (mac_os_x_minor_version >= 9) { |
| 52 EXPECT_GE(self_image_infos->version, 13u); |
| 53 } else if (mac_os_x_minor_version >= 7) { |
| 54 EXPECT_GE(self_image_infos->version, 8u); |
| 55 } else if (mac_os_x_minor_version >= 6) { |
| 56 EXPECT_GE(self_image_infos->version, 2u); |
| 57 } else { |
| 58 EXPECT_GE(self_image_infos->version, 1u); |
| 59 } |
| 60 |
| 61 EXPECT_GT(self_image_infos->infoArrayCount, 1u); |
| 62 if (self_image_infos->version >= 2) { |
| 63 EXPECT_TRUE(self_image_infos->libSystemInitialized); |
| 64 } |
| 65 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 |
| 66 if (self_image_infos->version >= 9) { |
| 67 EXPECT_EQ(self_image_infos, self_image_infos->dyldAllImageInfosAddress); |
| 68 } |
| 69 #endif |
| 70 |
| 71 // Get the out-of-process view of dyld_all_image_infos, and work with it |
| 72 // through the process_types interface. |
| 73 task_dyld_info_data_t dyld_info; |
| 74 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; |
| 75 kern_return_t kr = task_info(mach_task_self(), |
| 76 TASK_DYLD_INFO, |
| 77 reinterpret_cast<task_info_t>(&dyld_info), |
| 78 &count); |
| 79 ASSERT_EQ(KERN_SUCCESS, kr); |
| 80 |
| 81 EXPECT_EQ(reinterpret_cast<mach_vm_address_t>(self_image_infos), |
| 82 dyld_info.all_image_info_addr); |
| 83 EXPECT_GT(dyld_info.all_image_info_size, 1u); |
| 84 |
| 85 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 |
| 86 #if !defined(ARCH_CPU_64_BITS) |
| 87 EXPECT_EQ(TASK_DYLD_ALL_IMAGE_INFO_32, dyld_info.all_image_info_format); |
| 88 #else |
| 89 EXPECT_EQ(TASK_DYLD_ALL_IMAGE_INFO_64, dyld_info.all_image_info_format); |
| 90 #endif |
| 91 #endif |
| 92 |
| 93 ProcessReader process_reader; |
| 94 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); |
| 95 |
| 96 process_types::dyld_all_image_infos proctype_image_infos; |
| 97 ASSERT_TRUE(proctype_image_infos.Read(&process_reader, |
| 98 dyld_info.all_image_info_addr)); |
| 99 |
| 100 ASSERT_EQ(self_image_infos->version, proctype_image_infos.version); |
| 101 |
| 102 if (proctype_image_infos.version >= 1) { |
| 103 EXPECT_EQ(self_image_infos->infoArrayCount, |
| 104 proctype_image_infos.infoArrayCount); |
| 105 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->infoArray), |
| 106 proctype_image_infos.infoArray); |
| 107 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->notification), |
| 108 proctype_image_infos.notification); |
| 109 EXPECT_EQ(self_image_infos->processDetachedFromSharedRegion, |
| 110 proctype_image_infos.processDetachedFromSharedRegion); |
| 111 } |
| 112 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 |
| 113 if (proctype_image_infos.version >= 2) { |
| 114 EXPECT_EQ(self_image_infos->libSystemInitialized, |
| 115 proctype_image_infos.libSystemInitialized); |
| 116 EXPECT_EQ( |
| 117 reinterpret_cast<uint64_t>(self_image_infos->dyldImageLoadAddress), |
| 118 proctype_image_infos.dyldImageLoadAddress); |
| 119 } |
| 120 if (proctype_image_infos.version >= 3) { |
| 121 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->jitInfo), |
| 122 proctype_image_infos.jitInfo); |
| 123 } |
| 124 if (proctype_image_infos.version >= 5) { |
| 125 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->dyldVersion), |
| 126 proctype_image_infos.dyldVersion); |
| 127 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->errorMessage), |
| 128 proctype_image_infos.errorMessage); |
| 129 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->terminationFlags), |
| 130 proctype_image_infos.terminationFlags); |
| 131 |
| 132 TEST_STRING( |
| 133 process_reader, self_image_infos, proctype_image_infos, dyldVersion); |
| 134 TEST_STRING( |
| 135 process_reader, self_image_infos, proctype_image_infos, errorMessage); |
| 136 } |
| 137 if (proctype_image_infos.version >= 6) { |
| 138 EXPECT_EQ( |
| 139 reinterpret_cast<uint64_t>(self_image_infos->coreSymbolicationShmPage), |
| 140 proctype_image_infos.coreSymbolicationShmPage); |
| 141 } |
| 142 if (proctype_image_infos.version >= 7) { |
| 143 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->systemOrderFlag), |
| 144 proctype_image_infos.systemOrderFlag); |
| 145 } |
| 146 #endif |
| 147 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 |
| 148 if (proctype_image_infos.version >= 8) { |
| 149 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->uuidArrayCount), |
| 150 proctype_image_infos.uuidArrayCount); |
| 151 } |
| 152 if (proctype_image_infos.version >= 9) { |
| 153 EXPECT_EQ( |
| 154 reinterpret_cast<uint64_t>(self_image_infos->dyldAllImageInfosAddress), |
| 155 proctype_image_infos.dyldAllImageInfosAddress); |
| 156 } |
| 157 if (proctype_image_infos.version >= 10) { |
| 158 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->initialImageCount), |
| 159 proctype_image_infos.initialImageCount); |
| 160 } |
| 161 if (proctype_image_infos.version >= 11) { |
| 162 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->errorKind), |
| 163 proctype_image_infos.errorKind); |
| 164 EXPECT_EQ( |
| 165 reinterpret_cast<uint64_t>(self_image_infos->errorClientOfDylibPath), |
| 166 proctype_image_infos.errorClientOfDylibPath); |
| 167 EXPECT_EQ( |
| 168 reinterpret_cast<uint64_t>(self_image_infos->errorTargetDylibPath), |
| 169 proctype_image_infos.errorTargetDylibPath); |
| 170 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_infos->errorSymbol), |
| 171 proctype_image_infos.errorSymbol); |
| 172 |
| 173 TEST_STRING(process_reader, |
| 174 self_image_infos, |
| 175 proctype_image_infos, |
| 176 errorClientOfDylibPath); |
| 177 TEST_STRING(process_reader, |
| 178 self_image_infos, |
| 179 proctype_image_infos, |
| 180 errorTargetDylibPath); |
| 181 TEST_STRING( |
| 182 process_reader, self_image_infos, proctype_image_infos, errorSymbol); |
| 183 } |
| 184 if (proctype_image_infos.version >= 12) { |
| 185 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->sharedCacheSlide), |
| 186 proctype_image_infos.sharedCacheSlide); |
| 187 } |
| 188 #endif |
| 189 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9 |
| 190 if (proctype_image_infos.version >= 13) { |
| 191 EXPECT_EQ(0, |
| 192 memcmp(self_image_infos->sharedCacheUUID, |
| 193 proctype_image_infos.sharedCacheUUID, |
| 194 sizeof(self_image_infos->sharedCacheUUID))); |
| 195 } |
| 196 if (proctype_image_infos.version >= 14) { |
| 197 for (size_t index = 0; index < arraysize(self_image_infos->reserved); |
| 198 ++index) { |
| 199 EXPECT_EQ(static_cast<uint64_t>(self_image_infos->reserved[index]), |
| 200 proctype_image_infos.reserved[index]) |
| 201 << "index " << index; |
| 202 } |
| 203 } |
| 204 #endif |
| 205 |
| 206 if (proctype_image_infos.version >= 1) { |
| 207 std::vector<process_types::dyld_image_info> proctype_image_info_vector( |
| 208 proctype_image_infos.infoArrayCount); |
| 209 ASSERT_TRUE(process_types::dyld_image_info::ReadArrayInto( |
| 210 &process_reader, |
| 211 proctype_image_infos.infoArray, |
| 212 proctype_image_info_vector.size(), |
| 213 &proctype_image_info_vector[0])); |
| 214 |
| 215 for (size_t index = 0; index < proctype_image_infos.infoArrayCount; |
| 216 ++index) { |
| 217 const dyld_image_info* self_image_info = |
| 218 &self_image_infos->infoArray[index]; |
| 219 const process_types::dyld_image_info& proctype_image_info = |
| 220 proctype_image_info_vector[index]; |
| 221 |
| 222 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_info->imageLoadAddress), |
| 223 proctype_image_info.imageLoadAddress) |
| 224 << "index " << index; |
| 225 EXPECT_EQ(reinterpret_cast<uint64_t>(self_image_info->imageFilePath), |
| 226 proctype_image_info.imageFilePath) |
| 227 << "index " << index; |
| 228 EXPECT_EQ(static_cast<uint64_t>(self_image_info->imageFileModDate), |
| 229 proctype_image_info.imageFileModDate) |
| 230 << "index " << index; |
| 231 |
| 232 TEST_STRING( |
| 233 process_reader, self_image_info, proctype_image_info, imageFilePath); |
| 234 } |
| 235 } |
| 236 |
| 237 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 |
| 238 if (proctype_image_infos.version >= 8) { |
| 239 std::vector<process_types::dyld_uuid_info> proctype_uuid_info_vector( |
| 240 proctype_image_infos.uuidArrayCount); |
| 241 ASSERT_TRUE(process_types::dyld_uuid_info::ReadArrayInto( |
| 242 &process_reader, |
| 243 proctype_image_infos.uuidArray, |
| 244 proctype_uuid_info_vector.size(), |
| 245 &proctype_uuid_info_vector[0])); |
| 246 |
| 247 for (size_t index = 0; index < proctype_image_infos.uuidArrayCount; |
| 248 ++index) { |
| 249 const dyld_uuid_info* self_uuid_info = |
| 250 &self_image_infos->uuidArray[index]; |
| 251 const process_types::dyld_uuid_info& proctype_uuid_info = |
| 252 proctype_uuid_info_vector[index]; |
| 253 |
| 254 EXPECT_EQ(reinterpret_cast<uint64_t>(self_uuid_info->imageLoadAddress), |
| 255 proctype_uuid_info.imageLoadAddress) |
| 256 << "index " << index; |
| 257 EXPECT_EQ(0, |
| 258 memcmp(self_uuid_info->imageUUID, |
| 259 proctype_uuid_info.imageUUID, |
| 260 sizeof(self_uuid_info->imageUUID))) |
| 261 << "index " << index; |
| 262 } |
| 263 } |
| 264 #endif |
| 265 } |
| 266 |
| 267 } // namespace |
OLD | NEW |