c++ - 将 vector 传递到 Intel SGX 中的 enclave

标签 c++ c sgx

我有一个vector<vector <string>> a ;我怎样才能将它传递到飞地?我如何声明 edl 函数。 非常感谢应用程序、edl 和 enclave 的示例函数声明。

我知道这一点:C++ Arguments to SGX Enclave Edge Functions .

甚至可以通过 vector<string> 的样本对我来说没问题。

更新1: 我想出了这个:

应用程序.cpp

const char *convert(const std::string & s)
{
   return s.c_str();
}

vector<string> members_data;
member_data.push_back("apple");
member_data.push_back("orange"); //just for sample

    std::vector<const char*>  vc;
    std::transform(members_data.begin(), members_data.end(), std::back_inserter(vc), convert);

edl:

trusted {
       public void ecall_receive_vector([in, size=len] const char **arr, size_t len);
};

飞地

void ecall_receive_vector(const char *arr[], size_t len)
{
    vector<string> v(arr, arr+len);

    printf("%s\n", v[2].c_str());

}

但是enclave没有接收到任何数据,程序编译完美,没有错误。有人可以帮忙吗? printf 是示例 ocall。

最佳答案

在 EDL 中使用 count而不是size .

trusted {
    public void ecall_receive_vector([in, count=len] const char **arr, size_t len);
};

您正在传递一个双指针,它是一个指向 char ( char ** ) 的指针。

在编码/解码指针时,EDL 处理器仅处理(复制并验证输入和输出)第一级间接,由开发人员负责处理其他间接级别。因此,对于指针数组,它只会复制第一个指针数组,而不是指向的值,复制它们是开发人员的责任。

如果未指定countsize默认为1sizeof(<pointed-type>)分别。在你的情况下size = sizeof(<pointer>)在大多数平台上是 4 .

就您而言,您仅提供了 size 。由于您没有提供调用者代码,我假设您正在传递字符串的长度,如 count未指定,默认为 1 。然后是总字节数,基于Total number of bytes = count * size将是1 * len这是错误的。

仅使用 count会让size默认为sizeof(<pointed-type>) ,然后Total number of bytes = count * size将是count * sizeof(<pointed-type>) ,这是正确的,因为您正在传递一个指针数组。

要关闭,一旦进入飞地,您需要复制指针的数据,因为这些指针驻留在飞地之外,这可以通过将它们分配给 std::string 自动完成。 .


来自英特尔 SGX SDK 文档:

Pointer Handling (the last paragraph)

You may use the direction attribute to trade protection for performance. Otherwise, you must use the user_check attribute described below and validate the data obtained from untrusted memory via pointers before using it, since the memory a pointer points to could change unexpectedly because it is stored in untrusted memory. However, the direction attribute does not help with structures that contain pointers. In this scenario, developers have to validate and copy the buffer contents, recursively if needed, themselves.

并且,

Buffer Size Calculation

The generalized formula for calculating the buffer size using these attributes:

Total number of bytes = count * size

  • The above formula holds when both count and size/sizefunc are specified.
  • size can be specified by either size or sizefunc attribute.
  • If count is not specified for the pointer parameter, then it is assumed to be equal to 1, i.e., count=1. Then total number of bytes equals to size/sizefunc.
  • If size is not specified, then the buffer size is calculated using the above formula where size is sizeof (element pointed by the pointer).

关于c++ - 将 vector 传递到 Intel SGX 中的 enclave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871693/

相关文章:

c# - 如何在 C# 中使用 Private Inheritance aka C++ 以及为什么它不存在于 C# 中

c++ - OpenCL 结构体值在 CPU 上正确,但在 GPU 上不正确

c - 按值将数组传递给递归函数可能吗?

c - 在链表中添加冗余节点

intel - Intel SGX 是否在模拟模式下提供软件内存保护?

适用于 Linux 的英特尔 SGX 模拟器

c++ - 沿任一方向遍历容器并删除元素

c++ - 了解 union 的内存内容

c - 出现错误时安全退出到特定状态

c++ - 如何在套接字编程C++中使用snprintf的结果并将其从服务器发送到客户端