Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/e_os.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-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
#ifndef OSSL_E_OS_H
11
# define OSSL_E_OS_H
12
13
# include <limits.h>
14
# include <openssl/opensslconf.h>
15
16
# include <openssl/e_os2.h>
17
# include <openssl/crypto.h>
18
# include "internal/nelem.h"
19
20
/*
21
 * <openssl/e_os2.h> contains what we can justify to make visible to the
22
 * outside; this file e_os.h is not part of the exported interface.
23
 */
24
25
# if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
26
#  define NO_CHMOD
27
#  define NO_SYSLOG
28
# endif
29
30
847M
# define get_last_sys_error()    errno
31
335k
# define clear_sys_error()       errno=0
32
847M
# define set_sys_error(e)        errno=(e)
33
34
/********************************************************************
35
 The Microsoft section
36
 ********************************************************************/
37
# if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
38
#  define WIN32
39
# endif
40
# if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
41
#  define WINDOWS
42
# endif
43
# if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
44
#  define MSDOS
45
# endif
46
47
# ifdef WIN32
48
#  undef get_last_sys_error
49
#  undef clear_sys_error
50
#  undef set_sys_error
51
#  define get_last_sys_error()    GetLastError()
52
#  define clear_sys_error()       SetLastError(0)
53
#  define set_sys_error(e)        SetLastError(e)
54
#  if !defined(WINNT)
55
#   define WIN_CONSOLE_BUG
56
#  endif
57
# else
58
# endif
59
60
# if (defined(WINDOWS) || defined(MSDOS))
61
62
#  ifdef __DJGPP__
63
#   include <unistd.h>
64
#   include <sys/stat.h>
65
#   define _setmode setmode
66
#   define _O_TEXT O_TEXT
67
#   define _O_BINARY O_BINARY
68
#   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
69
#   undef DEVRANDOM
70
#   define DEVRANDOM "/dev/urandom\x24"
71
#  endif                        /* __DJGPP__ */
72
73
#  ifndef S_IFDIR
74
#   define S_IFDIR     _S_IFDIR
75
#  endif
76
77
#  ifndef S_IFMT
78
#   define S_IFMT      _S_IFMT
79
#  endif
80
81
#  if !defined(WINNT) && !defined(__DJGPP__)
82
#   define NO_SYSLOG
83
#  endif
84
85
#  ifdef WINDOWS
86
#   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
87
       /*
88
        * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
89
        * Most notably we ought to check for availability of each specific
90
        * routine that was introduced after denoted _WIN32_WINNT with
91
        * GetProcAddress(). Normally newer functions are masked with higher
92
        * _WIN32_WINNT in SDK headers. So that if you wish to use them in
93
        * some module, you'd need to override _WIN32_WINNT definition in
94
        * the target module in order to "reach for" prototypes, but replace
95
        * calls to new functions with indirect calls. Alternatively it
96
        * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
97
        * and check for current OS version instead.
98
        */
99
#    define _WIN32_WINNT 0x0501
100
#   endif
101
#   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
102
       /*
103
        * Just like defining _WIN32_WINNT including winsock2.h implies
104
        * certain "discipline" for maintaining [broad] binary compatibility.
105
        * As long as structures are invariant among Winsock versions,
106
        * it's sufficient to check for specific Winsock2 API availability
107
        * at run-time [DSO_global_lookup is recommended]...
108
        */
109
#    include <winsock2.h>
110
#    include <ws2tcpip.h>
111
       /*
112
        * Clang-based C++Builder 10.3.3 toolchains cannot find C inline
113
        * definitions at link-time.  This header defines WspiapiLoad() as an
114
        * __inline function.  https://quality.embarcadero.com/browse/RSP-33806
115
        */
116
#    if !defined(__BORLANDC__) || !defined(__clang__)
117
#     include <wspiapi.h>
118
#    endif
119
       /* yes, they have to be #included prior to <windows.h> */
120
#   endif
121
#   include <windows.h>
122
#   include <stdio.h>
123
#   include <stddef.h>
124
#   include <errno.h>
125
#   if defined(_WIN32_WCE) && !defined(EACCES)
126
#    define EACCES   13
127
#   endif
128
#   include <string.h>
129
#   include <malloc.h>
130
#   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
131
#    if _MSC_VER>=1300 && _MSC_VER<1600
132
#     undef stdin
133
#     undef stdout
134
#     undef stderr
135
FILE *__iob_func();
136
#     define stdin  (&__iob_func()[0])
137
#     define stdout (&__iob_func()[1])
138
#     define stderr (&__iob_func()[2])
139
#    endif
140
#   endif
141
#  endif
142
#  include <io.h>
143
#  include <fcntl.h>
144
145
#  ifdef OPENSSL_SYS_WINCE
146
#   define OPENSSL_NO_POSIX_IO
147
#  endif
148
149
#  define EXIT(n) exit(n)
150
#  define LIST_SEPARATOR_CHAR ';'
151
#  ifndef W_OK
152
#   define W_OK        2
153
#  endif
154
#  ifndef R_OK
155
#   define R_OK        4
156
#  endif
157
#  ifdef OPENSSL_SYS_WINCE
158
#   define DEFAULT_HOME  ""
159
#  else
160
#   define DEFAULT_HOME  "C:"
161
#  endif
162
163
/* Avoid Visual Studio 13 GetVersion deprecated problems */
164
#  if defined(_MSC_VER) && _MSC_VER>=1800
165
#   define check_winnt() (1)
166
#   define check_win_minplat(x) (1)
167
#  else
168
#   define check_winnt() (GetVersion() < 0x80000000)
169
#   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
170
#  endif
171
172
# else                          /* The non-microsoft world */
173
174
#  if defined(OPENSSL_SYS_VXWORKS)
175
#   include <time.h>
176
#  else
177
#   include <sys/time.h>
178
#  endif
179
180
#  ifdef OPENSSL_SYS_VMS
181
#   define VMS 1
182
  /*
183
   * some programs don't include stdlib, so exit() and others give implicit
184
   * function warnings
185
   */
186
#   include <stdlib.h>
187
#   if defined(__DECC)
188
#    include <unistd.h>
189
#   else
190
#    include <unixlib.h>
191
#   endif
192
#   define LIST_SEPARATOR_CHAR ','
193
  /* We don't have any well-defined random devices on VMS, yet... */
194
#   undef DEVRANDOM
195
  /*-
196
     We need to do this since VMS has the following coding on status codes:
197
198
     Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
199
               The important thing to know is that odd numbers are considered
200
               good, while even ones are considered errors.
201
     Bits 3-15: actual status number
202
     Bits 16-27: facility number.  0 is considered "unknown"
203
     Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
204
                 output the message (which, for random codes, just looks ugly)
205
206
     So, what we do here is to change 0 to 1 to get the default success status,
207
     and everything else is shifted up to fit into the status number field, and
208
     the status is tagged as an error, which is what is wanted here.
209
210
     Finally, we add the VMS C facility code 0x35a000, because there are some
211
     programs, such as Perl, that will reinterpret the code back to something
212
     POSIX.  'man perlvms' explains it further.
213
214
     NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
215
     codes (status type = 1).  I couldn't disagree more.  Fortunately, the
216
     status type doesn't seem to bother Perl.
217
     -- Richard Levitte
218
  */
219
#   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
220
221
#   define DEFAULT_HOME "SYS$LOGIN:"
222
223
#  else
224
     /* !defined VMS */
225
#   include <unistd.h>
226
#   include <sys/types.h>
227
#   ifdef OPENSSL_SYS_WIN32_CYGWIN
228
#    include <io.h>
229
#    include <fcntl.h>
230
#   endif
231
232
0
#   define LIST_SEPARATOR_CHAR ':'
233
#   define EXIT(n)             exit(n)
234
#  endif
235
236
# endif
237
238
/***********************************************/
239
240
# if defined(OPENSSL_SYS_WINDOWS)
241
#  if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
242
#   define open _open
243
#   define fdopen _fdopen
244
#   define close _close
245
#   ifndef strdup
246
#    define strdup _strdup
247
#   endif
248
#   define unlink _unlink
249
#   define fileno _fileno
250
#  endif
251
# else
252
#  include <strings.h>
253
# endif
254
255
/* vxworks */
256
# if defined(OPENSSL_SYS_VXWORKS)
257
#  include <ioLib.h>
258
#  include <tickLib.h>
259
#  include <sysLib.h>
260
#  include <vxWorks.h>
261
#  include <sockLib.h>
262
#  include <taskLib.h>
263
264
#  define TTY_STRUCT int
265
#  define sleep(a) taskDelay((a) * sysClkRateGet())
266
267
/*
268
 * NOTE: these are implemented by helpers in database app! if the database is
269
 * not linked, we need to implement them elsewhere
270
 */
271
struct hostent *gethostbyname(const char *name);
272
struct hostent *gethostbyaddr(const char *addr, int length, int type);
273
struct servent *getservbyname(const char *name, const char *proto);
274
275
# endif
276
/* end vxworks */
277
278
/* system-specific variants defining ossl_sleep() */
279
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
280
# include <unistd.h>
281
static ossl_inline void ossl_sleep(unsigned long millis)
282
0
{
283
# ifdef OPENSSL_SYS_VXWORKS
284
    struct timespec ts;
285
    ts.tv_sec = (long int) (millis / 1000);
286
    ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
287
    nanosleep(&ts, NULL);
288
# elif defined(__TANDEM) && !defined(_REENTRANT)
289
#   include <cextdecs.h(PROCESS_DELAY_)>
290
291
    /* HPNS does not support usleep for non threaded apps */
292
    PROCESS_DELAY_(millis * 1000);
293
# else
294
0
    unsigned int s = (unsigned int)(millis / 1000);
295
0
    unsigned int us = (unsigned int)((millis % 1000) * 1000);
296
297
0
    if (s > 0)
298
0
        sleep(s);
299
0
    usleep(us);
300
0
# endif
301
0
}
Unexecuted instantiation: bf_prefix.c:ossl_sleep
Unexecuted instantiation: bio_dump.c:ossl_sleep
Unexecuted instantiation: bio_lib.c:ossl_sleep
Unexecuted instantiation: bio_meth.c:ossl_sleep
Unexecuted instantiation: bio_sock.c:ossl_sleep
Unexecuted instantiation: bio_sock2.c:ossl_sleep
Unexecuted instantiation: bss_null.c:ossl_sleep
Unexecuted instantiation: bss_sock.c:ossl_sleep
Unexecuted instantiation: err.c:ossl_sleep
Unexecuted instantiation: cryptlib.c:ossl_sleep
Unexecuted instantiation: init.c:ossl_sleep
Unexecuted instantiation: mem.c:ossl_sleep
Unexecuted instantiation: mem_sec.c:ossl_sleep
Unexecuted instantiation: o_str.c:ossl_sleep
Unexecuted instantiation: rand_lib.c:ossl_sleep
Unexecuted instantiation: rand_unix.c:ossl_sleep
Unexecuted instantiation: bio_addr.c:ossl_sleep
Unexecuted instantiation: conf_lib.c:ossl_sleep
Unexecuted instantiation: eng_init.c:ossl_sleep
Unexecuted instantiation: eng_lib.c:ossl_sleep
Unexecuted instantiation: tb_asnmth.c:ossl_sleep
Unexecuted instantiation: cpuid.c:ossl_sleep
Unexecuted instantiation: getenv.c:ossl_sleep
Unexecuted instantiation: o_names.c:ossl_sleep
Unexecuted instantiation: property_parse.c:ossl_sleep
Unexecuted instantiation: capabilities.c:ossl_sleep
Unexecuted instantiation: hkdf.c:ossl_sleep
Unexecuted instantiation: kbkdf.c:ossl_sleep
Unexecuted instantiation: tls1_prf.c:ossl_sleep
Unexecuted instantiation: x942kdf.c:ossl_sleep
Unexecuted instantiation: bf_readbuff.c:ossl_sleep
Unexecuted instantiation: bss_core.c:ossl_sleep
Unexecuted instantiation: bss_file.c:ossl_sleep
Unexecuted instantiation: bss_mem.c:ossl_sleep
Unexecuted instantiation: ossl_core_bio.c:ossl_sleep
Unexecuted instantiation: conf_api.c:ossl_sleep
Unexecuted instantiation: conf_def.c:ossl_sleep
Unexecuted instantiation: cfb_enc.c:ossl_sleep
Unexecuted instantiation: dh_kdf.c:ossl_sleep
Unexecuted instantiation: dso_dlfcn.c:ossl_sleep
Unexecuted instantiation: ec_backend.c:ossl_sleep
Unexecuted instantiation: decoder_lib.c:ossl_sleep
Unexecuted instantiation: http_client.c:ossl_sleep
Unexecuted instantiation: http_lib.c:ossl_sleep
Unexecuted instantiation: o_dir.c:ossl_sleep
Unexecuted instantiation: o_fopen.c:ossl_sleep
Unexecuted instantiation: ui_openssl.c:ossl_sleep
Unexecuted instantiation: v3_utl.c:ossl_sleep
Unexecuted instantiation: bf_buff.c:ossl_sleep
Unexecuted instantiation: bss_conn.c:ossl_sleep
Unexecuted instantiation: f_impl64.c:ossl_sleep
Unexecuted instantiation: v3_tlsf.c:ossl_sleep
Unexecuted instantiation: methods.c:ossl_sleep
Unexecuted instantiation: s3_lib.c:ossl_sleep
Unexecuted instantiation: s3_msg.c:ossl_sleep
Unexecuted instantiation: ssl_cert.c:ossl_sleep
Unexecuted instantiation: ssl_ciph.c:ossl_sleep
Unexecuted instantiation: ssl_init.c:ossl_sleep
Unexecuted instantiation: ssl_lib.c:ossl_sleep
Unexecuted instantiation: ssl_mcnf.c:ossl_sleep
Unexecuted instantiation: ssl_rsa.c:ossl_sleep
Unexecuted instantiation: ssl_sess.c:ossl_sleep
Unexecuted instantiation: t1_lib.c:ossl_sleep
Unexecuted instantiation: tls13_enc.c:ossl_sleep
Unexecuted instantiation: tls_depr.c:ossl_sleep
Unexecuted instantiation: tls_srp.c:ossl_sleep
Unexecuted instantiation: rec_layer_d1.c:ossl_sleep
Unexecuted instantiation: rec_layer_s3.c:ossl_sleep
Unexecuted instantiation: ssl3_buffer.c:ossl_sleep
Unexecuted instantiation: ssl3_record.c:ossl_sleep
Unexecuted instantiation: ssl3_record_tls13.c:ossl_sleep
Unexecuted instantiation: extensions.c:ossl_sleep
Unexecuted instantiation: extensions_clnt.c:ossl_sleep
Unexecuted instantiation: extensions_cust.c:ossl_sleep
Unexecuted instantiation: extensions_srvr.c:ossl_sleep
Unexecuted instantiation: statem.c:ossl_sleep
Unexecuted instantiation: statem_clnt.c:ossl_sleep
Unexecuted instantiation: statem_dtls.c:ossl_sleep
Unexecuted instantiation: statem_lib.c:ossl_sleep
Unexecuted instantiation: statem_srvr.c:ossl_sleep
Unexecuted instantiation: d1_lib.c:ossl_sleep
Unexecuted instantiation: d1_msg.c:ossl_sleep
Unexecuted instantiation: d1_srtp.c:ossl_sleep
Unexecuted instantiation: pqueue.c:ossl_sleep
Unexecuted instantiation: s3_enc.c:ossl_sleep
Unexecuted instantiation: ssl_asn1.c:ossl_sleep
Unexecuted instantiation: ssl_conf.c:ossl_sleep
Unexecuted instantiation: t1_enc.c:ossl_sleep
Unexecuted instantiation: dtls1_bitmap.c:ossl_sleep
Unexecuted instantiation: store_lib.c:ossl_sleep
Unexecuted instantiation: store_result.c:ossl_sleep
Unexecuted instantiation: by_dir.c:ossl_sleep
Unexecuted instantiation: cmp_client.c:ossl_sleep
Unexecuted instantiation: ssl_txt.c:ossl_sleep
302
#elif defined(_WIN32)
303
# include <windows.h>
304
static ossl_inline void ossl_sleep(unsigned long millis)
305
{
306
    Sleep(millis);
307
}
308
#else
309
/* Fallback to a busy wait */
310
static ossl_inline void ossl_sleep(unsigned long millis)
311
{
312
    struct timeval start, now;
313
    unsigned long elapsedms;
314
315
    gettimeofday(&start, NULL);
316
    do {
317
        gettimeofday(&now, NULL);
318
        elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
319
                     + now.tv_usec - start.tv_usec) / 1000;
320
    } while (elapsedms < millis);
321
}
322
#endif /* defined OPENSSL_SYS_UNIX */
323
324
/* ----------------------------- HP NonStop -------------------------------- */
325
/* Required to support platform variant without getpid() and pid_t. */
326
# if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
327
#  include <strings.h>
328
#  include <netdb.h>
329
#  define getservbyname(name,proto)          getservbyname((char*)name,proto)
330
#  define gethostbyname(name)                gethostbyname((char*)name)
331
#  define ioctlsocket(a,b,c)  ioctl(a,b,c)
332
#  ifdef NO_GETPID
333
inline int nssgetpid();
334
#   ifndef NSSGETPID_MACRO
335
#    define NSSGETPID_MACRO
336
#    include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
337
#    include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
338
       inline int nssgetpid()
339
       {
340
         short phandle[10]={0};
341
         union pseudo_pid {
342
          struct {
343
           short cpu;
344
           short pin;
345
         } cpu_pin ;
346
         int ppid;
347
        } ppid = { 0 };
348
        PROCESSHANDLE_GETMINE_(phandle);
349
        PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
350
        return ppid.ppid;
351
       }
352
#    define getpid(a) nssgetpid(a)
353
#   endif /* NSSGETPID_MACRO */
354
#  endif /* NO_GETPID */
355
/*#  define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
356
/*#  define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
357
/*#  define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
358
/*#  define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
359
/*#  define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
360
#  if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
361
  /*
362
   * HPNS SPT threads
363
   */
364
#   define  SPT_THREAD_SIGNAL 1
365
#   define  SPT_THREAD_AWARE 1
366
#   include <spthread.h>
367
#   undef close
368
#   define close spt_close
369
/*
370
#   define get_last_socket_error()  errno
371
#   define clear_socket_error() errno=0
372
#   define ioctlsocket(a,b,c) ioctl(a,b,c)
373
#   define closesocket(s)   close(s)
374
#   define readsocket(s,b,n)  read((s),(char*)(b),(n))
375
#   define writesocket(s,b,n) write((s),(char*)(b),(n)
376
*/
377
#   define accept(a,b,c)        accept(a,(struct sockaddr *)b,c)
378
#   define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
379
#  endif
380
# endif
381
382
# ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
383
1.99k
#  define CRYPTO_memcmp memcmp
384
# endif
385
386
# ifndef OPENSSL_NO_SECURE_MEMORY
387
   /* unistd.h defines _POSIX_VERSION */
388
#  if (defined(OPENSSL_SYS_UNIX) \
389
        && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
390
             || defined(__sun) || defined(__hpux) || defined(__sgi)      \
391
             || defined(__osf__) )) \
392
      || defined(_WIN32)
393
      /* secure memory is implemented */
394
#   else
395
#     define OPENSSL_NO_SECURE_MEMORY
396
#   endif
397
# endif
398
399
/*
400
 * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
401
 * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
402
 * There are also equivalent functions on Windows.
403
 * There is no locale_t on NONSTOP.
404
 */
405
# if defined(OPENSSL_SYS_WINDOWS)
406
#  define locale_t _locale_t
407
#  define freelocale _free_locale
408
#  define strcasecmp_l _stricmp_l
409
#  define strncasecmp_l _strnicmp_l
410
#  define strcasecmp _stricmp
411
#  define strncasecmp _strnicmp
412
# elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
413
     || defined(OPENSSL_SYS_TANDEM)
414
#  ifndef OPENSSL_NO_LOCALE
415
#   define OPENSSL_NO_LOCALE
416
#  endif
417
# endif
418
419
#endif