gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] branch master updated (da42f22c -> edf692a6)


From: gnunet
Subject: [libmicrohttpd] branch master updated (da42f22c -> edf692a6)
Date: Wed, 05 Apr 2023 18:46:35 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from da42f22c Upgraded TLS: use more available memory for pumping the data
     new 79c6f9d5 memorypool: cosmetics
     new f37c98c5 memorypool: fixed wrong pointer comparison introduced by 
3444792f14d96a58648add92b8f3b70e4babf772
     new 95ad2a9a memorypool: added more asserts
     new edf692a6 Implemented and used new function MHD_pool_deallocate()

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 configure.ac                |   7 +++
 src/microhttpd/connection.c |  27 ++++++----
 src/microhttpd/memorypool.c | 124 ++++++++++++++++++++++++++++++++++++++++++--
 src/microhttpd/memorypool.h |  18 +++++++
 src/microhttpd/response.c   |  33 +++++-------
 5 files changed, 175 insertions(+), 34 deletions(-)

diff --git a/configure.ac b/configure.ac
index 700c46a4..c1115135 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4303,6 +4303,13 @@ AS_VAR_IF([enable_sanitizers], ["no"], [:],
                    AC_CHECK_HEADERS([sanitizer/asan_interface.h], [], [], 
[AC_INCLUDES_DEFAULT])
                    AS_VAR_IF([ac_cv_header_sanitizer_asan_interface_h],["yes"],
                      [
+                       CFLAGS="${CFLAGS_ac} ${san_CFLAGS} ${san_FLAGS} 
${errattr_CFLAGS} ${user_CFLAGS}"
+                       MHD_CHECK_FUNC([__asan_address_is_poisoned],[[#include 
<sanitizer/asan_interface.h>]],
+                         [[int a_var=1; 
i][f(__asan_address_is_poisoned((void*) &a_var)) return 3;]]
+                       )
+                       MHD_CHECK_FUNC([__asan_region_is_poisoned],[[#include 
<sanitizer/asan_interface.h>]],
+                         [[int a_var=1; i][f(((void*) 0) != 
__asan_region_is_poisoned((void*) &a_var, sizeof(a_var))) return 3;]]
+                       )
                        AC_CACHE_CHECK([whether special function attribute is 
needed for user-poison], [mhd_cv_func_u_p_attribute_needed],
                          [
                            
ASAN_OPTIONS="exitcode=88:detect_invalid_pointer_pairs=3:halt_on_error=1"
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index f378334c..82b127b9 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -1597,11 +1597,20 @@ connection_shrink_read_buffer (struct MHD_Connection 
*connection)
   }
 
   mhd_assert (c->read_buffer_offset <= c->read_buffer_size);
-  new_buf = MHD_pool_reallocate (c->pool, c->read_buffer, c->read_buffer_size,
-                                 c->read_buffer_offset);
-  mhd_assert (c->read_buffer == new_buf);
-  c->read_buffer = new_buf;
-  c->read_buffer_size = c->read_buffer_offset;
+  if (0 == c->read_buffer_offset)
+  {
+    MHD_pool_deallocate (c->pool, c->read_buffer, c->read_buffer_size);
+    c->read_buffer = NULL;
+    c->read_buffer_size = 0;
+  }
+  else
+  {
+    new_buf = MHD_pool_reallocate (c->pool, c->read_buffer, 
c->read_buffer_size,
+                                   c->read_buffer_offset);
+    mhd_assert (c->read_buffer == new_buf);
+    c->read_buffer = new_buf;
+    c->read_buffer_size = c->read_buffer_offset;
+  }
 }
 
 
@@ -2424,10 +2433,10 @@ transmit_error_response_len (struct MHD_Connection 
*connection,
   {
     /* Read buffer is not needed anymore, discard it
      * to free some space for error response. */
-    connection->read_buffer = MHD_pool_reallocate (connection->pool,
-                                                   connection->read_buffer,
-                                                   
connection->read_buffer_size,
-                                                   0);
+    MHD_pool_deallocate (connection->pool,
+                         connection->read_buffer,
+                         connection->read_buffer_size);
+    connection->read_buffer = NULL;
     connection->read_buffer_size = 0;
     connection->read_buffer_offset = 0;
   }
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c
index 92ec6edc..8d80f888 100644
--- a/src/microhttpd/memorypool.c
+++ b/src/microhttpd/memorypool.c
@@ -106,13 +106,13 @@
  * Boolean 'true' if the first pointer is less or equal the second pointer
  */
 #define mp_ptr_le_(p1,p2) \
-  (((const uint8_t*)p1) <= ((const uint8_t*)p2))
+  (((const uint8_t*)(p1)) <= ((const uint8_t*)(p2)))
 /**
  * The difference in bytes between positions of the first and
  * the second pointers
  */
 #define mp_ptr_diff_(p1,p2) \
-  ((size_t)(((const uint8_t*)p1) - ((const uint8_t*)p2)))
+  ((size_t)(((const uint8_t*)(p1)) - ((const uint8_t*)(p2))))
 #else  /* MHD_ASAN_POISON_ACTIVE */
 #define _MHD_RED_ZONE_SIZE (ALIGN_SIZE)
 #define ROUND_TO_ALIGN_PLUS_RED_ZONE(n) (ROUND_TO_ALIGN(n) + 
_MHD_RED_ZONE_SIZE)
@@ -125,14 +125,14 @@
  * Boolean 'true' if the first pointer is less or equal the second pointer
  */
 #define mp_ptr_le_(p1,p2) \
-  (((uintptr_t)((const void*)(p1))) <= ((uintptr_t)((const void*)(p1))))
+  (((uintptr_t)((const void*)(p1))) <= ((uintptr_t)((const void*)(p2))))
 /**
  * The difference in bytes between positions of the first and
  * the second pointers
  */
 #define mp_ptr_diff_(p1,p2) \
-  ((size_t)(((uintptr_t)((const uint8_t*)p1)) - \
-            ((uintptr_t)((const uint8_t*)p2))))
+  ((size_t)(((uintptr_t)((const uint8_t*)(p1))) - \
+            ((uintptr_t)((const uint8_t*)(p2)))))
 #elif defined(FUNC_ATTR_PTRCOMPARE_WORKS) && \
   defined(FUNC_ATTR_PTRSUBTRACT_WORKS)
 #ifdef _DEBUG
@@ -345,6 +345,7 @@ MHD_pool_destroy (struct MemoryPool *pool)
 
   mhd_assert (pool->end >= pool->pos);
   mhd_assert (pool->size >= pool->end - pool->pos);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
   _MHD_UNPOISON_MEMORY (pool->memory, pool->size);
   if (! pool->is_mmap)
     free (pool->memory);
@@ -374,6 +375,7 @@ MHD_pool_get_free (struct MemoryPool *pool)
 {
   mhd_assert (pool->end >= pool->pos);
   mhd_assert (pool->size >= pool->end - pool->pos);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
 #ifdef MHD_ASAN_POISON_ACTIVE
   if ((pool->end - pool->pos) <= _MHD_RED_ZONE_SIZE)
     return 0;
@@ -403,6 +405,7 @@ MHD_pool_allocate (struct MemoryPool *pool,
 
   mhd_assert (pool->end >= pool->pos);
   mhd_assert (pool->size >= pool->end - pool->pos);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
   asize = ROUND_TO_ALIGN_PLUS_RED_ZONE (size);
   if ( (0 == asize) && (0 != size) )
     return NULL; /* size too close to SIZE_MAX */
@@ -452,6 +455,7 @@ MHD_pool_try_alloc (struct MemoryPool *pool,
 
   mhd_assert (pool->end >= pool->pos);
   mhd_assert (pool->size >= pool->end - pool->pos);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
   asize = ROUND_TO_ALIGN_PLUS_RED_ZONE (size);
   if ( (0 == asize) && (0 != size) )
   { /* size is too close to SIZE_MAX, very unlikely */
@@ -505,6 +509,10 @@ MHD_pool_reallocate (struct MemoryPool *pool,
   mhd_assert (pool->size >= pool->end - pool->pos);
   mhd_assert (old != NULL || old_size == 0);
   mhd_assert (pool->size >= old_size);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
+#if defined(MHD_ASAN_POISON_ACTIVE) && defined(HAVE___ASAN_REGION_IS_POISONED)
+  mhd_assert (NULL == __asan_region_is_poisoned (old, old_size));
+#endif /* MHD_ASAN_POISON_ACTIVE && HAVE___ASAN_REGION_IS_POISONED */
 
   if (NULL != old)
   {   /* Have previously allocated data */
@@ -568,6 +576,109 @@ MHD_pool_reallocate (struct MemoryPool *pool,
 }
 
 
+/**
+ * Deallocate a block of memory obtained from the pool.
+ *
+ * If the given block is not the most recently
+ * (re)allocated block, the memory of the this block
+ * allocation may be not released until the pool is
+ * destroyed or reset.
+ *
+ * @param pool memory pool to use for the operation
+ * @param block the allocated block, the NULL is tolerated
+ * @param block_size the size of the allocated block
+ */
+void
+MHD_pool_deallocate (struct MemoryPool *pool,
+                     void *block,
+                     size_t block_size)
+{
+  mhd_assert (pool->end >= pool->pos);
+  mhd_assert (pool->size >= pool->end - pool->pos);
+  mhd_assert (block != NULL || block_size == 0);
+  mhd_assert (pool->size >= block_size);
+  mhd_assert (pool->pos == ROUND_TO_ALIGN (pool->pos));
+
+  if (NULL != block)
+  {   /* Have previously allocated data */
+    const size_t block_offset = mp_ptr_diff_ (block, pool->memory);
+    mhd_assert (mp_ptr_le_ (pool->memory, block));
+    mhd_assert (block_offset <= pool->size);
+    mhd_assert ((block_offset != pool->pos) || (block_size == 0));
+    /* Zero-out deallocated region */
+    if (0 != block_size)
+    {
+      memset (block, 0, block_size);
+      _MHD_POISON_MEMORY (block, block_size);
+    }
+#if ! defined(MHD_FAVOR_SMALL_CODE) && ! defined(MHD_ASAN_POISON_ACTIVE)
+    else
+      return; /* Zero size, no need to do anything */
+#endif /* ! MHD_FAVOR_SMALL_CODE && ! MHD_ASAN_POISON_ACTIVE */
+    if (block_offset <= pool->pos)
+    {
+      /* "Normal" block, not allocated "from the end". */
+      const size_t alg_end =
+        ROUND_TO_ALIGN_PLUS_RED_ZONE (block_offset + block_size);
+      mhd_assert (alg_end <= pool->pos);
+      if (alg_end == pool->pos)
+      {
+        /* The last allocated block, return deallocated block to the pool */
+        size_t alg_start = ROUND_TO_ALIGN (block_offset);
+        mhd_assert (alg_start >= block_offset);
+#if defined(MHD_ASAN_POISON_ACTIVE)
+        if (alg_start != block_offset)
+        {
+          _MHD_POISON_MEMORY (pool->memory + block_offset, \
+                              alg_start - block_offset);
+        }
+        else if (0 != alg_start)
+        {
+          bool need_red_zone_before;
+          mhd_assert (_MHD_RED_ZONE_SIZE <= alg_start);
+#if defined(HAVE___ASAN_REGION_IS_POISONED)
+          need_red_zone_before =
+            (NULL == __asan_region_is_poisoned (pool->memory
+                                                + alg_start
+                                                - _MHD_RED_ZONE_SIZE,
+                                                _MHD_RED_ZONE_SIZE));
+#elif defined(HAVE___ASAN_ADDRESS_IS_POISONED)
+          need_red_zone_before =
+            (0 == __asan_address_is_poisoned (pool->memory + alg_start - 1));
+#else  /* ! HAVE___ASAN_ADDRESS_IS_POISONED */
+          need_red_zone_before = true; /* Unknown, assume new red zone needed 
*/
+#endif /* ! HAVE___ASAN_ADDRESS_IS_POISONED */
+          if (need_red_zone_before)
+          {
+            _MHD_POISON_MEMORY (pool->memory + alg_start, _MHD_RED_ZONE_SIZE);
+            alg_start += _MHD_RED_ZONE_SIZE;
+          }
+        }
+#endif /* MHD_ASAN_POISON_ACTIVE */
+        mhd_assert (alg_start <= pool->pos);
+        mhd_assert (alg_start == ROUND_TO_ALIGN (alg_start));
+        pool->pos = alg_start;
+      }
+    }
+    else
+    {
+      /* Allocated "from the end" block. */
+      /* The size and the pointers of such block should not be manipulated by
+         MHD code (block split is disallowed). */
+      mhd_assert (block_offset >= pool->end);
+      mhd_assert (ROUND_TO_ALIGN (block_offset) == block_offset);
+      if (block_offset == pool->end)
+      {
+        /* The last allocated block, return deallocated block to the pool */
+        const size_t alg_end =
+          ROUND_TO_ALIGN_PLUS_RED_ZONE (block_offset + block_size);
+        pool->end = alg_end;
+      }
+    }
+  }
+}
+
+
 /**
  * Clear all entries from the memory pool except
  * for @a keep of the given @a copy_bytes.  The pointer
@@ -596,6 +707,9 @@ MHD_pool_reset (struct MemoryPool *pool,
   /* (keep == NULL || pool->memory + pool->size >= (uint8_t*) keep + 
copy_bytes) */
   mhd_assert ((keep == NULL) || \
               (pool->size >= mp_ptr_diff_ (keep, pool->memory) + copy_bytes));
+#if defined(MHD_ASAN_POISON_ACTIVE) && defined(HAVE___ASAN_REGION_IS_POISONED)
+  mhd_assert (NULL == __asan_region_is_poisoned (keep, copy_bytes));
+#endif /* MHD_ASAN_POISON_ACTIVE && HAVE___ASAN_REGION_IS_POISONED */
   _MHD_UNPOISON_MEMORY (pool->memory, new_size);
   if ( (NULL != keep) &&
        (keep != pool->memory) )
diff --git a/src/microhttpd/memorypool.h b/src/microhttpd/memorypool.h
index 98f96bda..49bbcbe1 100644
--- a/src/microhttpd/memorypool.h
+++ b/src/microhttpd/memorypool.h
@@ -147,6 +147,24 @@ size_t
 MHD_pool_get_free (struct MemoryPool *pool);
 
 
+/**
+ * Deallocate a block of memory obtained from the pool.
+ *
+ * If the given block is not the most recently
+ * (re)allocated block, the memory of the this block
+ * allocation may be not released until the pool is
+ * destroyed or reset.
+ *
+ * @param pool memory pool to use for the operation
+ * @param block the allocated block, the NULL is tolerated
+ * @param block_size the size of the allocated block
+ */
+void
+MHD_pool_deallocate (struct MemoryPool *pool,
+                     void *block,
+                     size_t block_size);
+
+
 /**
  * Clear all entries from the memory pool except
  * for @a keep of the given @a copy_bytes.  The pointer
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 159c8ecb..d125ea24 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -2148,28 +2148,21 @@ MHD_response_execute_upgrade_ (struct MHD_Response 
*response,
     size_t avail;
     char *buf;
 
-    if (0 != connection->write_buffer_size)
-    {
-      mhd_assert (NULL != connection->write_buffer);
-      /* All data should be sent already */
-      mhd_assert (connection->write_buffer_send_offset == \
-                  connection->write_buffer_append_offset);
-      (void) MHD_pool_reallocate (pool, connection->write_buffer,
-                                  connection->write_buffer_size, 0);
-      connection->write_buffer_append_offset = 0;
-      connection->write_buffer_send_offset = 0;
-      connection->write_buffer_size = 0;
-    }
+    /* All data should be sent already */
+    mhd_assert (connection->write_buffer_send_offset == \
+                connection->write_buffer_append_offset);
+    MHD_pool_deallocate (pool, connection->write_buffer,
+                         connection->write_buffer_size);
+    connection->write_buffer_append_offset = 0;
+    connection->write_buffer_send_offset = 0;
+    connection->write_buffer_size = 0;
     connection->write_buffer = NULL;
 
-    if (0 != connection->read_buffer_size)
-    {
-      mhd_assert (NULL != connection->read_buffer);
-      (void) MHD_pool_reallocate (pool, connection->read_buffer,
-                                  connection->read_buffer_size, 0);
-      connection->read_buffer_offset = 0;
-      connection->read_buffer_size = 0;
-    }
+    /* Extra read data should be processed already by the application */
+    MHD_pool_deallocate (pool, connection->read_buffer,
+                         connection->read_buffer_size);
+    connection->read_buffer_offset = 0;
+    connection->read_buffer_size = 0;
     connection->read_buffer = NULL;
 
     avail = MHD_pool_get_free (pool);

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]