Skip to content

Commit cc0a04d

Browse files
runtime: fix errno sign for some mmap and mincore cases
The caller of mmap expects it to return a positive errno value, but the linux-arm64 and nacl-386 system calls returned a negative errno value. Correct them to negate the errno value. The caller of mincore expects it to return a negative errno value (yes, this is inconsistent), but the linux-mips64x and linux-ppc64x system call returned a positive errno value. Correct them to negate the errno value. Add a test that mmap returns errno with the correct sign. Brad added a test for mincore's errno value in https://golang.org/cl/19457. Fixes #14297. Change-Id: I2b93f32e679bd1eae1c9aef9ae7bcf0ba39521b5 Reviewed-on: https://go-review.googlesource.com/19455 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Minux Ma <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 70418eb commit cc0a04d

10 files changed

+69
-16
lines changed

src/runtime/export_linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
package runtime
88

99
var NewOSProc0 = newosproc0
10+
var Mincore = mincore

src/runtime/export_mmap_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 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+
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
6+
7+
// Export guts for testing.
8+
9+
package runtime
10+
11+
var Mmap = mmap
12+
13+
const ENOMEM = _ENOMEM
14+
const MAP_ANON = _MAP_ANON
15+
const MAP_PRIVATE = _MAP_PRIVATE

src/runtime/export_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ var Exitsyscall = exitsyscall
2828
var LockedOSThread = lockedOSThread
2929
var Xadduintptr = atomic.Xadduintptr
3030

31-
var Mincore = mincore
32-
3331
var FuncPC = funcPC
3432

3533
var Fastlog2 = fastlog2

src/runtime/mem_bsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
5959
return p
6060
}
6161

62-
func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
63-
const _ENOMEM = 12
62+
const _ENOMEM = 12
6463

64+
func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
6565
mSysStatInc(sysStat, n)
6666

6767
// On 64-bit, we don't actually have v reserved, so tread carefully.

src/runtime/os2_nacl.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ const (
1010

1111
// native_client/src/trusted/service_runtime/include/sys/errno.h
1212
// The errors are mainly copied from Linux.
13-
_EPERM = 1 /* Operation not permitted */
14-
_ENOENT = 2 /* No such file or directory */
15-
_ESRCH = 3 /* No such process */
16-
_EINTR = 4 /* Interrupted system call */
17-
_EIO = 5 /* I/O error */
18-
_ENXIO = 6 /* No such device or address */
19-
_E2BIG = 7 /* Argument list too long */
20-
_ENOEXEC = 8 /* Exec format error */
21-
_EBADF = 9 /* Bad file number */
22-
_ECHILD = 10 /* No child processes */
23-
_EAGAIN = 11 /* Try again */
24-
_ENOMEM = 12 /* Out of memory */
13+
_EPERM = 1 /* Operation not permitted */
14+
_ENOENT = 2 /* No such file or directory */
15+
_ESRCH = 3 /* No such process */
16+
_EINTR = 4 /* Interrupted system call */
17+
_EIO = 5 /* I/O error */
18+
_ENXIO = 6 /* No such device or address */
19+
_E2BIG = 7 /* Argument list too long */
20+
_ENOEXEC = 8 /* Exec format error */
21+
_EBADF = 9 /* Bad file number */
22+
_ECHILD = 10 /* No child processes */
23+
_EAGAIN = 11 /* Try again */
24+
// _ENOMEM is defined in mem_bsd.go for nacl.
25+
// _ENOMEM = 12 /* Out of memory */
2526
_EACCES = 13 /* Permission denied */
2627
_EFAULT = 14 /* Bad address */
2728
_EBUSY = 16 /* Device or resource busy */

src/runtime/runtime_mmap_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 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+
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
6+
7+
package runtime_test
8+
9+
import (
10+
"runtime"
11+
"runtime/internal/sys"
12+
"testing"
13+
)
14+
15+
// Test that the error value returned by mmap is positive, as that is
16+
// what the code in mem_bsd.go, mem_darwin.go, and mem_linux.go expects.
17+
// See the uses of ENOMEM in sysMap in those files.
18+
func TestMmapErrorSign(t *testing.T) {
19+
p := runtime.Mmap(nil, ^uintptr(0)&^(sys.PhysPageSize-1), 0, runtime.MAP_ANON|runtime.MAP_PRIVATE, -1, 0)
20+
21+
// The runtime.mmap function is nosplit, but t.Errorf is not.
22+
// Reset the pointer so that we don't get an "invalid stack
23+
// pointer" error from t.Errorf if we call it.
24+
v := uintptr(p)
25+
p = nil
26+
27+
if v != runtime.ENOMEM {
28+
t.Errorf("mmap = %v, want %v", v, runtime.ENOMEM)
29+
}
30+
}

src/runtime/sys_linux_arm64.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ TEXT runtime·mmap(SB),NOSPLIT,$-8
269269

270270
MOVD $SYS_mmap, R8
271271
SVC
272+
CMN $4095, R0
273+
BCC 2(PC)
274+
NEG R0,R0
272275
MOVD R0, ret+32(FP)
273276
RET
274277

src/runtime/sys_linux_mips64x.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$-8-28
168168
MOVV dst+16(FP), R6
169169
MOVV $SYS_mincore, R2
170170
SYSCALL
171+
SUBVU R2, R0, R2 // caller expects negative errno
171172
MOVW R2, ret+24(FP)
172173
RET
173174

src/runtime/sys_linux_ppc64x.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
153153
MOVD n+8(FP), R4
154154
MOVD dst+16(FP), R5
155155
SYSCALL $SYS_mincore
156+
NEG R3 // caller expects negative errno
156157
MOVW R3, ret+24(FP)
157158
RET
158159

src/runtime/sys_nacl_386.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ TEXT runtime·mmap(SB),NOSPLIT,$32
227227
LEAL 24(SP), AX
228228
MOVL AX, 20(SP)
229229
NACL_SYSCALL(SYS_mmap)
230+
CMPL AX, $-4095
231+
JNA 2(PC)
232+
NEGL AX
230233
MOVL AX, ret+24(FP)
231234
RET
232235

0 commit comments

Comments
 (0)