lietzsche;stokes

updated 2026-11-26·4 min read

Boundary Graphs vs. Whole-Program Taint Analysis

Why interprocedural taint analysis stalls in CI and how sparse boundary graphs check producer counts against fixed buffers.

Whole-program taint analysis (CodeQL, Semgrep) slows down fast in polyglot CI because tracing data from ClickHouse DDL through Python frameworks like Pydantic or SQLAlchemy means building O(N^3) pointer-alias graphs over millions of library lines. That takes minutes per CI run, trips constantly on reflection and dynamic dicts, and adds heavy overhead if you shift it into runtime eBPF probes.

Stokes ignores internal helper arithmetic altogether. It strips away everything outside boundary files and builds a small directed graph over just the schemas, serialization sinks, and fixed buffers, running locally with zero production overhead.

DimensionWhole-Program Taint AnalysisStokes Sparse Boundary Graphs
Algorithmic ModelContext-Free Language (CFL) reachability over all AST nodesExtracts only boundary schemas, serialization sinks, and fixed buffers
Complexity ClassO(N^3) across application, framework, and library codeO(V + E) over a small boundary graph
CI ScopeFull cross-library call graph per runBoundary files only, no containers or rustc
False Positive RateConfounded by reflection, decorators, and dynamic dictsDeterministic on declared buffer bounds
Runtime Overhead15% to 40% throughput loss if shifted to eBPF/bytecode probesExactly 0 ns (zero binary instrumentation)

How Boundary Graphs Trace Column Counts

Stokes extracts boundary declarations into a directed graph connecting upstream schemas to downstream buffers across three node types:

  • Schema Nodes (ClickHouse / SQL): Tables and views, each tracking how many columns an active query emits.
  • Transport Channels (Python / KV / Protobuf): Serialization sinks (json.dumps(), Kafka topics, KV keys) forwarding reflected fields.
  • Intake Buffers (Rust / C++): Fixed-capacity allocations such as [Feature; N], recording their slot limit N.

For each downstream buffer, Stokes walks backwards to reachable upstream schemas, sums their emitted column counts, and compares that total against the buffer’s slot capacity (flagging CI whenever total_emitted > buffer_slots and printing the exact file chain from SQL query to .try_into().unwrap()):

  1. Upstream Schema Nodes (200 + 80 columns)

    bot_signals DDL (200 columns) and shard_r0 DDL (80 internal columns) both match an unscoped reflection query.

  2. Wire Transport Node

    Python ETL worker serializes all 280 reflected fields into an unbounded KV payload.

  3. Consumer Buffer Node (200 slots)

    Rust proxy stack buffer allocates 200 slots. Comparing 280 emitted columns against 200 slots trips CI immediately (risk_ratio = 1.40).

Sparse Extraction Without Native Build Overhead

TreeSitterLoader ships .scm query definitions alongside compiled Python regex patterns (_PATTERNS) as its out-of-the-box fallback when native .so grammars aren’t installed, so CI runners only inspect boundary declarations and skip the rest of the codebase.

Tree-sitter S-Expression Queries (.scm) for SQL, Python, and Rust
;; SQL: Extract catalog reflection queries missing database = currentDatabase()
(select_statement
  (from_clause
    (table_expression
      (table_identifier) @catalog_table
      (#match? @catalog_table "^(system\\.)?(columns|tables)$")))
  (where_clause
    (binary_expression
      left: (column_identifier) @filter_col
      operator: "="
      right: (string_literal) @table_name)) @where_clause
  (#not-has-child? @where_predicate
    (binary_expression
      left: (column_identifier) @db_col
      (#match? @db_col "^database$")))) @unscoped_reflection_error
;; Python: Extract unbounded feature extraction loops
(function_definition
  name: (identifier) @fn_name
  (#match? @fn_name "extract_features|serialize_signals")
  body: (block
    (for_statement
      left: (identifier) @item
      right: (identifier) @source_collection
      body: (block
        (expression_statement
          (call
            function: (attribute
              object: (identifier) @target_list
              attribute: (identifier) @method
              (#eq? @method "append"))
            arguments: (argument_list
              (dictionary) @payload_dict))))))) @unbounded_payload_append
;; Rust: Extract fixed-size stack conversions with unwrap or expect
(call_expression
  function: (field_expression
    value: (call_expression
      function: (field_expression
        value: (_) @source_slice
        field: (field_identifier) @try_into_fn
        (#eq? @try_into_fn "try_into")))
    field: (field_identifier) @unwrap_fn
    (#match? @unwrap_fn "^(unwrap|expect)$"))) @fatal_slice_unwrap

Why the Graph Pass Stays Fast

Because the boundary graph typically holds a handful of schema and buffer nodes rather than an entire application call graph, Stokes skips containers and full compiler passes (rustc, mypy), parses only boundary files, normalizes declarations into SHA-256 digests, and checks stokes.lock in one pass.

Projection Consumption Isolation

Naive contract gates get stuck in constant false-positive lockouts: adding an internal analytics column breaks downstream proxy CI even though the proxy never reads that column. Stokes isolates unconsumed columns from the consumer boundary digest:

Tracked Projected Columns (200)

Explicitly queried fields (SELECT ja4_hash, ip_reputation, ...) are tracked by Stokes and included in the consumer boundary contract.

Unconsumed Offline Columns (5)

Internal metrics on the same ClickHouse table (bot_signals at 205 total columns) are isolated from the gate and ignored without CI failure.

CI fails only when a consumed column changes or when an unconstrained wildcard (SELECT * or unscoped system.columns reflection) is introduced. See Projection Isolation for how explicit field masks restrict reflected columns and Channel Manifests for manifest binding.

Search wiki and portfolio

type to search across all entries.