From caa2e5a4ac45c5e107b6802db99c158083321bc4 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Mon, 27 Aug 2018 11:34:20 -0700 Subject: [PATCH] plugin: do not fail crosvm_vcpu_get_msrs() if we failed to fetch some KVM_GET_MSRS may return less MSRs that were requested; do not fail but instead let callers to know how many were fetched. BUG=None TEST=cargo test --features plugin Change-Id: Ie14a3d38b66bfe34f5279543bea9c6c78423527e Signed-off-by: Dmitry Torokhov Reviewed-on: https://chromium-review.googlesource.com/1192232 Reviewed-by: Zach Reizner Reviewed-by: Dylan Reid --- crosvm_plugin/crosvm.h | 3 ++- crosvm_plugin/src/lib.rs | 16 +++++++++++----- tests/plugins.rs | 7 ++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/crosvm_plugin/crosvm.h b/crosvm_plugin/crosvm.h index c2139b3ea6..39c2453325 100644 --- a/crosvm_plugin/crosvm.h +++ b/crosvm_plugin/crosvm.h @@ -509,7 +509,8 @@ int crosvm_vcpu_set_xcrs(struct crosvm_vcpu*, const struct kvm_xcrs*); /* Gets the MSRs of the vcpu indicated by the index field of each entry. */ int crosvm_vcpu_get_msrs(struct crosvm_vcpu*, uint32_t __msr_count, - struct kvm_msr_entry *__msr_entries); + struct kvm_msr_entry *__msr_entries, + uint32_t *__out_count); /* Sets the MSRs of the vcpu indicated by the index field of each entry. */ int crosvm_vcpu_set_msrs(struct crosvm_vcpu*, uint32_t __msr_count, const struct kvm_msr_entry *__msr_entries); diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs index 6567ee8949..06cb367ed3 100644 --- a/crosvm_plugin/src/lib.rs +++ b/crosvm_plugin/src/lib.rs @@ -995,7 +995,9 @@ impl crosvm_vcpu { Ok(()) } - fn get_msrs(&mut self, msr_entries: &mut [kvm_msr_entry]) -> result::Result<(), c_int> { + fn get_msrs(&mut self, msr_entries: &mut [kvm_msr_entry], msr_count: &mut usize) + -> result::Result<(), c_int> { + *msr_count = 0; let mut r = VcpuRequest::new(); { let entry_indices: &mut Vec = r.mut_get_msrs().mut_entry_indices(); @@ -1008,8 +1010,9 @@ impl crosvm_vcpu { return Err(EPROTO); } let get_msrs: &VcpuResponse_GetMsrs = response.get_get_msrs(); - if get_msrs.get_entry_data().len() != msr_entries.len() { - return Err(EPROTO); + *msr_count = get_msrs.get_entry_data().len(); + if *msr_count > msr_entries.len() { + return Err(E2BIG); } for (&msr_data, msr_entry) in get_msrs @@ -1464,12 +1467,15 @@ pub unsafe extern "C" fn crosvm_vcpu_set_xcrs(this: *mut crosvm_vcpu, #[no_mangle] pub unsafe extern "C" fn crosvm_vcpu_get_msrs(this: *mut crosvm_vcpu, msr_count: u32, - msr_entries: *mut kvm_msr_entry) + msr_entries: *mut kvm_msr_entry, + out_count: *mut u32) -> c_int { let _u = STATS.record(Stat::VcpuGetMsrs); let this = &mut *this; let msr_entries = from_raw_parts_mut(msr_entries, msr_count as usize); - let ret = this.get_msrs(msr_entries); + let mut count: usize = 0; + let ret = this.get_msrs(msr_entries, &mut count); + *out_count = count as u32; to_crosvm_rc(ret) } diff --git a/tests/plugins.rs b/tests/plugins.rs index 7d2d8ff834..987b0338ee 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -411,6 +411,7 @@ fn test_msrs() { #define KILL_ADDRESS 0x3000 int g_kill_evt; + uint32_t g_msr2_count; struct kvm_msr_entry g_msr2; int setup_vm(struct crosvm *crosvm, void *mem) { @@ -445,7 +446,7 @@ fn test_msrs() { { uint64_t dummy = 1; g_msr2.index = MSR2_INDEX; - crosvm_vcpu_get_msrs(vcpu, 1, &g_msr2); + crosvm_vcpu_get_msrs(vcpu, 1, &g_msr2, &g_msr2_count); write(g_kill_evt, &dummy, sizeof(dummy)); return 1; } @@ -458,6 +459,10 @@ fn test_msrs() { fprintf(stderr, "msr1 has unexpected value: 0x%x\n", msr1_data); return 1; } + if (g_msr2_count != 1) { + fprintf(stderr, "incorrect number of returned MSRSs: %d\n", g_msr2_count); + return 1; + } if (g_msr2.data != MSR2_DATA) { fprintf(stderr, "msr2 has unexpected value: 0x%x\n", g_msr2.data); return 1;