ltrzesniewski / InlineIL.Fody

Inject arbitrary IL code at compile time.
MIT License
240 stars 17 forks source link

Some IL instructions "always" generate unverifiable code #9

Closed michel-pi closed 5 years ago

michel-pi commented 5 years ago

Hey.

It seem like IL code generated for initblk and copyblk always generate unverfiable code.

This is also the case for the https://github.com/ltrzesniewski/InlineIL.Fody/blob/master/src/InlineIL.Examples/Unsafe.cs class provided as an example. (opcodes are the same as in the original. .maxstack is 8 for InlineIL and 3 for Unsafe.)

The original Unsafe library does not produce non verifiable code for these instructions.

Is this a problem with InlineIL which can be fixed?

ltrzesniewski commented 5 years ago

Hi,

It seem like IL code generated for initblk and copyblk always generate unverfiable code.

Yes, those two instructions are always unverifiable, per the CLI spec. See ECMA-335 at §III.3.36 and III.3.30.

.maxstack is 8 for InlineIL and 3 for Unsafe

This is not an issue. Cecil tries to emit so-called "tiny method headers" whenever possible. Those assume a maxstack of 8. See ECMA-335 §II.25.4 for more info.

The original Unsafe library does not produce non verifiable code for these instructions.

You must have checked it wrong. Here's what PEVerify says for the current version:

D:\Temp>peverify System.Runtime.CompilerServices.Unsafe.dll

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

[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Read[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::ReadUnaligned[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::ReadUnaligned[T]][offset 0x00000004][found Byte][expected (unboxed) 'T'] Unexpected type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Write[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::WriteUnaligned[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::WriteUnaligned[T]][offset 0x00000005][found (unboxed) 'T'][expected Byte] Unexpected type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Copy[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Copy[T]][offset 0x00000001] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::AsPointer[T]][offset 0x00000001][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::As[T]][offset 0x00000001][found ref 'System.Object'][expected (unboxed) 'T'] Unexpected type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::AsRef[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::AsRef[T]][offset 0x00000001] Return type is ByRef, TypedReference, ArgHandle, or ArgIterator.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::As[TFrom,TTo]][offset 0x00000001][found address of ref ][expected address of ref ] Unexpected type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::As[TFrom,TTo]][offset 0x00000001] Return type is ByRef, TypedReference, ArgHandle, or ArgIterator.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Unbox[T]][offset 0x00000001][found (unboxed) 'T'] Value type expected.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Add[T]][offset 0x0000000A][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Add[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Add[T]][offset 0x00000009][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::AddByteOffset[T]][offset 0x00000002][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Subtract[T]][offset 0x0000000A][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Subtract[T]][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::Subtract[T]][offset 0x00000009][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::SubtractByteOffset[T]][offset 0x00000002][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::ByteOffset[T]][offset 0x00000002][found address of ref ] Expected numeric type on the stack.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::CopyBlock][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::CopyBlock][offset 0x00000003] Instruction cannot be verified.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::CopyBlockUnaligned][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::CopyBlockUnaligned][offset 0x00000006] Instruction cannot be verified.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::InitBlock][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::InitBlock][offset 0x00000003] Instruction cannot be verified.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::InitBlockUnaligned][offset 0x00000000] Unmanaged pointers are not a verifiable type.
[IL]: Error: [D:\Temp\System.Runtime.CompilerServices.Unsafe.dll : System.Runtime.CompilerServices.Unsafe::InitBlockUnaligned][offset 0x00000006] Instruction cannot be verified.
32 Error(s) Verifying System.Runtime.CompilerServices.Unsafe.dll

I guess you checked the library from the ref directory in the NuGet package. Reference libs contain no implementation.

Is this a problem with InlineIL which can be fixed?

There's nothing to fix here 😉

michel-pi commented 5 years ago

I somehow managed to get same number of errors (32) for the unsafe assemly but i did not get the same error messages...

Sorry for my mistake 😄

Thank you for your great help