casaroli commented on code in PR #21215:
URL: https://github.com/apache/nuttx/pull/21215#discussion_r4426282058


##########
libs/libc/elf/elf_load.c:
##########
@@ -56,9 +56,11 @@
 #define ELF_ALIGNUP(a)   (((unsigned long)(a) + ELF_ALIGN_MASK) & 
~ELF_ALIGN_MASK)
 #define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
 
-/* _ALIGN_UP: 'a' is assumed to be a power of two */
+/* _ALIGN_UP: 'a' is assumed to be a power of two.  0 and 1 mean no
+ * alignment (sh_addralign in the ELF specification).
+ */
 
-#define _ALIGN_UP(v, a)  (((v) + ((a) - 1)) & ~((a) - 1))
+#define _ALIGN_UP(v, a)  ((a) > 1 ? (((v) + ((a) - 1)) & ~((a) - 1)) : (v))

Review Comment:
   Done.
   



##########
libs/libc/elf/elf_load.c:
##########
@@ -56,9 +56,11 @@
 #define ELF_ALIGNUP(a)   (((unsigned long)(a) + ELF_ALIGN_MASK) & 
~ELF_ALIGN_MASK)
 #define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)

Review Comment:
   Done.
   



##########
libs/libc/elf/elf_load.c:
##########
@@ -145,15 +145,54 @@ static int libelf_section_alloc(FAR struct mod_loadinfo_s 
*loadinfo,
 }
 #endif
 
+/****************************************************************************
+ * Name: libelf_addsize
+ *
+ * Description:
+ *   Add a section of the given alignment and size to a running size.
+ *   The ELF specification allows only 0 and powers of two as alignment.
+ *   Fail with -ENOEXEC for any other alignment, and when a size is more
+ *   than half of the address space: no image can be that large, and the
+ *   later round-ups (to the alignment, to pages) then cannot wrap.
+ *
+ ****************************************************************************/
+
+static int libelf_addsize(FAR size_t *total, uintmax_t align,
+                          uintmax_t size)
+{
+  size_t start;
+  size_t end;
+
+  if ((align & (align - 1)) != 0 || align > SIZE_MAX / 2 ||
+      size > SIZE_MAX / 2)
+    {
+      return -ENOEXEC;
+    }
+
+  start = _ALIGN_UP(*total, (size_t)align);

Review Comment:
   Done.
   



##########
libs/libc/elf/elf_load.c:
##########
@@ -145,15 +145,54 @@ static int libelf_section_alloc(FAR struct mod_loadinfo_s 
*loadinfo,
 }
 #endif
 
+/****************************************************************************
+ * Name: libelf_addsize
+ *
+ * Description:
+ *   Add a section of the given alignment and size to a running size.
+ *   The ELF specification allows only 0 and powers of two as alignment.
+ *   Fail with -ENOEXEC for any other alignment, and when a size is more
+ *   than half of the address space: no image can be that large, and the
+ *   later round-ups (to the alignment, to pages) then cannot wrap.
+ *
+ ****************************************************************************/
+
+static int libelf_addsize(FAR size_t *total, uintmax_t align,
+                          uintmax_t size)
+{
+  size_t start;
+  size_t end;
+
+  if ((align & (align - 1)) != 0 || align > SIZE_MAX / 2 ||
+      size > SIZE_MAX / 2)
+    {
+      return -ENOEXEC;
+    }
+
+  start = _ALIGN_UP(*total, (size_t)align);
+  end   = start + ELF_ALIGNUP(size);
+  if (start < *total || end < start || end > SIZE_MAX / 2)
+    {
+      return -ENOEXEC;
+    }
+
+  *total = end;
+  return OK;
+}
+
 /****************************************************************************
  * Name: libelf_elfsize
  *
  * Description:
  *   Calculate total memory allocation for the ELF file.
  *
+ * Returned Value:
+ *   0 (OK) is returned on success; -ENOEXEC when a section has an invalid
+ *   alignment or the sizes are more than half of the address space.
+ *
  ****************************************************************************/
 
-static void libelf_elfsize(FAR struct mod_loadinfo_s *loadinfo, bool alloc)
+static int libelf_elfsize(FAR struct mod_loadinfo_s *loadinfo, bool alloc)

Review Comment:
   Done.
   



##########
libs/libc/elf/elf_load.c:
##########
@@ -145,15 +145,54 @@ static int libelf_section_alloc(FAR struct mod_loadinfo_s 
*loadinfo,
 }
 #endif
 
+/****************************************************************************
+ * Name: libelf_addsize
+ *
+ * Description:
+ *   Add a section of the given alignment and size to a running size.
+ *   The ELF specification allows only 0 and powers of two as alignment.
+ *   Fail with -ENOEXEC for any other alignment, and when a size is more
+ *   than half of the address space: no image can be that large, and the
+ *   later round-ups (to the alignment, to pages) then cannot wrap.
+ *
+ ****************************************************************************/
+
+static int libelf_addsize(FAR size_t *total, uintmax_t align,

Review Comment:
   Done.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to