Skip to content

Commit

Permalink
Re-apply "[ORC-RT] Add unit test infrastructure, extensible_rtti..."
Browse files Browse the repository at this point in the history
This reapplies 6d263b6 (which was reverted in 1c7c6f2) with a fix for a
CMake issue.
  • Loading branch information
lhames committed May 11, 2021
1 parent 5a9497d commit e0b6c99
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} powe
endif()
set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64})

if (APPLE)
if (UNIX)
set(ALL_ORC_SUPPORTED_ARCH ${X86_64})
endif()

Expand Down
9 changes: 7 additions & 2 deletions compiler-rt/lib/orc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# ORC runtime library implementation files.
set(ORC_SOURCES
placeholder.cpp
extensible_rtti.cpp
)

# Implementation files for all ORC architectures.
Expand All @@ -12,6 +12,7 @@ set(x86_64_SOURCES

set(ORC_IMPL_HEADERS
# Implementation headers will go here.
extensible_rtti.h
)

# Create list of all source files for
Expand Down Expand Up @@ -70,7 +71,7 @@ else() # not Apple
ARCHS ${arch}
SOURCES ${ORC_SOURCES} ${${arch}_SOURCES}
ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS}
CFLAGS $ORC_CFLAGS}
CFLAGS ${ORC_CFLAGS}
DEPS ${ORC_DEPS})

# Common ORC archive for instrumented binaries.
Expand All @@ -82,3 +83,7 @@ else() # not Apple
PARENT_TARGET orc)
endforeach()
endif() # not Apple

if(COMPILER_RT_INCLUDE_TESTS)
add_subdirectory(unittests)
endif()
24 changes: 24 additions & 0 deletions compiler-rt/lib/orc/extensible_rtti.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- extensible_rtti.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime support library.
//
// Note:
// This source file was adapted from lib/Support/ExtensibleRTTI.cpp, however
// the data structures are not shared and the code need not be kept in sync.
//
//===----------------------------------------------------------------------===//

#include "extensible_rtti.h"

namespace __orc_rt {

char RTTIRoot::ID = 0;
void RTTIRoot::anchor() {}

} // end namespace __orc_rt
145 changes: 145 additions & 0 deletions compiler-rt/lib/orc/extensible_rtti.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//===------ extensible_rtti.h - Extensible RTTI for ORC RT ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// \file
//
// Provides an extensible RTTI mechanism, that can be used regardless of whether
// the runtime is built with -frtti or not. This is predominantly used to
// support error handling.
//
// The RTTIRoot class defines methods for comparing type ids. Implementations
// of these methods can be injected into new classes using the RTTIExtends
// class template.
//
// E.g.
//
// @code{.cpp}
// class MyBaseClass : public RTTIExtends<MyBaseClass, RTTIRoot> {
// public:
// static char ID;
// virtual void foo() = 0;
// };
//
// class MyDerivedClass1 : public RTTIExtends<MyDerivedClass1, MyBaseClass> {
// public:
// static char ID;
// void foo() override {}
// };
//
// class MyDerivedClass2 : public RTTIExtends<MyDerivedClass2, MyBaseClass> {
// public:
// static char ID;
// void foo() override {}
// };
//
// char MyBaseClass::ID = 0;
// char MyDerivedClass1::ID = 0;
// char MyDerivedClass2:: ID = 0;
//
// void fn() {
// std::unique_ptr<MyBaseClass> B = std::make_unique<MyDerivedClass1>();
// outs() << isa<MyBaseClass>(B) << "\n"; // Outputs "1".
// outs() << isa<MyDerivedClass1>(B) << "\n"; // Outputs "1".
// outs() << isa<MyDerivedClass2>(B) << "\n"; // Outputs "0'.
// }
//
// @endcode
//
// Note:
// This header was adapted from llvm/Support/ExtensibleRTTI.h, however the
// data structures are not shared and the code need not be kept in sync.
//
//===----------------------------------------------------------------------===//

#ifndef ORC_RT_EXTENSIBLE_RTTI_H
#define ORC_RT_EXTENSIBLE_RTTI_H

