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

    <tr>
        <th>Summary</th>
        <td>
            [BOLT][AArch64] Bolted Binary Crashes in TCMalloc
        </td>
    </tr>

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

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

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

<pre>
    Hello! We have been experimenting using Bolt on Mongodb aarch64 and have run into it crashing on launch on a subset of our host machines. The crash happens inside tcmalloc, and specifically i have noticed the crashes only happen when either of these two functions end up in the fdata:

```
1 TcmallocSlab_Internal_Pop_trampoline_12/1 0 2
1 TcmallocSlab_Internal_Push_trampoline_13/1 0 1
```
Crash is consistent on the machines it does crash on during startup, but only happens on a subset of OS versions for some reason. The data collected was on a Bolt version ~2 months old, but i tried with the newest head as of this morning and it still repros.

Command we run to Bolt:
```
llvm-bolt ./mongod -o ./mongod.bolt -data ./mongod.fdata -reorder-blocks=ext-tsp -reorder-functions=cdsort -split-functions -split-all-cold -split-eh -dyno-stats --lite -skip-funcs=_ZN8tcmalloc17tcmalloc_internal6subtle6percpu12TcmallocSlab4GrowEimmN4absl12lts_2023080211FunctionRefIFmhEEE,_ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE21DeallocateSlowNoHooksEPvm,_ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE19AllocateSlowNoHooksEm,calloc*,malloc*,_Znwm*,_ZnwmSt11align_val_t*,_ZdaPv*,realloc*,_ZdlPvmSt11align_val_t*,_ZdlPvm*,_ZN8tcmalloc17tcmalloc_internal6subtle6percpu12TcmallocSlab16CacheCpuSlabSlowEv,TcmallocSlab_Internal_PushBatch,TcmallocSlab_Internal_PopBatch
```


Here is a stack trace of the crash:
```
#0  0x0000aaaab3a2e1a4 in tcmalloc::tcmalloc_internal::subtle::percpu::TcmallocSlab::Pop (size_class=3, this=<optimized out>) at src/third_party/tcmalloc/dist/tcmalloc/internal/percpu_tcmalloc.h:877
#1 tcmalloc::tcmalloc_internal::cpu_cache_internal::CpuCache<tcmalloc::tcmalloc_internal::cpu_cache_internal::StaticForwarder>::AllocateFast (size_class=3, this=<optimized out>) at src/third_party/tcmalloc/dist/tcmalloc/cpu_cache.h:698
#2 tcmalloc::tcmalloc_internal::fast_alloc<tcmalloc::tcmalloc_internal::TCMallocPolicy<tcmalloc::tcmalloc_internal::CppOomPolicy, tcmalloc::tcmalloc_internal::DefaultAlignPolicy, tcmalloc::tcmalloc_internal::AllocationAccessHotPolicy, tcmalloc::tcmalloc_internal::InvokeHooksPolicy, tcmalloc::tcmalloc_internal::NonSizeReturningPolicy, tcmalloc::tcmalloc_internal::LocalNumaPartitionPolicy>, void*> (size=32, policy=...) at src/third_party/tcmalloc/dist/tcmalloc/tcmalloc.cc:1011
#3 TCMallocInternalNew (size=32) at src/third_party/tcmalloc/dist/tcmalloc/tcmalloc.cc:1102
#4 0x0000aaaab2524a0c in google::protobuf::internal::ShutdownData::get () at src/third_party/protobuf/dist/src/google/protobuf/message_lite.cc:679
#5 google::protobuf::internal::OnShutdownRun (f=0xaaaab24de878 <google::protobuf::internal::DestroyString(void const*)>, arg=0xaaaab3f6d5f0 <google::protobuf::internal::fixed_address_empty_string[abi:cxx11]>)
    at src/third_party/protobuf/dist/src/google/protobuf/message_lite.cc:697
#6 0x0000aaaab24de8e4 in google::protobuf::internal::OnShutdownDestroyString (ptr=0x3) at src/third_party/protobuf/dist/src/google/protobuf/generated_message_util.h:180
```

The instruction it crashes on:
```
   0x0000aaaab3a2e198 <+88>:    adrp    x15, 0xaaaab3e8a000 <_ZN4absl12lts_2025051212_GLOBAL__N_119submit_profile_dataE>
 0x0000aaaab3a2e19c <+92>:    add     x15, x15, #0x380
 0x0000aaaab3a2e1a0 <+96>:    str     x15, [x11]
=> 0x0000aaaab3a2e1a4 <+100>:   ldr     x13, [x12]
   0x0000aaaab3a2e1a8 <+104>:   tbz     x13, #63, 0xaaaab3a2e1f8 <TCMallocInternalNew(size_t)+184>
```

Memory at that instruction when it crashes:
```
(gdb) x/10gx $x12
0xfffff7fb789c: 0x0000000600000000      0xb3e8a38000000006
0xfffff7fb78ac: 0x000000000000aaaa      0x0000000000000000
0xfffff7fb78bc: 0xb3a9b440ffffffff      0x000000000000aaaa
0xfffff7fb78cc: 0xac49f12200000000      0xac49f1220000aaaa
0xfffff7fb78dc: 0x007070610000aaaa      0x0000000000000000
```

Memory at that instruction on a machine where it doesn't crash:
```
(gdb) x/10gx $x12
0xfffff7fb389c: 0x0000000000000000      0xb3e8a380fffffffe
0xfffff7fb38ac: 0x000000000000aaaa      0x0000000000000000
0xfffff7fb38bc: 0xb3a9b440ffffffff      0x000000000000aaaa
0xfffff7fb38cc: 0xac49f12200000000      0xac49f1220000aaaa
0xfffff7fb38dc: 0x007070610000aaaa      0x0000000000000000
```
Let me know if there's any other data you want me to collect.

As an aside, is there a way to avoid this in aarch64? I've seen some comments in older bugs mentioning --skip-funcs= isn't supported on aarch64, is that still true? I've noticed manually removing
``` 
TcmallocSlab_Internal_Pop_trampoline_12
TcmallocSlab_Internal_Push_trampoline_13 
```
from the fdata will cause it not to reproduce, but i'm unsure of how good an idea that is (if it actually prevents optimization on that function or if its possible some other function has the ability to bring in those functions to be optimized anyways).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8WEtv4zjy_zTKpWCDovyQDzk4fkw30JMOJg38gbkIlFiy-G-KFEjKjz7sZ1-QkhzbSXrSuzMbGJFE8ldvFovFrBU7hXgfTR-i6fqOta7S5v7BMG4rdvhxl2t-uv-EUuqIxvB_CBXbI-SICvDYoBE1KifUDlrr_z9o6UAr-F2rneY5MGaKajYBpniHNK0CoZwG4aAwzFYepRVI1qqi8m8MbJtbdKBL0K2BSlsHNSsqodCO4VuFHR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