[tlbuild] dvi test?

John Hawkinson jhawk at alum.mit.edu
Tue Feb 1 03:42:19 CET 2022


Karl Berry <karl at freefriends.org> wrote on Mon, 31 Jan 2022
at 21:25:22 EST in <202202010225.2112PM9E003560 at freefriends.org>:

>      3208/0x31ed:  ftruncate(0x6, 0x0, 0x0)		 = 0 0
>      3208/0x31ed:  lseek(0x6, 0xFFFFFFFFFFFFF7C7, 0x1)		 = -1 Err#22
> 
> fseek (aka lseek) should presumably not be getting called with that
> strangely huge value (-something?) right after ftruncate has truncated
> it to empty.

Note that the 3rd parameter is 0x1 which is SEEK_CUR, rather than the specified SEEK_SET.

At first I ws going to speculate that MacOS libc is translating something in the chain from fseek() to fseeko() to __sflush() to _sseek() to __lseek() that ultimately happens, but actually I don't think so:

> ftruncate is only used directly in one place in xdvi: dvi-init.c
> line 1193:
> 	if (ftruncate(tmp_fd, 0) < 0) {
> 	  <failure code>
> 	}
> 	fseek(target_fp, 0L, SEEK_SET);
> 	fseek(source_fp, 0L, SEEK_SET);

Yup, that's where it is:

(lldb) b ftruncate
Breakpoint 1: where = libsystem_kernel.dylib`ftruncate, address = 0x00000001802d5008
(lldb) r
Process 65966 launched: '/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/xdvi-bin' (arm64)
3debug try_fopen t2a.dvi for fd 4
3debug try_fopen /Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi for fd 5
debug fileno is 5
debug fd position: 0, 
debug fd position: 2184, 
debug read 8 bytes from fp: -9 2 1 -125 -110 -64 28 59 
3debug try_fopen /usr/local/texlive/2021/texmf-config/dvips/config/config.ps for fd 5
fopen: No such file or directory
3debug while try_fopen /Users/jhawk/.dvipsrc
3debug try_fopen /usr/local/texlive/2021/texmf-dist/dvips/xdvi/config.xdvi for fd 5
3debug try_fopen /usr/local/texlive/2021/texmf-var/fonts/map/dvips/updmap/psfonts.map for fd 5
3debug try_fopen /Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi for fd 4
debug fileno is 6
debug fd position: 0, 
debug fd position: 2184, 
debug read 8 bytes from fp: -9 2 1 -125 -110 -64 28 59 
3debug try_fopen /usr/local/texlive/2021/texmf-dist/dvips/l3backend/l3backend-dvips.pro for fd 8
3debug try_fopen /usr/local/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx12.pfb for fd 8
3debug try_fopen /usr/local/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmcsc10.pfb for fd 9
3debug try_fopen /usr/local/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb for fd 10
debug fileno is 4
debug fd position: 0, 
debug fd position: 2184, 
debug read 8 bytes from fp: -9 2 1 -125 -110 -64 28 59 
3debug try_fopen /Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi for fd 4
Process 65966 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001ae8cd008 libsystem_kernel.dylib`ftruncate
libsystem_kernel.dylib`ftruncate:
->  0x1ae8cd008 <+0>:  mov    x16, #0xc9
    0x1ae8cd00c <+4>:  svc    #0x80
    0x1ae8cd010 <+8>:  b.lo   0x1ae8cd030               ; <+40>
    0x1ae8cd014 <+12>: pacibsp 
Target 0: (xdvi-bin) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x00000001ae8cd008 libsystem_kernel.dylib`ftruncate
    frame #1: 0x0000000100013590 xdvi-bin`make_backup_fp(source_fp=0x0000000207f6ec10, target_fp=0x0000000207f6ed40) at dvi-init.c:1209:6
    frame #2: 0x0000000100012f44 xdvi-bin`internal_open_dvi(path="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", errflag=0x000000016fdff1cc, load_fonts='\x01') at dvi-init.c:1404:61
    frame #3: 0x0000000100014fec xdvi-bin`load_dvi_file(load_fonts='\x01', errflag=0x000000016fdff1cc) at dvi-init.c:1886:10
    frame #4: 0x00000001000230e4 xdvi-bin`do_pages at events.c:6235:11
    frame #5: 0x00000001000566b8 xdvi-bin`run_dvi_file(filename="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", data=0x00000001001762e0) at xdvi.c:3622:5
    frame #6: 0x0000000100001298 xdvi-bin`main(argc=2, argv=0x000000016fdff650) at main.c:1311:6
    frame #7: 0x00000001002010f4 dyld`start + 520
