diff --git a/asylo/distrib/toolchain/newlib-2.5.0.20170922.patch b/asylo/distrib/toolchain/newlib-2.5.0.20170922.patch index 32a5b1c73e..3a07f56be9 100644 --- a/asylo/distrib/toolchain/newlib-2.5.0.20170922.patch +++ b/asylo/distrib/toolchain/newlib-2.5.0.20170922.patch @@ -5046,7 +5046,7 @@ diff -Naur ../newlib-2.5.0.20170922/newlib/libc/include/stdlib.h ./newlib/libc/i diff -Naur ../newlib-2.5.0.20170922/newlib/libc/include/sys/features.h ./newlib/libc/include/sys/features.h --- ../newlib-2.5.0.20170922/newlib/libc/include/sys/features.h +++ ./newlib/libc/include/sys/features.h -@@ -384,6 +384,15 @@ +@@ -384,6 +384,17 @@ # define _POSIX_VERSION 199009L #endif @@ -5055,6 +5055,8 @@ diff -Naur ../newlib-2.5.0.20170922/newlib/libc/include/sys/features.h ./newlib/ +#define _POSIX_TIMERS 1 +#define _POSIX_THREADS 1 +#define _POSIX_MONOTONIC_CLOCK 200112L ++#define _POSIX_CPUTIME 200112L ++#define _POSIX_THREAD_CPUTIME 200112L +#define _POSIX_READER_WRITER_LOCKS 200112L +#define _UNIX98_THREAD_MUTEX_ATTRIBUTES 1 +#endif @@ -5185,6 +5187,25 @@ diff -Naur ../newlib-2.5.0.20170922/newlib/libc/include/sys/_types.h ./newlib/li typedef long __suseconds_t; /* microseconds (signed) */ typedef unsigned long __useconds_t; /* microseconds (unsigned) */ +diff -Naur ../newlib-2.5.0.20170922/newlib/libc/include/time.h ./newlib/libc/include/time.h +--- ../newlib-2.5.0.20170922/newlib/libc/include/time.h ++++ ./newlib/libc/include/time.h +@@ -301,15 +301,6 @@ + + #endif /* _POSIX_CPUTIME */ + +-#if defined(_POSIX_CPUTIME) || defined(_POSIX_THREAD_CPUTIME) +- +-/* CPU-time Clock Attribute Access, P1003.4b/D8, p. 56 */ +- +-int _EXFUN(clock_setenable_attr, (clockid_t clock_id, int attr)); +-int _EXFUN(clock_getenable_attr, (clockid_t clock_id, int *attr)); +- +-#endif /* _POSIX_CPUTIME or _POSIX_THREAD_CPUTIME */ +- + #ifdef __cplusplus + } + #endif diff -Naur ../newlib-2.5.0.20170922/newlib/libc/locale/lctype.c ./newlib/libc/locale/lctype.c --- ../newlib-2.5.0.20170922/newlib/libc/locale/lctype.c +++ ./newlib/libc/locale/lctype.c diff --git a/asylo/platform/host_call/exit_handler_constants.h b/asylo/platform/host_call/exit_handler_constants.h index 18358f1c59..3947752af0 100644 --- a/asylo/platform/host_call/exit_handler_constants.h +++ b/asylo/platform/host_call/exit_handler_constants.h @@ -106,6 +106,10 @@ static constexpr uint64_t kIfIndexToNameHandler = static constexpr uint64_t kGetIfAddrsHandler = primitives::kSelectorHostCall + 21; +// Exit handler constant for |GetCpuClockIdHandler|. +static constexpr uint64_t kGetCpuClockIdHandler = + primitives::kSelectorHostCall + 22; + } // namespace host_call } // namespace asylo diff --git a/asylo/platform/host_call/trusted/host_calls.cc b/asylo/platform/host_call/trusted/host_calls.cc index 0c8415bb07..5559b80064 100644 --- a/asylo/platform/host_call/trusted/host_calls.cc +++ b/asylo/platform/host_call/trusted/host_calls.cc @@ -565,6 +565,29 @@ int enc_untrusted_clock_gettime(clockid_t clk_id, struct timespec *tp) { return result; } +int enc_untrusted_clock_getcpuclockid(pid_t pid, clockid_t *clock_id) { + MessageWriter input; + input.Push(pid); + MessageReader output; + asylo::primitives::PrimitiveStatus status = + asylo::host_call::NonSystemCallDispatcher( + asylo::host_call::kGetCpuClockIdHandler, &input, &output); + if (!status.ok()) { + TrustedPrimitives::BestEffortAbort( + "enc_untrusted_clock_getcpuclockid failed"); + } + // clock_getcpuclockid returns an errno value directly, without setting errno. + // The value must still be translated in order to be interpreted. + int klinux_errno_result = output.next(); + if (klinux_errno_result != 0) { + return FromkLinuxErrorNumber(klinux_errno_result); + } + + clockid_t klinux_clk_id = output.next(); + *clock_id = FromkLinuxClockId(klinux_clk_id); + return 0; +} + int enc_untrusted_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { socklen_t klinux_sock_len = diff --git a/asylo/platform/host_call/trusted/host_calls.h b/asylo/platform/host_call/trusted/host_calls.h index 8fc9e661a4..2d213ceac7 100644 --- a/asylo/platform/host_call/trusted/host_calls.h +++ b/asylo/platform/host_call/trusted/host_calls.h @@ -104,6 +104,7 @@ int enc_untrusted_wait(int *wstatus); int enc_untrusted_close(int fd); int enc_untrusted_nanosleep(const struct timespec *req, struct timespec *rem); int enc_untrusted_clock_gettime(clockid_t clk_id, struct timespec *tp); +int enc_untrusted_clock_getcpuclockid(pid_t pid, clockid_t *clock_id); int enc_untrusted_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int enc_untrusted_connect(int sockfd, const struct sockaddr *addr, diff --git a/asylo/platform/host_call/untrusted/host_call_handlers.cc b/asylo/platform/host_call/untrusted/host_call_handlers.cc index ab9f90bf28..17cfdcc074 100644 --- a/asylo/platform/host_call/untrusted/host_call_handlers.cc +++ b/asylo/platform/host_call/untrusted/host_call_handlers.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include "asylo/platform/host_call/serializer_functions.h" #include "asylo/platform/primitives/util/message.h" @@ -461,5 +462,16 @@ Status GetIfAddrsHandler(const std::shared_ptr &client, return Status::OkStatus(); } +Status GetCpuClockIdHandler(const std::shared_ptr &client, + void *context, primitives::MessageReader *input, + primitives::MessageWriter *output) { + ASYLO_RETURN_IF_READER_NOT_EMPTY(*input); + pid_t pid = input->next(); + clockid_t klinux_clock_id; + output->Push(clock_getcpuclockid(pid, &klinux_clock_id)); + output->Push(static_cast(klinux_clock_id)); + return Status::OkStatus(); +} + } // namespace host_call } // namespace asylo diff --git a/asylo/platform/posix/time.cc b/asylo/platform/posix/time.cc index 75df8dcb72..e36c5beaf2 100644 --- a/asylo/platform/posix/time.cc +++ b/asylo/platform/posix/time.cc @@ -75,6 +75,11 @@ int clock_gettime(clockid_t clock_id, struct timespec *time) { return result; } + +int clock_getcpuclockid(pid_t pid, clockid_t *clock_id) { + return enc_untrusted_clock_getcpuclockid(pid, clock_id); +} + int getitimer(int which, struct itimerval *curr_value) { return enc_untrusted_getitimer(which, curr_value); } diff --git a/asylo/platform/system_call/type_conversions/define_types.py b/asylo/platform/system_call/type_conversions/define_types.py index 474cf26fda..a67a042061 100644 --- a/asylo/platform/system_call/type_conversions/define_types.py +++ b/asylo/platform/system_call/type_conversions/define_types.py @@ -230,7 +230,10 @@ define_constants( name="ClockId", - values=["CLOCK_REALTIME", "CLOCK_MONOTONIC"], + values=[ + "CLOCK_REALTIME", + "CLOCK_MONOTONIC", + ], include_header_file="time.h", default_value_newlib=-1, default_value_host=-1, diff --git a/asylo/platform/system_call/type_conversions/generated_types_functions_test.cc b/asylo/platform/system_call/type_conversions/generated_types_functions_test.cc index 0d56651fb6..120b316ef1 100644 --- a/asylo/platform/system_call/type_conversions/generated_types_functions_test.cc +++ b/asylo/platform/system_call/type_conversions/generated_types_functions_test.cc @@ -557,9 +557,12 @@ TEST_F(GeneratedTypesFunctionsTest, BaseSignalNumberTest) { } TEST_F(GeneratedTypesFunctionsTest, ClockIdTest) { - std::vector from_consts = {kLinux_CLOCK_MONOTONIC, - kLinux_CLOCK_REALTIME}; - std::vector to_consts = {CLOCK_MONOTONIC, CLOCK_REALTIME}; + std::vector from_consts = { + kLinux_CLOCK_MONOTONIC, kLinux_CLOCK_REALTIME, + }; + std::vector to_consts = { + CLOCK_MONOTONIC, CLOCK_REALTIME, + }; for (int i = 0; i < from_consts.size(); i++) { EXPECT_THAT(TokLinuxClockId(to_consts[i]), Eq(from_consts[i])); diff --git a/asylo/platform/system_call/type_conversions/types_functions.h b/asylo/platform/system_call/type_conversions/types_functions.h index 6a85b9ba6e..27c6b8ea92 100644 --- a/asylo/platform/system_call/type_conversions/types_functions.h +++ b/asylo/platform/system_call/type_conversions/types_functions.h @@ -22,7 +22,7 @@ // This is the top-level include file for using type conversion functions // between newlib and the host implementation. -#include "asylo/platform/system_call/type_conversions/generated_types_functions.h" -#include "asylo/platform/system_call/type_conversions/manual_types_functions.h" +#include "asylo/platform/system_call/type_conversions/generated_types_functions.h" // IWYU pragma: export +#include "asylo/platform/system_call/type_conversions/manual_types_functions.h" // IWYU pragma: export #endif // ASYLO_PLATFORM_SYSTEM_CALL_TYPE_CONVERSIONS_TYPES_FUNCTIONS_H_