From d449dba58a3ff510f29f802b08223d9566f31968 Mon Sep 17 00:00:00 2001 From: Andre Maroneze <andre.maroneze@cea.fr> Date: Wed, 10 Feb 2021 16:18:45 +0100 Subject: [PATCH] [Kernel] audit mode: add check for missing hashes, with tests --- src/kernel_services/ast_queries/file.ml | 38 +++++++++++++++++---- tests/value/audit-in.json | 7 ++-- tests/value/audit.c | 3 +- tests/value/audit_included.h | 2 +- tests/value/audit_included_but_not_listed.h | 1 + tests/value/oracle/audit-out.json | 6 ++-- tests/value/oracle/audit.res.oracle | 9 +++-- 7 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 tests/value/audit_included_but_not_listed.h diff --git a/src/kernel_services/ast_queries/file.ml b/src/kernel_services/ast_queries/file.ml index 7c3f748eae6..63f07080b6a 100644 --- a/src/kernel_services/ast_queries/file.ml +++ b/src/kernel_services/ast_queries/file.ml @@ -1697,14 +1697,40 @@ let source_hashes_of_json path = (Yojson.Basic.pretty_to_string v) let check_source_hashes expected actual_table = - Hashtbl.iter (fun fp hash -> - let fp = Filepath.Normalized.to_pretty_string fp in - let expected_hash = List.assoc_opt fp expected in - if Some hash <> expected_hash then + let checked, diffs = + Hashtbl.fold (fun fp hash (acc_checked, acc_diffs) -> + let fp = Filepath.Normalized.to_pretty_string fp in + let expected_hash = List.assoc_opt fp expected in + let checked = Datatype.String.Set.add fp acc_checked in + let diffs = + if Some hash = expected_hash then acc_diffs + else (fp, hash, expected_hash) :: acc_diffs + in + checked, diffs + ) actual_table (Datatype.String.Set.empty, []) + in + if diffs <> [] then begin + let diffs = + List.sort (fun (fp1, _, _) (fp2, _, _) -> + Extlib.compare_ignore_case fp1 fp2) diffs + in + List.iter (fun (fp, got, expected) -> Kernel.warning ~wkey:Kernel.wkey_audit "different hashes for %s: got %s, expected %s" - fp hash (Option.value ~default:("<none> (not in list)") expected_hash) - ) actual_table + fp got (Option.value ~default:("<none> (not in list)") expected) + ) diffs + end; + let expected_names = List.map fst expected in + let missing = + List.filter (fun fp -> not (Datatype.String.Set.mem fp checked)) + expected_names + in + if missing <> [] then begin + let missing = List.sort Extlib.compare_ignore_case missing in + Kernel.warning ~wkey:Kernel.wkey_audit + "missing hashes for files:@\n%a" + (Pretty_utils.pp_list ~sep:"@\n" Format.pp_print_string) missing + end let print_and_exit cpp_commands = let print_cpp_cmd (cpp_cmd, _ppf, _) = diff --git a/tests/value/audit-in.json b/tests/value/audit-in.json index fc5e7af0541..d6abf18d0ff 100644 --- a/tests/value/audit-in.json +++ b/tests/value/audit-in.json @@ -1,7 +1,8 @@ { "sources": { - "tests/value/audit.c": "4574f9d5090319c57dab0c9cb81fda32", - "tests/value/audit_included.h": "6164442da331e54680e25ff2fe1ba74e" + "tests/value/audit.c": "01010101010101010101010101010101", + "tests/value/audit_included.h": "c2cc488143a476f69cf2ed04c3439e6e", + "tests/value/non_existing_file.h": "1234567890abcdef1234567890abcdef" }, "kernel": { "warning-categories": { @@ -73,4 +74,4 @@ ] } } -} \ No newline at end of file +} diff --git a/tests/value/audit.c b/tests/value/audit.c index 952edfab1ee..7b3e27c0eb1 100644 --- a/tests/value/audit.c +++ b/tests/value/audit.c @@ -1,9 +1,10 @@ /* run.config LOG: audit-out.json - STDOPT: #"-audit-check @PTEST_DIR@/audit-in.json -audit-prepare @PTEST_DIR@/result/audit-out.json" + STDOPT: #"-audit-check @PTEST_DIR@/audit-in.json -audit-prepare @PTEST_DIR@/result/audit-out.json -kernel-warn-key audit=active" */ #include "audit_included.h" +#include "audit_included_but_not_listed.h" void main() { float f = 2.1; // to trigger a syntactic warning diff --git a/tests/value/audit_included.h b/tests/value/audit_included.h index 0db378b820a..d4b481cc2ee 100644 --- a/tests/value/audit_included.h +++ b/tests/value/audit_included.h @@ -1 +1 @@ -// This file is included by audit.i +// This file is included by audit.c diff --git a/tests/value/audit_included_but_not_listed.h b/tests/value/audit_included_but_not_listed.h new file mode 100644 index 00000000000..d4b481cc2ee --- /dev/null +++ b/tests/value/audit_included_but_not_listed.h @@ -0,0 +1 @@ +// This file is included by audit.c diff --git a/tests/value/oracle/audit-out.json b/tests/value/oracle/audit-out.json index e12d53d0b0e..f9aa254ce11 100644 --- a/tests/value/oracle/audit-out.json +++ b/tests/value/oracle/audit-out.json @@ -70,7 +70,9 @@ } }, "sources": { - "tests/value/audit.c": "d91c35d6c3eb02ede2ca2bdf92fda63d", - "tests/value/audit_included.h": "6164442da331e54680e25ff2fe1ba74e" + "tests/value/audit.c": "08f73691217888d926a0ee15cbe18159", + "tests/value/audit_included.h": "c2cc488143a476f69cf2ed04c3439e6e", + "tests/value/audit_included_but_not_listed.h": + "c2cc488143a476f69cf2ed04c3439e6e" } } diff --git a/tests/value/oracle/audit.res.oracle b/tests/value/oracle/audit.res.oracle index c599da1fc5f..8cb4e84028b 100644 --- a/tests/value/oracle/audit.res.oracle +++ b/tests/value/oracle/audit.res.oracle @@ -1,8 +1,13 @@ [kernel:audit] Warning: - different hashes for tests/value/audit.c: got d91c35d6c3eb02ede2ca2bdf92fda63d, expected 4574f9d5090319c57dab0c9cb81fda32 + different hashes for tests/value/audit.c: got 08f73691217888d926a0ee15cbe18159, expected 01010101010101010101010101010101 +[kernel:audit] Warning: + different hashes for tests/value/audit_included_but_not_listed.h: got c2cc488143a476f69cf2ed04c3439e6e, expected <none> (not in list) +[kernel:audit] Warning: + missing hashes for files: + tests/value/non_existing_file.h [kernel] Audit: sources list written to: tests/value/result/audit-out.json [kernel] Parsing tests/value/audit.c (with preprocessing) -[kernel:parser:decimal-float] tests/value/audit.c:9: Warning: +[kernel:parser:decimal-float] tests/value/audit.c:10: Warning: Floating-point constant 2.1 is not represented exactly. Will use 0x1.0cccccccccccdp1. (warn-once: no further messages from category 'parser:decimal-float' will be emitted) [eva] Analyzing a complete application starting at main -- GitLab