(lldb) b fseek
Breakpoint 2: where = libsystem_c.dylib`fseek, address = 0x00000001ae7e018c
(lldb) c
Process 65966 resuming
Process 65966 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001ae8cd008 libsystem_kernel.dylib`ftruncate
libsystem_kernel.dylib`ftruncate:
->  0x1ae8cd008 <+0>:  mov    x16, #0xc9
    0x1ae8cd00c <+4>:  svc    #0x80
    0x1ae8cd010 <+8>:  b.lo   0x1ae8cd030               ; <+40>
    0x1ae8cd014 <+12>: pacibsp 
Target 0: (xdvi-bin) stopped.
(lldb) c
Process 65966 resuming
Process 65966 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x00000001ae7e018c libsystem_c.dylib`fseek
libsystem_c.dylib`fseek:
->  0x1ae7e018c <+0>:  pacibsp 
    0x1ae7e0190 <+4>:  stp    x22, x21, [sp, #-0x30]!
    0x1ae7e0194 <+8>:  stp    x20, x19, [sp, #0x10]
    0x1ae7e0198 <+12>: stp    x29, x30, [sp, #0x20]
Target 0: (xdvi-bin) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x00000001ae7e018c libsystem_c.dylib`fseek
    frame #1: 0x0000000100013658 xdvi-bin`make_backup_fp(source_fp=0x0000000207f6ec10, target_fp=0x0000000207f6ed40) at dvi-init.c:1219:2
    frame #2: 0x0000000100012f44 xdvi-bin`internal_open_dvi(path="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", errflag=0x000000016fdff1cc, load_fonts='\x01') at dvi-init.c:1404:61
    frame #3: 0x0000000100014fec xdvi-bin`load_dvi_file(load_fonts='\x01', errflag=0x000000016fdff1cc) at dvi-init.c:1886:10
    frame #4: 0x00000001000230e4 xdvi-bin`do_pages at events.c:6235:11
    frame #5: 0x00000001000566b8 xdvi-bin`run_dvi_file(filename="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", data=0x00000001001762e0) at xdvi.c:3622:5
    frame #6: 0x0000000100001298 xdvi-bin`main(argc=2, argv=0x000000016fdff650) at main.c:1311:6
    frame #7: 0x00000001002010f4 dyld`start + 520
(lldb) up
frame #1: 0x0000000100013658 xdvi-bin`make_backup_fp(source_fp=0x0000000207f6ec10, target_fp=0x0000000207f6ed40) at dvi-init.c:1219:2
   1216		    return NULL;
   1217		}
   1218	#endif
-> 1219		fseek(target_fp, 0L, SEEK_SET);
   1220		fseek(source_fp, 0L, SEEK_SET);
   1221	    }
   1222	
(lldb) b lseek
Breakpoint 3: where = libsystem_kernel.dylib`__lseek, address = 0x00000001ae8cb248
(lldb) c
Process 65966 resuming
Process 65966 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x00000001ae7e018c libsystem_c.dylib`fseek
libsystem_c.dylib`fseek:
->  0x1ae7e018c <+0>:  pacibsp 
    0x1ae7e0190 <+4>:  stp    x22, x21, [sp, #-0x30]!
    0x1ae7e0194 <+8>:  stp    x20, x19, [sp, #0x10]
    0x1ae7e0198 <+12>: stp    x29, x30, [sp, #0x20]
Target 0: (xdvi-bin) stopped.
(lldb) c
Process 65966 resuming
Process 65966 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
    frame #0: 0x00000001ae8cb248 libsystem_kernel.dylib`__lseek
