简体   繁体   English

是否管理C#不安全代码?

[英]Is C# unsafe code managed?

If you wrote a C# program and part of the code was written using the unsafe key word, would that code still be considered "managed" code? 如果您编写了C#程序,并且部分代码是使用unsafe关键字编写的,那么该代码是否仍被视为“托管”代码?

ie. 即。 would it be running under the CLR? 它会在CLR下运行吗?

"Managed code" does not help you think about the unsafe keyword correctly. “托管代码”不能帮助您正确考虑unsafe关键字。 The most fundamental aspect about managed code is that it is compatible with the garbage collector. 关于托管代码的最基本方面是它与垃圾回收器兼容。 It must be able to find object references back that are used by that code so it can properly determine whether the object is in use. 它必须能够找到该代码使用的对象引用,以便可以正确确定该对象是否正在使用。 That requires one very specific detail, at runtime the GC must be able to find a table back that describes where object references are stored. 这需要一个非常具体的细节,在运行时,GC必须能够找到一张表,该表描述了对象引用的存储位置。 Local variables and method arguments are the tricky ones. 局部变量和方法参数是棘手的。 That table is generated by the just-in-time compiler or by an ahead-of-time compiler like Ngen.exe or .NET Native. 该表是由即时编译器或Ngen.exe或.NET Native等提前编译器生成的。

C# always generates managed code, there is no option to generate methods that the GC cannot probe. C# 始终生成托管代码,没有选项来生成GC无法探测的方法。 Generating unmanaged code in a .NET assembly is possible, the C++/CLI compiler can do that. 可以在.NET程序集中生成非托管代码,C ++ / CLI编译器可以做到这一点。 Implied is that C# code always requires the CLR, you don't have GC without it. 暗示C#代码始终需要CLR,没有它就没有GC。

What is highly specific to unsafe code is that it is not verifiable . unsafe代码的高度特定之处在于它不可验证 It uses MSIL instructions that the just-in-time compiler cannot check to ensure that it won't corrupt memory. 它使用即时编译器无法检查的MSIL指令来确保它不会破坏内存。 Pointers being the most obvious case, the jitter cannot know if a pointer dereference is safe, it doesn't know the pointer value yet. 指针是最明显的情况,抖动无法知道指针取消引用是否安全,它尚不知道指针值。

The consequence of unverifiable code is that somebody can load your assembly in a sandbox and insist that all code must be verifiable. 无法验证的代码的结果是,有人可以将您的程序集加载到沙箱中,并坚持要求所有代码都必须是可验证的。 It won't work. 它不会工作。 And the ultimate consequence of course, you fumble the code and write to an arbitrary address in memory, producing a very difficult bug to diagnose. 当然,最终的结果是,您弄乱了代码并写入内存中的任意地址,从而产生了非常难以诊断的错误。 Losing a week of your life finding that bug is expected. 损失了一周的生命才发现该错误是可以预期的。

Managed Code (from MSDN): 托管代码 (来自MSDN):

Managed code is code written in one of over twenty high-level programming languages that are available for use with the Microsoft .NET Framework, including C#, J#, Microsoft Visual Basic .NET, Microsoft JScript .NET, and C++. 托管代码是使用二十多种高级编程语言之一编写的代码,这些语言可与Microsoft .NET Framework一起使用,包括C#,J#,Microsoft Visual Basic .NET,Microsoft JScript .NET和C ++。 All of these languages share a unified set of class libraries and can be encoded into an Intermediate Language (IL). 所有这些语言共享一组统一的类库,并且可以被编码为中间语言(IL)。 A runtime-aware compiler compiles the IL into native executable code within a managed execution environment that ensures type safety, array bound and index checking, exception handling, and garbage collection. 运行时感知的编译器将IL编译为托管执行环境中的本机可执行代码,以确保类型安全,数组绑定和索引检查,异常处理以及垃圾回收。 By using managed code and compiling in this managed execution environment, you can avoid many typical programming mistakes that lead to security holes and unstable applications. 通过使用托管代码并在此托管执行环境中进行编译,您可以避免许多典型的编程错误,这些错误会导致安全漏洞和不稳定的应用程序。 Also, many unproductive programming tasks are automatically taken care of, such as type safety checking, memory management, and destruction of unneeded objects. 同样,许多非生产性的编程任务会自动执行,例如类型安全检查,内存管理和不需要的对象的销毁。

Managed code runs under supervision of the CLR which is responsible among others for memory management and garbage collection. 托管代码在CLR的监督下运行,而CLR负责内存管理和垃圾回收。

Otherwise unmanaged code runs outside of the context of the CLR. 否则, 非托管代码将在CLR上下文之外运行。

Unsafe code still runs under the CLR and it is translated into IL, but it will let you access memory directly through pointers. 不安全的代码仍在CLR下运行,并且已转换为IL,但是它将使您直接通过指针访问内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM