Briefly managed execution and CLI

The processor can not be interpreted directly assemblies. Assembly with another language, i.e., the common intermediate language (Common Intermediate Language, CIL), otherwise known as an intermediate language (of IL) . 1 .

C # C # compiler converts the source code into an intermediate language file. To convert CIL code into machine code processor can be understood, must complete an additional step (typically performed at run-time). This step involves an important element of C # program execution: VES (Virtual Execution System, a virtual execution system). When VES also called run (runtime).

It needs to be compiled according to the CIL code, a process called in-time compilation, or JIT compiler (just-in-time compilation). The code executes as "run time" of such an "agent" in this context, is called managed code (managed code), the process is performed under the control "run time" is referred to as a managed execution (managed execution).

It is called "managed", because "runtime" manages aspects such as memory allocation, security and JIT compiler, so as to control the main program behavior. No code "run time" is referred to as native code (native code) or unmanaged code (unmanaged code) is executed.

Description: "Running" may refer to both "program execution" may also refer to a "virtual execution system." For clarity, use the "execution" stands for "program execution", with "run-time" indicates the agent responsible for managing the execution of a C # program. 2

"Run time" in the specification includes a specification of the wider inclusive, i.e. CLI (Common Language Infrastructure, Common Language Infrastructure) specification. As an international standard, CLI specification includes the following aspects:

  • VES or "runtime."
  • CIL。
  • Support for language interoperability type system, called CTS (Common Type System, a common type system).
  • How to write guidelines for CLI compatible language by accessing the library, this part of the concrete on the Common Language Specification (Common Language Specification, CLS) in.
  • CLI can make various services identified metadata (including file layout or assembly format specification).
  • The context of the execution engine to run in the "Run", the programmer need not write code directly can use several services and features, including:

    • Language Interoperability: Interoperability between different source language. Each language compiler converts the source language into the same intermediate language (the CIL) to achieve this interoperability.
    • Type safety: check room type conversion, in order to ensure compatibility of the type of conversion. This helps to prevent buffer overflow (which is the main reason for security risk).
    • Code Access Security: The assembly code that developers have the right to prove executed on a computer.
    • Garbage collection: A memory management mechanism, the automatic release of Space "run-time" data distribution.
    • Platform portability: the same assembly can run on multiple operating systems. To achieve this, an obvious limitation is that you can not use platform-specific libraries. So the platform dependent issues to be addressed separately.
    • BCL (Base Class Library): Provides developers can (in all .NET Framework) rely on large code bases, so that it does not have to write the code yourself.

Note: Benpian simply introduces the CLI, the purpose is to make the reader familiar with the C # implementation of environmental programs. Also herein, this will also mention some of the terms used in this blog series behind the text. In the time is right, I will specifically CLI summarizes its relationship with the C #.

CIL and ILDASM

As mentioned above, C # C # compiler converts the code into machine code instead of CIL code. The processor only understands machine code, so the CIL code must be converted into machine code to be executed by the processor. Available CIL disassembler program set deconstructed into CIL. Commonly used Microsoft-specific file name to call this ILDASM CIL disassembler (IL Disassembler ILDASM is short for), it can disassemble the assembly performs extraction C # compiler generates CIL.

