Subject: Re: bug#58042: 29.0.50; ASAN use-after-free in re_match_2_internal

From: Gerd Möllmann <[email-masked]>
Date: Sat, 24 Sep 2022 16:17:20 +0200

Gerd Möllmann <[email-masked]> writes:

> In GNU Emacs 29.0.50 (build 1, aarch64-apple-darwin21.6.0, NS
>  appkit-2113.60 Version 12.6 (Build 21G115)) of 2022-09-21 built on
>  Mini.fritz.box
> Repository revision: 1231a601ebe1fd9fe454c504dbeb9267440242e7
> Repository branch: master
> Windowing system distributor 'Apple', version 10.3.2113
> System Description:  macOS 12.6
>
> Configured using:
>  'configure --cache-file /Users/gerd/tmp/config.cache.master
>  --with-native-compilation'
>  
> Configured features:
> ACL DBUS GLIB GNUTLS JSON LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY
> KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TOOLKIT_SCROLL_BARS XIM ZLIB
>
> I got the following ASAN error today.  Unfortunately, I don't have the
> slightest idea how to reproduce this.
>
> ==79227==ERROR: AddressSanitizer: heap-use-after-free on address 0x00011f81e7d1 at pc 0x0001005825c4 bp 0x00016fdcf370 sp 0x00016fdcf368
> READ of size 1 at 0x00011f81e7d1 thread T0
>     #0 0x1005825c0 in re_match_2_internal regex-emacs.c:4352
>     #1 0x10057e5cc in rpl_re_search_2 regex-emacs.c:3383
>     #2 0x10057d1c4 in rpl_re_search regex-emacs.c:3177
>     #3 0x10056115c in fast_string_match_internal search.c:492
>     #4 0x1005045c0 in fast_string_match lisp.h:4818
>     #5 0x100504018 in Ffind_file_name_handler fileio.c:324
>     #6 0x1006dbe5c in openp lread.c:1911
>     #7 0x1006d8844 in Fload lread.c:1302
>     #8 0x1006e1af0 in save_match_data_load lread.c:1630
>     #9 0x10064f8cc in load_with_autoload_queue eval.c:2269
>     #10 0x10067d2f8 in Frequire fns.c:3274

Forget to copy the part where it is freed:

freed by thread T0 here:
    #0 0x103332de4 in wrap_free+0x98 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3ede4)
    #1 0x100985e38 in rpl_free free.c:48
    #2 0x1005b71a4 in lisp_free alloc.c:1038
    #3 0x1005cbda4 in compact_small_strings alloc.c:2191
    #4 0x1005c9f24 in sweep_strings alloc.c:2072
    #5 0x1005bd028 in gc_sweep alloc.c:7397
    #6 0x1005bb178 in garbage_collect alloc.c:6245
    #7 0x1005ba694 in maybe_garbage_collect alloc.c:6090
    #8 0x1006505ac in maybe_gc lisp.h:5624
    #9 0x100648ffc in Ffuncall eval.c:2972
    #10 0x10064bcd0 in internal_condition_case_n eval.c:1555
    #11 0x1000cdc8c in safe__call xdisp.c:3026
    #12 0x1000cdfc4 in safe__call1 xdisp.c:3062
    #13 0x1001d6404 in prepare_menu_bars xdisp.c:13572
    #14 0x1000f2340 in redisplay_internal xdisp.c:16523
    #15 0x100108f34 in redisplay xdisp.c:16105
    #16 0x10088fa84 in -[EmacsView layoutSublayersOfLayer:] nsterm.m:8662
    #17 0x1900a9624 in CA::Layer::layout_if_needed(CA::Transaction*)+0x224 (QuartzCore:arm64e+0x20624)
    #18 0x1901f661c in CA::Context::commit_transaction(CA::Transaction*, double, double*)+0x1c0 (QuartzCore:arm64e+0x16d61c)
    #19 0x19008b4c8 in CA::Transaction::commit()+0x2bc (QuartzCore:arm64e+0x24c8)
    #20 0x18bee1698 in __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke+0x12c (AppKit:arm64e+0x1ac698)
    #21 0x18c646754 in ___NSRunLoopObserverCreateWithHandler_block_invoke+0x3c (AppKit:arm64e+0x911754)
    #22 0x1892101a0 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__+0x20 (CoreFoundation:arm64e+0x841a0)
    #23 0x18920fff0 in __CFRunLoopDoObservers+0x24c (CoreFoundation:arm64e+0x83ff0)
    #24 0x18920f524 in __CFRunLoopRun+0x300 (CoreFoundation:arm64e+0x83524)
    #25 0x18920ea80 in CFRunLoopRunSpecific+0x254 (CoreFoundation:arm64e+0x82a80)
    #26 0x191e4e334 in RunCurrentEventLoopInMode+0x120 (HIToolbox:arm64e+0x32334)
    #27 0x191e4dfc0 in ReceiveNextEventCommon+0x140 (HIToolbox:arm64e+0x31fc0)
    #28 0x191e4de64 in _BlockUntilNextEventMatchingListInModeWithFilter+0x44 (HIToolbox:arm64e+0x31e64)
    #29 0x18bd76518 in _DPSNextEvent+0x358 (AppKit:arm64e+0x41518)

>
> previously allocated by thread T0 here:
>     #0 0x103332ca8 in wrap_malloc+0x94 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3eca8)
>     #1 0x1005ae8fc in lmalloc alloc.c:1361
>     #2 0x1005b0188 in lisp_malloc alloc.c:994
>     #3 0x1005b0a5c in allocate_string_data alloc.c:1889
>     #4 0x1005b1bd8 in make_clear_multibyte_string alloc.c:2475
>     #5 0x1005b1670 in make_clear_string alloc.c:2443
>     #6 0x1005b2714 in make_uninit_string alloc.c:2454
>     #7 0x100666c14 in concat_to_string fns.c:821
>     #8 0x100666420 in concat2 fns.c:600
>     #9 0x1006d7870 in Fget_load_suffixes lread.c:1123
>     #10 0x1006d86ac in Fload lread.c:1296
>     #11 0x1006e1af0 in save_match_data_load lread.c:1630
>     #12 0x10064f8cc in load_with_autoload_queue eval.c:2269
>
> rame #5: 0x00000001005825c4 emacs`re_match_2_internal(bufp=0x000000010111b890, string1=0x0000000000000000, size1=0, string2="/Users/gerd/.config/emacs.d.default/elpa/company-0.9.13/lsp-protocol.el.gz", size2=74, pos=0, regs=0x0000000000000000, stop=74) at regex-emacs.c:4352:18
>    4349	
>    4350		    PREFETCH ();
>    4351		    int len;
> -> 4352		    int corig = RE_STRING_CHAR_AND_LENGTH (d, len, target_multibyte);
>    4353		    int c = corig;
>    4354		    if (target_multibyte)
>    4355		      {
>
> And to make things worse, I can't get an xbacktrace because the "new"
> lldb, which I got with Xcode 14, says it has a bug.  Tadah :-/.
>
> (lldb) xbacktrace
> PLEASE submit a bug report to https://developer.apple.com/bug-reporting/ and include the crash backtrace.
> Stack dump: