3

我很难让我的自定义异常处理程序工作。

这是飞地代码:

#include "Enclave_DivideZero_t.h"
#include "sgx_trts_exception.h"
#include "sgx_trts.h"
#include <string>

static char buf[200] = "Handler not called";

int divide_by_zero_handler(sgx_exception_info_t* info) {

    buf[0] = '1';
    return EXCEPTION_CONTINUE_EXECUTION;
}

void Enclave_DivideByZero() {
    Ocall_printf(buf);
    if (sgx_register_exception_handler(1, divide_by_zero_handler) == NULL) {
        Ocall_printf("register failed");
    } else {
        Ocall_printf("register success");
    }
    int a(1);
    int b(3/(a-a));
    (void) a;
    (void) b;
    Ocall_printf(buf);
}

我们用作buf指示处理程序是否已实际执行。但是,输出是这样的:

Enclave created!
[Ocall printf] - Handler not called
[Ocall printf] - register success
[Ocall printf] - Handler not called <-- should be: "1andler not called" ("1andler" instead of "Handler")

另外,这里是App代码(即不受信任的代码)

#include "stdafx.h"
#include "sgx_urts.h"
#include "Enclave_DivideZero_u.h"
#define ENCLAVE_FILE _T("Enclave_DivideZero.signed.dll")
sgx_status_t createEnclave(sgx_enclave_id_t *eid) {
    sgx_status_t        ret   = SGX_SUCCESS;
    sgx_launch_token_t  token = {0};
    int                 updated = 0;    
    ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token, &updated, eid, NULL);
    return ret;
}
void Ocall_printf( char* str) {
    printf("[Ocall printf] - %s\n", str);
}
int _tmain(int argc, _TCHAR* argv[]) {
    sgx_enclave_id_t eid;
    sgx_status_t res = createEnclave(&eid);
    if (res != SGX_SUCCESS) {
        printf("App: error-, failed to create enclave.\n");
        return -1;
    } else {
        printf("Enclave created!\n");
    }
    Enclave_DivideByZero(eid);
    return 0;
}

问题:从输出中可以看出,处理程序注册成功,但没有执行。

1-为什么注册不起作用?

2-我尝试使用App代码进行注册,为此我必须在 中添加处理程序edl,问题是传递sgx_exception_info_t *info参数,不清楚它需要哪些标志(即[in, .. <'flags'>])。那么,在 Enclave 内部定义它是否正确?

ps 我用PrereleaseMode 运行代码。

[编辑]我在这里记录了我在 SGX 上执行的项目 + 代码

4

1 回答 1

0

所以这个问题现在有点老了,但我被困在同一个地方,所以也许有人仍然可以使用我的洞察力。

在 trts_veh.cpp 中,检查是否处理了异常,它在注释中说“将再次执行触发异常的指令”。正如您自己注意到的那样,这会导致异常处理循环。

但是,您可以自己增加 rip,因为它存储在指向 sgx_exception_info_t 的指针中,方法是使用“info->cpu_context.rip += 2;”。在您的异常处理程序中添加它应该可以解决问题。

于 2021-03-31T16:42:58.310 回答