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

Side by Side Diff: mojo/public/cpp/bindings/lib/bindings_serialization.h

Issue 293983026: Mojo cpp bindings: remove redundant validation in Decode*(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_BINDINGS_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "mojo/public/cpp/bindings/message.h" 10 #include "mojo/public/cpp/system/core.h"
11 11
12 namespace mojo { 12 namespace mojo {
13 namespace internal { 13 namespace internal {
14 14
15 class BoundsChecker; 15 class BoundsChecker;
16 16
17 // Please note that this is a different value than |mojo::kInvalidHandleValue|, 17 // Please note that this is a different value than |mojo::kInvalidHandleValue|,
18 // which is the "decoded" invalid handle. 18 // which is the "decoded" invalid handle.
19 const MojoHandle kEncodedInvalidHandleValue = static_cast<MojoHandle>(-1); 19 const MojoHandle kEncodedInvalidHandleValue = static_cast<MojoHandle>(-1);
20 20
21 size_t Align(size_t size); 21 size_t Align(size_t size);
22 char* AlignPointer(char* ptr); 22 char* AlignPointer(char* ptr);
23 23
24 bool IsAligned(const void* ptr); 24 bool IsAligned(const void* ptr);
25 25
26 // Pointers are encoded as relative offsets. The offsets are relative to the 26 // Pointers are encoded as relative offsets. The offsets are relative to the
27 // address of where the offset value is stored, such that the pointer may be 27 // address of where the offset value is stored, such that the pointer may be
28 // recovered with the expression: 28 // recovered with the expression:
29 // 29 //
30 // ptr = reinterpret_cast<char*>(offset) + *offset 30 // ptr = reinterpret_cast<char*>(offset) + *offset
31 // 31 //
32 // A null pointer is encoded as an offset value of 0. 32 // A null pointer is encoded as an offset value of 0.
33 // 33 //
34 void EncodePointer(const void* ptr, uint64_t* offset); 34 void EncodePointer(const void* ptr, uint64_t* offset);
35 // Note: This function doesn't validate the encoded pointer value.
35 const void* DecodePointerRaw(const uint64_t* offset); 36 const void* DecodePointerRaw(const uint64_t* offset);
36 37
38 // Note: This function doesn't validate the encoded pointer value.
37 template <typename T> 39 template <typename T>
38 inline void DecodePointer(const uint64_t* offset, T** ptr) { 40 inline void DecodePointer(const uint64_t* offset, T** ptr) {
39 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset))); 41 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset)));
40 } 42 }
41 43
42 // Checks whether decoding the pointer will overflow and produce a pointer 44 // Checks whether decoding the pointer will overflow and produce a pointer
43 // smaller than |offset|. 45 // smaller than |offset|.
44 bool ValidateEncodedPointer(const uint64_t* offset); 46 bool ValidateEncodedPointer(const uint64_t* offset);
45 47
46 // Check that the given pointer references memory contained within the message.
47 bool ValidatePointer(const void* ptr, const Message& message);
48
49 // Handles are encoded as indices into a vector of handles. These functions 48 // Handles are encoded as indices into a vector of handles. These functions
50 // manipulate the value of |handle|, mapping it to and from an index. 49 // manipulate the value of |handle|, mapping it to and from an index.
51 void EncodeHandle(Handle* handle, std::vector<Handle>* handles); 50 void EncodeHandle(Handle* handle, std::vector<Handle>* handles);
52 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles); 51 // Note: This function doesn't validate the encoded handle value.
52 void DecodeHandle(Handle* handle, std::vector<Handle>* handles);
53 53
54 // The following 2 functions are used to encode/decode all objects (structs and 54 // The following 2 functions are used to encode/decode all objects (structs and
55 // arrays) in a consistent manner. 55 // arrays) in a consistent manner.
56 56
57 template <typename T> 57 template <typename T>
58 inline void Encode(T* obj, std::vector<Handle>* handles) { 58 inline void Encode(T* obj, std::vector<Handle>* handles) {
59 if (obj->ptr) 59 if (obj->ptr)
60 obj->ptr->EncodePointersAndHandles(handles); 60 obj->ptr->EncodePointersAndHandles(handles);
61 EncodePointer(obj->ptr, &obj->offset); 61 EncodePointer(obj->ptr, &obj->offset);
62 } 62 }
63 63
64 // TODO(yzshen): Remove all redundant validation during decoding. And make 64 // Note: This function doesn't validate the encoded pointer and handle values.
65 // Decode*() functions/methods return void.
66 template <typename T> 65 template <typename T>
67 inline bool Decode(T* obj, Message* message) { 66 inline void Decode(T* obj, std::vector<Handle>* handles) {
68 DecodePointer(&obj->offset, &obj->ptr); 67 DecodePointer(&obj->offset, &obj->ptr);
69 if (obj->ptr) { 68 if (obj->ptr)
70 if (!ValidatePointer(obj->ptr, *message)) 69 obj->ptr->DecodePointersAndHandles(handles);
71 return false;
72 if (!obj->ptr->DecodePointersAndHandles(message))
73 return false;
74 }
75 return true;
76 } 70 }
77 71
78 // If returns true, this function also claims the memory range of the size 72 // If returns true, this function also claims the memory range of the size
79 // specified in the struct header, starting from |data|. 73 // specified in the struct header, starting from |data|.
80 bool ValidateStructHeader(const void* data, 74 bool ValidateStructHeader(const void* data,
81 uint32_t min_num_bytes, 75 uint32_t min_num_bytes,
82 uint32_t min_num_fields, 76 uint32_t min_num_fields,
83 BoundsChecker* bounds_checker); 77 BoundsChecker* bounds_checker);
84 78
85 } // namespace internal 79 } // namespace internal
86 } // namespace mojo 80 } // namespace mojo
87 81
88 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 82 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/array_internal.cc ('k') | mojo/public/cpp/bindings/lib/bindings_serialization.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698