Skip to content

Commit 1e9bf55

Browse files
committed
Add PE verification
1 parent 105cafe commit 1e9bf55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+647
-272
lines changed

src/LibObjectFile.Tests/PE/PEReaderTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task TestPrinter(string name)
3636
await Verifier.Verify(afterReadText).UseParameters(name);
3737

3838
// Update the layout
39-
var diagnostics = new DiagnosticBag();
39+
var diagnostics = new DiagnosticBag() { EnableStackTrace = true };
4040
peImage.UpdateLayout(diagnostics);
4141

4242
var afterUpdateWriter = new StringWriter();
@@ -56,7 +56,7 @@ public async Task TestPrinter(string name)
5656

5757
// Write the PE back to a byte buffer
5858
var output = new MemoryStream();
59-
peImage.Write(output);
59+
peImage.Write(output, new PEImageWriterOptions() { EnableStackTrace = true });
6060
output.Position = 0;
6161
byte[] outputBuffer = output.ToArray();
6262

@@ -142,7 +142,7 @@ public void TestCreatePE()
142142
// Write the PE to a file
143143
// ***************************************************************************
144144
var output = new MemoryStream();
145-
pe.Write(output);
145+
pe.Write(output, new() { EnableStackTrace = true });
146146
output.Position = 0;
147147

148148
var sourceFile = Path.Combine(AppContext.BaseDirectory, "PE", "generated_win64.exe");
@@ -176,7 +176,10 @@ public async Task TestWindows(string sourceFile)
176176
var inputBuffer = new byte[stream.Length];
177177
stream.ReadExactly(inputBuffer);
178178

179-
peImage.UpdateLayout(new DiagnosticBag());
179+
peImage.UpdateLayout(new DiagnosticBag()
180+
{
181+
EnableStackTrace = true
182+
});
180183

181184
var newSizeOfInitializedData = peImage.OptionalHeader.SizeOfInitializedData;
182185

@@ -188,7 +191,7 @@ public async Task TestWindows(string sourceFile)
188191

189192
// Write the PE back to a byte buffer
190193
var output = new MemoryStream();
191-
peImage.Write(output);
194+
peImage.Write(output, new PEImageWriterOptions() { EnableStackTrace = true });
192195
output.Position = 0;
193196
var outputBuffer = output.ToArray();
194197

src/LibObjectFile/Diagnostics/DiagnosticId.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public enum DiagnosticId
136136
PE_ERR_InvalidBaseOfCode = 3018,
137137
PE_ERR_InvalidAddressOfEntryPoint = 3019,
138138
PE_ERR_DirectoryWithSameKindAlreadyAdded = 3020,
139+
PE_ERR_VerifyContextInvalidObject = 3021,
139140

140141
// PE Exception directory
141142
PE_ERR_InvalidExceptionDirectory_Entries = 3100,
@@ -175,7 +176,9 @@ public enum DiagnosticId
175176
PE_ERR_InvalidDataDirectorySection = 3703,
176177
PE_ERR_BaseRelocationDirectoryInvalidVirtualAddress = 3704,
177178
PE_ERR_BaseRelocationDirectoryInvalidSizeOfBlock = 3705,
178-
179+
PE_ERR_InvalidBaseRelocationBlock = 3706,
180+
PE_ERR_BaseRelocationDirectoryInvalidSectionLink = 3707,
181+
179182
// PE Import
180183
PE_ERR_ImportDirectoryInvalidEndOfStream = 3800,
181184
PE_ERR_ImportLookupTableInvalidEndOfStream = 3801,

src/LibObjectFile/Dwarf/DwarfAbbreviation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -206,7 +206,7 @@ public override void Write(DwarfWriter writer)
206206
Debug.Assert(writer.Position - startOffset == Size);
207207
}
208208

209-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
209+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
210210
{
211211
var endOffset = Position;
212212

@@ -216,7 +216,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
216216
{
217217
var item = itemPair.Value;
218218
item.Position = endOffset;
219-
item.UpdateLayout(layoutContext);
219+
item.UpdateLayout(context);
220220
endOffset += item.Size;
221221
}
222222

@@ -228,7 +228,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
228228
foreach (var item in _items)
229229
{
230230
item.Position = endOffset;
231-
item.UpdateLayout(layoutContext);
231+
item.UpdateLayout(context);
232232
endOffset += item.Size;
233233
}
234234
}

