Skip to content

Instantly share code, notes, and snippets.

@kbagher
Last active April 4, 2023 09:09
Show Gist options
  • Save kbagher/e20a46773a772ec57c5022ef40d01f24 to your computer and use it in GitHub Desktop.
Save kbagher/e20a46773a772ec57c5022ef40d01f24 to your computer and use it in GitHub Desktop.
Passing callback function pointer to an enclave (Intel SGX)
void CallMe(int x) {
printf("Num %d\n",x);
}
int main() {
/* Setup enclave */
sgx_enclave_id_t eid;
sgx_status_t ret;
sgx_launch_token_t token = { 0 };
int token_updated = 0;
ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token,
&token_updated, &eid, NULL);
if (ret != SGX_SUCCESS) {
cout << "sgx_create_enclave failed: 0x" << std::hex << ret << endl;
return 1;
}
FunctionPointer ptr = { &CallMe };
ecall_pass_func(eid, &ptr);
sgx_destroy_enclave(eid);
}
void ecall_pass_func(FunctionPointer *ptr){
ptr->ptr(10);
}
enclave {
include "user_type.h"
trusted {
public void ecall_pass_func([user_check] FunctionPointer *ptr);
};
untrusted {
};
};
struct FunctionPointer{
void (*ptr)(int);
};
typedef struct FunctionPointer FunctionPointer;
@kbagher
Copy link
Author

kbagher commented Mar 26, 2020

This approach won’t work if you pass class member function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment