ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Support System.Runtime.CompilerServices.Unsafe intrinsics

Open dgrunwald opened this issue 6 years ago • 3 comments

C++/CLI code may use managed references in ways more flexible than C#. In such cases, the decompiler could use System.Runtime.CompilerServices.Unsafe method calls to represent the missing operations.

The Unsafe class itself can serve as test case.

dgrunwald avatar Jul 28 '18 17:07 dgrunwald

How many of these are still unsupported (including your PR #2045)?

siegfriedpammer avatar Jun 25 '20 12:06 siegfriedpammer

All the Unsafe members in the test case that aren't decompiled into Unsafe calls are currently emitting code that fails to compile. So this issue is still less than 50% done.

dgrunwald avatar Jun 25 '20 17:06 dgrunwald

Status:

  • ReadUnaligned, WriteUnaligned: implemented as of 782e4ae88c66d7dccb210cc530900f62a1c3aeae
  • AsPointer: implemented: ExpressionBuilder.VisitConv will emit this for ConversionKind.StopGCTracking.
  • Read, Write, Copy, SizeOf: these are only invalid because the generated code using pointer arithmetic is not valid with unconstrained generic types. See also #2144
  • Add/Subtract/ByteOffset: Managed reference overloads are implemented. Pointer overloads are invalid because the generated code using pointer arithmetic is not valid with unconstrained generic types.
  • T As<T>(object): This is currently decompiled into a cast. Fixing this pretty much would involve replacing the fallback case in the ConvertTo method. But I think currently ConvertTo is used in so many places that this would end up with unexpected side effects.
  • AreSame/IsAddressGreaterThan/IsAddressLessThan: TODO, these currently produce invalid syntax
  • Unbox: TODO, currently emits invalid syntax ref (T)obj
  • NullRef: emits ref *(T*)null which is invalid when T is unconstrained generic
  • IsNullRef: emits Unsafe.AsPointer(ref source) == null which I guess is OK
  • SkipInit: TODO, currently ILSpy can emit code with a compiler errors due to uninitialized variable

dgrunwald avatar Nov 12 '20 21:11 dgrunwald