热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【Coding】——SGXSDK_SampleEncalve

文章目录AppTrustedLibraryEnclaveTrustedLibraryInclude运行AppApp.cpp这个文件中只对官方文档中main函数进行了少部分更改*


文章目录

      • App
        • TrustedLibrary
      • Enclave
        • TrustedLibrary
      • Include
      • 运行


App

//App.cpp这个文件中只对官方文档中main函数进行了少部分更改
/* Application entry */
int SGX_CDECL main(int argc, char *argv[]){(void)(argc);(void)(argv);/* Initialize the enclave */if(initialize_enclave() < 0){printf("Enter a character before exit ...\n");getchar();return -1; }ecall_printf(global_eid,"ecall,helloworld\n");/* Destroy the enclave */ecall_thread_functions();sgx_destroy_enclave(global_eid);printf("Info: SampleEnclave successfully returned.\n");printf("Enter a character before exit ...\n");getchar();return 0;
}

//App.h#ifndef _APP_H_
#define _APP_H_#include
#include
#include
#include #include "sgx_error.h" /* sgx_status_t */
#include "sgx_eid.h" /* sgx_enclave_id_t */#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
# define TOKEN_FILENAME "enclave.token"
# define ENCLAVE_FILENAME "enclave.signed.so"extern sgx_enclave_id_t global_eid; /* global enclave id */#if defined(__cplusplus)
extern "C"{
#endif
void ecall_thread_functions(void);
#if defined(__cplusplus)
}
#endif
#endif /* !_APP_H_ */

TrustedLibrary

Thread.cpp文件为调用Encalve中一些ECALL的接口函数&#xff0c;调用该函数即可调用对应的ECALL&#xff0c;
global_eid在App.h中设置为全局变量&#xff0c;在这里添加上该头文件即可使用该变量

