|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +public unsafe class MisSizedStructs |
| 9 | +{ |
| 10 | + public const byte ByteValue = 0xC1; |
| 11 | + |
| 12 | + public static int Main() |
| 13 | + { |
| 14 | + const int BytesSize = 256; |
| 15 | + var bytes = stackalloc byte[BytesSize]; |
| 16 | + Unsafe.InitBlock(bytes, ByteValue, BytesSize); |
| 17 | + |
| 18 | + if (ProblemWithStructWithThreeBytes(bytes)) |
| 19 | + { |
| 20 | + return 101; |
| 21 | + } |
| 22 | + |
| 23 | + if (ProblemWithStructWithFiveBytes(bytes)) |
| 24 | + { |
| 25 | + return 102; |
| 26 | + } |
| 27 | + |
| 28 | + if (ProblemWithStructWithSixBytes(bytes)) |
| 29 | + { |
| 30 | + return 103; |
| 31 | + } |
| 32 | + |
| 33 | + if (ProblemWithStructWithSevenBytes(bytes)) |
| 34 | + { |
| 35 | + return 104; |
| 36 | + } |
| 37 | + |
| 38 | + if (ProblemWithStructWithElevenBytes(bytes)) |
| 39 | + { |
| 40 | + return 105; |
| 41 | + } |
| 42 | + |
| 43 | + if (ProblemWithStructWithThirteenBytes(bytes)) |
| 44 | + { |
| 45 | + return 106; |
| 46 | + } |
| 47 | + |
| 48 | + if (ProblemWithStructWithFourteenBytes(bytes)) |
| 49 | + { |
| 50 | + return 107; |
| 51 | + } |
| 52 | + |
| 53 | + if (ProblemWithStructWithFifteenBytes(bytes)) |
| 54 | + { |
| 55 | + return 108; |
| 56 | + } |
| 57 | + |
| 58 | + if (ProblemWithStructWithNineteenBytes(bytes)) |
| 59 | + { |
| 60 | + return 109; |
| 61 | + } |
| 62 | + |
| 63 | + return 100; |
| 64 | + } |
| 65 | + |
| 66 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 67 | + private static bool ProblemWithStructWithThreeBytes(byte* bytes) |
| 68 | + { |
| 69 | + return CallForStructWithThreeBytes(default, *(StructWithThreeBytes*)bytes) != ByteValue; |
| 70 | + } |
| 71 | + |
| 72 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 73 | + private static bool ProblemWithStructWithFiveBytes(byte* bytes) |
| 74 | + { |
| 75 | + return CallForStructWithFiveBytes(default, *(StructWithFiveBytes*)bytes) != ByteValue; |
| 76 | + } |
| 77 | + |
| 78 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 79 | + private static bool ProblemWithStructWithSixBytes(byte* bytes) |
| 80 | + { |
| 81 | + return CallForStructWithSixBytes(default, *(StructWithSixBytes*)bytes) != ByteValue; |
| 82 | + } |
| 83 | + |
| 84 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 85 | + private static bool ProblemWithStructWithSevenBytes(byte* bytes) |
| 86 | + { |
| 87 | + return CallForStructWithSevenBytes(default, *(StructWithSevenBytes*)bytes) != ByteValue; |
| 88 | + } |
| 89 | + |
| 90 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 91 | + private static bool ProblemWithStructWithElevenBytes(byte* bytes) |
| 92 | + { |
| 93 | + return CallForStructWithElevenBytes(default, *(StructWithElevenBytes*)bytes) != ByteValue; |
| 94 | + } |
| 95 | + |
| 96 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 97 | + private static bool ProblemWithStructWithThirteenBytes(byte* bytes) |
| 98 | + { |
| 99 | + return CallForStructWithThirteenBytes(default, *(StructWithThirteenBytes*)bytes) != ByteValue; |
| 100 | + } |
| 101 | + |
| 102 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 103 | + private static bool ProblemWithStructWithFourteenBytes(byte* bytes) |
| 104 | + { |
| 105 | + return CallForStructWithFourteenBytes(default, *(StructWithFourteenBytes*)bytes) != ByteValue; |
| 106 | + } |
| 107 | + |
| 108 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 109 | + private static bool ProblemWithStructWithFifteenBytes(byte* bytes) |
| 110 | + { |
| 111 | + return CallForStructWithFifteenBytes(default, *(StructWithFifteenBytes*)bytes) != ByteValue; |
| 112 | + } |
| 113 | + |
| 114 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 115 | + private static bool ProblemWithStructWithNineteenBytes(byte* bytes) |
| 116 | + { |
| 117 | + return CallForStructWithNineteenBytes(default, *(StructWithNineteenBytes*)bytes) != ByteValue; |
| 118 | + } |
| 119 | + |
| 120 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 121 | + private static byte CallForStructWithThreeBytes(ForceStackUsage fs, StructWithThreeBytes value) => value.Bytes[2]; |
| 122 | + |
| 123 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 124 | + private static byte CallForStructWithFiveBytes(ForceStackUsage fs, StructWithFiveBytes value) => value.Bytes[4]; |
| 125 | + |
| 126 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 127 | + private static byte CallForStructWithSixBytes(ForceStackUsage fs, StructWithSixBytes value) => value.Bytes[5]; |
| 128 | + |
| 129 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 130 | + private static byte CallForStructWithSevenBytes(ForceStackUsage fs, StructWithSevenBytes value) => value.Bytes[6]; |
| 131 | + |
| 132 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 133 | + private static byte CallForStructWithElevenBytes(ForceStackUsage fs, StructWithElevenBytes value) => value.Bytes[10]; |
| 134 | + |
| 135 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 136 | + private static byte CallForStructWithThirteenBytes(ForceStackUsage fs, StructWithThirteenBytes value) => value.Bytes[12]; |
| 137 | + |
| 138 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 139 | + private static byte CallForStructWithFourteenBytes(ForceStackUsage fs, StructWithFourteenBytes value) => value.Bytes[13]; |
| 140 | + |
| 141 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 142 | + private static byte CallForStructWithFifteenBytes(ForceStackUsage fs, StructWithFifteenBytes value) => value.Bytes[14]; |
| 143 | + |
| 144 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 145 | + private static byte CallForStructWithNineteenBytes(ForceStackUsage fs, StructWithNineteenBytes value) => value.Bytes[18]; |
| 146 | + |
| 147 | + struct ForceStackUsage |
| 148 | + { |
| 149 | + public fixed byte Bytes[40]; |
| 150 | + } |
| 151 | + |
| 152 | + struct StructWithThreeBytes |
| 153 | + { |
| 154 | + public fixed byte Bytes[3]; |
| 155 | + } |
| 156 | + |
| 157 | + struct StructWithFiveBytes |
| 158 | + { |
| 159 | + public fixed byte Bytes[5]; |
| 160 | + } |
| 161 | + |
| 162 | + struct StructWithSixBytes |
| 163 | + { |
| 164 | + public fixed byte Bytes[6]; |
| 165 | + } |
| 166 | + |
| 167 | + struct StructWithSevenBytes |
| 168 | + { |
| 169 | + public fixed byte Bytes[7]; |
| 170 | + } |
| 171 | + |
| 172 | + struct StructWithElevenBytes |
| 173 | + { |
| 174 | + public fixed byte Bytes[11]; |
| 175 | + } |
| 176 | + |
| 177 | + struct StructWithThirteenBytes |
| 178 | + { |
| 179 | + public fixed byte Bytes[13]; |
| 180 | + } |
| 181 | + |
| 182 | + struct StructWithFourteenBytes |
| 183 | + { |
| 184 | + public fixed byte Bytes[14]; |
| 185 | + } |
| 186 | + |
| 187 | + struct StructWithFifteenBytes |
| 188 | + { |
| 189 | + public fixed byte Bytes[15]; |
| 190 | + } |
| 191 | + |
| 192 | + struct StructWithNineteenBytes |
| 193 | + { |
| 194 | + public fixed byte Bytes[19]; |
| 195 | + } |
| 196 | +} |
0 commit comments