Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent thread creation in AppDomain

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. (:

like image 526
Philipp Spiess Avatar asked Dec 27 '11 11:12

Philipp Spiess


People also ask

What is the difference between AppDomain assembly process and a thread?

A process is an executing application (waaaay oversimplified). A thread is an execution context. The operating system executes code within a thread. The operating system switches between threads, allowing each to execute in turn, thus giving the impression that multiple applications are running at the same time.

What is AppDomain CurrentDomain?

The CurrentDomain property is used to obtain an AppDomain object that represents the current application domain. The FriendlyName property provides the name of the current application domain, which is then displayed at the command line.

What is thread safety in C#?

A piece of code or data structure is thread safe, when the outcome of the code and underlying resources do not create undesirable results (inconsistent data, exception etc.), because of multiple threads interacting with the code concurrently. That simply means: All threads behave properly.


1 Answers

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?

like image 149
zmbq Avatar answered Oct 24 '22 12:10

zmbq