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

From: Gerd Möllmann <[email-masked]>
Date: Wed, 05 Oct 2022 11:00:00 +0200

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

> Eli Zaretskii <[email-masked]> writes:
>
>>> What I can see is that, apparently, redisplay got called because Emacs
>>> received a MacOS event, and did a prepare_menu_bars etc etc.
>>
>> You mean, a macOS event can be received asynchronously, and will
>> interrupt some processing in C, like inside regex-emacs.c?
>
> If it can, I don't know.  But is the GC during redisplay is the one
> moving the string, that would be the consequence, I think.
>
>> If that can happen, no code in Emacs is safe, ever.  I don't believe
>> this is possible: we no longer process window-system events
>> asynchronously, AFAIK, and for this very reason.  But maybe macOS is
>> different?  In that case, either we should change the macOS code to
>> avoid doing that, or we should have some means of blocking such
>> "interrupts" around specific code fragments, akin to block_input.
>
> Yeah.  It would be good if that wouldn't happen ever, if it can.

I just got another ASAN error in a branch based on master.  It looks
completely different, but I find it eye-opening for our case.  Look at
this:

==45724==ERROR: AddressSanitizer: heap-use-after-free on address 0x000107130d00 at pc 0x0001002a4b04 bp 0x00016fd155e0 sp 0x00016fd155d8
READ of size 8 at 0x000107130d00 thread T0
    #0 0x1002a4b00 in PSEUDOVECTORP lisp.h:1110
    #1 0x1002a4b70 in SYMBOL_WITH_POS_P lisp.h:1122
    #2 0x10025a620 in EQ lisp.h:1342
    #3 0x100281198 in run_window_change_functions window.c:3964
    #4 0x1000f1bac in redisplay_internal xdisp.c:16600
    #5 0x100107ee0 in redisplay xdisp.c:16111
    #6 0x10089366c in -[EmacsView layoutSublayersOfLayer:] nsterm.m:8661
    #7 0x1900a9624 in CA::Layer::layout_if_needed(CA::Transaction*)+0x224 (QuartzCore:arm64e+0x20624)
    #8 0x1901f661c in CA::Context::commit_transaction(CA::Transaction*, double, double*)+0x1c0 (QuartzCore:arm64e+0x16d61c)
    #9 0x19008b4c8 in CA::Transaction::commit()+0x2bc (QuartzCore:arm64e+0x24c8)
    #10 0x18bee1698 in __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke+0x12c (AppKit:arm64e+0x1ac698)
    #11 0x18c646754 in ___NSRunLoopObserverCreateWithHandler_block_invoke+0x3c (AppKit:arm64e+0x911754)
    #12 0x1892101a0 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__+0x20 (CoreFoundation:arm64e+0x841a0)
    #13 0x18920fff0 in __CFRunLoopDoObservers+0x24c (CoreFoundation:arm64e+0x83ff0)
    #14 0x18920f524 in __CFRunLoopRun+0x300 (CoreFoundation:arm64e+0x83524)
    #15 0x18920ea80 in CFRunLoopRunSpecific+0x254 (CoreFoundation:arm64e+0x82a80)
    #16 0x191e4e334 in RunCurrentEventLoopInMode+0x120 (HIToolbox:arm64e+0x32334)
    #17 0x191e4dfc0 in ReceiveNextEventCommon+0x140 (HIToolbox:arm64e+0x31fc0)
    #18 0x191e4de64 in _BlockUntilNextEventMatchingListInModeWithFilter+0x44 (HIToolbox:arm64e+0x31e64)
    #19 0x18bd76518 in _DPSNextEvent+0x358 (AppKit:arm64e+0x41518)
    #20 0x18bd74e10 in -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]+0x52c (AppKit:arm64e+0x3fe10)
    #21 0x18bd66fdc in -[NSApplication run]+0x250 (AppKit:arm64e+0x31fdc)
    #22 0x100870bd0 in -[EmacsApp run] nsterm.m:5799
    #23 0x1008c7b2c in ns_read_socket_1 nsterm.m:4679
    #24 0x1008ae550 in ns_read_socket nsterm.m:4697
    #25 0x100437394 in gobble_input keyboard.c:7379
    #26 0x100438bfc in handle_async_input keyboard.c:7610
    #27 0x100438bdc in process_pending_signals keyboard.c:7624
    #28 0x10064bd90 in probably_quit eval.c:1657
    #29 0x10065fe6c in maybe_quit lisp.h:3737
    #30 0x10066cb7c in Fmemq fns.c:1837
    #31 0x100645de8 in FletX eval.c:936

There is a path from maybe_quit to redisplay, and didn't we have
maybe_quit alreasy in the matcher code?  Mind-boggling!