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

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

Issue 289333002: Mojo cpp bindings: validation logic for incoming messages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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/buffer.h" 10 #include "mojo/public/cpp/bindings/buffer.h"
11 #include "mojo/public/cpp/bindings/message.h" 11 #include "mojo/public/cpp/bindings/message.h"
12 12
13 namespace mojo { 13 namespace mojo {
14 namespace internal { 14 namespace internal {
15 15
16 class BoundsChecker;
17
16 size_t Align(size_t size); 18 size_t Align(size_t size);
17 char* AlignPointer(char* ptr); 19 char* AlignPointer(char* ptr);
18 20
21 bool IsAligned(const void* ptr);
Tom Sepez 2014/05/22 19:39:21 nit: is aligned to what? 4? 8? maybe a better name
yzshen1 2014/05/22 20:56:22 I agree that it will be more clear as you suggeste
22
19 // Pointers are encoded as relative offsets. The offsets are relative to the 23 // Pointers are encoded as relative offsets. The offsets are relative to the
20 // address of where the offset value is stored, such that the pointer may be 24 // address of where the offset value is stored, such that the pointer may be
21 // recovered with the expression: 25 // recovered with the expression:
22 // 26 //
23 // ptr = reinterpret_cast<char*>(offset) + *offset 27 // ptr = reinterpret_cast<char*>(offset) + *offset
24 // 28 //
25 // A null pointer is encoded as an offset value of 0. 29 // A null pointer is encoded as an offset value of 0.
26 // 30 //
27 void EncodePointer(const void* ptr, uint64_t* offset); 31 void EncodePointer(const void* ptr, uint64_t* offset);
28 const void* DecodePointerRaw(const uint64_t* offset); 32 const void* DecodePointerRaw(const uint64_t* offset);
29 33
30 template <typename T> 34 template <typename T>
31 inline void DecodePointer(const uint64_t* offset, T** ptr) { 35 inline void DecodePointer(const uint64_t* offset, T** ptr) {
32 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset))); 36 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset)));
33 } 37 }
34 38
39 // Checks whether decoding the pointer will overflow and produce a poniter
Tom Sepez 2014/05/22 19:39:21 nit: sp. poniter
yzshen1 2014/05/22 20:56:22 Done.
40 // smaller than |offset|.
41 bool ValidateEncodedPointer(const uint64_t* offset);
42
35 // Check that the given pointer references memory contained within the message. 43 // Check that the given pointer references memory contained within the message.
36 bool ValidatePointer(const void* ptr, const Message& message); 44 bool ValidatePointer(const void* ptr, const Message& message);
37 45
38 // Handles are encoded as indices into a vector of handles. These functions 46 // Handles are encoded as indices into a vector of handles. These functions
39 // manipulate the value of |handle|, mapping it to and from an index. 47 // manipulate the value of |handle|, mapping it to and from an index.
40 void EncodeHandle(Handle* handle, std::vector<Handle>* handles); 48 void EncodeHandle(Handle* handle, std::vector<Handle>* handles);
41 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles); 49 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles);
42 50
43 // The following 2 functions are used to encode/decode all objects (structs and 51 // The following 2 functions are used to encode/decode all objects (structs and
44 // arrays) in a consistent manner. 52 // arrays) in a consistent manner.
45 53
46 template <typename T> 54 template <typename T>
47 inline void Encode(T* obj, std::vector<Handle>* handles) { 55 inline void Encode(T* obj, std::vector<Handle>* handles) {
48 if (obj->ptr) 56 if (obj->ptr)
49 obj->ptr->EncodePointersAndHandles(handles); 57 obj->ptr->EncodePointersAndHandles(handles);
50 EncodePointer(obj->ptr, &obj->offset); 58 EncodePointer(obj->ptr, &obj->offset);
51 } 59 }
52 60
61 // TODO(yzshen): Remove all redundant validation during decoding. And make
62 // Decode*() functions/methods return void.
53 template <typename T> 63 template <typename T>
54 inline bool Decode(T* obj, Message* message) { 64 inline bool Decode(T* obj, Message* message) {
55 DecodePointer(&obj->offset, &obj->ptr); 65 DecodePointer(&obj->offset, &obj->ptr);
56 if (obj->ptr) { 66 if (obj->ptr) {
57 if (!ValidatePointer(obj->ptr, *message)) 67 if (!ValidatePointer(obj->ptr, *message))
58 return false; 68 return false;
59 if (!obj->ptr->DecodePointersAndHandles(message)) 69 if (!obj->ptr->DecodePointersAndHandles(message))
60 return false; 70 return false;
61 } 71 }
62 return true; 72 return true;
63 } 73 }
64 74
75 // If returns true, this function also claims the memory range of the size
76 // specified in the struct header, starting from |data|.
77 bool ValidateStructHeader(const void* data,
78 uint32_t min_num_bytes,
79 uint32_t min_num_fields,
80 BoundsChecker* bounds_checker);
81
65 } // namespace internal 82 } // namespace internal
66 } // namespace mojo 83 } // namespace mojo
67 84
68 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 85 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698