Repository: rust
Issue: macos: rustc built without jemalloc sporadically fails with realloc failure
Labels: O-macos,C-bug

Building rustc on macos without jemalloc (the default in config.toml) will trigger frequent SIGABRT.  The error is:

```
rustc(14225,0x70000fab4000) malloc: *** error for object 0x600005c70800: pointer being realloc'd was not allocated
rustc(14225,0x70000fab4000) malloc: *** set a breakpoint in malloc_error_break to debug
```

The backtrace is:

<details><summary>Backtrace</summary>
<p>

```
* thread #2, name = 'rustc', stop reason = breakpoint 1.1
  * frame #0: 0x00007ff80c53f5d7 libsystem_malloc.dylib`malloc_error_break
    frame #1: 0x00007ff80c531376 libsystem_malloc.dylib`malloc_vreport + 440
    frame #2: 0x00007ff80c5345ed libsystem_malloc.dylib`malloc_report + 151
    frame #3: 0x00007ff80c525542 libsystem_malloc.dylib`realloc + 328
    frame #4: 0x000000010c886c64 librustc_driver-cea70d83b3706378.dylib`_RINvNtCsk2H0Gr1Y1z1_5alloc7raw_vec11finish_growNtNtB4_5alloc6GlobalECsetNeWcxqv12_12rustc_expand + 52
    frame #5: 0x000000010c88721d librustc_driver-cea70d83b3706378.dylib`_RNvMs_NtCsk2H0Gr1Y1z1_5alloc7raw_vecINtB4_6RawVecNtNtCsetNeWcxqv12_12rustc_expand3mbe9TokenTreeE16reserve_for_pushBP_ + 125
    frame #6: 0x000000010c7f7222 librustc_driver-cea70d83b3706378.dylib`_RNvNtNtCsetNeWcxqv12_12rustc_expand3mbe6quoted5parse + 4002
    frame #7: 0x000000010c7f63fe librustc_driver-cea70d83b3706378.dylib`_RNvNtNtCsetNeWcxqv12_12rustc_expand3mbe6quoted5parse + 382
    frame #8: 0x000000010c8138eb librustc_driver-cea70d83b3706378.dylib`_RINvXs0_NtNtNtCsasXF9FuNFFy_4core4iter8adapters3mapINtB6_3MapINtNtNtBc_5slice4iter4IterNtNtNtCsetNeWcxqv12_12rustc_expand3mbe12macro_parser10NamedMatchENCNvNtB1r_11macro_rules25compile_declarative_macros_0ENtNtNtBa_6traits8iterator8Iterator4folduNCINvNvB3i_8for_each4callNtB1r_9TokenTreeNCNvXs_NtNtCsk2H0Gr1Y1z1_5alloc3vec11spec_extendINtB4K_3VecB4l_EINtB4I_10SpecExtendB4l_BN_E11spec_extend0E0EB1t_ + 267
    frame #9: 0x000000010c861d47 librustc_driver-cea70d83b3706378.dylib`_RNvXNtNtCsk2H0Gr1Y1z1_5alloc3vec14spec_from_iterINtB4_3VecNtNtCsetNeWcxqv12_12rustc_expand3mbe9TokenTreeEINtB2_12SpecFromIterBU_INtNtNtNtCsasXF9FuNFFy_4core4iter8adapters3map3MapINtNtNtB2b_5slice4iter4IterNtNtBW_12macro_parser10NamedMatchENCNvNtBW_11macro_rules25compile_declarative_macros_0EE9from_iterBY_ + 247
    frame #10: 0x000000010c8bd753 librustc_driver-cea70d83b3706378.dylib`_RNvNtNtCsetNeWcxqv12_12rustc_expand3mbe11macro_rules25compile_declarative_macro + 2451
    frame #11: 0x000000010b75c904 librustc_driver-cea70d83b3706378.dylib`_RNvMs_NtCs9b1qYhXJ0Cf_13rustc_resolve6macrosNtB6_8Resolver13compile_macro + 68
    frame #12: 0x000000010b70fd37 librustc_driver-cea70d83b3706378.dylib`_RNvMs3_NtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graphNtB5_24BuildReducedGraphVisitor12define_macro + 295
    frame #13: 0x000000010b710e08 librustc_driver-cea70d83b3706378.dylib`_RNvXs4_NtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graphNtB5_24BuildReducedGraphVisitorNtNtCs9cCHMvxMcfg_9rustc_ast5visit7Visitor10visit_item + 56
    frame #14: 0x000000010b783a5c librustc_driver-cea70d83b3706378.dylib`_RINvNtCs9cCHMvxMcfg_9rustc_ast5visit9walk_itemNtNtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graph24BuildReducedGraphVisitorEBM_ + 572
    frame #15: 0x000000010b71304c librustc_driver-cea70d83b3706378.dylib`_RNvXs4_NtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graphNtB5_24BuildReducedGraphVisitorNtNtCs9cCHMvxMcfg_9rustc_ast5visit7Visitor10visit_item + 8828
    frame #16: 0x000000010b783a5c librustc_driver-cea70d83b3706378.dylib`_RINvNtCs9cCHMvxMcfg_9rustc_ast5visit9walk_itemNtNtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graph24BuildReducedGraphVisitorEBM_ + 572
    frame #17: 0x000000010b71304c librustc_driver-cea70d83b3706378.dylib`_RNvXs4_NtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graphNtB5_24BuildReducedGraphVisitorNtNtCs9cCHMvxMcfg_9rustc_ast5visit7Visitor10visit_item + 8828
    frame #18: 0x000000010b7d332d librustc_driver-cea70d83b3706378.dylib`_RINvMs6_NtCsetNeWcxqv12_12rustc_expand6expandNtB6_11AstFragment10visit_withNtNtCs9b1qYhXJ0Cf_13rustc_resolve19build_reduced_graph24BuildReducedGraphVisitorEB1f_ + 685
    frame #19: 0x000000010b7519c7 librustc_driver-cea70d83b3706378.dylib`_RNvXNtCs9b1qYhXJ0Cf_13rustc_resolve6macrosNtB4_8ResolverNtNtCsetNeWcxqv12_12rustc_expand4base14ResolverExpand36visit_ast_fragment_with_placeholders + 487
    frame #20: 0x000000010c7e0cc3 librustc_driver-cea70d83b3706378.dylib`_RNvMs1_NtCsetNeWcxqv12_12rustc_expand6expandNtB5_13MacroExpander19collect_invocations + 707
    frame #21: 0x000000010c7dbe80 librustc_driver-cea70d83b3706378.dylib`_RNvMs1_NtCsetNeWcxqv12_12rustc_expand6expandNtB5_13MacroExpander21fully_expand_fragment + 256
    frame #22: 0x000000010c7dbba2 librustc_driver-cea70d83b3706378.dylib`_RNvMs1_NtCsetNeWcxqv12_12rustc_expand6expandNtB5_13MacroExpander12expand_crate + 1186
    frame #23: 0x0000000108c098bc librustc_driver-cea70d83b3706378.dylib`_RINvMNtCs3NuVQBhYW7C_13rustc_session5utilsNtNtB5_7session7Session4timeINtNtCsasXF9FuNFFy_4core6result6ResultNtNtCs9cCHMvxMcfg_9rustc_ast3ast5CrateNtCsdjh14Z7klAl_12rustc_errors13ErrorReportedENCNvNtCsBlFisMo4u3_15rustc_interface6passes20configure_and_expands_0EB3a_ + 1116
    frame #24: 0x0000000108b749c0 librustc_driver-cea70d83b3706378.dylib`_RNvNtCsBlFisMo4u3_15rustc_interface6passes20configure_and_expand + 528
    frame #25: 0x0000000108b7a354 librustc_driver-cea70d83b3706378.dylib`_RNvMs0_NtCsBlFisMo4u3_15rustc_interface7queriesNtB5_7Queries9expansion + 836
    frame #26: 0x0000000108a5520f librustc_driver-cea70d83b3706378.dylib`_RINvMs2_NtCsBlFisMo4u3_15rustc_interface7queriesNtNtB8_9interface8Compiler5enterNCNCNvCsQmx6cSBOMI_12rustc_driver12run_compilers_0s0_0INtNtCsasXF9FuNFFy_4core6result6ResultINtNtB2d_6option6OptionNtB6_6LinkerENtCsdjh14Z7klAl_12rustc_errors13ErrorReportedEEB1m_ + 1327
    frame #27: 0x0000000108a4aaa8 librustc_driver-cea70d83b3706378.dylib`_RINvCskTEkA9M7v6o_10rustc_span15with_source_mapINtNtCsasXF9FuNFFy_4core6result6ResultuNtCsdjh14Z7klAl_12rustc_errors13ErrorReportedENCINvNtCsBlFisMo4u3_15rustc_interface9interface23create_compiler_and_runBJ_NCNvCsQmx6cSBOMI_12rustc_driver12run_compilers_0Es_0EB3n_ + 376
    frame #28: 0x0000000108a5639f librustc_driver-cea70d83b3706378.dylib`_RINvMs_Cs7KVLOe8Wn3E_10scoped_tlsINtB5_9ScopedKeyNtCskTEkA9M7v6o_10rustc_span14SessionGlobalsE3setNCNCINvNtCsBlFisMo4u3_15rustc_interface4util51setup_callbacks_and_run_in_thread_pool_with_globalsNCINvNtB1H_9interface12run_compilerINtNtCsasXF9FuNFFy_4core6result6ResultuNtCsdjh14Z7klAl_12rustc_errors13ErrorReportedENCNvCsQmx6cSBOMI_12rustc_driver12run_compilers_0E0B3G_E00B3G_EB57_ + 1311
    frame #29: 0x0000000108a52ea2 librustc_driver-cea70d83b3706378.dylib`_RINvNtNtCs57FQuS03DEe_3std10sys_common9backtrace28___rust_begin_short_backtraceNCINvNtCsBlFisMo4u3_15rustc_interface4util51setup_callbacks_and_run_in_thread_pool_with_globalsNCINvNtB1m_9interface12run_compilerINtNtCsasXF9FuNFFy_4core6result6ResultuNtCsdjh14Z7klAl_12rustc_errors13ErrorReportedENCNvCsQmx6cSBOMI_12rustc_driver12run_compilers_0E0B3l_E0B3l_EB4M_ + 146
    frame #30: 0x0000000108a3fae5 librustc_driver-cea70d83b3706378.dylib`_RNSNvYNCINvMNtCs57FQuS03DEe_3std6threadNtBa_7Builder15spawn_uncheckedNCINvNtCsBlFisMo4u3_15rustc_interface4util51setup_callbacks_and_run_in_thread_pool_with_globalsNCINvNtB1c_9interface12run_compilerINtNtCsasXF9FuNFFy_4core6result6ResultuNtCsdjh14Z7klAl_12rustc_errors13ErrorReportedENCNvCsQmx6cSBOMI_12rustc_driver12run_compilers_0E0B3b_E0B3b_Es_0INtNtNtB3g_3ops8function6FnOnceuE9call_once6vtableB4C_ + 165
    frame #31: 0x00000001007157c7 libstd-f20edb373511b023.dylib`std::sys::unix::thread::Thread::new::thread_start::h13e1d52aa9cad226 + 39
    frame #32: 0x00007ff80c707514 libsystem_pthread.dylib`_pthread_start + 125
    frame #33: 0x00007ff80c70302f libsystem_pthread.dylib`thread_start + 15
```

</p>
</details>

This happens almost 100% of the the time for me, though it can be sporadic.

Doing something like `./x.py build --stage=2 src/tools/cargo` is enough to reproduce.  It almost always fails while building `core` for stage2.  

I've spent some time trying to debug it, but have not figured anything out.

Using jemalloc makes the error go away.

Currently using master (8ad3c1dd1d47f9ce7dfdf4a14c70c67e1790b0f5), though this has been going on for a while.  If I get a chance, I'll try to bisect to see when it started. But for me, I think it started after I updated to Monterey (macOS 12.0).

I have XCode 13.1.

Other users have reported this at https://github.com/rust-lang/cargo/issues/10022.