This is an archive of the discontinued LLVM Phabricator instance.

Fill the last page of each executable section with 0xcc or equivalent.
AcceptedPublic

Authored by ruiu on Mar 27 2018, 9:56 AM.

Details

Summary

I think this patch is better than https://reviews.llvm.org/D44775.

Event Timeline

ruiu created this revision.Mar 27 2018, 9:56 AM
grimar added inline comments.
lld/ELF/Writer.cpp
2210

Does it really make sense to optimize here? If so, then probably my patch was better,
since it writes fewer bytes and is more straightforward.
If no (and I expect it is the case), then I would remove the check to simplify the code,
writing few more pages should not be slow.

2212

I do not think it is the correct assertion.
memset has int value argument, though the description is:
`The value is passed as an int, but the function fills the block of memory
using the unsigned char conversion of this value.`

So I think what you want to check here that all bytes of TrapInstr are the same.
Btw, that is true for all targets atm. What about landing D44979 then and removing the assert?

ruiu added inline comments.Mar 28 2018, 9:17 AM
lld/ELF/Writer.cpp
2210

Writing to a single page is extremely fast, so no. And your patch is not straightforward because the code is more complicated. The point of this patch is that you don't need to iterate over Phdrs. You should just iterate over OutputSections.

ruiu updated this revision to Diff 140102.Mar 28 2018, 10:47 AM
  • do not use memset
espindola added inline comments.Mar 28 2018, 7:46 PM
lld/ELF/Writer.cpp
2211

Iterating over OutputSections seems like a good idea, but couldn't this fill just the area between two sections?

ruiu added inline comments.Mar 28 2018, 7:52 PM
lld/ELF/Writer.cpp
2211

We can. The reason why I'm doing this is because this way, trap instructions are always 4 byte aligned even if the last segment is not 4 byte aligned. But I can fix it some way.

espindola added inline comments.Mar 28 2018, 8:37 PM
lld/ELF/Writer.cpp
2211

Interesting. We have the same latent bug in OutputSection::writeTo. I guess we have to add nop for ppc (where not all bytes are the same) to test a fix.

grimar accepted this revision.Mar 29 2018, 1:50 AM

LGTM + nit/suggestion.

lld/ELF/Writer.cpp
2209

So, should this check be removed for simplicity of code?
(basing on comments, it makes no sense to have it, right?)

Then, btw, code could be:

for (OutputSection *Sec : OutputSections)
  if (Sec->PtLoad && Sec->PtLoad == PT_LOAD && (Sec->PtLoad->p_flags & PF_X))
    fillTrap(Buf + alignDown(Sec->Offset + Sec->Size, Target->PageSize));
This revision is now accepted and ready to land.Mar 29 2018, 1:50 AM
ruiu added inline comments.Mar 29 2018, 1:00 PM
lld/ELF/Writer.cpp
2209

No it can't. If a section ends exactly at a page boundary, no padding is needed.

2211

I thought about how to fill only gaps between sections, but there seems no easy way of doing it. It's doable , but it's perhaps not worth writing more code for it. So I think I'd prefer the current code.

grimar added inline comments.Mar 30 2018, 1:02 AM
lld/ELF/Writer.cpp
2209

Ah, I see now, it would fill the next page with traps without this check.

This has been accepted for quite some time, any reason why we're not landing it?

The bug is now #36201.

Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: MaskRay. · View Herald Transcript