Skip to content

Instantly share code, notes, and snippets.

@lengyijun
Created August 22, 2020 03:25
Show Gist options
  • Select an option

  • Save lengyijun/67defcb5a2e242dbbdabfe06f9f96d6b to your computer and use it in GitHub Desktop.

Select an option

Save lengyijun/67defcb5a2e242dbbdabfe06f9f96d6b to your computer and use it in GitHub Desktop.
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License..
#![crate_name = "filesampleenclave"]
#![crate_type = "staticlib"]
#![cfg_attr(not(target_env = "sgx"), no_std)]
#![cfg_attr(target_env = "sgx", feature(rustc_private))]
#[cfg(not(target_env = "sgx"))]
#[macro_use]
extern crate sgx_tstd as std;
extern crate sgx_rand;
#[macro_use]
extern crate sgx_rand_derive;
extern crate sgx_serialize;
#[macro_use]
extern crate sgx_serialize_derive;
use std::sgxfs::SgxFile;
use std::io::{Read, Write};
use sgx_serialize::{SerializeHelper, DeSerializeHelper};
#[derive(Copy, Clone, Default, Debug, Serializable, DeSerializable, Rand)]
struct RandData {
key: u32,
rand: u32,
}
#[no_mangle]
pub extern "C" fn write_file() -> i32 {
let rand = sgx_rand::random::<RandData>();
let helper = SerializeHelper::new();
let data = match helper.encode(rand) {
Some(d) => d,
None => {
println!("encode data failed.");
return 1;
},
};
let mut file = match SgxFile::create("sgx_file") {
Ok(f) => f,
Err(_) => {
println!("SgxFile::create failed.");
return 2;
},
};
let write_size = match file.write(data.as_slice()) {
Ok(len) => len,
Err(_) => {
println!("SgxFile::write failed.");
return 3;
},
};
println!("write file success, write size: {}, {:?}.", write_size, rand);
0
}
#[no_mangle]
pub extern "C" fn read_file() -> i32 {
let a1=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.term" ).unwrap();
let a2=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.store" ).unwrap();
let a3=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.idx" ).unwrap();
let a4=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.pos" ).unwrap();
let a5=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.posidx" ).unwrap();
let a6=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.fast" ).unwrap();
let a7=std::sgxfs::read( "20a0074dfedd47d18fe7bf3073a8e997.fieldnorm" ).unwrap();
let b1=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.term" ).unwrap();
println!("b1");
let b2=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.store" ).unwrap();
println!("b2");
let b3=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.idx" ).unwrap();
println!("b3");
let b4=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.pos" ).unwrap();
println!("b4");
let b5=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.posidx" ).unwrap();
println!("b5");
let b6=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.fast" ).unwrap();
println!("b6");
let b7=std::sgxfs::read( "b5fc089a7af64244965d34cc054bf899.fieldnorm" ).unwrap();
println!("b7");
0
}
@lengyijun

Copy link
Copy Markdown
Author
[+] global_eid: 2
write file success, write size: 10, RandData { key: 1934764888, rand: 3771513708 }.
write_file success ...
b1
b2
b3
memory allocation of 1073741824 bytes failed[1]    18588 illegal hardware instruction  ./app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment