Skip to content

Commit d14235c

Browse files
committed
disable mut pointer accesses, various test fixes
1 parent 6e23a4d commit d14235c

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

src/tools/miri/src/shims/native_lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,15 @@ fn imm_to_carg<'tcx>(v: ImmTy<'tcx>, cx: &impl HasDataLayout) -> InterpResult<'t
237237
ty::Uint(UintTy::U64) => CArg::UInt64(v.to_scalar().to_u64()?),
238238
ty::Uint(UintTy::Usize) =>
239239
CArg::USize(v.to_scalar().to_target_usize(cx)?.try_into().unwrap()),
240-
ty::RawPtr(..) => {
241-
let s = v.to_scalar().to_pointer(cx)?.addr();
242-
// This relies on the `expose_provenance` in `addr_from_alloc_id`.
243-
CArg::RawPtr(std::ptr::with_exposed_provenance_mut(s.bytes_usize()))
240+
ty::RawPtr(_, mutability) => {
241+
// Arbitrary mutable pointer accesses are not currently supported in Miri.
242+
if mutability.is_mut() {
243+
throw_unsup_format!("unsupported mutable pointer type for native call: {}", v.layout.ty);
244+
} else {
245+
let s = v.to_scalar().to_pointer(cx)?.addr();
246+
// This relies on the `expose_provenance` in `addr_from_alloc_id`.
247+
CArg::RawPtr(std::ptr::with_exposed_provenance_mut(s.bytes_usize()))
248+
}
244249
},
245250
_ => throw_unsup_format!("unsupported argument type for native call: {}", v.layout.ty),
246251
})

src/tools/miri/tests/native-lib/native-lib.map

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ CODEABI_1.0 {
1717

1818
# The rest remains private.
1919
local: *;
20-
};
20+
};

src/tools/miri/tests/native-lib/pass/ptr_read_access.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ fn main() {
1111
test_static();
1212
}
1313

14-
// Test function that dereferences a pointer and prints its contents from C.
14+
// Test void function that dereferences a pointer and prints its contents from C.
1515
fn test_pointer() {
1616
extern "C" {
17-
fn print_pointer(ptr: *mut i32);
17+
fn print_pointer(ptr: *const i32);
1818
}
1919

20-
let mut x = 42;
21-
let ptr = &mut x as *mut i32;
20+
let x = 42;
2221

23-
unsafe { print_pointer(ptr) };
22+
unsafe { print_pointer(&x) };
2423
}
2524

2625
// Test function that dereferences a simple struct pointer and accesses a field.
@@ -31,14 +30,12 @@ fn test_simple() {
3130
}
3231

3332
extern "C" {
34-
fn access_simple(s_ptr: *mut Simple) -> i32;
33+
fn access_simple(s_ptr: *const Simple) -> i32;
3534
}
3635

37-
let mut simple = Simple { field: -42 };
38-
let s_ptr = &mut simple as *mut Simple;
36+
let simple = Simple { field: -42 };
3937

40-
let result = unsafe { access_simple(s_ptr) };
41-
assert_eq!(result, -42);
38+
assert_eq!(unsafe { access_simple(&simple) }, -42);
4239
}
4340

4441
// Test function that dereferences nested struct pointers and accesses fields.
@@ -53,17 +50,14 @@ fn test_nested() {
5350
}
5451

5552
extern "C" {
56-
fn access_nested(n_ptr: *mut Nested) -> i32;
53+
fn access_nested(n_ptr: *const Nested) -> i32;
5754
}
5855

59-
let mut nested_0 = Nested { value: 0, next: None };
60-
let mut nested_1 = Nested { value: 1, next: NonNull::new(&mut nested_0) };
61-
let mut nested_2 = Nested { value: 2, next: NonNull::new(&mut nested_1) };
62-
let mut nested_3 = Nested { value: 3, next: NonNull::new(&mut nested_2) };
63-
let n_ptr = &mut nested_3 as *mut Nested;
56+
let mut nested_0 = Nested { value: 97, next: None };
57+
let mut nested_1 = Nested { value: 98, next: NonNull::new(&mut nested_0) };
58+
let nested_2 = Nested { value: 99, next: NonNull::new(&mut nested_1) };
6459

