Skip to content

Commit

Permalink
sys: add type safe Pointer wrapper
Browse files Browse the repository at this point in the history
Add TypedPointer which is a type safe wrapper around Pointer. This
relieves users of the generated bindings from knowing which types
are expected. This should make it less likely that we cause a hard
to debug panic or crash due to the kernel overwriting runtime memory.

sys.Pointer still exists because there are some cases where we
legitimately don't know what type to point at, for example the link info
APIs. Map APIs are also unchanged for now because they don't benefit
much from converting to TypedPointer[byte].

The trick to include "_ [0]*T" in TypedPointer is taken from atomic.Pointer
in the Go stdlib.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Feb 14, 2025
1 parent b8bc976 commit 427cfcd
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 170 deletions.
2 changes: 1 addition & 1 deletion btf/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func probeBTF(typ Type) error {
}

fd, err := sys.BtfLoad(&sys.BtfLoadAttr{
Btf: sys.NewSlicePointer(buf),
Btf: sys.SlicePointer(buf),
BtfSize: uint32(len(buf)),
})

Expand Down
10 changes: 6 additions & 4 deletions btf/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewHandleFromRawBTF(btf []byte) (*Handle, error) {
}

attr := &sys.BtfLoadAttr{
Btf: sys.NewSlicePointer(btf),
Btf: sys.SlicePointer(btf),
BtfSize: uint32(len(btf)),
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func NewHandleFromRawBTF(btf []byte) (*Handle, error) {

logBuf = make([]byte, logSize)
attr.BtfLogSize = logSize
attr.BtfLogBuf = sys.NewSlicePointer(logBuf)
attr.BtfLogBuf = sys.SlicePointer(logBuf)
attr.BtfLogLevel = 1
}

Expand Down Expand Up @@ -133,7 +133,8 @@ func NewHandleFromID(id ID) (*Handle, error) {
func (h *Handle) Spec(base *Spec) (*Spec, error) {
var btfInfo sys.BtfInfo
btfBuffer := make([]byte, h.size)
btfInfo.Btf, btfInfo.BtfSize = sys.NewSlicePointerLen(btfBuffer)
btfInfo.Btf = sys.SlicePointer(btfBuffer)
btfInfo.BtfSize = uint32(len(btfBuffer))

if err := sys.ObjInfo(h.fd, &btfInfo); err != nil {
return nil, err
Expand Down Expand Up @@ -204,7 +205,8 @@ func newHandleInfoFromFD(fd *sys.FD) (*HandleInfo, error) {
btfInfo.BtfSize = 0

nameBuffer := make([]byte, btfInfo.NameLen)
btfInfo.Name, btfInfo.NameLen = sys.NewSlicePointerLen(nameBuffer)
btfInfo.Name = sys.SlicePointer(nameBuffer)
btfInfo.NameLen = uint32(len(nameBuffer))
if err := sys.ObjInfo(fd, &btfInfo); err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {
if info.NrMapIds > 0 {
pi.maps = make([]MapID, info.NrMapIds)
info2.NrMapIds = info.NrMapIds
info2.MapIds = sys.NewSlicePointer(pi.maps)
info2.MapIds = sys.SlicePointer(pi.maps)
makeSecondCall = true
} else if haveProgramInfoMapIDs() == nil {
// This program really has no associated maps.
Expand All @@ -294,13 +294,13 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {
if info.XlatedProgLen > 0 {
pi.insns = make([]byte, info.XlatedProgLen)
info2.XlatedProgLen = info.XlatedProgLen
info2.XlatedProgInsns = sys.NewSlicePointer(pi.insns)
info2.XlatedProgInsns = sys.SlicePointer(pi.insns)
makeSecondCall = true
}

if info.NrLineInfo > 0 {
pi.lineInfos = make([]byte, btf.LineInfoSize*info.NrLineInfo)
info2.LineInfo = sys.NewSlicePointer(pi.lineInfos)
info2.LineInfo = sys.SlicePointer(pi.lineInfos)
info2.LineInfoRecSize = btf.LineInfoSize
info2.NrLineInfo = info.NrLineInfo
pi.numLineInfos = info.NrLineInfo
Expand All @@ -309,7 +309,7 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {

if info.NrFuncInfo > 0 {
pi.funcInfos = make([]byte, btf.FuncInfoSize*info.NrFuncInfo)
info2.FuncInfo = sys.NewSlicePointer(pi.funcInfos)
info2.FuncInfo = sys.SlicePointer(pi.funcInfos)
info2.FuncInfoRecSize = btf.FuncInfoSize
info2.NrFuncInfo = info.NrFuncInfo
pi.numFuncInfos = info.NrFuncInfo
Expand All @@ -321,31 +321,31 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {
pi.jitedInfo.numInsns = info.JitedProgLen
pi.jitedInfo.insns = make([]byte, info.JitedProgLen)
info2.JitedProgLen = info.JitedProgLen
info2.JitedProgInsns = sys.NewSlicePointer(pi.jitedInfo.insns)
info2.JitedProgInsns = sys.SlicePointer(pi.jitedInfo.insns)
makeSecondCall = true
}

if info.NrJitedFuncLens > 0 {
pi.jitedInfo.numFuncLens = info.NrJitedFuncLens
pi.jitedInfo.funcLens = make([]uint32, info.NrJitedFuncLens)
info2.NrJitedFuncLens = info.NrJitedFuncLens
info2.JitedFuncLens = sys.NewSlicePointer(pi.jitedInfo.funcLens)
info2.JitedFuncLens = sys.SlicePointer(pi.jitedInfo.funcLens)
makeSecondCall = true
}

if info.NrJitedLineInfo > 0 {
pi.jitedInfo.numLineInfos = info.NrJitedLineInfo
pi.jitedInfo.lineInfos = make([]uint64, info.NrJitedLineInfo)
info2.NrJitedLineInfo = info.NrJitedLineInfo
info2.JitedLineInfo = sys.NewSlicePointer(pi.jitedInfo.lineInfos)
info2.JitedLineInfo = sys.SlicePointer(pi.jitedInfo.lineInfos)
info2.JitedLineInfoRecSize = info.JitedLineInfoRecSize
makeSecondCall = true
}

if info.NrJitedKsyms > 0 {
pi.jitedInfo.numKsyms = info.NrJitedKsyms
pi.jitedInfo.ksyms = make([]uint64, info.NrJitedKsyms)
info2.JitedKsyms = sys.NewSlicePointer(pi.jitedInfo.ksyms)
info2.JitedKsyms = sys.SlicePointer(pi.jitedInfo.ksyms)
info2.NrJitedKsyms = info.NrJitedKsyms
makeSecondCall = true
}
Expand Down
Loading

0 comments on commit 427cfcd

Please sign in to comment.