diff mbox series

[U-Boot] dlmalloc: ensure gd is set for early free

Message ID 20180523190710.30260-1-erosca@de.adit-jv.com
State Deferred
Delegated to: Tom Rini
Headers show
Series [U-Boot] dlmalloc: ensure gd is set for early free | expand

Commit Message

Eugeniu Rosca May 23, 2018, 7:07 p.m. UTC
This fix seems to be a twin of v2015.01 commit 854d2b9753e4 ("dlmalloc:
ensure gd is set for early alloc"). Here is a gdb backtrace to make them
look even more similar (sandbox build):

(gdb) run
Starting program: /path/to/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00000000004123c0 in free (mem=0x0) at common/dlmalloc.c:2460
2460	  if (mem == NULL)                              /* free(0) has no effect */
(gdb) where
 #0  0x00000000004123c0 in free (mem=0x0) at common/dlmalloc.c:2460
 #1  0x00007ffff3f46cea in ?? () from /lib/x86_64-linux-gnu/libselinux.so.1
 #2  0x00007ffff7de76ba in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffd928, env=env@entry=0x7fffffffd938) at dl-init.c:72
 #3  0x00007ffff7de77cb in call_init (env=0x7fffffffd938, argv=0x7fffffffd928, argc=1, l=<optimized out>) at dl-init.c:30
 #4  _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffd928, env=0x7fffffffd938) at dl-init.c:120
 #5  0x00007ffff7dd7c6a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
 #6  0x0000000000000001 in ?? ()
 #7  0x00007fffffffddbb in ?? ()
 #8  0x0000000000000000 in ?? ()

Interestingly, this issue appears on a very old v2015.04 U-boot, but
not on u-boot/master (even if the fix applies cleanly to u-boot/master).
With the patch applied, my ancient u-boot starts to work properly:

$ ./u-boot

U-Boot 2015.04-00280-g5755c9e48b83 (May 23 2018 - 20:53:31)

DRAM:  128 MiB
Using default environment

In:    serial
Out:   lcd
Err:   lcd
=>

Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
---
 common/dlmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Stephen Warren May 23, 2018, 7:32 p.m. UTC | #1
On 05/23/2018 01:07 PM, Eugeniu Rosca wrote:
> This fix seems to be a twin of v2015.01 commit 854d2b9753e4 ("dlmalloc:
> ensure gd is set for early alloc"). Here is a gdb backtrace to make them
> look even more similar (sandbox build):
...
> Interestingly, this issue appears on a very old v2015.04 U-boot, but
> not on u-boot/master (even if the fix applies cleanly to u-boot/master).
> With the patch applied, my ancient u-boot starts to work properly:

That's probably because of 2f0bcd4de1ab0cb03f01428a334cd91f8870504c 
"malloc: use hidden visibility" which prevents code outside of U-Boot 
from using U-Boot's malloc/free?

(I only remember this because I very recently bisected an issue in an 
old branch that caused sandbox crashes after upgrading the OS on a test 
machine and triggering the bug that commit fixes.)
Eugeniu Rosca May 23, 2018, 8:28 p.m. UTC | #2
Hi Stephen,

On Wed, May 23, 2018 at 01:32:53PM -0600, Stephen Warren wrote:
> On 05/23/2018 01:07 PM, Eugeniu Rosca wrote:
> >This fix seems to be a twin of v2015.01 commit 854d2b9753e4 ("dlmalloc:
> >ensure gd is set for early alloc"). Here is a gdb backtrace to make them
> >look even more similar (sandbox build):
> ...
> >Interestingly, this issue appears on a very old v2015.04 U-boot, but
> >not on u-boot/master (even if the fix applies cleanly to u-boot/master).
> >With the patch applied, my ancient u-boot starts to work properly:
> 
> That's probably because of 2f0bcd4de1ab0cb03f01428a334cd91f8870504c "malloc:
> use hidden visibility" which prevents code outside of U-Boot from using
> U-Boot's malloc/free?

This fixes my issue in apparently much cleaner way. Thanks! The break
down of sandbox into phases is very informative. Like the comments/code
ratio.

> (I only remember this because I very recently bisected an issue in an old
> branch that caused sandbox crashes after upgrading the OS on a test machine
> and triggering the bug that commit fixes.)

Thanks again!

Best regards,
Eugeniu.
diff mbox series

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index b395eefbf862..6012f9f162c0 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -1524,7 +1524,7 @@  void fREe(mem) Void_t* mem;
 
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* free() is a no-op - all the memory will be freed on relocation */
-	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
+	if (gd && !(gd->flags & GD_FLG_FULL_MALLOC_INIT))
 		return;
 #endif