public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] memory corruption on nios2 due to overlap of gbl data and malloc
@ 2012-02-20 23:24 Alex Hornung
  2012-02-28 22:29 ` Albert ARIBAUD
  0 siblings, 1 reply; 17+ messages in thread
From: Alex Hornung @ 2012-02-20 23:24 UTC (permalink / raw)
  To: u-boot

Hi,

I've run into some memory corruption due to an error in the logic used
to allocate the bd (and gd) during board_init of the nios2.


#define CONFIG_SYS_GBL_DATA_OFFSET      (CONFIG_SYS_MALLOC_BASE - \
                                         GENERATED_GBL_DATA_SIZE)
[...]

        gd = (gd_t *)CONFIG_SYS_GBL_DATA_OFFSET;
[...]
        gd->bd = (bd_t *)(gd+1);        /* At end of global data */
[...]
        mem_malloc_init(CONFIG_SYS_MALLOC_BASE, CONFIG_SYS_MALLOC_LEN);

The relevant points here are that CONFIG_SYS_GBL_DATA_OFFSET is
GENERATED_GBL_DATA_SIZE (80) bytes below the CONFIG_SYS_MALLOC_BASE.

Given that gd is 68 bytes big, now the start of bd is only 12 bytes from
the beginning of the malloc base - but the size of bd is 36 bytes!

In other words, bd and the malloc base overlap, causing memory
corruption in some of the malloc'd memory when some bd elements are
populated. In my case in particular some content of the flash mtd
eraseregions is getting corrupted by the write to bd->bi_ip_addr after
initializing the flash stuff.

I'm not sure how this should be dealt with - I'd think the best approach
here is to change the CONFIG_SYS_GBL_DATA_OFFSET to include some space
for the bd, or malloc'ing the bd.

If you let me know which one is the preferred approach, I'll gladly
provide a patch.

Cheers,
Alex Hornung

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2012-03-02  3:22 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-20 23:24 [U-Boot] memory corruption on nios2 due to overlap of gbl data and malloc Alex Hornung
2012-02-28 22:29 ` Albert ARIBAUD
2012-02-28 22:39   ` Graeme Russ
2012-02-28 22:55     ` Albert ARIBAUD
2012-02-28 23:20       ` Graeme Russ
2012-02-28 23:24         ` Albert ARIBAUD
2012-02-28 23:32           ` Graeme Russ
2012-02-29 19:04             ` Mike Frysinger
2012-02-29 22:22               ` Graeme Russ
2012-02-29 22:29                 ` Mike Frysinger
2012-02-29 22:41                   ` Graeme Russ
2012-03-01  7:09                     ` [U-Boot] [PATCH] nios2: move gd and bd into BSS Thomas Chou
2012-03-01 17:17                       ` Mike Frysinger
2012-03-02  2:55                         ` [U-Boot] [PATCH v2] " Thomas Chou
2012-03-02  3:22                           ` Mike Frysinger
2012-03-01 21:57                     ` [U-Boot] memory corruption on nios2 due to overlap of gbl data and malloc Albert ARIBAUD
2012-03-01 22:11                       ` Graeme Russ

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox