Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell crashes when read times out #103

Closed
posguy99 opened this issue Aug 1, 2020 · 49 comments
Closed

shell crashes when read times out #103

posguy99 opened this issue Aug 1, 2020 · 49 comments
Labels
bug Something is not working

Comments

@posguy99
Copy link

posguy99 commented Aug 1, 2020

[ 6:38 PM][ttys001][~/test/bz573936]
[211] mbp13 $ type ksh
ksh is a tracked alias for /usr/local/bin/ksh
[ 6:38 PM][ttys001][~/test/bz573936]
[212] mbp13 $ ksh
[ 6:38 PM][ttys001][~/test/bz573936]
[213] mbp13 $ TMOUT=5
[ 6:38 PM][ttys001][~/test/bz573936]
[214] mbp13 $ read
[ 6:39 PM][ttys001][~/test/bz573936]
[215] mbp13 $                           
shell will timeout in 60 seconds due to inactivity
Memory fault
[ 6:39 PM][ttys001][~/test/bz573936]
[215] mbp13 $

I read https://bugzilla.redhat.com/show_bug.cgi?id=573936 and tested it, didn't see the behavior the bug talked about, figured it must no longer be a problem.

Then the window I was running ksh in closed.

The read timed out after 5 seconds, and the prompt came back. Then ksh popped the timeout message and the memory fault, and the shell crashed.

I can't make it happen if I put it in a script and call the script, but it happens repeatably if I do it interactively. If I don't set a timeout and just let the read sit there, it doesn't crash.

(macOS 10.15.6, Version AJM 93u+m 2020-07-29)

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

Crash dump:

Process:               ksh [38128]
Path:                  /usr/local/bin/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [37422]
Responsible:           Terminal [406]
User ID:               501

Date/Time:             2020-07-31 18:39:08.197 -0700
OS Version:            Mac OS X 10.15.6 (19G73)
Report Version:        12
Bridge OS Version:     4.6 (17P6065)
Anonymous UUID:        FDAFEF58-17EF-0FD0-8BE4-E164085D539D

Sleep/Wake UUID:       E080ABE0-0646-4FF6-97E4-FD58E5AFC401

Time Awake Since Boot: 32000 seconds
Time Since Wake:       23000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [38128]

VM Regions Near 0:
--> 
    __TEXT                 000000010e202000-000000010e312000 [ 1088K] r-x/r-x SM=COW  /usr/local/bin/ksh

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x000000010e2318ca job_list + 159
1   ksh                           	0x000000010e23156d job_reap + 1581
2   ksh                           	0x000000010e232462 job_wait + 946
3   ksh                           	0x000000010e25de5a sh_exec + 19229
4   ksh                           	0x000000010e2570e1 sh_subshell + 1662
5   ksh                           	0x000000010e23ad9a comsubst + 763
6   ksh                           	0x000000010e23c100 varsub + 3194
7   ksh                           	0x000000010e2395d8 copyto + 2370
8   ksh                           	0x000000010e238be0 sh_mactrim + 274
9   ksh                           	0x000000010e238a6e sh_mactry + 168
10  ksh                           	0x000000010e230934 io_prompt + 197
11  ksh                           	0x000000010e22cad7 slowread + 155
12  ksh                           	0x000000010e2af8c5 sfrd + 1154
13  ksh                           	0x000000010e2abaf3 _sffilbuf + 573
14  ksh                           	0x000000010e2affdb sfreserve + 686
15  ksh                           	0x000000010e2123ee exfile + 1873
16  ksh                           	0x000000010e212b43 sh_main + 1077
17  libdyld.dylib                 	0x00007fff693b6cc9 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x000000010e32c088  rdx: 0xffffffffffffffff
  rdi: 0x000000010e32b8a0  rsi: 0x000000000000000a  rbp: 0x00007ffee19fc3a0  rsp: 0x00007ffee19fc370
   r8: 0x000000010e383bd2   r9: 0x2220002040004600  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x000000010e32b8a0  r13: 0x0000000000000012  r14: 0x000000000000000a  r15: 0x0000000000000001
  rip: 0x000000010e2318ca  rfl: 0x0000000000010287  cr2: 0x0000000000000000
  
Logical CPU:     4
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14


Binary Images:
       0x10e202000 -        0x10e311fff +ksh (0) <EF013A6A-DE5C-382C-A916-0C03067788F9> /usr/local/bin/ksh
       0x11bdb0000 -        0x11be41f47  dyld (750.6) <34A11073-9E4C-38C3-9293-7D566ABAE8B6> /usr/lib/dyld
    0x7fff6639b000 -     0x7fff6639cfff  libSystem.B.dylib (1281.100.1) <001B3B7F-D02C-31D3-B961-1ED445D5A266> /usr/lib/libSystem.B.dylib
    0x7fff66681000 -     0x7fff666d3fff  libc++.1.dylib (902.1) <59A8239F-C28A-3B59-B8FA-11340DC85EDC> /usr/lib/libc++.1.dylib
    0x7fff666d4000 -     0x7fff666e9ffb  libc++abi.dylib (902) <E692F14F-C65E-303B-9921-BB7E97D77855> /usr/lib/libc++abi.dylib
    0x7fff666ea000 -     0x7fff666eafff  libcharset.1.dylib (59) <72447768-9244-39AB-8E79-2FA14EC0AD33> /usr/lib/libcharset.1.dylib
    0x7fff67174000 -     0x7fff67264fff  libiconv.2.dylib (59) <18311A67-E4EF-3CC7-95B3-C0EDEE3A282F> /usr/lib/libiconv.2.dylib
    0x7fff681fb000 -     0x7fff6822efde  libobjc.A.dylib (787.1) <6DF81160-5E7F-3E31-AA1E-C875E3B98AF6> /usr/lib/libobjc.A.dylib
    0x7fff687b1000 -     0x7fff687b4ffb  libutil.dylib (57) <F01467F6-23A7-37EE-A170-33CE1577B41D> /usr/lib/libutil.dylib
    0x7fff69198000 -     0x7fff6919dff3  libcache.dylib (83) <5940876E-AC8A-3BE0-80B3-DE3FB14E257A> /usr/lib/system/libcache.dylib
    0x7fff6919e000 -     0x7fff691a9fff  libcommonCrypto.dylib (60165.120.1) <C095BD55-1D27-337F-9B02-885E1C7FF87A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff691aa000 -     0x7fff691b1fff  libcompiler_rt.dylib (101.2) <6E80AC11-A277-31FA-AEEF-E5A528274C77> /usr/lib/system/libcompiler_rt.dylib
    0x7fff691b2000 -     0x7fff691bbff7  libcopyfile.dylib (166.40.1) <EB5E0BC8-873D-3546-A40E-C36DC46FA8F6> /usr/lib/system/libcopyfile.dylib
    0x7fff691bc000 -     0x7fff6924efdb  libcorecrypto.dylib (866.140.1) <0B6C52DB-5A50-3FCD-8B5E-C0C2F35857E3> /usr/lib/system/libcorecrypto.dylib
    0x7fff6935b000 -     0x7fff6939bff0  libdispatch.dylib (1173.100.2) <EAD535EE-1270-39A9-A254-95CF117FF3B0> /usr/lib/system/libdispatch.dylib
    0x7fff6939c000 -     0x7fff693d2fff  libdyld.dylib (750.6) <24C41E8B-6B33-30C7-94C9-02D2BD051D66> /usr/lib/system/libdyld.dylib
    0x7fff693d3000 -     0x7fff693d3ffb  libkeymgr.dylib (30) <6F582FDB-EB1A-3ED2-A989-B750643E2647> /usr/lib/system/libkeymgr.dylib
    0x7fff693e1000 -     0x7fff693e1ff7  liblaunch.dylib (1738.140.1) <AFBCBDD3-0B55-3ECD-8E04-A73A3A57356B> /usr/lib/system/liblaunch.dylib
    0x7fff693e2000 -     0x7fff693e7ff7  libmacho.dylib (959.0.1) <1B0296B5-3FD0-342C-BCC2-9886351A4391> /usr/lib/system/libmacho.dylib
    0x7fff693e8000 -     0x7fff693eaff3  libquarantine.dylib (110.40.3) <67FE2676-F9E8-3797-AEE5-F5F9D191CFA3> /usr/lib/system/libquarantine.dylib
    0x7fff693eb000 -     0x7fff693ecff7  libremovefile.dylib (48) <5CEBDAB2-988A-3B66-87BC-3C45D1C08730> /usr/lib/system/libremovefile.dylib
    0x7fff693ed000 -     0x7fff69404ff3  libsystem_asl.dylib (377.60.2) <7A07FF86-658E-35D7-8136-829737E98B7B> /usr/lib/system/libsystem_asl.dylib
    0x7fff69405000 -     0x7fff69405ff7  libsystem_blocks.dylib (74) <0D53847E-AF5F-3ACF-B51F-A15DEA4DEC58> /usr/lib/system/libsystem_blocks.dylib
    0x7fff69406000 -     0x7fff6948dfff  libsystem_c.dylib (1353.100.2) <AF7873B0-AC4A-3C67-89BB-B8DA87718DAE> /usr/lib/system/libsystem_c.dylib
    0x7fff6948e000 -     0x7fff69491ffb  libsystem_configuration.dylib (1061.141.1) <0EE84C33-64FD-372B-974A-AF7A136F2068> /usr/lib/system/libsystem_configuration.dylib
    0x7fff69492000 -     0x7fff69495fff  libsystem_coreservices.dylib (114) <08B89E9B-C5B2-3E73-8964-03E58692B21F> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff69496000 -     0x7fff6949efff  libsystem_darwin.dylib (1353.100.2) <BD5BAD5B-AC3F-371A-B4FC-ADF86D6DCE51> /usr/lib/system/libsystem_darwin.dylib
    0x7fff6949f000 -     0x7fff694a6fff  libsystem_dnssd.dylib (1096.100.3) <01E0965B-83E5-356C-AC1F-C723F5AAB483> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff694a7000 -     0x7fff694a8ffb  libsystem_featureflags.dylib (17) <6317641E-B43F-36A3-974E-6073786B94B9> /usr/lib/system/libsystem_featureflags.dylib
    0x7fff694a9000 -     0x7fff694f6ff7  libsystem_info.dylib (538) <4DD813CB-D7B0-3AB5-9054-D2E4FF7A64B9> /usr/lib/system/libsystem_info.dylib
    0x7fff694f7000 -     0x7fff69523ff7  libsystem_kernel.dylib (6153.141.1) <2B6311E6-6240-3EF7-8C87-475B66F7452C> /usr/lib/system/libsystem_kernel.dylib
    0x7fff69524000 -     0x7fff6956bfff  libsystem_m.dylib (3178) <77A9D888-36E9-3968-8103-C82FFFC60B9E> /usr/lib/system/libsystem_m.dylib
    0x7fff6956c000 -     0x7fff69593fff  libsystem_malloc.dylib (283.100.6) <059F4DBD-856A-3A59-8B20-FD4B6918C5F2> /usr/lib/system/libsystem_malloc.dylib
    0x7fff69594000 -     0x7fff695a1ffb  libsystem_networkextension.dylib (1095.140.2) <4CCE78C9-1DC4-3375-9828-D5BD739D23F3> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff695a2000 -     0x7fff695abff7  libsystem_notify.dylib (241.100.2) <DEAD75F2-DD34-3E8F-82DE-344625A2C25E> /usr/lib/system/libsystem_notify.dylib
    0x7fff695ac000 -     0x7fff695b4fef  libsystem_platform.dylib (220.100.1) <97E825F6-D823-366C-9FF3-B1C8EA891044> /usr/lib/system/libsystem_platform.dylib
    0x7fff695b5000 -     0x7fff695bffff  libsystem_pthread.dylib (416.100.3) <80B053AA-B6F7-3B59-BC44-78A5A4F7368F> /usr/lib/system/libsystem_pthread.dylib
    0x7fff695c0000 -     0x7fff695c4ff3  libsystem_sandbox.dylib (1217.140.4) <20BF94E2-DAB8-3EBE-AACE-99DF84C1C391> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff695c5000 -     0x7fff695c7fff  libsystem_secinit.dylib (62.100.2) <28EDEB6F-899B-373D-B761-4C20D39285D9> /usr/lib/system/libsystem_secinit.dylib
    0x7fff695c8000 -     0x7fff695cfffb  libsystem_symptoms.dylib (1238.120.1) <22800D8C-5C2A-3171-8C29-311BBE67F198> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff695d0000 -     0x7fff695e6ff2  libsystem_trace.dylib (1147.120) <DC00C77E-4709-31F0-B913-CF09559B7A6F> /usr/lib/system/libsystem_trace.dylib
    0x7fff695e8000 -     0x7fff695edff7  libunwind.dylib (35.4) <42B7B509-BAFE-365B-893A-72414C92F5BF> /usr/lib/system/libunwind.dylib
    0x7fff695ee000 -     0x7fff69623ffe  libxpc.dylib (1738.140.1) <58E276A9-EE11-3F02-9D3E-5371E604E677> /usr/lib/system/libxpc.dylib

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: 26493
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=395.5M resident=0K(0%) swapped_out_or_unallocated=395.5M(100%)
Writable regions: Total=293.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=293.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            45.1M       16 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         240.0M        2         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        576K        6 
__DATA                             651K       44 
__DATA_CONST                        80K        2 
__LINKEDIT                       388.8M        3 
__OBJC_RO                         32.3M        1 
__OBJC_RW                         1908K        2 
__TEXT                            6820K       43 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            780.0M      130 
TOTAL, minus reserved VM space   540.0M      130 

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

Slightly different behavior with the shipping ksh93u+ 20120801...

[ 7:02 PM][ttys001][~/test/bz573936]
[236] mbp13 $ /bin/ksh
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[237] mbp13 $ TMOUT=5
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[238] mbp13 $ read
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[239] mbp13 $ echo  
shell will timeout in 60 seconds due to inactivity
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[239] mbp13 $ echo $KSH_VERSION
Version AJM 93u+ 2012-08-01
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[240] mbp13 $ 
shell will timeout in 60 seconds due to inactivity
[Jul 31 19:02:02 PM][ttys001][~/test/bz573936]
[240] mbp13 $ /bin/ksh: timed out waiting for input

[ 7:03 PM][ttys001][~/test/bz573936]
[240] mbp13 $ echo $KSH_VERSION
Version AJM 93u+m 2020-07-29
[ 7:04 PM][ttys001][~/test/bz573936]
[241] mbp13 $ 

So ksh-20120801 exits cleanly, since I'd set TMOUT, but u+m segfaults. Both of them have the 60 seconds behavior.

Leaving aside the bizarre behavior that the interactive shell should exit just because I'd set the timeout, but that's the documented behavior...

              TMOUT  If  set  to  a value greater than zero, TMOUT will be the default timeout value for the
                     read built-in command.  The select compound command terminates after TMOUT seconds when
                     input is from a terminal.  Otherwise, the shell will terminate if a line is not entered
                     within the prescribed number of seconds while reading from a terminal.  (Note that  the
                     shell can be compiled with a maximum bound for this value which cannot be exceeded.)

