How do I dynamically load raw assemblies that contains unmanaged code?(bypassing 'Unverifiable code failed policy check' exception)

21

I'm going to give an example of using System.Data.SQLite.DLL which is a mixed assembly with unmanaged code: If I execute this :

  var assembly= Assembly.LoadFrom("System.Data.SQLite.DLL")

No exceptions are thrown, but if I do this :

  var rawAssembly = File.ReadAllBytes("System.Data.SQLite.DLL");
  var assembly = Assembly.Load(rawAssembly);

The CLR throws a FileLoadException with "Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)". Let's say I'm trying to load this assembly on a child AppDomain, how can I customize the AppDomain's security to allow me pass the policy check?

c#
.net
appdomain
late-binding
appdomainsetup
asked on Stack Overflow May 31, 2010 by Thiago de Arruda • edited May 31, 2010 by Hans Passant

2 Answers

20

We are the victim of a crummy exception message. Loading assemblies with Assembly.Load(byte[]) that contain unmanaged code is not supported. This is the subject of this feedback item.

UPDATE: the linked feedback item is gone, deleted as part of the cleanup at VS2012 release time. The only part of it could still recover is this fragment, copied from another web page:

“[…] we only allow ILOnly images to be loaded […] since anything else is not safe”--

UPDATE: link fixed with archive.org backup copy.

answered on Stack Overflow May 31, 2010 by Hans Passant • edited Mar 22, 2018 by CJBS
12

The problem is that the CLR does not perform the normal DLL loading steps - like mapping the dlls separate sections into different pages, adjusting fixups, etc. When an assembly is loaded from raw bytes, those raw bytes are mapped into memory as is, and only managed meta-data is read. No amount of evidence or security settings will change this behavior.

answered on Stack Overflow May 27, 2011 by Paul Alexander

User contributions licensed under CC BY-SA 3.0