65-
let result = unsafe { access_nested(n_ptr) };
66-
assert_eq!(result, 0);
60+
assert_eq!(unsafe { access_nested(&nested_2) }, 97);
6761
}
6862

6963
// Test function that dereferences static struct pointers and accesses fields.
@@ -84,6 +78,5 @@ fn test_static() {
8478
recurse: &STATIC,
8579
};
8680

87-
let result = unsafe { access_static(&STATIC) };
88-
assert_eq!(result, 9001);
89-
}
81+
assert_eq!(unsafe { access_static(&STATIC) }, 9001);
82+
}

src/tools/miri/tests/native-lib/pass/scalar_arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ fn main() {
4343
// test void function that prints from C
4444
printer();
4545
}
46-
}
46+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
#include <stdio.h>
22

3-
/* Test. */
3+
/* Test: `test_pointer` */
44

5-
void print_pointer(int *ptr) {
5+
void print_pointer(const int *ptr) {
66
printf("printing pointer dereference from C: %d\n", *ptr);
77
}
88

9-
/* Test. */
9+
/* Test: `test_simple` */
1010

1111
typedef struct Simple {
1212
int field;
1313
} Simple;
1414

15-
int access_simple(Simple *s_ptr) {
15+
int access_simple(const Simple *s_ptr) {
1616
return s_ptr->field;
1717
}
1818

19-
/* Test. */
19+
/* Test: `test_nested` */
2020

2121
typedef struct Nested {
2222
int value;
2323
struct Nested *next;
2424
} Nested;
2525

26-
// Returns the innermost/last `value` of the `Nested` pointer chain.
27-
int access_nested(Nested *n_ptr) {
28-
// Edge case: `n_ptr == NULL`, first Nested is None).
26+
// Returns the innermost/last `value` of a `Nested` pointer chain.
27+
int access_nested(const Nested *n_ptr) {
28+
// Edge case: `n_ptr == NULL`, first `Nested` is None).
2929
if (!n_ptr) { return 0; }
3030

3131
while (n_ptr->next) {
@@ -35,13 +35,13 @@ int access_nested(Nested *n_ptr) {
3535
return n_ptr->value;
3636
}
3737

38-
/* Test. */
38+
/* Test: `test_static */
3939

4040
typedef struct Static {
4141
int value;
4242
struct Static *recurse;
4343
} Static;
4444

45-
int access_static(Static *s_ptr) {
45+
int access_static(const Static *s_ptr) {
4646
return s_ptr->recurse->recurse->value;
4747
}

src/tools/miri/tests/native-lib/scalar_arguments.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ short add_int16(short x) {
2424

2525
long add_short_to_long(short x, long y) {
2626
return x + y;
27-
}
27+
}

src/tools/miri/tests/ui.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,25 @@ fn build_native_lib() -> PathBuf {
3636
// Create the directory if it does not already exist.
3737
std::fs::create_dir_all(&so_target_dir)
3838
.expect("Failed to create directory for shared object file");
39-
/*let so_file_path = so_target_dir.join("scalar_arguments.so");*/
4039
let so_file_path = so_target_dir.join("native-lib.so");
4140
let cc_output = Command::new(cc)
4241
.args([
4342
"-shared",
4443
"-o",
4544
so_file_path.to_str().unwrap(),
45+
// FIXME: Automate gathering of all relevant C source files in the directory.
4646
"tests/native-lib/scalar_arguments.c",
4747
"tests/native-lib/ptr_read_access.c",
4848
// Only add the functions specified in libcode.version to the shared object file.
4949
// This is to avoid automatically adding `malloc`, etc.
5050
// Source: https://anadoxin.org/blog/control-over-symbol-exports-in-gcc.html/
5151
"-fPIC",
5252
"-Wl,--version-script=tests/native-lib/native-lib.map",
53-
"-Werror",
53+
// Ensure we notice serious problems in the C code.
54+
"-Wall",
5455
"-Wextra",
5556
"-Wpedantic",
57+
"-Werror",
5658
])
5759
.output()
5860
.expect("failed to generate shared object file for testing native function calls");

0 commit comments

Comments
 (0)