BUG: PE File Is Unverifiable When You Use PEVerify Tool (315479)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)

This article was previously published under Q315479

SYMPTOMS

When you use the PEVerify tool (Peverify.exe) to determine whether a portable executable (PE) file and associated metadata meet type safety requirements, the resulting PE file is unverifiable in some scenarios (such as those shown in the sample code in the "More Information" section of this article), even though it does not use any unsafe code.

CAUSE

The private member X of the class A is accessible only from member functions and friends of the class A. The issue described in the "Symptoms" section of this article occurs when you attempt to "spoof" the C# compiler to circumvent this limitation by tampering with the definition of the class A, which makes the member X public. Under these circumstances, the C# compiler ignores the resolution scope and references the inappropriate DLL file. The code is compiled successfully. The problem occurs only when the code is verified by the PEVerify tool.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Problem

The following four code samples demonstrate how this behavior occurs when you use the PEverify tool:
A.cs
// Compiler option needed: csc /target:library A.cs
public class A {

     private int x;
     public A () { this.x = 0; }

}
				
ASpoof.cs
// Compiler option needed: csc /target:library ASpoof.cs
public class A {

     public int x;
     public A () { this.x = 1; }
				
B.cs
// Compiler option needed: csc /r:A.dll /target:library B.cs
using System.Reflection;
public class B {

     public static A mkA () {

           Assembly x = Assembly.LoadFrom ("A.dll");
           A a = (A)x.CreateInstance ("A");
           return a;

  }

}
				
C.cs
// Compiler option needed: csc /r:ASpoof.dll /r:B.dll C.cs
public class Demo {

     public static void Main () {

         A a = B.mkA ();
         System.Console.WriteLine (a.GetType());
         System.Console.WriteLine (a.x);

  }

}
				
Run PEVerify on C.exe. PEVerify returns the following error message:
[IL]: Error: [h:\test\948\c.exe : Demo::Main] [offset 0x00000005]
[opcode stloc.0] [found objref 'A'] [expected objref 'A'] Unexpected type on the stack.
1 Errors Verifying C.exe
However, C.exe runs correctly and returns the expected results.

C.exe output:

A
0


Modification Type:MajorLast Reviewed:4/11/2003
Keywords:kbbug kbnofix KB315479