Cygwin IPC - ftok() returns negative values - Bug Report

Stanisław Wawszczak stanislaw.wawszczak@iscg.pl
Wed Jun 29 13:36:00 GMT 2016


Dear All,
 
I have had to compile sblim-sfcbd-1.4.10 on Cygwin. It is using IPC semaphores.
Unfortunately it is returning wrong value as the result of complicated bit-wise logical operations.
I have tried to “hack the system” and make multiplication of returned value by -1, but it triggers error in semget()
Environment:
Windows 2012 R2 with latest patches
I have installed Cygwin from official installer couple days ago.
 
I have attached the gdb session from such invocation. You can see the path and id in arguments are correct and stat() function returns correct values.
Finally result in %rax is negative:
rax            0xaf0b000701cc1d53       -5833568862233420461

Best Regards,
Stanislaw Wawszczak
Iscg Poland 

###############################################################
# GDB session
###############################################################
 
Breakpoint 1, ftok (path=0x4b11f1912 <nls+666> "/usr/local/sbin/sfcbd.exe", id=83) at /usr/src/debug/cygwin-2.5.2-1/winsup/cygwin/ipc.cc:18
18      {
(gdb) x/5i
   0x4b1201438 <sfcbSockets>:   (bad)
   0x4b1201439 <sfcbSockets+1>: add    %al,(%rax)
   0x4b120143b <sfcbSockets+3>: add    %al,(%rdi)
   0x4b120143d <sfcbSockets+5>: add    %al,(%rax)
   0x4b120143f <sfcbSockets+7>: add    %al,(%rbx)
(gdb) x/5i
   0x4b1201441 <resultSockets+1>:       add    %al,(%rax)
   0x4b1201443 <resultSockets+3>:       add    %al,0x0(%rip)        # 0x4b1201449 <ptBase+1>
   0x4b1201449 <ptBase+1>:      add    %al,(%rax)
   0x4b120144b <ptBase+3>:      add    %al,(%rax)
  0x4b120144d <stBase+1>:      add    %al,(%rax)
(gdb) x/5i $pc
=> 0x1800c6e50 <ftok(char const*, int)>:        push   %rbx
   0x1800c6e51 <ftok(char const*, int)+1>:      sub    $0xa0,%rsp
   0x1800c6e58 <ftok(char const*, int)+8>:      mov    %edx,%ebx
  0x1800c6e5a <ftok(char const*, int)+10>:     lea    0x20(%rsp),%rdx
   0x1800c6e5f <ftok(char const*, int)+15>:     callq  0x180144970 <stat64(char const*, stat*)>
(gdb) x/5i
   0x1800c6e64 <ftok(char const*, int)+20>:     test   %eax,%eax
   0x1800c6e66 <ftok(char const*, int)+22>:     jne    0x1800c6ec0 <ftok(char const*, int)+112>
   0x1800c6e68 <ftok(char const*, int)+24>:     mov    0x20(%rsp),%edx
   0x1800c6e6c <ftok(char const*, int)+28>:     mov    0x28(%rsp),%r8
   0x1800c6e71 <ftok(char const*, int)+33>:     mov    %edx,%eax
(gdb) x/5i
   0x1800c6e73 <ftok(char const*, int)+35>:     movzbl %dl,%edx
   0x1800c6e76 <ftok(char const*, int)+38>:     and    $0xff0000,%eax
   0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
   0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
   0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
(gdb) x/5i
   0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
   0x1800c6e90 <ftok(char const*, int)+64>:     and    %r8,%rdx
   0x1800c6e93 <ftok(char const*, int)+67>:     and    $0x3fffff,%r8d
   0x1800c6e9a <ftok(char const*, int)+74>:     shr    $0x10,%rdx
   0x1800c6e9e <ftok(char const*, int)+78>:     shl    $0x8,%r8
(gdb) x/5i
   0x1800c6ea2 <ftok(char const*, int)+82>:     or     %rdx,%rax
   0x1800c6ea5 <ftok(char const*, int)+85>:     mov    %rax,%rcx
   0x1800c6ea8 <ftok(char const*, int)+88>:     movzbl %bl,%eax
   0x1800c6eab <ftok(char const*, int)+91>:     or     %r8,%rax
   0x1800c6eae <ftok(char const*, int)+94>:     or     %rcx,%rax
(gdb) x/5i
   0x1800c6eb1 <ftok(char const*, int)+97>:     add    $0xa0,%rsp
   0x1800c6eb8 <ftok(char const*, int)+104>:    pop    %rbx
   0x1800c6eb9 <ftok(char const*, int)+105>:    retq
   0x1800c6eba <ftok(char const*, int)+106>:    nopw   0x0(%rax,%rax,1)
   0x1800c6ec0 <ftok(char const*, int)+112>:    mov    $0xffffffffffffffff,%rax
(gdb) x/5i
   0x1800c6ec7 <ftok(char const*, int)+119>:    jmp    0x1800c6eb1 <ftok(char const*, int)+97>
   0x1800c6ec9 <ftok+121>:      nop
   0x1800c6eca <ftok+122>:      nop
   0x1800c6ecb <ftok+123>:      nop
   0x1800c6ecc <ftok+124>:      nop
(gdb) ni
0x00000001800c6e51      18      {
(gdb) ni
18      {
(gdb) ni
21        if (stat64 (path, &statbuf))
(gdb) x/5i $pc
=> 0x1800c6e5a <ftok(char const*, int)+10>:     lea    0x20(%rsp),%rdx
   0x1800c6e5f <ftok(char const*, int)+15>:     callq  0x180144970 <stat64(char const*, stat*)>
   0x1800c6e64 <ftok(char const*, int)+20>:     test   %eax,%eax
   0x1800c6e66 <ftok(char const*, int)+22>:     jne    0x1800c6ec0 <ftok(char const*, int)+112>
   0x1800c6e68 <ftok(char const*, int)+24>:     mov    0x20(%rsp),%edx
(gdb) ni
0x00000001800c6e5f      21        if (stat64 (path, &statbuf))
(gdb) x/5i $pc
=> 0x1800c6e5f <ftok(char const*, int)+15>:     callq  0x180144970 <stat64(char const*, stat*)>
   0x1800c6e64 <ftok(char const*, int)+20>:     test   %eax,%eax
   0x1800c6e66 <ftok(char const*, int)+22>:     jne    0x1800c6ec0 <ftok(char const*, int)+112>
   0x1800c6e68 <ftok(char const*, int)+24>:     mov    0x20(%rsp),%edx
   0x1800c6e6c <ftok(char const*, int)+28>:     mov    0x28(%rsp),%r8
(gdb) ni
0x00000001800c6e64      21        if (stat64 (path, &statbuf))
(gdb) x/5i $pc
=> 0x1800c6e64 <ftok(char const*, int)+20>:     test   %eax,%eax
   0x1800c6e66 <ftok(char const*, int)+22>:     jne    0x1800c6ec0 <ftok(char const*, int)+112>
   0x1800c6e68 <ftok(char const*, int)+24>:     mov    0x20(%rsp),%edx
   0x1800c6e6c <ftok(char const*, int)+28>:     mov    0x28(%rsp),%r8
   0x1800c6e71 <ftok(char const*, int)+33>:     mov    %edx,%eax
(gdb) r %eax
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) info registers
rax            0x0      0
rbx            0x53     83
rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x0      0 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0xffffc768       4294952808
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e64      0x1800c6e64 <ftok(char const*, int)+20> eflags         0x202    [ IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6e66      21        if (stat64 (path, &statbuf))
(gdb) r %eax
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) x/5i $pc
=> 0x1800c6e66 <ftok(char const*, int)+22>:     jne    0x1800c6ec0 <ftok(char const*, int)+112>
   0x1800c6e68 <ftok(char const*, int)+24>:     mov    0x20(%rsp),%edx
   0x1800c6e6c <ftok(char const*, int)+28>:     mov    0x28(%rsp),%r8
   0x1800c6e71 <ftok(char const*, int)+33>:     mov    %edx,%eax
   0x1800c6e73 <ftok(char const*, int)+35>:     movzbl %dl,%edx
(gdb) ni
78        tmp  = (((key_t) statbuf.st_dev) & 0x0000000000ff0000LL) << 40;
(gdb) print 0x20(%rsp
A syntax error in expression, near `%rsp'.
(gdb) print 0x20(%rsp)
A syntax error in expression, near `%rsp)'.
(gdb) ni
83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e6c <ftok(char const*, int)+28>:     mov    0x28(%rsp),%r8
   0x1800c6e71 <ftok(char const*, int)+33>:     mov    %edx,%eax
   0x1800c6e73 <ftok(char const*, int)+35>:     movzbl %dl,%edx
   0x1800c6e76 <ftok(char const*, int)+38>:     and    $0xff0000,%eax
   0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
(gdb) info registers
rax            0x0      0
rbx            0x53     83
rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x7eafb40b       2125444107 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0xffffc768       4294952808
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e6c      0x1800c6e6c <ftok(char const*, int)+28> eflags         0x246    [ PF ZF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
78        tmp  = (((key_t) statbuf.st_dev) & 0x0000000000ff0000LL) << 40;
(gdb) x/5i $pc
=> 0x1800c6e71 <ftok(char const*, int)+33>:     mov    %edx,%eax
   0x1800c6e73 <ftok(char const*, int)+35>:     movzbl %dl,%edx
   0x1800c6e76 <ftok(char const*, int)+38>:     and    $0xff0000,%eax
   0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
   0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
(gdb) ni
83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e73 <ftok(char const*, int)+35>:     movzbl %dl,%edx
   0x1800c6e76 <ftok(char const*, int)+38>:     and    $0xff0000,%eax
   0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
   0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
   0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
(gdb) info registers
rax            0x7eafb40b       2125444107 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x7eafb40b       2125444107 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e73      0x1800c6e73 <ftok(char const*, int)+35> eflags         0x246    [ PF ZF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
78        tmp  = (((key_t) statbuf.st_dev) & 0x0000000000ff0000LL) << 40;
(gdb) info registers
rax            0x7eafb40b       2125444107 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xb      11 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e76      0x1800c6e76 <ftok(char const*, int)+38> eflags         0x246    [ PF ZF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) x/5i $pc
=> 0x1800c6e76 <ftok(char const*, int)+38>:     and    $0xff0000,%eax
   0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
   0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
   0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
   0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
(gdb) ni; x/5i $pc; info registers
Invalid character ';' in expression.
(gdb) ni
83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e7b <ftok(char const*, int)+43>:     shl    $0x30,%rdx
   0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
   0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
   0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
   0x1800c6e90 <ftok(char const*, int)+64>:     and    %r8,%rdx
(gdb) info registers
rax            0xaf0000 11468800
rbx            0x53     83
rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xb      11 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e7b      0x1800c6e7b <ftok(char const*, int)+43> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
78        tmp  = (((key_t) statbuf.st_dev) & 0x0000000000ff0000LL) << 40;
(gdb) info registers
rax            0xaf0000 11468800
rbx            0x53     83
rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xb000000000000  3096224743817216 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e7f      0x1800c6e7f <ftok(char const*, int)+47> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) x/5i $pc
=> 0x1800c6e7f <ftok(char const*, int)+47>:     shl    $0x28,%rax
   0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
   0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
   0x1800c6e90 <ftok(char const*, int)+64>:     and    %r8,%rdx
   0x1800c6e93 <ftok(char const*, int)+67>:     and    $0x3fffff,%r8d
(gdb) ni
83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e83 <ftok(char const*, int)+51>:     or     %rdx,%rax
   0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
   0x1800c6e90 <ftok(char const*, int)+64>:     and    %r8,%rdx
   0x1800c6e93 <ftok(char const*, int)+67>:     and    $0x3fffff,%r8d
   0x1800c6e9a <ftok(char const*, int)+74>:     shr    $0x10,%rdx
(gdb) info registers
rax            0xaf00000000000000       -5836665117072162816 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xb000000000000  3096224743817216 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e83      0x1800c6e83 <ftok(char const*, int)+51> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6e86      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e86 <ftok(char const*, int)+54>:     movabs $0xffffc00000000000,%rdx
   0x1800c6e90 <ftok(char const*, int)+64>:     and    %r8,%rdx
   0x1800c6e93 <ftok(char const*, int)+67>:     and    $0x3fffff,%r8d
   0x1800c6e9a <ftok(char const*, int)+74>:     shr    $0x10,%rdx
   0x1800c6e9e <ftok(char const*, int)+78>:     shl    $0x8,%r8
(gdb) info registers
rax            0xaf0b000000000000       -5833568892328345600 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xb000000000000  3096224743817216 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e86      0x1800c6e86 <ftok(char const*, int)+54> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6e90      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) info registers
rax            0xaf0b000000000000       -5833568892328345600 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0xffffc00000000000       -70368744177664 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e90      0x1800c6e90 <ftok(char const*, int)+64> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6e93      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6e93 <ftok(char const*, int)+67>:     and    $0x3fffff,%r8d
   0x1800c6e9a <ftok(char const*, int)+74>:     shr    $0x10,%rdx
   0x1800c6e9e <ftok(char const*, int)+78>:     shl    $0x8,%r8
   0x1800c6ea2 <ftok(char const*, int)+82>:     or     %rdx,%rax
   0x1800c6ea5 <ftok(char const*, int)+85>:     mov    %rax,%rcx
(gdb) info registers
rax            0xaf0b000000000000       -5833568892328345600 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x7000000000000  1970324836974592 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x700000001cc1d  1970324837092381
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e93      0x1800c6e93 <ftok(char const*, int)+67> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6e9a      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) ni
0x00000001800c6e9e      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) info registers
rax            0xaf0b000000000000       -5833568892328345600 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d  117789
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6e9e      0x1800c6e9e <ftok(char const*, int)+78> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6ea2      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) ni
0x00000001800c6ea5      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) info registers
rax            0xaf0b000700000000       -5833568862263574528 rbx            0x53     83 rcx            0x7ffa7b5dac7a   140713788288122 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6ea5      0x1800c6ea5 <ftok(char const*, int)+85> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) x/5i $pc
=> 0x1800c6ea5 <ftok(char const*, int)+85>:     mov    %rax,%rcx
   0x1800c6ea8 <ftok(char const*, int)+88>:     movzbl %bl,%eax
   0x1800c6eab <ftok(char const*, int)+91>:     or     %r8,%rax
   0x1800c6eae <ftok(char const*, int)+94>:     or     %rcx,%rax
   0x1800c6eb1 <ftok(char const*, int)+97>:     add    $0xa0,%rsp