(those timestamps look odd because 20120801 doesn't have the %T fix)

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

I've got more than one crash file, sometimes it looks like this:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                             0x0000000102b52dcb job_unpost + 49
1   ksh                             0x0000000102b5357a job_reap + 1594
2   ksh                             0x0000000102b54650 job_wait + 1440
3   ksh                             0x0000000102b7f58b sh_exec + 16974
4   ksh                             0x0000000102b7ca6a sh_exec + 5933
5   ksh                             0x0000000102b790e1 sh_subshell + 1662

Sometimes job_list, sometimes job_unpost.

@McDutchie
Copy link

Very odd. Try as I may, I cannot reproduce this crash at all, either on current 93u+m or 93u+. I'm on macOS 10.14.6.

Could you try moving your existing arch directory out of the way and then compiling a ksh from scratch using:

bin/package make CCFLAGS='-O0 -g'

This disables compiler optimisations and adds extra debugging information. If you can reproduce the crash with that ksh, then the crash log will show the exact source code line where the crash occurred, which will help in figuring out what happens.

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

Ok, I got three test failures when I compiled with that line...

[ 7:24 AM][ttys000][~/src/ksh][master]
[694] mbp13 $ bin/shtests path
#### Regression-testing /Users/mwilson/src/ksh/arch/darwin.i386-64/bin/ksh ####
test path begins at 2020-08-01+07:26:03
shtests: line 348: 74724: Illegal instruction
test path failed at 2020-08-01+07:26:03 with exit code 260 [ 73 tests (killed by SIGILL) ]
test path(C.UTF-8) begins at 2020-08-01+07:26:03
shtests: line 348: 74861: Illegal instruction
test path(C.UTF-8) failed at 2020-08-01+07:26:03 with exit code 260 [ 73 tests (killed by SIGILL) ]
test path(shcomp) begins at 2020-08-01+07:26:03
shtests: line 381: 74996: Illegal instruction
test path(shcomp) failed at 2020-08-01+07:26:04 with exit code 260 [ 73 tests (killed by SIGILL) ]
Total errors: 3
CPU time       user:      system:
main:      0m00.009s    0m00.022s
tests:     0m00.405s    0m00.598s

But it still works when I launch it and I can make it crash...

[269] mbp13 $ ls -l src/ksh/arch/darwin.i386-64/bin/ksh
-rwxr-xr-x  1 mwilson  staff  2100812 Aug  1 07:20 src/ksh/arch/darwin.i386-64/bin/ksh*
[ 7:24 AM][ttys001][~]
[270] mbp13 $ src/ksh/arch/darwin.i386-64/bin/ksh      
[ 7:26 AM][ttys001][~]
[271] mbp13 $ echo $KSH_VERSION
Version AJM 93u+m 2020-07-31
[ 7:27 AM][ttys001][~]
[272] mbp13 $ TMOUT=5
[ 7:27 AM][ttys001][~]
[273] mbp13 $ read
[ 7:27 AM][ttys001][~]
[274] mbp13 $ 
shell will timeout in 60 seconds due to inactivity
Memory fault
[ 7:27 AM][ttys001][~]
[274] mbp13 $ echo $KSH_VERSION
Version AJM 93u+m 2020-07-29

And here's the crash dump for it.

Process:               ksh [76030]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [42903]
Responsible:           Terminal [406]
User ID:               501

Date/Time:             2020-08-01 07:33:41.730 -0700
OS Version:            Mac OS X 10.15.6 (19G73)
Report Version:        12
Bridge OS Version:     4.6 (17P6065)
Anonymous UUID:        FDAFEF58-17EF-0FD0-8BE4-E164085D539D

Sleep/Wake UUID:       FCE88173-FDC2-4498-9BE5-529C99C76DC6

Time Awake Since Boot: 75000 seconds
Time Since Wake:       5500 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000000000000002c
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [76030]

VM Regions Near 0x2c:
--> 
    __TEXT                 000000010b4f0000-000000010b69a000 [ 1704K] r-x/r-x SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x000000010b53cbe3 job_unpost + 19 (jobs.c:1653)
1   ksh                           	0x000000010b53d76d job_reap + 2349 (jobs.c:467)
2   ksh                           	0x000000010b53f321 job_wait + 1857 (jobs.c:1477)
3   ksh                           	0x000000010b5828ed sh_exec + 12285 (xec.c:1623)
4   ksh                           	0x000000010b57bfe7 sh_subshell + 2551 (subshell.c:607)
5   ksh                           	0x000000010b54d57c comsubst + 2588 (macro.c:2185)
6   ksh                           	0x000000010b54e0f1 varsub + 1409 (macro.c:1169)
7   ksh                           	0x000000010b54ab92 copyto + 3186 (macro.c:633)
8   ksh                           	0x000000010b549e62 sh_mactrim + 418 (macro.c:184)
9   ksh                           	0x000000010b549c45 sh_mactry + 197 (macro.c:145)
10  ksh                           	0x000000010b53c49b io_prompt + 363 (io.c:2094)
11  ksh                           	0x000000010b5361b5 slowread + 261 (io.c:1933)
12  ksh                           	0x000000010b61d9b4 sfrd + 1332 (sfrd.c:253)
13  ksh                           	0x000000010b615e71 _sffilbuf + 1105 (sffilbuf.c:105)
14  ksh                           	0x000000010b61ed65 sfreserve + 1653
15  ksh                           	0x000000010b508ff1 exfile + 2305 (main.c:528)
16  ksh                           	0x000000010b50a278 sh_main + 3352 (main.c:353)
17  ksh                           	0x000000010b4f0796 main + 38 (pmain.c:45)
18  libdyld.dylib                 	0x00007fff693b6cc9 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x0000000000010000  rdx: 0x000000010b7140d0
  rdi: 0x0000000000000000  rsi: 0x0000000000000001  rbp: 0x00007ffee470dc20  rsp: 0x00007ffee470dbf0
   r8: 0x000000010b735fa0   r9: 0x0000000000000000  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x000000010b53cbe3  rfl: 0x0000000000010206  cr2: 0x000000000000002c
  
Logical CPU:     0
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14


Binary Images:
       0x10b4f0000 -        0x10b699fff +ksh (0) <4EF66B3F-6F6D-3783-BA3E-31EA40FE141B> /Users/USER/*/ksh
       0x11af22000 -        0x11afb3f47  dyld (750.6) <34A11073-9E4C-38C3-9293-7D566ABAE8B6> /usr/lib/dyld
    0x7fff6639b000 -     0x7fff6639cfff  libSystem.B.dylib (1281.100.1) <001B3B7F-D02C-31D3-B961-1ED445D5A266> /usr/lib/libSystem.B.dylib
    0x7fff66681000 -     0x7fff666d3fff  libc++.1.dylib (902.1) <59A8239F-C28A-3B59-B8FA-11340DC85EDC> /usr/lib/libc++.1.dylib
    0x7fff666d4000 -     0x7fff666e9ffb  libc++abi.dylib (902) <E692F14F-C65E-303B-9921-BB7E97D77855> /usr/lib/libc++abi.dylib
    0x7fff666ea000 -     0x7fff666eafff  libcharset.1.dylib (59) <72447768-9244-39AB-8E79-2FA14EC0AD33> /usr/lib/libcharset.1.dylib
    0x7fff67174000 -     0x7fff67264fff  libiconv.2.dylib (59) <18311A67-E4EF-3CC7-95B3-C0EDEE3A282F> /usr/lib/libiconv.2.dylib
    0x7fff681fb000 -     0x7fff6822efde  libobjc.A.dylib (787.1) <6DF81160-5E7F-3E31-AA1E-C875E3B98AF6> /usr/lib/libobjc.A.dylib
    0x7fff687b1000 -     0x7fff687b4ffb  libutil.dylib (57) <F01467F6-23A7-37EE-A170-33CE1577B41D> /usr/lib/libutil.dylib
    0x7fff69198000 -     0x7fff6919dff3  libcache.dylib (83) <5940876E-AC8A-3BE0-80B3-DE3FB14E257A> /usr/lib/system/libcache.dylib
    0x7fff6919e000 -     0x7fff691a9fff  libcommonCrypto.dylib (60165.120.1) <C095BD55-1D27-337F-9B02-885E1C7FF87A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff691aa000 -     0x7fff691b1fff  libcompiler_rt.dylib (101.2) <6E80AC11-A277-31FA-AEEF-E5A528274C77> /usr/lib/system/libcompiler_rt.dylib
    0x7fff691b2000 -     0x7fff691bbff7  libcopyfile.dylib (166.40.1) <EB5E0BC8-873D-3546-A40E-C36DC46FA8F6> /usr/lib/system/libcopyfile.dylib
    0x7fff691bc000 -     0x7fff6924efdb  libcorecrypto.dylib (866.140.1) <0B6C52DB-5A50-3FCD-8B5E-C0C2F35857E3> /usr/lib/system/libcorecrypto.dylib
    0x7fff6935b000 -     0x7fff6939bff0  libdispatch.dylib (1173.100.2) <EAD535EE-1270-39A9-A254-95CF117FF3B0> /usr/lib/system/libdispatch.dylib
    0x7fff6939c000 -     0x7fff693d2fff  libdyld.dylib (750.6) <24C41E8B-6B33-30C7-94C9-02D2BD051D66> /usr/lib/system/libdyld.dylib
    0x7fff693d3000 -     0x7fff693d3ffb  libkeymgr.dylib (30) <6F582FDB-EB1A-3ED2-A989-B750643E2647> /usr/lib/system/libkeymgr.dylib
    0x7fff693e1000 -     0x7fff693e1ff7  liblaunch.dylib (1738.140.1) <AFBCBDD3-0B55-3ECD-8E04-A73A3A57356B> /usr/lib/system/liblaunch.dylib
    0x7fff693e2000 -     0x7fff693e7ff7  libmacho.dylib (959.0.1) <1B0296B5-3FD0-342C-BCC2-9886351A4391> /usr/lib/system/libmacho.dylib
    0x7fff693e8000 -     0x7fff693eaff3  libquarantine.dylib (110.40.3) <67FE2676-F9E8-3797-AEE5-F5F9D191CFA3> /usr/lib/system/libquarantine.dylib
    0x7fff693eb000 -     0x7fff693ecff7  libremovefile.dylib (48) <5CEBDAB2-988A-3B66-87BC-3C45D1C08730> /usr/lib/system/libremovefile.dylib
    0x7fff693ed000 -     0x7fff69404ff3  libsystem_asl.dylib (377.60.2) <7A07FF86-658E-35D7-8136-829737E98B7B> /usr/lib/system/libsystem_asl.dylib
    0x7fff69405000 -     0x7fff69405ff7  libsystem_blocks.dylib (74) <0D53847E-AF5F-3ACF-B51F-A15DEA4DEC58> /usr/lib/system/libsystem_blocks.dylib
    0x7fff69406000 -     0x7fff6948dfff  libsystem_c.dylib (1353.100.2) <AF7873B0-AC4A-3C67-89BB-B8DA87718DAE> /usr/lib/system/libsystem_c.dylib
    0x7fff6948e000 -     0x7fff69491ffb  libsystem_configuration.dylib (1061.141.1) <0EE84C33-64FD-372B-974A-AF7A136F2068> /usr/lib/system/libsystem_configuration.dylib
    0x7fff69492000 -     0x7fff69495fff  libsystem_coreservices.dylib (114) <08B89E9B-C5B2-3E73-8964-03E58692B21F> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff69496000 -     0x7fff6949efff  libsystem_darwin.dylib (1353.100.2) <BD5BAD5B-AC3F-371A-B4FC-ADF86D6DCE51> /usr/lib/system/libsystem_darwin.dylib
    0x7fff6949f000 -     0x7fff694a6fff  libsystem_dnssd.dylib (1096.100.3) <01E0965B-83E5-356C-AC1F-C723F5AAB483> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff694a7000 -     0x7fff694a8ffb  libsystem_featureflags.dylib (17) <6317641E-B43F-36A3-974E-6073786B94B9> /usr/lib/system/libsystem_featureflags.dylib
    0x7fff694a9000 -     0x7fff694f6ff7  libsystem_info.dylib (538) <4DD813CB-D7B0-3AB5-9054-D2E4FF7A64B9> /usr/lib/system/libsystem_info.dylib
    0x7fff694f7000 -     0x7fff69523ff7  libsystem_kernel.dylib (6153.141.1) <2B6311E6-6240-3EF7-8C87-475B66F7452C> /usr/lib/system/libsystem_kernel.dylib
    0x7fff69524000 -     0x7fff6956bfff  libsystem_m.dylib (3178) <77A9D888-36E9-3968-8103-C82FFFC60B9E> /usr/lib/system/libsystem_m.dylib
    0x7fff6956c000 -     0x7fff69593fff  libsystem_malloc.dylib (283.100.6) <059F4DBD-856A-3A59-8B20-FD4B6918C5F2> /usr/lib/system/libsystem_malloc.dylib
    0x7fff69594000 -     0x7fff695a1ffb  libsystem_networkextension.dylib (1095.140.2) <4CCE78C9-1DC4-3375-9828-D5BD739D23F3> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff695a2000 -     0x7fff695abff7  libsystem_notify.dylib (241.100.2) <DEAD75F2-DD34-3E8F-82DE-344625A2C25E> /usr/lib/system/libsystem_notify.dylib
    0x7fff695ac000 -     0x7fff695b4fef  libsystem_platform.dylib (220.100.1) <97E825F6-D823-366C-9FF3-B1C8EA891044> /usr/lib/system/libsystem_platform.dylib
    0x7fff695b5000 -     0x7fff695bffff  libsystem_pthread.dylib (416.100.3) <80B053AA-B6F7-3B59-BC44-78A5A4F7368F> /usr/lib/system/libsystem_pthread.dylib
    0x7fff695c0000 -     0x7fff695c4ff3  libsystem_sandbox.dylib (1217.140.4) <20BF94E2-DAB8-3EBE-AACE-99DF84C1C391> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff695c5000 -     0x7fff695c7fff  libsystem_secinit.dylib (62.100.2) <28EDEB6F-899B-373D-B761-4C20D39285D9> /usr/lib/system/libsystem_secinit.dylib
    0x7fff695c8000 -     0x7fff695cfffb  libsystem_symptoms.dylib (1238.120.1) <22800D8C-5C2A-3171-8C29-311BBE67F198> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff695d0000 -     0x7fff695e6ff2  libsystem_trace.dylib (1147.120) <DC00C77E-4709-31F0-B913-CF09559B7A6F> /usr/lib/system/libsystem_trace.dylib
    0x7fff695e8000 -     0x7fff695edff7  libunwind.dylib (35.4) <42B7B509-BAFE-365B-893A-72414C92F5BF> /usr/lib/system/libunwind.dylib
    0x7fff695ee000 -     0x7fff69623ffe  libxpc.dylib (1738.140.1) <58E276A9-EE11-3F02-9D3E-5371E604E677> /usr/lib/system/libxpc.dylib

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: 62942
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=396.3M resident=0K(0%) swapped_out_or_unallocated=396.3M(100%)
Writable regions: Total=294.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=294.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            46.1M       17 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         240.0M        2         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        576K        6 
__DATA                             651K       44 
__DATA_CONST                        80K        2 
__LINKEDIT                       389.0M        3 
__OBJC_RO                         32.3M        1 
__OBJC_RW                         1908K        2 
__TEXT                            7436K       43 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            781.8M      131 
TOTAL, minus reserved VM space   541.8M      131 

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

I just tried with HEAD, and I can get it to happen on 10.14.6 as well.

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

I have set -b in my .kshrc.

If I set +b then the segfault does not occur, but the 60 seconds behavior does still occur.

[ 7:54 AM][ttys002][~/src/ksh][master]
[407] iMac $ arch/darwin.i386-64/bin/ksh
[ 8:31 AM][ttys002][~/src/ksh][master]
[408] iMac $ set +b
[ 8:31 AM][ttys002][~/src/ksh][master]
[409] iMac $ echo $KSH_VERSION
Version AJM 93u+m 2020-07-31
[ 8:32 AM][ttys002][~/src/ksh][master]
[410] iMac $ TMOUT=5
[ 8:32 AM][ttys002][~/src/ksh][master]
[411] iMac $ read
[ 8:32 AM][ttys002][~/src/ksh][master]
[412] iMac $ 
shell will timeout in 60 seconds due to inactivity
[ 8:32 AM][ttys002][~/src/ksh][master]
[412] iMac $ echo $KSH_VERSION
Version AJM 93u+m 2020-07-31
[ 8:32 AM][ttys002][~/src/ksh][master]
[413] iMac $ 
shell will timeout in 60 seconds due to inactivity
[ 8:32 AM][ttys002][~/src/ksh][master]
[413] iMac $ arch/darwin.i386-64/bin/ksh: timed out waiting for input

@JohnoKing
Copy link

The following patch might fix the segfault with set -b. This code was behind SHOPT_COSHELL in #74, but it may be required anyway for waitevent. I can't reproduce the crash though and I couldn't get a crash to occur if sh_offstate is replaced with abort so IDK if this will have any effect:

--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -451,6 +451,8 @@ int job_reap(register int sig)
 		}
 #endif
 	}
+	if(!was_ttywait_on)
+		sh_offstate(SH_TTYWAIT);
 	if(errno==ECHILD)
 	{
 		errno = oerrno;

@McDutchie
Copy link

The "60 second" behaviour is clearly intentional, there is a time_grace() function in sh/io.c (line 1855) that implements this behaviour. If there is a bug there, it's that the manual page doesn't document it.

@McDutchie
Copy link

@posguy99, a closer look at your crash dump seems to suggest that your ksh is running a script on exit, as the traceback includes comsubst (command substitution) and sh_subshell. Does your ~/.kshrc set any EXIT traps? If you could run ksh as ksh -x and then reproduce the crash again, perhaps we can see what command triggers it.

Unless of course @JohnoKing's diff fixes it, in which case never mind.

@McDutchie McDutchie added the bug Something is not working label Aug 1, 2020
@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

If you could run ksh as ksh -x and then reproduce the crash again, perhaps we can see what command triggers it.

The only trap I have is KEYBD. I fiddled briefly with an EXIT trap, but it's not reliable. If you kill Terminal.app with ⌘Q, ksh never gets a chance to run an EXIT trap, so I took it out again. Is it displaying the prompt that makes it look like it's running a subshell? I have dgkorn's PS1.set (well, a hacked up one) doing my prompt. I tried to only use shell constructs but it calls sed and git among other things.

[ 9:34 AM][ttys001][~/src/ksh][master]
[302] mbp13 $ arch/darwin.i386-64/bin/ksh -x
+ [ -f /etc/kshrc ]
+ PATH='/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Users/mwilson/bin:/Users/mwilson/bin'
+ PATH='/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Users/mwilson/bin:/Users/mwilson/bin'
+ set -o emacs
+ set +o multiline
+ set -o ignoreeof
+ set -o nolog
+ set -b
+ FOLDER=/Users/mwilson/.ksh
+ mkdir -p /Users/mwilson/.ksh/fun
+ FPATH=/Users/mwilson/.ksh/fun
+ mkdir -p /Users/mwilson/.ksh/rc
+ RCPATH=/Users/mwilson/.ksh/rc
+ mkdir -p /Users/mwilson/.ksh/histfiles
+ tty
+ basename /dev/ttys001
+ TTY=ttys001
+ HISTFILE=/Users/mwilson/.ksh/histfiles/history.ttys001
+ printf '\033[0;30m'
+ BLACK=$'\E[0;30m'
+ printf '\033[0;31m'
+ RED=$'\E[0;31m'
+ printf '\033[0;32m'
+ GREEN=$'\E[0;32m'
+ printf '\033[0;33m'
+ YELLOW=$'\E[0;33m'
+ printf '\033[0;34m'
+ BLUE=$'\E[0;34m'
+ printf '\033[0;35m'
+ PURPLE=$'\E[0;35m'
+ printf '\033[0;36m'
+ CYAN=$'\E[0;36m'
+ printf '\033[0;37m'
+ WHITE=$'\E[0;37m'
+ printf '\033[0m'
+ OFF=$'\E[0m'
+ [[ -e /Users/mwilson/.ksh/rc ]]
+ [[ -e /Users/mwilson/.ksh/rc/aliases.ksh ]]
+ . /Users/mwilson/.ksh/rc/aliases.ksh
+ [[ -e /Users/mwilson/.bcrc ]]
+ alias bc='bc -q $HOME/.bcrc'
+ alias cdb='cd $OLDPWD'
+ alias cls='tput clear'
+ alias grep='grep --color=auto'
+ alias ls='ls -GF'
+ alias ll='ls -lG'
+ alias la='ls -AG'
+ alias l='ls -CFG'
+ alias m='most -t4'
+ alias md=mkdir
+ alias rebuildLS='/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user'
+ alias mv='mv -i'
+ alias rm='rm -i'
+ alias tree=$'find . -print | sed -e \'s;[^/]*/;|____;g;s;____|; |;g\''
+ [[ -e /Users/mwilson/.ksh/rc/editor.ksh ]]
+ . /Users/mwilson/.ksh/rc/editor.ksh
+ [[ -e /usr/local/bin/mate ]]
+ EDITOR='/usr/local/bin/mate -w'
+ export EDITOR
+ VISUAL='/usr/local/bin/mate -w'
+ export VISUAL
+ which mg
+ [[ -e /opt/local/bin/mg ]]
+ which mg
+ FCEDIT=/opt/local/bin/mg
+ export FCEDIT
+ [[ -e /Users/mwilson/.ksh/rc/keyboard.ksh ]]
+ . /Users/mwilson/.ksh/rc/keyboard.ksh
+ trap keybd_trap KEYBD
+ [[ -e /Users/mwilson/.ksh/rc/pager.ksh ]]
+ . /Users/mwilson/.ksh/rc/pager.ksh
+ which most
+ [[ -e /opt/local/bin/most ]]
+ which most
+ PAGER=/opt/local/bin/most
+ export PAGER
+ [[ -e /Users/mwilson/.ksh/rc/prompt.ksh ]]
+ . /Users/mwilson/.ksh/rc/prompt.ksh
+ unset -v ksh
+ PS1='\033]0;[\u@\h][\S]\007${RED}[\@]${OFF}${BLUE}[\l]${OFF}${YELLOW}[\S]${OFF}${CYAN}\b${OFF}\n${GREEN}[\#]${OFF} \h \$ '
+ printf %s '~/src/ksh'
+ printf '%(%l:%M %p)T'
+ tty
+ basename /dev/ttys001
+ printf %s '~/src/ksh'
+ git branch
+ sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
+ 2> /dev/null
[ 9:35 AM][ttys001][~/src/ksh][master]
[303] mbp13 $ + keybd_trap
e+ keybd_trap
c+ keybd_trap
h+ keybd_trap
o+ keybd_trap
 + keybd_trap
$+ keybd_trap
K+ keybd_trap
S+ keybd_trap
H+ keybd_trap
_+ keybd_trap
V+ keybd_trap
E+ keybd_trap
R+ keybd_trap
S+ keybd_trap
I+ keybd_trap
O+ keybd_trap
N+ keybd_trap

+ echo Version AJM 93u+m 2020-07-31
Version AJM 93u+m 2020-07-31
+ printf %s '~/src/ksh'
+ printf '%(%l:%M %p)T'
+ tty
+ basename /dev/ttys001
+ printf %s '~/src/ksh'
+ git branch
+ sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
+ 2> /dev/null
[ 9:35 AM][ttys001][~/src/ksh][master]
[304] mbp13 $ + keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
TMOUT=5
+ TMOUT=5
+ printf %s '~/src/ksh'
+ printf '%(%l:%M %p)T'
+ tty
+ basename /dev/ttys001
+ printf %s '~/src/ksh'
+ git branch
+ sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
+ 2> /dev/null
[ 9:35 AM][ttys001][~/src/ksh][master]
[305] mbp13 $ + keybd_trap
+ keybd_trap
+ keybd_trap
+ keybd_trap
read+ keybd_trap

+ read
+ printf %s '~/src/ksh'
+ printf '%(%l:%M %p)T'
+ tty
+ basename /dev/ttys001
+ printf %s '~/src/ksh'
+ git branch
+ 2> /dev/null
+ sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
[ 9:36 AM][ttys001][~/src/ksh][master]
[306] mbp13 $ 
shell will timeout in 60 seconds due to inactivity
+ printf %s '~/src/ksh'
+ printf '%(%l:%M %p)T'
+ tty
+ basename /dev/ttys001
Memory fault
[ 9:36 AM][ttys001][~/src/ksh][master]
[306] mbp13 $ 

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

The following patch might fix the segfault with set -b. This code was behind SHOPT_COSHELL in #74, but it may be required anyway for waitevent. I can't reproduce the crash though and I couldn't get a crash to occur if sh_offstate is replaced with abort so IDK if this will have any effect:

--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -451,6 +451,8 @@ int job_reap(register int sig)
 		}
 #endif
 	}
