mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-04 20:19:47 +08:00
binder: Use seq_buf in binder_alloc kunit tests
Replace instances of snprintf with seq_buf functions, as suggested by
Kees [1].
[1] https://lore.kernel.org/all/202507160743.15E8044@keescook/
Fixes: d1934ed980
("binder: encapsulate individual alloc test cases")
Suggested-by: Kees Cook <kees@kernel.org>
Cc: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Tiffany Yang <ynaffit@google.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Link: https://lore.kernel.org/r/20250722234508.232228-2-ynaffit@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
8a8d47e86c
commit
fa3f79e82d
@ -15,6 +15,7 @@
|
|||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <linux/mm.h>
|
#include <linux/mm.h>
|
||||||
#include <linux/mman.h>
|
#include <linux/mman.h>
|
||||||
|
#include <linux/seq_buf.h>
|
||||||
#include <linux/sizes.h>
|
#include <linux/sizes.h>
|
||||||
|
|
||||||
#include "../binder_alloc.h"
|
#include "../binder_alloc.h"
|
||||||
@ -107,40 +108,33 @@ static const char *const buf_end_align_type_strs[LOOP_END] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct binder_alloc_test_case_info {
|
struct binder_alloc_test_case_info {
|
||||||
|
char alignments[ALIGNMENTS_BUFLEN];
|
||||||
|
struct seq_buf alignments_sb;
|
||||||
size_t *buffer_sizes;
|
size_t *buffer_sizes;
|
||||||
int *free_sequence;
|
int *free_sequence;
|
||||||
char alignments[ALIGNMENTS_BUFLEN];
|
|
||||||
bool front_pages;
|
bool front_pages;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void stringify_free_seq(struct kunit *test, int *seq, char *buf,
|
static void stringify_free_seq(struct kunit *test, int *seq, struct seq_buf *sb)
|
||||||
size_t buf_len)
|
|
||||||
{
|
{
|
||||||
size_t bytes = 0;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < BUFFER_NUM; i++) {
|
for (i = 0; i < BUFFER_NUM; i++)
|
||||||
bytes += snprintf(buf + bytes, buf_len - bytes, "[%d]", seq[i]);
|
seq_buf_printf(sb, "[%d]", seq[i]);
|
||||||
if (bytes >= buf_len)
|
|
||||||
break;
|
KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
|
||||||
}
|
|
||||||
KUNIT_EXPECT_LT(test, bytes, buf_len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stringify_alignments(struct kunit *test, int *alignments,
|
static void stringify_alignments(struct kunit *test, int *alignments,
|
||||||
char *buf, size_t buf_len)
|
struct seq_buf *sb)
|
||||||
{
|
{
|
||||||
size_t bytes = 0;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < BUFFER_NUM; i++) {
|
for (i = 0; i < BUFFER_NUM; i++)
|
||||||
bytes += snprintf(buf + bytes, buf_len - bytes, "[ %d:%s ]", i,
|
seq_buf_printf(sb, "[ %d:%s ]", i,
|
||||||
buf_end_align_type_strs[alignments[i]]);
|
buf_end_align_type_strs[alignments[i]]);
|
||||||
if (bytes >= buf_len)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
KUNIT_EXPECT_LT(test, bytes, buf_len);
|
KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool check_buffer_pages_allocated(struct kunit *test,
|
static bool check_buffer_pages_allocated(struct kunit *test,
|
||||||
@ -311,19 +305,20 @@ static void permute_frees(struct kunit *test, struct binder_alloc *alloc,
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (index == BUFFER_NUM) {
|
if (index == BUFFER_NUM) {
|
||||||
char freeseq_buf[FREESEQ_BUFLEN];
|
DECLARE_SEQ_BUF(freeseq_sb, FREESEQ_BUFLEN);
|
||||||
|
|
||||||
case_failed = binder_alloc_test_alloc_free(test, alloc, tc, end);
|
case_failed = binder_alloc_test_alloc_free(test, alloc, tc, end);
|
||||||
*runs += 1;
|
*runs += 1;
|
||||||
*failures += case_failed;
|
*failures += case_failed;
|
||||||
|
|
||||||
if (case_failed || PRINT_ALL_CASES) {
|
if (case_failed || PRINT_ALL_CASES) {
|
||||||
stringify_free_seq(test, tc->free_sequence, freeseq_buf,
|
stringify_free_seq(test, tc->free_sequence,
|
||||||
FREESEQ_BUFLEN);
|
&freeseq_sb);
|
||||||
kunit_err(test, "case %lu: [%s] | %s - %s - %s", *runs,
|
kunit_err(test, "case %lu: [%s] | %s - %s - %s", *runs,
|
||||||
case_failed ? "FAILED" : "PASSED",
|
case_failed ? "FAILED" : "PASSED",
|
||||||
tc->front_pages ? "front" : "back ",
|
tc->front_pages ? "front" : "back ",
|
||||||
tc->alignments, freeseq_buf);
|
seq_buf_str(&tc->alignments_sb),
|
||||||
|
seq_buf_str(&freeseq_sb));
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -383,8 +378,9 @@ static void gen_buf_offsets(struct kunit *test, struct binder_alloc *alloc,
|
|||||||
if (index == BUFFER_NUM) {
|
if (index == BUFFER_NUM) {
|
||||||
struct binder_alloc_test_case_info tc = {0};
|
struct binder_alloc_test_case_info tc = {0};
|
||||||
|
|
||||||
stringify_alignments(test, alignments, tc.alignments,
|
seq_buf_init(&tc.alignments_sb, tc.alignments,
|
||||||
ALIGNMENTS_BUFLEN);
|
ALIGNMENTS_BUFLEN);
|
||||||
|
stringify_alignments(test, alignments, &tc.alignments_sb);
|
||||||
|
|
||||||
gen_buf_sizes(test, alloc, &tc, end_offset, runs, failures);
|
gen_buf_sizes(test, alloc, &tc, end_offset, runs, failures);
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user