#[Bug] BadImageFormatException: Expected reference type but got type kind 17

1 messages · Page 1 of 1 (latest)

tiny mural
#

Using RegisterGenericSystemType in the main assembly to register a system from another assembly in a local package leads to the following exception when entering playmode:

Unity.Entities.SystemBaseRegistry+<>c__DisplayClass9_0.<SelectBurstFn>b__0 (System.IntPtr system, System.IntPtr state) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/SystemBaseRegistry.cs:256)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.<>c__DisplayClass9_0:<SelectBurstFn>b__0(IntPtr, IntPtr) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/SystemBaseRegistry.cs:260)
Unity.Entities.UnmanagedUpdate_0000158C$BurstDirectCall:wrapper_native_indirect_000001E426A889B0(IntPtr&, Void*)
Unity.Entities.UnmanagedUpdate_0000158C$BurstDirectCall:Invoke(Void*)
Unity.Entities.WorldUnmanagedImpl:UnmanagedUpdate(Void*) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/WorldUnmanaged.cs:828)
Unity.Entities.WorldUnmanagedImpl:UpdateSystem(SystemHandle) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/WorldUnmanaged.cs:894)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ComponentSystemGroup.cs:729)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ComponentSystemGroup.cs:693)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/SystemBase.cs:418)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)```
#

I was able to reproduce this on a fresh project using Unity 2022.3.5f1 with the following code:
TestType.cs in the MainAssembly

using MainAssembly;
using TestPackage;
using Unity.Entities;

[assembly: RegisterGenericSystemType(typeof(GenericSystem<TestType>))]

namespace MainAssembly {
    public struct TestType {
        public int value;
    }
}

and
GenericSystem.cs in a locally defined package

using Unity.Entities;

namespace TestPackage {
    public partial struct GenericSystem<T> : ISystem where T : unmanaged {
        public void OnUpdate(ref SystemState state) {
            UnityEngine.Debug.Log($"Hello {typeof(T)}");
        }
    }
}

If TestType.cs is simple moved to the package, everything then properly works.

robust star
#

uh hi i probably screwed up. will fix! sorry about that! if i'm right about what happened, later today i can provide a one-line fix if you're willing to modify the entities package

tiny mural
tiny mural
#

Indeed I think I was able to fix it by changing the following line: https://github.com/needle-mirror/com.unity.entities/blob/3f511e712e68abc3c5174b1086f2aefb4dd45c06/Unity.Entities.CodeGen/ISystemPostProcessor.cs#L206

-mod.ImportReference(_targetMethodRef.MakeGenericHostMethod(specializedSystemType));
+mod.ImportReference(_targetMethodRef.MakeGenericHostMethod(LaunderTypeRef(specializedSystemType)));
GitHub

The Entities package provides a modern Entity Component System (ECS) implementation with a basic set of systems and components made for Unity. 📦 [Mirrored from UPM, not affiliated with Unity Techno...

robust star
#

that is exactly the line sir

tiny mural
#

I suppose that I can assume that a similar fix will be included in a future release of the package?

robust star
#

Yep yep

#

Might have missed the deadline for the next one though, will see

tiny mural
#

Actually, I had Brust compilation disabled, and this solution leads to some errors at compile time with Burst:

Burst internal compiler error: System.Exception: Error while generating hash for method references
[...]
System.InvalidOperationException: Could not find type `3d7adc9dd1f0cd241d477bec8b383079` in assembly `Burst.Compiler.IL.AssemblyNameReferenceAndMetadata`
  at Burst.Compiler.IL.Hashing.CacheRuntime.LocalAssemblyState.GetType (Burst.Compiler.IL.Helpers.Hash128& searchFullTypeNameHash) [0x00030] in <532a8a169e6a4953b5d9a4c213b71772>:0 
  at Burst.Compiler.IL.Hashing.CacheRuntime.LocalHashCacheAssemblyStore.GetType (System.String assemblyName, Burst.Compiler.IL.Helpers.Hash128& fullTypeNameHash) [0x00008]

Trying to resolve this 😄

tiny mural
#

Somehow I can't find the source code of Burst.Compiler.IL.Hashing.CacheRuntime.LocalAssemblyState.GetType and I don't think I will be able to make much progress without it; so in the meantime I will stick to Burst disabled 😄

robust star
#

that is probably at least a burst bug

#

possibly a known one

#

but i'll attempt to repro first thing tomorrow

robust star
#

so the odd thing about this is that i have almost exactly this setup in a test

#

which passes, and i can enter play mode in that project just fine

#

oh but you probably don't have disableautocreation on that thing huh

#

that's probably the playmode thing huh

tiny mural
#

I have made a more or less minimal project to reproduce the issue if it helps

robust star
#

ok yeah i think i better get that since i can't seem to make it happen

tiny mural
#

What is the best way to share the project?

robust star
#

a zip is fine. also just double checking what version are you on?

#

oh it says, 1.0.11

#

yeah that's supposed to have the last fix that i made for this

#

(i would also be fine with a repo or whatever is convenient, really)

tiny mural
#

(I guess it would be better to not include automatically generated files, since otherwise the project is more than 1 Go)

robust star
#

it worked! got the error!

#

i notice you don't have fast enter playmode on

#

i suppose it's off by default and this is an empty project

tiny mural
robust star
#

do you also get the burst error in this project?

#

(i haven't)

tiny mural
#

the burst error is only after applying the fix to ISystemPostProcessor

#

but I did not try in this project

robust star
#

yep did that here, no burst error

#

how convinced are you that the burst error is related to the ilpp problem

#

because i've been seeing people mention an error that looks kinda like that around lately

#

and i don't think they were all using generic isystems

tiny mural
#

What seems to be true is that without the fix, I don't get the burst error

robust star
#

well but it's too busy failing to even load the assembly right

#

so it never has a chance to try to burst things and then explode

#

i think the burst issue is a real burst bug, btw

#

just think it might be separate

tiny mural
#

OK I see, it is just a bit strange that the burst erros appears before entring playmode

robust star
#

it's true, although with the IL being malformed, i wonder if burst doesn't just see it's bad and stop trying to burst

tiny mural
#

makes sense 👍

#

I will try to make a minimal reproduction for the burst bug then 😄

#

Also, is the source code of Burst.Compiler.IL.Hashing.CacheRuntime.LocalAssemblyState.GetType public?

robust star
#

sadly not

#

that said

tiny mural
#

ok, makes sense that I did not find it then 😄

robust star
#

you could probably decompile it via ilspy in bcl.exe and similar

#

it's not obfuscated or anything i don't think

#

and c# decompiles really cleanly

tiny mural
#

good idea, thanks 😄
though I am not sure what executable/dll it is from
(it seems not to be importable in my projects for instance)

robust star
#

com.unity.burst/.Runtime

#

but yeah it wouldn't be in a project

#

omg i added com.unity.entities to the testables in manifest.json to your project and got the burst error!

tiny mural
#

nice!
very strange indeed though

robust star
#

aha, searching internally, looks like the fix for the burst issue will be in the next burst release

tiny mural
#

good to know, thanks a lot 😄

robust star
#

thank you for the repro! found a bug in the test that i thought covered this

tiny mural
#

Hello, it seems like the issue is still present in the newly released 1.0.16 version.
It is not really urgent but I am saying this just in case this issue was forgotten 😄

tiny mural
#

@robust star Any news regarding this?
It seems that 1.1.0-exp.1 does not contain a fix for the issue 😢

Also after testing, I can confirm that the burst hashing issue is definitely related to the tentative fix I made for the generic system.

robust star
#

yeah it's been blocked on the burst hashing issue, which i have had no luck getting people to deal with

#

but thanks for the prod, i'll try again

tiny mural
#

Alright, thanks a lot 🙏

tiny mural
#

Hello,
Seeing the fix is still not included in entities 1.2.1, I guess either this was forgotten or the fix is causing issues.
Anyways, I created a bug report IN-75454; hopping this will help the process.
(Also the burst hashing issue is also still present)

tiny mural
#

[Bug] BadImageFormatException: Expected reference type but got type kind 17

robust star
#

sorry i did drop the ball and thanks for the ping!

robust star
#

OH MY GOD THE BURST BUG IS STILL THERE. well, i'll bug burst people 😦

tiny mural
#

Thanks a lot! I tried understanding the issue more, but I don't think I can do much more (for instance I was not able to see how the generated IL differs with the proposed fix (using the IL inspector from Rider)).

robust star
#

yeah it's an extremely arcane issue that i only figured out after inspecting the actual CIL spec

tiny mural
#

yeah I saw this link in the ISystemPostProcessor comments, but what does not make sense to me is that:

The resolution scope shall be an assembly reference
syntactically represented using the notation [AssemblyRefName]
This seems to be correct even without my change (by inspecting IL in the DLLs generated in Library/ScriptAssemblies)
The referenced
assembly shall be declared in the manifest for the current assembly (§II.6.3), the
type shall be declared in the referenced assembly’s manifest, and the type shall be
marked as exported from that assembly (§II.6.7 and §II.10.1.1).
I don't see how my change would improve this, as using LaunderTypeRef once on a type in an assembly should be enough to ensure that?

I guess it might be a bit out of reach for me to truly understand what is going on here 😅

formal ore
#

@robust star bit of a bump, but I just ran into this

System.TypeInitializationException: The type initializer for 'Unity.Entities.CodeGeneratedRegistry.AssemblyTypeRegistry' threw an exception. ---> System.BadImageFormatException: Expected reference type but got type kind 17
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Reflection.RuntimeFieldInfo.GetValueInternal(System.Reflection.RuntimeFieldInfo,object)
at System.Reflection.RuntimeFieldInfo.GetValue (System.Object obj) [0x0005e] in <7d32335d397147e2804452d40e5cb9f4>:0
at Unity.Entities.TypeManager.RegisterStaticAssemblyTypes (System.Reflection.Assembly[] assemblies, System.Collections.Generic.HashSet1[System.Type]& componentTypeSet, System.Collections.Generic.List1[System.Type]& typesToReprocess) [0x0007d] in .\Packages\com.unity.entities\Unity.Entities\Types\TypeManager.cs:3599
at Unity.Entities.TypeManager.InitializeAllComponentTypes () [0x00057] in .\Packages\com.unity.entities\Unity.Entities\Types\TypeManager.cs:2730
at Unity.Entities.TypeManager.Initialize () [0x00250] in .\Packages\com.unity.entities\Unity.Entities\Types\TypeManager.cs:1612
at Unity.Entities.BindingRegistry.Initialize () [0x00001] in .\Packages\com.unity.entities\Unity.Entities.Hybrid\Bindings\BindingRegistry.cs:83
at Unity.Entities.BindingRegistry..cctor () [0x00028] in .\Packages\com.unity.entities\Unity.Entities.Hybrid\Bindings\BindingRegistry.cs:76
UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes (System.ReadOnlySpan`1<intptr>)

narrowed it down to updating before one of Rukhankas generic systems

[UpdateBefore(typeof(AnimatorControllerSystem<AnimatorControllerQuery>))] 
public partial struct RukhankaAnimatorParameterTrackSystem : ISystem```
which looking at package is registered
`[assembly: RegisterGenericSystemType(typeof(AnimatorControllerSystem<AnimatorControllerQuery>))]`
#

removing the UpdateBefore and everything is fine - i don't really need this UpdateBefore anyway it's not blocking, just a very obscure error

robust star
#

i think this is actually a separate instance of the same mistake; the original fix did land iirc

#

but anyway, good catch and will fix the new one. if you could attach a zip of your scriptassemblies it might speed it up a bit, but i bet i can get it either way

formal ore
#

i can send you it to you on slack if you want