Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): fewer crashes with dynamic loading #7512

Merged

Conversation

coryan
Copy link
Contributor

@coryan coryan commented Oct 23, 2021

absl::any does not support dynamically loaded libraries: it supports
.so libraries, it does not support libraries loaded with dlopen() or
a similar call. For our needs, it is easy to implement type-erasure in
google::cloud::Options.

Fixes #7505


This change is Reviewable

`absl::any` does not support dynamically loaded libraries: it supports
`.so` libraries, it does not support libraries loaded with `dlopen()` or
a similar call. For our needs, it is easy to implement type-erasure in
`google::cloud::Options`.
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 23, 2021
@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: ca7eed52c2e832203948350d6213fac93c35e93c

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@codecov
Copy link

codecov bot commented Oct 23, 2021

Codecov Report

Merging #7512 (ca7eed5) into main (10c28c1) will decrease coverage by 0.00%.
The diff coverage is 95.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7512      +/-   ##
==========================================
- Coverage   93.66%   93.66%   -0.01%     
==========================================
  Files        1361     1361              
  Lines      118086   118101      +15     
==========================================
+ Hits       110606   110619      +13     
- Misses       7480     7482       +2     
Impacted Files Coverage Δ
google/cloud/options.h 97.43% <95.65%> (-2.57%) ⬇️
google/cloud/grpc_error_delegate.cc 95.83% <0.00%> (-4.17%) ⬇️
...le/cloud/internal/default_completion_queue_impl.cc 97.00% <0.00%> (-0.60%) ⬇️
...cloud/pubsub/internal/subscription_session_test.cc 97.75% <0.00%> (-0.25%) ⬇️
.../cloud/storage/benchmarks/throughput_experiment.cc 74.87% <0.00%> (+0.50%) ⬆️
...bigtable/examples/bigtable_hello_instance_admin.cc 83.51% <0.00%> (+2.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10c28c1...ca7eed5. Read the comment docs.

@coryan coryan marked this pull request as ready for review October 23, 2021 15:41
@coryan coryan requested a review from a team as a code owner October 23, 2021 15:41
Copy link
Contributor

@devjgm devjgm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

p = m_.emplace(typeid(T), absl::make_unique<Data<T>>(std::move(value)))
.first;
}
auto* v = p->second->data_address();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider whether you'd like to factor these two lines (w/ the reinterpret_cast) into a private helper function template in this class, maybe like:

template <typename T, typename Holder>
T& GetValueReference(Holder&& h) {
  auto* v = std::forward<Holder>(h)->data_address();
  return *reinterpret_cast<T*>(v);
}

IFF that works (it's untested, so maybe doesn't even compile), would that let us isolate the slightly subtle reinterpret cast stuff into a single place. Your call about whether you even think this is an improvement. I'm happy w/ whatever you decide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I am going to pass. One is const-qualified, the other is not. I am sure we could make that work, not sure it is worth the trouble.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing SGTM. But just to clarify the suggestion, the use of std::forward would avoid the const-qualification issues. The following works:

diff --git a/google/cloud/options.h b/google/cloud/options.h
index a9d4856f2..0f06fe9a2 100644
--- a/google/cloud/options.h
+++ b/google/cloud/options.h
@@ -165,8 +165,7 @@ class Options {
     static auto const* const kDefaultValue = new ValueTypeT<T>{};
     auto const it = m_.find(typeid(T));
     if (it == m_.end()) return *kDefaultValue;
-    auto const* value = it->second->data_address();
-    return *reinterpret_cast<ValueTypeT<T> const*>(value);
+    return GetValueReference<ValueTypeT<T> const>(*it->second);
   }

   /**
@@ -196,8 +195,7 @@ class Options {
       p = m_.emplace(typeid(T), absl::make_unique<Data<T>>(std::move(value)))
               .first;
     }
-    auto* v = p->second->data_address();
-    return *reinterpret_cast<ValueTypeT<T>*>(v);
+    return GetValueReference<ValueTypeT<T>>(*p->second);
   }

  private:
@@ -214,6 +212,11 @@ class Options {
     virtual std::unique_ptr<DataHolder> clone() const = 0;
   };

+  template <typename T, typename Holder>
+  static T& GetValueReference(Holder&& h) {
+    return *reinterpret_cast<T*>(std::forward<Holder>(h).data_address());
+  }
+
   // The data holder for all the option values.
   template <typename T>
   class Data : public DataHolder {

I don't actually care about this change; just wanted to see if it worked like I was thinking.

@coryan coryan merged commit a716db5 into googleapis:main Oct 25, 2021
@coryan coryan deleted the fix-common-crashes-with-dynamic-loading branch October 25, 2021 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClientOptions.download_buffer_size SegFault
3 participants