<commit_before>/* SPDX-License-Identifier: BSD-2-Clause */

#include <tilck/mods/tracing.h>

#include <sys/syscall.h> // system header

#if defined(__x86_64__)

   #define STAT_SYSCALL_N      SYS_stat
   #define LSTAT_SYSCALL_N     SYS_lstat
   #define FSTAT_SYSCALL_N     SYS_fstat
   #define FCNTL_SYSCALL_N     SYS_fcntl
   #define MMAP_SYSCALL_N      SYS_mmap

#elif defined(__i386__)

   #define STAT_SYSCALL_N      SYS_stat64
   #define LSTAT_SYSCALL_N     SYS_lstat64
   #define FSTAT_SYSCALL_N     SYS_fstat64
   #define FCNTL_SYSCALL_N     SYS_fcntl64
   #define MMAP_SYSCALL_N      192

   #undef SYS_getuid
   #undef SYS_getgid
   #undef SYS_geteuid
   #undef SYS_getegid

   #define SYS_getuid            199
   #define SYS_getgid            200
   #define SYS_geteuid           201
   #define SYS_getegid           202

   #define SYS_getuid16          24
   #define SYS_getgid16          47
   #define SYS_geteuid16         49
   #define SYS_getegid16         50

#else
   #error Architecture not supported
#endif


#define SIMPLE_PARAM(_name, _type, _kind)                \
   {                                                     \
      .name = _name,                                     \
      .type = _type,                                     \
      .kind = _kind,                                     \
   }

#define BUFFER_PARAM(_name, _type, _kind, _sz_param)                 \
   {                                                                 \
      .name = _name,                                                 \
      .type = _type,                                                 \
      .kind = _kind,                                                 \
      .size_param_name = _sz_param,                                  \
      .real_sz_in_ret = true,                                        \
   }

#define SYSCALL_TYPE_0(sys)                                          \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 0,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = { }                                                  \
   }

#define SYSCALL_TYPE_1(sys, par1)                                    \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 1,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in),               \
      }                                                              \
   }

#define SYSCALL_TYPE_2(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
         SIMPLE_PARAM(par2, &ptype_oct, sys_param_in),               \
      }                                                              \
   }

#define SYSCALL_TYPE_3(sys, par1)                                    \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 1,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
      }                                                              \
   }

#define SYSCALL_TYPE_4(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
         SIMPLE_PARAM(par2, &ptype_path, sys_param_in),              \
      }                                                              \
   }

#define SYSCALL_TYPE_5(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in),               \
         SIMPLE_PARAM(par2, &ptype_int, sys_param_in),               \
      }                                                              \
   }

#define SYSCALL_RW(sys, par1, par2, par2_type, par2_kind, par3)            \
   {                                                                       \
      .sys_n = sys,                                                        \
      .n_params = 3,                                                       \
      .exp_block = true,                                                   \
      .ret_type = &ptype_errno_or_val,                                     \
      .params = {                                                          \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in_out),                 \
         BUFFER_PARAM(par2, par2_type, par2_kind, par3),                   \
         SIMPLE_PARAM(par3, &ptype_int, sys_param_in),                     \
      },                                                                   \
   }

