* [LTP] [PATCH v2 1/1] overcommit_memory: Disable optimization for malloc to prevent false positives
@ 2025-05-22 15:21 Jan Polensky
2025-05-23 6:22 ` Jan Stancek via ltp
0 siblings, 1 reply; 2+ messages in thread
From: Jan Polensky @ 2025-05-22 15:21 UTC (permalink / raw)
To: ltp
When compiling with GCC 15.1.1+, the default-enabled -fmalloc-dce=2 and
-fallocation-dce under -O2 can eliminate the entire malloc block because its
result is only compared to NULL and/or passed to free().
This leads to false positives in tests that expect malloc() to fail under memory
pressure, as the allocation is optimized away.
Disable this optimization for the affected function to preserve the intended
test behavior.
References: https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/Optimize-Options.html
Signed-off-by: Jan Polensky <japo@linux.ibm.com>
---
Changes since v1 (thanks to Cyril Hrubis):
- Use LTP_VAR_USED macro to prevent GCC from optimizing away malloc().
testcases/kernel/mem/tunable/overcommit_memory.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
index b5beebbcd3c4..9b2cb479d5ee 100644
--- a/testcases/kernel/mem/tunable/overcommit_memory.c
+++ b/testcases/kernel/mem/tunable/overcommit_memory.c
@@ -62,6 +62,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
+#include "tst_common.h"
#include "tst_test.h"
#define DEFAULT_OVER_RATIO 50L
@@ -155,7 +156,7 @@ static int heavy_malloc(long size)
{
char *p;
- p = malloc(size * TST_KB);
+ LTP_VAR_USED(p) = malloc(size * TST_KB);
if (p != NULL) {
tst_res(TINFO, "malloc %ld kB successfully", size);
free(p);
--
2.49.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [LTP] [PATCH v2 1/1] overcommit_memory: Disable optimization for malloc to prevent false positives
2025-05-22 15:21 [LTP] [PATCH v2 1/1] overcommit_memory: Disable optimization for malloc to prevent false positives Jan Polensky
@ 2025-05-23 6:22 ` Jan Stancek via ltp
0 siblings, 0 replies; 2+ messages in thread
From: Jan Stancek via ltp @ 2025-05-23 6:22 UTC (permalink / raw)
To: Jan Polensky; +Cc: ltp
On Thu, May 22, 2025 at 5:21 PM Jan Polensky <japo@linux.ibm.com> wrote:
>
> When compiling with GCC 15.1.1+, the default-enabled -fmalloc-dce=2 and
> -fallocation-dce under -O2 can eliminate the entire malloc block because its
> result is only compared to NULL and/or passed to free().
>
> This leads to false positives in tests that expect malloc() to fail under memory
> pressure, as the allocation is optimized away.
>
> Disable this optimization for the affected function to preserve the intended
> test behavior.
>
> References: https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/Optimize-Options.html
> Signed-off-by: Jan Polensky <japo@linux.ibm.com>
Pushed.
Thanks,
Jan
> ---
> Changes since v1 (thanks to Cyril Hrubis):
> - Use LTP_VAR_USED macro to prevent GCC from optimizing away malloc().
>
> testcases/kernel/mem/tunable/overcommit_memory.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
> index b5beebbcd3c4..9b2cb479d5ee 100644
> --- a/testcases/kernel/mem/tunable/overcommit_memory.c
> +++ b/testcases/kernel/mem/tunable/overcommit_memory.c
> @@ -62,6 +62,7 @@
> #include <stdio.h>
> #include <stdlib.h>
> #include <limits.h>
> +#include "tst_common.h"
> #include "tst_test.h"
>
> #define DEFAULT_OVER_RATIO 50L
> @@ -155,7 +156,7 @@ static int heavy_malloc(long size)
> {
> char *p;
>
> - p = malloc(size * TST_KB);
> + LTP_VAR_USED(p) = malloc(size * TST_KB);
> if (p != NULL) {
> tst_res(TINFO, "malloc %ld kB successfully", size);
> free(p);
> --
> 2.49.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-05-23 6:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-22 15:21 [LTP] [PATCH v2 1/1] overcommit_memory: Disable optimization for malloc to prevent false positives Jan Polensky
2025-05-23 6:22 ` Jan Stancek via ltp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox