forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafeCode.cs
More file actions
664 lines (564 loc) · 12.8 KB
/
UnsafeCode.cs
File metadata and controls
664 lines (564 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
#if !NET40 && ROSLYN
using System.Runtime.CompilerServices;
#endif
using System.Runtime.InteropServices;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class SizeofTest
{
private struct StructWithStaticField
{
public static object StaticObj;
#if CS110 && NET70
public nint A;
#else
public IntPtr A;
#endif
}
private struct UnmanagedStruct
{
public StructWithStaticField Value;
}
private struct ManagedStruct
{
public object Obj;
}
#if CS73
private unsafe int GenericMethod<T>() where T : unmanaged
{
return sizeof(T);
}
#endif
private unsafe void Test(out int s1, out int s2, out int s3)
{
s1 = 0;
s2 = 0;
s3 = 0;
#if CS73
GenericMethod<UnmanagedStruct>();
#endif
s1 = sizeof(UnmanagedStruct);
#if !NET40 && ROSLYN
s2 = Unsafe.SizeOf<UnmanagedStruct>();
s3 = Unsafe.SizeOf<ManagedStruct>();
#endif
}
}
public class UnsafeCode
{
public struct SimpleStruct
{
public int X;
public double Y;
}
public struct ResultStruct
{
public unsafe byte* ptr1;
public unsafe byte* ptr2;
public unsafe ResultStruct(byte* ptr1, byte* ptr2)
{
this.ptr1 = ptr1;
this.ptr2 = ptr2;
}
}
public struct StructWithFixedSizeMembers
{
public unsafe fixed int Integers[100];
public int NormalMember;
public unsafe fixed double Doubles[200];
[Obsolete("another attribute")]
public unsafe fixed byte Old[1];
}
private struct Data
{
public Vector Position;
}
[StructLayout(LayoutKind.Sequential, Size = 1)]
private struct Vector
{
public override int GetHashCode()
{
return 0;
}
}
#if CS73
public class CustomPinnable
{
public ref int GetPinnableReference()
{
throw new NotImplementedException();
}
}
#endif
public unsafe delegate void UnsafeDelegate(byte* ptr);
private UnsafeDelegate unsafeDelegate;
private static UnsafeDelegate staticUnsafeDelegate;
#if CS60
public unsafe int* NullPointer => null;
#else
public unsafe int* NullPointer {
get {
return null;
}
}
#endif
unsafe static UnsafeCode()
{
staticUnsafeDelegate = UnsafeStaticMethod;
}
public unsafe UnsafeCode()
{
unsafeDelegate = UnsafeMethod;
}
public unsafe int SizeOf()
{
return sizeof(SimpleStruct);
}
private static void UseBool(bool b)
{
}
private unsafe void UnsafeMethod(byte* ptr)
{
}
private unsafe static void UnsafeStaticMethod(byte* ptr)
{
}
public unsafe void PointerComparison(int* a, double* b)
{
UseBool(a == b);
UseBool(a != b);
UseBool(a < b);
UseBool(a > b);
UseBool(a <= b);
UseBool(a >= b);
}
public unsafe void PointerComparisonWithNull(int* a)
{
UseBool(a == null);
UseBool(a != null);
}
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 int PointerCasts()
{
int result = 0;
*(float*)(&result) = 0.5f;
((sbyte*)(&result))[3] = 3;
((sbyte*)(&result))[3] = -1;
return result;
}
public unsafe void PassRefParameterAsPointer(ref int p)
{
fixed (int* ptr = &p)
{
UsePointer(ptr);
}
}
public unsafe void PassPointerAsRefParameter(int* p)
{
UseReference(ref *p);
}
public unsafe void PassPointerCastAsRefParameter(uint* p)
{
UseReference(ref *(int*)p);
}
public unsafe void AddressInMultiDimensionalArray(double[,] matrix)
{
fixed (double* d = &matrix[1, 2])
{
PointerReferenceExpression(d);
PointerReferenceExpression(d);
}
}
public unsafe void FixedStringAccess(string text)
{
fixed (char* ptr = text)
{
for (char* ptr2 = ptr; *ptr2 == 'a'; ptr2++)
{
*ptr2 = 'A';
}
}
}
public unsafe void FixedStringNoPointerUse(string text)
{
fixed (char* ptr = text)
{
}
}
#if !(LEGACY_CSC && OPT)
// legacy csc manages to optimize out the pinned variable altogether in this case;
// leaving no pinned region we could detect.
public unsafe void FixedArrayNoPointerUse(int[] arr)
{
fixed (int* ptr = arr)
{
}
}
#endif
public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val)
{
fixed (long* ptr = array)
{
*(double*)(ptr + index) = val;
}
}
public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val)
{
fixed (long* ptr = &array[index])
{
*(double*)ptr = val;
}
}
public unsafe string PointerReferenceExpression(double* d)
{
return d->ToString();
}
public unsafe string PointerReferenceExpression2(long addr)
{
return ((int*)addr)->ToString();
}
public unsafe int* PointerArithmetic(int* p)
{
return p + 2;
}
public unsafe long* PointerArithmetic2(long* p)
{
return 3 + p;
}
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 q[i] + *(byte*)p;
}
public unsafe int PointerArithmetic6(SimpleStruct* p, int i)
{
return p[i].X;
}
public unsafe int* PointerArithmeticLong1(int* p, long offset)
{
return p + offset;
}
public unsafe int* PointerArithmeticLong2(int* p, long offset)
{
return offset + p;
}
public unsafe int* PointerArithmeticLong3(int* p, long offset)
{
return p - offset;
}
public unsafe SimpleStruct* PointerArithmeticLong1s(SimpleStruct* p, long offset)
{
return p + offset;
}
public unsafe SimpleStruct* PointerArithmeticLong2s(SimpleStruct* p, long offset)
{
return offset + p;
}
public unsafe SimpleStruct* PointerArithmeticLong3s(SimpleStruct* p, long offset)
{
return p - offset;
}
public unsafe int PointerSubtraction(long* p, long* q)
{
return (int)(p - q);
}
public unsafe long PointerSubtractionLong(long* p, long* q)
{
return p - q;
}
public unsafe int PointerSubtraction2(long* p, short* q)
{
return (int)((byte*)p - (byte*)q);
}
public unsafe int PointerSubtraction3(void* p, void* q)
{
return (int)((byte*)p - (byte*)q);
}
public unsafe long PointerSubtraction4(sbyte* p, sbyte* q)
{
return p - q;
}
public unsafe long PointerSubtraction5(SimpleStruct* p, SimpleStruct* q)
{
return p - q;
}
public unsafe long Issue2158a(void* p, void* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158b(sbyte* p, sbyte* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158c(int* p, int* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158d(SimpleStruct* p, SimpleStruct* q)
{
return (long)p - (long)q;
}
public unsafe double FixedMemberAccess(StructWithFixedSizeMembers* m, int i)
{
return (double)m->Integers[i] + m->Doubles[i];
}
public unsafe double* FixedMemberBasePointer(StructWithFixedSizeMembers* m)
{
return m->Doubles;
}
public unsafe void UseFixedMemberAsPointer(StructWithFixedSizeMembers* m)
{
UsePointer(m->Integers);
}
public unsafe void UseFixedMemberAsReference(StructWithFixedSizeMembers* m)
{
UseReference(ref *m->Integers);
UseReference(ref m->Integers[1]);
}
public unsafe void PinFixedMember(ref StructWithFixedSizeMembers m)
{
fixed (int* integers = m.Integers)
{
UsePointer(integers);
}
}
private void UseReference(ref int i)
{
}
public unsafe string UsePointer(int* ptr)
{
return ptr->ToString();
}
public unsafe string UsePointer(double* ptr)
{
return ptr->ToString();
}
public unsafe void FixedMultiDimArray(int[,] arr)
{
fixed (int* ptr = arr)
{
UsePointer(ptr);
}
}
#if CS73 && !NET40
public unsafe void FixedSpan(Span<int> span)
{
fixed (int* ptr = span)
{
UsePointer(ptr);
}
}
#endif
#if CS73
public unsafe void FixedCustomReferenceType(CustomPinnable mem)
{
fixed (int* ptr = mem)
{
UsePointer(ptr);
}
}
public unsafe void FixedCustomReferenceTypeNoPointerUse(CustomPinnable mem)
{
fixed (int* ptr = mem)
{
Console.WriteLine("Hello World!");
}
}
public unsafe void FixedCustomReferenceTypeExplicitGetPinnableReference(CustomPinnable mem)
{
fixed (int* ptr = &mem.GetPinnableReference())
{
UsePointer(ptr);
}
}
#endif
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;
ptr2[i] = '\0';
}
return UsePointer((double*)ptr);
}
public unsafe string StackAllocStruct(int count)
{
SimpleStruct* ptr = stackalloc SimpleStruct[checked(count * 2)];
#if !(ROSLYN && OPT)
// unused stackalloc gets optimized out by roslyn
SimpleStruct* ptr2 = stackalloc SimpleStruct[10];
#endif
ptr->X = count;
ptr[1].X = ptr->X;
for (int i = 2; i < 10; i++)
{
ptr[i].X = count;
}
return UsePointer(&ptr->Y);
}
unsafe ~UnsafeCode()
{
PassPointerAsRefParameter(NullPointer);
}
private unsafe void Issue990()
{
Data data = default(Data);
Data* ptr = &data;
ConvertIntToFloat(ptr->Position.GetHashCode());
}
private unsafe static void Issue1021(ref byte* bytePtr, ref short* shortPtr)
{
bytePtr += 4;
shortPtr += 2;
bytePtr -= 4;
shortPtr = (short*)((byte*)shortPtr - 3);
}
private static T Get<T>()
{
return default(T);
}
private unsafe static ResultStruct NestedFixedBlocks(byte[] array)
{
try
{
fixed (byte* ptr = array)
{
fixed (byte* ptr2 = Get<byte[]>())
{
return new ResultStruct(ptr, ptr2);
}
}
}
finally
{
Console.WriteLine("Finally");
}
}
private unsafe static object CreateBuffer(int length, byte* ptr)
{
throw new NotImplementedException();
}
private unsafe static object Issue1386(int arraySize, bool createFirstBuffer)
{
if (createFirstBuffer)
{
byte[] array = new byte[arraySize];
Console.WriteLine("first fixed");
fixed (byte* ptr = array)
{
return CreateBuffer(array.Length, ptr);
}
}
byte[] array2 = new byte[arraySize];
Console.WriteLine("second fixed");
fixed (byte* ptr2 = array2)
{
return CreateBuffer(array2.Length, ptr2);
}
}
private unsafe static void Issue1499(StructWithFixedSizeMembers value, int index)
{
int num = value.Integers[index];
num.ToString();
}
#if CS73
private unsafe static int Issue2287(ref StructWithFixedSizeMembers value)
{
return value.Integers[0] + value.Integers[1];
}
#endif
private unsafe static int Issue2305(StructWithFixedSizeMembers value, StringComparison s)
{
return value.Integers[(int)s];
}
#if CS90
private unsafe static void* CastNIntToVoidPtr(nint intptr)
{
return (void*)intptr;
}
private unsafe static void* CastNIntToVoidPtr(nuint intptr)
{
return (void*)intptr;
}
#endif
#if !(CS110 && NET70)
private unsafe static void* CastToVoidPtr(IntPtr intptr)
{
return (void*)intptr;
}
private unsafe static void* CastToVoidPtr(UIntPtr intptr)
{
return (void*)intptr;
}
#endif
private unsafe static void* CastToVoidPtr(int* intptr)
{
return intptr;
}
public unsafe void ConditionalPointer(bool a, int* ptr)
{
UsePointer(a ? ptr : null);
}
public unsafe void UseArrayOfPointers(int*[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = null;
}
}
public unsafe void PassNullPointer1()
{
PointerReferenceExpression(null);
}
public unsafe void PassNullPointer2()
{
UseArrayOfPointers(null);
}
}
}