Skip to content

Commit

Permalink
pkg/util/allstacks: wrap the stack traces with debugutil.SafeStack
Browse files Browse the repository at this point in the history
The functions Get() and GetWithBuf() internally call runtime.Stack() to
fetch the calling go routine's stack trace.

This commit changes the return type of both the function from []byte to
debugutil.SafeStack (introduced in #136288). This will ensure that the
stack traces generated using these function are not redacted.

Part of: CRDB-15292
Epic: CRDB-37533
Release note: None
  • Loading branch information
arjunmahishi committed Dec 9, 2024
1 parent c83ac56 commit 101b9ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/util/allstacks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go_library(
srcs = ["allstacks.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/util/allstacks",
visibility = ["//visibility:public"],
deps = ["//pkg/util/debugutil"],
)
10 changes: 7 additions & 3 deletions pkg/util/allstacks/allstacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

package allstacks

import "runtime"
import (
"runtime"

"github.com/cockroachdb/cockroach/pkg/util/debugutil"
)

// Get returns all stacks, except if that takes more than 512mb of memory, in
// which case it returns only 512mb worth of stacks (clipping the last stack
// if necessary).
func Get() []byte {
func Get() debugutil.SafeStack {
return GetWithBuf(nil)
}

// GetWithBuf is like Get, but tries to use the provided slice first, allocating
// a new, larger, slice only if necessary.
func GetWithBuf(buf []byte) []byte {
func GetWithBuf(buf []byte) debugutil.SafeStack {
buf = buf[:cap(buf)]
// We don't know how big the traces are, so grow a few times if they don't
// fit. Start large, though.
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/debugutil/debugutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func init() {
}(os.Getppid()))
}

// SafeStack is an alias for []byte that handles redaction. Use this type
// instead of []byte when you are sure that the stack trace does not contain
// sensitive information.
type SafeStack []byte

func (s SafeStack) SafeValue() {}
Expand Down

0 comments on commit 101b9ec

Please sign in to comment.