|
6 | 6 | using System.Security.Cryptography;
|
7 | 7 | using System.Threading;
|
8 | 8 | using System.Threading.Tasks;
|
| 9 | +using Microsoft.DotNet.XUnitExtensions; |
9 | 10 | using Microsoft.Win32.SafeHandles;
|
10 | 11 | using Xunit;
|
11 | 12 |
|
@@ -133,5 +134,130 @@ public async Task DuplicatedBufferDuplicatesContentAsync(FileOptions options)
|
133 | 134 | Assert.Equal(repeatCount, actualContent.Length);
|
134 | 135 | Assert.All(actualContent, actual => Assert.Equal(value, actual));
|
135 | 136 | }
|
| 137 | + |
| 138 | + [OuterLoop("It consumes a lot of resources (disk space and memory).")] |
| 139 | + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess), nameof(PlatformDetection.IsReleaseRuntime))] |
| 140 | + [InlineData(false, false)] |
| 141 | + [InlineData(false, true)] |
| 142 | + [InlineData(true, true)] |
| 143 | + [InlineData(true, false)] |
| 144 | + public async Task NoInt32OverflowForLargeInputs(bool asyncFile, bool asyncMethod) |
| 145 | + { |
| 146 | + // We need to write more than Int32.MaxValue bytes to the disk to reproduce the problem. |
| 147 | + // To reduce the number of used memory, we allocate only one write buffer and simply repeat it multiple times. |
| 148 | + // For reading, we need unique buffers to ensure that all of them are getting populated with the right data. |
| 149 | + |
| 150 | + const int BufferCount = 1002; |
| 151 | + const int BufferSize = int.MaxValue / 1000; |
| 152 | + const long FileSize = (long)BufferCount * BufferSize; |
| 153 | + string filePath = GetTestFilePath(); |
| 154 | + ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); |
| 155 | + List<ReadOnlyMemory<byte>> writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToList(); |
| 156 | + List<Memory<byte>> readBuffers = new List<Memory<byte>>(BufferCount); |
| 157 | + |
| 158 | + try |
| 159 | + { |
| 160 | + for (int i = 0; i < BufferCount; i++) |
| 161 | + { |
| 162 | + readBuffers.Add(new byte[BufferSize]); |
| 163 | + } |
| 164 | + } |
| 165 | + catch (OutOfMemoryException) |
| 166 | + { |
| 167 | + throw new SkipTestException("Not enough memory."); |
| 168 | + } |
| 169 | + |
| 170 | + FileOptions options = asyncFile ? FileOptions.Asynchronous : FileOptions.None; // we need to test both code paths |
| 171 | + options |= FileOptions.DeleteOnClose; |
| 172 | + |
| 173 | + SafeFileHandle? sfh; |
| 174 | + try |
| 175 | + { |
| 176 | + sfh = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, options, preallocationSize: FileSize); |
| 177 | + } |
| 178 | + catch (IOException) |
| 179 | + { |
| 180 | + throw new SkipTestException("Not enough disk space."); |
| 181 | + } |
| 182 | + |
| 183 | + long fileOffset = 0, bytesRead = 0; |
| 184 | + try |
| 185 | + { |
| 186 | + if (asyncMethod) |
| 187 | + { |
| 188 | + await RandomAccess.WriteAsync(sfh, writeBuffers, fileOffset); |
| 189 | + bytesRead = await RandomAccess.ReadAsync(sfh, readBuffers, fileOffset); |
| 190 | + } |
| 191 | + else |
| 192 | + { |
| 193 | + RandomAccess.Write(sfh, writeBuffers, fileOffset); |
| 194 | + bytesRead = RandomAccess.Read(sfh, readBuffers, fileOffset); |
| 195 | + } |
| 196 | + } |
| 197 | + finally |
| 198 | + { |
| 199 | + sfh.Dispose(); // delete the file ASAP to avoid running out of resources in CI |
| 200 | + } |
| 201 | + |
| 202 | + Assert.Equal(FileSize, bytesRead); |
| 203 | + for (int i = 0; i < BufferCount; i++) |
| 204 | + { |
| 205 | + Assert.Equal(writeBuffer, readBuffers[i]); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + [Theory] |
| 210 | + [InlineData(false, false)] |
| 211 | + [InlineData(false, true)] |
| 212 | + [InlineData(true, true)] |
| 213 | + [InlineData(true, false)] |
| 214 | + public async Task IovLimitsAreRespected(bool asyncFile, bool asyncMethod) |
| 215 | + { |
| 216 | + // We need to write and read more than IOV_MAX buffers at a time. |
| 217 | + // IOV_MAX typical value is 1024. |
| 218 | + const int BufferCount = 1026; |
| 219 | + const int BufferSize = 1; // the less resources we use, the better |
| 220 | + const int FileSize = BufferCount * BufferSize; |
| 221 | + |
| 222 | + ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); |
| 223 | + ReadOnlyMemory<byte>[] writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToArray(); |
| 224 | + Memory<byte>[] readBuffers = Enumerable.Range(0, BufferCount).Select(_ => new byte[BufferSize].AsMemory()).ToArray(); |
| 225 | + |
| 226 | + FileOptions options = asyncFile ? FileOptions.Asynchronous : FileOptions.None; // we need to test both code paths |
| 227 | + options |= FileOptions.DeleteOnClose; |
| 228 | + |
| 229 | + using SafeFileHandle sfh = File.OpenHandle(GetTestFilePath(), FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, options); |
| 230 | + |
| 231 | + if (asyncMethod) |
| 232 | + { |
| 233 | + await RandomAccess.WriteAsync(sfh, writeBuffers, 0); |
| 234 | + } |
| 235 | + else |
| 236 | + { |
| 237 | + RandomAccess.Write(sfh, writeBuffers, 0); |
| 238 | + } |
| 239 | + |
| 240 | + Assert.Equal(FileSize, RandomAccess.GetLength(sfh)); |
| 241 | + |
| 242 | + long fileOffset = 0; |
| 243 | + int bufferOffset = 0; |
| 244 | + while (fileOffset < FileSize) |
| 245 | + { |
| 246 | + ArraySegment<Memory<byte>> left = new ArraySegment<Memory<byte>>(readBuffers, bufferOffset, readBuffers.Length - bufferOffset); |
| 247 | + |
| 248 | + long bytesRead = asyncMethod |
| 249 | + ? await RandomAccess.ReadAsync(sfh, left, fileOffset) |
| 250 | + : RandomAccess.Read(sfh, left, fileOffset); |
| 251 | + |
| 252 | + fileOffset += bytesRead; |
| 253 | + // The following operation is correct only because the BufferSize is 1. |
| 254 | + bufferOffset += (int)bytesRead; |
| 255 | + } |
| 256 | + |
| 257 | + for (int i = 0; i < BufferCount; ++i) |
| 258 | + { |
| 259 | + Assert.Equal(writeBuffers[i], readBuffers[i]); |
| 260 | + } |
| 261 | + } |
136 | 262 | }
|
137 | 263 | }
|
0 commit comments