crovm/plugin: fix PerVcpuState to be per vcpu

Because resize was used to grow a vec, each Arc<Mutex<PerVcpuState>> was
cloned from the original Default, merely increasing the ref count on the
same default data.

This change manually pushes a unique set of data per vcpu.

BUG=chromium:835916
TEST=None

Change-Id: I7116c764effd0f33f706f912bcf4d5d28ba1e08e
Reviewed-on: https://chromium-review.googlesource.com/1024504
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Aleksandr Kartashov <regmka@gmail.com>
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Zach Reizner 2018-04-23 11:28:59 -07:00 committed by chrome-bot
parent a894f53439
commit da3f64c8fe

View file

@ -144,8 +144,15 @@ impl Process {
for _ in 0..cpu_count { for _ in 0..cpu_count {
vcpu_sockets.push(new_seqpacket_pair().map_err(Error::CreateVcpuSocket)?); vcpu_sockets.push(new_seqpacket_pair().map_err(Error::CreateVcpuSocket)?);
} }
let mut per_vcpu_states: Vec<Arc<Mutex<PerVcpuState>>> = Vec::new(); let mut per_vcpu_states: Vec<Arc<Mutex<PerVcpuState>>> = Vec::with_capacity(cpu_count as
per_vcpu_states.resize(cpu_count as usize, Default::default()); usize);
// TODO(zachr): replace with `resize_default` when that stabilizes. Using a plain `resize`
// is incorrect because each element in the `Vec` will contain a shared reference to the
// same `PerVcpuState` instance. This happens because `resize` fills new slots using clones
// of the instance given to `resize`.
for _ in 0..cpu_count {
per_vcpu_states.push(Default::default());
}
let plugin_pid = match jail { let plugin_pid = match jail {
Some(jail) => { Some(jail) => {