linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Christopher Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH v6 25/52] llvm: give correct type to binops
Date: Mon, 27 Mar 2017 23:23:49 +0200	[thread overview]
Message-ID: <20170327212416.18536-26-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170327212416.18536-1-luc.vanoostenryck@gmail.com>

Pointer arithmetic and/or simplification can mixup pointer
and integer types.

Fix this by adding casts before all non-floating point binops
and adjust the result type if needed to match the instructio.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 sparse-llvm.c                    | 30 ++++++++++++++++++++++
 validation/backend/pointer-add.c | 54 ++++++++++++++++++++++++++++++++++++++++
 validation/backend/pointer-sub.c | 18 ++++++++++++++
 validation/backend/symaddr.c     |  8 ++++++
 4 files changed, 110 insertions(+)
 create mode 100644 validation/backend/pointer-add.c
 create mode 100644 validation/backend/pointer-sub.c

diff --git a/sparse-llvm.c b/sparse-llvm.c
index f81a8c657..b89b04105 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -416,6 +416,33 @@ static LLVMValueRef pseudo_to_rvalue(struct function *fn, struct instruction *in
 	return LLVMBuildBitCast(fn->builder, val, dtype, "");
 }
 
+static LLVMValueRef value_to_ivalue(struct function *fn, LLVMValueRef val)
+{
+	if (LLVMGetTypeKind(LLVMTypeOf(val)) == LLVMPointerTypeKind) {
+		LLVMTypeRef dtype = LLVMIntType(bits_in_pointer);
+		val = LLVMBuildPtrToInt(fn->builder, val, dtype, "");
+	}
+	return val;
+}
+
+static LLVMValueRef value_to_pvalue(struct function *fn, struct symbol *ctype, LLVMValueRef val)
+{
+	if (LLVMGetTypeKind(LLVMTypeOf(val)) == LLVMIntegerTypeKind) {
+		LLVMTypeRef dtype = symbol_type(ctype);
+		val = LLVMBuildIntToPtr(fn->builder, val, dtype, "");
+	}
+	return val;
+}
+
+static LLVMValueRef adjust_type(struct function *fn, struct symbol *ctype, LLVMValueRef val)
+{
+	if (is_int_type(ctype))
+		return value_to_ivalue(fn, val);
+	if (is_ptr_type(ctype))
+		return value_to_pvalue(fn, ctype, val);
+	return val;
+}
+
 static LLVMValueRef calc_gep(LLVMBuilderRef builder, LLVMValueRef base, LLVMValueRef off)
 {
 	LLVMTypeRef type = LLVMTypeOf(base);
@@ -475,8 +502,10 @@ static void output_op_binary(struct function *fn, struct instruction *insn)
 	char target_name[64];
 
 	lhs = pseudo_to_value(fn, insn, insn->src1);
+	lhs = value_to_ivalue(fn, lhs);
 
 	rhs = pseudo_to_value(fn, insn, insn->src2);
+	rhs = value_to_ivalue(fn, rhs);
 
 	pseudo_name(insn->target, target_name);
 
@@ -577,6 +606,7 @@ static void output_op_binary(struct function *fn, struct instruction *insn)
 		break;
 	}
 
+	target = adjust_type(fn, insn->type, target);
 	insn->target->priv = target;
 }
 