+	if(!was_ttywait_on)
+		sh_offstate(SH_TTYWAIT);
 	if(errno==ECHILD)
 	{
 		errno = oerrno;

No, it still segfaults.

Crash dump:

Process:               ksh [13226]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [42903]
Responsible:           Terminal [406]
User ID:               501

Date/Time:             2020-08-01 09:52:56.520 -0700
OS Version:            Mac OS X 10.15.6 (19G73)
Report Version:        12
Bridge OS Version:     4.6 (17P6065)
Anonymous UUID:        FDAFEF58-17EF-0FD0-8BE4-E164085D539D

Sleep/Wake UUID:       767DB1DB-0D04-4825-91A9-62985CA64D3E

Time Awake Since Boot: 81000 seconds
Time Since Wake:       1200 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000000000000002c
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [13226]

VM Regions Near 0x2c:
--> 
    __TEXT                 000000010dc72000-000000010de1c000 [ 1704K] r-x/r-x SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x000000010dcbebe3 job_unpost + 19 (jobs.c:1653)
1   ksh                           	0x000000010dcbf76d job_reap + 2349 (jobs.c:467)
2   ksh                           	0x000000010dcc1321 job_wait + 1857 (jobs.c:1477)
3   ksh                           	0x000000010dd048ed sh_exec + 12285 (xec.c:1623)
4   ksh                           	0x000000010dcfdfe7 sh_subshell + 2551 (subshell.c:607)
5   ksh                           	0x000000010dccf57c comsubst + 2588 (macro.c:2185)
6   ksh                           	0x000000010dcd00f1 varsub + 1409 (macro.c:1169)
7   ksh                           	0x000000010dcccb92 copyto + 3186 (macro.c:633)
8   ksh                           	0x000000010dccde36 sh_macexpand + 870 (macro.c:247)
9   ksh                           	0x000000010dc9d14d arg_expand + 349 (args.c:876)
10  ksh                           	0x000000010dc9ce0f sh_argbuild + 303 (args.c:730)
11  ksh                           	0x000000010dd01d41 sh_exec + 1105 (xec.c:993)
12  ksh                           	0x000000010dcfdfe7 sh_subshell + 2551 (subshell.c:607)
13  ksh                           	0x000000010dccf57c comsubst + 2588 (macro.c:2185)
14  ksh                           	0x000000010dcd00f1 varsub + 1409 (macro.c:1169)
15  ksh                           	0x000000010dcccb92 copyto + 3186 (macro.c:633)
16  ksh                           	0x000000010dccbe62 sh_mactrim + 418 (macro.c:184)
17  ksh                           	0x000000010dccbc45 sh_mactry + 197 (macro.c:145)
18  ksh                           	0x000000010dcbe49b io_prompt + 363 (io.c:2094)
19  ksh                           	0x000000010dcb81b5 slowread + 261 (io.c:1933)
20  ksh                           	0x000000010dd9f9b4 sfrd + 1332 (sfrd.c:253)
21  ksh                           	0x000000010dd97e71 _sffilbuf + 1105 (sffilbuf.c:105)
22  ksh                           	0x000000010dda0d65 sfreserve + 1653
23  ksh                           	0x000000010dc8aff1 exfile + 2305 (main.c:528)
24  ksh                           	0x000000010dc8c278 sh_main + 3352 (main.c:353)
25  ksh                           	0x000000010dc72796 main + 38 (pmain.c:45)
26  libdyld.dylib                 	0x00007fff693b6cc9 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x0000000000010000  rdx: 0x000000010de960c0
  rdi: 0x0000000000000000  rsi: 0x0000000000000001  rbp: 0x00007ffee1f8a5f0  rsp: 0x00007ffee1f8a5c0
   r8: 0x000000010deb7f60   r9: 0x0000000000000000  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x000000010dcbebe3  rfl: 0x0000000000010206  cr2: 0x000000000000002c
  
Logical CPU:     6
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14


Binary Images:
       0x10dc72000 -        0x10de1bfff +ksh (0) <4EF66B3F-6F6D-3783-BA3E-31EA40FE141B> /Users/USER/*/ksh
       0x110c54000 -        0x110ce5f47  dyld (750.6) <34A11073-9E4C-38C3-9293-7D566ABAE8B6> /usr/lib/dyld
    0x7fff6639b000 -     0x7fff6639cfff  libSystem.B.dylib (1281.100.1) <001B3B7F-D02C-31D3-B961-1ED445D5A266> /usr/lib/libSystem.B.dylib
    0x7fff66681000 -     0x7fff666d3fff  libc++.1.dylib (902.1) <59A8239F-C28A-3B59-B8FA-11340DC85EDC> /usr/lib/libc++.1.dylib
    0x7fff666d4000 -     0x7fff666e9ffb  libc++abi.dylib (902) <E692F14F-C65E-303B-9921-BB7E97D77855> /usr/lib/libc++abi.dylib
    0x7fff666ea000 -     0x7fff666eafff  libcharset.1.dylib (59) <72447768-9244-39AB-8E79-2FA14EC0AD33> /usr/lib/libcharset.1.dylib
    0x7fff67174000 -     0x7fff67264fff  libiconv.2.dylib (59) <18311A67-E4EF-3CC7-95B3-C0EDEE3A282F> /usr/lib/libiconv.2.dylib
    0x7fff681fb000 -     0x7fff6822efde  libobjc.A.dylib (787.1) <6DF81160-5E7F-3E31-AA1E-C875E3B98AF6> /usr/lib/libobjc.A.dylib
    0x7fff687b1000 -     0x7fff687b4ffb  libutil.dylib (57) <F01467F6-23A7-37EE-A170-33CE1577B41D> /usr/lib/libutil.dylib
    0x7fff69198000 -     0x7fff6919dff3  libcache.dylib (83) <5940876E-AC8A-3BE0-80B3-DE3FB14E257A> /usr/lib/system/libcache.dylib
    0x7fff6919e000 -     0x7fff691a9fff  libcommonCrypto.dylib (60165.120.1) <C095BD55-1D27-337F-9B02-885E1C7FF87A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff691aa000 -     0x7fff691b1fff  libcompiler_rt.dylib (101.2) <6E80AC11-A277-31FA-AEEF-E5A528274C77> /usr/lib/system/libcompiler_rt.dylib
    0x7fff691b2000 -     0x7fff691bbff7  libcopyfile.dylib (166.40.1) <EB5E0BC8-873D-3546-A40E-C36DC46FA8F6> /usr/lib/system/libcopyfile.dylib
    0x7fff691bc000 -     0x7fff6924efdb  libcorecrypto.dylib (866.140.1) <0B6C52DB-5A50-3FCD-8B5E-C0C2F35857E3> /usr/lib/system/libcorecrypto.dylib
    0x7fff6935b000 -     0x7fff6939bff0  libdispatch.dylib (1173.100.2) <EAD535EE-1270-39A9-A254-95CF117FF3B0> /usr/lib/system/libdispatch.dylib
    0x7fff6939c000 -     0x7fff693d2fff  libdyld.dylib (750.6) <24C41E8B-6B33-30C7-94C9-02D2BD051D66> /usr/lib/system/libdyld.dylib
    0x7fff693d3000 -     0x7fff693d3ffb  libkeymgr.dylib (30) <6F582FDB-EB1A-3ED2-A989-B750643E2647> /usr/lib/system/libkeymgr.dylib
    0x7fff693e1000 -     0x7fff693e1ff7  liblaunch.dylib (1738.140.1) <AFBCBDD3-0B55-3ECD-8E04-A73A3A57356B> /usr/lib/system/liblaunch.dylib
    0x7fff693e2000 -     0x7fff693e7ff7  libmacho.dylib (959.0.1) <1B0296B5-3FD0-342C-BCC2-9886351A4391> /usr/lib/system/libmacho.dylib
    0x7fff693e8000 -     0x7fff693eaff3  libquarantine.dylib (110.40.3) <67FE2676-F9E8-3797-AEE5-F5F9D191CFA3> /usr/lib/system/libquarantine.dylib
    0x7fff693eb000 -     0x7fff693ecff7  libremovefile.dylib (48) <5CEBDAB2-988A-3B66-87BC-3C45D1C08730> /usr/lib/system/libremovefile.dylib
    0x7fff693ed000 -     0x7fff69404ff3  libsystem_asl.dylib (377.60.2) <7A07FF86-658E-35D7-8136-829737E98B7B> /usr/lib/system/libsystem_asl.dylib
    0x7fff69405000 -     0x7fff69405ff7  libsystem_blocks.dylib (74) <0D53847E-AF5F-3ACF-B51F-A15DEA4DEC58> /usr/lib/system/libsystem_blocks.dylib
    0x7fff69406000 -     0x7fff6948dfff  libsystem_c.dylib (1353.100.2) <AF7873B0-AC4A-3C67-89BB-B8DA87718DAE> /usr/lib/system/libsystem_c.dylib
    0x7fff6948e000 -     0x7fff69491ffb  libsystem_configuration.dylib (1061.141.1) <0EE84C33-64FD-372B-974A-AF7A136F2068> /usr/lib/system/libsystem_configuration.dylib
    0x7fff69492000 -     0x7fff69495fff  libsystem_coreservices.dylib (114) <08B89E9B-C5B2-3E73-8964-03E58692B21F> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff69496000 -     0x7fff6949efff  libsystem_darwin.dylib (1353.100.2) <BD5BAD5B-AC3F-371A-B4FC-ADF86D6DCE51> /usr/lib/system/libsystem_darwin.dylib
    0x7fff6949f000 -     0x7fff694a6fff  libsystem_dnssd.dylib (1096.100.3) <01E0965B-83E5-356C-AC1F-C723F5AAB483> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff694a7000 -     0x7fff694a8ffb  libsystem_featureflags.dylib (17) <6317641E-B43F-36A3-974E-6073786B94B9> /usr/lib/system/libsystem_featureflags.dylib
    0x7fff694a9000 -     0x7fff694f6ff7  libsystem_info.dylib (538) <4DD813CB-D7B0-3AB5-9054-D2E4FF7A64B9> /usr/lib/system/libsystem_info.dylib
    0x7fff694f7000 -     0x7fff69523ff7  libsystem_kernel.dylib (6153.141.1) <2B6311E6-6240-3EF7-8C87-475B66F7452C> /usr/lib/system/libsystem_kernel.dylib
    0x7fff69524000 -     0x7fff6956bfff  libsystem_m.dylib (3178) <77A9D888-36E9-3968-8103-C82FFFC60B9E> /usr/lib/system/libsystem_m.dylib
    0x7fff6956c000 -     0x7fff69593fff  libsystem_malloc.dylib (283.100.6) <059F4DBD-856A-3A59-8B20-FD4B6918C5F2> /usr/lib/system/libsystem_malloc.dylib
    0x7fff69594000 -     0x7fff695a1ffb  libsystem_networkextension.dylib (1095.140.2) <4CCE78C9-1DC4-3375-9828-D5BD739D23F3> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff695a2000 -     0x7fff695abff7  libsystem_notify.dylib (241.100.2) <DEAD75F2-DD34-3E8F-82DE-344625A2C25E> /usr/lib/system/libsystem_notify.dylib
    0x7fff695ac000 -     0x7fff695b4fef  libsystem_platform.dylib (220.100.1) <97E825F6-D823-366C-9FF3-B1C8EA891044> /usr/lib/system/libsystem_platform.dylib
    0x7fff695b5000 -     0x7fff695bffff  libsystem_pthread.dylib (416.100.3) <80B053AA-B6F7-3B59-BC44-78A5A4F7368F> /usr/lib/system/libsystem_pthread.dylib
    0x7fff695c0000 -     0x7fff695c4ff3  libsystem_sandbox.dylib (1217.140.4) <20BF94E2-DAB8-3EBE-AACE-99DF84C1C391> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff695c5000 -     0x7fff695c7fff  libsystem_secinit.dylib (62.100.2) <28EDEB6F-899B-373D-B761-4C20D39285D9> /usr/lib/system/libsystem_secinit.dylib
    0x7fff695c8000 -     0x7fff695cfffb  libsystem_symptoms.dylib (1238.120.1) <22800D8C-5C2A-3171-8C29-311BBE67F198> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff695d0000 -     0x7fff695e6ff2  libsystem_trace.dylib (1147.120) <DC00C77E-4709-31F0-B913-CF09559B7A6F> /usr/lib/system/libsystem_trace.dylib
    0x7fff695e8000 -     0x7fff695edff7  libunwind.dylib (35.4) <42B7B509-BAFE-365B-893A-72414C92F5BF> /usr/lib/system/libunwind.dylib
    0x7fff695ee000 -     0x7fff69623ffe  libxpc.dylib (1738.140.1) <58E276A9-EE11-3F02-9D3E-5371E604E677> /usr/lib/system/libxpc.dylib

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: 68607
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=396.3M resident=0K(0%) swapped_out_or_unallocated=396.3M(100%)
Writable regions: Total=420.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=420.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            52.1M       16 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         360.0M        3         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        576K        6 
__DATA                             651K       44 
__DATA_CONST                        80K        2 
__LINKEDIT                       389.0M        3 
__OBJC_RO                         32.3M        1 
__OBJC_RW                         1908K        2 
__TEXT                            7436K       43 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            907.8M      131 
TOTAL, minus reserved VM space   547.8M      131 

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

The "60 second" behaviour is clearly intentional, there is a time_grace() function in sh/io.c (line 1855) that implements this behaviour. If there is a bug there, it's that the manual page doesn't document it.

Is that another issue to be opened, then? The man page talks re TMOUT about there being a maximum timeout that can't be changed, but if the timeout has been set to 5 seconds, and that period expires, why is the hard timeout controlling at all? Not relevant to this crash discussion, but I think there should be a discussion about what the code is clearly doing vs what might have been intended.

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

MOST interesting! If I reset PS1, I can't make the segfault occur. See here, where first it occurs, then reset PS1, then it doesn't occur. I have set -b for this test.

[10:13 AM][ttys001][~/.ksh/rc]
[328] mbp13 $ ksh
[10:13 AM][ttys001][~/.ksh/rc]
[329] mbp13 $ TMOUT=5
[10:13 AM][ttys001][~/.ksh/rc]
[330] mbp13 $ read
[10:13 AM][ttys001][~/.ksh/rc]
[331] mbp13 $ 
shell will timeout in 60 seconds due to inactivity
Memory fault
[10:13 AM][ttys001][~/.ksh/rc]
[331] mbp13 $ ksh
[10:14 AM][ttys001][~/.ksh/rc]
[332] mbp13 $ PS1="$ "
$ TMOUT=5
$ read
$ 
shell will timeout in 60 seconds due to inactivity
$ ksh: timed out waiting for input

[10:15 AM][ttys001][~/.ksh/rc]
[335] mbp13 $ 

My usual prompt is:

case $TERM in
    # xterm and variants, set the titlebar
    xterm*) PS1='\033]0;[\u@\h][\S]\007${RED}[\@]${OFF}${BLUE}[\l]${OFF}${YELLOW}[\S]${OFF}${CYAN}\b${OFF}\n${GREEN}[\#]${OFF} \h \$ ' ;;
    # anything else, don't try to set the titlebar
    *) PS1='${RED}[\@]${OFF}${BLUE}[\l]${OFF}${YELLOW}[\S]${OFF}\n\h \$ ' ;;
esac

Which ends up being this after the discipline function gets through with it...

[337] mbp13 $ echo $PS1
${RED}[$(printf '%(%l:%M %p)T')]${OFF}${BLUE}[$(basename "$(tty)")]${OFF}${YELLOW}[$(printf '%s' ${RELATIVE_PWD})]${OFF}${CYAN}$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/')${OFF} ${GREEN}[!]${OFF} mbp13 $

And here is prompt.ksh.

# dicipline finction for setting PS1.  ksh93u+ includes a slightly different one from dgk
# I use this one because I didn't know about the other one
# https://blog.fpmurphy.com/2016/08/bash-like-customizable-prompt-in-korn-shell.html

function PS1.set
{
    typeset prefix remaining=${.sh.value} var= n= k=
    set -A .sh.lversion ${.sh.version}

    while [[ $remaining ]]
    do
        prefix=${remaining%%'\'*}
        remaining=${remaining#$prefix}
        var+="$prefix"

        case ${remaining:1:1} in
            A)    var+="\$(printf '%(%R)T')";;
            b)    var+="\$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/')";;
            @)    var+="\$(printf '%(%l:%M %p)T')";;  # was var+="\$(printf '%(%H:%M %p)T')"
            d)    var+="\$(printf '%(%a %b:%d)T')";;
            e)    var+="\$'\e'";;
            h)    var+=$(hostname -s);;
            H)    var+=$(hostname);;
            j)    var+="\$(jobs | wc -l)";;
            l)    var+="\$(basename \"\$(tty)\")";;
            n)    var+=$'\n';;
            r)    var+=$'\r';;
            s)    var+="\$(basename \"\$0\")";;
            S)    var+="\$(printf '%s' \${RELATIVE_PWD})" ;; # added this one to the list
            t)    var+="\$(printf '%(%H:%M:%S)T')";;
            T)    var+="\$(printf '%(%I:%M:%S)T')";;
            u)    var+=$USER;;
            v)    var+="\${.sh.lversion[2]}";;
            V)    var+="\${.sh.lversion[2]} (\${.sh.lversion[1]})";;
            w)    var+="\$(pwd)";;
            W)    var+="\$(basename \"\$(pwd)\")";;
          '#')    var+=!;;
            !)    var+=!;;
          '$')    if (( $(id -u) == 0 ))
                  then
                      var+='#'
                  else
                      var+='$'
                  fi;;
          '\')    var+='\\';;
      '['|']')    ;;
        [0-7])    case ${remaining:1:3} in
                   [0-7][0-7][0-7])   k=4;;
                            [0-7][0-7])   k=3;;
                                     *)   k=2;;
                  esac
                  eval n="\$'"${remaining:0:k}"'"
                  var+=$n
                  remaining=${remaining:k}
                  continue ;;
           "")    ;;
            *)    var+='\'${remaining:0:2};;
        esac
        remaining=${remaining:2}
    done
    .sh.value=$var
}

# RELATIVE_PWD.get is actually stolen from polyglot.sh
# https://github.com/agkozak/polyglot
# Copyright 2017-2020 Alexandros Kozak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

function RELATIVE_PWD.get {

    typeset POLYGLOT_DIRTRIM_ELEMENTS= POLYGLOT_PWD_MINUS_HOME= POLYGLOT_OLD_IFS= POLYGLOT_ABBREVIATED_PATH=

      POLYGLOT_DIRTRIM_ELEMENTS="${1:-2}"

      # If root has / as $HOME, print /, not ~
      [ "$PWD" = '/' ] && printf '%s' '/' && return
      [ "$PWD" = "$HOME" ] && printf '%s' '~' && return

      case $HOME in
        /) POLYGLOT_PWD_MINUS_HOME="$PWD" ;;            # In case root's $HOME is /
        *) POLYGLOT_PWD_MINUS_HOME="${PWD#$HOME}" ;;
      esac

      if [ "$POLYGLOT_DIRTRIM_ELEMENTS" -eq 0 ]; then
        [ "$HOME" = '/' ] && printf '%s' "$PWD" && return # need to fix this one yet
        case $PWD in
          ${HOME}*) .sh.value="$(printf '~%s' "$POLYGLOT_PWD_MINUS_HOME")" ;;
          *) .sh.value="$(printf '%s' "$PWD")" ;;
        esac
      else
        # Calculate the part of $PWD that will be displayed in the prompt
        POLYGLOT_OLD_IFS="$IFS"
        IFS='/'
        # shellcheck disable=SC2086
        set -- $POLYGLOT_PWD_MINUS_HOME
        shift                                  # Discard empty first field preceding /

        # Discard path elements > $POLYGLOT_PROMPT_DIRTRIM
        while [ $# -gt "$POLYGLOT_DIRTRIM_ELEMENTS" ]; do
          shift
        done

        # Reassemble the remaining path elements with slashes
        while [ $# -ne 0 ]; do
          POLYGLOT_ABBREVIATED_PATH="${POLYGLOT_ABBREVIATED_PATH}/$1"
          shift
        done

        IFS="$POLYGLOT_OLD_IFS"

        # If the working directory has not been abbreviated, display it thus
        if [ "$POLYGLOT_ABBREVIATED_PATH" = "${POLYGLOT_PWD_MINUS_HOME}" ]; then
          if [ "$HOME" = '/' ]; then
            printf '%s' "$PWD"
          else
            case $PWD in
              ${HOME}*) .sh.value=$(printf '~%s' "${POLYGLOT_PWD_MINUS_HOME}") ;;
              *) .sh.value="$(printf '%s' "$PWD")" ;;
            esac
          fi
        # Otherwise include an ellipsis to show that abbreviation has taken place
        else
          if [ "$HOME" = '/' ]; then
            .sh.value="$(printf '...%s' "$POLYGLOT_ABBREVIATED_PATH")"
          else
            case $PWD in
              ${HOME}*) .sh.value="$(printf '~/...%s' "$POLYGLOT_ABBREVIATED_PATH")" ;;
              *) .sh.value="$(printf '...%s' "$POLYGLOT_ABBREVIATED_PATH")" ;;
            esac
          fi
        fi
      fi
}

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

Ok, this is enough to make it segfault...

[ttys001]
mbp13 $ echo $PS1
[$(basename "$(tty)")] mbp13 $
[ttys001]
mbp13 $ TMOUT=5
[ttys001]
mbp13 $ read
[ttys001]
mbp13 $ 
shell will timeout in 60 seconds due to inactivity
Memory fault

That's with letting the discipline function mangle PS1, I'm setting PS1 to PS1='[\l]\n\h \$ '

If I remove the discipline function and set PS1 to the result of the expansion, I still get the segfault, so it's not PS1.set itself that's causing it.

[ttys001] mbp13 $ PS1='[$(basename "$(tty)")] mbp13 $ '
[ttys001] mbp13 $ TMOUT=5
[ttys001] mbp13 $ read
[ttys001] mbp13 $ 
shell will timeout in 60 seconds due to inactivity
Memory fault

ksh u+ 20120801 does not segfault with the same prompt...

[ttys001]nmbp13 $ TMOUT=5
[ttys001]nmbp13 $ read
[ttys001]nmbp13 $ 
shell will timeout in 60 seconds due to inactivity
[ttys001]nmbp13 $ /bin/ksh: timed out waiting for input

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

And the crash dump from the minimum reproducer segfault...

Process:               ksh [21725]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [19883]
Responsible:           Terminal [406]
User ID:               501

Date/Time:             2020-08-01 11:12:13.704 -0700
OS Version:            Mac OS X 10.15.6 (19G73)
Report Version:        12
Bridge OS Version:     4.6 (17P6065)
Anonymous UUID:        FDAFEF58-17EF-0FD0-8BE4-E164085D539D

Sleep/Wake UUID:       767DB1DB-0D04-4825-91A9-62985CA64D3E

Time Awake Since Boot: 86000 seconds
Time Since Wake:       5900 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000000000000002c
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [21725]

VM Regions Near 0x2c:
--> 
    __TEXT                 000000010d666000-000000010d810000 [ 1704K] r-x/r-x SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x000000010d6b2be3 job_unpost + 19 (jobs.c:1653)
1   ksh                           	0x000000010d6b376d job_reap + 2349 (jobs.c:467)
2   ksh                           	0x000000010d6b5321 job_wait + 1857 (jobs.c:1477)
3   ksh                           	0x000000010d6f88ed sh_exec + 12285 (xec.c:1623)
4   ksh                           	0x000000010d6f1fe7 sh_subshell + 2551 (subshell.c:607)
5   ksh                           	0x000000010d6c357c comsubst + 2588 (macro.c:2185)
6   ksh                           	0x000000010d6c40f1 varsub + 1409 (macro.c:1169)
7   ksh                           	0x000000010d6c0b92 copyto + 3186 (macro.c:633)
8   ksh                           	0x000000010d6c1e36 sh_macexpand + 870 (macro.c:247)
9   ksh                           	0x000000010d69114d arg_expand + 349 (args.c:876)
10  ksh                           	0x000000010d690e0f sh_argbuild + 303 (args.c:730)
11  ksh                           	0x000000010d6f5d41 sh_exec + 1105 (xec.c:993)
12  ksh                           	0x000000010d6f1fe7 sh_subshell + 2551 (subshell.c:607)
13  ksh                           	0x000000010d6c357c comsubst + 2588 (macro.c:2185)
14  ksh                           	0x000000010d6c40f1 varsub + 1409 (macro.c:1169)
15  ksh                           	0x000000010d6c0b92 copyto + 3186 (macro.c:633)
16  ksh                           	0x000000010d6bfe62 sh_mactrim + 418 (macro.c:184)
17  ksh                           	0x000000010d6bfc45 sh_mactry + 197 (macro.c:145)
18  ksh                           	0x000000010d6b249b io_prompt + 363 (io.c:2094)
19  ksh                           	0x000000010d6ac1b5 slowread + 261 (io.c:1933)
20  ksh                           	0x000000010d7939b4 sfrd + 1332 (sfrd.c:253)
21  ksh                           	0x000000010d78be71 _sffilbuf + 1105 (sffilbuf.c:105)
22  ksh                           	0x000000010d794d65 sfreserve + 1653
23  ksh                           	0x000000010d67eff1 exfile + 2305 (main.c:528)
24  ksh                           	0x000000010d680278 sh_main + 3352 (main.c:353)
25  ksh                           	0x000000010d666796 main + 38 (pmain.c:45)
26  libdyld.dylib                 	0x00007fff693b6cc9 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x0000000000010000  rdx: 0x000000010d88a0c0
  rdi: 0x0000000000000000  rsi: 0x0000000000000001  rbp: 0x00007ffee25965f0  rsp: 0x00007ffee25965c0
   r8: 0x000000010d8abf60   r9: 0x0000000000000000  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x000000010d6b2be3  rfl: 0x0000000000010206  cr2: 0x000000000000002c
  
Logical CPU:     6
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14


Binary Images:
       0x10d666000 -        0x10d80ffff +ksh (0) <4EF66B3F-6F6D-3783-BA3E-31EA40FE141B> /Users/USER/*/ksh
       0x119d8f000 -        0x119e20f47  dyld (750.6) <34A11073-9E4C-38C3-9293-7D566ABAE8B6> /usr/lib/dyld
    0x7fff6639b000 -     0x7fff6639cfff  libSystem.B.dylib (1281.100.1) <001B3B7F-D02C-31D3-B961-1ED445D5A266> /usr/lib/libSystem.B.dylib
    0x7fff66681000 -     0x7fff666d3fff  libc++.1.dylib (902.1) <59A8239F-C28A-3B59-B8FA-11340DC85EDC> /usr/lib/libc++.1.dylib
    0x7fff666d4000 -     0x7fff666e9ffb  libc++abi.dylib (902) <E692F14F-C65E-303B-9921-BB7E97D77855> /usr/lib/libc++abi.dylib
    0x7fff666ea000 -     0x7fff666eafff  libcharset.1.dylib (59) <72447768-9244-39AB-8E79-2FA14EC0AD33> /usr/lib/libcharset.1.dylib
    0x7fff67174000 -     0x7fff67264fff  libiconv.2.dylib (59) <18311A67-E4EF-3CC7-95B3-C0EDEE3A282F> /usr/lib/libiconv.2.dylib
    0x7fff681fb000 -     0x7fff6822efde  libobjc.A.dylib (787.1) <6DF81160-5E7F-3E31-AA1E-C875E3B98AF6> /usr/lib/libobjc.A.dylib
    0x7fff687b1000 -     0x7fff687b4ffb  libutil.dylib (57) <F01467F6-23A7-37EE-A170-33CE1577B41D> /usr/lib/libutil.dylib
    0x7fff69198000 -     0x7fff6919dff3  libcache.dylib (83) <5940876E-AC8A-3BE0-80B3-DE3FB14E257A> /usr/lib/system/libcache.dylib
    0x7fff6919e000 -     0x7fff691a9fff  libcommonCrypto.dylib (60165.120.1) <C095BD55-1D27-337F-9B02-885E1C7FF87A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff691aa000 -     0x7fff691b1fff  libcompiler_rt.dylib (101.2) <6E80AC11-A277-31FA-AEEF-E5A528274C77> /usr/lib/system/libcompiler_rt.dylib
    0x7fff691b2000 -     0x7fff691bbff7  libcopyfile.dylib (166.40.1) <EB5E0BC8-873D-3546-A40E-C36DC46FA8F6> /usr/lib/system/libcopyfile.dylib
    0x7fff691bc000 -     0x7fff6924efdb  libcorecrypto.dylib (866.140.1) <0B6C52DB-5A50-3FCD-8B5E-C0C2F35857E3> /usr/lib/system/libcorecrypto.dylib
    0x7fff6935b000 -     0x7fff6939bff0  libdispatch.dylib (1173.100.2) <EAD535EE-1270-39A9-A254-95CF117FF3B0> /usr/lib/system/libdispatch.dylib
    0x7fff6939c000 -     0x7fff693d2fff  libdyld.dylib (750.6) <24C41E8B-6B33-30C7-94C9-02D2BD051D66> /usr/lib/system/libdyld.dylib
    0x7fff693d3000 -     0x7fff693d3ffb  libkeymgr.dylib (30) <6F582FDB-EB1A-3ED2-A989-B750643E2647> /usr/lib/system/libkeymgr.dylib
    0x7fff693e1000 -     0x7fff693e1ff7  liblaunch.dylib (1738.140.1) <AFBCBDD3-0B55-3ECD-8E04-A73A3A57356B> /usr/lib/system/liblaunch.dylib
    0x7fff693e2000 -     0x7fff693e7ff7  libmacho.dylib (959.0.1) <1B0296B5-3FD0-342C-BCC2-9886351A4391> /usr/lib/system/libmacho.dylib
    0x7fff693e8000 -     0x7fff693eaff3  libquarantine.dylib (110.40.3) <67FE2676-F9E8-3797-AEE5-F5F9D191CFA3> /usr/lib/system/libquarantine.dylib
    0x7fff693eb000 -     0x7fff693ecff7  libremovefile.dylib (48) <5CEBDAB2-988A-3B66-87BC-3C45D1C08730> /usr/lib/system/libremovefile.dylib
    0x7fff693ed000 -     0x7fff69404ff3  libsystem_asl.dylib (377.60.2) <7A07FF86-658E-35D7-8136-829737E98B7B> /usr/lib/system/libsystem_asl.dylib
    0x7fff69405000 -     0x7fff69405ff7  libsystem_blocks.dylib (74) <0D53847E-AF5F-3ACF-B51F-A15DEA4DEC58> /usr/lib/system/libsystem_blocks.dylib
    0x7fff69406000 -     0x7fff6948dfff  libsystem_c.dylib (1353.100.2) <AF7873B0-AC4A-3C67-89BB-B8DA87718DAE> /usr/lib/system/libsystem_c.dylib
    0x7fff6948e000 -     0x7fff69491ffb  libsystem_configuration.dylib (1061.141.1) <0EE84C33-64FD-372B-974A-AF7A136F2068> /usr/lib/system/libsystem_configuration.dylib
    0x7fff69492000 -     0x7fff69495fff  libsystem_coreservices.dylib (114) <08B89E9B-C5B2-3E73-8964-03E58692B21F> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff69496000 -     0x7fff6949efff  libsystem_darwin.dylib (1353.100.2) <BD5BAD5B-AC3F-371A-B4FC-ADF86D6DCE51> /usr/lib/system/libsystem_darwin.dylib
    0x7fff6949f000 -     0x7fff694a6fff  libsystem_dnssd.dylib (1096.100.3) <01E0965B-83E5-356C-AC1F-C723F5AAB483> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff694a7000 -     0x7fff694a8ffb  libsystem_featureflags.dylib (17) <6317641E-B43F-36A3-974E-6073786B94B9> /usr/lib/system/libsystem_featureflags.dylib
    0x7fff694a9000 -     0x7fff694f6ff7  libsystem_info.dylib (538) <4DD813CB-D7B0-3AB5-9054-D2E4FF7A64B9> /usr/lib/system/libsystem_info.dylib
    0x7fff694f7000 -     0x7fff69523ff7  libsystem_kernel.dylib (6153.141.1) <2B6311E6-6240-3EF7-8C87-475B66F7452C> /usr/lib/system/libsystem_kernel.dylib
    0x7fff69524000 -     0x7fff6956bfff  libsystem_m.dylib (3178) <77A9D888-36E9-3968-8103-C82FFFC60B9E> /usr/lib/system/libsystem_m.dylib
    0x7fff6956c000 -     0x7fff69593fff  libsystem_malloc.dylib (283.100.6) <059F4DBD-856A-3A59-8B20-FD4B6918C5F2> /usr/lib/system/libsystem_malloc.dylib
    0x7fff69594000 -     0x7fff695a1ffb  libsystem_networkextension.dylib (1095.140.2) <4CCE78C9-1DC4-3375-9828-D5BD739D23F3> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff695a2000 -     0x7fff695abff7  libsystem_notify.dylib (241.100.2) <DEAD75F2-DD34-3E8F-82DE-344625A2C25E> /usr/lib/system/libsystem_notify.dylib
    0x7fff695ac000 -     0x7fff695b4fef  libsystem_platform.dylib (220.100.1) <97E825F6-D823-366C-9FF3-B1C8EA891044> /usr/lib/system/libsystem_platform.dylib
    0x7fff695b5000 -     0x7fff695bffff  libsystem_pthread.dylib (416.100.3) <80B053AA-B6F7-3B59-BC44-78A5A4F7368F> /usr/lib/system/libsystem_pthread.dylib
    0x7fff695c0000 -     0x7fff695c4ff3  libsystem_sandbox.dylib (1217.140.4) <20BF94E2-DAB8-3EBE-AACE-99DF84C1C391> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff695c5000 -     0x7fff695c7fff  libsystem_secinit.dylib (62.100.2) <28EDEB6F-899B-373D-B761-4C20D39285D9> /usr/lib/system/libsystem_secinit.dylib
    0x7fff695c8000 -     0x7fff695cfffb  libsystem_symptoms.dylib (1238.120.1) <22800D8C-5C2A-3171-8C29-311BBE67F198> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff695d0000 -     0x7fff695e6ff2  libsystem_trace.dylib (1147.120) <DC00C77E-4709-31F0-B913-CF09559B7A6F> /usr/lib/system/libsystem_trace.dylib
    0x7fff695e8000 -     0x7fff695edff7  libunwind.dylib (35.4) <42B7B509-BAFE-365B-893A-72414C92F5BF> /usr/lib/system/libunwind.dylib
    0x7fff695ee000 -     0x7fff69623ffe  libxpc.dylib (1738.140.1) <58E276A9-EE11-3F02-9D3E-5371E604E677> /usr/lib/system/libxpc.dylib

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: 72681
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=396.3M resident=0K(0%) swapped_out_or_unallocated=396.3M(100%)
Writable regions: Total=420.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=420.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            52.1M       16 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         360.0M        3         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        576K        6 
__DATA                             651K       44 
__DATA_CONST                        80K        2 
__LINKEDIT                       389.0M        3 
__OBJC_RO                         32.3M        1 
__OBJC_RW                         1908K        2 
__TEXT                            7436K       43 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            907.8M      131 
TOTAL, minus reserved VM space   547.8M      131 

@posguy99
Copy link
Author

posguy99 commented Aug 1, 2020

Twice now I have had the segfaulting subshell really mess up the parent's history file, so something's really getting corrupted somewhere.

(wasn't that less than helpful?)

Ok, at commit 88e8fa6 it still segfaults with the minimum reproducer.

[ 2:27 PM][ttys001][~/.../mwilson/ksh][(HEAD detached at 88e8fa6)]
[482] mbp13 $ arch/darwin.i386-64/bin/ksh
[ 2:29 PM][ttys001][~/.../mwilson/ksh][(HEAD detached at 88e8fa6)]
[483] mbp13 $ . /Users/mwilson/set_prompt.ksh
[ttys001]nmbp13 $ TMOUT=5
[ttys001]nmbp13 $ read
[ttys001]nmbp13 $ 
shell will timeout in 60 seconds due to inactivity
Memory fault


But at commit db72f41, it does not.

[ 2:36 PM][ttys001][~/.../mwilson/ksh][(HEAD detached at db72f41)]
[493] mbp13 $ arch/darwin.i386-64/bin/ksh
[ 2:37 PM][ttys001][~/.../mwilson/ksh][(HEAD detached at db72f41)]
[494] mbp13 $ . /Users/mwilson/set_prompt.ksh
[ttys001]nmbp13 $ TMOUT=5
[ttys001]nmbp13 $ read
[ttys001]nmbp13 $ 
shell will timeout in 60 seconds due to inactivity
[ttys001]nmbp13 $ arch/darwin.i386-64/bin/ksh: timed out waiting for input

@posguy99
Copy link
Author

posguy99 commented Aug 2, 2020

And I obviously don't understand how this works, I reverted just that commit and it still crashes.

@posguy99
Copy link
Author

posguy99 commented Aug 6, 2020

The presentation of the fault has changed.

[ 7:27 PM][ttys000 +1][~]
[786] iMac $ echo $KSH_VERSION
Version AJM 93u+m 2020-08-05
[ 7:39 PM][ttys000 +1][~]
[788] iMac $ ksh
[ 7:40 PM][ttys000 +2][~]
[789] iMac $ TMOUT=5
[ 7:40 PM][ttys000 +2][~]
[790] iMac $ read
[ 7:40 PM][ttys000 +2][~]
[791] iMac $ 
shell will timeout in 60 seconds due to inactivity

[1]                            Memory fault
[ 7:40 PM][ttys000 +1][~]
[791] iMac $ 

Wondering what the [1] represents...

@McDutchie
Copy link

McDutchie commented Aug 6, 2020

I'm getting nowhere with trying to reproduce any variant of this crash, even when using your complete prompt setup. I just can't get it to malfunction at all. There must still be something in your setup that we're missing.

However, based on your crash backtrace, I came up with something. The backtrace includes comsubst() and sh_subshell(), indicating that the crash occurs while a command substitution is being executed. Job control is disabled in command substitutions, so it doesn't seem to make sense to call job_unpost() (the function that crashed) while in a command substitution. It's a bit of a shot in the dark, but I'm curious if the following patch makes any difference on your end...

diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index 3170276..915258f 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
+	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@JohnoKing
Copy link

JohnoKing commented Aug 6, 2020

I can get the crash to occur on Linux with @posguy99's $PS1 prompt (after running set -b). I've tested @McDutchie's patch and it fixes the crash on my system.

@McDutchie
Copy link

McDutchie commented Aug 6, 2020

Yes of course, I neglected to set -b (a.k.a. set -o notify). Duh; the check for it is right there in the code. With that option set, I can now reproduce the crash with the minimal reproducer.

If @posguy99 can confirm the fix as well, then I'll commit it.

The reproducer is oddly specific (changing just about anything will make it fail to crash), but that's what you sometimes get with undefined behaviour.

@posguy99
Copy link
Author

posguy99 commented Aug 6, 2020

I can confirm that with this patch, I no longer get the crash with either my complex PS1 or the minimum reproducer.

@McDutchie
Copy link

McDutchie commented Aug 6, 2020

Wondering what the [1] represents...

That would be job_list() trying to list your jobs, but producing unpredictable behaviour due to an inconsistent state when called while in a command substitution.

@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

I spoke too soon. I have not been able to reproduce it on 10.15.6 since the patch (except cancel that, because now I just did)... but now I have access to the 10.14.6 machine again, and I can on 10.14.6. I'll be damned if I can figure out a minimal reproducer this time, or even a reliable way to reproduce it. It's much harder to make it happen, while before the patch it was every time.

Same TMOUT + read trick, but rather than every time, it's try like 10 times before it happens. It's easier to make it happen if you immediately launch the parent, launch the subshell, then try to make it fault.

Here's one crash....

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                             0x000000010e730228 job_unpost + 49
1   ksh                             0x000000010e730a03 job_reap + 1639
2   libsystem_platform.dylib        0x00007fff7dbf1b5d _sigtramp + 29
3   ???                             000000000000000000 0 + 0
4   ksh                             0x000000010e7a8d01 dtvsearch + 123
5   ksh                             0x000000010e745e82 sh_scoped + 22
6   ksh                             0x000000010e73a274 comsubst + 1007
7   ksh                             0x000000010e73b7d4 varsub + 3918
8   ksh                             0x000000010e738a63 copyto + 2565
9   ksh                             0x000000010e737fa8 sh_mactrim + 274
10  ksh                             0x000000010e740f02 nv_setlist + 3912
11  ksh                             0x000000010e75b244 sh_exec + 10313
12  ksh                             0x000000010e760419 sh_funscope + 1294
13  ksh                             0x000000010e75e7a3 sh_funct + 283
14  ksh                             0x000000010e760a18 sh_fun + 755
15  ksh                             0x000000010e714502 lookup + 395
16  ksh                             0x000000010e73dfcb varsub + 14149
17  ksh                             0x000000010e738a63 copyto + 2565
18  ksh                             0x000000010e737fa8 sh_mactrim + 274

And here's another crash...

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                             0x0000000101d09228 job_unpost + 49
1   ksh                             0x0000000101d09a03 job_reap + 1639
2   libsystem_platform.dylib        0x00007fff7dbf1b5d _sigtramp + 29
3   ksh                             0x0000000101d81d01 dtvsearch + 123
4   ksh                             0x0000000101d10fa8 sh_mactrim + 274
5   ksh                             0x0000000101d19f02 nv_setlist + 3912
6   ksh                             0x0000000101d34244 sh_exec + 10313
7   ksh                             0x0000000101d39419 sh_funscope + 1294
8   ksh                             0x0000000101d377a3 sh_funct + 283
9   ksh                             0x0000000101d39a18 sh_fun + 755
10  ksh                             0x0000000101ced502 lookup + 395
11  ksh                             0x0000000101d16fcb varsub + 14149
12  ksh                             0x0000000101d11a63 copyto + 2565
13  ksh                             0x0000000101d10fa8 sh_mactrim + 274

Here's one compiled with debugging...

Process:               ksh [97017]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [96993]
Responsible:           ksh [97017]
User ID:               501

Date/Time:             2020-08-06 20:53:45.228 -0700
OS Version:            Mac OS X 10.14.6 (18G6020)
Report Version:        12
Anonymous UUID:        2A76C314-8AFE-BD60-069A-087862B98653

Sleep/Wake UUID:       E3BCC316-D0CA-4529-9DCC-08356E62B449

Time Awake Since Boot: 880000 seconds
Time Since Wake:       700000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000032
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [97017]

VM Regions Near 0x32:
--> 
    __TEXT                 000000010fbea000-000000010fd99000 [ 1724K] r-x/rwx SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x000000010fc38748 job_unpost + 40 (jobs.c:1655)
1   ksh                           	0x000000010fc39310 job_reap + 2432 (jobs.c:467)
2   ksh                           	0x000000010fc39f56 job_waitsafe + 86 (jobs.c:486)
3   libsystem_platform.dylib      	0x00007fff7dbf1b5d _sigtramp + 29
4   ???                           	0x0000000100000000 0 + 4294967296
5   ksh                           	0x000000010fd1d479 sfreserve + 1673 (sfreserve.c:148)
6   ksh                           	0x000000010fc49a26 comsubst + 3366 (macro.c:2217)
7   ksh                           	0x000000010fc4a33a varsub + 1418 (macro.c:1169)
8   ksh                           	0x000000010fc46c12 copyto + 3298 (macro.c:633)
9   ksh                           	0x000000010fc45e78 sh_mactrim + 424 (macro.c:183)
10  ksh                           	0x000000010fc52511 nv_setlist + 529 (name.c:339)
11  ksh                           	0x000000010fc7d874 sh_exec + 3796 (xec.c:1151)
12  ksh                           	0x000000010fc882ea sh_funscope + 2106 (xec.c:3140)
13  ksh                           	0x000000010fc84d68 sh_funct + 728 (xec.c:3223)
14  ksh                           	0x000000010fc88b7f sh_fun + 1055 (xec.c:3304)
15  ksh                           	0x000000010fc095fa lookup + 506 (nvdisc.c:397)
16  ksh                           	0x000000010fc06ee5 lookups + 37 (nvdisc.c:435)
17  ksh                           	0x000000010fc05530 nv_getv + 272 (nvdisc.c:58)
18  ksh                           	0x000000010fc5bc8e nv_getval + 590 (name.c:2830)
19  ksh                           	0x000000010fc4bf35 varsub + 8581 (macro.c:1462)
20  ksh                           	0x000000010fc46c12 copyto + 3298 (macro.c:633)
21  ksh                           	0x000000010fc45e78 sh_mactrim + 424 (macro.c:183)
22  ksh                           	0x000000010fc45c5e sh_mactry + 190 (macro.c:145)
23  ksh                           	0x000000010fc37fc7 io_prompt + 375 (io.c:2094)
24  ksh                           	0x000000010fc31a48 slowread + 264 (io.c:1933)
25  ksh                           	0x000000010fd1c085 sfrd + 1349 (sfrd.c:253)
26  ksh                           	0x000000010fd14569 _sffilbuf + 1113 (sffilbuf.c:105)
27  ksh                           	0x000000010fd1d479 sfreserve + 1673 (sfreserve.c:148)
28  ksh                           	0x000000010fc03b52 exfile + 2338 (main.c:528)
29  ksh                           	0x000000010fc04e1f sh_main + 3375 (main.c:353)
30  ksh                           	0x000000010fbea9a6 main + 38 (pmain.c:45)
31  libdyld.dylib                 	0x00007fff7da063d5 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x00007ffee00125d0  rcx: 0x0000000000000000  rdx: 0x000000010fe130c0
  rdi: 0x0000000000000000  rsi: 0x0000000000000001  rbp: 0x00007ffee0012060  rsp: 0x00007ffee0012030
   r8: 0x00007ffee00125d0   r9: 0x347b814674a1c64e  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x347b814674a1c64e  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x000000010fc38748  rfl: 0x0000000000010246  cr2: 0x0000000000000032
  
Logical CPU:     0
Error Code:      0x00000004
Trap Number:     14


Binary Images:
       0x10fbea000 -        0x10fd98fff +ksh (0) <FF327439-C716-3DB0-B3C5-312E1919BD42> /Users/USER/*/ksh
       0x117f35000 -        0x117f9f70f  dyld (655.1.1) <91A01B2E-622F-3FBC-8D67-AC6D5D1C0023> /usr/lib/dyld
    0x7fff7a9ea000 -     0x7fff7a9ebffb  libSystem.B.dylib (1252.250.1) <9074C961-AB6C-38D1-B436-353A9BC637B9> /usr/lib/libSystem.B.dylib
    0x7fff7ac2f000 -     0x7fff7ac82ff7  libc++.1.dylib (400.9.4) <9A60A190-6C34-339F-BB3D-AACE942009A4> /usr/lib/libc++.1.dylib
    0x7fff7ac83000 -     0x7fff7ac98ff7  libc++abi.dylib (400.17) <38C09CED-9090-3719-90F3-04A2749F5428> /usr/lib/libc++abi.dylib
    0x7fff7ac99000 -     0x7fff7ac99ff3  libcharset.1.dylib (51.200.6) <2A27E064-314C-359C-93FC-8A9B06206174> /usr/lib/libcharset.1.dylib
    0x7fff7b5ee000 -     0x7fff7b6defff  libiconv.2.dylib (51.200.6) <2047C9B7-3F74-3A95-810D-2ED8F0475A99> /usr/lib/libiconv.2.dylib
    0x7fff7c223000 -     0x7fff7c9a8fdf  libobjc.A.dylib (756.2) <7C312627-43CB-3234-9324-4DEA92D59F50> /usr/lib/libobjc.A.dylib
    0x7fff7cf70000 -     0x7fff7cf73ff7  libutil.dylib (51.200.4) <CE9B18C9-66ED-32D4-9D29-01F8FCB467B0> /usr/lib/libutil.dylib
    0x7fff7d889000 -     0x7fff7d88dff3  libcache.dylib (81) <1987D1E1-DB11-3291-B12A-EBD55848E02D> /usr/lib/system/libcache.dylib
    0x7fff7d88e000 -     0x7fff7d898ff3  libcommonCrypto.dylib (60118.250.2) <1765BB6E-6784-3653-B16B-CB839721DC9A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff7d899000 -     0x7fff7d8a0ff7  libcompiler_rt.dylib (63.4) <5212BA7B-B7EA-37B4-AF6E-AC4F507EDFB8> /usr/lib/system/libcompiler_rt.dylib
    0x7fff7d8a1000 -     0x7fff7d8aaff7  libcopyfile.dylib (146.250.1) <98CD00CD-9B91-3B5C-A9DB-842638050FA8> /usr/lib/system/libcopyfile.dylib
    0x7fff7d8ab000 -     0x7fff7d92ffc3  libcorecrypto.dylib (602.260.2) <01464D24-570C-3B83-9D18-467769E0FCDD> /usr/lib/system/libcorecrypto.dylib
    0x7fff7d9b6000 -     0x7fff7d9efff7  libdispatch.dylib (1008.270.1) <97273678-E94C-3C8C-89F6-2E2020F4B43B> /usr/lib/system/libdispatch.dylib
    0x7fff7d9f0000 -     0x7fff7da1cff7  libdyld.dylib (655.1.1) <002418CC-AD11-3D10-865B-015591D24E6C> /usr/lib/system/libdyld.dylib
    0x7fff7da1d000 -     0x7fff7da1dffb  libkeymgr.dylib (30) <0D0F9CA2-8D5A-3273-8723-59987B5827F2> /usr/lib/system/libkeymgr.dylib
    0x7fff7da2b000 -     0x7fff7da2bff7  liblaunch.dylib (1336.261.5) <5CB1CB72-8BDD-38F4-8BE0-2C76098BD915> /usr/lib/system/liblaunch.dylib
    0x7fff7da2c000 -     0x7fff7da31fff  libmacho.dylib (927.0.3) <A377D608-77AB-3F6E-90F0-B4F251A5C12F> /usr/lib/system/libmacho.dylib
    0x7fff7da32000 -     0x7fff7da34ff7  libquarantine.dylib (86.270.1) <3F36A3D6-9606-3D90-B520-809BAEF981C3> /usr/lib/system/libquarantine.dylib
    0x7fff7da35000 -     0x7fff7da36ff7  libremovefile.dylib (45.200.2) <9FBEB2FF-EEBE-31BC-BCFC-C71F8D0E99B6> /usr/lib/system/libremovefile.dylib
    0x7fff7da37000 -     0x7fff7da4eff3  libsystem_asl.dylib (356.200.4) <A62A7249-38B8-33FA-9875-F1852590796C> /usr/lib/system/libsystem_asl.dylib
    0x7fff7da4f000 -     0x7fff7da4fff7  libsystem_blocks.dylib (73) <A453E8EE-860D-3CED-B5DC-BE54E9DB4348> /usr/lib/system/libsystem_blocks.dylib
    0x7fff7da50000 -     0x7fff7dad7fff  libsystem_c.dylib (1272.250.1) <7EDACF78-2FA3-35B8-B051-D70475A35117> /usr/lib/system/libsystem_c.dylib
    0x7fff7dad8000 -     0x7fff7dadbffb  libsystem_configuration.dylib (963.270.3) <2B4A836D-68A4-33E6-8D48-CD4486B03387> /usr/lib/system/libsystem_configuration.dylib
    0x7fff7dadc000 -     0x7fff7dadfff7  libsystem_coreservices.dylib (66) <719F75A4-74C5-3BA6-A09E-0C5A3E5889D7> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff7dae0000 -     0x7fff7dae6fff  libsystem_darwin.dylib (1272.250.1) <EC9B39A5-9592-3577-8997-7DC721D20D8C> /usr/lib/system/libsystem_darwin.dylib
    0x7fff7dae7000 -     0x7fff7daedffb  libsystem_dnssd.dylib (878.270.3) <D5352ABD-0311-3327-8E64-93F29EB19BF1> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff7daee000 -     0x7fff7db39ffb  libsystem_info.dylib (517.200.9) <D09D5AE0-2FDC-3A6D-93EC-729F931B1457> /usr/lib/system/libsystem_info.dylib
    0x7fff7db3a000 -     0x7fff7db62ff7  libsystem_kernel.dylib (4903.278.43) <40D55D88-D331-37A2-B7C0-3CD99DE39403> /usr/lib/system/libsystem_kernel.dylib
    0x7fff7db63000 -     0x7fff7dbaeff7  libsystem_m.dylib (3158.200.7) <F19B6DB7-014F-3820-831F-389CCDA06EF6> /usr/lib/system/libsystem_m.dylib
    0x7fff7dbaf000 -     0x7fff7dbd9fff  libsystem_malloc.dylib (166.270.1) <011F3AD0-8E6A-3A89-AE64-6E5F6840F30A> /usr/lib/system/libsystem_malloc.dylib
    0x7fff7dbda000 -     0x7fff7dbe4ff7  libsystem_networkextension.dylib (767.250.2) <FF06F13A-AEFE-3A27-A073-910EF78AEA36> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff7dbe5000 -     0x7fff7dbecfff  libsystem_notify.dylib (172.200.21) <145B5CFC-CF73-33CE-BD3D-E8DDE268FFDE> /usr/lib/system/libsystem_notify.dylib
    0x7fff7dbed000 -     0x7fff7dbf6fef  libsystem_platform.dylib (177.270.1) <9D1FE5E4-EB7D-3B3F-A8D1-A96D9CF1348C> /usr/lib/system/libsystem_platform.dylib
    0x7fff7dbf7000 -     0x7fff7dc01ff7  libsystem_pthread.dylib (330.250.2) <2D5C08FF-484F-3D59-9132-CE1DCB3F76D7> /usr/lib/system/libsystem_pthread.dylib
    0x7fff7dc02000 -     0x7fff7dc05ff7  libsystem_sandbox.dylib (851.270.3) <0F89B133-8D87-3B2E-BA5A-C7138738C581> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff7dc06000 -     0x7fff7dc08ff3  libsystem_secinit.dylib (30.260.2) <EF1EA47B-7B22-35E8-BD9B-F7003DCB96AE> /usr/lib/system/libsystem_secinit.dylib
    0x7fff7dc09000 -     0x7fff7dc10ff3  libsystem_symptoms.dylib (820.267.1) <03F1C2DD-0F5A-3D9D-88F6-B26C0F94EB52> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff7dc11000 -     0x7fff7dc26ff7  libsystem_trace.dylib (906.260.2) <12C1B9A2-39D6-3428-AE60-2303BD201A57> /usr/lib/system/libsystem_trace.dylib
    0x7fff7dc28000 -     0x7fff7dc2dffb  libunwind.dylib (35.4) <24A97A67-F017-3CFC-B0D0-6BD0224B1336> /usr/lib/system/libunwind.dylib
    0x7fff7dc2e000 -     0x7fff7dc5dfff  libxpc.dylib (1336.261.5) <A1EABC2B-A88E-365C-AEA5-1543FD75BAC7> /usr/lib/system/libxpc.dylib

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: 796614
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=237.1M resident=0K(0%) swapped_out_or_unallocated=237.1M(100%)
Writable regions: Total=293.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=293.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            61.1M       14 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         224.0M        2         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        672K        1 
__DATA                            2372K       42 
__LINKEDIT                       222.8M        3 
__TEXT                            14.3M       42 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            589.2M      115 
TOTAL, minus reserved VM space   365.2M      115

@McDutchie McDutchie reopened this Aug 7, 2020
@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

Finally got a dump from 10.15.6.

Process:               ksh [92793]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [88190]
Responsible:           Terminal [88180]
User ID:               501

Date/Time:             2020-08-06 21:02:40.055 -0700
OS Version:            Mac OS X 10.15.6 (19G73)
Report Version:        12
Bridge OS Version:     4.6 (17P6065)
Anonymous UUID:        FDAFEF58-17EF-0FD0-8BE4-E164085D539D

Sleep/Wake UUID:       37CA98C2-6558-4851-9FC9-DF1DC26A66CF

Time Awake Since Boot: 170000 seconds
Time Since Wake:       450 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000032
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [92793]

VM Regions Near 0x32:
--> 
    __TEXT                 0000000103ecd000-0000000103fde000 [ 1092K] r-x/r-x SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x0000000103efc7a3 job_unpost + 49
1   ksh                           	0x0000000103efcf76 job_reap + 1630
2   libsystem_platform.dylib      	0x00007fff67eb25fd _sigtramp + 29
3   ???                           	0x000000010404aa40 0 + 4362381888
4   ksh                           	0x0000000103f755db sfclose + 294
5   ksh                           	0x0000000103f06b26 comsubst + 1675
6   ksh                           	0x0000000103f07ae8 varsub + 3174
7   ksh                           	0x0000000103f04fd4 copyto + 2370
8   ksh                           	0x0000000103f045dc sh_mactrim + 274
9   ksh                           	0x0000000103f0cda7 nv_setlist + 763
10  ksh                           	0x0000000103f27e11 sh_exec + 11068
11  ksh                           	0x0000000103f2cbb0 sh_funscope + 1295
12  ksh                           	0x0000000103f2afab sh_funct + 282
13  ksh                           	0x0000000103f2d1a3 sh_fun + 755
14  ksh                           	0x0000000103ee0e1c lookup + 391
15  ksh                           	0x0000000103f085b9 varsub + 5943
16  ksh                           	0x0000000103f04fd4 copyto + 2370
17  ksh                           	0x0000000103f045dc sh_mactrim + 274
18  ksh                           	0x0000000103f0446a sh_mactry + 168
19  ksh                           	0x0000000103efc30c io_prompt + 197
20  ksh                           	0x0000000103ef84af slowread + 155
21  ksh                           	0x0000000103f7b8c5 sfrd + 1154
22  ksh                           	0x0000000103f77af3 _sffilbuf + 573
23  ksh                           	0x0000000103f7bfdb sfreserve + 686
24  ksh                           	0x0000000103eddd89 exfile + 1873
25  ksh                           	0x0000000103ede4de sh_main + 1077
26  libdyld.dylib                 	0x00007fff67cb9cc9 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000001  rbx: 0x0000000000000000  rcx: 0x0000000000000000  rdx: 0xffffffffffffffff
  rdi: 0x0000000103ff1db8  rsi: 0x0000000000000001  rbp: 0x00007ffeebd30480  rsp: 0x00007ffeebd30460
   r8: 0x0000000103ff1dea   r9: 0x97dc6ec960e39423  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x0000000000000001  r13: 0x0000000000000013  r14: 0x0000000103ff8078  r15: 0x0000000000000014
  rip: 0x0000000103efc7a3  rfl: 0x0000000000010246  cr2: 0x0000000000000032
  
Logical CPU:     4
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14


Binary Images:
       0x103ecd000 -        0x103fddfff +ksh (0) <44FFC5C1-F83D-3E45-A2BA-09B1596590A3> /Users/USER/*/ksh
       0x107537000 -        0x1075c8f47  dyld (750.6) <34A11073-9E4C-38C3-9293-7D566ABAE8B6> /usr/lib/dyld
    0x7fff64c9e000 -     0x7fff64c9ffff  libSystem.B.dylib (1281.100.1) <001B3B7F-D02C-31D3-B961-1ED445D5A266> /usr/lib/libSystem.B.dylib
    0x7fff64f84000 -     0x7fff64fd6fff  libc++.1.dylib (902.1) <59A8239F-C28A-3B59-B8FA-11340DC85EDC> /usr/lib/libc++.1.dylib
    0x7fff64fd7000 -     0x7fff64fecffb  libc++abi.dylib (902) <E692F14F-C65E-303B-9921-BB7E97D77855> /usr/lib/libc++abi.dylib
    0x7fff64fed000 -     0x7fff64fedfff  libcharset.1.dylib (59) <72447768-9244-39AB-8E79-2FA14EC0AD33> /usr/lib/libcharset.1.dylib
    0x7fff65a77000 -     0x7fff65b67fff  libiconv.2.dylib (59) <18311A67-E4EF-3CC7-95B3-C0EDEE3A282F> /usr/lib/libiconv.2.dylib
    0x7fff66afe000 -     0x7fff66b31fde  libobjc.A.dylib (787.1) <6DF81160-5E7F-3E31-AA1E-C875E3B98AF6> /usr/lib/libobjc.A.dylib
    0x7fff670b4000 -     0x7fff670b7ffb  libutil.dylib (57) <F01467F6-23A7-37EE-A170-33CE1577B41D> /usr/lib/libutil.dylib
    0x7fff67a9b000 -     0x7fff67aa0ff3  libcache.dylib (83) <5940876E-AC8A-3BE0-80B3-DE3FB14E257A> /usr/lib/system/libcache.dylib
    0x7fff67aa1000 -     0x7fff67aacfff  libcommonCrypto.dylib (60165.120.1) <C095BD55-1D27-337F-9B02-885E1C7FF87A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff67aad000 -     0x7fff67ab4fff  libcompiler_rt.dylib (101.2) <6E80AC11-A277-31FA-AEEF-E5A528274C77> /usr/lib/system/libcompiler_rt.dylib
    0x7fff67ab5000 -     0x7fff67abeff7  libcopyfile.dylib (166.40.1) <EB5E0BC8-873D-3546-A40E-C36DC46FA8F6> /usr/lib/system/libcopyfile.dylib
    0x7fff67abf000 -     0x7fff67b51fdb  libcorecrypto.dylib (866.140.1) <0B6C52DB-5A50-3FCD-8B5E-C0C2F35857E3> /usr/lib/system/libcorecrypto.dylib
    0x7fff67c5e000 -     0x7fff67c9eff0  libdispatch.dylib (1173.100.2) <EAD535EE-1270-39A9-A254-95CF117FF3B0> /usr/lib/system/libdispatch.dylib
    0x7fff67c9f000 -     0x7fff67cd5fff  libdyld.dylib (750.6) <24C41E8B-6B33-30C7-94C9-02D2BD051D66> /usr/lib/system/libdyld.dylib
    0x7fff67cd6000 -     0x7fff67cd6ffb  libkeymgr.dylib (30) <6F582FDB-EB1A-3ED2-A989-B750643E2647> /usr/lib/system/libkeymgr.dylib
    0x7fff67ce4000 -     0x7fff67ce4ff7  liblaunch.dylib (1738.140.1) <AFBCBDD3-0B55-3ECD-8E04-A73A3A57356B> /usr/lib/system/liblaunch.dylib
    0x7fff67ce5000 -     0x7fff67ceaff7  libmacho.dylib (959.0.1) <1B0296B5-3FD0-342C-BCC2-9886351A4391> /usr/lib/system/libmacho.dylib
    0x7fff67ceb000 -     0x7fff67cedff3  libquarantine.dylib (110.40.3) <67FE2676-F9E8-3797-AEE5-F5F9D191CFA3> /usr/lib/system/libquarantine.dylib
    0x7fff67cee000 -     0x7fff67cefff7  libremovefile.dylib (48) <5CEBDAB2-988A-3B66-87BC-3C45D1C08730> /usr/lib/system/libremovefile.dylib
    0x7fff67cf0000 -     0x7fff67d07ff3  libsystem_asl.dylib (377.60.2) <7A07FF86-658E-35D7-8136-829737E98B7B> /usr/lib/system/libsystem_asl.dylib
    0x7fff67d08000 -     0x7fff67d08ff7  libsystem_blocks.dylib (74) <0D53847E-AF5F-3ACF-B51F-A15DEA4DEC58> /usr/lib/system/libsystem_blocks.dylib
    0x7fff67d09000 -     0x7fff67d90fff  libsystem_c.dylib (1353.100.2) <AF7873B0-AC4A-3C67-89BB-B8DA87718DAE> /usr/lib/system/libsystem_c.dylib
    0x7fff67d91000 -     0x7fff67d94ffb  libsystem_configuration.dylib (1061.141.1) <0EE84C33-64FD-372B-974A-AF7A136F2068> /usr/lib/system/libsystem_configuration.dylib
    0x7fff67d95000 -     0x7fff67d98fff  libsystem_coreservices.dylib (114) <08B89E9B-C5B2-3E73-8964-03E58692B21F> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff67d99000 -     0x7fff67da1fff  libsystem_darwin.dylib (1353.100.2) <BD5BAD5B-AC3F-371A-B4FC-ADF86D6DCE51> /usr/lib/system/libsystem_darwin.dylib
    0x7fff67da2000 -     0x7fff67da9fff  libsystem_dnssd.dylib (1096.100.3) <01E0965B-83E5-356C-AC1F-C723F5AAB483> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff67daa000 -     0x7fff67dabffb  libsystem_featureflags.dylib (17) <6317641E-B43F-36A3-974E-6073786B94B9> /usr/lib/system/libsystem_featureflags.dylib
    0x7fff67dac000 -     0x7fff67df9ff7  libsystem_info.dylib (538) <4DD813CB-D7B0-3AB5-9054-D2E4FF7A64B9> /usr/lib/system/libsystem_info.dylib
    0x7fff67dfa000 -     0x7fff67e26ff7  libsystem_kernel.dylib (6153.141.1) <2B6311E6-6240-3EF7-8C87-475B66F7452C> /usr/lib/system/libsystem_kernel.dylib
    0x7fff67e27000 -     0x7fff67e6efff  libsystem_m.dylib (3178) <77A9D888-36E9-3968-8103-C82FFFC60B9E> /usr/lib/system/libsystem_m.dylib
    0x7fff67e6f000 -     0x7fff67e96fff  libsystem_malloc.dylib (283.100.6) <059F4DBD-856A-3A59-8B20-FD4B6918C5F2> /usr/lib/system/libsystem_malloc.dylib
    0x7fff67e97000 -     0x7fff67ea4ffb  libsystem_networkextension.dylib (1095.140.2) <4CCE78C9-1DC4-3375-9828-D5BD739D23F3> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff67ea5000 -     0x7fff67eaeff7  libsystem_notify.dylib (241.100.2) <DEAD75F2-DD34-3E8F-82DE-344625A2C25E> /usr/lib/system/libsystem_notify.dylib
    0x7fff67eaf000 -     0x7fff67eb7fef  libsystem_platform.dylib (220.100.1) <97E825F6-D823-366C-9FF3-B1C8EA891044> /usr/lib/system/libsystem_platform.dylib
    0x7fff67eb8000 -     0x7fff67ec2fff  libsystem_pthread.dylib (416.100.3) <80B053AA-B6F7-3B59-BC44-78A5A4F7368F> /usr/lib/system/libsystem_pthread.dylib
    0x7fff67ec3000 -     0x7fff67ec7ff3  libsystem_sandbox.dylib (1217.140.4) <20BF94E2-DAB8-3EBE-AACE-99DF84C1C391> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff67ec8000 -     0x7fff67ecafff  libsystem_secinit.dylib (62.100.2) <28EDEB6F-899B-373D-B761-4C20D39285D9> /usr/lib/system/libsystem_secinit.dylib
    0x7fff67ecb000 -     0x7fff67ed2ffb  libsystem_symptoms.dylib (1238.120.1) <22800D8C-5C2A-3171-8C29-311BBE67F198> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff67ed3000 -     0x7fff67ee9ff2  libsystem_trace.dylib (1147.120) <DC00C77E-4709-31F0-B913-CF09559B7A6F> /usr/lib/system/libsystem_trace.dylib
    0x7fff67eeb000 -     0x7fff67ef0ff7  libunwind.dylib (35.4) <42B7B509-BAFE-365B-893A-72414C92F5BF> /usr/lib/system/libunwind.dylib
    0x7fff67ef1000 -     0x7fff67f26ffe  libxpc.dylib (1738.140.1) <58E276A9-EE11-3F02-9D3E-5371E604E677> /usr/lib/system/libxpc.dylib

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: 143547
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=395.5M resident=0K(0%) swapped_out_or_unallocated=395.5M(100%)
Writable regions: Total=421.0M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=421.0M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            52.1M       16 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         360.0M        3         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        672K        7 
__DATA                             651K       44 
__DATA_CONST                        80K        2 
__LINKEDIT                       388.8M        3 
__OBJC_RO                         32.3M        1 
__OBJC_RW                         1908K        2 
__TEXT                            6824K       43 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            907.1M      132 
TOTAL, minus reserved VM space   547.1M      132 


@McDutchie
Copy link

McDutchie commented Aug 7, 2020

Going by the backtraces, each of your crashes seem to end up in job_reap() via a different code path. I've no idea how that works. :-/ A signal seems to be involved in al the new crashes somehow.

Let's try another shot in the dark. Simply refuse to list jobs if we're in any kind of subshell. Let me know if this makes a difference.

diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index 31b5d46..5164d32 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
+	if(!shp->subshell && !shp->comsub && job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@McDutchie
Copy link

Hmmm. When we enter the 60 second grace period, this happens:

ksh/src/cmd/ksh93/sh/io.c

Lines 1869 to 1872 in 9de6521

errormsg(SH_DICT,0,e_timewarn);
sh_onstate(SH_GRACE);
sigrelease(SIGALRM);
shp->trapnote |= SH_SIGTRAP;

So it prints the 60 second warning message, sets a state flag (SH_GRACE), unblocks SIGALRM, and sets a flag indicating a trap is pending.

We could try using either the SH_GRACE flag or the trapnote flag. The latter may be better as it more directly addresses the (apparent) cause. Try this patch (replaces previous one):

diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index 31b5d46..b22ecbc 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
+	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT) && !(shp->trapnote & SH_SIGTRAP))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

Nope. Faulted again. And that time it faulted immediately, I didn't have to try more than once. Killed Terminal.app, relaunched, started a subshell, and it faulted.

Last login: Thu Aug  6 21:55:41 on ttys000
[10:55 PM][ttys000 +1][~]
[971] iMac $ src/ksh-segfault/arch/darwin.i386-64/bin/ksh
[10:55 PM][ttys000 +2][~]
[972] iMac $ TMOUT=5
[10:56 PM][ttys000 +2][~]
[973] iMac $ read
[10:56 PM][ttys000 +2][~]
[974] iMac $ 
shell will timeout in 60 seconds due to inactivity
Memory fault
[10:56 PM][ttys000 +1][~]
[974] iMac $ 
Crash dump:

Process:               ksh [51227]
Path:                  /Users/USER/*/ksh
Identifier:            ksh
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ksh [51181]
Responsible:           ksh [51227]
User ID:               501

Date/Time:             2020-08-06 22:56:13.801 -0700
OS Version:            Mac OS X 10.14.6 (18G6020)
Report Version:        12
Anonymous UUID:        2A76C314-8AFE-BD60-069A-087862B98653

Sleep/Wake UUID:       E3BCC316-D0CA-4529-9DCC-08356E62B449

Time Awake Since Boot: 890000 seconds
Time Since Wake:       700000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000032
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [51227]

VM Regions Near 0x32:
--> 
    __TEXT                 00000001020fb000-00000001022aa000 [ 1724K] r-x/rwx SM=COW  /Users/USER/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ksh                           	0x0000000102149738 job_unpost + 40 (jobs.c:1655)
1   ksh                           	0x000000010214a314 job_reap + 2452 (jobs.c:467)
2   ksh                           	0x000000010214af56 job_waitsafe + 86 (jobs.c:486)
3   libsystem_platform.dylib      	0x00007fff7dbf1b5d _sigtramp + 29
4   ksh                           	0x00000001021c0621 init + 3681 (optget.c:1126)
5   ksh                           	0x000000010222f0f4 sfseek + 1940 (sfseek.c:179)
6   ksh                           	0x000000010218ab10 sh_subshell + 5664 (subshell.c:784)
7   ksh                           	0x000000010215a77a comsubst + 2682 (macro.c:2185)
8   ksh                           	0x000000010215b33a varsub + 1418 (macro.c:1169)
9   ksh                           	0x0000000102157c12 copyto + 3298 (macro.c:633)
10  ksh                           	0x0000000102156e78 sh_mactrim + 424 (macro.c:183)
11  ksh                           	0x0000000102163511 nv_setlist + 529 (name.c:339)
12  ksh                           	0x000000010218e874 sh_exec + 3796 (xec.c:1151)
13  ksh                           	0x00000001021992ea sh_funscope + 2106 (xec.c:3140)
14  ksh                           	0x0000000102195d68 sh_funct + 728 (xec.c:3223)
15  ksh                           	0x0000000102199b7f sh_fun + 1055 (xec.c:3304)
16  ksh                           	0x000000010211a5ea lookup + 506 (nvdisc.c:397)
17  ksh                           	0x0000000102117ed5 lookups + 37 (nvdisc.c:435)
18  ksh                           	0x0000000102116520 nv_getv + 272 (nvdisc.c:58)
19  ksh                           	0x000000010216cc8e nv_getval + 590 (name.c:2830)
20  ksh                           	0x000000010215cf35 varsub + 8581 (macro.c:1462)
21  ksh                           	0x0000000102157c12 copyto + 3298 (macro.c:633)
22  ksh                           	0x0000000102156e78 sh_mactrim + 424 (macro.c:183)
23  ksh                           	0x0000000102156c5e sh_mactry + 190 (macro.c:145)
24  ksh                           	0x0000000102148fb7 io_prompt + 375 (io.c:2094)
25  ksh                           	0x0000000102142a38 slowread + 264 (io.c:1933)
26  ksh                           	0x000000010222d085 sfrd + 1349 (sfrd.c:253)
27  ksh                           	0x0000000102225569 _sffilbuf + 1113 (sffilbuf.c:105)
28  ksh                           	0x000000010222e479 sfreserve + 1673 (sfreserve.c:148)
29  ksh                           	0x0000000102114b42 exfile + 2338 (main.c:528)
30  ksh                           	0x0000000102115e0f sh_main + 3375 (main.c:353)
31  ksh                           	0x00000001020fb996 main + 38 (pmain.c:45)
32  libdyld.dylib                 	0x00007fff7da063d5 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x00007ffeedb01150  rcx: 0x0000000000000000  rdx: 0x00000001023250d0
  rdi: 0x0000000000000000  rsi: 0x0000000000000001  rbp: 0x00007ffeedb00be0  rsp: 0x00007ffeedb00bb0
   r8: 0x00007ffeedb01150   r9: 0x32503e0b25ed3926  r10: 0x0000000000000000  r11: 0x0000000000000247
  r12: 0x32503e0b25ed3926  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x0000000102149738  rfl: 0x0000000000010246  cr2: 0x0000000000000032
  
Logical CPU:     0
Error Code:      0x00000004
Trap Number:     14


Binary Images:
       0x1020fb000 -        0x1022a9fff +ksh (0) <02C77255-A930-3E39-AABF-B5664905FDF1> /Users/USER/*/ksh
       0x10ab49000 -        0x10abb370f  dyld (655.1.1) <91A01B2E-622F-3FBC-8D67-AC6D5D1C0023> /usr/lib/dyld
    0x7fff7a9ea000 -     0x7fff7a9ebffb  libSystem.B.dylib (1252.250.1) <9074C961-AB6C-38D1-B436-353A9BC637B9> /usr/lib/libSystem.B.dylib
    0x7fff7ac2f000 -     0x7fff7ac82ff7  libc++.1.dylib (400.9.4) <9A60A190-6C34-339F-BB3D-AACE942009A4> /usr/lib/libc++.1.dylib
    0x7fff7ac83000 -     0x7fff7ac98ff7  libc++abi.dylib (400.17) <38C09CED-9090-3719-90F3-04A2749F5428> /usr/lib/libc++abi.dylib
    0x7fff7ac99000 -     0x7fff7ac99ff3  libcharset.1.dylib (51.200.6) <2A27E064-314C-359C-93FC-8A9B06206174> /usr/lib/libcharset.1.dylib
    0x7fff7b5ee000 -     0x7fff7b6defff  libiconv.2.dylib (51.200.6) <2047C9B7-3F74-3A95-810D-2ED8F0475A99> /usr/lib/libiconv.2.dylib
    0x7fff7c223000 -     0x7fff7c9a8fdf  libobjc.A.dylib (756.2) <7C312627-43CB-3234-9324-4DEA92D59F50> /usr/lib/libobjc.A.dylib
    0x7fff7cf70000 -     0x7fff7cf73ff7  libutil.dylib (51.200.4) <CE9B18C9-66ED-32D4-9D29-01F8FCB467B0> /usr/lib/libutil.dylib
    0x7fff7d889000 -     0x7fff7d88dff3  libcache.dylib (81) <1987D1E1-DB11-3291-B12A-EBD55848E02D> /usr/lib/system/libcache.dylib
    0x7fff7d88e000 -     0x7fff7d898ff3  libcommonCrypto.dylib (60118.250.2) <1765BB6E-6784-3653-B16B-CB839721DC9A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff7d899000 -     0x7fff7d8a0ff7  libcompiler_rt.dylib (63.4) <5212BA7B-B7EA-37B4-AF6E-AC4F507EDFB8> /usr/lib/system/libcompiler_rt.dylib
    0x7fff7d8a1000 -     0x7fff7d8aaff7  libcopyfile.dylib (146.250.1) <98CD00CD-9B91-3B5C-A9DB-842638050FA8> /usr/lib/system/libcopyfile.dylib
    0x7fff7d8ab000 -     0x7fff7d92ffc3  libcorecrypto.dylib (602.260.2) <01464D24-570C-3B83-9D18-467769E0FCDD> /usr/lib/system/libcorecrypto.dylib
    0x7fff7d9b6000 -     0x7fff7d9efff7  libdispatch.dylib (1008.270.1) <97273678-E94C-3C8C-89F6-2E2020F4B43B> /usr/lib/system/libdispatch.dylib
    0x7fff7d9f0000 -     0x7fff7da1cff7  libdyld.dylib (655.1.1) <002418CC-AD11-3D10-865B-015591D24E6C> /usr/lib/system/libdyld.dylib
    0x7fff7da1d000 -     0x7fff7da1dffb  libkeymgr.dylib (30) <0D0F9CA2-8D5A-3273-8723-59987B5827F2> /usr/lib/system/libkeymgr.dylib
    0x7fff7da2b000 -     0x7fff7da2bff7  liblaunch.dylib (1336.261.5) <5CB1CB72-8BDD-38F4-8BE0-2C76098BD915> /usr/lib/system/liblaunch.dylib
    0x7fff7da2c000 -     0x7fff7da31fff  libmacho.dylib (927.0.3) <A377D608-77AB-3F6E-90F0-B4F251A5C12F> /usr/lib/system/libmacho.dylib
    0x7fff7da32000 -     0x7fff7da34ff7  libquarantine.dylib (86.270.1) <3F36A3D6-9606-3D90-B520-809BAEF981C3> /usr/lib/system/libquarantine.dylib
    0x7fff7da35000 -     0x7fff7da36ff7  libremovefile.dylib (45.200.2) <9FBEB2FF-EEBE-31BC-BCFC-C71F8D0E99B6> /usr/lib/system/libremovefile.dylib
    0x7fff7da37000 -     0x7fff7da4eff3  libsystem_asl.dylib (356.200.4) <A62A7249-38B8-33FA-9875-F1852590796C> /usr/lib/system/libsystem_asl.dylib
    0x7fff7da4f000 -     0x7fff7da4fff7  libsystem_blocks.dylib (73) <A453E8EE-860D-3CED-B5DC-BE54E9DB4348> /usr/lib/system/libsystem_blocks.dylib
    0x7fff7da50000 -     0x7fff7dad7fff  libsystem_c.dylib (1272.250.1) <7EDACF78-2FA3-35B8-B051-D70475A35117> /usr/lib/system/libsystem_c.dylib
    0x7fff7dad8000 -     0x7fff7dadbffb  libsystem_configuration.dylib (963.270.3) <2B4A836D-68A4-33E6-8D48-CD4486B03387> /usr/lib/system/libsystem_configuration.dylib
    0x7fff7dadc000 -     0x7fff7dadfff7  libsystem_coreservices.dylib (66) <719F75A4-74C5-3BA6-A09E-0C5A3E5889D7> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff7dae0000 -     0x7fff7dae6fff  libsystem_darwin.dylib (1272.250.1) <EC9B39A5-9592-3577-8997-7DC721D20D8C> /usr/lib/system/libsystem_darwin.dylib
    0x7fff7dae7000 -     0x7fff7daedffb  libsystem_dnssd.dylib (878.270.3) <D5352ABD-0311-3327-8E64-93F29EB19BF1> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff7daee000 -     0x7fff7db39ffb  libsystem_info.dylib (517.200.9) <D09D5AE0-2FDC-3A6D-93EC-729F931B1457> /usr/lib/system/libsystem_info.dylib
    0x7fff7db3a000 -     0x7fff7db62ff7  libsystem_kernel.dylib (4903.278.43) <40D55D88-D331-37A2-B7C0-3CD99DE39403> /usr/lib/system/libsystem_kernel.dylib
    0x7fff7db63000 -     0x7fff7dbaeff7  libsystem_m.dylib (3158.200.7) <F19B6DB7-014F-3820-831F-389CCDA06EF6> /usr/lib/system/libsystem_m.dylib
    0x7fff7dbaf000 -     0x7fff7dbd9fff  libsystem_malloc.dylib (166.270.1) <011F3AD0-8E6A-3A89-AE64-6E5F6840F30A> /usr/lib/system/libsystem_malloc.dylib
    0x7fff7dbda000 -     0x7fff7dbe4ff7  libsystem_networkextension.dylib (767.250.2) <FF06F13A-AEFE-3A27-A073-910EF78AEA36> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff7dbe5000 -     0x7fff7dbecfff  libsystem_notify.dylib (172.200.21) <145B5CFC-CF73-33CE-BD3D-E8DDE268FFDE> /usr/lib/system/libsystem_notify.dylib
    0x7fff7dbed000 -     0x7fff7dbf6fef  libsystem_platform.dylib (177.270.1) <9D1FE5E4-EB7D-3B3F-A8D1-A96D9CF1348C> /usr/lib/system/libsystem_platform.dylib
    0x7fff7dbf7000 -     0x7fff7dc01ff7  libsystem_pthread.dylib (330.250.2) <2D5C08FF-484F-3D59-9132-CE1DCB3F76D7> /usr/lib/system/libsystem_pthread.dylib
    0x7fff7dc02000 -     0x7fff7dc05ff7  libsystem_sandbox.dylib (851.270.3) <0F89B133-8D87-3B2E-BA5A-C7138738C581> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff7dc06000 -     0x7fff7dc08ff3  libsystem_secinit.dylib (30.260.2) <EF1EA47B-7B22-35E8-BD9B-F7003DCB96AE> /usr/lib/system/libsystem_secinit.dylib
    0x7fff7dc09000 -     0x7fff7dc10ff3  libsystem_symptoms.dylib (820.267.1) <03F1C2DD-0F5A-3D9D-88F6-B26C0F94EB52> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff7dc11000 -     0x7fff7dc26ff7  libsystem_trace.dylib (906.260.2) <12C1B9A2-39D6-3428-AE60-2303BD201A57> /usr/lib/system/libsystem_trace.dylib
    0x7fff7dc28000 -     0x7fff7dc2dffb  libunwind.dylib (35.4) <24A97A67-F017-3CFC-B0D0-6BD0224B1336> /usr/lib/system/libunwind.dylib
    0x7fff7dc2e000 -     0x7fff7dc5dfff  libxpc.dylib (1336.261.5) <A1EABC2B-A88E-365C-AEA5-1543FD75BAC7> /usr/lib/system/libxpc.dylib

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: 803301
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=237.1M resident=0K(0%) swapped_out_or_unallocated=237.1M(100%)
Writable regions: Total=292.9M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=292.9M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                            60.1M       13 
MALLOC guard page                   24K        5 
MALLOC_MEDIUM (reserved)         224.0M        2         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                        672K        1 
__DATA                            2372K       42 
__LINKEDIT                       222.8M        3 
__TEXT                            14.3M       42 
shared memory                       12K        3 
===========                     =======  ======= 
TOTAL                            588.2M      114 
TOTAL, minus reserved VM space   364.2M      114 


@McDutchie
Copy link

OK, and this one then?

diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index 31b5d46..b22ecbc 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
+	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT) && !sh_isstate(SH_GRACE))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

