热门标签 | 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

运行

在这里插入图片描述


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
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社区 版权所有