Botan  2.10.0
Crypto and TLS for C++11
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 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 */
35 
36 /**
37 * Test if we are currently running with elevated permissions
38 * eg setuid, setgid, or with POSIX caps set.
39 */
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 */
52 
53 /*
54 * @return best resolution timestamp available
55 *
56 * The epoch and update rate of this clock is arbitrary and depending
57 * on the hardware it may not tick at a constant rate.
58 *
59 * Uses hardware cycle counter, if available.
60 * On POSIX platforms clock_gettime is used with a monotonic timer
61 * As a final fallback std::chrono::high_resolution_clock is used.
62 */
64 
65 /**
66 * @return system clock (reflecting wall clock) with best resolution
67 * available, normalized to nanoseconds resolution.
68 */
70 
71 /**
72 * @return maximum amount of memory (in bytes) Botan could/should
73 * hyptothetically allocate for the memory poool. Reads environment
74 * variable "BOTAN_MLOCK_POOL_SIZE", set to "0" to disable pool.
75 */
77 
78 /**
79 * Return the size of a memory page, if that can be derived on the
80 * current system. Otherwise returns some default value (eg 4096)
81 */
82 size_t system_page_size();
83 
84 /**
85 * Read the value of an environment variable. Return nullptr if
86 * no such variable is set. If the process seems to be running in
87 * a privileged state (such as setuid) then always returns nullptr,
88 * similiar to glibc's secure_getenv.
89 */
90 const char* read_env_variable(const std::string& var_name);
91 
92 /**
93 * Read the value of an environment variable and convert it to an
94 * integer. If not set or conversion fails, returns the default value.
95 *
96 * If the process seems to be running in a privileged state (such as setuid)
97 * then always returns nullptr, similiar to glibc's secure_getenv.
98 */
99 size_t read_env_variable_sz(const std::string& var_name, size_t def_value = 0);
100 
101 /**
102 * Request @count pages of RAM which are locked into memory using mlock,
103 * VirtualLock, or some similar OS specific API. Free it with free_locked_pages.
104 *
105 * Returns an empty list on failure. This function is allowed to return fewer
106 * than @count pages.
107 *
108 * The contents of the allocated pages are undefined.
109 *
110 * Each page is preceded by and followed by a page which is marked
111 * as noaccess, such that accessing it will cause a crash. This turns
112 * out of bound reads/writes into crash events.
113 *
114 * @param count requested number of locked pages
115 */
116 std::vector<void*> allocate_locked_pages(size_t count);
117 
118 /**
119 * Free memory allocated by allocate_locked_pages
120 * @param pages a list of pages returned by allocate_locked_pages
121 */
122 void free_locked_pages(const std::vector<void*>& pages);
123 
124 /**
125 * Set the MMU to prohibit access to this page
126 */
127 void page_prohibit_access(void* page);
128 
129 /**
130 * Set the MMU to allow R/W access to this page
131 */
132 void page_allow_access(void* page);
133 
134 
135 /**
136 * Run a probe instruction to test for support for a CPU instruction.
137 * Runs in system-specific env that catches illegal instructions; this
138 * function always fails if the OS doesn't provide this.
139 * Returns value of probe_fn, if it could run.
140 * If error occurs, returns negative number.
141 * This allows probe_fn to indicate errors of its own, if it wants.
142 * For example the instruction might not only be only available on some
143 * CPUs, but also buggy on some subset of these - the probe function
144 * can test to make sure the instruction works properly before
145 * indicating that the instruction is available.
146 *
147 * @warning on Unix systems uses signal handling in a way that is not
148 * thread safe. It should only be called in a single-threaded context
149 * (ie, at static init time).
150 *
151 * If probe_fn throws an exception the result is undefined.
152 *
153 * Return codes:
154 * -1 illegal instruction detected
155 */
156 int BOTAN_TEST_API run_cpu_instruction_probe(std::function<int ()> probe_fn);
157 
158 /**
159 * Represents a terminal state
160 */
162  {
163  public:
164  /**
165  * Reenable echo on this terminal. Can be safely called
166  * multiple times. May throw if an error occurs.
167  */
168  virtual void reenable_echo() = 0;
169 
170  /**
171  * Implicitly calls reenable_echo, but swallows/ignored all
172  * errors which would leave the terminal in an invalid state.
173  */
174  virtual ~Echo_Suppression() = default;
175  };
176 
177 /**
178 * Suppress echo on the terminal
179 * Returns null if this operation is not supported on the current system.
180 */
181 std::unique_ptr<Echo_Suppression> BOTAN_UNSTABLE_API suppress_echo_on_terminal();
182 
183 }
184 
185 }
186 
187 #endif
#define BOTAN_UNSTABLE_API
Definition: compiler.h:38
int BOTAN_TEST_API run_cpu_instruction_probe(std::function< int()> probe_fn)
Definition: os_utils.cpp:459
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition: os_utils.cpp:105
uint32_t BOTAN_TEST_API get_process_id()
Definition: os_utils.cpp:81
void free_locked_pages(const std::vector< void *> &pages)
Definition: os_utils.cpp:421
void page_prohibit_access(void *page)
Definition: os_utils.cpp:409
#define BOTAN_TEST_API
Definition: compiler.h:45
size_t get_memory_locking_limit()
Definition: os_utils.cpp:251
bool running_in_privileged_state()
Definition: os_utils.cpp:94
std::vector< void * > allocate_locked_pages(size_t count)
Definition: os_utils.cpp:334
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:218
Definition: alg_id.cpp:13
size_t read_env_variable_sz(const std::string &var_name, size_t def_value=0)
Definition: os_utils.cpp:319
std::unique_ptr< Echo_Suppression > BOTAN_UNSTABLE_API suppress_echo_on_terminal()
Definition: os_utils.cpp:514
size_t system_page_size()
Definition: os_utils.cpp:232
void page_allow_access(void *page)
Definition: os_utils.cpp:397
const char * read_env_variable(const std::string &var_name)
Definition: os_utils.cpp:311
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:166