2006-11-01 Mark Rowe <bdash@webkit.org>
[WebKit-https.git] / WebKitTools / WebKitLauncher / WebKitNightlyEnabler.m
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Graham Dennis.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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. 
17  *
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.
28  */
29
30 #import <Cocoa/Cocoa.h>
31 #include <mach-o/dyld.h>
32 #include <dlfcn.h>
33 #include <mach-o/loader.h>
34 #include <mach-o/nlist.h>
35 #include <string.h>
36
37 static void cleanUpAfterOurselves(void) __attribute__ ((constructor));
38 static void *symbol_lookup(char *symbol);
39
40 static NSString *WKNERunState = @"WKNERunState";
41 static NSString *WKNEShouldMonitorShutdowns = @"WKNEShouldMonitorShutdowns";
42
43 typedef enum {
44     RunStateShutDown,
45     RunStateInitializing,
46     RunStateRunning
47 } WKNERunStates;
48
49 static bool extensionBundlesWereLoaded = NO;
50 static NSSet *extensionPaths = nil;
51
52 static void myBundleDidLoad(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
53 {
54     // Break out early if we have already detected an extension
55     if (extensionBundlesWereLoaded)
56         return;
57
58     NSBundle *bundle = (NSBundle *)object;
59     NSString *bundlePath = [[bundle bundlePath] stringByAbbreviatingWithTildeInPath];
60     NSString *bundleFileName = [bundlePath lastPathComponent];
61
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"])
66         return;
67
68     // If the bundle lives inside a known extension path, flag it as an extension
69     NSEnumerator *e = [extensionPaths objectEnumerator];
70     NSString *path = nil;
71     while (path = [e nextObject]) {
72         if ([bundlePath length] < [path length])
73             continue;
74
75         if ([[bundlePath substringToIndex:[path length]] isEqualToString:path]) {
76             extensionBundlesWereLoaded = YES;
77             break;
78         }
79     }
80 }
81
82 static void myApplicationWillFinishLaunching(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
83 {
84     CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching, NULL, NULL);
85     CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myBundleDidLoad, NULL, NULL);
86     [extensionPaths release];
87     extensionPaths = nil;
88
89     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
90     [userDefaults setInteger:RunStateRunning forKey:WKNERunState];
91     [userDefaults synchronize];
92
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",
96                                      nil, nil);
97 }
98
99 static void myApplicationWillTerminate(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
100 {
101     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
102     [userDefaults setInteger:RunStateShutDown forKey:WKNERunState];
103     [userDefaults synchronize];
104 }
105
106 void cleanUpAfterOurselves(void)
107 {
108     char **args = *(char***)_NSGetArgv();
109     char **procPath = symbol_lookup("___CFProcessPath");
110     char *procPathBackup = *procPath;
111     *procPath = args[0];
112     CFBundleGetMainBundle();
113     *procPath = procPathBackup;
114     unsetenv("DYLD_INSERT_LIBRARIES");
115     unsetenv("CFProcessPath");
116
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/",
120                                                     nil];
121
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,
133                                            NULL, NULL, NULL,
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);
137         }
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);
143         }
144     }
145     [userDefaults setInteger:RunStateInitializing forKey:WKNERunState];
146     [userDefaults synchronize];
147
148     CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching,
149                                     myApplicationWillFinishLaunching, (CFStringRef) NSApplicationWillFinishLaunchingNotification,
150                                     NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
151     CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillTerminate,
152                                     myApplicationWillTerminate, (CFStringRef) NSApplicationWillTerminateNotification,
153                                     NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
154     [pool release];
155 }
156
157 #if __LP64__
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
164 #else
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
170 #endif
171
172 static void *GDSymbolLookup(const struct macho_header *header, const char *symbol);
173
174 static void *symbol_lookup(char *symbol)
175 {
176     int i;
177     for (i = 0; i < _dyld_image_count(); i++) {
178         void *symbolResult = GDSymbolLookup((const struct macho_header*)_dyld_get_image_header(i), symbol);
179         if (symbolResult)
180             return symbolResult;
181     }
182     return NULL;
183 }
184
185 static void *GDSymbolLookup(const struct macho_header *header, const char *symbol)
186 {
187     if (!header || !symbol)
188         return NULL;
189
190     if (header->magic != MH_MAGIC && header->magic != MH_MAGIC_64)
191         return NULL;
192
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;
196
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;
201
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;
210                     break;
211
212             case LC_SYMTAB:
213                 symtabCommand = (const struct symtab_command *)loadCommand;
214                 break;
215
216             case LC_DYSYMTAB:
217                 dysymtabCommand = (const struct dysymtab_command *)loadCommand;
218                 break;
219         }
220
221         loadCommand = (const struct load_command *)(((void*)loadCommand) + loadCommand->cmdsize);
222     }
223     if (!textSegment || !linkEditSegment || !symtabCommand || !dysymtabCommand)
224         return NULL;
225
226     typedef enum { Start = 0, LocalSymbols, ExternalSymbols, Done } SymbolSearchState;
227     uint32_t currentSymbolIndex;
228     uint32_t maximumSymbolIndex;
229     SymbolSearchState state;
230
231     for (state = Start + 1; state < Done; state++) {
232         switch (state) {
233             case LocalSymbols:
234                 currentSymbolIndex = dysymtabCommand->ilocalsym;
235                 maximumSymbolIndex = dysymtabCommand->ilocalsym + dysymtabCommand->nlocalsym;
236                 break;
237
238             case ExternalSymbols:
239                 currentSymbolIndex = dysymtabCommand->iextdefsym;
240                 maximumSymbolIndex = dysymtabCommand->nextdefsym;
241                 break;
242
243             default:
244                 return NULL;
245         }
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)
254                 continue;
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;
260         }
261         state++;
262     }
263     return NULL;
264 }
265