arch

package
v0.0.0-...-a858404 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: Apache-2.0, MIT Imports: 22 Imported by: 0

Documentation

Overview

Package arch provides abstractions around architecture-dependent details, such as syscall calling conventions, native types, etc.

Index

Constants

View Source
const Host = AMD64

Host specifies the host architecture.

View Source
const StackBottomMagic = ^hostarch.Addr(0) // hostarch.Addr(-1)

StackBottomMagic is the special address callers must past to all stack marshalling operations to cause the src/dst address to be computed based on the current end of the stack.

View Source
const (
	// SyscallWidth is the width of syscall, sysenter, and int 80 insturctions.
	SyscallWidth = 2
)

System-related constants for x86.

Variables

View Source
var (
	// TrapInstruction is the x86 trap instruction.
	TrapInstruction = [1]byte{0xcc}

	// CPUIDInstruction is the x86 CPUID instruction.
	CPUIDInstruction = [2]byte{0xf, 0xa2}

	// X86TrapFlag is an exported const for use by other packages.
	X86TrapFlag uint64 = (1 << 8)
)

Functions

This section is empty.

Types

type Arch

type Arch int

Arch describes an architecture.

const (
	// AMD64 is the x86-64 architecture.
	AMD64 Arch = iota
	// ARM64 is the aarch64 architecture.
	ARM64
)

func (Arch) String

func (a Arch) String() string

String implements fmt.Stringer.

type AuxEntry

type AuxEntry struct {
	Key   uint64
	Value hostarch.Addr
}

An AuxEntry represents an entry in an ELF auxiliary vector.

+stateify savable

func (*AuxEntry) StateFields

func (a *AuxEntry) StateFields() []string

func (*AuxEntry) StateLoad

func (a *AuxEntry) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*AuxEntry) StateSave

func (a *AuxEntry) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*AuxEntry) StateTypeName

func (a *AuxEntry) StateTypeName() string

type Auxv

type Auxv []AuxEntry

An Auxv represents an ELF auxiliary vector.

type Context64

type Context64 struct {
	State
}

Context64 represents an AMD64 context.

+stateify savable

func New

func New(arch Arch) *Context64

New returns a new architecture context.

func (*Context64) Arch

func (c *Context64) Arch() Arch

Arch implements Context.Arch.

func (*Context64) FloatingPointData

func (c *Context64) FloatingPointData() *fpu.State

FloatingPointData returns the state of the floating-point unit.

func (*Context64) Fork

func (c *Context64) Fork() *Context64

Fork returns an exact copy of this context.

func (*Context64) IP

func (c *Context64) IP() uintptr

IP returns the current instruction pointer.

func (*Context64) Native

func (c *Context64) Native(val uintptr) marshal.Marshallable

Native returns the native type for the given val.

func (*Context64) NewMmapLayout

func (c *Context64) NewMmapLayout(min, max hostarch.Addr, r *limits.LimitSet) (MmapLayout, error)

NewMmapLayout implements Context.NewMmapLayout consistently with Linux.

func (*Context64) PIELoadAddress

func (c *Context64) PIELoadAddress(l MmapLayout) hostarch.Addr

PIELoadAddress implements Context.PIELoadAddress.

func (*Context64) PtracePeekUser

func (c *Context64) PtracePeekUser(addr uintptr) (marshal.Marshallable, error)

PtracePeekUser implements Context.PtracePeekUser.

func (*Context64) PtracePokeUser

func (c *Context64) PtracePokeUser(addr, data uintptr) error

PtracePokeUser implements Context.PtracePokeUser.

func (*Context64) RestartSyscall

func (c *Context64) RestartSyscall()

RestartSyscall implements Context.RestartSyscall.

func (*Context64) RestartSyscallWithRestartBlock

func (c *Context64) RestartSyscallWithRestartBlock()

RestartSyscallWithRestartBlock implements Context.RestartSyscallWithRestartBlock.

func (*Context64) Return

func (c *Context64) Return() uintptr

Return returns the current syscall return value.

