Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-lld-coff @llvm/pr-subscribers-lld Author: Petr Beneš (wbenny) ChangesWhen merging This caused a crash on Windows with The fix bounds all memset operations to Fixes #180406 Full diff: https://github.com/llvm/llvm-project/pull/180411.diff 1 Files Affected:
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 559bd387fa9cb..a5e30e26b9e5b 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2619,12 +2619,17 @@ void Writer::writeSections() {
if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
(ctx.config.machine == AMD64 || ctx.config.machine == I386)) {
uint32_t prevEnd = 0;
+ uint32_t rawSize = sec->getRawSize();
for (Chunk *c : sec->chunks) {
uint32_t off = c->getRVA() - sec->getRVA();
+ // Chunks without data (e.g., .bss) have virtual addresses beyond
+ // rawSize; stop filling when we reach the end of raw data.
+ if (off >= rawSize)
+ break;
memset(secBuf + prevEnd, 0xCC, off - prevEnd);
- prevEnd = off + c->getSize();
+ prevEnd = std::min(off + static_cast<uint32_t>(c->getSize()), rawSize);
}
- memset(secBuf + prevEnd, 0xCC, sec->getRawSize() - prevEnd);
+ memset(secBuf + prevEnd, 0xCC, rawSize - prevEnd);
}
parallelForEach(sec->chunks, [&](Chunk *c) {
|
|
This fix looks good, thanks. Are you able to craft a testcase please? |
|
Amended the commit with a test case and verified it doesn't pass without the fix and passes with it. If I understand correctly, the |
| # RUN: llvm-readobj --sections %t.exe | FileCheck %s | ||
|
|
||
| # CHECK: Name: .text | ||
| # CHECK-NEXT: VirtualSize: 0x |
There was a problem hiding this comment.
nit: I guess we can write the virtual size that we're expecting here: 0x104
| uint32_t off = c->getRVA() - sec->getRVA(); | ||
| // Chunks without data (e.g., .bss) have virtual addresses beyond | ||
| // rawSize; stop filling when we reach the end of raw data. | ||
| if (off >= rawSize) |
There was a problem hiding this comment.
I am wondering if this check could prevent from filling the gap between the last .text chunk and the .bss, if the file alignement is larger than the last .text chunk?
There was a problem hiding this comment.
No, the memset after the loop handles that.
There was a problem hiding this comment.
this exposed a new bug I guess:
clang_release_ble/DM40Native.exe //20.1.8
FileSize=55550 EP=0xd8b5 SizeOfImage=0xd8fe SizeOfHeaders=0x198
[.data] VA=0x198 VSize=0xd766 RawOff=0x198 RawSz=0xd766 Chars=0xe0000020 VSize-RawSz=0
clang_release_ble/DM40Native_broken.exe //trunk
FileSize=55710 EP=0xd949 SizeOfImage=0xd9a4 SizeOfHeaders=0x198
[.data] VA=0x198 VSize=0xd80c RawOff=0x198 RawSz=0xd806 Chars=0xe0000020 VSize-RawSz=6
clang_release_bt/DM40Native.exe //20.1.8
FileSize=52542 EP=0xccc2 SizeOfImage=0xcd3e SizeOfHeaders=0x198
[.data] VA=0x198 VSize=0xcba6 RawOff=0x198 RawSz=0xcba6 Chars=0xe0000020 VSize-RawSz=0
clang_release_bt/DM40Native_broken.exe //trunk
FileSize=52718 EP=0xcd6a SizeOfImage=0xcdf4 SizeOfHeaders=0x198
[.data] VA=0x198 VSize=0xcc5c RawOff=0x198 RawSz=0xcc56 Chars=0xe0000020 VSize-RawSz=6
PE loader rejects the exe
seems to be due to:
if (c->hasData)
rawSize = alignTo(virtualSize, config->fileAlign);since bss is not handled in this block it gets excluded by this, locally for me this fixed it but this probably isn't the correct solution since filealign 2 and 4 would still probably be outside the rawSize:
if (config->fileAlign == 1 && rawSize < virtualSize)
rawSize = virtualSize;so maybe something like this would be better:
if (rawSize < alignTo(virtualSize, config->fileAlign))
rawSize = alignTo(virtualSize, config->fileAlign);There was a problem hiding this comment.
Are you sure that this issue that you're observing is related to this commit? As far as I can see, this change shouldn't affect the final layout of the PE image (which can cause the image to be rejected), only what is filled in it.
Can you file a separate issue about the problem you're seeing, so we can try to track it down? Ideally, can you provider a reproducer? If you add -reproduce:repro.tar to the link command, it'll produce a file repro.tar which contains everything required for rerunning the link command. That would allow others to pinpoint exactly which LLD change is causing the PE loader to reject it. (If linking through the compiler driver in mingw mode, pass -Wl,--reproduce=repro.tar instead.)
|
@wbenny Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Can we backport this to release/22? |
|
/cherry-pick 6558595 |
We can try; we're already kinda far in the 22 release series, so the release managers are getting more selective in what fixes they approve for backports, but we can see what they say about this. |
|
/pull-request #198212 |
When merging
.bssinto a code section (e.g.,/MERGE:.bss=.text), the INT3 gap-filling loop inwriteSections()would write past the output buffer. This happens because.bsschunks havehasData=false, so they contribute toVirtualSizebut notSizeOfRawData. The loop was using chunk RVAs without checking if they exceeded the raw data region.This caused a crash on Windows with
/FILEALIGN:1(access violation 0xC0000005). The tight alignment leaves no slack in the mapped buffer, so the overflow immediately hits unmapped memory.The fix bounds all memset operations to
rawSizeand exits early when encountering chunks beyond the raw data boundary.Fixes #180406