[tor-commits] [tor/master] New script to check includes for modularity violations

nickm at torproject.org nickm at torproject.org
Thu Jun 21 18:21:24 UTC 2018


commit 999f7984e189250216fcd0bbbe1c4c8d1b0c380b
Author: Nick Mathewson <nickm at torproject.org>
Date:   Thu Jun 21 14:02:11 2018 -0400

    New script to check includes for modularity violations
    
    Includes configuration files to enforce these rules on lib and
    common.  Of course, "common" *is* a modularity violation right now,
    so these rules aren't as strict as I would like them to be.
---
 scripts/maint/checkIncludes.py   | 70 ++++++++++++++++++++++++++++++++++++++++
 src/common/.may_include          | 14 ++++++++
 src/lib/cc/.may_include          |  1 +
 src/lib/compress/.may_include    |  6 ++++
 src/lib/crypt_ops/.may_include   | 14 ++++++++
 src/lib/ctime/.may_include       |  6 ++++
 src/lib/err/.may_include         |  3 ++
 src/lib/testsupport/.may_include |  0
 src/lib/tls/.may_include         | 11 +++++++
 src/lib/tls/buffers_tls.c        |  2 --
 src/lib/trace/.may_include       |  5 +++
 11 files changed, 130 insertions(+), 2 deletions(-)

diff --git a/scripts/maint/checkIncludes.py b/scripts/maint/checkIncludes.py
new file mode 100755
index 000000000..b96b7f4b3
--- /dev/null
+++ b/scripts/maint/checkIncludes.py
@@ -0,0 +1,70 @@
+#!/usr/bin/python3
+
+import fnmatch
+import os
+import re
+import sys
+
+trouble = False
+
+def err(msg):
+    global trouble
+    trouble = True
+    print(msg, file=sys.stderr)
+
+def fname_is_c(fname):
+    return fname.endswith(".h") or fname.endswith(".c")
+
+INCLUDE_PATTERN = re.compile(r'\s*#\s*include\s+"([^"]*)"')
+RULES_FNAME = ".may_include"
+
+class Rules(object):
+    def __init__(self):
+        self.patterns = []
+
+    def addPattern(self, pattern):
+        self.patterns.append(pattern)
+
+    def includeOk(self, path):
+        for pattern in self.patterns:
+            if fnmatch.fnmatchcase(path, pattern):
+                return True
+        return False
+
+    def applyToLines(self, lines, context=""):
+        lineno = 0
+        for line in lines:
+            lineno += 1
+            m = INCLUDE_PATTERN.match(line)
+            if m:
+                include = m.group(1)
+                if not self.includeOk(include):
+                    err("Forbidden include of {} on line {}{}".format(
+                        include, lineno, context))
+
+    def applyToFile(self, fname):
+        with open(fname, 'r') as f:
+            #print(fname)
+            self.applyToLines(iter(f), " of {}".format(fname))
+
+def load_include_rules(fname):
+    result = Rules()
+    with open(fname, 'r') as f:
+        for line in f:
+            line = line.strip()
+            if line.startswith("#") or not line:
+                continue
+            result.addPattern(line)
+    return result
+
+for dirpath, dirnames, fnames in os.walk("src"):
+    if ".may_include" in fnames:
+        rules = load_include_rules(os.path.join(dirpath, RULES_FNAME))
+        for fname in fnames:
+            if fname_is_c(fname):
+                rules.applyToFile(os.path.join(dirpath,fname))
+
+if trouble:
+    err(
+"""To change which includes are allowed in a C file, edit the {} files in its
+enclosing directory.""".format(RULES_FNAME))
diff --git a/src/common/.may_include b/src/common/.may_include
new file mode 100644
index 000000000..d2a3d2fff
--- /dev/null
+++ b/src/common/.may_include
@@ -0,0 +1,14 @@
+orconfig.h
+common/*.h
+lib/*/*.h
+
+# XXXX These all belong somewhere else
+ht.h
+linux_syscalls.inc
+micro-revision.i
+siphash.h
+src/ext/timeouts/timeout.c
+strlcat.c
+strlcpy.c
+tor_queue.h
+tor_readpassphrase.h
diff --git a/src/lib/cc/.may_include b/src/lib/cc/.may_include
new file mode 100644
index 000000000..2b06e8519
--- /dev/null
+++ b/src/lib/cc/.may_include
@@ -0,0 +1 @@
+orconfig.h
diff --git a/src/lib/compress/.may_include b/src/lib/compress/.may_include
new file mode 100644
index 000000000..70528a7df
--- /dev/null
+++ b/src/lib/compress/.may_include
@@ -0,0 +1,6 @@
+orconfig.h
+lib/cc/*.h
+lib/compress/*.h
+
+# XXX I'd like to remove this.
+common/*.h
diff --git a/src/lib/crypt_ops/.may_include b/src/lib/crypt_ops/.may_include
new file mode 100644
index 000000000..6eefec158
--- /dev/null
+++ b/src/lib/crypt_ops/.may_include
@@ -0,0 +1,14 @@
+orconfig.h
+lib/cc/*.h
+lib/crypt_ops/*.h
+lib/ctime/*.h
+lib/err/*.h
+lib/testsupport/testsupport.h
+
+trunnel/pwbox.h
+
+keccak-tiny/*.h
+ed25519/*.h
+
+# XXX I'd like to remove this.
+common/*.h
diff --git a/src/lib/ctime/.may_include b/src/lib/ctime/.may_include
new file mode 100644
index 000000000..72d854c37
--- /dev/null
+++ b/src/lib/ctime/.may_include
@@ -0,0 +1,6 @@
+orconfig.h
+lib/cc/*.h
+lib/ctime/*.h
+
+# XXXX I'd like to remove this
+common/util.h
diff --git a/src/lib/err/.may_include b/src/lib/err/.may_include
new file mode 100644
index 000000000..48cc0ef08
--- /dev/null
+++ b/src/lib/err/.may_include
@@ -0,0 +1,3 @@
+orconfig.h
+lib/cc/*.h
+lib/err/*.h
diff --git a/src/lib/testsupport/.may_include b/src/lib/testsupport/.may_include
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/lib/tls/.may_include b/src/lib/tls/.may_include
new file mode 100644
index 000000000..22792b6bf
--- /dev/null
+++ b/src/lib/tls/.may_include
@@ -0,0 +1,11 @@
+orconfig.h
+lib/cc/*.h
+lib/crypt_ops/*.h
+lib/err/*.h
+lib/testsupport/testsupport.h
+lib/tls/*.h
+
+ciphers.inc
+
+# XXX I'd like to remove this.
+common/*.h
diff --git a/src/lib/tls/buffers_tls.c b/src/lib/tls/buffers_tls.c
index 5accb2d91..55c3ac334 100644
--- a/src/lib/tls/buffers_tls.c
+++ b/src/lib/tls/buffers_tls.c
@@ -10,7 +10,6 @@
 #include "common/buffers.h"
 #include "lib/tls/buffers_tls.h"
 #include "common/compat.h"
-#include "lib/compress/compress.h"
 #include "common/util.h"
 #include "lib/cc/torint.h"
 #include "common/torlog.h"
@@ -176,4 +175,3 @@ buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen,
   tor_assert(flushed < INT_MAX);
   return (int)flushed;
 }
-
diff --git a/src/lib/trace/.may_include b/src/lib/trace/.may_include
new file mode 100644
index 000000000..694c8405e
--- /dev/null
+++ b/src/lib/trace/.may_include
@@ -0,0 +1,5 @@
+orconfig.h
+lib/trace/*.h
+
+# XXXX
+common/torlog.h





More information about the tor-commits mailing list