我有一个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 处理器仅处理(复制并验证输入和输出)第一级间接,由开发人员负责处理其他间接级别。因此,对于指针数组,它只会复制第一个指针数组,而不是指向的值,复制它们是开发人员的责任。
如果未指定count
和size
默认为1
和sizeof(<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.
并且,
The generalized formula for calculating the buffer size using these attributes:
Total number of bytes = count * size
- The above formula holds when both
count
andsize/sizefunc
are specified.size
can be specified by eithersize
orsizefunc
attribute.- If
count
is not specified for the pointer parameter, then it is assumed to be equal to1
, i.e.,count=1
. Then total number of bytes equals tosize/sizefunc
.- If
size
is not specified, then the buffer size is calculated using the above formula wheresize
issizeof (element pointed by the pointer)
.
关于c++ - 将 vector 传递到 Intel SGX 中的 enclave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871693/