Error checking unsafe return type C # using Peverify and ILVerify

I ran into this problem when checking for code containing an unsafe method that returns a pointer.

An example can be expressed as follows:

public class A
{
    public static unsafe int* GetAnswer()
    {
        int fakeValue = 42;
        return &(fakeValue);
    }

    public static void Main()
    {
        int i = 0;
        unsafe { i = *A.GetAnswer(); }
        System.Console.WriteLine(i);
    }
}

I use two separate validation tools: ILVerify and Peverify.

Steps to play:

  • compile sample code with csc example.cs /t:library /unsafe
  • check peverify example.dll
  • check ILVerify.exe -r C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll example.dll

Both 2. and 3. will result in the error message below:

[IL]: Error: [C: \ src \ test \ example.dll: A :: GetAnswer ()] [offset 0x00000006] [Int32 address found] Expected numeric type on the stack.

[IL]: Error: [C: \ src \ test \ example.dll: A :: Main ()] [offset 0x00000009] [found Native Int] Expected ByRef value on the stack. 2 Error Checking C: \ src \ test \ example.dll

, , , . - - , ?

+6
1

: . , , , : (badum tsh)!

: - , , -. , : .

, ref return; :

static ref int GetAnswer(int[] arr)
{
    return ref arr[0];
}

static void Main()
{
    int i = 0;
    int[] j = new int[] { 42 };
    i = A.GetAnswer(j);
    System.Console.WriteLine(i);
}

. GetAnswer ( ) - (ref T - ; T* - ). i = {someRef} ( i = ref {someRef}) , i = *{somePtr} .

:

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

All Classes and Methods in ConsoleApp35.exe Verified.
+7

All Articles