Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException when md rva isn't alignment #543

Open
CreateAndInject opened this issue Feb 3, 2024 · 5 comments
Open

InvalidOperationException when md rva isn't alignment #543

CreateAndInject opened this issue Feb 3, 2024 · 5 comments

Comments

@CreateAndInject
Copy link
Contributor

public void SetOffset(FileOffset offset, RVA rva) {
// This method can be called twice by NativeModuleWriter. It needs to know the size
// of the final metadata. If it fits in the old location, the new MD will be written
// there (smaller file size). If the new MD doesn't fit in the old location, this
// method gets called a second time with the updated offset + rva.
bool initAll = this.offset == 0;
this.offset = offset;
this.rva = rva;
if (initAll) {
// #Strings heap is initialized in OnBeforeSetOffset()
blobHeap.SetReadOnly();
guidHeap.SetReadOnly();
tablesHeap.SetReadOnly();
pdbHeap.SetReadOnly();
tablesHeap.BigStrings = stringsHeap.IsBig;
tablesHeap.BigBlob = blobHeap.IsBig;
tablesHeap.BigGuid = guidHeap.IsBig;
metadataHeader.Heaps = GetHeaps();
}
metadataHeader.SetOffset(offset, rva);
uint len = metadataHeader.GetFileLength();
offset += len;
rva += len;
foreach (var heap in metadataHeader.Heaps) {
offset = offset.AlignUp(HEAP_ALIGNMENT);
rva = rva.AlignUp(HEAP_ALIGNMENT);
heap.SetOffset(offset, rva);
len = heap.GetFileLength();
offset += len;
rva += len;
}
Debug.Assert(initAll || length == rva - this.rva);
if (!(initAll || length == rva - this.rva))
throw new InvalidOperationException();
length = rva - this.rva;
if (!isStandaloneDebugMetadata && initAll)
UpdateMethodAndFieldRvas();
}

this?

		public void SetOffset(FileOffset offset, RVA rva) {
			uint rem = (uint)rva % HEAP_ALIGNMENT;
                        ......
			foreach (var heap in metadataHeader.Heaps) {
				offset = (offset - rem).AlignUp(HEAP_ALIGNMENT) + rem;
				rva = (rva - rem).AlignUp(HEAP_ALIGNMENT) + rem;
				...
			}
@wtfsck
Copy link
Contributor

wtfsck commented Feb 7, 2024

Why isn't it aligned?

@CreateAndInject
Copy link
Contributor Author

CreateAndInject commented Feb 8, 2024

test.zip

var module = ModuleDefMD.Load(@"test.exe");
module.NativeWrite("output.exe"); // InvalidOperationException 

@CreateAndInject
Copy link
Contributor Author

What's wrong?

@wtfsck
Copy link
Contributor

wtfsck commented Feb 24, 2024

I haven't looked at this yet, but if you know how to fix this, you can send a PR.

@CreateAndInject
Copy link
Contributor Author

No idea if alignment is requirement and how the alignment should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants