MobiVM / robovm

Ahead of time compiler for JVM bytecode targetting iOS, Mac OSX and Linux
https://mobivm.github.io
942 stars 132 forks source link

* fixed: libgdx/7212 -- crash when getting value by pointer, affected by GC #743

Closed dkimitsa closed 10 months ago

dkimitsa commented 10 months ago

Source

https://github.com/libgdx/libgdx/issues/7212

root case

GC code was setting up mark handler only for direct subclass of Struct class to mark its native structure content during GC mark phase. As result memory behind native struct area was release and available for allocations. As result this area was taken by next requested object/data. That caused crashed related to ObjC marshalling, like objc[503]: Attempt to use unknown class 0x104094d80. But all pointer extended intermediate classes after Struct: in this case NSErrorPtr->Ptr->Struct

Fix

Code changed to check for subclass instead direct subclass.

Minimal sample to reproduce.

void test2() {
    NSObjectPtr ptr = new NSObjectPtr();
    NSObject o = new NSObject();
    ptr.set(o);
    for (int i = 0; i < 100_000_000;i++) {
        new NSObject();
        if (VM.getLong(ptr.getHandle()) != o.getHandle()) {
            throw new RuntimeException(ptr.getClass() + " " + Long.toHexString(ptr.getHandle()) + "-" + Long.toHexString(VM.getLong(ptr.getHandle())));
        }
    }
}