Skip to content

Commit

Permalink
Showing 7 changed files with 165 additions and 13 deletions.
7 changes: 6 additions & 1 deletion analyze/action.yml
@@ -4,17 +4,22 @@ author: 'GitHub'
inputs:
check_name:
description: The name of the check run to add text to.
required: false
output:
description: The path of the directory in which to save the SARIF results
required: false
default: '../results'
upload:
description: Upload the SARIF file
required: false
default: true
default: "true"
ram:
description: Override the amount of memory in MB to be used by CodeQL. By default, almost all the memory of the machine is used.
required: false
threads:
description: The number of threads to be used by CodeQL.
required: false
default: "1"
token:
default: ${{ github.token }}
matrix:
18 changes: 18 additions & 0 deletions lib/finalize-db.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/finalize-db.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions lib/finalize-db.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/finalize-db.test.js.map
58 changes: 58 additions & 0 deletions src/finalize-db.test.ts
@@ -0,0 +1,58 @@
import test from "ava";
import * as os from "os";

import {
getMemoryFlag,
getThreadsFlag
} from "./finalize-db";

test('getMemoryFlag() should return the correct --ram flag', t => {

const totalMem = os.totalmem() / (1024 * 1024);

const tests = {
"": `--ram=${totalMem - 256}`,
"512": "--ram=512",
};

for (const [input, expectedFlag] of Object.entries(tests)) {

process.env['INPUT_RAM'] = input;

const flag = getMemoryFlag();
t.deepEqual(flag, expectedFlag);
}
});

test('getMemoryFlag() throws if the ram input is < 0 or NaN', t => {
for (const input of ["-1", "hello!"]) {
process.env['INPUT_RAM'] = input;
t.throws(getMemoryFlag);
}
});

test('getThreadsFlag() should return the correct --threads flag', t => {

const numCpus = os.cpus().length;

const tests = {
"0": "--threads=0",
"1": "--threads=1",
[`${numCpus + 1}`]: `--threads=${numCpus}`
};

for (const [input, expectedFlag] of Object.entries(tests)) {

process.env['INPUT_THREADS'] = input;

const flag = getThreadsFlag();
t.deepEqual(flag, expectedFlag);
}
});

test('getThreadsFlag() throws if the ram input is < 0 or NaN', t => {
for (const input of ["-1", "hello!"]) {
process.env['INPUT_THREADS'] = input;
t.throws(getThreadsFlag);
}
});
39 changes: 28 additions & 11 deletions src/finalize-db.ts
@@ -20,7 +20,7 @@ import * as util from './util';
*
* Format is a map from language to an array of path suffixes of .ql files.
*/
const DISABLED_BUILTIN_QUERIES: {[language: string]: string[]} = {
const DISABLED_BUILTIN_QUERIES: { [language: string]: string[]; } = {
'csharp': [
'ql/src/Security Features/CWE-937/VulnerablePackage.ql',
'ql/src/Security Features/CWE-451/MissingXFrameOptions.ql',
@@ -32,7 +32,7 @@ function queryIsDisabled(language, query): boolean {
.some(disabledQuery => query.endsWith(disabledQuery));
}

function getMemoryFlag(): string {
export function getMemoryFlag(): string {
let memoryToUseMegaBytes: number;
const memoryToUseString = core.getInput("ram");
if (memoryToUseString) {
@@ -49,6 +49,22 @@ function getMemoryFlag(): string {
return "--ram=" + Math.floor(memoryToUseMegaBytes);
}

export function getThreadsFlag(): string {
let numThreads = 1;
const numThreadsString = core.getInput("threads");
if (numThreadsString) {
numThreads = Number(numThreadsString);
if (Number.isNaN(numThreads) || numThreads < 0) {
throw new Error(`Invalid threads setting "${numThreadsString}", specified.`);
}
const maxThreads = os.cpus().length;
if (numThreads > maxThreads) {
numThreads = maxThreads;
}
}
return `--threads=${numThreads}`;
}

async function createdDBForScannedLanguages(codeqlCmd: string, databaseFolder: string) {
const scannedLanguages = process.env[sharedEnv.CODEQL_ACTION_SCANNED_LANGUAGES];
if (scannedLanguages) {
@@ -93,14 +109,14 @@ async function finalizeDatabaseCreation(codeqlCmd: string, databaseFolder: strin
interface ResolveQueriesOutput {
byLanguage: {
[language: string]: {
[queryPath: string]: {}
}
[queryPath: string]: {};
};
};
noDeclaredLanguage: {
[queryPath: string]: {}
[queryPath: string]: {};
};
multipleDeclaredLanguages: {
[queryPath: string]: {}
[queryPath: string]: {};
};
}

@@ -116,11 +132,11 @@ async function runResolveQueries(codeqlCmd: string, queries: string[]): Promise<

await exec.exec(
codeqlCmd, [
'resolve',
'queries',
...queries,
'--format=bylanguage'
],
'resolve',
'queries',
...queries,
'--format=bylanguage'
],
options);

return JSON.parse(output);
@@ -201,6 +217,7 @@ async function runQueries(codeqlCmd: string, databaseFolder: string, sarifFolder
'database',
'analyze',
getMemoryFlag(),
getThreadsFlag(),
path.join(databaseFolder, database),
'--format=sarif-latest',
'--output=' + sarifFile,

0 comments on commit d55f711

Please sign in to comment.