Beispiel #1
0
		public static unsafe TypeDefRow ResolveTypeRef (AssemblyMetadata assembly, TypeRefRow row,
			out AssemblyMetadata destAssembly)
		{
			uint token;

			return ResolveTypeRef (assembly, row, out destAssembly, out token);
		}
Beispiel #2
0
		public unsafe static CString8 *GetString (AssemblyMetadata assembly, uint str)
		{
			int length = 0;
			CString8 *buf;
			int index = 0;

			Diagnostics.Assert (str < assembly.StringsHeap.Length,
				"Runtime.ReadString(): parameter `str' is out of range");
			Diagnostics.Assert (assembly != null,
				"Runtime.ReadString(): parameter `assembly' is null");

			length = GetStringLength (assembly, str);
			buf = CString8.Create (length);

			for (int x = (int) str; index < length; ++x) {
				buf->SetChar (index, assembly.StringsHeap [x]);
				++index;
			}

			return buf;
		}
Beispiel #3
0
		public static unsafe TypeRefRow GetTypeRef (AssemblyMetadata assembly, string ns, string name, out uint token)
		{
			CString8 *cns, cname;

			cns = CString8.Copy (ns);
			cname = CString8.Copy (name);

			try {
				return GetTypeRef (assembly, cns, cname, out token);
			} finally {
				MemoryManager.Free (cns);
				MemoryManager.Free (cname);
			}
		}
Beispiel #4
0
		public static unsafe AssemblyMetadata ResolveAssemblyRef (AssemblyMetadata assembly, AssemblyRefRow row)
		{
			// TODO: check more than just the name here

			Diagnostics.Assert (assembly != null,
				"Runtime.ResolveAssemblyRef(): parameter `assembly' is null");
			Diagnostics.Assert (row != null,
				"Runtime.ResolveAssemblyRef(): parameter `row' is null");

			CString8 *name = GetString (assembly, row.Name);
			AssemblyMetadata result = null;

			for (int x = 0; x < Root.Assemblies.Length; ++x) {
				AssemblyMetadata assembly2 = Root.Assemblies [x];
				CString8 *name2;

				if (assembly2.Assembly.Length == 0)
					continue;	// unnamed assembly

				name2 = GetString (assembly2, assembly2.Assembly [0].Name);

				if (name->Compare (name2) == 0)
					result = assembly2;

				MemoryManager.Free (name2);

				if (result != null)
					break;
			}

			MemoryManager.Free (name);
			return result;
		}
Beispiel #5
0
		protected Signature (AssemblyMetadata assembly):
			base (assembly)
		{
		}
Beispiel #6
0
		static bool IsValidAssemblyMetadata (AssemblyMetadata asm)
		{
			for (int x = 0; x < Root.Assemblies.Length; ++x) {
				if (Root.Assemblies [x] == asm)
					return true;
			}

			return false;
		}
Beispiel #7
0
		public unsafe static void DumpTypeDef (AssemblyMetadata assembly, TypeDefRow row, int index)
		{
			Debug.COM1.Write (" ");
			Debug.COM1.Write ("TypeDefRow#");
			Debug.COM1.Write (index);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.Flags, true);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.Name, true);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.Namespace, true);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.Extends, true);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.FieldList, true);
			Debug.COM1.Write (" ");
			Debug.COM1.Write ((int)row.MethodList, true);
			Debug.COM1.Write (" ");
			PrintTypeName (assembly, row);
			Debug.COM1.WriteLine ();
		}
Beispiel #8
0
		public unsafe static void PrintTypeName (AssemblyMetadata assembly, TypeRefRow type)
		{
			CString8 *name, ns;

			name = GetString (assembly, type.Name);
			ns = GetString (assembly, type.Namespace);

			Debug.COM1.Write (ns);
			Debug.COM1.Write (".");
			Debug.COM1.Write (name);

			MemoryManager.Free (name);
			MemoryManager.Free (ns);
		}
Beispiel #9
0
		public static TypeDefRow GetType (AssemblyMetadata assembly, TokenType type, uint rid)
		{
			AssemblyMetadata dest;
			return GetType (assembly, type, rid, out dest);
		}
