使用不安全代码会产生什么影响

发布于 2024-07-16 00:25:24 字数 229 浏览 4 评论 0原文

除了代码本身可以直接访问内存这一事实之外。 使用“/unsafe”编译器标志和“fixed”关键字还有什么其他含义? 是否有与我的 .exe 的代码签名和部署相关的任何连锁效应(我的应用程序仅限桌面)?

(这不是关于我是否应该这样做,为什么在我的问题此处)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

背叛残局 2024-07-23 00:25:24

您可以将其影响分为两个部分。

首先是它如何影响您的应用程序环境。 使用不安全的代码要求您的程序集在完全信任的环境中运行。 无法在受限环境(例如某些 Click Once 安全设置)中运行。 原因是不安全代码阻止 CLR 确保类型安全。 尽管没有安全限制,但单击一次应该不会有问题。

第二个是它对你的编码方式意味着什么。 使用不安全代码通常涉及使用指针,特别是使用它们通过 PInvoke 执行高级编组。 不过,这些行为本质上并没有什么错误。 它只是需要比“安全”代码更多地了解 CLR 和编组。 对象固定是一个很好的例子,在开始使用这些功能之前,您需要牢牢掌握这些知识。

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.

风启觞 2024-07-23 00:25:24

不安全的代码是不可验证的,所以你必须意识到这一点。 在完全信任环境中,这不是什么大问题,但如果您有其他环境的权限集更受限制,那么这可能会影响您。

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.

我不在是我 2024-07-23 00:25:24

添加 Jared 对对象固定的引用...

在 C# 中使用指针直接访问内存时,您很容易受到 CLR 在运行时在内存中移动对象的影响。 这意味着您的指针可能会突然指向错误的内存部分。 Fixed 关键字将将对象固定在内存中,这样就可以避免这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文