func (*Context64) SetIP

func (c *Context64) SetIP(value uintptr)

SetIP sets the current instruction pointer.

func (*Context64) SetOldRSeqInterruptedIP

func (c *Context64) SetOldRSeqInterruptedIP(value uintptr)

SetOldRSeqInterruptedIP implements Context.SetOldRSeqInterruptedIP.

func (*Context64) SetReturn

func (c *Context64) SetReturn(value uintptr)

SetReturn sets the syscall return value.

func (*Context64) SetStack

func (c *Context64) SetStack(value uintptr)

SetStack sets the current stack pointer.

func (*Context64) SetTLS

func (c *Context64) SetTLS(value uintptr) bool

SetTLS sets the current TLS pointer. Returns false if value is invalid.

func (*Context64) SignalRestore

func (c *Context64) SignalRestore(st *Stack, rt bool, featureSet cpuid.FeatureSet) (linux.SignalSet, linux.SignalStack, error)

SignalRestore implements Context.SignalRestore. (Compare to Linux's arch/x86/kernel/signal.c:sys_rt_sigreturn().)

func (*Context64) SignalSetup

func (c *Context64) SignalSetup(st *Stack, act *linux.SigAction, info *linux.SignalInfo, alt *linux.SignalStack, sigset linux.SignalSet, featureSet cpuid.FeatureSet) error

SignalSetup implements Context.SignalSetup. (Compare to Linux's arch/x86/kernel/signal.c:__setup_rt_frame().)

func (*Context64) Stack

func (c *Context64) Stack() uintptr

Stack returns the current stack pointer.

func (*Context64) StateFields

func (c *Context64) StateFields() []string

func (*Context64) StateLoad

func (c *Context64) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*Context64) StateSave

func (c *Context64) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*Context64) StateTypeName

func (c *Context64) StateTypeName() string

func (*Context64) SyscallArgs

func (c *Context64) SyscallArgs() SyscallArguments

SyscallArgs provides syscall arguments according to the 64-bit convention.

Due to the way addresses are mapped for the sentry this binary *must* be built in 64-bit mode. So we can just assume the syscall numbers that come back match the expected host system call numbers.

func (*Context64) SyscallNo

func (c *Context64) SyscallNo() uintptr

SyscallNo returns the syscall number according to the 64-bit convention.

func (*Context64) SyscallSaveOrig

func (c *Context64) SyscallSaveOrig()

SyscallSaveOrig save the value of the register which is clobbered in syscall handler(doSyscall()).

Noop on x86.

func (*Context64) TLS

func (c *Context64) TLS() uintptr

TLS returns the current TLS pointer.

func (*Context64) Value

func (c *Context64) Value(val marshal.Marshallable) uintptr

Value returns the generic val for the given native type.

func (*Context64) Width

func (c *Context64) Width() uint

Width returns the byte width of this architecture.

type MmapDirection

type MmapDirection int

MmapDirection is a search direction for mmaps.

const (
	// MmapBottomUp instructs mmap to prefer lower addresses.
	MmapBottomUp MmapDirection = iota

	// MmapTopDown instructs mmap to prefer higher addresses.
	MmapTopDown
)

type MmapLayout

type MmapLayout struct {
	// MinAddr is the lowest mappable address.
	MinAddr hostarch.Addr

	// MaxAddr is the highest mappable address.
	MaxAddr hostarch.Addr

	// BottomUpBase is the lowest address that may be returned for a
	// MmapBottomUp mmap.
	BottomUpBase hostarch.Addr

	// TopDownBase is the highest address that may be returned for a
	// MmapTopDown mmap.
	TopDownBase hostarch.Addr

	// DefaultDirection is the direction for most non-fixed mmaps in this
	// layout.
	DefaultDirection MmapDirection

	// MaxStackRand is the maximum randomization to apply to stack
	// allocations to maintain a proper gap between the stack and
	// TopDownBase.
	MaxStackRand uint64
}

MmapLayout defines the layout of the user address space for a particular MemoryManager.

Note that "highest address" below is always exclusive.

