Skip to content

Commit

Permalink
Export of internal Abseil changes
Browse files Browse the repository at this point in the history
--
803abc2dcad8b2354c988e9bf58dac4a17683832 by Gennadiy Rozental <rogeeff@google.com>:

Avoid warning when RTTI is not enabled.

PiperOrigin-RevId: 294247546

--
5a7b0b4d07d1d6e56fbb0b0ffbf4f8fcab772dbf by Derek Mauro <dmauro@google.com>:

Add a public Abseil FAQ

PiperOrigin-RevId: 294226960

--
6945c4a6df7d7679711fea31aacf4fba6ac7baa1 by Gennadiy Rozental <rogeeff@google.com>:

Re-enable type mismatch check, which works in all the cases including shared libraries.

We will use RTTI in case when our hand written approximation of it reports a type mismatch. This way we can ensure that if a flag is defined in one shared object and referenced in another we do not report spurious errors.

PiperOrigin-RevId: 293905563
GitOrigin-RevId: 803abc2dcad8b2354c988e9bf58dac4a17683832
Change-Id: I1a23776d227ed2734c2e7183323786b7a95c3cc7
  • Loading branch information
Abseil Team authored and mbxx committed Feb 10, 2020
1 parent d95d156 commit bf78e97
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 71 deletions.
144 changes: 144 additions & 0 deletions absl/FAQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Abseil FAQ

## Is Abseil the right home for my utility library?

