From 9a101c2c0f1e4598cd5bd2710e6021052e4ebdfa Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 11 Sep 2019 18:43:16 -0400 Subject: Add a script to run spatch with appropriate arguments It's a bit tricky to remember the right incantation to get the proper include paths and incantations for coccinelle, but without it, coccinelle is less effective at parsing our C. --- scripts/coccinelle/apply.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 scripts/coccinelle/apply.sh (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/apply.sh b/scripts/coccinelle/apply.sh new file mode 100755 index 0000000000..82e773dc39 --- /dev/null +++ b/scripts/coccinelle/apply.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# apply.sh: +# run spatch with appropriate includes and builtins for the Tor source code + +top="$(dirname "$0")/../.." + +spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ + -I "$top" -I "$top"/src -I "$top"/ext "$@" -- cgit v1.2.3-54-g00ecf From c6191983e93fc9d377650555fb78649e9fe8713b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 11 Sep 2019 18:44:10 -0400 Subject: Add a script to tell whether a file can be perfectly parsed by spatch spatch can let us know whether a file has parsed "perfectly" or not. The more perfect it parses, the likelier any semantic patches are to apply. I've used this script to identify problem areas in our code. --- scripts/coccinelle/try_parse.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 scripts/coccinelle/try_parse.sh (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/try_parse.sh b/scripts/coccinelle/try_parse.sh new file mode 100755 index 0000000000..865c85570a --- /dev/null +++ b/scripts/coccinelle/try_parse.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Echo the name of every argument of this script that is not "perfect" +# according to coccinelle's --parse-c. + +top="$(dirname "$0")/../.." + +for fn in "$@"; do + + if spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ + -I "$top" -I "$top"/src -I "$top"/ext --parse-c "$fn" \ + 2>/dev/null | grep "perfect = 1" > /dev/null; then + : # it's perfect + else + echo "$fn" + fi + +done -- cgit v1.2.3-54-g00ecf From 25ed698fb8a4f438c2fc804cdb2b0bf92e641f59 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 11 Sep 2019 18:45:52 -0400 Subject: Add some more of our trickier macros to tor-coccinelle.h Note that this header file behaves a bit strangely. It is used by coccinelle just for the purpose of knowing how to parse difficult-to-parse stuff. It doesn't need to produce good C -- just grammatical C. --- scripts/coccinelle/tor-coccinelle.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index 8f625dcee4..a94c3ef679 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -1,3 +1,21 @@ #define MOCK_IMPL(a, b, c) a b c #define CHECK_PRINTF(a, b) #define STATIC static + +#define STMT_BEGIN do { +#define STMT_END } while (0) + +#define BUG(x) (x) +#define IF_BUG_ONCE(x) if (x) + +#define ATTR_NORETURN +#define ATTR_UNUSED +#define ATTR_CONST +#define ATTR_MALLOC +#define ATTR_WUR + +#define HT_ENTRY(x) void * +#define HT_HEAD(a,b) struct ht_head +#define HT_INITIALIZER { } +#define X509 struct x509_st +#define STACK_OF(x) struct foo_stack_t -- cgit v1.2.3-54-g00ecf From 4ab85f4928be7ed096643bd5f7d8d5f2b42859fe Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:23:49 -0400 Subject: Document tor-coccinelle.h --- scripts/coccinelle/tor-coccinelle.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index a94c3ef679..f597e912b4 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -1,3 +1,22 @@ +/* Copyright (c) 2001 Matej Pfajfar. + * Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2019, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/* + * This file looks like a C header, but its purpose is a bit different. + * + * We never include it from our real C files; we only tell Coccinelle + * about it in apply.sh. + * + * It tells the Coccinelle semantic patching tool how to understand + * things that would otherwise not be good C syntax, or which would + * otherwise not make sense to it as C. It doesn't need to produce + * semantically equivalent C, or even correct C: it only has to produce + * syntactically valid C. + */ + #define MOCK_IMPL(a, b, c) a b c #define CHECK_PRINTF(a, b) #define STATIC static -- cgit v1.2.3-54-g00ecf From 9ab96550da079abe048d1e0e2065e3fb85634cda Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:27:48 -0400 Subject: document test-operator-cleanup --- scripts/coccinelle/test-operator-cleanup | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/test-operator-cleanup b/scripts/coccinelle/test-operator-cleanup index e7822542a4..28b4d4f588 100755 --- a/scripts/coccinelle/test-operator-cleanup +++ b/scripts/coccinelle/test-operator-cleanup @@ -1,4 +1,17 @@ #!/usr/bin/perl -w -p -i +# +# Copyright (c) 2001 Matej Pfajfar. +# Copyright (c) 2001-2004, Roger Dingledine. +# Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. +# Copyright (c) 2007-2019, The Tor Project, Inc. +# See LICENSE for licensing information + +# This script looks for instances of C comparison operators as macro arguments, +# and replaces them with our OP_* equivalents. +# +# Some macros that take operators are our tt_int_op() testing macro, and the +# standard timercmp() macro. Coccinelle can't handle their syntax, however, +# unless we give them their operators as a macro too. next if m#^ */\*# or m#^ *\* #; -- cgit v1.2.3-54-g00ecf From 51c2097586dd6133a862985ab68ded85736d40e8 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:31:10 -0400 Subject: try_parse.sh: add a verbose mode and a meaningful exit code. --- scripts/coccinelle/try_parse.sh | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/try_parse.sh b/scripts/coccinelle/try_parse.sh index 865c85570a..3033242946 100755 --- a/scripts/coccinelle/try_parse.sh +++ b/scripts/coccinelle/try_parse.sh @@ -5,6 +5,8 @@ top="$(dirname "$0")/../.." +exitcode=0 + for fn in "$@"; do if spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ @@ -13,6 +15,13 @@ for fn in "$@"; do : # it's perfect else echo "$fn" + if test "${VERBOSE}" != ""; then + spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ + -I "$top" -I "$top"/src -I "$top"/ext --parse-c "$fn" + fi + exitcode=1 fi done + +exit "$exitcode" -- cgit v1.2.3-54-g00ecf From eb1260e3465b455db0a20d0d0770e85aaa061228 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:38:43 -0400 Subject: tor-coccinelle: expect parentheses after HT_INITIALIZER --- scripts/coccinelle/tor-coccinelle.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index f597e912b4..ee709e2570 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -35,6 +35,6 @@ #define HT_ENTRY(x) void * #define HT_HEAD(a,b) struct ht_head -#define HT_INITIALIZER { } +#define HT_INITIALIZER() { } #define X509 struct x509_st #define STACK_OF(x) struct foo_stack_t -- cgit v1.2.3-54-g00ecf From c13c0c899993cc015cd26006dcc5c656255abbc1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:52:05 -0400 Subject: tor-coccinelle.h: Add {EN,DIS}ABLE_GCC_WARNING. --- scripts/coccinelle/tor-coccinelle.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index ee709e2570..288722b5ff 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -32,6 +32,8 @@ #define ATTR_CONST #define ATTR_MALLOC #define ATTR_WUR +#define DISABLE_GCC_WARNING(x) +#define ENABLE_GCC_WARNING(x) #define HT_ENTRY(x) void * #define HT_HEAD(a,b) struct ht_head -- cgit v1.2.3-54-g00ecf From 017c62000cc078e2e858bc0a9a7efa60c4196c60 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 09:58:42 -0400 Subject: tor-coccinelle: handle SIMPLEQ and TAILQ better. --- scripts/coccinelle/tor-coccinelle.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index 288722b5ff..87871b40b2 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -40,3 +40,7 @@ #define HT_INITIALIZER() { } #define X509 struct x509_st #define STACK_OF(x) struct foo_stack_t +#define TOR_TAILQ_HEAD(a,b) struct tailq_head +#define TOR_TAILQ_ENTRY(a) struct tailq_entry +#define TOR_SIMPLEQ_HEAD(a,b) struct simpleq_entry +#define TOR_SIMPLEQ_ENTRY(a) struct simpleq_entry -- cgit v1.2.3-54-g00ecf From ea47e53252adc5aed81cfb334b20229718fd8ebf Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:01:19 -0400 Subject: tor-coccinelle.h: handle NS and NS_DECL. --- scripts/coccinelle/tor-coccinelle.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index 87871b40b2..ea12a61fef 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -44,3 +44,6 @@ #define TOR_TAILQ_ENTRY(a) struct tailq_entry #define TOR_SIMPLEQ_HEAD(a,b) struct simpleq_entry #define TOR_SIMPLEQ_ENTRY(a) struct simpleq_entry + +#define NS_DECL(a, b, c) a b c +#define NS(a) a -- cgit v1.2.3-54-g00ecf From a2bb66c3558be224f3537296ee0ebec179164f57 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:07:50 -0400 Subject: Add a "COCCI" macro that we tell coccinelle is always defined. This will let us give specific in-file equivalents to given macros or preprocessor directives, to make things parse. --- scripts/coccinelle/apply.sh | 2 +- scripts/coccinelle/try_parse.sh | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/apply.sh b/scripts/coccinelle/apply.sh index 82e773dc39..f531d7fa32 100755 --- a/scripts/coccinelle/apply.sh +++ b/scripts/coccinelle/apply.sh @@ -6,4 +6,4 @@ top="$(dirname "$0")/../.." spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ - -I "$top" -I "$top"/src -I "$top"/ext "$@" + -I "$top" -I "$top"/src -I "$top"/ext --defined COCCI "$@" diff --git a/scripts/coccinelle/try_parse.sh b/scripts/coccinelle/try_parse.sh index 3033242946..0f91e31702 100755 --- a/scripts/coccinelle/try_parse.sh +++ b/scripts/coccinelle/try_parse.sh @@ -10,14 +10,16 @@ exitcode=0 for fn in "$@"; do if spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ - -I "$top" -I "$top"/src -I "$top"/ext --parse-c "$fn" \ + -I "$top" -I "$top"/src -I "$top"/ext --defined COCCI \ + --parse-c "$fn" \ 2>/dev/null | grep "perfect = 1" > /dev/null; then : # it's perfect else echo "$fn" if test "${VERBOSE}" != ""; then spatch -macro_file_builtins "$top"/scripts/coccinelle/tor-coccinelle.h \ - -I "$top" -I "$top"/src -I "$top"/ext --parse-c "$fn" + -I "$top" -I "$top"/src -I "$top"/ext --defined COCCI \ + --parse-c "$fn" fi exitcode=1 fi -- cgit v1.2.3-54-g00ecf From 18f1624bbef19c89be1b116b950134c0b808d4fc Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:19:58 -0400 Subject: tor-coccinelle.h: handle SLIST and LIST. --- scripts/coccinelle/tor-coccinelle.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index ea12a61fef..a31a5cd80d 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -44,6 +44,10 @@ #define TOR_TAILQ_ENTRY(a) struct tailq_entry #define TOR_SIMPLEQ_HEAD(a,b) struct simpleq_entry #define TOR_SIMPLEQ_ENTRY(a) struct simpleq_entry +#define TOR_LIST_HEAD(a,b) struct list_head +#define TOR_LIST_ENTRY(a) struct list_entry +#define TOR_SLIST_HEAD(a,b) struct slist_head +#define TOR_SLIST_ENTRY(a) struct slist_entry #define NS_DECL(a, b, c) a b c #define NS(a) a -- cgit v1.2.3-54-g00ecf From 7798c53c989408c64a6d43175e84dbc4bfa137f5 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:21:15 -0400 Subject: tor-coccinelle.h: add MOCK_DECL --- scripts/coccinelle/tor-coccinelle.h | 1 + 1 file changed, 1 insertion(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index a31a5cd80d..97935c2dd4 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -17,6 +17,7 @@ * syntactically valid C. */ +#define MOCK_DECL(a, b, c) a b c #define MOCK_IMPL(a, b, c) a b c #define CHECK_PRINTF(a, b) #define STATIC static -- cgit v1.2.3-54-g00ecf From 2e64dfea95c95499291661256ce97e6ae3d8bc88 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:38:22 -0400 Subject: tor-coccinelle.h: add CHECK_SCANF --- scripts/coccinelle/tor-coccinelle.h | 1 + 1 file changed, 1 insertion(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index 97935c2dd4..cbb9220a4f 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -20,6 +20,7 @@ #define MOCK_DECL(a, b, c) a b c #define MOCK_IMPL(a, b, c) a b c #define CHECK_PRINTF(a, b) +#define CHECK_SCANF(a, b) #define STATIC static #define STMT_BEGIN do { -- cgit v1.2.3-54-g00ecf From d129b503c0ef64547d6a875752d8ce530bbfd1bf Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:49:47 -0400 Subject: tor-coccinelle.h: add EAT_SEMICOLON --- scripts/coccinelle/tor-coccinelle.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index cbb9220a4f..a064c79809 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -53,3 +53,5 @@ #define NS_DECL(a, b, c) a b c #define NS(a) a + +#define EAT_SEMICOLON extern int dummy__; -- cgit v1.2.3-54-g00ecf From 6696a5bbae4bd21f84a5b0730273fd847b8f0e45 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 10:54:11 -0400 Subject: tor-coccinelle.h: add HANDLE_{DECL,IMPL} --- scripts/coccinelle/tor-coccinelle.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index a064c79809..0727caa4dc 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -37,6 +37,8 @@ #define DISABLE_GCC_WARNING(x) #define ENABLE_GCC_WARNING(x) +#define HANDLE_DECL(a,b,c) +#define HANDLE_IMPL(a,b,c) #define HT_ENTRY(x) void * #define HT_HEAD(a,b) struct ht_head #define HT_INITIALIZER() { } -- cgit v1.2.3-54-g00ecf From d6ce8527f8795c3385320baa17e080bde999e29a Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 11:14:28 -0400 Subject: tor-coccinelle.h: add a definition for EXTERN --- scripts/coccinelle/tor-coccinelle.h | 1 + 1 file changed, 1 insertion(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index 0727caa4dc..c83dce3c5c 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -22,6 +22,7 @@ #define CHECK_PRINTF(a, b) #define CHECK_SCANF(a, b) #define STATIC static +#define EXTERN(a,b) extern a b; #define STMT_BEGIN do { #define STMT_END } while (0) -- cgit v1.2.3-54-g00ecf From 3e41459dff9e62cabe2636eac61d19b32dd0a4e1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 Oct 2019 12:39:31 -0400 Subject: confmacros.h: fix coccinelle parsing. --- scripts/coccinelle/tor-coccinelle.h | 3 +++ src/lib/conf/confmacros.h | 2 ++ 2 files changed, 5 insertions(+) (limited to 'scripts/coccinelle') diff --git a/scripts/coccinelle/tor-coccinelle.h b/scripts/coccinelle/tor-coccinelle.h index c83dce3c5c..44d79325eb 100644 --- a/scripts/coccinelle/tor-coccinelle.h +++ b/scripts/coccinelle/tor-coccinelle.h @@ -57,4 +57,7 @@ #define NS_DECL(a, b, c) a b c #define NS(a) a +#define CONF_TEST_MEMBERS(a,b,c) +#define DUMMY_CONF_TEST_MEMBERS + #define EAT_SEMICOLON extern int dummy__; diff --git a/src/lib/conf/confmacros.h b/src/lib/conf/confmacros.h index 68121891f1..6449458a10 100644 --- a/src/lib/conf/confmacros.h +++ b/src/lib/conf/confmacros.h @@ -15,11 +15,13 @@ #include "orconfig.h" #include "lib/conf/conftesting.h" +#ifndef COCCI /** * Used to indicate the end of an array of configuration variables. **/ #define END_OF_CONFIG_VARS \ { .member = { .name = NULL } DUMMY_CONF_TEST_MEMBERS } +#endif /** * Declare a config_var_t as a member named membername of the structure -- cgit v1.2.3-54-g00ecf