Skip to content

Commit

Permalink
feat(parse): envelope different OS GUP flags parsing
Browse files Browse the repository at this point in the history
GUP flags are changed between different OS versions.
Create parsing functions to envelope the different type implemented to
parse the flags for each version.
  • Loading branch information
AlonZivony committed Nov 26, 2023
1 parent b235fb5 commit 4be87a8
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions helpers/argumentParsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"strconv"
"strings"
"sync/atomic"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -3158,6 +3159,67 @@ func ParseLegacyGUPFlags(rawValue uint64) LegacyGUPFlag {
return LegacyGUPFlag{stringValue: strings.Join(f, "|"), rawValue: uint32(rawValue)}
}

var currentOSGUPFlagsParse uint32
var skipDetermineGUPFlagsFunc uint32

const gupFlagsChangeVersion = "6.3.0"

// ParseGUPFlagsCurrentOS parse the GUP flags received according to current machine OS version.
// It uses optimizations to perform better than ParseGUPFlagsForOS
func ParseGUPFlagsCurrentOS(rawValue uint64) (SystemFunctionArgument, error) {
const (
newVersionsParsing = iota
legacyParsing
)
if atomic.LoadUint32(&skipDetermineGUPFlagsFunc) == 0 {
osInfo, err := GetOSInfo()
if err != nil {
return nil, fmt.Errorf("error getting current OS info - %s", err)
}
compare, err := osInfo.CompareOSBaseKernelRelease(gupFlagsChangeVersion)
if err != nil {
return nil, fmt.Errorf(
"error comparing OS versions to determine how to parse GUP flags - %s",
err,
)
}
if compare == KernelVersionOlder {
atomic.StoreUint32(&currentOSGUPFlagsParse, legacyParsing)
} else {
atomic.StoreUint32(&currentOSGUPFlagsParse, newVersionsParsing)
}
// Avoid doing this check in the future
atomic.StoreUint32(&skipDetermineGUPFlagsFunc, 1)
}

// Don't really need to use atomics here, as the value is only used here
// and is set in an atomic way
switch currentOSGUPFlagsParse {
case legacyParsing:
return ParseLegacyGUPFlags(rawValue), nil
case newVersionsParsing:
return ParseGUPFlags(rawValue), nil
default:
return nil, fmt.Errorf("no parsing function for GUP flags was found to this OD version")
}
}

// ParseGUPFlagsForOS parse the GUP flags received according to given OS version.
func ParseGUPFlagsForOS(osInfo *OSInfo, rawValue uint64) (SystemFunctionArgument, error) {
compare, err := osInfo.CompareOSBaseKernelRelease(gupFlagsChangeVersion)
if err != nil {
return nil, fmt.Errorf(
"error comparing OS versions to determine how to parse GUP flags - %s",
err,
)
}

if compare == KernelVersionOlder {
return ParseLegacyGUPFlags(rawValue), nil
}
return ParseGUPFlags(rawValue), nil
}

// =====================================================

// VmFlag represents the flags in the `vm_area_struct` in x86 64bit architecture
Expand Down

0 comments on commit 4be87a8

Please sign in to comment.