src/LibObjectFile/Dwarf/DwarfAbbreviationItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -30,7 +30,7 @@ internal DwarfAbbreviationItem(ulong code, DwarfTagEx tag, bool hasChildren, Dwa
3030

3131
public DwarfAttributeDescriptors Descriptors { get; private set; }
3232

33-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
33+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
3434
{
3535
var endOffset = Position;
3636

src/LibObjectFile/Dwarf/DwarfAbbreviationTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -28,13 +28,13 @@ internal void Reset()
2828
_abbreviations.Clear();
2929
}
3030

31-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
31+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
3232
{
3333
ulong endOffset = Position;
3434
foreach (var abbreviation in _abbreviations)
3535
{
3636
abbreviation.Position = endOffset;
37-
abbreviation.UpdateLayout(layoutContext);
37+
abbreviation.UpdateLayout(context);
3838
endOffset += abbreviation.Size;
3939
}
4040

src/LibObjectFile/Dwarf/DwarfAddressRangeTable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -139,7 +139,7 @@ public override void Verify(DwarfVerifyContext context)
139139
}
140140
}
141141

142-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
142+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
143143
{
144144
ulong sizeOf = 0;
145145
// unit_length

src/LibObjectFile/Dwarf/DwarfAttribute.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -130,10 +130,10 @@ protected override bool PrintMembers(StringBuilder builder)
130130
return true;
131131
}
132132

133-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
133+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
134134
{
135-
var addressSize = layoutContext.CurrentUnit!.AddressSize;
136-
var is64BitEncoding = layoutContext.CurrentUnit.Is64BitEncoding;
135+
var addressSize = context.CurrentUnit!.AddressSize;
136+
var is64BitEncoding = context.CurrentUnit.Is64BitEncoding;
137137

138138
var endOffset = Position;
139139
switch (Form.Value)
@@ -214,7 +214,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
214214
case DwarfAttributeForm.RefUdata:
215215
{
216216
var dieRef = (DwarfDIE)ValueAsObject!;
217-
endOffset += DwarfHelper.SizeOfULEB128(dieRef.Position - layoutContext.CurrentUnit.Position); // WriteULEB128((dieRef.Offset - _currentUnit.Offset));
217+
endOffset += DwarfHelper.SizeOfULEB128(dieRef.Position - context.CurrentUnit.Position); // WriteULEB128((dieRef.Offset - _currentUnit.Offset));
218218
break;
219219
}
220220

@@ -239,7 +239,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
239239
case DwarfAttributeForm.Exprloc:
240240
var expr = (DwarfExpression)ValueAsObject!;
241241
expr.Position = endOffset;
242-
expr.UpdateLayout(layoutContext);
242+
expr.UpdateLayout(context);
243243
endOffset += expr.Size;
244244
break;
245245

src/LibObjectFile/Dwarf/DwarfDIE.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -296,7 +296,7 @@ protected unsafe void SetAttributeValueOpt<TValue>(DwarfAttributeKind kind, TVal
296296
}
297297
}
298298

299-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
299+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
300300
{
301301
var abbrev = Abbrev;
302302

@@ -310,7 +310,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
310310
foreach (var attr in _attributes)
311311
{
312312
attr.Position = endOffset;
313-
attr.UpdateLayout(layoutContext);
313+
attr.UpdateLayout(context);
314314
endOffset += attr.Size;
315315
}
316316

@@ -319,7 +319,7 @@ protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
319319
foreach (var child in _children)
320320
{
321321
child.Position = endOffset;
322-
child.UpdateLayout(layoutContext);
322+
child.UpdateLayout(context);
323323
endOffset += child.Size;
324324
}
325325

src/LibObjectFile/Dwarf/DwarfExpression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -94,9 +94,9 @@ internal void UpdateLayout(DwarfLayoutContext layoutContext, bool inLocationSect
9494
Size = OperationLengthInBytes + deltaLength;
9595
}
9696

97-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
97+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
9898
{
99-
UpdateLayout(layoutContext, inLocationSection: false);
99+
UpdateLayout(context, inLocationSection: false);
100100
}
101101

102102
public override void Read(DwarfReader reader)

src/LibObjectFile/Dwarf/DwarfFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

@@ -448,7 +448,7 @@ private static void CheckErrors(DiagnosticBag diagnostics)
448448
}
449449
}
450450

451-
protected override void UpdateLayoutCore(DwarfLayoutContext layoutContext)
451+
protected override void UpdateLayoutCore(DwarfLayoutContext context)
452452
{
453453
}
454454

0 commit comments

Comments
 (0)