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

Side by Side 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: validate in mojo serializer 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 11 matching lines...) Expand all
22 namespace internal { 22 namespace internal {
23 23
24 // Generated bindings for native-only types will specialize this to |true|. 24 // Generated bindings for native-only types will specialize this to |true|.
25 // It can be used as a signal (by e.g. the Array serializer) for when to use 25 // It can be used as a signal (by e.g. the Array serializer) for when to use
26 // SerializeNative_ with a type. 26 // SerializeNative_ with a type.
27 template <typename E> 27 template <typename E>
28 struct ShouldUseNativeSerializer { static const bool value = false; }; 28 struct ShouldUseNativeSerializer { static const bool value = false; };
29 29
30 template <typename T> 30 template <typename T>
31 size_t GetSerializedSizeNative_(const T& value) { 31 size_t GetSerializedSizeNative_(const T& value) {
32 return IPC::ParamTraits<T>::GetSize(value); 32 base::PickleSizer sizer;
33 IPC::ParamTraits<T>::GetSize(&sizer, value);
34 return sizer.payload_size();
33 } 35 }
34 36
35 template <typename T> 37 template <typename T>
36 void SerializeNative_(const T& value, 38 void SerializeNative_(const T& value,
37 Buffer* buffer, 39 Buffer* buffer,
38 Array_Data<uint8_t>** out) { 40 Array_Data<uint8_t>** out) {
39 PickleBuffer* pickler = buffer->AsPickleBuffer(); 41 PickleBuffer* pickler = buffer->AsPickleBuffer();
40 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; 42 DCHECK(pickler) << "Native types can only be used with PickleBuffers.";
41 43
42 ArrayHeader* header = 44 ArrayHeader* header =
43 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); 45 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader)));
44 46
45 // Remember where the Pickle started before writing. 47 // Remember where the Pickle started before writing.
46 base::Pickle* pickle = pickler->pickle(); 48 base::Pickle* pickle = pickler->pickle();
47 const char* data_start = pickle->end_of_payload(); 49 const char* data_start = pickle->end_of_payload();
48 50
51 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
52 size_t size_before_write = pickle->payload_size();
53 #endif
54
49 IPC::ParamTraits<T>::Write(pickle, value); 55 IPC::ParamTraits<T>::Write(pickle, value);
50 56
57 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
58 DCHECK_GE(pickle->payload_size(), size_before_write);
jam 2016/02/04 18:34:01 this is checking that the payload size didn't shri
59 // Explicitly validate that the value returned by GetSize() always equals the
60 // number of bytes actually written by Write().
61 size_t bytes_written = pickle->payload_size() - size_before_write;
62 DCHECK_EQ(bytes_written, GetSerializedSizeNative_(value));
63 #endif
64
51 // Fix up the ArrayHeader so that num_elements contains the length of the 65 // Fix up the ArrayHeader so that num_elements contains the length of the
52 // pickled data. 66 // pickled data.
53 size_t pickled_size = pickle->end_of_payload() - data_start; 67 size_t pickled_size = pickle->end_of_payload() - data_start;
54 size_t total_size = pickled_size + sizeof(ArrayHeader); 68 size_t total_size = pickled_size + sizeof(ArrayHeader);
55 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); 69 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max());
56 header->num_bytes = static_cast<uint32_t>(total_size); 70 header->num_bytes = static_cast<uint32_t>(total_size);
57 header->num_elements = static_cast<uint32_t>(pickled_size); 71 header->num_elements = static_cast<uint32_t>(pickled_size);
58 72
59 *out = reinterpret_cast<Array_Data<uint8_t>*>(header); 73 *out = reinterpret_cast<Array_Data<uint8_t>*>(header);
60 } 74 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 // Return the header to its original state. 110 // Return the header to its original state.
97 header->num_bytes += sizeof(ArrayHeader); 111 header->num_bytes += sizeof(ArrayHeader);
98 112
99 return true; 113 return true;
100 } 114 }
101 115
102 } // namespace internal 116 } // namespace internal
103 } // namespace mojo 117 } // namespace mojo
104 118
105 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ 119 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_
OLDNEW
« 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