From patchwork Thu Nov 24 16:15:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Arnez X-Patchwork-Id: 17846 Received: (qmail 48409 invoked by alias); 24 Nov 2016 16:15:19 -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 48374 invoked by uid 89); 24 Nov 2016 16:15:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=D*linux.vnet.ibm.com, spell, STR, naming X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0b-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.158.5) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 24 Nov 2016 16:15:13 +0000 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uAOGDuHK083600 for ; Thu, 24 Nov 2016 11:15:12 -0500 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0b-001b2d01.pphosted.com with ESMTP id 26x0tau4j1-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 24 Nov 2016 11:15:11 -0500 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 24 Nov 2016 16:15:09 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 24 Nov 2016 16:15:06 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 450361B08023; Thu, 24 Nov 2016 16:17:25 +0000 (GMT) Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id uAOGF5gr50987078; Thu, 24 Nov 2016 16:15:05 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 31C3952047; Thu, 24 Nov 2016 15:13:52 +0000 (GMT) Received: from oc1027705133.ibm.com (unknown [9.152.212.182]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTPS id 03AC552043; Thu, 24 Nov 2016 15:13:51 +0000 (GMT) From: Andreas Arnez To: Pedro Alves Cc: gdb-patches@sourceware.org Subject: Re: [PATCH 2/3] Fix copy_bitwise() References: <1479135786-31150-1-git-send-email-arnez@linux.vnet.ibm.com> <1479135786-31150-3-git-send-email-arnez@linux.vnet.ibm.com> <6032efa2-828d-7423-4720-6925a9b4ea4b@redhat.com> <22c53936-c8c5-fc1e-f50b-d208ef21587f@redhat.com> Date: Thu, 24 Nov 2016 17:15:03 +0100 In-Reply-To: <22c53936-c8c5-fc1e-f50b-d208ef21587f@redhat.com> (Pedro Alves's message of "Tue, 22 Nov 2016 23:18:15 +0000") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16112416-0028-0000-0000-0000025720C2 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16112416-0029-0000-0000-00002158B1C9 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-11-24_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1611240285 X-IsSubscribed: yes On Tue, Nov 22 2016, Pedro Alves wrote: > I read the whole series now, and it looks OK to me. Good, thanks for reviewing! I'll push the series as soon as we've settled on the self test. > More below on the selftest though. [...] > So now I have a suggested patch, either on top of yours, or to > merge with yours, whatever you prefer. > >> +static void >> +copy_bitwise_tests (void) >> +{ >> + /* Some random data, to be used for source and target buffers. */ >> + static const gdb_byte source_data[] = >> + { 0xdd, 0x85, 0x51, 0x93, 0xb3, 0xc0, 0x62, 0xd4, >> + 0xa6, 0x4b, 0x0a, 0xde, 0x36, 0x35, 0x1a, 0x66 }; >> + >> + static const gdb_byte dest_data[] = >> + { 0x47, 0x6a, 0x65, 0x61, 0xab, 0xed, 0xb6, 0xa9, >> + 0xbf, 0x67, 0xb2, 0xeb, 0x86, 0x45, 0xfe, 0x09 }; > > I think that instead writing a all-zeros source on top of an all-zeros > destination and the converse would be clearer IMO. Also, to cover > alternating bits we can include all 0xaa's and all 0x55's patterns. > I think that gives as much, if not more coverage while being more > "deterministic". Hm, I'm not quite convinced. Fooling the test with a broken algorithm is probably easier with regular test patterns than with pseudo-random ones. For instance, consider an implementation that does not advance the byte position in the source buffer at all. I do understand your concern about being "deterministic", though. Thus I've taken another shot, see the patch below. > I think it's beneficial for readability to spell out "offset", > and to normalize from -> source, to -> dest, to match the other > variables and the parameter names of the callees. Yeah, consistent naming is a good idea. > And finally, getting back to check_copy_bitwise, I think > a comment like the one added by this patch makes it easier > to understand what's going on. Sure. Maybe I was too much into the material and thus considered it obvious ;-) Here's the adjusted patch. -- >8 -- Subject: [PATCH v3] Add unit test for copy_bitwise This adds a unit test for the copy_bitwise function in dwarf2loc.c. With the old (broken) version of copy_bitwise this test would generate the following failure message: (gdb) maintenance selftest Self test failed: copy_bitwise 11000000 != 10000000 (7+2 -> 0) gdb/ChangeLog: 2016-11-24 Andreas Arnez Pedro Alves * dwarf2loc.c (bits_to_str, check_copy_bitwise) (copy_bitwise_tests): New functions. (_initialize_dwarf2loc): Register the new function copy_bitwise_tests as a unit test. * selftest.c (run_self_tests): Improve the failure message's wording and formatting. --- gdb/dwarf2loc.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/selftest.c | 3 +- 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c index b238133..f7f13cf 100644 --- a/gdb/dwarf2loc.c +++ b/gdb/dwarf2loc.c @@ -38,6 +38,7 @@ #include "dwarf2loc.h" #include "dwarf2-frame.h" #include "compile/compile.h" +#include "selftest.h" #include #include @@ -1567,6 +1568,141 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset, } } +#if GDB_SELF_TEST + +namespace selftests { + +/* Helper function for the unit test of copy_bitwise. Convert NBITS bits + out of BITS, starting at OFFS, to the respective '0'/'1'-string. MSB0 + specifies whether to assume big endian bit numbering. Store the + resulting (not null-terminated) string at STR. */ + +static void +bits_to_str (char *str, const gdb_byte *bits, ULONGEST offs, + ULONGEST nbits, int msb0) +{ + unsigned int j; + size_t i; + + for (i = offs / 8, j = offs % 8; nbits; i++, j = 0) + { + unsigned int ch = bits[i]; + for (; j < 8 && nbits; j++, nbits--) + *str++ = (ch & (msb0 ? (1 << (7 - j)) : (1 << j))) ? '1' : '0'; + } +} + +/* Check one invocation of copy_bitwise with the given parameters. */ + +static void +check_copy_bitwise (const gdb_byte *dest, unsigned int dest_offset, + const gdb_byte *source, unsigned int source_offset, + unsigned int nbits, int msb0) +{ + size_t len = align_up (dest_offset + nbits, 8); + char *expected = (char *) alloca (len + 1); + char *actual = (char *) alloca (len + 1); + gdb_byte *buf = (gdb_byte *) alloca (len / 8); + + /* Compose a '0'/'1'-string that represents the expected result of + copy_bitwise below: + Bits from [0, DEST_OFFSET) are filled from DEST. + Bits from [DEST_OFFSET, DEST_OFFSET + NBITS) are filled from SOURCE. + Bits from [DEST_OFFSET + NBITS, LEN) are filled from DEST. + + E.g., with: + dest_offset: 4 + nbits: 2 + len: 8 + dest: 00000000 + source: 11111111 + + We should end up with: + buf: 00001100 + DDDDSSDD (D=dest, S=source) + */ + bits_to_str (expected, dest, 0, len, msb0); + bits_to_str (expected + dest_offset, source, source_offset, nbits, msb0); + + /* Fill BUF with data from DEST, apply copy_bitwise, and convert the + result to a '0'/'1'-string. */ + memcpy (buf, dest, len / 8); + copy_bitwise (buf, dest_offset, source, source_offset, nbits, msb0); + bits_to_str (actual, buf, 0, len, msb0); + + /* Compare the resulting strings. */ + expected[len] = actual[len] = '\0'; + if (strcmp (expected, actual) != 0) + error (_("copy_bitwise %s != %s (%u+%u -> %u)"), + expected, actual, source_offset, nbits, dest_offset); +} + +/* Unit test for copy_bitwise. */ + +static void +copy_bitwise_tests (void) +{ + /* Data to be used as both source and destination buffers. The two + arrays below represent the lsb0- and msb0- encoded versions of the + following bit string, respectively: + 00000000 00011111 11111111 01001000 10100101 11110010 + This pattern is chosen such that it contains: + - constant 0- and 1- chunks of more than a full byte; + - 0/1- and 1/0 transitions on all bit positions within a byte; + - several sufficiently asymmetric bytes. + */ + static const gdb_byte data_lsb0[] = { + 0x00, 0xf8, 0xff, 0x12, 0xa5, 0x4f + }; + static const gdb_byte data_msb0[] = { + 0x00, 0x1f, 0xff, 0x48, 0xa5, 0xf2 + }; + + constexpr size_t data_nbits = 8 * sizeof (data_lsb0); + constexpr unsigned max_nbits = 24; + + /* Try all combinations of: + lsb0/msb0 bit order (using the respective data array) + X [0, MAX_NBITS] copy bit width + X feasible source offsets for the given copy bit width + X feasible destination offsets + */ + for (int msb0 = 0; msb0 < 2; msb0++) + { + const gdb_byte *data = msb0 ? data_msb0 : data_lsb0; + + for (unsigned int nbits = 1; nbits <= max_nbits; nbits++) + { + const unsigned int max_offset = data_nbits - nbits; + + for (unsigned source_offset = 0; + source_offset <= max_offset; + source_offset++) + { + for (unsigned dest_offset = 0; + dest_offset <= max_offset; + dest_offset++) + { + check_copy_bitwise (data + dest_offset / 8, + dest_offset % 8, + data + source_offset / 8, + source_offset % 8, + nbits, msb0); + } + } + } + + /* Special cases: copy all, copy nothing. */ + check_copy_bitwise (data_lsb0, 0, data_msb0, 0, data_nbits, msb0); + check_copy_bitwise (data_msb0, 0, data_lsb0, 0, data_nbits, msb0); + check_copy_bitwise (data, data_nbits - 7, data, 9, 0, msb0); + } +} + +} /* namespace selftests */ + +#endif /* GDB_SELF_TEST */ + static void read_pieced_value (struct value *v) { @@ -4527,4 +4663,8 @@ _initialize_dwarf2loc (void) NULL, show_entry_values_debug, &setdebuglist, &showdebuglist); + +#if GDB_SELF_TEST + register_self_test (selftests::copy_bitwise_tests); +#endif } diff --git a/gdb/selftest.c b/gdb/selftest.c index 110b9e5..f771e57 100644 --- a/gdb/selftest.c +++ b/gdb/selftest.c @@ -50,8 +50,7 @@ run_self_tests (void) CATCH (ex, RETURN_MASK_ERROR) { ++failed; - exception_fprintf (gdb_stderr, ex, - _("Self test threw exception")); + exception_fprintf (gdb_stderr, ex, _("Self test failed: ")); } END_CATCH }