Fixed incorrect decompilation in UIX

This commit is contained in:
Joshua Askharoun
2021-04-02 18:49:40 -05:00
parent 799fca8d97
commit 2b20ca46ad
6 changed files with 195 additions and 22 deletions
+181 -9
View File
@@ -12,7 +12,7 @@ using System.Runtime.InteropServices;
namespace Microsoft.Iris.Markup
{
internal class ByteCodeReader : DisposableObject
internal class ManagedByteCodeReader : DisposableObject
{
private static char[] s_scratchCharArray = new char[100];
private BinaryReader _reader;
@@ -20,13 +20,13 @@ namespace Microsoft.Iris.Markup
private byte[] _bytes;
private bool _inFixedMemory;
public unsafe ByteCodeReader(IntPtr buffer, long size, bool ownsAllocation)
public unsafe ManagedByteCodeReader(IntPtr buffer, long size, bool ownsAllocation)
{
byte[] bytes = new byte[size];
Marshal.Copy(buffer, bytes, 0, (int)size);
_bytes = bytes;
this._reader = new BinaryReader(new MemoryStream(_bytes));
_reader = new BinaryReader(new MemoryStream(_bytes));
_buffer = buffer;
}
@@ -42,9 +42,9 @@ namespace Microsoft.Iris.Markup
public unsafe IntPtr CurrentAddress => new IntPtr(_buffer.ToInt64() + _reader.BaseStream.Position);
public bool IsInFixedMemory => this._inFixedMemory;
public bool IsInFixedMemory => _inFixedMemory;
public void MarkAsInFixedMemory() => this._inFixedMemory = true;
public void MarkAsInFixedMemory() => _inFixedMemory = true;
public bool ReadBool() => _reader.ReadBoolean();
@@ -80,7 +80,7 @@ namespace Microsoft.Iris.Markup
public unsafe string ReadString()
{
uint num1 = this.ReadUInt16();
uint num1 = ReadUInt16();
if (num1 == ushort.MaxValue)
return null;
bool flag;
@@ -96,10 +96,10 @@ namespace Microsoft.Iris.Markup
flag = false;
num2 = num1 * 2U;
}
if (this.CurrentOffset + num2 > this.Size)
this.ThrowReadError();
if (CurrentOffset + num2 > Size)
ThrowReadError();
char[] chArray = num1 >= s_scratchCharArray.Length ? new char[num1] : s_scratchCharArray;
byte* numPtr1 = (byte*)(_buffer.ToInt32() + (int)CurrentOffset);
byte* numPtr1 = (byte*)(_buffer.ToInt64() + CurrentOffset);
if (flag)
{
for (int index = 0; index < num1; ++index)
@@ -139,4 +139,176 @@ namespace Microsoft.Iris.Markup
_buffer = IntPtr.Zero;
}
}
internal class ByteCodeReader : DisposableObject
{
private static char[] s_scratchCharArray = new char[100];
private unsafe byte* _buffer;
private uint _offset;
private uint _size;
private bool _ownsAllocation;
private bool _inFixedMemory;
public unsafe ByteCodeReader(IntPtr buffer, uint size, bool ownsAllocation)
{
_buffer = (byte*)buffer.ToPointer();
_size = size;
_offset = 0U;
_ownsAllocation = ownsAllocation;
int num = _ownsAllocation ? 1 : 0;
}
public uint Size => _size;
public uint CurrentOffset
{
get => _offset;
set => _offset = value <= _size ? value : throw new Exception("Invalid offset");
}
public unsafe IntPtr CurrentAddress => new IntPtr(_buffer + (int)_offset);
public void MarkAsInFixedMemory() => _inFixedMemory = true;
public bool IsInFixedMemory => _inFixedMemory;
public bool ReadBool() => ReadByte() != 0;
public unsafe byte ReadByte()
{
if (_offset >= _size)
ThrowReadError();
byte num = _buffer[(int)_offset];
++_offset;
return num;
}
public char ReadChar() => (char)ReadUInt16();
public unsafe ushort ReadUInt16()
{
if (_offset + 2U > _size)
ThrowReadError();
byte* numPtr = _buffer + (int)_offset;
byte num1 = *numPtr;
byte num2 = numPtr[1];
_offset += 2U;
return (ushort)(num1 | (uint)num2 << 8);
}
public int ReadInt32() => (int)ReadUInt32();
public unsafe uint ReadUInt32()
{
if (_offset + 4U > _size)
ThrowReadError();
byte* numPtr = _buffer + (int)_offset;
byte num1 = *numPtr;
byte num2 = numPtr[1];
byte num3 = numPtr[2];
byte num4 = numPtr[3];
_offset += 4U;
return (uint)(num1 | num2 << 8 | num3 << 16 | num4 << 24);
}
public static unsafe ushort ReadUInt16(IntPtr pData)
{
byte* pointer = (byte*)pData.ToPointer();
return (ushort)(*pointer | (uint)pointer[1] << 8);
}
public static unsafe uint ReadUInt32(IntPtr pData)
{
byte* pointer = (byte*)pData.ToPointer();
return (uint)(*pointer | pointer[1] << 8 | pointer[2] << 16 | pointer[3] << 24);
}
public long ReadInt64() => (long)ReadUInt64();
public unsafe ulong ReadUInt64()
{
if (_offset + 8U > _size)
ThrowReadError();
byte* numPtr = _buffer + (int)_offset;
byte num1 = *numPtr;
byte num2 = numPtr[1];
byte num3 = numPtr[2];
byte num4 = numPtr[3];
byte num5 = numPtr[4];
byte num6 = numPtr[5];
byte num7 = numPtr[6];
byte num8 = numPtr[7];
_offset += 8U;
uint num9 = (uint)(num1 | num2 << 8 | num3 << 16 | num4 << 24);
return (ulong)(uint)(num5 | num6 << 8 | num7 << 16 | num8 << 24) << 32 | num9;
}
//public unsafe float ReadSingle() => *(float*)&ReadUInt32();
public unsafe float ReadSingle() => BitConverter.ToSingle(BitConverter.GetBytes(ReadUInt32()), 0);
//public unsafe double ReadDouble() => *(double*)&ReadInt64();
public unsafe double ReadDouble() => BitConverter.ToSingle(BitConverter.GetBytes(ReadUInt64()), 0);
public unsafe string ReadString()
{
uint num1 = ReadUInt16();
if (num1 == ushort.MaxValue)
return null;
bool flag;
uint num2;
if (((int)num1 & 32768) != 0)
{
flag = true;
num1 &= (uint)short.MaxValue;
num2 = num1;
}
else
{
flag = false;
num2 = num1 * 2U;
}
if (_offset + num2 > _size)
ThrowReadError();
char[] chArray = num1 >= s_scratchCharArray.Length ? new char[num1] : ByteCodeReader.s_scratchCharArray;
byte* numPtr1 = _buffer + (int)_offset;
if (flag)
{
for (int index = 0; index < num1; ++index)
chArray[index] = (char)*numPtr1++;
}
else
{
for (int index = 0; index < num1; ++index)
{
byte* numPtr2 = numPtr1;
byte* numPtr3 = numPtr2 + 1;
byte num3 = *numPtr2;
byte* numPtr4 = numPtr3;
numPtr1 = numPtr4 + 1;
byte num4 = *numPtr4;
chArray[index] = (char)(num3 | (uint)num4 << 8);
}
}
_offset += num2;
return new string(chArray, 0, (int)num1);
}
public unsafe IntPtr ToIntPtr(out uint size)
{
size = _size;
return new IntPtr(_buffer);
}
public unsafe IntPtr GetAddress(uint offset) => new IntPtr(_buffer + (int)offset);
private void ThrowReadError() => throw new Exception("Attempted to read past the end of the buffer.");
protected override unsafe void OnDispose()
{
base.OnDispose();
if (_ownsAllocation)
NativeApi.MemFree(new IntPtr(_buffer));
_buffer = null;
}
}
}
+1 -1
View File
@@ -153,7 +153,7 @@ namespace Microsoft.Iris.Markup
public unsafe void Write(ByteCodeReader value, uint offset)
{
IntPtr intPtr = value.ToIntPtr(out long size);
IntPtr intPtr = value.ToIntPtr(out uint size);
if (offset > size)
throw new ArgumentOutOfRangeException(nameof(offset));
this.Write(new IntPtr(intPtr.ToInt64() + offset), (uint)(size - offset));
@@ -542,7 +542,8 @@ namespace Microsoft.Iris.Markup
if (this._reader.IsInFixedMemory)
{
typeExport.SetAddressOfInheritableSymbolTable(this._reader.CurrentAddress);
this._reader.CurrentOffset += this._reader.ReadUInt32();
uint num = this._reader.ReadUInt32();
this._reader.CurrentOffset += num;
}
else
DecodeInheritableSymbolTable(typeExport, this._reader, IntPtr.Zero);
+3 -3
View File
@@ -91,10 +91,10 @@ namespace Microsoft.Iris.Markup
ErrorManager.ReportError("Unable to open output file '{0}'. Error code {1}", outputFile, Marshal.GetLastWin32Error());
if (!watermark.ErrorsDetected)
{
long size = 0;
uint size = 0;
IntPtr intPtr = reader.ToIntPtr(out size);
uint lpNumberOfBytesWritten = 0;
if (!Win32Api.WriteFile(file, intPtr, (uint)size, out lpNumberOfBytesWritten, IntPtr.Zero))
if (!Win32Api.WriteFile(file, intPtr, size, out lpNumberOfBytesWritten, IntPtr.Zero))
ErrorManager.ReportError("An error occurred while saving data to output file '{0}'. Error code {1}", outputFile, Marshal.GetLastWin32Error());
}
if (file != Win32Api.INVALID_HANDLE_VALUE)
@@ -487,7 +487,7 @@ namespace Microsoft.Iris.Markup
{
this._writer.Overwrite(this._objectSectionStartFixup, this._writer.DataSize);
this._writer.Write(this._loadResult.ObjectSection);
this._loadResult.ObjectSection.ToIntPtr(out long _);
this._loadResult.ObjectSection.ToIntPtr(out uint _);
this._writer.Overwrite(this._objectSectionEndFixup, this._writer.DataSize);
}
@@ -31,16 +31,16 @@ namespace Microsoft.Iris.Markup
{
DynamicMethod dynamicMethod = new DynamicMethod("MethodInvoker", typeof(object), new Type[2]
{
typeof (object),
typeof (object[])
typeof (object),
typeof (object[])
}, s_irisModule, true);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
ParameterInfo[] parameters = methodBase.GetParameters();
Type[] typeArray = new Type[parameters.Length];
for (int index = 0; index < parameters.Length; ++index)
typeArray[index] = parameters[index].ParameterType;
MethodInfo methodInfo = null;
if (methodBase is MethodInfo && !methodInfo.IsStatic)
MethodInfo methodInfo = methodBase as MethodInfo;
if (methodInfo != null && !methodInfo.IsStatic)
{
methodInfo = (MethodInfo)methodBase;
ilGenerator.Emit(OpCodes.Ldarg_0);