Skip to content

Commit

Permalink
Work on clang-tidy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
charmoniumQ committed Aug 4, 2024
1 parent d153d3a commit a5a2fb0
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 130 deletions.
19 changes: 15 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ check-mypy:
mypy --strict --package probe_py.generated
mypy --strict probe_src/libprobe

check-clang-tidy:
for file in $(find probe_src -name "*.h" -o -name "*.c"); do clang-tidy $file -- -Iinclude; done
clang-tidy:
clang-tidy \
probe_src/tests/c/*.c \
probe_src/libprobe/generated/*.{h,c} \
probe_src/libprobe/arena/include/libprobe/*.h \
probe_src/libprobe/arena/*.c \
probe_src/libprobe/include/*.h \
probe_src/libprobe/src/*.c \
-- \
-Iprobe_src/libprobe/include \
-Iprobe_src/libprobe/arena/include \
-std=c11 \
-DARENA_USE_UNWRAPPED_LIBC \

compile-lib:
make --directory=probe_src/libprobe all
Expand All @@ -52,6 +63,6 @@ test-dev: compile-lib
check-flake:
nix flake check --all-systems

pre-commit: fix-format-nix fix-ruff fix-format-rust fix-clippy check-clang-tidy compile check-mypy test-dev
pre-commit: fix-format-nix fix-ruff fix-format-rust fix-clippy clang-tidy compile check-mypy test-dev

on-push: check-format-nix check-ruff check-format-rust check-clippy check-clang-tidy compile check-mypy check-flake test-ci
on-push: check-format-nix check-ruff check-format-rust check-clippy clang-tidy compile check-mypy check-flake test-ci
34 changes: 34 additions & 0 deletions probe_src/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,-modernize-use-trailing-return-type,-modernize-macro-to-enum,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-cppcoreguidelines-avoid-non-const-global-variables'
WarningsAsErrors: ''
HeaderFileExtensions:
- ''
- h
- hh
- hpp
- hxx
ImplementationFileExtensions:
- c
- cc
- cpp
- cxx
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: none
User: sam
CheckOptions:
cert-dcl16-c.NewSuffixes: 'L;LL;LU;LLU'
google-readability-namespace-comments.ShortNamespaceLines: '10'
cert-err33-c.CheckedFunctions: '::aligned_alloc;::asctime_s;::at_quick_exit;::atexit;::bsearch;::bsearch_s;::btowc;::c16rtomb;::c32rtomb;::calloc;::clock;::cnd_broadcast;::cnd_init;::cnd_signal;::cnd_timedwait;::cnd_wait;::ctime_s;::fclose;::fflush;::fgetc;::fgetpos;::fgets;::fgetwc;::fopen;::fopen_s;::fprintf;::fprintf_s;::fputc;::fputs;::fputwc;::fputws;::fread;::freopen;::freopen_s;::fscanf;::fscanf_s;::fseek;::fsetpos;::ftell;::fwprintf;::fwprintf_s;::fwrite;::fwscanf;::fwscanf_s;::getc;::getchar;::getenv;::getenv_s;::gets_s;::getwc;::getwchar;::gmtime;::gmtime_s;::localtime;::localtime_s;::malloc;::mbrtoc16;::mbrtoc32;::mbsrtowcs;::mbsrtowcs_s;::mbstowcs;::mbstowcs_s;::memchr;::mktime;::mtx_init;::mtx_lock;::mtx_timedlock;::mtx_trylock;::mtx_unlock;::printf_s;::putc;::putwc;::raise;::realloc;::remove;::rename;::scanf;::scanf_s;::setlocale;::setvbuf;::signal;::snprintf;::snprintf_s;::sprintf;::sprintf_s;::sscanf;::sscanf_s;::strchr;::strerror_s;::strftime;::strpbrk;::strrchr;::strstr;::strtod;::strtof;::strtoimax;::strtok;::strtok_s;::strtol;::strtold;::strtoll;::strtoul;::strtoull;::strtoumax;::strxfrm;::swprintf;::swprintf_s;::swscanf;::swscanf_s;::thrd_create;::thrd_detach;::thrd_join;::thrd_sleep;::time;::timespec_get;::tmpfile;::tmpfile_s;::tmpnam;::tmpnam_s;::tss_create;::tss_get;::tss_set;::ungetc;::ungetwc;::vfprintf;::vfprintf_s;::vfscanf;::vfscanf_s;::vfwprintf;::vfwprintf_s;::vfwscanf;::vfwscanf_s;::vprintf_s;::vscanf;::vscanf_s;::vsnprintf;::vsnprintf_s;::vsprintf;::vsprintf_s;::vsscanf;::vsscanf_s;::vswprintf;::vswprintf_s;::vswscanf;::vswscanf_s;::vwprintf_s;::vwscanf;::vwscanf_s;::wcrtomb;::wcschr;::wcsftime;::wcspbrk;::wcsrchr;::wcsrtombs;::wcsrtombs_s;::wcsstr;::wcstod;::wcstof;::wcstoimax;::wcstok;::wcstok_s;::wcstol;::wcstold;::wcstoll;::wcstombs;::wcstombs_s;::wcstoul;::wcstoull;::wcstoumax;::wcsxfrm;::wctob;::wctrans;::wctype;::wmemchr;::wprintf_s;::wscanf;::wscanf_s;'
llvm-else-after-return.WarnOnUnfixable: 'false'
cert-str34-c.DiagnoseSignedUnsignedCharComparisons: 'false'
google-readability-namespace-comments.SpacesBeforeComments: '2'
cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: 'true'
google-readability-braces-around-statements.ShortStatementLines: '1'
google-readability-function-size.StatementThreshold: '800'
llvm-qualified-auto.AddConstToQualified: 'false'
llvm-else-after-return.WarnOnConditionVariables: 'false'
cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField: 'false'
SystemHeaders: false
...

2 changes: 1 addition & 1 deletion probe_src/libprobe/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SOURCE_VERSION ?= $(shell git rev-parse --short HEAD)
CFLAGS ?= -DSOURCE_VERSION=\"$(SOURCE_VERSION)\" -Wno-cast-function-type -Wno-array-parameter -ffreestanding -Wl,--as-needed -Wall -Wextra -pthread -fPIC -nostdlib -shared $(NIX_CFLAGS_COMPILE)
CFLAGS ?= -DSOURCE_VERSION=\"$(SOURCE_VERSION)\" -Wno-cast-function-type -Wno-array-parameter -ffreestanding -Wl,--as-needed -Wall -Wextra -pthread -fPIC -nostdlib -shared $(NIX_CFLAGS_COMPILE) -std=c11
DBGCFLAGS ?= -Og -g
OPTCFLAGS ?= -O3 -DNDEBUG

Expand Down
7 changes: 0 additions & 7 deletions probe_src/libprobe/arena/include/arena.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#pragma once

#define _GNU_SOURCE
#ifdef PYCPARSER
#define __attribute__(x)
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#else
#include <stddef.h>
#include <stdio.h>
#include <stdalign.h>
Expand All @@ -19,7 +13,6 @@
#include <assert.h>
#include <unistd.h>
#include <sys/stat.h>
#endif

#ifdef ARENA_DEBUG
#define ARENA_PERROR
Expand Down
4 changes: 2 additions & 2 deletions probe_src/libprobe/arena/test_arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string.h>
#include <sys/stat.h>
#define ARENA_PERROR
#define strncpy_s(dest, destsz, src, count)
#define strncpy_s(dest, destsz, src, count) strncpy(dest, src, count)
#include "arena.h"

int main() {
Expand All @@ -33,7 +33,7 @@ int main() {
/* This is greater than the old capacity of the arena */
char* foo = arena_calloc(&arena_dir, 81920, sizeof(char));
assert(foo);
strncpy_s(foo, 4192,"this is a reaaally long string", 40);
strncpy_s(foo, 40,"this is a reaaally long string", 40);
/* This next line is totally optional */
/* arena_destroy(&arena_dir); */
return 0;
Expand Down
10 changes: 9 additions & 1 deletion probe_src/libprobe/generated/libc_hooks.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@

