Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / objc / objc_debugHelpers.m
blobea9e20768df578d1cd03e2fe018ffd4a0a032494
1 /* Copyright (c) 2008 Johannes Fortmann
2  
3  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4  
5  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6  
7  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <signal.h>
10 #import <objc/objc.h>
11 #import <objc/runtime.h>
12 #import <string.h>
14 #import "objc_class.h"
16 static id objc_lookUpMetaClass(const char *name) {
17    Class c=objc_lookUpClass(name);
18    if(c)
19       return c->isa;
20    return nil;
23 BOOL _objc_checkObject(volatile id object)
25    // assume no objects below a certain address 
26    if(object<(id)0x2000)
27       return NO;
28    // objects begin at even addresses
29    if((long)object%4!=0)
30       return NO;
31    volatile Class isa=object->isa;
32    
33    if(isa<(Class)0x2000)
34       return NO;
35    if((long)isa%4!=0)
36       return NO;
37    
38    // check if name is all-ascii. We can't assume that name points to a nul-terminated string,
39    // so only copy the first 256 characters.
40    char *saneName=__builtin_alloca(256);
41    strncpy(saneName, isa->name, 256);
42    saneName[255]='\0';
43    char* cur;
44    for(cur=saneName; *cur!='\0'; cur++) {
45       if(((uint8_t)*cur<=32 || (uint8_t)*cur>128))
46       {
47          return NO;
48       }
49    }
50    // name is ok; lookup class and compare with what it should be
51    Class accordingToName=Nil;
52    
53    if(isa->info&CLASS_INFO_META)
54       accordingToName=objc_lookUpMetaClass(saneName);
55    else
56       accordingToName=objc_lookUpClass(saneName);
57    
58    if(isa==accordingToName)
59       return YES;
60    
61    return NO;