Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/native_library.h" | 5 #include "base/native_library.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <mach/task_info.h> | |
| 9 #include <mach-o/dyld_images.h> | |
| 10 #include <mach-o/getsect.h> | |
| 11 | |
| 12 #import <Foundation/Foundation.h> | |
| 8 | 13 |
| 9 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 15 #include "base/logging.h" |
| 16 #include "base/mac/mac_util.h" | |
| 11 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 15 | 21 |
| 16 namespace base { | 22 namespace base { |
| 17 | 23 |
| 24 static bool ImageContainsObjectiveC(const base::FilePath& library_path) { | |
| 25 // Get the dyld info from the kernel, which will contain the array of all | |
| 26 // loaded modules. | |
| 27 task_dyld_info_data_t task_dyld_info; | |
|
Scott Hess - ex-Googler
2013/03/12 20:43:53
I'm having a mental-model issue with this. Usuall
Mark Mentovai
2013/03/12 20:51:03
shess wrote:
Scott Hess - ex-Googler
2013/03/12 20:55:01
So, is this thread safe? Perhaps implicitly, beca
| |
| 28 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; | |
| 29 kern_return_t result = task_info(mach_task_self(), TASK_DYLD_INFO, | |
| 30 reinterpret_cast<task_info_t>(&task_dyld_info), &count); | |
| 31 if (result != KERN_SUCCESS) { | |
| 32 VLOG(1) << "Failed to get TASK_DYLD_INFO: " << result; | |
| 33 return false; | |
| 34 } | |
| 35 | |
| 36 const char* const library_path_c = library_path.value().c_str(); | |
| 37 | |
| 38 // Search the array of loaded images for the |library_path| that is being | |
| 39 // tested for ObjC. | |
| 40 struct dyld_all_image_infos* infos = | |
| 41 reinterpret_cast<struct dyld_all_image_infos*>( | |
| 42 task_dyld_info.all_image_info_addr); | |
| 43 for (uint32_t i = 0; i < infos->infoArrayCount; ++i) { | |
| 44 const struct dyld_image_info image_info = infos->infoArray[i]; | |
| 45 if (strcmp(image_info.imageFilePath, library_path_c) != 0) | |
| 46 continue; | |
| 47 | |
| 48 // See if the the image contains an "ObjC image info" segment. This method | |
| 49 // of testing is used in _CFBundleGrokObjcImageInfoFromFile in | |
| 50 // CF-744/CFBundle.c, around lines 2447-2474. | |
| 51 // | |
| 52 // In 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas | |
| 53 // in 64-bit, the data is in __DATA,__objc_imageinfo. | |
| 54 #if __LP64__ | |
| 55 const section_64* section = getsectbynamefromheader_64( | |
| 56 reinterpret_cast<const struct mach_header_64*>( | |
| 57 image_info.imageLoadAddress), | |
| 58 SEG_DATA, "__objc_imageinfo"); | |
| 59 #else | |
| 60 const section* section = getsectbynamefromheader( | |
| 61 image_info.imageLoadAddress, SEG_OBJC, "__image_info"); | |
| 62 #endif | |
| 63 return section != NULL; | |
| 64 } | |
| 65 | |
| 66 VLOG(1) << "Could not find image info for " << library_path_c; | |
| 67 | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 18 // static | 71 // static |
| 19 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, | 72 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| 20 std::string* error) { | 73 std::string* error) { |
| 21 // dlopen() etc. open the file off disk. | 74 // dlopen() etc. open the file off disk. |
| 22 if (library_path.Extension() == "dylib" || | 75 if (library_path.Extension() == "dylib" || |
| 23 !file_util::DirectoryExists(library_path)) { | 76 !file_util::DirectoryExists(library_path)) { |
| 24 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 77 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 25 if (!dylib) | 78 if (!dylib) |
| 26 return NULL; | 79 return NULL; |
| 27 NativeLibrary native_lib = new NativeLibraryStruct(); | 80 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 28 native_lib->type = DYNAMIC_LIB; | 81 native_lib->type = DYNAMIC_LIB; |
| 29 native_lib->dylib = dylib; | 82 native_lib->dylib = dylib; |
| 83 native_lib->image_path = library_path; | |
| 30 return native_lib; | 84 return native_lib; |
| 31 } | 85 } |
| 32 base::mac::ScopedCFTypeRef<CFURLRef> url( | 86 base::mac::ScopedCFTypeRef<CFURLRef> url( |
| 33 CFURLCreateFromFileSystemRepresentation( | 87 CFURLCreateFromFileSystemRepresentation( |
| 34 kCFAllocatorDefault, | 88 kCFAllocatorDefault, |
| 35 (const UInt8*)library_path.value().c_str(), | 89 (const UInt8*)library_path.value().c_str(), |
| 36 library_path.value().length(), | 90 library_path.value().length(), |
| 37 true)); | 91 true)); |
| 38 if (!url) | 92 if (!url) |
| 39 return NULL; | 93 return NULL; |
| 40 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 94 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 41 if (!bundle) | 95 if (!bundle) |
| 42 return NULL; | 96 return NULL; |
| 43 | 97 |
| 44 NativeLibrary native_lib = new NativeLibraryStruct(); | 98 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 45 native_lib->type = BUNDLE; | 99 native_lib->type = BUNDLE; |
| 46 native_lib->bundle = bundle; | 100 native_lib->bundle = bundle; |
| 47 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 101 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| 102 | |
| 103 base::mac::ScopedCFTypeRef<CFURLRef> executable_url( | |
| 104 CFBundleCopyExecutableURL(bundle)); | |
| 105 NSURL* executable_url_ns = base::mac::CFToNSCast(executable_url); | |
| 106 native_lib->image_path = | |
| 107 base::FilePath([[executable_url_ns path] fileSystemRepresentation]); | |
| 48 return native_lib; | 108 return native_lib; |
| 49 } | 109 } |
| 50 | 110 |
| 51 // static | 111 // static |
| 52 void UnloadNativeLibrary(NativeLibrary library) { | 112 void UnloadNativeLibrary(NativeLibrary library) { |
| 53 if (library->type == BUNDLE) { | 113 if (ImageContainsObjectiveC(library->image_path)) { |
| 54 CFBundleCloseBundleResourceMap(library->bundle, | 114 VLOG(2) << "Not unloading NativeLibrary at " |
| 55 library->bundle_resource_ref); | 115 << library->image_path.MaybeAsASCII() |
| 56 CFRelease(library->bundle); | 116 << " because it contains an Objective-C segment."; |
| 117 // Deliberately do not CFRelease the bundle or dlclose the dylib because | |
| 118 // doing so can corrupt the ObjC runtime method caches. See | |
| 119 // http://crbug.com/172319 for details. | |
| 57 } else { | 120 } else { |
| 58 dlclose(library->dylib); | 121 if (library->type == BUNDLE) { |
| 122 CFBundleCloseBundleResourceMap(library->bundle, | |
| 123 library->bundle_resource_ref); | |
| 124 CFRelease(library->bundle); | |
| 125 } else { | |
| 126 dlclose(library->dylib); | |
| 127 } | |
| 59 } | 128 } |
| 60 delete library; | 129 delete library; |
| 61 } | 130 } |
| 62 | 131 |
| 63 // static | 132 // static |
| 64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 133 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 65 const char* name) { | 134 const char* name) { |
| 66 if (library->type == BUNDLE) { | 135 if (library->type == BUNDLE) { |
| 67 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( | 136 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( |
| 68 CFStringCreateWithCString(kCFAllocatorDefault, name, | 137 CFStringCreateWithCString(kCFAllocatorDefault, name, |
| 69 kCFStringEncodingUTF8)); | 138 kCFStringEncodingUTF8)); |
| 70 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); | 139 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); |
| 71 } | 140 } |
| 72 return dlsym(library->dylib, name); | 141 return dlsym(library->dylib, name); |
| 73 } | 142 } |
| 74 | 143 |
| 75 // static | 144 // static |
| 76 string16 GetNativeLibraryName(const string16& name) { | 145 string16 GetNativeLibraryName(const string16& name) { |
| 77 return name + ASCIIToUTF16(".dylib"); | 146 return name + ASCIIToUTF16(".dylib"); |
| 78 } | 147 } |
| 79 | 148 |
| 80 } // namespace base | 149 } // namespace base |
| OLD | NEW |