diff mbox series

[v3,3/3] hw/core/loader: clear uninitialized ROM space

Message ID 20210429141326.69245-4-laurent@vivier.eu
State New
Headers show
Series hw/elf_ops: clear uninitialized segment space | expand

Commit Message

Laurent Vivier April 29, 2021, 2:13 p.m. UTC
As for "hw/elf_ops: clear uninitialized segment space" we need to
clear the uninitialized space when the ELF is set in ROM.

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 hw/core/loader.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Philippe Mathieu-Daudé April 29, 2021, 2:52 p.m. UTC | #1
On 4/29/21 4:13 PM, Laurent Vivier wrote:
> As for "hw/elf_ops: clear uninitialized segment space" we need to
> clear the uninitialized space when the ELF is set in ROM.
> 
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
>  hw/core/loader.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/hw/core/loader.c b/hw/core/loader.c
> index d3e5f3b423f6..8146fdcbb7a0 100644
> --- a/hw/core/loader.c
> +++ b/hw/core/loader.c
> @@ -1146,9 +1146,13 @@ static void rom_reset(void *unused)
>          if (rom->mr) {
>              void *host = memory_region_get_ram_ptr(rom->mr);
>              memcpy(host, rom->data, rom->datasize);
> +            memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
>          } else {
>              address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
>                                      rom->data, rom->datasize);
> +            address_space_set(rom->as, rom->addr + rom->datasize, 0,
> +                              rom->romsize - rom->datasize,
> +                              MEMTXATTRS_UNSPECIFIED);
>          }
>          if (rom->isrom) {
>              /* rom needs to be written only once */
> 

This is consistent with the comment from commit d60fa42e8ba
("Save memory allocation in the elf loader"):

    /* datasize is the amount of memory allocated in "data". If datasize
is less
     * than romsize, it means that the area from datasize to romsize is
filled
     * with zeros.
     */

Therefore:

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

However depending on the underlying media, there might be cases
where we want to fill with -1 instead. Just to keep in mind, if
one day it bites us.

Regards,

Phil.
Stefano Garzarella April 30, 2021, 7:03 a.m. UTC | #2
On Thu, Apr 29, 2021 at 04:13:26PM +0200, Laurent Vivier wrote:
>As for "hw/elf_ops: clear uninitialized segment space" we need to
>clear the uninitialized space when the ELF is set in ROM.
>
>Signed-off-by: Laurent Vivier <laurent@vivier.eu>

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
diff mbox series

Patch

diff --git a/hw/core/loader.c b/hw/core/loader.c
index d3e5f3b423f6..8146fdcbb7a0 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -1146,9 +1146,13 @@  static void rom_reset(void *unused)
         if (rom->mr) {
             void *host = memory_region_get_ram_ptr(rom->mr);
             memcpy(host, rom->data, rom->datasize);
+            memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
         } else {
             address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
                                     rom->data, rom->datasize);
+            address_space_set(rom->as, rom->addr + rom->datasize, 0,
+                              rom->romsize - rom->datasize,
+                              MEMTXATTRS_UNSPECIFIED);
         }
         if (rom->isrom) {
             /* rom needs to be written only once */