Skip to content

Commit

Permalink
More checks
Browse files Browse the repository at this point in the history
  • Loading branch information
drowaudio committed May 20, 2024
1 parent 1cbe545 commit 01b4097
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 5 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,60 @@ Dynamic library to catch run-time safety violations heavily inspired by [RADSan]
- [ ] macOS

## Functions
- [ ] syscall (futex)
- Time
- [x] sleep
- [x] nanosleep
- [x] usleep
- Memory
- [x] malloc
- [x] calloc
- [x] realloc
- [x] free
- [ ] reallocf (macOS)
- [x] valloc
- [x] posix_memalign
- [x] mmap
- [x] munmap
- Threads
- [ ] pthread_create
- [ ] pthread_mutex_lock
- [ ] pthread_mutex_unlock
- [ ] pthread_join
- [ ] pthread_cond_signal
- [ ] pthread_cond_broadcast
- [ ] pthread_cond_wait
- [ ] pthread_cond_timedwait
- [ ] pthread_rwlock_rdlock
- [ ] pthread_rwlock_unlock
- [ ] pthread_rwlock_wrlock
- [ ] pthread_spin_lock
- Files
- [ ] open
- [ ] openat
- [ ] close
- [ ] fopen
- [ ] fread
- [ ] fwrite
- [ ] fclose
- [ ] fcntl
- [ ] creat
- [ ] puts
- [ ] fputs
- [x] stat
- [ ] stat64
- [ ] fstat
- [ ] fstat64
- IO
- [ ] socket
- [ ] send
- [ ] sendmsg
- [ ] sendto
- [ ] recv
- [ ] recvmsg
- [ ] recvfrom
- [ ] shutdown
- System calls
- [x] syscall

## CI/Tests
- [ ] Failures
Expand Down
85 changes: 81 additions & 4 deletions src/lib_rt_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "lib_rt_check.h"

Expand Down Expand Up @@ -64,18 +65,66 @@ extern "C" void *malloc(size_t size)
{
log_function_if_realtime_context (__func__);

static auto real_malloc = (void* (*)(size_t))dlsym(RTLD_NEXT, "malloc");
return real_malloc(size);
static auto real = (void* (*)(size_t))dlsym(RTLD_NEXT, "malloc");
return real(size);
}

extern "C" void *calloc(size_t size, size_t item_size)
{
log_function_if_realtime_context (__func__);

static auto real = (void* (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
return real(size, item_size);
}

extern "C" void *realloc(void *ptr, size_t new_size)
{
log_function_if_realtime_context (__func__);

static auto real = (void* (*)(void*, size_t))dlsym(RTLD_NEXT, "realloc");
return real(ptr, new_size);
}

extern "C" void *valloc(size_t size)
{
log_function_if_realtime_context (__func__);

static auto real = (void* (*)(size_t))dlsym(RTLD_NEXT, "valloc");
return real(size);
}

extern "C" void free(void* ptr)
{
log_function_if_realtime_context (__func__);

static auto real_free = (void (*)(void*))dlsym(RTLD_NEXT, "free");
return real_free(ptr);
static auto real = (void (*)(void*))dlsym(RTLD_NEXT, "free");
return real(ptr);
}

extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size)
{
log_function_if_realtime_context (__func__);

static auto real = (int (*)(void**, size_t, size_t))dlsym(RTLD_NEXT, "posix_memalign");
return real(memptr, alignment, size);
}

extern "C" void *mmap(void* addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
log_function_if_realtime_context (__func__);

static auto real = (void* (*)(void*, size_t, int, int, int, off_t))dlsym(RTLD_NEXT, "mmap");
return real(addr, length, prot, flags, fd, offset);
}

extern "C" int munmap(void* addr, size_t length)
{
log_function_if_realtime_context (__func__);

static auto real = (int (*)(void*, size_t))dlsym(RTLD_NEXT, "munmap");
return real(addr, length);
}

// //==============================================================================
// // threads
Expand All @@ -96,6 +145,34 @@ extern "C" int pthread_mutex_unlock(pthread_mutex_t *mutex)
return real_pthread_mutex_unlock(mutex);
}

