jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
callocator_sec.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef C_ALLOCATOR_SEC_HPP
26#define C_ALLOCATOR_SEC_HPP
27
28#include <cinttypes>
29#include <memory>
30#include <cstring>
31
32#include <jau/basic_types.hpp>
33#include <jau/secmem.hpp>
34
35namespace jau {
36
37/**
38 * A simple secure allocator for integral types using POSIX C functions: <code>::malloc()</code> and <code>::free()</code>.
39 *
40 * callocator_sec is similar to callocator, but
41 * - only works for integral types
42 * - deallocate explicitly zero_bytes_sec() the memory before free for secure scrubbing.
43 * - dropped realloc() for security reasons, since realloc() could free old memory block w/o scrubbing.
44 *
45 * This class shall be compliant with <i>C++ named requirements for Allocator</i>.
46 *
47 * Not implementing deprecated (C++17) and removed (C++20)
48 * methods: address(), max_size(), construct() and destroy().
49 */
50template <typename T,
51 std::enable_if_t< std::is_integral_v<T>, bool> = true>
53{
54 public:
55 // typedefs' for C++ named requirements: Allocator
56 typedef T value_type;
57 typedef std::size_t size_type;
58 typedef std::ptrdiff_t difference_type;
60
61 // C++17, deprecated in C++20
62 typedef std::true_type is_always_equal;
63
64 // deprecated in C++17 and removed in C++20
65 typedef T* pointer;
66 typedef const T* const_pointer;
67 typedef T& reference;
68 typedef const T& const_reference;
69 template <class U> struct rebind {typedef callocator_sec<U> other;};
70
71 private:
72 typedef std::remove_const_t<T> value_type_mutable;
73 /** Required to create and move immutable elements, aka const */
74 typedef value_type_mutable* pointer_mutable;
75
76 public:
77 callocator_sec() noexcept = default; // C++11
78
79#if __cplusplus > 201703L
80 constexpr callocator_sec(const callocator_sec&) noexcept // NOLINT(modernize-use-equals-default)
81 {} // C++20
82#else
83 callocator_sec(const callocator_sec&) noexcept // NOLINT(modernize-use-equals-default)
84 { }
85#endif
86
87#if __cplusplus > 201703L
88 template <typename U>
89 constexpr callocator_sec(const callocator_sec<U>&) noexcept // NOLINT(modernize-use-equals-default)
90 { } // C++20
91#else
92 template <typename U>
93 callocator_sec(const callocator_sec<U>&) noexcept // NOLINT(modernize-use-equals-default)
94 { }
95#endif
96
97#if __cplusplus > 201703L
98 constexpr ~callocator_sec() = default; // C++20
99#else
100 ~callocator_sec() = default;
101#endif
102
103#if __cplusplus <= 201703L
104 value_type* allocate(std::size_t n, const void*) { // C++17 deprecated; C++20 removed
105 return reinterpret_cast<value_type*>( ::malloc( n * sizeof(value_type) ) );
106 }
107#endif
108
109#if __cplusplus > 201703L
110 [[nodiscard]] constexpr value_type* allocate(std::size_t n) { // C++20
111 return reinterpret_cast<value_type*>( ::malloc( n * sizeof(value_type) ) );
112 }
113#else
114 value_type* allocate(std::size_t n) { // C++17
115 return reinterpret_cast<value_type*>( ::malloc( n * sizeof(value_type) ) );
116 }
117#endif
118
119#if __cplusplus > 201703L
120 constexpr void deallocate(value_type* p, std::size_t n ) noexcept {
121 zero_bytes_sec(p, n);
122 ::free( reinterpret_cast<void*>( const_cast<pointer_mutable>(p) ) );
123 }
124#else
125 void deallocate(value_type* p, std::size_t n ) noexcept {
126 zero_bytes_sec(p, n);
127 ::free( reinterpret_cast<void*>( const_cast<pointer_mutable>(p) ) );
128 }
129#endif
130
131};
132
133
134#if __cplusplus > 201703L
135template <class T1, class T2>
136 constexpr bool operator==(const callocator_sec<T1>& lhs, const callocator_sec<T2>& rhs) noexcept {
137#if 0
138 if( &lhs == &rhs ) {
139 return true;
140 }
141 return lhs.memory_usage == rhs.memory_usage;
142#else
143 (void)lhs;
144 (void)rhs;
145 return true;
146#endif
147 }
148#else
149 template <class T1, class T2>
150 bool operator==(const callocator_sec<T1>& lhs, const callocator_sec<T2>& rhs) noexcept {
151#if 0
152 if( &lhs == &rhs ) {
153 return true;
154 }
155 return lhs.memory_usage == rhs.memory_usage;
156#else
157 (void)lhs;
158 (void)rhs;
159 return true;
160#endif
161 }
162 template <class T1, class T2>
163 bool operator!=(const callocator_sec<T1>& lhs, const callocator_sec<T2>& rhs) noexcept {
164 return !(lhs==rhs);
165 }
166#endif
167
168} /* namespace jau */
169
170#endif // C_ALLOCATOR_SEC_HPP
171
void zero_bytes_sec(void *s, size_t n) noexcept __attrdecl_no_optimize__
Wrapper to ::explicit_bzero(), ::bzero() or ::memset(), whichever is available in that order.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
bool operator==(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
bool operator!=(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
A simple secure allocator for integral types using POSIX C functions: ::malloc() and ::free().
std::true_type is_always_equal
std::ptrdiff_t difference_type
value_type * allocate(std::size_t n)
std::true_type propagate_on_container_move_assignment
callocator_sec(const callocator_sec< U > &) noexcept
~callocator_sec()=default
callocator_sec() noexcept=default
value_type * allocate(std::size_t n, const void *)
callocator_sec(const callocator_sec &) noexcept
void deallocate(value_type *p, std::size_t n) noexcept