Skip to content

Commit c2cf76e

Browse files
philbertyCohenArthur
authored andcommitted
gccrs: Fix bad uninit intrinsic
We were using the DECL_RESULT but this just contains the TREE_TYPE of the retval. It was also missing taking the address of the destination for the memset call. This changes the code to create a temp variable for the return value and asserts the destination size is the same as the size of the template param. Fixes Rust-GCC#2583 gcc/rust/ChangeLog: * backend/rust-compile-intrinsic.cc (enter_intrinsic_block): take the locals vector (uninit_handler): make a temp variable and use the address of it gcc/testsuite/ChangeLog: * rust/execute/torture/issue-2583.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent f8777e3 commit c2cf76e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

gcc/rust/backend/rust-compile-intrinsic.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,14 @@ compile_intrinsic_function (Context *ctx, TyTy::FnType *fntype)
315315
}
316316

317317
static void
318-
enter_intrinsic_block (Context *ctx, tree fndecl)
318+
enter_intrinsic_block (Context *ctx, tree fndecl,
319+
const std::vector<Bvariable *> &vars = {})
319320
{
320321
tree enclosing_scope = NULL_TREE;
321322
location_t start_location = UNDEF_LOCATION;
322323
location_t end_location = UNDEF_LOCATION;
323324

324-
auto block = ctx->get_backend ()->block (fndecl, enclosing_scope, {},
325+
auto block = ctx->get_backend ()->block (fndecl, enclosing_scope, vars,
325326
start_location, end_location);
326327

327328
ctx->push_block (block);
@@ -1010,7 +1011,19 @@ uninit_handler (Context *ctx, TyTy::FnType *fntype)
10101011
tree template_parameter_type
10111012
= TyTyResolveCompile::compile (ctx, resolved_tyty);
10121013

1013-
enter_intrinsic_block (ctx, fndecl);
1014+
// result temporary
1015+
tree dst_type = TREE_TYPE (DECL_RESULT (fndecl));
1016+
rust_assert (TYPE_SIZE_UNIT (template_parameter_type)
1017+
== TYPE_SIZE_UNIT (dst_type));
1018+
1019+
tree tmp_stmt = error_mark_node;
1020+
Bvariable *bvar
1021+
= ctx->get_backend ()->temporary_variable (fndecl, NULL_TREE, dst_type,
1022+
NULL_TREE,
1023+
true /*address_is_taken*/,
1024+
UNDEF_LOCATION, &tmp_stmt);
1025+
1026+
enter_intrinsic_block (ctx, fndecl, {bvar});
10141027

10151028
// BUILTIN size_of FN BODY BEGIN
10161029

@@ -1021,20 +1034,20 @@ uninit_handler (Context *ctx, TyTy::FnType *fntype)
10211034
// call memset with 0x01 and size of the thing see
10221035
// https://github.com/Rust-GCC/gccrs/issues/1899
10231036

1024-
tree dst = DECL_RESULT (fndecl);
1037+
tree dst = bvar->get_tree (BUILTINS_LOCATION);
1038+
tree dst_addr = build_fold_addr_expr_loc (BUILTINS_LOCATION, dst);
10251039
tree constant_byte = build_int_cst (integer_type_node, 0x01);
10261040
tree size_expr = TYPE_SIZE_UNIT (template_parameter_type);
10271041

10281042
tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, memset_builtin, 3,
1029-
dst, constant_byte, size_expr);
1043+
dst_addr, constant_byte, size_expr);
10301044
TREE_READONLY (memset_call) = 0;
10311045
TREE_SIDE_EFFECTS (memset_call) = 1;
10321046

10331047
ctx->add_statement (memset_call);
10341048

10351049
auto return_statement
1036-
= ctx->get_backend ()->return_statement (fndecl, DECL_RESULT (fndecl),
1037-
UNDEF_LOCATION);
1050+
= ctx->get_backend ()->return_statement (fndecl, dst, UNDEF_LOCATION);
10381051
ctx->add_statement (return_statement);
10391052
// BUILTIN size_of FN BODY END
10401053

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[lang = "sized"]
2+
pub trait Sized {}
3+
4+
mod intrinsics {
5+
extern "rust-intrinsic" {
6+
pub fn uninit<T>() -> T;
7+
}
8+
}
9+
10+
pub fn main() -> i32 {
11+
let _val: usize = unsafe { intrinsics::uninit() };
12+
0
13+
}

0 commit comments

Comments
 (0)