Package: memtester
Severity: important
Tags: patch

To Whom It May Concern:

When attempting to test a range of memory smaller than the pagesize, membytes
is underflowed, and the software attempts to memlock 4GB, thrashing the machine
for several minutes until it is killed.  Find attached a patch to implementing
use of posix_memalign, rather than custom and buggy memory allocation and
allignment.

diff -ur memtester-2.93.1.orig/memtest.c memtester-2.93.1/memtest.c
--- memtester-2.93.1.orig/memtest.c     2000-04-26 18:58:42.000000000 -0400
+++ memtester-2.93.1/memtest.c  2007-01-16 16:01:16.000000000 -0500
@@ -92,9 +92,9 @@
 ui32 run, maxruns, runerrors, totalerrors;
 
 /* Memory globals */
-ui8 *buf, *buf_raw, *s1, *s2;
+ui8 *buf, *s1, *s2;
 ui32 *bp1, *bp2;
-ui32 membytes, memsplit, count, waste;
+ui32 membytes, memsplit, count;
 
 /* Control globals */
 int do_munlock = 1;
@@ -182,8 +182,8 @@
                exit (ERROR);
        }
 
-       sprintf (msg, "Testing %lu bytes at 0x%08lx (%lu bytes lost to page 
alignment)."
-                        "\n\n", membytes, (ui32) buf, waste);
+       sprintf (msg, "Testing %lu bytes at 0x%08lx."
+                        "\n\n", membytes, (ui32) buf);
        OUTBOTH (msg);
 
        /* Main testing loop. */
@@ -310,12 +310,7 @@
 int
 get_mem (void)
 {
-       int rc, pagesize;
-       int do_reduce = 0, do_trylock = 1;
-       ui32 pagesizemask;
-
-       pagesize = getpagesize ();
-       pagesizemask = (ui32) ~(pagesize - 1);
+       int rc, do_reduce = 0, do_trylock = 1;
 
        while (1)
        {
@@ -328,14 +323,15 @@
                        else if (membytes > (2UL << MB)) membytes -= (64UL << 
KB);
                        else if (membytes > (256UL << KB)) membytes -= (16UL << 
KB);
                        else if (membytes > (64UL << KB)) membytes -= (4UL << 
KB);
-                       else membytes -= (1UL << KB);
+                       else membytes -= (1UL << KB); 
+               
                }
 
                /* Make sure it's a multiple of 64. */
                membytes = membytes & 0xFFFFFFC0;
 
-               buf_raw = (ui8 *) malloc (membytes);
-               if (!buf_raw)
+               if (posix_memalign(&buf,  getpagesize(), membytes)
+                                       || buf==NULL)
                {
                        /* Allocation failed.  Trim and try again. */
                        sprintf (msg, "Unable to malloc %lu bytes.\n", 
membytes);
@@ -351,16 +347,6 @@
                {
                        /* Align to page and round off. */
                        OUTBOTH ("trying mlock...");
-                       buf = (char *) ((ui32) buf_raw & pagesizemask);
-                       if (buf < buf_raw) buf += pagesize;
-                       waste = (ui32)buf - (ui32) buf_raw;
-                       membytes = (membytes - waste) & 0xFFFFFFC0;
-                       if (membytes < pagesize)
-                       {
-                               fprintf (stderr, "failed to align to pagesize 
of %i bytes.\n",
-                                                pagesize);
-                               return (ERROR);
-                       }
 
                    rc = mlock (buf, membytes);
                        if (!rc)
@@ -378,7 +364,7 @@
                                case EAGAIN:
                                        OUTBOTH ("failed:  insufficient 
resources.\n");
                                        do_reduce = 1;
-                                       free (buf_raw);
+                                       free (buf);
                                        continue;
 
                                default:
@@ -421,7 +407,7 @@
        }
        if (buf)
        {
-               free (buf_raw);
+               free (buf);
                buf = NULL;
        }
 }
@@ -441,7 +427,8 @@
 void print_summary (void)
 {
        sprintf (msg, "%lu runs completed.  %lu errors detected."
-                       "  Total runtime:  %lu seconds.\n\n", run - 1, 
totalerrors,
+                       "  Total runtime:  %lu seconds.\n\n",
+                       run?run - 1:0, totalerrors,
                        (mtime2 - mtime1));
        OUTBOTH (msg);
        OUTBOTH ("Exiting...\n");


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to