From f73671a5dd909c0e4e3aa39769471435463a3c09 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Tue, 3 Dec 2019 06:47:34 -0800 Subject: [PATCH] [test determinator] Matching on mangled C and C++ names Summary: The clang plugin exports C++ mangled names in hashed form for perf reasons, so we need to hash the incoming mangled names in the profiler samples, so that we can compare them. Reviewed By: skcho Differential Revision: D18761842 fbshipit-source-id: 3072b5e33 --- infer/src/atd/clang_profiler_samples.atd | 1 + .../src/test_determinator/testDeterminator.ml | 54 +++++++++++++++++-- .../profiler_samples.json | 30 +++++++++++ .../test_determinator.json.mod1.exp | 2 +- .../test_determinator.json.mod2.exp | 2 +- 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/infer/src/atd/clang_profiler_samples.atd b/infer/src/atd/clang_profiler_samples.atd index 6f63cfdad..2fafb84ed 100644 --- a/infer/src/atd/clang_profiler_samples.atd +++ b/infer/src/atd/clang_profiler_samples.atd @@ -8,6 +8,7 @@ type native_symbol = { name : string; ?mangled_name : string option; + ?hashed_mangled_name : string option; } type profiler_sample = { diff --git a/infer/src/test_determinator/testDeterminator.ml b/infer/src/test_determinator/testDeterminator.ml index 0252d57e4..76cf7c7cf 100644 --- a/infer/src/test_determinator/testDeterminator.ml +++ b/infer/src/test_determinator/testDeterminator.ml @@ -216,17 +216,56 @@ let remove_llvm_suffix_native_symbols native_symbols = let native_symbol_mangled_name = Option.map ~f:remove_llvm_suffix native_symbol.Clang_profiler_samples_t.mangled_name in - {Clang_profiler_samples_t.name= native_symbol_name; mangled_name= native_symbol_mangled_name} + { native_symbol with + Clang_profiler_samples_t.name= native_symbol_name + ; mangled_name= native_symbol_mangled_name } in List.map ~f:remove_llvm_suffix_native_symbol native_symbols +(* The clang plugin exports C++ mangled names in hashed form for perf reasons, so here we +hash the incoming mangled names in the profiler samples, so that we can compare them. *) +let add_hash_mangled_native_symbols native_symbols = + let add_hash_mangled_native_symbol native_symbol = + let hash_mangled mangled_name = + match mangled_name with + | Some mangled_name -> + Some (Fnv64Hash.fnv64_hash mangled_name) + | None -> + None + in + let native_symbol_hashed_mangled_name = + hash_mangled native_symbol.Clang_profiler_samples_t.mangled_name + in + { native_symbol with + Clang_profiler_samples_t.hashed_mangled_name= native_symbol_hashed_mangled_name } + in + List.map ~f:add_hash_mangled_native_symbol native_symbols + + +let match_mangled_names affected_function_mangled_name native_symbol = + match + (affected_function_mangled_name, native_symbol.Clang_profiler_samples_t.hashed_mangled_name) + with + | Some affected_function_mangled_name, Some hashed_mangled_name -> + String.equal affected_function_mangled_name hashed_mangled_name + | _ -> + false + + let match_profiler_samples_affected_methods native_symbols affected_methods = let match_samples_method affected_method = let match_sample_method affected_method native_symbol = match affected_method with - | Some (ClangProc.CFunction {name= affected_function_name}) -> - String.equal affected_function_name native_symbol.Clang_profiler_samples_t.name + | Some + (ClangProc.CFunction + {name= affected_function_name; mangled_name= affected_function_mangled_name}) -> + (* This is needed because for simple c functions from the standard library, the name + matches but the mangled name doesn't quite match, there is a __1 at the beginning + in the name we get from the plugin, but not in the name we get from the profiler samples. *) + if String.equal affected_function_name native_symbol.Clang_profiler_samples_t.name then + true + else match_mangled_names affected_function_mangled_name native_symbol | Some (ClangProc.ObjcMethod {mangled_name= affected_method_mangled_name}) -> String.equal affected_method_mangled_name native_symbol.Clang_profiler_samples_t.name | Some (ClangProc.ObjcBlock {mangled_name= affected_block_mangled_name}) -> ( @@ -236,6 +275,8 @@ let match_profiler_samples_affected_methods native_symbols affected_methods = String.equal native_symbol_mangled_name affected_block_mangled_name | None -> false ) + | Some (ClangProc.CppMethod {mangled_name= affected_function_mangled_name}) -> + match_mangled_names (Some affected_function_mangled_name) native_symbol | _ -> false (* TODO: deal with mangled names, other method kinds *) @@ -259,8 +300,11 @@ let clang_test_to_run ~clang_range_map ~source_file () = List.fold profiler_samples ~init:[] ~f:(fun acc ({test; native_symbols} : Clang_profiler_samples_t.profiler_sample) -> let native_symbols_no_suffix = remove_llvm_suffix_native_symbols native_symbols in - if match_profiler_samples_affected_methods native_symbols_no_suffix affected_methods then - test :: acc + let native_symbols_with_hash_mangled = + add_hash_mangled_native_symbols native_symbols_no_suffix + in + if match_profiler_samples_affected_methods native_symbols_with_hash_mangled affected_methods + then test :: acc else acc ) diff --git a/infer/tests/build_systems/clang_test_determinator/profiler_samples.json b/infer/tests/build_systems/clang_test_determinator/profiler_samples.json index f30b7bce2..3288d82f6 100644 --- a/infer/tests/build_systems/clang_test_determinator/profiler_samples.json +++ b/infer/tests/build_systems/clang_test_determinator/profiler_samples.json @@ -101,5 +101,35 @@ ], "field5": {}, "field6": {} + }, + { + "field1": {}, + "field2": {}, + "field3": {}, + "test": "label1_cpp_method", + "field4": {}, + "native_symbols": [ + { + "name": "area", + "mangled_name": "_ZN6Shapes4Cube4areaEv" + } + ], + "field5": {}, + "field6": {} + }, + { + "field1": {}, + "field2": {}, + "field3": {}, + "test": "label2_cpp_method", + "field4": {}, + "native_symbols": [ + { + "name": "sort", + "mangled_name": "_ZN6Shapes4Cube4sortEPS0_j" + } + ], + "field5": {}, + "field6": {} } ] diff --git a/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod1.exp b/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod1.exp index dfee7718f..c8eb5a0f9 100644 --- a/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod1.exp +++ b/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod1.exp @@ -1 +1 @@ -["label1_block","label1_c_function","label1_objc_method"] \ No newline at end of file +["label1_block","label1_c_function","label1_cpp_method","label1_objc_method"] \ No newline at end of file diff --git a/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod2.exp b/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod2.exp index cd91799b1..7a83e3990 100644 --- a/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod2.exp +++ b/infer/tests/build_systems/clang_test_determinator/test_determinator.json.mod2.exp @@ -1 +1 @@ -["label2_block","label2_c_function","label2_objc_method"] \ No newline at end of file +["label2_block","label2_c_function","label2_cpp_method","label2_objc_method"] \ No newline at end of file