CCF
Loading...
Searching...
No Matches
enclave_time.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#include "ccf/ds/logger.h"
6
7#include <atomic>
8#include <chrono>
9
10namespace ccf
11{
12 namespace enclavetime
13 {
14 extern std::atomic<long long>* host_time_us;
15 extern std::atomic<std::chrono::microseconds> last_value;
16 }
17
18 static std::chrono::microseconds get_enclave_time()
19 {
20 auto last = enclavetime::last_value.load();
21
22 // Update cached value if possible, but never move backwards
23 if (enclavetime::host_time_us != nullptr)
24 {
25 const auto current_time = enclavetime::host_time_us->load();
26 if (current_time >= last.count())
27 {
28 // If this fails, it simply means another thread has fetched and updated
29 // the in-enclave last_value independently. Both are happy that their
30 // values do not decrease time, so either may succeed.
31 enclavetime::last_value.compare_exchange_weak(
32 last, std::chrono::microseconds(current_time));
33 }
34 else
35 {
37 "Host attempting to move enclave time backwards! Last value was {}, "
38 "now {}",
39 last.count(),
40 current_time);
41 }
42 }
43
44 return last;
45 }
46}
#define LOG_FAIL_FMT
Definition logger.h:396
std::atomic< long long > * host_time_us
Definition enclave_time.cpp:7
std::atomic< std::chrono::microseconds > last_value
Definition app_interface.h:15