Skip to content

Seems to crash when trying to get the type of a UIViewController. #36

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

Open
valleyman86 opened this issue Mar 16, 2019 · 8 comments
Open

Comments

@valleyman86
Copy link

The following code will crash

class ViewController: UIViewController { }
let info = try? typeInfo(of: ViewController.self)

in
RelativePointer->advanced()
on
let offsetCopy = self.offset

Any ideas what would cause this?

@wickwirew
Copy link
Owner

What version of swift?

@valleyman86
Copy link
Author

Swift 4.2

@valleyman86
Copy link
Author

Not sure if it is relevant but let info = try? typeInfo(of: UIViewController.self) will return nil. The error being "couldNotGetTypeInfo". I assume this is because "Runtime.Kind.objCClassWrapper" is not supported.

@valleyman86
Copy link
Author

Update: I was able to get objC classes to work by adding support for an ObjCWrapper. I am now able to createInstance on things like UIViewController and UIButtons.

As for this crash I think it is related to accessing typeDescriptor (Nominal Type Descriptor). From what I understand in swift 4.2 this is going away right? Anyways I didn't see Runtime doing much with mangledName so I commented that out (I dont need it for my use case anyways). I then set the properties for the class using ObjC runtime if the rodataPointer lowest bit is 0. According to the docs this will be 1 for swift classes and it seemed to reflect that in my testing.

Long story short I think I got this mostly working now. Maybe I can make a PR for this later but I was honestly unable to get SPM working and cocoapods was out of date so I just manually created a new framework for runtime.

@valleyman86
Copy link
Author

Seems after getting an object created and such I still cant set the values of a swift class that inherits from UIViewController. (Same code as above). No error it just doesn't do anything and the values remain whatever they were. I suspect this is an issue with a class that inherits from NSObject eventually cant have its values set the same way... not quite sure.

@valleyman86
Copy link
Author

valleyman86 commented Mar 18, 2019

Further research into this suggest it may because we need to account for the super class when dealing with the field offsets. I was able to detect that the first field is actually perfectly offset at the value returned by class_getInstanceSize of the superclass.

I found this code in the swift runtime (Metadata.h)

unsigned getFieldOffsetVectorOffset(const ClassMetadata *metadata) const {
    const auto *description = metadata->getDescription();

    if (description->hasResilientSuperclass())
      return metadata->Superclass->getSizeInWords() + FieldOffsetVectorOffset;

    return FieldOffsetVectorOffset;
  }

I guess the main challenge here is that these fields are only used for swift objects.

@wildthink
Copy link

@valleyman86
How did you fix the support for ObjCWrapper.?
#36 (comment)
thanks.

@NSExceptional
Copy link
Contributor

NSExceptional commented Jan 28, 2021

@wickwirew any update on this? I'm running into very simple issues when working with objects, wondering if this is related.

Edit: opened a new issue about this #81

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

No branches or pull requests

4 participants