yck1509 / ConfuserEx

An open-source, free protector for .NET applications
http://yck1509.github.io/ConfuserEx/
Other
3.57k stars 1.64k forks source link

System.TypeInitializationException on Mono #535

Open tyronx opened 8 years ago

tyronx commented 8 years ago

Heyho trying to run my obfuscated binaries on mono (windows or linux) gives me this exception:

Unhandled Exception: System.TypeInitializationException: The type initializer for '[Module]' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object at .‮‮‎‫‎‭​‪‫‎‫‌​‭‭‭‭‌‮‭‪‫‬‫‪‪‮ () <0x40884310 + 0x00874> in :0 at ..cctor () <0x40884100 + 0x00017> in :0 --- End of inner exception stack trace --- at Vintagestory.Server.Program.Main (System.String[] args) <0x408814c0 + 0x002bb> in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for '' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object at .‮‮‎‫‎‭​‪‫‎‫‌​‭‭‭‭‌‮‭‪‫‬‫‪‪‮ () <0x40884310 + 0x00874> in :0 at ..cctor () <0x40884100 + 0x00017> in :0 --- End of inner exception stack trace --- at Vintagestory.Server.Program.Main (System.String[] args) <0x408814c0 + 0x002bb> in :0

The unobfuscated version runs fine, and both the obfuscated and nonobfuscated runs fine on windows using the .net framework.

Mono version: 4.4.2 Compiled with target framework: 4.5.2 Confused with ConfuserEx v1.0.0 using default settings (at least for the binary itself)

I could try to reduce the bug to a minimal test case, but I don't know where to start with this odd exception being thrown. Also I could swear it already worked once, but apparently some change made in incompatible. Any ideas how I could trace this?

tyronx commented 8 years ago

Here is the static constructor that I see in the decompiled code using dotPeek:

`public static class _fXDVcL5EsSl2qroOUDl3n2bvJUI { public static string _1wKDbWmthkUZNtrbPuC6genyfuJ = \u003CModule\u003E.\u202D‏‭‏‏‭‫‍‌‏‭‭‬​‪‬‌‭‪‌‫‭​‌‪‫‮(595118489U); public static string _ZCiYOY15Kh1Cj7LwGrx5G2f7dPO; public static string _DkYD7senwKYi1KYMe9vPsZO68fe; public static string _102koeFZeHXWEOpBYHqeOjIS2ly; public static string _z0eBuqbgx2k7CswTy5PHl1mv7Mj; public static string _n6MB2NNBwwz0Da01IzlTA27yKkp; public static string _WfFJzdd4g4Us9Z04BV3LjqWO3Jg; public static string _Raitofo9akpGcspBFIZn0IHSDN8; public static string _Lef1hfls2Q9kA3m1ZU44u9DfZJc;

static _fXDVcL5EsSl2qroOUDl3n2bvJUI() { label_1: int num1 = -1418751270; while (true) { int num2 = -619600823; uint num3; switch ((num3 = (uint) (num1 ^ num2)) % 7U) { case 0: _fXDVcL5EsSl2qroOUDl3n2bvJUI._DkYD7senwKYi1KYMe9vPsZO68fe = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u202D‏‭‏‏‭‫‍‌‏‭‭‬​‪‬‌‭‪‌‫‭​‌‪‫‮(2321357424U)); num1 = (int) num3 * -1469424329 ^ 1151036620; continue; case 2: _fXDVcL5EsSl2qroOUDl3n2bvJUI._n6MB2NNBwwz0Da01IzlTA27yKkp = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u206D‪‬​‮‬‬‮​‪‭‏‮‪‍‫‏‫‌‬‮‍‭​‮​‍‬‎‪‌‮(1231513991U)); num1 = (int) num3 * 746992650 ^ -1780078258; continue; case 3: goto label_1; case 4: _fXDVcL5EsSl2qroOUDl3n2bvJUI._102koeFZeHXWEOpBYHqeOjIS2ly = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u206C‪‪‍‬​‍‪‬‏‍‬‭‏‬‏‫​‭‭‍‭‬‭‬​​‮(1079795841U)); _fXDVcL5EsSl2qroOUDl3n2bvJUI._z0eBuqbgx2k7CswTy5PHl1mv7Mj = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u206C‪‪‍‬​‍‪‬‏‍‬‭‏‬‏‫​‭‭‍‭‬‭‬​​‮(3877922168U)); num1 = (int) num3 * 444357651 ^ 600548940; continue; case 5: _fXDVcL5EsSl2qroOUDl3n2bvJUI._WfFJzdd4g4Us9Z04BV3LjqWO3Jg = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u200B‭‏‬‭‬‏‍‭‍‫‭‪‫‍‎‮‮‎‎‏‪‎‬‍‪‮(3676568306U)); _fXDVcL5EsSl2qroOUDl3n2bvJUI._Raitofo9akpGcspBFIZn0IHSDN8 = _fXDVcL5EsSl2qroOUDl3n2bvJUI.\u200B‭‎​‭‌‫‭‪‌‮‪‫‏‪‎​​‍‫‪‎‮(_fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(), \u003CModule\u003E.\u200B‭‏‬‭‬‏‍‭‍‫‭‪‫‍‎‮‮‎‎‏‪‎‬‍‪‮(439950324U)); num1 = (int) num3 * 223681179 ^ 1426166855; continue; case 6: _fXDVcL5EsSl2qroOUDl3n2bvJUI._ZCiYOY15Kh1Cj7LwGrx5G2f7dPO = _fXDVcL5EsSl2qroOUDl3n2bvJUI._jCSWYeKT8uVDNHhPNycaUM0DIgv(); num1 = (int) num3 * 2117652869 ^ 517834334; continue; default: goto label_8; } } label_8: _fXDVcL5EsSl2qroOUDl3n2bvJUI._Lef1hfls2Q9kA3m1ZU44u9DfZJc = \u003CModule\u003E.\u200B‭‏‬‭‬‏‍‭‍‫‭‪‫‍‎‮‮‎‎‏‪‎‬‍‪‮(4174957943U); }`

