Use proper types for time values

This commit is contained in:
Michał Kowalczyk
2020-07-23 21:18:31 +02:00
parent bdcf29ba33
commit e65af7cb1a
14 changed files with 49 additions and 55 deletions
+8 -8
View File
@@ -71,16 +71,16 @@ int shim_do_setitimer(int which, struct __kernel_itimerval* value,
if (ovalue && test_user_memory(ovalue, sizeof(*ovalue), true))
return -EFAULT;
unsigned long setup_time = DkSystemTimeQuery();
uint64_t setup_time = DkSystemTimeQuery();
unsigned long next_value = value->it_value.tv_sec * 1000000 + value->it_value.tv_usec;
unsigned long next_reset = value->it_interval.tv_sec * 1000000 + value->it_interval.tv_usec;
uint64_t next_value = value->it_value.tv_sec * (uint64_t)1000000 + value->it_value.tv_usec;
uint64_t next_reset = value->it_interval.tv_sec * (uint64_t)1000000 + value->it_interval.tv_usec;
MASTER_LOCK();
unsigned long current_timeout =
uint64_t current_timeout =
real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
unsigned long current_reset = real_itimer.reset;
uint64_t current_reset = real_itimer.reset;
int64_t ret =
install_async_event(NULL, next_value, &signal_itimer, (void*)(setup_time + next_value));
@@ -114,12 +114,12 @@ int shim_do_getitimer(int which, struct __kernel_itimerval* value) {
if (test_user_memory(value, sizeof(*value), true))
return -EFAULT;
unsigned long setup_time = DkSystemTimeQuery();
uint64_t setup_time = DkSystemTimeQuery();
MASTER_LOCK();
unsigned long current_timeout =
uint64_t current_timeout =
real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
unsigned long current_reset = real_itimer.reset;
uint64_t current_reset = real_itimer.reset;
MASTER_UNLOCK();
value->it_interval.tv_sec = current_reset / 1000000;
+6 -6
View File
@@ -25,9 +25,9 @@ int shim_do_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone*
if (tz && test_user_memory(tz, sizeof(*tz), true))
return -EFAULT;
long time = DkSystemTimeQuery();
uint64_t time = DkSystemTimeQuery();
if (time == -1)
if (time == (uint64_t)-1)
return -PAL_ERRNO();
tv->tv_sec = time / 1000000;
@@ -36,9 +36,9 @@ int shim_do_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone*
}
time_t shim_do_time(time_t* tloc) {
long time = DkSystemTimeQuery();
uint64_t time = DkSystemTimeQuery();
if (time == -1)
if (time == (uint64_t)-1)
return -PAL_ERRNO();
if (tloc && test_user_memory(tloc, sizeof(*tloc), true))
@@ -62,9 +62,9 @@ int shim_do_clock_gettime(clockid_t which_clock, struct timespec* tp) {
if (test_user_memory(tp, sizeof(*tp), true))
return -EFAULT;
long time = DkSystemTimeQuery();
uint64_t time = DkSystemTimeQuery();
if (time == -1)
if (time == (uint64_t)-1)
return -PAL_ERRNO();
tp->tv_sec = time / 1000000;
+1 -2
View File
@@ -692,8 +692,7 @@ void DkObjectClose(PAL_HANDLE objectHandle);
* \brief Get the current time
* \return the current time in microseconds
*/
PAL_NUM
DkSystemTimeQuery(void);
PAL_NUM DkSystemTimeQuery(void);
/*!
* \brief Cryptographically secure random.
+5 -5
View File
@@ -45,12 +45,12 @@ int main(void) {
pal_printf("DkThreadCreate failed\n");
return 1;
}
unsigned long t_start = DkSystemTimeQuery();
uint64_t t_start = DkSystemTimeQuery();
pal_printf("Testing wait with too short timeout...\n");
DkSynchronizationObjectWait(event1, 1000000);
unsigned long t_wait1 = DkSystemTimeQuery();
unsigned long dt_wait1 = t_wait1 - t_start;
uint64_t t_wait1 = DkSystemTimeQuery();
uint64_t dt_wait1 = t_wait1 - t_start;
pal_printf("Wait returned after %lu us.\n", dt_wait1);
pal_printf("Timeout count: %d\n", timeouts);
if (dt_wait1 > 1000000 && dt_wait1 < 1100000 && timeouts == 1) {
@@ -59,8 +59,8 @@ int main(void) {
pal_printf("Testing wait with long enough timeout...\n");
DkSynchronizationObjectWait(event1, 5000000);
unsigned long t_wait2 = DkSystemTimeQuery();
unsigned long dt_wait2 = t_wait2 - t_start;
uint64_t t_wait2 = DkSystemTimeQuery();
uint64_t dt_wait2 = t_wait2 - t_start;
pal_printf("Wait returned after %lu us since start.\n", dt_wait2);
pal_printf("Timeout count: %d\n", timeouts);
if (dt_wait2 > 3000000 && dt_wait2 < 3100000 && timeouts == 1) {
+6 -6
View File
@@ -3,8 +3,8 @@
#include "pal_debug.h"
int main(int argc, const char** argv, const char** envp) {
unsigned long time1 = DkSystemTimeQuery();
unsigned long time2 = DkSystemTimeQuery();
uint64_t time1 = DkSystemTimeQuery();
uint64_t time2 = DkSystemTimeQuery();
pal_printf("Time Query 1: %ld\n", time1);
pal_printf("Time Query 2: %ld\n", time2);
@@ -12,18 +12,18 @@ int main(int argc, const char** argv, const char** envp) {
if (time1 <= time2)
pal_printf("Query System Time OK\n");
unsigned long time3 = DkSystemTimeQuery();
uint64_t time3 = DkSystemTimeQuery();
DkThreadDelayExecution(10000);
unsigned long time4 = DkSystemTimeQuery();
uint64_t time4 = DkSystemTimeQuery();
pal_printf("Sleeped %ld Microseconds\n", time4 - time3);
if (time3 < time4 && time4 - time3 > 10000)
pal_printf("Delay Execution for 10000 Microseconds OK\n");
unsigned long time5 = DkSystemTimeQuery();
uint64_t time5 = DkSystemTimeQuery();
DkThreadDelayExecution(3000000);
unsigned long time6 = DkSystemTimeQuery();
uint64_t time6 = DkSystemTimeQuery();
pal_printf("Sleeped %ld Microseconds\n", time6 - time5);
+3 -3
View File
@@ -16,7 +16,7 @@ int main(int argc, char** argv) {
#endif
if (argc == 1) {
unsigned long time = DkSystemTimeQuery();
uint64_t time = DkSystemTimeQuery();
char time_arg[24];
snprintf(time_arg, 24, "%ld", time);
@@ -46,8 +46,8 @@ int main(int argc, char** argv) {
DkObjectClose(proc);
} else {
unsigned long end = DkSystemTimeQuery();
unsigned long start = atol(argv[2]);
uint64_t end = DkSystemTimeQuery();
uint64_t start = atol(argv[2]);
pal_printf("wall time = %ld\n", end - start);
}
}
+2 -3
View File
@@ -114,9 +114,8 @@ int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
return 0;
}
int _DkThreadDelayExecution (unsigned long * duration)
{
int ret = ocall_sleep(duration);
int _DkThreadDelayExecution(uint64_t* duration_us) {
int ret = ocall_sleep(duration_us);
return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
}
+2 -3
View File
@@ -1254,8 +1254,7 @@ int ocall_gettime(uint64_t* microsec) {
return retval;
}
int ocall_sleep (unsigned long * microsec)
{
int ocall_sleep(uint64_t* microsec) {
int retval = 0;
ms_ocall_sleep_t * ms;
@@ -1274,7 +1273,7 @@ int ocall_sleep (unsigned long * microsec)
if (!retval) {
*microsec = 0;
} else if (retval == -EINTR) {
unsigned long untrusted_microsec = READ_ONCE(ms->ms_microsec);
uint64_t untrusted_microsec = READ_ONCE(ms->ms_microsec);
if (*microsec < untrusted_microsec) {
retval = -EPERM;
goto out;
+1 -1
View File
@@ -77,7 +77,7 @@ int ocall_futex(uint32_t *uaddr, int op, int val, int64_t timeout_us);
int ocall_gettime(uint64_t* microsec);
int ocall_sleep (unsigned long * microsec);
int ocall_sleep(uint64_t* microsec);
int ocall_socketpair (int domain, int type, int protocol, int sockfds[2]);
+1 -1
View File
@@ -247,7 +247,7 @@ typedef struct {
} ms_ocall_gettime_t;
typedef struct {
unsigned long ms_microsec;
uint64_t ms_microsec;
} ms_ocall_sleep_t;
typedef struct {
+4 -4
View File
@@ -575,7 +575,7 @@ static long sgx_ocall_gettime(void * pms)
static long sgx_ocall_sleep(void * pms)
{
ms_ocall_sleep_t * ms = (ms_ocall_sleep_t *) pms;
ms_ocall_sleep_t* ms = (ms_ocall_sleep_t*)pms;
long ret;
ODEBUG(OCALL_SLEEP, ms);
if (!ms->ms_microsec) {
@@ -583,15 +583,15 @@ static long sgx_ocall_sleep(void * pms)
return 0;
}
struct timespec req, rem;
unsigned long microsec = ms->ms_microsec;
const unsigned long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
uint64_t microsec = ms->ms_microsec;
const uint64_t VERY_LONG_TIME_IN_US = (uint64_t)1000000 * 60 * 60 * 24 * 365 * 128;
if (ms->ms_microsec > VERY_LONG_TIME_IN_US) {
/* avoid overflow with time_t */
req.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
req.tv_nsec = 0;
} else {
req.tv_sec = ms->ms_microsec / 1000000;
req.tv_nsec = (microsec - req.tv_sec * 1000000) * 1000;
req.tv_nsec = (microsec - req.tv_sec * (uint64_t)1000000) * 1000;
}
ret = INLINE_SYSCALL(nanosleep, 2, &req, &rem);
+1 -1
View File
@@ -166,7 +166,7 @@ noreturn static void print_usage_and_exit(const char* argv_0) {
void pal_linux_main(void* initial_rsp, void* fini_callback) {
__UNUSED(fini_callback); // TODO: We should call `fini_callback` at the end.
unsigned long start_time = _DkSystemTimeQueryEarly();
uint64_t start_time = _DkSystemTimeQueryEarly();
g_pal_state.start_time = start_time;
int argc;
+7 -10
View File
@@ -177,28 +177,25 @@ err:
return ret;
}
int _DkThreadDelayExecution (unsigned long * duration)
{
int _DkThreadDelayExecution(uint64_t* duration_us) {
struct timespec sleeptime;
struct timespec remainingtime;
const unsigned long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
if (*duration > VERY_LONG_TIME_IN_US) {
const uint64_t VERY_LONG_TIME_IN_US = (uint64_t)1000000 * 60 * 60 * 24 * 365 * 128;
if (*duration_us > VERY_LONG_TIME_IN_US) {
/* avoid overflow with time_t */
sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
sleeptime.tv_nsec = 0;
} else {
sleeptime.tv_sec = *duration / 1000000;
sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
sleeptime.tv_sec = *duration_us / 1000000;
sleeptime.tv_nsec = (*duration_us - sleeptime.tv_sec * (uint64_t)1000000) * 1000;
}
int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
if (IS_ERR(ret)) {
PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
remainingtime.tv_nsec / 1000;
*duration -= remaining;
PAL_NUM remaining = remainingtime.tv_sec * 1000000 + remainingtime.tv_nsec / 1000;
*duration_us -= remaining;
return -PAL_ERROR_INTERRUPTED;
}
+2 -2
View File
@@ -169,7 +169,7 @@ extern struct pal_internal_state {
PAL_HANDLE console;
unsigned long start_time;
uint64_t start_time;
} g_pal_state;
extern PAL_CONTROL g_pal_control;
@@ -236,7 +236,7 @@ int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo);
int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
const void * param);
noreturn void _DkThreadExit(int* clear_child_tid);
int _DkThreadDelayExecution (unsigned long * duration);
int _DkThreadDelayExecution(uint64_t* duration_us);
void _DkThreadYieldExecution (void);
int _DkThreadResume (PAL_HANDLE threadHandle);
int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,