arch

package
v0.0.0-...-9a2cfe9 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: Apache-2.0, MIT Imports: 20 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

type Auxv

type Auxv []AuxEntry

An Auxv represents an ELF auxiliary vector.

type Context

type Context interface {
	// Arch returns the architecture for this Context.
	Arch() Arch

	// Native converts a generic type to a native value.
	//
	// Because the architecture is not specified here, we may be dealing
	// with return values of varying sizes (for example ARCH_GETFS). This
	// is a simple utility function to convert to the native size in these
	// cases, and then we can CopyOut.
	Native(val uintptr) marshal.Marshallable

	// Value converts a native type back to a generic value.
	// Once a value has been converted to native via the above call -- it
	// can be converted back here.
	Value(val marshal.Marshallable) uintptr

	// Width returns the number of bytes for a native value.
	Width() uint

	// Fork creates a clone of the context.
	Fork() Context

	// SyscallNo returns the syscall number.
	SyscallNo() uintptr

	// SyscallSaveOrig save orignal register value.
	SyscallSaveOrig()

	// SyscallArgs returns the syscall arguments in an array.
	SyscallArgs() SyscallArguments

	// Return returns the return value for a system call.
	Return() uintptr

	// SetReturn sets the return value for a system call.
	SetReturn(value uintptr)

	// RestartSyscall reverses over the current syscall instruction, such that
	// when the application resumes execution the syscall will be re-attempted.
	RestartSyscall()

	// RestartSyscallWithRestartBlock reverses over the current syscall
	// instraction and overwrites the current syscall number with that of
	// restart_syscall(2). This causes the application to restart the current
	// syscall with a custom function when execution resumes.
	RestartSyscallWithRestartBlock()

	// IP returns the current instruction pointer.
	IP() uintptr

	// SetIP sets the current instruction pointer.
	SetIP(value uintptr)

	// Stack returns the current stack pointer.
	Stack() uintptr

	// SetStack sets the current stack pointer.
	SetStack(value uintptr)

	// TLS returns the current TLS pointer.
	TLS() uintptr

	// SetTLS sets the current TLS pointer. Returns false if value is invalid.
	SetTLS(value uintptr) bool

	// SetOldRSeqInterruptedIP sets the register that contains the old IP
	// when an "old rseq" restartable sequence is interrupted.
	SetOldRSeqInterruptedIP(value uintptr)

	// StateData returns a pointer to underlying architecture state.
	StateData() *State

	// RegisterMap returns a map of all registers.
	RegisterMap() (map[string]uintptr, error)

	// SignalSetup modifies the context in preparation for handling the
	// given signal.
	//
	// st is the stack where the signal handler frame should be
	// constructed.
	//
	// act is the SigAction that specifies how this signal is being
	// handled.
	//
	// info is the SignalInfo of the signal being delivered.
	//
	// alt is the alternate signal stack (even if the alternate signal
	// stack is not going to be used).
	//
	// sigset is the signal mask before entering the signal handler.
	SignalSetup(st *Stack, act *linux.SigAction, info *linux.SignalInfo, alt *linux.SignalStack, sigset linux.SignalSet) error

	// SignalRestore restores context after returning from a signal
	// handler.
	//
	// st is the current thread stack.
	//
	// rt is true if SignalRestore is being entered from rt_sigreturn and
	// false if SignalRestore is being entered from sigreturn.
	// SignalRestore returns the thread's new signal mask.
	SignalRestore(st *Stack, rt bool) (linux.SignalSet, linux.SignalStack, error)

	// CPUIDEmulate emulates a CPUID instruction according to current register state.
	CPUIDEmulate(l log.Logger)

	// SingleStep returns true if single stepping is enabled.
	SingleStep() bool

	// SetSingleStep enables single stepping.
	SetSingleStep()

	// ClearSingleStep disables single stepping.
	ClearSingleStep()

	// FloatingPointData will be passed to underlying save routines.
	FloatingPointData() *fpu.State

	// NewMmapLayout returns a layout for a new MM, where MinAddr for the
	// returned layout must be no lower than min, and MaxAddr for the returned
	// layout must be no higher than max. Repeated calls to NewMmapLayout may
	// return different layouts.
	NewMmapLayout(min, max hostarch.Addr, limits *limits.LimitSet) (MmapLayout, error)

	// PIELoadAddress returns a preferred load address for a
	// position-independent executable within l.
	PIELoadAddress(l MmapLayout) hostarch.Addr

	// FeatureSet returns the FeatureSet in use in this context.
	FeatureSet() *cpuid.FeatureSet

	// PtracePeekUser implements ptrace(PTRACE_PEEKUSR).
	PtracePeekUser(addr uintptr) (marshal.Marshallable, error)

	// PtracePokeUser implements ptrace(PTRACE_POKEUSR).
	PtracePokeUser(addr, data uintptr) error

	// PtraceGetRegs implements ptrace(PTRACE_GETREGS) by writing the
	// general-purpose registers represented by this Context to dst and
	// returning the number of bytes written.
	PtraceGetRegs(dst io.Writer) (int, error)

	// PtraceSetRegs implements ptrace(PTRACE_SETREGS) by reading
	// general-purpose registers from src into this Context and returning the
	// number of bytes read.
	PtraceSetRegs(src io.Reader) (int, error)

	// PtraceGetRegSet implements ptrace(PTRACE_GETREGSET) by writing the
	// register set given by architecture-defined value regset from this
	// Context to dst and returning the number of bytes written, which must be
	// less than or equal to maxlen.
	PtraceGetRegSet(regset uintptr, dst io.Writer, maxlen int) (int, error)

	// PtraceSetRegSet implements ptrace(PTRACE_SETREGSET) by reading the
	// register set given by architecture-defined value regset from src and
	// returning the number of bytes read, which must be less than or equal to
	// maxlen.
	PtraceSetRegSet(regset uintptr, src io.Reader, maxlen int) (int, error)

	// FullRestore returns 'true' if all CPU registers must be restored
	// when switching to the untrusted application. Typically a task enters
	// and leaves the kernel via a system call. Platform.Switch() may
	// optimize for this by not saving/restoring all registers if allowed
	// by the ABI. For e.g. the amd64 ABI specifies that syscall clobbers
	// %rcx and %r11. If FullRestore returns true then these optimizations
	// must be disabled and all registers restored.
	FullRestore() bool
}

Context provides architecture-dependent information for a specific thread.

NOTE(b/34169503): Currently we use uintptr here to refer to a generic native register value. While this will work for the foreseeable future, it isn't strictly correct. We may want to create some abstraction that makes this more clear or enables us to store values of arbitrary widths. This is particularly true for RegisterMap().

func New

func New(arch Arch, fs *cpuid.FeatureSet) Context

New returns a new architecture context.

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) 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

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. See b/33003106#comment8.
	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

type Stack

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

	// 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

	// FeatureSet is a pointer to the currently active feature set.
	FeatureSet *cpuid.FeatureSet
	// 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) CPUIDEmulate

func (s *State) CPUIDEmulate(l log.Logger)

CPUIDEmulate emulates a cpuid instruction.

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) (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) (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.

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

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