miri icon indicating copy to clipboard operation
miri copied to clipboard

Add direct tests for all shims

Open RalfJung opened this issue 3 years ago • 34 comments

For example: "lseek" on Linux is currently only tested via the libstd functions, but not via direct libc calls. And really we should ideally have direct tests for all of our shims to make sure they don't start to bitrot when the standard library changes.

Shared (foreign_items.rs)

  • [x] exit
  • [x] abort
  • [x] memcmp

Unix (unix/foreign_items.rs)

  • [x] getenv
  • [x] setenv
  • [x] unsetenv
  • [x] getcwd
  • [x] chdir
  • [x] realpath
  • [x] pread
  • [x] pwrite
  • [x] lseek
  • [x] fcntl
  • [x] fstat
  • [x] stat
  • [x] lstat
  • [x] unlink
  • [x] symlink
  • [x] mkdir
  • [x] rmdir
  • [x] opendir
  • [x] readdir
  • [x] closedir
  • [x] fsync
  • [x] fdatasync
  • [x] aligned_alloc
  • [x] pthread_key_delete
  • [x] pthread_getspecific
  • [x] pthread_setspecific
  • [x] sched_getaffinity
  • [x] pthread_atfork
  • [x] arc4random_buf
  • [x] _Unwind_RaiseException
  • [x] sched_yield

Android (unix/android/foreign_items.rs)

All implemented

FreeBSD (unix/freebds/foreign_items.rs)

All implemented

Linux (unix/linux/foreign_items.rs)

  • [x] open64
  • [x] pread64
  • [x] pwrite64
  • [x] lseek64
  • [x] readdir64
  • [x] statx
  • [x] syscall (needs one for each syscall, though some are already covered)
  • [x] __xpg_strerror_r
  • [x] __libc_current_sigrtmin
  • [x] __libc_current_sigrtmax

MacOS (unix/macos/foreign_items.rs)

  • [x] __error
  • [x] _NSGetEnviron
  • [x] _NSGetArgc
  • [x] _NSGetArgv
  • [x] _NSGetExecutablePath
  • [x] mach_timebase_info
  • [x] _tlv_atexit
  • [x] pthread_get_stackaddr_np
  • [x] pthread_get_stacksize_np

Solarish (unix/solarish/foreign_items.rs)

  • [x] __xnet_socketpair
  • [x] stack_getbounds
  • [x] pset_info
  • [x] __sysconf_xpg7

Windows (windows/foreign_items.rs)

  • [ ] GetEnvironmentVariableW
  • [ ] SetEnvironmentVariableW
  • [ ] GetEnvironmentStringsW
  • [ ] FreeEnvironmentStringsW
  • [ ] GetCurrentDirectoryW
  • [ ] SetCurrentDirectoryW
  • [ ] GetUserProfileDirectoryW
  • [x] GetCurrentProcessId
  • [ ] GetFullPathNameW
  • [ ] HeapAlloc
  • [ ] HeapFree
  • [ ] HeapReAlloc
  • [ ] GetSystemInfo
  • [ ] GetCommandLineW
  • [ ] GetSystemTimeAsFileTime
  • [x] QueryPerformanceCounter
  • [x] QueryPerformanceFrequency
  • [ ] CreateWaitableTimerExW
  • [ ] WaitOnAddress
  • [ ] WakeByAddressSingle
  • [ ] WakeByAddressAll
  • [x] SetThreadDescription
  • [x] GetThreadDescription
  • [ ] ExitProcess
  • [x] ProcessPrng
  • [ ] GetConsoleScreenBufferInfo
  • [ ] GetStdHandle
  • [ ] GetModuleFileNameW
  • [ ] FormatMessageW
  • [ ] GetProcAddress
  • [ ] GetSystemTimePreciseAsFileTime
  • [ ] GetTempPathW
  • [ ] Sleep

RalfJung avatar Nov 20 '23 12:11 RalfJung

Hi there! I'm interested in contributing and thought I'd start with this issue which sounds doable. I'm studying software engineering at uni and I had some experience with C and currently going deeper into Rust after taking a course at uni.

From what I understand so far for this issue, I wrote some code and this is what I have so far and I was wondering if I'm in the right direction?

tests/pass-dep/shims/libc-fs.rs

fn main() {
    // ...
    test_ftruncate();
}

