container/seccomp: use native types
All checks were successful
Test / Create distribution (push) Successful in 32s
Test / Sandbox (push) Successful in 2m16s
Test / Hakurei (push) Successful in 3m15s
Test / Hpkg (push) Successful in 4m2s
Test / Sandbox (race detector) (push) Successful in 4m12s
Test / Hakurei (race detector) (push) Successful in 5m1s
Test / Flake checks (push) Successful in 1m30s

This prepares NativeRule for relocation to std for #15.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-11-05 05:48:59 +09:00
parent b65aba9446
commit 7f27a6dc51
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 46 additions and 12 deletions

View File

@ -54,10 +54,19 @@ func (e *LibraryError) Is(err error) bool {
} }
type ( type (
// scmpUint is equivalent to [ScmpUint].
scmpUint = C.uint
// ScmpUint is equivalent to C.uint.
ScmpUint uint32
// scmpInt is equivalent to [ScmpInt].
scmpInt = C.int
// ScmpInt is equivalent to C.int.
ScmpInt int32
// ScmpSyscall represents a syscall number passed to libseccomp via [NativeRule.Syscall]. // ScmpSyscall represents a syscall number passed to libseccomp via [NativeRule.Syscall].
ScmpSyscall C.int ScmpSyscall ScmpInt
// ScmpErrno represents an errno value passed to libseccomp via [NativeRule.Errno]. // ScmpErrno represents an errno value passed to libseccomp via [NativeRule.Errno].
ScmpErrno C.int ScmpErrno ScmpInt
// A NativeRule specifies an arch-specific action taken by seccomp under certain conditions. // A NativeRule specifies an arch-specific action taken by seccomp under certain conditions.
NativeRule struct { NativeRule struct {
@ -182,9 +191,12 @@ func Export(rules []NativeRule, flags ExportFlag) (data []byte, err error) {
// Errors returned by libseccomp is wrapped in [LibraryError]. // Errors returned by libseccomp is wrapped in [LibraryError].
func Load(rules []NativeRule, flags ExportFlag) error { return makeFilter(rules, flags, nil) } func Load(rules []NativeRule, flags ExportFlag) error { return makeFilter(rules, flags, nil) }
// ScmpCompare is the equivalent of scmp_compare; type (
// Comparison operators // Comparison operators.
type ScmpCompare = C.enum_scmp_compare scmpCompare = C.enum_scmp_compare
// ScmpCompare is equivalent to enum scmp_compare;
ScmpCompare ScmpUint
)
const ( const (
_SCMP_CMP_MIN = C._SCMP_CMP_MIN _SCMP_CMP_MIN = C._SCMP_CMP_MIN
@ -210,17 +222,15 @@ const (
type ( type (
// Argument datum. // Argument datum.
scmpDatum = C.scmp_datum_t scmpDatum = C.scmp_datum_t
// ScmpDatum is equivalent to scmp_datum_t. // ScmpDatum is equivalent to scmp_datum_t.
ScmpDatum uint64 ScmpDatum uint64
// Argument / Value comparison definition. // Argument / Value comparison definition.
scmpArgCmp = C.struct_scmp_arg_cmp scmpArgCmp = C.struct_scmp_arg_cmp
// ScmpArgCmp is equivalent to struct scmp_arg_cmp. // ScmpArgCmp is equivalent to struct scmp_arg_cmp.
ScmpArgCmp struct { ScmpArgCmp struct {
// argument number, starting at 0 // argument number, starting at 0
Arg C.uint Arg ScmpUint
// the comparison op, e.g. SCMP_CMP_* // the comparison op, e.g. SCMP_CMP_*
Op ScmpCompare Op ScmpCompare

View File

@ -23,17 +23,41 @@ func TestSyscallResolveName(t *testing.T) {
} }
} }
func TestRuleSize(t *testing.T) { func TestRuleType(t *testing.T) {
assertKind[ScmpUint, scmpUint](t)
assertKind[ScmpInt, scmpInt](t)
assertSize[NativeRule, syscallRule](t) assertSize[NativeRule, syscallRule](t)
assertSize[ScmpDatum, scmpDatum](t) assertKind[ScmpDatum, scmpDatum](t)
assertKind[ScmpCompare, scmpCompare](t)
assertSize[ScmpArgCmp, scmpArgCmp](t) assertSize[ScmpArgCmp, scmpArgCmp](t)
} }
// assertSize asserts that native and equivalent are of the same size. // assertSize asserts that native and equivalent are of the same size.
func assertSize[native, equivalent any](t *testing.T) { func assertSize[native, equivalent any](t *testing.T) {
got := unsafe.Sizeof(*new(native)) t.Helper()
want := unsafe.Sizeof(*new(equivalent))
got, want := unsafe.Sizeof(*new(native)), unsafe.Sizeof(*new(equivalent))
if got != want { if got != want {
t.Fatalf("%s: %d, want %d", reflect.TypeFor[native]().Name(), got, want) t.Fatalf("%s: %d, want %d", reflect.TypeFor[native]().Name(), got, want)
} }
} }
// assertKind asserts that native and equivalent are of the same kind.
func assertKind[native, equivalent any](t *testing.T) {
t.Helper()
assertSize[native, equivalent](t)
nativeType, equivalentType := reflect.TypeFor[native](), reflect.TypeFor[equivalent]()
got, want := nativeType.Kind(), equivalentType.Kind()
if got == reflect.Invalid || want == reflect.Invalid {
t.Fatalf("%s: invalid call to assertKind", nativeType.Name())
}
if got == reflect.Struct {
t.Fatalf("%s: struct is unsupported by assertKind", nativeType.Name())
}
if got != want {
t.Fatalf("%s: %s, want %s", nativeType.Name(), nativeType.Kind(), equivalentType.Kind())
}
}