#define _GNU_SOURCE
#include "libc_hooks.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
/*
* pycparser cannot parse type-names as function-arguments (as in `va_arg(var_name, type_name)`)
* so we use some macros instead.
* To pycparser, these macros are defined as variable names (parsable as arguments).
* To GCC these macros are defined as type names.
* */
#define __type_mode_t mode_t

#define __type_charp char*
#define __type_charpp char**
void init_function_pointers()
{
unwrapped_fopen = dlsym(RTLD_NEXT, "fopen");
Expand Down
14 changes: 8 additions & 6 deletions probe_src/libprobe/generated/libc_hooks.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ftw.h>
#include <sys/wait.h>
#include <threads.h>

typedef int (*fn_ptr_int_void_ptr)(void*);
static FILE * (*unwrapped_fopen)(const char *filename, const char *opentype);
static FILE * (*unwrapped_freopen)(const char *filename, const char *opentype, FILE *stream);
static int (*unwrapped_fclose)(FILE *stream);
Expand All @@ -16,8 +18,8 @@ static int (*unwrapped_close)(int filedes);
static int (*unwrapped_close_range)(unsigned int lowfd, unsigned int maxfd, int flags);
static void (*unwrapped_closefrom)(int lowfd);
static int (*unwrapped_dup)(int old);
static int (*unwrapped_dup2)(int old, int new_);
static int (*unwrapped_dup3)(int old, int new_, int flags);
static int (*unwrapped_dup2)(int old, int new);
static int (*unwrapped_dup3)(int old, int new, int flags);
static int (*unwrapped_fcntl)(int filedes, int command, ...);
static int (*unwrapped_chdir)(const char *filename);
static int (*unwrapped_fchdir)(int filedes);
Expand Down Expand Up @@ -85,9 +87,9 @@ static FILE * (*unwrapped_tmpfile64)();
static char * (*unwrapped_tmpnam)(char *result);
static char * (*unwrapped_tmpnam_r)(char *result);
static char * (*unwrapped_tempnam)(const char *dir, const char *prefix);
static char * (*unwrapped_mktemp)(char *template_);
static int (*unwrapped_mkstemp)(char *template_);
static char * (*unwrapped_mkdtemp)(char *template_);
static char * (*unwrapped_mktemp)(char *template);
static int (*unwrapped_mkstemp)(char *template);
static char * (*unwrapped_mkdtemp)(char *template);
static int (*unwrapped_execv)(const char *filename, char * const argv[]);
static int (*unwrapped_execl)(const char *filename, const char *arg0, ...);
static int (*unwrapped_execve)(const char *filename, char * const argv[], char * const env[]);
Expand All @@ -113,4 +115,4 @@ static FILE * (*unwrapped_fopen64)(const char *filename, const char *opentype);
static FILE * (*unwrapped_freopen64)(const char *filename, const char *opentype, FILE *stream);
static int (*unwrapped_openat64)(int dirfd, const char *filename, int flags, ...);
static int (*unwrapped_open64)(const char *filename, int flags, ...);
static int (*unwrapped_create64)(const char *filename, mode_t mode);
static int (*unwrapped_create64)(const char *filename, mode_t mode);
36 changes: 34 additions & 2 deletions probe_src/libprobe/generator/gen_libc_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,47 @@ def wrapper_func_body(func: ParsedFunc) -> typing.Sequence[Node]:
).definition()
for _, func in funcs.items()
]

