Skip to content

Commit

Permalink
runtime: add page tracer
Browse files Browse the repository at this point in the history
This change adds a new GODEBUG flag called pagetrace that writes a
low-overhead trace of how pages of memory are managed by the Go runtime.

The page tracer is kept behind a GOEXPERIMENT flag due to a potential
security risk for setuid binaries.

Change-Id: I6f4a2447d02693c25214400846a5d2832ad6e5c0
Reviewed-on: https://go-review.googlesource.com/c/go/+/444157
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
mknyszek committed Nov 18, 2022
1 parent 0613418 commit e4435cb
Show file tree
Hide file tree
Showing 51 changed files with 636 additions and 29 deletions.
9 changes: 9 additions & 0 deletions src/internal/goexperiment/exp_pagetrace_off.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/internal/goexperiment/exp_pagetrace_on.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/internal/goexperiment/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ type Flags struct {
// Arenas causes the "arena" standard library package to be visible
// to the outside world.
Arenas bool

// PageTrace enables GODEBUG=pagetrace=/path/to/result. This feature
// is a GOEXPERIMENT due to a security risk with setuid binaries:
// this compels the Go runtime to write to some arbitrary file, which
// may be exploited.
PageTrace bool
}
14 changes: 14 additions & 0 deletions src/runtime/create_file_nounix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !unix

package runtime

const canCreateFile = false

func create(name *byte, perm int32) int32 {
throw("unimplemented")
return -1
}
14 changes: 14 additions & 0 deletions src/runtime/create_file_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix

package runtime

const canCreateFile = true

// create returns an fd to a write-only file.
func create(name *byte, perm int32) int32 {
return open(name, _O_CREAT|_O_WRONLY|_O_TRUNC, perm)
}
3 changes: 3 additions & 0 deletions src/runtime/defs1_netbsd_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const (
_EFAULT = 0xe
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x400000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs1_netbsd_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const (
_EFAULT = 0xe
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x400000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs1_netbsd_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const (
_EFAULT = 0xe
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x400000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs1_netbsd_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const (
_EFAULT = 0xe
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x400000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs1_solaris_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ const (

_MAXHOSTNAMELEN = 0x100

_O_WRONLY = 0x1
_O_NONBLOCK = 0x80
_O_TRUNC = 0x200
_O_CREAT = 0x100
_O_CLOEXEC = 0x800000
_FD_CLOEXEC = 0x1
_F_GETFL = 0x3
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ const (
_ITIMER_PROF = C.ITIMER_PROF

_O_RDONLY = C.O_RDONLY
_O_WRONLY = C.O_WRONLY
_O_NONBLOCK = C.O_NONBLOCK
_O_CREAT = C.O_CREAT
_O_TRUNC = C.O_TRUNC

_SS_DISABLE = C.SS_DISABLE
_SI_USER = C.SI_USER
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const (
_ITIMER_PROF = 0x2

_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x100
_O_TRUNC = 0x200

_SS_DISABLE = 0x2
_SI_USER = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ const (
F_SETFL = C.F_SETFL
FD_CLOEXEC = C.FD_CLOEXEC

O_WRONLY = C.O_WRONLY
O_NONBLOCK = C.O_NONBLOCK
O_CREAT = C.O_CREAT
O_TRUNC = C.O_TRUNC
)

type StackT C.struct_sigaltstack
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/defs_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const (
_F_SETFL = 0x4
_FD_CLOEXEC = 0x1

_O_NONBLOCK = 4
_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
)

type stackt struct {
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/defs_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ const (
_F_SETFL = 0x4
_FD_CLOEXEC = 0x1

_O_NONBLOCK = 4
_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
)

type stackt struct {
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const (
EBUSY = C.EBUSY
EAGAIN = C.EAGAIN

O_WRONLY = C.O_WRONLY
O_NONBLOCK = C.O_NONBLOCK
O_CREAT = C.O_CREAT
O_TRUNC = C.O_TRUNC
O_CLOEXEC = C.O_CLOEXEC

PROT_NONE = C.PROT_NONE
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_dragonfly_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const (
_EBUSY = 0x10
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x20000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ const (
EAGAIN = C.EAGAIN
ETIMEDOUT = C.ETIMEDOUT

O_WRONLY = C.O_WRONLY
O_NONBLOCK = C.O_NONBLOCK
O_CREAT = C.O_CREAT
O_TRUNC = C.O_TRUNC
O_CLOEXEC = C.O_CLOEXEC

PROT_NONE = C.PROT_NONE
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/runtime/defs_freebsd_riscv64.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const (
_EAGAIN = 0x23
_ETIMEDOUT = 0x3c

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x100000

_PROT_NONE = 0x0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ const (
_SIGEV_THREAD_ID = 0x4

_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
)
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const (
_ITIMER_PROF = 0x2
_ITIMER_VIRTUAL = 0x1
_O_RDONLY = 0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
)
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_loong64.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
)
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_mips64x.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x100
_O_TRUNC = 0x200
_O_NONBLOCK = 0x80
_O_CLOEXEC = 0x80000
_SA_RESTORER = 0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_mipsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_NONBLOCK = 0x80
_O_CREAT = 0x100
_O_TRUNC = 0x200
_O_CLOEXEC = 0x80000
_SA_RESTORER = 0
)
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
_SA_RESTORER = 0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
_SA_RESTORER = 0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_riscv64.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
)
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_linux_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ type sigevent struct {

const (
_O_RDONLY = 0x0
_O_WRONLY = 0x1
_O_CREAT = 0x40
_O_TRUNC = 0x200
_O_NONBLOCK = 0x800
_O_CLOEXEC = 0x80000
_SA_RESTORER = 0
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const (
EFAULT = C.EFAULT
EAGAIN = C.EAGAIN

O_WRONLY = C.O_WRONLY
O_NONBLOCK = C.O_NONBLOCK
O_CREAT = C.O_CREAT
O_TRUNC = C.O_TRUNC
O_CLOEXEC = C.O_CLOEXEC

PROT_NONE = C.PROT_NONE
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/defs_openbsd_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const (
_EFAULT = 0xe
_EAGAIN = 0x23

_O_WRONLY = 0x1
_O_NONBLOCK = 0x4
_O_CREAT = 0x200
_O_TRUNC = 0x400
_O_CLOEXEC = 0x10000

_PROT_NONE = 0x0
Expand Down
Loading

0 comments on commit e4435cb

Please sign in to comment.