@@ -26,7 +26,7 @@ extern crate sgx_trts;
26
26
extern crate sgx_types;
27
27
28
28
use core:: ffi:: c_void;
29
- use sgx_trts:: emm:: { self , AllocFlags , PageInfo , PageType , PfHandler , PfInfo , ProtFlags } ;
29
+ use sgx_trts:: emm:: { self , AllocFlags , EmaOptions , PageType , PfInfo , ProtFlags } ;
30
30
use sgx_trts:: veh:: HandleResult ;
31
31
use sgx_types:: error:: errno:: { EACCES , EEXIST , EINVAL , EPERM } ;
32
32
use sgx_types:: error:: SgxStatus ;
@@ -69,9 +69,13 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
69
69
let prot = ProtFlags :: from_bits ( pd. access as u8 ) . unwrap ( ) ;
70
70
let rw_bit = unsafe { pd. pf . pfec . bits . rw ( ) } ;
71
71
if ( rw_bit == 1 ) && ( prot == ProtFlags :: W ) {
72
- emm:: user_mm_modify_perms ( addr, SE_PAGE_SIZE , ProtFlags :: W | ProtFlags :: R ) ;
72
+ if emm:: user_mm_modify_perms ( addr, SE_PAGE_SIZE , ProtFlags :: W | ProtFlags :: R ) . is_err ( ) {
73
+ panic ! ( )
74
+ } ;
73
75
} else if ( rw_bit == 0 ) && prot. contains ( ProtFlags :: R ) {
74
- emm:: user_mm_modify_perms ( addr, SE_PAGE_SIZE , prot) ;
76
+ if emm:: user_mm_modify_perms ( addr, SE_PAGE_SIZE , prot) . is_err ( ) {
77
+ panic ! ( )
78
+ } ;
75
79
} else {
76
80
panic ! ( )
77
81
}
@@ -82,20 +86,13 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
82
86
#[ no_mangle]
83
87
fn test_modify_perms ( ) -> SgxStatus {
84
88
let mut pd = PfData :: default ( ) ;
85
-
86
89
// example 1:
87
- let base = emm:: user_mm_alloc (
88
- None ,
89
- ALLOC_SIZE ,
90
- AllocFlags :: COMMIT_NOW ,
91
- PageInfo {
92
- typ : PageType :: Reg ,
93
- prot : ProtFlags :: R | ProtFlags :: W ,
94
- } ,
90
+ let mut options = EmaOptions :: new ( 0 , ALLOC_SIZE , AllocFlags :: COMMIT_NOW ) ;
91
+ options. handle (
95
92
Some ( permission_pfhandler) ,
96
93
Some ( & mut pd as * mut PfData as * mut c_void ) ,
97
- )
98
- . unwrap ( ) ;
94
+ ) ;
95
+ let base = emm :: user_mm_alloc ( & mut options ) . unwrap ( ) ;
99
96
100
97
let data = unsafe { ( base as * const u8 ) . read ( ) } ;
101
98
assert ! ( data == 0 ) ;
@@ -116,7 +113,6 @@ fn test_modify_perms() -> SgxStatus {
116
113
// read success without PF
117
114
assert ! ( unsafe { pd. pf. pfec. errcd } == 0 ) ;
118
115
119
- // 出问题了
120
116
pd. access = ProtFlags :: W . bits ( ) as i32 ;
121
117
let count = ( ALLOC_SIZE - 1 ) as isize ;
122
118
unsafe {
@@ -134,10 +130,7 @@ fn test_modify_perms() -> SgxStatus {
134
130
// write indicated with PFEC
135
131
assert ! ( unsafe { pd. pf. pfec. bits. rw( ) } == 1 ) ;
136
132
137
- println ! (
138
- "{}" ,
139
- "Successfully run modify permissions and customized page fault handler!"
140
- ) ;
133
+ println ! ( "Successfully run modify permissions and customized page fault handler!" ) ;
141
134
SgxStatus :: Success
142
135
}
143
136
@@ -147,33 +140,24 @@ fn test_dynamic_expand_tcs() -> SgxStatus {
147
140
. name ( "thread1" . to_string ( ) )
148
141
. spawn ( move || {
149
142
println ! ( "Hello, this is a spawned thread!" ) ;
150
- } ) ;
143
+ } )
144
+ . expect ( "Failed to create thread!" ) ;
151
145
152
146
for _ in 0 ..40 {
153
147
let _t = thread:: spawn ( move || {
154
148
println ! ( "Hello, this is a spawned thread!" ) ;
155
149
} ) ;
156
150
}
157
151
158
- println ! ( "{}" , " Successfully dynamic expand tcs!") ;
152
+ println ! ( "Successfully dynamic expand tcs!" ) ;
159
153
SgxStatus :: Success
160
154
}
161
155
162
156
#[ no_mangle]
163
157
fn test_modify_types ( ) -> SgxStatus {
164
158
// example 1:
165
- let base = emm:: user_mm_alloc (
166
- None ,
167
- SE_PAGE_SIZE ,
168
- AllocFlags :: COMMIT_NOW ,
169
- PageInfo {
170
- typ : PageType :: Reg ,
171
- prot : ProtFlags :: R | ProtFlags :: W ,
172
- } ,
173
- None ,
174
- None ,
175
- )
176
- . unwrap ( ) ;
159
+ let mut options = EmaOptions :: new ( 0 , SE_PAGE_SIZE , AllocFlags :: COMMIT_NOW ) ;
160
+ let base = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
177
161
178
162
let res = emm:: user_mm_modify_type ( base, SE_PAGE_SIZE , PageType :: Tcs ) ;
179
163
assert ! ( res. is_ok( ) ) ;
@@ -182,18 +166,8 @@ fn test_modify_types() -> SgxStatus {
182
166
assert ! ( res. is_ok( ) ) ;
183
167
184
168
// example 2:
185
- let base = emm:: user_mm_alloc (
186
- None ,
187
- SE_PAGE_SIZE ,
188
- AllocFlags :: COMMIT_NOW ,
189
- PageInfo {
190
- typ : PageType :: Reg ,
191
- prot : ProtFlags :: R | ProtFlags :: W ,
192
- } ,
193
- None ,
194
- None ,
195
- )
196
- . unwrap ( ) ;
169
+ let mut options = EmaOptions :: new ( 0 , SE_PAGE_SIZE , AllocFlags :: COMMIT_NOW ) ;
170
+ let base = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
197
171
198
172
let res = emm:: user_mm_modify_perms ( base, SE_PAGE_SIZE , ProtFlags :: NONE ) ;
199
173
assert ! ( res. is_ok( ) ) ;
@@ -205,18 +179,8 @@ fn test_modify_types() -> SgxStatus {
205
179
let res = emm:: user_mm_dealloc ( 0 , ALLOC_SIZE ) ;
206
180
assert ! ( res == Err ( EINVAL ) ) ;
207
181
208
- let base = emm:: user_mm_alloc (
209
- None ,
210
- ALLOC_SIZE ,
211
- AllocFlags :: COMMIT_NOW ,
212
- PageInfo {
213
- typ : PageType :: Reg ,
214
- prot : ProtFlags :: R | ProtFlags :: W ,
215
- } ,
216
- None ,
217
- None ,
218
- )
219
- . unwrap ( ) ;
182
+ let mut options = EmaOptions :: new ( 0 , ALLOC_SIZE , AllocFlags :: COMMIT_NOW ) ;
183
+ let base = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
220
184
221
185
let res = emm:: user_mm_modify_type ( base + SE_PAGE_SIZE , SE_PAGE_SIZE , PageType :: Frist ) ;
222
186
assert ! ( res == Err ( EPERM ) ) ;
@@ -243,7 +207,7 @@ fn test_modify_types() -> SgxStatus {
243
207
let res = emm:: user_mm_dealloc ( base, ALLOC_SIZE ) ;
244
208
assert ! ( res. is_ok( ) ) ;
245
209
246
- println ! ( "{}" , " Successfully run modify types!") ;
210
+ println ! ( "Successfully run modify types!" ) ;
247
211
SgxStatus :: Success
248
212
}
249
213
@@ -252,33 +216,15 @@ fn test_commit_and_uncommit() -> SgxStatus {
252
216
let res = emm:: user_mm_dealloc ( 0 , ALLOC_SIZE ) ;
253
217
assert ! ( res == Err ( EINVAL ) ) ;
254
218
255
- let base = emm:: user_mm_alloc (
256
- None ,
257
- ALLOC_SIZE ,
258
- AllocFlags :: COMMIT_NOW ,
259
- PageInfo {
260
- typ : PageType :: Reg ,
261
- prot : ProtFlags :: R | ProtFlags :: W ,
262
- } ,
263
- None ,
264
- None ,
265
- )
266
- . unwrap ( ) ;
219
+ let mut options = EmaOptions :: new ( 0 , ALLOC_SIZE , AllocFlags :: COMMIT_NOW ) ;
220
+ let base = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
267
221
268
222
let res = emm:: user_mm_commit ( base, ALLOC_SIZE ) ;
269
223
assert ! ( res. is_ok( ) ) ;
270
224
271
- let res = emm:: user_mm_alloc (
272
- Some ( base) ,
273
- ALLOC_SIZE ,
274
- AllocFlags :: COMMIT_NOW | AllocFlags :: FIXED ,
275
- PageInfo {
276
- typ : PageType :: Reg ,
277
- prot : ProtFlags :: R | ProtFlags :: W ,
278
- } ,
279
- None ,
280
- None ,
281
- ) ;
225
+ let mut options = EmaOptions :: new ( base, ALLOC_SIZE , AllocFlags :: COMMIT_NOW | AllocFlags :: FIXED ) ;
226
+ let res = emm:: user_mm_alloc ( & mut options) ;
227
+
282
228
assert ! ( res == Err ( EEXIST ) ) ;
283
229
284
230
let res = emm:: user_mm_uncommit ( base, ALLOC_SIZE ) ;
@@ -299,31 +245,25 @@ fn test_commit_and_uncommit() -> SgxStatus {
299
245
let res = emm:: user_mm_uncommit ( base, ALLOC_SIZE ) ;
300
246
assert ! ( res == Err ( EINVAL ) ) ;
301
247
302
- let base2 = emm :: user_mm_alloc (
303
- None ,
248
+ let mut options = EmaOptions :: new (
249
+ 0 ,
304
250
ALLOC_SIZE ,
305
251
AllocFlags :: COMMIT_ON_DEMAND | AllocFlags :: FIXED ,
306
- PageInfo {
307
- typ : PageType :: Reg ,
308
- prot : ProtFlags :: R | ProtFlags :: W ,
309
- } ,
310
- None ,
311
- None ,
312
- )
313
- . unwrap ( ) ;
252
+ ) ;
253
+ let base2 = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
314
254
315
255
assert ! ( base == base2) ;
316
256
317
257
let ptr = base2 as * mut u8 ;
318
258
unsafe {
319
259
ptr. write ( 0xFF ) ;
320
- ptr. offset ( ( ALLOC_SIZE - 1 ) as isize ) . write ( 0xFF ) ;
260
+ ptr. add ( ALLOC_SIZE - 1 ) . write ( 0xFF ) ;
321
261
} ;
322
262
323
263
let res = emm:: user_mm_dealloc ( base2, ALLOC_SIZE ) ;
324
264
assert ! ( res. is_ok( ) ) ;
325
265
326
- println ! ( "{}" , " Successfully run commit and uncommit!") ;
266
+ println ! ( "Successfully run commit and uncommit!" ) ;
327
267
SgxStatus :: Success
328
268
}
329
269
@@ -337,7 +277,7 @@ fn test_stack_expand() -> SgxStatus {
337
277
for ( idx, item) in buf. iter ( ) . enumerate ( ) {
338
278
assert ! ( * item == ( idx % 256 ) as u8 ) ;
339
279
}
340
- println ! ( "{}" , " Successfully expand stack!") ;
280
+ println ! ( "Successfully expand stack!" ) ;
341
281
SgxStatus :: Success
342
282
}
343
283
@@ -346,22 +286,12 @@ fn test_emm_alloc_dealloc() -> SgxStatus {
346
286
let res = emm:: user_mm_dealloc ( 0 , ALLOC_SIZE ) ;
347
287
assert ! ( res == Err ( EINVAL ) ) ;
348
288
349
- let base = emm:: user_mm_alloc (
350
- None ,
351
- ALLOC_SIZE ,
352
- AllocFlags :: COMMIT_NOW ,
353
- PageInfo {
354
- typ : PageType :: Reg ,
355
- prot : ProtFlags :: R | ProtFlags :: W ,
356
- } ,
357
- None ,
358
- None ,
359
- )
360
- . unwrap ( ) ;
289
+ let mut options = EmaOptions :: new ( 0 , ALLOC_SIZE , AllocFlags :: COMMIT_NOW ) ;
290
+ let base = emm:: user_mm_alloc ( & mut options) . unwrap ( ) ;
361
291
362
292
let res = emm:: user_mm_dealloc ( base, ALLOC_SIZE ) ;
363
293
assert ! ( res. is_ok( ) ) ;
364
- println ! ( "{}" , " Successfully run alloc and dealloc!") ;
294
+ println ! ( "Successfully run alloc and dealloc!" ) ;
365
295
SgxStatus :: Success
366
296
}
367
297
0 commit comments