eal: fix cleanup on Windows

Message ID 20241108130822.4073057-1-thomas@monjalon.net (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series eal: fix cleanup on Windows |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS

Commit Message

Thomas Monjalon Nov. 8, 2024, 1:08 p.m. UTC
The memory allocated with _aligned_malloc()
must be released with _aligned_free() on Windows.

The POSIX free() was called in eal_lcore_var_cleanup(),
called in rte_eal_cleanup(), and triggered a heap corruption:
exit status 3221226356 or signal 3221226228 SIGinvalid
with MALLOC_PERTURB_=86

Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")

Reported-by: David Marchand <david.marchand@redhat.com>
Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 lib/eal/common/eal_common_lcore_var.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Comments

David Marchand Nov. 8, 2024, 1:11 p.m. UTC | #1
On Fri, Nov 8, 2024 at 2:08 PM Thomas Monjalon <thomas@monjalon.net> wrote:
>
> The memory allocated with _aligned_malloc()
> must be released with _aligned_free() on Windows.
>
> The POSIX free() was called in eal_lcore_var_cleanup(),
> called in rte_eal_cleanup(), and triggered a heap corruption:
> exit status 3221226356 or signal 3221226228 SIGinvalid
> with MALLOC_PERTURB_=86
>
> Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
>
> Reported-by: David Marchand <david.marchand@redhat.com>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>

Reviewed-by: David Marchand <david.marchand@redhat.com>
  
Morten Brørup Nov. 8, 2024, 1:33 p.m. UTC | #2
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
  
Dmitry Kozlyuk Nov. 8, 2024, 1:39 p.m. UTC | #3
2024-11-08 14:08 (UTC+0100), Thomas Monjalon:
> The memory allocated with _aligned_malloc()
> must be released with _aligned_free() on Windows.
> 
> The POSIX free() was called in eal_lcore_var_cleanup(),
> called in rte_eal_cleanup(), and triggered a heap corruption:
> exit status 3221226356 or signal 3221226228 SIGinvalid
> with MALLOC_PERTURB_=86
> 
> Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
> 
> Reported-by: David Marchand <david.marchand@redhat.com>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>

Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
  
Mattias Rönnblom Nov. 8, 2024, 3:43 p.m. UTC | #4
On 2024-11-08 14:08, Thomas Monjalon wrote:
> The memory allocated with _aligned_malloc()
> must be released with _aligned_free() on Windows.
> 
> The POSIX free() was called in eal_lcore_var_cleanup(),

Referring to free() as a part of POSIX is true, but a bit misleading, 
since it's also standard C.

> called in rte_eal_cleanup(), and triggered a heap corruption:
> exit status 3221226356 or signal 3221226228 SIGinvalid
> with MALLOC_PERTURB_=86
> 
> Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
> 
> Reported-by: David Marchand <david.marchand@redhat.com>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> ---
>   lib/eal/common/eal_common_lcore_var.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/eal/common/eal_common_lcore_var.c b/lib/eal/common/eal_common_lcore_var.c
> index 0e9e8e4804..a1b2458839 100644
> --- a/lib/eal/common/eal_common_lcore_var.c
> +++ b/lib/eal/common/eal_common_lcore_var.c
> @@ -54,7 +54,6 @@ lcore_var_alloc(size_t size, size_t align)
>   		current_buffer = _aligned_malloc(alloc_size, RTE_CACHE_LINE_SIZE);
>   #else
>   		current_buffer = aligned_alloc(RTE_CACHE_LINE_SIZE, alloc_size);
> -
>   #endif
>   		RTE_VERIFY(current_buffer != NULL);
>   
> @@ -108,7 +107,11 @@ eal_lcore_var_cleanup(void)
>   	while (current_buffer != NULL) {
>   		struct lcore_var_buffer *prev = current_buffer->prev;
>   
> +#ifdef RTE_EXEC_ENV_WINDOWS
> +		_aligned_free(current_buffer);
> +#else
>   		free(current_buffer);
> +#endif
>   
>   		current_buffer = prev;
>   	}

Reviewed-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
  
Stephen Hemminger Nov. 8, 2024, 4 p.m. UTC | #5
On Fri,  8 Nov 2024 14:08:21 +0100
Thomas Monjalon <thomas@monjalon.net> wrote:

> The memory allocated with _aligned_malloc()
> must be released with _aligned_free() on Windows.
> 
> The POSIX free() was called in eal_lcore_var_cleanup(),
> called in rte_eal_cleanup(), and triggered a heap corruption:
> exit status 3221226356 or signal 3221226228 SIGinvalid
> with MALLOC_PERTURB_=86
> 
> Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
> 
> Reported-by: David Marchand <david.marchand@redhat.com>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>

Is there anyway to use the function attributes to catch this at compile
time (for Gcc builds).
  
Morten Brørup Nov. 8, 2024, 5:43 p.m. UTC | #6
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Friday, 8 November 2024 17.01
> 
> On Fri,  8 Nov 2024 14:08:21 +0100
> Thomas Monjalon <thomas@monjalon.net> wrote:
> 
> > The memory allocated with _aligned_malloc()
> > must be released with _aligned_free() on Windows.
> >
> > The POSIX free() was called in eal_lcore_var_cleanup(),
> > called in rte_eal_cleanup(), and triggered a heap corruption:
> > exit status 3221226356 or signal 3221226228 SIGinvalid
> > with MALLOC_PERTURB_=86
> >
> > Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation
> facility")
> >
> > Reported-by: David Marchand <david.marchand@redhat.com>
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> 
> Is there anyway to use the function attributes to catch this at compile
> time (for Gcc builds).

I took a look at it, and it boils down to building for a Windows environment, where RTE_EXEC_ENV_WINDOWS is defined, and then the alloc function attributes are disabled.
  
Thomas Monjalon Nov. 8, 2024, 9:57 p.m. UTC | #7
08/11/2024 16:43, Mattias Rönnblom:
> On 2024-11-08 14:08, Thomas Monjalon wrote:
> > The memory allocated with _aligned_malloc()
> > must be released with _aligned_free() on Windows.
> > 
> > The POSIX free() was called in eal_lcore_var_cleanup(),
> 
> Referring to free() as a part of POSIX is true, but a bit misleading, 
> since it's also standard C.
> 
> > called in rte_eal_cleanup(), and triggered a heap corruption:
> > exit status 3221226356 or signal 3221226228 SIGinvalid
> > with MALLOC_PERTURB_=86
> > 
> > Fixes: 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
> > 
> > Reported-by: David Marchand <david.marchand@redhat.com>
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> 
> Reviewed-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>

Applied
  

Patch

diff --git a/lib/eal/common/eal_common_lcore_var.c b/lib/eal/common/eal_common_lcore_var.c
index 0e9e8e4804..a1b2458839 100644
--- a/lib/eal/common/eal_common_lcore_var.c
+++ b/lib/eal/common/eal_common_lcore_var.c
@@ -54,7 +54,6 @@  lcore_var_alloc(size_t size, size_t align)
 		current_buffer = _aligned_malloc(alloc_size, RTE_CACHE_LINE_SIZE);
 #else
 		current_buffer = aligned_alloc(RTE_CACHE_LINE_SIZE, alloc_size);
-
 #endif
 		RTE_VERIFY(current_buffer != NULL);
 
@@ -108,7 +107,11 @@  eal_lcore_var_cleanup(void)
 	while (current_buffer != NULL) {
 		struct lcore_var_buffer *prev = current_buffer->prev;
 
+#ifdef RTE_EXEC_ENV_WINDOWS
+		_aligned_free(current_buffer);
+#else
 		free(current_buffer);
+#endif
 
 		current_buffer = prev;
 	}