getrandom: Support `x86_64-unknown-linux-none`

The target was added in https://github.com/rust-lang/rust/pull/125023 and is available in the latest Nightly builds. Use cargo +nightly build --target=x86_64-unknown-linux-none -Zbuild-std.

The libc crate does NOT support this target, so libc::syscall and libc::getrandom cannot be used on this target.

I am planning to support this target in ring soon.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 32 (27 by maintainers)

Most upvoted comments

Perhaps I didn’t explain myself properly. What I mean to say is that syscalls are not something we can automatically generate but need to be manually implemented. linux-raw-sys is fully generated code.

I think it would be possible for the linux-raw-sys crate to generate the syscall wrapper functions, as all their information is present in the various Linux headers, so they could (in theory) be auto-generated. For example:

  • include/uapi/asm-generic/unistd.h (and arch-specific files) defines __NR_getrandom and references sys_getrandom
    #define __NR_getrandom 278
    __SYSCALL(__NR_getrandom, sys_getrandom)
    
  • include/linux/syscalls.h defines the prototype for sys_getrandom
    asmlinkage long sys_getrandom(char __user *buf, size_t count, unsigned int flags);
    
  • linux_raw_sys::general::__NR_getrandom would still exist as a constant
  • linux_raw_sys::syscalls::sys_getrandom would be a function whose implementation could be generated based on the “ABI” value in the syscall table:
    #[inline]
    pub unsafe fn sys_getrandom(buf: *mut c_char, count: c_size_t, flags: c_uint) -> c_long
    
  • There would not be a generic syscall() function. I agree with the above points that a function which can invoke any syscall is impossible to implement reliably.

However, I definitely recognize that such auto-generation is quite complex, compared to what linux-raw-sys currently does.

Zooming out, I think it would be fine for this crate to depend on rustix/linux-raw-sys on either all Linux targets or just the linux-none targets, provided that adoption is sufficiently widespread. A good litmus test for this would be libstd itself depending on rustix/linux-raw-sys on some/all Linux targets.

Until that point, having our own syscall wrapper seems like the least bad option. It will work with our without rustix, and we can always move to rustix/linux-raw-sys when there is consensus.

In the PR, @morr0ne said that x86_64-unknown-linux-none won’t have libc, so it’s likely that many crates will use rustix for I/O on *-linux-none, so rustix will often be in people’s dependency trees already. Perhaps that makes it feasible for getrandom to just use rustix::rand::getrandom_uninit on x86_64-unknown-linux-none.

If it were just getrandom it may seem ok to hard-code a little bit of asm, but if other crates start following this example and we end up with many crates doing their own syscall asms, I think that would be unfortunate. And, it’s reasonable to anticipate more linux-none targets than just x86_64.