//==============================================================================
// sleep
//==============================================================================
extern "C" unsigned int sleep(unsigned int seconds)
{
log_function_if_realtime_context (__func__);

static auto real = (unsigned int (*)(unsigned int))dlsym(RTLD_NEXT, "sleep");
return real(seconds);
}

extern "C" int usleep(useconds_t useconds)
{
log_function_if_realtime_context (__func__);

static auto real = (int (*)(useconds_t))dlsym(RTLD_NEXT, "usleep");
return real(useconds);
}

extern "C" int nanosleep(const struct timespec *req,
struct timespec * rem)
{
log_function_if_realtime_context (__func__);

static auto real = (int (*)(const struct timespec *, struct timespec *))dlsym(RTLD_NEXT, "nanosleep");
return real(req, rem);
}

//==============================================================================
// files
//==============================================================================
Expand Down
10 changes: 10 additions & 0 deletions tests/fail_calloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <lib_rt_check.h>

int main()
{
realtime_context rc;

[[ maybe_unused ]] auto res = calloc (1024, 4);

return 0;
}
44 changes: 44 additions & 0 deletions tests/fail_mmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <lib_rt_check.h>

int main()
{
int fd = open("/usr/bin/awk", O_RDONLY);

if (fd == -1)
{
std::cout << "ERROR: open failed\n";
return 0;
}

struct stat s;
int status = fstat (fd, &s);
auto size = s.st_size;

char* map = nullptr;

{
realtime_context rc;
map = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0);
}

if (map == MAP_FAILED)
{
std::cout << "ERROR: MAP_FAILED\n";
return 0;
}

if (munmap(map, size) == -1)
{
std::cout << "ERROR: munmap failed\n";
return 0;
}

close(fd);

return 0;
}
46 changes: 46 additions & 0 deletions tests/fail_munmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <lib_rt_check.h>

int main()
{
int fd = open("/usr/bin/awk", O_RDONLY);

if (fd == -1)
{
std::cout << "ERROR: open failed\n";
return 0;
}

struct stat s;
int status = fstat (fd, &s);
auto size = s.st_size;

auto map = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0);

if (map == MAP_FAILED)
{
std::cout << "ERROR: MAP_FAILED\n";
return 0;
}

int res;

{
realtime_context rc;
res = munmap(map, size);
}

if (res == -1)
{
std::cout << "ERROR: munmap failed\n";
return 0;
}

close(fd);

return 0;
}
12 changes: 12 additions & 0 deletions tests/fail_nanosleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <unistd.h>
#include <lib_rt_check.h>

int main()
{
timespec req;

realtime_context rc;
nanosleep(&req, nullptr);

return 0;
}
11 changes: 11 additions & 0 deletions tests/fail_posix_memalign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <lib_rt_check.h>

int main()
{
realtime_context rc;

void* p;
[[ maybe_unused ]] auto res = posix_memalign (&p, 32, 128);

return 0;
}
11 changes: 11 additions & 0 deletions tests/fail_realloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <lib_rt_check.h>

int main()
{
auto res = malloc (1024);

realtime_context rc;
res = realloc (res, 1024 * 4);

return 0;
}
10 changes: 10 additions & 0 deletions tests/fail_sleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <unistd.h>
#include <lib_rt_check.h>

int main()
{
realtime_context rc;
sleep(1);

return 0;
}
13 changes: 13 additions & 0 deletions tests/fail_stdsleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <chrono>
#include <thread>
#include <lib_rt_check.h>

int main()
{
using namespace std::chrono_literals;

realtime_context rc;
std::this_thread::sleep_for (1ns);

return 0;
}
10 changes: 10 additions & 0 deletions tests/fail_usleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <unistd.h>
#include <lib_rt_check.h>

int main()
{
realtime_context rc;
usleep(100);

return 0;
}
10 changes: 10 additions & 0 deletions tests/fail_valloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <lib_rt_check.h>

int main()
{
realtime_context rc;

[[ maybe_unused ]] auto res = valloc (1024);

return 0;
}

0 comments on commit 01b4097

Please sign in to comment.