+stateify savable

func (*MmapLayout) StateFields

func (m *MmapLayout) StateFields() []string

func (*MmapLayout) StateLoad

func (m *MmapLayout) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*MmapLayout) StateSave

func (m *MmapLayout) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*MmapLayout) StateTypeName

func (m *MmapLayout) StateTypeName() string

func (*MmapLayout) Valid

func (m *MmapLayout) Valid() bool

Valid returns true if this layout is valid.

type Registers

type Registers struct {
	linux.PtraceRegs
}

Registers represents the CPU registers for this architecture.

+stateify savable

func (*Registers) StateFields

func (r *Registers) StateFields() []string

func (*Registers) StateLoad

func (r *Registers) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*Registers) StateSave

func (r *Registers) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*Registers) StateTypeName

func (r *Registers) StateTypeName() string

type SignalContext64

type SignalContext64 struct {
	R8      uint64
	R9      uint64
	R10     uint64
	R11     uint64
	R12     uint64
	R13     uint64
	R14     uint64
	R15     uint64
	Rdi     uint64
	Rsi     uint64
	Rbp     uint64
	Rbx     uint64
	Rdx     uint64
	Rax     uint64
	Rcx     uint64
	Rsp     uint64
	Rip     uint64
	Eflags  uint64
	Cs      uint16
	Gs      uint16 // always 0 on amd64.
	Fs      uint16 // always 0 on amd64.
	Ss      uint16 // only restored if _UC_STRICT_RESTORE_SS (unsupported).
	Err     uint64
	Trapno  uint64
	Oldmask linux.SignalSet
	Cr2     uint64
	// Pointer to a struct _fpstate.
	Fpstate  uint64
	Reserved [8]uint64
}

SignalContext64 is equivalent to struct sigcontext, the type passed as the second argument to signal handlers set by signal(2).

+marshal

func (*SignalContext64) CopyIn

func (s *SignalContext64) CopyIn(cc marshal.CopyContext, addr hostarch.Addr) (int, error)

CopyIn implements marshal.Marshallable.CopyIn.

func (*SignalContext64) CopyOut

func (s *SignalContext64) CopyOut(cc marshal.CopyContext, addr hostarch.Addr) (int, error)

CopyOut implements marshal.Marshallable.CopyOut.

func (*SignalContext64) CopyOutN

func (s *SignalContext64) CopyOutN(cc marshal.CopyContext, addr hostarch.Addr, limit int) (int, error)

CopyOutN implements marshal.Marshallable.CopyOutN.

func (*SignalContext64) MarshalBytes

func (s *SignalContext64) MarshalBytes(dst []byte) []byte

MarshalBytes implements marshal.Marshallable.MarshalBytes.

func (*SignalContext64) MarshalUnsafe

func (s *SignalContext64) MarshalUnsafe(dst []byte) []byte

MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe.

func (*SignalContext64) Packed

func (s *SignalContext64) Packed() bool

Packed implements marshal.Marshallable.Packed.

func (*SignalContext64) SizeBytes

func (s *SignalContext64) SizeBytes() int

SizeBytes implements marshal.Marshallable.SizeBytes.

func (*SignalContext64) UnmarshalBytes

func (s *SignalContext64) UnmarshalBytes(src []byte) []byte

UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.

func (*SignalContext64) UnmarshalUnsafe

func (s *SignalContext64) UnmarshalUnsafe(src []byte) []byte

UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe.

func (*SignalContext64) WriteTo

func (s *SignalContext64) WriteTo(writer io.Writer) (int64, error)

WriteTo implements io.WriterTo.WriteTo.

type Stack

type Stack struct {
	// Our arch info.
	// We use this for automatic Native conversion of hostarch.Addrs during
	// Push() and Pop().
	Arch *Context64

	// The interface used to actually copy user memory.
	IO usermem.IO

	// Our current stack bottom.
	Bottom hostarch.Addr
	// contains filtered or unexported fields
}