#include
#include
using namespace std;
#include "../App.h"
#include "Enclave_u.h"static size_t counter &#61; 0;
void increase_counter(void){size_t cnr &#61; 0;sgx_status_t ret &#61; SGX_ERROR_UNEXPECTED;ret &#61; ecall_increase_counter(global_eid, &cnr);if (cnr !&#61; 0) counter &#61; cnr; if (ret !&#61; SGX_SUCCESS)abort();
}void data_producer(void){sgx_status_t ret &#61; SGX_ERROR_UNEXPECTED;ret &#61; ecall_producer(global_eid);if (ret !&#61; SGX_SUCCESS)abort();
}void data_consumer(void){sgx_status_t ret &#61; SGX_ERROR_UNEXPECTED;ret &#61; ecall_consumer(global_eid);if (ret !&#61; SGX_SUCCESS)abort();
}
void ecall_thread_functions(void){thread adder1(increase_counter);thread adder2(increase_counter);thread adder3(increase_counter);thread adder4(increase_counter);adder1.join();adder2.join();adder3.join();adder4.join();assert(counter &#61;&#61; 4*LOOPS_PER_THREAD);printf("Info: executing thread synchronization, please wait... \n");thread consumer1(data_consumer);thread producer0(data_producer);thread consumer2(data_consumer);thread consumer3(data_consumer);thread consumer4(data_consumer);consumer1.join();consumer2.join();consumer3.join();consumer4.join();producer0.join();
}

Enclave

//Enclave.cpp
#include "Enclave.h"
#include "Enclave_t.h" /* print_string */
#include
#include /* vsnprintf */
#include
int printf(const char* fmt, ...){char buf[BUFSIZ] &#61; { &#39;\0&#39; };va_list ap;va_start(ap, fmt);vsnprintf(buf, BUFSIZ, fmt, ap);va_end(ap);ocall_print_string(buf);return (int)strnlen(buf, BUFSIZ - 1) &#43; 1;
}
void ecall_printf(const char * str){ocall_print_string(str);//这里可以调用ocall&#xff0c;也可以调用上面的printf
}

//enclave.h#ifndef _ENCLAVE_H_
#define _ENCLAVE_H_
#include
#include
#if defined(__cplusplus)
extern "C" {
#endif
int printf(const char* fmt, ...);
#if defined(__cplusplus)
}
#endif
#endif /* !_ENCLAVE_H_ */

//enclave.edl
enclave {include "user_types.h"//需要导入对应头文件&#xff0c;其中包含了Thread.cpp中要使用的自定义变量&#xff0c;也可以将其中的变量添加到对应的文件中&#xff0c;而非包含在这个头文件中from "TrustedLibrary/Thread.edl" import *;trusted{public void ecall_printf([in,string] const char * str);};untrusted {void ocall_print_string([in, string] const char *str);};};

TrustedLibrary

供外部调用的ECALL函数的实现&#xff0c;要实现线程功能&#xff0c;需要在edl中引入一些函数&#xff0c;在开发文档中有相应描述

//Thread.cpp
#include "../Enclave.h"
#include "Enclave_t.h"
#include "sgx_thread.h"static size_t global_counter &#61; 0;
static sgx_thread_mutex_t global_mutex &#61; SGX_THREAD_MUTEX_INITIALIZER;#define BUFFER_SIZE 50typedef struct {int buf[BUFFER_SIZE];int occupied;int nextin;int nextout;sgx_thread_mutex_t mutex;sgx_thread_cond_t more;sgx_thread_cond_t less;
} cond_buffer_t;static cond_buffer_t buffer &#61; {{0, 0, 0, 0, 0, 0}, 0, 0, 0,SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_COND_INITIALIZER, SGX_THREAD_COND_INITIALIZER};/** ecall_increase_counter:* Utilize thread APIs inside the enclave.*/
size_t ecall_increase_counter(void){size_t ret &#61; 0;for (int i &#61; 0; i < LOOPS_PER_THREAD; i&#43;&#43;) {sgx_thread_mutex_lock(&global_mutex);/* mutually exclusive adding */size_t tmp &#61; global_counter;global_counter &#61; &#43;&#43;tmp;if (4*LOOPS_PER_THREAD &#61;&#61; global_counter)ret &#61; global_counter;sgx_thread_mutex_unlock(&global_mutex);}return ret;
}void ecall_producer(void){for (int i &#61; 0; i < 4*LOOPS_PER_THREAD; i&#43;&#43;) {cond_buffer_t *b &#61; &buffer;sgx_thread_mutex_lock(&b->mutex);while (b->occupied >&#61; BUFFER_SIZE)sgx_thread_cond_wait(&b->less, &b->mutex);b->buf[b->nextin] &#61; b->nextin;b->nextin&#43;&#43;;b->nextin %&#61; BUFFER_SIZE;b->occupied&#43;&#43;;sgx_thread_cond_signal(&b->more);sgx_thread_mutex_unlock(&b->mutex);}
}void ecall_consumer(void){for (int i &#61; 0; i < LOOPS_PER_THREAD; i&#43;&#43;) {cond_buffer_t *b &#61; &buffer;sgx_thread_mutex_lock(&b->mutex);while(b->occupied <&#61; 0)sgx_thread_cond_wait(&b->more, &b->mutex);b->buf[b->nextout&#43;&#43;] &#61; 0;b->nextout %&#61; BUFFER_SIZE;b->occupied--;sgx_thread_cond_signal(&b->less);sgx_thread_mutex_unlock(&b->mutex);}
}

edl文件中引入对应的可信sgx接口函数

//Thread.edl
enclave {from "sgx_tstdc.edl" import sgx_thread_wait_untrusted_event_ocall, sgx_thread_set_untrusted_event_ocall, sgx_thread_setwait_untrusted_events_ocall, sgx_thread_set_multiple_untrusted_events_ocall;trusted {/** Use SGX mutex.*/public size_t ecall_increase_counter();/** Use SGX condition variables.*/public void ecall_producer();public void ecall_consumer();};
};

Include

用户自定义的类型&#xff0c;在这里进行包含&#xff0c;直接在enclave.edl中进行引入即可

//user_types.h
#define LOOPS_PER_THREAD 500

运行

在这里插入图片描述


推荐阅读
author-avatar
一加一等于贰_661
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有