防止在 AppDomain 中创建线程

发布于 2024-12-23 11:52:07 字数 1051 浏览 1 评论 0原文

我设置了一个小示例,其中我在没有任何权限的情况下将程序集加载到新的 AppDomain 中。这工作正常,程序集无法访问文件系统并且无法侦听套接字。

但我想阻止另一件事:线程创建。为什么?因为理论上这个程序集可以创建一个线程,从而创建更多线程并淹没我的内存。

我想到了(在我看来)最好的方法:限制 AppDomain 的内存。这可能吗?如果不是,我该怎么做才能避免创建线程?

使用此代码创建线程

Thread t = new Thread(this.DoWork);
t.Start();

和 AppDomain 的此代码

 PermissionSet set = new PermissionSet(PermissionState.None);
 set.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
 set.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read |
                                        FileIOPermissionAccess.PathDiscovery, 
                                        this.path));

 AppDomainSetup info = new AppDomainSetup { ApplicationBase = this.path };

 this.domain = AppDomain.CreateDomain("Sandbox", null, info, set, null);  

(好吧,我授予了对要加载程序集的文件夹中的文件系统的访问权限,这只是因为 StrongName fullTrustAssembly = typeof(SecureInstance).Assembly .Evidence.GetHostEvidence(); 对我也不起作用

希望 s/o 可以提供帮助。

I've set up a little example where I loaded an assembly into a new AppDomain without any Permission. This works fine, the assembly can't access the file system and can't listen to sockets.

But there is another thing i want to prevent: Thread creation. Why? Cause theoreticly this assembly can create a thread which creates even more threads and flood my memory.

I thought of the (in my opinion) best way: Limiting the memory of an AppDomain. Is this possible? And if not, what can i do to avoid thread creation?

Used this code to create the thread

Thread t = new Thread(this.DoWork);
t.Start();

And this code for the AppDomain

 PermissionSet set = new PermissionSet(PermissionState.None);
 set.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
 set.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read |
                                        FileIOPermissionAccess.PathDiscovery, 
                                        this.path));

 AppDomainSetup info = new AppDomainSetup { ApplicationBase = this.path };

 this.domain = AppDomain.CreateDomain("Sandbox", null, info, set, null);  

(Ok, i gave access to the file system in the folder where i want to load the assembly, this is just because StrongName fullTrustAssembly = typeof(SecureInstance).Assembly.Evidence.GetHostEvidence<StrongName>(); don't work for me either.

Hope s/o can help. (:

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

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

发布评论

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

评论(1

只想待在家 2024-12-30 11:52:07

似乎这个问题没有简单的答案。您可以做的是使用 .NET Profiling API 来尝试监视 AppDomain 中的内存使用情况。您可以在此处找到有关它的更多信息,但您需要进行一些挖掘: http://msdn.microsoft.com/en-us/library/bb384493.aspx

无论如何,在一个较低优先级的单独进程中运行您想要运行的任何内容不是更好吗?所有内存分配都疯狂,它不会影响您的进程,操作系统会杀死它并且它不会影响您的主进程?

Seems like there's no easy answer for this. What you can do is use the .NET Profiling API to try and monitor memory usage in your AppDomain. You can find out more about it here, but you'll need to do some digging: http://msdn.microsoft.com/en-us/library/bb384493.aspx

Anyway, isn't it better to run whatever you want to run in a separate process with a lower priority, so if it goes all wild with memory allocations, it doesn't affect your process, the OS kills it and it doesn't affect your main process?

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