Well, for what it's worth, I haven't been able to get this one to fault yet, on either 10.15.6 or 10.14.6.

Have you been able to get current HEAD to fault again, irrespective of patches?

@McDutchie
Copy link

Good news so far. Let me know if that changes.

I can consistently reproduce the segfault with your minimum reproducer plus set -o notify on current HEAD if I remove both extra checks (job.jobcontrol and !sh_isstate(SH_GRACE)) from sh/jobs.c, but for me, adding the job.jobcontrol check is enough to make the segfault impossible to reproduce. I'm now using all the bits of your setup that you've revealed here plus set -o notify.

But it shouldn't really matter. The new !sh_isstate(SH_GRACE) check really should work (otherwise I would have thrown up my arms and given up at this point). We know that the SH_GRACE state is set immediately after printing the 60 second grace period warning message (and that the crashes occur upon re-evaluating the $PS1 prompt after setting the SH_GRACE state). We know that the SH_GRACE state is not turned off again until either the user enters a line, or the shell times out after the grace period. Unlike trapnote, the SH_GRACE state flag is not used or changed in any other context (verified with grep -rn SH_GRACE src/cmd/ksh93). So, logically, this should suffice to make sure the crash stays gone.

@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

Don't know if I want to put this out there, but I got a hang with the latest patch. Thought I'd try something deliberately job control related...

