Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1079)

Issue 139360044: code review 139360044: syscall: pin allocated C string across call to Syscall (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
9 years, 7 months ago by rsc
Modified:
9 years, 7 months ago
Reviewers:
khr, iant, rlh, dfc, aram, brainman
CC:
iant, khr, bradfitz, khr1, aram, rlh, dvyukov, golang-codereviews, r
Visibility:
Public.

Description

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.

Patch Set 1 #

Patch Set 2 : diff -r 9e30a2df4e1d3babaf09895488730b26f829d768 https://code.google.com/p/go/ #

Patch Set 3 : diff -r 9e30a2df4e1d3babaf09895488730b26f829d768 https://code.google.com/p/go/ #

Patch Set 4 : diff -r 9e30a2df4e1d3babaf09895488730b26f829d768 https://code.google.com/p/go/ #

Patch Set 5 : diff -r 9e30a2df4e1d3babaf09895488730b26f829d768 https://code.google.com/p/go/ #

Patch Set 6 : diff -r ad5d9f8f9be743e72f89d85d8bd6348807bdac90 https://code.google.com/p/go/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+607 lines, -23 lines) Patch
A src/syscall/asm.s View 1 1 chunk +8 lines, -0 lines 0 comments Download
M src/syscall/dll_windows.go View 1 2 3 4 1 chunk +1 line, -0 lines 0 comments Download
M src/syscall/mksyscall.pl View 1 2 3 chunks +6 lines, -0 lines 0 comments Download
M src/syscall/mksyscall_solaris.pl View 1 2 4 chunks +6 lines, -2 lines 0 comments Download
M src/syscall/so_solaris.go View 1 2 3 4 2 chunks +2 lines, -0 lines 0 comments Download
M src/syscall/syscall.go View 1 2 chunks +7 lines, -0 lines 0 comments Download
M src/syscall/syscall_darwin.go View 1 2 3 4 1 chunk +1 line, -0 lines 0 comments Download
M src/syscall/syscall_linux.go View 1 2 3 4 2 chunks +6 lines, -2 lines 0 comments Download
M src/syscall/syscall_linux_386.go View 1 2 3 4 1 chunk +1 line, -0 lines 0 comments Download
M src/syscall/syscall_linux_arm.go View 1 2 3 4 1 chunk +1 line, -0 lines 0 comments Download
M src/syscall/syscall_plan9.go View 1 2 3 4 1 chunk +2 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_darwin_386.go View 1 2 28 chunks +32 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_darwin_amd64.go View 1 2 28 chunks +32 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_dragonfly_386.go View 1 2 27 chunks +30 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_dragonfly_amd64.go View 1 2 27 chunks +30 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_freebsd_386.go View 1 2 27 chunks +30 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_freebsd_amd64.go View 1 2 27 chunks +30 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_freebsd_arm.go View 1 2 27 chunks +30 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_linux_386.go View 1 2 40 chunks +50 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_linux_amd64.go View 1 2 41 chunks +51 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_linux_arm.go View 1 2 40 chunks +50 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_netbsd_386.go View 1 2 24 chunks +27 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_netbsd_amd64.go View 1 2 24 chunks +27 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_netbsd_arm.go View 1 2 24 chunks +27 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_openbsd_386.go View 1 2 26 chunks +29 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_openbsd_amd64.go View 1 2 26 chunks +29 lines, -0 lines 0 comments Download
M src/syscall/zsyscall_plan9_386.go View 1 2 18 chunks +28 lines, -18 lines 0 comments Download
M src/syscall/zsyscall_plan9_amd64.go View 1 2 9 chunks +11 lines, -1 line 0 comments Download
M src/syscall/zsyscall_solaris_amd64.go View 1 2 20 chunks +23 lines, -0 lines 0 comments Download

Messages

Total messages: 23
rsc
Hello iant@golang.org (cc: aram@mgk.ro, dvyukov@google.com, golang-codereviews@googlegroups.com, khr@golang.org, r@golang.org, rlh@golang.org), I'd like you to review this ...
9 years, 7 months ago (2014-09-08 18:57:06 UTC) #1
iant
LGTM Does this handle every case, though? What about something like syscall.Fstat(0, &s) where s ...
9 years, 7 months ago (2014-09-08 19:05:37 UTC) #2
khr
LGTM. What method did you use to find all of these? And/or: how do we ...
9 years, 7 months ago (2014-09-08 19:09:02 UTC) #3
bradfitz
Keith, most look like they were done via the Perl scripts modified in this CL ...
9 years, 7 months ago (2014-09-08 19:10:10 UTC) #4
rsc
On Mon, Sep 8, 2014 at 3:05 PM, <iant@golang.org> wrote: > LGTM > > Does ...
9 years, 7 months ago (2014-09-08 19:27:35 UTC) #5
rsc
Yes, all the z files were regenerated by editing the perl scripts and running 'mkall.sh ...
9 years, 7 months ago (2014-09-08 19:28:10 UTC) #6
khr1
On Mon, Sep 8, 2014 at 12:27 PM, Russ Cox <rsc@golang.org> wrote: > On Mon, ...
9 years, 7 months ago (2014-09-08 19:32:46 UTC) #7
aram
LGTM Happy that the Solaris port exposed a bug in all the other platforms.
9 years, 7 months ago (2014-09-08 19:55:19 UTC) #8
rlh
On 2014/09/08 19:55:19, aram wrote: > LGTM > > Happy that the Solaris port exposed ...
9 years, 7 months ago (2014-09-08 20:03:41 UTC) #9
rsc
Thanks. I am still internalizing that what GC people mean by 'pin' is not what ...
9 years, 7 months ago (2014-09-08 20:59:06 UTC) #10
rsc
*** Submitted as https://code.google.com/p/go/source/detail?r=f7d4308ba6fe *** syscall: keep allocated C string live across call to Syscall ...
9 years, 7 months ago (2014-09-08 21:00:06 UTC) #11
dfc
Thanks for digging into this and finding a solution for Solaris Russ. I appreciate the ...
9 years, 7 months ago (2014-09-08 22:18:02 UTC) #12
brainman
On 2014/09/08 21:00:06, rsc wrote: > *** Submitted as https://code.google.com/p/go/source/detail?r=f7d4308ba6fe *** > > syscall: keep ...
9 years, 7 months ago (2014-09-08 23:43:14 UTC) #13
bradfitz
On Mon, Sep 8, 2014 at 4:43 PM, <alex.brainman@gmail.com> wrote: > On 2014/09/08 21:00:06, rsc ...
9 years, 7 months ago (2014-09-08 23:51:14 UTC) #14
brainman
> ... You should make the same edits to that. ... Will do. And go.sys ...
9 years, 7 months ago (2014-09-09 00:08:09 UTC) #15
rsc
On Mon, Sep 8, 2014 at 7:43 PM, <alex.brainman@gmail.com> wrote: > On 2014/09/08 21:00:06, rsc ...
9 years, 7 months ago (2014-09-09 00:36:32 UTC) #16
bradfitz
On Mon, Sep 8, 2014 at 5:08 PM, <alex.brainman@gmail.com> wrote: > ... You should make ...
9 years, 7 months ago (2014-09-09 00:38:28 UTC) #17
brainman
On 2014/09/09 00:36:32, rsc wrote: This is all well for us, but what about other ...
9 years, 7 months ago (2014-09-09 00:38:41 UTC) #18
bradfitz
On Mon, Sep 8, 2014 at 5:38 PM, <alex.brainman@gmail.com> wrote: > On 2014/09/09 00:36:32, rsc ...
9 years, 7 months ago (2014-09-09 00:39:11 UTC) #19
brainman
On 2014/09/09 00:39:11, bradfitz wrote: > > They get to keep both pieces. I really ...
9 years, 7 months ago (2014-09-09 00:48:49 UTC) #20
rsc
On Mon, Sep 8, 2014 at 8:48 PM, <alex.brainman@gmail.com> wrote: > On 2014/09/09 00:39:11, bradfitz ...
9 years, 7 months ago (2014-09-09 00:51:19 UTC) #21
bradfitz
On Mon, Sep 8, 2014 at 5:48 PM, <alex.brainman@gmail.com> wrote: > On 2014/09/09 00:39:11, bradfitz ...
9 years, 7 months ago (2014-09-09 00:52:31 UTC) #22
brainman
9 years, 7 months ago (2014-09-09 01:16:03 UTC) #23
Message was sent while issue was closed.
On 2014/09/09 00:51:19, rsc wrote:
> 
> Look at the number of changes needed in Brad's CL. It's not every call. If
> you write a typed wrapper, everything works fine. It's only the wrappers
> that do their own allocations inside the wrapper that are problematic.
> 

Fair enough. But still it needs to be documented somewhere. We don't want
chasing memory corruption bugs.

On 2014/09/09 00:52:31, bradfitz wrote:
> 
> Few people use syscall.Syscall directly, anyway, and it's generally only
> (should only be?) advanced users.

Unfortunately I cannot say it is true on Windows. Standard library is quite good
for what it does, but anything outside of that you need to call syscall.Syscall.
Every library you need to use is just a dll.

Alex
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b