Skip to content

Commit c05eac0

Browse files
committed
CTL: Add a CTL sources to the UMF
This commit introduces sources and compilation of the CTL. The CTL sources are copied from: https://github.com/pmem/pmdk/tree/master/src/common Signed-off-by: Krzysztof Filipek <[email protected]>
1 parent 19bd9fd commit c05eac0

File tree

11 files changed

+1675
-3
lines changed

11 files changed

+1675
-3
lines changed

cmake/helpers.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,8 @@ function(add_umf_target_compile_options name)
232232
PRIVATE -fPIC
233233
-Wall
234234
-Wextra
235-
-Wpedantic
236235
-Wformat-security
237-
-Wcast-qual
236+
-Wno-cast-qual
238237
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=auto>)
239238
if(CMAKE_BUILD_TYPE STREQUAL "Release")
240239
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)

src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ add_subdirectory(coarse)
2626

2727
set(UMF_LIBS $<BUILD_INTERFACE:umf_utils> $<BUILD_INTERFACE:coarse>)
2828

29+
if(LINUX OR MACOSX)
30+
set(CTL_SOURCES
31+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/alloc.c
32+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl.c
33+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl_debug.c)
34+
endif()
35+
2936
if(LINUX)
3037
set(BA_SOURCES ${BA_SOURCES}
3138
${CMAKE_CURRENT_SOURCE_DIR}/base_alloc/base_alloc_linux.c)
@@ -45,6 +52,7 @@ set(HWLOC_DEPENDENT_SOURCES topology.c)
4552

4653
set(UMF_SOURCES
4754
${BA_SOURCES}
55+
${CTL_SOURCES}
4856
libumf.c
4957
ipc.c
5058
ipc_cache.c

src/ctl/alloc.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2019-2024, Intel Corporation */
3+
4+
#include <errno.h>
5+
6+
#include "alloc.h"
7+
#include <string.h>
8+
9+
// #include "fault_injection.h"
10+
11+
Malloc_func fn_malloc = malloc;
12+
Realloc_func fn_realloc = realloc;
13+
14+
#if FAULT_INJECTION
15+
#include "log_internal.h"
16+
17+
static __thread int malloc_num;
18+
static __thread int fail_malloc_num;
19+
static __thread const char *fail_malloc_from;
20+
21+
void *_flt_Malloc(size_t size, const char *func) {
22+
if (fail_malloc_from && strcmp(func, fail_malloc_from) == 0) {
23+
if (++malloc_num == fail_malloc_num) {
24+
errno = ENOMEM;
25+
return NULL;
26+
}
27+
}
28+
return fn_malloc(size);
29+
}
30+
31+
static __thread int realloc_num;
32+
static __thread int fail_realloc_num;
33+
static __thread const char *fail_realloc_from;
34+
35+
void *_flt_Realloc(void *ptr, size_t size, const char *func) {
36+
if (fail_realloc_from && strcmp(func, fail_realloc_from) == 0) {
37+
if (++realloc_num == fail_realloc_num) {
38+
errno = ENOMEM;
39+
return NULL;
40+
}
41+
}
42+
return fn_realloc(ptr, size);
43+
}
44+
45+
void core_inject_fault_at(enum pmem_allocation_type type, int nth,
46+
const char *at) {
47+
switch (type) {
48+
case PMEM_MALLOC:
49+
malloc_num = 0;
50+
fail_malloc_num = nth;
51+
fail_malloc_from = at;
52+
break;
53+
case PMEM_REALLOC:
54+
realloc_num = 0;
55+
fail_realloc_num = nth;
56+
fail_realloc_from = at;
57+
break;
58+
default:
59+
CORE_LOG_FATAL("unknown allocation type");
60+
}
61+
}
62+
63+
int core_fault_injection_enabled(void) { return 1; }
64+
#else
65+
void *_Malloc(size_t size) { return fn_malloc(size); }
66+
67+
void *_Realloc(void *ptr, size_t size) { return fn_realloc(ptr, size); }
68+
#endif
69+
70+
void set_func_malloc(void *(*malloc_func)(size_t size)) {
71+
fn_malloc = (malloc_func == NULL) ? malloc : malloc_func;
72+
}
73+
74+
void set_func_realloc(void *(*realloc_func)(void *ptr, size_t size)) {
75+
fn_realloc = (realloc_func == NULL) ? realloc : realloc_func;
76+
}
77+
78+
/*
79+
* our versions of malloc & friends start off pointing to the libc versions
80+
*/
81+
Free_func Free = free;
82+
Strdup_func Strdup = strdup;
83+
84+
/*
85+
* Zalloc -- allocate zeroed memory
86+
*/
87+
void *Zalloc(size_t sz) {
88+
void *ret = Malloc(sz);
89+
if (!ret) {
90+
return NULL;
91+
}
92+
return memset(ret, 0, sz);
93+
}
94+
95+
/*
96+
* util_set_alloc_funcs -- allow one to override malloc, etc.
97+
*/
98+
void util_set_alloc_funcs(void *(*malloc_func)(size_t size),
99+
void (*free_func)(void *ptr),
100+
void *(*realloc_func)(void *ptr, size_t size),
101+
char *(*strdup_func)(const char *s)) {
102+
set_func_malloc(malloc_func);
103+
Free = (free_func == NULL) ? free : free_func;
104+
set_func_realloc(realloc_func);
105+
Strdup = (strdup_func == NULL) ? strdup : strdup_func;
106+
}

src/ctl/alloc.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
/* Copyright 2019-2024, Intel Corporation */
3+
4+
#ifndef COMMON_ALLOC_H
5+
#define COMMON_ALLOC_H
6+
7+
#include <stdlib.h>
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
typedef void *(*Malloc_func)(size_t size);
14+
typedef void *(*Realloc_func)(void *ptr, size_t size);
15+
16+
extern Malloc_func fn_malloc;
17+
extern Realloc_func fn_realloc;
18+
19+
#if FAULT_INJECTION
20+
void *_flt_Malloc(size_t, const char *);
21+
void *_flt_Realloc(void *, size_t, const char *);
22+
23+
#define Malloc(size) _flt_Malloc(size, __func__)
24+
#define Realloc(ptr, size) _flt_Realloc(ptr, size, __func__)
25+
#else
26+
void *_Malloc(size_t);
27+
void *_Realloc(void *, size_t);
28+
29+
#define Malloc(size) _Malloc(size)
30+
#define Realloc(ptr, size) _Realloc(ptr, size)
31+
#endif
32+
33+
void set_func_malloc(void *(*malloc_func)(size_t size));
34+
void set_func_realloc(void *(*realloc_func)(void *ptr, size_t size));
35+
36+
/*
37+
* overridable names for malloc & friends used by this library
38+
*/
39+
typedef void (*Free_func)(void *ptr);
40+
typedef char *(*Strdup_func)(const char *s);
41+
42+
extern Free_func Free;
43+
extern Strdup_func Strdup;
44+
extern void *Zalloc(size_t sz);
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
#endif

0 commit comments

Comments
 (0)