public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: carlos@redhat.com, libc-alpha@sourceware.org
Subject: Re: [PATCH] [BZ 20628] make mallinfo saturating
Date: Thu, 06 Oct 2016 17:20:00 -0000	[thread overview]
Message-ID: <xnk2dlpf6q.fsf@greed.delorie.com> (raw)
In-Reply-To: <9b2ad307-2db2-c270-a4b9-97ca87e5208f@cs.ucla.edu> (message from Paul Eggert on Wed, 5 Oct 2016 23:34:07 -0700)


Version 3.  Cast SUM to unsigned first, in case it's pre-wrapped.
Expanded comment.  Verified that ADD is always INTERNAL_SIZE_T so no
need to cast that to unsigned.

	[BZ #20628]
	* malloc/malloc.c (int_mallinfo): Use saturating add instead of
	overflow to collect statistics into a fixed "int" container.

diff --git a/malloc/malloc.c b/malloc/malloc.c
index ef04360..9d4018f 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -224,6 +224,7 @@
 #include <unistd.h>
 #include <stdio.h>    /* needed for malloc_stats */
 #include <errno.h>
+#include <limits.h>
 
 #include <shlib-compat.h>
 
@@ -4594,8 +4595,8 @@ int_mallinfo (mstate av, struct mallinfo *m)
   mchunkptr p;
   INTERNAL_SIZE_T avail;
   INTERNAL_SIZE_T fastavail;
-  int nblocks;
-  int nfastblocks;
+  INTERNAL_SIZE_T nblocks;
+  INTERNAL_SIZE_T nfastblocks;
 
   /* Ensure initialization */
   if (av->top == 0)
@@ -4633,18 +4634,38 @@ int_mallinfo (mstate av, struct mallinfo *m)
         }
     }
 
-  m->smblks += nfastblocks;
-  m->ordblks += nblocks;
-  m->fordblks += avail;
-  m->uordblks += av->system_mem - avail;
-  m->arena += av->system_mem;
-  m->fsmblks += fastavail;
+/* Saturated add - add ADD to SUM.  If the result exceeds the range of
+   "int" (or, wrapped, "unsigned int"), set SUM to UINT_MAX instead
+   ((int)-1).  Assumes ADD and SUM reflect positive values, even when
+   they wrap to negative, and that SUM is type "int".  The published
+   ABI prevents us from bumping "int" to a larger type.  Note: this
+   macro doesn't handle overflow when INTERNAL_SIZE_T is the same size
+   as "int", but in that case, the things we're counting wouldn't
+   cause an overflow anyway.
+
+   The net result is that sums which would wrap around and become
+   misleading positive values again, stop at -1, so any positive value
+   we report is accurate, and any negative number other than -1 we
+   report can be cast to unsigned to become accurate.  */
+#define SAT_ADD(SUM, ADD) \
+  ({ INTERNAL_SIZE_T tmp = (INTERNAL_SIZE_T)(unsigned)(SUM) + (INTERNAL_SIZE_T)(ADD); SUM = (tmp > UINT_MAX) ? -1 : tmp; })
+
+/* Likewise, but assign ADD to SUM.  */
+#define SAT_SET(SUM, ADD) \
+  ({ SUM = ((INTERNAL_SIZE_T)(ADD) > UINT_MAX) ? -1 : (ADD); })
+
+  SAT_ADD (m->smblks, nfastblocks);
+  SAT_ADD (m->ordblks, nblocks);
+  SAT_ADD (m->fordblks, avail);
+  SAT_ADD (m->uordblks, av->system_mem - avail);
+  SAT_ADD (m->arena, av->system_mem);
+  SAT_ADD (m->fsmblks, fastavail);
   if (av == &main_arena)
     {
-      m->hblks = mp_.n_mmaps;
-      m->hblkhd = mp_.mmapped_mem;
-      m->usmblks = 0;
-      m->keepcost = chunksize (av->top);
+      SAT_SET (m->hblks, mp_.n_mmaps);
+      SAT_SET (m->hblkhd, mp_.mmapped_mem);
+      SAT_SET (m->usmblks, 0);
+      SAT_SET (m->keepcost, chunksize (av->top));
     }
 }
 

  parent reply	other threads:[~2016-10-06 17:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-27 18:09 DJ Delorie
2016-09-27 19:07 ` Paul Eggert
2016-09-27 19:17   ` DJ Delorie
2016-09-28 20:26   ` Carlos O'Donell
2016-10-04 20:04     ` DJ Delorie
2016-10-06  0:45       ` Paul Eggert
2016-10-06  1:46         ` DJ Delorie
2016-10-06  6:34           ` Paul Eggert
2016-10-06 16:52             ` DJ Delorie
2016-10-06 20:40               ` Paul Eggert
2016-10-06 20:43                 ` Joseph Myers
2016-10-06 21:21                   ` Paul Eggert
2016-10-06 17:20             ` DJ Delorie [this message]
2017-01-13  2:14 DJ Delorie

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=xnk2dlpf6q.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=carlos@redhat.com \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.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).