(gdb) ni
0x00000001800c6ea8      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) x/5i $pc
=> 0x1800c6ea8 <ftok(char const*, int)+88>:     movzbl %bl,%eax
   0x1800c6eab <ftok(char const*, int)+91>:     or     %r8,%rax
   0x1800c6eae <ftok(char const*, int)+94>:     or     %rcx,%rax
   0x1800c6eb1 <ftok(char const*, int)+97>:     add    $0xa0,%rsp
   0x1800c6eb8 <ftok(char const*, int)+104>:    pop    %rbx
(gdb) info registers
rax            0xaf0b000700000000       -5833568862263574528 rbx            0x53     83 rcx            0xaf0b000700000000       -5833568862263574528 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6ea8      0x1800c6ea8 <ftok(char const*, int)+88> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6eab      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) info registers
rax            0x53     83
rbx            0x53     83
rcx            0xaf0b000700000000       -5833568862263574528 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6eab      0x1800c6eab <ftok(char const*, int)+91> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6eae      83        tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
(gdb) info registers
rax            0x1cc1d53        30154067 rbx            0x53     83 rcx            0xaf0b000700000000       -5833568862263574528 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6eae      0x1800c6eae <ftok(char const*, int)+94> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) x/5i $pc
=> 0x1800c6eae <ftok(char const*, int)+94>:     or     %rcx,%rax
   0x1800c6eb1 <ftok(char const*, int)+97>:     add    $0xa0,%rsp
   0x1800c6eb8 <ftok(char const*, int)+104>:    pop    %rbx
   0x1800c6eb9 <ftok(char const*, int)+105>:    retq
   0x1800c6eba <ftok(char const*, int)+106>:    nopw   0x0(%rax,%rax,1)