Stack is a simple wrapper around a hostarch.IO and an address. Stack implements marshal.CopyContext, and marshallable values can be pushed or popped from the stack through the marshal.Marshallable interface.

Stack is not thread-safe.

func (*Stack) Align

func (s *Stack) Align(offset int)

Align aligns the stack to the given offset.

func (*Stack) CopyInBytes

func (s *Stack) CopyInBytes(sentinel hostarch.Addr, b []byte) (int, error)

CopyInBytes implements marshal.CopyContext.CopyInBytes. CopyInBytes computes an appropriate address based on the current end of the stack. Callers must use the sentinel address StackBottomMagic to marshal methods to indicate this.

func (*Stack) CopyOutBytes

func (s *Stack) CopyOutBytes(sentinel hostarch.Addr, b []byte) (int, error)

CopyOutBytes implements marshal.CopyContext.CopyOutBytes. CopyOutBytes computes an appropriate address based on the current end of the stack. Callers use the sentinel address StackBottomMagic to marshal methods to indicate this.

func (*Stack) CopyScratchBuffer

func (s *Stack) CopyScratchBuffer(size int) []byte

CopyScratchBuffer implements marshal.CopyContext.CopyScratchBuffer.

func (*Stack) Load

func (s *Stack) Load(args []string, env []string, aux Auxv) (StackLayout, error)

Load pushes the given args, env and aux vector to the stack using the well-known format for a new executable. It returns the start and end of the argument and environment vectors.

func (*Stack) PushNullTerminatedByteSlice

func (s *Stack) PushNullTerminatedByteSlice(bs []byte) (int, error)

PushNullTerminatedByteSlice writes bs to the stack, followed by an extra null byte at the end. On error, the contents of the stack and the bottom cursor are undefined.

type StackLayout

type StackLayout struct {
	// ArgvStart is the beginning of the argument vector.
	ArgvStart hostarch.Addr

	// ArgvEnd is the end of the argument vector.
	ArgvEnd hostarch.Addr

	// EnvvStart is the beginning of the environment vector.
	EnvvStart hostarch.Addr

	// EnvvEnd is the end of the environment vector.
	EnvvEnd hostarch.Addr
}

StackLayout describes the location of the arguments and environment on the stack.

type State

type State struct {
	// The system registers.
	Regs Registers
	// contains filtered or unexported fields
}

State contains the common architecture bits for X86 (the build tag of this file ensures it's only built on x86).

+stateify savable

func (*State) ClearSingleStep

func (s *State) ClearSingleStep()

ClearSingleStep enables single stepping.

func (*State) Fork

func (s *State) Fork() State

Fork creates and returns an identical copy of the state.

func (*State) FullRestore

func (s *State) FullRestore() bool

FullRestore indicates whether a full restore is required.

func (State) Proto

func (s State) Proto() *rpb.Registers

Proto returns a protobuf representation of the system registers in State.

func (*State) PtraceGetRegSet

func (s *State) PtraceGetRegSet(regset uintptr, dst io.Writer, maxlen int, fs cpuid.FeatureSet) (int, error)

PtraceGetRegSet implements Context.PtraceGetRegSet.

func (*State) PtraceGetRegs

func (s *State) PtraceGetRegs(dst io.Writer) (int, error)

PtraceGetRegs implements Context.PtraceGetRegs.

func (*State) PtraceSetRegSet

func (s *State) PtraceSetRegSet(regset uintptr, src io.Reader, maxlen int, fs cpuid.FeatureSet) (int, error)

PtraceSetRegSet implements Context.PtraceSetRegSet.

func (*State) PtraceSetRegs

func (s *State) PtraceSetRegs(src io.Reader) (int, error)

PtraceSetRegs implements Context.PtraceSetRegs.

func (*State) RegisterMap

func (s *State) RegisterMap() (map[string]uintptr, error)

RegisterMap returns a map of all registers.

func (*State) SetSingleStep

func (s *State) SetSingleStep()

SetSingleStep enables single stepping.

func (*State) SingleStep

func (s *State) SingleStep() bool

SingleStep implements Context.SingleStep.

func (*State) StateData

func (s *State) StateData() *State

StateData implements Context.StateData.

func (*State) StateFields

func (s *State) StateFields() []string

func (*State) StateLoad

func (s *State) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*State) StateSave

