Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1929)

Unified Diff: base/native_library_mac.mm

Issue 12793004: [Mac] Do not unload base::NativeLibary-ies if they contain ObjC segments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/native_library.h ('K') | « base/native_library.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/native_library_mac.mm
diff --git a/base/native_library_mac.mm b/base/native_library_mac.mm
index eec586b863588d9856cc0221da1e73973e10e8de..bbab6827a307cdf58547180c6d2dd43bfbdca56a 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>
+
+#include <Foundation/Foundation.h>
Mark Mentovai 2013/03/12 19:19:31 #import me.
Robert Sesek 2013/03/12 20:02:10 Done.
#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,57 @@
namespace base {
+// In 32-bit images, ObjC can be recognized in __OBJC.__image_info, whereas
Mark Mentovai 2013/03/12 19:19:31 It’s usual to separate the segment and section nam
Robert Sesek 2013/03/12 20:02:10 Done.
+// in 64-bit, the data is in __DATA.__objc_imageinfo.
+static const char kObjCImageInfo32[] = "__image_info";
Mark Mentovai 2013/03/12 19:19:31 Why do you need to check for both flavors? If you’
Robert Sesek 2013/03/12 20:02:10 Done.
+static const char kObjCImageInfo64[] = "__objc_imageinfo";
+
+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;
+ 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_ascii = library_path.MaybeAsASCII().c_str();
Mark Mentovai 2013/03/12 19:19:31 This is bogus since you know what platform you’re
Robert Sesek 2013/03/12 20:02:10 Done.
+ VLOG(2) << "Looking up image data for " << library_path_ascii;
Mark Mentovai 2013/03/12 19:19:31 Is this line even needed now?
Robert Sesek 2013/03/12 20:02:10 Removed.
+
+ // 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_ascii) != 0)
+ continue;
+
+ // See if the the image contains an "ObjC image info" segment. This method
+ // of testing is copied from _CFBundleGrokObjcImageInfoFromFile in
+ // CF-744/CFBundle.c, around lines 2447-2474.
+ if (image_info.imageLoadAddress->cputype == CPU_TYPE_X86_64) {
Mark Mentovai 2013/03/12 19:19:31 Evaluating this conditional at runtime is stupid (
Robert Sesek 2013/03/12 20:02:10 Done.
+ const section_64* section = getsectbynamefromheader_64(
+ reinterpret_cast<const struct mach_header_64*>(
+ image_info.imageLoadAddress),
+ SEG_DATA, kObjCImageInfo64);
Mark Mentovai 2013/03/12 19:19:31 kObjCImageInfo64/32 didn’t need to be defined exte
Robert Sesek 2013/03/12 20:02:10 Done.
+ return section != NULL;
+ } else {
+ const section* section = getsectbynamefromheader(
+ image_info.imageLoadAddress, SEG_OBJC, kObjCImageInfo32);
+ return section != NULL;
+ }
+ }
+
+ VLOG(1) << "Could not find image info for " << library_path_ascii;
+
+ return false;
Scott Hess - ex-Googler 2013/03/12 20:10:38 AFAICT, in this case we couldn't find the informat
Robert Sesek 2013/03/12 20:19:43 I see your point, but what should happen in the He
Scott Hess - ex-Googler 2013/03/12 20:43:53 Am I reading it wrong? In the HellIfIKnow case, t
+}
+
// static
NativeLibrary LoadNativeLibrary(const base::FilePath& library_path,
std::string* error) {
@@ -27,6 +84,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,11 +103,28 @@ 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] UTF8String]);
Mark Mentovai 2013/03/12 19:19:31 Not UTF8String, but fileSystemRepresentation.
Robert Sesek 2013/03/12 20:02:10 Done.
return native_lib;
}
// static
void UnloadNativeLibrary(NativeLibrary library) {
+ if (ImageContainsObjectiveC(library->image_path)) {
Scott Hess - ex-Googler 2013/03/12 20:10:38 Is there any downside to looking this up at Load a
Robert Sesek 2013/03/12 20:19:43 We think alike ;). But that's not possible because
Scott Hess - ex-Googler 2013/03/12 20:43:53 Sigh. Is there any case which differs from: Line
+ VLOG(2) << "Not unloading NativeLibrary at "
+ << library->image_path.MaybeAsASCII() << " because it contains "
+ << "an Objective-C segment.";
Mark Mentovai 2013/03/12 19:19:31 Simplify the object code by coalescing these last
Robert Sesek 2013/03/12 20:02:10 Done.
+ // 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.
+ delete library;
Mark Mentovai 2013/03/12 19:19:31 Why don’t you share the “delete” with the delete b
Robert Sesek 2013/03/12 20:02:10 Done.
+ return;
+ }
+
if (library->type == BUNDLE) {
CFBundleCloseBundleResourceMap(library->bundle,
library->bundle_resource_ref);
« base/native_library.h ('K') | « base/native_library.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698