|
|
|
@ -18,201 +18,194 @@
@@ -18,201 +18,194 @@
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
|
|
|
|
|
public class UnsafeCode |
|
|
|
|
namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness |
|
|
|
|
{ |
|
|
|
|
struct SimpleStruct |
|
|
|
|
public class UnsafeCode |
|
|
|
|
{ |
|
|
|
|
public int X; |
|
|
|
|
public double Y; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void Main() |
|
|
|
|
{ |
|
|
|
|
// TODO: test behavior, or convert this into a pretty-test
|
|
|
|
|
// (but for now, it's already valuable knowing whether the decompiled code can be re-compiled)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int* NullPointer |
|
|
|
|
{ |
|
|
|
|
get |
|
|
|
|
struct SimpleStruct |
|
|
|
|
{ |
|
|
|
|
return null; |
|
|
|
|
public int X; |
|
|
|
|
public double Y; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int* PointerCast(long* p) |
|
|
|
|
{ |
|
|
|
|
return (int*)p; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long ConvertDoubleToLong(double d) |
|
|
|
|
{ |
|
|
|
|
return *(long*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe double ConvertLongToDouble(long d) |
|
|
|
|
{ |
|
|
|
|
return *(double*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int ConvertFloatToInt(float d) |
|
|
|
|
{ |
|
|
|
|
return *(int*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe float ConvertIntToFloat(int d) |
|
|
|
|
{ |
|
|
|
|
return *(float*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void PassRefParameterAsPointer(ref int p) |
|
|
|
|
{ |
|
|
|
|
fixed (int* ptr = &p) |
|
|
|
|
|
|
|
|
|
static void Main() |
|
|
|
|
{ |
|
|
|
|
this.PassPointerAsRefParameter(ptr); |
|
|
|
|
// TODO: test behavior, or convert this into a pretty-test
|
|
|
|
|
// (but for now, it's already valuable knowing whether the decompiled code can be re-compiled)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void PassPointerAsRefParameter(int* p) |
|
|
|
|
{ |
|
|
|
|
this.PassRefParameterAsPointer(ref *p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void AddressInMultiDimensionalArray(double[,] matrix) |
|
|
|
|
{ |
|
|
|
|
fixed (double* ptr = &matrix[1, 2]) |
|
|
|
|
|
|
|
|
|
public unsafe int* NullPointer |
|
|
|
|
{ |
|
|
|
|
this.PointerReferenceExpression(ptr); |
|
|
|
|
this.PointerReferenceExpression(ptr); |
|
|
|
|
get { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int MultipleExitsOutOfFixedBlock(int[] arr) |
|
|
|
|
{ |
|
|
|
|
fixed (int* ptr = &arr[0]) |
|
|
|
|
{ |
|
|
|
|
if (*ptr < 0) |
|
|
|
|
return *ptr; |
|
|
|
|
if (*ptr == 21) |
|
|
|
|
return 42; |
|
|
|
|
if (*ptr == 42) |
|
|
|
|
goto outside; |
|
|
|
|
} |
|
|
|
|
return 1; |
|
|
|
|
outside: |
|
|
|
|
Console.WriteLine("outside"); |
|
|
|
|
return 2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void FixedStringAccess(string text) |
|
|
|
|
{ |
|
|
|
|
fixed (char* ptr = text) |
|
|
|
|
|
|
|
|
|
public unsafe int* PointerCast(long* p) |
|
|
|
|
{ |
|
|
|
|
char* ptr2 = ptr; |
|
|
|
|
while (*ptr2 != '\0') |
|
|
|
|
{ |
|
|
|
|
*ptr2 = 'A'; |
|
|
|
|
ptr2++; |
|
|
|
|
return (int*)p; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long ConvertDoubleToLong(double d) |
|
|
|
|
{ |
|
|
|
|
return *(long*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe double ConvertLongToDouble(long d) |
|
|
|
|
{ |
|
|
|
|
return *(double*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int ConvertFloatToInt(float d) |
|
|
|
|
{ |
|
|
|
|
return *(int*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe float ConvertIntToFloat(int d) |
|
|
|
|
{ |
|
|
|
|
return *(float*)(&d); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void PassRefParameterAsPointer(ref int p) |
|
|
|
|
{ |
|
|
|
|
fixed (int* ptr = &p) { |
|
|
|
|
this.PassPointerAsRefParameter(ptr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val) |
|
|
|
|
{ |
|
|
|
|
fixed (long* ptr = array) |
|
|
|
|
|
|
|
|
|
public unsafe void PassPointerAsRefParameter(int* p) |
|
|
|
|
{ |
|
|
|
|
((double*)ptr)[index] = val; |
|
|
|
|
this.PassRefParameterAsPointer(ref *p); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val) |
|
|
|
|
{ |
|
|
|
|
fixed (long* ptr = &array[index]) |
|
|
|
|
|
|
|
|
|
public unsafe void AddressInMultiDimensionalArray(double[,] matrix) |
|
|
|
|
{ |
|
|
|
|
*(double*)ptr = val; |
|
|
|
|
fixed (double* ptr = &matrix[1, 2]) { |
|
|
|
|
this.PointerReferenceExpression(ptr); |
|
|
|
|
this.PointerReferenceExpression(ptr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe string PointerReferenceExpression(double* d) |
|
|
|
|
{ |
|
|
|
|
return d->ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe string PointerReferenceExpression2(long addr) |
|
|
|
|
{ |
|
|
|
|
return ((int*)addr)->ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe void FixMultipleStrings(string text) |
|
|
|
|
{ |
|
|
|
|
fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text) |
|
|
|
|
|
|
|
|
|
public unsafe int MultipleExitsOutOfFixedBlock(int[] arr) |
|
|
|
|
{ |
|
|
|
|
*ptr = 'c'; |
|
|
|
|
*userName = 'd'; |
|
|
|
|
*ptr2 = 'e'; |
|
|
|
|
fixed (int* ptr = &arr[0]) { |
|
|
|
|
if (*ptr < 0) |
|
|
|
|
return *ptr; |
|
|
|
|
if (*ptr == 21) |
|
|
|
|
return 42; |
|
|
|
|
if (*ptr == 42) |
|
|
|
|
goto outside; |
|
|
|
|
} |
|
|
|
|
return 1; |
|
|
|
|
outside: |
|
|
|
|
Console.WriteLine("outside"); |
|
|
|
|
return 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe string StackAlloc(int count) |
|
|
|
|
{ |
|
|
|
|
char* ptr = stackalloc char[count]; |
|
|
|
|
char* ptr2 = stackalloc char[100]; |
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
|
|
|
|
|
|
public unsafe void FixedStringAccess(string text) |
|
|
|
|
{ |
|
|
|
|
ptr[i] = (char)i; |
|
|
|
|
fixed (char* ptr = text) { |
|
|
|
|
char* ptr2 = ptr; |
|
|
|
|
while (*ptr2 != '\0') { |
|
|
|
|
*ptr2 = 'A'; |
|
|
|
|
ptr2++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return this.PointerReferenceExpression((double*)ptr); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe string StackAllocStruct(int count) |
|
|
|
|
{ |
|
|
|
|
SimpleStruct* s = stackalloc SimpleStruct[checked(count * 2)]; |
|
|
|
|
SimpleStruct* p = stackalloc SimpleStruct[10]; |
|
|
|
|
return this.PointerReferenceExpression(&s->Y); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int* PointerArithmetic(int* p) |
|
|
|
|
{ |
|
|
|
|
return p + 2; |
|
|
|
|
} |
|
|
|
|
public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val) |
|
|
|
|
{ |
|
|
|
|
fixed (long* ptr = array) { |
|
|
|
|
((double*)ptr)[index] = val; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe byte* PointerArithmetic2(long* p, int y, int x) |
|
|
|
|
{ |
|
|
|
|
return (byte*)((short*)p + (y * x)); |
|
|
|
|
} |
|
|
|
|
public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val) |
|
|
|
|
{ |
|
|
|
|
fixed (long* ptr = &array[index]) { |
|
|
|
|
*(double*)ptr = val; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long* PointerArithmetic3(long* p) |
|
|
|
|
{ |
|
|
|
|
return (long*)((byte*)p + 3); |
|
|
|
|
} |
|
|
|
|
public unsafe string PointerReferenceExpression(double* d) |
|
|
|
|
{ |
|
|
|
|
return d->ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long* PointerArithmetic4(void* p) |
|
|
|
|
{ |
|
|
|
|
return (long*)((byte*)p + 3); |
|
|
|
|
} |
|
|
|
|
public unsafe string PointerReferenceExpression2(long addr) |
|
|
|
|
{ |
|
|
|
|
return ((int*)addr)->ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerArithmetic5(void* p, byte* q, int i) |
|
|
|
|
{ |
|
|
|
|
return (int)(q[i] + *(byte*)p); |
|
|
|
|
} |
|
|
|
|
public unsafe void FixMultipleStrings(string text) |
|
|
|
|
{ |
|
|
|
|
fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text) { |
|
|
|
|
*ptr = 'c'; |
|
|
|
|
*userName = 'd'; |
|
|
|
|
*ptr2 = 'e'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction(long* p, long* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)(p - q)); |
|
|
|
|
} |
|
|
|
|
public unsafe string StackAlloc(int count) |
|
|
|
|
{ |
|
|
|
|
char* ptr = stackalloc char[count]; |
|
|
|
|
char* ptr2 = stackalloc char[100]; |
|
|
|
|
for (int i = 0; i < count; i++) { |
|
|
|
|
ptr[i] = (char)i; |
|
|
|
|
} |
|
|
|
|
return this.PointerReferenceExpression((double*)ptr); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction2(long* p, short* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)((byte*)p - (byte*)q)); |
|
|
|
|
} |
|
|
|
|
public unsafe string StackAllocStruct(int count) |
|
|
|
|
{ |
|
|
|
|
SimpleStruct* s = stackalloc SimpleStruct[checked(count * 2)]; |
|
|
|
|
SimpleStruct* p = stackalloc SimpleStruct[10]; |
|
|
|
|
return this.PointerReferenceExpression(&s->Y); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction3(void* p, void* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)((byte*)p - (byte*)q)); |
|
|
|
|
} |
|
|
|
|
public unsafe int* PointerArithmetic(int* p) |
|
|
|
|
{ |
|
|
|
|
return p + 2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unsafe ~UnsafeCode() |
|
|
|
|
{ |
|
|
|
|
this.PassPointerAsRefParameter(this.NullPointer); |
|
|
|
|
public unsafe byte* PointerArithmetic2(long* p, int y, int x) |
|
|
|
|
{ |
|
|
|
|
return (byte*)((short*)p + (y * x)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long* PointerArithmetic3(long* p) |
|
|
|
|
{ |
|
|
|
|
return (long*)((byte*)p + 3); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe long* PointerArithmetic4(void* p) |
|
|
|
|
{ |
|
|
|
|
return (long*)((byte*)p + 3); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerArithmetic5(void* p, byte* q, int i) |
|
|
|
|
{ |
|
|
|
|
return (int)(q[i] + *(byte*)p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction(long* p, long* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)(p - q)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction2(long* p, short* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)((byte*)p - (byte*)q)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public unsafe int PointerSubtraction3(void* p, void* q) |
|
|
|
|
{ |
|
|
|
|
return (int)((long)((byte*)p - (byte*)q)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unsafe ~UnsafeCode() |
|
|
|
|
{ |
|
|
|
|
this.PassPointerAsRefParameter(this.NullPointer); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |