Skip to content

Commit 5fb9325

Browse files
committed
rustc: Fix a bunch of memory management bugs relating to generic interior vectors. Uncomment all tests in lib-ivec.
1 parent 68122ed commit 5fb9325

File tree

3 files changed

+110
-12
lines changed

3 files changed

+110
-12
lines changed

src/comp/middle/trans.rs

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,8 @@ fn dynamic_size_of(&@block_ctxt cx, ty::t t) -> result {
13271327
auto bcx = rslt.bcx;
13281328
auto llunitszptr = rslt.val;
13291329
auto llunitsz = bcx.build.Load(llunitszptr);
1330-
auto llsz = bcx.build.Add(llsize_of(T_opaque_ivec()), llunitsz);
1330+
auto llsz = bcx.build.Add(llsize_of(T_opaque_ivec()),
1331+
bcx.build.Mul(llunitsz, C_uint(abi::ivec_default_length)));
13311332
ret res(bcx, llsz);
13321333
}
13331334
}
@@ -2969,12 +2970,23 @@ fn memmove_ty(&@block_ctxt cx, ValueRef dst, ValueRef src, &ty::t t) ->
29692970
} else { ret res(cx, cx.build.Store(cx.build.Load(src), dst)); }
29702971
}
29712972

2973+
// Duplicates the heap-owned memory owned by a value of the given type.
2974+
fn duplicate_heap_parts(&@block_ctxt cx, ValueRef vptr, ty::t typ) -> result {
2975+
alt (ty::struct(cx.fcx.lcx.ccx.tcx, typ)) {
2976+
case (ty::ty_ivec(?tm)) {
2977+
ret ivec::duplicate_heap_part(cx, vptr, tm.ty);
2978+
}
2979+
case (ty::ty_str) {
2980+
ret ivec::duplicate_heap_part(cx, vptr,
2981+
ty::mk_mach(cx.fcx.lcx.ccx.tcx, common::ty_u8));
2982+
}
2983+
}
2984+
}
2985+
29722986
tag copy_action { INIT; DROP_EXISTING; }
29732987

