Skip to content

Commit 7df02ab

Browse files
authored
Merge pull request #567 from immunant/tests.refs.fields
add test for fields of references
2 parents 241ac8a + 41d6fe2 commit 7df02ab

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

analysis/test/src/pointers.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ pub unsafe extern "C" fn test_unique_ref() {
376376
*fresh1 = &mut x as *mut i32;
377377
}
378378
#[no_mangle]
379+
pub unsafe extern "C" fn test_ref_field() {
380+
let t = T {
381+
field: 0i32,
382+
field2: 0u64,
383+
field3: 0 as *const S,
384+
field4: 0i32,
385+
};
386+
387+
let ref mut s = S {
388+
field: 0i32,
389+
field2: 0u64,
390+
field3: 0 as *const S,
391+
field4: t,
392+
};
393+
s.field4.field4 = s.field4.field4;
394+
}
395+
#[no_mangle]
379396
pub unsafe extern "C" fn test_realloc_reassign() {
380397
let mut s = malloc(::std::mem::size_of::<S>() as libc::c_ulong);
381398
s = realloc(s, 2 * mem::size_of::<S>() as c_ulong);
@@ -519,6 +536,7 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib
519536
test_load_value_store_value();
520537
let nums = &mut [2i32, 5i32, 3i32, 1i32, 6i32];
521538
insertion_sort(nums.len() as libc::c_int, nums as *mut libc::c_int);
539+
test_ref_field();
522540
return 0i32;
523541
}
524542
pub fn main() {

dynamic_instrumentation/src/instrument.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,12 @@ impl<'tcx> Visitor<'tcx> for InstrumentationAdder<'_, 'tcx> {
232232
Rvalue::AddressOf(_, p)
233233
if has_outer_deref(p)
234234
&& place_ty(&remove_outer_deref(*p, self.tcx())).is_region_ptr() =>
235-
{
235+
{
236+
let source = remove_outer_deref(*p, self.tcx());
236237
// Instrument which local's address is taken
237238
self.loc(location.successor_within_block(), copy_fn)
238239
.arg_var(dest)
239-
.source(p)
240+
.source(&source)
240241
.dest(&dest)
241242
.debug_mir(location)
242243
.add_to(self);

pdg/src/snapshots/c2rust_pdg__tests__analysis_test_pdg_snapshot.snap

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ g {
6464
n[1]: copy n[0] => _2 @ bb0[0]: fn push; _36 = push(move _37, move _38);
6565
n[2]: value.store _ => _20.* @ bb4[8]: fn invalid; (*_20) = const 0_usize as *mut pointers::S (Misc);
6666
n[3]: value.store _ => _17.* @ bb8[5]: fn fdevent_unregister; (*_17) = const 0_usize as *mut pointers::fdnode_st (Misc);
67+
n[4]: int_to_ptr _ => _2 @ bb0[3]: fn test_ref_field; _2 = const 0_usize as *const pointers::S (Misc);
68+
n[5]: int_to_ptr _ => _5 @ bb0[9]: fn test_ref_field; _5 = const 0_usize as *const pointers::S (Misc);
6769
}
6870
nodes_that_need_write = []
6971

@@ -165,59 +167,59 @@ g {
165167
n[1]: copy n[0] => _7 @ bb2[14]: fn exercise_allocator; _7 = move _8 as *const u8 (Pointer(ArrayToPointer));
166168
n[2]: copy n[1] => _6 @ bb2[16]: fn exercise_allocator; _6 = move _7 as *const i8 (Misc);
167169
n[3]: copy n[2] => _1 @ bb0[0]: fn printf; _5 = printf(move _6, move _10);
168-
n[4]: copy n[0] => _30 @ bb11[7]: fn exercise_allocator; _30 = &raw const (*_31);
170+
n[4]: copy _ => _30 @ bb11[7]: fn exercise_allocator; _30 = &raw const (*_31);
169171
n[5]: copy n[4] => _29 @ bb11[8]: fn exercise_allocator; _29 = move _30 as *const u8 (Pointer(ArrayToPointer));
170172
n[6]: copy n[5] => _28 @ bb11[10]: fn exercise_allocator; _28 = move _29 as *const i8 (Misc);
171173
n[7]: copy n[6] => _1 @ bb0[0]: fn printf; _27 = printf(move _28, move _32);
172-
n[8]: copy n[0] => _30 @ bb11[7]: fn exercise_allocator; _30 = &raw const (*_31);
174+
n[8]: copy _ => _30 @ bb11[7]: fn exercise_allocator; _30 = &raw const (*_31);
173175
n[9]: copy n[8] => _29 @ bb11[8]: fn exercise_allocator; _29 = move _30 as *const u8 (Pointer(ArrayToPointer));
174176
n[10]: copy n[9] => _28 @ bb11[10]: fn exercise_allocator; _28 = move _29 as *const i8 (Misc);
175177
n[11]: copy n[10] => _1 @ bb0[0]: fn printf; _27 = printf(move _28, move _32);
176-
n[12]: copy n[0] => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
178+
n[12]: copy _ => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
177179
n[13]: copy n[12] => _59 @ bb29[8]: fn exercise_allocator; _59 = move _60 as *const u8 (Pointer(ArrayToPointer));
178180
n[14]: copy n[13] => _58 @ bb29[10]: fn exercise_allocator; _58 = move _59 as *const i8 (Misc);
179181
n[15]: copy n[14] => _1 @ bb0[0]: fn printf; _57 = printf(move _58, move _62);
180-
n[16]: copy n[0] => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
182+
n[16]: copy _ => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
181183
n[17]: copy n[16] => _59 @ bb29[8]: fn exercise_allocator; _59 = move _60 as *const u8 (Pointer(ArrayToPointer));
182184
n[18]: copy n[17] => _58 @ bb29[10]: fn exercise_allocator; _58 = move _59 as *const i8 (Misc);
183185
n[19]: copy n[18] => _1 @ bb0[0]: fn printf; _57 = printf(move _58, move _62);
184-
n[20]: copy n[0] => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
186+
n[20]: copy _ => _60 @ bb29[7]: fn exercise_allocator; _60 = &raw const (*_61);
185187
n[21]: copy n[20] => _59 @ bb29[8]: fn exercise_allocator; _59 = move _60 as *const u8 (Pointer(ArrayToPointer));
186188
n[22]: copy n[21] => _58 @ bb29[10]: fn exercise_allocator; _58 = move _59 as *const i8 (Misc);
187189
n[23]: copy n[22] => _1 @ bb0[0]: fn printf; _57 = printf(move _58, move _62);
188-
n[24]: copy n[0] => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
190+
n[24]: copy _ => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
189191
n[25]: copy n[24] => _92 @ bb49[8]: fn exercise_allocator; _92 = move _93 as *const u8 (Pointer(ArrayToPointer));
190192
n[26]: copy n[25] => _91 @ bb49[10]: fn exercise_allocator; _91 = move _92 as *const i8 (Misc);
191193
n[27]: copy n[26] => _1 @ bb0[0]: fn printf; _90 = printf(move _91, move _95);
192-
n[28]: copy n[0] => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
194+
n[28]: copy _ => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
193195
n[29]: copy n[28] => _92 @ bb49[8]: fn exercise_allocator; _92 = move _93 as *const u8 (Pointer(ArrayToPointer));
194196
n[30]: copy n[29] => _91 @ bb49[10]: fn exercise_allocator; _91 = move _92 as *const i8 (Misc);
195197
n[31]: copy n[30] => _1 @ bb0[0]: fn printf; _90 = printf(move _91, move _95);
196-
n[32]: copy n[0] => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
198+
n[32]: copy _ => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
197199
n[33]: copy n[32] => _92 @ bb49[8]: fn exercise_allocator; _92 = move _93 as *const u8 (Pointer(ArrayToPointer));
198200
n[34]: copy n[33] => _91 @ bb49[10]: fn exercise_allocator; _91 = move _92 as *const i8 (Misc);
199201
n[35]: copy n[34] => _1 @ bb0[0]: fn printf; _90 = printf(move _91, move _95);
200-
n[36]: copy n[0] => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
202+
n[36]: copy _ => _93 @ bb49[7]: fn exercise_allocator; _93 = &raw const (*_94);
201203
n[37]: copy n[36] => _92 @ bb49[8]: fn exercise_allocator; _92 = move _93 as *const u8 (Pointer(ArrayToPointer));
202204
n[38]: copy n[37] => _91 @ bb49[10]: fn exercise_allocator; _91 = move _92 as *const i8 (Misc);
203205
n[39]: copy n[38] => _1 @ bb0[0]: fn printf; _90 = printf(move _91, move _95);
204-
n[40]: copy n[0] => _8 @ bb2[13]: fn simple_analysis; _8 = &raw const (*_9);
206+
n[40]: copy _ => _8 @ bb2[13]: fn simple_analysis; _8 = &raw const (*_9);
205207
n[41]: copy n[40] => _7 @ bb2[14]: fn simple_analysis; _7 = move _8 as *const u8 (Pointer(ArrayToPointer));
206208
n[42]: copy n[41] => _6 @ bb2[16]: fn simple_analysis; _6 = move _7 as *const i8 (Misc);
207209
n[43]: copy n[42] => _1 @ bb0[0]: fn printf; _5 = printf(move _6, move _10);
208-
n[44]: copy n[0] => _5 @ bb0[7]: fn analysis2_helper; _5 = &raw const (*_6);
210+
n[44]: copy n[42] => _5 @ bb0[7]: fn analysis2_helper; _5 = &raw const (*_6);
209211
n[45]: copy n[44] => _4 @ bb0[8]: fn analysis2_helper; _4 = move _5 as *const u8 (Pointer(ArrayToPointer));
210212
n[46]: copy n[45] => _3 @ bb0[10]: fn analysis2_helper; _3 = move _4 as *const i8 (Misc);
211213
n[47]: copy n[46] => _1 @ bb0[0]: fn printf; _2 = printf(move _3, move _7);
212-
n[48]: copy n[0] => _8 @ bb2[13]: fn inter_function_analysis; _8 = &raw const (*_9);
214+
n[48]: copy _ => _8 @ bb2[13]: fn inter_function_analysis; _8 = &raw const (*_9);
213215
n[49]: copy n[48] => _7 @ bb2[14]: fn inter_function_analysis; _7 = move _8 as *const u8 (Pointer(ArrayToPointer));
214216
n[50]: copy n[49] => _6 @ bb2[16]: fn inter_function_analysis; _6 = move _7 as *const i8 (Misc);
215217
n[51]: copy n[50] => _1 @ bb0[0]: fn printf; _5 = printf(move _6, move _10);
216-
n[52]: copy n[0] => _10 @ bb2[20]: fn invalid; _10 = &raw const (*_11);
218+
n[52]: copy _ => _10 @ bb2[20]: fn invalid; _10 = &raw const (*_11);
217219
n[53]: copy n[52] => _9 @ bb2[21]: fn invalid; _9 = move _10 as *const u8 (Pointer(ArrayToPointer));
218220
n[54]: copy n[53] => _8 @ bb2[23]: fn invalid; _8 = move _9 as *const i8 (Misc);
219221
n[55]: copy n[54] => _1 @ bb0[0]: fn printf; _7 = printf(move _8, move _12);
220-
n[56]: copy n[0] => _16 @ bb3[11]: fn invalid; _16 = &raw const (*_17);
222+
n[56]: copy _ => _16 @ bb3[11]: fn invalid; _16 = &raw const (*_17);
221223
n[57]: copy n[56] => _15 @ bb3[12]: fn invalid; _15 = move _16 as *const u8 (Pointer(ArrayToPointer));
222224
n[58]: copy n[57] => _14 @ bb3[14]: fn invalid; _14 = move _15 as *const i8 (Misc);
223225
n[59]: copy n[58] => _1 @ bb0[0]: fn printf; _13 = printf(move _14, move _18);
@@ -533,7 +535,7 @@ g {
533535
n[2]: copy n[1] => _1 @ bb0[0]: fn connection_accepted; _13 = connection_accepted(move _14, const 0_i32);
534536
n[3]: field.0 n[2] => _10 @ bb2[10]: fn connection_accepted; _10 = ((*_1).0: *mut pointers::fdevents);
535537
n[4]: addr.load n[3] => _ @ bb2[10]: fn connection_accepted; _10 = ((*_1).0: *mut pointers::fdevents);
536-
n[5]: copy n[0] => _16 @ bb5[5]: fn lighttpd_test; _16 = &raw mut (*_10);
538+
n[5]: copy n[3] => _16 @ bb5[5]: fn lighttpd_test; _16 = &raw mut (*_10);
537539
n[6]: copy n[5] => _1 @ bb0[0]: fn connection_close; _15 = connection_close(move _16, move _17);
538540
n[7]: field.0 n[6] => _4 @ bb0[2]: fn connection_close; _4 = ((*_1).0: *mut pointers::fdevents);
539541
n[8]: addr.load n[7] => _ @ bb0[2]: fn connection_close; _4 = ((*_1).0: *mut pointers::fdevents);
@@ -664,7 +666,7 @@ g {
664666
n[3]: copy n[2] => _1 @ bb0[0]: fn shared_ref_foo; _4 = shared_ref_foo(move _5);
665667
n[4]: copy n[3] => _0 @ bb0[1]: fn shared_ref_foo; _0 = _1;
666668
n[5]: copy n[4] => _4 @ bb1[0]: fn test_shared_ref; _4 = shared_ref_foo(move _5);
667-
n[6]: copy n[0] => _6 @ bb1[4]: fn test_shared_ref; _6 = &raw const (*_4);
669+
n[6]: copy n[5] => _6 @ bb1[4]: fn test_shared_ref; _6 = &raw const (*_4);
668670
}
669671
nodes_that_need_write = []
670672

@@ -964,6 +966,17 @@ g {
964966
}
965967
nodes_that_need_write = [75, 74, 73, 66, 65, 64, 63, 62, 61, 54, 53, 52, 45, 44, 43, 33, 32, 31, 27, 26, 25, 15, 14, 13, 6, 5, 4, 0]
966968

967-
num_graphs = 76
968-
num_nodes = 658
969+
g {
970+
n[0]: &_4 _ => _3 @ bb0[15]: fn test_ref_field; _3 = &mut _4;
971+
n[1]: field.3 n[0] => _ @ bb0[17]: fn test_ref_field; _7 = (((*_3).3: pointers::T).3: i32);
972+
n[2]: field.3 n[1] => _7 @ bb0[17]: fn test_ref_field; _7 = (((*_3).3: pointers::T).3: i32);
973+
n[3]: addr.load n[2] => _ @ bb0[17]: fn test_ref_field; _7 = (((*_3).3: pointers::T).3: i32);
974+
n[4]: field.3 n[0] => _ @ bb0[18]: fn test_ref_field; (((*_3).3: pointers::T).3: i32) = move _7;
975+
n[5]: field.3 n[4] => _ @ bb0[18]: fn test_ref_field; (((*_3).3: pointers::T).3: i32) = move _7;
976+
n[6]: addr.store n[5] => _ @ bb0[18]: fn test_ref_field; (((*_3).3: pointers::T).3: i32) = move _7;
977+
}
978+
nodes_that_need_write = [6, 5, 4, 0]
979+
980+
num_graphs = 77
981+
num_nodes = 667
969982

0 commit comments

Comments
 (0)