Skip to content

Commit

Permalink
✨ log mmap syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
froz42 committed Oct 15, 2023
1 parent 58ffcc1 commit c08ba23
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ SRCS += syscall/syscall_get_description.c \
syscall/syscall_log.c \
syscall/syscall_log_param.c \
syscall/syscall_is_execve.c \
syscall/param_log/flags_log.c \
syscall/param_log/log_int.c \
syscall/param_log/log_signed_int.c \
syscall/param_log/log_none.c \
syscall/param_log/log_ptr.c \
syscall/param_log/log_hex.c \
syscall/param_log/log_memseg.c \
syscall/param_log/log_string.c \
syscall/param_log/log_open_flags.c \
Expand All @@ -52,6 +58,8 @@ SRCS += syscall/syscall_get_description.c \
syscall/param_log/log_poll_fds.c \
syscall/param_log/log_poll_fds_after.c \
syscall/param_log/log_seek_whence.c \
syscall/param_log/log_mmap_prot.c \
syscall/param_log/log_mmap_flags.c \
syscall/syscall_handle.c

# registers srcs
Expand Down
34 changes: 34 additions & 0 deletions srcs/syscall/param_log/flags_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "param_log.h"
#include <ft_printf.h>

/**
* @brief Log a set of flags
*
* @param flags the flags to log
* @param flag_strs the flags strings
* @param flag_strs_size the size of the flag strings array
* @return int the number of bytes written
*/
int flags_log(uint64_t flags, const flag_str_t *flag_strs, size_t flag_strs_size)
{
int size_written = 0;
bool_t first = true;
for (size_t i = 0; i < flag_strs_size; i++)
{
if (flags & flag_strs[i].flag)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%s", flag_strs[i].str);
first = false;
flags &= ~flag_strs[i].flag;
}
}
if (flags != 0)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%#llx", flags);
}
return size_written;
}
35 changes: 35 additions & 0 deletions srcs/syscall/param_log/log_mmap_flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "param_log.h"
#include <ft_printf.h>
#include <sys/mman.h>
#include <macros.h>

static const flag_str_t mmap_flags[] = {
FLAG_STR(MAP_SHARED),
FLAG_STR(MAP_PRIVATE),
FLAG_STR(MAP_FIXED),
FLAG_STR(MAP_ANONYMOUS),
FLAG_STR(MAP_GROWSDOWN),
FLAG_STR(MAP_DENYWRITE),
FLAG_STR(MAP_EXECUTABLE),
FLAG_STR(MAP_LOCKED),
FLAG_STR(MAP_NORESERVE),
FLAG_STR(MAP_POPULATE),
FLAG_STR(MAP_NONBLOCK),
FLAG_STR(MAP_STACK),
FLAG_STR(MAP_HUGETLB),
FLAG_STR(MAP_SYNC),
FLAG_STR(MAP_FIXED_NOREPLACE),
};

/**
* @brief Log mmap flags
*
* @param value the value to log
* @return int the number of bytes written
*/
int log_MMAP_FLAGS(uint64_t value)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "MAP_FILE");
return flags_log(value, mmap_flags, ELEM_COUNT(mmap_flags));
}
23 changes: 23 additions & 0 deletions srcs/syscall/param_log/log_mmap_prot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "param_log.h"
#include <ft_printf.h>
#include <sys/mman.h>
#include <macros.h>

static const flag_str_t mmap_prot_flags[] = {
FLAG_STR(PROT_READ),
FLAG_STR(PROT_WRITE),
FLAG_STR(PROT_EXEC),
};

/**
* @brief Log mmap protection flags
*
* @param value the value to log
* @return int the number of bytes written
*/
int log_MMAP_PROT(uint64_t value)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "PROT_NONE");
return flags_log(value, mmap_prot_flags, ELEM_COUNT(mmap_prot_flags));
}
28 changes: 27 additions & 1 deletion srcs/syscall/param_log/param_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ typedef struct

typedef int (*log_function_t)();

/**
* @brief Log a set of flags
*
* @param flags the flags to log
* @param flag_strs the flags strings
* @param flag_strs_size the size of the flag strings array
* @return int the number of bytes written
*/
int flags_log(uint64_t flags, const flag_str_t *flag_strs, size_t flag_strs_size);

/**
* @brief log a hexadecimal value
*
Expand Down Expand Up @@ -129,4 +139,20 @@ int log_POLL_FDS_AFTER(uint64_t value, syscall_log_param_t *context);
* @param value the value to log
* @return int the number of bytes written
*/
int log_SEEK_WHENCE(uint64_t value);
int log_SEEK_WHENCE(uint64_t value);

/**
* @brief Log mmap protection flags
*
* @param value the value to log
* @return int the number of bytes written
*/
int log_MMAP_PROT(uint64_t value);

/**
* @brief Log mmap flags
*
* @param value the value to log
* @return int the number of bytes written
*/
int log_MMAP_FLAGS(uint64_t value);
3 changes: 2 additions & 1 deletion srcs/syscall/syscall_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ static const syscall_description_t x86_64_syscalls[] = {
[5] = {"fstat", SIGNED_INT, {-INT, STAT_STRUCT, NONE}},
[6] = {"lstat", SIGNED_INT, {-STRING, STAT_STRUCT, NONE}},
[7] = {"poll", POLL_FDS_AFTER, {-POLL_FDS, -INT, -SIGNED_INT, NONE}},
[8] = {"lseek", SIGNED_INT, {-SIGNED_INT, -SIGNED_INT, -SEEK_WHENCE, NONE}}};
[8] = {"lseek", SIGNED_INT, {-SIGNED_INT, -SIGNED_INT, -SEEK_WHENCE, NONE}},
[9] = {"mmap", PTR, {-PTR, -INT, -MMAP_PROT, -MMAP_FLAGS, -SIGNED_INT, HEX}}};
2 changes: 2 additions & 0 deletions srcs/syscall/syscall_log_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static const log_function_t log_functions[] = {
ADD_LOGGER(POLL_FDS_AFTER),
ADD_LOGGER(SEEK_WHENCE),
ADD_LOGGER(PTR),
ADD_LOGGER(MMAP_PROT),
ADD_LOGGER(MMAP_FLAGS),
};

typedef int (*log_function_with_param_t)(uint64_t value, syscall_log_param_t *context);
Expand Down

0 comments on commit c08ba23

Please sign in to comment.