Skip to content

Commit

Permalink
Change all Parameter uses to pointers
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <[email protected]>
  • Loading branch information
grantseltzer committed Dec 2, 2024
1 parent 58ba2aa commit b3edbd7
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 83 deletions.
2 changes: 0 additions & 2 deletions pkg/dynamicinstrumentation/codegen/c/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,10 @@ static __always_inline int read_str_to_output(struct expression_context context,
if (length > limit) {
length = limit;
}

err = bpf_probe_read(&context.event->output[*(context.output_offset)], length, (char*)characterPointer);
if (err != 0) {
bpf_printk("error reading string: %d", err);
}
// Have to check again to make sure the verifier knows
if (length > limit) {
length = limit;
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/dynamicinstrumentation/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func GenerateBPFParamsCode(procInfo *ditypes.ProcessInfo, probe *ditypes.Probe)
if probe.InstrumentationInfo.InstrumentationOptions.CaptureParameters {
params := procInfo.TypeMap.Functions[probe.FuncName] //applyCaptureDepth(procInfo.TypeMap.Functions[probe.FuncName], probe.InstrumentationInfo.InstrumentationOptions.MaxReferenceDepth)
if params != nil {
for i := range *params {
flattenedParams := flattenParameters([]ditypes.Parameter{(*params)[i]})
for i := range params {
flattenedParams := flattenParameters([]*ditypes.Parameter{params[i]})
err := generateHeadersText(flattenedParams, out)
if err != nil {
return err
Expand Down Expand Up @@ -67,7 +67,7 @@ func resolveHeaderTemplate(param *ditypes.Parameter) (*template.Template, error)
}
}

func generateHeadersText(params []ditypes.Parameter, out io.Writer) error {
func generateHeadersText(params []*ditypes.Parameter, out io.Writer) error {
for i := range params {
err := generateHeaderText(params[i], out)
if err != nil {
Expand All @@ -77,13 +77,13 @@ func generateHeadersText(params []ditypes.Parameter, out io.Writer) error {
return nil
}

func generateHeaderText(param ditypes.Parameter, out io.Writer) error {
func generateHeaderText(param *ditypes.Parameter, out io.Writer) error {
if reflect.Kind(param.Kind) == reflect.Slice {
return generateSliceHeader(&param, out)
return generateSliceHeader(param, out)
} else if reflect.Kind(param.Kind) == reflect.String {
return generateStringHeader(&param, out)
return generateStringHeader(param, out)
} else { //nolint:revive // TODO
tmplt, err := resolveHeaderTemplate(&param)
tmplt, err := resolveHeaderTemplate(param)
if err != nil {
return err
}
Expand All @@ -98,9 +98,9 @@ func generateHeaderText(param ditypes.Parameter, out io.Writer) error {
return nil
}

func generateParametersTextViaLocationExpressions(params []ditypes.Parameter, out io.Writer) error {
func generateParametersTextViaLocationExpressions(params []*ditypes.Parameter, out io.Writer) error {
for i := range params {
collectedExpressions := collectLocationExpressions(&params[i])
collectedExpressions := collectLocationExpressions(params[i])
for _, locationExpression := range collectedExpressions {
locationExpression.InstructionID = randomID()
template, err := resolveLocationExpressionTemplate(locationExpression)
Expand Down Expand Up @@ -131,7 +131,7 @@ func collectLocationExpressions(param *ditypes.Parameter) []ditypes.LocationExpr
top = queue[0]
queue = queue[1:]
for i := range top.ParameterPieces {
queue = append(queue, &top.ParameterPieces[i])
queue = append(queue, top.ParameterPieces[i])
}
if len(top.LocationExpressions) > 0 {
collectedExpressions = append(top.LocationExpressions, collectedExpressions...)
Expand Down
10 changes: 5 additions & 5 deletions pkg/dynamicinstrumentation/codegen/output_offsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func applyCaptureDepth(params []ditypes.Parameter, maxDepth int) []ditypes.Param

if front.depth == maxDepth {
// max capture depth reached, remove parameters below this level.
front.param.ParameterPieces = []ditypes.Parameter{}
front.param.ParameterPieces = []*ditypes.Parameter{}
if front.param.Kind == uint(reflect.Struct) {
// struct size reflects the number of fields,
// setting to 0 tells the user space parsing not to
Expand All @@ -49,16 +49,16 @@ func applyCaptureDepth(params []ditypes.Parameter, maxDepth int) []ditypes.Param
for i := range front.param.ParameterPieces {
queue = append(queue, paramDepthCounter{
depth: front.depth + 1,
param: &front.param.ParameterPieces[i],
param: front.param.ParameterPieces[i],
})
}
}
}
return params
}

func flattenParameters(params []ditypes.Parameter) []ditypes.Parameter {
flattenedParams := []ditypes.Parameter{}
func flattenParameters(params []*ditypes.Parameter) []*ditypes.Parameter {
flattenedParams := []*ditypes.Parameter{}
for i := range params {
kind := reflect.Kind(params[i].Kind)
if kind == reflect.Slice || kind == reflect.String {
Expand All @@ -69,9 +69,9 @@ func flattenParameters(params []ditypes.Parameter) []ditypes.Parameter {
flattenedParams = append(flattenedParams, params[i])
} else if hasHeader(kind) {
paramHeader := params[i]
paramHeader.ParameterPieces = nil
flattenedParams = append(flattenedParams, paramHeader)
flattenedParams = append(flattenedParams, flattenParameters(params[i].ParameterPieces)...)
paramHeader.ParameterPieces = nil
} else if len(params[i].ParameterPieces) > 0 {
flattenedParams = append(flattenedParams, flattenParameters(params[i].ParameterPieces)...)
} else {
Expand Down
28 changes: 14 additions & 14 deletions pkg/dynamicinstrumentation/diconfig/binary_inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func AnalyzeBinary(procInfo *ditypes.ProcessInfo) error {

fieldIDs := make([]bininspect.FieldIdentifier, 0)
for _, funcParams := range typeMap.Functions {
for _, param := range *funcParams {
for _, param := range funcParams {
fieldIDs = append(fieldIDs,
collectFieldIDs(param)...)
}
Expand All @@ -82,17 +82,17 @@ func AnalyzeBinary(procInfo *ditypes.ProcessInfo) error {
for functionName, functionMetadata := range r.Functions {
putLocationsInParams(functionMetadata.Parameters, r.StructOffsets, procInfo.TypeMap.Functions, functionName)
populateLocationExpressions(r.Functions, procInfo)
correctStructSizes(*procInfo.TypeMap.Functions[functionName])
correctStructSizes(procInfo.TypeMap.Functions[functionName])
}

return nil
}

// collectFieldIDs returns all struct fields if there are any amongst types of parameters
// including if there's structs that are nested deep within complex types
func collectFieldIDs(param ditypes.Parameter) []bininspect.FieldIdentifier {
func collectFieldIDs(param *ditypes.Parameter) []bininspect.FieldIdentifier {
fieldIDs := []bininspect.FieldIdentifier{}
stack := append([]ditypes.Parameter{param}, param.ParameterPieces...)
stack := append([]*ditypes.Parameter{param}, param.ParameterPieces...)

for len(stack) != 0 {

Expand Down Expand Up @@ -147,11 +147,11 @@ func populateLocationExpressions(
if !ok || limitInfo == nil {
continue
}
for i := range *parameters {
for i := range parameters {
if i >= len(funcMetadata.Parameters) {
return errors.New("parameter metadata does not line up with parameter itself")
}
(*parameters)[i].LocationExpressions = GenerateLocationExpression(limitInfo, &(*parameters)[i])
parameters[i].LocationExpressions = GenerateLocationExpression(limitInfo, parameters[i])
}
}
return nil
Expand All @@ -160,7 +160,7 @@ func populateLocationExpressions(
func putLocationsInParams(
paramMetadatas []bininspect.ParameterMetadata,
fieldLocations map[bininspect.FieldIdentifier]uint64,
funcMap map[string]*[]ditypes.Parameter,
funcMap map[string][]*ditypes.Parameter,
funcName string) {

params := funcMap[funcName]
Expand All @@ -177,20 +177,20 @@ func putLocationsInParams(
}
}

assignLocationsInOrder(*params, locations)
for i := range *params {
correctStructLocations(&(*params)[i], fieldLocations)
assignLocationsInOrder(params, locations)
for i := range params {
correctStructLocations(params[i], fieldLocations)
}
funcMap[funcName] = params
}

func assignLocationsInOrder(params []ditypes.Parameter, locations []ditypes.Location) {
func assignLocationsInOrder(params []*ditypes.Parameter, locations []ditypes.Location) {
stack := []*ditypes.Parameter{}
locationCounter := 0

// Start by pushing addresses of all parameters to stack
for i := range params {
stack = append(stack, &params[len(params)-1-i])
stack = append(stack, params[len(params)-1-i])
}

for {
Expand All @@ -203,7 +203,7 @@ func assignLocationsInOrder(params []ditypes.Parameter, locations []ditypes.Loca
current.Kind != uint(reflect.Array) &&
current.Kind != uint(reflect.Pointer) {
for i := range current.ParameterPieces {
stack = append(stack, &current.ParameterPieces[len(current.ParameterPieces)-1-i])
stack = append(stack, current.ParameterPieces[len(current.ParameterPieces)-1-i])
}
} else {
// Location fields are directly assigned instead of setting the whole
Expand All @@ -229,7 +229,7 @@ func correctStructLocations(structParam *ditypes.Parameter, fieldLocations map[b
}
offset := fieldLocations[fieldID]
structParam.ParameterPieces[i].FieldOffset = offset
correctStructLocations(&structParam.ParameterPieces[i], fieldLocations)
correctStructLocations(structParam.ParameterPieces[i], fieldLocations)
}
}

Expand Down
48 changes: 24 additions & 24 deletions pkg/dynamicinstrumentation/diconfig/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func loadFunctionDefinitions(dwarfData *dwarf.Data, targetFunctions map[string]b
var funcName string

var result = ditypes.TypeMap{
Functions: make(map[string]*[]ditypes.Parameter),
Functions: make(map[string][]*ditypes.Parameter),
InlinedFunctions: make(map[uint64][]*dwarf.Entry),
}

Expand Down Expand Up @@ -117,8 +117,8 @@ entryLoop:
if !targetFunctions[funcName] {
continue entryLoop
}
params := make([]ditypes.Parameter, 0)
result.Functions[funcName] = &params
params := make([]*ditypes.Parameter, 0)
result.Functions[funcName] = params
readingAFunction = true
continue entryLoop
}
Expand Down Expand Up @@ -165,7 +165,7 @@ entryLoop:
if typeFields != nil {
// We've collected information about this ditypes.Parameter, append it to the slice of ditypes.Parameters for this function
typeFields.Name = name
*result.Functions[funcName] = append((*result.Functions[funcName]), *typeFields)
result.Functions[funcName] = append(result.Functions[funcName], typeFields)
}
seenTypes = make(map[string]*seenTypeCounter) // reset seen types map for next parameter
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func getSliceField(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Parame
return []ditypes.Parameter{sliceParameter}, nil
}

func getIndividualArrayElements(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Parameter, error) {
func getIndividualArrayElements(offset dwarf.Offset, dwarfData *dwarf.Data) ([]*ditypes.Parameter, error) {
savedArrayEntryOffset := offset
typeReader := dwarfData.Reader()

Expand Down Expand Up @@ -361,25 +361,25 @@ func getIndividualArrayElements(offset dwarf.Offset, dwarfData *dwarf.Data) ([]d
}
}

arrayElements := []ditypes.Parameter{}
arrayElements := []*ditypes.Parameter{}
for h := 0; h < int(arrayLength); h++ {
newParam := ditypes.Parameter{}
copyTree(&newParam.ParameterPieces, &elementFields.ParameterPieces)
newParam.Name = fmt.Sprintf("[%d]%s[%d]", arrayLength, elementTypeName, h)
newParam.Type = elementTypeName
newParam.Kind = elementTypeKind
newParam.TotalSize = elementTypeSize
arrayElements = append(arrayElements, newParam)
arrayElements = append(arrayElements, &newParam)
}

return arrayElements, nil
}

func getStructFields(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Parameter, error) {
func getStructFields(offset dwarf.Offset, dwarfData *dwarf.Data) ([]*ditypes.Parameter, error) {
inOrderReader := dwarfData.Reader()
typeReader := dwarfData.Reader()

structFields := []ditypes.Parameter{}
structFields := []*ditypes.Parameter{}
fieldEntry := &dwarf.Entry{}

// Start at the entry of the definition of the struct
Expand All @@ -394,14 +394,14 @@ func getStructFields(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Para
for {
fieldEntry, err = inOrderReader.Next()
if err != nil {
return []ditypes.Parameter{}, err
return []*ditypes.Parameter{}, err
}

if entryIsEmpty(fieldEntry) || fieldEntry.Tag != dwarf.TagMember {
break
}

newStructField := ditypes.Parameter{}
newStructField := &ditypes.Parameter{}

for i := range fieldEntry.Field {

Expand All @@ -415,30 +415,30 @@ func getStructFields(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Para
typeReader.Seek(fieldEntry.Field[i].Val.(dwarf.Offset))
typeEntry, err := typeReader.Next()
if err != nil {
return []ditypes.Parameter{}, err
return []*ditypes.Parameter{}, err
}

if !entryTypeIsSupported(typeEntry) {
unsupportedType := resolveUnsupportedEntry(typeEntry)
structFields = append(structFields, *unsupportedType)
structFields = append(structFields, unsupportedType)
continue
}

if typeEntry.Tag == dwarf.TagTypedef {
typeEntry, err = resolveTypedefToRealType(typeEntry, typeReader)
if err != nil {
return []ditypes.Parameter{}, err
return []*ditypes.Parameter{}, err
}
}

newStructField.Type, newStructField.TotalSize, newStructField.Kind = getTypeEntryBasicInfo(typeEntry)
if typeEntry.Tag != dwarf.TagBaseType {
field, err := expandTypeData(typeEntry.Offset, dwarfData)
if err != nil {
return []ditypes.Parameter{}, err
return []*ditypes.Parameter{}, err
}
field.Name = newStructField.Name
structFields = append(structFields, *field)
structFields = append(structFields, field)
} else {
structFields = append(structFields, newStructField)
}
Expand All @@ -448,7 +448,7 @@ func getStructFields(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Para
return structFields, nil
}

func getPointerLayers(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Parameter, error) {
func getPointerLayers(offset dwarf.Offset, dwarfData *dwarf.Data) ([]*ditypes.Parameter, error) {
typeReader := dwarfData.Reader()
typeReader.Seek(offset)
pointerEntry, err := typeReader.Next()
Expand All @@ -472,9 +472,9 @@ func getPointerLayers(offset dwarf.Offset, dwarfData *dwarf.Data) ([]ditypes.Par
}
}
if underlyingType == nil {
return []ditypes.Parameter{}, nil
return []*ditypes.Parameter{}, nil
}
return []ditypes.Parameter{*underlyingType}, nil
return []*ditypes.Parameter{underlyingType}, nil
}

// Can use `Children` field, but there's also always a NULL/empty entry at the end of entry trees.
Expand Down Expand Up @@ -551,9 +551,9 @@ func resolveTypedefToRealType(outerType *dwarf.Entry, reader *dwarf.Reader) (*dw
return outerType, nil
}

func correctStructSizes(params []ditypes.Parameter) {
func correctStructSizes(params []*ditypes.Parameter) {
for i := range params {
correctStructSize(&params[i])
correctStructSize(params[i])
}
}

Expand All @@ -566,15 +566,15 @@ func correctStructSize(param *ditypes.Parameter) {
return
}
for i := range param.ParameterPieces {
correctStructSize(&param.ParameterPieces[i])
correctStructSize(param.ParameterPieces[i])
}
}

func copyTree(dst, src *[]ditypes.Parameter) {
func copyTree(dst, src *[]*ditypes.Parameter) {
if dst == nil || src == nil || len(*src) == 0 {
return
}
*dst = make([]ditypes.Parameter, len(*src))
*dst = make([]*ditypes.Parameter, len(*src))
copy(*dst, *src)
for i := range *src {
copyTree(&((*dst)[i].ParameterPieces), &((*src)[i].ParameterPieces))
Expand Down
Loading

0 comments on commit b3edbd7

Please sign in to comment.