static const struct syscall_info __tracing_metadata[] =
{
   SYSCALL_TYPE_0(SYS_fork),
   SYSCALL_TYPE_0(SYS_pause),
   SYSCALL_TYPE_0(SYS_getuid),
   SYSCALL_TYPE_0(SYS_getgid),
   SYSCALL_TYPE_0(SYS_geteuid),
   SYSCALL_TYPE_0(SYS_getegid),

#if defined(__i386__)
   SYSCALL_TYPE_0(SYS_getuid16),
   SYSCALL_TYPE_0(SYS_getgid16),
   SYSCALL_TYPE_0(SYS_geteuid16),
   SYSCALL_TYPE_0(SYS_getegid16),
#endif

   SYSCALL_TYPE_0(SYS_gettid),
   SYSCALL_TYPE_0(SYS_setsid),
   SYSCALL_TYPE_0(SYS_sync),
   SYSCALL_TYPE_0(SYS_getppid),
   SYSCALL_TYPE_0(SYS_getpgrp),
   SYSCALL_TYPE_0(SYS_sched_yield),

   SYSCALL_TYPE_1(SYS_close, "fd"),
   SYSCALL_TYPE_1(SYS_dup, "dup"),
   SYSCALL_TYPE_1(SYS_getpgid, "pid"),
   SYSCALL_TYPE_1(SYS_getsid, "pid"),

   SYSCALL_TYPE_2(SYS_creat, "path", "mode"),
   SYSCALL_TYPE_2(SYS_chmod, "path", "mode"),
   SYSCALL_TYPE_2(SYS_mkdir, "path", "mode"),
   SYSCALL_TYPE_2(SYS_access, "path", "mode"),

   SYSCALL_TYPE_3(SYS_unlink, "path"),
   SYSCALL_TYPE_3(SYS_rmdir, "path"),
   SYSCALL_TYPE_3(SYS_chdir, "path"),

   SYSCALL_TYPE_4(SYS_link, "oldpath", "newpath"),
   SYSCALL_TYPE_4(SYS_symlink, "target", "linkpath"),
   SYSCALL_TYPE_4(SYS_rename, "oldpath", "newpath"),

   SYSCALL_TYPE_5(SYS_setpgid, "pid", "pgid"),
   SYSCALL_TYPE_5(SYS_dup2, "oldfd", "newfd"),
   SYSCALL_TYPE_5(SYS_kill, "pid", "sig"),
   SYSCALL_TYPE_5(SYS_tkill, "tid", "sig"),

   SYSCALL_RW(SYS_read, "fd", "buf", &ptype_buffer, sys_param_out, "count"),
   SYSCALL_RW(SYS_write, "fd", "buf", &ptype_buffer, sys_param_in, "count"),
   SYSCALL_RW(SYS_readv, "fd", "iov", &ptype_iov_out, sys_param_out, "iovcnt"),
   SYSCALL_RW(SYS_writev, "fd", "iov", &ptype_iov_in, sys_param_in, "iovcnt"),

   {
      .sys_n = SYS_exit,
      .n_params = 1,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,
      .params = {
         SIMPLE_PARAM("status", &ptype_int, sys_param_in),
      }
   },

   {
      .sys_n = SYS_exit_group,
      .n_params = 1,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,
      .params = {
         SIMPLE_PARAM("status", &ptype_int, sys_param_in),
      }
   },

   {
      .sys_n = SYS_vfork,
      .n_params = 0,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,<commit_msg>[tracing] Add metadata for chown, lchown, fchown<commit_after>/* SPDX-License-Identifier: BSD-2-Clause */

#include <tilck/mods/tracing.h>

#include <sys/syscall.h> // system header

#if defined(__x86_64__)

   #define STAT_SYSCALL_N      SYS_stat
   #define LSTAT_SYSCALL_N     SYS_lstat
   #define FSTAT_SYSCALL_N     SYS_fstat
   #define FCNTL_SYSCALL_N     SYS_fcntl
   #define MMAP_SYSCALL_N      SYS_mmap

#elif defined(__i386__)

   #define STAT_SYSCALL_N      SYS_stat64
   #define LSTAT_SYSCALL_N     SYS_lstat64
   #define FSTAT_SYSCALL_N     SYS_fstat64
   #define FCNTL_SYSCALL_N     SYS_fcntl64
   #define MMAP_SYSCALL_N        192

   #undef SYS_getuid
   #undef SYS_getgid
   #undef SYS_geteuid
   #undef SYS_getegid

   #define SYS_getuid            199
   #define SYS_getgid            200
   #define SYS_geteuid           201
   #define SYS_getegid           202

   #define SYS_getuid16           24
   #define SYS_getgid16           47
   #define SYS_geteuid16          49
   #define SYS_getegid16          50

   #undef SYS_lchown
   #undef SYS_fchown
   #undef SYS_chown

   #define SYS_lchown            198
   #define SYS_fchown            207
   #define SYS_chown             212

   #define SYS_lchown16           16
   #define SYS_fchown16           95
   #define SYS_chown16           182

#else
   #error Architecture not supported
#endif


#define SIMPLE_PARAM(_name, _type, _kind)                \
   {                                                     \
      .name = _name,                                     \
      .type = _type,                                     \
      .kind = _kind,                                     \
   }

#define BUFFER_PARAM(_name, _type, _kind, _sz_param)                 \
   {                                                                 \
      .name = _name,                                                 \
      .type = _type,                                                 \
      .kind = _kind,                                                 \
      .size_param_name = _sz_param,                                  \
      .real_sz_in_ret = true,                                        \
   }

/* Syscall (void) */
#define SYSCALL_TYPE_0(sys)                                          \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 0,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = { }                                                  \
   }

/* Syscall (int) */
#define SYSCALL_TYPE_1(sys, par1)                                    \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 1,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in),               \
      }                                                              \
   }

/* Syscall (path, oct_int) */
#define SYSCALL_TYPE_2(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
         SIMPLE_PARAM(par2, &ptype_oct, sys_param_in),               \
      }                                                              \
   }

/* Syscall (path) */
#define SYSCALL_TYPE_3(sys, par1)                                    \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 1,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
      }                                                              \
   }

/* Syscall (path, path) */
#define SYSCALL_TYPE_4(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
         SIMPLE_PARAM(par2, &ptype_path, sys_param_in),              \
      }                                                              \
   }

/* Syscall (int, int) */
#define SYSCALL_TYPE_5(sys, par1, par2)                              \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 2,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in),               \
         SIMPLE_PARAM(par2, &ptype_int, sys_param_in),               \
      }                                                              \
   }

/* Syscall (path, int, int) */
#define SYSCALL_TYPE_6(sys, par1, par2, par3)                        \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 3,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_path, sys_param_in),              \
         SIMPLE_PARAM(par2, &ptype_int, sys_param_in),               \
         SIMPLE_PARAM(par3, &ptype_int, sys_param_in),               \
      }                                                              \
   }

/* Syscall (int, int, int) */
#define SYSCALL_TYPE_7(sys, par1, par2, par3)                        \
   {                                                                 \
      .sys_n = sys,                                                  \
      .n_params = 3,                                                 \
      .exp_block = false,                                            \
      .ret_type = &ptype_errno_or_val,                               \
      .params = {                                                    \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in),               \
         SIMPLE_PARAM(par2, &ptype_int, sys_param_in),               \
         SIMPLE_PARAM(par3, &ptype_int, sys_param_in),               \
      }                                                              \
   }

/* Syscall (int, buffer_type, int) */
#define SYSCALL_RW(sys, par1, par2, par2_type, par2_kind, par3)            \
   {                                                                       \
      .sys_n = sys,                                                        \
      .n_params = 3,                                                       \
      .exp_block = true,                                                   \
      .ret_type = &ptype_errno_or_val,                                     \
      .params = {                                                          \
         SIMPLE_PARAM(par1, &ptype_int, sys_param_in_out),                 \
         BUFFER_PARAM(par2, par2_type, par2_kind, par3),                   \
         SIMPLE_PARAM(par3, &ptype_int, sys_param_in),                     \
      },                                                                   \
   }

