[libcxx-commits] [libcxx] [ASan][libc++] String annotations optimizations fix with lambda (PR #76200)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 21 18:19:08 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Tacet (AdvenamTacet)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/76200.diff


1 Files Affected:

- (modified) libcxx/include/string (+4-1) 


``````````diff
diff --git a/libcxx/include/string b/libcxx/include/string
index c676182fba8bac..03f6655bb1e76e 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -922,7 +922,10 @@ 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_))) {
+      // Lambda is used because of optimization challenges encountered within comma constructors.
+      // Lambda with argument is correctly optimized, but it does not solve the problem with internal memory
+      // access macro.
+      : __r_([](basic_string &__s){ if(!__s.__is_long()) __s.__annotate_delete(); return std::move(__s.__r_);}(__str)) {
     __str.__r_.first() = __rep();
     __str.__annotate_new(0);
     if (!__is_long())

``````````

</details>


https://github.com/llvm/llvm-project/pull/76200


More information about the libcxx-commits mailing list