Repository: compiler-explorer/compiler-explorer
Pull request #6399 (2024-04-25 12:39:57)

Fix duplicated filters in logging
Should fix https://github.com/compiler-explorer/infra/issues/1284



Patch:
diff --git a/lib/stats.ts b/lib/stats.ts
index 686320dc1..4429b2763 100644
--- a/lib/stats.ts
+++ b/lib/stats.ts
@@ -55,6 +55,7 @@ type CompilationRecord = {
     executionParamsHash: string;
     options: string[];
     filters: Record<string, boolean>;
+    backendOptions: string[];
     bypassCache: boolean;
     libraries: string[];
     tools: string[];
@@ -69,10 +70,12 @@ export function filterCompilerOptions(args: string[]): string[] {
     return args.filter(x => capturableArg.exec(x) && !unwantedArg.test(x));
 }
 
+// note: any type on `request` is on purpose, we cannot trust ParsedRequest to be truthful to the type as it is user input
+
 export function makeSafe(
     time: Date,
     compilerId: string,
-    request: ParsedRequest,
+    request: ParsedRequest | any,
     files: FiledataPair[],
     buildMethod: string,
 ): CompilationRecord {
@@ -84,8 +87,17 @@ export function makeSafe(
         executionParamsHash: getHash(request.executeParameters),
         options: filterCompilerOptions(request.options),
         filters: Object.fromEntries(
-            Object.entries(request.filters).filter(value => typeof value[1] === 'boolean'),
+            Object.entries(request.filters)
+                .filter(value => typeof value[1] === 'boolean')
+                .map(item => [item[0].toLowerCase(), item[1]]),
         ) as Record<string, boolean>,
+        backendOptions: Object.entries(
+            Object.fromEntries(
+                Object.entries(request.backendOptions)
+                    .filter(item => item[0] !== 'overrides')
+                    .map(item => [item[0].toLowerCase(), item[1]]),
+            ),
+        ).map(item => `${item[0]}=${item[1] ? '1' : '0'}`),
         bypassCache: !!request.bypassCache,
         libraries: (request.libraries || []).map(lib => lib.id + '/' + lib.version),
         tools: (request.tools || []).map(tool => tool.id),


Test patch:
diff --git a/test/stats-test.ts b/test/stats-test.ts
index 726c70a68..6210f2204 100644
--- a/test/stats-test.ts
+++ b/test/stats-test.ts
@@ -24,6 +24,7 @@
 
 import {describe, expect, it} from 'vitest';
 
+import {ParsedRequest} from '../lib/handlers/compile.js';
 import {filterCompilerOptions, KnownBuildMethod, makeSafe} from '../lib/stats.js';
 import {getHash} from '../lib/utils.js';
 
@@ -71,17 +72,17 @@ describe('Stats', () => {
             executionParamsHash: getHash(executionParameters),
             filters: {
                 binary: false,
-                binaryObject: false,
-                commentOnly: true,
-                debugCalls: false,
+                binaryobject: false,
+                commentonly: true,
+                debugcalls: false,
                 demangle: true,
                 directives: true,
-                dontMaskFilenames: true,
+                dontmaskfilenames: true,
                 execute: false,
                 intel: true,
                 labels: true,
-                libraryCode: true,
-                optOutput: true,
+                librarycode: true,
+                optoutput: true,
                 trim: true,
             },
             libraries: [],
@@ -91,12 +92,88 @@ describe('Stats', () => {
             tools: [],
             overrides: [],
             runtimeTools: [],
+            backendOptions: [],
             buildMethod: KnownBuildMethod.Compile,
         });
     });
+
     it('should filter compiler arguments', () => {
         expect(filterCompilerOptions(['-moo', 'foo', '/moo'])).toEqual(['-moo', '/moo']);
         expect(filterCompilerOptions(['-Dsecret=1234', '/Dsecret'])).toEqual([]);
         expect(filterCompilerOptions(['-ithings', '/Ithings'])).toEqual([]);
     });
+
+    it('should sanitize some duplications', () => {
+        const executionParameters = {};
+        expect(
+            makeSafe(
+                someDate,
+                'g130',
+                {
+                    source: '',
+                    options: ['-O2', '-fsanitize=undefined'],
+                    backendOptions: {
+                        overrides: [{name: 'test', value: '123'}],
+                        skipAsm: false,
+                        SKIPASM: 'hello123',
+                    },
+                    filters: {
+                        binary: false,
+                        binaryObject: false,
+                        execute: false,
+                        demangle: true,
+                        intel: true,
+                        labels: true,
+                        libraryCode: true,
+                        directives: true,
+                        commentOnly: true,
+                        trim: true,
+                        debugCalls: false,
+                        dontMaskFilenames: true,
+                        skipAsm: true,
+                        SKIPASM: true,
+                        skipasm: true,
+                        optOutput: true,
+                        preProcessLines: lines => lines,
+                        preProcessBinaryAsmLines: lines => lines,
+                    } as unknown as ParsedRequest,
+                    bypassCache: 0,
+                    tools: undefined,
+                    executeParameters: executionParameters,
+                    libraries: [],
+                },
+                [],
+                KnownBuildMethod.Compile,
+            ),
+        ).toEqual({
+            compilerId: 'g130',
+            bypassCache: false,
+            executionParamsHash: getHash(executionParameters),
+            filters: {
+                binary: false,
+                binaryobject: false,
+                commentonly: true,
+                debugcalls: false,
+                demangle: true,
+                directives: true,
+                dontmaskfilenames: true,
+                execute: false,
+                intel: true,
+                labels: true,
+                librarycode: true,
+                optoutput: true,
+                skipasm: true,
+                trim: true,
+            },
+            libraries: [],
+            options: ['-O2', '-fsanitize=undefined'],
+            sourceHash: getHash('[]'),
+            time: '2023-07-12T02:04:06.000Z',
+            tools: [],
+            overrides: ['test=123'],
+            backendOptions: ['skipasm=1'],
+            runtimeTools: [],
+            buildMethod: KnownBuildMethod.Compile,
+        });
+    });
 });