2

I have a vector<vector <string>> a; How could I pass it to the enclave? How I declare edl function. A sample function declaration for the app, edl and enclave is much appreciated.

I am aware of this: C++ Arguments to SGX Enclave Edge Functions.

A sample to pass even a vector<string> is ok for me.

update1: I came up with this:

App.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);
};

enclave

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

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

}

But enclave does not receive any data, the program compiles perfectly with no error. Could anyone help? The printf is the sample ocall.

4

1 回答 1

3

在 EDL 中使用count而不是size.

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

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

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

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

在您的情况下,您仅提供了size. 由于您没有提供调用者代码,我假设您正在传递字符串的长度,并且count未指定它默认为1. 那么总字节数,根据哪个Total number of bytes = count * size1 * len错误的。

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

要关闭,一旦进入 Enclave,您需要复制指针的数据,因为这些指针位于 enclave 之外,这可以通过将它们分配给std::string.


来自英特尔 SGX SDK 文档:

指针处理最后一段

您可以使用方向属性来换取性能保护。否则,您必须使用user_check下面描述的属性并在使用它之前验证通过指针从不受信任的内存中获取的数据,因为指针指向的内存可能会意外更改,因为它存储在不受信任的内存中。但是,方向属性对包含指针的结构没有帮助。在这种情况下,开发人员必须自己验证和复制缓冲区内容,如果需要,可以递归地进行

和,

缓冲区大小计算

使用这些属性计算缓冲区大小的通用公式:

Total number of bytes = count * size

  • 当同时指定count和时,上述公式成立size/sizefunc
  • size可以由其中一个sizesizefunc属性指定。
  • 如果count没有为指针参数指定,则假定它等于1,即count=1。那么总字节数等于size/sizefunc
  • 如果size未指定,则使用上述公式计算缓冲区大小,其中sizeis sizeof (element pointed by the pointer)
于 2018-02-27T14:06:37.480 回答