diff options
-rw-r--r-- | mcs/class/PEAPI/ChangeLog | 15 | ||||
-rw-r--r-- | mcs/class/PEAPI/Metadata.cs | 164 | ||||
-rw-r--r-- | mcs/class/PEAPI/PEAPI.cs | 5 | ||||
-rw-r--r-- | mcs/ilasm/codegen/ChangeLog | 13 | ||||
-rw-r--r-- | mcs/ilasm/codegen/CodeGen.cs | 35 | ||||
-rw-r--r-- | mcs/ilasm/codegen/DeclSecurity.cs | 56 | ||||
-rw-r--r-- | mcs/ilasm/codegen/ExternTable.cs | 12 | ||||
-rw-r--r-- | mcs/ilasm/codegen/Permission.cs | 81 | ||||
-rw-r--r-- | mcs/ilasm/codegen/PermissionMember.cs | 60 | ||||
-rw-r--r-- | mcs/ilasm/codegen/PermissionSet.cs | 67 | ||||
-rw-r--r-- | mcs/ilasm/ilasm.exe.sources | 3 | ||||
-rw-r--r-- | mcs/ilasm/parser/ChangeLog | 17 | ||||
-rw-r--r-- | mcs/ilasm/parser/ILParser.jay | 181 | ||||
-rw-r--r-- | mcs/ilasm/scanner/ChangeLog | 4 | ||||
-rw-r--r-- | mcs/ilasm/scanner/ILTables.cs | 2 | ||||
-rw-r--r-- | mcs/ilasm/tests/ChangeLog | 5 | ||||
-rw-r--r-- | mcs/ilasm/tests/test-perm_pass-3.il | 60 |
17 files changed, 714 insertions, 66 deletions
diff --git a/mcs/class/PEAPI/ChangeLog b/mcs/class/PEAPI/ChangeLog index 0c3b2772481..012d7b73b24 100644 --- a/mcs/class/PEAPI/ChangeLog +++ b/mcs/class/PEAPI/ChangeLog @@ -1,3 +1,18 @@ +2006-05-26 Ankit Jain <jankit@novell.com> + + * Metadata.cs (DeclSecurity): Rename to .. + (BaseDeclSecurity): .. this. + (DeclSecurity): New. Derive from BaseDeclSecurity. + (DeclSecurity_20): Likewise. For 2.0 style declaritive security. + (PermissionSet): New. + (Permission): New. + (PermissionMember): New. + (MetaData.AddDeclSecurity): Update to use BaseDeclSecurity. + (MetaData.BuildMetaData): Likewise. + + * PEAPI.cs (PEFile.AddDeclSecurity): New overload for new PermissionSet + class. + 2006-05-10 Ankit Jain <jankit@novell.com> * Metadata.cs (NativeType): Add LPStruct and Error. diff --git a/mcs/class/PEAPI/Metadata.cs b/mcs/class/PEAPI/Metadata.cs index d7fb6d21df1..cd580be3d59 100644 --- a/mcs/class/PEAPI/Metadata.cs +++ b/mcs/class/PEAPI/Metadata.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Collections; using System.Text; +using System.Reflection; namespace PEAPI { @@ -489,19 +490,17 @@ namespace PEAPI { /// Descriptor for security permissions for a class or a method /// </summary> - public class DeclSecurity : MetaDataElement { + public abstract class BaseDeclSecurity : MetaDataElement { ushort action; MetaDataElement parent; uint permissionIx; - byte [] byteVal; - internal DeclSecurity(MetaDataElement paren, ushort act, byte [] val) + internal BaseDeclSecurity(MetaDataElement paren, ushort act) { parent = paren; action = act; tabIx = MDTable.DeclSecurity; - byteVal = val; } internal override uint SortKey() @@ -518,15 +517,18 @@ namespace PEAPI { internal sealed override void BuildTables(MetaData md) { if (done) return; + BinaryWriter bw = new BinaryWriter (new MemoryStream ()); - bw.Write (byteVal); md.AddToTable (MDTable.DeclSecurity, this); MemoryStream str = (MemoryStream)bw.BaseStream; + WriteSig (bw); permissionIx = md.AddToBlobHeap(str.ToArray()); done = true; } + internal abstract void WriteSig (BinaryWriter bw); + internal sealed override void Write(FileImage output) { output.Write(action); @@ -536,6 +538,154 @@ namespace PEAPI { } + public class DeclSecurity : BaseDeclSecurity { + + byte [] byteVal; + + internal DeclSecurity(MetaDataElement paren, ushort act, byte [] val) + : base (paren, act) + { + byteVal = val; + } + + internal override void WriteSig (BinaryWriter bw) + { + bw.Write (byteVal); + } + + } + + public class DeclSecurity_20 : BaseDeclSecurity { + + PermissionSet ps; + + internal DeclSecurity_20 (MetaDataElement paren, ushort act, PermissionSet ps) + : base (paren, act) + { + this.ps = ps; + } + + internal override void WriteSig (BinaryWriter bw) + { + ps.Write (bw); + } + } + + public class PermissionMember { + + MemberTypes member_type; + PEAPI.Type type; + string name; + object value; + + public PermissionMember (MemberTypes member_type, PEAPI.Type type, string name, object value) + { + this.member_type = member_type; + this.type = type; + this.name = name; + this.value = value; + } + + public void Write (BinaryWriter bw) + { + byte [] b; + + if (member_type == MemberTypes.Field) + bw.Write ((byte) 0x53); + else + //Property + bw.Write ((byte) 0x54); + + if (type is PrimitiveType) { + bw.Write (type.GetTypeIndex ()); + } else { + //must be enum + bw.Write ((byte) 0x55); //ENUM + + b = Encoding.UTF8.GetBytes (((ClassRef) type).TypeName ()); + MetaData.CompressNum ((uint) b.Length, (MemoryStream) bw.BaseStream); + bw.Write (b); + } + + b = Encoding.UTF8.GetBytes (name); + MetaData.CompressNum ((uint) b.Length, (MemoryStream) bw.BaseStream); + bw.Write (b); + + ((Constant) value).Write (bw); + } + + } + + public class Permission + { + PEAPI.Type type; + + //PermissionMembers + ArrayList members; + string name; + + public Permission (PEAPI.Type type, string name) + { + this.type = type; + this.name = name; + } + + public void AddMember (PEAPI.PermissionMember member) + { + if (members == null) + members = new ArrayList (); + + members.Add (member); + } + + public void Write (BinaryWriter bw) + { + byte [] b = Encoding.UTF8.GetBytes (name); + MetaData.CompressNum ((uint) b.Length, (MemoryStream) bw.BaseStream); + bw.Write (b); + + BinaryWriter perm_writer = new BinaryWriter (new MemoryStream (), Encoding.Unicode); + MemoryStream str = (MemoryStream) perm_writer.BaseStream; + + MetaData.CompressNum ((uint) members.Count, str);//number of params + foreach (PermissionMember member in members) + member.Write (perm_writer); + + bw.Write ((byte) str.Length); //(optional) parameters length + bw.Write (str.ToArray ()); + } + } + + public class PermissionSet + { + PEAPI.SecurityAction sec_action; + ArrayList permissions; + PEAPI.PermissionSet ps; + + public PermissionSet (PEAPI.SecurityAction sec_action) + { + this.sec_action = sec_action; + } + + public void AddPermission (PEAPI.Permission perm) + { + if (permissions == null) + permissions = new ArrayList (); + + permissions.Add (perm); + } + + public void Write (BinaryWriter bw) + { + bw.Write ((byte) 0x2e); + MetaData.CompressNum ((uint) permissions.Count, (MemoryStream) bw.BaseStream); + + foreach (Permission perm in permissions) + perm.Write (bw); + } + + } + /**************************************************************************/ /// <summary> /// Descriptor for layout information for a field @@ -4948,7 +5098,7 @@ namespace PEAPI { cattr_list.Add (cattr); } - internal void AddDeclSecurity (DeclSecurity decl_sec) + internal void AddDeclSecurity (BaseDeclSecurity decl_sec) { if (declsec_list == null) declsec_list = new ArrayList (); @@ -5235,7 +5385,7 @@ namespace PEAPI { } if (declsec_list != null) { - foreach (DeclSecurity decl_sec in declsec_list) + foreach (BaseDeclSecurity decl_sec in declsec_list) decl_sec.BuildTables (this); } diff --git a/mcs/class/PEAPI/PEAPI.cs b/mcs/class/PEAPI/PEAPI.cs index 21f26f32f14..23b7d352132 100644 --- a/mcs/class/PEAPI/PEAPI.cs +++ b/mcs/class/PEAPI/PEAPI.cs @@ -929,6 +929,11 @@ namespace PEAPI { metaData.AddDeclSecurity (new DeclSecurity (element, (ushort) sec_action, data)); } + public void AddDeclSecurity (SecurityAction sec_action, PEAPI.PermissionSet ps, MetaDataElement element) + { + metaData.AddDeclSecurity (new DeclSecurity_20 (element, (ushort) sec_action, ps)); + } + /// <summary> /// Add a managed resource from another assembly. /// </summary> diff --git a/mcs/ilasm/codegen/ChangeLog b/mcs/ilasm/codegen/ChangeLog index e47bf30af71..ff553c38b79 100644 --- a/mcs/ilasm/codegen/ChangeLog +++ b/mcs/ilasm/codegen/ChangeLog @@ -1,3 +1,16 @@ +2006-05-26 Ankit Jain <jankit@novell.com> + + * PermissionSet.cs: New. + * Permission.cs: New. + * PermissionMember.cs: New. Classes for 2.0 metadata format of + declarative security. + + * DeclSecurity.cs (DeclSecurity.AddPermissionSet): New. Overload for new + PermissionSet class. + (DeclSecurity.AddTo): Add new style PermissionSets also. + * CodeGen.cs (CodeGen.AddPermisson): Handle new PermissionSets also. + * ExternTable.cs (ExternAssembly.AssemblyName): New. + 2006-05-23 Ankit Jain <jankit@novell.com> * DeclSecurity.cs (IDeclSecurityTarget): Remove AddPermission & diff --git a/mcs/ilasm/codegen/CodeGen.cs b/mcs/ilasm/codegen/CodeGen.cs index eab7668acac..5971e442139 100644 --- a/mcs/ilasm/codegen/CodeGen.cs +++ b/mcs/ilasm/codegen/CodeGen.cs @@ -18,6 +18,8 @@ using System.Reflection.Emit; using System.Text;
using System.Security;
+using SSPermissionSet = System.Security.PermissionSet;
+using MIPermissionSet = Mono.ILASM.PermissionSet;
namespace Mono.ILASM {
@@ -462,11 +464,7 @@ namespace Mono.ILASM { if (assembly_declsec == null)
assembly_declsec = new DeclSecurity ();
- PermissionSet ps = perm as PermissionSet;
- if (ps == null)
- assembly_declsec.AddPermission (sec_action, (IPermission) perm);
- else
- assembly_declsec.AddPermissionSet (sec_action, ps);
+ AddPermission (sec_action, perm, assembly_declsec);
}
public void AddPermission (PEAPI.SecurityAction sec_action, object perm)
@@ -474,11 +472,28 @@ namespace Mono.ILASM { if (CurrentDeclSecurityTarget == null)
return;
- PermissionSet ps = perm as PermissionSet;
- if (ps == null)
- CurrentDeclSecurityTarget.DeclSecurity.AddPermission (sec_action, (IPermission) perm);
- else
- CurrentDeclSecurityTarget.DeclSecurity.AddPermissionSet (sec_action, ps);
+ AddPermission (sec_action, perm, CurrentDeclSecurityTarget.DeclSecurity);
+ }
+
+ private void AddPermission (PEAPI.SecurityAction sec_action, object perm, DeclSecurity decl_sec)
+ {
+ SSPermissionSet ps = perm as SSPermissionSet;
+ if (ps != null) {
+ decl_sec.AddPermissionSet (sec_action, ps);
+ return;
+ }
+
+ IPermission iper = perm as IPermission;
+ if (iper != null) {
+ decl_sec.AddPermission (sec_action, iper);
+ return;
+ }
+
+ MIPermissionSet ps20 = perm as MIPermissionSet;
+ if (ps20 != null) {
+ decl_sec.AddPermissionSet (sec_action, ps20);
+ return;
+ }
}
public void Write ()
diff --git a/mcs/ilasm/codegen/DeclSecurity.cs b/mcs/ilasm/codegen/DeclSecurity.cs index 66e5e9338f6..c318a0f7180 100644 --- a/mcs/ilasm/codegen/DeclSecurity.cs +++ b/mcs/ilasm/codegen/DeclSecurity.cs @@ -13,6 +13,9 @@ using System.Collections; using System.Security; using System.Security.Permissions; +using SSPermissionSet = System.Security.PermissionSet; +using MIPermissionSet = Mono.ILASM.PermissionSet; + namespace Mono.ILASM { public interface IDeclSecurityTarget { @@ -22,6 +25,7 @@ namespace Mono.ILASM { public class DeclSecurity { private Hashtable permissionset_table; + private Hashtable permissionset20_table; public DeclSecurity () { @@ -30,18 +34,18 @@ namespace Mono.ILASM { public void AddPermission (PEAPI.SecurityAction sec_action, IPermission perm) { - PermissionSet ps = (PermissionSet) permissionset_table [sec_action]; + SSPermissionSet ps = (SSPermissionSet) permissionset_table [sec_action]; if (ps == null) { - ps = new PermissionSet (PermissionState.None); + ps = new SSPermissionSet (PermissionState.None); permissionset_table [sec_action] = ps; } ps.AddPermission (perm); } - public void AddPermissionSet (PEAPI.SecurityAction sec_action, PermissionSet perm_set) + public void AddPermissionSet (PEAPI.SecurityAction sec_action, SSPermissionSet perm_set) { - PermissionSet ps = (PermissionSet) permissionset_table [sec_action]; + SSPermissionSet ps = (SSPermissionSet) permissionset_table [sec_action]; if (ps == null) { permissionset_table [sec_action] = perm_set; return; @@ -50,14 +54,50 @@ namespace Mono.ILASM { foreach (IPermission iper in perm_set) ps.AddPermission (iper); } - + + //Not called by parser for profile != NET_2_0 + public void AddPermissionSet (PEAPI.SecurityAction sec_action, MIPermissionSet perm_set) + { + PermissionSet ps = null; + + if (permissionset20_table == null) + permissionset20_table = new Hashtable (); + else + ps = (MIPermissionSet) permissionset20_table [sec_action]; + + if (ps == null) { + permissionset20_table [sec_action] = perm_set; + return; + } + + foreach (Permission perm in perm_set.Permissions) + ps.AddPermission (perm); + } + public void AddTo (CodeGen code_gen, PEAPI.MetaDataElement elem) { System.Text.UnicodeEncoding ue = new System.Text.UnicodeEncoding (); - foreach (PEAPI.SecurityAction sec_action in permissionset_table.Keys) - code_gen.PEFile.AddDeclSecurity (sec_action, - ue.GetBytes (((PermissionSet) permissionset_table [sec_action]).ToXml ().ToString ()), + foreach (DictionaryEntry entry in permissionset_table) { + PEAPI.SecurityAction sec_action = (PEAPI.SecurityAction) entry.Key; + SSPermissionSet ps = (SSPermissionSet) entry.Value; + + code_gen.PEFile.AddDeclSecurity (sec_action, + ue.GetBytes (ps.ToXml ().ToString ()), + elem); + } + + if (permissionset20_table == null) + return; + + foreach (DictionaryEntry entry in permissionset20_table) { + PEAPI.SecurityAction sec_action = (PEAPI.SecurityAction) entry.Key; + MIPermissionSet ps = (MIPermissionSet) entry.Value; + + code_gen.PEFile.AddDeclSecurity (sec_action, + ps.Resolve (code_gen), elem); + } + } } diff --git a/mcs/ilasm/codegen/ExternTable.cs b/mcs/ilasm/codegen/ExternTable.cs index 7d4dd7da229..69cb3c0eaa9 100644 --- a/mcs/ilasm/codegen/ExternTable.cs +++ b/mcs/ilasm/codegen/ExternTable.cs @@ -11,6 +11,7 @@ using System; using System.Collections; using System.Reflection; using System.Security; +using System.Globalization; namespace Mono.ILASM { @@ -144,10 +145,12 @@ namespace Mono.ILASM { private string locale; private byte [] hash; private DeclSecurity decl_sec; + private AssemblyName asmb_name; public ExternAssembly (string name, AssemblyName asmb_name) : base (name) { this.name = name; + this.asmb_name = asmb_name; major = minor = build = revision = -1; } @@ -159,6 +162,10 @@ namespace Mono.ILASM { } } + public AssemblyName AssemblyName { + get { return asmb_name; } + } + public DeclSecurity DeclSecurity { get { if (decl_sec == null) @@ -207,21 +214,26 @@ namespace Mono.ILASM { this.minor = minor; this.build = build; this.revision = revision; + asmb_name.Version = new Version (major, minor, build, revision); } public void SetPublicKey (byte [] public_key) { this.public_key = public_key; + asmb_name.SetPublicKey (public_key); } public void SetPublicKeyToken (byte [] public_key_token) { this.public_key_token = public_key_token; + asmb_name.SetPublicKey (public_key); } public void SetLocale (string locale) { this.locale = locale; + //FIXME: is this correct? + asmb_name.CultureInfo = new CultureInfo (locale); } public void SetHash (byte [] hash) diff --git a/mcs/ilasm/codegen/Permission.cs b/mcs/ilasm/codegen/Permission.cs new file mode 100644 index 00000000000..4338646d792 --- /dev/null +++ b/mcs/ilasm/codegen/Permission.cs @@ -0,0 +1,81 @@ +// +// Mono.ILASM.Permission +// +// Author(s): +// Ankit Jain <JAnkit@novell.com> +// +// Copyright (C) 2006 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Collections; + +namespace Mono.ILASM { + + public class Permission + { + BaseTypeRef type_ref; + + //PermissionMembers + ArrayList members; + PEAPI.Permission perm; + + public Permission (BaseTypeRef type_ref, ArrayList members) + { + this.type_ref = type_ref; + this.members = members; + } + + public PEAPI.Permission Resolve (CodeGen code_gen) + { + string fname; + + type_ref.Resolve (code_gen); + + if (type_ref is ExternTypeRef) { + ExternAssembly ea = ((ExternTypeRef) type_ref).ExternRef as ExternAssembly; + if (ea == null) + //FIXME: module.. ? + throw new NotImplementedException (); + + string name; + ExternTypeRef etr = type_ref as ExternTypeRef; + if (etr != null) + name = etr.Name; + else + name = type_ref.FullName; + + fname = String.Format ("{0}, {1}", name, ea.AssemblyName.FullName); + } else { + fname = type_ref.FullName; + } + + perm = new PEAPI.Permission (type_ref.PeapiType, fname); + + foreach (PermissionMember member in members) + perm.AddMember (member.Resolve (code_gen)); + + return perm; + } + } + +} diff --git a/mcs/ilasm/codegen/PermissionMember.cs b/mcs/ilasm/codegen/PermissionMember.cs new file mode 100644 index 00000000000..fad69eda984 --- /dev/null +++ b/mcs/ilasm/codegen/PermissionMember.cs @@ -0,0 +1,60 @@ +// +// Mono.ILASM.PermissionMember +// +// Author(s): +// Ankit Jain <JAnkit@novell.com> +// +// Copyright (C) 2006 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Reflection; + +namespace Mono.ILASM { + + public class PermissionMember { + + MemberTypes member_type; + BaseTypeRef type_ref; + string name; + object value; + + PEAPI.PermissionMember member; + + public PermissionMember (MemberTypes member_type, BaseTypeRef type_ref, string name, object value) + { + this.member_type = member_type; + this.type_ref = type_ref; + this.name = name; + this.value = value; + } + + public PEAPI.PermissionMember Resolve (CodeGen code_gen) + { + type_ref.Resolve (code_gen); + + member = new PEAPI.PermissionMember (member_type, type_ref.PeapiType, name, value); + + return member; + } + } + +} diff --git a/mcs/ilasm/codegen/PermissionSet.cs b/mcs/ilasm/codegen/PermissionSet.cs new file mode 100644 index 00000000000..95661661ce2 --- /dev/null +++ b/mcs/ilasm/codegen/PermissionSet.cs @@ -0,0 +1,67 @@ +// +// Mono.ILASM.PermissionSet +// +// Author(s): +// Ankit Jain <JAnkit@novell.com> +// +// Copyright (C) 2006 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Collections; + +namespace Mono.ILASM { + + public class PermissionSet + { + PEAPI.SecurityAction sec_action; + ArrayList permissions; + PEAPI.PermissionSet ps; + + public PermissionSet (PEAPI.SecurityAction sec_action, ArrayList permissions) + { + this.sec_action = sec_action; + this.permissions = permissions; + } + + public ArrayList Permissions { + get { return permissions; } + } + + public PEAPI.SecurityAction SecurityAction { + get { return sec_action; } + } + + public void AddPermission (Permission perm) + { + permissions.Add (perm); + } + + public PEAPI.PermissionSet Resolve (CodeGen code_gen) + { + ps = new PEAPI.PermissionSet (sec_action); + foreach (Permission perm in permissions) + ps.AddPermission (perm.Resolve (code_gen)); + + return ps; + } + } +} diff --git a/mcs/ilasm/ilasm.exe.sources b/mcs/ilasm/ilasm.exe.sources index a2e78f04df7..8a82cc9b921 100644 --- a/mcs/ilasm/ilasm.exe.sources +++ b/mcs/ilasm/ilasm.exe.sources @@ -1,6 +1,9 @@ Driver.cs Report.cs AssemblyInfo.cs +codegen/PermissionMember.cs +codegen/PermissionSet.cs +codegen/Permission.cs codegen/CodeGen.cs codegen/Module.cs codegen/DebuggingInfo.cs diff --git a/mcs/ilasm/parser/ChangeLog b/mcs/ilasm/parser/ChangeLog index 81c42a50d95..87fde52d96f 100644 --- a/mcs/ilasm/parser/ChangeLog +++ b/mcs/ilasm/parser/ChangeLog @@ -1,3 +1,20 @@ +2006-05-26 Ankit Jain <jankit@novell.com> + + Add support or 2.0 style declarative security attributes. + * ILParser.jay (primitive_type): New. Extracted from 'type'. + (field_init_primitive): New. Extracted from 'field_init', with all + primitive types. + (sec_decl | ..): New rule for 2.0 style permissions. + + (permissions): + (permission): + (permission_members): + (permission_member): + (perm_mbr_nameval_pair): + (prop_or_field): New rules for 2.0 style permissions. + + (AddSecDecl): New. + 2006-05-11 Ankit Jain <jankit@novell.com> * ILParser.jay (type |type MODREQ ..): Use CustomModifier.modreq . diff --git a/mcs/ilasm/parser/ILParser.jay b/mcs/ilasm/parser/ILParser.jay index c19ae452b55..bf04b9b150a 100644 --- a/mcs/ilasm/parser/ILParser.jay +++ b/mcs/ilasm/parser/ILParser.jay @@ -15,6 +15,9 @@ using System.Reflection; using System.Security;
using System.Security.Permissions;
+using MIPermission = Mono.ILASM.Permission;
+using MIPermissionSet = Mono.ILASM.PermissionSet;
+
namespace Mono.ILASM {
public class ILParser {
@@ -64,6 +67,22 @@ namespace Mono.ILASM { return true;
}
+ public void AddSecDecl (object perm)
+ {
+ PermPair pp = perm as PermPair;
+
+ if (pp == null) {
+ MIPermissionSet ps_20 = (MIPermissionSet) perm;
+ codegen.AddPermission (ps_20.SecurityAction, ps_20);
+ return;
+ }
+
+ if (!CheckSecurityActionValidity ((System.Security.Permissions.SecurityAction) pp.sec_action, false))
+ Report.Error (String.Format ("Invalid security action : {0}", pp.sec_action));
+
+ codegen.AddPermission (pp.sec_action, pp.perm);
+ }
+
public object ClassRefToObject (object class_ref, object val)
{
ExternTypeRef etr = class_ref as ExternTypeRef;
@@ -850,11 +869,7 @@ class_decl : method_all | data_decl
| sec_decl
{
- PermPair pp = (PermPair) $1;
- if (!CheckSecurityActionValidity ((System.Security.Permissions.SecurityAction) (short) pp.sec_action, false))
- Report.Error (String.Format ("Invalid security action : {0}", pp.sec_action));
-
- codegen.AddPermission (pp.sec_action, pp.perm);
+ AddSecDecl ($1);
}
| extsource_spec
| customattr_decl
@@ -907,10 +922,6 @@ type : generic_class_ref {
$$ = new PrimitiveTypeRef (PrimitiveType.Object, "System.Object");
}
- | K_STRING
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.String, "System.String");
- }
| K_VALUE K_CLASS class_ref
{
BaseClassRef class_ref = (BaseClassRef) $3;
@@ -985,28 +996,12 @@ type : generic_class_ref {
$$ = new MethodPointerTypeRef ((CallConv) $2, (BaseTypeRef) $3, (ArrayList) $6);
}
- | K_TYPEDREF
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.TypedRef,
- "System.TypedReference");
- }
- | K_CHAR
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");
- }
- | K_WCHAR
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");
- }
- | K_VOID
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.Void, "System.Void");
- }
- | K_BOOL
- {
- $$ = new PrimitiveTypeRef (PrimitiveType.Boolean, "System.Boolean");
- }
- | K_INT8
+ | primitive_type
+ ;
+
+ ;
+
+primitive_type : K_INT8
{
$$ = new PrimitiveTypeRef (PrimitiveType.Int8, "System.SByte");
}
@@ -1046,7 +1041,6 @@ type : generic_class_ref {
$$ = new PrimitiveTypeRef (PrimitiveType.UInt16, "System.UInt16");
}
-
| K_UNSIGNED K_INT32
{
$$ = new PrimitiveTypeRef (PrimitiveType.UInt32, "System.UInt32");
@@ -1068,7 +1062,6 @@ type : generic_class_ref // TODO: Is this the proper full name
$$ = new PrimitiveTypeRef (PrimitiveType.NativeInt, "System.IntPtr");
}
-
| K_NATIVE K_UNSIGNED K_INT
{
$$ = new PrimitiveTypeRef (PrimitiveType.NativeUInt, "System.UIntPtr");
@@ -1077,6 +1070,31 @@ type : generic_class_ref {
$$ = new PrimitiveTypeRef (PrimitiveType.NativeUInt, "System.UIntPtr");
}
+ | K_TYPEDREF
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.TypedRef,
+ "System.TypedReference");
+ }
+ | K_CHAR
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");
+ }
+ | K_WCHAR
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.Char, "System.Char");
+ }
+ | K_VOID
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.Void, "System.Void");
+ }
+ | K_BOOL
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.Boolean, "System.Boolean");
+ }
+ | K_STRING
+ {
+ $$ = new PrimitiveTypeRef (PrimitiveType.String, "System.String");
+ }
;
bounds : bound
@@ -1567,7 +1585,7 @@ init_opt : /* EMPTY */ }
;
-field_init : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS
+field_init_primitive : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS
{
$$ = new FloatConst (Convert.ToSingle ($3));
}
@@ -1627,6 +1645,9 @@ field_init : K_FLOAT32 OPEN_PARENS float64 CLOSE_PARENS {
$$ = new BoolConst ((bool) $3);
}
+ ;
+
+field_init : field_init_primitive
| K_BYTEARRAY bytes_list
{
$$ = new ByteArrConst ((byte[]) $2);
@@ -2127,11 +2148,7 @@ method_decl : D_EMITBYTE int32 | instr
| sec_decl
{
- PermPair pp = (PermPair) $1;
- if (!CheckSecurityActionValidity ((System.Security.Permissions.SecurityAction) (short) pp.sec_action, false))
- Report.Error (String.Format ("Invalid security action : {0}", pp.sec_action));
-
- codegen.AddPermission (pp.sec_action, pp.perm);
+ AddSecDecl ($1);
}
| extsource_spec
| language_decl
@@ -2785,6 +2802,84 @@ sec_decl : D_PERMISSION sec_action type_spec OPEN_PARENS nameval_pairs CLOSE_PA psa.XML = (string) $3;
$$ = new PermPair ((PEAPI.SecurityAction) $2, psa.CreatePermissionSet ());
}
+ | D_PERMISSIONSET sec_action ASSIGN OPEN_BRACE permissions CLOSE_BRACE
+ {
+#if NET_2_0
+ $$ = new MIPermissionSet ((PEAPI.SecurityAction) $2, (ArrayList) $5);
+#else
+ Report.Error ("Use ilasm2 for 2.0 style declarative security attributes.");
+#endif
+ }
+ ;
+
+permissions : permission
+ {
+ ArrayList list = new ArrayList ();
+ list.Add ($1);
+ $$ = list;
+ }
+ | permissions COMMA permission
+ {
+ ArrayList list = (ArrayList) $1;
+ list.Add ($3);
+ $$ = list;
+ }
+ ;
+
+permission : class_ref ASSIGN OPEN_BRACE permission_members CLOSE_BRACE
+ {
+ $$ = new MIPermission ((BaseTypeRef) $1, (ArrayList) $4);
+ }
+ ;
+
+permission_members : permission_member
+ {
+ ArrayList list = new ArrayList ();
+ list.Add ($1);
+ $$ = list;
+ }
+ | permission_members permission_member
+ {
+ ArrayList list = (ArrayList) $1;
+ list.Add ($2);
+ $$ = list;
+ }
+ ;
+
+permission_member : prop_or_field primitive_type perm_mbr_nameval_pair
+ {
+ NameValuePair pair = (NameValuePair) $3;
+ $$ = new PermissionMember ((MemberTypes) $1, (BaseTypeRef) $2, pair.Name, pair.Value);
+ }
+ | prop_or_field K_ENUM class_ref perm_mbr_nameval_pair
+ {
+ NameValuePair pair = (NameValuePair) $4;
+ $$ = new PermissionMember ((MemberTypes) $1, (BaseTypeRef) $3, pair.Name, pair.Value);
+ }
+ ;
+
+perm_mbr_nameval_pair : SQSTRING ASSIGN field_init_primitive
+ {
+ $$ = new NameValuePair ((string) $1, (PEAPI.Constant) $3);
+ }
+ | SQSTRING ASSIGN K_BYTEARRAY bytes_list
+ {
+ $$ = new NameValuePair ((string) $1, new ByteArrConst ((byte[]) $4));
+ }
+ | SQSTRING ASSIGN K_STRING OPEN_PARENS SQSTRING CLOSE_PARENS
+ {
+ $$ = new NameValuePair ((string) $1, new StringConst ((string) $5));
+ }
+ ;
+
+prop_or_field : K_PROPERTY
+ {
+ $$ = MemberTypes.Property;
+ }
+ | K_FIELD
+ {
+ $$ = MemberTypes.Field;
+ }
;
nameval_pairs : nameval_pair
@@ -2985,7 +3080,13 @@ assembly_decl : D_PUBLICKEY ASSIGN bytes_list }
| sec_decl
{
- PermPair pp = (PermPair) $1;
+ PermPair pp = $1 as PermPair;
+ if (pp == null) {
+ MIPermissionSet ps_20 = (MIPermissionSet) $1;
+ codegen.AddAssemblyPermission (ps_20.SecurityAction, ps_20);
+ break;
+ }
+
if (!CheckSecurityActionValidity ((System.Security.Permissions.SecurityAction) (short) pp.sec_action, true))
Report.Error (String.Format ("Invalid security action : {0}", pp.sec_action));
diff --git a/mcs/ilasm/scanner/ChangeLog b/mcs/ilasm/scanner/ChangeLog index 4a27fa6a392..064721497aa 100644 --- a/mcs/ilasm/scanner/ChangeLog +++ b/mcs/ilasm/scanner/ChangeLog @@ -1,3 +1,7 @@ +2006-05-26 Ankit Jain <jankit@novell.com> + + * ILTables.cs (keywords): Move 'property' out of NET_2_0 . + 2006-05-11 Ankit Jain <jankit@novell.com> * ILTokenizingExpcetion.cs (Location): Remove. Already inherited from diff --git a/mcs/ilasm/scanner/ILTables.cs b/mcs/ilasm/scanner/ILTables.cs index 34273adefc8..e215ff960ad 100644 --- a/mcs/ilasm/scanner/ILTables.cs +++ b/mcs/ilasm/scanner/ILTables.cs @@ -257,8 +257,8 @@ namespace Mono.ILASM { keywords ["modreq"] = new ILToken (Token.K_MODREQ, "modreq");
keywords ["modopt"] = new ILToken (Token.K_MODOPT, "modopt");
keywords ["typedref"] = new ILToken (Token.K_TYPEDREF, "typedref");
-#if NET_2_0 || BOOTSTRAP_NET_2_0
keywords ["property"] = new ILToken (Token.K_PROPERTY, "property");
+#if NET_2_0 || BOOTSTRAP_NET_2_0
keywords ["type"] = new ILToken (Token.K_TYPE, "type");
#endif
keywords ["refany"] = new ILToken (Token.K_TYPEDREF, "typedref");
diff --git a/mcs/ilasm/tests/ChangeLog b/mcs/ilasm/tests/ChangeLog index 95d5b124e68..92a9ed0d7c6 100644 --- a/mcs/ilasm/tests/ChangeLog +++ b/mcs/ilasm/tests/ChangeLog @@ -1,3 +1,8 @@ +2006-05-26 Ankit Jain <jankit@novell.com> + + * test-perm_pass-3.il: New. Test for 2.0 style declarative security + attributes. + 2006-05-11 Ankit Jain <jankit@novell.com> * test-modifiers.il: New. Test for custom modifiers, modreq & modopt. diff --git a/mcs/ilasm/tests/test-perm_pass-3.il b/mcs/ilasm/tests/test-perm_pass-3.il new file mode 100644 index 00000000000..77e3ffe3450 --- /dev/null +++ b/mcs/ilasm/tests/test-perm_pass-3.il @@ -0,0 +1,60 @@ +.assembly extern mscorlib
+{
+ .ver 2:0:0:0
+}
+.assembly 'test-perm_pass-3'
+{
+ .permissionset reqopt
+ "<PermissionSet class=\"System.Security.PermissionSe"
+ + "t\"\n version=\"1\">\n <IPermission class=\"System"
+ + ".Security.Permissions.SecurityPermission, mscorlib, Version="
+ + "2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\n "
+ + " version=\"1\"\n Flags=\"SkipVerifi"
+ + "cation\"/>\n</PermissionSet>\n"
+
+ .permissionset assert = {
+ [mscorlib]System.Security.Permissions.IsolatedStorageFilePermissionAttribute = {
+ property enum [mscorlib]System.Security.Permissions.IsolatedStorageContainment 'UserQuota' = int32 (5)
+ }
+ }
+
+ .ver 0:0:0:0
+}
+
+.module 'test-perm_pass-3.dll'
+.imagebase 0x00400000
+.file alignment 0x00000200
+.stackreserve 0x00100000
+.subsystem 0x0003
+.corflags 0x00000001
+
+.class public foo
+{
+ .permissionset assert = {
+ [mscorlib]System.Security.Permissions.ReflectionPermissionAttribute = {
+ property bool 'MemberAccess' = bool(true)
+ }
+ }
+
+ .method public hidebysig newslot abstract virtual
+ instance void abc() cil managed
+ {
+ .permissionset demand =
+ {
+ [mscorlib]System.Security.Permissions.IsolatedStorageFilePermissionAttribute = {
+ property enum [mscorlib]System.Security.Permissions.IsolatedStorageContainment 'UserQuota' = int32 (50)
+ },
+
+ [mscorlib]System.Security.Permissions.IsolatedStorageFilePermissionAttribute = {
+ property enum [mscorlib]System.Security.Permissions.IsolatedStorageContainment 'UsageAllowed' = int32 (123)
+ },
+
+ [mscorlib]System.Security.Permissions.ReflectionPermissionAttribute = {
+ property char 'MemberAccess' = char (16)
+ }
+ }
+
+
+ }
+
+}
|