123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #include <string.h>
- #include <assert.h>
- #include <fstream>
- #include <thread>
- #include <iostream>
- #include "Enclave_u.h"
- #include "sgx_urts.h"
- #include "sgx_tseal.h"
- #include "rwlock.h"
- #include "ErrorSupport.h"
- #define ENCLAVE_NAME "libenclave.signed.so"
- #define TOKEN_NAME "Enclave.token"
- #define THREAD_NUM 3
- sgx_enclave_id_t global_eid = 0;
- sgx_launch_token_t token = {0};
- rwlock_t lock_eid;
- struct sealed_buf_t sealed_buf;
- using namespace std;
- void print(const char *str)
- {
- cout<<str;
- }
- sgx_status_t load_and_initialize_enclave(sgx_enclave_id_t *eid, struct sealed_buf_t *sealed_buf)
- {
- sgx_status_t ret = SGX_SUCCESS;
- int retval = 0;
- int updated = 0;
- for( ; ; )
- {
-
-
- if(*eid != 0)
- {
- sgx_destroy_enclave(*eid);
- }
-
-
-
- ret = sgx_create_enclave(ENCLAVE_NAME, SGX_DEBUG_FLAG, &token, &updated, eid, NULL);
- if(ret != SGX_SUCCESS)
- return ret;
-
- if(updated == 1)
- {
- ofstream ofs(TOKEN_NAME, std::ios::binary|std::ios::out);
- if(!ofs.good())
- {
- cout<< "Warning: Failed to save the launch token to \"" <<TOKEN_NAME <<"\""<<endl;
- }
- else
- ofs << token;
- }
-
-
-
-
- ret = initialize_enclave(*eid, &retval, sealed_buf);
- if(ret == SGX_ERROR_ENCLAVE_LOST)
- {
- cout<<"Power transition occured in initialize_enclave()" <<endl;
- continue;
- }
- else
- {
-
-
- if(ret == SGX_SUCCESS && retval != 0)
- {
- ret = SGX_ERROR_UNEXPECTED;
- sgx_destroy_enclave(*eid);
- }
- break;
- }
- }
- return ret;
- }
- bool increase_and_seal_data_in_enclave()
- {
- size_t thread_id = std::hash<std::thread::id>()(std::this_thread::get_id());
- sgx_status_t ret = SGX_SUCCESS;
- int retval = 0;
- sgx_enclave_id_t current_eid = 0;
-
- for(unsigned int i = 0; i< 50000; i++)
- {
- for( ; ; )
- {
-
-
-
-
- rdlock(&lock_eid);
- current_eid = global_eid;
- rdunlock(&lock_eid);
- ret = increase_and_seal_data(current_eid, &retval, thread_id, &sealed_buf);
- if(ret == SGX_ERROR_ENCLAVE_LOST)
- {
-
-
- wtlock(&lock_eid);
-
- if(current_eid == global_eid)
- {
- cout <<"power transition occured in increase_and_seal_data()." << endl;
-
- if((ret = load_and_initialize_enclave(¤t_eid, &sealed_buf)) != SGX_SUCCESS)
- {
- ret_error_support(ret);
- wtunlock(&lock_eid);
- return false;
- }
- else
- {
-
- global_eid = current_eid;
- }
- }
- else
- {
-
-
- current_eid = global_eid;
- }
- wtunlock(&lock_eid);
- }
- else
- {
-
- break;
- }
- }
- if(ret != SGX_SUCCESS)
- {
- ret_error_support(ret);
- return false;
- }
- else if(retval != 0)
- {
- return false;
- }
- }
- return true;
- }
- void thread_func()
- {
- if(increase_and_seal_data_in_enclave() != true)
- {
- abort();
- }
- }
- bool set_global_data()
- {
-
- init_rwlock(&lock_eid);
-
-
- ifstream ifs(TOKEN_NAME, std::ios::binary | std::ios::in);
- if(!ifs.good())
- {
- memset(token, 0, sizeof(sgx_launch_token_t));
- }
- else
- {
- ifs.read(reinterpret_cast<char *>(&token), sizeof(sgx_launch_token_t));
- if(ifs.fail())
- {
- memset(&token, 0, sizeof(sgx_launch_token_t));
- }
- }
-
- uint32_t sealed_len = sizeof(sgx_sealed_data_t) + sizeof(uint32_t);
- for(int i = 0; i < BUF_NUM; i++)
- {
- sealed_buf.sealed_buf_ptr[i] = (uint8_t *)malloc(sealed_len);
- if(sealed_buf.sealed_buf_ptr[i] == NULL)
- {
- cout << "Out of memory" << endl;
- return false;
- }
- memset(sealed_buf.sealed_buf_ptr[i], 0, sealed_len);
- }
- sealed_buf.index = 0;
- return true;
- }
- void release_source()
- {
- for(int i = 0; i < BUF_NUM; i++)
- {
- if(sealed_buf.sealed_buf_ptr[i] != NULL)
- {
- free(sealed_buf.sealed_buf_ptr[i]);
- sealed_buf.sealed_buf_ptr[i] = NULL;
- }
- }
- fini_rwlock(&lock_eid);
- return;
- }
- int main(int argc, char* argv[])
- {
- (void)argc, (void)argv;
-
- if(!set_global_data())
- {
- release_source();
- cout << "Enter a character before exit ..." << endl;
- getchar();
- return -1;
- }
-
-
- sgx_status_t ret = load_and_initialize_enclave(&global_eid , NULL);
- if(ret != SGX_SUCCESS)
- {
- ret_error_support(ret);
- release_source();
- cout << "Enter a character before exit ..." << endl;
- getchar();
- return -1;
- }
- cout << "****************************************************************" << endl;
- cout << "Demonstrating Power transition needs your cooperation." << endl
- << "Please take the following actions:" << endl
- << " 1. Enter a character;" << endl
- << " 2. Manually put the OS into a sleep or hibernate state;" << endl
- << " 3. Resume the OS from that state;" << endl
- << "Then you will see the application continues." << endl;
- cout << "****************************************************************" << endl;
- cout << "Now enter a character ...";
- getchar();
-
- thread trd[THREAD_NUM];
- for (int i = 0; i< THREAD_NUM; i++)
- {
- trd[i] = thread(thread_func);
- }
- for (int i = 0; i < THREAD_NUM; i++)
- {
- trd[i].join();
- }
-
- release_source();
-
- sgx_destroy_enclave(global_eid);
- cout << "Enter a character before exit ..." << endl;
- getchar();
- return 0;
- }
|