Skip to content

Commit 4df5659

Browse files
Merge pull request #557 from bratpiorka/rrudnick_sdl_flags
add more secure compilation flags
2 parents 0176f45 + 2bb6908 commit 4df5659

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+246
-121
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ option(UMF_BUILD_BENCHMARKS_MT "Build UMF multithreaded benchmarks" OFF)
3838
option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
3939
option(UMF_BUILD_FUZZTESTS "Build UMF fuzz tests" OFF)
4040
option(UMF_BUILD_GPU_EXAMPLES "Build UMF GPU examples" OFF)
41-
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
42-
OFF)
41+
option(UMF_DEVELOPER_MODE "Enable additional developer checks" OFF)
4342
option(UMF_FORMAT_CODE_STYLE
4443
"Add clang, cmake, and black -format-check and -format-apply targets"
4544
OFF)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ List of options provided by CMake:
118118
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
119119
| UMF_BUILD_FUZZTESTS | Build UMF fuzz tests | ON/OFF | OFF |
120120
| UMF_BUILD_GPU_EXAMPLES | Build UMF GPU examples | ON/OFF | OFF |
121-
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
121+
| UMF_DEVELOPER_MODE | Enable additional developer checks | ON/OFF | OFF |
122122
| UMF_FORMAT_CODE_STYLE | Add clang, cmake, and black -format-check and -format-apply targets to make | ON/OFF | OFF |
123123
| UMF_TESTS_FAIL_ON_SKIP | Treat skips in tests as fail | ON/OFF | OFF |
124124
| USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |

benchmark/ubench.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
*
88
*/
99

10+
#include <stdbool.h>
11+
12+
#ifndef _WIN32
13+
#include <unistd.h>
14+
#endif
15+
1016
#include <umf/ipc.h>
1117
#include <umf/memory_pool.h>
1218
#include <umf/pools/pool_proxy.h>
@@ -22,19 +28,28 @@
2228
#include <umf/pools/pool_jemalloc.h>
2329
#endif
2430

25-
#include <stdbool.h>
26-
27-
#ifndef _WIN32
28-
#include <unistd.h>
29-
#endif
30-
31-
#include "ubench.h"
3231
#include "utils_common.h"
3332

3433
#if (defined UMF_BUILD_GPU_TESTS)
3534
#include "utils_level_zero.h"
3635
#endif
3736

37+
// NOTE: with strict compilation flags, ubench compilation throws some
38+
// warnings. We disable them here because we do not want to change the ubench
39+
// code.
40+
41+
// disable warning 6308:'realloc' might return null pointer: assigning null
42+
// pointer to 'failed_benchmarks', which is passed as an argument to 'realloc',
43+
// will cause the original memory block to be leaked.
44+
// disable warning 6001: Using uninitialized memory
45+
// '*ubench_state.benchmarks.name'.
46+
#if defined(_MSC_VER)
47+
#pragma warning(push)
48+
#pragma warning(disable : 6308)
49+
#pragma warning(disable : 6001)
50+
#endif // _MSC_VER
51+
52+
#include "ubench.h"
3853
// BENCHMARK CONFIG
3954
#define N_ITERATIONS 1000
4055
#define ALLOC_SIZE (util_get_page_size())
@@ -63,7 +78,7 @@ static void do_benchmark(alloc_t *array, size_t iters, malloc_t malloc_f,
6378
int i = 0;
6479
do {
6580
array[i].ptr = malloc_f(provider, Alloc_size, 0);
66-
} while (array[i++].ptr != NULL && i < iters);
81+
} while (array[i++].ptr != NULL && i < (int)iters);
6782