(gdb) info registers
rax            0x1cc1d53        30154067 rbx            0x53     83 rcx            0xaf0b000700000000       -5833568862263574528 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6eae      0x1800c6eae <ftok(char const*, int)+94> eflags         0x206    [ PF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
89      }
(gdb) info registers
rax            0xaf0b000701cc1d53       -5833568862233420461 rbx            0x53     83 rcx            0xaf0b000700000000       -5833568862263574528 rdx            0x700000000      30064771072 rsi            0x4      4 rdi            0x10040596e      4299184494 rbp            0x4b1201464      0x4b1201464 <labelProcs> rsp            0xffffc930       0xffffc930
r8             0x1cc1d00        30153984
r9             0x4b1201464      20151538788
r10            0x0      0
r11            0x203    515
r12            0x4b11ef0c0      20151464128
r13            0x20     32
r14            0x0      0
r15            0x5      5
rip            0x1800c6eb1      0x1800c6eb1 <ftok(char const*, int)+97> eflags         0x286    [ PF SF IF ] cs             0x33     51 ss             0x2b     43 ds             0x2b     43 es             0x2b     43 fs             0x53     83 gs             0x2b     43
(gdb) ni
0x00000001800c6eb8      89      }
(gdb) x/5i $pc
=> 0x1800c6eb8 <ftok(char const*, int)+104>:    pop    %rbx
   0x1800c6eb9 <ftok(char const*, int)+105>:    retq
   0x1800c6eba <ftok(char const*, int)+106>:    nopw   0x0(%rax,%rax,1)
   0x1800c6ec0 <ftok(char const*, int)+112>:    mov    $0xffffffffffffffff,%rax
   0x1800c6ec7 <ftok(char const*, int)+119>:    jmp    0x1800c6eb1 <ftok(char const*, int)+97>