header_includes = """
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ftw.h>
#include <sys/wait.h>
#include <threads.h>
typedef int (*fn_ptr_int_void_ptr)(void*);
"""
pathlib.Path("generated/libc_hooks.h").write_text(
GccCGenerator().visit(
header_includes
+ GccCGenerator().visit(
pycparser.c_ast.FileAST(ext=[
*func_pointer_declarations,
])
)
)
c_includes = """
#define _GNU_SOURCE
#include "libc_hooks.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/types.h>
/*
* pycparser cannot parse type-names as function-arguments (as in `va_arg(var_name, type_name)`)
* so we use some macros instead.
* To pycparser, these macros are defined as variable names (parsable as arguments).
* To GCC these macros are defined as type names.
* */
#define __type_mode_t mode_t
#define __type_charp char*
#define __type_charpp char**
"""
pathlib.Path("generated/libc_hooks.c").write_text(
GccCGenerator().visit(
c_includes
+ GccCGenerator().visit(
pycparser.c_ast.FileAST(ext=[
init_function_pointers,
*static_args_wrapper_func_declarations,
Expand Down
Empty file.
1 change: 0 additions & 1 deletion probe_src/libprobe/include/general.h

This file was deleted.

32 changes: 8 additions & 24 deletions probe_src/libprobe/include/libprobe/prov_ops.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
#ifdef PYCPARSER
#define _BITS_BYTESWAP_H
#define __attribute__(x)
#define __bswap_64(x)
#define __restrict
#define __inline
#define __extension__
#define BORROWED
#define OWNED
#define _GNU_SOURCE
#define __asm__(x)
#define __signed__ signed
#pragma once

#include <linux/stat.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <utime.h>
#include <threads.h>
#include <pthread.h>
#endif

#include <linux/stat.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <utime.h>
#include <threads.h>
#include <pthread.h>
// See note in build.rs
#ifndef RUST_BINDGEN
#include <sys/resource.h>
#endif

#define OWNED

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#pragma once

#include <stdbool.h>

#define ARENA_USE_UNWRAPPED_LIBC
#define ARENA_PERROR
#include "../arena/include/arena.h"

/*
* TODO: Do I really need prov_log_disable?
*
Expand All @@ -10,3 +17,13 @@ static _Atomic bool __prov_log_disable = false;
static void prov_log_disable() { __prov_log_disable = true; }
static void prov_log_enable () { __prov_log_disable = false; }
static bool prov_log_is_enabled () { return !__prov_log_disable; }

static void maybe_init_thread();
static void reinit_process();
static void prov_log_disable();
static int get_exec_epoch_safe();
static struct ArenaDir* get_data_arena();

#define ENV_VAR_PREFIX "PROBE_"

#define PRIVATE_ENV_VAR_PREFIX "__PROBE_"
6 changes: 6 additions & 0 deletions probe_src/libprobe/src/global_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <stdbool.h>
#include <unistd.h>
#include <limits.h>
#include "declarations.h"

#define ARENA_USE_UNWRAPPED_LIBC
#define ARENA_PERROR
#include "../arena/include/arena.h"

/*
* For each member of global state $X of type $T, we have
*
Expand Down
32 changes: 2 additions & 30 deletions probe_src/libprobe/src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,19 @@
#include <threads.h>
#include <pthread.h>

/*
* pycparser cannot parse type-names as function-arguments (as in `va_arg(var_name, type_name)`)
* so we use some macros instead.
* To pycparser, these macros are defined as variable names (parsable as arguments).
* To GCC these macros are defined as type names.
* */
#define __type_mode_t mode_t
#define __type_charp char*
#define __type_charpp char**

/*
* Likewise, ther is some bug with pycparser unable to parse inline funciton pointers.
* So we will use a typedef alias.
* */
typedef int (*fn_ptr_int_void_ptr)(void*);

static void maybe_init_thread();
static void reinit_process();
static void prov_log_disable();
static int get_exec_epoch_safe();
static bool __process_inited = false;
static __thread bool __thread_inited = false;

#define ENV_VAR_PREFIX "PROBE_"

#define PRIVATE_ENV_VAR_PREFIX "__PROBE_"

#include "../generated/libc_hooks.h"

#include "prov_enable.c"
#include "declarationss.h"

#include "util.c"
#include "util.h"

/* #include "fd_table.c" */

#include "../include/libprobe/prov_ops.h"

#define ARENA_USE_UNWRAPPED_LIBC
#define ARENA_PERROR
#include "../arena/include/arena.h"

#include "global_state.c"

#include "prov_ops.c"
Expand Down
2 changes: 2 additions & 0 deletions probe_src/libprobe/src/lookup_on_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "util.h"
#include "libprobe/prov_ops.h"
#define BORROWED


Expand Down
6 changes: 5 additions & 1 deletion probe_src/libprobe/src/prov_buffer.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
Expand All @@ -8,7 +9,10 @@
#include <limits.h>
#include <linux/stat.h>
#include <pthread.h>
#include <threads.h>
#include <threads.h>
#include "libprobe/prov_ops.h"
#include "util.h"
#include "arena.h"

#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
Expand Down
Loading

0 comments on commit a5a2fb0

Please sign in to comment.