This is an archive of the discontinued LLVM Phabricator instance.

[Sanitizers] ASan: detect new/delete calls with mismatched alignment.
ClosedPublic

Authored by alekseyshl on Oct 4 2017, 9:41 PM.

Details

Summary

ASan allocator stores the requested alignment for new and new[] calls
and on delete and delete[] verifies that alignments do match.

The representable alignments are: default alignment, 8, 16, 32, 64, 128,
256 and 512 bytes. Alignments > 512 are stored as 512, hence two
different alignments > 512 will pass the check (possibly masking the bug),
but limited memory requirements deemed to be a resonable tradeoff for
relaxed conditions.

The feature is controlled by new_delete_type_mismatch flag, the same one
protecting new/delete matching size check.

Issue: https://github.com/google/sanitizers/issues/799

Diff Detail

Repository
rL LLVM

Event Timeline

alekseyshl created this revision.Oct 4 2017, 9:41 PM
eugenis edited edge metadata.Oct 23 2017, 3:07 PM

What exactly is "default alignment" and what happens if it is equal to 8? Will it be treated as incompatible with 8? What happens if once source file is built with aligned-delete support, and the other - without (-std=c++14)? Looks like new() from the former would not be compatible with delete() from the latter.

What exactly is "default alignment" and what happens if it is equal to 8? Will it be treated as incompatible with 8?

The wording is derived from the standard, default-aligned vs over-aligned memory. malloc/etc allocate default-aligned memory, posix_memalign allocate over-aligned memory.
The standard says that new/delete pairs must match in regard of the alignement and default- and over-aligned memory must not be mixed.
There's STDCPP_DEFAULT_NEW_ALIGNMENT defined and alignments > it will be passed explicitly, otherwise it's implicit, that is, if it happens to be equal to 8, it' supposed to be treated as default.

What happens if once source file is built with aligned-delete support, and the other - without (-std=c++14)? Looks like new() from the former would not be compatible with delete() from the latter.

Well, the standard says that new/delete pairs must match, so you're correct, they will be incompatible. Using their example, imagine aligned new uses a separate arena for allocation, how would alignment unaware delete be able to free that memory?

eugenis accepted this revision.Oct 24 2017, 1:29 PM

OK, sounds good.

This revision is now accepted and ready to land.Oct 24 2017, 1:29 PM
This revision was automatically updated to reflect the committed changes.