Skip to content

Commit

Permalink
[ASan][libc++] Initialize a variable with lambda
Browse files Browse the repository at this point in the history
This commit is a refactor (increases readability) and optimization fix.

This is a fixed commit of llvm#76200
First reverthed here: llvm@1ea7a56

The difference is a return type of the lambda.

Original description:

This commit addresses optimization and instrumentation challenges encountered within comma constructors.
  1) _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS does not work in comma constructors.
  2) Code inside comma constructors is not always correctly optimized. Problematic code examples:
        - `: __r_(((__str.__is_long() ? 0 : (__str.__annotate_delete(), 0)), std::move(__str.__r_))) {`
        - `: __r_(__r_([&](){ if(!__s.__is_long()) __s.__annotate_delete(); return std::move(__s.__r_);}())) {`

However, lambda with argument seems to be correctly optimized. This patch uses that fact.

Use of lambda based on idea from @ldionne.
  • Loading branch information
Advenam Tacet committed Jan 8, 2024
1 parent 1ea7a56 commit 4ea954b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public:
// Turning off ASan instrumentation for variable initialization with _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
// does not work consistently during initialization of __r_, so we instead unpoison __str's memory manually first.
// __str's memory needs to be unpoisoned only in the case where it's a short string.
: __r_(((__str.__is_long() ? 0 : (__str.__annotate_delete(), 0)), std::move(__str.__r_))) {
: __r_([](basic_string &__s) -> auto&& { if(!__s.__is_long()) __s.__annotate_delete(); return std::move(__s.__r_); }(__str)) {
__str.__r_.first() = __rep();
__str.__annotate_new(0);
if (!__is_long())
Expand Down

0 comments on commit 4ea954b

Please sign in to comment.