namespace __orc_rt {

template <typename ThisT, typename ParentT> class RTTIExtends;

/// Base class for the extensible RTTI hierarchy.
///
/// This class defines virtual methods, dynamicClassID and isA, that enable
/// type comparisons.
class RTTIRoot {
public:
virtual ~RTTIRoot() = default;

/// Returns the class ID for this type.
static const void *classID() { return &ID; }

/// Returns the class ID for the dynamic type of this RTTIRoot instance.
virtual const void *dynamicClassID() const = 0;

/// Returns true if this class's ID matches the given class ID.
virtual bool isA(const void *const ClassID) const {
return ClassID == classID();
}

/// Check whether this instance is a subclass of QueryT.
template <typename QueryT> bool isA() const { return isA(QueryT::classID()); }

static bool classof(const RTTIRoot *R) { return R->isA<RTTIRoot>(); }

private:
virtual void anchor();

static char ID;
};

/// Inheritance utility for extensible RTTI.
///
/// Supports single inheritance only: A class can only have one
/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
/// though it can have many non-ExtensibleRTTI parents.
///
/// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
/// newly introduced type, and the *second* argument is the parent class.
///
/// class MyType : public RTTIExtends<MyType, RTTIRoot> {
/// public:
/// static char ID;
/// };
///
/// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> {
/// public:
/// static char ID;
/// };
///
template <typename ThisT, typename ParentT> class RTTIExtends : public ParentT {
public:
// Inherit constructors and isA methods from ParentT.
using ParentT::isA;
using ParentT::ParentT;

static char ID;

static const void *classID() { return &ThisT::ID; }

const void *dynamicClassID() const override { return &ThisT::ID; }

bool isA(const void *const ClassID) const override {
return ClassID == classID() || ParentT::isA(ClassID);
}

static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
};

template <typename ThisT, typename ParentT>
char RTTIExtends<ThisT, ParentT>::ID = 0;

/// Returns true if the given value is an instance of the template type
/// parameter.
template <typename To, typename From> bool isa(const From &Value) {
return To::classof(&Value);
}

} // end namespace __orc_rt

#endif // ORC_RT_EXTENSIBLE_RTTI_H
1 change: 0 additions & 1 deletion compiler-rt/lib/orc/placeholder.cpp

This file was deleted.

101 changes: 101 additions & 0 deletions compiler-rt/lib/orc/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
include(CompilerRTCompile)

include_directories(..)

add_custom_target(OrcRTUnitTests)
set_target_properties(OrcRTUnitTests PROPERTIES FOLDER "OrcRT unittests")

set(ORC_UNITTEST_CFLAGS
${ORC_CFLAGS}
${COMPILER_RT_UNITTEST_CFLAGS}
${COMPILER_RT_GTEST_CFLAGS}
-I${COMPILER_RT_SOURCE_DIR}/lib/orc
)

# We add the include directories one at a time in our CFLAGS.
foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
list(APPEND ORC_UNITTEST_CFLAGS -I${DIR})
endforeach()

function(add_orc_lib library)
add_library(${library} STATIC ${ARGN})
set_target_properties(${library} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
FOLDER "Compiler-RT Runtime tests")
endfunction()

function(get_orc_lib_for_arch arch lib)
if(APPLE)
set(tgt_name "RTOrc.test.osx")
else()
set(tgt_name "RTOrc.test.${arch}")
endif()
set(${lib} "${tgt_name}" PARENT_SCOPE)
endfunction()

set(ORC_TEST_ARCH ${ORC_SUPPORTED_ARCH})
set(ORC_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${CMAKE_THREAD_LIBS_INIT}
)

if(APPLE)
darwin_filter_host_archs(ORC_SUPPORTED_ARCH ORC_TEST_ARCH)
list(APPEND ORC_UNITTEST_CFLAGS ${DARWIN_osx_CFLAGS})
list(APPEND ORC_UNITTEST_LINK_FLAGS ${DARWIN_osx_LINK_FLAGS})
else()
append_list_if(COMPILER_RT_HAS_LIBM -lm ORC_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBRT -lrt ORC_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBDL -ldl ORC_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ORC_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBEXECINFO -lexecinfo ORC_UNITTEST_LINK_FLAGS)
endif()

foreach(lib ${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND ORC_UNITTEST_LINK_FLAGS -l${lib})
endforeach()

set(ORC_DEPS gtest orc)
# ORC uses C++ standard library headers.
if (TARGET cxx-headers OR HAVE_LIBCXX)
set(ORC_DEPS cxx-headers)
endif()

macro(add_orc_unittest testname)
cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
if(UNIX)
foreach(arch ${ORC_TEST_ARCH})
set(TEST_OBJECTS)
get_orc_lib_for_arch(${arch} ORC_RUNTIME_LIBS)
generate_compiler_rt_tests(TEST_OBJECTS
OrcRTUnitTests "${testname}-${arch}-Test" "${arch}"
SOURCES ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
RUNTIME "${ORC_RUNTIME_LIBS}"
COMPILE_DEPS ${TEST_HEADERS}
DEPS ${ORC_DEPS}
CFLAGS ${ORC_UNITTEST_CFLAGS}
LINK_FLAGS ${ORC_UNITTEST_LINK_FLAGS})
endforeach()
endif()
endmacro()

set(UNITTEST_SOURCES
extensible_rtti_test.cpp
orc_unit_test_main.cpp
)

if (COMPILER_RT_CAN_EXECUTE_TESTS)

if (APPLE)
add_orc_lib("RTOrc.test.osx"
$<TARGET_OBJECTS:RTOrc.osx>)
else()
foreach(arch ${ORC_SUPPORTED_ARCH})
add_orc_lib("RTOrc.test.${arch}"
$<TARGET_OBJECTS:RTOrc.${arch}>)
endforeach()
endif()

add_orc_unittest(OrcUnitTest SOURCES ${UNITTEST_SOURCES})

endif()
54 changes: 54 additions & 0 deletions compiler-rt/lib/orc/unittests/extensible_rtti_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===-- extensible_rtti_test.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime.
//
// Note:
// This unit test was adapted from
// llvm/unittests/Support/ExtensibleRTTITest.cpp
//
//===----------------------------------------------------------------------===//

#include "extensible_rtti.h"
#include "gtest/gtest.h"

using namespace __orc_rt;

namespace {

class MyBase : public RTTIExtends<MyBase, RTTIRoot> {};

class MyDerivedA : public RTTIExtends<MyDerivedA, MyBase> {};

class MyDerivedB : public RTTIExtends<MyDerivedB, MyBase> {};

} // end anonymous namespace

TEST(ExtensibleRTTITest, BaseCheck) {
MyBase MB;
MyDerivedA MDA;
MyDerivedB MDB;

// Check MB properties.
EXPECT_TRUE(isa<RTTIRoot>(MB));
EXPECT_TRUE(isa<MyBase>(MB));
EXPECT_FALSE(isa<MyDerivedA>(MB));
EXPECT_FALSE(isa<MyDerivedB>(MB));

// Check MDA properties.
EXPECT_TRUE(isa<RTTIRoot>(MDA));
EXPECT_TRUE(isa<MyBase>(MDA));
EXPECT_TRUE(isa<MyDerivedA>(MDA));
EXPECT_FALSE(isa<MyDerivedB>(MDA));

// Check MDB properties.
EXPECT_TRUE(isa<RTTIRoot>(MDB));
EXPECT_TRUE(isa<MyBase>(MDB));
EXPECT_FALSE(isa<MyDerivedA>(MDB));
EXPECT_TRUE(isa<MyDerivedB>(MDB));
}
18 changes: 18 additions & 0 deletions compiler-rt/lib/orc/unittests/orc_unit_test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- orc_unit_test_main.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"

int main(int argc, char **argv) {
testing::GTEST_FLAG(death_test_style) = "threadsafe";
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit e0b6c99

Please sign in to comment.