This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] Do not use the alignment-rounded-up size when using the secondary
ClosedPublic

Authored by cryptoad on Dec 5 2016, 1:59 PM.

Details

Summary

The combined allocator rounds up the requested size with regard to the
alignment, which makes sense when being serviced by the primary as it comes
with alignment guarantees, but not with the secondary. For the rare case of
large alignments, it wastes memory, and entices unnecessarily large fields for
the Scudo header. With this patch, we pass the non-alignement-rounded-up size
to the secondary, and adapt the Scudo code for this change.

Diff Detail

Repository
rL LLVM

Event Timeline

cryptoad updated this revision to Diff 80325.Dec 5 2016, 1:59 PM
cryptoad retitled this revision from to [sanitizer] Do not use the alignment-rounded-up size when using the secondary.
cryptoad updated this object.
cryptoad updated this revision to Diff 80326.Dec 5 2016, 2:03 PM

Cleaner patch for the combined allocator.

cryptoad added a subscriber: llvm-commits.
alekseyshl added inline comments.Dec 6 2016, 9:31 AM
lib/sanitizer_common/sanitizer_allocator_combined.h
62 ↗(On Diff #80326)

Is it safe to run this CHECK now, when secondary is using non-adjusted allocation size?

64 ↗(On Diff #80326)

Same here, it tries to zero out the result up to the rounded up size, not the original size.

cryptoad marked 2 inline comments as done.Dec 6 2016, 9:45 AM
cryptoad added inline comments.
lib/sanitizer_common/sanitizer_allocator_combined.h
62 ↗(On Diff #80326)

sanitizer_allocator_secondary.h adds alignment to the size prior to allocation and align the result properly.
It also does a similar CHECK there.
Same with the Scudo allocator, but the job is split between the frontend and the secondary.
So the CHECK is still safe.

64 ↗(On Diff #80326)

If the allocation comes from the secondary, it's mmap backed and it's zero'd out, and we don't need to clear the memory again.
We need to zero out only when it comes from the primary, which uses the rounded size rather than the original size for its allocation.

alekseyshl added inline comments.Dec 6 2016, 10:43 AM
lib/sanitizer_common/sanitizer_allocator_combined.h
62 ↗(On Diff #80326)

Understood, but this check feels a bit arbitrary now (to me, that is). Before your change it matched well with the similar if statement adjusting 'size', now it is puzzling why, while both allocators accept the alignment as a parameter, the resulting pointer is expected to be aligned only when alignment is greater than 8?
I bet there's a reason, but it's not clear from the code, which just become more obscure.

64 ↗(On Diff #80326)

Ah, right, there's '&& from_primary' check... Then it seems to make sense to push 'cleared' flag down to allocators, since the particular allocator knows better the current state of the memory block. Anyway, not for this patch.

cryptoad marked 2 inline comments as done.Dec 6 2016, 11:00 AM
cryptoad added inline comments.
lib/sanitizer_common/sanitizer_allocator_combined.h
62 ↗(On Diff #80326)

I agree it's a bit obscure, all the more since the primary doesn't take the alignment as parameter, but is expected to produced a 2^x aligned chunked when requested 2^x bytes (https://github.com/llvm-mirror/compiler-rt/blob/master/lib/sanitizer_common/sanitizer_allocator_combined.h#L20).
It sort of messes up the logic as the secondary is being passed the alignment as a parameter, and it actually matters for the secondary.
I am opened to suggestion as to how it would make it clearer for you.

alekseyshl edited edge metadata.Dec 6 2016, 11:19 AM

LGTM

lib/sanitizer_common/sanitizer_allocator_combined.h
62 ↗(On Diff #80326)

Well, in the context of this patch, not much. Maybe a comment why if this is enforced only when alignment > 8?

And primary allocator does care about alignment, although indirectly, in CanAllocate call.

cryptoad updated this revision to Diff 80455.Dec 6 2016, 11:36 AM
cryptoad marked 2 inline comments as done.
cryptoad edited edge metadata.

Addressing Aleksey's comments.

cryptoad marked 4 inline comments as done.Dec 6 2016, 11:40 AM

Added comments to the combined allocator Allocate to make clearer the expected behavior.

kcc accepted this revision.Dec 8 2016, 9:40 AM
kcc edited edge metadata.

LGTM

This revision is now accepted and ready to land.Dec 8 2016, 9:40 AM
cryptoad updated this revision to Diff 80788.Dec 8 2016, 11:08 AM
cryptoad edited edge metadata.

Wording changes in the comments.

cryptoad closed this revision.Dec 8 2016, 11:15 AM
This revision was automatically updated to reflect the committed changes.