]> Savannah Git Hosting - gnulib.git/commitdiff
malloca: Take advantage of CHERI bounds-checking.
authorBruno Haible <bruno@clisp.org>
Sat, 11 Nov 2023 19:04:02 +0000 (20:04 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 11 Nov 2023 19:04:02 +0000 (20:04 +0100)
* lib/malloca.h: Include <cheri.h>.
(malloca) [CHERI]: In the stack-allocation case, return a pointer with
a tight lower bound and a tight upper bound.
* lib/malloca.c: Include <cheri.h>.
(small_t) [CHERI]: Define as uintptr_t.
(mmalloca) [CHERI]: Return a pointer with a tight upper bound.
(freea) [CHERI]: Update.

ChangeLog
lib/malloca.c
lib/malloca.h

index 1fd39a4a448b7cb780d5609c3d8b0238b7f956ec..af8ead5fc2c4d1a96ebe8c2e11bedb5a875f751a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-11-11  Bruno Haible  <bruno@clisp.org>
+
+       malloca: Take advantage of CHERI bounds-checking.
+       * lib/malloca.h: Include <cheri.h>.
+       (malloca) [CHERI]: In the stack-allocation case, return a pointer with
+       a tight lower bound and a tight upper bound.
+       * lib/malloca.c: Include <cheri.h>.
+       (small_t) [CHERI]: Define as uintptr_t.
+       (mmalloca) [CHERI]: Return a pointer with a tight upper bound.
+       (freea) [CHERI]: Update.
+
 2023-11-11  Bruno Haible  <bruno@clisp.org>
 
        safe-alloc: Take advantage of CHERI bounds-checking.
index 690ce2324b2119905ea82f574267b0d185bc3925..f98fdf152d56b7bc1985a4b7c202811a80b70b0c 100644 (file)
@@ -22,6 +22,9 @@
 #include "malloca.h"
 
 #include <stdckdint.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
 
 #include "idx.h"
 
        allocation.
      - NULL comes from a failed heap allocation.  */
 
+#if defined __CHERI__
+/* Type for holding the original malloc() result.  */
+typedef uintptr_t small_t;
+#else
 /* Type for holding very small pointer differences.  */
 typedef unsigned char small_t;
 /* Verify that it is wide enough.  */
 static_assert (2 * sa_alignment_max - 1 <= (small_t) -1);
+#endif
 
 void *
 mmalloca (size_t n)
@@ -56,20 +64,28 @@ mmalloca (size_t n)
 
       if (mem != NULL)
         {
-          uintptr_t umem = (uintptr_t)mem, umemplus;
+          uintptr_t umem = (uintptr_t) mem;
           /* The ckd_add avoids signed integer overflow on
              theoretical platforms where UINTPTR_MAX <= INT_MAX.  */
+          uintptr_t umemplus;
           ckd_add (&umemplus, umem, sizeof (small_t) + sa_alignment_max - 1);
           idx_t offset = (umemplus - umemplus % (2 * sa_alignment_max)
                           + sa_alignment_max - umem);
-          void *vp = mem + offset;
-          small_t *p = vp;
+          void *p = mem + offset;
           /* Here p >= mem + sizeof (small_t),
              and p <= mem + sizeof (small_t) + 2 * sa_alignment_max - 1
              hence p + n <= mem + nplus.
              So, the memory range [p, p+n) lies in the allocated memory range
              [mem, mem + nplus).  */
-          p[-1] = offset;
+          small_t *sp = p;
+# if defined __CHERI__
+          sp[-1] = umem;
+          p = (char *) cheri_bounds_set ((char *) p - sizeof (small_t),
+                                         sizeof (small_t) + n)
+              + sizeof (small_t);
+# else
+          sp[-1] = offset;
+# endif
           /* p ≡ sa_alignment_max mod 2*sa_alignment_max.  */
           return p;
         }
@@ -101,7 +117,11 @@ freea (void *p)
     {
       char *cp = p;
       small_t *sp = p;
+# if defined __CHERI__
+      void *mem = sp[-1];
+# else
       void *mem = cp - sp[-1];
+# endif
       free (mem);
     }
 }
index f68ddfe0105a7e1a935b487f7c82323f5d479896..120f406880e4f731daacca96b646d60d422b1113 100644 (file)
@@ -28,6 +28,9 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdint.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
 
 #include "xalloc-oversized.h"
 
@@ -68,12 +71,24 @@ extern void freea (void *p);
    memory allocated on the stack, that must be freed using freea() before
    the function returns.  Upon failure, it returns NULL.  */
 #if HAVE_ALLOCA
-# define malloca(N) \
-  ((N) < 4032 - (2 * sa_alignment_max - 1)                                   \
-   ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
-                + (2 * sa_alignment_max - 1))                                \
-               & ~(uintptr_t)(2 * sa_alignment_max - 1))                     \
-   : mmalloca (N))
+# if defined __CHERI__
+#  define malloca(N) \
+    ((N) < 4032 - (2 * sa_alignment_max - 1)                                  \
+     ? cheri_bounds_set ((void *) (((uintptr_t)                               \
+                                     (char *)                                 \
+                                      alloca ((N) + 2 * sa_alignment_max - 1) \
+                                    + (2 * sa_alignment_max - 1))             \
+                                   & ~(uintptr_t)(2 * sa_alignment_max - 1)), \
+                         (N))                                                 \
+     : mmalloca (N))
+# else
+#  define malloca(N) \
+    ((N) < 4032 - (2 * sa_alignment_max - 1)                                   \
+     ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
+                  + (2 * sa_alignment_max - 1))                                \
+                 & ~(uintptr_t)(2 * sa_alignment_max - 1))                     \
+     : mmalloca (N))
+# endif
 #else
 # define malloca(N) \
   mmalloca (N)