Skip to content

Commit cb6c1ca

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a6c8f5e + f9751de commit cb6c1ca

File tree

13 files changed

+233
-88
lines changed

13 files changed

+233
-88
lines changed

Mono.Cecil.PE/ImageReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void ReadDebugHeader ()
350350
PointerToRawData = ReadInt32 (),
351351
};
352352

353-
if (directory.AddressOfRawData == 0) {
353+
if (directory.PointerToRawData == 0) {
354354
entries [i] = new ImageDebugHeaderEntry (directory, Empty<byte>.Array);
355355
continue;
356356
}

Mono.Cecil.PE/ImageWriter.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,6 @@ public uint GetHeaderSize ()
814814
return pe_header_size + SizeOfOptionalHeader () + (sections * section_header_size);
815815
}
816816

817-
public void PatchMvid (Guid guid)
818-
{
819-
uint offset = GetRVAFileOffset (text, text_map.GetRVA (TextSegment.GuidHeap));
820-
BaseStream.Seek (offset, SeekOrigin.Begin);
821-
var arr = guid.ToByteArray ();
822-
BaseStream.Write (arr, 0, arr.Length);
823-
}
824-
825817
void PatchWin32Resources (ByteBuffer resources)
826818
{
827819
PatchResourceDirectoryTable (resources);

Mono.Cecil.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
4-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
54
</PropertyGroup>
65
<ItemGroup>
76
<Compile Include="ProjectInfo.cs" />

Mono.Cecil/AssemblyReader.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ static void ReadSymbols (ModuleDefinition module, ReaderParameters parameters)
103103
? symbol_reader_provider.GetSymbolReader (module, parameters.SymbolStream)
104104
: symbol_reader_provider.GetSymbolReader (module, module.FileName);
105105

106-
if (reader != null)
107-
module.ReadSymbols (reader, parameters.ThrowIfSymbolsAreNotMatching);
106+
if (reader != null) {
107+
try {
108+
module.ReadSymbols (reader, parameters.ThrowIfSymbolsAreNotMatching);
109+
} catch (Exception) {
110+
reader.Dispose ();
111+
throw;
112+
}
113+
}
108114
}
109115

110116
if (module.Image.HasDebugTables ())
@@ -237,12 +243,20 @@ void ReadGenericParameters (IGenericParameterProvider provider)
237243
var parameter = parameters [i];
238244

239245
if (parameter.HasConstraints)
240-
Mixin.Read (parameter.Constraints);
246+
ReadGenericParameterConstraints (parameter);
241247

242248
ReadCustomAttributes (parameter);
243249
}
244250
}
245251

252+
void ReadGenericParameterConstraints (GenericParameter parameter)
253+
{
254+
var constraints = parameter.Constraints;
255+
256+
for (int i = 0; i < constraints.Count; i++)
257+
ReadCustomAttributes (constraints [i]);
258+
}
259+
246260
void ReadSecurityDeclarations (ISecurityDeclarationProvider provider)
247261
{
248262
if (!provider.HasSecurityDeclarations)
@@ -1987,27 +2001,31 @@ public bool HasGenericConstraints (GenericParameter generic_parameter)
19872001
{
19882002
InitializeGenericConstraints ();
19892003

1990-
Collection<MetadataToken> mapping;
2004+
Collection<Row<uint, MetadataToken>> mapping;
19912005
if (!metadata.TryGetGenericConstraintMapping (generic_parameter, out mapping))
19922006
return false;
19932007

19942008
return mapping.Count > 0;
19952009
}
19962010

1997-
public Collection<TypeReference> ReadGenericConstraints (GenericParameter generic_parameter)
2011+
public GenericParameterConstraintCollection ReadGenericConstraints (GenericParameter generic_parameter)
19982012
{
19992013
InitializeGenericConstraints ();
20002014

2001-
Collection<MetadataToken> mapping;
2015+
Collection<Row<uint, MetadataToken>> mapping;
20022016
if (!metadata.TryGetGenericConstraintMapping (generic_parameter, out mapping))
2003-
return new Collection<TypeReference> ();
2017+
return new GenericParameterConstraintCollection (generic_parameter);
20042018

2005-
var constraints = new Collection<TypeReference> (mapping.Count);
2019+
var constraints = new GenericParameterConstraintCollection (generic_parameter, mapping.Count);
20062020

20072021
this.context = (IGenericContext) generic_parameter.Owner;
20082022

2009-
for (int i = 0; i < mapping.Count; i++)
2010-
constraints.Add (GetTypeDefOrRef (mapping [i]));
2023+
for (int i = 0; i < mapping.Count; i++) {
2024+
constraints.Add (
2025+
new GenericParameterConstraint (
2026+
GetTypeDefOrRef (mapping [i].Col2),
2027+
new MetadataToken (TokenType.GenericParamConstraint, mapping [i].Col1)));
2028+
}
20112029

20122030
metadata.RemoveGenericConstraintMapping (generic_parameter);
20132031

@@ -2021,15 +2039,16 @@ void InitializeGenericConstraints ()
20212039

20222040
var length = MoveTo (Table.GenericParamConstraint);
20232041

2024-
metadata.GenericConstraints = new Dictionary<uint, Collection<MetadataToken>> (length);
2042+
metadata.GenericConstraints = new Dictionary<uint, Collection<Row<uint, MetadataToken>>> (length);
20252043

2026-
for (int i = 1; i <= length; i++)
2044+
for (uint i = 1; i <= length; i++) {
20272045
AddGenericConstraintMapping (
20282046
ReadTableIndex (Table.GenericParam),
2029-
ReadMetadataToken (CodedIndex.TypeDefOrRef));
2047+
new Row<uint, MetadataToken> (i, ReadMetadataToken (CodedIndex.TypeDefOrRef)));
2048+
}
20302049
}
20312050

