Commit 3c3848ad authored by Dmitry Vyukov's avatar Dmitry Vyukov

runtime: fix system memory allocator on plan9

The following line in sysFree:
n += (n + memRound) &^ memRound
doubles value of n (n += n).
Which is wrong and can lead to memory corruption.

Fixes #9712

Change-Id: I3c141b71da11e38837c09408cf4f1d22e8f7f36e
Reviewed-on: https://go-review.googlesource.com/3602Reviewed-by: default avatarDavid du Colombier <0intro@gmail.com>
parent 256116ad
...@@ -9,21 +9,24 @@ import "unsafe" ...@@ -9,21 +9,24 @@ import "unsafe"
var bloc uintptr var bloc uintptr
var memlock mutex var memlock mutex
const memRound = _PAGESIZE - 1 func memRound(p uintptr) uintptr {
return (p + _PAGESIZE - 1) &^ (_PAGESIZE - 1)
}
func initBloc() { func initBloc() {
bloc = uintptr(unsafe.Pointer(&end)) bloc = memRound(uintptr(unsafe.Pointer(&end)))
} }
func sbrk(n uintptr) unsafe.Pointer { func sbrk(n uintptr) unsafe.Pointer {
lock(&memlock) lock(&memlock)
// Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c // Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c
bl := (bloc + memRound) &^ memRound bl := bloc
n = memRound(n)
if brk_(unsafe.Pointer(bl+n)) < 0 { if brk_(unsafe.Pointer(bl+n)) < 0 {
unlock(&memlock) unlock(&memlock)
return nil return nil
} }
bloc = bl + n bloc += n
unlock(&memlock) unlock(&memlock)
return unsafe.Pointer(bl) return unsafe.Pointer(bl)
} }
...@@ -42,7 +45,7 @@ func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) { ...@@ -42,7 +45,7 @@ func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) {
// from tiny/mem.c // from tiny/mem.c
// Push pointer back if this is a free // Push pointer back if this is a free
// of the most recent sysAlloc. // of the most recent sysAlloc.
n += (n + memRound) &^ memRound n = memRound(n)
if bloc == uintptr(v)+n { if bloc == uintptr(v)+n {
bloc -= n bloc -= n
} }
......
...@@ -20,8 +20,6 @@ func skipTraceTestsIfNeeded(t *testing.T) { ...@@ -20,8 +20,6 @@ func skipTraceTestsIfNeeded(t *testing.T) {
switch runtime.GOOS { switch runtime.GOOS {
case "solaris": case "solaris":
t.Skip("skipping: solaris timer can go backwards (http://golang.org/issue/8976)") t.Skip("skipping: solaris timer can go backwards (http://golang.org/issue/8976)")
case "plan9":
t.Skip("skipping: plan9 tests fail with out of memory (http://golang.org/issue/9712")
} }
switch runtime.GOARCH { switch runtime.GOARCH {
...@@ -234,7 +232,7 @@ eventLoop: ...@@ -234,7 +232,7 @@ eventLoop:
for _, f := range ev.stk { for _, f := range ev.stk {
if strings.HasSuffix(f.file, "trace_test.go") && if strings.HasSuffix(f.file, "trace_test.go") &&
strings.HasSuffix(f.fn, "pprof_test.TestTraceSymbolize") && strings.HasSuffix(f.fn, "pprof_test.TestTraceSymbolize") &&
f.line == 218 { f.line == 216 {
found = true found = true
break eventLoop break eventLoop
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment