Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc955cc

Browse files
committedSep 22, 2021
Make use of multi-language and indirect tracing
1 parent e40e887 commit cc955cc

19 files changed

+193
-78
lines changed
 

‎.github/workflows/pr-checks.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,9 @@ jobs:
207207
208208
- name: Build code
209209
shell: powershell
210-
# Note we want to make sure that the .win32env file is read correctly, so we unset the CODEQL_EXTRACTOR_CSHARP_ROOT from the .sh file.
211210
run: |
212211
cat ./codeql-runner/codeql-env.sh | Invoke-Expression
213-
$Env:CODEQL_EXTRACTOR_CSHARP_ROOT = ""
212+
$Env:CODEQL_EXTRACTOR_CSHARP_ROOT = "" # Unset an environment variable to make sure the tracer resists this
214213
& $Env:CODEQL_RUNNER dotnet build /p:UseSharedCompilation=false
215214
216215
- name: Upload tracer logs

‎lib/codeql.js

+25-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/init-action.js

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/init-action.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/init.js

+11-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/init.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/runner.js

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/runner.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/tracer-config.js

+22-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/tracer-config.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/util.js

-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/codeql.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as api from "./api-client";
1212
import { PackWithVersion } from "./config-utils";
1313
import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool!
1414
import { errorMatchers } from "./error-matcher";
15-
import { Language } from "./languages";
15+
import { isTracedLanguage, Language } from "./languages";
1616
import { Logger } from "./logging";
1717
import * as toolcache from "./toolcache";
1818
import { toolrunnerErrorCatcher } from "./toolrunner-error-catcher";
@@ -75,6 +75,16 @@ export interface CodeQL {
7575
language: Language,
7676
sourceRoot: string
7777
): Promise<void>;
78+
/**
79+
* Run 'codeql database init --db-cluster'.
80+
*/
81+
databaseInitCluster(
82+
databasePath: string,
83+
languages: Language[],
84+
sourceRoot: string,
85+
processName: string | undefined,
86+
processLevel: number | undefined
87+
): Promise<void>;
7888
/**
7989
* Runs the autobuilder for the given language.
8090
*/
@@ -198,6 +208,7 @@ const CODEQL_VERSION_DIAGNOSTICS = "2.5.6";
198208
const CODEQL_VERSION_METRICS = "2.5.5";
199209
const CODEQL_VERSION_GROUP_RULES = "2.5.5";
200210
const CODEQL_VERSION_SARIF_GROUP = "2.5.3";
211+
export const CODEQL_VERSION_NEW_TRACING = "2.6.0"; // Use multi-language (>= 2.5.6) and indirect (>= 2.6.0) tracing.
201212

