Coverage Report

Created: 2023-06-07 07:00

/src/botan/build/include/botan/internal/os_utils.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* OS specific utility functions
3
* (C) 2015,2016,2017,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_OS_UTILS_H_
9
#define BOTAN_OS_UTILS_H_
10
11
#include <botan/types.h>
12
#include <functional>
13
#include <string>
14
#include <vector>
15
16
namespace Botan::OS {
17
18
/*
19
* This header is internal (not installed) and these functions are not
20
* intended to be called by applications. However they are given public
21
* visibility (using BOTAN_TEST_API macro) for the tests. This also probably
22
* allows them to be overridden by the application on ELF systems, but
23
* this hasn't been tested.
24
*/
25
26
/**
27
* @return process ID assigned by the operating system.
28
*
29
* On Unix and Windows systems, this always returns a result
30
*
31
* On systems where there is no processes to speak of (for example on baremetal
32
* systems or within a unikernel), this function returns zero.
33
*/
34
uint32_t BOTAN_TEST_API get_process_id();
35
36
/**
37
* Test if we are currently running with elevated permissions
38
* eg setuid, setgid, or with POSIX caps set.
39
*/
40
bool running_in_privileged_state();
41
42
/**
43
* @return CPU processor clock, if available
44
*
45
* On Windows, calls QueryPerformanceCounter.
46
*
47
* Under GCC or Clang on supported platforms the hardware cycle counter is queried.
48
* Currently supported processors are x86, PPC, Alpha, SPARC, IA-64, S/390x, and HP-PA.
49
* If no CPU cycle counter is available on this system, returns zero.
50
*/
51
uint64_t BOTAN_TEST_API get_cpu_cycle_counter();
52
53
size_t BOTAN_TEST_API get_cpu_available();
54
55
/**
56
* Return the ELF auxiliary vector cooresponding to the given ID.
57
* This only makes sense on Unix-like systems and is currently
58
* only supported on Linux, Android, and FreeBSD.
59
*
60
* Returns zero if not supported on the current system or if
61
* the id provided is not known.
62
*/
63
unsigned long get_auxval(unsigned long id);
64
65
/*
66
* @return best resolution timestamp available
67
*
68
* The epoch and update rate of this clock is arbitrary and depending
69
* on the hardware it may not tick at a constant rate.
70
*
71
* Uses hardware cycle counter, if available.
72
* On POSIX platforms clock_gettime is used with a monotonic timer
73
* As a final fallback std::chrono::high_resolution_clock is used.
74
*/
75
uint64_t BOTAN_TEST_API get_high_resolution_clock();
76
77
/**
78
* @return system clock (reflecting wall clock) with best resolution
79
* available, normalized to nanoseconds resolution.
80
*/
81
uint64_t BOTAN_TEST_API get_system_timestamp_ns();
82
83
/**
84
* @return maximum amount of memory (in bytes) Botan could/should
85
* hyptothetically allocate for the memory poool. Reads environment
86
* variable "BOTAN_MLOCK_POOL_SIZE", set to "0" to disable pool.
87
*/
88
size_t get_memory_locking_limit();
89
90
/**
91
* Return the size of a memory page, if that can be derived on the
92
* current system. Otherwise returns some default value (eg 4096)
93
*/
94
size_t system_page_size();
95
96
/**
97
* Read the value of an environment variable, setting it to value_out if it
98
* exists.  Returns false and sets value_out to empty string if no such variable
99
* is set. If the process seems to be running in a privileged state (such as
100
* setuid) then always returns false and does not examine the environment.
101
*/
102
bool read_env_variable(std::string& value_out, std::string_view var_name);
103
104
/**
105
* Read the value of an environment variable and convert it to an
106
* integer. If not set or conversion fails, returns the default value.
107
*
108
* If the process seems to be running in a privileged state (such as setuid)
109
* then always returns nullptr, similiar to glibc's secure_getenv.
110
*/
111
size_t read_env_variable_sz(std::string_view var_name, size_t def_value = 0);
112
113
/**
114
* Request count pages of RAM which are locked into memory using mlock,
115
* VirtualLock, or some similar OS specific API. Free it with free_locked_pages.
116
*
117
* Returns an empty list on failure. This function is allowed to return fewer
118
* than count pages.
119
*
120
* The contents of the allocated pages are undefined.
121
*
122
* Each page is preceded by and followed by a page which is marked
123
* as noaccess, such that accessing it will cause a crash. This turns
124
* out of bound reads/writes into crash events.
125
*
126
* @param count requested number of locked pages
127
*/
128
std::vector<void*> allocate_locked_pages(size_t count);
129
130
/**
131
* Free memory allocated by allocate_locked_pages
132
* @param pages a list of pages returned by allocate_locked_pages
133
*/
134
void free_locked_pages(const std::vector<void*>& pages);
135
136
/**
137
* Set the MMU to prohibit access to this page
138
*/
139
void page_prohibit_access(void* page);
140
141
/**
142
* Set the MMU to allow R/W access to this page
143
*/
144
void page_allow_access(void* page);
145
146
/**
147
* Set a ID to a page's range expressed by size bytes
148
*/
149
void page_named(void* page, size_t size);
150
151
/**
152
* Run a probe instruction to test for support for a CPU instruction.
153
* Runs in system-specific env that catches illegal instructions; this
154
* function always fails if the OS doesn't provide this.
155
* Returns value of probe_fn, if it could run.
156
* If error occurs, returns negative number.
157
* This allows probe_fn to indicate errors of its own, if it wants.
158
* For example the instruction might not only be only available on some
159
* CPUs, but also buggy on some subset of these - the probe function
160
* can test to make sure the instruction works properly before
161
* indicating that the instruction is available.
162
*
163
* @warning on Unix systems uses signal handling in a way that is not
164
* thread safe. It should only be called in a single-threaded context
165
* (ie, at static init time).
166
*
167
* If probe_fn throws an exception the result is undefined.
168
*
169
* Return codes:
170
* -1 illegal instruction detected
171
*/
172
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function<int()>& probe_fn);
173
174
/**
175
* Represents a terminal state
176
*/
177
class BOTAN_UNSTABLE_API Echo_Suppression {
178
   public:
179
      /**
180
      * Reenable echo on this terminal. Can be safely called
181
      * multiple times. May throw if an error occurs.
182
      */
183
      virtual void reenable_echo() = 0;
184
185
      /**
186
      * Implicitly calls reenable_echo, but swallows/ignored all
187
      * errors which would leave the terminal in an invalid state.
188
      */
189
0
      virtual ~Echo_Suppression() = default;
190
};
191
192
/**
193
* Suppress echo on the terminal
194
* Returns null if this operation is not supported on the current system.
195
*/
196
std::unique_ptr<Echo_Suppression> BOTAN_UNSTABLE_API suppress_echo_on_terminal();
197
198
}  // namespace Botan::OS
199
200
#endif