-
Notifications
You must be signed in to change notification settings - Fork 4k
/
EmbeddedText.cs
380 lines (327 loc) · 14.7 KB
/
EmbeddedText.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents text to be embedded in a PDB.
/// </summary>
public sealed class EmbeddedText
{
/// <summary>
/// The maximum number of bytes in to write out uncompressed.
///
/// This prevents wasting resources on compressing tiny files with little to negative gain
/// in PDB file size.
///
/// Chosen as the point at which we start to see > 10% blob size reduction using all
/// current source files in corefx and roslyn as sample data.
/// </summary>
internal const int CompressionThreshold = 200;
/// <summary>
/// The path to the file to embed.
/// </summary>
/// <remarks>See remarks of <see cref="SyntaxTree.FilePath"/></remarks>
/// <remarks>Empty file paths are disallowed, as the debugger finds source by looking up files by their name (and then verifying their signature)</remarks>
public string FilePath { get; }
/// <summary>
/// Hash algorithm to use to calculate checksum of the text that's saved to PDB.
/// </summary>
public SourceHashAlgorithm ChecksumAlgorithm { get; }
/// <summary>
/// The <see cref="ChecksumAlgorithm"/> hash of the uncompressed bytes
/// that's saved to the PDB.
/// </summary>
public ImmutableArray<byte> Checksum { get; }
private EmbeddedText(string filePath, ImmutableArray<byte> checksum, SourceHashAlgorithm checksumAlgorithm, ImmutableArray<byte> blob)
{
Debug.Assert(filePath?.Length > 0);
Debug.Assert(Cci.DebugSourceDocument.IsSupportedAlgorithm(checksumAlgorithm));
Debug.Assert(!blob.IsDefault && blob.Length >= sizeof(int));
FilePath = filePath;
Checksum = checksum;
ChecksumAlgorithm = checksumAlgorithm;
Blob = blob;
}
/// <summary>
/// The content that will be written to the PDB.
/// </summary>
/// <remarks>
/// Internal since this is an implementation detail. The only public
/// contract is that you can pass EmbeddedText instances to Emit.
/// It just so happened that doing this up-front was most practical
/// and efficient, but we don't want to be tied to it.
///
/// For efficiency, the format of this blob is exactly as it is written
/// to the PDB,which prevents extra copies being made during emit.
///
/// The first 4 bytes (little endian int32) indicate the format:
///
/// 0: data that follows is uncompressed
/// Positive: data that follows is deflate compressed and value is original, uncompressed size
/// Negative: invalid at this time, but reserved to mark a different format in the future.
/// </remarks>
internal ImmutableArray<byte> Blob { get; }
/// <summary>
/// Constructs a <see cref="EmbeddedText"/> for embedding the given <see cref="SourceText"/>.
/// </summary>
/// <param name="filePath">The file path (pre-normalization) to use in the PDB.</param>
/// <param name="text">The source text to embed.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="filePath"/> is null.
/// <paramref name="text"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="filePath"/> empty.
/// <paramref name="text"/> cannot be embedded (see <see cref="SourceText.CanBeEmbedded"/>).
/// </exception>
public static EmbeddedText FromSource(string filePath, SourceText text)
{
ValidateFilePath(filePath);
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
if (!text.CanBeEmbedded)
{
throw new ArgumentException(CodeAnalysisResources.SourceTextCannotBeEmbedded, nameof(text));
}
if (!text.PrecomputedEmbeddedTextBlob.IsDefault)
{
return new EmbeddedText(filePath, text.GetChecksum(), text.ChecksumAlgorithm, text.PrecomputedEmbeddedTextBlob);
}
return new EmbeddedText(filePath, text.GetChecksum(), text.ChecksumAlgorithm, CreateBlob(text));
}
/// <summary>
/// Constructs an <see cref="EmbeddedText"/> from stream content.
/// </summary>
/// <param name="filePath">The file path (pre-normalization) to use in the PDB.</param>
/// <param name="stream">The stream.</param>
/// <param name="checksumAlgorithm">Hash algorithm to use to calculate checksum of the text that's saved to PDB.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="filePath" /> is null.
/// <paramref name="stream"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="filePath" /> is empty.
/// <paramref name="stream"/> doesn't support reading or seeking.
/// <paramref name="checksumAlgorithm"/> is not supported.
/// </exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
/// <remarks>Reads from the beginning of the stream. Leaves the stream open.</remarks>
public static EmbeddedText FromStream(string filePath, Stream stream, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
{
ValidateFilePath(filePath);
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!stream.CanRead || !stream.CanSeek)
{
throw new ArgumentException(CodeAnalysisResources.StreamMustSupportReadAndSeek, nameof(stream));
}
SourceText.ValidateChecksumAlgorithm(checksumAlgorithm);
return new EmbeddedText(
filePath,
SourceText.CalculateChecksum(stream, checksumAlgorithm),
checksumAlgorithm,
CreateBlob(stream));
}
/// <summary>
/// Constructs an <see cref="EmbeddedText"/> from bytes.
/// </summary>
/// <param name="filePath">The file path (pre-normalization) to use in the PDB.</param>
/// <param name="bytes">The bytes.</param>
/// <param name="checksumAlgorithm">Hash algorithm to use to calculate checksum of the text that's saved to PDB.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="bytes"/> is default-initialized.
/// <paramref name="filePath" /> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="filePath" /> is empty.
/// <paramref name="checksumAlgorithm"/> is not supported.
/// </exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
/// <remarks>Reads from the beginning of the stream. Leaves the stream open.</remarks>
public static EmbeddedText FromBytes(string filePath, ArraySegment<byte> bytes, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
{
ValidateFilePath(filePath);
if (bytes.Array == null)
{
throw new ArgumentNullException(nameof(bytes));
}
SourceText.ValidateChecksumAlgorithm(checksumAlgorithm);
return new EmbeddedText(
filePath,
SourceText.CalculateChecksum(bytes.Array, bytes.Offset, bytes.Count, checksumAlgorithm),
checksumAlgorithm,
CreateBlob(bytes));
}
/// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is empty.</exception>
private static void ValidateFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
if (filePath.Length == 0)
{
throw new ArgumentException(CodeAnalysisResources.ArgumentCannotBeEmpty, nameof(filePath));
}
}
/// <summary>
/// Creates the blob to be saved to the PDB.
/// </summary>
internal static ImmutableArray<byte> CreateBlob(Stream stream)
{
Debug.Assert(stream != null);
Debug.Assert(stream.CanRead);
Debug.Assert(stream.CanSeek);
long longLength = stream.Length;
Debug.Assert(longLength >= 0);
if (longLength > int.MaxValue)
{
throw new IOException(CodeAnalysisResources.StreamIsTooLong);
}
stream.Seek(0, SeekOrigin.Begin);
int length = (int)longLength;
if (length < CompressionThreshold)
{
using (var builder = Cci.PooledBlobBuilder.GetInstance())
{
builder.WriteInt32(0);
int bytesWritten = builder.TryWriteBytes(stream, length);
if (length != bytesWritten)
{
throw new EndOfStreamException();
}
return builder.ToImmutableArray();
}
}
else
{
using (var builder = BlobBuildingStream.GetInstance())
{
builder.WriteInt32(length);
using (var deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true))
{
stream.CopyTo(deflater);
if (length != deflater.BytesWritten)
{
throw new EndOfStreamException();
}
}
return builder.ToImmutableArray();
}
}
}
internal static ImmutableArray<byte> CreateBlob(ArraySegment<byte> bytes)
{
Debug.Assert(bytes.Array != null);
if (bytes.Count < CompressionThreshold)
{
using (var builder = Cci.PooledBlobBuilder.GetInstance())
{
builder.WriteInt32(0);
builder.WriteBytes(bytes.Array, bytes.Offset, bytes.Count);
return builder.ToImmutableArray();
}
}
else
{
using (var builder = BlobBuildingStream.GetInstance())
{
builder.WriteInt32(bytes.Count);
using (var deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true))
{
deflater.Write(bytes.Array, bytes.Offset, bytes.Count);
}
return builder.ToImmutableArray();
}
}
}
private static ImmutableArray<byte> CreateBlob(SourceText text)
{
Debug.Assert(text != null);
Debug.Assert(text.CanBeEmbedded);
Debug.Assert(text.Encoding != null);
Debug.Assert(text.PrecomputedEmbeddedTextBlob.IsDefault);
int maxByteCount;
try
{
maxByteCount = text.Encoding.GetMaxByteCount(text.Length);
}
catch (ArgumentOutOfRangeException)
{
// Encoding does not provide a way to predict that max byte count would not
// fit in Int32 and we must therefore catch ArgumentOutOfRange to handle that
// case.
maxByteCount = int.MaxValue;
}
using (var builder = BlobBuildingStream.GetInstance())
{
if (maxByteCount < CompressionThreshold)
{
builder.WriteInt32(0);
using (var writer = new StreamWriter(builder, text.Encoding, bufferSize: Math.Max(1, text.Length), leaveOpen: true))
{
text.Write(writer);
}
}
else
{
Blob reserved = builder.ReserveBytes(4);
using (var deflater = new CountingDeflateStream(builder, CompressionLevel.Optimal, leaveOpen: true))
{
using (var writer = new StreamWriter(deflater, text.Encoding, bufferSize: 1024, leaveOpen: true))
{
text.Write(writer);
}
new BlobWriter(reserved).WriteInt32(deflater.BytesWritten);
}
}
return builder.ToImmutableArray();
}
}
internal Cci.DebugSourceInfo GetDebugSourceInfo()
{
return new Cci.DebugSourceInfo(Checksum, ChecksumAlgorithm, Blob);
}
private sealed class CountingDeflateStream : DeflateStream
{
public CountingDeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen)
: base(stream, compressionLevel, leaveOpen)
{
}
public int BytesWritten { get; private set; }
public override void Write(byte[] array, int offset, int count)
{
base.Write(array, offset, count);
// checked arithmetic is release-enabled quasi-assert. We start with at most
// int.MaxValue chars so compression or encoding would have to be abysmal for
// this to overflow. We'd probably be lucky to even get this far but if we do
// we should fail fast.
checked { BytesWritten += count; }
}
public override void WriteByte(byte value)
{
base.WriteByte(value);
// same rationale for checked arithmetic as above.
checked { BytesWritten++; };
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw ExceptionUtilities.Unreachable;
}
}
}
}