@@ -126,21 +126,16 @@ impl Vcpu {
126
126
127
127
/// Deassociates `self` from the current thread.
128
128
///
129
- /// Should be called if the current `self` had called `init_thread_local_data()` and
130
- /// now needs to move to a different thread.
131
- ///
132
- /// Fails if `self` was not previously associated with the current thread.
133
- fn reset_thread_local_data ( & mut self ) -> Result < ( ) , VcpuError > {
129
+ /// Must be called after `init_thread_local_data`.
130
+ fn reset_thread_local_data ( & mut self ) {
134
131
// Best-effort to clean up TLS. If the `Vcpu` was moved to another thread
135
132
// _before_ running this, then there is nothing we can do.
136
- Self :: TLS_VCPU_PTR . with ( |cell : & VcpuCell | {
137
- if let Some ( vcpu_ptr) = cell. get ( ) {
138
- if std:: ptr:: eq ( vcpu_ptr, self ) {
139
- Self :: TLS_VCPU_PTR . with ( |cell : & VcpuCell | cell. take ( ) ) ;
140
- return Ok ( ( ) ) ;
141
- }
142
- }
143
- Err ( VcpuError :: VcpuTlsNotPresent )
133
+ Self :: TLS_VCPU_PTR . with ( |cell| {
134
+ let vcpu_ptr = cell. get ( ) . unwrap ( ) ;
135
+ assert ! ( std:: ptr:: eq( vcpu_ptr, self ) ) ;
136
+ // Have to do this trick because `update` method is
137
+ // not stable on Cell.
138
+ Self :: TLS_VCPU_PTR . with ( |cell| cell. take ( ) ) ;
144
139
} )
145
140
}
146
141
@@ -615,7 +610,7 @@ fn handle_kvm_exit(
615
610
616
611
impl Drop for Vcpu {
617
612
fn drop ( & mut self ) {
618
- let _ = self . reset_thread_local_data ( ) ;
613
+ self . reset_thread_local_data ( ) ;
619
614
}
620
615
}
621
616
@@ -1039,15 +1034,24 @@ pub(crate) mod tests {
1039
1034
}
1040
1035
1041
1036
// Reset vcpu TLS.
1042
- vcpu. reset_thread_local_data ( ) . unwrap ( ) ;
1037
+ vcpu. reset_thread_local_data ( ) ;
1043
1038
1044
1039
// Running on the TLS vcpu after TLS reset should fail.
1045
1040
unsafe {
1046
1041
Vcpu :: run_on_thread_local ( |_| ( ) ) . unwrap_err ( ) ;
1047
1042
}
1048
1043
1049
1044
// Second reset should return error.
1050
- vcpu. reset_thread_local_data ( ) . unwrap_err ( ) ;
1045
+ vcpu. reset_thread_local_data ( ) ;
1046
+ }
1047
+
1048
+ #[ test]
1049
+ #[ should_panic]
1050
+ fn test_vcpu_tls_double_reset ( ) {
1051
+ let ( _, _, mut vcpu) = setup_vcpu ( 0x1000 ) ;
1052
+ vcpu. init_thread_local_data ( ) ;
1053
+ vcpu. reset_thread_local_data ( ) ;
1054
+ vcpu. reset_thread_local_data ( ) ;
1051
1055
}
1052
1056
1053
1057
#[ test]
0 commit comments