Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #1 fix (GetSyscallFnNameWithSymFile memory leak) #2

Merged
merged 1 commit into from
Mar 20, 2022
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
18 changes: 9 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ func GetSyscallFnNameWithSymFile(name string, symFile string) (string, error) {
if err != nil {
return "", err
}
// copy to avoid memory leak due to go subslice
// see: https://go101.org/article/memory-leaking.html
var b strings.Builder
b.WriteString(syscallName)
syscallName = b.String()

syscallPrefix = strings.TrimSuffix(syscallName, "open")
}
Expand Down Expand Up @@ -143,12 +138,14 @@ func getSyscallFnNameWithKallsyms(name string, kallsymsContent string) (string,
default:
arch = "x64"
}
var b strings.Builder

regexStr := `(\b` + name + `\b)`
fnRegex := regexp.MustCompile(regexStr)
match := fnRegex.FindAllString(kallsymsContent, -1)
if len(match) > 0 {
return match[0], nil
b.WriteString(match[0])
return b.String(), nil
}

// We should search for new syscall function like "__x64__sys_open"
Expand All @@ -158,7 +155,8 @@ func getSyscallFnNameWithKallsyms(name string, kallsymsContent string) (string,

match = fnRegex.FindAllString(kallsymsContent, -1)
if len(match) > 0 {
return match[0], nil
b.WriteString(match[0])
return b.String(), nil
}

// If nothing found, search for old syscall function to be sure
Expand All @@ -168,7 +166,8 @@ func getSyscallFnNameWithKallsyms(name string, kallsymsContent string) (string,
// If we get something like 'sys_open' or 'SyS_open', return
// either (they have same addr) else, just return original string
if len(match) > 0 {
return match[0], nil
b.WriteString(match[0])
return b.String(), nil
}

// check for '__' prefixed functions, like '__sys_open'
Expand All @@ -178,7 +177,8 @@ func getSyscallFnNameWithKallsyms(name string, kallsymsContent string) (string,
// If we get something like '__sys_open' or '__SyS_open', return
// either (they have same addr) else, just return original string
if len(match) > 0 {
return match[0], nil
b.WriteString(match[0])
return b.String(), nil
}

return "", errors.New("could not find a valid syscall name")
Expand Down