Coverage Report

Created: 2021-01-13 07:05

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