static const struct syscall_info __tracing_metadata[] =
{
   SYSCALL_TYPE_0(SYS_fork),
   SYSCALL_TYPE_0(SYS_pause),
   SYSCALL_TYPE_0(SYS_getuid),
   SYSCALL_TYPE_0(SYS_getgid),
   SYSCALL_TYPE_0(SYS_geteuid),
   SYSCALL_TYPE_0(SYS_getegid),

#if defined(__i386__)
   SYSCALL_TYPE_0(SYS_getuid16),
   SYSCALL_TYPE_0(SYS_getgid16),
   SYSCALL_TYPE_0(SYS_geteuid16),
   SYSCALL_TYPE_0(SYS_getegid16),
#endif

   SYSCALL_TYPE_0(SYS_gettid),
   SYSCALL_TYPE_0(SYS_setsid),
   SYSCALL_TYPE_0(SYS_sync),
   SYSCALL_TYPE_0(SYS_getppid),
   SYSCALL_TYPE_0(SYS_getpgrp),
   SYSCALL_TYPE_0(SYS_sched_yield),

   SYSCALL_TYPE_1(SYS_close, "fd"),
   SYSCALL_TYPE_1(SYS_dup, "dup"),
   SYSCALL_TYPE_1(SYS_getpgid, "pid"),
   SYSCALL_TYPE_1(SYS_getsid, "pid"),

   SYSCALL_TYPE_2(SYS_creat, "path", "mode"),
   SYSCALL_TYPE_2(SYS_chmod, "path", "mode"),
   SYSCALL_TYPE_2(SYS_mkdir, "path", "mode"),
   SYSCALL_TYPE_2(SYS_access, "path", "mode"),

   SYSCALL_TYPE_3(SYS_unlink, "path"),
   SYSCALL_TYPE_3(SYS_rmdir, "path"),
   SYSCALL_TYPE_3(SYS_chdir, "path"),

   SYSCALL_TYPE_4(SYS_link, "oldpath", "newpath"),
   SYSCALL_TYPE_4(SYS_symlink, "target", "linkpath"),
   SYSCALL_TYPE_4(SYS_rename, "oldpath", "newpath"),

   SYSCALL_TYPE_5(SYS_setpgid, "pid", "pgid"),
   SYSCALL_TYPE_5(SYS_dup2, "oldfd", "newfd"),
   SYSCALL_TYPE_5(SYS_kill, "pid", "sig"),
   SYSCALL_TYPE_5(SYS_tkill, "tid", "sig"),

#if defined(__i386__)
   SYSCALL_TYPE_6(SYS_chown16, "path", "owner", "group"),
   SYSCALL_TYPE_6(SYS_lchown16, "path", "owner", "group"),

   SYSCALL_TYPE_7(SYS_fchown16, "fd", "owner", "group"),
#endif

   SYSCALL_TYPE_6(SYS_chown, "path", "owner", "group"),
   SYSCALL_TYPE_6(SYS_lchown, "path", "owner", "group"),

   SYSCALL_TYPE_7(SYS_fchown, "fd", "owner", "group"),

   SYSCALL_RW(SYS_read, "fd", "buf", &ptype_buffer, sys_param_out, "count"),
   SYSCALL_RW(SYS_write, "fd", "buf", &ptype_buffer, sys_param_in, "count"),
   SYSCALL_RW(SYS_readv, "fd", "iov", &ptype_iov_out, sys_param_out, "iovcnt"),
   SYSCALL_RW(SYS_writev, "fd", "iov", &ptype_iov_in, sys_param_in, "iovcnt"),

   {
      .sys_n = SYS_exit,
      .n_params = 1,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,
      .params = {
         SIMPLE_PARAM("status", &ptype_int, sys_param_in),
      }
   },

   {
      .sys_n = SYS_exit_group,
      .n_params = 1,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,
      .params = {
         SIMPLE_PARAM("status", &ptype_int, sys_param_in),
      }
   },

   {
      .sys_n = SYS_vfork,
      .n_params = 0,
      .exp_block = true,
      .ret_type = &ptype_errno_or_val,
{'fraction_non_alphanumeric': 0.08519869835321961, 'fraction_numerical': 0.016024060743516418, 'mean_word_length': 0.8442444080741953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 228, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6868469', 'n_tokens_mistral': 5817, 'n_tokens_neox': 5573, 'n_words': 843}