Skip to content

Commit 059718f

Browse files
committed
Implement EmaOptions for reducing the number of inarguments
1 parent b374c98 commit 059718f

File tree

10 files changed

+289
-413
lines changed

10 files changed

+289
-413
lines changed

samplecode/emm/enclave/src/lib.rs

+37-107
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern crate sgx_trts;
2626
extern crate sgx_types;
2727

2828
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};
3030
use sgx_trts::veh::HandleResult;
3131
use sgx_types::error::errno::{EACCES, EEXIST, EINVAL, EPERM};
3232
use sgx_types::error::SgxStatus;
@@ -69,9 +69,13 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
6969
let prot = ProtFlags::from_bits(pd.access as u8).unwrap();
7070
let rw_bit = unsafe { pd.pf.pfec.bits.rw() };
7171
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+
};
7375
} 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+
};
7579
} else {
7680
panic!()
7781
}
@@ -82,20 +86,13 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
8286
#[no_mangle]
8387
fn test_modify_perms() -> SgxStatus {
8488
let mut pd = PfData::default();
85-
8689
// 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(
9592
Some(permission_pfhandler),
9693
Some(&mut pd as *mut PfData as *mut c_void),
97-
)
98-
.unwrap();
94+
);
95+
let base = emm::user_mm_alloc(&mut options).unwrap();
9996

10097
let data = unsafe { (base as *const u8).read() };
10198
assert!(data == 0);
@@ -116,7 +113,6 @@ fn test_modify_perms() -> SgxStatus {
116113
// read success without PF
117114
assert!(unsafe { pd.pf.pfec.errcd } == 0);
118115

119-
// 出问题了
120116
pd.access = ProtFlags::W.bits() as i32;
121117
let count = (ALLOC_SIZE - 1) as isize;
122118
unsafe {
@@ -134,10 +130,7 @@ fn test_modify_perms() -> SgxStatus {
134130
// write indicated with PFEC
135131
assert!(unsafe { pd.pf.pfec.bits.rw() } == 1);
136132

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!");
141134
SgxStatus::Success
142135
}
143136

@@ -147,33 +140,24 @@ fn test_dynamic_expand_tcs() -> SgxStatus {
147140
.name("thread1".to_string())
148141
.spawn(move || {
149142
println!("Hello, this is a spawned thread!");
150-
});
143+
})
144+
.expect("Failed to create thread!");
151145

152146
for _ in 0..40 {
153147
let _t = thread::spawn(move || {
154148
println!("Hello, this is a spawned thread!");
155149
});
156150
}
157151

158-
println!("{}", "Successfully dynamic expand tcs!");
152+
println!("Successfully dynamic expand tcs!");
159153
SgxStatus::Success
160154
}
161155

