• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_
18 #define SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_
19 
20 #include <stddef.h>
21 
22 #include <list>
23 #include <memory>
24 
25 #include <sys/mman.h>
26 
27 #include "perfetto/ext/base/paged_memory.h"
28 #include "perfetto/ext/base/utils.h"
29 #include "perfetto/ext/ipc/basic_types.h"
30 
31 namespace perfetto {
32 
33 namespace protos {
34 namespace gen {
35 class IPCFrame;
36 }  // namespace gen
37 }  // namespace protos
38 
39 namespace ipc {
40 
41 using Frame = ::perfetto::protos::gen::IPCFrame;
42 
43 // Deserializes incoming frames, taking care of buffering and tokenization.
44 // Used by both host and client to decode incoming frames.
45 //
46 // Which problem does it solve?
47 // ----------------------------
48 // The wire protocol is as follows:
49 // [32-bit frame size][proto-encoded Frame], e.g:
50 // [06 00 00 00][00 11 22 33 44 55 66]
51 // [02 00 00 00][AA BB]
52 // [04 00 00 00][CC DD EE FF]
53 // However, given that the socket works in SOCK_STREAM mode, the recv() calls
54 // might see the following:
55 // 06 00 00
56 // 00 00 11 22 33 44 55
57 // 66 02 00 00 00 ...
58 // This class takes care of buffering efficiently the data received, without
59 // making any assumption on how the incoming data will be chunked by the socket.
60 // For instance, it is possible that a recv() doesn't produce any frame (because
61 // it received only a part of the frame) or produces more than one frame.
62 //
63 // Usage
64 // -----
65 // Both host and client use this as follows:
66 //
67 // auto buf = rpc_frame_decoder.BeginReceive();
68 // size_t rsize = socket.recv(buf.first, buf.second);
69 // rpc_frame_decoder.EndReceive(rsize);
70 // while (Frame frame = rpc_frame_decoder.PopNextFrame()) {
71 //   ... process |frame|
72 // }
73 //
74 // Design goals:
75 // -------------
76 // - Optimize for the realistic case of each recv() receiving one or more
77 //   whole frames. In this case no memmove is performed.
78 // - Guarantee that frames lay in a virtually contiguous memory area.
79 //   This allows to use the protobuf-lite deserialization API (scattered
80 //   deserialization is supported only by libprotobuf-full).
81 // - Put a hard boundary to the size of the incoming buffer. This is to prevent
82 //   that a malicious sends an abnormally large frame and OOMs us.
83 // - Simplicity: just use a linear mmap region. No reallocations or scattering.
84 //   Takes care of madvise()-ing unused memory.
85 
86 class BufferedFrameDeserializer {
87  public:
88   struct ReceiveBuffer {
89     char* data;
90     size_t size;
91   };
92 
93   // |max_capacity| is overridable only for tests.
94   explicit BufferedFrameDeserializer(size_t max_capacity = kIPCBufferSize);
95   ~BufferedFrameDeserializer();
96 
97   // This function doesn't really belong here as it does Serialization, unlike
98   // the rest of this class. However it is so small and has so many dependencies
99   // in common that doesn't justify having its own class.
100   static std::string Serialize(const Frame&);
101 
102   // Returns a buffer that can be passed to recv(). The buffer is deliberately
103   // not initialized.
104   ReceiveBuffer BeginReceive();
105 
106   // Must be called soon after BeginReceive().
107   // |recv_size| is the number of valid bytes that have been written into the
108   // buffer previously returned by BeginReceive() (the return value of recv()).
109   // Returns false if a header > |max_capacity| is received, in which case the
110   // caller is expected to shutdown the socket and terminate the ipc.
111   bool EndReceive(size_t recv_size) PERFETTO_WARN_UNUSED_RESULT;
112 
113   // Decodes and returns the next decoded frame in the buffer if any, nullptr
114   // if no further frames have been decoded.
115   std::unique_ptr<Frame> PopNextFrame();
116 
capacity()117   size_t capacity() const { return capacity_; }
size()118   size_t size() const { return size_; }
119 
120  private:
121   BufferedFrameDeserializer(const BufferedFrameDeserializer&) = delete;
122   BufferedFrameDeserializer& operator=(const BufferedFrameDeserializer&) =
123       delete;
124 
125   // If a valid frame is decoded it is added to |decoded_frames_|.
126   void DecodeFrame(const char*, size_t);
127 
buf()128   char* buf() { return reinterpret_cast<char*>(buf_.Get()); }
129 
130   base::PagedMemory buf_;
131   const size_t capacity_ = 0;  // sizeof(|buf_|).
132 
133   // THe number of bytes in |buf_| that contain valid data (as a result of
134   // EndReceive()). This is always <= |capacity_|.
135   size_t size_ = 0;
136 
137   std::list<std::unique_ptr<Frame>> decoded_frames_;
138 };
139 
140 }  // namespace ipc
141 }  // namespace perfetto
142 
143 #endif  // SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_
144