Skip to content

Commit

Permalink
binfmt: Fix memory leak in ELF loader
Browse files Browse the repository at this point in the history
Summary:
- I noticed that the hello (ELF) application causes a memory leak.
- Finally, I found that the data section is not deallocated.
- This commit fixes this issue.

Impact:
- ELF loader with CONFIG_ARCH_ADDRENV=n

Testing:
- Tested with the following configs
  - sprensense:elf, esp32-devkitc:elf, sabre-6quad:elf
  - spresense:wifi_smp, rv-virt:nsh64, sabre-6quad:netnsh

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
  • Loading branch information
SPRESENSE committed Jul 29, 2022
1 parent c7eb96d commit 4ec6c5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions binfmt/binfmt_unloadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,15 @@ int unload_module(FAR struct binary_s *binp)
{
binfo("Freeing alloc[%d]: %p\n", i, binp->alloc[i]);
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
up_textheap_free((FAR void *)binp->alloc[i]);
#else
kumm_free((FAR void *)binp->alloc[i]);
if (i == 0)
{
up_textheap_free((FAR void *)binp->alloc[i]);
}
else
#endif
{
kumm_free((FAR void *)binp->alloc[i]);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions binfmt/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ static int elf_loadbinary(FAR struct binary_s *binp,
up_addrenv_clone(&loadinfo.addrenv, &binp->addrenv);
#else
binp->alloc[0] = (FAR void *)loadinfo.textalloc;
binp->alloc[1] = (FAR void *)loadinfo.dataalloc;
#ifdef CONFIG_BINFMT_CONSTRUCTORS
binp->alloc[1] = loadinfo.ctoralloc;
binp->alloc[2] = loadinfo.dtoralloc;
binp->alloc[2] = loadinfo.ctoralloc;
binp->alloc[3] = loadinfo.dtoralloc;
#endif
#endif

Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/binfmt/binfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Pre-processor Definitions
****************************************************************************/

#define BINFMT_NALLOC 3
#define BINFMT_NALLOC 4

/****************************************************************************
* Public Types
Expand Down

0 comments on commit 4ec6c5c

Please sign in to comment.