diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2020-01-23 17:01:39 +0100 |
---|---|---|
committer | Paul Moore <paul@paul-moore.com> | 2020-03-23 11:14:35 -0400 |
commit | 9b129c41ac1f43d373742697aa2faf6040b9dfab (patch) | |
tree | 25c69d113e954473adb7b5209c76b67773a383e9 /tests | |
parent | f5b3166d6126f4a45b59d6af6780b5e00a0e9867 (diff) | |
download | libseccomp-9b129c41ac1f43d373742697aa2faf6040b9dfab.tar.gz |
arch: use gperf to generate a perfact hash to lookup syscall names
This patch significantly improves the performance of
seccomp_syscall_resolve_name since it replaces the expensive strcmp
for each syscall in the database, with a lookup table.
The complexity for syscall_resolve_num is not changed and it
uses the linear search, that is anyway less expensive than
seccomp_syscall_resolve_name as it uses an index for comparison
instead of doing a string comparison.
On my machine, calling 1000 seccomp_syscall_resolve_name_arch and
seccomp_syscall_resolve_num_arch over the entire syscalls DB passed
from ~0.45 sec to ~0.06s.
PM: After talking with Giuseppe I made a number of additional
changes, some substantial, the highlights include:
* various style tweaks
* .gitignore fixes
* fixed subject line, tweaked the description
* dropped the arch-syscall-validate changes as they were masking
other problems
* extracted the syscalls.csv and file deletions to other patches
to keep this one more focused
* fixed the x86, x32, arm, all the MIPS ABIs, s390, and s390x ABIs as
the syscall offsets were not properly incorporated into this change
* cleaned up the ABI specific headers
* cleaned up generate_syscalls_perf.sh and renamed to
arch-gperf-generate
* fixed problems with automake's file packaging
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
[PM: see notes in the "PM" section above]
Signed-off-by: Paul Moore <paul@paul-moore.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/56-basic-iterate_syscalls.c | 90 | ||||
-rwxr-xr-x | tests/56-basic-iterate_syscalls.py | 65 | ||||
-rw-r--r-- | tests/56-basic-iterate_syscalls.tests | 11 | ||||
-rw-r--r-- | tests/Makefile.am | 9 |
5 files changed, 173 insertions, 3 deletions
diff --git a/tests/.gitignore b/tests/.gitignore index bd00e14..c536766 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -61,3 +61,4 @@ util.pyc 53-sim-binary_tree 54-live-binary_tree 55-basic-pfc_binary_tree +56-basic-iterate_syscalls diff --git a/tests/56-basic-iterate_syscalls.c b/tests/56-basic-iterate_syscalls.c new file mode 100644 index 0000000..5e7ab67 --- /dev/null +++ b/tests/56-basic-iterate_syscalls.c @@ -0,0 +1,90 @@ +/** + * Seccomp Library test program + * + * Copyright (c) 2020 Red Hat <gscrivan@redhat.com> + * Author: Giuseppe Scrivano <gscrivan@redhat.com> + */ + +/* + * This library is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License as + * published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see <http://www.gnu.org/licenses>. + */ + +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +#include <seccomp.h> + +unsigned int arch_list[] = { + SCMP_ARCH_NATIVE, + SCMP_ARCH_X86, + SCMP_ARCH_X86_64, + SCMP_ARCH_X32, + SCMP_ARCH_ARM, + SCMP_ARCH_AARCH64, + SCMP_ARCH_MIPS, + SCMP_ARCH_MIPS64, + SCMP_ARCH_MIPS64N32, + SCMP_ARCH_MIPSEL, + SCMP_ARCH_MIPSEL64, + SCMP_ARCH_MIPSEL64N32, + SCMP_ARCH_PPC, + SCMP_ARCH_PPC64, + SCMP_ARCH_PPC64LE, + SCMP_ARCH_S390, + SCMP_ARCH_S390X, + SCMP_ARCH_PARISC, + SCMP_ARCH_PARISC64, + SCMP_ARCH_RISCV64, + -1 +}; + +static int test_arch(int arch, int init) +{ + int n, iter = 0; + + for (iter = init; iter < init + 1000; iter++) { + char *name; + + name = seccomp_syscall_resolve_num_arch(arch, iter); + if (name == NULL) + continue; + + n = seccomp_syscall_resolve_name_arch(arch, name); + if (n != iter) + return 1; + } + return 0; +} + +int main(int argc, char *argv[]) +{ + int iter = 0; + + for (iter = 0; arch_list[iter] != -1; iter++) { + int init = 0; + if (arch_list[iter] == SCMP_ARCH_X32) + init = 0x40000000; + else if (arch_list[iter] == SCMP_ARCH_MIPS) + init = 4000; + else if (arch_list[iter] == SCMP_ARCH_MIPS64) + init = 5000; + else if (arch_list[iter] == SCMP_ARCH_MIPS64N32) + init = 6000; + if (test_arch(arch_list[iter], init) < 0) + return 1; + } + + return 0; +} diff --git a/tests/56-basic-iterate_syscalls.py b/tests/56-basic-iterate_syscalls.py new file mode 100755 index 0000000..77a5b89 --- /dev/null +++ b/tests/56-basic-iterate_syscalls.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +# +# Seccomp Library test program +# +# Copyright (c) 2020 Red Hat <gscrivan@redhat.com> +# Author: Giuseppe Scrivano <gscrivan@redhat.com> +# + +# +# This library is free software; you can redistribute it and/or modify it +# under the terms of version 2.1 of the GNU Lesser General Public License as +# published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, see <http://www.gnu.org/licenses>. +# + +import argparse +import sys + +import util + +from seccomp import * + +arch_list = ["x86", + "x86_64", + "x32", + "arm", + "aarch64", + "mipsel", + "mipsel64", + "mipsel64n32", + "ppc64le", + "riscv64"] + +def test_arch(arch, init): + for i in range(init, init + 1000): + sys_name = resolve_syscall(arch, i) + if sys_name is None: + continue + n = resolve_syscall(i, sys_name) + if i != n: + raise RuntimeError("Test failure") + +def test(): + for i in arch_list: + init = 0 + if i == "x32": + init = 0x40000000 + elif i == "mipsel": + init = 4000 + elif i == "mipsel64": + init = 5000 + elif i == "mipsel64n32": + init = 6000 + test_arch(Arch(i), init) + +# kate: syntax python; +# kate: indent-mode python; space-indent on; indent-width 4; mixedindent off; diff --git a/tests/56-basic-iterate_syscalls.tests b/tests/56-basic-iterate_syscalls.tests new file mode 100644 index 0000000..a84415a --- /dev/null +++ b/tests/56-basic-iterate_syscalls.tests @@ -0,0 +1,11 @@ +# +# libseccomp regression test automation data +# +# Copyright (c) 2020 Red Hat <gscrivan@redhat.com> +# Author: Giuseppe Scrivano <gscrivan@redhat.com> +# + +test type: basic + +# Test command +56-basic-iterate_syscalls diff --git a/tests/Makefile.am b/tests/Makefile.am index 4bd19c8..a135278 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -94,7 +94,8 @@ check_PROGRAMS = \ 52-basic-load \ 53-sim-binary_tree \ 54-live-binary_tree \ - 55-basic-pfc_binary_tree + 55-basic-pfc_binary_tree \ + 56-basic-iterate_syscalls EXTRA_DIST_TESTPYTHON = \ util.py \ @@ -150,7 +151,8 @@ EXTRA_DIST_TESTPYTHON = \ 51-live-user_notification.py \ 52-basic-load.py \ 53-sim-binary_tree.py \ - 54-live-binary_tree.py + 54-live-binary_tree.py \ + 56-basic-iterate_syscalls.py EXTRA_DIST_TESTCFGS = \ 01-sim-allow.tests \ @@ -207,7 +209,8 @@ EXTRA_DIST_TESTCFGS = \ 52-basic-load.tests \ 53-sim-binary_tree.tests \ 54-live-binary_tree.tests \ - 55-basic-pfc_binary_tree.tests + 55-basic-pfc_binary_tree.tests \ + 56-basic-iterate_syscalls.tests EXTRA_DIST_TESTSCRIPTS = \ 38-basic-pfc_coverage.sh 38-basic-pfc_coverage.pfc \ |