diff options
author | Russ Cox <rsc@golang.org> | 2014-09-08 16:59:59 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-09-08 16:59:59 -0400 |
commit | cf68c86ed9b58d520f423ba7859ca78bbbeafe75 (patch) | |
tree | 1e9db0155e423a7b2c6380231aa033af41a38890 /src/syscall/zsyscall_openbsd_386.go | |
parent | 13d89ea50fbfcf3ee71dba797c49e157453981b4 (diff) | |
download | go-cf68c86ed9b58d520f423ba7859ca78bbbeafe75.tar.gz |
syscall: keep allocated C string live across call to Syscall
Given:
p := alloc()
fn_taking_ptr(p)
p is NOT recorded as live at the call to fn_taking_ptr:
it's not needed by the code following the call.
p was passed to fn_taking_ptr, and fn_taking_ptr must keep
it alive as long as it needs it.
In practice, fn_taking_ptr will keep its own arguments live
for as long as the function is executing.
But if instead you have:
p := alloc()
i := uintptr(unsafe.Pointer(p))
fn_taking_int(i)
p is STILL NOT recorded as live at the call to fn_taking_int:
it's not needed by the code following the call.
fn_taking_int is responsible for keeping its own arguments
live, but fn_taking_int is written to take an integer, so even
though fn_taking_int does keep its argument live, that argument
does not keep the allocated memory live, because the garbage
collector does not dereference integers.
The shorter form:
p := alloc()
fn_taking_int(uintptr(unsafe.Pointer(p)))
and the even shorter form:
fn_taking_int(uintptr(unsafe.Pointer(alloc())))
are both the same as the 3-line form above.
syscall.Syscall is like fn_taking_int: it is written to take a list
of integers, and yet those integers are sometimes pointers.
If there is no other copy of those pointers being kept live,
the memory they point at may be garbage collected during
the call to syscall.Syscall.
This is happening on Solaris: for whatever reason, the timing
is such that the garbage collector manages to free the string
argument to the open(2) system call before the system call
has been invoked.
Change the system call wrappers to insert explicit references
that will keep the allocations alive in the original frame
(and therefore preserve the memory) until after syscall.Syscall
has returned.
Should fix Solaris flakiness.
This is not a problem for cgo, because cgo wrappers have
correctly typed arguments.
LGTM=iant, khr, aram, rlh
R=iant, khr, bradfitz, aram, rlh
CC=dvyukov, golang-codereviews, r
https://codereview.appspot.com/139360044
Diffstat (limited to 'src/syscall/zsyscall_openbsd_386.go')
-rw-r--r-- | src/syscall/zsyscall_openbsd_386.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/syscall/zsyscall_openbsd_386.go b/src/syscall/zsyscall_openbsd_386.go index 785e7c3b8..5b005d2dc 100644 --- a/src/syscall/zsyscall_openbsd_386.go +++ b/src/syscall/zsyscall_openbsd_386.go @@ -230,6 +230,7 @@ func utimes(path string, timeval *[2]Timeval) (err error) { return } _, _, e1 := Syscall(SYS_UTIMES, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(timeval)), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -293,6 +294,7 @@ func Access(path string, mode uint32) (err error) { return } _, _, e1 := Syscall(SYS_ACCESS, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -318,6 +320,7 @@ func Chdir(path string) (err error) { return } _, _, e1 := Syscall(SYS_CHDIR, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -333,6 +336,7 @@ func Chflags(path string, flags int) (err error) { return } _, _, e1 := Syscall(SYS_CHFLAGS, uintptr(unsafe.Pointer(_p0)), uintptr(flags), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -348,6 +352,7 @@ func Chmod(path string, mode uint32) (err error) { return } _, _, e1 := Syscall(SYS_CHMOD, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -363,6 +368,7 @@ func Chown(path string, uid int, gid int) (err error) { return } _, _, e1 := Syscall(SYS_CHOWN, uintptr(unsafe.Pointer(_p0)), uintptr(uid), uintptr(gid)) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -378,6 +384,7 @@ func Chroot(path string) (err error) { return } _, _, e1 := Syscall(SYS_CHROOT, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -680,6 +687,7 @@ func Lchown(path string, uid int, gid int) (err error) { return } _, _, e1 := Syscall(SYS_LCHOWN, uintptr(unsafe.Pointer(_p0)), uintptr(uid), uintptr(gid)) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -700,6 +708,8 @@ func Link(path string, link string) (err error) { return } _, _, e1 := Syscall(SYS_LINK, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_p1)), 0) + use(unsafe.Pointer(_p0)) + use(unsafe.Pointer(_p1)) if e1 != 0 { err = e1 } @@ -725,6 +735,7 @@ func Lstat(path string, stat *Stat_t) (err error) { return } _, _, e1 := Syscall(SYS_LSTAT, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(stat)), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -740,6 +751,7 @@ func Mkdir(path string, mode uint32) (err error) { return } _, _, e1 := Syscall(SYS_MKDIR, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -755,6 +767,7 @@ func Mkfifo(path string, mode uint32) (err error) { return } _, _, e1 := Syscall(SYS_MKFIFO, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -770,6 +783,7 @@ func Mknod(path string, mode uint32, dev int) (err error) { return } _, _, e1 := Syscall(SYS_MKNOD, uintptr(unsafe.Pointer(_p0)), uintptr(mode), uintptr(dev)) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -795,6 +809,7 @@ func Open(path string, mode int, perm uint32) (fd int, err error) { return } r0, _, e1 := Syscall(SYS_OPEN, uintptr(unsafe.Pointer(_p0)), uintptr(mode), uintptr(perm)) + use(unsafe.Pointer(_p0)) fd = int(r0) if e1 != 0 { err = e1 @@ -811,6 +826,7 @@ func Pathconf(path string, name int) (val int, err error) { return } r0, _, e1 := Syscall(SYS_PATHCONF, uintptr(unsafe.Pointer(_p0)), uintptr(name), 0) + use(unsafe.Pointer(_p0)) val = int(r0) if e1 != 0 { err = e1 @@ -884,6 +900,7 @@ func Readlink(path string, buf []byte) (n int, err error) { _p1 = unsafe.Pointer(&_zero) } r0, _, e1 := Syscall(SYS_READLINK, uintptr(unsafe.Pointer(_p0)), uintptr(_p1), uintptr(len(buf))) + use(unsafe.Pointer(_p0)) n = int(r0) if e1 != 0 { err = e1 @@ -905,6 +922,8 @@ func Rename(from string, to string) (err error) { return } _, _, e1 := Syscall(SYS_RENAME, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_p1)), 0) + use(unsafe.Pointer(_p0)) + use(unsafe.Pointer(_p1)) if e1 != 0 { err = e1 } @@ -920,6 +939,7 @@ func Revoke(path string) (err error) { return } _, _, e1 := Syscall(SYS_REVOKE, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -935,6 +955,7 @@ func Rmdir(path string) (err error) { return } _, _, e1 := Syscall(SYS_RMDIR, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1001,6 +1022,7 @@ func Setlogin(name string) (err error) { return } _, _, e1 := Syscall(SYS_SETLOGIN, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1097,6 +1119,7 @@ func Stat(path string, stat *Stat_t) (err error) { return } _, _, e1 := Syscall(SYS_STAT, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(stat)), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1112,6 +1135,7 @@ func Statfs(path string, stat *Statfs_t) (err error) { return } _, _, e1 := Syscall(SYS_STATFS, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(stat)), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1132,6 +1156,8 @@ func Symlink(path string, link string) (err error) { return } _, _, e1 := Syscall(SYS_SYMLINK, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_p1)), 0) + use(unsafe.Pointer(_p0)) + use(unsafe.Pointer(_p1)) if e1 != 0 { err = e1 } @@ -1157,6 +1183,7 @@ func Truncate(path string, length int64) (err error) { return } _, _, e1 := Syscall6(SYS_TRUNCATE, uintptr(unsafe.Pointer(_p0)), 0, uintptr(length), uintptr(length>>32), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1180,6 +1207,7 @@ func Unlink(path string) (err error) { return } _, _, e1 := Syscall(SYS_UNLINK, uintptr(unsafe.Pointer(_p0)), 0, 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } @@ -1195,6 +1223,7 @@ func Unmount(path string, flags int) (err error) { return } _, _, e1 := Syscall(SYS_UNMOUNT, uintptr(unsafe.Pointer(_p0)), uintptr(flags), 0) + use(unsafe.Pointer(_p0)) if e1 != 0 { err = e1 } |