-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Wire up -fsanitize=alloc-token #156839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
melver
wants to merge
38
commits into
main
Choose a base branch
from
users/melver/spr/clang-introduce-fsanitizealloc-token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 26 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d97ba0f
[𝘀𝗽𝗿] initial version
melver ec80a6b
[𝘀𝗽𝗿] changes to main this commit is based on
melver b365333
fixup! Insert AllocToken into index.rst
melver 7f1cbf9
[𝘀𝗽𝗿] changes introduced through rebase
melver 08d0fad
fixup! Switch to fixed MD
melver ca7e255
[𝘀𝗽𝗿] changes introduced through rebase
melver 3c760b5
fixup! address reviewer comments
melver 3071ee6
[𝘀𝗽𝗿] changes introduced through rebase
melver 31ca802
fixup! address reviewer comments round 2
melver 6e4c2cb
[𝘀𝗽𝗿] changes introduced through rebase
melver 276d084
fixup! use update_test_checks.py for opt tests
melver 0172860
[𝘀𝗽𝗿] changes introduced through rebase
melver b56084e
fixup! do not strip _
melver cc62d76
[𝘀𝗽𝗿] changes introduced through rebase
melver 5af7e7c
fixup! address some comments
melver 110cef2
[𝘀𝗽𝗿] changes introduced through rebase
melver 7510ebf
fixup! address more comments
melver 4ff2136
[𝘀𝗽𝗿] changes introduced through rebase
melver 5dd8067
rebase
melver 9c8454d
[𝘀𝗽𝗿] changes introduced through rebase
melver 76d0c51
fixup! address comments
melver fb43ef1
[𝘀𝗽𝗿] changes introduced through rebase
melver 0c44a0a
fixup!
melver 8dec1a6
fixup! switch Clang tests back to manually written
melver 9a0ab30
[𝘀𝗽𝗿] changes introduced through rebase
melver 5ba014e
fixup! factor out some CodeGen changes
melver de04875
[𝘀𝗽𝗿] changes introduced through rebase
melver 6814ad5
rebase
melver 30c499f
[𝘀𝗽𝗿] changes introduced through rebase
melver b26b266
rebase
melver 660a1e6
[𝘀𝗽𝗿] changes introduced through rebase
melver 090caf4
rebase
melver ee3ec27
[𝘀𝗽𝗿] changes introduced through rebase
melver 129b88d
rebase
melver cb88bde
[𝘀𝗽𝗿] changes introduced through rebase
melver a975cf4
rebase
melver 1bc7080
rebase
melver 197fcfe
rebase
melver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
================= | ||
Allocation Tokens | ||
================= | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Introduction | ||
============ | ||
|
||
Clang provides support for allocation tokens to enable allocator-level heap | ||
organization strategies. Clang assigns mode-dependent token IDs to allocation | ||
calls; the runtime behavior depends entirely on the implementation of a | ||
compatible memory allocator. | ||
|
||
Possible allocator strategies include: | ||
|
||
* **Security Hardening**: Placing allocations into separate, isolated heap | ||
partitions. For example, separating pointer-containing types from raw data | ||
can mitigate exploits that rely on overflowing a primitive buffer to corrupt | ||
object metadata. | ||
|
||
* **Memory Layout Optimization**: Grouping related allocations to improve data | ||
locality and cache utilization. | ||
|
||
* **Custom Allocation Policies**: Applying different management strategies to | ||
different partitions. | ||
|
||
Token Assignment Mode | ||
===================== | ||
|
||
The default mode to calculate tokens is: | ||
|
||
* ``typehash``: This mode assigns a token ID based on the hash of the allocated | ||
type's name. | ||
|
||
Other token ID assignment modes are supported, but they may be subject to | ||
change or removal. These may (experimentally) be selected with ``-mllvm | ||
-alloc-token-mode=<mode>``: | ||
|
||
* ``random``: This mode assigns a statically-determined random token ID to each | ||
allocation site. | ||
|
||
* ``increment``: This mode assigns a simple, incrementally increasing token ID | ||
to each allocation site. | ||
|
||
Allocation Token Instrumentation | ||
================================ | ||
|
||
To enable instrumentation of allocation functions, code can be compiled with | ||
the ``-fsanitize=alloc-token`` flag: | ||
|
||
.. code-block:: console | ||
|
||
% clang++ -fsanitize=alloc-token example.cc | ||
|
||
The instrumentation transforms allocation calls to include a token ID. For | ||
example: | ||
|
||
.. code-block:: c | ||
|
||
// Original: | ||
ptr = malloc(size); | ||
|
||
// Instrumented: | ||
ptr = __alloc_token_malloc(size, <token id>); | ||
|
||
The following command-line options affect generated token IDs: | ||
|
||
* ``-falloc-token-max=<N>`` | ||
Configures the maximum number of tokens. No max by default (tokens bounded | ||
by ``SIZE_MAX``). | ||
|
||
.. code-block:: console | ||
|
||
% clang++ -fsanitize=alloc-token -falloc-token-max=512 example.cc | ||
|
||
Runtime Interface | ||
----------------- | ||
|
||
A compatible runtime must be provided that implements the token-enabled | ||
allocation functions. The instrumentation generates calls to functions that | ||
take a final ``size_t token_id`` argument. | ||
|
||
.. code-block:: c | ||
|
||
// C standard library functions | ||
void *__alloc_token_malloc(size_t size, size_t token_id); | ||
void *__alloc_token_calloc(size_t count, size_t size, size_t token_id); | ||
void *__alloc_token_realloc(void *ptr, size_t size, size_t token_id); | ||
// ... | ||
|
||
// C++ operators (mangled names) | ||
// operator new(size_t, size_t) | ||
void *__alloc_token__Znwm(size_t size, size_t token_id); | ||
// operator new[](size_t, size_t) | ||
void *__alloc_token__Znam(size_t size, size_t token_id); | ||
// ... other variants like nothrow, etc., are also instrumented. | ||
|
||
Fast ABI | ||
-------- | ||
|
||
An alternative ABI can be enabled with ``-fsanitize-alloc-token-fast-abi``, | ||
which encodes the token ID hint in the allocation function name. | ||
|
||
.. code-block:: c | ||
|
||
void *__alloc_token_0_malloc(size_t size); | ||
void *__alloc_token_1_malloc(size_t size); | ||
void *__alloc_token_2_malloc(size_t size); | ||
... | ||
void *__alloc_token_0_Znwm(size_t size); | ||
void *__alloc_token_1_Znwm(size_t size); | ||
void *__alloc_token_2_Znwm(size_t size); | ||
... | ||
|
||
This ABI provides a more efficient alternative where | ||
``-falloc-token-max`` is small. | ||
|
||
Disabling Instrumentation | ||
------------------------- | ||
|
||
To exclude specific functions from instrumentation, you can use the | ||
``no_sanitize("alloc-token")`` attribute: | ||
|
||
.. code-block:: c | ||
|
||
__attribute__((no_sanitize("alloc-token"))) | ||
void* custom_allocator(size_t size) { | ||
return malloc(size); // Uses original malloc | ||
} | ||
|
||
Note: Independent of any given allocator support, the instrumentation aims to | ||
remain performance neutral. As such, ``no_sanitize("alloc-token")`` | ||
functions may be inlined into instrumented functions and vice-versa. If | ||
correctness is affected, such functions should explicitly be marked | ||
``noinline``. | ||
|
||
The ``__attribute__((disable_sanitizer_instrumentation))`` is also supported to | ||
disable this and other sanitizer instrumentations. | ||
|
||
Suppressions File (Ignorelist) | ||
------------------------------ | ||
|
||
AllocToken respects the ``src`` and ``fun`` entity types in the | ||
:doc:`SanitizerSpecialCaseList`, which can be used to omit specified source | ||
files or functions from instrumentation. | ||
|
||
.. code-block:: bash | ||
melver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[alloc-token] | ||
# Exclude specific source files | ||
src:third_party/allocator.c | ||
# Exclude function name patterns | ||
fun:*custom_malloc* | ||
fun:LowLevel::* | ||
|
||
.. code-block:: console | ||
|
||
% clang++ -fsanitize=alloc-token -fsanitize-ignorelist=my_ignorelist.txt example.cc | ||
|
||
Conditional Compilation with ``__SANITIZE_ALLOC_TOKEN__`` | ||
----------------------------------------------------------- | ||
|
||
In some cases, one may need to execute different code depending on whether | ||
AllocToken instrumentation is enabled. The ``__SANITIZE_ALLOC_TOKEN__`` macro | ||
can be used for this purpose. | ||
|
||
.. code-block:: c | ||
|
||
#ifdef __SANITIZE_ALLOC_TOKEN__ | ||
// Code specific to -fsanitize=alloc-token builds | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.