mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] KASan: test_kasan: hide buggy accesses from compiler
Date: Thu,  9 Nov 2023 12:38:06 +0100	[thread overview]
Message-ID: <20231109113807.1193935-1-a.fatoum@pengutronix.de> (raw)

Once we add __alloc_size attributes to allocations, GCC will complain
about violation of memory safety in test_kasan.c.

That memory violation is intended though as test_kasan is meant to
trigger kasan at runtime to verify correct operation.

Silence the warnings by hiding the origin of ptr, so the compiler loses
context about the size of the allocation.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 lib/kasan/test_kasan.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/kasan/test_kasan.c b/lib/kasan/test_kasan.c
index 14511cdb80bd..a74251a6d9ad 100644
--- a/lib/kasan/test_kasan.c
+++ b/lib/kasan/test_kasan.c
@@ -38,6 +38,8 @@ static noinline void malloc_oob_right(void)
 		return;
 	}
 
+	OPTIMIZER_HIDE_VAR(ptr);
+
 	ptr[size] = 'x';
 
 	free(ptr);
@@ -55,6 +57,8 @@ static noinline void malloc_oob_left(void)
 		return;
 	}
 
+	OPTIMIZER_HIDE_VAR(ptr);
+
 	*ptr = *(ptr - 1);
 	free(ptr);
 }
@@ -75,6 +79,8 @@ static noinline void malloc_oob_realloc_more(void)
 		return;
 	}
 
+	OPTIMIZER_HIDE_VAR(ptr2);
+
 	ptr2[size2] = 'x';
 
 	free(ptr2);
@@ -95,6 +101,8 @@ static noinline void malloc_oob_realloc_less(void)
 		return;
 	}
 
+	OPTIMIZER_HIDE_VAR(ptr2);
+
 	ptr2[size2] = 'x';
 
 	free(ptr2);
@@ -115,6 +123,9 @@ static noinline void malloc_oob_16(void)
 		free(ptr2);
 		return;
 	}
+
+	OPTIMIZER_HIDE_VAR(ptr1);
+
 	*ptr1 = *ptr2;
 	free(ptr1);
 	free(ptr2);
-- 
2.39.2




             reply	other threads:[~2023-11-09 11:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 11:38 Ahmad Fatoum [this message]
2023-11-09 11:38 ` [PATCH 2/2] malloc: use __attribute((alloc_size)) for dynamic memory allocation Ahmad Fatoum
2023-11-10 13:08 ` [PATCH 1/2] KASan: test_kasan: hide buggy accesses from compiler Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231109113807.1193935-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox