@@ -126,21 +126,14 @@ 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
133
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 )
134
+ let vcpu_ptr = cell. get ( ) . unwrap ( ) ;
135
+ assert ! ( std:: ptr:: eq( vcpu_ptr, self ) ) ;
136
+ cell. update ( |_| None ) ;
144
137
} )
145
138
}
146
139
@@ -615,7 +608,7 @@ fn handle_kvm_exit(
615
608
616
609
impl Drop for Vcpu {
617
610
fn drop ( & mut self ) {
618
- let _ = self . reset_thread_local_data ( ) ;
611
+ self . reset_thread_local_data ( ) ;
619
612
}
620
613
}
621
614
@@ -1039,7 +1032,7 @@ pub(crate) mod tests {
1039
1032
}
1040
1033
1041
1034
// Reset vcpu TLS.
1042
- vcpu. reset_thread_local_data ( ) . unwrap ( ) ;
1035
+ vcpu. reset_thread_local_data ( ) ;
1043
1036
1044
1037
// Running on the TLS vcpu after TLS reset should fail.
1045
1038
unsafe {
@@ -1050,6 +1043,15 @@ pub(crate) mod tests {
1050
1043
vcpu. reset_thread_local_data ( ) . unwrap_err ( ) ;
1051
1044
}
1052
1045
1046
+ #[ test]
1047
+ #[ should_panic]
1048
+ fn test_vcpu_tls_double_reset ( ) {
1049
+ let ( _, _, mut vcpu) = setup_vcpu ( 0x1000 ) ;
1050
+ vcpu. init_thread_local_data ( ) ;
1051
+ vcpu. reset_thread_local_data ( ) ;
1052
+ vcpu. reset_thread_local_data ( ) ;
1053
+ }
1054
+
1053
1055
#[ test]
1054
1056
#[ should_panic]
1055
1057
fn test_invalid_tls ( ) {
0 commit comments