diff --git a/validation/backend/pointer-add.c b/validation/backend/pointer-add.c
new file mode 100644
index 000000000..f92c892b8
--- /dev/null
+++ b/validation/backend/pointer-add.c
@@ -0,0 +1,54 @@
+char *caddv(char *p, int o) { char *r = p; r = r + o; return r; }
+void *vaddv(void *p, int o) { void *r = p; r = r + o; return r; }
+int  *iaddv(int  *p, int o) { int  *r = p; r = r + o; return r; }
+
+char *caddc(char *p, int o) { char *r = p; r = r + 3; return r; }
+void *vaddc(void *p, int o) { void *r = p; r = r + 3; return r; }
+int  *iaddc(int  *p, int o) { int  *r = p; r = r + 3; return r; }
+
+char *cincv(char *p, int o) { char *r = p; r += o; return r; }
+void *vincv(void *p, int o) { void *r = p; r += o; return r; }
+int  *iincv(int  *p, int o) { int  *r = p; r += o; return r; }
+
+char *cincc(char *p, int o) { char *r = p; r += 3; return r; }
+void *vincc(void *p, int o) { void *r = p; r += 3; return r; }
+int  *iincc(int  *p, int o) { int  *r = p; r += 3; return r; }
+
+
+char *ciniaddv(char *p, int o) { char *r = p + o; return r; }
+void *viniaddv(void *p, int o) { void *r = p + o; return r; }
+int  *iiniaddv(int  *p, int o) { int  *r = p + o; return r; }
+
+char *ciniaddc(char *p, int o) { char *r = p + 3; return r; }
+void *viniaddc(void *p, int o) { void *r = p + 3; return r; }
+int  *iiniaddc(int  *p, int o) { int  *r = p + 3; return r; }
+
+char *ciniincv(char *p, int o) { char *r = p += o; return r; }
+void *viniincv(void *p, int o) { void *r = p += o; return r; }
+int  *iiniincv(int  *p, int o) { int  *r = p += o; return r; }
+
+char *ciniincc(char *p, int o) { char *r = p += 3; return r; }
+void *viniincc(void *p, int o) { void *r = p += 3; return r; }
+int  *iiniincc(int  *p, int o) { int  *r = p += 3; return r; }
+
+
+char *cretaddv(char *p, int o) { return p + o; }
+void *vretaddv(void *p, int o) { return p + o; }
+int  *iretaddv(int  *p, int o) { return p + o; }
+
+char *cretaddc(char *p, int o) { return p + 3; }
+void *vretaddc(void *p, int o) { return p + 3; }
+int  *iretaddc(int  *p, int o) { return p + 3; }
+
+char *cretincv(char *p, int o) { return p += o; }
+void *vretincv(void *p, int o) { return p += o; }
+int  *iretincv(int  *p, int o) { return p += o; }
+
+char *cretincc(char *p, int o) { return p += 3; }
+void *vretincc(void *p, int o) { return p += 3; }
+int  *iretincc(int  *p, int o) { return p += 3; }
+
+/*
+ * check-name: pointer-add
+ * check-command: ./sparsec -Wno-decl -c $file -o r.o
+ */
diff --git a/validation/backend/pointer-sub.c b/validation/backend/pointer-sub.c
new file mode 100644
index 000000000..4017faf69
--- /dev/null
+++ b/validation/backend/pointer-sub.c
@@ -0,0 +1,18 @@
+long subv0(void *p, int   a) { return p - ((void*)0); }
+long subvc(void *p, int   a) { return p - ((void*)8); }
+long subva(void *p, int   a) { return p - ((void*)a); }
+long subvq(void *p, void *q) { return p - q; }
+
+long subi0(int  *p, int   a) { return p - ((int *)0); }
+long subic(int  *p, int   a) { return p - ((int *)8); }
+long subia(int  *p, int   a) { return p - ((int *)a); }
+long subiq(int  *p, int  *q) { return p - q; }
+
+long subvm3(void *p, int   a) { return (p - ((void*)0)) * 3; }
+long subvx3(void *p, int   a) { return (p - ((void*)0)) ^ 3; }
+
+/*
+ * check-name: pointer-sub
+ * check-command: sparsec -Wno-decl -c $file -o tmp.o
+ * check-known-to-fail
+ */
diff --git a/validation/backend/symaddr.c b/validation/backend/symaddr.c
index 71bca2d76..71fb9deff 100644
--- a/validation/backend/symaddr.c
+++ b/validation/backend/symaddr.c
@@ -10,7 +10,9 @@ void lfoo(int *p, int a)
 	useip(p);
 	useip(larra);
 	useip(larrb + 1);
+	useip(larrc + a);
 	useip(&larrd[1]);
+	useip(&larre[a]);
 	useia(&larrf);
 }
 
@@ -22,7 +24,9 @@ void sfoo(int *p, int a)
 	useip(&s);
 	useip(sarra);
 	useip(sarrb + 1);
+	useip(sarrc + a);
 	useip(&sarrd[1]);
+	useip(&sarre[a]);
 	useia(&sarrf);
 	usevp(sfun);
 	usevp(&spun);
