将向量传递到英特尔 SGX 中的飞地
Passing vector to enclave in Intel SGX
我有一个vector<vector <string>> a
;我怎么能把它传递给飞地?我如何声明 edl 函数。
非常感谢应用程序、edl 和 enclave 的示例函数声明。
我知道这个:。
一个样本甚至通过 vector<string>
对我来说都可以。
更新1:
我想到了这个:
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);
};
飞地
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 **
) 的指针。
虽然 marshaling/unmarshaling 指针,但 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>)
,这是正确的,因为你传递了一个指针数组。
要关闭,一旦进入 Enclave,您需要复制指针的数据,因为这些指针位于 enclave 之外,这可以通过将它们分配给 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
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)
.
我有一个vector<vector <string>> a
;我怎么能把它传递给飞地?我如何声明 edl 函数。
非常感谢应用程序、edl 和 enclave 的示例函数声明。
我知道这个:
一个样本甚至通过 vector<string>
对我来说都可以。
更新1: 我想到了这个:
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);
};
飞地
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 **
) 的指针。
虽然 marshaling/unmarshaling 指针,但 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>)
,这是正确的,因为你传递了一个指针数组。
要关闭,一旦进入 Enclave,您需要复制指针的数据,因为这些指针位于 enclave 之外,这可以通过将它们分配给 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)
.