Skip to content

Commit

Permalink
refactor!: remove direct syscall support
Browse files Browse the repository at this point in the history
  • Loading branch information
f1zm0 committed Apr 24, 2023
1 parent 887c346 commit 919ad92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 106 deletions.
22 changes: 22 additions & 0 deletions acheron.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package acheron

import (
"fmt"

"github.com/f1zm0/acheron/internal/resolver"
"github.com/f1zm0/acheron/internal/resolver/rvasort"
"github.com/f1zm0/acheron/pkg/hashing"
Expand All @@ -19,6 +21,9 @@ type options struct {
hashFunction hashing.HashFunction
}

// stub for asm implementation
func execIndirectSyscall(ssn uint16, gateAddr uintptr, argh ...uintptr) (errcode uint32)

// New returns a new Acheron instance with the given options, or an error if initialization fails.
func New(opts ...Option) (*Acheron, error) {
options := &options{
Expand Down Expand Up @@ -49,3 +54,20 @@ func WithHashFunction(f hashing.HashFunction) Option {
func (a *Acheron) HashString(s string) uint64 {
return a.hashFunction([]byte(s))
}

// GetSyscall returns the Syscall struct for the given function hash.
func (a *Acheron) GetSyscall(fnHash uint64) (*resolver.Syscall, error) {
return a.resolver.GetSyscall(fnHash)
}

// 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)
}
return nil
}
73 changes: 1 addition & 72 deletions syscall_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


// func execIndirectSyscall(ssn uint16, trampoline uintptr, argh ...uintptr) (errcode uint32)
TEXT ·execIndirectSyscall(SB),NOSPLIT, $0-32
TEXT ·execIndirectSyscall(SB),NOSPLIT, $0-144
XORQ AX, AX
MOVW ssn+0(FP), AX

Expand Down Expand Up @@ -81,74 +81,3 @@ jumpcall:
POPQ CX
MOVL AX, errcode+40(FP)
RET


// func execDirectSyscall(ssn uint16, trampoline uintptr, argh ...uintptr) (errcode uint32)
TEXT ·execDirectSyscall(SB),NOSPLIT, $0-32
XORQ AX, AX
MOVW ssn+0(FP), AX

PUSHQ CX

//put variadic pointer into SI
MOVQ argh_base+16(FP),SI

//put variadic size into CX
MOVQ argh_len+24(FP),CX

// SetLastError(0).
MOVQ 0x30(GS), DI
MOVL $0, 0x68(DI)

// room for args
SUBQ $(maxargs*8), SP

//no parameters, special case
CMPL CX, $0
JLE jumpcall

// Fast version, do not store args on the stack.
CMPL CX, $4
JLE loadregs

// Check we have enough room for args.
CMPL CX, $maxargs
JLE 2(PC)

// not enough room -> crash
INT $3

// Copy args to the stack.
MOVQ SP, DI
CLD
REP; MOVSQ
MOVQ SP, SI

loadregs:

// Load first 4 args into correspondent registers.
MOVQ 0(SI), CX
MOVQ 8(SI), DX
MOVQ 16(SI), R8
MOVQ 24(SI), R9

// Floating point arguments are passed in the XMM registers
// Set them here in case any of the arguments are floating point values.
// For details see: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
MOVQ CX, X0
MOVQ DX, X1
MOVQ R8, X2
MOVQ R9, X3

jumpcall:
MOVQ CX, R10

// direcy syscall
SYSCALL

ADDQ $((maxargs)*8), SP

// Return result
POPQ CX
MOVL AX, errcode+40(FP)
RET
34 changes: 0 additions & 34 deletions syscalls.go

This file was deleted.

0 comments on commit 919ad92

Please sign in to comment.