What are the implications of using unsafe code
Asked Answered
C

3

25

Aside from the fact that the code itself can access memory directly. What are the other implications of using the "/unsafe" compiler flag and the "fixed" keyword? Are there any knock on effects related to code signing and deployment of my .exe (my app is desktop only)?

(This isn't about whether or not I should be doing this, the why is covered in my question here)

Cantle answered 1/4, 2009 at 17:5 Comment(0)
H
7

Unsafe code is not verifiable, so you have to be aware of that. In a Full Trust environment, that's not a big deal, but if you have other environments which have a more restricted permission set, then this might impact you there.

Hashish answered 1/4, 2009 at 17:20 Comment(0)
S
16

You can put the implications into two buckets.

The first is how it affects your application environment. Using unsafe code requires that your assembly be run in a full trust environment. It's not possible to run in a restricted environment such as certain Click Once security settings. The reason being that unsafe code prevents the CLR from ensuring type safety. Click Once though with no security restrictions should not have a problem.

The second is what it means for the way you code. Using unsafe code typically involves using pointers and in particular, using them to performed advanced marshalling via PInvoke. There is nothing inherently wrong with either of these actions though. It just requires significantly more understanding of the CLR and marshalling than "safe" code does. Object pinning is a great example of knowledge you'd need to have a firm grasp on before you started using these features.

Slow answered 1/4, 2009 at 17:26 Comment(1)
I would have +1'ed, but to be pedantic, pinning is absolutely not a necessity when using fixed buffers with unsafe. Maybe you can edit your question to reflect that.Dioptase
H
7

Unsafe code is not verifiable, so you have to be aware of that. In a Full Trust environment, that's not a big deal, but if you have other environments which have a more restricted permission set, then this might impact you there.

Hashish answered 1/4, 2009 at 17:20 Comment(0)
V
3

To add to Jared's reference to object pinning...

When using pointers to access memory directly in C#, you are vulnerable to the CLR moving an object around in memory at runtime. This means that your pointer may all of a sudden point at the wrong section of memory. The Fixed keyword will pin the object in memory so that this problem may be avoided.

Virility answered 1/4, 2009 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.