Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Unified Diff: mojo/public/cpp/bindings/lib/native_serialization.h

Issue 1655333002: Add message sizing to basic IPC traits and struct macros. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@with-pickles
Patch Set: include ArrayHeader in size (oops!) and ensure the buffer doesn't relocate Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ipc/param_traits_size_macros.h ('k') | mojo/public/cpp/bindings/tests/pickled_struct_blink.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/native_serialization.h
diff --git a/mojo/public/cpp/bindings/lib/native_serialization.h b/mojo/public/cpp/bindings/lib/native_serialization.h
index 3c2e54efac58d2416d45b6ca3934afd582f94cdd..d2f9088f7781165eb919eadc383760c680cb66f5 100644
--- a/mojo/public/cpp/bindings/lib/native_serialization.h
+++ b/mojo/public/cpp/bindings/lib/native_serialization.h
@@ -29,7 +29,9 @@ struct ShouldUseNativeSerializer { static const bool value = false; };
template <typename T>
size_t GetSerializedSizeNative_(const T& value) {
- return IPC::ParamTraits<T>::GetSize(value);
+ base::PickleSizer sizer;
+ IPC::ParamTraits<T>::GetSize(&sizer, value);
+ return sizer.payload_size() + sizeof(ArrayHeader);
}
template <typename T>
@@ -46,8 +48,24 @@ void SerializeNative_(const T& value,
base::Pickle* pickle = pickler->pickle();
const char* data_start = pickle->end_of_payload();
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+ const char* payload_base = pickle->payload();
+ size_t size_before_write = pickle->payload_size();
+#endif
+
IPC::ParamTraits<T>::Write(pickle, value);
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+ // Ensure the pickle buffer hasn't moved.
+ DCHECK_EQ(payload_base, pickle->payload());
+ // Explicitly validate that the value returned by GetSize() always equals the
+ // number of bytes actually written by Write().
+ DCHECK_GE(pickle->payload_size(), size_before_write);
+ size_t bytes_written = pickle->payload_size() - size_before_write;
+ DCHECK_EQ(bytes_written + sizeof(ArrayHeader),
+ GetSerializedSizeNative_(value));
+#endif
+
// Fix up the ArrayHeader so that num_elements contains the length of the
// pickled data.
size_t pickled_size = pickle->end_of_payload() - data_start;
« no previous file with comments | « ipc/param_traits_size_macros.h ('k') | mojo/public/cpp/bindings/tests/pickled_struct_blink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698