Most often the answer to the question is "no." As both the [About
Abseil](https://abseil.io/about/) page and our [contributing
guidelines](https://github.com/abseil/abseil-cpp/blob/master/CONTRIBUTING.md#contribution-guidelines)
explain, Abseil contains a variety of core C++ library code that is widely used
at [Google](https://www.google.com/). As such, Abseil's primary purpose is to be
used as a dependency by Google's open source C++ projects. While we do hope that
Abseil is also useful to the C++ community at large, this added constraint also
means that we are unlikely to accept a contribution of utility code that isn't
already widely used by Google.

## How to I set the C++ dialect used to build Abseil?

The short answer is that whatever mechanism you choose, you need to make sure
that you set this option consistently at the global level for your entire
project. If, for example, you want to set the C++ dialect to C++17, with
[Bazel](https://bazel/build/) as the build system and `gcc` or `clang` as the
compiler, there several ways to do this:
* Pass `--cxxopt=-std=c++17` on the command line (for example, `bazel build
--cxxopt=-std=c++17 ...`)
* Set the environment variable `BAZEL_CXXOPTS` (for example,
`BAZEL_CXXOPTS=-std=c++17`)
* Add `build --cxxopt=-std=c++17` to your [`.bazelrc`
file](https://docs.bazel.build/versions/master/guide.html#bazelrc)

If you are using CMake as the build system, you'll need to add a line like
`set(CMAKE_CXX_STANDARD 17)` to your top level `CMakeLists.txt` file. See the
[CMake build
instructions](https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md)
for more information.

For a longer answer to this question and to understand why some other approaches
don't work, see the answer to "What is ABI and why don't you recommend using a
pre-compiled version of Abseil?"

## What is ABI and why don't you recommend using a pre-compiled version of Abseil?

For the purposes of this discussion, you can think of
[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) as the
compiled representation of the interfaces in code. This is in contrast to
[API](https://en.wikipedia.org/wiki/Application_programming_interface), which
you can think of as the interfaces as defined by the code itself. [Abseil has a
strong promise of API compatibility, but does not make any promise of ABI
compatibility](https://abseil.io/about/compatibility). Let's take a look at what
this means in practice.

You might be tempted to do something like this in a
[Bazel](https://bazel.build/) `BUILD` file:

```
# DON'T DO THIS!!!
cc_library(
name = "my_library",
srcs = ["my_library.cc"],
copts = ["-std=c++17"], # May create a mixed-mode compile!
deps = ["@com_google_absl//absl/strings"],
)
```

Applying `-std=c++17` to an individual target in your `BUILD` file is going to
compile that specific target in C++17 mode, but it isn't going to ensure the
Abseil library is built in C++17 mode, since the Abseil library itself is a
different build target. If your code includes an Abseil header, then your
program may contain conflicting definitions of the same
class/function/variable/enum, etc. As a rule, all compile options that affect
the ABI of a program need to be applied to the entire build on a global basis.

C++ has something called the [One Definition
Rule](https://en.wikipedia.org/wiki/One_Definition_Rule) (ODR). C++ doesn't
allow multiple definitions of the same class/function/variable/enum, etc. ODR
violations sometimes result in linker errors, but linkers do not always catch
violations. Uncaught ODR violations can result in strange runtime behaviors or
crashes that can be hard to debug.

If you build the Abseil library and your code using different compile options
that affect ABI, there is a good chance you will run afoul of the One Definition
Rule. Examples of GCC compile options that affect ABI include (but aren't
limited to) language dialect (e.g. `-std=`), optimization level (e.g. `-O2`),
code generation flags (e.g. `-fexceptions`), and preprocessor defines
(e.g. `-DNDEBUG`).

If you use a pre-compiled version of Abseil, (for example, from your Linux
distribution package manager or from something like
[vcpkg](https://github.com/microsoft/vcpkg)) you have to be very careful to
ensure ABI compatibility across the components of your program. The only way you
can be sure your program is going to be correct regarding ABI is to ensure
you've used the exact same compile options as were used to build the
pre-compiled library. This does not mean that Abseil cannot work as part of a
Linux distribution since a knowledgeable binary packager will have ensured that
all packages have been built with consistent compile options. This is one of the
reasons we warn against - though do not outright reject - using Abseil as a
pre-compiled library.

Another possible way that you might afoul of ABI issues is if you accidentally
include two versions of Abseil in your program. Multiple versions of Abseil can
end up within the same binary if your program uses the Abseil library and
another library also transitively depends on Abseil (resulting in what is
sometimes called the diamond dependency problem). In cases such as this you must
structure your build so that all libraries use the same version of Abseil.
[Abseil's strong promise of API compatibility between
releases](https://abseil.io/about/compatibility) means the latest "HEAD" release
of Abseil is almost certainly the right choice if you are doing as we recommend
and building all of your code from source.

For these reasons we recommend you avoid pre-compiled code and build the Abseil
library yourself in a consistent manner with the rest of your code.

## What is "live at head" and how do I do it?

From Abseil's point-of-view, "live at head" means that every Abseil source
release (which happens on an almost daily basis) is either API compatible with
the previous release, or comes with an automated tool that you can run over code
to make it compatible. In practice, the need to use an automated tool is
extremely rare. This means that upgrading from one source release to another
should be a routine practice that can and should be performed often.

We recommend you update to the latest release of Abseil as often as
possible. Not only will you pick up bug fixes more quickly, but if you have good
automated testing, you will catch and be able to fix any [Hyrum's
Law](https://www.hyrumslaw.com/) dependency problems on an incremental basis
instead of being overwhelmed by them and having difficulty isolating them if you
wait longer between updates.

If you are using the [Bazel](https://bazel.build/) build system and its
[external dependencies](https://docs.bazel.build/versions/master/external.html)
feature, updating the
[`http_archive`](https://docs.bazel.build/versions/master/repo/http.html#http_archive)
rule in your
[`WORKSPACE`](https://docs.bazel.build/versions/master/be/workspace.html) for
`com_google_abseil` to point to the latest release is all you need to do. You
can commit the updated `WORKSPACE` file to your source control every time you
update, and if you have good automated testing, you might even consider
automating this.

One thing we don't recommend is using GitHub's `master.zip` files (for example
[https://github.com/abseil/abseil-cpp/archive/master.zip](https://github.com/abseil/abseil-cpp/archive/master.zip)),
which are always the latest commit in the `master` branch, to implement live at
head. Since these `master.zip` URLs are not versioned, you will lose build
reproducibility. In addition, some build systems, including Bazel, will simply
cache this file, which means you won't actually be updating to the latest
release until your cache is cleared or invalidated.
1 change: 1 addition & 0 deletions absl/flags/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ cc_library(
"//absl/flags:__pkg__",
],
deps = [
":config",
":marshalling",
"//absl/base:config",
"//absl/base:core_headers",
Expand Down
8 changes: 8 additions & 0 deletions absl/flags/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@
#define ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD 1
#endif

// ABSL_FLAGS_INTERNAL_HAS_RTTI macro is used for selecting if we can use RTTI
// for flag type identification.
#ifdef ABSL_FLAGS_INTERNAL_HAS_RTTI
#error ABSL_FLAGS_INTERNAL_HAS_RTTI cannot be directly set
#elif !defined(__GNUC__) || defined(__GXX_RTTI)
#define ABSL_FLAGS_INTERNAL_HAS_RTTI 1
#endif // !defined(__GNUC__) || defined(__GXX_RTTI)

#endif // ABSL_FLAGS_CONFIG_H_
7 changes: 0 additions & 7 deletions absl/flags/flag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
namespace absl {
ABSL_NAMESPACE_BEGIN

#ifndef NDEBUG
#define ABSL_FLAGS_GET(T) \
T GetFlag(const absl::Flag<T>& flag) { return flag.Get(); }
ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_GET)
#undef ABSL_FLAGS_GET
#endif

// This global mutex protects on-demand construction of flag objects in MSVC
// builds.
#if defined(_MSC_VER) && !defined(__clang__)
Expand Down
15 changes: 0 additions & 15 deletions absl/flags/flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,6 @@ ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
return flag.Get();
}

#ifndef NDEBUG
// We want to validate the type mismatch between type definition and
// declaration. The lock-free implementation does not allow us to do it,
// so in debug builds we always use the slower implementation, which always
// validates the type.

// We currently need an external linkage for built-in types because shared
// libraries have different addresses of flags_internal::FlagOps<T> which
// might cause log spam when checking the same flag type.
#define ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT(T) \
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT)
#undef ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT
#endif

// SetFlag()
//
// Sets the value of an `absl::Flag` to the value `v`. Do not construct an
Expand Down
13 changes: 7 additions & 6 deletions absl/flags/flag_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,20 @@ TEST_F(FlagTest, TestCustomUDT) {

// MSVC produces link error on the type mismatch.
// Linux does not have build errors and validations work as expected.
#if 0 // !defined(_WIN32) && GTEST_HAS_DEATH_TEST
#if !defined(_WIN32) && GTEST_HAS_DEATH_TEST

TEST(Flagtest, TestTypeMismatchValidations) {
// For builtin types, GetFlag() only does validation in debug mode.
using FlagDeathTest = FlagTest;

TEST_F(FlagDeathTest, TestTypeMismatchValidations) {
EXPECT_DEBUG_DEATH(
absl::GetFlag(FLAGS_mistyped_int_flag),
static_cast<void>(absl::GetFlag(FLAGS_mistyped_int_flag)),
"Flag 'mistyped_int_flag' is defined as one type and declared "
"as another");
EXPECT_DEATH(absl::SetFlag(&FLAGS_mistyped_int_flag, 0),
EXPECT_DEATH(absl::SetFlag(&FLAGS_mistyped_int_flag, 1),
"Flag 'mistyped_int_flag' is defined as one type and declared "
"as another");

EXPECT_DEATH(absl::GetFlag(FLAGS_mistyped_string_flag),
EXPECT_DEATH(static_cast<void>(absl::GetFlag(FLAGS_mistyped_string_flag)),
"Flag 'mistyped_string_flag' is defined as one type and "
"declared as another");
EXPECT_DEATH(
Expand Down
19 changes: 18 additions & 1 deletion absl/flags/internal/commandlineflag.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

#include <memory>
#include <string>
#include <typeinfo>

#include "absl/base/config.h"
#include "absl/base/macros.h"
#include "absl/flags/config.h"
#include "absl/flags/marshalling.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
Expand All @@ -41,7 +43,10 @@ enum FlagOp {
kCopyConstruct,
kSizeof,
kParse,
kUnparse
kUnparse,
#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
kRuntimeTypeId
#endif
};
using FlagOpFn = void* (*)(FlagOp, const void*, void*);
using FlagMarshallingOpFn = void* (*)(FlagOp, const void*, void*, void*);
Expand Down Expand Up @@ -84,6 +89,11 @@ void* FlagOps(FlagOp op, const void* v1, void* v2) {
return nullptr;
case kSizeof:
return reinterpret_cast<void*>(sizeof(T));
#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
case kRuntimeTypeId:
return const_cast<std::type_info*>(&typeid(T));
break;
#endif
default:
return nullptr;
}
Expand Down Expand Up @@ -146,6 +156,13 @@ inline size_t Sizeof(FlagOpFn op) {
op(flags_internal::kSizeof, nullptr, nullptr)));
}

#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
inline const std::type_info& RuntimeTypeId(FlagOpFn op) {
return *static_cast<const std::type_info*>(
op(flags_internal::kRuntimeTypeId, nullptr, nullptr));
}
#endif

// Handle to FlagState objects. Specific flag state objects will restore state
// of a flag produced this flag state from method CommandLineFlag::SaveState().
class FlagStateInterface {
Expand Down
45 changes: 23 additions & 22 deletions absl/flags/internal/flag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ bool ShouldValidateFlagValue(FlagOpFn flag_type_id) {
return true;
}

#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
bool MatchRuntimeTypeId(FlagOpFn lhs_type_id, FlagOpFn rhs_type_id) {
return RuntimeTypeId(lhs_type_id) == RuntimeTypeId(rhs_type_id);
}
#else
bool MatchRuntimeTypeId(FlagOpFn, FlagOpFn) { return true; }
#endif

// RAII helper used to temporarily unlock and relock `absl::Mutex`.
// This is used when we need to ensure that locks are released while
// invoking user supplied callbacks and then reacquired, since callbacks may
Expand Down Expand Up @@ -133,6 +141,18 @@ void FlagImpl::Destroy() {
is_data_guard_inited_ = false;
}

void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const {
// `op` is the unmarshaling operation corresponding to the declaration
// visibile at the call site. `op_` is the Flag's defined unmarshalling
// operation. They must match for this operation to be well-defined.
if (ABSL_PREDICT_FALSE(op != op_) && !MatchRuntimeTypeId(op, op_)) {
ABSL_INTERNAL_LOG(
FATAL,
absl::StrCat("Flag '", Name(),
"' is defined as one type and declared as another"));
}
}

std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
void* res = nullptr;
if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
Expand Down Expand Up @@ -219,7 +239,7 @@ bool FlagImpl::RestoreState(const void* value, bool modified,
if (counter_ == counter) return false;
}

Write(value, op_);
Write(value);

{
absl::MutexLock l(DataGuard());
Expand Down Expand Up @@ -254,18 +274,9 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value,
return true;
}

void FlagImpl::Read(void* dst, const flags_internal::FlagOpFn dst_op) const {
void FlagImpl::Read(void* dst) const {
absl::ReaderMutexLock l(DataGuard());

// `dst_op` is the unmarshaling operation corresponding to the declaration
// visibile at the call site. `op` is the Flag's defined unmarshalling
// operation. They must match for this operation to be well-defined.
if (ABSL_PREDICT_FALSE(dst_op != op_)) {
ABSL_INTERNAL_LOG(
ERROR,
absl::StrCat("Flag '", Name(),
"' is defined as one type and declared as another"));
}
CopyConstruct(op_, value_.dynamic, dst);
}

Expand All @@ -286,19 +297,9 @@ void FlagImpl::StoreAtomic() {
#endif
}

void FlagImpl::Write(const void* src, const flags_internal::FlagOpFn src_op) {
void FlagImpl::Write(const void* src) {
absl::MutexLock l(DataGuard());

// `src_op` is the marshalling operation corresponding to the declaration
// visible at the call site. `op` is the Flag's defined marshalling operation.
// They must match for this operation to be well-defined.
if (ABSL_PREDICT_FALSE(src_op != op_)) {
ABSL_INTERNAL_LOG(
ERROR,
absl::StrCat("Flag '", Name(),
"' is defined as one type and declared as another"));
}

if (ShouldValidateFlagValue(op_)) {
void* obj = Clone(op_, src);
std::string ignored_error;
Expand Down
Loading

0 comments on commit bf78e97

Please sign in to comment.