2032-
void AddGenericConstraintMapping (uint generic_parameter, MetadataToken constraint)
2051+
void AddGenericConstraintMapping (uint generic_parameter, Row<uint, MetadataToken> constraint)
20332052
{
20342053
metadata.SetGenericConstraintMapping (
20352054
generic_parameter,

Mono.Cecil/AssemblyWriter.cs

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static void Write (ModuleDefinition module, Disposable<Stream> stream, WriterPar
109109

110110
if (parameters.DeterministicMvid)
111111
module.Mvid = Guid.Empty;
112+
112113
var metadata = new MetadataBuilder (module, fq_name, timestamp, symbol_writer_provider);
113114
try {
114115
module.metadata_builder = metadata;
@@ -117,62 +118,21 @@ static void Write (ModuleDefinition module, Disposable<Stream> stream, WriterPar
117118
metadata.SetSymbolWriter (symbol_writer);
118119
BuildMetadata (module, metadata);
119120

121+
if (parameters.DeterministicMvid)
122+
metadata.ComputeDeterministicMvid ();
123+
120124
var writer = ImageWriter.CreateWriter (module, metadata, stream);
121125
stream.value.SetLength (0);
122126
writer.WriteImage ();
123127

124128
if (parameters.StrongNameKeyPair != null)
125129
CryptoService.StrongName (stream.value, writer, parameters.StrongNameKeyPair);
126-
if (parameters.DeterministicMvid) {
127-
module.Mvid = ComputeGuid (stream.value);
128-
writer.PatchMvid (module.Mvid);
129-
}
130130
}
131131
} finally {
132132
module.metadata_builder = null;
133133
}
134134
}
135135

136-
static void CopyStreamChunk (Stream stream, Stream dest_stream, byte [] buffer, int length)
137-
{
138-
while (length > 0) {
139-
int read = stream.Read (buffer, 0, System.Math.Min (buffer.Length, length));
140-
dest_stream.Write (buffer, 0, read);
141-
length -= read;
142-
}
143-
}
144-
145-
static byte [] ComputeHash (Stream stream)
146-
{
147-
const int buffer_size = 8192;
148-
149-
var sha1 = new SHA1Managed ();
150-
151-
stream.Seek (0, SeekOrigin.Begin);
152-
var buffer = new byte [buffer_size];
153-
154-
using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write))
155-
CopyStreamChunk (stream, crypto_stream, buffer, (int) stream.Length);
156-
return sha1.Hash;
157-
}
158-
159-
static unsafe Guid ComputeGuid (Stream stream)
160-
{
161-
byte[] hashCode = ComputeHash (stream);
162-
163-
// From corefx/src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobContentId.cs
164-
Guid guid = default(Guid);
165-
byte* guidPtr = (byte*)&guid;
166-
for (var i = 0; i < 16; i++) {
167-
guidPtr[i] = hashCode[i];
168-
}
169-
// modify the guid data so it decodes to the form of a "random" guid ala rfc4122
170-
guidPtr[7] = (byte)((guidPtr[7] & 0x0f) | (4 << 4));
171-
guidPtr[8] = (byte)((guidPtr[8] & 0x3f) | (2 << 6));
172-
173-
return guid;
174-
}
175-
176136
static void BuildMetadata (ModuleDefinition module, MetadataBuilder metadata)
177137
{
178138
if (!module.HasImage) {
@@ -1559,12 +1519,20 @@ void AddConstraints (GenericParameter generic_parameter, GenericParamConstraintT
15591519
{
15601520
var constraints = generic_parameter.Constraints;
15611521

1562-
var rid = generic_parameter.token.RID;
1522+
var gp_rid = generic_parameter.token.RID;
1523+
1524+
for (int i = 0; i < constraints.Count; i++) {
1525+
var constraint = constraints [i];
1526+
1527+
var rid = table.AddRow (new GenericParamConstraintRow (
1528+
gp_rid,
1529+
MakeCodedRID (GetTypeToken (constraint.ConstraintType), CodedIndex.TypeDefOrRef)));
1530+
1531+
constraint.token = new MetadataToken (TokenType.GenericParamConstraint, rid);
15631532

1564-
for (int i = 0; i < constraints.Count; i++)
1565-
table.AddRow (new GenericParamConstraintRow (
1566-
rid,
1567-
MakeCodedRID (GetTypeToken (constraints [i]), CodedIndex.TypeDefOrRef)));
1533+
if (constraint.HasCustomAttributes)
1534+
AddCustomAttributes (constraint);
1535+
}
15681536
}
15691537

15701538
void AddInterfaces (TypeDefinition type)
@@ -2690,6 +2658,25 @@ void AddSequencePoints (MethodDebugInformation info)
26902658

26912659
method_debug_information_table.rows [rid - 1].Col2 = GetBlobIndex (signature);
26922660
}
2661+
2662+
public void ComputeDeterministicMvid ()
2663+
{
2664+
var guid = CryptoService.ComputeGuid (CryptoService.ComputeHash (
2665+
data,
2666+
resources,
2667+
string_heap,
2668+
user_string_heap,
2669+
blob_heap,
2670+
table_heap,
2671+
code));
2672+
2673+
var position = guid_heap.position;
2674+
guid_heap.position = 0;
2675+
guid_heap.WriteBytes (guid.ToByteArray ());
2676+
guid_heap.position = position;
2677+
2678+
module.Mvid = guid;
2679+
}
26932680
}
26942681

26952682
sealed class SignatureWriter : ByteBuffer {

Mono.Cecil/GenericParameter.cs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
2323
internal IGenericParameterProvider owner;
2424

2525
ushort attributes;
26-
Collection<TypeReference> constraints;
26+
GenericParameterConstraintCollection constraints;
2727
Collection<CustomAttribute> custom_attributes;
2828

2929
public GenericParameterAttributes Attributes {
@@ -52,15 +52,15 @@ public bool HasConstraints {
5252
}
5353
}
5454

55-
public Collection<TypeReference> Constraints {
55+
public Collection<GenericParameterConstraint> Constraints {
5656
get {
5757
if (constraints != null)
5858
return constraints;
5959

6060
if (HasImage)
6161
return Module.Read (ref constraints, this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
6262

63-
return constraints = new Collection<TypeReference> ();
63+
return constraints = new GenericParameterConstraintCollection (this);
6464
}
6565
}
6666

@@ -265,4 +265,94 @@ protected override void OnRemove (GenericParameter item, int index)
265265
items[i].position = i - 1;
266266
}
267267
}
268+
269+
public sealed class GenericParameterConstraint : ICustomAttributeProvider {
270+
271+
internal GenericParameter generic_parameter;
272+
internal MetadataToken token;
273+
274+
TypeReference constraint_type;
275+
Collection<CustomAttribute> custom_attributes;
276+
277+
public TypeReference ConstraintType {
278+
get { return constraint_type; }
279+
set { constraint_type = value; }
280+
}
281+
282+
public bool HasCustomAttributes {
283+
get {
284+
if (custom_attributes != null)
285+
return custom_attributes.Count > 0;
286+
287+
if (generic_parameter == null)
288+
return false;
289+
290+
return this.GetHasCustomAttributes (generic_parameter.Module);
291+
}
292+
}
293+
294+
public Collection<CustomAttribute> CustomAttributes {
295+
get {
296+
if (generic_parameter == null)
297+
return custom_attributes = new Collection<CustomAttribute> ();
298+
299+
return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, generic_parameter.Module));
300+
}
301+
}
302+
303+
public MetadataToken MetadataToken {
304+
get { return token; }
305+
set { token = value; }
306+
}
307+
308+
internal GenericParameterConstraint (TypeReference constraintType, MetadataToken token)
309+
{
310+
this.constraint_type = constraintType;
311+
this.token = token;
312+
}
313+
314+
public GenericParameterConstraint (TypeReference constraintType)
315+
{
316+
Mixin.CheckType (constraintType, Mixin.Argument.constraintType);
317+
318+
this.constraint_type = constraintType;
319+
this.token = new MetadataToken (TokenType.GenericParamConstraint);
320+
}
321+
}
322+
323+
class GenericParameterConstraintCollection : Collection<GenericParameterConstraint>
324+
{
325+
readonly GenericParameter generic_parameter;
326+
327+
internal GenericParameterConstraintCollection (GenericParameter genericParameter)
328+
{
329+
this.generic_parameter = genericParameter;
330+
}
331+
332+
internal GenericParameterConstraintCollection (GenericParameter genericParameter, int length)
333+
: base (length)
334+
{
335+
this.generic_parameter = genericParameter;
336+
}
337+
338+
protected override void OnAdd (GenericParameterConstraint item, int index)
339+
{
340+
item.generic_parameter = generic_parameter;
341+
}
342+
343+
protected override void OnInsert (GenericParameterConstraint item, int index)
344+
{
345+
item.generic_parameter = generic_parameter;
346+
}
347+
348+
protected override void OnSet (GenericParameterConstraint item, int index)
349+
{
350+
item.generic_parameter = generic_parameter;
351+
}
352+
353+
protected override void OnRemove (GenericParameterConstraint item, int index)
354+
{
355+
item.generic_parameter = null;
356+
}
357+
}
268358
}

0 commit comments

Comments
 (0)