Beispiel #10
0
		public static TypeDefRow GetType (AssemblyMetadata assembly, MetadataToken token)
		{
			AssemblyMetadata dest;
			return GetType (assembly, token, out dest);
		}
Beispiel #11
0
		public static TypeDefRow GetType (AssemblyMetadata assembly, MetadataToken token, out AssemblyMetadata dest)
		{
			return GetType (assembly, token.Type, token.RID, out dest);
		}
Beispiel #12
0
		public static unsafe ModuleRow GetModule (AssemblyMetadata assembly, CString8 *name)
		{
			Diagnostics.Assert (assembly == null,
				"Runtime.GetModule(): parameter `assembly' is null");
			Diagnostics.Assert (name == null,
				"Runtime.GetModule(): parameter `name' is null");

			for (int x = 0; x < assembly.Module.Length; ++x) {
				ModuleRow row = assembly.Module [x];
				int len = GetStringLength (assembly, row.Name);

				if (name->Compare (0, assembly.StringsHeap, (int) row.Name, len) == 0)
					return row;
			}

			return null;
		}
Beispiel #13
0
		public static unsafe ModuleRow ResolveModuleRef (AssemblyMetadata assembly, ModuleRefRow row, out AssemblyMetadata destAssembly)
		{
			CString8 *moduleName;
			ModuleRow result = null;

			Diagnostics.Assert (assembly != null,
				"Runtime.ResolveModuleRef(): parameter `assembly' is null");
			Diagnostics.Assert (row != null,
				"Runtime.ResolveModuleRef(): parameter `row' is null");

			moduleName = GetString (assembly, row.Name);
			destAssembly = null;

			for (int x = 0; x < Root.Assemblies.Length; ++x) {
				AssemblyMetadata assembly2 = Root.Assemblies [x];

				for (int y = 0; y < assembly2.Module.Length; ++y) {
					ModuleRow mod = assembly2.Module [y];
					int len = GetStringLength (assembly2, mod.Name);

					if (moduleName->Compare (0, assembly2.StringsHeap, (int) mod.Name, len) == 0) {
						result = mod;
						destAssembly = assembly2;
						break;
					}
				}
			}

			return result;
		}
Beispiel #14
0
		public static ModuleRow ResolveModuleRef (AssemblyMetadata assembly, ModuleRefRow row)
		{
			AssemblyMetadata assem2;

			Diagnostics.Assert (assembly != null,
				"Runtime.ResolveModuleRef(): parameter `assembly' is null");

			Diagnostics.Assert (row != null,
				"Runtime.ResolveModuleRef(): parameter `row' is null");

			return ResolveModuleRef (assembly, row, out assem2);
		}
Beispiel #15
0
		public static int GetStringLength (AssemblyMetadata assembly, uint str)
		{
			int len = 0;

			Diagnostics.Assert (assembly != null,
				"Runtime.GetStringLength(): parameter `assembly' is null");

			Diagnostics.Assert (str < assembly.StringsHeap.Length,
				"Runtime.GetStringLength(): invalid StringsHeap token");

			for (int z = (int) str; z < assembly.StringsHeap.Length; ++z) {
				if (assembly.StringsHeap [z] == (byte) '\0')
					break;
				++len;
			}

			return len;
		}
Beispiel #16
0
		public static unsafe TypeRefRow GetTypeRef (AssemblyMetadata assembly, CString8 *ns, CString8 *name, out uint token)
		{
			TypeRefRow result = null;
			token = 0;

			for (int x = 0; x < assembly.TypeRef.Length; ++x) {
				int nameLength = 0;
				int nsLength = 0;
				TypeRefRow inspect = assembly.TypeRef [x];

				nameLength = GetStringLength (assembly, inspect.Name);
				nsLength = GetStringLength (assembly, inspect.Namespace);

				if (name->Compare (0, assembly.StringsHeap, (int) inspect.Name, nameLength) != 0)
					continue;
				if (ns->Compare (0, assembly.StringsHeap, (int) inspect.Namespace, nsLength) != 0)
					continue;

				result = inspect;
				token = (uint)TokenType.TypeRef | (uint)x+1U;
				break;
			}

			return result;
		}
