@@ -109,58 +109,80 @@ struct Exception {
109
109
// [1]: https://www.geoffchappell.com/studies/msvc/language/predefined/
110
110
111
111
#[ cfg( target_arch = "x86" ) ]
112
- #[ macro_use]
113
112
mod imp {
114
- pub type ptr_t = * mut u8 ;
115
-
116
- macro_rules! ptr {
117
- ( 0 ) => {
118
- core:: ptr:: null_mut( )
119
- } ;
120
- ( $e: expr) => {
121
- $e as * mut u8
122
- } ;
113
+ #[ repr( transparent) ]
114
+ #[ derive( Copy , Clone ) ]
115
+ pub struct ptr_t ( * mut u8 ) ;
116
+
117
+ impl ptr_t {
118
+ pub const fn null ( ) -> Self {
119
+ Self ( core:: ptr:: null_mut ( ) )
120
+ }
121
+
122
+ pub const fn new ( ptr : * mut u8 ) -> Self {
123
+ Self ( ptr)
124
+ }
123
125
}
124
126
}
125
127
126
128
#[ cfg( not( target_arch = "x86" ) ) ]
127
- #[ macro_use]
128
129
mod imp {
129
- pub type ptr_t = u32 ;
130
+ use core:: ptr:: addr_of;
131
+
132
+ // On 64-bit systems, SEH represents pointers as 32-bit offsets from `__ImageBase`.
133
+ #[ repr( transparent) ]
134
+ #[ derive( Copy , Clone ) ]
135
+ pub struct ptr_t ( u32 ) ;
130
136
131
137
extern "C" {
132
138
pub static __ImageBase: u8 ;
133
139
}
134
140
135
- macro_rules! ptr {
136
- ( 0 ) => ( 0 ) ;
137
- ( $e: expr) => {
138
- ( ( $e as usize ) - ( addr_of!( imp:: __ImageBase) as usize ) ) as u32
141
+ impl ptr_t {
142
+ pub const fn null ( ) -> Self {
143
+ Self ( 0 )
144
+ }
145
+
146
+ pub fn new ( ptr : * mut u8 ) -> Self {
147
+ // We need to expose the provenance of the pointer because it is not carried by
148
+ // the `u32`, while the FFI needs to have this provenance to excess our statics.
149
+ //
150
+ // NOTE(niluxv): we could use `MaybeUninit<u32>` instead to leak the provenance
151
+ // into the FFI. In theory then the other side would need to do some processing
152
+ // to get a pointer with correct provenance, but these system functions aren't
153
+ // going to be cross-lang LTOed anyway. However, using expose is shorter and
154
+ // requires less unsafe.
155
+ let addr: usize = ptr. expose_provenance ( ) ;
156
+ let image_base = unsafe { addr_of ! ( __ImageBase) } . addr ( ) ;
157
+ let offset: usize = addr - image_base;
158
+ Self ( offset as u32 )
139
159
}
140
160
}
141
161
}
142
162
163
+ use imp:: ptr_t;
164
+
143
165
#[ repr( C ) ]
144
166
pub struct _ThrowInfo {
145
167
pub attributes : c_uint ,
146
- pub pmfnUnwind : imp :: ptr_t ,
147
- pub pForwardCompat : imp :: ptr_t ,
148
- pub pCatchableTypeArray : imp :: ptr_t ,
168
+ pub pmfnUnwind : ptr_t ,
169
+ pub pForwardCompat : ptr_t ,
170
+ pub pCatchableTypeArray : ptr_t ,
149
171
}
150
172
151
173
#[ repr( C ) ]
152
174
pub struct _CatchableTypeArray {
153
175
pub nCatchableTypes : c_int ,
154
- pub arrayOfCatchableTypes : [ imp :: ptr_t ; 1 ] ,
176
+ pub arrayOfCatchableTypes : [ ptr_t ; 1 ] ,
155
177
}
156
178
157
179
#[ repr( C ) ]
158
180
pub struct _CatchableType {
159
181
pub properties : c_uint ,
160
- pub pType : imp :: ptr_t ,
182
+ pub pType : ptr_t ,
161
183
pub thisDisplacement : _PMD ,
162
184
pub sizeOrOffset : c_int ,
163
- pub copyFunction : imp :: ptr_t ,
185
+ pub copyFunction : ptr_t ,
164
186
}
165
187
166
188
#[ repr( C ) ]
@@ -186,20 +208,20 @@ const TYPE_NAME: [u8; 11] = *b"rust_panic\0";
186
208
187
209
static mut THROW_INFO : _ThrowInfo = _ThrowInfo {
188
210
attributes : 0 ,
189
- pmfnUnwind : ptr ! ( 0 ) ,
190
- pForwardCompat : ptr ! ( 0 ) ,
191
- pCatchableTypeArray : ptr ! ( 0 ) ,
211
+ pmfnUnwind : ptr_t :: null ( ) ,
212
+ pForwardCompat : ptr_t :: null ( ) ,
213
+ pCatchableTypeArray : ptr_t :: null ( ) ,
192
214
} ;
193
215
194
216
static mut CATCHABLE_TYPE_ARRAY : _CatchableTypeArray =
195
- _CatchableTypeArray { nCatchableTypes : 1 , arrayOfCatchableTypes : [ ptr ! ( 0 ) ] } ;
217
+ _CatchableTypeArray { nCatchableTypes : 1 , arrayOfCatchableTypes : [ ptr_t :: null ( ) ] } ;
196
218
197
219
static mut CATCHABLE_TYPE : _CatchableType = _CatchableType {
198
220
properties : 0 ,
199
- pType : ptr ! ( 0 ) ,
221
+ pType : ptr_t :: null ( ) ,
200
222
thisDisplacement : _PMD { mdisp : 0 , pdisp : -1 , vdisp : 0 } ,
201
223
sizeOrOffset : mem:: size_of :: < Exception > ( ) as c_int ,
202
- copyFunction : ptr ! ( 0 ) ,
224
+ copyFunction : ptr_t :: null ( ) ,
203
225
} ;
204
226
205
227
extern "C" {
@@ -246,9 +268,9 @@ macro_rules! define_cleanup {
246
268
super :: __rust_drop_panic( ) ;
247
269
}
248
270
}
249
- unsafe extern $abi2 fn exception_copy( _dest : * mut Exception ,
250
- _src: * mut Exception )
251
- -> * mut Exception {
271
+ unsafe extern $abi2 fn exception_copy(
272
+ _dest : * mut Exception , _src: * mut Exception
273
+ ) -> * mut Exception {
252
274
panic!( "Rust panics cannot be copied" ) ;
253
275
}
254
276
}
@@ -296,24 +318,24 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
296
318
// In any case, we basically need to do something like this until we can
297
319
// express more operations in statics (and we may never be able to).
298
320
atomic_store_seqcst (
299
- addr_of_mut ! ( THROW_INFO . pmfnUnwind) as * mut u32 ,
300
- ptr ! ( exception_cleanup) as u32 ,
321
+ addr_of_mut ! ( THROW_INFO . pmfnUnwind) . cast ( ) ,
322
+ ptr_t :: new ( exception_cleanup as * mut u8 ) ,
301
323
) ;
302
324
atomic_store_seqcst (
303
- addr_of_mut ! ( THROW_INFO . pCatchableTypeArray) as * mut u32 ,
304
- ptr ! ( addr_of !( CATCHABLE_TYPE_ARRAY ) ) as u32 ,
325
+ addr_of_mut ! ( THROW_INFO . pCatchableTypeArray) . cast ( ) ,
326
+ ptr_t :: new ( addr_of_mut ! ( CATCHABLE_TYPE_ARRAY ) . cast ( ) ) ,
305
327
) ;
306
328
atomic_store_seqcst (
307
- addr_of_mut ! ( CATCHABLE_TYPE_ARRAY . arrayOfCatchableTypes[ 0 ] ) as * mut u32 ,
308
- ptr ! ( addr_of !( CATCHABLE_TYPE ) ) as u32 ,
329
+ addr_of_mut ! ( CATCHABLE_TYPE_ARRAY . arrayOfCatchableTypes[ 0 ] ) . cast ( ) ,
330
+ ptr_t :: new ( addr_of_mut ! ( CATCHABLE_TYPE ) . cast ( ) ) ,
309
331
) ;
310
332
atomic_store_seqcst (
311
- addr_of_mut ! ( CATCHABLE_TYPE . pType) as * mut u32 ,
312
- ptr ! ( addr_of !( TYPE_DESCRIPTOR ) ) as u32 ,
333
+ addr_of_mut ! ( CATCHABLE_TYPE . pType) . cast ( ) ,
334
+ ptr_t :: new ( addr_of_mut ! ( TYPE_DESCRIPTOR ) . cast ( ) ) ,
313
335
) ;
314
336
atomic_store_seqcst (
315
- addr_of_mut ! ( CATCHABLE_TYPE . copyFunction) as * mut u32 ,
316
- ptr ! ( exception_copy) as u32 ,
337
+ addr_of_mut ! ( CATCHABLE_TYPE . copyFunction) . cast ( ) ,
338
+ ptr_t :: new ( exception_copy as * mut u8 ) ,
317
339
) ;
318
340
319
341
extern "system-unwind" {
0 commit comments