Skip to content

Commit

Permalink
refactor: improve error handling resolver and syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
f1zm0 committed Apr 24, 2023
1 parent 6da875b commit 2d2fb24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
18 changes: 11 additions & 7 deletions acheron.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ func (a *Acheron) HashString(s string) uint64 {

// GetSyscall returns the Syscall struct for the given function hash.
func (a *Acheron) GetSyscall(fnHash uint64) (*resolver.Syscall, error) {
return a.resolver.GetSyscall(fnHash)
if sys := a.resolver.GetSyscall(fnHash); sys == nil {
return nil, fmt.Errorf("failed with code: %d", ErrSyscallNotFound)
} else {
return sys, nil
}
}

// Syscall executes an indirect syscall with the given function hash and arguments. Returns the error code if it fails.
func (a *Acheron) Syscall(fnHash uint64, args ...uintptr) error {
sys, err := a.resolver.GetSyscall(fnHash)
if err != nil {
return err
}
if errCode := execIndirectSyscall(sys.SSN, sys.TrampolineAddr, args...); errCode < 0 { // !NT_SUCCESS
return fmt.Errorf("syscall failed with error code %d", errCode)
if sys := a.resolver.GetSyscall(fnHash); sys == nil {
return fmt.Errorf("failed with: %d", ErrSyscallNotFound)
} else {
if st := execIndirectSyscall(sys.SSN, sys.TrampolineAddr, args...); !NT_SUCCESS(st) {
return fmt.Errorf("failed with: %d", st)
}
}
return nil
}
2 changes: 1 addition & 1 deletion internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package resolver

type Resolver interface {
// GetSyscallSSN returns the syscall SSN.
GetSyscall(funcNameHash uint64) (*Syscall, error)
GetSyscall(funcNameHash uint64) *Syscall
}
9 changes: 4 additions & 5 deletions internal/resolver/rvasort/resolver.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rvasort

import (
"errors"
"fmt"
"sort"

"github.com/f1zm0/acheron/internal/resolver"
Expand Down Expand Up @@ -51,9 +49,10 @@ func NewResolver(h hashing.HashFunction) (resolver.Resolver, error) {
return r, nil
}

func (r *ssnSortResolver) GetSyscall(fnHash uint64) (*resolver.Syscall, error) {
// GetSyscall returns the Syscall struct for the given function hash, or nil if not found.
func (r *ssnSortResolver) GetSyscall(fnHash uint64) *resolver.Syscall {
if v, ok := r.syscallStubs[fnHash]; ok {
return v, nil
return v
}
return nil, errors.New(fmt.Sprintf("syscall with hash %d not found", fnHash))
return nil
}

0 comments on commit 2d2fb24

Please sign in to comment.