Skip to content

Commit b6ac68d

Browse files
committed
[TypeSystem] Fix inspection of Objective-C object types
ptr_refs exposed a problem in ClangASTContext's implementation: it uses an accessor to downcast a QualType to an ObjCObjectPointerType, but the accessor is not fully general. getAs() is the safer way to go. I've added a test case that uses ptr_refs in a way that would crash before the fix. <rdar://problem/31363513> git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@303110 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 199a5ff commit b6ac68d

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LEVEL = ../../../make
2+
3+
OBJC_SOURCES := main.m
4+
5+
include $(LEVEL)/Makefile.rules
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Test the ptr_refs tool on Darwin with Objective-C
3+
"""
4+
5+
from __future__ import print_function
6+
7+
import os
8+
import lldb
9+
from lldbsuite.test.decorators import *
10+
from lldbsuite.test.lldbtest import *
11+
from lldbsuite.test import lldbutil
12+
13+
14+
class TestPtrRefsObjC(TestBase):
15+
16+
mydir = TestBase.compute_mydir(__file__)
17+
18+
@skipUnlessDarwin
19+
def test_ptr_refs(self):
20+
"""Test the ptr_refs tool on Darwin with Objective-C"""
21+
self.build()
22+
exe_name = 'a.out'
23+
exe = os.path.join(os.getcwd(), exe_name)
24+
25+
target = self.dbg.CreateTarget(exe)
26+
self.assertTrue(target, VALID_TARGET)
27+
28+
main_file_spec = lldb.SBFileSpec('main.m')
29+
breakpoint = target.BreakpointCreateBySourceRegex(
30+
'break', main_file_spec)
31+
self.assertTrue(breakpoint and
32+
breakpoint.GetNumLocations() == 1,
33+
VALID_BREAKPOINT)
34+
35+
process = target.LaunchSimple(
36+
None, None, self.get_process_working_directory())
37+
self.assertTrue(process, PROCESS_IS_VALID)
38+
39+
# Frame #0 should be on self.line1 and the break condition should hold.
40+
thread = lldbutil.get_stopped_thread(
41+
process, lldb.eStopReasonBreakpoint)
42+
self.assertTrue(
43+
thread.IsValid(),
44+
"There should be a thread stopped due to breakpoint condition")
45+
46+
frame = thread.GetFrameAtIndex(0)
47+
48+
self.dbg.HandleCommand("script import lldb.macosx.heap")
49+
self.expect("ptr_refs self", substrs=["malloc", "stack"])
50+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- main.c --------------------------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#import <Foundation/Foundation.h>
11+
12+
@interface MyClass : NSObject {
13+
};
14+
-(void)test;
15+
@end
16+
17+
@implementation MyClass
18+
-(void)test {
19+
printf("%p\n", self); // break here
20+
}
21+
@end
22+
23+
@interface MyOwner : NSObject {
24+
@public id ownedThing; // should be id, to test <rdar://problem/31363513>
25+
};
26+
@end
27+
28+
@implementation MyOwner
29+
@end
30+
31+
int main (int argc, char const *argv[]) {
32+
@autoreleasepool {
33+
MyOwner *owner = [[MyOwner alloc] init];
34+
owner->ownedThing = [[MyClass alloc] init];
35+
[(MyClass*)owner->ownedThing test];
36+
}
37+
return 0;
38+
}
39+

source/Symbol/ClangASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,7 +4551,7 @@ ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
45514551

45524552
case clang::Type::ObjCObjectPointer: {
45534553
const clang::ObjCObjectPointerType *objc_class_type =
4554-
qual_type->getAsObjCInterfacePointerType();
4554+
qual_type->getAs<clang::ObjCObjectPointerType>();
45554555
const clang::ObjCInterfaceType *objc_interface_type =
45564556
objc_class_type->getInterfaceType();
45574557
if (objc_interface_type &&
@@ -4659,7 +4659,7 @@ ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
46594659

46604660
case clang::Type::ObjCObjectPointer: {
46614661
const clang::ObjCObjectPointerType *objc_class_type =
4662-
qual_type->getAsObjCInterfacePointerType();
4662+
qual_type->getAs<clang::ObjCObjectPointerType>();
46634663
const clang::ObjCInterfaceType *objc_interface_type =
46644664
objc_class_type->getInterfaceType();
46654665
if (objc_interface_type &&
@@ -5766,7 +5766,7 @@ uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
57665766

57675767
case clang::Type::ObjCObjectPointer: {
57685768
const clang::ObjCObjectPointerType *objc_class_type =
5769-
qual_type->getAsObjCInterfacePointerType();
5769+
qual_type->getAs<clang::ObjCObjectPointerType>();
57705770
const clang::ObjCInterfaceType *objc_interface_type =
57715771
objc_class_type->getInterfaceType();
57725772
if (objc_interface_type &&
@@ -5913,7 +5913,7 @@ CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
59135913

59145914
case clang::Type::ObjCObjectPointer: {
59155915
const clang::ObjCObjectPointerType *objc_class_type =
5916-
qual_type->getAsObjCInterfacePointerType();
5916+
qual_type->getAs<clang::ObjCObjectPointerType>();
59175917
const clang::ObjCInterfaceType *objc_interface_type =
59185918
objc_class_type->getInterfaceType();
59195919
if (objc_interface_type &&

0 commit comments

Comments
 (0)