RuntimeAssembly.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. //
  2. // System.Reflection/RuntimeAssembly.cs
  3. //
  4. // Author:
  5. // Rodrigo Kumpera ([email protected])
  6. //
  7. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Collections;
  31. using System.Globalization;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.CompilerServices;
  34. #if !FULL_AOT_RUNTIME
  35. using System.Reflection.Emit;
  36. #endif
  37. using System.Collections.Generic;
  38. using System.Runtime.Serialization;
  39. using System.Threading;
  40. using System.Diagnostics.Contracts;
  41. using System.Security;
  42. using System.Security.Policy;
  43. using System.Security.Permissions;
  44. using Mono;
  45. namespace System.Reflection {
  46. [ComVisible (true)]
  47. [ComDefaultInterfaceAttribute (typeof (_Assembly))]
  48. [Serializable]
  49. [ClassInterface(ClassInterfaceType.None)]
  50. [StructLayout (LayoutKind.Sequential)]
  51. class RuntimeAssembly : Assembly
  52. {
  53. #region Sync with AssemblyBuilder.cs and ReflectionAssembly in object-internals.h
  54. #pragma warning disable 649
  55. internal IntPtr _mono_assembly;
  56. #pragma warning restore 649
  57. #if !MOBILE
  58. internal Evidence _evidence;
  59. #else
  60. object _evidence;
  61. #endif
  62. #endregion
  63. internal ResolveEventHolder resolve_event_holder;
  64. #if !MOBILE
  65. internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
  66. internal PermissionSet _optional; // for SecurityAction.RequestOptional
  67. internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
  68. internal PermissionSet _granted; // for the resolved assembly granted permissions
  69. internal PermissionSet _denied; // for the resolved assembly denied permissions
  70. #else
  71. object _minimum, _optional, _refuse, _granted, _denied;
  72. #endif
  73. internal bool fromByteArray;
  74. internal string assemblyName;
  75. internal class UnmanagedMemoryStreamForModule : UnmanagedMemoryStream
  76. {
  77. #pragma warning disable 414
  78. Module module;
  79. #pragma warning restore
  80. public unsafe UnmanagedMemoryStreamForModule (byte* pointer, long length, Module module)
  81. : base (pointer, length)
  82. {
  83. this.module = module;
  84. }
  85. protected override void Dispose (bool disposing)
  86. {
  87. if (_isOpen) {
  88. /*
  89. * The returned pointer points inside metadata, so
  90. * we have to increase the refcount of the module, and decrease
  91. * it when the stream is finalized.
  92. */
  93. module = null;
  94. }
  95. base.Dispose (disposing);
  96. }
  97. }
  98. protected RuntimeAssembly ()
  99. {
  100. resolve_event_holder = new ResolveEventHolder ();
  101. }
  102. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  103. {
  104. if (info == null)
  105. throw new ArgumentNullException ("info");
  106. UnitySerializationHolder.GetUnitySerializationInfo (info,
  107. UnitySerializationHolder.AssemblyUnity,
  108. this.FullName,
  109. this);
  110. }
  111. internal static RuntimeAssembly GetExecutingAssembly (ref StackCrawlMark stackMark)
  112. {
  113. // Mono runtime does not support StackCrawlMark, The easiest workaround is to replace use
  114. // of StackCrawlMark.LookForMyCaller with GetCallingAssembly
  115. throw new NotSupportedException ();
  116. }
  117. // Creates AssemblyName. Fills assembly if AssemblyResolve event has been raised.
  118. [System.Security.SecurityCritical] // auto-generated
  119. internal static AssemblyName CreateAssemblyName(
  120. String assemblyString,
  121. bool forIntrospection,
  122. out RuntimeAssembly assemblyFromResolveEvent)
  123. {
  124. if (assemblyString == null)
  125. throw new ArgumentNullException("assemblyString");
  126. Contract.EndContractBlock();
  127. if ((assemblyString.Length == 0) ||
  128. (assemblyString[0] == '\0'))
  129. throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
  130. if (forIntrospection)
  131. AppDomain.CheckReflectionOnlyLoadSupported();
  132. AssemblyName an = new AssemblyName();
  133. an.Name = assemblyString;
  134. assemblyFromResolveEvent = null; // instead of an.nInit(out assemblyFromResolveEvent, forIntrospection, true);
  135. return an;
  136. }
  137. internal static RuntimeAssembly InternalLoadAssemblyName(
  138. AssemblyName assemblyRef,
  139. Evidence assemblySecurity,
  140. RuntimeAssembly reqAssembly,
  141. ref StackCrawlMark stackMark,
  142. #if FEATURE_HOSTED_BINDER
  143. IntPtr pPrivHostBinder,
  144. #endif
  145. bool throwOnFileNotFound,
  146. bool forIntrospection,
  147. bool suppressSecurityChecks)
  148. {
  149. if (assemblyRef == null)
  150. throw new ArgumentNullException("assemblyRef");
  151. Contract.EndContractBlock();
  152. if (assemblyRef.CodeBase != null)
  153. {
  154. AppDomain.CheckLoadFromSupported();
  155. }
  156. assemblyRef = (AssemblyName)assemblyRef.Clone();
  157. #if FEATURE_VERSIONING
  158. if (!forIntrospection &&
  159. (assemblyRef.ProcessorArchitecture != ProcessorArchitecture.None)) {
  160. // PA does not have a semantics for by-name binds for execution
  161. assemblyRef.ProcessorArchitecture = ProcessorArchitecture.None;
  162. }
  163. #endif
  164. if (assemblySecurity != null)
  165. {
  166. #if FEATURE_CAS_POLICY
  167. if (!AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
  168. {
  169. throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
  170. }
  171. #endif // FEATURE_CAS_POLICY
  172. if (!suppressSecurityChecks)
  173. {
  174. #if MONO_FEATURE_CAS
  175. #pragma warning disable 618
  176. new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
  177. #pragma warning restore 618
  178. #endif
  179. }
  180. }
  181. return (RuntimeAssembly) Assembly.Load (assemblyRef);
  182. }
  183. internal static RuntimeAssembly LoadWithPartialNameInternal (String partialName, Evidence securityEvidence, ref StackCrawlMark stackMark)
  184. {
  185. // Mono runtime does not support StackCrawlMark
  186. //FIXME stackMark should probably change method behavior in some cases.
  187. return (RuntimeAssembly) Assembly.LoadWithPartialName (partialName, securityEvidence);
  188. }
  189. internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Evidence securityEvidence, ref StackCrawlMark stackMark)
  190. {
  191. return LoadWithPartialNameInternal (an.ToString (), securityEvidence, ref stackMark);
  192. }
  193. // the security runtime requires access to the assemblyname (e.g. to get the strongname)
  194. public override AssemblyName GetName (bool copiedName)
  195. {
  196. #if !MOBILE
  197. // CodeBase, which is restricted, will be copied into the AssemblyName object so...
  198. if (SecurityManager.SecurityEnabled) {
  199. var _ = CodeBase; // this will ensure the Demand is made
  200. }
  201. #endif
  202. return AssemblyName.Create (this, true);
  203. }
  204. public
  205. override
  206. Type GetType (string name, bool throwOnError, bool ignoreCase)
  207. {
  208. Type res;
  209. if (name == null)
  210. throw new ArgumentNullException (name);
  211. if (name.Length == 0)
  212. throw new ArgumentException ("name", "Name cannot be empty");
  213. res = InternalGetType (null, name, throwOnError, ignoreCase);
  214. return res;
  215. }
  216. public
  217. override
  218. Module GetModule (String name)
  219. {
  220. if (name == null)
  221. throw new ArgumentNullException ("name");
  222. if (name.Length == 0)
  223. throw new ArgumentException ("Name can't be empty");
  224. Module[] modules = GetModules (true);
  225. foreach (Module module in modules) {
  226. if (module.ScopeName == name)
  227. return module;
  228. }
  229. return null;
  230. }
  231. public
  232. override
  233. AssemblyName[] GetReferencedAssemblies () {
  234. return GetReferencedAssemblies (this);
  235. }
  236. public
  237. override
  238. Module[] GetModules (bool getResourceModules) {
  239. Module[] modules = GetModulesInternal ();
  240. if (!getResourceModules) {
  241. var result = new List<Module> (modules.Length);
  242. foreach (Module m in modules)
  243. if (!m.IsResource ())
  244. result.Add (m);
  245. return result.ToArray ();
  246. }
  247. else
  248. return modules;
  249. }
  250. [MonoTODO ("Always returns the same as GetModules")]
  251. public
  252. override
  253. Module[] GetLoadedModules (bool getResourceModules)
  254. {
  255. return GetModules (getResourceModules);
  256. }
  257. [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
  258. public
  259. override
  260. Assembly GetSatelliteAssembly (CultureInfo culture)
  261. {
  262. StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  263. return GetSatelliteAssembly (culture, null, true, ref stackMark);
  264. }
  265. [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
  266. public
  267. override
  268. Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
  269. {
  270. StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  271. return GetSatelliteAssembly (culture, version, true, ref stackMark);
  272. }
  273. //FIXME remove GetManifestModule under v4, it's a v2 artifact
  274. [ComVisible (false)]
  275. public
  276. override
  277. Module ManifestModule {
  278. get {
  279. return GetManifestModule ();
  280. }
  281. }
  282. public
  283. override
  284. bool GlobalAssemblyCache {
  285. get {
  286. return get_global_assembly_cache ();
  287. }
  288. }
  289. public override Type[] GetExportedTypes ()
  290. {
  291. return GetTypes (true);
  292. }
  293. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  294. extern static string get_code_base (Assembly a, bool escaped);
  295. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  296. private extern string get_location ();
  297. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  298. internal extern static string get_fullname (Assembly a);
  299. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  300. internal extern static string GetAotId ();
  301. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  302. internal extern static string InternalImageRuntimeVersion (Assembly a);
  303. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  304. internal extern bool get_global_assembly_cache ();
  305. public override extern MethodInfo EntryPoint {
  306. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  307. get;
  308. }
  309. [ComVisible (false)]
  310. public override extern bool ReflectionOnly {
  311. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  312. get;
  313. }
  314. // SECURITY: this should be the only caller to icall get_code_base
  315. internal static string GetCodeBase (Assembly a, bool escaped)
  316. {
  317. string cb = get_code_base (a, escaped);
  318. #if !MOBILE
  319. if (SecurityManager.SecurityEnabled) {
  320. // we cannot divulge local file informations
  321. if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
  322. string file = cb.Substring (7);
  323. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
  324. }
  325. }
  326. #endif
  327. return cb;
  328. }
  329. public override string CodeBase {
  330. get { return GetCodeBase (this, false); }
  331. }
  332. public override string EscapedCodeBase {
  333. get { return GetCodeBase (this, true); }
  334. }
  335. public override string FullName {
  336. get {
  337. return get_fullname (this);
  338. }
  339. }
  340. [ComVisible (false)]
  341. public override string ImageRuntimeVersion {
  342. get {
  343. return InternalImageRuntimeVersion (this);
  344. }
  345. }
  346. internal override IntPtr MonoAssembly {
  347. get {
  348. return _mono_assembly;
  349. }
  350. }
  351. internal override bool FromByteArray {
  352. set { fromByteArray = value; }
  353. }
  354. public override String Location {
  355. get {
  356. if (fromByteArray)
  357. return String.Empty;
  358. string loc = get_location ();
  359. #if !MOBILE
  360. if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
  361. // we cannot divulge local file informations
  362. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
  363. }
  364. #endif
  365. return loc;
  366. }
  367. }
  368. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  369. private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
  370. public override ManifestResourceInfo GetManifestResourceInfo (String resourceName)
  371. {
  372. if (resourceName == null)
  373. throw new ArgumentNullException ("resourceName");
  374. if (resourceName.Length == 0)
  375. throw new ArgumentException ("String cannot have zero length.");
  376. ManifestResourceInfo result = new ManifestResourceInfo (null, null, 0);
  377. bool found = GetManifestResourceInfoInternal (resourceName, result);
  378. if (found)
  379. return result;
  380. else
  381. return null;
  382. }
  383. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  384. public override extern String[] GetManifestResourceNames ();
  385. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  386. internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
  387. public override Stream GetManifestResourceStream (String name)
  388. {
  389. if (name == null)
  390. throw new ArgumentNullException ("name");
  391. if (name.Length == 0)
  392. throw new ArgumentException ("String cannot have zero length.",
  393. "name");
  394. ManifestResourceInfo info = GetManifestResourceInfo (name);
  395. if (info == null) {
  396. Assembly a = AppDomain.CurrentDomain.DoResourceResolve (name, this);
  397. if (a != null && a != this)
  398. return a.GetManifestResourceStream (name);
  399. else
  400. return null;
  401. }
  402. if (info.ReferencedAssembly != null)
  403. return info.ReferencedAssembly.GetManifestResourceStream (name);
  404. if ((info.FileName != null) && (info.ResourceLocation == 0)) {
  405. if (fromByteArray)
  406. throw new FileNotFoundException (info.FileName);
  407. string location = Path.GetDirectoryName (Location);
  408. string filename = Path.Combine (location, info.FileName);
  409. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  410. }
  411. int size;
  412. Module module;
  413. IntPtr data = GetManifestResourceInternal (name, out size, out module);
  414. if (data == (IntPtr) 0)
  415. return null;
  416. else {
  417. UnmanagedMemoryStream stream;
  418. unsafe {
  419. stream = new UnmanagedMemoryStreamForModule ((byte*) data, size, module);
  420. }
  421. return stream;
  422. }
  423. }
  424. [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
  425. public override Stream GetManifestResourceStream (Type type, String name)
  426. {
  427. StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  428. return GetManifestResourceStream(type, name, false, ref stackMark);
  429. }
  430. public override bool IsDefined (Type attributeType, bool inherit)
  431. {
  432. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  433. }
  434. public override object [] GetCustomAttributes (bool inherit)
  435. {
  436. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  437. }
  438. public override object [] GetCustomAttributes (Type attributeType, bool inherit)
  439. {
  440. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  441. }
  442. public override IList<CustomAttributeData> GetCustomAttributesData ()
  443. {
  444. return CustomAttributeData.GetCustomAttributes (this);
  445. }
  446. //
  447. // We can't store the event directly in this class, since the
  448. // compiler would silently insert the fields before _mono_assembly
  449. //
  450. public override event ModuleResolveEventHandler ModuleResolve {
  451. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  452. add {
  453. resolve_event_holder.ModuleResolve += value;
  454. }
  455. [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
  456. remove {
  457. resolve_event_holder.ModuleResolve -= value;
  458. }
  459. }
  460. internal override Module GetManifestModule () {
  461. return GetManifestModuleInternal ();
  462. }
  463. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  464. internal extern Module GetManifestModuleInternal ();
  465. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  466. internal override extern Module[] GetModulesInternal ();
  467. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  468. private extern object GetFilesInternal (String name, bool getResourceModules);
  469. public override FileStream [] GetFiles (bool getResourceModules)
  470. {
  471. string[] names = (string[]) GetFilesInternal (null, getResourceModules);
  472. if (names == null)
  473. return EmptyArray<FileStream>.Value;
  474. string location = Location;
  475. FileStream[] res;
  476. if (location != String.Empty) {
  477. res = new FileStream [names.Length + 1];
  478. res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
  479. for (int i = 0; i < names.Length; ++i)
  480. res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  481. } else {
  482. res = new FileStream [names.Length];
  483. for (int i = 0; i < names.Length; ++i)
  484. res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
  485. }
  486. return res;
  487. }
  488. public override FileStream GetFile (String name)
  489. {
  490. if (name == null)
  491. throw new ArgumentNullException (null, "Name cannot be null.");
  492. if (name.Length == 0)
  493. throw new ArgumentException ("Empty name is not valid");
  494. string filename = (string)GetFilesInternal (name, true);
  495. if (filename != null)
  496. return new FileStream (filename, FileMode.Open, FileAccess.Read);
  497. else
  498. return null;
  499. }
  500. public override int GetHashCode ()
  501. {
  502. return base.GetHashCode ();
  503. }
  504. public override bool Equals (object o)
  505. {
  506. if (((object) this) == o)
  507. return true;
  508. if (o == null)
  509. return false;
  510. if (!(o is RuntimeAssembly))
  511. return false;
  512. var other = (RuntimeAssembly) o;
  513. return other._mono_assembly == _mono_assembly;
  514. }
  515. public override string ToString ()
  516. {
  517. // note: ToString work without requiring CodeBase (so no checks are needed)
  518. if (assemblyName != null)
  519. return assemblyName;
  520. assemblyName = FullName;
  521. return assemblyName;
  522. }
  523. public override Evidence Evidence {
  524. [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
  525. get { return UnprotectedGetEvidence (); }
  526. }
  527. // note: the security runtime requires evidences but may be unable to do so...
  528. internal override Evidence UnprotectedGetEvidence ()
  529. {
  530. #if MOBILE
  531. return null;
  532. #else
  533. // if the host (runtime) hasn't provided it's own evidence...
  534. if (_evidence == null) {
  535. // ... we will provide our own
  536. lock (this) {
  537. _evidence = Evidence.GetDefaultHostEvidence (this);
  538. }
  539. }
  540. return _evidence;
  541. #endif
  542. }
  543. #if !MOBILE
  544. // Code Access Security
  545. internal void Resolve ()
  546. {
  547. lock (this) {
  548. // FIXME: As we (currently) delay the resolution until the first CAS
  549. // Demand it's too late to evaluate the Minimum permission set as a
  550. // condition to load the assembly into the AppDomain
  551. LoadAssemblyPermissions ();
  552. Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
  553. e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
  554. _granted = SecurityManager.ResolvePolicy (e,
  555. _minimum, _optional, _refuse, out _denied);
  556. }
  557. }
  558. internal override PermissionSet GrantedPermissionSet {
  559. get {
  560. if (_granted == null) {
  561. if (SecurityManager.ResolvingPolicyLevel != null) {
  562. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  563. return DefaultPolicies.FullTrust;
  564. else
  565. return null; // we can't resolve during resolution
  566. }
  567. Resolve ();
  568. }
  569. return _granted;
  570. }
  571. }
  572. internal override PermissionSet DeniedPermissionSet {
  573. get {
  574. // yes we look for granted, as denied may be null
  575. if (_granted == null) {
  576. if (SecurityManager.ResolvingPolicyLevel != null) {
  577. if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
  578. return null;
  579. else
  580. return DefaultPolicies.FullTrust; // deny unrestricted
  581. }
  582. Resolve ();
  583. }
  584. return _denied;
  585. }
  586. }
  587. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  588. extern internal static bool LoadPermissions (Assembly a,
  589. ref IntPtr minimum, ref int minLength,
  590. ref IntPtr optional, ref int optLength,
  591. ref IntPtr refused, ref int refLength);
  592. // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
  593. private void LoadAssemblyPermissions ()
  594. {
  595. IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
  596. int minLength = 0, optLength = 0, refLength = 0;
  597. if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
  598. ref optLength, ref refused, ref refLength)) {
  599. // Note: no need to cache these permission sets as they will only be created once
  600. // at assembly resolution time.
  601. if (minLength > 0) {
  602. byte[] data = new byte [minLength];
  603. Marshal.Copy (minimum, data, 0, minLength);
  604. _minimum = SecurityManager.Decode (data);
  605. }
  606. if (optLength > 0) {
  607. byte[] data = new byte [optLength];
  608. Marshal.Copy (optional, data, 0, optLength);
  609. _optional = SecurityManager.Decode (data);
  610. }
  611. if (refLength > 0) {
  612. byte[] data = new byte [refLength];
  613. Marshal.Copy (refused, data, 0, refLength);
  614. _refuse = SecurityManager.Decode (data);
  615. }
  616. }
  617. }
  618. public override PermissionSet PermissionSet {
  619. get { return this.GrantedPermissionSet; }
  620. }
  621. #endif
  622. }
  623. }