(gdb) ni
0x00000001800c6eb9      89      }
(gdb) n
 
##############################################################
# ftok() C source from debugger
##############################################################
 
 
/* ipc.cc: Single unix specification IPC interface for Cygwin
 
   Originally written by Robert Collins <robert.collins@hotmail.com>
   Updated to 64 bit key_t by Charles Wilson <cygwin@cwilson.fastmail.fm>
 
   This file is part of Cygwin.
 
   This software is a copyrighted work licensed under the terms of the
   Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
   details. */
 
#include "winsup.h"
#include <sys/stat.h>
 
/* Notes: we return a valid key even if id's low order 8 bits are 0. */ extern "C" key_t ftok (const char *path, int id) {
  struct stat statbuf;
  key_t tmp;
  if (stat64 (path, &statbuf))
    {
      /* stat set the appropriate errno for us */
      return (key_t) -1;
    }
 
  /* Since Cygwin 1.5
     dev_t is 32bits for cygwin
     ino_t is 64bits for cygwin
     and we need 8 bits for the id.
     thus key_t needs 104 bits total -- but we only have 64 (long long)
     We will have to alias; leaving open the possibility that the same
     key will be returned for multiple files.  This possibility exists
     also on Linux; the question is, how to minimize this possibility.
 
     How to solve?  Well, based on C. Vinschen's research, the nFileIndex*
     words vary as follows, on a partition with > 110,000 files
     nFileIndexHigh:    564 values between 0x00010000 -- 0xffff0000
     nFileIndexLow : 103812 values between 0x00000000 -- 0x0003ffff
     R. Collins suggests that these may represent a tree path,
     and that it would require ~2.9M files to force the tree depth
     to increase and reveal more bit usage.
 
     Implementation details: dev_t is 32bits, but is formed by
                device(32bits) << 16 | unit(32bits)
     But device is ACTUALLY == status & FH_DEVMASK, where FH_DEVMASK
     is 0x00000fff --> 12 bits
 
     As it happens, the maximum number of devices is actually
     FH_NDEV, not FH_DEVMASK, where FH_NDEV is currently 0x0000001d.
     However, FH_NDEV grows as new device types are added.  So
     currently the device number needs 5 bits, but later?  Let's
     take a cue from Linux, and use the lower 8 bits (instead of the
     lower 12 or 16) for the device (major?) number.
 
     Similarly, while 'units' is an int (32bits), it is unclear
     how many of these are significant. For most devices, it seems that
     'units' is equivalent to 'minor'.  For FH_TAPE, it's obvious that
     only 8 bits are important.  However, for FH_SOCKET...it might be
     as high as 16 significant bits.
 
     Let's assume that we only need 8 bits from device (major) and
     only 8 bits from unit (minor). (On linux, only 8 bits of minor
     are used, and none from major).
     ---> so, we only need 0x00ff00ff (16 bits) of dev_t
 
     ---> we MUST have all 8 bits of id.
 
     ---> So, we only have 64 - 8 - 16 = 40 bits for ino_t.  But, we
     need 0xffff0000 for nFileIndexHigh and 0x0003ffff for nFileIndexLow
     minimum, or 16 + 18 = 34 bits.  Lucky us - we have 6 more bits
     to distribute.
 
     For lack of a better idea, we'll allocate 2 of the extra bits to
     nFileIndexHigh and 4 to nFileIndexLow.  */
 
  /* get 8 bits from dev_t (major), put into 0xff00000000000000L */
  tmp  = (((key_t) statbuf.st_dev) & 0x0000000000ff0000LL) << 40;
  /* get 8 bits from dev_t (minor), put into 0x00ff000000000000L */
  tmp |= (((key_t) statbuf.st_dev) & 0x00000000000000ffLL) << 48;
  /* get upper 16+2 bits from nFileInfoHigh, put into 0x0000ffffc0000000L
     shift down first, then mask, to avoid sign extension on rightshift  */
  tmp |= (((key_t) statbuf.st_ino) & 0xffffc00000000000LL) >> 16;
  /* get lower 18+4 bits from nFileInfoLow, put into  0x000000003fffff00L  */
  tmp |= (((key_t) statbuf.st_ino) & 0x00000000003fffffLL) << 8;
  /* use all 8 bits of id, and put into 0x00000000000000ffL */
  tmp |= (id & 0x00ff);
  return tmp;
}
 
 



More information about the Cygwin mailing list