func (s *State) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*State) StateTypeName

func (s *State) StateTypeName() string

type SyscallArgument

type SyscallArgument struct {
	// Prefer to use accessor methods instead of 'Value' directly.
	Value uintptr
}

SyscallArgument is an argument supplied to a syscall implementation. The methods used to access the arguments are named after the ***C type name*** and they convert to the closest Go type available. For example, Int() refers to a 32-bit signed integer argument represented in Go as an int32.

Using the accessor methods guarantees that the conversion between types is correct, taking into account size and signedness (i.e., zero-extension vs signed-extension).

func (SyscallArgument) Int

func (a SyscallArgument) Int() int32

Int returns the int32 representation of a 32-bit signed integer argument.

func (SyscallArgument) Int64

func (a SyscallArgument) Int64() int64

Int64 returns the int64 representation of a 64-bit signed integer argument.

func (SyscallArgument) ModeT

func (a SyscallArgument) ModeT() uint

ModeT returns the int representation of a mode_t argument.

func (SyscallArgument) Pointer

func (a SyscallArgument) Pointer() hostarch.Addr

Pointer returns the hostarch.Addr representation of a pointer argument.

func (SyscallArgument) SizeT

func (a SyscallArgument) SizeT() uint

SizeT returns the uint representation of a size_t argument.

func (SyscallArgument) Uint

func (a SyscallArgument) Uint() uint32

Uint returns the uint32 representation of a 32-bit unsigned integer argument.

func (SyscallArgument) Uint64

func (a SyscallArgument) Uint64() uint64

Uint64 returns the uint64 representation of a 64-bit unsigned integer argument.

type SyscallArguments

type SyscallArguments [6]SyscallArgument

SyscallArguments represents the set of arguments passed to a syscall.

type UContext64

type UContext64 struct {
	Flags    uint64
	Link     uint64
	Stack    linux.SignalStack
	MContext SignalContext64
	Sigset   linux.SignalSet
}

UContext64 is equivalent to ucontext_t on 64-bit x86.

+marshal

func (*UContext64) CopyIn

func (u *UContext64) CopyIn(cc marshal.CopyContext, addr hostarch.Addr) (int, error)

CopyIn implements marshal.Marshallable.CopyIn.

func (*UContext64) CopyOut

func (u *UContext64) CopyOut(cc marshal.CopyContext, addr hostarch.Addr) (int, error)

CopyOut implements marshal.Marshallable.CopyOut.

func (*UContext64) CopyOutN

func (u *UContext64) CopyOutN(cc marshal.CopyContext, addr hostarch.Addr, limit int) (int, error)

CopyOutN implements marshal.Marshallable.CopyOutN.

func (*UContext64) MarshalBytes

func (u *UContext64) MarshalBytes(dst []byte) []byte

MarshalBytes implements marshal.Marshallable.MarshalBytes.

func (*UContext64) MarshalUnsafe

func (u *UContext64) MarshalUnsafe(dst []byte) []byte

MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe.

func (*UContext64) Packed

func (u *UContext64) Packed() bool

Packed implements marshal.Marshallable.Packed.

func (*UContext64) SizeBytes

func (u *UContext64) SizeBytes() int

SizeBytes implements marshal.Marshallable.SizeBytes.

func (*UContext64) UnmarshalBytes

func (u *UContext64) UnmarshalBytes(src []byte) []byte

UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.

func (*UContext64) UnmarshalUnsafe

func (u *UContext64) UnmarshalUnsafe(src []byte) []byte

UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe.

func (*UContext64) WriteTo

func (u *UContext64) WriteTo(writer io.Writer) (int64, error)

WriteTo implements io.WriterTo.WriteTo.

Directories

Path Synopsis
Package fpu provides basic floating point helpers.
Package fpu provides basic floating point helpers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL