• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <errno.h>
6 #include <linux/futex.h>
7 #include <sys/syscall.h>
8 #include <sys/time.h>
9 #include <unistd.h>
10 
11 #include "components/nacl/loader/nonsfi/irt_interfaces.h"
12 #include "components/nacl/loader/nonsfi/irt_util.h"
13 #include "native_client/src/trusted/service_runtime/include/sys/time.h"
14 
15 namespace nacl {
16 namespace nonsfi {
17 namespace {
18 
19 // Converts a pair of NaCl's timespec of absolute time and host's timespec of
20 // the current time to host's timespec of the relative time between them.
21 // Note that assuming tv_nsec for the both input structs are non-negative,
22 // the returned reltime's tv_nsec is also always non-negative.
23 // (I.e., for -0.1 sec, {tv_sec: -1, tv_nsec: 900000000} will be returned).
NaClAbsTimeToRelTime(const struct nacl_abi_timespec & nacl_abstime,const struct timespec & now,struct timespec * reltime)24 void NaClAbsTimeToRelTime(const struct nacl_abi_timespec& nacl_abstime,
25                           const struct timespec& now,
26                           struct timespec* reltime) {
27   reltime->tv_sec = nacl_abstime.tv_sec - now.tv_sec;
28   reltime->tv_nsec = nacl_abstime.tv_nsec - now.tv_nsec;
29   if (reltime->tv_nsec < 0) {
30     reltime->tv_sec -= 1;
31     reltime->tv_nsec += 1000000000;
32   }
33 }
34 
IrtFutexWaitAbs(volatile int * addr,int value,const struct nacl_abi_timespec * abstime)35 int IrtFutexWaitAbs(volatile int* addr, int value,
36                     const struct nacl_abi_timespec* abstime) {
37   struct timespec timeout;
38   struct timespec* timeout_ptr = NULL;
39   if (abstime) {
40     // futex syscall takes relative timeout, but the ABI for IRT's
41     // futex_wait_abs is absolute timeout. So, here we convert it.
42     struct timespec now;
43     if (clock_gettime(CLOCK_REALTIME, &now))
44       return errno;
45     NaClAbsTimeToRelTime(*abstime, now, &timeout);
46 
47     // Linux's FUTEX_WAIT returns EINVAL for negative timeout, but an absolute
48     // time that is in the past is a valid argument to irt_futex_wait_abs(),
49     // and a caller expects ETIMEDOUT.
50     // Here check only tv_sec since we assume time_t is signed. See also
51     // the comment for NaClAbsTimeToRelTime.
52     if (timeout.tv_sec < 0)
53       return ETIMEDOUT;
54     timeout_ptr = &timeout;
55   }
56   return CheckError(
57       syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, value, timeout_ptr, 0, 0));
58 }
59 
IrtFutexWake(volatile int * addr,int nwake,int * count)60 int IrtFutexWake(volatile int* addr, int nwake, int* count) {
61   return CheckErrorWithResult(
62       syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, nwake, 0, 0, 0), count);
63 }
64 
65 }  // namespace
66 
67 // For futex_wait_abs, the argument type should be nacl_abi_timespec. However,
68 // the definition uses its host type, struct timespec. So, here we need to cast
69 // it.
70 const nacl_irt_futex kIrtFutex = {
71   reinterpret_cast<int(*)(volatile int*, int, const struct timespec*)>(
72       IrtFutexWaitAbs),
73   IrtFutexWake,
74 };
75 
76 }  // namespace nonsfi
77 }  // namespace nacl
78