Beispiel #17
0
		public static unsafe bool IsTypeSystemObject (AssemblyMetadata assembly, TokenType type, uint rid)
		{
			CString8 *systemNS = CString8.Copy ("System");
			CString8 *objectName = CString8.Copy ("Object");
			int nameLen, nsLen;
			bool result = false;

			Diagnostics.Assert (type == TokenType.TypeRef || type == TokenType.TypeDef,
				"Runtime.IsTypeSystemObject(): invalid metadata token, must be of type `TypeDef' or `TypeRef' only");

			if (type == TokenType.TypeRef) {
				TypeRefRow row;

				// Handle this specially in case mscorlib is not included
				// (otherwise we will definitely get null from GetType())

				Diagnostics.Assert (rid < assembly.TypeRef.Length,
					"Runtime.IsTypeSystemObject(): invalid metadata token, too large for TypeRef table!");

				row = assembly.TypeRef [rid - 1];

				nsLen = GetStringLength (assembly, row.Namespace);
				nameLen = GetStringLength (assembly, row.Name);

				if (systemNS->Compare (0, assembly.StringsHeap, (int) row.Namespace, nsLen) == 0 &&
				    objectName->Compare (0, assembly.StringsHeap, (int) row.Name, nameLen) == 0)
					result = true;
			} else if (type == TokenType.TypeDef) {
				TypeDefRow row;

				row = GetType (assembly, type, rid);
				nsLen = GetStringLength (assembly, row.Namespace);
				nameLen = GetStringLength (assembly, row.Name);

				if (systemNS->Compare (0, assembly.StringsHeap, (int) row.Namespace, nsLen) == 0 &&
				    objectName->Compare (0, assembly.StringsHeap, (int) row.Name, nameLen) == 0)
					result = true;
			}

			MemoryManager.Free (systemNS);
			MemoryManager.Free (objectName);

			return result;
		}
Beispiel #18
0
		public static TypeDefRow GetType (AssemblyMetadata assembly, TokenType type, uint rid,
			out AssemblyMetadata destAssembly)
		{
			uint token;

			return GetType (assembly, type, rid, out destAssembly, out token);
		}
Beispiel #19
0
		public static TypeDefRow GetType (AssemblyMetadata assembly, TokenType type, uint rid,
			out AssemblyMetadata destAssembly, out uint typeDefToken)
		{
			if (type != TokenType.TypeRef && type != TokenType.TypeDef) {
				Debug.COM1.Write ("Token type: ");
				Debug.COM1.WriteLine (MetadataToken.GetTokenTypeString (type));
				throw new System.Exception ("token type must be either TypeRef or TypeDef");
			}

			Diagnostics.Assert (type == TokenType.TypeRef || type == TokenType.TypeDef,
				"Runtime.GetType(): token type must be either TypeRef or TypeDef");

			if (type == TokenType.TypeDef) {
				Diagnostics.Assert (rid < assembly.TypeDef.Length,
					"Runtime.GetType(): token out of range for TypeDef table");

				destAssembly = assembly;
				typeDefToken = rid;
				return assembly.TypeDef [rid - 1];
			} else {
				TypeRefRow row = assembly.TypeRef [rid - 1];

				return ResolveTypeRef (assembly, row, out destAssembly, out typeDefToken);
			}
		}
Beispiel #20
0
		public static unsafe TypeDefRow GetType (CString8 *ns, CString8 *name, out AssemblyMetadata assembly, out uint token)
		{
			token = 0;
			assembly = null;

			Diagnostics.Assert (ns != null,
				"Runtime.GetType(): parameter `ns' is null");
			Diagnostics.Assert (name != null,
				"Runtime.GetType(): parameter `name' is null");

			for (int x = 0; x < Root.Assemblies.Length; ++x) {
				TypeDefRow row = GetType (Root.Assemblies [x], ns, name, out token);

				if (row != null) {
					assembly = Root.Assemblies [x];
					return row;
				}
			}

			return null;
		}
