Repository: neovim
Issue: crash: "abort() called" with `vim.regex():match_str` from a luv callback
Labels: bug-crash,event-loop,lua

### Neovim version (nvim -v)

NVIM v0.7.0-dev+1453-g8486c87e5

### Vim (not Nvim) behaves the same?

N/A

### Operating system/version

macOS 12.3.1

### Terminal name/version

Alacritty 0.10.1

### $TERM environment variable

screen-256color

### Installation

Manually compiled

### How to reproduce the issue

I ran into this issue while implementing filewatch for LSP, so I actually saw this issue in a callback for fs_event, but it's easier to reproduce in a snippet with a timer.

```
% cat >file.lua
local re = vim.regex("^.*\\.go$")

local timer = vim.loop.new_timer()
timer:start(200, 200, function()
  local files = {
    "internal/config/config.go";
  }
  for _, file in ipairs(files) do
    if re:match_str(file) then
      print("it matches!")
    end
  end
end)
^-D
% nvim -E --clean 'source file.lua'
# after some time:
Entering Ex mode.  Type "visual" to go to Normal mode.
:zsh: abort      nvim --clean -E +'source file.lua'
```

(can also repro in non Ex mode, just sharing those instructions for simplicity).

Note: If I `vim.schedule_wrap` the callback it works fine, but my understanding is that that shouldn't be required for vim.regex? Let me know if my understanding is incorrect.

### Expected behavior

"it matches!" gets printed every 200ms.

### Actual behavior

Neovim crashes, terminal says:

```
:zsh: abort      nvim --clean -E +'source file.lua'
```

Crash report from Console.app says:

```
-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               nvim [90675]
Path:                  /Users/USER/*/nvim
Identifier:            nvim
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        zsh [87722]
Responsible:           alacritty [32426]
User ID:               501

Date/Time:             2022-04-14 23:07:10.7689 -0400
OS Version:            macOS 12.3.1 (21E258)
Report Version:        12
Anonymous UUID:        DAA217B7-03D8-C9A5-C0F6-7D50DA273EA4

Sleep/Wake UUID:       156BBEDB-9FBE-44E1-A6DC-8974693BB62C

Time Awake Since Boot: 250000 seconds
Time Since Wake:       1408 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
abort() called


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	       0x198248db8 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x19827dee0 pthread_kill + 288
2   libsystem_c.dylib             	       0x1981b8340 abort + 168
3   nvim                          	       0x1050b6554 loop_poll_events + 352
4   nvim                          	       0x10524a324 nfa_regmatch + 14804
5   nvim                          	       0x105245ef8 nfa_regexec_both + 1132
6   nvim                          	       0x10521d774 nfa_regexec_nl + 80
7   nvim                          	       0x10521c2a4 vim_regexec_string + 280
8   nvim                          	       0x10515b114 regex_match_str + 112
9   nvim                          	       0x104f47d58 lj_BC_FUNCC + 44
10  nvim                          	       0x104f5db4c lua_pcall + 228 (lj_api.c:1145)
11  nvim                          	       0x104efff0c uv__run_timers + 40 (timer.c:178)
12  nvim                          	       0x104f02fac uv_run + 448 (core.c:413)
13  nvim                          	       0x1050b6464 loop_poll_events + 112
14  nvim                          	       0x1051f9868 inbuf_poll + 620
15  nvim                          	       0x1051f9304 os_inchar + 124
16  nvim                          	       0x1052c0f10 state_enter + 368
17  nvim                          	       0x1050f19f0 command_line_enter + 2724
18  nvim                          	       0x1050d4500 do_cmdline + 2120
19  nvim                          	       0x1050d3b04 do_exmode + 364
20  nvim                          	       0x1051c2cc8 normal_check + 2356
21  nvim                          	       0x1052c0e0c state_enter + 108
22  nvim                          	       0x1051634dc main + 15560
23  dyld                          	       0x1056d5088 start + 516

Thread 1:
0   libsystem_pthread.dylib       	       0x198279078 start_wqthread + 0

Thread 2:
0   libsystem_kernel.dylib        	       0x198246c40 kevent + 8
1   nvim                          	       0x104f13cc4 uv__io_poll + 768 (kqueue.c:231)
2   nvim                          	       0x104f02f70 uv_run + 388 (core.c:391)
3   nvim                          	       0x1050b6518 loop_poll_events + 292
4   nvim                          	       0x1052e6944 tui_main + 780
5   libsystem_pthread.dylib       	       0x19827e26c _pthread_start + 148
6   libsystem_pthread.dylib       	       0x19827908c thread_start + 8


Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000001   x5: 0x0000000000000078   x6: 0x0000000000000000   x7: 0x0000000000000000
    x8: 0x0058d3261a44711f   x9: 0x0058d3271f30f49f  x10: 0x000000010535cf96  x11: 0x000000010524ae30
   x12: 0x0000000000000246  x13: 0x0000000000000001  x14: 0x0000000000000001  x15: 0x0000000000000003
   x16: 0x0000000000000148  x17: 0x00000001f20eb640  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000105748580  x21: 0x0000000000000103  x22: 0x0000000105748660  x23: 0x000000016af18190
   x24: 0x0000000132008610  x25: 0x0000000000000001  x26: 0x0000000132008200  x27: 0x0000000000000069
   x28: 0x0000000126ec8da0   fp: 0x000000016af17c60   lr: 0x000000019827dee0
    sp: 0x000000016af17c40   pc: 0x0000000198248db8 cpsr: 0x40001000
   far: 0x0000000104b58000  esr: 0x56000080  Address size fault

Binary Images:
       0x19823f000 -        0x198276fff libsystem_kernel.dylib (*) <1d7b3b8e-75a1-34ea-aa52-9f7c23155c55> /usr/lib/system/libsystem_kernel.dylib
       0x198277000 -        0x198283fff libsystem_pthread.dylib (*) <cee8bc77-6923-34d9-89a3-6f8f7279605e> /usr/lib/system/libsystem_pthread.dylib
       0x19813e000 -        0x1981bffff libsystem_c.dylib (*) <fd566a15-42d8-314a-a99a-b59237ddf5bc> /usr/lib/system/libsystem_c.dylib
       0x104ee4000 -        0x1053bffff nvim (*) <f1a50b76-289f-38ec-9b78-426c2b9a6a8d> /Users/USER/*/nvim
       0x1056d0000 -        0x10572ffff dyld (*) <fbb89662-e6f2-3434-b542-f75185ac5e74> /usr/lib/dyld

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=603.6M resident=0K(0%) swapped_out_or_unallocated=603.6M(100%)
Writable regions: Total=709.7M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=709.7M(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Activity Tracing                   256K        1 
Kernel Alloc Once                   32K        1 
MALLOC                           188.2M       20 
MALLOC guard page                   96K        4 
MALLOC_MEDIUM (reserved)         120.0M        1         reserved VM address space (unallocated)
MALLOC_NANO (reserved)           384.0M        1         reserved VM address space (unallocated)
STACK GUARD                       56.0M        3 
Stack                             16.5M        3 
VM_ALLOCATE                        704K        6 
__AUTH                              53K       12 
__AUTH_CONST                      1029K       45 
__DATA                             419K       46 
__DATA_CONST                      2962K       48 
__DATA_DIRTY                        93K       23 
__LINKEDIT                       585.2M        4 
__OBJC_CONST                        37K        6 
__OBJC_RO                         82.9M        1 
__OBJC_RW                         3152K        1 
__TEXT                            18.4M       51 
__UNICODE                          592K        1 
dyld private memory               1024K        1 
mapped file                         48K        1 
shared memory                       64K        4 
===========                     =======  ======= 
TOTAL                              1.4G      284 
TOTAL, minus reserved VM space   957.6M      284 



-----------
Full Report
-----------

{"app_name":"nvim","timestamp":"2022-04-14 23:07:10.00 -0400","app_version":"","slice_uuid":"f1a50b76-289f-38ec-9b78-426c2b9a6a8d","build_version":"","platform":1,"share_with_app_devs":0,"is_first_party":1,"bug_type":"309","os_version":"macOS 12.3.1 (21E258)","incident_id":"776D1786-CC93-43D4-9F65-9F1260C4E457","name":"nvim"}
{
  "uptime" : 250000,
  "procLaunch" : "2022-04-14 23:06:53.4584 -0400",
  "procRole" : "Unspecified",
  "version" : 2,
  "userID" : 501,
  "deployVersion" : 210,
  "modelCode" : "MacBookPro18,4",
  "procStartAbsTime" : 6186002913191,
  "coalitionID" : 1058,
  "osVersion" : {
    "train" : "macOS 12.3.1",
    "build" : "21E258",
    "releaseType" : "User"
  },
  "captureTime" : "2022-04-14 23:07:10.7689 -0400",
  "incident" : "776D1786-CC93-43D4-9F65-9F1260C4E457",
  "bug_type" : "309",
  "pid" : 90675,
  "procExitAbsTime" : 6186418291853,
  "translated" : false,
  "cpuType" : "ARM-64",
  "procName" : "nvim",
  "procPath" : "\/Users\/USER\/*\/nvim",
  "parentProc" : "zsh",
  "parentPid" : 87722,
  "coalitionName" : "io.alacritty",
  "crashReporterKey" : "DAA217B7-03D8-C9A5-C0F6-7D50DA273EA4",
  "responsiblePid" : 32426,
  "responsibleProc" : "alacritty",
  "wakeTime" : 1408,
  "sleepWakeUUID" : "156BBEDB-9FBE-44E1-A6DC-8974693BB62C",
  "sip" : "enabled",
  "isCorpse" : 1,
  "exception" : {"codes":"0x0000000000000000, 0x0000000000000000","rawCodes":[0,0],"type":"EXC_CRASH","signal":"SIGABRT"},
  "asi" : {"libsystem_c.dylib":["abort() called"]},
  "extMods" : {"caller":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"system":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"targeted":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"warnings":0},
  "faultingThread" : 0,
  "threads" : [{"triggered":true,"id":1736439,"threadState":{"x":[{"value":0},{"value":0},{"value":0},{"value":0},{"value":1},{"value":120},{"value":0},{"value":0},{"value":25001958553448735},{"value":25001962931025055},{"value":4382379926,"symbolLocation":6097,"symbol":"push_event_key.key"},{"value":4381257264,"symbolLocation":2524,"symbol":"addstate"},{"value":582},{"value":1},{"value":1},{"value":3},{"value":328},{"value":8356017728},{"value":0},{"value":6},{"value":4386489728,"symbolLocation":0,"symbol":"_main_thread"},{"value":259},{"value":4386489952,"symbolLocation":224,"symbol":"_main_thread"},{"value":6089179536},{"value":5133862416},{"value":1},{"value":5133861376},{"value":105},{"value":4948004256}],"flavor":"ARM_THREAD_STATE64","lr":{"value":6847717088},"cpsr":{"value":1073745920},"fp":{"value":6089178208},"sp":{"value":6089178176},"esr":{"value":1442840704,"description":" Address size fault"},"pc":{"value":6847499704,"matchesCrashFrame":1},"far":{"value":4373970944}},"queue":"com.apple.main-thread","frames":[{"imageOffset":40376,"symbol":"__pthread_kill","symbolLocation":8,"imageIndex":0},{"imageOffset":28384,"symbol":"pthread_kill","symbolLocation":288,"imageIndex":1},{"imageOffset":500544,"symbol":"abort","symbolLocation":168,"imageIndex":2},{"imageOffset":1910100,"symbol":"loop_poll_events","symbolLocation":352,"imageIndex":3},{"imageOffset":3564324,"symbol":"nfa_regmatch","symbolLocation":14804,"imageIndex":3},{"imageOffset":3546872,"symbol":"nfa_regexec_both","symbolLocation":1132,"imageIndex":3},{"imageOffset":3381108,"symbol":"nfa_regexec_nl","symbolLocation":80,"imageIndex":3},{"imageOffset":3375780,"symbol":"vim_regexec_string","symbolLocation":280,"imageIndex":3},{"imageOffset":2584852,"symbol":"regex_match_str","symbolLocation":112,"imageIndex":3},{"imageOffset":408920,"symbol":"lj_BC_FUNCC","symbolLocation":44,"imageIndex":3},{"imageOffset":498508,"sourceLine":1145,"sourceFile":"lj_api.c","symbol":"lua_pcall","imageIndex":3,"symbolLocation":228},{"imageOffset":114444,"sourceLine":178,"sourceFile":"timer.c","symbol":"uv__run_timers","imageIndex":3,"symbolLocation":40},{"imageOffset":126892,"sourceLine":413,"sourceFile":"core.c","symbol":"uv_run","imageIndex":3,"symbolLocation":448},{"imageOffset":1909860,"symbol":"loop_poll_events","symbolLocation":112,"imageIndex":3},{"imageOffset":3233896,"symbol":"inbuf_poll","symbolLocation":620,"imageIndex":3},{"imageOffset":3232516,"symbol":"os_inchar","symbolLocation":124,"imageIndex":3},{"imageOffset":4050704,"symbol":"state_enter","symbolLocation":368,"imageIndex":3},{"imageOffset":2152944,"symbol":"command_line_enter","symbolLocation":2724,"imageIndex":3},{"imageOffset":2032896,"symbol":"do_cmdline","symbolLocation":2120,"imageIndex":3},{"imageOffset":2030340,"symbol":"do_exmode","symbolLocation":364,"imageIndex":3},{"imageOffset":3009736,"symbol":"normal_check","symbolLocation":2356,"imageIndex":3},{"imageOffset":4050444,"symbol":"state_enter","symbolLocation":108,"imageIndex":3},{"imageOffset":2618588,"symbol":"main","symbolLocation":15560,"imageIndex":3},{"imageOffset":20616,"symbol":"start","symbolLocation":516,"imageIndex":4}]},{"id":1736441,"frames":[{"imageOffset":8312,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":1}]},{"id":1736443,"frames":[{"imageOffset":31808,"symbol":"kevent","symbolLocation":8,"imageIndex":0},{"imageOffset":195780,"sourceLine":231,"sourceFile":"kqueue.c","symbol":"uv__io_poll","imageIndex":3,"symbolLocation":768},{"imageOffset":126832,"sourceLine":391,"sourceFile":"core.c","symbol":"uv_run","imageIndex":3,"symbolLocation":388},{"imageOffset":1910040,"symbol":"loop_poll_events","symbolLocation":292,"imageIndex":3},{"imageOffset":4204868,"symbol":"tui_main","symbolLocation":780,"imageIndex":3},{"imageOffset":29292,"symbol":"_pthread_start","symbolLocation":148,"imageIndex":1},{"imageOffset":8332,"symbol":"thread_start","symbolLocation":8,"imageIndex":1}]}],
  "usedImages" : [
  {
    "source" : "P",
    "arch" : "arm64e",
    "base" : 6847459328,
    "size" : 229376,
    "uuid" : "1d7b3b8e-75a1-34ea-aa52-9f7c23155c55",
    "path" : "\/usr\/lib\/system\/libsystem_kernel.dylib",
    "name" : "libsystem_kernel.dylib"
  },
  {
    "source" : "P",
    "arch" : "arm64e",
    "base" : 6847688704,
    "size" : 53248,
    "uuid" : "cee8bc77-6923-34d9-89a3-6f8f7279605e",
    "path" : "\/usr\/lib\/system\/libsystem_pthread.dylib",
    "name" : "libsystem_pthread.dylib"
  },
  {
    "source" : "P",
    "arch" : "arm64e",
    "base" : 6846406656,
    "size" : 532480,
    "uuid" : "fd566a15-42d8-314a-a99a-b59237ddf5bc",
    "path" : "\/usr\/lib\/system\/libsystem_c.dylib",
    "name" : "libsystem_c.dylib"
  },
  {
    "source" : "P",
    "arch" : "arm64",
    "base" : 4377690112,
    "size" : 5095424,
    "uuid" : "f1a50b76-289f-38ec-9b78-426c2b9a6a8d",
    "path" : "\/Users\/USER\/*\/nvim",
    "name" : "nvim"
  },
  {
    "source" : "P",
    "arch" : "arm64e",
    "base" : 4385996800,
    "size" : 393216,
    "uuid" : "fbb89662-e6f2-3434-b542-f75185ac5e74",
    "path" : "\/usr\/lib\/dyld",
    "name" : "dyld"
  }
],
  "sharedCache" : {
  "base" : 6844399616,
  "size" : 3136438272,
  "uuid" : "1df3dfc1-141a-35d0-a4e5-f1e113894c6e"
},
  "vmSummary" : "ReadOnly portion of Libraries: Total=603.6M resident=0K(0%) swapped_out_or_unallocated=603.6M(100%)\nWritable regions: Total=709.7M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=709.7M(100%)\n\n                                VIRTUAL   REGION \nREGION TYPE                        SIZE    COUNT (non-coalesced) \n===========                     =======  ======= \nActivity Tracing                   256K        1 \nKernel Alloc Once                   32K        1 \nMALLOC                           188.2M       20 \nMALLOC guard page                   96K        4 \nMALLOC_MEDIUM (reserved)         120.0M        1         reserved VM address space (unallocated)\nMALLOC_NANO (reserved)           384.0M        1         reserved VM address space (unallocated)\nSTACK GUARD                       56.0M        3 \nStack                             16.5M        3 \nVM_ALLOCATE                        704K        6 \n__AUTH                              53K       12 \n__AUTH_CONST                      1029K       45 \n__DATA                             419K       46 \n__DATA_CONST                      2962K       48 \n__DATA_DIRTY                        93K       23 \n__LINKEDIT                       585.2M        4 \n__OBJC_CONST                        37K        6 \n__OBJC_RO                         82.9M        1 \n__OBJC_RW                         3152K        1 \n__TEXT                            18.4M       51 \n__UNICODE                          592K        1 \ndyld private memory               1024K        1 \nmapped file                         48K        1 \nshared memory                       64K        4 \n===========                     =======  ======= \nTOTAL                              1.4G      284 \nTOTAL, minus reserved VM space   957.6M      284 \n",
  "legacyInfo" : {
  "threadTriggered" : {
    "queue" : "com.apple.main-thread"
  }
},
  "trialInfo" : {
  "rollouts" : [
    {
      "rolloutId" : "6112e14f37f5d11121dcd519",
      "factorPackIds" : {
        "SIRI_TEXT_TO_SPEECH" : "6254bfaedadcf4388c93b4b4"
      },
      "deploymentId" : 240000090
    },
    {
      "rolloutId" : "60da5e84ab0ca017dace9abf",
      "factorPackIds" : {

      },
      "deploymentId" : 240000008
    }
  ],
  "experiments" : [

  ]
}
}
```