github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/internal/poll/fd_windows.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package poll 6 7 import ( 8 "errors" 9 "internal/race" 10 "internal/syscall/windows" 11 "io" 12 "sync" 13 "syscall" 14 "unicode/utf16" 15 "unicode/utf8" 16 "unsafe" 17 ) 18 19 var ( 20 initErr error 21 ioSync uint64 22 ) 23 24 // This package uses the SetFileCompletionNotificationModes Windows 25 // API to skip calling GetQueuedCompletionStatus if an IO operation 26 // completes synchronously. There is a known bug where 27 // SetFileCompletionNotificationModes crashes on some systems (see 28 // https://support.microsoft.com/kb/2568167 for details). 29 30 var useSetFileCompletionNotificationModes bool // determines is SetFileCompletionNotificationModes is present and safe to use 31 32 // checkSetFileCompletionNotificationModes verifies that 33 // SetFileCompletionNotificationModes Windows API is present 34 // on the system and is safe to use. 35 // See https://support.microsoft.com/kb/2568167 for details. 36 func checkSetFileCompletionNotificationModes() { 37 err := syscall.LoadSetFileCompletionNotificationModes() 38 if err != nil { 39 return 40 } 41 protos := [2]int32{syscall.IPPROTO_TCP, 0} 42 var buf [32]syscall.WSAProtocolInfo 43 len := uint32(unsafe.Sizeof(buf)) 44 n, err := syscall.WSAEnumProtocols(&protos[0], &buf[0], &len) 45 if err != nil { 46 return 47 } 48 for i := int32(0); i < n; i++ { 49 if buf[i].ServiceFlags1&syscall.XP1_IFS_HANDLES == 0 { 50 return 51 } 52 } 53 useSetFileCompletionNotificationModes = true 54 } 55 56 func init() { 57 var d syscall.WSAData 58 e := syscall.WSAStartup(uint32(0x202), &d) 59 if e != nil { 60 initErr = e 61 } 62 checkSetFileCompletionNotificationModes() 63 } 64 65 // operation contains superset of data necessary to perform all async IO. 66 type operation struct { 67 // Used by IOCP interface, it must be first field 68 // of the struct, as our code rely on it. 69 o syscall.Overlapped 70 71 // fields used by runtime.netpoll 72 runtimeCtx uintptr 73 mode int32 74 errno int32 75 qty uint32 76 77 // fields used only by net package 78 fd *FD 79 buf syscall.WSABuf 80 msg windows.WSAMsg 81 sa syscall.Sockaddr 82 rsa *syscall.RawSockaddrAny 83 rsan int32 84 handle syscall.Handle 85 flags uint32 86 bufs []syscall.WSABuf 87 } 88 89 func (o *operation) InitBuf(buf []byte) { 90 o.buf.Len = uint32(len(buf)) 91 o.buf.Buf = nil 92 if len(buf) != 0 { 93 o.buf.Buf = &buf[0] 94 } 95 } 96 97 func (o *operation) InitBufs(buf *[][]byte) { 98 if o.bufs == nil { 99 o.bufs = make([]syscall.WSABuf, 0, len(*buf)) 100 } else { 101 o.bufs = o.bufs[:0] 102 } 103 for _, b := range *buf { 104 if len(b) == 0 { 105 o.bufs = append(o.bufs, syscall.WSABuf{}) 106 continue 107 } 108 for len(b) > maxRW { 109 o.bufs = append(o.bufs, syscall.WSABuf{Len: maxRW, Buf: &b[0]}) 110 b = b[maxRW:] 111 } 112 if len(b) > 0 { 113 o.bufs = append(o.bufs, syscall.WSABuf{Len: uint32(len(b)), Buf: &b[0]}) 114 } 115 } 116 } 117 118 // ClearBufs clears all pointers to Buffers parameter captured 119 // by InitBufs, so it can be released by garbage collector. 120 func (o *operation) ClearBufs() { 121 for i := range o.bufs { 122 o.bufs[i].Buf = nil 123 } 124 o.bufs = o.bufs[:0] 125 } 126 127 func (o *operation) InitMsg(p []byte, oob []byte) { 128 o.InitBuf(p) 129 o.msg.Buffers = &o.buf 130 o.msg.BufferCount = 1 131 132 o.msg.Name = nil 133 o.msg.Namelen = 0 134 135 o.msg.Flags = 0 136 o.msg.Control.Len = uint32(len(oob)) 137 o.msg.Control.Buf = nil 138 if len(oob) != 0 { 139 o.msg.Control.Buf = &oob[0] 140 } 141 } 142 143 // execIO executes a single IO operation o. It submits and cancels 144 // IO in the current thread for systems where Windows CancelIoEx API 145 // is available. Alternatively, it passes the request onto 146 // runtime netpoll and waits for completion or cancels request. 147 func execIO(o *operation, submit func(o *operation) error) (int, error) { 148 if o.fd.pd.runtimeCtx == 0 { 149 return 0, errors.New("internal error: polling on unsupported descriptor type") 150 } 151 152 fd := o.fd 153 // Notify runtime netpoll about starting IO. 154 err := fd.pd.prepare(int(o.mode), fd.isFile) 155 if err != nil { 156 return 0, err 157 } 158 // Start IO. 159 err = submit(o) 160 switch err { 161 case nil: 162 // IO completed immediately 163 if o.fd.skipSyncNotif { 164 // No completion message will follow, so return immediately. 165 return int(o.qty), nil 166 } 167 // Need to get our completion message anyway. 168 case syscall.ERROR_IO_PENDING: 169 // IO started, and we have to wait for its completion. 170 err = nil 171 default: 172 return 0, err 173 } 174 // Wait for our request to complete. 175 err = fd.pd.wait(int(o.mode), fd.isFile) 176 if err == nil { 177 // All is good. Extract our IO results and return. 178 if o.errno != 0 { 179 err = syscall.Errno(o.errno) 180 // More data available. Return back the size of received data. 181 if err == syscall.ERROR_MORE_DATA || err == windows.WSAEMSGSIZE { 182 return int(o.qty), err 183 } 184 return 0, err 185 } 186 return int(o.qty), nil 187 } 188 // IO is interrupted by "close" or "timeout" 189 netpollErr := err 190 switch netpollErr { 191 case ErrNetClosing, ErrFileClosing, ErrDeadlineExceeded: 192 // will deal with those. 193 default: 194 panic("unexpected runtime.netpoll error: " + netpollErr.Error()) 195 } 196 // Cancel our request. 197 err = syscall.CancelIoEx(fd.Sysfd, &o.o) 198 // Assuming ERROR_NOT_FOUND is returned, if IO is completed. 199 if err != nil && err != syscall.ERROR_NOT_FOUND { 200 // TODO(brainman): maybe do something else, but panic. 201 panic(err) 202 } 203 // Wait for cancellation to complete. 204 fd.pd.waitCanceled(int(o.mode)) 205 if o.errno != 0 { 206 err = syscall.Errno(o.errno) 207 if err == syscall.ERROR_OPERATION_ABORTED { // IO Canceled 208 err = netpollErr 209 } 210 return 0, err 211 } 212 // We issued a cancellation request. But, it seems, IO operation succeeded 213 // before the cancellation request run. We need to treat the IO operation as 214 // succeeded (the bytes are actually sent/recv from network). 215 return int(o.qty), nil 216 } 217 218 // FD is a file descriptor. The net and os packages embed this type in 219 // a larger type representing a network connection or OS file. 220 type FD struct { 221 // Lock sysfd and serialize access to Read and Write methods. 222 fdmu fdMutex 223 224 // System file descriptor. Immutable until Close. 225 Sysfd syscall.Handle 226 227 // Read operation. 228 rop operation 229 // Write operation. 230 wop operation 231 232 // I/O poller. 233 pd pollDesc 234 235 // Used to implement pread/pwrite. 236 l sync.Mutex 237 238 // For console I/O. 239 lastbits []byte // first few bytes of the last incomplete rune in last write 240 readuint16 []uint16 // buffer to hold uint16s obtained with ReadConsole 241 readbyte []byte // buffer to hold decoding of readuint16 from utf16 to utf8 242 readbyteOffset int // readbyte[readOffset:] is yet to be consumed with file.Read 243 244 // Semaphore signaled when file is closed. 245 csema uint32 246 247 skipSyncNotif bool 248 249 // Whether this is a streaming descriptor, as opposed to a 250 // packet-based descriptor like a UDP socket. 251 IsStream bool 252 253 // Whether a zero byte read indicates EOF. This is false for a 254 // message based socket connection. 255 ZeroReadIsEOF bool 256 257 // Whether this is a file rather than a network socket. 258 isFile bool 259 260 // The kind of this file. 261 kind fileKind 262 } 263 264 // fileKind describes the kind of file. 265 type fileKind byte 266 267 const ( 268 kindNet fileKind = iota 269 kindFile 270 kindConsole 271 kindDir 272 kindPipe 273 ) 274 275 // logInitFD is set by tests to enable file descriptor initialization logging. 276 var logInitFD func(net string, fd *FD, err error) 277 278 // Init initializes the FD. The Sysfd field should already be set. 279 // This can be called multiple times on a single FD. 280 // The net argument is a network name from the net package (e.g., "tcp"), 281 // or "file" or "console" or "dir". 282 // Set pollable to true if fd should be managed by runtime netpoll. 283 func (fd *FD) Init(net string, pollable bool) (string, error) { 284 if initErr != nil { 285 return "", initErr 286 } 287 288 switch net { 289 case "file": 290 fd.kind = kindFile 291 case "console": 292 fd.kind = kindConsole 293 case "dir": 294 fd.kind = kindDir 295 case "pipe": 296 fd.kind = kindPipe 297 case "tcp", "tcp4", "tcp6", 298 "udp", "udp4", "udp6", 299 "ip", "ip4", "ip6", 300 "unix", "unixgram", "unixpacket": 301 fd.kind = kindNet 302 default: 303 return "", errors.New("internal error: unknown network type " + net) 304 } 305 fd.isFile = fd.kind != kindNet 306 307 var err error 308 if pollable { 309 // Only call init for a network socket. 310 // This means that we don't add files to the runtime poller. 311 // Adding files to the runtime poller can confuse matters 312 // if the user is doing their own overlapped I/O. 313 // See issue #21172. 314 // 315 // In general the code below avoids calling the execIO 316 // function for non-network sockets. If some method does 317 // somehow call execIO, then execIO, and therefore the 318 // calling method, will return an error, because 319 // fd.pd.runtimeCtx will be 0. 320 err = fd.pd.init(fd) 321 } 322 if logInitFD != nil { 323 logInitFD(net, fd, err) 324 } 325 if err != nil { 326 return "", err 327 } 328 if pollable && useSetFileCompletionNotificationModes { 329 // We do not use events, so we can skip them always. 330 flags := uint8(syscall.FILE_SKIP_SET_EVENT_ON_HANDLE) 331 // It's not safe to skip completion notifications for UDP: 332 // https://docs.microsoft.com/en-us/archive/blogs/winserverperformance/designing-applications-for-high-performance-part-iii 333 if net == "tcp" { 334 flags |= syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS 335 } 336 err := syscall.SetFileCompletionNotificationModes(fd.Sysfd, flags) 337 if err == nil && flags&syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS != 0 { 338 fd.skipSyncNotif = true 339 } 340 } 341 // Disable SIO_UDP_CONNRESET behavior. 342 // http://support.microsoft.com/kb/263823 343 switch net { 344 case "udp", "udp4", "udp6": 345 ret := uint32(0) 346 flag := uint32(0) 347 size := uint32(unsafe.Sizeof(flag)) 348 err := syscall.WSAIoctl(fd.Sysfd, syscall.SIO_UDP_CONNRESET, (*byte)(unsafe.Pointer(&flag)), size, nil, 0, &ret, nil, 0) 349 if err != nil { 350 return "wsaioctl", err 351 } 352 } 353 fd.rop.mode = 'r' 354 fd.wop.mode = 'w' 355 fd.rop.fd = fd 356 fd.wop.fd = fd 357 fd.rop.runtimeCtx = fd.pd.runtimeCtx 358 fd.wop.runtimeCtx = fd.pd.runtimeCtx 359 return "", nil 360 } 361 362 func (fd *FD) destroy() error { 363 if fd.Sysfd == syscall.InvalidHandle { 364 return syscall.EINVAL 365 } 366 // Poller may want to unregister fd in readiness notification mechanism, 367 // so this must be executed before fd.CloseFunc. 368 fd.pd.close() 369 var err error 370 switch fd.kind { 371 case kindNet: 372 // The net package uses the CloseFunc variable for testing. 373 err = CloseFunc(fd.Sysfd) 374 case kindDir: 375 err = syscall.FindClose(fd.Sysfd) 376 default: 377 err = syscall.CloseHandle(fd.Sysfd) 378 } 379 fd.Sysfd = syscall.InvalidHandle 380 runtime_Semrelease(&fd.csema) 381 return err 382 } 383 384 // Close closes the FD. The underlying file descriptor is closed by 385 // the destroy method when there are no remaining references. 386 func (fd *FD) Close() error { 387 if !fd.fdmu.increfAndClose() { 388 return errClosing(fd.isFile) 389 } 390 if fd.kind == kindPipe { 391 syscall.CancelIoEx(fd.Sysfd, nil) 392 } 393 // unblock pending reader and writer 394 fd.pd.evict() 395 err := fd.decref() 396 // Wait until the descriptor is closed. If this was the only 397 // reference, it is already closed. 398 runtime_Semacquire(&fd.csema) 399 return err 400 } 401 402 // Windows ReadFile and WSARecv use DWORD (uint32) parameter to pass buffer length. 403 // This prevents us reading blocks larger than 4GB. 404 // See golang.org/issue/26923. 405 const maxRW = 1 << 30 // 1GB is large enough and keeps subsequent reads aligned 406 407 // Read implements io.Reader. 408 func (fd *FD) Read(buf []byte) (int, error) { 409 if err := fd.readLock(); err != nil { 410 return 0, err 411 } 412 defer fd.readUnlock() 413 414 if len(buf) > maxRW { 415 buf = buf[:maxRW] 416 } 417 418 var n int 419 var err error 420 if fd.isFile { 421 fd.l.Lock() 422 defer fd.l.Unlock() 423 switch fd.kind { 424 case kindConsole: 425 n, err = fd.readConsole(buf) 426 default: 427 n, err = syscall.Read(fd.Sysfd, buf) 428 if fd.kind == kindPipe && err == syscall.ERROR_OPERATION_ABORTED { 429 // Close uses CancelIoEx to interrupt concurrent I/O for pipes. 430 // If the fd is a pipe and the Read was interrupted by CancelIoEx, 431 // we assume it is interrupted by Close. 432 err = ErrFileClosing 433 } 434 } 435 if err != nil { 436 n = 0 437 } 438 } else { 439 o := &fd.rop 440 o.InitBuf(buf) 441 n, err = execIO(o, func(o *operation) error { 442 return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) 443 }) 444 if race.Enabled { 445 race.Acquire(unsafe.Pointer(&ioSync)) 446 } 447 } 448 if len(buf) != 0 { 449 err = fd.eofError(n, err) 450 } 451 return n, err 452 } 453 454 var ReadConsole = syscall.ReadConsole // changed for testing 455 456 // readConsole reads utf16 characters from console File, 457 // encodes them into utf8 and stores them in buffer b. 458 // It returns the number of utf8 bytes read and an error, if any. 459 func (fd *FD) readConsole(b []byte) (int, error) { 460 if len(b) == 0 { 461 return 0, nil 462 } 463 464 if fd.readuint16 == nil { 465 // Note: syscall.ReadConsole fails for very large buffers. 466 // The limit is somewhere around (but not exactly) 16384. 467 // Stay well below. 468 fd.readuint16 = make([]uint16, 0, 10000) 469 fd.readbyte = make([]byte, 0, 4*cap(fd.readuint16)) 470 } 471 472 for fd.readbyteOffset >= len(fd.readbyte) { 473 n := cap(fd.readuint16) - len(fd.readuint16) 474 if n > len(b) { 475 n = len(b) 476 } 477 var nw uint32 478 err := ReadConsole(fd.Sysfd, &fd.readuint16[:len(fd.readuint16)+1][len(fd.readuint16)], uint32(n), &nw, nil) 479 if err != nil { 480 return 0, err 481 } 482 uint16s := fd.readuint16[:len(fd.readuint16)+int(nw)] 483 fd.readuint16 = fd.readuint16[:0] 484 buf := fd.readbyte[:0] 485 for i := 0; i < len(uint16s); i++ { 486 r := rune(uint16s[i]) 487 if utf16.IsSurrogate(r) { 488 if i+1 == len(uint16s) { 489 if nw > 0 { 490 // Save half surrogate pair for next time. 491 fd.readuint16 = fd.readuint16[:1] 492 fd.readuint16[0] = uint16(r) 493 break 494 } 495 r = utf8.RuneError 496 } else { 497 r = utf16.DecodeRune(r, rune(uint16s[i+1])) 498 if r != utf8.RuneError { 499 i++ 500 } 501 } 502 } 503 buf = utf8.AppendRune(buf, r) 504 } 505 fd.readbyte = buf 506 fd.readbyteOffset = 0 507 if nw == 0 { 508 break 509 } 510 } 511 512 src := fd.readbyte[fd.readbyteOffset:] 513 var i int 514 for i = 0; i < len(src) && i < len(b); i++ { 515 x := src[i] 516 if x == 0x1A { // Ctrl-Z 517 if i == 0 { 518 fd.readbyteOffset++ 519 } 520 break 521 } 522 b[i] = x 523 } 524 fd.readbyteOffset += i 525 return i, nil 526 } 527 528 // Pread emulates the Unix pread system call. 529 func (fd *FD) Pread(b []byte, off int64) (int, error) { 530 // Call incref, not readLock, because since pread specifies the 531 // offset it is independent from other reads. 532 if err := fd.incref(); err != nil { 533 return 0, err 534 } 535 defer fd.decref() 536 537 if len(b) > maxRW { 538 b = b[:maxRW] 539 } 540 541 fd.l.Lock() 542 defer fd.l.Unlock() 543 curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent) 544 if e != nil { 545 return 0, e 546 } 547 defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart) 548 o := syscall.Overlapped{ 549 OffsetHigh: uint32(off >> 32), 550 Offset: uint32(off), 551 } 552 var done uint32 553 e = syscall.ReadFile(fd.Sysfd, b, &done, &o) 554 if e != nil { 555 done = 0 556 if e == syscall.ERROR_HANDLE_EOF { 557 e = io.EOF 558 } 559 } 560 if len(b) != 0 { 561 e = fd.eofError(int(done), e) 562 } 563 return int(done), e 564 } 565 566 // ReadFrom wraps the recvfrom network call. 567 func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) { 568 if len(buf) == 0 { 569 return 0, nil, nil 570 } 571 if len(buf) > maxRW { 572 buf = buf[:maxRW] 573 } 574 if err := fd.readLock(); err != nil { 575 return 0, nil, err 576 } 577 defer fd.readUnlock() 578 o := &fd.rop 579 o.InitBuf(buf) 580 n, err := execIO(o, func(o *operation) error { 581 if o.rsa == nil { 582 o.rsa = new(syscall.RawSockaddrAny) 583 } 584 o.rsan = int32(unsafe.Sizeof(*o.rsa)) 585 return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) 586 }) 587 err = fd.eofError(n, err) 588 if err != nil { 589 return n, nil, err 590 } 591 sa, _ := o.rsa.Sockaddr() 592 return n, sa, nil 593 } 594 595 // ReadFromInet4 wraps the recvfrom network call for IPv4. 596 func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) { 597 if len(buf) == 0 { 598 return 0, nil 599 } 600 if len(buf) > maxRW { 601 buf = buf[:maxRW] 602 } 603 if err := fd.readLock(); err != nil { 604 return 0, err 605 } 606 defer fd.readUnlock() 607 o := &fd.rop 608 o.InitBuf(buf) 609 n, err := execIO(o, func(o *operation) error { 610 if o.rsa == nil { 611 o.rsa = new(syscall.RawSockaddrAny) 612 } 613 o.rsan = int32(unsafe.Sizeof(*o.rsa)) 614 return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) 615 }) 616 err = fd.eofError(n, err) 617 if err != nil { 618 return n, err 619 } 620 rawToSockaddrInet4(o.rsa, sa4) 621 return n, err 622 } 623 624 // ReadFromInet6 wraps the recvfrom network call for IPv6. 625 func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) { 626 if len(buf) == 0 { 627 return 0, nil 628 } 629 if len(buf) > maxRW { 630 buf = buf[:maxRW] 631 } 632 if err := fd.readLock(); err != nil { 633 return 0, err 634 } 635 defer fd.readUnlock() 636 o := &fd.rop 637 o.InitBuf(buf) 638 n, err := execIO(o, func(o *operation) error { 639 if o.rsa == nil { 640 o.rsa = new(syscall.RawSockaddrAny) 641 } 642 o.rsan = int32(unsafe.Sizeof(*o.rsa)) 643 return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) 644 }) 645 err = fd.eofError(n, err) 646 if err != nil { 647 return n, err 648 } 649 rawToSockaddrInet6(o.rsa, sa6) 650 return n, err 651 } 652 653 // Write implements io.Writer. 654 func (fd *FD) Write(buf []byte) (int, error) { 655 if err := fd.writeLock(); err != nil { 656 return 0, err 657 } 658 defer fd.writeUnlock() 659 if fd.isFile { 660 fd.l.Lock() 661 defer fd.l.Unlock() 662 } 663 664 ntotal := 0 665 for len(buf) > 0 { 666 b := buf 667 if len(b) > maxRW { 668 b = b[:maxRW] 669 } 670 var n int 671 var err error 672 if fd.isFile { 673 switch fd.kind { 674 case kindConsole: 675 n, err = fd.writeConsole(b) 676 default: 677 n, err = syscall.Write(fd.Sysfd, b) 678 if fd.kind == kindPipe && err == syscall.ERROR_OPERATION_ABORTED { 679 // Close uses CancelIoEx to interrupt concurrent I/O for pipes. 680 // If the fd is a pipe and the Write was interrupted by CancelIoEx, 681 // we assume it is interrupted by Close. 682 err = ErrFileClosing 683 } 684 } 685 if err != nil { 686 n = 0 687 } 688 } else { 689 if race.Enabled { 690 race.ReleaseMerge(unsafe.Pointer(&ioSync)) 691 } 692 o := &fd.wop 693 o.InitBuf(b) 694 n, err = execIO(o, func(o *operation) error { 695 return syscall.WSASend(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil) 696 }) 697 } 698 ntotal += n 699 if err != nil { 700 return ntotal, err 701 } 702 buf = buf[n:] 703 } 704 return ntotal, nil 705 } 706 707 // writeConsole writes len(b) bytes to the console File. 708 // It returns the number of bytes written and an error, if any. 709 func (fd *FD) writeConsole(b []byte) (int, error) { 710 n := len(b) 711 runes := make([]rune, 0, 256) 712 if len(fd.lastbits) > 0 { 713 b = append(fd.lastbits, b...) 714 fd.lastbits = nil 715 716 } 717 for len(b) >= utf8.UTFMax || utf8.FullRune(b) { 718 r, l := utf8.DecodeRune(b) 719 runes = append(runes, r) 720 b = b[l:] 721 } 722 if len(b) > 0 { 723 fd.lastbits = make([]byte, len(b)) 724 copy(fd.lastbits, b) 725 } 726 // syscall.WriteConsole seems to fail, if given large buffer. 727 // So limit the buffer to 16000 characters. This number was 728 // discovered by experimenting with syscall.WriteConsole. 729 const maxWrite = 16000 730 for len(runes) > 0 { 731 m := len(runes) 732 if m > maxWrite { 733 m = maxWrite 734 } 735 chunk := runes[:m] 736 runes = runes[m:] 737 uint16s := utf16.Encode(chunk) 738 for len(uint16s) > 0 { 739 var written uint32 740 err := syscall.WriteConsole(fd.Sysfd, &uint16s[0], uint32(len(uint16s)), &written, nil) 741 if err != nil { 742 return 0, err 743 } 744 uint16s = uint16s[written:] 745 } 746 } 747 return n, nil 748 } 749 750 // Pwrite emulates the Unix pwrite system call. 751 func (fd *FD) Pwrite(buf []byte, off int64) (int, error) { 752 // Call incref, not writeLock, because since pwrite specifies the 753 // offset it is independent from other writes. 754 if err := fd.incref(); err != nil { 755 return 0, err 756 } 757 defer fd.decref() 758 759 fd.l.Lock() 760 defer fd.l.Unlock() 761 curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent) 762 if e != nil { 763 return 0, e 764 } 765 defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart) 766 767 ntotal := 0 768 for len(buf) > 0 { 769 b := buf 770 if len(b) > maxRW { 771 b = b[:maxRW] 772 } 773 var n uint32 774 o := syscall.Overlapped{ 775 OffsetHigh: uint32(off >> 32), 776 Offset: uint32(off), 777 } 778 e = syscall.WriteFile(fd.Sysfd, b, &n, &o) 779 ntotal += int(n) 780 if e != nil { 781 return ntotal, e 782 } 783 buf = buf[n:] 784 off += int64(n) 785 } 786 return ntotal, nil 787 } 788 789 // Writev emulates the Unix writev system call. 790 func (fd *FD) Writev(buf *[][]byte) (int64, error) { 791 if len(*buf) == 0 { 792 return 0, nil 793 } 794 if err := fd.writeLock(); err != nil { 795 return 0, err 796 } 797 defer fd.writeUnlock() 798 if race.Enabled { 799 race.ReleaseMerge(unsafe.Pointer(&ioSync)) 800 } 801 o := &fd.wop 802 o.InitBufs(buf) 803 n, err := execIO(o, func(o *operation) error { 804 return syscall.WSASend(o.fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil) 805 }) 806 o.ClearBufs() 807 TestHookDidWritev(n) 808 consume(buf, int64(n)) 809 return int64(n), err 810 } 811 812 // WriteTo wraps the sendto network call. 813 func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) { 814 if err := fd.writeLock(); err != nil { 815 return 0, err 816 } 817 defer fd.writeUnlock() 818 819 if len(buf) == 0 { 820 // handle zero-byte payload 821 o := &fd.wop 822 o.InitBuf(buf) 823 o.sa = sa 824 n, err := execIO(o, func(o *operation) error { 825 return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) 826 }) 827 return n, err 828 } 829 830 ntotal := 0 831 for len(buf) > 0 { 832 b := buf 833 if len(b) > maxRW { 834 b = b[:maxRW] 835 } 836 o := &fd.wop 837 o.InitBuf(b) 838 o.sa = sa 839 n, err := execIO(o, func(o *operation) error { 840 return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) 841 }) 842 ntotal += int(n) 843 if err != nil { 844 return ntotal, err 845 } 846 buf = buf[n:] 847 } 848 return ntotal, nil 849 } 850 851 // WriteToInet4 is WriteTo, specialized for syscall.SockaddrInet4. 852 func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) { 853 if err := fd.writeLock(); err != nil { 854 return 0, err 855 } 856 defer fd.writeUnlock() 857 858 if len(buf) == 0 { 859 // handle zero-byte payload 860 o := &fd.wop 861 o.InitBuf(buf) 862 n, err := execIO(o, func(o *operation) error { 863 return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) 864 }) 865 return n, err 866 } 867 868 ntotal := 0 869 for len(buf) > 0 { 870 b := buf 871 if len(b) > maxRW { 872 b = b[:maxRW] 873 } 874 o := &fd.wop 875 o.InitBuf(b) 876 n, err := execIO(o, func(o *operation) error { 877 return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) 878 }) 879 ntotal += int(n) 880 if err != nil { 881 return ntotal, err 882 } 883 buf = buf[n:] 884 } 885 return ntotal, nil 886 } 887 888 // WriteToInet6 is WriteTo, specialized for syscall.SockaddrInet6. 889 func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) { 890 if err := fd.writeLock(); err != nil { 891 return 0, err 892 } 893 defer fd.writeUnlock() 894 895 if len(buf) == 0 { 896 // handle zero-byte payload 897 o := &fd.wop 898 o.InitBuf(buf) 899 n, err := execIO(o, func(o *operation) error { 900 return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) 901 }) 902 return n, err 903 } 904 905 ntotal := 0 906 for len(buf) > 0 { 907 b := buf 908 if len(b) > maxRW { 909 b = b[:maxRW] 910 } 911 o := &fd.wop 912 o.InitBuf(b) 913 n, err := execIO(o, func(o *operation) error { 914 return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) 915 }) 916 ntotal += int(n) 917 if err != nil { 918 return ntotal, err 919 } 920 buf = buf[n:] 921 } 922 return ntotal, nil 923 } 924 925 // Call ConnectEx. This doesn't need any locking, since it is only 926 // called when the descriptor is first created. This is here rather 927 // than in the net package so that it can use fd.wop. 928 func (fd *FD) ConnectEx(ra syscall.Sockaddr) error { 929 o := &fd.wop 930 o.sa = ra 931 _, err := execIO(o, func(o *operation) error { 932 return ConnectExFunc(o.fd.Sysfd, o.sa, nil, 0, nil, &o.o) 933 }) 934 return err 935 } 936 937 func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *operation) (string, error) { 938 // Submit accept request. 939 o.handle = s 940 o.rsan = int32(unsafe.Sizeof(rawsa[0])) 941 _, err := execIO(o, func(o *operation) error { 942 return AcceptFunc(o.fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o) 943 }) 944 if err != nil { 945 CloseFunc(s) 946 return "acceptex", err 947 } 948 949 // Inherit properties of the listening socket. 950 err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.Sysfd)), int32(unsafe.Sizeof(fd.Sysfd))) 951 if err != nil { 952 CloseFunc(s) 953 return "setsockopt", err 954 } 955 956 return "", nil 957 } 958 959 // Accept handles accepting a socket. The sysSocket parameter is used 960 // to allocate the net socket. 961 func (fd *FD) Accept(sysSocket func() (syscall.Handle, error)) (syscall.Handle, []syscall.RawSockaddrAny, uint32, string, error) { 962 if err := fd.readLock(); err != nil { 963 return syscall.InvalidHandle, nil, 0, "", err 964 } 965 defer fd.readUnlock() 966 967 o := &fd.rop 968 var rawsa [2]syscall.RawSockaddrAny 969 for { 970 s, err := sysSocket() 971 if err != nil { 972 return syscall.InvalidHandle, nil, 0, "", err 973 } 974 975 errcall, err := fd.acceptOne(s, rawsa[:], o) 976 if err == nil { 977 return s, rawsa[:], uint32(o.rsan), "", nil 978 } 979 980 // Sometimes we see WSAECONNRESET and ERROR_NETNAME_DELETED is 981 // returned here. These happen if connection reset is received 982 // before AcceptEx could complete. These errors relate to new 983 // connection, not to AcceptEx, so ignore broken connection and 984 // try AcceptEx again for more connections. 985 errno, ok := err.(syscall.Errno) 986 if !ok { 987 return syscall.InvalidHandle, nil, 0, errcall, err 988 } 989 switch errno { 990 case syscall.ERROR_NETNAME_DELETED, syscall.WSAECONNRESET: 991 // ignore these and try again 992 default: 993 return syscall.InvalidHandle, nil, 0, errcall, err 994 } 995 } 996 } 997 998 // Seek wraps syscall.Seek. 999 func (fd *FD) Seek(offset int64, whence int) (int64, error) { 1000 if err := fd.incref(); err != nil { 1001 return 0, err 1002 } 1003 defer fd.decref() 1004 1005 fd.l.Lock() 1006 defer fd.l.Unlock() 1007 1008 return syscall.Seek(fd.Sysfd, offset, whence) 1009 } 1010 1011 // FindNextFile wraps syscall.FindNextFile. 1012 func (fd *FD) FindNextFile(data *syscall.Win32finddata) error { 1013 if err := fd.incref(); err != nil { 1014 return err 1015 } 1016 defer fd.decref() 1017 return syscall.FindNextFile(fd.Sysfd, data) 1018 } 1019 1020 // Fchmod updates syscall.ByHandleFileInformation.Fileattributes when needed. 1021 func (fd *FD) Fchmod(mode uint32) error { 1022 if err := fd.incref(); err != nil { 1023 return err 1024 } 1025 defer fd.decref() 1026 1027 var d syscall.ByHandleFileInformation 1028 if err := syscall.GetFileInformationByHandle(fd.Sysfd, &d); err != nil { 1029 return err 1030 } 1031 attrs := d.FileAttributes 1032 if mode&syscall.S_IWRITE != 0 { 1033 attrs &^= syscall.FILE_ATTRIBUTE_READONLY 1034 } else { 1035 attrs |= syscall.FILE_ATTRIBUTE_READONLY 1036 } 1037 if attrs == d.FileAttributes { 1038 return nil 1039 } 1040 1041 var du windows.FILE_BASIC_INFO 1042 du.FileAttributes = attrs 1043 l := uint32(unsafe.Sizeof(d)) 1044 return windows.SetFileInformationByHandle(fd.Sysfd, windows.FileBasicInfo, uintptr(unsafe.Pointer(&du)), l) 1045 } 1046 1047 // Fchdir wraps syscall.Fchdir. 1048 func (fd *FD) Fchdir() error { 1049 if err := fd.incref(); err != nil { 1050 return err 1051 } 1052 defer fd.decref() 1053 return syscall.Fchdir(fd.Sysfd) 1054 } 1055 1056 // GetFileType wraps syscall.GetFileType. 1057 func (fd *FD) GetFileType() (uint32, error) { 1058 if err := fd.incref(); err != nil { 1059 return 0, err 1060 } 1061 defer fd.decref() 1062 return syscall.GetFileType(fd.Sysfd) 1063 } 1064 1065 // GetFileInformationByHandle wraps GetFileInformationByHandle. 1066 func (fd *FD) GetFileInformationByHandle(data *syscall.ByHandleFileInformation) error { 1067 if err := fd.incref(); err != nil { 1068 return err 1069 } 1070 defer fd.decref() 1071 return syscall.GetFileInformationByHandle(fd.Sysfd, data) 1072 } 1073 1074 // RawRead invokes the user-defined function f for a read operation. 1075 func (fd *FD) RawRead(f func(uintptr) bool) error { 1076 if err := fd.readLock(); err != nil { 1077 return err 1078 } 1079 defer fd.readUnlock() 1080 for { 1081 if f(uintptr(fd.Sysfd)) { 1082 return nil 1083 } 1084 1085 // Use a zero-byte read as a way to get notified when this 1086 // socket is readable. h/t https://stackoverflow.com/a/42019668/332798 1087 o := &fd.rop 1088 o.InitBuf(nil) 1089 if !fd.IsStream { 1090 o.flags |= windows.MSG_PEEK 1091 } 1092 _, err := execIO(o, func(o *operation) error { 1093 return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) 1094 }) 1095 if err == windows.WSAEMSGSIZE { 1096 // expected with a 0-byte peek, ignore. 1097 } else if err != nil { 1098 return err 1099 } 1100 } 1101 } 1102 1103 // RawWrite invokes the user-defined function f for a write operation. 1104 func (fd *FD) RawWrite(f func(uintptr) bool) error { 1105 if err := fd.writeLock(); err != nil { 1106 return err 1107 } 1108 defer fd.writeUnlock() 1109 1110 if f(uintptr(fd.Sysfd)) { 1111 return nil 1112 } 1113 1114 // TODO(tmm1): find a way to detect socket writability 1115 return syscall.EWINDOWS 1116 } 1117 1118 func sockaddrInet4ToRaw(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet4) int32 { 1119 *rsa = syscall.RawSockaddrAny{} 1120 raw := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa)) 1121 raw.Family = syscall.AF_INET 1122 p := (*[2]byte)(unsafe.Pointer(&raw.Port)) 1123 p[0] = byte(sa.Port >> 8) 1124 p[1] = byte(sa.Port) 1125 raw.Addr = sa.Addr 1126 return int32(unsafe.Sizeof(*raw)) 1127 } 1128 1129 func sockaddrInet6ToRaw(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet6) int32 { 1130 *rsa = syscall.RawSockaddrAny{} 1131 raw := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa)) 1132 raw.Family = syscall.AF_INET6 1133 p := (*[2]byte)(unsafe.Pointer(&raw.Port)) 1134 p[0] = byte(sa.Port >> 8) 1135 p[1] = byte(sa.Port) 1136 raw.Scope_id = sa.ZoneId 1137 raw.Addr = sa.Addr 1138 return int32(unsafe.Sizeof(*raw)) 1139 } 1140 1141 func rawToSockaddrInet4(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet4) { 1142 pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa)) 1143 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 1144 sa.Port = int(p[0])<<8 + int(p[1]) 1145 sa.Addr = pp.Addr 1146 } 1147 1148 func rawToSockaddrInet6(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet6) { 1149 pp := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa)) 1150 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) 1151 sa.Port = int(p[0])<<8 + int(p[1]) 1152 sa.ZoneId = pp.Scope_id 1153 sa.Addr = pp.Addr 1154 } 1155 1156 func sockaddrToRaw(rsa *syscall.RawSockaddrAny, sa syscall.Sockaddr) (int32, error) { 1157 switch sa := sa.(type) { 1158 case *syscall.SockaddrInet4: 1159 sz := sockaddrInet4ToRaw(rsa, sa) 1160 return sz, nil 1161 case *syscall.SockaddrInet6: 1162 sz := sockaddrInet6ToRaw(rsa, sa) 1163 return sz, nil 1164 default: 1165 return 0, syscall.EWINDOWS 1166 } 1167 } 1168 1169 // ReadMsg wraps the WSARecvMsg network call. 1170 func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.Sockaddr, error) { 1171 if err := fd.readLock(); err != nil { 1172 return 0, 0, 0, nil, err 1173 } 1174 defer fd.readUnlock() 1175 1176 if len(p) > maxRW { 1177 p = p[:maxRW] 1178 } 1179 1180 o := &fd.rop 1181 o.InitMsg(p, oob) 1182 if o.rsa == nil { 1183 o.rsa = new(syscall.RawSockaddrAny) 1184 } 1185 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1186 o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) 1187 o.msg.Flags = uint32(flags) 1188 n, err := execIO(o, func(o *operation) error { 1189 return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) 1190 }) 1191 err = fd.eofError(n, err) 1192 var sa syscall.Sockaddr 1193 if err == nil { 1194 sa, err = o.rsa.Sockaddr() 1195 } 1196 return n, int(o.msg.Control.Len), int(o.msg.Flags), sa, err 1197 } 1198 1199 // ReadMsgInet4 is ReadMsg, but specialized to return a syscall.SockaddrInet4. 1200 func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.SockaddrInet4) (int, int, int, error) { 1201 if err := fd.readLock(); err != nil { 1202 return 0, 0, 0, err 1203 } 1204 defer fd.readUnlock() 1205 1206 if len(p) > maxRW { 1207 p = p[:maxRW] 1208 } 1209 1210 o := &fd.rop 1211 o.InitMsg(p, oob) 1212 if o.rsa == nil { 1213 o.rsa = new(syscall.RawSockaddrAny) 1214 } 1215 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1216 o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) 1217 o.msg.Flags = uint32(flags) 1218 n, err := execIO(o, func(o *operation) error { 1219 return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) 1220 }) 1221 err = fd.eofError(n, err) 1222 if err == nil { 1223 rawToSockaddrInet4(o.rsa, sa4) 1224 } 1225 return n, int(o.msg.Control.Len), int(o.msg.Flags), err 1226 } 1227 1228 // ReadMsgInet6 is ReadMsg, but specialized to return a syscall.SockaddrInet6. 1229 func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.SockaddrInet6) (int, int, int, error) { 1230 if err := fd.readLock(); err != nil { 1231 return 0, 0, 0, err 1232 } 1233 defer fd.readUnlock() 1234 1235 if len(p) > maxRW { 1236 p = p[:maxRW] 1237 } 1238 1239 o := &fd.rop 1240 o.InitMsg(p, oob) 1241 if o.rsa == nil { 1242 o.rsa = new(syscall.RawSockaddrAny) 1243 } 1244 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1245 o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) 1246 o.msg.Flags = uint32(flags) 1247 n, err := execIO(o, func(o *operation) error { 1248 return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) 1249 }) 1250 err = fd.eofError(n, err) 1251 if err == nil { 1252 rawToSockaddrInet6(o.rsa, sa6) 1253 } 1254 return n, int(o.msg.Control.Len), int(o.msg.Flags), err 1255 } 1256 1257 // WriteMsg wraps the WSASendMsg network call. 1258 func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, error) { 1259 if len(p) > maxRW { 1260 return 0, 0, errors.New("packet is too large (only 1GB is allowed)") 1261 } 1262 1263 if err := fd.writeLock(); err != nil { 1264 return 0, 0, err 1265 } 1266 defer fd.writeUnlock() 1267 1268 o := &fd.wop 1269 o.InitMsg(p, oob) 1270 if sa != nil { 1271 if o.rsa == nil { 1272 o.rsa = new(syscall.RawSockaddrAny) 1273 } 1274 len, err := sockaddrToRaw(o.rsa, sa) 1275 if err != nil { 1276 return 0, 0, err 1277 } 1278 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1279 o.msg.Namelen = len 1280 } 1281 n, err := execIO(o, func(o *operation) error { 1282 return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) 1283 }) 1284 return n, int(o.msg.Control.Len), err 1285 } 1286 1287 // WriteMsgInet4 is WriteMsg specialized for syscall.SockaddrInet4. 1288 func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (int, int, error) { 1289 if len(p) > maxRW { 1290 return 0, 0, errors.New("packet is too large (only 1GB is allowed)") 1291 } 1292 1293 if err := fd.writeLock(); err != nil { 1294 return 0, 0, err 1295 } 1296 defer fd.writeUnlock() 1297 1298 o := &fd.wop 1299 o.InitMsg(p, oob) 1300 if o.rsa == nil { 1301 o.rsa = new(syscall.RawSockaddrAny) 1302 } 1303 len := sockaddrInet4ToRaw(o.rsa, sa) 1304 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1305 o.msg.Namelen = len 1306 n, err := execIO(o, func(o *operation) error { 1307 return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) 1308 }) 1309 return n, int(o.msg.Control.Len), err 1310 } 1311 1312 // WriteMsgInet6 is WriteMsg specialized for syscall.SockaddrInet6. 1313 func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (int, int, error) { 1314 if len(p) > maxRW { 1315 return 0, 0, errors.New("packet is too large (only 1GB is allowed)") 1316 } 1317 1318 if err := fd.writeLock(); err != nil { 1319 return 0, 0, err 1320 } 1321 defer fd.writeUnlock() 1322 1323 o := &fd.wop 1324 o.InitMsg(p, oob) 1325 if o.rsa == nil { 1326 o.rsa = new(syscall.RawSockaddrAny) 1327 } 1328 len := sockaddrInet6ToRaw(o.rsa, sa) 1329 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) 1330 o.msg.Namelen = len 1331 n, err := execIO(o, func(o *operation) error { 1332 return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) 1333 }) 1334 return n, int(o.msg.Control.Len), err 1335 }