Skip to content

Commit 5e8da6d

Browse files
committed
add more secure compilation flags
1 parent d970b9b commit 5e8da6d

37 files changed

+227
-104
lines changed

benchmark/ubench.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void do_benchmark(alloc_t *array, size_t iters, malloc_t malloc_f,
6363
int i = 0;
6464
do {
6565
array[i].ptr = malloc_f(provider, Alloc_size, 0);
66-
} while (array[i++].ptr != NULL && i < iters);
66+
} while (array[i++].ptr != NULL && i < (int)iters);
6767

6868
while (--i >= 0) {
6969
free_f(provider, array[i].ptr, Alloc_size);
@@ -110,14 +110,14 @@ UBENCH_EX(simple, glibc_malloc) {
110110

111111
static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
112112
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
113-
/* .visibility */ UMF_MEM_MAP_PRIVATE,
113+
/* .visibility = */ UMF_MEM_MAP_PRIVATE,
114+
/* .shm_name = */ NULL,
114115

115116
// NUMA config
116-
/* .nodemask = */ NULL,
117-
/* .maxnode = */ 0,
117+
/* .numa_list = */ NULL,
118+
/* .numa_list_len = */ 0,
118119
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
119-
120-
// others
120+
/* .part_size = */ 0,
121121
};
122122

123123
static void *w_umfMemoryProviderAlloc(void *provider, size_t size,

cmake/helpers.cmake

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,24 @@ function(add_umf_target_compile_options name)
9191
target_compile_options(
9292
${name}
9393
PRIVATE -fPIC
94+
# -fPIE
9495
-Wall
96+
-Wextra
97+
-Werror
9598
-Wpedantic
9699
-Wempty-body
97100
-Wunused-parameter
101+
-Wformat
102+
-Wformat-security
103+
-fstack-protector-strong
104+
-D_FORTIFY_SOURCE=2
105+
# -flto -fvisibility=hidden
98106
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=auto>)
99107
if(CMAKE_BUILD_TYPE STREQUAL "Release")
100108
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
101109
endif()
102110
if(UMF_DEVELOPER_MODE)
103-
target_compile_options(
104-
${name} PRIVATE -Werror -fno-omit-frame-pointer
105-
-fstack-protector-strong)
111+
target_compile_options(${name} PRIVATE -fno-omit-frame-pointer)
106112
endif()
107113
if(USE_GCOV)
108114
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -113,20 +119,30 @@ function(add_umf_target_compile_options name)
113119
elseif(MSVC)
114120
target_compile_options(
115121
${name}
116-
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/MP> # clang-cl.exe does not
117-
# support /MP
118-
/W3 /MD$<$<CONFIG:Debug>:d> /GS)
119-
120-
if(UMF_DEVELOPER_MODE)
121-
target_compile_options(${name} PRIVATE /WX /GS)
122-
endif()
122+
PRIVATE # clang-cl.exe does not support /MP
123+
$<$<CXX_COMPILER_ID:MSVC>:/MP>
124+
/W4
125+
/WX
126+
/MD$<$<CONFIG:Debug>:d>
127+
/Gy
128+
/GS
129+
/analyze
130+
/sdl
131+
# warning 6326: Potential comparison of a constant with
132+
# another constant
133+
/wd6326
134+
/DYNAMICBASE
135+
/HIGHENTROPYVA
136+
/NXCOMPAT
137+
/ALLOWISOLATION)
123138
endif()
124139
endfunction()
125140

126141
function(add_umf_target_link_options name)
127142
if(NOT MSVC)
128143
if(NOT APPLE)
129-
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now")
144+
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now"
145+
)# -z,noexecstack
130146
if(USE_GCOV)
131147
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
132148
message(

examples/basic/ipc_ipcapi_consumer.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,14 @@ int main(int argc, char *argv[]) {
135135
memset(recv_buffer, 0, RECV_BUFF_SIZE);
136136

137137
// receive a size of the IPC handle from the producer's
138-
ssize_t len = recv(producer_socket, recv_buffer, RECV_BUFF_SIZE, 0);
139-
if (len < 0) {
138+
ssize_t recv_len = recv(producer_socket, recv_buffer, RECV_BUFF_SIZE, 0);
139+
if (recv_len < 0) {
140140
fprintf(
141141
stderr,
142142
"[consumer] ERROR: receiving a size of the IPC handle failed\n");
143143
goto err_close_producer_socket;
144144
}
145+
size_t len = (size_t)recv_len;
145146

146147
size_t size_IPC_handle = *(size_t *)recv_buffer;
147148

@@ -151,11 +152,13 @@ int main(int argc, char *argv[]) {
151152
len, size_IPC_handle);
152153

153154
// send received size to the producer as a confirmation
154-
len = send(producer_socket, &size_IPC_handle, sizeof(size_IPC_handle), 0);
155-
if (len < 0) {
155+
recv_len =
156+
send(producer_socket, &size_IPC_handle, sizeof(size_IPC_handle), 0);
157+
if (recv_len < 0) {
156158
fprintf(stderr, "[consumer] ERROR: sending confirmation failed\n");
157159
goto err_close_producer_socket;
158160
}
161+
len = (size_t)recv_len;
159162

160163
fprintf(stderr,
161164
"[consumer] Sent a confirmation to the producer (%zu bytes)\n",
@@ -169,11 +172,13 @@ int main(int argc, char *argv[]) {
169172
}
170173

171174
// receive the IPC handle from the producer's
172-
len = recv(producer_socket, IPC_handle, size_IPC_handle, 0);
173-
if (len < 0) {
175+
recv_len = recv(producer_socket, IPC_handle, size_IPC_handle, 0);
176+
if (recv_len < 0) {
174177
fprintf(stderr, "[consumer] ERROR: receiving the IPC handle failed\n");
175178
goto err_free_IPC_handle;
176179
}
180+
len = (size_t)recv_len;
181+
177182
if (len < size_IPC_handle) {
178183
fprintf(stderr,
179184
"[consumer] ERROR: receiving the IPC handle failed - received "

include/umf/proxy_lib_new_delete.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ SOFTWARE.
4848
#include <stdlib.h>
4949
#endif // _WIN32
5050

51+
// disable warning C28196: The requirement that '(_Param_(1)>0)?(return!=0):(1)'
52+
// is not satisfied. (The expression does not evaluate to true.)
53+
// disable warning C6387: 'return' could be '0': this does not adhere to the
54+
// specification for the function 'new'.
55+
#if defined(_MSC_VER)
56+
#pragma warning(push)
57+
#pragma warning(disable : 28196)
58+
#pragma warning(disable : 6387)
59+
#endif // _MSC_VER
60+
5161
static inline void *internal_aligned_alloc(size_t alignment, size_t size) {
5262
#ifdef _WIN32
5363
return _aligned_malloc(size, alignment);
@@ -147,6 +157,11 @@ void *operator new[](std::size_t n, std::align_val_t al,
147157
const std::nothrow_t &) noexcept {
148158
return internal_aligned_alloc(static_cast<size_t>(al), n);
149159
}
160+
161+
#if defined(_MSC_VER)
162+
#pragma warning(pop)
163+
#endif // _MSC_VER
164+
150165
#endif // (__cplusplus > 201402L || defined(__cpp_aligned_new))
151166
#endif // defined(__cplusplus)
152167

src/base_alloc/base_alloc.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
#include "utils_log.h"
1515
#include "utils_sanitizers.h"
1616

17+
// disable 4200 warning: nonstandard extension used: zero-sized array in
18+
// struct/union used in umf_ba_chunk_t, umf_ba_pool_t and umf_ba_next_pool_t
19+
#if defined(_MSC_VER)
20+
#pragma warning(push)
21+
#pragma warning(disable : 4200)
22+
#endif // _MSC_VER
23+
1724
// minimum size of a single pool of the base allocator
1825
#define MINIMUM_POOL_SIZE (ba_os_get_page_size())
1926

@@ -219,6 +226,12 @@ void *umf_ba_alloc(umf_ba_pool_t *pool) {
219226
// we'll mark the memory as undefined
220227
utils_annotate_memory_defined(chunk, sizeof(*chunk));
221228

229+
// check if the free list is not empty
230+
if (pool->metadata.free_list == NULL) {
231+
LOG_ERR("pool->metadata.free_list == NULL");
232+
return NULL;
233+
}
234+
222235
pool->metadata.free_list = pool->metadata.free_list->next;
223236
pool->metadata.n_allocs++;
224237
#ifndef NDEBUG
@@ -305,3 +318,7 @@ void umf_ba_destroy(umf_ba_pool_t *pool) {
305318
util_mutex_destroy_not_free(&pool->metadata.free_lock);
306319
ba_os_free(pool, size);
307320
}
321+
322+
#if defined(_MSC_VER)
323+
#pragma warning(pop)
324+
#endif // _MSC_VER

src/base_alloc/base_alloc_linear.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
#include "utils_concurrency.h"
1515
#include "utils_log.h"
1616

17+
// disable 4200 warning: nonstandard extension used: zero-sized array in
18+
// struct/union used in umf_ba_linear_pool and umf_ba_next_linear_pool_t
19+
#if defined(_MSC_VER)
20+
#pragma warning(push)
21+
#pragma warning(disable : 4200)
22+
#endif // _MSC_VER
23+
1724
#ifndef NDEBUG
1825
#define _DEBUG_EXECUTE(expression) DO_WHILE_EXPRS(expression)
1926
#else
@@ -197,9 +204,9 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
197204
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
198205
(pool->metadata.pool_size > page_size)) {
199206
// we can free the first (main) pool except of the first page containing the metadata
200-
void *ptr = (char *)pool + page_size;
207+
void *pool_ptr = (char *)pool + page_size;
201208
size_t size = pool->metadata.pool_size - page_size;
202-
ba_os_free(ptr, size);
209+
ba_os_free(pool_ptr, size);
203210
// update pool_size
204211
pool->metadata.pool_size = page_size;
205212
}
@@ -222,9 +229,9 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
222229
assert(prev_pool->next_pool == next_pool);
223230
prev_pool->next_pool = next_pool->next_pool;
224231
_DEBUG_EXECUTE(pool->metadata.n_pools--);
225-
void *ptr = next_pool;
232+
void *next_pool_ptr = next_pool;
226233
size_t size = next_pool->pool_size;
227-
ba_os_free(ptr, size);
234+
ba_os_free(next_pool_ptr, size);
228235
}
229236
_DEBUG_EXECUTE(ba_debug_checks(pool));
230237
util_mutex_unlock(&pool->metadata.lock);
@@ -295,3 +302,7 @@ size_t umf_ba_linear_pool_contains_pointer(umf_ba_linear_pool_t *pool,
295302
util_mutex_unlock(&pool->metadata.lock);
296303
return 0;
297304
}
305+
306+
#if defined(_MSC_VER)
307+
#pragma warning(pop)
308+
#endif // _MSC_VER

src/base_alloc/base_alloc_windows.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ void *ba_os_alloc(size_t size) {
1616
return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
1717
}
1818

19-
void ba_os_free(void *ptr, size_t size) { VirtualFree(ptr, 0, MEM_RELEASE); }
19+
void ba_os_free(void *ptr, size_t size) {
20+
(void)size;
21+
VirtualFree(ptr, 0, MEM_RELEASE);
22+
}
2023

2124
static void _ba_os_init_page_size(void) {
2225
SYSTEM_INFO SystemInfo;

src/cpp_helpers.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ template <typename T, typename ParamType> umf_memory_pool_ops_t poolMakeCOps() {
117117
}
118118

119119
if constexpr (std::is_same_v<ParamType, void>) {
120+
(void)params; // unused
120121
return detail::initialize<T>(reinterpret_cast<T *>(*obj),
121122
std::make_tuple(provider));
122123
} else {
@@ -145,6 +146,7 @@ umf_memory_provider_ops_t providerMakeCOps() {
145146
}
146147

147148
if constexpr (std::is_same_v<ParamType, void>) {
149+
(void)params; // unused
148150
return detail::initialize<T>(reinterpret_cast<T *>(*obj),
149151
std::make_tuple());
150152
} else {

src/ipc_internal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
#include <umf/base.h>
1414

15+
// disable 4200 warning: nonstandard extension used: zero-sized array in
16+
// struct/union used in umf_ipc_data_t
17+
#if defined(_MSC_VER)
18+
#pragma warning(push)
19+
#pragma warning(disable : 4200)
20+
#endif // _MSC_VER
21+
1522
#ifdef __cplusplus
1623
extern "C" {
1724
#endif
@@ -31,4 +38,8 @@ typedef struct umf_ipc_data_t {
3138
}
3239
#endif
3340

41+
#if defined(_MSC_VER)
42+
#pragma warning(pop)
43+
#endif // _MSC_VER
44+
3445
#endif /* UMF_IPC_INTERNAL_H */

src/libumf_windows.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#if defined(UMF_SHARED_LIBRARY) /* SHARED LIBRARY */
1515

1616
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
17+
(void)hinstDLL; // unused
18+
(void)lpvReserved; // unused
19+
1720
if (fdwReason == DLL_PROCESS_ATTACH) {
1821
(void)umfInit();
1922
} else if (fdwReason == DLL_PROCESS_DETACH) {
@@ -32,6 +35,10 @@ INIT_ONCE init_once_flag = INIT_ONCE_STATIC_INIT;
3235

3336
BOOL CALLBACK initOnceCb(PINIT_ONCE InitOnce, PVOID Parameter,
3437
PVOID *lpContext) {
38+
(void)InitOnce;
39+
(void)Parameter;
40+
(void)lpContext;
41+
3542
int ret = umfInit();
3643
atexit(umfTearDown);
3744
return (ret == 0) ? TRUE : FALSE;

src/memory_targets/memory_target_numa.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
#include <assert.h>
11-
#include <hwloc.h>
1211
#include <stdlib.h>
1312

1413
#include <umf/pools/pool_disjoint.h>

src/memspace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ umf_result_t umfMemspaceFilter(umf_memspace_handle_t hMemspace,
265265
}
266266

267267
size_t cloneIdx = 0;
268-
for (size_t cloneIdx = 0; cloneIdx < newMemspace->size; cloneIdx++) {
268+
for (cloneIdx = 0; cloneIdx < newMemspace->size; cloneIdx++) {
269269
ret = umfMemoryTargetClone(uniqueBestNodes[cloneIdx],
270270
&newMemspace->nodes[cloneIdx]);
271271
if (ret != UMF_RESULT_SUCCESS) {

src/memspaces/memspace_highest_bandwidth.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <assert.h>
1111
#include <ctype.h>
12-
#include <hwloc.h>
1312
#include <stdlib.h>
1413

1514
#include "base_alloc_global.h"

src/memspaces/memspace_highest_capacity.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
#include <assert.h>
11-
#include <hwloc.h>
1211
#include <stdlib.h>
1312

1413
#include "base_alloc_global.h"

src/memspaces/memspace_host_all.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
#include <assert.h>
11-
#include <hwloc.h>
1211
#include <stdlib.h>
1312

1413
#include "base_alloc_global.h"

src/memspaces/memspace_lowest_latency.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <assert.h>
1111
#include <ctype.h>
12-
#include <hwloc.h>
1312
#include <stdlib.h>
1413

1514
#include "base_alloc_global.h"

0 commit comments

Comments
 (0)