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