From 995968834daa322e0ccba242fbf8a3de8e421c8c Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Wed, 6 Nov 2024 21:54:18 +0300 Subject: [PATCH 1/4] Added epoll and eventfd for Android --- ci/ci.sh | 2 +- src/shims/unix/android/foreign_items.rs | 27 +++++++++++++++++++ src/shims/unix/linux/eventfd.rs | 3 --- tests/fail-dep/libc/libc-epoll-data-race.rs | 1 + tests/pass-dep/libc/libc-epoll-blocking.rs | 1 + tests/pass-dep/libc/libc-epoll-no-blocking.rs | 1 + tests/pass-dep/libc/libc-eventfd.rs | 1 + 7 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ci/ci.sh b/ci/ci.sh index 0356d7ecf1..1e5a2847c6 100755 --- a/ci/ci.sh +++ b/ci/ci.sh @@ -154,7 +154,7 @@ case $HOST_TARGET in TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe - TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap random sync threadname pthread + TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap random sync threadname pthread epoll eventfd TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC wasm TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std empty_main wasm # this target doesn't really have std TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std diff --git a/src/shims/unix/android/foreign_items.rs b/src/shims/unix/android/foreign_items.rs index 80ad40e162..4a005093b2 100644 --- a/src/shims/unix/android/foreign_items.rs +++ b/src/shims/unix/android/foreign_items.rs @@ -1,6 +1,8 @@ use rustc_abi::ExternAbi; use rustc_span::Symbol; +use self::shims::unix::linux::epoll::EvalContextExt as _; +use self::shims::unix::linux::eventfd::EvalContextExt as _; use crate::shims::unix::android::thread::prctl; use crate::shims::unix::linux::syscall::syscall; use crate::*; @@ -20,6 +22,31 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { ) -> InterpResult<'tcx, EmulateItemResult> { let this = self.eval_context_mut(); match link_name.as_str() { + // epoll, eventfd + "epoll_create1" => { + let [flag] = + this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?; + let result = this.epoll_create1(flag)?; + this.write_scalar(result, dest)?; + } + "epoll_ctl" => { + let [epfd, op, fd, event] = + this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?; + let result = this.epoll_ctl(epfd, op, fd, event)?; + this.write_scalar(result, dest)?; + } + "epoll_wait" => { + let [epfd, events, maxevents, timeout] = + this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?; + this.epoll_wait(epfd, events, maxevents, timeout, dest)?; + } + "eventfd" => { + let [val, flag] = + this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?; + let result = this.eventfd(val, flag)?; + this.write_scalar(result, dest)?; + } + // Miscellaneous "__errno" => { let [] = this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?; diff --git a/src/shims/unix/linux/eventfd.rs b/src/shims/unix/linux/eventfd.rs index 63b7d37b13..05276b16f1 100644 --- a/src/shims/unix/linux/eventfd.rs +++ b/src/shims/unix/linux/eventfd.rs @@ -144,9 +144,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { fn eventfd(&mut self, val: &OpTy<'tcx>, flags: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> { let this = self.eval_context_mut(); - // eventfd is Linux specific. - this.assert_target_os("linux", "eventfd"); - let val = this.read_scalar(val)?.to_u32()?; let mut flags = this.read_scalar(flags)?.to_i32()?; diff --git a/tests/fail-dep/libc/libc-epoll-data-race.rs b/tests/fail-dep/libc/libc-epoll-data-race.rs index 398bc92b39..0794867118 100644 --- a/tests/fail-dep/libc/libc-epoll-data-race.rs +++ b/tests/fail-dep/libc/libc-epoll-data-race.rs @@ -3,6 +3,7 @@ //! and therefore still report a data race for things that need to see the second event //! to be considered synchronized. //@only-target: linux +//@only-target: android // ensure deterministic schedule //@compile-flags: -Zmiri-preemption-rate=0 diff --git a/tests/pass-dep/libc/libc-epoll-blocking.rs b/tests/pass-dep/libc/libc-epoll-blocking.rs index 9bcc776e28..d45dc5af48 100644 --- a/tests/pass-dep/libc/libc-epoll-blocking.rs +++ b/tests/pass-dep/libc/libc-epoll-blocking.rs @@ -1,4 +1,5 @@ //@only-target: linux +//@only-target: android // test_epoll_block_then_unblock and test_epoll_race depend on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0 diff --git a/tests/pass-dep/libc/libc-epoll-no-blocking.rs b/tests/pass-dep/libc/libc-epoll-no-blocking.rs index 288c1d41f3..59141cd5dc 100644 --- a/tests/pass-dep/libc/libc-epoll-no-blocking.rs +++ b/tests/pass-dep/libc/libc-epoll-no-blocking.rs @@ -1,4 +1,5 @@ //@only-target: linux +//@only-target: android use std::convert::TryInto; diff --git a/tests/pass-dep/libc/libc-eventfd.rs b/tests/pass-dep/libc/libc-eventfd.rs index dd9c0eb0b5..767ab1065c 100644 --- a/tests/pass-dep/libc/libc-eventfd.rs +++ b/tests/pass-dep/libc/libc-eventfd.rs @@ -1,4 +1,5 @@ //@only-target: linux +//@only-target: android // test_race, test_blocking_read and test_blocking_write depend on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0 From 484764500af9f012a351d1681fa31c3616a22949 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Mon, 11 Nov 2024 15:38:10 +0300 Subject: [PATCH 2/4] Fixed test target Co-authored-by: Ralf Jung --- tests/fail-dep/libc/libc-epoll-data-race.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/fail-dep/libc/libc-epoll-data-race.rs b/tests/fail-dep/libc/libc-epoll-data-race.rs index 0794867118..7bef687e33 100644 --- a/tests/fail-dep/libc/libc-epoll-data-race.rs +++ b/tests/fail-dep/libc/libc-epoll-data-race.rs @@ -2,8 +2,7 @@ //! and we only read one of them, we do not synchronize with the other events //! and therefore still report a data race for things that need to see the second event //! to be considered synchronized. -//@only-target: linux -//@only-target: android +//@only-target: linux android // ensure deterministic schedule //@compile-flags: -Zmiri-preemption-rate=0 From 1804819111c7049931953b0f3e14bcd62ce5eca4 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Mon, 11 Nov 2024 18:20:26 +0300 Subject: [PATCH 3/4] Added linux_like module --- src/shims/unix/android/foreign_items.rs | 6 +++--- src/shims/unix/fd.rs | 2 +- src/shims/unix/linux/foreign_items.rs | 6 +++--- src/shims/unix/linux/mod.rs | 4 ---- src/shims/unix/{linux => linux_like}/epoll.rs | 0 src/shims/unix/{linux => linux_like}/eventfd.rs | 2 +- src/shims/unix/linux_like/mod.rs | 4 ++++ src/shims/unix/{linux => linux_like}/sync.rs | 0 src/shims/unix/{linux => linux_like}/syscall.rs | 4 ++-- src/shims/unix/mod.rs | 3 ++- src/shims/unix/unnamed_socket.rs | 2 +- 11 files changed, 17 insertions(+), 16 deletions(-) rename src/shims/unix/{linux => linux_like}/epoll.rs (100%) rename src/shims/unix/{linux => linux_like}/eventfd.rs (99%) create mode 100644 src/shims/unix/linux_like/mod.rs rename src/shims/unix/{linux => linux_like}/sync.rs (100%) rename src/shims/unix/{linux => linux_like}/syscall.rs (95%) diff --git a/src/shims/unix/android/foreign_items.rs b/src/shims/unix/android/foreign_items.rs index 4a005093b2..f900388545 100644 --- a/src/shims/unix/android/foreign_items.rs +++ b/src/shims/unix/android/foreign_items.rs @@ -1,10 +1,10 @@ use rustc_abi::ExternAbi; use rustc_span::Symbol; -use self::shims::unix::linux::epoll::EvalContextExt as _; -use self::shims::unix::linux::eventfd::EvalContextExt as _; use crate::shims::unix::android::thread::prctl; -use crate::shims::unix::linux::syscall::syscall; +use crate::shims::unix::linux_like::epoll::EvalContextExt as _; +use crate::shims::unix::linux_like::eventfd::EvalContextExt as _; +use crate::shims::unix::linux_like::syscall::syscall; use crate::*; pub fn is_dyn_sym(_name: &str) -> bool { diff --git a/src/shims/unix/fd.rs b/src/shims/unix/fd.rs index 27bdd508f7..4e80630e67 100644 --- a/src/shims/unix/fd.rs +++ b/src/shims/unix/fd.rs @@ -10,7 +10,7 @@ use std::rc::{Rc, Weak}; use rustc_abi::Size; use crate::helpers::check_min_arg_count; -use crate::shims::unix::linux::epoll::EpollReadyEvents; +use crate::shims::unix::linux_like::epoll::EpollReadyEvents; use crate::shims::unix::*; use crate::*; diff --git a/src/shims/unix/linux/foreign_items.rs b/src/shims/unix/linux/foreign_items.rs index 85f0d6e133..ac767ad050 100644 --- a/src/shims/unix/linux/foreign_items.rs +++ b/src/shims/unix/linux/foreign_items.rs @@ -1,10 +1,10 @@ use rustc_abi::ExternAbi; use rustc_span::Symbol; -use self::shims::unix::linux::epoll::EvalContextExt as _; -use self::shims::unix::linux::eventfd::EvalContextExt as _; use self::shims::unix::linux::mem::EvalContextExt as _; -use self::shims::unix::linux::syscall::syscall; +use self::shims::unix::linux_like::epoll::EvalContextExt as _; +use self::shims::unix::linux_like::eventfd::EvalContextExt as _; +use self::shims::unix::linux_like::syscall::syscall; use crate::machine::{SIGRTMAX, SIGRTMIN}; use crate::shims::unix::*; use crate::*; diff --git a/src/shims/unix/linux/mod.rs b/src/shims/unix/linux/mod.rs index 159e5aca03..c10dc52cb2 100644 --- a/src/shims/unix/linux/mod.rs +++ b/src/shims/unix/linux/mod.rs @@ -1,6 +1,2 @@ -pub mod epoll; -pub mod eventfd; pub mod foreign_items; pub mod mem; -pub mod sync; -pub mod syscall; diff --git a/src/shims/unix/linux/epoll.rs b/src/shims/unix/linux_like/epoll.rs similarity index 100% rename from src/shims/unix/linux/epoll.rs rename to src/shims/unix/linux_like/epoll.rs diff --git a/src/shims/unix/linux/eventfd.rs b/src/shims/unix/linux_like/eventfd.rs similarity index 99% rename from src/shims/unix/linux/eventfd.rs rename to src/shims/unix/linux_like/eventfd.rs index 05276b16f1..e864a24d2e 100644 --- a/src/shims/unix/linux/eventfd.rs +++ b/src/shims/unix/linux_like/eventfd.rs @@ -5,7 +5,7 @@ use std::io::ErrorKind; use crate::concurrency::VClock; use crate::shims::unix::fd::{FileDescriptionRef, WeakFileDescriptionRef}; -use crate::shims::unix::linux::epoll::{EpollReadyEvents, EvalContextExt as _}; +use crate::shims::unix::linux_like::epoll::{EpollReadyEvents, EvalContextExt as _}; use crate::shims::unix::*; use crate::*; diff --git a/src/shims/unix/linux_like/mod.rs b/src/shims/unix/linux_like/mod.rs new file mode 100644 index 0000000000..1a22539a4e --- /dev/null +++ b/src/shims/unix/linux_like/mod.rs @@ -0,0 +1,4 @@ +pub mod epoll; +pub mod eventfd; +pub mod sync; +pub mod syscall; diff --git a/src/shims/unix/linux/sync.rs b/src/shims/unix/linux_like/sync.rs similarity index 100% rename from src/shims/unix/linux/sync.rs rename to src/shims/unix/linux_like/sync.rs diff --git a/src/shims/unix/linux/syscall.rs b/src/shims/unix/linux_like/syscall.rs similarity index 95% rename from src/shims/unix/linux/syscall.rs rename to src/shims/unix/linux_like/syscall.rs index 9f6935f096..e9a32a2632 100644 --- a/src/shims/unix/linux/syscall.rs +++ b/src/shims/unix/linux_like/syscall.rs @@ -1,9 +1,9 @@ use rustc_abi::ExternAbi; use rustc_span::Symbol; -use self::shims::unix::linux::eventfd::EvalContextExt as _; use crate::helpers::check_min_arg_count; -use crate::shims::unix::linux::sync::futex; +use crate::shims::unix::linux_like::eventfd::EvalContextExt as _; +use crate::shims::unix::linux_like::sync::futex; use crate::*; pub fn syscall<'tcx>( diff --git a/src/shims/unix/mod.rs b/src/shims/unix/mod.rs index c8c25c636e..0620b57753 100644 --- a/src/shims/unix/mod.rs +++ b/src/shims/unix/mod.rs @@ -11,6 +11,7 @@ mod unnamed_socket; mod android; mod freebsd; mod linux; +mod linux_like; mod macos; mod solarish; @@ -18,7 +19,7 @@ mod solarish; pub use self::env::{EvalContextExt as _, UnixEnvVars}; pub use self::fd::{EvalContextExt as _, FdTable, FileDescription}; pub use self::fs::{DirTable, EvalContextExt as _}; -pub use self::linux::epoll::EpollInterestTable; +pub use self::linux_like::epoll::EpollInterestTable; pub use self::mem::EvalContextExt as _; pub use self::sync::EvalContextExt as _; pub use self::thread::{EvalContextExt as _, ThreadNameResult}; diff --git a/src/shims/unix/unnamed_socket.rs b/src/shims/unix/unnamed_socket.rs index 8ccce7c198..4815f8703b 100644 --- a/src/shims/unix/unnamed_socket.rs +++ b/src/shims/unix/unnamed_socket.rs @@ -11,7 +11,7 @@ use rustc_abi::Size; use crate::concurrency::VClock; use crate::shims::unix::fd::{FileDescriptionRef, WeakFileDescriptionRef}; -use crate::shims::unix::linux::epoll::{EpollReadyEvents, EvalContextExt as _}; +use crate::shims::unix::linux_like::epoll::{EpollReadyEvents, EvalContextExt as _}; use crate::shims::unix::*; use crate::*; From e3a980cf7781ae48804034554d31d3011e298273 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Mon, 11 Nov 2024 18:54:17 +0300 Subject: [PATCH 4/4] Fix the rest of the tests --- tests/pass-dep/libc/libc-epoll-blocking.rs | 3 +-- tests/pass-dep/libc/libc-epoll-no-blocking.rs | 3 +-- tests/pass-dep/libc/libc-eventfd.rs | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/pass-dep/libc/libc-epoll-blocking.rs b/tests/pass-dep/libc/libc-epoll-blocking.rs index d45dc5af48..e3c42b2701 100644 --- a/tests/pass-dep/libc/libc-epoll-blocking.rs +++ b/tests/pass-dep/libc/libc-epoll-blocking.rs @@ -1,5 +1,4 @@ -//@only-target: linux -//@only-target: android +//@only-target: linux android // test_epoll_block_then_unblock and test_epoll_race depend on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0 diff --git a/tests/pass-dep/libc/libc-epoll-no-blocking.rs b/tests/pass-dep/libc/libc-epoll-no-blocking.rs index 59141cd5dc..111e639c86 100644 --- a/tests/pass-dep/libc/libc-epoll-no-blocking.rs +++ b/tests/pass-dep/libc/libc-epoll-no-blocking.rs @@ -1,5 +1,4 @@ -//@only-target: linux -//@only-target: android +//@only-target: linux android use std::convert::TryInto; diff --git a/tests/pass-dep/libc/libc-eventfd.rs b/tests/pass-dep/libc/libc-eventfd.rs index 767ab1065c..538a157a43 100644 --- a/tests/pass-dep/libc/libc-eventfd.rs +++ b/tests/pass-dep/libc/libc-eventfd.rs @@ -1,5 +1,4 @@ -//@only-target: linux -//@only-target: android +//@only-target: linux android // test_race, test_blocking_read and test_blocking_write depend on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0