Skip to content
Merged
2 changes: 1 addition & 1 deletion src/common/capio/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ inline const std::filesystem::path &get_capio_dir() {
capio_dir = std::filesystem::path(buf.get());
for (auto &forbidden_path : CAPIO_DIR_FORBIDDEN_PATHS) {
if (capio_dir.native().rfind(forbidden_path, 0) == 0) {
ERR_EXIT("CAPIO_DIR inside %s file system is not supported", forbidden_path);
ERR_EXIT("CAPIO_DIR inside %s file system is not supported", forbidden_path.data());
}
}
}
Expand Down
27 changes: 23 additions & 4 deletions src/common/capio/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ inline auto open_server_logfile() {
std::to_string(capio_syscall(SYS_gettid)) + ".log";

logfile.open(logfile_name, std::ofstream::out);
capio_delete_vec(&hostname);
delete[] hostname;

return logfile_name;
}
Expand Down Expand Up @@ -319,6 +319,12 @@ class Logger {
#define ERR_EXIT(message, ...) \
log.log(message, ##__VA_ARGS__); \
if (!continue_on_error) { \
char tmp_buf[1024]; \
sprintf(tmp_buf, message, ##__VA_ARGS__); \
char node_name[HOST_NAME_MAX]{0}; \
gethostname(node_name, HOST_NAME_MAX); \
printf("%s [ %s ] %s\n", CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, node_name, tmp_buf); \
fflush(stdout); \
exit(EXIT_FAILURE); \
}
#define LOG(message, ...) log.log(message, ##__VA_ARGS__)
Expand Down Expand Up @@ -355,9 +361,22 @@ class Logger {

#else

#define ERR_EXIT(message, ...) \
if (!continue_on_error) \
exit(EXIT_FAILURE)
#ifndef __CAPIO_POSIX
inline bool syscall_no_intercept_flag = false;
#endif

#define ERR_EXIT(fmt, ...) \
if (!continue_on_error) { \
syscall_no_intercept_flag = true; \
char tmp_buf[1024]; \
sprintf(tmp_buf, fmt, ##__VA_ARGS__); \
char node_name[HOST_NAME_MAX]{0}; \
gethostname(node_name, HOST_NAME_MAX); \
printf("%s [ %s ] %s\n", CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, node_name, tmp_buf); \
fflush(stdout); \
exit(EXIT_FAILURE); \
}

#define LOG(message, ...)
#define START_LOG(tid, message, ...)
#define START_SYSCALL_LOGGING()
Expand Down
60 changes: 39 additions & 21 deletions src/common/capio/shm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CapioShmCanary {
auto message = new char[strlen(CAPIO_SHM_CANARY_ERROR)];
sprintf(message, CAPIO_SHM_CANARY_ERROR, _canary_name.data());
std::cout << CAPIO_SERVER_CLI_LOG_SERVER_ERROR << message << std::endl;
capio_delete_vec(&message);
delete[] message;
#endif
ERR_EXIT("ERR: shm canary flag already exists");
}
Expand Down Expand Up @@ -118,25 +118,46 @@ void *create_shm(const std::string &shm_name, const long int size) {
return p;
}

auto get_shm_size(int shm_fd, const char *shm_name) {
START_LOG(capio_syscall(SYS_gettid), "call(fd=%ld)", shm_fd);
struct stat sb = {0};
/* Open existing object */
/* Use shared memory object size as length argument for mmap()
and as number of bytes to write() */
if (fstat(shm_fd, &sb) == -1) {
ERR_EXIT("fstat %s", shm_name);
}

if (sb.st_size <= 0) {
LOG("WARN: size of stat is %ld. Retry once.", sb.st_size);
if (fstat(shm_fd, &sb) == -1) {
ERR_EXIT("fstat %s", shm_name);
}
if (sb.st_size <= 0) {
LOG("WARN: retry no. 2 gave a size of %ld", sb.st_size);
ERR_EXIT("FATAL: unable to obtain size of shm object %s after two tries...", shm_name);
}
}

LOG("Size of shm object %s : %ld", shm_name, sb.st_size);
return sb.st_size;
}

void *get_shm(const std::string &shm_name) {
START_LOG(capio_syscall(SYS_gettid), "call(shm_name=%s)", shm_name.c_str());

// if we are not creating a new object, mode is equals to 0
int fd = shm_open(shm_name.c_str(), O_RDWR, 0); // to be closed
struct stat sb = {0};
int fd = shm_open(shm_name.c_str(), O_RDWR, 0); // to be closed
if (fd == -1) {
ERR_EXIT("get_shm shm_open %s", shm_name.c_str());
}
/* Open existing object */
/* Use shared memory object size as length argument for mmap()
and as number of bytes to write() */
if (fstat(fd, &sb) == -1) {
ERR_EXIT("fstat %s", shm_name.c_str());
}
void *p = mmap(nullptr, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

auto size = get_shm_size(fd, shm_name.c_str());

void *p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
LOG("ERROR MMAP arg dump:");
LOG("mmap-size: %ld", sb.st_size);
LOG("mmap-size: %ld", size);
LOG("mmap-prot: %ld", PROT_READ | PROT_WRITE);
LOG("mmap-flags: %ld", MAP_SHARED);
LOG("mmap-fd: %ld", fd);
Expand All @@ -152,24 +173,21 @@ void *get_shm_if_exist(const std::string &shm_name) {
START_LOG(capio_syscall(SYS_gettid), "call(shm_name=%s)", shm_name.c_str());

// if we are not creating a new object, mode is equals to 0
int fd = shm_open(shm_name.c_str(), O_RDWR, 0); // to be closed
struct stat sb = {0};
int fd = shm_open(shm_name.c_str(), O_RDWR, 0); // to be closed

if (fd == -1) {
if (errno == ENOENT) {
return nullptr;
}
ERR_EXIT("ERROR: unable to open shared memory %s: %s", shm_name.c_str(), strerror(errno));
}
/* Open existing object */
/* Use shared memory object size as length argument for mmap()
and as number of bytes to write() */
if (fstat(fd, &sb) == -1) {
ERR_EXIT("fstat %s", shm_name.c_str());
}
void *p = mmap(nullptr, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

auto size = get_shm_size(fd, shm_name.c_str());

void *p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
LOG("ERROR MMAP arg dump:");
LOG("mmap-size: %ld", sb.st_size);
LOG("mmap-size: %ld", size);
LOG("mmap-prot: %ld", PROT_READ | PROT_WRITE);
LOG("mmap-flags: %ld", MAP_SHARED);
LOG("mmap-fd: %ld", fd);
Expand Down
48 changes: 0 additions & 48 deletions src/common/capio/utils.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/posix/handlers/exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int exit_handler(long arg0, long arg1, long arg2, long arg3, long arg4, long arg
LOG("Removed caches");

if (const auto itm = bufs_response->find(tid); itm != bufs_response->end()) {
capio_delete(&itm->second);
delete itm->second;
bufs_response->erase(tid);
LOG("Removed response buffer");
}
Expand Down
26 changes: 23 additions & 3 deletions src/posix/libcapio_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* logs up to CAPIO_MAX_LOG_LEVEL function calls
*/

#include <capio/utils.h>

#include <array>
#include <string>

Expand Down Expand Up @@ -411,7 +409,29 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3,
}

LOG("Handling syscall NO %ld (max num is %ld)", syscall_number, CAPIO_NR_SYSCALLS);
return syscallTable[syscall_number](arg0, arg1, arg2, arg3, arg4, arg5, result);
try {
return syscallTable[syscall_number](arg0, arg1, arg2, arg3, arg4, arg5, result);
} catch (const std::exception &exception) {
syscall_no_intercept_flag = true;

std::cout
<< std::endl
<< "~~~~~~~~~~~~~~[\033[31mlibcapio_posix.so: FATAL EXCEPTION\033[0m]~~~~~~~~~~~~~~"
<< std::endl
<< "| Exception thrown while handling syscall " << syscall_number << std::endl
<< "| TID of offending thread: " << syscall_no_intercept(SYS_gettid) << std::endl
<< "| PID of offending thread: " << syscall_no_intercept(SYS_getpid) << std::endl
<< "| PPID of offending thread: " << syscall_no_intercept(SYS_getppid) << std::endl
<< "| " << std::endl
<< "| `" << typeid(exception).name() << ": " << exception.what() << std::endl
<< "|" << std::endl
<< "~~~~~~~~~~~~~~[\033[31mlibcapio_posix.so: FATAL EXCEPTION\033[0m]~~~~~~~~~~~~~~"
<< std::endl
<< std::endl;

ERR_EXIT("%s", exception.what());
}
return 1;
}

static __attribute__((constructor)) void init() {
Expand Down
14 changes: 7 additions & 7 deletions src/posix/utils/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ inline void init_caches() {

inline void delete_caches() {
START_LOG(capio_syscall(SYS_gettid), "call()");
capio_delete(&write_request_cache_fs);
capio_delete(&read_request_cache_fs);
capio_delete(&consent_request_cache_fs);
capio_delete(&write_request_cache_mem);
capio_delete(&read_request_cache_mem);
delete write_request_cache_fs;
delete read_request_cache_fs;
delete consent_request_cache_fs;
delete write_request_cache_mem;
delete read_request_cache_mem;

capio_delete(&cts_queue);
delete cts_queue;
LOG("Removed cts_queue");
capio_delete(&stc_queue);
delete stc_queue;
LOG("Removed stc_queue");
}

Expand Down
2 changes: 1 addition & 1 deletion src/posix/utils/cache/consent_request_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConsentRequestCache {

~ConsentRequestCache() {
START_LOG(capio_syscall(SYS_gettid), "call()");
capio_delete(&available_consent);
delete available_consent;
};

void consent_request(const std::filesystem::path &path, long tid,
Expand Down
2 changes: 1 addition & 1 deletion src/posix/utils/cache/read_request_cache_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ReadRequestCacheFS {

~ReadRequestCacheFS() {
START_LOG(capio_syscall(SYS_gettid), "call()");
capio_delete(&available_read_cache);
delete available_read_cache;
};

void read_request(std::filesystem::path path, const long end_of_read, int tid, const int fd) {
Expand Down
2 changes: 1 addition & 1 deletion src/posix/utils/cache/read_request_cache_mem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ReadRequestCacheMEM {

~ReadRequestCacheMEM() {
START_LOG(capio_syscall(SYS_gettid), "call()");
capio_delete_vec(&_cache);
delete[] _cache;
}

void flush() {
Expand Down
6 changes: 3 additions & 3 deletions src/posix/utils/clone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ inline std::condition_variable clone_cv;
inline std::unordered_set<pid_t> *tids;

inline bool is_capio_tid(const pid_t tid) {
lockguard_guard(const std::lock_guard lg(clone_mutex));
const std::lock_guard lg(clone_mutex);
return tids->find(tid) != tids->end();
}

inline void register_capio_tid(const pid_t tid) {
START_LOG(syscall_no_intercept(SYS_gettid), "call(tid=%ld)", tid);
lockguard_guard(const std::lock_guard lg(clone_mutex));
const std::lock_guard lg(clone_mutex);
tids->insert(tid);
}

inline void remove_capio_tid(const pid_t tid) {
START_LOG(syscall_no_intercept(SYS_gettid), "call(tid=%ld)", tid);
lockguard_guard(std::lock_guard lg(clone_mutex));
std::lock_guard lg(clone_mutex);
if (tids->find(tid) != tids->end()) {
tids->erase(tid);
}
Expand Down
6 changes: 3 additions & 3 deletions src/posix/utils/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ inline void delete_capio_path(const std::string &path) {
*/
inline void destroy_filesystem() {
current_dir.reset();
capio_delete(&capio_files_descriptors);
capio_delete(&capio_files_paths);
capio_delete(&files);
delete capio_files_descriptors;
delete capio_files_paths;
delete files;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/posix/utils/requests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ inline void init_client() {
inline void handshake_request(const long tid, const long pid, const std::string &app_name) {
START_LOG(capio_syscall(SYS_gettid), "call(tid=%ld, pid=%ld, app_name=%s)", tid, pid,
app_name.c_str());
char req[CAPIO_REQ_MAX_SIZE];
sprintf(req, "%04d %ld %ld %s", CAPIO_REQUEST_HANDSHAKE, tid, pid, app_name.c_str());
buf_requests->write(req, CAPIO_REQ_MAX_SIZE);
LOG("Sent handshake request");

cts_queue = new SPSCQueue("queue-" + std::to_string(tid) + ".cts", get_cache_lines(),
get_cache_line_size(), get_capio_workflow_name(), true);
stc_queue = new SPSCQueue("queue-" + std::to_string(tid) + ".stc", get_cache_lines(),
get_cache_line_size(), get_capio_workflow_name(), true);
LOG("Initialized data transfer queues");

char req[CAPIO_REQ_MAX_SIZE];
sprintf(req, "%04d %ld %ld %s", CAPIO_REQUEST_HANDSHAKE, tid, pid, app_name.c_str());
buf_requests->write(req, CAPIO_REQ_MAX_SIZE);
LOG("Sent handshake request");
}

inline std::vector<std::regex> *file_in_memory_request(const long pid) {
Expand All @@ -69,7 +70,7 @@ inline std::vector<std::regex> *file_in_memory_request(const long pid) {
stc_queue->read(file, PATH_MAX);
LOG("Obtained path %s", file);
regex_vector->emplace_back(generateCapioRegex(file));
capio_delete_vec(&file);
delete[] file;
}
return regex_vector;
}
Expand Down
10 changes: 4 additions & 6 deletions src/server/capio-cl-engine/capio_cl_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*
*/
class CapioCLEngine {
friend class CapioFileManager;

private:
std::unordered_map<std::string, // path name
std::tuple<std::vector<std::string>, // Vector for producers [0]
Expand Down Expand Up @@ -438,12 +440,8 @@ class CapioCLEngine {
return files;
}

std::vector<std::string> getPathsInConfig() {
std::vector<std::string> paths;
std::transform(_locations.begin(), _locations.end(), std::back_inserter(paths),
[](auto pair) { return pair.first; });
return paths;
}
protected:
const auto *getLocations() const { return &_locations; }
};

inline CapioCLEngine *capio_cl_engine;
Expand Down
Loading