[AllocToken, Clang] Infer type hints from sizeof expressions and casts #156841
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For the AllocToken pass to accurately calculate token ID hints, we
should attach
!alloc_token_hint
metadata for allocation calls to avoidreverting to LLVM IR-type based hints (which depends on later "uses" and
is rather imprecise).
Unlike new expressions, untyped allocation calls (like
malloc
,calloc
,::operator new(..)
,__builtin_operator_new
, etc.) have nosyntactic type associated with them. For -fsanitize=alloc-token, type
hints are sufficient, and we can attempt to infer the type based on
common idioms.
When encountering allocation calls (with
__attribute__((malloc))
or__attribute__((alloc_size(..))
), attach!alloc_token_hint
byinferring the allocated type from (a) sizeof argument expressions such
as
malloc(sizeof(MyType))
, and (b) casts such as(MyType*)malloc(4096)
.Note that non-standard allocation functions with these attributes are
not instrumented by default. Use
-fsanitize-alloc-token-extended
toinstrument them as well.
Link: https://discourse.llvm.org/t/rfc-a-framework-for-allocator-partitioning-hints/87434
This change is part of the following series: