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

Crash using NSObject's -valueForKey: IMP with collections on arm64 #70

Closed
nolanw opened this issue Feb 16, 2014 · 11 comments
Closed

Crash using NSObject's -valueForKey: IMP with collections on arm64 #70

nolanw opened this issue Feb 16, 2014 · 11 comments
Labels

Comments

@nolanw
Copy link
Contributor

nolanw commented Feb 16, 2014

I must apologize up front for this less than helpful report. As I don't have an arm64 device at hand, I can't personally reproduce this crash, and thus don't trust myself to fix it. I'd appreciate any help others can provide.

There seems to be an issue using NSObject's -valueForKey: implementation for collections on arm64. Here's an example crash report. It points to GRMustacheKeyAccess.m line 80. A collaborator with an arm64 device caught this crash in the debugger and found that the key path was contactInfo.count. The key contactInfo correctly returns an NSArray, so it seems it's the .count that causes something to go haywire.

All reports of this crash come from arm64 devices.

Here's a test case that may trigger the crash. Be sure to run it on an arm64 device; it is not reproducible in the simulator (either x86 or x86_64).

[GRMustacheTemplate renderObject:@{ @"things": @[ @1, @2 ] }
                      fromString:@"{{#things.count}}There's things!{{/}}"
                           error:nil];

In the linked crash report, as well as in the test case above, I was following the runtime guide's suggestion for rendering a section only when a collection is nonempty. It's easy enough to work around the problem, but that section of the guide needs updating until a solution can be found.


As an aside, while I haven't done much research into the matter, I suspect GRMustache's current practice, of retrieving NSObject's -valueForKey: implementation and calling it on instances of NSArray et al, is not documented to work. I'm happy to file a radar asking for clarification and/or a fix, if that would be helpful, but I'm not the ideal candidate given my lack of arm64 hardware.

@ultramiraculous
Copy link

To add a little more color, the problem isn't specific to collections. If you attempt to use GRValueForKeyNSObjectIMP on anything at all, it goes equally haywire. i.e. replacing

        return [object valueForKey:key];

with

        return GRValueForKeyNSObjectIMP(object, @selector(valueForKey:), key);

causes crashes to happen for any object when you go to access a key value.

I can also confirm that Nolan's code snippet crashes gloriously on my Retina iPad Mini running 7.1 beta 5 and my iPhone 5s running 7.0.4.

@groue
Copy link
Owner

groue commented Feb 17, 2014

Thanks to all for your reports. I'm on vacation right now, so I won't attack this issue before two weeks. GRMustacheKeyAccess.m has a detailed comment on how NSArray, NSSet and NSOrderedSet are handled by GRMustache. Feel free to dig deeper on why IMPs can't be used directly on ARM64. Another option to look at is objcMsgSendSuper, should IMPs be banned for any reason.

@groue groue added the bug label Feb 19, 2014
groue added a commit that referenced this issue Feb 26, 2014
…rderedSet instead of direct use of NSObject’s valueForKey: implementation, in order to avoid a crash on arm64 (see issue #70).
@groue
Copy link
Owner

groue commented Feb 26, 2014

Hi @nolanw, @ultramiraculous.

I just pushed a build of GRMustache on the issue70 branch.

In this branch, GRMustacheKeyAccess.m has been updated, as well as the static library lib/libGRMustache6-iOS.a, so that objc_msgSendSuper is used instead of NSObject's implementation of valueForKey: (see commit 1c94c24).

Can you tell me if this solves the issue (I don't have any arm64 device)?

@ultramiraculous
Copy link

Same crash, unfortunately, but good idea. I threw up a radar just now, so I'll let you know if anything comes of it. I messed around with it more, and it's really just doing anything other than [object valueForKey:] seems to make it crash. Even objc_msgSend-ing valueForKey: to a vanilla NSObject doesn't work. Kinda bizarre.

@groue
Copy link
Owner

groue commented Feb 27, 2014

@nolanw wrote:

I was following the runtime guide's suggestion for rendering a section only when a collection is nonempty. It's easy enough to work around the problem, but that section of the guide needs updating until a solution can be found.

Yep. Let's update the doc.

Could anyone of you push a full stack trace of the exception here, please? (Enter the bt command in LLDB, and paste the output here)

@groue
Copy link
Owner

groue commented Feb 27, 2014

BTW, thanks @ultramiraculous for your test. I'm quite disappointed :-)

groue added a commit that referenced this issue Feb 27, 2014
…SArray, NSSet and NSOrderedSet instead of direct use of NSObject’s valueForKey: implementation, in order to avoid a crash on arm64 (see issue #70).
groue added a commit that referenced this issue Feb 27, 2014
@groue
Copy link
Owner

groue commented Feb 27, 2014

All right, here we go for another try, with a home-made implementation of valueForKey: (see commit 3133a3b). @ultramiraculous, I hope your device won't crash this time :-)

@groue
Copy link
Owner

groue commented Feb 27, 2014

@nolanw Thanks for the Crashlytics report, I didn't notice it the first time.

@ultramiraculous
Copy link

Yeah, that certainly appears to work. Wish it didn't have to come to this, but happy for the fix :)

@groue
Copy link
Owner

groue commented Feb 27, 2014

OK, so I'll merge this into master, and issue a new release. Thanks a lot, @ultramiraculous !!

@groue
Copy link
Owner

groue commented Feb 28, 2014

Hello @ultramiraculous, @nolanw. Thank you again for your much appreciated reports and help. GRMustache v6.9.2 is out, with a fix for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants