Coverage Report

Created: 2025-06-13 06:58

/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
13
/* system-specific variants defining OSSL_sleep() */
14
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
15
#include <unistd.h>
16
17
void OSSL_sleep(uint64_t millis)
18
0
{
19
# ifdef OPENSSL_SYS_VXWORKS
20
    struct timespec ts;
21
22
    ts.tv_sec = (long int) (millis / 1000);
23
    ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
24
    nanosleep(&ts, NULL);
25
# elif defined(__TANDEM) && !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
# else
31
0
    unsigned int s = (unsigned int)(millis / 1000);
32
0
    unsigned int us = (unsigned int)((millis % 1000) * 1000);
33
34
0
    sleep(s);
35
0
    usleep(us);
36
0
# endif
37
0
}
38
#elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI)
39
# include <windows.h>
40
41
void OSSL_sleep(uint64_t millis)
42
{
43
    /*
44
     * Windows' Sleep() takes a DWORD argument, which is smaller than
45
     * a uint64_t, so we need to limit it to 49 days, which should be enough.
46
     */
47
    DWORD limited_millis = (DWORD)-1;
48
49
    if (millis < limited_millis)
50
        limited_millis = (DWORD)millis;
51
    Sleep(limited_millis);
52
}
53
54
#else
55
/* Fallback to a busy wait */
56
# include "internal/time.h"
57
58
static void ossl_sleep_secs(uint64_t secs)
59
{
60
    /*
61
     * sleep() takes an unsigned int argument, which is smaller than
62
     * a uint64_t, so it needs to be limited to 136 years which
63
     * should be enough even for Sleeping Beauty.
64
     */
65
    unsigned int limited_secs = UINT_MAX;
66
67
    if (secs < limited_secs)
68
        limited_secs = (unsigned int)secs;
69
    sleep(limited_secs);
70
}
71
72
static void ossl_sleep_millis(uint64_t millis)
73
{
74
    const OSSL_TIME finish
75
        = ossl_time_add(ossl_time_now(), ossl_ms2time(millis));
76
77
    while (ossl_time_compare(ossl_time_now(), finish) < 0)
78
        /* busy wait */ ;
79
}
80
81
void OSSL_sleep(uint64_t millis)
82
{
83
    ossl_sleep_secs(millis / 1000);
84
    ossl_sleep_millis(millis % 1000);
85
}
86
#endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */