gvisor/test/syscalls
Ian Gudger 0a5ee6f7b2 Fix deadlock in fasync.
The deadlock can occur when both ends of a connected Unix socket which has
FIOASYNC enabled on at least one end are closed at the same time. One end
notifies that it is closing, calling (*waiter.Queue).Notify which takes
waiter.Queue.mu (as a read lock) and then calls (*FileAsync).Callback, which
takes FileAsync.mu. The other end tries to unregister for notifications by
calling (*FileAsync).Unregister, which takes FileAsync.mu and calls
(*waiter.Queue).EventUnregister which takes waiter.Queue.mu.

This is fixed by moving the calls to waiter.Waitable.EventRegister and
waiter.Waitable.EventUnregister outside of the protection of any mutex used
in (*FileAsync).Callback.

The new test is related, but does not cover this particular situation.

Also fix a data race on FileAsync.e.Callback. (*FileAsync).Callback checked
FileAsync.e.Callback under the protection of FileAsync.mu, but the waiter
calling (*FileAsync).Callback could not and did not. This is fixed by making
FileAsync.e.Callback immutable before passing it to the waiter for the first
time.

Fixes #346

PiperOrigin-RevId: 253138340
2019-06-13 17:26:22 -07:00
..
gtest Update canonical repository. 2019-06-13 16:50:15 -07:00
linux Fix deadlock in fasync. 2019-06-13 17:26:22 -07:00
BUILD Remove tmpfs restriction from test 2019-06-06 15:56:20 -07:00
README.md CONTRIBUTING: add style guide pointer 2019-04-11 14:18:01 -07:00
build_defs.bzl Add overlay dimension to FS related syscall tests 2019-06-06 14:38:47 -07:00
syscall_test_runner.go Update canonical repository. 2019-06-13 16:50:15 -07:00
syscall_test_runner.sh Change copyright notice to "The gVisor Authors" 2019-04-29 14:26:23 -07:00

README.md

gVisor system call test suite

This is a test suite for Linux system calls. It runs under both gVisor and Linux, and ensures compatibility between the two.

When adding support for a new syscall (or syscall argument) to gVisor, a corresponding syscall test should be added. It's usually recommended to write the test first and make sure that it passes on Linux before making changes to gVisor.

This document outlines the general guidelines for tests and specific rules that must be followed for new tests.

Running the tests

Each test file generates three different test targets that run in different environments:

  • a native target that runs directly on the host machine,
  • a runsc_ptrace target that runs inside runsc using the ptrace platform, and
  • a runsc_kvm target that runs inside runsc using the KVM platform.

For example, the test in access_test.cc generates the following targets:

  • //test/syscalls:access_test_native
  • //test/syscalls:access_test_runsc_ptrace
  • //test/syscalls:access_test_runsc_kvm

Any of these targets can be run directly via bazel test.

$ bazel test //test/syscalls:access_test_native
$ bazel test //test/syscalls:access_test_runsc_ptrace
$ bazel test //test/syscalls:access_test_runsc_kvm

To run all the tests on a particular platform, you can filter by the platform tag:

# Run all tests in native environment:
$ bazel test --test_tag_filters=native //test/syscalls/...

# Run all tests in runsc with ptrace:
$ bazel test --test_tag_filters=runsc_ptrace //test/syscalls/...

# Run all tests in runsc with kvm:
$ bazel test --test_tag_filters=runsc_kvm //test/syscalls/...

You can also run all the tests on every platform. (Warning, this may take a while to run.)

# Run all tests on every platform:
$ bazel test //test/syscalls/...

Writing new tests

Whenever we add support for a new syscall, or add support for a new argument or option for a syscall, we should always add a new test (perhaps many new tests).

In general, it is best to write the test first and make sure it passes on Linux by running the test on the native platform on a Linux machine. This ensures that the gVisor implementation matches actual Linux behavior. Sometimes man pages contain errors, so always check the actual Linux behavior.

gVisor uses the Google Test test framework, with a few custom matchers and guidelines, described below.

Syscall matchers

When testing an individual system call, use the following syscall matchers, which will match the value returned by the syscall and the errno.

SyscallSucceeds()
SyscallSucceedsWithValue(...)
SyscallFails()
SyscallFailsWithErrno(...)

Use test utilities (RAII classes)

The test utilties are written as RAII classes. These utilities should be preferred over custom test harnesses.

Local class instances should be preferred, wherever possible, over full test fixtures.

A test utility should be created when there is more than one test that requires that same functionality, otherwise the class should be test local.

Save/Restore support in tests

gVisor supports save/restore, and our syscall tests are written in a way to enable saving/restoring at certain points. Hence, there are calls to MaybeSave, and certain tests that should not trigger saves are named with NoSave.

However, the current open-source test runner does not yet support triggering save/restore, so these functions and annotations have no effect on the tests. We plan on extending the test runner to trigger save/restore. Until then, these functions and annotations should be ignored.