202213
function getCodeQLBundleName(): string {
203214
let platform: string;
@@ -528,6 +539,7 @@ export function setCodeQL(partialCodeql: Partial<CodeQL>): CodeQL {
528539
printVersion: resolveFunction(partialCodeql, "printVersion"),
529540
getTracerEnv: resolveFunction(partialCodeql, "getTracerEnv"),
530541
databaseInit: resolveFunction(partialCodeql, "databaseInit"),
542+
databaseInitCluster: resolveFunction(partialCodeql, "databaseInitCluster"),
531543
runAutobuild: resolveFunction(partialCodeql, "runAutobuild"),
532544
extractScannedLanguage: resolveFunction(
533545
partialCodeql,
@@ -643,6 +655,32 @@ async function getCodeQLForCmd(
643655
...getExtraOptionsFromEnv(["database", "init"]),
644656
]);
645657
},
658+
async databaseInitCluster(
659+
databasePath: string,
660+
languages: Language[],
661+
sourceRoot: string,
662+
processName: string | undefined,
663+
processLevel: number | undefined
664+
) {
665+
const extraArgs = languages.map((language) => `--language=${language}`);
666+
if (languages.filter(isTracedLanguage).length > 0) {
667+
extraArgs.push("--begin-tracing");
668+
if (processName !== undefined) {
669+
extraArgs.push(`--trace-process-name=${processName}`);
670+
} else {
671+
extraArgs.push(`--trace-process-level=${processLevel || 3}`);
672+
}
673+
}
674+
await runTool(cmd, [
675+
"database",
676+
"init",
677+
"--db-cluster",
678+
databasePath,
679+
`--source-root=${sourceRoot}`,
680+
...extraArgs,
681+
...getExtraOptionsFromEnv(["database", "init"]),
682+
]);
683+
},
646684
async runAutobuild(language: Language) {
647685
const cmdName =
648686
process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh";

‎src/init-action.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
StatusReportBase,
1313
validateWorkflow,
1414
} from "./actions-util";
15-
import { CodeQL } from "./codeql";
15+
import { CodeQL, CODEQL_VERSION_NEW_TRACING } from "./codeql";
1616
import * as configUtils from "./config-utils";
1717
import {
1818
initCodeQL,
@@ -30,6 +30,7 @@ import {
3030
Mode,
3131
checkGitHubVersionInRange,
3232
getGitHubVersion,
33+
codeQlVersionAbove,
3334
} from "./util";
3435

3536
// eslint-disable-next-line import/no-commonjs
@@ -210,13 +211,22 @@ async function run() {
210211
getOptionalInput("source-root") || ""
211212
);
212213

213-
const tracerConfig = await runInit(codeql, config, sourceRoot);
214+
const tracerConfig = await runInit(
215+
codeql,
216+
config,
217+
sourceRoot,
218+
"Runner.Worker.exe",
219+
undefined
220+
);
214221
if (tracerConfig !== undefined) {
215222
for (const [key, value] of Object.entries(tracerConfig.env)) {
216223
core.exportVariable(key, value);
217224
}
218225

219-
if (process.platform === "win32") {
226+
if (
227+
process.platform === "win32" &&
228+
!(await codeQlVersionAbove(codeql, CODEQL_VERSION_NEW_TRACING))
229+
) {
220230
await injectWindowsTracer(
221231
"Runner.Worker.exe",
222232
undefined,

‎src/init.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import * as safeWhich from "@chrisgavin/safe-which";
66

77
import * as analysisPaths from "./analysis-paths";
88
import { GitHubApiCombinedDetails, GitHubApiDetails } from "./api-client";
9-
import { CodeQL, setupCodeQL } from "./codeql";
9+
import { CodeQL, CODEQL_VERSION_NEW_TRACING, setupCodeQL } from "./codeql";
1010
import * as configUtils from "./config-utils";
1111
import { Logger } from "./logging";
1212
import { RepositoryNwo } from "./repository";
1313
import { TracerConfig, getCombinedTracerConfig } from "./tracer-config";
1414
import * as util from "./util";
15+
import { codeQlVersionAbove } from "./util";
1516

1617
export async function initCodeQL(
1718
codeqlURL: string | undefined,
@@ -75,18 +76,30 @@ export async function initConfig(
7576
export async function runInit(
7677
codeql: CodeQL,
7778
config: configUtils.Config,
78-
sourceRoot: string
79+
sourceRoot: string,
80+
processName: string | undefined,
81+
processLevel: number | undefined
7982
): Promise<TracerConfig | undefined> {
8083
fs.mkdirSync(config.dbLocation, { recursive: true });
8184

82-
// TODO: replace this code once CodeQL supports multi-language tracing
83-
for (const language of config.languages) {
84-
// Init language database
85-
await codeql.databaseInit(
86-
util.getCodeQLDatabasePath(config, language),
87-
language,
88-
sourceRoot
85+
if (await codeQlVersionAbove(codeql, CODEQL_VERSION_NEW_TRACING)) {
86+
// Init a database cluster
87+
await codeql.databaseInitCluster(
88+
config.dbLocation,
89+
config.languages,
90+
sourceRoot,
91+
processName,
92+
processLevel
8993
);
94+
} else {
95+
for (const language of config.languages) {
96+
// Init language database
97+
await codeql.databaseInit(
98+
util.getCodeQLDatabasePath(config, language),
99+
language,
100+
sourceRoot
101+
);
102+
}
90103
}
91104

92105
return await getCombinedTracerConfig(config, codeql);

‎src/runner.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Command } from "commander";
66

77
import { runFinalize, runQueries } from "./analyze";
88
import { determineAutobuildLanguage, runAutobuild } from "./autobuild";
9-
import { CodeQL, getCodeQL } from "./codeql";
9+
import { CodeQL, CODEQL_VERSION_NEW_TRACING, getCodeQL } from "./codeql";
1010
import { Config, getConfig } from "./config-utils";
1111
import { initCodeQL, initConfig, injectWindowsTracer, runInit } from "./init";
1212
import { Language, parseLanguage } from "./languages";
@@ -23,6 +23,7 @@ import {
2323
getGitHubAuth,
2424
initializeEnvironment,
2525
Mode,
26+
codeQlVersionAbove,
2627
} from "./util";
2728

2829
// eslint-disable-next-line import/no-commonjs
@@ -226,12 +227,21 @@ program
226227
);
227228

228229
const sourceRoot = checkoutPath;
229-
const tracerConfig = await runInit(codeql, config, sourceRoot);
230+
const tracerConfig = await runInit(
231+
codeql,
232+
config,
233+
sourceRoot,
234+
parseTraceProcessName(),
235+
parseTraceProcessLevel()
236+
);
230237
if (tracerConfig === undefined) {
231238
return;
232239
}
233240

234-
if (process.platform === "win32") {
241+
if (
242+
process.platform === "win32" &&
243+
!(await codeQlVersionAbove(codeql, CODEQL_VERSION_NEW_TRACING))
244+
) {
235245
await injectWindowsTracer(
236246
parseTraceProcessName(),
237247
parseTraceProcessLevel(),

‎src/tracer-config.ts

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4-
import { CodeQL } from "./codeql";
4+
import { CodeQL, CODEQL_VERSION_NEW_TRACING } from "./codeql";
55
import * as configUtils from "./config-utils";
66
import { Language, isTracedLanguage } from "./languages";
77
import * as util from "./util";
8+
import { codeQlVersionAbove } from "./util";
89

910
export type TracerConfig = {
1011
spec: string;
@@ -19,6 +20,24 @@ const CRITICAL_TRACER_VARS = new Set([
1920
"SEMMLE_JAVA_TOOL_OPTIONS",
2021
]);
2122

23+
export async function getTracerConfigForCluster(
24+
config: configUtils.Config
25+
): Promise<TracerConfig> {
26+
const tracingEnvVariables = JSON.parse(
27+
fs.readFileSync(
28+
path.resolve(
29+
config.dbLocation,
30+
"temp/tracingEnvironment/start-tracing.json"
31+
),
32+
"utf8"
33+
)
34+
);
35+
return {
36+
spec: tracingEnvVariables["ODASA_TRACER_CONFIGURATION"],
37+
env: tracingEnvVariables,
38+
};
39+
}
40+
2241
export async function getTracerConfigForLanguage(
2342
codeql: CodeQL,
2443
config: configUtils.Config,
@@ -179,16 +198,21 @@ export async function getCombinedTracerConfig(
179198
return undefined;
180199
}
181200

182-
// Get all the tracer configs and combine them together
183-
const tracedLanguageConfigs: { [lang: string]: TracerConfig } = {};
184-
for (const language of tracedLanguages) {
185-
tracedLanguageConfigs[language] = await getTracerConfigForLanguage(
186-
codeql,
187-
config,
188-
language
189-
);
201+
let mainTracerConfig: TracerConfig;
202+
if (await codeQlVersionAbove(codeql, CODEQL_VERSION_NEW_TRACING)) {
203+
mainTracerConfig = await getTracerConfigForCluster(config);
204+
} else {
205+
// Get all the tracer configs and combine them together
206+
const tracedLanguageConfigs: { [lang: string]: TracerConfig } = {};
207+
for (const language of tracedLanguages) {
208+
tracedLanguageConfigs[language] = await getTracerConfigForLanguage(
209+
codeql,
210+
config,
211+
language
212+
);
213+
}
214+
mainTracerConfig = concatTracerConfigs(tracedLanguageConfigs, config);
190215
}
191-
const mainTracerConfig = concatTracerConfigs(tracedLanguageConfigs, config);
192216

193217
// Add a couple more variables
194218
mainTracerConfig.env["ODASA_TRACER_CONFIGURATION"] = mainTracerConfig.spec;

‎src/util.ts

-15
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,6 @@ enum EnvVar {
422422
* not the cli.
423423
*/
424424
FEATURE_WILL_UPLOAD = "CODEQL_ACTION_FEATURE_WILL_UPLOAD",
425-
426-
/**
427-
* If set to the "true" string, then the codeql-action is using its
428-
* own deprecated and non-standard way of scanning for multiple
429-
* languages.
430-
*/
431-
FEATURE_MULTI_LANGUAGE = "CODEQL_ACTION_FEATURE_MULTI_LANGUAGE",
432-
433-
/**
434-
* If set to the "true" string, then the codeql-action is using its
435-
* own sandwiched workflow mechanism
436-
*/
437-
FEATURE_SANDWICH = "CODEQL_ACTION_FEATURE_SANDWICH",
438425
}
439426

440427
export function initializeEnvironment(mode: Mode, version: string) {
@@ -450,8 +437,6 @@ export function initializeEnvironment(mode: Mode, version: string) {
450437
exportVar(EnvVar.VERSION, version);
451438
exportVar(EnvVar.FEATURE_SARIF_COMBINE, "true");
452439
exportVar(EnvVar.FEATURE_WILL_UPLOAD, "true");
453-
exportVar(EnvVar.FEATURE_MULTI_LANGUAGE, "true");
454-
exportVar(EnvVar.FEATURE_SANDWICH, "true");
455440
}
456441

457442
export function getMode(): Mode {

0 commit comments

Comments
 (0)
Please sign in to comment.