Coverage Report

Created: 2023-04-12 06:22

/src/openssl/crypto/sleep.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/crypto.h>
11
#include "internal/e_os.h"
12
13
/* system-specific variants defining OSSL_sleep() */
14
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
15
16
void OSSL_sleep(uint64_t millis)
17
0
{
18
# ifdef OPENSSL_SYS_VXWORKS
19
    struct timespec ts;
20
21
    ts.tv_sec = (long int) (millis / 1000);
22
    ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
23
    nanosleep(&ts, NULL);
24
# elif defined(__TANDEM)
25
#  if !defined(_REENTRANT)
26
#   include <cextdecs.h(PROCESS_DELAY_)>
27
28
    /* HPNS does not support usleep for non threaded apps */
29
    PROCESS_DELAY_(millis * 1000);
30
#  elif defined(_SPT_MODEL_)
31
#   include <spthread.h>
32
#   include <spt_extensions.h>
33
34
    usleep(millis * 1000);
35
#  else
36
    usleep(millis * 1000);
37
#  endif
38
# else
39
0
    usleep(millis * 1000);
40
0
# endif
41
0
}
42
#elif defined(_WIN32)
43
# include <windows.h>
44
45
void OSSL_sleep(uint64_t millis)
46
{
47
    /*
48
     * Windows' Sleep() takes a DWORD argument, which is smaller than
49
     * a uint64_t, so we need to limit it to 49 days, which should be enough.
50
     */
51
    DWORD limited_millis = (DWORD)-1;
52
53
    if (millis < limited_millis)
54
        limited_millis = (DWORD)millis;
55
    Sleep(limited_millis);
56
}
57
58
#else
59
/* Fallback to a busy wait */
60
# include "internal/time.h"
61
62
static void ossl_sleep_secs(uint64_t secs)
63
{
64
    /*
65
     * sleep() takes an unsigned int argument, which is smaller than
66
     * a uint64_t, so it needs to be limited to 136 years which
67
     * should be enough even for Sleeping Beauty.
68
     */
69
    unsigned int limited_secs = UINT_MAX;
70
71
    if (secs < limited_secs)
72
        limited_secs = (unsigned int)secs;
73
    sleep(limited_secs);
74
}
75
76
static void ossl_sleep_millis(uint64_t millis)
77
{
78
    const OSSL_TIME finish
79
        = ossl_time_add(ossl_time_now(), ossl_ms2time(millis));
80
81
    while (ossl_time_compare(ossl_time_now(), finish) < 0)
82
        /* busy wait */ ;
83
}
84
85
void OSSL_sleep(uint64_t millis)
86
{
87
    ossl_sleep_secs(millis / 1000);
88
    ossl_sleep_millis(millis % 1000);
89
}
90
#endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */