From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27530 invoked by alias); 5 May 2015 21:47:40 -0000 Mailing-List: contact gdb-testers-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-testers-owner@sourceware.org Received: (qmail 27481 invoked by uid 89); 5 May 2015 21:47:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2 X-HELO: kwanyin.sergiodj.net Received: from kwanyin.sergiodj.net (HELO kwanyin.sergiodj.net) (176.31.208.32) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 05 May 2015 21:47:38 +0000 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [binutils-gdb] GDB crash trying to subscript array of variant record. From: sergiodj+buildbot@redhat.com To: gdb-testers@sourceware.org Message-Id: Date: Tue, 05 May 2015 21:47:00 -0000 X-SW-Source: 2015-q2/txt/msg03606.txt.bz2 *** TEST RESULTS FOR COMMIT fc958966e47f622d738088509bacd0573a9db2c7 *** Author: Joel Brobecker Branch: master Commit: fc958966e47f622d738088509bacd0573a9db2c7 GDB crash trying to subscript array of variant record. Consider the following declarations: subtype Small_Type is Integer range 0 .. 10; type Record_Type (I : Small_Type := 0) is record S : String (1 .. I); end record; A2 : Array_Type := (1 => (I => 2, S => "AB"), 2 => (I => 1, S => "A"), 3 => (I => 0, S => <>)); Compiled with -fgnat-encodings=minimal, and trying to print one element of our array, valgrind reports an invalid memory access. On certain GNU/Linux boxes, malloc even reports it as well, and causes GDB to crash. (gdb) print a2(1) *** glibc detected *** /[...]/gdb: malloc(): memory corruption: 0x0a30ba48 *** [crash] The invalid memory access occurs because of a simple buffer overflow in ada_value_primitive_packed_val. When this function is called, it is given a bit_size of 128 (or 16 bytes), which corresponds to the stride of our array. But the actual size of each element depends on its value. In particular, A2(1) is a record whose size is only 6 bytes. What happens in our example is that we start building a new value (v) where the element is to be unpacked, with any of its dynamic properties getting resolved as well. We then unpack the data into this value's buffer: unpacked = (unsigned char *) value_contents (v); [...] nsrc = len; [...] while (nsrc > 0) { [...] unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT); [...] targ += delta; [...] nsrc -= 1; [...] } In the loop above, targ starts at zero (for LE architectures), and len is 16. With delta being +1, we end up iterating 16 times, writing 16 bytes into a 6-bytes buffer. This patch fixes the issue by adjusting BIT_SIZE and recomputing LEN after having resolved our type if the resolved type turns out to be smaller than bit_size. gdb/ChangeLog: * ada-lang.c (ada_value_primitive_packed_val): Recompute BIT_SIZE and LEN if the size of the resolved type is smaller than BIT_SIZE * HOST_CHAR_BIT.