• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "src/profiling/memory/wire_protocol.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/base/unix_socket.h"
21 #include "perfetto/base/utils.h"
22 
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 
26 namespace perfetto {
27 namespace profiling {
28 
29 namespace {
30 template <typename T>
ViewAndAdvance(char ** ptr,T ** out,const char * end)31 bool ViewAndAdvance(char** ptr, T** out, const char* end) {
32   if (end - sizeof(T) < *ptr)
33     return false;
34   *out = reinterpret_cast<T*>(*ptr);
35   *ptr += sizeof(T);
36   return true;
37 }
38 
39 // We need this to prevent crashes due to FORTIFY_SOURCE.
UnsafeMemcpy(char * dest,const char * src,size_t n)40 void UnsafeMemcpy(char* dest, const char* src, size_t n)
41     __attribute__((no_sanitize("address", "hwaddress"))) {
42   for (size_t i = 0; i < n; ++i) {
43     dest[i] = src[i];
44   }
45 }
46 }  // namespace
47 
SendWireMessage(SharedRingBuffer * shmem,const WireMessage & msg)48 bool SendWireMessage(SharedRingBuffer* shmem, const WireMessage& msg) {
49   uint64_t total_size;
50   struct iovec iovecs[3] = {};
51   // TODO(fmayer): Maye pack these two.
52   iovecs[0].iov_base = const_cast<RecordType*>(&msg.record_type);
53   iovecs[0].iov_len = sizeof(msg.record_type);
54   if (msg.alloc_header) {
55     PERFETTO_DCHECK(msg.record_type == RecordType::Malloc);
56     iovecs[1].iov_base = msg.alloc_header;
57     iovecs[1].iov_len = sizeof(*msg.alloc_header);
58   } else if (msg.free_header) {
59     PERFETTO_DCHECK(msg.record_type == RecordType::Free);
60     iovecs[1].iov_base = msg.free_header;
61     iovecs[1].iov_len = sizeof(*msg.free_header);
62   } else {
63     PERFETTO_DFATAL("Neither alloc_header nor free_header set.");
64     errno = EINVAL;
65     return false;
66   }
67 
68   iovecs[2].iov_base = msg.payload;
69   iovecs[2].iov_len = msg.payload_size;
70 
71   struct msghdr hdr = {};
72   hdr.msg_iov = iovecs;
73   if (msg.payload) {
74     hdr.msg_iovlen = base::ArraySize(iovecs);
75     total_size = iovecs[0].iov_len + iovecs[1].iov_len + iovecs[2].iov_len;
76   } else {
77     // If we are not sending payload, just ignore that iovec.
78     hdr.msg_iovlen = base::ArraySize(iovecs) - 1;
79     total_size = iovecs[0].iov_len + iovecs[1].iov_len;
80   }
81 
82   SharedRingBuffer::Buffer buf;
83   {
84     ScopedSpinlock lock = shmem->AcquireLock(ScopedSpinlock::Mode::Try);
85     if (!lock.locked()) {
86       PERFETTO_DLOG("Failed to acquire spinlock.");
87       errno = EAGAIN;
88       return false;
89     }
90     buf = shmem->BeginWrite(lock, total_size);
91   }
92   if (!buf) {
93     PERFETTO_DFATAL("Buffer overflow.");
94     shmem->EndWrite(std::move(buf));
95     errno = EAGAIN;
96     return false;
97   }
98 
99   size_t offset = 0;
100   for (size_t i = 0; i < hdr.msg_iovlen; ++i) {
101     UnsafeMemcpy(reinterpret_cast<char*>(buf.data + offset),
102                  reinterpret_cast<const char*>(hdr.msg_iov[i].iov_base),
103                  hdr.msg_iov[i].iov_len);
104     offset += hdr.msg_iov[i].iov_len;
105   }
106   shmem->EndWrite(std::move(buf));
107   return true;
108 }
109 
ReceiveWireMessage(char * buf,size_t size,WireMessage * out)110 bool ReceiveWireMessage(char* buf, size_t size, WireMessage* out) {
111   RecordType* record_type;
112   char* end = buf + size;
113   if (!ViewAndAdvance<RecordType>(&buf, &record_type, end)) {
114     PERFETTO_DFATAL("Cannot read record type.");
115     return false;
116   }
117 
118   out->payload = nullptr;
119   out->payload_size = 0;
120   out->record_type = *record_type;
121 
122   if (*record_type == RecordType::Malloc) {
123     if (!ViewAndAdvance<AllocMetadata>(&buf, &out->alloc_header, end)) {
124       PERFETTO_DFATAL("Cannot read alloc header.");
125       return false;
126     }
127     out->payload = buf;
128     if (buf > end) {
129       PERFETTO_DFATAL("Buffer overflowed");
130       return false;
131     }
132     out->payload_size = static_cast<size_t>(end - buf);
133   } else if (*record_type == RecordType::Free) {
134     if (!ViewAndAdvance<FreeBatch>(&buf, &out->free_header, end)) {
135       PERFETTO_DFATAL("Cannot read free header.");
136       return false;
137     }
138   } else {
139     PERFETTO_DFATAL("Invalid record type.");
140     return false;
141   }
142   return true;
143 }
144 
145 }  // namespace profiling
146 }  // namespace perfetto
147