libsystem_kernel.dylib`__lseek:
->  0x1ae8cb248 <+0>:  mov    x16, #0xc7
    0x1ae8cb24c <+4>:  svc    #0x80
    0x1ae8cb250 <+8>:  b.lo   0x1ae8cb270               ; <+40>
    0x1ae8cb254 <+12>: pacibsp 
Target 0: (xdvi-bin) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
  * frame #0: 0x00000001ae8cb248 libsystem_kernel.dylib`__lseek
    frame #1: 0x00000001ae7e06c4 libsystem_c.dylib`_sseek + 72
    frame #2: 0x00000001ae7da2bc libsystem_c.dylib`__sflush + 108
    frame #3: 0x00000001ae7fcd64 libsystem_c.dylib`_fseeko + 716
    frame #4: 0x00000001ae7e01ec libsystem_c.dylib`fseek + 96
    frame #5: 0x0000000100013658 xdvi-bin`make_backup_fp(source_fp=0x0000000207f6ec10, target_fp=0x0000000207f6ed40) at dvi-init.c:1219:2
    frame #6: 0x0000000100012f44 xdvi-bin`internal_open_dvi(path="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", errflag=0x000000016fdff1cc, load_fonts='\x01') at dvi-init.c:1404:61
    frame #7: 0x0000000100014fec xdvi-bin`load_dvi_file(load_fonts='\x01', errflag=0x000000016fdff1cc) at dvi-init.c:1886:10
    frame #8: 0x00000001000230e4 xdvi-bin`do_pages at events.c:6235:11
    frame #9: 0x00000001000566b8 xdvi-bin`run_dvi_file(filename="/Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi", data=0x00000001001762e0) at xdvi.c:3622:5
    frame #10: 0x0000000100001298 xdvi-bin`main(argc=2, argv=0x000000016fdff650) at main.c:1311:6
    frame #11: 0x00000001002010f4 dyld`start + 520
(lldb) frame select 5
frame #5: 0x0000000100013658 xdvi-bin`make_backup_fp(source_fp=0x0000000207f6ec10, target_fp=0x0000000207f6ed40) at dvi-init.c:1219:2
   1216		    return NULL;
   1217		}
   1218	#endif
-> 1219		fseek(target_fp, 0L, SEEK_SET);
   1220		fseek(source_fp, 0L, SEEK_SET);
   1221	    }
   1222	



> I think it would be a good idea to check the return values from those
> fseeks.

Yes, especially since the system call trace has it returning EINVAL (22).

> My brain is too wiped (got ftruncated today I guess :) to commit the
> change now, even though it is trivial ... --thanks, karl.

Anyhow, sure enough:

--- a/texk/xdvik/dvi-init.c
+++ b/texk/xdvik/dvi-init.c
@@ -1200,8 +1216,12 @@ make_backup_fp(FILE *source_fp, FILE *target_fp)
 	    return NULL;
 	}
 #endif
-	fseek(target_fp, 0L, SEEK_SET);
-	fseek(source_fp, 0L, SEEK_SET);
+	if (fseek(target_fp, 0L, SEEK_SET)) {
+	  perror("fseek of target_fp");
+	}
+	if (fseek(source_fp, 0L, SEEK_SET)) {
+	  perror("fseek of source_fp");
+	}
     }
 
     /* copy the file */

produces (with my other debugging interspersed):

3debug try_fopen /Users/jhawk/src/xdvik-22.87.05/Work/texk/xdvik/t2a.dvi for fd 4
fseek of target_fp: Invalid argument
debug fileno is 6
debug fd position: 0, 
debug fd position: 4096, 
debug read 8 bytes from fp: 0 0 0 0 0 0 0 0 
xdvi-bin: set_no_char: attempt to set character of unknown font, offset 42
xdvi-bin 22.87.05 (Xaw toolkit): ../../../texk/xdvik/dvi-draw.c:488: Shouldn't happen: I'll abort now, to help you debugging this.
zsh: abort      TEXMFROOT=/usr/local/texlive/2021 TEXMFCNF=$TEXMFROOT/texmf-dist/web2c  


of course, we need to do more than just print diagnostics, we should presumably return NULL which will stop the crash, but doesn't quite tell us what went wrong. But definitely good hints at where to look.

--
jhawk at alum.mit.edu
John Hawkinson


More information about the tlbuild mailing list.