Skip to content

Commit b2777ab

Browse files
committed
End-to-end regression test for 97463.
incorporated review feedback, with comment explaining why this is calling CC instead of COMPILE_OBJ or NATIVE_STATICLIB. As drive-by, removed some other unnecessary commands from the recipe.
1 parent 7913b85 commit b2777ab

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-include ../tools.mk
2+
3+
# The issue exercised by this test, rust-lang/rust#97463, explicitly needs `-O`
4+
# flags (like `-O3`) to reproduce. Thus, we call $(CC) instead of nicer
5+
# alternatives provided by tools.mk like using `COMPILE_OBJ` or using a
6+
# `NATIVE_STATICLIB` dependency.
7+
8+
all:
9+
$(CC) -c -O3 -o $(TMPDIR)/bad.o bad.c
10+
$(AR) rcs $(TMPDIR)/libbad.a $(TMPDIR)/bad.o
11+
$(RUSTC) param_passing.rs -L$(TMPDIR) -lbad -C opt-level=3
12+
$(call RUN,param_passing)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <stdio.h>
4+
5+
6+
struct bloc {
7+
uint16_t a;
8+
uint16_t b;
9+
uint16_t c;
10+
};
11+
12+
uint16_t c_read_value(uint32_t a, uint32_t b, uint32_t c) {
13+
struct bloc *data = malloc(sizeof(struct bloc));
14+
15+
data->a = a & 0xFFFF;
16+
data->b = b & 0xFFFF;
17+
data->c = c & 0xFFFF;
18+
19+
printf("C struct: a = %u, b = %u, c = %u\n",
20+
(unsigned) data->a, (unsigned) data->b, (unsigned) data->c);
21+
printf("C function returns %u\n", (unsigned) data->b);
22+
23+
return data->b; /* leak data */
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// NOTE: Exposing the bug encoded in this test is sensitive to
2+
// LLVM optimization choices. See additional note below for an
3+
// example.
4+
5+
#[link(name = "bad")]
6+
extern "C" {
7+
pub fn c_read_value(a: u32, b: u32, c: u32) -> u16;
8+
}
9+
10+
fn main() {
11+
const C1: usize = 0x327b23c6;
12+
const C2: usize = C1 & 0xFFFF;
13+
14+
let r1: usize = 0x0;
15+
let r2: usize = C1;
16+
let r3: usize = 0x0;
17+
let value: u16 = unsafe { c_read_value(r1 as u32, r2 as u32, r3 as u32) };
18+
19+
// NOTE: as an example of the sensitivity of this test to optimization choices,
20+
// uncommenting this block of code makes the bug go away on pnkfeix's machine.
21+
// (But observing via `dbg!` doesn't hide the bug. At least sometimes.)
22+
/*
23+
println!("{}", value);
24+
println!("{}", value as usize);
25+
println!("{}", usize::from(value));
26+
println!("{}", (value as usize) & 0xFFFF);
27+
*/
28+
29+
let d1 = value;
30+
let d2 = value as usize;
31+
let d3 = usize::from(value);
32+
let d4 = (value as usize) & 0xFFFF;
33+
34+
let d = (&d1, &d2, &d3, &d4);
35+
let d_ = (d1, d2, d3, d4);
36+
37+
assert_eq!(((&(C2 as u16), &C2, &C2, &C2), (C2 as u16, C2, C2, C2)), (d, d_));
38+
}

0 commit comments

Comments
 (0)