6883
while (--i >= 0) {
6984
free_f(provider, array[i].ptr, Alloc_size);
@@ -110,14 +125,18 @@ UBENCH_EX(simple, glibc_malloc) {
110125

111126
static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
112127
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
113-
/* .visibility */ UMF_MEM_MAP_PRIVATE,
128+
/* .visibility = */ UMF_MEM_MAP_PRIVATE,
129+
/* .shm_name = */ NULL,
114130

115131
// NUMA config
116-
/* .nodemask = */ NULL,
117-
/* .maxnode = */ 0,
132+
/* .numa_list = */ NULL,
133+
/* .numa_list_len = */ 0,
134+
118135
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
136+
/* .part_size = */ 0,
119137

120-
// others
138+
/* .partitions = */ NULL,
139+
/* .partitions_len = */ 0,
121140
};
122141

123142
static void *w_umfMemoryProviderAlloc(void *provider, size_t size,
@@ -487,3 +506,7 @@ UBENCH_EX(ipc, disjoint_pool_with_level_zero_provider) {
487506
#endif /* (defined UMF_BUILD_LIBUMF_POOL_DISJOINT && defined UMF_BUILD_LEVEL_ZERO_PROVIDER && defined UMF_BUILD_GPU_TESTS) */
488507

489508
UBENCH_MAIN()
509+
510+
#if defined(_MSC_VER)
511+
#pragma warning(pop)
512+
#endif // _MSC_VER

cmake/helpers.cmake

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,20 @@ function(add_umf_target_compile_options name)
9292
${name}
9393
PRIVATE -fPIC
9494
-Wall
95+
-Wextra
96+
-Werror
9597
-Wpedantic
9698
-Wempty-body
9799
-Wunused-parameter
100+
-Wformat
101+
-Wformat-security
98102
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=auto>)
99103
if(CMAKE_BUILD_TYPE STREQUAL "Release")
100104
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
101105
endif()
102106
if(UMF_DEVELOPER_MODE)
103-
target_compile_options(
104-
${name} PRIVATE -Werror -fno-omit-frame-pointer
105-
-fstack-protector-strong)
107+
target_compile_options(${name} PRIVATE -fno-omit-frame-pointer
108+
-fstack-protector-strong)
106109
endif()
107110
if(USE_GCOV)
108111
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -113,12 +116,26 @@ function(add_umf_target_compile_options name)
113116
elseif(MSVC)
114117
target_compile_options(
115118
${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)
119+
PRIVATE /MD$<$<CONFIG:Debug>:d>
120+
$<$<CONFIG:Release>:/sdl>
121+
/analyze
122+
/DYNAMICBASE
123+
/W4
124+
/WX
125+
/Gy
126+
/GS
127+
# disable warning 6326: Potential comparison of a constant
128+
# with another constant
129+
/wd6326
130+
# disable 4200 warning: nonstandard extension used:
131+
# zero-sized array in struct/union
132+
/wd4200)
133+
if(${CMAKE_C_COMPILER_ID} MATCHES "MSVC")
134+
target_compile_options(
135+
${name}
136+
PRIVATE # below flags are not recognized by Clang
137+
/MP $<$<CONFIG:Release>:/LTCG>
138+
$<$<CONFIG:Release>:/NXCOMPAT>)
122139
endif()
123140
endif()
124141
endfunction()

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 "

examples/basic/ipc_level_zero.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ int create_level_zero_pool(ze_context_handle_t context,
2020
ze_device_handle_t device,
2121
umf_memory_pool_handle_t *pool) {
2222
// setup params
23-
level_zero_memory_provider_params_t params = {0};
23+
level_zero_memory_provider_params_t params;
2424
params.level_zero_context_handle = context;
2525
params.level_zero_device_handle = device;
2626
params.memory_type = UMF_MEMORY_TYPE_DEVICE;
27+
2728
// create Level Zero provider
2829
umf_memory_provider_handle_t provider = 0;
2930
umf_result_t umf_result = umfMemoryProviderCreate(

include/umf/proxy_lib_new_delete.h

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

51+
// disable warning 28251: "Inconsistent annotation for 'new': this instance
52+
// has no annotations." because different Win SDKs use slightly different
53+
// definitions of new
54+
#if defined(_MSC_VER)
55+
#pragma warning(push)
56+
#pragma warning(disable : 28251)
57+
#endif // _MSC_VER
58+
5159
static inline void *internal_aligned_alloc(size_t alignment, size_t size) {
5260
#ifdef _WIN32
5361
return _aligned_malloc(size, alignment);
@@ -75,10 +83,18 @@ void operator delete(void *p, const std::nothrow_t &) noexcept { free(p); }
7583
void operator delete[](void *p, const std::nothrow_t &) noexcept { free(p); }
7684

7785
decl_new(n) void *operator new(std::size_t n) noexcept(false) {
78-
return malloc(n);
86+
void *ptr = malloc(n);
87+
if (ptr == nullptr) {
88+
throw std::bad_alloc();
89+
}
90+
return ptr;
7991
}
8092
decl_new(n) void *operator new[](std::size_t n) noexcept(false) {
81-
return malloc(n);
93+
void *ptr = malloc(n);
94+
if (ptr == nullptr) {
95+
throw std::bad_alloc();
96+
}
97+
return ptr;
8298
}
8399

84100
decl_new_nothrow(n) void *operator new(std::size_t n,
@@ -134,10 +150,18 @@ void operator delete[](void *p, std::align_val_t al,
134150
}
135151

136152
void *operator new(std::size_t n, std::align_val_t al) noexcept(false) {
137-
return internal_aligned_alloc(static_cast<size_t>(al), n);
153+
void *ptr = internal_aligned_alloc(static_cast<size_t>(al), n);
154+
if (ptr == nullptr) {
155+
throw std::bad_alloc();
156+
}
157+
return ptr;
138158
}
139159
void *operator new[](std::size_t n, std::align_val_t al) noexcept(false) {
140-
return internal_aligned_alloc(static_cast<size_t>(al), n);
160+
void *ptr = internal_aligned_alloc(static_cast<size_t>(al), n);
161+
if (ptr == nullptr) {
162+
throw std::bad_alloc();
163+
}
164+
return ptr;
141165
}
142166
void *operator new(std::size_t n, std::align_val_t al,
143167
const std::nothrow_t &) noexcept {
@@ -147,6 +171,11 @@ void *operator new[](std::size_t n, std::align_val_t al,
147171
const std::nothrow_t &) noexcept {
148172
return internal_aligned_alloc(static_cast<size_t>(al), n);
149173
}
174+
175+
#if defined(_MSC_VER)
176+
#pragma warning(pop)
177+
#endif // _MSC_VER
178+
150179
#endif // (__cplusplus > 201402L || defined(__cpp_aligned_new))
151180
#endif // defined(__cplusplus)
152181

src/base_alloc/base_alloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ void *umf_ba_alloc(umf_ba_pool_t *pool) {
219219
// we'll mark the memory as undefined
220220
utils_annotate_memory_defined(chunk, sizeof(*chunk));
221221

222+
// check if the free list is not empty
223+
if (pool->metadata.free_list == NULL) {
224+
LOG_ERR("base_alloc: Free list should not be empty before new alloc");
225+
return NULL;
226+
}
227+
222228
pool->metadata.free_list = pool->metadata.free_list->next;
223229
pool->metadata.n_allocs++;
224230
#ifndef NDEBUG

src/base_alloc/base_alloc_linear.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
197197
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
198198
(pool->metadata.pool_size > page_size)) {
199199
// we can free the first (main) pool except of the first page containing the metadata
200-
void *ptr = (char *)pool + page_size;
200+
void *pool_ptr = (char *)pool + page_size;
201201
size_t size = pool->metadata.pool_size - page_size;
202-
ba_os_free(ptr, size);
202+
ba_os_free(pool_ptr, size);
203203
// update pool_size
204204
pool->metadata.pool_size = page_size;
205205
}
@@ -222,9 +222,9 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
222222
assert(prev_pool->next_pool == next_pool);
223223
prev_pool->next_pool = next_pool->next_pool;
224224
_DEBUG_EXECUTE(pool->metadata.n_pools--);
225-
void *ptr = next_pool;
225+
void *next_pool_ptr = next_pool;
226226
size_t size = next_pool->pool_size;
227-
ba_os_free(ptr, size);
227+
ba_os_free(next_pool_ptr, size);
228228
}
229229
_DEBUG_EXECUTE(ba_debug_checks(pool));
230230
util_mutex_unlock(&pool->metadata.lock);

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; // unused
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ template <typename T> umf_memory_provider_ops_t providerOpsBase() {
108108
template <typename T, typename ParamType> umf_memory_pool_ops_t poolMakeCOps() {
109109
umf_memory_pool_ops_t ops = detail::poolOpsBase<T>();
110110

111-
ops.initialize = [](umf_memory_provider_handle_t provider, void *params,
112-
void **obj) {
111+
ops.initialize = [](umf_memory_provider_handle_t provider,
112+
[[maybe_unused]] void *params, void **obj) {
113113
try {
114114
*obj = new T;
115115
} catch (...) {
@@ -137,7 +137,7 @@ template <typename T, typename ParamType>
137137
umf_memory_provider_ops_t providerMakeCOps() {
138138
umf_memory_provider_ops_t ops = detail::providerOpsBase<T>();
139139

140-
ops.initialize = [](void *params, void **obj) {
140+
ops.initialize = []([[maybe_unused]] void *params, void **obj) {
141141
try {
142142
*obj = new T;
143143
} catch (...) {

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; // unused
39+
(void)Parameter; // unused
40+
(void)lpContext; // unused
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>

0 commit comments

Comments
 (0)