very strange TypeInitializationException

Issues related to VMProtect
Post Reply
weloveayaka
Posts: 58
Joined: Wed Jul 05, 2023 6:21 am

very strange TypeInitializationException

Post by weloveayaka »

We used a static property in an unvirtualized class from another DLL in a Windows Application.
The getter of this static property returns the result of a virtualized static function that returns a constant.


this code below is only for code structure

Program

Code: Select all


void Main(string[] args){
 	AppDomain.CurrentDomain.UnhandledException += Program.HandlerUnCatchException;
 	Program.XXX = classB.SomeEnum;    <---- Exception
}



LibraryA:

Code: Select all

public sealed classB
    {
    private static classB CONSTANTS = new classB();
    
    	 private classB()
        {
        	// xxx
        }
        public classB(int x)
        {
            // ...
        }
        
        static string prop = string.Empty;

        
        [Obfuscation(Feature = "virtualization", Exclude = false)]    <------ this getter is not virtualized, but it's not a main problem.
        public static string SomeEnum  
        {
            get
            {
                if (string.IsNullOrEmpty(prop))  <---   TypeInitializationException here
                   {
                   // get prop from another place.
                   }
                return prop;
            }
            set
            {
                prop = value;
            }
        }
    }


LibraryB

Code: Select all

 public class LibraryB
    {
      
        [Obfuscation(Feature = "virtualization", Exclude = false)]
        public static SomeEnum GetEnum()
        {
            return SomeEnum.Something;
        }
    }
On some computers, DLLs would throw a TypeInitializationException.

the stack is

Code: Select all

System.NullReferenceException                      <------- LibraryB
    7BB4AD2E.E62B8B3C()
    7BB4AD2E.BD0CC42C(System.Object[], Int32)    
    <Module>..cctor()

Exception: System.TypeInitializationException       <---------- LibraryA
    LibraryA.ClassB..ctor()
    LibraryA.ClassB..cctor()

However, if we recompiled the DLL with the same codebase and VMP configuration and replaced it, it would not throw the exception anymore. (recompile LibraryB)

Even if I added a 16 character parameter (/ffffffffffffffff, which is meaningless) , it would not report the exception either.

What's even more bizarre is that if I start this program in a debugger, then it won't fail either.


But on some other computers, with the same files (the original file, not the rebuilt one) , compile configuration and run parameters, it would not report any exception.

This sounds like it could be a multi-threading, GC or memory issue.

The VMP configuration is:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<Document Version="2">
    <Protection InputFileName="LibraryB.dll" Options="69888" VMCodeSectionName=".???" VMComplexity="20">
        <Messages />
        <Folders />
        <Procedures />
        <Objects />
    </Protection>
    <Script />
</Document>
we use ObfuscationAttribute to assign virtualized method. so vmp configuration is empty.

Because we are unable to recompile a problematic version, we are unable to provide a minimized code sample that reproduces the issue.
Since this is part of a large software system, it is also very difficult for us to provide a runnable program.

Also, I tried disable DEP on OS, not working. very strange and unpredictable
Admin
Site Admin
Posts: 2586
Joined: Mon Aug 21, 2006 8:19 pm
Location: Russia, E-burg
Contact:

Re: very strange TypeInitializationException

Post by Admin »

Could you send us a very simple example (original binaries + VMP file) that shows your problem?
Post Reply