Skip to content

Commit

Permalink
WIP: Use Sizeof as the target is not always an Int (will squash)
Browse files Browse the repository at this point in the history
Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed Mar 8, 2022
1 parent 7ec3649 commit 303507e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions internal/btf/core.go
Expand Up @@ -651,9 +651,10 @@ func coreFindField(local Type, localAcc coreAccessor, target Type) (coreField, c
break
}

targetInt, ok := target.(*Int)
if !ok {
return coreField{}, coreField{}, fmt.Errorf("target not int: %w", errImpossibleRelocation)
targetSize, err := Sizeof(target)
if err != nil {
return coreField{}, coreField{},
fmt.Errorf("could not get target size: %w", err)
}

targetBitfieldOffset := targetOffset
Expand All @@ -666,21 +667,21 @@ func coreFindField(local Type, localAcc coreAccessor, target Type) (coreField, c
// 1) convert the bit offset to bytes with a flooring division, yielding "byte" aligned offset.
// 2) dividing and multiplying that offset by the load size, yielding the target load size aligned offset.
targetBitfieldSize = targetMember.BitfieldSize
targetOffset = 8 * (targetOffset / 8 / targetInt.Size * targetInt.Size)
targetOffset = 8 * (targetOffset / 8 / uint32(targetSize) * uint32(targetSize))

// As sanity check, verify that the bitfield is captured by the chosen load. This should only happen
// if one of the two assumptions are broken: the bitfield size is smaller than the type of the variable
// and that the loads are aligned.
if targetOffset > targetBitfieldOffset ||
targetOffset+targetInt.Size*8 < targetBitfieldOffset+targetMember.BitfieldSize {
targetOffset+uint32(targetSize)*8 < targetBitfieldOffset+targetMember.BitfieldSize {
return coreField{}, coreField{},
fmt.Errorf("could not find load for bitfield: load of %d bytes at %d does not capture bitfield of size %d at %d",
targetInt.Size, targetOffset, targetMember.BitfieldSize, targetBitfieldOffset)
targetSize, targetOffset, targetMember.BitfieldSize, targetBitfieldOffset)
}
} else {
// Going from a bitfield to a normal field. Since the original BTF had it as a bitfield, we'll
// need to "emulate" a bitfield in target to compute the shifts correctly.
targetBitfieldSize = targetInt.Size
targetBitfieldSize = uint32(targetSize)
}

if err := coreAreMembersCompatible(local, target); err != nil {
Expand Down

0 comments on commit 303507e

Please sign in to comment.