David Klempfner
David Klempfner

Reputation: 9900

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?

ie. would it be running under the CLR?

Upvotes: 2

Views: 1553

Answers (2)

Hans Passant
Hans Passant

Reputation: 942040

"Managed code" does not help you think about the unsafe keyword correctly. 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. 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.

C# always generates managed code, there is no option to generate methods that the GC cannot probe. Generating unmanaged code in a .NET assembly is possible, the C++/CLI compiler can do that. Implied is that C# code always requires the CLR, you don't have GC without it.

What is highly specific to unsafe code is that it is not verifiable. It uses MSIL instructions that the just-in-time compiler cannot check to ensure that it won't corrupt memory. 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.

Upvotes: 9

giammin
giammin

Reputation: 18958

Managed Code (from 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++. All of these languages share a unified set of class libraries and can be encoded into an Intermediate Language (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. 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.

Otherwise unmanaged code runs outside of the context of the CLR.

Unsafe code still runs under the CLR and it is translated into IL, but it will let you access memory directly through pointers.

Upvotes: 5

Related Questions