CCF
Loading...
Searching...
No Matches
virtual_enclave.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#if !defined(PLATFORM_VIRTUAL) && !defined(PLATFORM_SNP)
6# error Should only be included in cchost builds with virtual support
7#endif
8
10#include "consensus_type.h"
11
12#include <dlfcn.h>
13#include <stdlib.h>
14#include <string.h>
15#include <wchar.h>
16
17template <typename T>
19 void* virtual_enclave_handle, const char* func_name)
20{
21 if (virtual_enclave_handle == nullptr)
22 {
23 throw std::logic_error(
24 "Cannot find symbol - library was not loaded correctly");
25 }
26
27 void* sym = dlsym(virtual_enclave_handle, func_name);
28 if (sym == nullptr)
29 {
30 throw std::logic_error(
31 fmt::format("Failed to find symbol: {}\n {}", func_name, dlerror()));
32 }
33 return (T)sym;
34}
35
36#ifndef PLATFORM_SGX
37// If this build does not also include OE definitions, then recreate them here.
38// It should not matter if these do not match precisely OE's, so long as they
39// can be used consistently by the virtual build.
40using oe_result_t = int;
41constexpr oe_result_t OE_OK = 0;
42constexpr oe_result_t OE_FAILURE = 1;
43
44using oe_enclave_t = void;
45using oe_log_level_t = size_t;
46
51
52# define oe_result_str(x) x
53#endif
54
55#ifdef __cplusplus
56extern "C"
57{
58#endif
59
60 typedef void (*oe_ocall_func_t)(
61 const uint8_t* input_buffer,
62 size_t input_buffer_size,
63 uint8_t* output_buffer,
64 size_t output_buffer_size,
65 size_t* output_bytes_written);
66
67 /*ocall function table*/
68 static oe_ocall_func_t __ccf_ocall_function_table[] = {nullptr};
69
70 inline void* load_virtual_enclave(const char* path)
71 {
72 auto virtual_enclave_handle = dlopen(
73 path,
74 RTLD_NOW
75#if defined(__has_feature)
76# if __has_feature(address_sanitizer)
77 // Avoid unloading on delete under ASAN, so that leak checking can still
78 // access symbols
79 | RTLD_NODELETE
80# endif
81#endif
82 );
83 if (virtual_enclave_handle == nullptr)
84 {
85 throw std::logic_error(
86 fmt::format("Could not load virtual enclave: {}", dlerror()));
87 }
88 return virtual_enclave_handle;
89 }
90
91 inline void terminate_virtual_enclave(void* handle)
92 {
93 auto err = dlclose(handle);
94 if (err != 0)
95 {
96 LOG_FAIL_FMT("Error while terminating virtual enclave: {}", dlerror());
97 }
98 }
99
101 void* virtual_enclave_handle,
102 CreateNodeStatus* status,
103 void* enclave_config,
104 uint8_t* ccf_config,
105 size_t ccf_config_size,
106 uint8_t* startup_snapshot,
107 size_t startup_snapshot_size,
108 uint8_t* node_cert,
109 size_t node_cert_size,
110 size_t* node_cert_len,
111 uint8_t* service_cert,
112 size_t service_cert_size,
113 size_t* service_cert_len,
114 uint8_t* enclave_version,
115 size_t enclave_version_size,
116 size_t* enclave_version_len,
117 StartType start_type,
118 LoggerLevel enclave_log_level,
119 size_t num_worker_thread,
120 void* time_location)
121 {
122 using create_node_func_t = CreateNodeStatus (*)(
123 void*,
124 uint8_t*,
125 size_t,
126 uint8_t*,
127 size_t,
128 uint8_t*,
129 size_t,
130 size_t*,
131 uint8_t*,
132 size_t,
133 size_t*,
134 uint8_t*,
135 size_t,
136 size_t*,
137 StartType,
139 size_t,
140 void*);
141
142 static create_node_func_t create_node_func =
143 get_enclave_exported_function<create_node_func_t>(
144 virtual_enclave_handle, "enclave_create_node");
145
146 *status = create_node_func(
147 enclave_config,
148 ccf_config,
149 ccf_config_size,
150 startup_snapshot,
151 startup_snapshot_size,
152 node_cert,
153 node_cert_size,
154 node_cert_len,
155 service_cert,
156 service_cert_size,
157 service_cert_len,
158 enclave_version,
159 enclave_version_size,
160 enclave_version_len,
161 start_type,
162 enclave_log_level,
163 num_worker_thread,
164 time_location);
165
166 // Only return OE_OK when the error isn't OE related
167 switch (*status)
168 {
173 return OE_FAILURE;
174 default:
175 return OE_OK;
176 }
177 }
178
179 inline oe_result_t virtual_run(void* virtual_enclave_handle, bool* _retval)
180 {
181 using run_func_t = bool (*)();
182
183 static run_func_t run_func = get_enclave_exported_function<run_func_t>(
184 virtual_enclave_handle, "enclave_run");
185
186 *_retval = run_func();
187 return *_retval ? OE_OK : OE_FAILURE;
188 }
189
190#ifdef __cplusplus
191}
192#endif
CreateNodeStatus
Definition enclave_interface_types.h:8
@ OEAttesterInitFailed
Definition enclave_interface_types.h:34
@ OEVerifierInitFailed
Definition enclave_interface_types.h:31
@ MemoryNotOutsideEnclave
Definition enclave_interface_types.h:25
@ EnclaveInitFailed
Definition enclave_interface_types.h:28
StartType
Definition enclave_interface_types.h:113
#define LOG_FAIL_FMT
Definition logger.h:396
LoggerLevel
Definition logger_level.h:6
void terminate_virtual_enclave(void *handle)
Definition virtual_enclave.h:91
void * load_virtual_enclave(const char *path)
Definition virtual_enclave.h:70
void oe_enclave_t
Definition virtual_enclave.h:44
void(* oe_ocall_func_t)(const uint8_t *input_buffer, size_t input_buffer_size, uint8_t *output_buffer, size_t output_buffer_size, size_t *output_bytes_written)
Definition virtual_enclave.h:60
oe_result_t virtual_run(void *virtual_enclave_handle, bool *_retval)
Definition virtual_enclave.h:179
oe_enclave_type_t
Definition virtual_enclave.h:48
@ OE_ENCLAVE_TYPE_SGX
Definition virtual_enclave.h:49
size_t oe_log_level_t
Definition virtual_enclave.h:45
oe_result_t virtual_create_node(void *virtual_enclave_handle, CreateNodeStatus *status, void *enclave_config, uint8_t *ccf_config, size_t ccf_config_size, uint8_t *startup_snapshot, size_t startup_snapshot_size, uint8_t *node_cert, size_t node_cert_size, size_t *node_cert_len, uint8_t *service_cert, size_t service_cert_size, size_t *service_cert_len, uint8_t *enclave_version, size_t enclave_version_size, size_t *enclave_version_len, StartType start_type, LoggerLevel enclave_log_level, size_t num_worker_thread, void *time_location)
Definition virtual_enclave.h:100
T get_enclave_exported_function(void *virtual_enclave_handle, const char *func_name)
Definition virtual_enclave.h:18
constexpr oe_result_t OE_OK
Definition virtual_enclave.h:41
int oe_result_t
Definition virtual_enclave.h:40
constexpr oe_result_t OE_FAILURE
Definition virtual_enclave.h:42