[10:37 AM][ttys000 +1][~/src/ksh-segfault][master]
[1049] iMac $ arch/darwin.i386-64/bin/ksh
[10:37 AM][ttys000 +2][~/src/ksh-segfault][master]
[1050] iMac $ top &
[1]	72570
[10:37 AM][ttys000 +2][~/src/ksh-segfault][master]
[1051] iMac $ 
[1] + Stopped (SIGTTOU)        top &

[10:37 AM][ttys000 +2][~/src/ksh-segfault][master]
[1051] iMac $ fg
top 
[10:38 AM][ttys000 +2][~/src/ksh-segfault][master]
[1052] iMac $ 

And now the shell is unresponsive.

This is related to #89 , right?

I've managed to get it to do that four times now just doing the above, but it's not reliably repeatable. If it does not display the 'Stopped' line without hitting Return again, it seems to never do it. Only if it does that on its own, and not always.

I have to kill it from somewhere else or close Terminal.app.

74575 s000 S+ 0:00.02 arch/darwin.i386-64/bin/ksh

^ from the latest one.

Haven't been able to replicate the hang with HEAD yet, still trying.

@McDutchie
Copy link

Thank you for reporting that hang. I was initially able to reproduce it on my ksh with the latest patch for this bug. And now I can't reproduce it any more, even though nothing changed. So it's another intermittent problem. And because of that I've not (yet) been able to determine if it's related to the fix for this bug at all.

@McDutchie
Copy link

I've now been able to reproduce the intermittent hang once with a ksh compiled at 49ae483, which was before any fix for this bug. So I'm going to go with "not related".

This thread is getting long, so I'll commit the new fix and close this bug. Could you open a new issue for the hang problem?

@McDutchie
Copy link

Annnd reopening again. The fix breaks -o notify during the grace period. Which makes sense. We're going to have to find another way of fixing this.

@McDutchie
Copy link

Unfortunately, though, I'm completely out of ideas.

@McDutchie McDutchie added the help wanted Extra attention is needed label Aug 7, 2020
@posguy99
Copy link
Author

posguy99 commented Aug 7, 2020

u+m introduces it, u+ doesn't have it. But which change? I thought I was getting somewhere backing up in commit history but it was like killing a flea with a nuke. And my end result got me nowhere. And I'm no C programmer.

@McDutchie
Copy link

git bisect (see man git-bisect) is normally the tool to help you find out which commit introduced a certain change. But with an intermittent crash, that is hardly going to help.

@McDutchie
Copy link

Another idea occurred to me, but it's (again) a shot in the dark.

All these crashes occur while evaluating the expansions contained in $PS1 prompt.

So let's try to:

  1. remove the check for SH_GRACE that breaks -o notify during the grace period
  2. keep the check for job control
  3. disable job control while evaluating PS1..4 prompts.