162156
#[no_mangle]
163157
fn test_modify_types() -> SgxStatus {
164158
// 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();
177161

178162
let res = emm::user_mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
179163
assert!(res.is_ok());
@@ -182,18 +166,8 @@ fn test_modify_types() -> SgxStatus {
182166
assert!(res.is_ok());
183167

184168
// 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();
197171

198172
let res = emm::user_mm_modify_perms(base, SE_PAGE_SIZE, ProtFlags::NONE);
199173
assert!(res.is_ok());
@@ -205,18 +179,8 @@ fn test_modify_types() -> SgxStatus {
205179
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
206180
assert!(res == Err(EINVAL));
207181

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();
220184

221185
let res = emm::user_mm_modify_type(base + SE_PAGE_SIZE, SE_PAGE_SIZE, PageType::Frist);
222186
assert!(res == Err(EPERM));
@@ -243,7 +207,7 @@ fn test_modify_types() -> SgxStatus {
243207
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
244208
assert!(res.is_ok());
245209

246-
println!("{}", "Successfully run modify types!");
210+
println!("Successfully run modify types!");
247211
SgxStatus::Success
248212
}
249213

@@ -252,33 +216,15 @@ fn test_commit_and_uncommit() -> SgxStatus {
252216
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
253217
assert!(res == Err(EINVAL));
254218

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();
267221

268222
let res = emm::user_mm_commit(base, ALLOC_SIZE);
269223
assert!(res.is_ok());
270224

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+
282228
assert!(res == Err(EEXIST));
283229

284230
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
@@ -299,31 +245,25 @@ fn test_commit_and_uncommit() -> SgxStatus {
299245
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
300246
assert!(res == Err(EINVAL));
301247

302-
let base2 = emm::user_mm_alloc(
303-
None,
248+
let mut options = EmaOptions::new(
249+
0,
304250
ALLOC_SIZE,
305251
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();
314254

315255
assert!(base == base2);
316256

317257
let ptr = base2 as *mut u8;
318258
unsafe {
319259
ptr.write(0xFF);
320-
ptr.offset((ALLOC_SIZE - 1) as isize).write(0xFF);
260+
ptr.add(ALLOC_SIZE - 1).write(0xFF);
321261
};
322262

323263
let res = emm::user_mm_dealloc(base2, ALLOC_SIZE);
324264
assert!(res.is_ok());
325265

326-
println!("{}", "Successfully run commit and uncommit!");
266+
println!("Successfully run commit and uncommit!");
327267
SgxStatus::Success
328268
}
329269

@@ -337,7 +277,7 @@ fn test_stack_expand() -> SgxStatus {
337277
for (idx, item) in buf.iter().enumerate() {
338278
assert!(*item == (idx % 256) as u8);
339279
}
340-
println!("{}", "Successfully expand stack!");
280+
println!("Successfully expand stack!");
341281
SgxStatus::Success
342282
}
343283

@@ -346,22 +286,12 @@ fn test_emm_alloc_dealloc() -> SgxStatus {
346286
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
347287
assert!(res == Err(EINVAL));
348288

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();
361291

362292
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
363293
assert!(res.is_ok());
364-
println!("{}", "Successfully run alloc and dealloc!");
294+
println!("Successfully run alloc and dealloc!");
365295
SgxStatus::Success
366296
}
367297

sgx_trts/src/capi.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
use crate::arch::{SE_PAGE_SHIFT, SE_PAGE_SIZE};
1919
use crate::call::{ocall, OCallIndex, OcBuffer};
20-
use crate::emm::alloc::Alloc;
20+
use crate::emm::ema::EmaOptions;
2121
use crate::emm::page::AllocFlags;
2222
use crate::emm::pfhandler::PfHandler;
2323
use crate::emm::range::{
24-
RangeType, ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT,
25-
PAGE_TYPE_MASK, PAGE_TYPE_SHIFT, RM,
24+
ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT, PAGE_TYPE_MASK,
25+
PAGE_TYPE_SHIFT,
2626
};
27-
use crate::emm::{rts_mm_commit, rts_mm_uncommit, PageInfo, PageType, ProtFlags};
27+
use crate::emm::{rts_mm_commit, rts_mm_uncommit, user_mm_alloc, PageInfo, PageType, ProtFlags};
2828
use crate::enclave::{self, is_within_enclave, MmLayout};
2929
use crate::error;
3030
use crate::rand::rand;
@@ -261,7 +261,7 @@ pub unsafe extern "C" fn sgx_mm_alloc(
261261
}
262262
} else {
263263
PageInfo {
264-
prot: ProtFlags::R | ProtFlags::W,
264+
prot: ProtFlags::RW,
265265
typ: page_type,
266266
}
267267
};
@@ -272,17 +272,10 @@ pub unsafe extern "C" fn sgx_mm_alloc(
272272
Some(priv_data)
273273
};
274274

275-
let mut range_manage = RM.get().unwrap().lock();
276-
match range_manage.alloc(
277-
Some(addr),
278-
size,
279-
alloc_flags,
280-
info,
281-
handler,
282-
priv_data,
283-
RangeType::User,
284-
Alloc::Reserve,
285-
) {
275+
let mut options = EmaOptions::new(addr, size, alloc_flags);
276+
options.info(info).handle(handler, priv_data);
277+
278+
match user_mm_alloc(&mut options) {
286279
Ok(base) => {
287280
*out_addr = base as *mut u8;
288281
0

0 commit comments

Comments
 (0)