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

    <tr>
        <th>Summary</th>
        <td>
            compiler-rt/sanitizers/aarch64: CHECK failed: sanitizer_allocator_primary64.h:133
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          real-or-random
      </td>
    </tr>
</table>

<pre>
    
#### Symptoms
MSan fails to init with
```
MemorySanitizer: CHECK failed: sanitizer_allocator_primary64.h:133 "((kSpaceBeg)) == ((address_range.Init(TotalSpaceSize, PrimaryAllocatorName, kSpaceBeg)))" (0xe00000000000, 0xfffffffffffffff4) (tid=23879)
```

#### Environment
This is when running an MSan-instrumented aarch64 binary via `qemu-aarch64`.  I'm using a clang snapshot:
```
Debian clang version 18.0.0 (++20230829112257+96e83d3705c7-1~exp1~20230829112313.180) 
```

People report similar issues on Raspberry Pi 4, with ASan, and with Clang 16 and Clang 17, while Clang 15 works:
 - https://github.com/google/sanitizers/issues/1674
 - https://github.com/google/sanitizers/issues/1329
 - https://raspberrypi.stackexchange.com/questions/144012/clang-sanitizers-fail
 - https://gitlab.alpinelinux.org/alpine/aports/-/issues/15025
 - https://groups.google.com/g/thread-sanitizer/c/ML3IEO8Yby8 (but this one lacks details...)

So this seems to be a regression between 15 and 16.

#### Possible causes and fix

I have a strong suspicion that the reason for this is the commit https://github.com/llvm/llvm-project/commit/a588cfe37ea36ec4fae35a233a13d3557fba86e9, but I haven't tested recompiling with `-DSANITIZER_CAN_USE_ALLOCATOR64=0`. 

When I use qemu's strace functionality, I get this just before the crash:
```
mmap(0x0000e00000000000,8192,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE,-1,0) = -1 errno=12 (Cannot allocate memory)
```
And note that the value `0xfffffffffffffff4` from the assertion is equal to `-12`. Here, `0x0000e00000000000` is a 48-bit address, but perhaps QEMU's emulation supports only smaller (39 bits)? I suspect that the Raspberry Pi 4 also has a virtual address space smaller than 48 bits. When I try this with Clang 14, I see only mmap calls up to address `0x0000006000000000` (which is a 39-bit address).

The AArch64 memory layout on Linux can vary a lot (https://www.kernel.org/doc/html/v5.8/arm64/memory.html). Possible virtual address sizes are 39 bits, 42 bits, 48 bits, and 52 bits. Perhaps it's a good idea to detect this at runtime in the 64-bit allocator?


(note: Not sure if this is the right place to report. It seems to me that this is a compiler-rt issue, which seem to be tracked here? But please close if the existing https://github.com/google/sanitizers/issues/1674 is a better place to track this.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVktv4zrS_TXMpmBBoh6WF144fuAaX3eSL07fmTubgJbKFm8oUk1SSdyL-9sHRclxp7szwDwMIZFIVrF46tQpCufkUSPOWX7N8tWV6H1j7NyiUBNjJ1bo2rRXe1Of5ixesXjBePr2wO7Udt60bpj6vBMaDkIqB96A1NLDi_TNaFfE4zOsxdbY005o6eU3tCxdwPK39fL_gj3W9O3Ok49CKVMJb-xjZ2Ur7KnIooaliyRNgXHOeMl4-bTrRIXXeGR8xvgMWLpi6QqGSVHXFp17tEIfMdpq6RkvH4wXKljt5DdkfAl3g_vFeb8b0YbxH3yHh5Pr-BXjy4-Wxq-H978sBMNLL2uWrnhaToP9r0D5BcJr_Syt0S1qP8w-NNKBdPDSoAbbay31EYQGAn8itfO2p8VYgxC2aooM9lILe4JnKYAV8Vds-8k4xYo4AtgyPm2hd8ERVEroIzgtOtcYz9LFLyNd4V4KPS5-Ruuk0ZCUURzFA-LXjF_zmKdxyWdJwnk-Zfx6VmCZ1uk0zqvpJPkLX7vkr-8WpUkaJWUc8PoYnjs0nUKw2BnrwclWKmFBOtejA6PhXrhuj9ae4E5CRikhEsJiJzR9CF0PA8sQe1KEkfFjGpY3UuF5JIcXY5_cGw4wgcb7LgzwDeObo_RNv48q09KHMUeFjG_euOsY3wyxM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