<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54493>54493</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [ASAN] swapcontext interception is broken due to a magic kMaxSaneContextStackSize
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          apolukhin
      </td>
    </tr>
</table>

<pre>
    Initial report by @itrofimow from https://github.com/google/sanitizers/issues/1494:

 >   As far as i understand, intercepted swapcontext should unpoison shadow memory for ucps stack, however it only does so if ucps stack is [less than 4Mb](https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/asan/asan_interceptors.cpp#L251). This behavior was introduced 9 years ago ([llvm/llvm-project@4f1885a](https://github.com/llvm/llvm-project/commit/4f1885a1096c3ac51cc0c0fa3d403aa21aa5d976)) and i couldn't find the exact reasoning behind these changes.
>
>   Recently we introduced stacks bigger than 4Mb (via boost.Coroutine2) and asan-ed builds started crashing left and right at seemingly random places - changing kMaxSaneContextStackSize to bigger numbers fixed that.

The fix for the issue seems quite trivial:
```diff
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 2ff314a5a9cbd..f4c89cabe2737 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -248,8 +248,7 @@ static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
   uptr bottom = stack & ~(PageSize - 1);
   ssize += stack - bottom;
   ssize = RoundUpTo(ssize, PageSize);
-  static const uptr kMaxSaneContextStackSize = 1 << 22;  // 4 Mb
-  if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
+  if (AddrIsInMem(bottom) && ssize) {
     PoisonShadow(bottom, ssize, 0);
   }
 }
 ```
</pre>
<img width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