tyronx commented 8 years ago

What is this stuff with <Modue>.<string>? Is that valid c# syntax?

XenocodeRCE commented 8 years ago

<Modue>.<string>

is perfectly fine as far as C# syntax is concerned, so it's problably not the issue here. <Module> is the global type constructor, the very first executed method, way way before the Main method in the Program.cs class. <string> is a Type, and in-between you have a method named with some unprintable characters, tha's why you don't see them actually.

ConfuserEX include a new stack trace decoder, you can use it to , maybe, understand clearly what is going on :)

tyronx commented 8 years ago

Thank you for the response! Is there any documentation on how the stack trace decode works? A quick google search didn't give me any result.

tyronx commented 8 years ago

Update: Also happens under Windows Mono. Adjusting title..

tyronx commented 8 years ago

Here is the full mono trace where the exception happens:

[000000000000246C: 1.24378 3] ENTER: (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)(0000000000000000, 0000000000000000, 000000000019D500, 000000000424A360, ) [000000000000246C: 1.24380 4] ENTER: :.cctor ()() [000000000000246C: 1.24540 5] ENTER: :‏‪‬‏‌‍‌‎​‏‪‭‎‌‫‎‎‮‎‬‌‎​‍‭‮ ()() [000000000000246C: 1.24554 6] ENTER: System.RuntimeType:get_Module ()(this:0000000002C6F160[System.RuntimeType VintagestoryServer.exe], ) [000000000000246C: 1.24563 7] ENTER: System.RuntimeType:GetRuntimeModule ()(this:0000000002C6F160[System.RuntimeType VintagestoryServer.exe], ) [000000000000246C: 1.24577 8] ENTER: (wrapper managed-to-native) System.RuntimeTypeHandle:GetModule (System.RuntimeType)([TYPE:], ) [000000000000246C: 1.24580 8] LEAVE: (wrapper managed-to-native) System.RuntimeTypeHandle:GetModule (System.RuntimeType)[System.Reflection.MonoModule:0000000002422730] [000000000000246C: 1.24582 7] LEAVE: System.RuntimeType:GetRuntimeModule ()[System.Reflection.MonoModule:0000000002422730] [000000000000246C: 1.24583 6] LEAVE: System.RuntimeType:get_Module ()[System.Reflection.MonoModule:0000000002422730] [000000000000246C: 1.24597 6] ENTER: System.Reflection.MonoModule:get_FullyQualifiedName ()(this:0000000002422730[System.Reflection.MonoModule VintagestoryServer.exe], ) [000000000000246C: 1.24600 7] ENTER: (wrapper managed-to-native) System.Security.SecurityManager:get_SecurityEnabled ()() [000000000000246C: 1.24601 7] LEAVE: (wrapper managed-to-native) System.Security.SecurityManager:get_SecurityEnabled ()result=0 [000000000000246C: 1.24603 6] LEAVE: System.Reflection.MonoModule:get_FullyQualifiedName ()[System.String:00000000024227D0] [000000000000246C: 1.24617 6] ENTER: System.Runtime.InteropServices.Marshal:GetHINSTANCE (System.Reflection.Module)([System.Reflection.MonoModule:0000000002422730], ) [000000000000246C: 1.24644 7] ENTER: (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)(0000000000000000, 0000000000000000, 000000000019C940, 000000000424BF90, ) [000000000000246C: 1.24647 8] ENTER: System.Reflection.Module:.cctor ()() [000000000000246C: 1.24649 9] ENTER: (wrapper alloc) object:AllocSmall (intptr,intptr)(0000000004B3A128, 0000000000000070, ) [000000000000246C: 1.24651 9] LEAVE: (wrapper alloc) object:AllocSmall (intptr,intptr)[System.Reflection.TypeFilter:0000000002422900] [000000000000246C: 1.24653 9] ENTER: (wrapper alloc) object:AllocSmall (intptr,intptr)(0000000004B3A128, 0000000000000070, ) [000000000000246C: 1.24654 9] LEAVE: (wrapper alloc) object:AllocSmall (intptr,intptr)[System.Reflection.TypeFilter:0000000002422970] [000000000000246C: 1.24656 8] LEAVE: System.Reflection.Module:.cctor () [000000000000246C: 1.24657 7] LEAVE: (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)[OBJECT:0000000000000000] [000000000000246C: 1.24659 7] ENTER: System.Reflection.Module:op_Equality (System.Reflection.Module,System.Reflection.Module)([System.Reflection.MonoModule:0000000002422730], 0000000000000000, ) [000000000000246C: 1.24660 7] LEAVE: System.Reflection.Module:op_Equality (System.Reflection.Module,System.Reflection.Module)result=0 [000000000000246C: 1.24676 7] ENTER: (wrapper managed-to-native) System.Reflection.Module:GetHINSTANCE (System.Reflection.Module)([System.Reflection.MonoModule:0000000002422730], ) [000000000000246C: 1.24678 7] LEAVE: (wrapper managed-to-native) System.Reflection.Module:GetHINSTANCE (System.Reflection.Module)result=-1 [000000000000246C: 1.24680 6] LEAVE: System.Runtime.InteropServices.Marshal:GetHINSTANCE (System.Reflection.Module)result=-1 [000000000000246C: 1.24713 6] ENTER: (wrapper runtime-invoke) object:runtime_invoke_voidthis (object,intptr,intptr,intptr)([System.NullReferenceException:00000000024229E0], 0000000000000000, 0000000000000000, 000000000424C240, ) [000000000000246C: 1.24716 7] ENTER: System.NullReferenceException:.ctor ()(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], ) [000000000000246C: 1.24718 8] ENTER: System.SystemException:.ctor (string)(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], [STRING:0000000002C301D0:Object reference not set to an instance of an object.], ) [000000000000246C: 1.24720 9] ENTER: System.Exception:.ctor (string)(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], [STRING:0000000002C301D0:Object reference not set to an instance of an object.], ) [000000000000246C: 1.24722 10] ENTER: System.Exception:Init ()(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], ) [000000000000246C: 1.24723 11] ENTER: (wrapper alloc) object:AllocSmall (intptr,intptr)(000000000044B350, 0000000000000038, ) [000000000000246C: 1.24725 11] LEAVE: (wrapper alloc) object:AllocSmall (intptr,intptr)[System.Runtime.Serialization.SafeSerializationManager:0000000002422AE8] [000000000000246C: 1.24726 10] LEAVE: System.Exception:Init () [000000000000246C: 1.24727 9] LEAVE: System.Exception:.ctor (string) [000000000000246C: 1.24728 9] ENTER: System.Exception:SetErrorCode (int)(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], -2146233087, ) [000000000000246C: 1.24730 9] LEAVE: System.Exception:SetErrorCode (int) [000000000000246C: 1.24731 8] LEAVE: System.SystemException:.ctor (string) [000000000000246C: 1.24732 7] LEAVE: System.NullReferenceException:.ctor () [000000000000246C: 1.24733 6] LEAVE: (wrapper runtime-invoke) object:runtime_invoke_voidthis (object,intptr,intptr,intptr)[OBJECT:0000000000000000] [000000000000246C: 1.24736 6] ENTER: (wrapper runtime-invoke) :runtime_invoke_objectthis (object,intptr,intptr,intptr)([System.NullReferenceException:00000000024229E0], 0000000000000000, 000000000019BAC0, 000000000420D270, ) [000000000000246C: 1.24738 7] ENTER: System.Exception:get_Message ()(this:00000000024229E0[System.NullReferenceException VintagestoryServer.exe], ) [000000000000246C: 1.24740 7] LEAVE: System.Exception:get_Message ()[System.String:0000000002422B20] [000000000000246C: 1.24741 6] LEAVE: (wrapper runtime-invoke) :runtime_invoke_objectthis (object,intptr,intptr,intptr)[System.String:0000000002422B20] [000000000000246C:] EXCEPTION handling: System.NullReferenceException: Object reference not set to an instance of an object EXCEPTION: catch found at clause 0 of (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr) [000000000000246C: 1.24747 5] LEAVE: (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)[OBJECT:0000000000000000] [000000000000246C: 1.24789 5] ENTER: (wrapper runtime-invoke) :runtime_invoke_void__this___object_object (object,intptr,intptr,intptr)([System.TypeInitializationException:0000000002422C08], 000000000019D440, 0000000000000000, 000000000424C4B0, ) [000000000000246C: 1.24792 6] ENTER: System.TypeInitializationException:.ctor (string,System.Exception)(this:0000000002422

tyronx commented 8 years ago

Looking at the trace, it looks to me like it seems to have problems loading an external libary altogether?

tyronx commented 7 years ago

Update: This exception only happens on obfuscation level Maximum, when lowered to Aggressive it loads fine.