mirror of
https://github.com/torvalds/linux.git
synced 2024-11-26 06:02:05 +00:00
KVM: selftests: Add atoi_positive() and atoi_non_negative() for input validation
Many KVM selftests take command line arguments which are supposed to be positive (>0) or non-negative (>=0). Some tests do these validation and some missed adding the check. Add atoi_positive() and atoi_non_negative() to validate inputs in selftests before proceeding to use those values. Signed-off-by: Vipin Sharma <vipinsh@google.com> Reviewed-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20221103191719.1559407-7-vipinsh@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
parent
c15bdebb32
commit
0001725d0f
@ -414,36 +414,21 @@ static bool parse_args(int argc, char *argv[])
|
|||||||
while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) {
|
while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'n':
|
case 'n':
|
||||||
test_args.nr_vcpus = atoi_paranoid(optarg);
|
test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
if (test_args.nr_vcpus <= 0) {
|
if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
|
||||||
pr_info("Positive value needed for -n\n");
|
|
||||||
goto err;
|
|
||||||
} else if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
|
|
||||||
pr_info("Max allowed vCPUs: %u\n",
|
pr_info("Max allowed vCPUs: %u\n",
|
||||||
KVM_MAX_VCPUS);
|
KVM_MAX_VCPUS);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
test_args.nr_iter = atoi_paranoid(optarg);
|
test_args.nr_iter = atoi_positive("Number of iterations", optarg);
|
||||||
if (test_args.nr_iter <= 0) {
|
|
||||||
pr_info("Positive value needed for -i\n");
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
test_args.timer_period_ms = atoi_paranoid(optarg);
|
test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
|
||||||
if (test_args.timer_period_ms <= 0) {
|
|
||||||
pr_info("Positive value needed for -p\n");
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
test_args.migration_freq_ms = atoi_paranoid(optarg);
|
test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
|
||||||
if (test_args.migration_freq_ms < 0) {
|
|
||||||
pr_info("0 or positive value needed for -m\n");
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
|
@ -423,7 +423,7 @@ int main(int argc, char *argv[])
|
|||||||
while ((opt = getopt(argc, argv, "i:")) != -1) {
|
while ((opt = getopt(argc, argv, "i:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'i':
|
case 'i':
|
||||||
ss_iteration = atoi_paranoid(optarg);
|
ss_iteration = atoi_positive("Number of iterations", optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
|
@ -824,7 +824,7 @@ int main(int argc, char **argv)
|
|||||||
while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
|
while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'n':
|
case 'n':
|
||||||
nr_irqs = atoi_paranoid(optarg);
|
nr_irqs = atoi_non_negative("Number of IRQs", optarg);
|
||||||
if (nr_irqs > 1024 || nr_irqs % 32)
|
if (nr_irqs > 1024 || nr_irqs % 32)
|
||||||
help(argv[0]);
|
help(argv[0]);
|
||||||
break;
|
break;
|
||||||
|
@ -368,7 +368,7 @@ int main(int argc, char *argv[])
|
|||||||
params.vcpu_memory_bytes = parse_size(optarg);
|
params.vcpu_memory_bytes = parse_size(optarg);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
params.nr_vcpus = atoi_paranoid(optarg);
|
params.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
overlap_memory_access = true;
|
overlap_memory_access = true;
|
||||||
|
@ -427,8 +427,8 @@ int main(int argc, char *argv[])
|
|||||||
p.src_type = parse_backing_src_type(optarg);
|
p.src_type = parse_backing_src_type(optarg);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
nr_vcpus = atoi_paranoid(optarg);
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
|
TEST_ASSERT(nr_vcpus <= max_vcpus,
|
||||||
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
|
@ -416,9 +416,7 @@ int main(int argc, char *argv[])
|
|||||||
run_vcpus_while_disabling_dirty_logging = true;
|
run_vcpus_while_disabling_dirty_logging = true;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
p.wr_fract = atoi_paranoid(optarg);
|
p.wr_fract = atoi_positive("Write fraction", optarg);
|
||||||
TEST_ASSERT(p.wr_fract >= 1,
|
|
||||||
"Write fraction cannot be less than one");
|
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
dirty_log_manual_caps = 0;
|
dirty_log_manual_caps = 0;
|
||||||
@ -427,7 +425,7 @@ int main(int argc, char *argv[])
|
|||||||
help(argv[0]);
|
help(argv[0]);
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
p.iterations = atoi_paranoid(optarg);
|
p.iterations = atoi_positive("Number of iterations", optarg);
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
guest_modes_cmdline(optarg);
|
guest_modes_cmdline(optarg);
|
||||||
@ -445,12 +443,12 @@ int main(int argc, char *argv[])
|
|||||||
p.backing_src = parse_backing_src_type(optarg);
|
p.backing_src = parse_backing_src_type(optarg);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
nr_vcpus = atoi_paranoid(optarg);
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
|
TEST_ASSERT(nr_vcpus <= max_vcpus,
|
||||||
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
p.slots = atoi_paranoid(optarg);
|
p.slots = atoi_positive("Number of slots", optarg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
help(argv[0]);
|
help(argv[0]);
|
||||||
|
@ -154,4 +154,20 @@ static inline void *align_ptr_up(void *x, size_t size)
|
|||||||
|
|
||||||
int atoi_paranoid(const char *num_str);
|
int atoi_paranoid(const char *num_str);
|
||||||
|
|
||||||
|
static inline uint32_t atoi_positive(const char *name, const char *num_str)
|
||||||
|
{
|
||||||
|
int num = atoi_paranoid(num_str);
|
||||||
|
|
||||||
|
TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
|
||||||
|
{
|
||||||
|
int num = atoi_paranoid(num_str);
|
||||||
|
|
||||||
|
TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* SELFTEST_KVM_TEST_UTIL_H */
|
#endif /* SELFTEST_KVM_TEST_UTIL_H */
|
||||||
|
@ -461,8 +461,8 @@ int main(int argc, char *argv[])
|
|||||||
p.test_mem_size = parse_size(optarg);
|
p.test_mem_size = parse_size(optarg);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
nr_vcpus = atoi_paranoid(optarg);
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
|
TEST_ASSERT(nr_vcpus <= max_vcpus,
|
||||||
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
|
@ -193,16 +193,13 @@ int main(int argc, char *argv[])
|
|||||||
while ((opt = getopt(argc, argv, "c:h:m:s:H")) != -1) {
|
while ((opt = getopt(argc, argv, "c:h:m:s:H")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
nr_vcpus = atoi_paranoid(optarg);
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
TEST_ASSERT(nr_vcpus > 0, "number of vcpus must be >0");
|
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
max_mem = 1ull * atoi_paranoid(optarg) * SZ_1G;
|
max_mem = 1ull * atoi_positive("Memory size", optarg) * SZ_1G;
|
||||||
TEST_ASSERT(max_mem > 0, "memory size must be >0");
|
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
slot_size = 1ull * atoi_paranoid(optarg) * SZ_1G;
|
slot_size = 1ull * atoi_positive("Slot size", optarg) * SZ_1G;
|
||||||
TEST_ASSERT(slot_size > 0, "slot size must be >0");
|
|
||||||
break;
|
break;
|
||||||
case 'H':
|
case 'H':
|
||||||
hugepages = true;
|
hugepages = true;
|
||||||
|
@ -156,16 +156,14 @@ int main(int argc, char *argv[])
|
|||||||
guest_modes_cmdline(optarg);
|
guest_modes_cmdline(optarg);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
p.delay = atoi_paranoid(optarg);
|
p.delay = atoi_non_negative("Delay", optarg);
|
||||||
TEST_ASSERT(p.delay >= 0,
|
|
||||||
"A negative delay is not supported.");
|
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
guest_percpu_mem_size = parse_size(optarg);
|
guest_percpu_mem_size = parse_size(optarg);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
nr_vcpus = atoi_paranoid(optarg);
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
||||||
TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
|
TEST_ASSERT(nr_vcpus <= max_vcpus,
|
||||||
"Invalid number of vcpus, must be between 1 and %d",
|
"Invalid number of vcpus, must be between 1 and %d",
|
||||||
max_vcpus);
|
max_vcpus);
|
||||||
break;
|
break;
|
||||||
@ -173,7 +171,7 @@ int main(int argc, char *argv[])
|
|||||||
p.partition_vcpu_memory_access = false;
|
p.partition_vcpu_memory_access = false;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
p.nr_iterations = atoi_paranoid(optarg);
|
p.nr_iterations = atoi_positive("Number of iterations", optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
|
@ -892,33 +892,21 @@ static bool parse_args(int argc, char *argv[],
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
targs->tfirst = atoi_paranoid(optarg);
|
targs->tfirst = atoi_non_negative("First test", optarg);
|
||||||
if (targs->tfirst < 0) {
|
|
||||||
pr_info("First test to run has to be non-negative\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
targs->tlast = atoi_paranoid(optarg);
|
targs->tlast = atoi_non_negative("Last test", optarg);
|
||||||
if (targs->tlast < 0 || targs->tlast >= NTESTS) {
|
if (targs->tlast >= NTESTS) {
|
||||||
pr_info("Last test to run has to be non-negative and less than %zu\n",
|
pr_info("Last test to run has to be non-negative and less than %zu\n",
|
||||||
NTESTS);
|
NTESTS);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
targs->seconds = atoi_paranoid(optarg);
|
targs->seconds = atoi_non_negative("Test length", optarg);
|
||||||
if (targs->seconds < 0) {
|
|
||||||
pr_info("Test length in seconds has to be non-negative\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
targs->runs = atoi_paranoid(optarg);
|
targs->runs = atoi_positive("Runs per test", optarg);
|
||||||
if (targs->runs <= 0) {
|
|
||||||
pr_info("Runs per test has to be positive\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -407,7 +407,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
loops = atoi_paranoid(argv[1]);
|
loops = atoi_positive("Number of iterations", argv[1]);
|
||||||
else
|
else
|
||||||
loops = 10;
|
loops = 10;
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ int main(int argc, char **argv)
|
|||||||
while ((opt = getopt(argc, argv, "hp:t:r")) != -1) {
|
while ((opt = getopt(argc, argv, "hp:t:r")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'p':
|
case 'p':
|
||||||
reclaim_period_ms = atoi_paranoid(optarg);
|
reclaim_period_ms = atoi_non_negative("Reclaim period", optarg);
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
token = atoi_paranoid(optarg);
|
token = atoi_paranoid(optarg);
|
||||||
@ -257,7 +257,6 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_DISABLE_NX_HUGE_PAGES));
|
TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_DISABLE_NX_HUGE_PAGES));
|
||||||
TEST_REQUIRE(reclaim_period_ms > 0);
|
|
||||||
|
|
||||||
__TEST_REQUIRE(token == MAGIC_TOKEN,
|
__TEST_REQUIRE(token == MAGIC_TOKEN,
|
||||||
"This test must be run with the magic token %d.\n"
|
"This test must be run with the magic token %d.\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user