-
Notifications
You must be signed in to change notification settings - Fork 1
build: build the vendored Capstone from the autotools build #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ extern char* vgPlain_strncpy(char* d, const char* s, unsigned long n); | |
| extern char* vgPlain_strchr(const char* s, char c); | ||
| extern char* vgPlain_strrchr(const char* s, char c); | ||
| extern char* vgPlain_strstr(const char* h, const char* n); | ||
| extern char* vgPlain_strcat(char* d, const char* s); | ||
|
|
||
| static const char* const CLG_CD_CC = "clg.cycledecode"; | ||
|
|
||
|
|
@@ -91,10 +92,16 @@ int printf(const char* fmt, ...) | |
| } | ||
| int puts(const char* s) { return printf("%s\n", s); } | ||
|
|
||
| /* Capstone's SStream references stderr/fwrite on a buffer-overflow guard in the | ||
| * op_str text path (which this code never reads). Valgrind has no FILE* layer, | ||
| * so stderr is a sentinel and fwrite routes the bytes to the Valgrind log fd, | ||
| * making such an overflow visible rather than swallowed. */ | ||
| /* Capstone's SStream references stderr/fprintf on a buffer-overflow guard in | ||
| * the op_str text path (which this code never reads). Valgrind has no FILE* | ||
| * layer, so stderr is a sentinel and these route the bytes to the Valgrind log | ||
| * fd, making such an overflow visible rather than swallowed. | ||
| * | ||
| * Both fprintf and fwrite are needed: the guard calls fprintf, which GCC folds | ||
| * into fwrite for a format string with no conversions, but only when builtins | ||
| * are enabled. Valgrind's tool CFLAGS pass -fno-builtin, so whether the object | ||
| * ends up referencing fprintf or fwrite depends on how Capstone was compiled. | ||
| * Defining both keeps the tool linkable either way. */ | ||
| extern int vgPlain_write(int fd, const void* buf, int count); | ||
| FILE* stderr = 0; | ||
| size_t fwrite(const void* p, size_t size, size_t nmemb, FILE* f) | ||
|
|
@@ -103,6 +110,15 @@ size_t fwrite(const void* p, size_t size, size_t nmemb, FILE* f) | |
| vgPlain_write(2, p, (int)(size * nmemb)); | ||
| return nmemb; | ||
| } | ||
| int fprintf(FILE* f, const char* fmt, ...) | ||
| { | ||
| (void)f; | ||
| va_list ap; | ||
| va_start(ap, fmt); | ||
| unsigned int r = vgPlain_vprintf(fmt, ap); | ||
| va_end(ap); | ||
| return (int)r; | ||
| } | ||
|
Comment on lines
+113
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand why changes in this PR make this required, would you care to explain? |
||
|
|
||
| size_t strlen(const char* s) { return vgPlain_strlen(s); } | ||
| int strcmp(const char* a, const char* b) { return vgPlain_strcmp(a, b); } | ||
|
|
@@ -118,6 +134,7 @@ char* strncpy(char* d, const char* s, size_t n) | |
| char* strchr(const char* s, int c) { return vgPlain_strchr(s, (char)c); } | ||
| char* strrchr(const char* s, int c) { return vgPlain_strrchr(s, (char)c); } | ||
| char* strstr(const char* h, const char* n) { return vgPlain_strstr(h, n); } | ||
| char* strcat(char* d, const char* s) { return vgPlain_strcat(d, s); } | ||
|
|
||
| /*------------------------------------------------------------*/ | ||
| /*--- Capstone handle: open / decode -*/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,8 +36,9 @@ | |
| }); | ||
| in | ||
| { | ||
| # Expose the pinned Capstone so the autotools build and scripts can find | ||
| # it via `nix build .#capstone` or the CAPSTONE_DIR env var below. | ||
| # Expose the pinned Capstone for the builds that want a prebuilt decoder | ||
| # rather than the vendored submodule: `nix build .#capstone`, then pass it | ||
| # to configure as --with-capstone=PATH (or CAPSTONE_DIR). | ||
| packages.capstone = capstone; | ||
|
Comment on lines
+39
to
42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #40 (comment) |
||
|
|
||
| devShells.default = pkgs.mkShell { | ||
|
|
@@ -62,10 +63,6 @@ | |
| pkgs.gcc | ||
| pkgs.pkg-config | ||
| ]; | ||
|
|
||
| # Consumed by configure (--with-capstone), the LUT generator, and the | ||
| # standalone cycledecode test. Point them at the hardening-free build. | ||
| CAPSTONE_DIR = "${capstone}"; | ||
| }; | ||
| } | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # codspeed_capstone.m4 -- CodSpeed additions to Valgrind's configure. | ||
|
|
||
| # CODSPEED_CAPSTONE | ||
| # ----------------- | ||
| # Locate the Capstone decoder that Callgrind's per-instruction cycle | ||
| # estimation (--cycle-estimation=yes) links against, and export | ||
| # CAPSTONE_CFLAGS / CAPSTONE_LIBS for callgrind/Makefile.am. | ||
| # | ||
| # By default the vendored submodule in third_party/capstone is compiled by | ||
| # third_party/Makefile.am as part of `make`, so a plain | ||
| # `./autogen.sh && ./configure && make && make install` is all that is needed. | ||
| # --with-capstone=PATH (or the CAPSTONE_DIR environment variable, which `nix | ||
| # develop` sets) selects a prebuilt Capstone install instead. | ||
| AC_DEFUN([CODSPEED_CAPSTONE], [ | ||
| AC_ARG_WITH([capstone], | ||
| [AS_HELP_STRING([--with-capstone=PATH], | ||
| [use a prebuilt Capstone install for Callgrind cycle estimation instead | ||
| of the vendored third_party/capstone submodule. Defaults to the | ||
| CAPSTONE_DIR environment variable])], | ||
| [capstone_dir="$withval"], | ||
| [capstone_dir="$CAPSTONE_DIR"]) | ||
|
|
||
| AM_CONDITIONAL([BUILD_VENDORED_CAPSTONE], [test -z "$capstone_dir"]) | ||
|
|
||
| if test -z "$capstone_dir"; then | ||
| # Built by third_party/Makefile.am. Note there is deliberately no check | ||
| # for libcapstone.a here: it does not exist yet at configure time. | ||
| if test ! -f "$srcdir/third_party/capstone/cs.c"; then | ||
| AC_MSG_ERROR([third_party/capstone is empty. Run: | ||
| git submodule update --init third_party/capstone | ||
| or pass --with-capstone=PATH to use a prebuilt Capstone.]) | ||
| fi | ||
| CAPSTONE_INCLUDES='-I$(top_srcdir)/third_party/capstone/include' | ||
| CAPSTONE_LIBS='$(top_builddir)/third_party/libcapstone.a' | ||
| AC_MSG_NOTICE([Callgrind cycle estimation enabled with the vendored Capstone]) | ||
| else | ||
| if test ! -f "$capstone_dir/lib/libcapstone.a" \ | ||
| -o ! -f "$capstone_dir/include/capstone/capstone.h"; then | ||
| AC_MSG_ERROR([--with-capstone=$capstone_dir: libcapstone.a or capstone.h not found]) | ||
| fi | ||
| CAPSTONE_INCLUDES="-I$capstone_dir/include" | ||
| CAPSTONE_LIBS="$capstone_dir/lib/libcapstone.a" | ||
| AC_MSG_NOTICE([Callgrind cycle estimation enabled with Capstone at $capstone_dir]) | ||
| fi | ||
|
|
||
| # Fortify off: the tool links -nodefaultlibs, so glibc's __*_chk fortify | ||
| # wrappers are unavailable, and our libc shims (sprintf/snprintf/...) must | ||
| # be real definitions, not fortify macro-expansions. | ||
| CAPSTONE_CFLAGS="-DCLG_WITH_CAPSTONE -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 $CAPSTONE_INCLUDES" | ||
| AC_SUBST([CAPSTONE_CFLAGS]) | ||
| AC_SUBST([CAPSTONE_LIBS]) | ||
| ]) | ||
|
|
||
| # CODSPEED_C_STD_GNU17 | ||
| # -------------------- | ||
| # Pin the C dialect to gnu17. | ||
| # | ||
| # AC_PROG_CC under Autoconf 2.70+ selects the newest dialect the compiler | ||
| # supports, which is -std=gnu23 on GCC 15+. Under C23, glibc 2.42+ defines | ||
| # strchr/strrchr/strstr as _Generic macros, which clash with Callgrind's own | ||
| # definitions of those symbols. Appending to CFLAGS is enough to win: Autoconf | ||
| # puts its own -std= into $CC, and CFLAGS comes later on the command line. | ||
| # | ||
| # Must be called after AC_PROG_CC. | ||
| AC_DEFUN([CODSPEED_C_STD_GNU17], [ | ||
| AC_MSG_CHECKING([whether $CC accepts -std=gnu17]) | ||
| codspeed_save_CFLAGS="$CFLAGS" | ||
| CFLAGS="$CFLAGS -std=gnu17" | ||
| AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], | ||
| [AC_MSG_RESULT([yes])], | ||
| [AC_MSG_RESULT([no]) | ||
| CFLAGS="$codspeed_save_CFLAGS"]) | ||
| ]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cf the thread of the greptile comment: this is going to be run unconditionally, we do not want that. If people provide capstone, we have no business updating/pulling this submodule and potentially rpinting a warning.
Additionnally, these kind of comments are much to verbose. They are a side effect of claude, fell free to use our internal deslop skill, but be mindful about these, they are extremely annoying to deal with both when coding with claude and when reviewing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a ci build case with a pre-built capstone, installed with apt, to make sure we dont passively break this as well