fn test_ftruncate() {
    // Create a file with some content
    // Taken from the test functions above
    let path = prepare_with_content("test_ftruncate.txt", &[]);

    let mut name = path.into_os_string();
    name.push("\0");
    let name_ptr = name.as_bytes().as_ptr().cast::<libc::c_char>();
    let fd = unsafe { libc::open(name_ptr, libc::O_RDONLY, 42) };

    // We use i64 here because the second argument for ftruncate()
    // uses off_t which is a signed integer types and on my machine,
    // off_t is synonymous with long long
    // References:
    // - https://doc.rust-lang.org/std/os/raw/type.c_longlong.html
    // - https://locka99.gitbooks.io/a-guide-to-porting-c-to-rust/content/features_of_rust/types.html
    // - https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type
    let l: i64 = 4;
    let length = l.cast::<libc::c_longlong>(); // not too sure about this line
    let res = unsafe { libc::ftruncate(fd, length) };
    if res == 0 {
        // call succeeds
    } else {
        // call fails
    }
}

irfanzainudin avatar Nov 28 '23 13:11 irfanzainudin

Right, basically these tests should be fairly similar to this one but directly using the libc APIs rather than calling Rust's std::fs functions.

You can use the CString type to generate C-compatible strings, no need to manually push \0.

RalfJung avatar Nov 29 '23 15:11 RalfJung

