From: Mark Wielaard <mjw@redhat.com>
To: elfutils-devel@lists.fedorahosted.org
Subject: Re: [PATCH 2/3] Simplify and inline get_uleb128 and get_sleb128
Date: Tue, 22 Apr 2014 16:55:41 +0200 [thread overview]
Message-ID: <1398178541.29199.180.camel@bordewijk.wildebeest.org> (raw)
In-Reply-To: 52AA0932.6000704@redhat.com
[-- Attachment #1: Type: text/plain, Size: 1552 bytes --]
On Thu, 2013-12-12 at 11:06 -0800, Josh Stone wrote:
> On 12/12/2013 04:13 AM, Petr Machata wrote:
> > Josh Stone <jistone@redhat.com> writes:
> >> +#define get_sleb128_step(var, addr, nth) \
> >> do { \
> >> + unsigned char __b = *(addr)++; \
> >> + if (likely ((__b & 0x80) == 0)) \
> >> { \
> >> + struct { signed int i:7; } __s = { .i = __b }; \
> >> + (var) |= (typeof (var)) __s.i << ((nth) * 7); \
> >
> > Oh, the bitfield trick is clever!
>
> I should give credit, I found that trick here:
> http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
>
> The former code was trying to sign-extend after the value was shifted
> and combined, which as a variable width is harder. I really like that
> this way the compiler is fully aware that this is a sign extension,
> rather than being a side effect of ORing bits or left-right shifts.
Sadly the neat trick triggers undefined behavior since we are trying to
left shift a negative value. Even though it appears to work currently I
am slightly afraid a compiler optimization might take advantage of this
in the future (since it is undefined behavior it could just assume
negative values won't occur) especially since this code is inlined in a
lot of places, causing hard to diagnose errors.
The attached patch is very much not clever, but does what is intended in
a well-defined way (it is basically what the DWARF spec gives as pseudo
code). Does anybody see a better way?
Thanks,
Mark
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-libdw-get_sleb128_step-Remove-undefined-behavior.patch --]
[-- Type: text/x-patch, Size: 1707 bytes --]
>From 0eedb6486806dba9c454edcc249238e096961e09 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mjw@redhat.com>
Date: Tue, 22 Apr 2014 16:43:11 +0200
Subject: [PATCH] libdw (get_sleb128_step): Remove undefined behavior.
As pointed out by gcc -fsanitize=undefined left shifting a negative value
is undefined. Replace it with an explicit sign extension step.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
libdw/ChangeLog | 5 +++++
libdw/memory-access.h | 5 +++--
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 49d70af..e561e00 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2014-04-22 Mark Wielaard <mjw@redhat.com>
+
+ * memory-access.h (get_sleb128_step): Remove undefined behavior
+ of left shifting a signed value. Add explicit sign extension.
+
2014-04-13 Mark Wielaard <mjw@redhat.com>
* Makefile.am: Remove !MUDFLAP conditions.
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index d0ee63c..c6e4bdc 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.h
@@ -70,8 +70,9 @@ __libdw_get_uleb128 (const unsigned char **addrp)
unsigned char __b = *(addr)++; \
if (likely ((__b & 0x80) == 0)) \
{ \
- struct { signed int i:7; } __s = { .i = __b }; \
- (var) |= (typeof (var)) __s.i << ((nth) * 7); \
+ (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
+ if ((((nth) + 1) < 8 * sizeof (var)) && (__b & 0x40)) \
+ (var) |= -(((uint64_t) 1) << (((nth) + 1) * 7)); \
return (var); \
} \
(var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
--
1.9.0
next reply other threads:[~2014-04-22 14:55 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-22 14:55 Mark Wielaard [this message]
-- strict thread matches above, loose matches on Subject: below --
2014-04-24 9:49 Mark Wielaard
2014-04-23 22:32 Petr Machata
2014-04-23 21:54 Mark Wielaard
2014-04-23 20:29 Petr Machata
2014-04-23 19:01 Josh Stone
2014-04-23 18:51 Richard Henderson
2014-04-23 18:32 Petr Machata
2014-04-23 15:27 Richard Henderson
2014-04-23 10:17 Petr Machata
2014-04-22 22:04 Mark Wielaard
2014-04-22 15:58 Richard Henderson
2014-04-22 15:52 Josh Stone
2014-04-22 15:03 Richard Henderson
2014-04-22 15:01 Richard Henderson
2013-12-12 22:23 Petr Machata
2013-12-12 19:06 Josh Stone
2013-12-12 12:13 Petr Machata
2013-12-11 1:35 Josh Stone
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1398178541.29199.180.camel@bordewijk.wildebeest.org \
--to=mjw@redhat.com \
--cc=elfutils-devel@lists.fedorahosted.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).