2974-
// FIXME: This should copy the contents of the heap part for ivecs.
29752988
fn copy_val(&@block_ctxt cx, copy_action action, ValueRef dst, ValueRef src,
29762989
&ty::t t) -> result {
2977-
29782990
if (ty::type_is_scalar(cx.fcx.lcx.ccx.tcx, t) ||
29792991
ty::type_is_native(cx.fcx.lcx.ccx.tcx, t)) {
29802992
ret res(cx, cx.build.Store(src, dst));
@@ -2991,7 +3003,11 @@ fn copy_val(&@block_ctxt cx, copy_action action, ValueRef dst, ValueRef src,
29913003
ty::type_has_dynamic_size(cx.fcx.lcx.ccx.tcx, t)) {
29923004
auto r = take_ty(cx, src, t);
29933005
if (action == DROP_EXISTING) { r = drop_ty(r.bcx, dst, t); }
2994-
ret memmove_ty(r.bcx, dst, src, t);
3006+
r = memmove_ty(r.bcx, dst, src, t);
3007+
if (ty::type_owns_heap_mem(cx.fcx.lcx.ccx.tcx, t)) {
3008+
r = duplicate_heap_parts(cx, dst, t);
3009+
}
3010+
ret r;
29953011
}
29963012
cx.fcx.lcx.ccx.sess.bug("unexpected type in trans::copy_val: " +
29973013
ty_to_str(cx.fcx.lcx.ccx.tcx, t));
@@ -3500,9 +3516,13 @@ mod ivec {
35003516
copy_loop_header_cx.build.CondBr(not_yet_at_end,
35013517
copy_loop_body_cx.llbb,
35023518
next_cx.llbb);
3519+
35033520
auto copy_src_ptr = copy_loop_body_cx.build.Load(src_ptr);
3504-
rslt =
3505-
copy_val(copy_loop_body_cx, INIT, copy_dest_ptr, copy_src_ptr, t);
3521+
auto copy_src = load_if_immediate(copy_loop_body_cx, copy_src_ptr,
3522+
unit_ty);
3523+
3524+
rslt = copy_val(copy_loop_body_cx, INIT, copy_dest_ptr, copy_src,
3525+
unit_ty);
35063526
auto post_copy_cx = rslt.bcx;
35073527
// Increment both pointers.
35083528

@@ -3575,6 +3595,9 @@ mod ivec {
35753595
auto unit_sz = ares.llunitsz;
35763596
auto llalen = ares.llalen;
35773597

3598+
find_scope_cx(bcx).cleanups +=
3599+
[clean(bind drop_ty(_, llvecptr, vec_ty))];
3600+
35783601
auto llunitty = type_of_or_i8(bcx, unit_ty);
35793602
auto llheappartty = T_ivec_heap_part(llunitty);
35803603
auto lhs_len_and_data = get_len_and_data(bcx, lhs, unit_ty);
@@ -3740,6 +3763,66 @@ mod ivec {
37403763

37413764
ret res(next_cx, llvecptr);
37423765
}
3766+
3767+
// NB: This does *not* adjust reference counts. The caller must have done
3768+
// this via take_ty() beforehand.
3769+
fn duplicate_heap_part(&@block_ctxt cx, ValueRef orig_vptr,
3770+
ty::t unit_ty) -> result {
3771+
// Cast to an opaque interior vector if we can't trust the pointer
3772+
// type.
3773+
auto vptr;
3774+
if (ty::type_has_dynamic_size(cx.fcx.lcx.ccx.tcx, unit_ty)) {
3775+
vptr = cx.build.PointerCast(orig_vptr, T_ptr(T_opaque_ivec()));
3776+
} else {
3777+
vptr = orig_vptr;
3778+
}
3779+
3780+
auto llunitty = type_of_or_i8(cx, unit_ty);
3781+
auto llheappartty = T_ivec_heap_part(llunitty);
3782+
3783+
// Check to see if the vector is heapified.
3784+
auto stack_len_ptr = cx.build.InBoundsGEP(vptr, [C_int(0),
3785+
C_uint(abi::ivec_elt_len)]);
3786+
auto stack_len = cx.build.Load(stack_len_ptr);
3787+
auto stack_len_is_zero = cx.build.ICmp(lib::llvm::LLVMIntEQ,
3788+
stack_len, C_int(0));
3789+
auto maybe_on_heap_cx = new_sub_block_ctxt(cx, "maybe_on_heap");
3790+
auto next_cx = new_sub_block_ctxt(cx, "next");
3791+
cx.build.CondBr(stack_len_is_zero, maybe_on_heap_cx.llbb,
3792+
next_cx.llbb);
3793+
3794+
auto stub_ptr = maybe_on_heap_cx.build.PointerCast(vptr,
3795+
T_ptr(T_ivec_heap(llunitty)));
3796+
auto heap_ptr_ptr = maybe_on_heap_cx.build.InBoundsGEP(stub_ptr,
3797+
[C_int(0), C_uint(abi::ivec_heap_stub_elt_ptr)]);
3798+
auto heap_ptr = maybe_on_heap_cx.build.Load(heap_ptr_ptr);
3799+
auto heap_ptr_is_nonnull = maybe_on_heap_cx.build.ICmp(
3800+
lib::llvm::LLVMIntNE, heap_ptr, C_null(T_ptr(llheappartty)));
3801+
auto on_heap_cx = new_sub_block_ctxt(cx, "on_heap");
3802+
maybe_on_heap_cx.build.CondBr(heap_ptr_is_nonnull, on_heap_cx.llbb,
3803+
next_cx.llbb);
3804+
3805+
// Ok, the vector is on the heap. Copy the heap part.
3806+
auto alen_ptr = on_heap_cx.build.InBoundsGEP(stub_ptr,
3807+
[C_int(0), C_uint(abi::ivec_heap_stub_elt_alen)]);
3808+
auto alen = on_heap_cx.build.Load(alen_ptr);
3809+
3810+
auto heap_part_sz = on_heap_cx.build.Add(alen,
3811+
llsize_of(T_opaque_ivec_heap_part()));
3812+
auto rslt = trans_raw_malloc(on_heap_cx, T_ptr(llheappartty),
3813+
heap_part_sz);
3814+
on_heap_cx = rslt.bcx;
3815+
auto new_heap_ptr = rslt.val;
3816+
3817+
rslt = call_memmove(on_heap_cx, new_heap_ptr, heap_ptr, heap_part_sz,
3818+
C_int(4)); // FIXME: align
3819+
on_heap_cx = rslt.bcx;
3820+
3821+
on_heap_cx.build.Store(new_heap_ptr, heap_ptr_ptr);
3822+
on_heap_cx.build.Br(next_cx.llbb);
3823+
3824+
ret res(next_cx, C_nil());
3825+
}
37433826
}
37443827

37453828
fn trans_vec_add(&@block_ctxt cx, &ty::t t, ValueRef lhs, ValueRef rhs) ->
@@ -5431,6 +5514,8 @@ fn trans_ivec(@block_ctxt bcx, &vec[@ast::expr] args, &ast::ann ann) ->
54315514
auto unit_sz = ares.llunitsz;
54325515
auto llalen = ares.llalen;
54335516

5517+
find_scope_cx(bcx).cleanups += [clean(bind drop_ty(_, llvecptr, typ))];
5518+
54345519
auto lllen = bcx.build.Mul(C_uint(vec::len(args)), unit_sz);
54355520
// Allocate the vector pieces and store length and allocated length.
54365521

@@ -5459,15 +5544,17 @@ fn trans_ivec(@block_ctxt bcx, &vec[@ast::expr] args, &ast::ann ann) ->
54595544
auto llstubty = T_ivec_heap(llunitty);
54605545
auto llstubptr = bcx.build.PointerCast(llvecptr, T_ptr(llstubty));
54615546
bcx.build.Store(C_int(0), bcx.build.InBoundsGEP(llstubptr, stub_z));
5462-
bcx.build.Store(lllen, bcx.build.InBoundsGEP(llstubptr, stub_a));
54635547
auto llheapty = T_ivec_heap_part(llunitty);
54645548
if (vec::len(args) == 0u) {
54655549
// Null heap pointer indicates a zero-length vector.
54665550

5551+
bcx.build.Store(llalen, bcx.build.InBoundsGEP(llstubptr, stub_a));
54675552
bcx.build.Store(C_null(T_ptr(llheapty)),
54685553
bcx.build.InBoundsGEP(llstubptr, stub_p));
54695554
llfirsteltptr = C_null(T_ptr(llunitty));
54705555
} else {
5556+
bcx.build.Store(lllen, bcx.build.InBoundsGEP(llstubptr, stub_a));
5557+
54715558
auto llheapsz = bcx.build.Add(llsize_of(llheapty), lllen);
54725559
auto rslt = trans_raw_malloc(bcx, T_ptr(llheapty), llheapsz);
54735560
bcx = rslt.bcx;

src/rt/rust_builtin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ ivec_copy_from_buf(rust_task *task, type_desc *ty, rust_ivec *v, void *ptr,
682682
ivec_reserve(task, ty, v, count);
683683

684684
size_t new_size = count * ty->size;
685-
if (v->fill) {
685+
if (v->fill || !v->payload.ptr) {
686686
// On stack.
687687
memmove(v->payload.data, ptr, new_size);
688688
v->fill = new_size;

src/test/run-pass/lib-ivec.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,27 @@ fn test_unsafe_ptrs() {
3636

3737
fn test_init_fn() {
3838
fn square(uint n) -> uint { ret n * n; }
39+
40+
// Test on-stack init-fn.
3941
auto v = ivec::init_fn(square, 3u);
4042
assert (ivec::len(v) == 3u);
41-
assert (v.(0) == 1u);
42-
assert (v.(1) == 4u);
43-
assert (v.(2) == 9u);
43+
assert (v.(0) == 0u);
44+
assert (v.(1) == 1u);
45+
assert (v.(2) == 4u);
46+
47+
// Test on-heap init-fn.
48+
v = ivec::init_fn(square, 5u);
49+
assert (ivec::len(v) == 5u);
50+
assert (v.(0) == 0u);
51+
assert (v.(1) == 1u);
52+
assert (v.(2) == 4u);
53+
assert (v.(3) == 9u);
54+
assert (v.(4) == 16u);
4455
}
4556

4657
fn main() {
4758
test_reserve_and_on_heap();
4859
test_unsafe_ptrs();
49-
//test_init_fn();
60+
test_init_fn();
5061
}
5162

0 commit comments

Comments
 (0)