Skip to content

Commit fe85ce3

Browse files
committed
KVM: selftests: Assert that vcpu_{g,s}et_reg() won't truncate
Assert that the register being read/written by vcpu_{g,s}et_reg() is no larger than a uint64_t, i.e. that a selftest isn't unintentionally truncating the value being read/written. Ideally, the assert would be done at compile-time, but that would limit the checks to hardcoded accesses and/or require fancier compile-time assertion infrastructure to filter out dynamic usage. Reviewed-by: Andrew Jones <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 09bb926 commit fe85ce3

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,17 @@ static inline uint64_t vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id)
707707
uint64_t val;
708708
struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
709709

710+
TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id);
711+
710712
vcpu_ioctl(vcpu, KVM_GET_ONE_REG, &reg);
711713
return val;
712714
}
713715
static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
714716
{
715717
struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
716718

719+
TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id);
720+
717721
vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
718722
}
719723

0 commit comments

Comments
 (0)