Coverage Report

Created: 2025-08-28 07:07

/src/openssl32/crypto/sleep.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2022-2024 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
#include "internal/time.h"
13
14
/* system-specific variants defining OSSL_sleep() */
15
#if (defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)) \
16
    && !defined(OPENSSL_USE_SLEEP_BUSYLOOP)
17
# include <unistd.h>
18
19
static void ossl_sleep_millis(uint64_t millis)
20
0
{
21
# ifdef OPENSSL_SYS_VXWORKS
22
    struct timespec ts;
23
24
    ts.tv_sec = (long int) (millis / 1000);
25
    ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
26
    nanosleep(&ts, NULL);
27
# elif defined(__TANDEM) && !defined(_REENTRANT)
28
#   include <cextdecs.h(PROCESS_DELAY_)>
29
30
    /* HPNS does not support usleep for non threaded apps */
31
    PROCESS_DELAY_(millis * 1000);
32
# else
33
0
    unsigned int s = (unsigned int)(millis / 1000);
34
0
    unsigned int us = (unsigned int)((millis % 1000) * 1000);
35
36
0
    sleep(s);
37
0
    usleep(us);
38
0
# endif
39
0
}
40
#elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI)
41
# include <windows.h>
42
43
static void ossl_sleep_millis(uint64_t millis)
44
{
45
    /*
46
     * Windows' Sleep() takes a DWORD argument, which is smaller than
47
     * a uint64_t, so we need to limit it to 49 days, which should be enough.
48
     */
49
    DWORD limited_millis = (DWORD)-1;
50
51
    if (millis < limited_millis)
52
        limited_millis = (DWORD)millis;
53
    Sleep(limited_millis);
54
}
55
56
#else
57
/* Fallback to a busy wait */
58
# define USE_SLEEP_SECS
59
60
static void ossl_sleep_secs(uint64_t secs)
61
{
62
    /*
63
     * sleep() takes an unsigned int argument, which is smaller than
64
     * a uint64_t, so it needs to be limited to 136 years which
65
     * should be enough even for Sleeping Beauty.
66
     */
67
    unsigned int limited_secs = UINT_MAX;
68
69
    if (secs < limited_secs)
70
        limited_secs = (unsigned int)secs;
71
    sleep(limited_secs);
72
}
73
74
static void ossl_sleep_millis(uint64_t millis)
75
{
76
    const OSSL_TIME finish
77
        = ossl_time_add(ossl_time_now(), ossl_ms2time(millis));
78
79
    while (ossl_time_compare(ossl_time_now(), finish) < 0)
80
        /* busy wait */ ;
81
}
82
#endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */
83
84
void OSSL_sleep(uint64_t millis)
85
0
{
86
0
    OSSL_TIME now = ossl_time_now();
87
0
    OSSL_TIME finish = ossl_time_add(now, ossl_ms2time(millis));
88
0
    uint64_t left = millis;
89
90
#if defined(USE_SLEEP_SECS)
91
    do {
92
        ossl_sleep_secs(left / 1000);
93
        now = ossl_time_now();
94
        left = ossl_time2ms(ossl_time_subtract(finish, now));
95
    } while (ossl_time_compare(now, finish) < 0 && left > 1000);
96
97
    if (ossl_time_compare(now, finish) >= 0)
98
        return;
99
#endif
100
101
0
    do {
102
0
        ossl_sleep_millis(left);
103
0
        now = ossl_time_now();
104
0
        left = ossl_time2ms(ossl_time_subtract(finish, now));
105
0
    } while (ossl_time_compare(now, finish) < 0);
106
0
}