作者:一加一等于贰_661 | 来源:互联网 | 2023-09-04 18:23
文章目录AppTrustedLibraryEnclaveTrustedLibraryInclude运行AppApp.cpp这个文件中只对官方文档中main函数进行了少部分更改*
App int SGX_CDECL main ( int argc, char * argv[ ] ) { ( void ) ( argc) ; ( void ) ( argv) ; if ( initialize_enclave ( ) < 0 ) { printf ( "Enter a character before exit ...\n" ) ; getchar ( ) ; return - 1 ; } ecall_printf ( global_eid, "ecall,helloworld\n" ) ; ecall_thread_functions ( ) ; sgx_destroy_enclave ( global_eid) ; printf ( "Info: SampleEnclave successfully returned.\n" ) ; printf ( "Enter a character before exit ...\n" ) ; getchar ( ) ; return 0 ; }
# ifndef _APP_H_ # define _APP_H_ # include # include # include # include # include "sgx_error.h" # include "sgx_eid.h" # 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; # if defined ( __cplusplus) extern "C" { # endif void ecall_thread_functions ( void ) ; # if defined ( __cplusplus) } # endif # endif
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 # include "Enclave.h" # include "Enclave_t.h" # include # include # 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) ; }
# 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 { include "user_types.h" 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;在开发文档中有相应描述
# 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 50 typedef 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} ; 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) ; 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接口函数
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 { public size_t ecall_increase_counter ( ) ; public void ecall_producer ( ) ; public void ecall_consumer ( ) ; } ; } ;
Include 用户自定义的类型&#xff0c;在这里进行包含&#xff0c;直接在enclave.edl中进行引入即可
# define LOOPS_PER_THREAD 500
运行