From 223224dfc9f8bc247b41cc3983aa2dc69a470d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20H=C3=BCck?= Date: Sat, 23 Dec 2023 14:28:41 +0100 Subject: [PATCH] Maintenance (#141) * Use detected python interp for tests * Improve source loc handling --- lib/passes/filter/IRSearch.h | 2 +- lib/support/System.cpp | 51 ++++++++++++++++--- lib/support/System.h | 2 +- test/CMakeLists.txt | 2 + test/lit.cfg | 2 + test/lit.site.cfg.in | 2 + test/pass/new_delete/09_inv_struct_delete.cpp | 4 -- test/runtime/46_source_location.c | 2 +- test/script/12_ir_viewer.sh | 8 +-- test/script/13_ir_compare.sh | 10 ++-- 10 files changed, 65 insertions(+), 20 deletions(-) diff --git a/lib/passes/filter/IRSearch.h b/lib/passes/filter/IRSearch.h index 70421082..0f7245b7 100644 --- a/lib/passes/filter/IRSearch.h +++ b/lib/passes/filter/IRSearch.h @@ -26,7 +26,7 @@ struct DefaultSearch { if (isa(val)) { // FIXME - // this mechanism tries to avoid endless recurison in loops, i.e., + // this mechanism tries to avoid endless recursion in loops, i.e., // do we bounce around multiple phi nodes (visit counter >1), then // we should likely skip search // see amg with openmp `amg2013/parcsr_ls/par_lr_interp.c` diff --git a/lib/support/System.cpp b/lib/support/System.cpp index e5705bc7..2d12b07e 100644 --- a/lib/support/System.cpp +++ b/lib/support/System.cpp @@ -13,7 +13,9 @@ #include "System.h" #include +#include #include +#include // For link_map, see SourceLocation::create #include #include #include @@ -125,14 +127,51 @@ class SourceLocHelper { } }; +struct RemoveEnvInScope { + explicit RemoveEnvInScope(std::string_view var_name) : var_name_(var_name) { + old_val_ = [](std::string_view env_var_name) { + const auto* env_data = getenv(env_var_name.data()); + if (env_data) { + return env_data; + } + return ""; + }(var_name); + + if (!old_val_.empty()) { + setenv(var_name.data(), "", true); + } + } + + ~RemoveEnvInScope() { + if (!old_val_.empty()) { + setenv(var_name_.data(), old_val_.data(), true); + } + } + + private: + std::string_view var_name_; + std::string_view old_val_; +}; + } // namespace system -std::optional SourceLocation::create(const void* addr) { - const auto pipe = [](const void* addr) -> std::optional { - using namespace system; - const auto& sloc_helper = SourceLocHelper::get(); +std::optional SourceLocation::create(const void* addr, intptr_t offset_ptr) { + // Preload might cause infinite recursion, hence temp. remove this flag in this scope only: + system::RemoveEnvInScope rm_preload_var{"LD_PRELOAD"}; + + const auto pipe = [](const void* paddr, intptr_t offset_ptr) -> std::optional { + const auto& sloc_helper = system::SourceLocHelper::get(); const auto& proc = system::Process::get(); + // FIXME: Inst Pointer points one past what we need with __built_in_return_addr(0), hacky way to fix: + const auto addr = [](const auto addr) { // reinterpret_cast(paddr) - offset_ptr; + // Transform addr to VMA Addr: + Dl_info info; + link_map* link_map; + dladdr1((void*)addr, &info, (void**)&link_map, RTLD_DL_LINKMAP); + return addr - link_map->l_addr; + }(reinterpret_cast(paddr) - offset_ptr); + if (sloc_helper.hasLLVMSymbolizer()) { std::ostringstream command; command << "llvm-symbolizer --demangle --output-style=GNU -f -e " << proc.exe() << " " << addr; @@ -144,7 +183,7 @@ std::optional SourceLocation::create(const void* addr) { if (sloc_helper.hasAddr2line()) { std::ostringstream command; - command << "addr2line --demangle=auto -f -e " << proc.exe() << " " << addr; + command << "addr2line --demangle=auto -f -e " << proc.exe() << " " << std::hex << addr; auto addr2line = system::CommandPipe::create(command.str()); if (addr2line) { return addr2line; @@ -152,7 +191,7 @@ std::optional SourceLocation::create(const void* addr) { } return {}; - }(addr); + }(addr, offset_ptr); if (!pipe) { return {}; diff --git a/lib/support/System.h b/lib/support/System.h index fbca840a..63b91faa 100644 --- a/lib/support/System.h +++ b/lib/support/System.h @@ -41,7 +41,7 @@ struct SourceLocation { std::string file; std::string line; - static std::optional create(const void* addr); + static std::optional create(const void* addr, intptr_t offset_ptr = 1); }; } // namespace typeart diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d823fae5..1d9b2fd7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,7 @@ typeart_find_llvm_progs(TYPEART_FILECHECK_EXEC "FileCheck-${LLVM_VERSION_MAJOR};FileCheck" ABORT_IF_MISSING) +find_package(Python3 COMPONENTS Interpreter) + if(LLVM_EXTERNAL_LIT) cmake_path(GET LLVM_EXTERNAL_LIT PARENT_PATH LLVM_EXTERNAL_LIT_DIR) endif() diff --git a/test/lit.cfg b/test/lit.cfg index 2b8bd486..7ce2b754 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -123,3 +123,5 @@ config.substitutions.append(('%wrapper-ir-compare', '{}/scripts/typeart_ir_compa config.substitutions.append(('%mpicxx-compiler', '{}'.format(config.mpicxx))) config.substitutions.append(('%mpicc-compiler', '{}'.format(config.mpicc))) config.substitutions.append(('%mpi-exec', '{}'.format(config.mpiexec))) + +config.substitutions.append(('%python-interp', config.python_interp)) diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in index 8e2e9ddd..082971ca 100644 --- a/test/lit.site.cfg.in +++ b/test/lit.site.cfg.in @@ -37,6 +37,8 @@ config.asan=@TYPEARTPASS_ASAN@ config.ubsan=@TYPEARTPASS_UBSAN@ config.coverage=@TYPEART_COVERAGE@ +config.python_interp = "@Python3_EXECUTABLE@" + # Let the main config do the real work. config.loaded_site_config = True lit_config.load_config(config, "@TYPEARTPASS_PROJECT_DIR@/test/lit.cfg") diff --git a/test/pass/new_delete/09_inv_struct_delete.cpp b/test/pass/new_delete/09_inv_struct_delete.cpp index 0bd6a8b0..d5138939 100644 --- a/test/pass/new_delete/09_inv_struct_delete.cpp +++ b/test/pass/new_delete/09_inv_struct_delete.cpp @@ -12,8 +12,6 @@ struct S1 { // CHECK: invoke{{.*}} i8* @_Znwm(i64{{( noundef)?}} 16) // CHECK: call void @__typeart_alloc(i8* [[POINTER:%[0-9a-z]+]], i32 {{2[0-9]+}}, i64 1) // CHECK: bitcast i8* [[POINTER]] to %struct.S1* -// CHECK-NOT: call void @_ZdlPv(i8*{{( noundef)?}} [[POINTER2:%[0-9a-z]+]]) -// CHECK-NOT: call void @__typeart_free(i8* {{.*}}[[POINTER2]]) void foo() { S1* b{nullptr}; try { @@ -28,8 +26,6 @@ void foo() { // CHECK: invoke{{.*}} i8* @_Znwm(i64{{( noundef)?}} 16) // CHECK: call void @__typeart_alloc(i8* [[POINTER:%[0-9a-z]+]], i32 {{2[0-9]+}}, i64 1) // CHECK: bitcast i8* [[POINTER]] to %struct.S1* -// CHECK-NOT: call void @_ZdaPv(i8*{{( noundef)?}} [[POINTER2:%[0-9a-z]+]]) -// CHECK-NOT: call void @__typeart_free(i8* {{.*}}[[POINTER2]]) int main() { try { S1* ss = new S1; diff --git a/test/runtime/46_source_location.c b/test/runtime/46_source_location.c index 2404bea2..8d07f7d1 100644 --- a/test/runtime/46_source_location.c +++ b/test/runtime/46_source_location.c @@ -65,7 +65,7 @@ int main(int argc, char** argv) { // CHECK: Address check OK // CHECK: Loc File:{{.*}}46_source_location.c // CHECK: Loc Function: main -// CHECK: Loc Line: {{[0-9]+}} +// CHECK: Loc Line: 33 // CHECK: Address check OK // CHECK: Address check OK // CHECK: Address check OK diff --git a/test/script/12_ir_viewer.sh b/test/script/12_ir_viewer.sh index 7a74c3de..d38fbfb2 100755 --- a/test/script/12_ir_viewer.sh +++ b/test/script/12_ir_viewer.sh @@ -1,7 +1,7 @@ #!/bin/bash # RUN: chmod +x %s -# RUN: %s %wrapper-ir-viewer %wrapper-cc %S | %filecheck %s +# RUN: %s %wrapper-ir-viewer %wrapper-cc %S %python-interp | %filecheck %s function exists() { @@ -14,6 +14,8 @@ function exists() { cd "$3" +python_interp="$4" + # CHECK: 0 # CHECK: 0 # CHECK: 0 @@ -23,7 +25,7 @@ exists 12_ir_viewer_target_heap.ll exists 12_ir_viewer_target_opt.ll exists 12_ir_viewer_target_stack.ll -python $1 -s -w "$2" 12_ir_viewer_target.c -- -g +"$python_interp" $1 -s -w "$2" 12_ir_viewer_target.c -- -g # CHECK: 1 # CHECK: 1 # CHECK: 1 @@ -35,7 +37,7 @@ exists 12_ir_viewer_target_opt.ll exists 12_ir_viewer_target_stack.ll exists 12_ir_viewer_target-types-ir-viewer.yaml -python $1 -c 12_ir_viewer_target.c +"$python_interp" $1 -c 12_ir_viewer_target.c # CHECK: 0 # CHECK: 0 # CHECK: 0 diff --git a/test/script/13_ir_compare.sh b/test/script/13_ir_compare.sh index 56db68dc..4da955f4 100755 --- a/test/script/13_ir_compare.sh +++ b/test/script/13_ir_compare.sh @@ -1,7 +1,7 @@ #!/bin/bash # RUN: chmod +x %s -# RUN: %s %wrapper-ir-compare %wrapper-cc %wrapper-cc %S | %filecheck %s +# RUN: %s %wrapper-ir-compare %wrapper-cc %wrapper-cc %S %python-interp | %filecheck %s function exists() { @@ -14,12 +14,14 @@ function exists() { cd "$4" +python_interp="$5" + # CHECK: 0 # CHECK: 0 exists 12_ir_viewer_target_heap.ll-a exists 12_ir_viewer_target_heap.ll-b -python $1 -s -a "$2" -b "$3" 12_ir_viewer_target.c -- -g +"$python_interp" $1 -s -a "$2" -b "$3" 12_ir_viewer_target.c -- -g # CHECK: 1 # CHECK: 1 exists 12_ir_viewer_target_heap.ll-a @@ -32,7 +34,7 @@ rm 12_ir_viewer_target_heap.ll-a 12_ir_viewer_target_heap.ll-b exists 12_ir_viewer_target_stack.ll-a exists 12_ir_viewer_target_stack.ll-b -python $1 -s -m stack -a "$2" -b "$3" 12_ir_viewer_target.c -- -g +"$python_interp" $1 -s -m stack -a "$2" -b "$3" 12_ir_viewer_target.c -- -g # CHECK: 1 # CHECK: 1 exists 12_ir_viewer_target_stack.ll-a @@ -40,7 +42,7 @@ exists 12_ir_viewer_target_stack.ll-b rm 12_ir_viewer_target_stack.ll-a 12_ir_viewer_target_stack.ll-b -python $1 -s -m base -a "$2" -b "$3" 12_ir_viewer_target.c -- -g +"$python_interp" $1 -s -m base -a "$2" -b "$3" 12_ir_viewer_target.c -- -g # CHECK: 1 # CHECK: 1 exists 12_ir_viewer_target_base.ll-a