Skip to content

codeql database import followed by finalize drops all source files and locations when the source root contains an XML or properties file #23205

Backlog

Description

@darkrift

Summary

Importing partial, unfinalized Java databases into another unfinalized database with
codeql database import, then running codeql database finalize, produces a database that has the classes
and methods but no source files, no locations and no source compilation units, as soon as the source root
contains an XML file (or a .properties file). The same partial databases finalize correctly when their
trap/ and src/ directories are copied into one database and finalize runs once.

The order used (init, trace-command, import into an unfinalized target, then finalize) is the one the
database import manual describes ("Import unfinalized database(s) into another unfinalized database").

Version

CodeQL CLI 2.27.5 (codeql-bundle-v2.27.5), macOS arm64. Not tried on other versions or platforms.

Reproduction

A source root src/ with three files:

A.java:      public class A { int f() { return 1; } }
B.java:      public class B { int g() { return new A().f(); } }
config.xml:  <config><item/></config>

The commands (the script below runs all of this and counts the results):

# 1. reference: one database
codeql database init --language=java --source-root=src --begin-tracing single
codeql database trace-command single -- javac -d cls1 src/A.java src/B.java
codeql database finalize single

# 2. two partial databases, imported into a third, then finalized
codeql database init --language=java --source-root=src --begin-tracing part1
codeql database init --language=java --source-root=src --begin-tracing part2
codeql database trace-command part1 -- javac -d cls2 src/A.java
codeql database trace-command part2 -- javac -cp cls2 -d cls3 src/B.java
codeql database init --language=java --source-root=src --begin-tracing merged
codeql database import -- merged part1 part2
codeql database finalize merged

The query run on both databases (a pack depending on codeql/java-all):

import java
select count(File f | f.getExtension() = "java"), count(File f | f.getExtension() = "xml"),
       count(CompilationUnit c | c.fromSource())
database .java files .xml files source compilation units
single 2 1 2
merged (import, then finalize) 0 1 0
merged, same steps without config.xml (control) 2 0 2
repro.sh (usage: repro.sh /path/to/codeql)
#!/bin/bash
# Needs a CodeQL CLI bundle with the java extractor; tested with 2.27.5. Put A.java, B.java and
# config.xml (above) in a `src/` directory next to this script.
set -u
CODEQL="$1"; HERE="$(cd "$(dirname "$0")" && pwd)"; SRC="$HERE/src"; W="$HERE/work"
JAVAC="$(dirname "$CODEQL")/tools/$(ls "$(dirname "$CODEQL")/tools" | grep -E '^(osx64|linux64)$' | head -1)/java/bin/javac"
export CODEQL_ALLOW_INSTALLATION_ANYWHERE=true
rm -rf "$W"; mkdir -p "$W"
cat > "$W/q.ql" <<'QL'
import java
select count(File f | f.getExtension() = "java"), count(File f | f.getExtension() = "xml"), count(CompilationUnit c | c.fromSource())
QL
printf 'name: repro/q\nversion: 0.0.1\ndependencies:\n  codeql/java-all: "*"\n' > "$W/qlpack.yml"
count() { "$CODEQL" query run --database="$1" --search-path "$(dirname "$CODEQL")" "$W/q.ql" 2>&1 | grep -E '^\| *[0-9]'; }

# 1. normal flow: init, trace, finalize
"$CODEQL" database init --language=java --source-root="$SRC" --begin-tracing "$W/single" >/dev/null 2>&1
"$CODEQL" database trace-command "$W/single" -- "$JAVAC" -d "$W/cls1" "$SRC/A.java" "$SRC/B.java" >/dev/null 2>&1
"$CODEQL" database finalize "$W/single" >/dev/null 2>&1
echo "single database           (java files, xml files, source CUs):"; count "$W/single"

# 2. two partial databases, `database import`, then finalize
for n in 1 2; do "$CODEQL" database init --language=java --source-root="$SRC" --begin-tracing "$W/part$n" >/dev/null 2>&1; done
"$CODEQL" database trace-command "$W/part1" -- "$JAVAC" -d "$W/cls2" "$SRC/A.java" >/dev/null 2>&1
"$CODEQL" database trace-command "$W/part2" -- "$JAVAC" -cp "$W/cls2" -d "$W/cls3" "$SRC/B.java" >/dev/null 2>&1
"$CODEQL" database init --language=java --source-root="$SRC" --begin-tracing "$W/merged" >/dev/null 2>&1
"$CODEQL" database import -- "$W/merged" "$W/part1" "$W/part2" >/dev/null 2>&1
"$CODEQL" database finalize "$W/merged" >/dev/null 2>&1
echo "import then finalize      (java files, xml files, source CUs):"; count "$W/merged"

What I found

  • Without config.xml in the source root, merged is correct (2 / 0 / 2).
  • database import itself loads the TRAP files: its log shows files, folders and locations_default
    merged. The data is lost afterwards.
  • With the XML file present, database finalize first runs the XML extractor over the source root, then
    imports those TRAP files in a second TRAP import (the finalize log shows Found N TRAP files for the XML
    files). It looks like that second import replaces the files, folders, containerparent and
    locations_default relations that the first import loaded, instead of adding to them. Classes and methods
    survive, because the XML TRAP does not touch their relations. Without an XML or properties file the second
    import only writes sourceLocationPrefix, which is why small repros without such files pass. (This is my
    reading of the logs; I have not seen the importer's code.)
  • --no-linkage-aware-import, and --fail-on-trap-errors --check-undefined-labels --check-redefined-labels --check-repeated-labels, on database import change nothing and report no errors.
  • Workaround: copy the partial databases' trap/ and src/ directories into one unfinalized database and run
    finalize once. On the reproduction this gives the same result as single (2 / 1 / 2). On a larger
    project (480 javac invocations split over 8 partial databases and merged this way) the analysis results
    were identical to those of a single extraction: 991 alerts, same rules, files and lines.

Expected

Either import followed by finalize keeps the imported files and locations, or the manual states that
finalize replaces data from an earlier import, so that the sequence cannot be used when the source root has
files for the finalize-time extractors.

Why it matters

Splitting the extraction of a large build into independent units (for example one unit per remote build
action) and merging the results is the documented purpose of database import ("distributed build
systems"). The failure is silent: analyze succeeds and reports no results at all, because every result sits
in a file that is missing from the database.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions