Skip to contentSkip to conversation

simplify: conservative handling of casts with pointers

PATCH Jan Pokorný 2 participants 4 messages May 8, 2012 – May 10, 2012 Sparse checker

lore
Share & tools
lore
#0 lore raw

When the cast is optimized out/target pointer type information lost, it may be impossible to for backend to recover it (think of "struct foo *my_new_foo = malloc(sizeof(*my_new_foo))").

Losing such pointer type information can be wider issue (structs and unions maybe), but this is the most exposed one and the patch tries to be minimal in this regard and the impact seems to be minimal too as usually type-correctness is followed.

Annotated demonstration of the change (part of "test-linearize allocate.c", function allocate):

before:

.L0x7fd091ecb510:

        load.32     %r76 <- 20[%arg1]
        cast.64     %r78 <- (32) %r76
        call.64     %r79 <- blob_alloc, %r78
Quoted 1 line
Quoted 1 line
Quoted 1 line

no cast, %r79 is generic void *

        br          %r79, .L0x7fd091ecb650, .L0x7fd091ecb600

.L0x7fd091ecb600:
call die, "out of memory"
br .L0x7fd091ecb650

.L0x7fd091ecb650:

        load.32     %r85 <- 36[%arg1]
        add.32      %r87 <- %r85, %r76
        store.32    %r87 -> 36[%arg1]
Quoted 1 line
Quoted 1 line
Quoted 1 line

oops, cannot reconstruct what entity is being accessed from 0[%r79]

        store.64    %r30(blob) -> 0[%r79]
Quoted 1 line
Quoted 1 line
Quoted 1 line

in this case, it is unambiguous, but we could be lost easily

        store.64    %r79 -> 8[%arg1]
        add.64      %r98 <- %r28, $15
        [...]

after:

.L0x7fab5bc11510:

        load.32     %r76 <- 20[%arg1]
        cast.64     %r78 <- (32) %r76
        call.64     %r79 <- blob_alloc, %r78
Quoted 1 line
Quoted 1 line
Quoted 1 line

this cast from void * is kept, target pointer type preserved

        cast.64     %r80 <- (64) %r79
        br          %r80, .L0x7fab5bc11650, .L0x7fab5bc11600

.L0x7fab5bc11600:
call die, "out of memory"
br .L0x7fab5bc11650

.L0x7fab5bc11650:

        load.32     %r85 <- 36[%arg1]
        add.32      %r87 <- %r85, %r76
        store.32    %r87 -> 36[%arg1]
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

when we know the type of destination, we can reconstruct it (newblob->next = blob)

        store.64    %r30(blob) -> 0[%r80]
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

and also, based on types + offset, we can infer correct destination within %arg1 (desc->blobs = newblob)

        store.64    %r80 -> 8[%arg1]
        add.64      %r98 <- %r28, $15
        [...]
---
 simplify.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
simplify.c +8 -0
index 8200584..3b2b03c 100644
@@ -10,6 +10,7 @@
 #include "expression.h"
 #include "linearize.h"
 #include "flow.h"
+#include "symbol.h"
 
 /* Find the trivial parent for a phi-source */
 static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
@@ -667,6 +668,13 @@ static int simplify_cast(struct instruction *insn)
 	orig_type = insn->orig_type;
 	if (!orig_type)
 		return 0;
+
+	/* Avoid possible loss of pointer type info (OP_PTRCAST skipped below) */
+	if (is_ptr_type(orig_type) != is_ptr_type(insn->type))
+		return 0;  /* non-pointer vs. pointer or viceversa */
+	else if (orig_type->type == SYM_PTR && insn->opcode != OP_PTRCAST)
+		return 0;  /* any type from "void *" (see alloc_cast_instruction) */
+
 	orig_size = orig_type->bit_size;
 	size = insn->size;
 	src = insn->src;
Signature · 6 lines
1.7.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 8 lines · Jan Pokorný

When the cast is optimized out/target pointer type information lost, it may be impossible for backend to recover it (think of "struct foo *my_new_foo = malloc(sizeof(*my_new_foo))").

Losing such pointer type information can be wider issue (structs and unions maybe), but this is the most exposed one and the patch tries to be minimal in this regard and the impact seems to be minimal too as usually type-correctness is followed.

I expected that if both operands of the cast appear to be pointers, it is always OP_PTRCAST case, which is not true.

So this version exchanges inequality test for "or". Beside being shorter, it covers such previously missed cases.

Based on asserts I experimented with, OP_PTRCASTs are always captured by the condition implicitly (beside OP_CASTs with pointer-like operands).

---
 simplify.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
simplify.c +6 -0
index 8200584..bda4a5b 100644
@@ -10,6 +10,7 @@
 #include "expression.h"
 #include "linearize.h"
 #include "flow.h"
+#include "symbol.h"
 
 /* Find the trivial parent for a phi-source */
 static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
@@ -667,6 +668,11 @@ static int simplify_cast(struct instruction *insn)
 	orig_type = insn->orig_type;
 	if (!orig_type)
 		return 0;
+
+	/* Keep casts with pointer on either side (not only case of OP_PTRCAST) */
+	if (is_ptr_type(orig_type) || is_ptr_type(insn->type))
+		return 0;
+
 	orig_size = orig_type->bit_size;
 	size = insn->size;
 	src = insn->src;
Signature · 5 lines
1.7.3.4
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#2 lore raw
Quoted 10 lines · Jan Pokorný
Quoted 8 lines · Jan Pokorný

When the cast is optimized out/target pointer type information lost, it may be impossible for backend to recover it (think of "struct foo *my_new_foo = malloc(sizeof(*my_new_foo))").

Losing such pointer type information can be wider issue (structs and unions maybe), but this is the most exposed one and the patch tries to be minimal in this regard and the impact seems to be minimal too as usually type-correctness is followed.

Quoted 8 lines

I expected that if both operands of the cast appear to be pointers, it is always OP_PTRCAST case, which is not true.

So this version exchanges inequality test for "or".  Beside being shorter, it covers such previously missed cases.

Based on asserts I experimented with, OP_PTRCASTs are always captured by the condition implicitly (beside OP_CASTs with pointer-like operands).

The early sparse back end does not care about preserve the ctype in the back end. The type has been simplify to bit size. So the pointer type conversion which has the same bit size, it does not need to generate any back end machine code e.g. x86 instruction to perform the conversion. I think that is why it is skipped.

The recent changes, especially the llvm back end care more about preserving the ctype in the back end instruction level.

The patch is applied.

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 9 lines · Christopher Li

The early sparse back end does not care about preserve the ctype in the back end. The type has been simplify to bit size. So the pointer type conversion which has the same bit size, it does not need to generate any back end machine code e.g. x86 instruction to perform the conversion. I think that is why it is skipped.

The recent changes, especially the llvm back end care more about preserving the ctype in the back end instruction level.

As far as I can tell, the type can be recovered reliably, one way or the other, and missing pointer cast was one of a few (if not only one) source of type loss in the instruction stream (e.g., with those pointers returned by function call as demonstrated).

Quoted 1 line

The patch is applied.

Thanks.

-- Jan