Skip to content

Commit fd0534d

Browse files
author
Lander Brandt
committed
fix issues with tests on Android
1 parent 494cde2 commit fd0534d

File tree

12 files changed

+61
-8
lines changed

12 files changed

+61
-8
lines changed

ci.sh

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function run_tests {
2121
fi
2222

2323
./miri test --locked
24+
2425
if [ -z "${MIRI_TEST_TARGET+exists}" ]; then
2526
# Only for host architecture: tests with optimizations (`-O` is what cargo passes, but crank MIR
2627
# optimizations up all the way).
@@ -48,6 +49,7 @@ case $HOST_TARGET in
4849
MIRI_TEST_TARGET=i686-unknown-linux-gnu run_tests
4950
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
5051
MIRI_TEST_TARGET=i686-pc-windows-msvc run_tests
52+
MIRI_TEST_TARGET=aarch64-linux-android run_tests
5153
;;
5254
x86_64-apple-darwin)
5355
MIRI_TEST_TARGET=mips64-unknown-linux-gnuabi64 run_tests # big-endian architecture

src/helpers.rs

+11
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
437437
)
438438
}
439439

440+
/// Helper function used inside the shims of foreign functions to assert that the target OS
441+
/// is based on the Linux kernel. It panics showing a message with the `name` of the foreign function
442+
/// if this is not the case.
443+
fn assert_linux_based_target_os(&self, name: &str) {
444+
assert!(
445+
matches!(self.eval_context_ref().tcx.sess.target.os.as_str(), "linux" | "android"),
446+
"`{}` is only available for supported Linux-based targets",
447+
name,
448+
)
449+
}
450+
440451
/// Helper function used inside the shims of foreign functions to assert that the target OS
441452
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
442453
/// if this is not the case.

src/machine.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,23 @@ impl MemoryExtra {
253253
this.write_scalar(Scalar::from_u8(0), &place.into())?;
254254
Self::add_extern_static(this, "_tls_used", place.ptr);
255255
}
256-
"android" =>
256+
"android" => {
257+
// "environ"
258+
Self::add_extern_static(
259+
this,
260+
"environ",
261+
this.machine.env_vars.environ.unwrap().ptr,
262+
);
263+
// A couple zero-initialized pointer-sized extern statics.
264+
// Most of them are for weak symbols, which we all set to null (indicating that the
265+
// symbol is not supported, and triggering fallback code which ends up calling a
266+
// syscall that we do support).
267+
for name in &["__cxa_thread_atexit_impl", "getrandom", "statx"] {
268+
let layout = this.machine.layouts.usize;
269+
let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
270+
this.write_scalar(Scalar::from_machine_usize(0, this), &place.into())?;
271+
Self::add_extern_static(this, name, place.ptr);
272+
}
257273
for symbol_name in &["signal", "bsd_signal"] {
258274
let layout = this.machine.layouts.usize;
259275
let dlsym = Dlsym::from_str(symbol_name.as_bytes(), &this.tcx.sess.target.os)?
@@ -268,8 +284,9 @@ impl MemoryExtra {
268284
let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
269285
this.write_pointer(ptr, &place.into())?;
270286

271-
Self::add_extern_static(this, symbol_name, place.ptr.into());
272-
},
287+
Self::add_extern_static(this, symbol_name, place.ptr);
288+
}
289+
}
273290
_ => {} // No "extern statics" supported on this target
274291
}
275292
Ok(())

src/shims/env.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
88
use rustc_middle::ty::layout::LayoutOf;
99
use rustc_target::abi::Size;
1010

11+
use crate::helpers::target_os_is_unix;
1112
use crate::*;
1213

1314
/// Check whether an operation that writes to a target buffer was successful.
@@ -57,7 +58,7 @@ impl<'tcx> EnvVars<'tcx> {
5758
};
5859
if forward {
5960
let var_ptr = match target_os {
60-
"linux" | "macos" =>
61+
target if target_os_is_unix(target) =>
6162
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx)?,
6263
"windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?,
6364
unsupported =>

src/shims/posix/android/foreign_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_middle::mir;
22
use rustc_span::Symbol;
3+
use rustc_target::abi::{Align, Size};
34
use rustc_target::spec::abi::Abi;
45

56
use crate::*;

src/shims/posix/foreign_items.rs

+17
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
166166
}
167167
this.write_null(dest)?;
168168
}
169+
"memalign" => {
170+
let &[ref align, ref size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
171+
let align = this.read_scalar(align)?.to_machine_usize(this)?;
172+
let size = this.read_scalar(size)?.to_machine_usize(this)?;
173+
174+
// Align must be power of 2.
175+
if !align.is_power_of_two() {
176+
throw_ub_format!("memalign: alignment must be a power of two, but is {}", align);
177+
}
178+
179+
let ptr = this.memory.allocate(
180+
Size::from_bytes(size),
181+
Align::from_bytes(align).unwrap(),
182+
MiriMemoryKind::C.into(),
183+
)?;
184+
this.write_pointer(ptr, dest)?;
185+
}
169186

170187
// Dynamic symbol loading
171188
"dlsym" => {

src/shims/posix/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
937937
) -> InterpResult<'tcx, i32> {
938938
let this = self.eval_context_mut();
939939

940-
this.assert_target_os("linux", "statx");
940+
this.assert_linux_based_target_os("statx");
941941

942942
let statxbuf_ptr = this.read_pointer(statxbuf_op)?;
943943
let pathname_ptr = this.read_pointer(pathname_op)?;
@@ -1227,7 +1227,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
12271227
fn linux_readdir64(&mut self, dirp_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
12281228
let this = self.eval_context_mut();
12291229

1230-
this.assert_target_os("linux", "readdir64");
1230+
this.assert_linux_based_target_os("readdir64");
12311231

12321232
let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?;
12331233

src/shims/posix/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
106106
_arg5: &OpTy<'tcx, Tag>,
107107
) -> InterpResult<'tcx, i32> {
108108
let this = self.eval_context_mut();
109-
this.assert_target_os("linux", "prctl");
109+
this.assert_linux_based_target_os("prctl");
110110

111111
let option = this.read_scalar(option)?.to_i32()?;
112112
if option == this.eval_libc_i32("PR_SET_NAME")? {

src/shims/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2020
) -> InterpResult<'tcx, i32> {
2121
let this = self.eval_context_mut();
2222

23-
this.assert_target_os("linux", "clock_gettime");
23+
this.assert_linux_based_target_os("clock_gettime");
2424
this.check_no_isolation("`clock_gettime`")?;
2525

2626
let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;

tests/compile-fail/concurrency/thread-spawn.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// ignore-linux: Only Windows is not supported.
22
// ignore-macos: Only Windows is not supported.
3+
// ignore-android: Only Windows is not supported.
34

45
use std::thread;
56

tests/compiletest.rs

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ fn run_tests(mode: &str, path: &str, target: &str) {
4343
config.src_base = PathBuf::from(path);
4444
config.target = target.to_owned();
4545
config.target_rustcflags = Some(flags);
46+
// Android targets require this to be set so that UI tests don't fail
47+
config.adb_device_status = true;
4648
compiletest::run_tests(&config);
4749
}
4850

tests/run-pass/wtf8.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// ignore-linux: tests Windows-only APIs
22
// ignore-macos: tests Windows-only APIs
3+
// ignore-android: tests Windows-only APIs
34

45
use std::os::windows::ffi::{OsStrExt, OsStringExt};
56
use std::ffi::{OsStr, OsString};

0 commit comments

Comments
 (0)