From: Mark Wielaard <mark@klomp.org>
To: vvvvvv@google.com
Cc: elfutils-devel@sourceware.org, kernel-team@android.com,
maennich@google.com
Subject: Re: [PATCH] libdw: check memory access in get_(u|s)leb128
Date: Sun, 12 Feb 2023 00:42:58 +0100 [thread overview]
Message-ID: <20230211234258.GG2430@gnu.wildebeest.org> (raw)
In-Reply-To: <20230125160530.949622-1-vvvvvv@google.com>
Hi Aleksei,
On Wed, Jan 25, 2023 at 04:05:30PM +0000, Aleksei Vetrov via Elfutils-devel wrote:
> From: Aleksei Vetrov <vvvvvv@google.com>
>
> __libdw_get_uleb128 and __libdw_get_sleb128 should check if addrp has
> already reached the end before unrolling the first step. It is done by
> moving __libdw_max_len to the beginning of the function, which already
> has all the checks.
I did some performance tests and couldn't find any significant change.
Even with my other extra checks added in libdw and readelf.
One question about the sleb128 case though:
> static inline int64_t
> __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
> {
> + const size_t max = __libdw_max_len_sleb128 (*addrp, end);
> /* Do the work in an unsigned type, but use implementation-defined
> behavior to cast to signed on return. This avoids some undefined
> behavior when shifting. */
> @@ -131,9 +133,9 @@ __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
>
> /* Unroll the first step to help the compiler optimize
> for the common single-byte case. */
> - get_sleb128_step (acc, *addrp, 0);
> + if (likely (max > 0))
> + get_sleb128_step (acc, *addrp, 0);
>
> - const size_t max = __libdw_max_len_sleb128 (*addrp - 1, end);
> for (size_t i = 1; i < max; ++i)
> get_sleb128_step (acc, *addrp, i);
> if (*addrp == end)
But what about the case where *addrp > end?
After this code we will do:
/* There might be one extra byte. */
unsigned char b = **addrp;
++*addrp;
So I think we want to catch that too. Easiest imho seems to move (and
invert) the max check immediately after calculating max:
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index 1cac6af3..72348623 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.h
@@ -126,6 +126,9 @@ static inline int64_t
__libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
{
const size_t max = __libdw_max_len_sleb128 (*addrp, end);
+ if (unlikely (max == 0))
+ return INT64_MAX;
+
/* Do the work in an unsigned type, but use implementation-defined
behavior to cast to signed on return. This avoids some undefined
behavior when shifting. */
@@ -133,8 +136,7 @@ __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
/* Unroll the first step to help the compiler optimize
for the common single-byte case. */
- if (likely (max > 0))
- get_sleb128_step (acc, *addrp, 0);
+ get_sleb128_step (acc, *addrp, 0);
for (size_t i = 1; i < max; ++i)
get_sleb128_step (acc, *addrp, i);
Cheers,
Mark
next prev parent reply other threads:[~2023-02-11 23:42 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-25 16:05 vvvvvv
2023-01-26 21:05 ` Mark Wielaard
2023-02-07 16:17 ` Aleksei Vetrov
2023-02-07 17:17 ` Mark Wielaard
2023-02-11 23:42 ` Mark Wielaard [this message]
2023-02-13 20:03 ` Aleksei Vetrov
2023-02-13 20:10 ` [PATCH v2] " vvvvvv
2023-02-14 15:44 ` Mark Wielaard
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=20230211234258.GG2430@gnu.wildebeest.org \
--to=mark@klomp.org \
--cc=elfutils-devel@sourceware.org \
--cc=kernel-team@android.com \
--cc=maennich@google.com \
--cc=vvvvvv@google.com \
/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).