Beispiel #21
0
		static bool HasValidMetadataToken (AssemblyMetadata assembly, uint token)
		{
			TokenType type;
			uint rid;

			MetadataToken.Decode (token, out type, out rid);

			if (type != TokenType.TypeDef && type != TokenType.TypeRef)
				return false;

			if (type == TokenType.TypeDef && rid-1 >= assembly.TypeDef.Length)
				return false;
			else if (type == TokenType.TypeRef && rid-1 >= assembly.TypeRef.Length)
				return false;

			return true;
		}
Beispiel #22
0
		public static unsafe TypeDefRow GetType (CString8 *ns, CString8 *name, out AssemblyMetadata assembly)
		{
			uint token;

			return GetType (ns, name, out assembly, out token);
		}
Beispiel #23
0
		public static TypeDefRow ResolveTypeRef (AssemblyMetadata assembly, TypeRefRow row)
		{
			AssemblyMetadata dest;
			return ResolveTypeRef (assembly, row, out dest);
		}
Beispiel #24
0
		public static unsafe TypeDefRow GetType (AssemblyMetadata assembly, CString8 *ns, CString8 *name)
		{
			uint token;

			return GetType (assembly, name, ns, out token);
		}
Beispiel #25
0
		public static unsafe TypeDefRow ResolveTypeRef (AssemblyMetadata assembly, TypeRefRow row,
			out AssemblyMetadata destAssembly, out uint typeDefToken)
		{
			CString8 *typeName = GetString (assembly, row.Name);
			CString8 *typeNamespace = GetString (assembly, row.Namespace);
			TokenType resScopeType;
			uint resScopeRID;
			TypeDefRow result = null;

			Diagnostics.Assert (assembly != null,
				"Runtime.ResolveTypeDef(): parameter `assembly' is null");
			Diagnostics.Assert (row != null,
				"Runtime.ResolveTypeDef(): parameter `row' is null");

			MetadataToken.Decode (row.ResolutionScope, out resScopeType, out resScopeRID);

			Diagnostics.Assert (resScopeType == TokenType.Assembly || resScopeType == TokenType.Module ||
					resScopeType == TokenType.AssemblyRef || resScopeType == TokenType.ModuleRef,
				"Runtime.ResolveTypeRef(): resolution scope token is of an invalid type");

			destAssembly = null;
			typeDefToken = 0;

			if (resScopeType == TokenType.Assembly || resScopeType == TokenType.Module || resScopeType == TokenType.ModuleRef) {
				Diagnostics.Assert (resScopeRID == 0,
					"Runtime.ResolveTypeRef(): resolution scope of Assembly must be zero!");
				destAssembly = assembly;
				return GetType (assembly, typeName, typeNamespace);
			} else if (resScopeType == TokenType.AssemblyRef) {
				Diagnostics.Assert (resScopeRID < assembly.AssemblyRef.Length,
					"Runtime.ResolveTypeRef(): AssemblyRef metadata token out of range");
				AssemblyMetadata assembly2 = ResolveAssemblyRef (assembly, assembly.AssemblyRef [resScopeRID]);

				destAssembly = assembly2;
				return GetType (assembly2, typeName, typeNamespace);
			}

			MemoryManager.Free (typeName);
			MemoryManager.Free (typeNamespace);

			return result;
		}
Beispiel #26
0
		public static unsafe TypeRefRow GetTypeRef (CString8 *ns, CString8 *name, out AssemblyMetadata assembly, out uint token)
		{
			assembly = null;
			token = 0;

			for (int x = 0; x < Root.Assemblies.Length; ++x) {
				TypeRefRow result = GetTypeRef (Root.Assemblies [x], ns, name, out token);

				if (result != null) {
					assembly = Root.Assemblies [x];
					return result;
				}
			}

			return null;
		}
Beispiel #27
0
		public FieldSignature (AssemblyMetadata assembly, FieldRow fieldRow):
			base (assembly)
		{
			this.Field = fieldRow;

			int pos = (int)fieldRow.Signature - 1;
			byte [] blob = assembly.BlobHeap;
			int cmodCount = 0;
			int savedPos = 0;
			int cmodIndex = 0;

			// The first byte must be FIELD (0x6)

			Diagnostics.Assert (blob [pos++] == 0x6,
				"FieldSignature.ctor(): blob signature is not FIELD (0x6)");

			//if (blob [pos] != 0x6) {
			//	throw new Exception ("Invalid signature type for field"); // explode
			//}

			// First determine the amount of cmods we have

			savedPos = pos;

			while (true) {
				ElementType type = (ElementType) DecompressValue (blob, ref pos);

				if (type == ElementType.CModReqD || type == ElementType.CModOpt)
					++cmodCount;
				else
					break;
			}

			this.CustomModifiers = new SigCustomModifier [cmodCount];
			pos = savedPos;

			while (true) {
				ElementType type = (ElementType) DecompressValue (blob, ref pos);

				if (type == ElementType.CModReqD || type == ElementType.CModOpt) {
					CustomModifiers [cmodIndex++] =
						new SigCustomModifier (type, DecompressValue (blob, ref pos));
				} else {
					// now for the "Type"

					switch (type) {
					case ElementType.Class:
						this.Type = new SigClassType (type, DecompressValue (blob, ref pos));
						break;
					case ElementType.ValueType:
						this.Type = new SigClassType (type, DecompressValue (blob, ref pos));
						break;
					case ElementType.Boolean:
					case ElementType.Char:
					case ElementType.I1:
					case ElementType.U1:
					case ElementType.I2:
					case ElementType.U2:
					case ElementType.I4:
					case ElementType.U4:
					case ElementType.I8:
					case ElementType.U8:
					case ElementType.R4:
					case ElementType.R8:
					case ElementType.I:
					case ElementType.U:
					case ElementType.Object:
					case ElementType.String:
						this.Type = new SigType (type);
						break;
					default:
						//throw new NotSupportedException (); // explode
						break;
					}

					// hereby ends this thousand-year journey
					break;
				}
			}

			++pos;
		}
Beispiel #28
0
		public static unsafe bool IsBaseClassOf (AssemblyMetadata typeAsm, uint type, AssemblyMetadata baseAsm, uint baseType)
		{
			TokenType typeTokType, baseTokType, interimTokType;
			uint typeRID, baseRID, interimRID;
			TypeDefRow typeDef, baseDef, interimDef;
			AssemblyMetadata interimAssembly;
			TypeDefRow lastInterimDef = null;

			if (typeAsm == baseAsm && type == baseType)
				return true;

			MetadataToken.Decode (type, out typeTokType, out typeRID);
			MetadataToken.Decode (baseType, out baseTokType, out baseRID);

			if (IsTypeSystemObject (baseAsm, baseTokType, baseRID))
				return true;

			// Do not assume that 'baseDef' is more than null!

			typeDef = GetType (typeAsm, typeTokType, typeRID);
			baseDef = GetType (baseAsm, baseTokType, baseRID);
			interimDef = typeDef;
			interimAssembly = typeAsm;

			MetadataToken.Decode (typeDef.Extends, out interimTokType, out interimRID);

			while (true) {
				// If we've hit Object then the two types are unrelated

				if (IsTypeSystemObject (interimAssembly, interimTokType, interimRID))
					return false;

				lastInterimDef = interimDef;
				interimDef = GetType (interimAssembly, interimTokType, interimRID, out interimAssembly);

				// If baseDef is null, it's usually because of a TypeRef which pointed to an assembly that was not
				// included when AOTing, the primary example of this is mscorlib. To handle this, we make sure
				// that the involved tokens are TypeRefs, and if the interimRID == baseRID then we have reached
				// the base type we want.

				if (baseDef == null && interimDef == null && interimTokType == TokenType.TypeRef &&
				    baseTokType == TokenType.TypeRef && interimAssembly == baseAsm && baseRID == interimRID)
					return true;

				Diagnostics.Assert (interimDef != null,
					"Runtime.IsBaseClassOf(): Failed to find TypeDef for the base class of a type");

				Diagnostics.Assert (lastInterimDef != interimDef,
					"Runtime.IsBaseClassOf(): ERROR: TypeDef's base class is itself!!!");

				if (interimDef == baseDef)
					return true;

				// Reiterate

				MetadataToken.Decode (interimDef.Extends, out interimTokType, out interimRID);
			}
		}