|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Buffers; |
4 | 5 | using System.Collections.Generic;
|
5 | 6 | using System.Linq;
|
6 | 7 | using System.Security.Cryptography;
|
7 | 8 | using System.Threading;
|
8 | 9 | using System.Threading.Tasks;
|
| 10 | +using Microsoft.DotNet.XUnitExtensions; |
9 | 11 | using Microsoft.Win32.SafeHandles;
|
10 | 12 | using Xunit;
|
11 | 13 |
|
12 | 14 | namespace System.IO.Tests
|
13 | 15 | {
|
14 | 16 | [SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")]
|
| 17 | + [Collection(nameof(DisableParallelization))] // don't run in parallel, as some of these tests use a LOT of resources |
15 | 18 | public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask>
|
16 | 19 | {
|
17 | 20 | protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
|
@@ -133,5 +136,172 @@ public async Task DuplicatedBufferDuplicatesContentAsync(FileOptions options)
|
133 | 136 | Assert.Equal(repeatCount, actualContent.Length);
|
134 | 137 | Assert.All(actualContent, actual => Assert.Equal(value, actual));
|
135 | 138 | }
|
| 139 | + |
| 140 | + [OuterLoop("It consumes a lot of resources (disk space and memory).")] |
| 141 | + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess), nameof(PlatformDetection.IsReleaseRuntime))] |
| 142 | + [InlineData(false, false)] |
| 143 | + [InlineData(false, true)] |
| 144 | + [InlineData(true, true)] |
| 145 | + [InlineData(true, false)] |
| 146 | + public async Task NoInt32OverflowForLargeInputs(bool asyncFile, bool asyncMethod) |
| 147 | + { |
| 148 | + // We need to write more than Int32.MaxValue bytes to the disk to reproduce the problem. |
| 149 | + // To reduce the number of used memory, we allocate only one write buffer and simply repeat it multiple times. |
| 150 | + // For reading, we need unique buffers to ensure that all of them are getting populated with the right data. |
| 151 | + |
| 152 | + const int BufferCount = 1002; |
| 153 | + const int BufferSize = int.MaxValue / 1000; |
| 154 | + const long FileSize = (long)BufferCount * BufferSize; |
| 155 | + string filePath = GetTestFilePath(); |
| 156 | + |
| 157 | + FileOptions options = asyncFile ? FileOptions.Asynchronous : FileOptions.None; // we need to test both code paths |
| 158 | + options |= FileOptions.DeleteOnClose; |
| 159 | + |
| 160 | + SafeFileHandle? sfh; |
| 161 | + try |
| 162 | + { |
| 163 | + sfh = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, options, preallocationSize: FileSize); |
| 164 | + } |
| 165 | + catch (IOException) |
| 166 | + { |
| 167 | + throw new SkipTestException("Not enough disk space."); |
| 168 | + } |
| 169 | + |
| 170 | + using (sfh) |
| 171 | + { |
| 172 | + ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); |
| 173 | + List<ReadOnlyMemory<byte>> writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToList(); |
| 174 | + |
| 175 | + List<NativeMemoryManager> memoryManagers = new List<NativeMemoryManager>(BufferCount); |
| 176 | + List<Memory<byte>> readBuffers = new List<Memory<byte>>(BufferCount); |
| 177 | + |
| 178 | + try |
| 179 | + { |
| 180 | + try |
| 181 | + { |
| 182 | + for (int i = 0; i < BufferCount; i++) |
| 183 | + { |
| 184 | + // We are using native memory here to get OOM as soon as possible. |
| 185 | + NativeMemoryManager nativeMemoryManager = new(BufferSize); |
| 186 | + memoryManagers.Add(nativeMemoryManager); |
| 187 | + readBuffers.Add(nativeMemoryManager.Memory); |
| 188 | + } |
| 189 | + } |
| 190 | + catch (OutOfMemoryException) |
| 191 | + { |
| 192 | + throw new SkipTestException("Not enough memory."); |
| 193 | + } |
| 194 | + |
| 195 | + await Verify(asyncMethod, FileSize, sfh, writeBuffer, writeBuffers, readBuffers); |
| 196 | + } |
| 197 | + finally |
| 198 | + { |
| 199 | + foreach (IDisposable memoryManager in memoryManagers) |
| 200 | + { |
| 201 | + memoryManager.Dispose(); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + static async Task Verify(bool asyncMethod, long FileSize, SafeFileHandle sfh, ReadOnlyMemory<byte> writeBuffer, List<ReadOnlyMemory<byte>> writeBuffers, List<Memory<byte>> readBuffers) |
| 207 | + { |
| 208 | + if (asyncMethod) |
| 209 | + { |
| 210 | + await RandomAccess.WriteAsync(sfh, writeBuffers, 0); |
| 211 | + } |
| 212 | + else |
| 213 | + { |
| 214 | + RandomAccess.Write(sfh, writeBuffers, 0); |
| 215 | + } |
| 216 | + |
| 217 | + Assert.Equal(FileSize, RandomAccess.GetLength(sfh)); |
| 218 | + |
| 219 | + long fileOffset = 0; |
| 220 | + while (fileOffset < FileSize) |
| 221 | + { |
| 222 | + long bytesRead = asyncMethod |
| 223 | + ? await RandomAccess.ReadAsync(sfh, readBuffers, fileOffset) |
| 224 | + : RandomAccess.Read(sfh, readBuffers, fileOffset); |
| 225 | + |
| 226 | + Assert.InRange(bytesRead, 0, FileSize); |
| 227 | + |
| 228 | + while (bytesRead > 0) |
| 229 | + { |
| 230 | + Memory<byte> readBuffer = readBuffers[0]; |
| 231 | + if (bytesRead >= readBuffer.Length) |
| 232 | + { |
| 233 | + AssertExtensions.SequenceEqual(writeBuffer.Span, readBuffer.Span); |
| 234 | + |
| 235 | + bytesRead -= readBuffer.Length; |
| 236 | + fileOffset += readBuffer.Length; |
| 237 | + |
| 238 | + readBuffers.RemoveAt(0); |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + // A read has finished somewhere in the middle of one of the read buffers. |
| 243 | + // Example: buffer had 30 bytes and only 10 were read. |
| 244 | + // We don't read the missing part, but try to read the whole buffer again. |
| 245 | + // It's not optimal from performance perspective, but it keeps the test logic simple. |
| 246 | + break; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + [Theory] |
| 254 | + [InlineData(false, false)] |
| 255 | + [InlineData(false, true)] |
| 256 | + [InlineData(true, true)] |
| 257 | + [InlineData(true, false)] |
| 258 | + public async Task IovLimitsAreRespected(bool asyncFile, bool asyncMethod) |
| 259 | + { |
| 260 | + // We need to write and read more than IOV_MAX buffers at a time. |
| 261 | + // IOV_MAX typical value is 1024. |
| 262 | + const int BufferCount = 1026; |
| 263 | + const int BufferSize = 1; // the less resources we use, the better |
| 264 | + const int FileSize = BufferCount * BufferSize; |
| 265 | + |
| 266 | + ReadOnlyMemory<byte> writeBuffer = RandomNumberGenerator.GetBytes(BufferSize); |
| 267 | + ReadOnlyMemory<byte>[] writeBuffers = Enumerable.Repeat(writeBuffer, BufferCount).ToArray(); |
| 268 | + Memory<byte>[] readBuffers = Enumerable.Range(0, BufferCount).Select(_ => new byte[BufferSize].AsMemory()).ToArray(); |
| 269 | + |
| 270 | + FileOptions options = asyncFile ? FileOptions.Asynchronous : FileOptions.None; // we need to test both code paths |
| 271 | + options |= FileOptions.DeleteOnClose; |
| 272 | + |
| 273 | + using SafeFileHandle sfh = File.OpenHandle(GetTestFilePath(), FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, options); |
| 274 | + |
| 275 | + if (asyncMethod) |
| 276 | + { |
| 277 | + await RandomAccess.WriteAsync(sfh, writeBuffers, 0); |
| 278 | + } |
| 279 | + else |
| 280 | + { |
| 281 | + RandomAccess.Write(sfh, writeBuffers, 0); |
| 282 | + } |
| 283 | + |
| 284 | + Assert.Equal(FileSize, RandomAccess.GetLength(sfh)); |
| 285 | + |
| 286 | + long fileOffset = 0; |
| 287 | + int bufferOffset = 0; |
| 288 | + while (fileOffset < FileSize) |
| 289 | + { |
| 290 | + ArraySegment<Memory<byte>> left = new ArraySegment<Memory<byte>>(readBuffers, bufferOffset, readBuffers.Length - bufferOffset); |
| 291 | + |
| 292 | + long bytesRead = asyncMethod |
| 293 | + ? await RandomAccess.ReadAsync(sfh, left, fileOffset) |
| 294 | + : RandomAccess.Read(sfh, left, fileOffset); |
| 295 | + |
| 296 | + fileOffset += bytesRead; |
| 297 | + // The following operation is correct only because the BufferSize is 1. |
| 298 | + bufferOffset += (int)bytesRead; |
| 299 | + } |
| 300 | + |
| 301 | + for (int i = 0; i < BufferCount; ++i) |
| 302 | + { |
| 303 | + Assert.Equal(writeBuffers[i], readBuffers[i]); |
| 304 | + } |
| 305 | + } |
136 | 306 | }
|
137 | 307 | }
|
0 commit comments