safecopy

package
v0.0.0-202406181927 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: Apache-2.0, MIT, BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package safecopy provides an efficient implementation of functions to access memory that may result in SIGSEGV or SIGBUS being sent to the accessor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckXstate

func CheckXstate(state *byte) error

CheckXstate verifies that xstate can be restored by the xrstor instruction.

func CompareAndSwapUint32

func CompareAndSwapUint32(ptr unsafe.Pointer, old, new uint32) (uint32, error)

CompareAndSwapUint32 is equivalent to atomicbitops.CompareAndSwapUint32, except that it returns an error if SIGSEGV or SIGBUS is received while accessing ptr, or if ptr is not aligned to a 4-byte boundary.

func Copy

func Copy(dst, src unsafe.Pointer, toCopy uintptr) (uintptr, error)

Copy copies toCopy bytes from src to dst. It returns the number of bytes copied and an error if SIGSEGV or SIGBUS is received while reading from src or writing to dst.

Data is copied in order; if [src, src+toCopy) and [dst, dst+toCopy) overlap, the resulting contents of dst are unspecified.

func CopyIn

func CopyIn(dst []byte, src unsafe.Pointer) (int, error)

CopyIn copies len(dst) bytes from src to dst. It returns the number of bytes copied and an error if SIGSEGV or SIGBUS is received while reading from src.

func CopyOut

func CopyOut(dst unsafe.Pointer, src []byte) (int, error)

CopyOut copies len(src) bytes from src to dst. If returns the number of bytes done and an error if SIGSEGV or SIGBUS is received while writing to dst.

func FindEndAddress

func FindEndAddress(begin uintptr) uintptr

FindEndAddress returns the end address (one byte beyond the last) of the function that contains the specified address (begin).

func LoadUint32

func LoadUint32(ptr unsafe.Pointer) (uint32, error)

LoadUint32 is like sync/atomic.LoadUint32, but operates with user memory. It may fail with SIGSEGV or SIGBUS if it is received while reading from ptr.

Preconditions: ptr must be aligned to a 4-byte boundary.

func SwapUint32

func SwapUint32(ptr unsafe.Pointer, new uint32) (uint32, error)

SwapUint32 is equivalent to sync/atomic.SwapUint32, except that it returns an error if SIGSEGV or SIGBUS is received while accessing ptr, or if ptr is not aligned to a 4-byte boundary.

func SwapUint64

func SwapUint64(ptr unsafe.Pointer, new uint64) (uint64, error)

SwapUint64 is equivalent to sync/atomic.SwapUint64, except that it returns an error if SIGSEGV or SIGBUS is received while accessing ptr, or if ptr is not aligned to an 8-byte boundary.

func ZeroOut

func ZeroOut(dst unsafe.Pointer, toZero uintptr) (uintptr, error)

ZeroOut writes toZero zero bytes to dst. It returns the number of bytes written and an error if SIGSEGV or SIGBUS is received while writing to dst.

Types

type AlignmentError

type AlignmentError struct {
	// Addr is the invalid address.
	Addr uintptr

	// Alignment is the required alignment.
	Alignment uintptr
}

AlignmentError is returned when a safecopy function is passed an address that does not meet alignment requirements.

func (AlignmentError) Error

func (e AlignmentError) Error() string

Error implements error.Error.

type BusError

type BusError struct {
	// Addr is the address at which the SIGBUS occurred.
	Addr uintptr
}

BusError is returned when a safecopy function receives SIGBUS.

func (BusError) Error

func (e BusError) Error() string

Error implements error.Error.

type SegvError

type SegvError struct {
	// Addr is the address at which the SIGSEGV occurred.
	Addr uintptr
}

SegvError is returned when a safecopy function receives SIGSEGV.

func (SegvError) Error

func (e SegvError) Error() string

Error implements error.Error.

Jump to

Keyboard shortcuts

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