Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnsafeDataFlow.cs « DataFlow « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c4d9f98d584936bb6b8b174ad2d02357ab1d0bbf (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.CodeAnalysis;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.DataFlow
{
	[SetupCompileArgument ("/unsafe")]
	[SkipKeptItemsValidation]
	[ExpectedNoWarnings]
	class UnsafeDataFlow
	{
		public static void Main ()
		{
			TestReadFromPointer ();
			TestWriteToPointer ();
			TestWriteToStackAllocedStruct ();
		}

		// We don't analyze the pointer manipulation, so it should produce a warning
		// about reading an unknown type, without crashing the analyzer.
		[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll))]
		static unsafe void TestReadFromPointer ()
		{
			int i = 6;
			int* pI = &i;
			Type[] arr = new Type[] { GetWithPublicMethods () };
			arr[*pI].RequiresAll ();
		}

		// We don't analyze the pointer manipulation, so it should produce a warning
		// about reading an unknown type, without crashing the analyzer.
		[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll))]
		static unsafe void TestWriteToPointer ()
		{
			int i = 6;
			int* pI = &i;
			*pI = 0;
			Type[] arr = new Type[] { GetWithPublicMethods () };
			arr[i].RequiresAll ();
		}

		// We don't analyze the stackalloc'd struct member, so it should produce a warning
		// about reading an unknown type, without crashing the analyzer.
		[ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresAll))]
		static unsafe void TestWriteToStackAllocedStruct ()
		{
			var stackArr = stackalloc S[1];
			stackArr[0] = new S {
				I = 0
			};
			Type[] arr = new Type[] { GetWithPublicMethods () };
			arr[stackArr[0].I].RequiresAll ();
		}

		struct S
		{
			public int I;
		}

		[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
		static Type GetWithPublicMethods () => null;
	}
}