Skip to content

Commit

Permalink
Shouldn't call SetOffset if chunk can't be reused
Browse files Browse the repository at this point in the history
  • Loading branch information
CreateAndInject committed Dec 31, 2023
1 parent 9a7c6b2 commit 6d2d621
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/DotNet/Writer/NativeModuleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public sealed class NativeModuleWriter : ModuleWriterBase {
List<OrigSection> origSections;

readonly struct ReusedChunkInfo {
public PESection Section { get; }
public IReuseChunk Chunk { get; }
public RVA RVA { get; }
public ReusedChunkInfo(IReuseChunk chunk, RVA rva) {
public ReusedChunkInfo(PESection section, IReuseChunk chunk, RVA rva) {
Section = section;
Chunk = chunk;
RVA = rva;
}
Expand Down Expand Up @@ -354,9 +356,7 @@ void ReuseIfPossible(PESection section, IReuseChunk chunk, RVA origRva, uint ori
if (((uint)origRva & (requiredAlignment - 1)) != 0)
return;

if (section.Remove(chunk) is null)
throw new InvalidOperationException();
reusedChunks.Add(new ReusedChunkInfo(chunk, origRva));
reusedChunks.Add(new ReusedChunkInfo(section,chunk, origRva));
}

FileOffset GetNewFileOffset(RVA rva) {
Expand Down Expand Up @@ -412,6 +412,15 @@ long WriteFile() {
ReuseIfPossible(textSection, debugDirectory, debugDataDir.VirtualAddress, realDebugDirSize, DebugDirectory.DEFAULT_DEBUGDIRECTORY_ALIGNMENT);
}

if (Overlaps())
reusedChunks.Clear();
else {
foreach (var info in reusedChunks) {
if (info.Section.Remove(info.Chunk) is null)
throw new InvalidOperationException();
}
}

if (constants.IsEmpty)
textSection.Remove(constants);
if (netResources.IsEmpty)
Expand Down Expand Up @@ -451,8 +460,6 @@ long WriteFile() {
var offset = GetNewFileOffset(info.RVA);
info.Chunk.SetOffset(offset, info.RVA);
}
if (Overlaps())
reusedChunks.Clear();
}
metadata.UpdateMethodAndFieldRvas();
foreach (var section in origSections) {
Expand Down Expand Up @@ -845,11 +852,10 @@ bool GetEntryPoint(out uint ep) {

bool Overlaps() {
for (int i = 0; i < reusedChunks.Count; i++) {
uint start = (uint)reusedChunks[i].Chunk.RVA;
uint start = (uint)reusedChunks[i].RVA;
uint end = start + reusedChunks[i].Chunk.GetFileLength();
for (int j = 0; j < reusedChunks.Count; j++) {
var chunk = reusedChunks[j].Chunk;
if (i != j && start < (uint)chunk.RVA + chunk.GetFileLength() && end > (uint)chunk.RVA)
if (i != j && start < (uint)reusedChunks[j].RVA + reusedChunks[j].Chunk.GetFileLength() && end > (uint)reusedChunks[j].RVA)
return true;
}
}
Expand Down

0 comments on commit 6d2d621

Please sign in to comment.