Skip to content

Commit

Permalink
syscalls: use gperf for lookup
Browse files Browse the repository at this point in the history
use gperf to generate a perfect hash to lookup syscall names.   It
improves significantly the complexity for 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.

Closes: seccomp#207

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Feb 23, 2020
1 parent 5432e15 commit edbf744
Show file tree
Hide file tree
Showing 23 changed files with 677 additions and 8,135 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ addons:
- valgrind
- clang
- lcov
- gperf

env:
global:
Expand Down
5 changes: 5 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ AC_DEFINE_UNQUOTED([ENABLE_PYTHON],
[$(test "$enable_python" = yes && echo 1 || echo 0)],
[Python bindings build flag.])

AC_CHECK_TOOL(GPERF, gperf)
if test -z "$GPERF"; then
AC_MSG_ERROR(please install gperf)
fi

dnl ####
dnl coverity checks
dnl ####
Expand Down
38 changes: 24 additions & 14 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ SOURCES_ALL = \
hash.h hash.c \
db.h db.c \
arch.c arch.h \
arch-x86.h arch-x86.c arch-x86-syscalls.c \
arch-x86_64.h arch-x86_64.c arch-x86_64-syscalls.c \
arch-x32.h arch-x32.c arch-x32-syscalls.c \
arch-arm.h arch-arm.c arch-arm-syscalls.c \
arch-aarch64.h arch-aarch64.c arch-aarch64-syscalls.c \
arch-mips.h arch-mips.c arch-mips-syscalls.c \
arch-mips64.h arch-mips64.c arch-mips64-syscalls.c \
arch-mips64n32.h arch-mips64n32.c arch-mips64n32-syscalls.c \
arch-parisc.h arch-parisc.c arch-parisc64.c arch-parisc-syscalls.c \
arch-ppc.h arch-ppc.c arch-ppc-syscalls.c \
arch-ppc64.h arch-ppc64.c arch-ppc64-syscalls.c \
arch-riscv64.h arch-riscv64.c arch-riscv64-syscalls.c \
arch-s390.h arch-s390.c arch-s390-syscalls.c \
arch-s390x.h arch-s390x.c arch-s390x-syscalls.c
arch-x86.h arch-x86.c \
arch-x86_64.h arch-x86_64.c \
arch-x32.h arch-x32.c \
arch-arm.h arch-arm.c \
arch-aarch64.h arch-aarch64.c \
arch-mips.h arch-mips.c \
arch-mips64.h arch-mips64.c \
arch-mips64n32.h arch-mips64n32.c \
arch-parisc.h arch-parisc.c arch-parisc64.c \
arch-ppc.h arch-ppc.c \
arch-ppc64.h arch-ppc64.c \
arch-riscv64.h arch-riscv64.c \
arch-s390.h arch-s390.c \
arch-s390x.h arch-s390x.c\
syscalls.c syscalls.perf.c

EXTRA_DIST = arch-syscall-validate

Expand All @@ -68,5 +69,14 @@ libseccomp_la_CFLAGS = ${AM_CFLAGS} ${CODE_COVERAGE_CFLAGS} ${CFLAGS} \
libseccomp_la_LDFLAGS = ${AM_LDFLAGS} ${CODE_COVERAGE_LDFLAGS} ${LDFLAGS} \
-version-number ${VERSION_MAJOR}:${VERSION_MINOR}:${VERSION_MICRO}

EXTRA_DIST += syscalls.perf.c syscalls.perf
CLEANFILES = syscalls.perf.c syscalls.perf

syscalls.perf: syscalls.csv syscalls.perf.template
${AM_V_GEN} ./generate_syscalls_perf.sh

syscalls.perf.c: syscalls.perf
${GPERF} -m 100 --null-strings --pic -tCEG -T -S1 $< > $@

check-build:
${MAKE} ${AM_MAKEFLAGS} ${check_PROGRAMS}
Loading

0 comments on commit edbf744

Please sign in to comment.