From patchwork Thu Apr 6 23:00:29 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: 9668539 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 6C931602B8 for ; Thu, 6 Apr 2017 23:00:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7103326E1A for ; Thu, 6 Apr 2017 23:00:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 65D87285DD; Thu, 6 Apr 2017 23:00:43 +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 E654E26E1A for ; Thu, 6 Apr 2017 23:00:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754134AbdDFXAm (ORCPT ); Thu, 6 Apr 2017 19:00:42 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:36666 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753035AbdDFXAl (ORCPT ); Thu, 6 Apr 2017 19:00:41 -0400 Received: by mail-wr0-f195.google.com with SMTP id o21so10188783wrb.3 for ; Thu, 06 Apr 2017 16:00:40 -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=cIdQuyPQiyNf43072HAL8575zxfkRaTEgGbeTPAF+jE=; b=fTI6qmE7323pHV78K4tRlI5MEdKlFaKJHeyk2HNYrP+87KnltSsFxKLdYYenHjvWnO mWd+fiOlGmgTvWW4vZH/m8OoS2isq18ni9OK9LXtL6tDgJ7h19qJbhXKVScvRq42+5t3 FMa0cgGCCzLUIc9fjqalFvQrJTG527JLv8o0fwG6t6VkOWgc1gpTNTindySjuUlutgmJ v/xQKDSI/mPbpyOb7c4HJdVuecFJsi7WJlTLsKb9vGKIOnkl/u8BHV8iHdpqPqls4FDC GhN7P3gCnnMXUmCuTr8dlK8MSFBUMCXpNzaGsXilL4B97nfNQZQdrLsv/Iodj6DnfIrq zrYQ== 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=cIdQuyPQiyNf43072HAL8575zxfkRaTEgGbeTPAF+jE=; b=JaOrnV4JT8RPvFlGSNkwTvMav5gpBGXO5KYbG6lS0oSyuDbZR3Thr+jkSxeDV5LMXo olkUFKbFNi9lJC/1+a96zXHvAx7WPgp4hiY9fjg+GTJwxu5KX6+JfK8Fbg9SH4Aa47GH kTZ5YeTxNgjcnckHLyMwYcZJKbCCgBOt5hHZkPkivERy24EH6mf8LH8l5A+tIs3AKT6L VRwtqVKGUffTpNadpKkuTZR+D3Vjb96kEdZZB56CmTuF5CubmT/uRReGC6/3vY+oFHuo +UaZg78m+ndxpWqaLEs1bgznySzMV6Q+kMxMGhmQVtxzZbv1DNF/NrMzIaGHhnvu6Qo0 bVHA== X-Gm-Message-State: AFeK/H1lhER5ybo/EQ4IMZwaxe5qiDF7baCY7Ru3belnI8+u7d6WiClTEw80YNcxDuZjqA== X-Received: by 10.223.148.102 with SMTP id 93mr29486957wrq.144.1491519639352; Thu, 06 Apr 2017 16:00:39 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:88d:1900:b468:4a21:54b4:3699]) by smtp.gmail.com with ESMTPSA id i7sm4019595wmg.30.2017.04.06.16.00.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Apr 2017 16:00:38 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Christopher Li , Luc Van Oostenryck Subject: [PATCH 4/4] fix implicit zero initializer. Date: Fri, 7 Apr 2017 01:00:29 +0200 Message-Id: <20170406230029.11384-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170406230029.11384-1-luc.vanoostenryck@gmail.com> References: <20170406230029.11384-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 The C standard requires that, when initializing an aggregate, all fieds not explicitly initialized shall be implicity zero-initialized (more exactly "the same as objects that have static storage duration" [6.7.9.21]). Until now sparse didn't did this. Fix this (when an initializer is present and the object not a scalar) by first storing zeroes in the whole object before doing the initialization of each fields explicitly initialized. Note 1: the code simplify nicely when there is a single field that is initialized, much less so when there is several ones. Note 2: this implicit initialization is not needed if all fields are explicitly initialized but is done anyway for the moment. Signed-off-by: Luc Van Oostenryck --- linearize.c | 11 ++++ validation/linear/bitfield-init-zero.c | 102 ++++++++++++++++++++++++++++++++ validation/linear/struct-init-full.c | 28 +++++++++ validation/linear/struct-init-partial.c | 41 +++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 validation/linear/bitfield-init-zero.c create mode 100644 validation/linear/struct-init-full.c create mode 100644 validation/linear/struct-init-partial.c diff --git a/linearize.c b/linearize.c index e9610932b..e4cbc8bd4 100644 --- a/linearize.c +++ b/linearize.c @@ -1634,6 +1634,17 @@ static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym) sym->initialized = 1; ad.address = symbol_pseudo(ep, sym); + + if (sym->initializer && !is_scalar_type(sym)) { + // default zero initialization [6.7.9.21] + struct expression *expr = sym->initializer; + ad.pos = expr->pos; + ad.result_type = sym; + ad.source_type = base_type(sym); + ad.address = symbol_pseudo(ep, sym); + linearize_store_gen(ep, value_pseudo(0), &ad); + } + value = linearize_initializer(ep, sym->initializer, &ad); finish_address_gen(ep, &ad); return value; diff --git a/validation/linear/bitfield-init-zero.c b/validation/linear/bitfield-init-zero.c new file mode 100644 index 000000000..39a64345e --- /dev/null +++ b/validation/linear/bitfield-init-zero.c @@ -0,0 +1,102 @@ +struct bfu { + unsigned int a:11; + unsigned int f:9; + unsigned int :2; + unsigned int z:3; +}; + +struct bfu bfuu_init(unsigned int a) +{ + struct bfu bf = { .f = a, }; + return bf; +} + +struct bfu bfus_init(int a) +{ + struct bfu bf = { .f = a, }; + return bf; +} + +unsigned int bfu_get0(void) +{ + struct bfu bf = { }; + return bf.f; +} + + +struct bfs { + signed int a:11; + signed int f:9; + signed int :2; + signed int z:3; +}; + +struct bfs bfsu_init(unsigned int a) +{ + struct bfs bf = { .f = a, }; + return bf; +} + +struct bfs bfss_init(int a) +{ + struct bfs bf = { .f = a, }; + return bf; +} + +int bfs_get0(void) +{ + struct bfs bf = { }; + return bf.f; +} + +/* + * check-name: bitfield implicit init zero + * check-command: test-linearize -Wno-decl $file + * + * check-output-start +bfuu_init: +.L0: + + cast.9 %r2 <- (32) %arg1 + shl.32 %r4 <- %r2, $11 + ret.32 %r4 + + +bfus_init: +.L2: + + scast.9 %r10 <- (32) %arg1 + shl.32 %r12 <- %r10, $11 + ret.32 %r12 + + +bfu_get0: +.L4: + + ret.32 $0 + + +bfsu_init: +.L6: + + cast.9 %r23 <- (32) %arg1 + shl.32 %r25 <- %r23, $11 + ret.32 %r25 + + +bfss_init: +.L8: + + scast.9 %r31 <- (32) %arg1 + shl.32 %r33 <- %r31, $11 + ret.32 %r33 + + +bfs_get0: +.L10: + + ret.32 $0 + + + * check-output-end + */ diff --git a/validation/linear/struct-init-full.c b/validation/linear/struct-init-full.c new file mode 100644 index 000000000..f1b03db71 --- /dev/null +++ b/validation/linear/struct-init-full.c @@ -0,0 +1,28 @@ +struct s { + int a, b, c; +}; + +struct s s_init_all(int a) +{ + struct s s = { .a = a, .b = 42, .c = 123, }; + return s; +} + +/* + * check-name: struct implicit init zero not needed + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-start +s_init_all: +.L4: + + store.32 %arg1 -> 0[s] + store.32 $42 -> 4[s] + store.32 $123 -> 8[s] + load.96 %r8 <- 0[s] + ret.96 %r8 + + + * check-output-end + */ diff --git a/validation/linear/struct-init-partial.c b/validation/linear/struct-init-partial.c new file mode 100644 index 000000000..1f5078bfa --- /dev/null +++ b/validation/linear/struct-init-partial.c @@ -0,0 +1,41 @@ +struct s { + int a, b, c; +}; + +struct s s_init_first(int a) +{ + struct s s = { .a = a, }; + return s; +} + +struct s s_init_third(int a) +{ + struct s s = { .c = a, }; + return s; +} + +/* + * check-name: struct implicit init zero needed + * check-command: test-linearize -Wno-decl $file + * + * check-output-start +s_init_first: +.L0: + + store.96 $0 -> 0[s] + store.32 %arg1 -> 0[s] + load.96 %r2 <- 0[s] + ret.96 %r2 + + +s_init_third: +.L2: + + store.96 $0 -> 0[s] + store.32 %arg1 -> 8[s] + load.96 %r5 <- 0[s] + ret.96 %r5 + + + * check-output-end + */