Skip to content

Commit

Permalink
Add clock_getcpuclockid support
Browse files Browse the repository at this point in the history
The removed time.h declarations are never defined or used. From an RTEMS
discussion*, it appears that they were never POSIX either. This commit
removes them to avoid errors from -Wl,--no-undefined.

* https://lists.rtems.org/pipermail/vc/2016-January/009509.html

PiperOrigin-RevId: 273665075
Change-Id: I3527203a1a28de808df01a4e2019ae456d3ad01b
  • Loading branch information
deeglaze committed Oct 9, 2019
1 parent 7967d8f commit ad1ee9b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 7 deletions.
23 changes: 22 additions & 1 deletion asylo/distrib/toolchain/newlib-2.5.0.20170922.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions asylo/platform/host_call/exit_handler_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions asylo/platform/host_call/trusted/host_calls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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<int32_t>();
if (klinux_errno_result != 0) {
return FromkLinuxErrorNumber(klinux_errno_result);
}

clockid_t klinux_clk_id = output.next<uint64_t>();
*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 =
Expand Down
1 change: 1 addition & 0 deletions asylo/platform/host_call/trusted/host_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions asylo/platform/host_call/untrusted/host_call_handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ctime>

#include "asylo/platform/host_call/serializer_functions.h"
#include "asylo/platform/primitives/util/message.h"
Expand Down Expand Up @@ -461,5 +462,16 @@ Status GetIfAddrsHandler(const std::shared_ptr<primitives::Client> &client,
return Status::OkStatus();
}

Status GetCpuClockIdHandler(const std::shared_ptr<primitives::Client> &client,
void *context, primitives::MessageReader *input,
primitives::MessageWriter *output) {
ASYLO_RETURN_IF_READER_NOT_EMPTY(*input);
pid_t pid = input->next<int32_t>();
clockid_t klinux_clock_id;
output->Push<int>(clock_getcpuclockid(pid, &klinux_clock_id));
output->Push<uint64_t>(static_cast<uint64_t>(klinux_clock_id));
return Status::OkStatus();
}

} // namespace host_call
} // namespace asylo
5 changes: 5 additions & 0 deletions asylo/platform/posix/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion asylo/platform/system_call/type_conversions/define_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,12 @@ TEST_F(GeneratedTypesFunctionsTest, BaseSignalNumberTest) {
}

TEST_F(GeneratedTypesFunctionsTest, ClockIdTest) {
std::vector<clockid_t> from_consts = {kLinux_CLOCK_MONOTONIC,
kLinux_CLOCK_REALTIME};
std::vector<clockid_t> to_consts = {CLOCK_MONOTONIC, CLOCK_REALTIME};
std::vector<clockid_t> from_consts = {
kLinux_CLOCK_MONOTONIC, kLinux_CLOCK_REALTIME,
};
std::vector<clockid_t> 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]));
Expand Down
4 changes: 2 additions & 2 deletions asylo/platform/system_call/type_conversions/types_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_

0 comments on commit ad1ee9b

Please sign in to comment.