-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<optional>: Add Address Sanitizer annotations
#6010
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
base: main
Are you sure you want to change the base?
Changes from all commits
c46178c
ca0aa13
2f78a66
aa2197a
2b571c3
297ab67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_17_matrix.lst |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <optional> | ||
|
|
||
| #ifdef __SANITIZE_ADDRESS__ | ||
| extern "C" int __cdecl __asan_address_is_poisoned(void const volatile* addr); | ||
| #define ASAN_VERIFY_POISONED(addr) assert(__asan_address_is_poisoned((addr)) != 0) | ||
| #define ASAN_VERIFY_UNPOISONED(addr) assert(__asan_address_is_poisoned((addr)) == 0) | ||
| #else | ||
| #define ASAN_VERIFY_POISONED(addr) ((void) (addr)) | ||
| #define ASAN_VERIFY_UNPOISONED(addr) ((void) (addr)) | ||
| #endif | ||
|
|
||
| struct Payload { | ||
| long long x; | ||
| long long y; | ||
| long long z; | ||
| long long w; | ||
| }; | ||
|
|
||
| void test_poison_on_empty_access() { | ||
| [[maybe_unused]] std::optional<Payload> opt; | ||
| ASAN_VERIFY_POISONED(reinterpret_cast<Payload*>(&opt)); | ||
| } | ||
|
|
||
| void test_emplace_unpoisoning() { | ||
| std::optional<Payload> opt; | ||
| opt.emplace(); | ||
| ASAN_VERIFY_UNPOISONED(reinterpret_cast<Payload*>(&opt)); | ||
| } | ||
|
|
||
| void test_assignment_unpoisoning() { | ||
| std::optional<Payload> opt = std::nullopt; | ||
| opt = Payload{}; | ||
| ASAN_VERIFY_UNPOISONED(reinterpret_cast<Payload*>(&opt)); | ||
| } | ||
|
|
||
| void test_repoison_after_reset() { | ||
| std::optional<Payload> opt = Payload{}; | ||
| ASAN_VERIFY_UNPOISONED(reinterpret_cast<Payload*>(&opt)); | ||
| opt.reset(); | ||
| ASAN_VERIFY_POISONED(reinterpret_cast<Payload*>(&opt)); | ||
| } | ||
|
|
||
| constexpr bool test_constexpr() { | ||
| #if _HAS_CXX20 | ||
| bool res = true; | ||
| std::optional<Payload> opt = std::nullopt; | ||
| opt = Payload{}; | ||
| opt.reset(); | ||
| opt = Payload{86, 0, 0, 0}; | ||
| res = opt->x == 86; | ||
| opt.emplace(42, 0, 0, 0); | ||
| res = res && (opt->x == 42); | ||
| return res; | ||
| #else | ||
| std::optional<Payload> opt{Payload{86, 0, 0, 0}}; | ||
| return opt->x == 86; | ||
| #endif | ||
| } | ||
|
|
||
| int main() { | ||
| test_poison_on_empty_access(); | ||
| test_emplace_unpoisoning(); | ||
| test_assignment_unpoisoning(); | ||
| test_repoison_after_reset(); | ||
|
Comment on lines
+65
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can make these cases constexpr-friendly and test them in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a separate function to test constexpr functionality in |
||
| static_assert(test_constexpr(), "constexpr test failed"); | ||
|
|
||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable isn't used. Is it really necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a check for this variable inside the
if(!_STD _Is_constant_evaluated())block.