Fix unity pointer paths on mono v2/v3 that have generic classes#132
Open
mitchell-merry wants to merge 11 commits into
Open
Fix unity pointer paths on mono v2/v3 that have generic classes#132mitchell-merry wants to merge 11 commits into
mitchell-merry wants to merge 11 commits into
Conversation
mitchell-merry
commented
May 10, 2026
Comment on lines
+67
to
+82
| // See https://github.com/mono/mono/blob/337052f86112fc0dc8435c5c4a2de43b399a14bb/mono/metadata/class-internals.h#L327 | ||
| Version::V2 => { | ||
| // TODO I feel like I'm doing this very poorly | ||
|
|
||
| let byte = | ||
| process.read::<u8>(self.class + module.offsets.class.class_kind)? & 0x7u8; | ||
|
|
||
| if !MonoTypeKind::is_valid_bit_pattern(&byte) { | ||
| return Err(Error {}); | ||
| } | ||
|
|
||
| // SAFETY: We just checked if this was valid | ||
| let kind: MonoTypeKind = unsafe { (&raw const byte).cast::<MonoTypeKind>().read() }; | ||
|
|
||
| Ok(kind) | ||
| } |
Contributor
Author
There was a problem hiding this comment.
Er, my rust noob-ness is showing here. I'm trying to read just these three bits into a MonoTypeKind, but I suspect this can be done much simpler than I've done it here... could use help on that
Maybe I should drop the enum and just use a u8?
mitchell-merry
commented
May 10, 2026
Comment on lines
+131
to
+132
| class_kind: 0x1E, | ||
| generic_class: 0x94, |
Contributor
Author
There was a problem hiding this comment.
These are the untested offsets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why, what is the context?
Trying to fetch field offsets on generic classes in mono v2/v3, e.g.
class MyClass : Singleton<MyClass>doesn't work since the field count logic is different on for generic classes in those versions.What?
Add v2/v3 logic, which fetches the class kind and then if it's GINST fetches the generic container class.
This logic is stolen from a variety of places, but in particular asl-help does this: https://github.com/just-ero/asl-help/blob/9a17c5ec7108aba1fe1932358703f4e6adaa6b2c/src/Unity/Mono/Managers/MonoV2Manager.cs#L16 and I also had a look at the mono source to verify it: https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/metadata/class-accessors.c#L216
Testing
I tested this on:
Additional thoughts
Not exactly sure what the best practice is in rust for this, but I think we should be using some sort of strategy pattern similar to what asl-help does. Having these
match (mono version)cases everywhere is going to get convoluted, and in this case I've even had to include offsets that don't even exist in mono v1 (and thus I've defaulted them to 0x0).