Skip to content

Commit

Permalink
runtime: Update open/close/read/write to return -1 on error.
Browse files Browse the repository at this point in the history
Error detection code copied from syscall, where presumably
we actually do it right.

Note that we throw the errno away.  The runtime doesn't use it.

Fixes golang#10052

Change-Id: I8de77dda6bf287276b137646c26b84fa61554ec8
Reviewed-on: https://go-review.googlesource.com/6571
Run-TryBot: Keith Randall <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
  • Loading branch information
randall77 committed Mar 3, 2015
1 parent 74e88df commit f584c05
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/runtime/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ var Gostringnocopy = gostringnocopy
var Maxstring = &maxstring

type Uintreg uintreg

var Open = open
var Close = close
var Read = read
var Write = write
5 changes: 5 additions & 0 deletions src/runtime/os_nacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ func sigpanic() {

func raiseproc(sig int32) {
}

// Stubs so tests can link correctly. These should never be called.
func open(name *byte, mode, perm int32) int32
func close(fd int32) int32
func read(fd int32, p unsafe.Pointer, n int32) int32
16 changes: 15 additions & 1 deletion src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package runtime

import _ "unsafe" // for go:linkname
import "unsafe"

type stdFunction *byte

Expand Down Expand Up @@ -39,3 +39,17 @@ func sigpanic() {
}
throw("fault")
}

// Stubs so tests can link correctly. These should never be called.
func open(name *byte, mode, perm int32) int32 {
throw("unimplemented")
return -1
}
func close(fd int32) int32 {
throw("unimplemented")
return -1
}
func read(fd int32, p unsafe.Pointer, n int32) int32 {
throw("unimplemented")
return -1
}
26 changes: 26 additions & 0 deletions src/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,29 @@ func TestTrailingZero(t *testing.T) {
t.Errorf("sizeof(%#v)==%d, want 0", T5{}, unsafe.Sizeof(T5{}))
}
}

func TestBadOpen(t *testing.T) {
if GOOS == "windows" || GOOS == "nacl" {
t.Skip("skipping OS that doesn't have open/read/write/close")
}
// make sure we get the correct error code if open fails. Same for
// read/write/close on the resulting -1 fd. See issue 10052.
nonfile := []byte("/notreallyafile")
fd := Open(&nonfile[0], 0, 0)
if fd != -1 {
t.Errorf("open(\"%s\")=%d, want -1", string(nonfile), fd)
}
var buf [32]byte
r := Read(-1, unsafe.Pointer(&buf[0]), int32(len(buf)))
if r != -1 {
t.Errorf("read()=%d, want -1", r)
}
w := Write(^uintptr(0), unsafe.Pointer(&buf[0]), int32(len(buf)))
if w != -1 {
t.Errorf("write()=%d, want -1", w)
}
c := Close(-1)
if c != -1 {
t.Errorf("close()=%d, want -1", c)
}
}
8 changes: 8 additions & 0 deletions src/runtime/sys_darwin_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,32 @@ TEXT runtime·exit1(SB),NOSPLIT,$0
TEXT runtime·open(SB),NOSPLIT,$0
MOVL $5, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$0
MOVL $6, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+4(FP)
RET