Results disassembler .NET assembly easier to understand than the machine code. Many developers fear that others do not even get the source code, the program is also easy to disassemble and exposed its algorithm. In fact, whether based CLI, any procedures to prevent decompiling the only safe way is to disable access to the compiled program (for example, only programs stored on the site, without distributing it to the user's machine).

However, if the purpose is to reduce the likelihood of someone else for the source code, consider using some confusion (Obfuscator) product. This product opens the IL code, but the same is converted into a functional form understood more difficult. This prevents the average developer access code, the assembly is difficult to decompile the code easy to understand. Unless the program requires algorithms for advanced security protection, otherwise confusion is enough.

View CIL's output myApp.dll

myApp.dll program please refer to this article: https://www.vinanysoft.com/c-sharp-basics/introducing/start-with-hello-world/

After the installation ILDASM Visual Studio is installed by default, the location is: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A(随便一个目录)\bin\NETFX 4.8 Tools(随便一个目录)\x64.

Double click ildasm.exe, the myApp.dll dragged into, as follows:

Double-click to view the IL code, the following IL code is MANIFEST

// Metadata version: v4.0.30319
.assembly extern System.Runtime
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
  .ver 4:2:1:0
}
.assembly extern System.Console
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
  .ver 4:1:1:0
}
.assembly myApp
{
  .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                   63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.

  // --- 下列自定义特性会自动添加,不要取消注释 -------
  //  .custom instance void [System.Runtime]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) 

  .custom instance void [System.Runtime]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 18 2E 4E 45 54 43 6F 72 65 41 70 70 2C 56   // ....NETCoreApp,V
                                                                                                              65 72 73 69 6F 6E 3D 76 33 2E 30 01 00 54 0E 14   // ersion=v3.0..T..
                                                                                                              46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61 79   // FrameworkDisplay
                                                                                                              4E 61 6D 65 00 )                                  // Name.
  .custom instance void [System.Runtime]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 05 6D 79 41 70 70 00 00 )                   // ...myApp..
  .custom instance void [System.Runtime]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 05 44 65 62 75 67 00 00 )                   // ...Debug..
  .custom instance void [System.Runtime]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )             // ...1.0.0.0..
  .custom instance void [System.Runtime]System.Reflection.AssemblyInformationalVersionAttribute::.ctor(string) = ( 01 00 05 31 2E 30 2E 30 00 00 )                   // ...1.0.0..
  .custom instance void [System.Runtime]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 05 6D 79 41 70 70 00 00 )                   // ...myApp..
  .custom instance void [System.Runtime]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 05 6D 79 41 70 70 00 00 )                   // ...myApp..
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}
.module myApp.dll
// MVID: {29FC93A2-9A52-445C-A581-09AA5BCC11C7}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x000002015D090000

.class private auto ansi beforefieldint

.class private auto ansi beforefieldinit myApp.Program
       extends [System.Runtime]System.Object
{
} // end of class myApp.Program

.ctor:void()

.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // 代码大小       8 (0x8)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void [System.Runtime]System.Object::.ctor()
  IL_0006:  nop
  IL_0007:  ret
} // end of method Program::.ctor

Main:void(string[])

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // 代码大小       42 (0x2a)
  .maxstack  2
  .locals init (valuetype [System.Runtime]System.DateTime V_0)
  IL_0000:  nop
  IL_0001:  ldstr      "Hello World!"
  IL_0006:  call       void [System.Console]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  ldstr      "The current time is "
  IL_0011:  call       valuetype [System.Runtime]System.DateTime [System.Runtime]System.DateTime::get_Now()
  IL_0016:  stloc.0
  IL_0017:  ldloca.s   V_0
  IL_0019:  call       instance string [System.Runtime]System.DateTime::ToString()
  IL_001e:  call       string [System.Runtime]System.String::Concat(string,
                                                                    string)
  IL_0023:  call       void [System.Console]System.Console::WriteLine(string)
  IL_0028:  nop
  IL_0029:  ret
} // end of method Program::Main

The most is the beginning of the list (manifest) information. These include the anti-compiled modules full name (myApp) Not only that, it also includes all dependent modules and assemblies and version information.

CIL code based on such a list, the most interesting possibility is that we can relatively easily understand what the program does, it is easy to read and understand than machine code (assembler) more.

The code arisen for System.Console.WriteLine()explicit reference. CIL code list contains a number of external information, but if a developer wants to understand C # module (or any CLI-based program) internal works, but can not get the source code, as long as the author does not use an obfuscator, understand this CIL code the list is relatively easy.

In fact, some free tools (such as Red Gate Reflector, ILSpy, JustDecompile, dotPeek and CodeReflect) CIL will automatically be able to decompile C #.

Use ILSpy view the decompiled code myApp.dll

ILSpy address:https://github.com/icsharpcode/ILSpy

Double click ILSpy.exe, the myApp.dll dragged into, as follows:

Note: Disassembly difference (disassemble), and decompilation (decompile) a. Disassembly assembly code is obtained, resulting decompilation is the language of the source code.

Original link: https://www.vinanysoft.com/c-sharp-basics/introducing/managed-execution-and-the-common-language-infrastructure/


  1. CIL's third argument is Microsoft IL (MSIL). This blog uses the term CIL, because it is a standard CLI adopted. C # programmers often use when communicating IL term, because they assume that means CIL IL intermediate language rather than the other.

  2. "Runtime" (runtime) all add quotation marks when used as a noun.

Guess you like

Origin www.cnblogs.com/vin-c/p/12011943.html