2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Graham Dennis. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #import <Cocoa/Cocoa.h>
31 #include <mach-o/dyld.h>
33 #include <mach-o/loader.h>
34 #include <mach-o/nlist.h>
37 static void cleanUpAfterOurselves(void) __attribute__ ((constructor));
38 static void *symbol_lookup(char *symbol);
40 static NSString *WKNERunState = @"WKNERunState";
41 static NSString *WKNEShouldMonitorShutdowns = @"WKNEShouldMonitorShutdowns";
49 static bool extensionBundlesWereLoaded = NO;
50 static NSSet *extensionPaths = nil;
52 static void myBundleDidLoad(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
54 // Break out early if we have already detected an extension
55 if (extensionBundlesWereLoaded)
58 NSBundle *bundle = (NSBundle *)object;
59 NSString *bundlePath = [[bundle bundlePath] stringByAbbreviatingWithTildeInPath];
60 NSString *bundleFileName = [bundlePath lastPathComponent];
62 // Explicitly ignore SIMBL.bundle, as its only purpose is to load extensions
63 // on a per-application basis. It's presence indicates a user has application
64 // extensions, but not that any will be loaded into Safari
65 if ([bundleFileName isEqualToString:@"SIMBL.bundle"])
68 // If the bundle lives inside a known extension path, flag it as an extension
69 NSEnumerator *e = [extensionPaths objectEnumerator];
71 while (path = [e nextObject]) {
72 if ([bundlePath length] < [path length])
75 if ([[bundlePath substringToIndex:[path length]] isEqualToString:path]) {
76 extensionBundlesWereLoaded = YES;
82 static void myApplicationWillFinishLaunching(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
84 CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching, NULL, NULL);
85 CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myBundleDidLoad, NULL, NULL);
86 [extensionPaths release];
89 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
90 [userDefaults setInteger:RunStateRunning forKey:WKNERunState];
91 [userDefaults synchronize];
93 if (extensionBundlesWereLoaded)
94 NSRunInformationalAlertPanel(@"Safari extensions detected",
95 @"Safari extensions were detected on your system. Extensions are incompatible with nightly builds of WebKit, and may cause crashes or incorrect behavior. Please disable them if you experience such behavior.", @"Continue",
99 static void myApplicationWillTerminate(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
101 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
102 [userDefaults setInteger:RunStateShutDown forKey:WKNERunState];
103 [userDefaults synchronize];
106 void cleanUpAfterOurselves(void)
108 char **args = *(char***)_NSGetArgv();
109 char **procPath = symbol_lookup("___CFProcessPath");
110 char *procPathBackup = *procPath;
112 CFBundleGetMainBundle();
113 *procPath = procPathBackup;
114 unsetenv("DYLD_INSERT_LIBRARIES");
115 unsetenv("CFProcessPath");
117 extensionPaths = [[NSSet alloc] initWithObjects:@"~/Library/InputManagers/", @"/Library/InputManagers/",
118 @"~/Library/Application Support/SIMBL/Plugins/", @"/Library/Application Support/SIMBL/Plugins/",
119 @"~/Library/Application Enhancers/", @"/Library/Application Enhancers/",
122 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
123 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
124 NSDictionary *defaultPrefs = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:RunStateShutDown], WKNERunState,
125 [NSNumber numberWithBool:YES], WKNEShouldMonitorShutdowns, nil];
126 [userDefaults registerDefaults:defaultPrefs];
127 if ([userDefaults boolForKey:WKNEShouldMonitorShutdowns]) {
128 WKNERunStates savedState = (WKNERunStates)[userDefaults integerForKey:WKNERunState];
129 if (savedState == RunStateInitializing) {
130 // Use CoreFoundation here as AppKit hasn't been initialized at this stage of Safari's lifetime
131 CFOptionFlags responseFlags;
132 CFUserNotificationDisplayAlert(0, kCFUserNotificationCautionAlertLevel,
134 CFSTR("WebKit failed to open correctly"),
135 CFSTR("WebKit failed to open correctly on your previous attempt. Please disable any Safari extensions that you may have installed. If the problem continues to occur, please file a bug report at http://webkit.org/quality/reporting.html"),
136 CFSTR("Continue"), NULL, NULL, &responseFlags);
138 else if (savedState == RunStateRunning) {
139 NSLog(@"WebKit failed to shut down cleanly. Checking for Safari extensions.");
140 CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myBundleDidLoad,
141 myBundleDidLoad, (CFStringRef) NSBundleDidLoadNotification,
142 NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
145 [userDefaults setInteger:RunStateInitializing forKey:WKNERunState];
146 [userDefaults synchronize];
148 CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching,
149 myApplicationWillFinishLaunching, (CFStringRef) NSApplicationWillFinishLaunchingNotification,
150 NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
151 CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillTerminate,
152 myApplicationWillTerminate, (CFStringRef) NSApplicationWillTerminateNotification,
153 NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
158 #define LC_SEGMENT_COMMAND LC_SEGMENT_64
159 #define macho_header mach_header_64
160 #define macho_segment_command segment_command_64
161 #define macho_section section_64
162 #define getsectdatafromheader getsectdatafromheader_64
163 #define macho_nlist nlist_64
165 #define LC_SEGMENT_COMMAND LC_SEGMENT
166 #define macho_header mach_header
167 #define macho_segment_command segment_command
168 #define macho_section section
169 #define macho_nlist nlist
172 static void *GDSymbolLookup(const struct macho_header *header, const char *symbol);
174 static void *symbol_lookup(char *symbol)
177 for (i = 0; i < _dyld_image_count(); i++) {
178 void *symbolResult = GDSymbolLookup((const struct macho_header*)_dyld_get_image_header(i), symbol);
185 static void *GDSymbolLookup(const struct macho_header *header, const char *symbol)
187 if (!header || !symbol)
190 if (header->magic != MH_MAGIC && header->magic != MH_MAGIC_64)
193 uint32_t currCommand;
194 const struct load_command *loadCommand = (const struct load_command *)( ((void *)header) + sizeof(struct macho_header));
195 const struct macho_segment_command *segCommand;
197 const struct symtab_command *symtabCommand = NULL;
198 const struct dysymtab_command *dysymtabCommand = NULL;
199 const struct macho_segment_command *textSegment = NULL;
200 const struct macho_segment_command *linkEditSegment = NULL;
202 for (currCommand = 0; currCommand < header->ncmds; currCommand++) {
203 switch (loadCommand->cmd) {
204 case LC_SEGMENT_COMMAND:
205 segCommand = (const struct macho_segment_command *)loadCommand;
206 if (!strcmp(segCommand->segname, "__TEXT"))
207 textSegment = segCommand;
208 else if (!strcmp(segCommand->segname, "__LINKEDIT"))
209 linkEditSegment = segCommand;
213 symtabCommand = (const struct symtab_command *)loadCommand;
217 dysymtabCommand = (const struct dysymtab_command *)loadCommand;
221 loadCommand = (const struct load_command *)(((void*)loadCommand) + loadCommand->cmdsize);
223 if (!textSegment || !linkEditSegment || !symtabCommand || !dysymtabCommand)
226 typedef enum { Start = 0, LocalSymbols, ExternalSymbols, Done } SymbolSearchState;
227 uint32_t currentSymbolIndex;
228 uint32_t maximumSymbolIndex;
229 SymbolSearchState state;
231 for (state = Start + 1; state < Done; state++) {
234 currentSymbolIndex = dysymtabCommand->ilocalsym;
235 maximumSymbolIndex = dysymtabCommand->ilocalsym + dysymtabCommand->nlocalsym;
238 case ExternalSymbols:
239 currentSymbolIndex = dysymtabCommand->iextdefsym;
240 maximumSymbolIndex = dysymtabCommand->nextdefsym;
246 for (; currentSymbolIndex < maximumSymbolIndex; currentSymbolIndex++) {
247 const struct macho_nlist *symbolTableEntry;
248 symbolTableEntry = (const struct macho_nlist *)(currentSymbolIndex*sizeof(struct macho_nlist)
249 + (ptrdiff_t)header + symtabCommand->symoff
250 + linkEditSegment->vmaddr - linkEditSegment->fileoff
251 - textSegment->vmaddr);
252 int32_t stringTableIndex = symbolTableEntry->n_un.n_strx;
253 if (stringTableIndex < 0)
255 const char *stringTableEntry = (const char*)(stringTableIndex + (ptrdiff_t)header + symtabCommand->stroff
256 + linkEditSegment->vmaddr - linkEditSegment->fileoff
257 - textSegment->vmaddr);
258 if (!strcmp(symbol, stringTableEntry))
259 return ((void*)header) - textSegment->vmaddr + symbolTableEntry->n_value;