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