Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 79be50c9e9dc43431f488a28a416a9d81548e36184fd1341b7c20e15b5d90bd3
Ticket: 7371b6270b727521cf0c7044a882d16ef5c4e96b
AddressSanitizer use-after-return detection breaks NRE tests, coroutines
User & Date: chrstphrchvz on 2023-08-04 04:36:43
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    AddressSanitizer (ASan) can detect use-after-return errors. Depending on the platform, this is enabled either by default, or by using the appropriate compiler flags or runtime environment variables (e.g. <tt>ASAN_OPTIONS=detect_use_after_return=1</tt>).
    
    But enabling this can completely break Tcl coroutines: there will be unexpected “cannot yield: C stack busy” errors, and numerous tests will fail or even hang (e.g. http-3.10.0, io-28.6). This is because the real stack gets replaced with a heap-allocated fake stack, and there is [https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterReturn#compatibility|no guarantee] that the address of a stack variable will be consistent for a given C stack depth, as assumed by the coroutines implementation.
    
    Rather than requiring AddressSanitizer use-after-return detection to be disabled, I suggest that Tcl use AddressSanitizer-friendly approaches instead. For GCC and Clang, there is <a href="https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html#index-_005f_005fbuiltin_005fframe_005faddress"><tt>__builtin_frame_address(0)</tt></a>; and for MSVC, there is <a href="https://learn.microsoft.com/en-us/cpp/intrinsics/addressofreturnaddress"><tt>_AddressOfReturnAddress()</tt></a>. And I suggest always using these when available, for consistency between builds with and without AddressSanitizer, and in case of other use-after-return detection tools with similar limitations. See attached patch. [https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0-rc1/clang/lib/Basic/Stack.cpp#L24] shows a very similar example.
    
  5. foundin changed to: "8.6.13"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "6133eb3026f9b10aa9b7b48da92ae2834fc225b0"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "chrstphrchvz"
  14. subsystem changed to: "60. NRE and coroutines"
  15. title changed to:
    AddressSanitizer use-after-return detection breaks coroutines
    
  16. type changed to: "Patch"