From patchwork Sun Mar 19 01:42:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9632421 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4D862601E9 for ; Sun, 19 Mar 2017 01:43:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F61F282E8 for ; Sun, 19 Mar 2017 01:43:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 344B528338; Sun, 19 Mar 2017 01:43:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D92B282E8 for ; Sun, 19 Mar 2017 01:43:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751441AbdCSBnH (ORCPT ); Sat, 18 Mar 2017 21:43:07 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:36188 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751440AbdCSBnH (ORCPT ); Sat, 18 Mar 2017 21:43:07 -0400 Received: by mail-wr0-f193.google.com with SMTP id l37so13792319wrc.3 for ; Sat, 18 Mar 2017 18:43:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y9fHuDLzWvHHKQyNUsDvD1R9C/Q0xsHjy0zC/1Esi3E=; b=kcEizYVVxBeV4eX42gKA6G61/C4KZ6+YnCk7iklJn8QO9zmdFEbeLVUvLS1rRW1OmQ +iATcLRLkyi1l2Aqt+GsSt1EEz/KhoFRUjPf2gsIu0a5IuXOwJkGovVdTinlqMeglvMX aMTOaggmALUKcYcfVLFtncmeFCHvzfbsZqNEDn0i724kuAJQ7MPutu7OEkS4FMrxOByG otpoF5bva5CMgO8EjEh6BxNS0ssFoRV2wbSoiYeym3MOScLJAM9HLQj0a4dISIPs/Wwq TiFYGM6+zfhYNt7xqs0KP4sSIWmU1lHAvWPyA9/PO+L5du2xZoDtZGQvXUjtbT+pHu/7 eoSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Y9fHuDLzWvHHKQyNUsDvD1R9C/Q0xsHjy0zC/1Esi3E=; b=fRiG0q9t9doTLm/JmkGDa+IZoU2ZjbPW9V7OV9RE0wVqaFjjZ86h/g7CKc6V+Mj/EG yOM/cCgbws74/EuMcHoRga+F5uGY2Xyd2YXPhv2uJ6akxc/FwZZxcewUoUoWsBi9/y6B TU3WaKq2YOdwM7zQNtpgQDCk3d0oeJ9EISnrwPjSBiMXPvdZhWpffF1lcXS5edwJDsQK gBQfrEdjOQuhWtg6Oj4nDXHj1gHNF/Fk+mRMOwPSgVNoWREP7FTR0nJ80Xks+p4DAlp6 2taC5K8aeniuEOezZ/S9gwh9br9jUHCik1ILcV+MmMmi1HYNkfn+LqNETyLeCrsGg/Sq L4yQ== X-Gm-Message-State: AFeK/H2uRsBb+H1C164UyJQy+93hnyJa+BZAYyPL8W2BtPuxYnQ3XwWnpecdOJXRQU0q+A== X-Received: by 10.223.149.37 with SMTP id 34mr19824072wrs.107.1489887785058; Sat, 18 Mar 2017 18:43:05 -0700 (PDT) Received: from localhost.localdomain (83.254-65-87.adsl-dyn.isp.belgacom.be. [87.65.254.83]) by smtp.gmail.com with ESMTPSA id m139sm10814099wma.2.2017.03.18.18.43.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Mar 2017 18:43:04 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Dibyendu Majumdar , Luc Van Oostenryck Subject: [PATCH v3 27/30] llvm: give correct type to binops Date: Sun, 19 Mar 2017 02:42:24 +0100 Message-Id: <20170319014227.8833-28-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170319014227.8833-1-luc.vanoostenryck@gmail.com> References: <20170319014227.8833-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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 fda9484d7..332a5e2a1 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);