Here's a patch against current HEAD. @posguy99, since you're currently the only one who can reproduce these crashes, please test this and let me know if it makes a difference or not.

diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c
index 26ba79c..b914d22 100644
--- a/src/cmd/ksh93/sh/io.c
+++ b/src/cmd/ksh93/sh/io.c
@@ -2066,12 +2066,15 @@ static int	io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
 	char *endprompt;
 	static short cmdno;
 	int sfflags;
+	char save_jc;
 	if(flag<3 && !sh_isstate(SH_INTERACTIVE))
 		flag = 0;
 	if(flag==2 && sfpkrd(sffileno(iop),buff,1,'\n',0,1) >= 0)
 		flag = 0;
 	if(flag==0)
 		return(sfsync(sfstderr));
+	save_jc = job.jobcontrol;
+	job.jobcontrol = 0;
 	sfflags = sfset(sfstderr,SF_SHARE|SF_PUBLIC|SF_READ,0);
 	if(!(shp->prompt=(char*)sfreserve(sfstderr,0,0)))
 		shp->prompt = "";
@@ -2124,6 +2127,7 @@ static int	io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
 done:
 	if(*shp->prompt && (endprompt=(char*)sfreserve(sfstderr,0,0)))
 		*endprompt = 0;
+	job.jobcontrol = save_jc;
 	sfset(sfstderr,sfflags&SF_READ|SF_SHARE|SF_PUBLIC,1);
 	return(sfsync(sfstderr));
 }
diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index c243634..31b5d46 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT) && !sh_isstate(SH_GRACE))
+	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@posguy99
Copy link
Author

posguy99 commented Aug 9, 2020

I have not been able to crash it with this patch. Has anyone else? This is like one of those things where you visit the doctor and say "it hurts when I do this..." and the only thing the doctor can say is "well, don't do that?"

@McDutchie
Copy link

Well, crashes are bad. It should not be possible to make the shell crash.

It's good news that the patch appears to have made the crash go away.

Unfortunately, the check for job.jobcontrol introduced in 3385868 causes a regression test failure on FreeBSD:

$ bin/shtests -p pty
#### Regression-testing /usr/local/src/ksh/arch/freebsd12.amd64-64/bin/ksh ####
test pty begins at 2020-08-09+09:37:37
        pty.sh[553]: notify job state changes: line 560: expected "Done", got EOF
test pty failed at 2020-08-09+09:37:50 with exit code 1 [ 25 tests 1 error ]

No idea why that occurs on FreeBSD and not Linux or the Mac, but it still means we can't use that check. And we can't use the SH_GRACE check because it breaks set -b in the grace period.

But it now looks like the key is to avoid calling job_list() and job_unpost() while expanding $PS1. After reverting the job.jobcontrol check, this is done if both the -b/notify (SH_NOTIFY) option and the SSH_TTYWAIT state are on. So it should be enough to turn off the SSH_TTYWAIT state while expanding/showing prompts.

Please test the following patch against current HEAD. Hopefully it still fixes this and #112.

diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c
index f1e72f4..179bd3f 100644
--- a/src/cmd/ksh93/sh/io.c
+++ b/src/cmd/ksh93/sh/io.c
@@ -2067,12 +2067,15 @@ static int	io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
 	char *endprompt;
 	static short cmdno;
 	int sfflags;
+	int was_ttywait_on;
 	if(flag<3 && !sh_isstate(SH_INTERACTIVE))
 		flag = 0;
 	if(flag==2 && sfpkrd(sffileno(iop),buff,1,'\n',0,1) >= 0)
 		flag = 0;
 	if(flag==0)
 		return(sfsync(sfstderr));
+	was_ttywait_on = sh_isstate(SH_TTYWAIT);
+	sh_offstate(SH_TTYWAIT);
 	sfflags = sfset(sfstderr,SF_SHARE|SF_PUBLIC|SF_READ,0);
 	if(!(shp->prompt=(char*)sfreserve(sfstderr,0,0)))
 		shp->prompt = "";
@@ -2125,6 +2128,8 @@ static int	io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
 done:
 	if(*shp->prompt && (endprompt=(char*)sfreserve(sfstderr,0,0)))
 		*endprompt = 0;
+	if(was_ttywait_on)
+		sh_onstate(SH_TTYWAIT);
 	sfset(sfstderr,sfflags&SF_READ|SF_SHARE|SF_PUBLIC,1);
 	return(sfsync(sfstderr));
 }
diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c
index c243634..3170276 100644
--- a/src/cmd/ksh93/sh/jobs.c
+++ b/src/cmd/ksh93/sh/jobs.c
@@ -460,7 +460,7 @@ int job_reap(register int sig)
 		nochild = 1;
 	}
 	shp->gd->waitevent = waitevent;
-	if(job.jobcontrol && sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT) && !sh_isstate(SH_GRACE))
+	if(sh_isoption(SH_NOTIFY) && sh_isstate(SH_TTYWAIT))
 	{
 		outfile = sfstderr;
 		job_list(pw,JOB_NFLAG|JOB_NLFLAG);

@posguy99
Copy link
Author

posguy99 commented Aug 9, 2020

Applied to 10.15.6. Trying to break it now. I won't have access to the 10.14.6 machine again until tonight.

@McDutchie McDutchie removed the help wanted Extra attention is needed label Aug 9, 2020
@posguy99
Copy link
Author

I ran it all day on 10.15.6 and it didn't crash, either line editing or trying to force the read timeout segfault.

Amusingly, I put it on 10.14.6 and the first thing I got was the background job hang. :). Nothing else to report so far.

set -o notify is NOT broken with this one, as outlined by @JohnoKing in e805c7d#commitcomment-41290508.

@posguy99
Copy link
Author

OK, I deleted all of that thread, it's nonsense, I made up a new testing set up and am testing again on 10.14.6.

@posguy99
Copy link
Author

Results... it hasn't crashed on 10.15.6, it hasn't crashed on 10.14.6.

My self-imposed carelessness last night with copying around versions of the source tree did not lead to verifiable testing (hence the now-deleted report of a worse crash). That was entirely on me and I apologize for the noise.

@McDutchie
Copy link

No problem, thanks for your persistence! Your testing has been really important and I am now hopeful that we've got a correct fix.

As for the hang, that's #111 and will hopefully be dealt with separately at some point.

@McDutchie
Copy link

For the historic record: The fix for this bug was incomplete and papered over the problem. I believe I applied the real fix for this bug in 51b2e36 on 20th February 2021. That crash was really another manifestation of this one. In job_reap(), the local pw variable was not initialised, and there are code paths in that function where it never gets a value. So, with SH_NOTIFY on, it could reach the point of the crash, calling job_list and job_unpost with potentially random garbage for a job pointer. The fix was of course to initialise the pointer and check it got a value before trying to use it. So the workaround from 61437b2 can be deleted.

McDutchie added a commit that referenced this issue Jul 19, 2022
The fix for #103 was incomplete and papered over the problem. The
real fix for this bug was applied in 51b2e36 on 20th February 2021.
That crash was the same one triggered differently.

#103 (comment)
McDutchie added a commit that referenced this issue Jul 19, 2022
The fix for #103 was incomplete and papered over the problem. The
real fix for this bug was applied in 51b2e36 on 20th February 2021.
That crash was the same one triggered differently.

#103 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working
Projects
None yet
Development

No branches or pull requests

3 participants