Skip to contentSkip to conversation

spl: fix linker size check off-by-one errors

PATCH New Simon Goldschmidt 3 participants 15 messages Apr 25, 2019 – May 6, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/2 spl: fix linker size check off-by-one errors

This fixes SPL linker script size checks for 3 lds files where the size checks were implemented as "x < YYY_MAX_SIZE".

Fix the size checks to be "x <= YYY_MAX_SIZE" instead.

---

Changes in v5:
- added patch to fix linker size checks off-by-one error

 arch/arm/cpu/u-boot-spl.lds                 | 6 +++---
 arch/arm/mach-at91/arm926ejs/u-boot-spl.lds | 4 ++--
 arch/microblaze/cpu/u-boot-spl.lds          | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
arch/arm/cpu/u-boot-spl.lds +3 -3
index a2aa93a735..97899a567f 100644
@@ -79,16 +79,16 @@ SECTIONS
 }
 
 #if defined(IMAGE_MAX_SIZE)
-ASSERT(__image_copy_end - __image_copy_start < (IMAGE_MAX_SIZE), \
+ASSERT(__image_copy_end - __image_copy_start <= (IMAGE_MAX_SIZE), \
 	"SPL image too big");
 #endif
 
 #if defined(CONFIG_SPL_BSS_MAX_SIZE)
-ASSERT(__bss_end - __bss_start < (CONFIG_SPL_BSS_MAX_SIZE), \
+ASSERT(__bss_end - __bss_start <= (CONFIG_SPL_BSS_MAX_SIZE), \
 	"SPL image BSS too big");
 #endif
 
 #if defined(CONFIG_SPL_MAX_FOOTPRINT)
-ASSERT(__bss_end - _start < (CONFIG_SPL_MAX_FOOTPRINT), \
+ASSERT(__bss_end - _start <= (CONFIG_SPL_MAX_FOOTPRINT), \
 	"SPL image plus BSS too big");
 #endif
arch/arm/mach-at91/arm926ejs/u-boot-spl.lds +2 -2
index 3955bea23a..74f6355229 100644
@@ -52,11 +52,11 @@ SECTIONS
 }
 
 #if defined(IMAGE_MAX_SIZE)
-ASSERT(__image_copy_end - __start < (IMAGE_MAX_SIZE), \
+ASSERT(__image_copy_end - __start <= (IMAGE_MAX_SIZE), \
 	"SPL image too big");
 #endif
 
 #if defined(CONFIG_SPL_BSS_MAX_SIZE)
-ASSERT(__bss_end - __bss_start < (CONFIG_SPL_BSS_MAX_SIZE), \
+ASSERT(__bss_end - __bss_start <= (CONFIG_SPL_BSS_MAX_SIZE), \
 	"SPL image BSS too big");
 #endif
arch/microblaze/cpu/u-boot-spl.lds +1 -1
index 99c62b51a1..3387eb7189 100644
@@ -57,6 +57,6 @@ SECTIONS
 }
 
 #if defined(CONFIG_SPL_MAX_FOOTPRINT)
-ASSERT(__end - _start < (CONFIG_SPL_MAX_FOOTPRINT), \
+ASSERT(__end - _start <= (CONFIG_SPL_MAX_FOOTPRINT), \
         "SPL image plus BSS too big");
 #endif
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/2 spl: fix linker size check off-by-one errors

Quoted 4 lines · Simon Goldschmidt

This fixes SPL linker script size checks for 3 lds files where the size checks were implemented as "x < YYY_MAX_SIZE".

Fix the size checks to be "x <= YYY_MAX_SIZE" instead.

After updating Ubuntu to 19.04 today, the new 8.3 gcc makes smartweb_defconfig fail compiling SPL without this because __image_copy_end==0x1000

I guess we're lucky travis uses gcc 7.3... (or, are we?)

Regards,
Simon

Quoted 67 lines

---

Changes in v5:
- added patch to fix linker size checks off-by-one error

  arch/arm/cpu/u-boot-spl.lds                 | 6 +++---
  arch/arm/mach-at91/arm926ejs/u-boot-spl.lds | 4 ++--
  arch/microblaze/cpu/u-boot-spl.lds          | 2 +-
  3 files changed, 6 insertions(+), 6 deletions(-)
arch/arm/cpu/u-boot-spl.lds +3 -3
index a2aa93a735..97899a567f 100644
@@ -79,16 +79,16 @@ SECTIONS
  }
 
  #if defined(IMAGE_MAX_SIZE)
-ASSERT(__image_copy_end - __image_copy_start < (IMAGE_MAX_SIZE), \
+ASSERT(__image_copy_end - __image_copy_start <= (IMAGE_MAX_SIZE), \
  	"SPL image too big");
  #endif
 
  #if defined(CONFIG_SPL_BSS_MAX_SIZE)
-ASSERT(__bss_end - __bss_start < (CONFIG_SPL_BSS_MAX_SIZE), \
+ASSERT(__bss_end - __bss_start <= (CONFIG_SPL_BSS_MAX_SIZE), \
  	"SPL image BSS too big");
  #endif
 
  #if defined(CONFIG_SPL_MAX_FOOTPRINT)
-ASSERT(__bss_end - _start < (CONFIG_SPL_MAX_FOOTPRINT), \
+ASSERT(__bss_end - _start <= (CONFIG_SPL_MAX_FOOTPRINT), \
  	"SPL image plus BSS too big");
  #endif
arch/arm/mach-at91/arm926ejs/u-boot-spl.lds +2 -2
index 3955bea23a..74f6355229 100644
@@ -52,11 +52,11 @@ SECTIONS
  }
 
  #if defined(IMAGE_MAX_SIZE)
-ASSERT(__image_copy_end - __start < (IMAGE_MAX_SIZE), \
+ASSERT(__image_copy_end - __start <= (IMAGE_MAX_SIZE), \
  	"SPL image too big");
  #endif
 
  #if defined(CONFIG_SPL_BSS_MAX_SIZE)
-ASSERT(__bss_end - __bss_start < (CONFIG_SPL_BSS_MAX_SIZE), \
+ASSERT(__bss_end - __bss_start <= (CONFIG_SPL_BSS_MAX_SIZE), \
  	"SPL image BSS too big");
  #endif
arch/microblaze/cpu/u-boot-spl.lds +1 -1
index 99c62b51a1..3387eb7189 100644
@@ -57,6 +57,6 @@ SECTIONS
  }
 
  #if defined(CONFIG_SPL_MAX_FOOTPRINT)
-ASSERT(__end - _start < (CONFIG_SPL_MAX_FOOTPRINT), \
+ASSERT(__end - _start <= (CONFIG_SPL_MAX_FOOTPRINT), \
          "SPL image plus BSS too big");
  #endif
#2 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

In reply to Simon Goldschmidt

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +5 -1
index 6f12a18d54..38859ecbd4 100644
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
 	mem_malloc_start = start;
-	mem_malloc_end = start + size;
 	mem_malloc_brk = start;
+	mem_malloc_end = start + size;
+	if (size > mem_malloc_end) {
+		/* overflow: malloc area is at end of address range */
+		mem_malloc_end--;
+	}
 
 	debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
 	      mem_malloc_end);
Signature · 1 line
2.17.1
#3 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 23 lines · Simon Goldschmidt

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -0
index 6f12a18d54..38859ecbd4 100644

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 9 lines
common/dlmalloc.c +4 -1
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
 	mem_malloc_start = start;
-	mem_malloc_end = start + size;
 	mem_malloc_brk = start;
+	mem_malloc_end = start + size;
+	if (size > mem_malloc_end) {
+		/* overflow: malloc area is at end of address range */
+		mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 5 lines
common/dlmalloc.c +1 -0
+	}
 
 	debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
 	      mem_malloc_end);
Signature · 2 lines
Best regards,
Marek Vasut
#4 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 29 lines · Simon Goldschmidt
Quoted 28 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -1
index 6f12a18d54..38859ecbd4 100644
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
Quoted 9 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 4 lines
common/dlmalloc.c +4 -0
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

#5 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 41 lines · Simon Goldschmidt

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 29 lines · Simon Goldschmidt
Quoted 28 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -1
index 6f12a18d54..38859ecbd4 100644
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
Quoted 9 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 4 lines
common/dlmalloc.c +4 -0
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 5 lines

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Quoted 20 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

Signature · 2 lines
Best regards,
Marek Vasut
#6 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 43 lines · Marek Vasut
Quoted 41 lines · Simon Goldschmidt

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 38 lines · Simon Goldschmidt
Quoted 23 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -0
index 6f12a18d54..38859ecbd4 100644

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 9 lines
common/dlmalloc.c +4 -1
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 8 lines
Quoted 5 lines

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Yes it fails. But in contrast, that's an invalid configuration, while my patch makes
a valid configuration work. I don't know if we want to fix all invalid configurations.
You could as well enter a range without RAM, that would fail as well.

A different approach to fix my valid end-of-ram configuration would be to set the end to "start + size - 1" and to change all the checks using it. But that would probably lead to more code size problems in various SPL...

Regards,
Simon

Quoted 26 lines
Quoted 21 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

#7 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 50 lines · Simon Goldschmidt
Quoted 49 lines · Marek Vasut
Quoted 46 lines · Simon Goldschmidt

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 29 lines · Simon Goldschmidt
Quoted 28 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -1
index 6f12a18d54..38859ecbd4 100644
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
Quoted 9 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 4 lines
common/dlmalloc.c +4 -0
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

Quoted 7 lines
Quoted 2 lines

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Yes it fails. But in contrast, that's an invalid configuration, while my patch makes
a valid configuration work. I don't know if we want to fix all invalid configurations.

Yes ? Should be easy, just clamp() size to (size, (BIT(32) - 1) - mem_malloc_start) or similar for 64bit systems.

Quoted 1 line

You could as well enter a range without RAM, that would fail as well.

That info is available in gd , but I wonder whether this is the right place to check for it.

Quoted 33 lines

A different approach to fix my valid end-of-ram configuration would be to set the end to "start + size - 1" and to change all the checks using it. But that would probably lead to more code size problems in various SPL...

Regards,
Simon

Quoted 26 lines
Quoted 21 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

Signature · 2 lines
Best regards,
Marek Vasut
#8 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 55 lines · Marek Vasut
Quoted 53 lines · Simon Goldschmidt
Quoted 51 lines · Marek Vasut
Quoted 41 lines · Simon Goldschmidt

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 29 lines · Simon Goldschmidt
Quoted 28 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -1
index 6f12a18d54..38859ecbd4 100644
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
Quoted 9 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 4 lines
common/dlmalloc.c +4 -0
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 5 lines

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Quoted 7 lines
Quoted 4 lines

Yes it fails. But in contrast, that's an invalid configuration, while my patch makes
a valid configuration work. I don't know if we want to fix all invalid configurations.

Yes ? Should be easy, just clamp() size to (size, (BIT(32) - 1) - mem_malloc_start) or similar for 64bit systems.

I'm not convinced we should. This range is normally generated using something like:
SIZE=2048
START=RAM_END - SIZE

I don't want to be overprotective here. I don't think there's much point in fixing the out-of-ram-range check if it produces an overflow but not fix it if it's in the middle of an address space.

Again, this patch simply fixes the case for something like this:
RAM_SIZE=0x10000
RAM_START=0xFFFF0000
so RAM_END=0

We can use clamp as you suggested, but what would it be good for if it only fixes an out-of-range heap if an overflow occurs?

Quoted 5 lines
Quoted 2 lines

You could as well enter a range without RAM, that would fail as well.

That info is available in gd , but I wonder whether this is the right place to check for it.

Indeed, that would seem misplaced here.

Regards,
Simon

Quoted 39 lines
Quoted 34 lines

A different approach to fix my valid end-of-ram configuration would be to set the end to "start + size - 1" and to change all the checks using it. But that would probably lead to more code size problems in various SPL...

Regards,
Simon

Quoted 26 lines
Quoted 21 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

#9 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 61 lines · Simon Goldschmidt
Quoted 60 lines · Marek Vasut
Quoted 57 lines · Simon Goldschmidt
Quoted 43 lines · Marek Vasut
Quoted 41 lines · Simon Goldschmidt

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 38 lines · Simon Goldschmidt
Quoted 23 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

 common/dlmalloc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -0
index 6f12a18d54..38859ecbd4 100644

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 9 lines
common/dlmalloc.c +4 -1
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
 void mem_malloc_init(ulong start, ulong size)
 {
      mem_malloc_start = start;
-     mem_malloc_end = start + size;
      mem_malloc_brk = start;
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 8 lines
Quoted 5 lines

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Yes it fails. But in contrast, that's an invalid configuration, while my patch makes
a valid configuration work. I don't know if we want to fix all invalid configurations.

Quoted 7 lines
Quoted 2 lines

Yes ? Should be easy, just clamp() size to (size, (BIT(32) - 1) - mem_malloc_start) or similar for 64bit systems.

I'm not convinced we should. This range is normally generated using something like:
SIZE=2048
START=RAM_END - SIZE

Normally ... on SoCFPGA . Other ARM32 platforms can have OCRAM mapped somewhere in the middle of the address space. Take R-Car Gen2, which has it at 0xe6300000 + 64k or something like that.

And, to make things worse, you cannot detect these overflows at compile time, since the DRAM layout, which is passed to malloc init can come from DT.

Thus, you might want to sanitize the input, properly.

Quoted 11 lines

I don't want to be overprotective here. I don't think there's much point in fixing the out-of-ram-range check if it produces an overflow but not fix it if it's in the middle of an address space.

Again, this patch simply fixes the case for something like this:
RAM_SIZE=0x10000
RAM_START=0xFFFF0000
so RAM_END=0

We can use clamp as you suggested, but what would it be good for if it only fixes an out-of-range heap if an overflow occurs?

It's better than nothing. Further refinements welcome.

Quoted 49 lines
Quoted 4 lines
Quoted 1 line

You could as well enter a range without RAM, that would fail as well.

That info is available in gd , but I wonder whether this is the right place to check for it.

Indeed, that would seem misplaced here.

Regards,
Simon

Quoted 39 lines
Quoted 34 lines

A different approach to fix my valid end-of-ram configuration would be to set the end to "start + size - 1" and to change all the checks using it. But that would probably lead to more code size problems in various SPL...

Regards,
Simon

Quoted 26 lines
Quoted 21 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
      debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
            mem_malloc_end);

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

Signature · 2 lines
Best regards,
Marek Vasut
#10 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Tom,

Am 26.04.2019 um 13:00 schrieb Marek Vasut:

Quoted 9 lines · Simon Goldschmidt
Quoted 8 lines · Marek Vasut
Quoted 7 lines · Simon Goldschmidt
Quoted 5 lines · Marek Vasut
Quoted 4 lines · Simon Goldschmidt
Quoted 2 lines

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Simon Goldschmidt
Quoted 7 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

Since there's no way this fits without breaking smartweb, I'd rather drop this for now in order to get 1/2 accepted.

Regards,
Simon

Quoted 128 lines
Quoted 45 lines
Quoted 45 lines
Quoted 44 lines
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 16 lines

---

Changes in v5:
- this patch was 1/2 in v4 but is now 2/2 as the 2nd patch of v4 has already been accepted
- rearrange the code to make it only 8 bytes plus in code size for arm (which fixes smartweb SPL overflowing)

  common/dlmalloc.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
common/dlmalloc.c +0 -0
index 6f12a18d54..38859ecbd4 100644

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 608

Quoted 9 lines
common/dlmalloc.c +4 -1
@@ -601,8 +601,12 @@ void *sbrk(ptrdiff_t increment)
  void mem_malloc_init(ulong start, ulong size)
  {
       mem_malloc_start = start;
-     mem_malloc_end = start + size;
       mem_malloc_brk = start;
+     mem_malloc_end = start + size;
+     if (size > mem_malloc_end) {
+             /* overflow: malloc area is at end of address range */
+             mem_malloc_end--;

Does this mean a memory wrap-around happened ?
I don't think decrementing malloc area size by 1 is a proper solution. You can have it overflow by 2 and decrementing by 1 won't help.

Quoted 8 lines
Quoted 5 lines

No, not a real overflow. Instead, as I tried to described in the commit message, mem_malloc_end gets 0 if the range is at the end of addr range, e.g. malloc start is 0xffff0000 and malloc size is 0x10000. Subtracting 1 will be enough here. It reduces the available mall of aize, but I don't think that should be a problem.

That's a wrap-around . What happens with your example if malloc_size is 0x10001 ? Hint: It fails ...

Yes it fails. But in contrast, that's an invalid configuration, while my patch makes
a valid configuration work. I don't know if we want to fix all invalid configurations.

Quoted 7 lines
Quoted 2 lines

Yes ? Should be easy, just clamp() size to (size, (BIT(32) - 1) - mem_malloc_start) or similar for 64bit systems.

I'm not convinced we should. This range is normally generated using something like:
SIZE=2048
START=RAM_END - SIZE

Normally ... on SoCFPGA . Other ARM32 platforms can have OCRAM mapped somewhere in the middle of the address space. Take R-Car Gen2, which has it at 0xe6300000 + 64k or something like that.

And, to make things worse, you cannot detect these overflows at compile time, since the DRAM layout, which is passed to malloc init can come from DT.

Thus, you might want to sanitize the input, properly.

Quoted 11 lines

I don't want to be overprotective here. I don't think there's much point in fixing the out-of-ram-range check if it produces an overflow but not fix it if it's in the middle of an address space.

Again, this patch simply fixes the case for something like this:
RAM_SIZE=0x10000
RAM_START=0xFFFF0000
so RAM_END=0

We can use clamp as you suggested, but what would it be good for if it only fixes an out-of-range heap if an overflow occurs?

It's better than nothing. Further refinements welcome.

Quoted 49 lines
Quoted 4 lines
Quoted 1 line

You could as well enter a range without RAM, that would fail as well.

That info is available in gd , but I wonder whether this is the right place to check for it.

Indeed, that would seem misplaced here.

Regards,
Simon

Quoted 39 lines
Quoted 34 lines

A different approach to fix my valid end-of-ram configuration would be to set the end to "start + size - 1" and to change all the checks using it. But that would probably lead to more code size problems in various SPL...

Regards,
Simon

Quoted 26 lines
Quoted 21 lines

I got this when experimenting with full heap in socfpga. Due to other patches not being accepted, this is not an issue currebtly, but can easily become one on the future.

Regrds,
Simon

Quoted 11 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 612

Quoted 5 lines
common/dlmalloc.c +1 -0
+     }
 
       debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
             mem_malloc_end);

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

--
Best regards,
Marek Vasut

#11 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 17 lines · Simon Goldschmidt

Tom,

Am 26.04.2019 um 13:00 schrieb Marek Vasut:

Quoted 14 lines · Simon Goldschmidt
Quoted 13 lines · Marek Vasut
Quoted 12 lines · Simon Goldschmidt
Quoted 10 lines · Marek Vasut
Quoted 9 lines · Simon Goldschmidt
Quoted 7 lines

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 5 lines · Simon Goldschmidt
Quoted 4 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Fix this by subtracting 1 of 'start + size' overflows to zero.

Since there's no way this fits without breaking smartweb, I'd rather drop this for now in order to get 1/2 accepted.

I thought that with 1/2 this fit again, with gcc-7.3 at least? Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190505/6d10c4d9/attachment.sig>
#12 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Am 05.05.2019 um 13:38 schrieb Tom Rini:

Quoted 18 lines · Simon Goldschmidt
Quoted 17 lines

Tom,

Am 26.04.2019 um 13:00 schrieb Marek Vasut:

Quoted 14 lines · Simon Goldschmidt
Quoted 13 lines · Marek Vasut
Quoted 12 lines · Simon Goldschmidt
Quoted 10 lines · Marek Vasut
Quoted 9 lines · Simon Goldschmidt
Quoted 7 lines

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 5 lines · Simon Goldschmidt
Quoted 4 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Fix this by subtracting 1 of 'start + size' overflows to zero.

Since there's no way this fits without breaking smartweb, I'd rather drop this for now in order to get 1/2 accepted.

I thought that with 1/2 this fit again, with gcc-7.3 at least? Thanks!

I'm not sure, as I don't have it here to test. But as this patch doesn't actually fix a board but fixes an issue in the code that *might* appear in the future, I'm not convinced it would be the right thing to merge it like it is.

And I'm also a little short on time to investigate this further, as it's not a real bug, currently.

Regards,
Simon

#13 lore raw

PATCH v5 2/2 dlmalloc: fix malloc range at end of ram

Quoted 28 lines · Simon Goldschmidt

Am 05.05.2019 um 13:38 schrieb Tom Rini:

Quoted 18 lines · Simon Goldschmidt
Quoted 17 lines

Tom,

Am 26.04.2019 um 13:00 schrieb Marek Vasut:

Quoted 14 lines · Simon Goldschmidt
Quoted 13 lines · Marek Vasut
Quoted 12 lines · Simon Goldschmidt
Quoted 10 lines · Marek Vasut
Quoted 9 lines · Simon Goldschmidt
Quoted 7 lines

Marek Vasut <[email protected]> schrieb am Fr., 26. Apr. 2019, 00:22:

Quoted 5 lines · Simon Goldschmidt
Quoted 4 lines

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Fix this by subtracting 1 of 'start + size' overflows to zero.

Since there's no way this fits without breaking smartweb, I'd rather drop this for now in order to get 1/2 accepted.

I thought that with 1/2 this fit again, with gcc-7.3 at least? Thanks!

Quoted 7 lines

I'm not sure, as I don't have it here to test. But as this patch doesn't actually fix a board but fixes an issue in the code that *might* appear in the future, I'm not convinced it would be the right thing to merge it like it is.

And I'm also a little short on time to investigate this further, as it's not a real bug, currently.

OK, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190505/862d365b/attachment.sig>
#14 lore raw

PATCH v5 1/2 spl: fix linker size check off-by-one errors

In reply to Simon Goldschmidt
Quoted 6 lines · Simon Goldschmidt

This fixes SPL linker script size checks for 3 lds files where the size checks were implemented as "x < YYY_MAX_SIZE".

Fix the size checks to be "x <= YYY_MAX_SIZE" instead.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190506/1ec9026e/attachment.sig>