Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions interpreter/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"path"
"reflect"
"regexp"
"slices"
Expand Down Expand Up @@ -48,6 +49,23 @@ func pythonVer(major, minor uint16) uint16 {
return major*0x100 + minor
}

func readPyVersionHex(ef *pfelf.File) (major uint8, minor uint8, err error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a coredump test hitting this logic?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not super easy for arbitrary contributors to add core dumps afaict. Do I not need s3 put access first?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not super easy for arbitrary contributors to add core dumps afaict. Do I not need s3 put access first?

create a google drive link (or similar shareable blob store) and share it with one of the maintainers giving them read access, @florianl has been very helpful with uploading coredumps for the ruby tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A full coredump test might be a bit much to test this function. Therefore it is fine for me to continue and merge it without. A more simpler way to test readPyVersionHex() could be to have a base64 encoded data string, like helloworldBin in testsupport, and add a regular unit test.

// Py_Version is referenced in CPython internals for versioned Python binaries.
// https://github.com/python/cpython/blob/v3.11.0/Doc/c-api/apiabiversion.rst
addr, err := ef.LookupSymbolAddress("Py_Version")
Comment thread
dkratunov marked this conversation as resolved.
if err != nil {
return 0, 0, err
}
rm := ef.GetRemoteMemory()
versionHex := rm.Uint32(libpf.Address(addr))
major = uint8((versionHex >> 24) & 0xff)
minor = uint8((versionHex >> 16) & 0xff)
if major == 0 {
return 0, 0, fmt.Errorf("invalid Py_Version 0x%x", versionHex)
}
return major, minor, nil
}

//nolint:lll
type pythonData struct {
version uint16
Expand Down Expand Up @@ -721,19 +739,36 @@ func decodeStub(ef *pfelf.File, memoryBase libpf.SymbolValue,

func Loader(ebpf interpreter.EbpfHandler, info *interpreter.LoaderInfo) (interpreter.Data, error) {
mainDSO := false
major := uint16(0)
minor := uint16(0)
matches := libpythonRegex.FindStringSubmatch(info.FileName())
if matches == nil {
mainDSO = true
matches = pythonRegex.FindStringSubmatch(info.FileName())
if matches == nil {
}
if matches == nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we move readPyVersionHex invocation under this matches==nil case block and avoid major == 0 check ?

if !strings.HasPrefix(path.Base(info.FileName()), "python") {
return nil, nil
}
} else {
majorValue, _ := strconv.ParseUint(matches[1], 10, 16)
minorValue, _ := strconv.ParseUint(matches[2], 10, 16)
major = uint16(majorValue)
minor = uint16(minorValue)
}

ef, err := info.GetELF()
if err != nil {
return nil, err
}
if major == 0 {
majorFromSym, minorFromSym, versionErr := readPyVersionHex(ef)
if versionErr != nil {
return nil, nil
}
major = uint16(majorFromSym)
minor = uint16(minorFromSym)
}

if mainDSO {
var needed []string
Expand All @@ -749,9 +784,7 @@ func Loader(ebpf interpreter.EbpfHandler, info *interpreter.LoaderInfo) (interpr
}

var pyruntimeAddr, autoTLSKey libpf.SymbolValue
major, _ := strconv.ParseUint(matches[1], 10, 16)
minor, _ := strconv.ParseUint(matches[2], 10, 16)
version := pythonVer(uint16(major), uint16(minor))
version := pythonVer(major, minor)

minVer := pythonVer(3, 6)
maxVer := pythonVer(3, 14)
Expand Down
Loading