public void PersistentName() { const string newName = "MyResource"; var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var resource = CreateDummyResource(); resource.Name = newName; image.Assembly.Resources.Add(resource); var mapping = header.UnlockMetadata(); var resourceRow = header.GetStream <TableStream>() .GetTable <ManifestResourceTable>()[(int)(mapping[resource].Rid - 1)]; Assert.Equal(newName, header.GetStream <StringStream>().GetStringByOffset(resourceRow.Column3)); image = header.LockMetadata(); resource = (ManifestResource)image.ResolveMember(mapping[resource]); Assert.Equal(newName, resource.Name); }
public void PersistentData() { var newData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var resource = CreateDummyResource(); resource.Data = newData; image.Assembly.Resources.Add(resource); var mapping = header.UnlockMetadata(); var resourceRow = header.GetStream <TableStream>() .GetTable <ManifestResourceTable>()[(int)(mapping[resource].Rid - 1)]; Assert.Equal(newData, header.NetDirectory.ResourcesManifest.GetResourceData(resourceRow.Column1)); image = header.LockMetadata(); resource = (ManifestResource)image.ResolveMember(mapping[resource]); Assert.Equal(newData, resource.Data); }
public void PersistentAttributes() { const ManifestResourceAttributes newAttributes = ManifestResourceAttributes.Private; var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var resource = CreateDummyResource(); resource.Attributes = newAttributes; image.Assembly.Resources.Add(resource); var mapping = header.UnlockMetadata(); var resourceRow = header.GetStream <TableStream>() .GetTable <ManifestResourceTable>()[(int)(mapping[resource].Rid - 1)]; Assert.Equal(newAttributes, resourceRow.Column2); image = header.LockMetadata(); resource = (ManifestResource)image.ResolveMember(mapping[resource]); Assert.Equal(newAttributes, resource.Attributes); }
public void CloneParameterReferences() { var sourceAssembly = WindowsAssembly.FromFile(typeof(SimpleClass).Assembly.Location); var sourceImage = sourceAssembly.NetDirectory.MetadataHeader.LockMetadata(); var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var cloner = new MemberCloner(image); var variablesClass = sourceImage.Assembly.Modules[0].TopLevelTypes.First(x => x.Name == "Variables"); var clonedClass = cloner.CloneType(variablesClass); image.Assembly.Modules[0].TopLevelTypes.Add(clonedClass); foreach (var clonedMethod in clonedClass.Methods.Where(x => x.CilMethodBody != null)) { var body = clonedMethod.CilMethodBody; var parameters = clonedMethod.Signature.Parameters; var originalBody = variablesClass.Methods.First(x => x.Name == clonedMethod.Name).CilMethodBody; var originalParameters = originalBody.Method.Signature.Parameters; foreach (var instruction in body.Instructions.Where(x => x.OpCode.OperandType == CilOperandType.InlineArgument || x.OpCode.OperandType == CilOperandType.ShortInlineArgument)) { var originalInstruction = originalBody.Instructions.GetByOffset(instruction.Offset); Assert.NotNull(instruction.Operand); Assert.Equal(originalParameters.IndexOf((ParameterSignature)originalInstruction.Operand), parameters.IndexOf((ParameterSignature)instruction.Operand)); } } }
public void ExtensionMethodTest() { var sourceAssembly = WindowsAssembly.FromFile(typeof(StaticClass).Assembly.Location); var sourceImage = sourceAssembly.NetDirectory.MetadataHeader.LockMetadata(); var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); var cloner = new MemberCloner(image); var staticClass = sourceImage.Assembly.Modules[0].TopLevelTypes.First(x => x.Name == "StaticClass"); var clonedType = cloner.CloneType(staticClass); var main = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static, new MethodSignature(image.TypeSystem.Void)); main.CilMethodBody = new CilMethodBody(main); var instructions = main.CilMethodBody.Instructions; instructions.Add(CilInstruction.Create(CilOpCodes.Ldc_I4_1)); instructions.Add(CilInstruction.Create(CilOpCodes.Call, clonedType.Methods.First(x => x.Name == "SomeExtension"))); instructions.Add(CilInstruction.Create(CilOpCodes.Call, importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new[] { typeof(int) })))); instructions.Add(CilInstruction.Create(CilOpCodes.Ret)); image.Assembly.Modules[0].TopLevelTypes.Add(clonedType); image.Assembly.Modules[0].TopLevelTypes[0].Methods.Add(main); image.ManagedEntrypoint = main; header.UnlockMetadata(); _context.VerifyOutput(assembly, "4"); }
public void PersistentNamedArgument() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); var namedArg = new CustomAttributeNamedArgument(CustomAttributeArgumentMemberType.Property, image.TypeSystem.Boolean, "Exclude", new CustomAttributeArgument(image.TypeSystem.Boolean, new ElementSignature(true))); var customAttribute = new CustomAttribute( (ICustomAttributeType)importer.ImportMethod( typeof(ObfuscationAttribute).GetConstructor(Type.EmptyTypes)), new CustomAttributeSignature { NamedArguments = { namedArg } }); image.Assembly.CustomAttributes.Add(customAttribute); header.UnlockMetadata(); image = header.LockMetadata(); Assert.Single(image.Assembly.CustomAttributes); var newArg = image.Assembly.CustomAttributes[0].Signature.NamedArguments[0]; Assert.Equal(namedArg.MemberName, newArg.MemberName); Assert.Equal(namedArg.ArgumentMemberType, newArg.ArgumentMemberType); Assert.Equal(namedArg.Argument.ArgumentType, newArg.Argument.ArgumentType, _comparer); Assert.Equal(namedArg.Argument.Elements[0].Value, newArg.Argument.Elements[0].Value); }
public void PersistentEnumArgument() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); const int argument = (int)DebuggableAttribute.DebuggingModes.EnableEditAndContinue; var customAttribute = new CustomAttribute( (ICustomAttributeType)importer.ImportMethod( typeof(DebuggableAttribute).GetConstructor(new[] { typeof(DebuggableAttribute.DebuggingModes) })), new CustomAttributeSignature(new[] { new CustomAttributeArgument(importer.ImportTypeSignature(typeof(DebuggableAttribute.DebuggingModes)), new ElementSignature(argument)) })); image.Assembly.CustomAttributes.Add(customAttribute); header.UnlockMetadata(); image = header.LockMetadata(); Assert.Single(image.Assembly.CustomAttributes); Assert.Equal(argument, image.Assembly.CustomAttributes[0].Signature.FixedArguments[0].Elements[0].Value); }
public void PersistentTypeArgument() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); var argument = importer.ImportTypeSignature(typeof(object)); var customAttribute = new CustomAttribute( (ICustomAttributeType)importer.ImportMethod( typeof(DesignerAttribute).GetConstructor(new[] { typeof(Type) })), new CustomAttributeSignature(new[] { new CustomAttributeArgument(image.TypeSystem.Type, new ElementSignature(argument)) })); image.Assembly.CustomAttributes.Add(customAttribute); header.UnlockMetadata(); image = header.LockMetadata(); Assert.Single(image.Assembly.CustomAttributes); Assert.Equal(argument, image.Assembly.CustomAttributes[0].Signature.FixedArguments[0].Elements[0].Value as ITypeDescriptor, _comparer); }
private TypeSpecification CreateDummyType() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var typeSpec = (TypeSpecification)importer.ImportType(new TypeSpecification(image.TypeSystem.Void)); var method = new MethodDefinition("SomeMethod", MethodAttributes.Public | MethodAttributes.Static, new MethodSignature(image.TypeSystem.Void)); method.CilMethodBody = new CilMethodBody(method) { Instructions = { CilInstruction.Create(CilOpCodes.Ldtoken, typeSpec), CilInstruction.Create(CilOpCodes.Pop), CilInstruction.Create(CilOpCodes.Ret), } }; image.Assembly.Modules[0].TopLevelTypes[0].Methods.Add(method); return(typeSpec); }
public void EntrypointNotAdded() { // Create new assembly. var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, false); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var writeLine = importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) })); // Create but don't add main method. var main = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static, new MethodSignature(image.TypeSystem.Void)); main.CilMethodBody = new CilMethodBody(main); main.CilMethodBody.Instructions.AddRange(new[] { CilInstruction.Create(CilOpCodes.Ldstr, "Hello world!"), CilInstruction.Create(CilOpCodes.Call, writeLine), CilInstruction.Create(CilOpCodes.Ret), }); image.ManagedEntrypoint = main; Assert.Throws <MemberNotImportedException>(() => image.Header.UnlockMetadata()); }
private static ModuleDefinition CreateDummyModule() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var module = assembly.NetDirectory.MetadataHeader.LockMetadata().Assembly.Modules[0]; return(module); }
public void PersistentEntrypoint() { // Create new assembly. var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, false); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var writeLine = importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) })); // Create and add main method. var main = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static, new MethodSignature(image.TypeSystem.Void)); main.CilMethodBody = new CilMethodBody(main); main.CilMethodBody.Instructions.AddRange(new[] { CilInstruction.Create(CilOpCodes.Ldstr, "Hello world!"), CilInstruction.Create(CilOpCodes.Call, writeLine), CilInstruction.Create(CilOpCodes.Ret), }); image.Assembly.Modules[0].TopLevelTypes[0].Methods.Add(main); image.ManagedEntrypoint = main; // Commit. var mapping = image.Header.UnlockMetadata(); // Test. Assert.Equal(mapping[main].ToUInt32(), assembly.NetDirectory.EntryPointToken); var newImage = assembly.NetDirectory.MetadataHeader.LockMetadata(); Assert.Equal(main, newImage.ManagedEntrypoint, _comparer); }
public void PersistentMethodImplementations() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public, importer.ImportType(typeof(object))); var @interface = importer.ImportType(typeof(IList)); type.Interfaces.Add(new InterfaceImplementation(@interface)); var addMethodRef = (IMethodDefOrRef)importer.ImportMethod(typeof(IList).GetMethod("Add", new[] { typeof(object) })); var addMethodDef = new MethodDefinition("Add", MethodAttributes.Public, (MethodSignature)addMethodRef.Signature); type.Methods.Add(addMethodDef); type.MethodImplementations.Add(new MethodImplementation(addMethodDef, addMethodRef)); image.Assembly.Modules[0].TopLevelTypes.Add(type); var mapping = header.UnlockMetadata(); image = header.LockMetadata(); type = (TypeDefinition)image.ResolveMember(mapping[type]); Assert.Single(type.MethodImplementations); Assert.Same(type, type.MethodImplementations[0].Class); Assert.Equal(addMethodDef, type.MethodImplementations[0].MethodBody, _comparer); Assert.Equal(addMethodRef, type.MethodImplementations[0].MethodDeclaration, _comparer); }
public void PersistentVersion() { var newVersion = new Version(1, 2, 3, 4); var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var reference = CreateAndAddDummyReference(image); reference.Version = newVersion; var mapping = header.UnlockMetadata(); var assemblyRow = header.GetStream <TableStream>().GetTable <AssemblyReferenceTable>()[(int)(mapping[reference].Rid - 1)]; Assert.Equal(newVersion.Major, assemblyRow.Column1); Assert.Equal(newVersion.Minor, assemblyRow.Column2); Assert.Equal(newVersion.Build, assemblyRow.Column3); Assert.Equal(newVersion.Revision, assemblyRow.Column4); image = header.LockMetadata(); var newReference = (AssemblyReference)image.ResolveMember(mapping[reference]); Assert.Equal(newVersion, newReference.Version); }
public void ImportGenericInstanceType() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var signature = new GenericInstanceTypeSignature(CreateTypeReference(typeof(List <>))); var genericArg = CreateTypeDefOrRef(typeof(Form)); signature.GenericArguments.Add(genericArg); var newSignature = importer.ImportTypeSignature(signature); Assert.NotSame(signature, newSignature); Assert.Equal(signature, newSignature, _comparer); var newGenericSiganture = (GenericInstanceTypeSignature)newSignature; Assert.Equal(image, newGenericSiganture.GenericType.Image); var genericArgElementType = newGenericSiganture.GenericArguments[0].GetElementType(); Assert.IsAssignableFrom <ITypeDefOrRef>(genericArgElementType); Assert.Equal(image, ((ITypeDefOrRef)genericArgElementType).Image); }
public void PersistentNestedClasses() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var header = assembly.NetDirectory.MetadataHeader; var image = header.LockMetadata(); var importer = new ReferenceImporter(image); var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public, importer.ImportType(typeof(object))); var nestedType = new TypeDefinition(null, "NestedType", TypeAttributes.NestedPublic, importer.ImportType(typeof(object))); type.NestedClasses.Add(new NestedClass(nestedType)); image.Assembly.Modules[0].TopLevelTypes.Add(type); var mapping = header.UnlockMetadata(); image = header.LockMetadata(); type = (TypeDefinition)image.ResolveMember(mapping[type]); Assert.Single(type.NestedClasses); Assert.Same(type, type.NestedClasses[0].EnclosingClass); Assert.Equal(nestedType, type.NestedClasses[0].Class, _comparer); Assert.DoesNotContain(type.NestedClasses[0].Class, image.Assembly.Modules[0].TopLevelTypes); Assert.Contains(type.NestedClasses[0].Class, image.Assembly.Modules[0].GetAllTypes()); }
public void MetadataHeaders() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); Assert.NotNull(assembly.NetDirectory); Assert.NotNull(assembly.NetDirectory.MetadataHeader); Assert.Null(assembly.NetDirectory.MetadataHeader.Image); }
public void FileHeader() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); Assert.True(assembly.NtHeaders.FileHeader.Characteristics.HasFlag(ImageCharacteristics.Dll)); assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false); Assert.False(assembly.NtHeaders.FileHeader.Characteristics.HasFlag(ImageCharacteristics.Dll)); }
public void MetadataImage() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); Assert.NotNull(image.Assembly); Assert.Equal("SomeAssembly", image.Assembly.Name); }
public void ImportDirectory() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); Assert.Contains("mscoree.dll", assembly.ImportDirectory.ModuleImports.Select(x => x.Name)); Assert.Contains("_CorDllMain", assembly.ImportDirectory.ModuleImports.First(x => x.Name == "mscoree.dll").SymbolImports.Select(x => x.HintName.Name)); assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false); Assert.Contains("mscoree.dll", assembly.ImportDirectory.ModuleImports.Select(x => x.Name)); Assert.Contains("_CorExeMain", assembly.ImportDirectory.ModuleImports.First(x => x.Name == "mscoree.dll").SymbolImports.Select(x => x.HintName.Name)); }
public void ImportCorlibType() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var signature = image.TypeSystem.Byte; var newSignature = importer.ImportTypeSignature(signature); Assert.Same(signature, newSignature); }
public void ImportTypeDefOrRefSignature() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var signature = CreateTypeDefOrRef(typeof(Form)); var newSignature = importer.ImportTypeSignature(signature); Assert.NotSame(signature, newSignature); Assert.Equal(signature, newSignature, _comparer); }
public void ImportNewFieldReference() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var reference = new MemberReference( CreateTypeReference(typeof(string)), "Empty", new FieldSignature(image.TypeSystem.String)); VerifyImportedReference(image, reference, importer.ImportMember(reference)); }
public void ImportNewMethodReference() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var reference = new MemberReference( CreateTypeReference(typeof(Console)), "WriteLine", new MethodSignature(new[] { image.TypeSystem.String }, image.TypeSystem.Void)); VerifyImportedReference(image, reference, importer.ImportMember(reference)); }
public void ImportModuleReference() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var module = new ModuleReference("SomeModule"); var newModule = importer.ImportModule(module); Assert.NotSame(module, newModule); Assert.Equal(image, newModule.Image); Assert.Equal(newModule.Name, newModule.Name); }
public void ImportTypeSpecification() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var typeSpec = new TypeSpecification(CreateTypeDefOrRef(typeof(Form))); var newSpec = importer.ImportType(typeSpec); Assert.NotSame(typeSpec, newSpec); Assert.Equal(typeSpec, newSpec, _comparer); }
public void LockMetadata() { var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", true); var header = assembly.NetDirectory.MetadataHeader; Assert.False(header.IsLocked); Assert.Null(header.Image); var tableStream = header.GetStream <TableStream>(); Assert.False(tableStream.IsReadOnly); Assert.All(tableStream.GetPresentTables(), x => Assert.False(x.IsReadOnly)); tableStream.GetTable <TypeReferenceTable>().Add(new MetadataRow <uint, uint, uint> { Column1 = 1, Column2 = 2, Column3 = 3, }); var image = header.LockMetadata(); Assert.True(header.IsLocked); Assert.Equal(header.Image, image); Assert.True(tableStream.IsReadOnly); Assert.All(tableStream.GetPresentTables(), x => Assert.True(x.IsReadOnly)); Assert.Throws <MetadataLockedException>(() => { tableStream.GetTable <TypeReferenceTable>() .Add(new MetadataRow <uint, uint, uint> { Column1 = 1, Column2 = 2, Column3 = 3, }); }); Assert.Throws <MetadataLockedException>(() => { tableStream.GetTable <TypeReferenceTable>()[0].Column2 = 4; }); header.UnlockMetadata(); Assert.False(header.IsLocked); Assert.Null(header.Image); tableStream = header.GetStream <TableStream>(); Assert.False(tableStream.IsReadOnly); Assert.All(tableStream.GetPresentTables(), x => Assert.False(x.IsReadOnly)); }
public void ImportInternalTypeDefinition() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var typeDef = new TypeDefinition("SomeNamespace", "SomeName"); image.Assembly.Modules[0].TopLevelTypes.Add(typeDef); var newReference = importer.ImportType(typeDef); Assert.Same(typeDef, newReference); }
public void ImportReflectionAssembly() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var assemblyName = typeof(Form).Assembly.GetName(); var wrapper = new ReflectionAssemblyNameWrapper(assemblyName); var newReference = importer.ImportAssembly(assemblyName); Assert.Contains(newReference, image.Assembly.AssemblyReferences); Assert.True(_comparer.Equals(wrapper, newReference)); }
public void ImportReflectionField() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var reference = new MemberReference( CreateTypeReference(typeof(string)), "Empty", new FieldSignature(image.TypeSystem.String)); var newReference = importer.ImportField(typeof(string).GetField("Empty", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)); VerifyImportedReference(image, reference, newReference); }