feat: Adding malloc wrapper for memory safety.
This commit is contained in:
44
src/list.c
Normal file
44
src/list.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "list.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void set_ll_append(struct SETBlockMeta *head, struct SETBlockMeta *next)
|
||||
{
|
||||
while (head->next)
|
||||
head = head->next;
|
||||
|
||||
head->next = next;
|
||||
next->prev = head;
|
||||
}
|
||||
|
||||
void set_ll_free_all(struct SETBlockMeta *head)
|
||||
{
|
||||
while (head)
|
||||
{
|
||||
void *block_start = head;
|
||||
head = head->next;
|
||||
free(block_start);
|
||||
}
|
||||
}
|
||||
|
||||
struct SETBlockMeta *set_ll_free_one(struct SETBlockMeta *head, void *address)
|
||||
{
|
||||
struct SETBlockMeta *meta = address - (sizeof(struct SETBlockMeta));
|
||||
|
||||
if (meta == head)
|
||||
{
|
||||
struct SETBlockMeta *ret = meta->next;
|
||||
ret->prev = NULL;
|
||||
ret->end = meta->end;
|
||||
free(meta);
|
||||
return ret;
|
||||
}
|
||||
|
||||
meta->prev->next = meta->next;
|
||||
if (meta->next)
|
||||
meta->next->prev = meta->prev;
|
||||
|
||||
free(meta);
|
||||
|
||||
return head;
|
||||
}
|
175
src/set.c
Normal file
175
src/set.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "set.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "set_asserts.h"
|
||||
|
||||
#ifdef COLORIZED
|
||||
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
#else
|
||||
|
||||
#define ANSI_COLOR_RED
|
||||
#define ANSI_COLOR_GREEN
|
||||
#define ANSI_COLOR_YELLOW
|
||||
#define ANSI_COLOR_BLUE
|
||||
#define ANSI_COLOR_MAGENTA
|
||||
#define ANSI_COLOR_CYAN
|
||||
#define ANSI_COLOR_RESET
|
||||
|
||||
#endif
|
||||
|
||||
// clang-format off
|
||||
#define BANNER \
|
||||
"" \
|
||||
" _____ _ _ _____ _ " \
|
||||
"_____ _ \n" \
|
||||
" / ___| | | | | ___| | | |_ " \
|
||||
"_| | | \n" \
|
||||
" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | " \
|
||||
"| ___ ___| |_ ___ _ __ \n" \
|
||||
" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ " \
|
||||
" | |/ _ / __| __/ _ | '__|\n" \
|
||||
" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | " \
|
||||
"| __\\__ | || __| | \n" \
|
||||
" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| " \
|
||||
"|_| \\_/\\___|___/\\__\\___|_| \n" \
|
||||
" __/ | " \
|
||||
" \n" \
|
||||
" |___/ " \
|
||||
" \n" \
|
||||
""
|
||||
//clang-format on
|
||||
|
||||
|
||||
static void log_test_summary(struct SETest *test)
|
||||
{
|
||||
if (test->passed)
|
||||
{
|
||||
printf("Test: %s" ANSI_COLOR_GREEN " - passed." ANSI_COLOR_RESET"\n",
|
||||
test->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test: %s" ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET
|
||||
" Reason: \n %s\n\n",
|
||||
test->name, test->error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_abnormal_termination(int status)
|
||||
{
|
||||
fprintf(stderr,
|
||||
ANSI_COLOR_RED "A test terminated with signal: %d" ANSI_COLOR_RESET,
|
||||
WTERMSIG(status));
|
||||
}
|
||||
|
||||
static void dispatch_single_test(struct SETest *test)
|
||||
{
|
||||
pid_t testPID = fork();
|
||||
|
||||
if (testPID == 0)
|
||||
{
|
||||
test->function(test);
|
||||
log_test_summary(test);
|
||||
// set_free_all();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
static void dispatch_tests(struct SETSuit *suit)
|
||||
{
|
||||
printf("\n\n==== Running suit: %s (%d) ====\n\n", suit->name, suit->len);
|
||||
|
||||
if (suit->setup != NULL && !suit->setup())
|
||||
{
|
||||
fprintf(stderr, "Suit not executed: Setup failed.\n");
|
||||
}
|
||||
|
||||
struct SETest *test = suit->tests;
|
||||
for (int i = 0; i < suit->len; i++)
|
||||
{
|
||||
dispatch_single_test(&test[i]);
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
// Wait for all tests.
|
||||
while (wait(&status) > 0)
|
||||
{
|
||||
if (WTERMSIG(status) != 0)
|
||||
log_abnormal_termination(status);
|
||||
};
|
||||
if (shmdt(suit->tests) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s\n", "Couldn't detach suit space after dispatch.");
|
||||
perror("shmat");
|
||||
exit(1);
|
||||
}
|
||||
if (shmctl(suit->shm_key, IPC_RMID, 0) == -1)
|
||||
{
|
||||
fprintf(stderr, "%s\n", "Couldn't remove suit space after dispatch.");
|
||||
perror("shctl");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (suit->tear_down != NULL && !suit->tear_down())
|
||||
{
|
||||
fprintf(stderr, "Teardown failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void dispatch_test_suits(struct SETSuit **suits, int counter)
|
||||
{
|
||||
for (int i = 0; i < counter; i++)
|
||||
{
|
||||
dispatch_tests(suits[i]);
|
||||
munmap((void*)suits[i], sizeof(struct SETSuit));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifndef NO_BANNER
|
||||
|
||||
printf("\n\n%s", BANNER);
|
||||
#endif /* ifndef NO_BANNER */
|
||||
|
||||
if (!set_up())
|
||||
{
|
||||
fprintf(stderr, "%s\n", "Setup call returned with error. Aborting execution.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
|
||||
set_bundle_suits(NULL, &counter, true);
|
||||
|
||||
struct SETSuit *suits[counter];
|
||||
|
||||
counter = 0;
|
||||
|
||||
set_bundle_suits(suits, &counter, false);
|
||||
|
||||
dispatch_test_suits(suits, counter);
|
||||
|
||||
if (!tear_down())
|
||||
{
|
||||
fprintf(stderr, "%s\n", "Teardown returned with error.");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
77
src/utils.c
Normal file
77
src/utils.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "list.h"
|
||||
#include <unistd.h>
|
||||
|
||||
struct SETBlockMeta *block_meta_head = NULL;
|
||||
|
||||
char *format_string(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
size_t size = vsnprintf(NULL, 0, fmt, args);
|
||||
char *out = (char *)malloc(size + 1);
|
||||
|
||||
if (!out)
|
||||
{
|
||||
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
|
||||
va_start(args, fmt);
|
||||
vsnprintf(out, size + 1, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
int create_shared_suit_space(size_t size)
|
||||
{
|
||||
int suit_space_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
|
||||
|
||||
if (suit_space_id == -1)
|
||||
{
|
||||
fprintf(stdout, "Couldn't create suite space of size: %lu\n", size);
|
||||
perror("shmget");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return suit_space_id;
|
||||
}
|
||||
|
||||
void *set_malloc(size_t n)
|
||||
{
|
||||
void *blocks = malloc(n + sizeof(struct SETBlockMeta));
|
||||
struct SETBlockMeta *meta = blocks;
|
||||
meta->next = NULL;
|
||||
|
||||
if (block_meta_head == NULL)
|
||||
{
|
||||
meta->end = meta;
|
||||
}
|
||||
else
|
||||
{
|
||||
meta->end = NULL;
|
||||
set_ll_append(block_meta_head->end, meta);
|
||||
block_meta_head->end = meta;
|
||||
}
|
||||
|
||||
return blocks + sizeof(struct SETBlockMeta);
|
||||
}
|
||||
|
||||
void set_free(void *address)
|
||||
{
|
||||
block_meta_head = set_ll_free_one(block_meta_head, address);
|
||||
}
|
||||
|
||||
void set_free_all()
|
||||
{
|
||||
set_ll_free_all(block_meta_head);
|
||||
block_meta_head = NULL;
|
||||
}
|
Reference in New Issue
Block a user