Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong strong layout result because of unaligned instance start. #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FBRetainCycleDetector/Layout/Classes/FBClassStrongLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ static NSUInteger FBGetMinimumIvarIndex(__unsafe_unretained Class aCls) {
if (count > 0) {
Ivar ivar = ivars[0];
ptrdiff_t offset = ivar_getOffset(ivar);
minimumIndex = offset / (sizeof(void *));
// unaligned bytes at the start of this class's ivars are not represented in the layout bitmap.
minimumIndex = (offset + (sizeof(void *) - 1)) / (sizeof(void *));
}

free(ivars);
Expand Down
24 changes: 24 additions & 0 deletions FBRetainCycleDetectorTests/FBClassStrongLayoutTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#import <FBRetainCycleDetector/FBClassStrongLayout.h>
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#import <FBRetainCycleDetector/FBObjectReference.h>

@interface _RCDTestEmptyClass : NSObject
@end
Expand Down Expand Up @@ -157,6 +158,22 @@ @implementation _RCDTestClassWithCppStructAndStrongProperty
}
@end

@interface _RCDTestClassWithUnalignEndIvar : NSObject {
char content[20];
}
@end
@implementation _RCDTestClassWithUnalignEndIvar
@end

@interface _RCDTestClassWithUnalignEndIvarParentClass : _RCDTestClassWithUnalignEndIvar {
int integer;
__weak NSObject *object1;
NSObject *object2;
}
@end
@implementation _RCDTestClassWithUnalignEndIvarParentClass
@end

@interface FBClassStrongLayoutTests : XCTestCase
@end

Expand Down Expand Up @@ -260,4 +277,11 @@ - (void)testLayoutForClassWithCppStructAndStrongPropertyWillNotCrashAndFetchStro
XCTAssertEqual([ivars count], 1);
}

- (void)testLayoutForClassWithParentClassHasUnalignEndIvar
{
NSArray<id<FBObjectReference>> *ivars = FBGetObjectStrongReferences([_RCDTestClassWithUnalignEndIvarParentClass new], nil);
XCTAssertEqual([ivars count], 1);
XCTAssertEqualObjects([[ivars firstObject] namePath], @[@"object2"]);
}

@end