Chromium Code Reviews| Index: base/native_library_mac.mm |
| diff --git a/base/native_library_mac.mm b/base/native_library_mac.mm |
| index eec586b863588d9856cc0221da1e73973e10e8de..80fcb4f6e57a5de9d4ec004bd78f3671ee616629 100644 |
| --- a/base/native_library_mac.mm |
| +++ b/base/native_library_mac.mm |
| @@ -5,9 +5,15 @@ |
| #include "base/native_library.h" |
| #include <dlfcn.h> |
| +#include <mach/task_info.h> |
| +#include <mach-o/dyld_images.h> |
| +#include <mach-o/getsect.h> |
| + |
| +#import <Foundation/Foundation.h> |
| #include "base/file_util.h" |
| -#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/mac/mac_util.h" |
| #include "base/mac/scoped_cftyperef.h" |
| #include "base/string_util.h" |
| #include "base/threading/thread_restrictions.h" |
| @@ -15,6 +21,53 @@ |
| namespace base { |
| +static bool ImageContainsObjectiveC(const base::FilePath& library_path) { |
| + // Get the dyld info from the kernel, which will contain the array of all |
| + // loaded modules. |
| + 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
|
| + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; |
| + kern_return_t result = task_info(mach_task_self(), TASK_DYLD_INFO, |
| + reinterpret_cast<task_info_t>(&task_dyld_info), &count); |
| + if (result != KERN_SUCCESS) { |
| + VLOG(1) << "Failed to get TASK_DYLD_INFO: " << result; |
| + return false; |
| + } |
| + |
| + const char* const library_path_c = library_path.value().c_str(); |
| + |
| + // Search the array of loaded images for the |library_path| that is being |
| + // tested for ObjC. |
| + struct dyld_all_image_infos* infos = |
| + reinterpret_cast<struct dyld_all_image_infos*>( |
| + task_dyld_info.all_image_info_addr); |
| + for (uint32_t i = 0; i < infos->infoArrayCount; ++i) { |
| + const struct dyld_image_info image_info = infos->infoArray[i]; |
| + if (strcmp(image_info.imageFilePath, library_path_c) != 0) |
| + continue; |
| + |
| + // See if the the image contains an "ObjC image info" segment. This method |
| + // of testing is used in _CFBundleGrokObjcImageInfoFromFile in |
| + // CF-744/CFBundle.c, around lines 2447-2474. |
| + // |
| + // In 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas |
| + // in 64-bit, the data is in __DATA,__objc_imageinfo. |
| +#if __LP64__ |
| + const section_64* section = getsectbynamefromheader_64( |
| + reinterpret_cast<const struct mach_header_64*>( |
| + image_info.imageLoadAddress), |
| + SEG_DATA, "__objc_imageinfo"); |
| +#else |
| + const section* section = getsectbynamefromheader( |
| + image_info.imageLoadAddress, SEG_OBJC, "__image_info"); |
| +#endif |
| + return section != NULL; |
| + } |
| + |
| + VLOG(1) << "Could not find image info for " << library_path_c; |
| + |
| + return false; |
| +} |
| + |
| // static |
| NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| std::string* error) { |
| @@ -27,6 +80,7 @@ NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| NativeLibrary native_lib = new NativeLibraryStruct(); |
| native_lib->type = DYNAMIC_LIB; |
| native_lib->dylib = dylib; |
| + native_lib->image_path = library_path; |
| return native_lib; |
| } |
| base::mac::ScopedCFTypeRef<CFURLRef> url( |
| @@ -45,17 +99,32 @@ NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| native_lib->type = BUNDLE; |
| native_lib->bundle = bundle; |
| native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| + |
| + base::mac::ScopedCFTypeRef<CFURLRef> executable_url( |
| + CFBundleCopyExecutableURL(bundle)); |
| + NSURL* executable_url_ns = base::mac::CFToNSCast(executable_url); |
| + native_lib->image_path = |
| + base::FilePath([[executable_url_ns path] fileSystemRepresentation]); |
| return native_lib; |
| } |
| // static |
| void UnloadNativeLibrary(NativeLibrary library) { |
| - if (library->type == BUNDLE) { |
| - CFBundleCloseBundleResourceMap(library->bundle, |
| - library->bundle_resource_ref); |
| - CFRelease(library->bundle); |
| + if (ImageContainsObjectiveC(library->image_path)) { |
| + VLOG(2) << "Not unloading NativeLibrary at " |
| + << library->image_path.MaybeAsASCII() |
| + << " because it contains an Objective-C segment."; |
| + // Deliberately do not CFRelease the bundle or dlclose the dylib because |
| + // doing so can corrupt the ObjC runtime method caches. See |
| + // http://crbug.com/172319 for details. |
| } else { |
| - dlclose(library->dylib); |
| + if (library->type == BUNDLE) { |
| + CFBundleCloseBundleResourceMap(library->bundle, |
| + library->bundle_resource_ref); |
| + CFRelease(library->bundle); |
| + } else { |
| + dlclose(library->dylib); |
| + } |
| } |
| delete library; |
| } |