Support for extra modes in open (as implemented in https://github.com/rust-lang/miri/pull/3240) should also be covered by such a test.

RalfJung avatar Dec 27 '23 23:12 RalfJung

I looked over all foreign_item.rss and for each function there looked for corresponding direct libc tests, these are the functions which do not have any direct tests associated, categorized by where they are located in the source code. If I found a single instance of a direct function call in tests, I immediately discarded it, so it is possible for functions not listed here to have been called only once in a test and not have sufficient test coverage. I also did not include any functions which check for this.frame_in_std().

(List moved to issue summary.)

If I missed any (or incorrectly counted a function as not being tested when it was), feel free to tell me

eduardorittner avatar Nov 11 '25 15:11 eduardorittner

Thanks for making a list! Your categorization is a bit off, lseek* and f*sync are not "directory operations".

And there are definitely more, e.g. some of the ones in src/shims/unix/linux/foreign_items.rs (such as readdir64 and statx) or in src/shims/unix/macos/foreign_items.rs (such as mach_timebase_info, _NSGetExecutablePath).

RalfJung avatar Nov 11 '25 17:11 RalfJung

Thanks for the heads up, I'll try to look a bit more closely to add the ones I've missed to the list

eduardorittner avatar Nov 11 '25 17:11 eduardorittner

I took a look at pthread_getattr_np, there are not libc::pthread_getattr_np in any of the tests, but due to the comment this looks like a special case, should we still add direct libc tests for this?

// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
// These shims are enabled only when the caller is in the standard library.

eduardorittner avatar Nov 11 '25 17:11 eduardorittner

Yeah, the shims with this extra this.frame_in_std() check do not need dedicates tests.

RalfJung avatar Nov 11 '25 17:11 RalfJung

Another question, there are some functions (specially in linux) which start with a __, like __errno, __errno_location and so on. Do they need dedicated tests as well?

eduardorittner avatar Nov 11 '25 17:11 eduardorittner

Those are still part of the libc ABI and actually used by user crates, so yes.

bjorn3 avatar Nov 11 '25 18:11 bjorn3

I re-checked all foreign_items.rs, some updates:

Shared (foreign_items.rs)

  • [ ] exit
  • [ ] abort

Unix (unix/foreign_items.rs)

  • [ ] getcwd
  • [ ] chdir
  • [ ] symlink
  • [ ] mkdir
  • [ ] rmdir
  • [ ] lseek
  • [ ] fsync
  • [ ] fdatasync
  • [ ] sched_yield
  • [ ] pthread_atfork

Android (unix/android/foreign_items.rs)

  • [ ] pread64
  • [ ] pwrite64
  • [ ] lseek64
  • [ ] ftruncate64

FreeBSD (unix/freebsd/foreign_items.rs)

All tested

Linux (unix/linux/foreign_items.rs)

  • [ ] open64
  • [ ] openat64
  • [ ] pread64
  • [ ] pwrite64
  • [ ] lseek64
  • [ ] posix_fallocate64
  • [ ] fallocate64
  • [ ] ftruncate64
  • [ ] mmap64
  • [ ] __libc_current_sigrtmin
  • [ ] __libc_current_sigrtmax

MacOS (unix/macos/foreign_items.rs)

  • [ ] _NSGetEnviron
  • [ ] _NSGetArgc
  • [ ] _NSGetArgv
  • [ ] _NSGetExecutablePath
  • [ ] mach_timebase_info
  • [ ] mach_wait_until
  • [ ] pthread_get_stackaddr_np
  • [ ] pthread_get_stacksize_np
  • [ ] confstr

NetBSD (unix/netbsd/foreign_items.rs)

  • [ ] __errno

Solarish (unix/solarish/foreign_items.rs)

  • [ ] stack_getbounds
  • [ ] pset_info

Windows (windows/foreign_items.rs)

  • [ ] GetEnvironmentVariableW
  • [ ] SetEnvironmentVariableW
  • [ ] GetEnvironmentStringsW
  • [ ] FreeEnvironmentStringsW
  • [ ] GetCurrentDirectoryW
  • [ ] SetCurrentDirectoryW
  • [ ] GetUserProfileDirectoryW
  • [ ] GetCurrentProcessId
  • [ ] GetTempPathW
  • [ ] GetFullPathNameW
  • [ ] HeapAlloc
  • [ ] HeapFree
  • [ ] HeapReAlloc
  • [ ] GetSystemInfo
  • [ ] GetCommandLineW
  • [ ] GetSystemTimeAsFileTime
  • [ ] GetSystemTimePreciseAsFileTime
  • [ ] QueryPerformanceCounter
  • [ ] QueryPerformanceFrequency
  • [ ] Sleep
  • [ ] CreateWaitableTimerExW
  • [ ] WaitOnAddress
  • [ ] WakeByAddressSingle
  • [ ] WakeByAddressAll
  • [ ] GetProcAddress
  • [ ] ExitProcess
  • [ ] GetConsoleScreenBufferInfo
  • [ ] GetModuleFileNameW
  • [ ] FormatMessageW

If I missed any (or incorrectly counted a function as not being tested when it was), feel free to tell me

Please let's not have multiple lists of checkboxes. I can update the list at the top of the issue if you let me know what needs updating.

I also added this in the source code to every shim that does not have a test:

// FIXME: This does not have a direct test (#3179).

If you find a shim where that FIXME is missing, please make a PR.

RalfJung avatar Oct 22 '26 05:10 RalfJung

From what I can tell, these shims are all directly tested but their corresponding checkboxes in this issue are marked empty, so they should probably be updated:

  • realpath
  • opendir
  • readdir
  • closedir
  • aligned_alloc
  • sched_getaffinity
  • readdir64
  • statx
  • __error
  • _tlv_atexit

eduardorittner avatar Oct 22 '26 16:10 eduardorittner

Although the file src/shims/unix/android/foreign_items.rs contains “FIXME” lines for pread64, pwrite64, and lseek64, the list indicates that these are “All implemented” for Android. These three shims were added to the Android file in January 2026, after the list was created. I want to write tests for these three shims. Since there are no direct tests for Linux either, the tests will cover both. If I'm wrong, please feel free to correct me.

mehmettoplu-rs avatar Oct 22 '26 17:10 mehmettoplu-rs

This sounds right. The list is incomplete but still helpful for finding places to get started, the FIXMEs are more up-to-date.

RalfJung avatar Oct 22 '26 19:10 RalfJung

Additionally, I’d like to include the lseek function from the unix/foreign_items.rs file in the same PR, because lseek and lseek64 are two different versions of the same function, and both can be tested with a single generic test.

mehmettoplu-rs avatar Oct 22 '26 20:10 mehmettoplu-rs

I'll take fsync and fdatasync in unix/foreign_items.rs.

mehmettoplu-rs avatar Oct 28 '26 11:10 mehmettoplu-rs

I'll take open64 and openat64 in unix/linux/foreign_items.rs.

mehmettoplu-rs avatar Nov 03 '26 09:11 mehmettoplu-rs

I'll take getcwd and chdir in unix/foreign_items.rs.

mehmettoplu-rs avatar Nov 04 '26 19:11 mehmettoplu-rs

I'll take unlink and symlink in unix/foreign_items.rs.

mehmettoplu-rs avatar Nov 07 '26 13:11 mehmettoplu-rs