TEXT runtime·read(SB),NOSPLIT,$0
MOVL $3, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·write(SB),NOSPLIT,$0
MOVL $4, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/sys_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ TEXT runtime·open(SB),NOSPLIT,$0
MOVL perm+12(FP), DX // arg 3 mode
MOVL $(0x2000000+5), AX // syscall entry
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+16(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$0
MOVL fd+0(FP), DI // arg 1 fd
MOVL $(0x2000000+6), AX // syscall entry
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+8(FP)
RET

Expand All @@ -54,6 +58,8 @@ TEXT runtime·read(SB),NOSPLIT,$0
MOVL n+16(FP), DX // arg 3 count
MOVL $(0x2000000+3), AX // syscall entry
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand All @@ -63,6 +69,8 @@ TEXT runtime·write(SB),NOSPLIT,$0
MOVL n+16(FP), DX // arg 3 count
MOVL $(0x2000000+4), AX // syscall entry
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/sys_darwin_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ TEXT runtime·open(SB),NOSPLIT,$0
MOVW perm+8(FP), R2
MOVW $SYS_open, R12
SWI $0x80
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$0
MOVW fd+0(FP), R0
MOVW $SYS_close, R12
SWI $0x80
MOVW.CS $-1, R0
MOVW R0, ret+4(FP)
RET

Expand All @@ -64,6 +66,7 @@ TEXT runtime·write(SB),NOSPLIT,$0
MOVW n+8(FP), R2
MOVW $SYS_write, R12
SWI $0x80
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

Expand All @@ -73,6 +76,7 @@ TEXT runtime·read(SB),NOSPLIT,$0
MOVW n+8(FP), R2
MOVW $SYS_read, R12
SWI $0x80
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/sys_dragonfly_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,32 @@ TEXT runtime·exit1(SB),NOSPLIT,$16
TEXT runtime·open(SB),NOSPLIT,$-4
MOVL $5, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$-4
MOVL $6, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+4(FP)
RET

TEXT runtime·read(SB),NOSPLIT,$-4
MOVL $3, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·write(SB),NOSPLIT,$-4
MOVL $4, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/sys_dragonfly_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ TEXT runtime·open(SB),NOSPLIT,$-8
MOVL perm+12(FP), DX // arg 3 mode
MOVL $5, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+16(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$-8
MOVL fd+0(FP), DI // arg 1 fd
MOVL $6, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+8(FP)
RET

Expand All @@ -93,6 +97,8 @@ TEXT runtime·read(SB),NOSPLIT,$-8
MOVL n+16(FP), DX // arg 3 count
MOVL $3, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand All @@ -102,6 +108,8 @@ TEXT runtime·write(SB),NOSPLIT,$-8
MOVL n+16(FP), DX // arg 3 count
MOVL $4, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/sys_freebsd_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,32 @@ TEXT runtime·exit1(SB),NOSPLIT,$-4
TEXT runtime·open(SB),NOSPLIT,$-4
MOVL $5, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$-4
MOVL $6, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+4(FP)
RET

TEXT runtime·read(SB),NOSPLIT,$-4
MOVL $3, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·write(SB),NOSPLIT,$-4
MOVL $4, AX
INT $0x80
JAE 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/sys_freebsd_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ TEXT runtime·open(SB),NOSPLIT,$-8
MOVL perm+12(FP), DX // arg 3 mode
MOVL $5, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+16(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$-8
MOVL fd+0(FP), DI // arg 1 fd
MOVL $6, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+8(FP)
RET

Expand All @@ -83,6 +87,8 @@ TEXT runtime·read(SB),NOSPLIT,$-8
MOVL n+16(FP), DX // arg 3 count
MOVL $3, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand All @@ -92,6 +98,8 @@ TEXT runtime·write(SB),NOSPLIT,$-8
MOVL n+16(FP), DX // arg 3 count
MOVL $4, AX
SYSCALL
JCC 2(PC)
MOVL $-1, AX
MOVL AX, ret+24(FP)
RET

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/sys_freebsd_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ TEXT runtime·open(SB),NOSPLIT,$-8
MOVW perm+8(FP), R2 // arg 3 perm
MOVW $SYS_open, R7
SWI $0
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

Expand All @@ -102,6 +103,7 @@ TEXT runtime·read(SB),NOSPLIT,$-8
MOVW n+8(FP), R2 // arg 3 count
MOVW $SYS_read, R7
SWI $0
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

Expand All @@ -111,13 +113,15 @@ TEXT runtime·write(SB),NOSPLIT,$-8
MOVW n+8(FP), R2 // arg 3 count
MOVW $SYS_write, R7
SWI $0
MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$-8
MOVW fd+0(FP), R0 // arg 1 fd
MOVW $SYS_close, R7
SWI $0
MOVW.CS $-1, R0
MOVW R0, ret+4(FP)
RET

Expand Down
12 changes: 12 additions & 0 deletions src/runtime/sys_linux_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ TEXT runtime·open(SB),NOSPLIT,$0
MOVL mode+4(FP), CX
MOVL perm+8(FP), DX
CALL *runtime·_vdso(SB)
CMPL AX, $0xfffff001
JLS 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

TEXT runtime·close(SB),NOSPLIT,$0
MOVL $6, AX // syscall - close
MOVL fd+0(FP), BX
CALL *runtime·_vdso(SB)
CMPL AX, $0xfffff001
JLS 2(PC)
MOVL $-1, AX
MOVL AX, ret+4(FP)
RET

Expand All @@ -46,6 +52,9 @@ TEXT runtime·write(SB),NOSPLIT,$0
MOVL p+4(FP), CX
MOVL n+8(FP), DX
CALL *runtime·_vdso(SB)
CMPL AX, $0xfffff001
JLS 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

Expand All @@ -55,6 +64,9 @@ TEXT runtime·read(SB),NOSPLIT,$0
MOVL p+4(FP), CX
MOVL n+8(FP), DX
CALL *runtime·_vdso(SB)
CMPL AX, $0xfffff001
JLS 2(PC)
MOVL $-1, AX
MOVL AX, ret+12(FP)
RET

Expand Down
Loading

0 comments on commit f584c05

Please sign in to comment.