@@ -36,7 +40,9 @@ void xfoo(int *p, int a)
 	useip(&x);
 	useip(xarra);
 	useip(xarrb + 1);
+	useip(xarrc + a);
 	useip(&xarrd[1]);
+	useip(&xarre[a]);
 	useia(&xarrf);
 	usevp(xfun);
 	usevp(&xpun);
@@ -50,7 +56,9 @@ void gfoo(int *p, int a)
 	useip(&g);
 	useip(garra);
 	useip(garrb + 1);
+	useip(garrc + a);
 	useip(&garrd[1]);
+	useip(&garre[a]);
 	useia(&garrf);
 	usevp(gfun);
 	usevp(&gpun);
-- 
2.12.0


  parent reply	other threads:[~2017-03-27 21:26 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-27 21:23 [PATCH v6 00/52] LLVM fixes Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 01/52] llvm: remove unneeded arg 'module' Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 02/52] llvm: remove unneeded 'generation' Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 03/52] llvm: remove unneeded function::type Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 04/52] llvm: reduce scope of 'bb_nr' Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 05/52] llvm: use pseudo_list_size() instead of open coding it Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 06/52] llvm: give arguments a name Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 07/52] llvm: give a name to call's return values Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 08/52] llvm: avoid useless temp variable Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 09/52] llvm: extract get_sym_value() from pseudo_to_value() Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 10/52] llvm: fix test of floating-point type Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 11/52] llvm: fix translation of PSEUDO_VALs into a ValueRefs Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 12/52] llvm: fix output_op_store() which modify its operand Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 13/52] llvm: fix output_op_[ptr]cast() Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 14/52] llvm: take care of degenerated rvalues Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 15/52] llvm: add test cases for symbol's address Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 16/52] llvm: add test cases for pointers passed as argument Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 17/52] llvm: add test cases for arrays " Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 18/52] llvm: add test cases for degenerated pointers Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 19/52] llvm: add support for OP_NEG Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 20/52] llvm: add support for OP_SETVAL with floats Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 21/52] llvm: add support for OP_SETVAL with labels Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 22/52] llvm: ignore OP_INLINED_CALL Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 23/52] llvm: fix pointer/float mixup in comparisons Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 24/52] llvm: fix type in comparison with an address constant Luc Van Oostenryck
2017-03-27 21:23 ` Luc Van Oostenryck [this message]
2017-03-27 21:23 ` [PATCH v6 26/52] llvm: adjust OP_RET's type Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 27/52] llvm: variadic functions are not being marked as such Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 28/52] llvm: fix type of switch constants Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 29/52] llvm: make pseudo_name() more flexible Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 30/52] llvm: give a name to all values Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 31/52] llvm: add support for OP_SWITCH with a range Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 32/52] llvm: fix OP_SWITCH has no target Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 33/52] llvm: make value_to_pvalue() more flexible Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 34/52] llvm: make value_to_ivalue() " Luc Van Oostenryck
2017-03-27 21:23 ` [PATCH v6 35/52] llvm: add test case pointer compare with cast Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 36/52] llvm: let pseudo_to_value() directly use the type Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 37/52] llvm: remove unneeded pseudo_to_value() unneeded argument Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 38/52] llvm: introduce get_ioperand() Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 39/52] llvm: fix mutating function pointer Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 40/52] llvm: fix mutated OP_RET Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 41/52] llvm: fix mutated OP_SEL Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 42/52] llvm: fix mutated OP_SWITCH Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 43/52] llvm: fix mutated OP_PHISOURCE Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 44/52] llvm: fix mutated OP_[PTR]CAST Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 45/52] llvm: add support for restricted types Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 46/52] llvm: fix get value from initialized symbol Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 47/52] llvm: fix get value from non-anonymous symbol Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 48/52] llvm: fix type of bitfields Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 49/52] llvm: add support for OP_FPCAST Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 50/52] llvm: add support for cast from floats Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 51/52] llvm: cleanup of output_[ptr]cast() Luc Van Oostenryck
2017-03-27 21:24 ` [PATCH v6 52/52] llvm: fix creation of sparsec's tmp files Luc Van Oostenryck
2017-03-31  5:28 ` [PATCH v6 00/52] LLVM fixes Christopher Li
2017-03-31  5:32   ` Christopher Li
2017-03-31  8:56     ` Luc Van Oostenryck
2017-04-01 10:53 ` [GIT PULL v6] " Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170327212416.18536-26-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).