From patchwork Mon Apr 14 17:13:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 545 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx20.g.dreamhost.com (mx2.sub5.homie.mail.dreamhost.com [208.113.200.128]) by wilcox.dreamhost.com (Postfix) with ESMTP id C16E9360075 for ; Mon, 14 Apr 2014 10:14:33 -0700 (PDT) Received: by homiemail-mx20.g.dreamhost.com (Postfix, from userid 14314964) id 7FB92415CD5D8; Mon, 14 Apr 2014 10:14:33 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx20.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx20.g.dreamhost.com (Postfix) with ESMTPS id 541A5413784FB for ; Mon, 14 Apr 2014 10:14:33 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=QBEK7yKfmhrDVRMuAQluQJe8PEZIT1q 1/eu8J3rOUUv+fUSTR+jMXw1ntAVeeb8ydVtjC2vG4jpyjvaTcZhlh0H++n7Mkvn 8cY1c6N9UL5/nnSZqH2reDAN0YNjoj3CPsF3oGR53zZO2ggJOmhqvMnrbQw/fAEX qPxy7sFWFuvA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=YKEsc/Q4bC32aaoWXcFlKyGyy3I=; b=bf+Ga AHD9kTXYdvNNeGfp8wJ6qccZKeIgZh4Y9Fkoh8ZGhO3/UD/eiuqSwq+i+kZyCWUq ekKjMahpSu+uW6QUGaZX2TqfsOFZ/g/h6CvTrZwFJrZMycdvSnSHMuLLl36vwaDd 7l9Rd27U5hw9Bct5u/QDxu56E82q70Liqilj4o= Received: (qmail 9632 invoked by alias); 14 Apr 2014 17:13:58 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 9512 invoked by uid 89); 14 Apr 2014 17:13:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Mon, 14 Apr 2014 17:13:54 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 97F621160DB; Mon, 14 Apr 2014 13:13:52 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id l1xECKoLX0sj; Mon, 14 Apr 2014 13:13:52 -0400 (EDT) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 5E471116174; Mon, 14 Apr 2014 13:13:52 -0400 (EDT) Received: by joel.gnat.com (Postfix, from userid 1000) id 022BBE02C0; Mon, 14 Apr 2014 10:13:57 -0700 (PDT) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Sanimir Agovic Subject: [PATCH 08/12] vla: evaluate operand of sizeof if its type is a vla Date: Mon, 14 Apr 2014 10:13:12 -0700 Message-Id: <1397495596-25364-9-git-send-email-brobecker@adacore.com> In-Reply-To: <1397495596-25364-1-git-send-email-brobecker@adacore.com> References: <0377C58828D86C4588AEEC42FC3B85A71D8507B7@IRSMSX105.ger.corp.intel.com> <1397495596-25364-1-git-send-email-brobecker@adacore.com> X-DH-Original-To: gdb@patchwork.siddhesh.in From: Sanimir Agovic The c99 standard in "6.5.3.4 The sizeof operator" states: If the type of the operand is a variable length array type, the operand is evaluated;[...] This patch mirrors the following c99 semantic in gdb: 1| int vla[n][m]; 2| int i = 1; 3| sizeof(vla[i++][0]); // No sideffect 4| assert (i == 1); 5| sizeof(vla[i++]); // With sideffect 6| assert (i == 2); Note: ptype/whatis still do not allow any sideeffects. This patch was motivated by: https://sourceware.org/ml/gdb-patches/2014-01/msg00732.html gdb/ChangeLog: * eval.c (evaluate_subexp_for_sizeof): Add enum noside argument. (evaluate_subexp_standard): Pass noside argument. (evaluate_subexp_for_sizeof) : Handle subscript case if noside equals EVAL_NORMAL. If the subscript yields a vla type re-evaluate subscript operation with EVAL_NORMAL to enable sideffects. * gdbtypes.c (resolve_dynamic_bounds): Mark bound as evaluated. * gdbtypes.h (enum range_flags): Add RANGE_EVALUATED case. testsuite/ChangeLog: * gdb.base/vla-sideeffect.c: New file. * gdb.base/vla-sideeffect.exp: New file. --- gdb/ChangeLog | 10 ++++ gdb/eval.c | 43 +++++++++++++-- gdb/gdbtypes.c | 1 + gdb/gdbtypes.h | 5 ++ gdb/testsuite/ChangeLog | 5 ++ gdb/testsuite/gdb.base/vla-sideeffect.c | 42 +++++++++++++++ gdb/testsuite/gdb.base/vla-sideeffect.exp | 89 +++++++++++++++++++++++++++++++ 7 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.base/vla-sideeffect.c create mode 100644 gdb/testsuite/gdb.base/vla-sideeffect.exp diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 3ea7687..3717d0d 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,15 @@ 2014-04-14 Sanimir Agovic + * eval.c (evaluate_subexp_for_sizeof): Add enum noside argument. + (evaluate_subexp_standard): Pass noside argument. + (evaluate_subexp_for_sizeof) : Handle subscript case + if noside equals EVAL_NORMAL. If the subscript yields a vla type + re-evaluate subscript operation with EVAL_NORMAL to enable sideffects. + * gdbtypes.c (resolve_dynamic_bounds): Mark bound as evaluated. + * gdbtypes.h (enum range_flags): Add RANGE_EVALUATED case. + +2014-04-14 Sanimir Agovic + * findvar.c (default_read_var_value): Resolve dynamic bounds if location points to a constant blob. diff --git a/gdb/eval.c b/gdb/eval.c index 22392eb..3e62eadc 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -50,7 +50,8 @@ extern int overload_resolution; /* Prototypes for local functions. */ -static struct value *evaluate_subexp_for_sizeof (struct expression *, int *); +static struct value *evaluate_subexp_for_sizeof (struct expression *, int *, + enum noside); static struct value *evaluate_subexp_for_address (struct expression *, int *, enum noside); @@ -2562,7 +2563,7 @@ evaluate_subexp_standard (struct type *expect_type, evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); goto nosideret; } - return evaluate_subexp_for_sizeof (exp, pos); + return evaluate_subexp_for_sizeof (exp, pos, noside); case UNOP_CAST: (*pos) += 2; @@ -2996,10 +2997,13 @@ evaluate_subexp_with_coercion (struct expression *exp, /* Evaluate a subexpression of EXP, at index *POS, and return a value for the size of that subexpression. - Advance *POS over the subexpression. */ + Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL + we allow side-effects on the operand if its type is a variable + length array. */ static struct value * -evaluate_subexp_for_sizeof (struct expression *exp, int *pos) +evaluate_subexp_for_sizeof (struct expression *exp, int *pos, + enum noside noside) { /* FIXME: This should be size_t. */ struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; @@ -3052,6 +3056,37 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos) (*pos) += 4; break; + /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting + type of the subscript is a variable length array type. In this case we + must re-evaluate the right hand side of the subcription to allow + side-effects. */ + case BINOP_SUBSCRIPT: + if (noside == EVAL_NORMAL) + { + int pc = (*pos) + 1; + + val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS); + type = check_typedef (value_type (val)); + if (TYPE_CODE (type) == TYPE_CODE_ARRAY) + { + type = check_typedef (TYPE_TARGET_TYPE (type)); + if (TYPE_CODE (type) == TYPE_CODE_ARRAY) + { + type = TYPE_INDEX_TYPE (type); + /* Only re-evaluate the right hand side if the resulting type + is a variable length type. */ + if (TYPE_RANGE_DATA (type)->flag_bound_evaluated) + { + val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL); + return value_from_longest + (size_type, (LONGEST) TYPE_LENGTH (value_type (val))); + } + } + } + } + + /* Fall through. */ + default: val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); type = value_type (val); diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 57101e1..b1102f6 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -1719,6 +1719,7 @@ resolve_dynamic_bounds (struct type *type, CORE_ADDR addr) range_type = create_range_type (NULL, TYPE_TARGET_TYPE (range_type), &low_bound, &high_bound); + TYPE_RANGE_DATA (range_type)->flag_bound_evaluated = 1; return create_array_type (copy_type (type), elt_type, range_type); diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 6c3bb33..4c9d7c2 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -668,6 +668,11 @@ struct main_type subrange. This affects how the final hight bound is computed. */ int flag_upper_bound_is_count : 1; + + /* True if LOW or/and HIGH are resolved into a static bound from + a dynamic one. */ + + int flag_bound_evaluated : 1; } *bounds; } flds_bnds; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 2e9024f..7d0bd2e 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-04-14 Sanimir Agovic + + * gdb.base/vla-sideeffect.c: New file. + * gdb.base/vla-sideeffect.exp: New file. + 2014-04-14 David Blaikie * gdb.mi/non-stop.c: Add return value for non-void function return diff --git a/gdb/testsuite/gdb.base/vla-sideeffect.c b/gdb/testsuite/gdb.base/vla-sideeffect.c new file mode 100644 index 0000000..6e42a64 --- /dev/null +++ b/gdb/testsuite/gdb.base/vla-sideeffect.c @@ -0,0 +1,42 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2014 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#define SIZE 10 + +int +main (void) +{ + int n = SIZE; + int i = 0; + int j = 0; + int vla2[SIZE][n]; + int vla1[n]; + + for (i = 0; i < n; i++) + vla1[i] = (i * 2) + n; + + for (i = 0; i < SIZE; i++) + for (j = 0; j < n; j++) + vla2[i][j] = (i + j) + n; + + + i = 0; + j = 0; + + return 0; /* vla-filled */ +} diff --git a/gdb/testsuite/gdb.base/vla-sideeffect.exp b/gdb/testsuite/gdb.base/vla-sideeffect.exp new file mode 100644 index 0000000..517d78b --- /dev/null +++ b/gdb/testsuite/gdb.base/vla-sideeffect.exp @@ -0,0 +1,89 @@ +# Copyright 2014 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Tests side-effects of sizeof evaluation. +# Based on gcc/testsuite/gcc.dg/vla-4.c; vla-15.c + +standard_testfile + +if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +gdb_breakpoint [gdb_get_line_number "vla-filled"] +gdb_continue_to_breakpoint "vla-filled" + +# Check side effects for sizeof argument. +set sizeof_int [get_sizeof "int" 4] +set sizeof_vla [ expr "10" * "$sizeof_int" ] + +gdb_test "print sizeof (vla1\[i++\])" " = ${sizeof_int}" \ + "print sizeof (vla1\[i++\])" +gdb_test "print i" " = 0" \ + "print i - sizeof no side effects" + +gdb_test "print sizeof (++vla1\[0\])" " = ${sizeof_int}" \ + "print sizeof (++vla1\[0\])" +gdb_test "print vla1\[0\]" " = 10" \ + "print vla1\[0\] - sizeof no side effects" + +gdb_test "ptype ++vla1\[0\]" "type = int" "ptype ++vla1\[0\]" +gdb_test "print vla1\[0\]" " = 10" \ + "print vla1\[0\] - ptype no side effects" + +gdb_test "whatis ++vla1\[0\]" "type = int" "whatis ++vla1\[0\]" +gdb_test "print vla1\[0\]" " = 10" \ + "print vla1\[0\] - whatis no side effects" + + +gdb_test "print sizeof (vla2\[i++\])" " = ${sizeof_vla}" \ + "print sizeof (vla2\[i++\])" +gdb_test "print i" " = 1" \ + "print i - sizeof with side effects (1)" + +gdb_test "print sizeof (vla2\[i++ + sizeof(j++)\])" " = ${sizeof_vla}" \ + "print sizeof (vla2\[i++ + sizeof(j++)\])" +gdb_test "print i" " = 2" \ + "print i - sizeof with side effects (2)" +gdb_test "print j" " = 0" \ + "print j - sizeof with no side effects" + +gdb_test "ptype vla2\[i++\]" "type = int \\\[10\\\]" \ + "ptype vla2\[i++\]" +gdb_test "print i" " = 2" \ + "print i - ptype with side effects (1)" + +gdb_test "ptype vla2\[i++ + sizeof(j++)\]" "type = int \\\[10\\\]" \ + "ptype vla2\[i++ + sizeof(j++)\]" +gdb_test "print i" " = 2" \ + "print i - ptype with side effects (2)" +gdb_test "print j" " = 0" \ + "print j - ptype with no side effects" + +gdb_test "whatis vla2\[i++\]" "type = int \\\[10\\\]" \ + "whatis vla2\[i++\]" +gdb_test "print i" " = 2" \ + "print i - whatis with side effects (1)" + +gdb_test "whatis vla2\[i++ + sizeof(j++)\]" "type = int \\\[10\\\]" \ + "whatis vla2\[i++ + sizeof(j++)\]" +gdb_test "print i" " = 2" \ + "print i - whatis with side effects (2)" +gdb_test "print j" " = 0" \ + "print j - whatis with no side effects"