1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <stddef.h>
20 #include <sys/uio.h>
21 #include <cstdint>
22 #include <optional>
23 
24 #include <binder/Common.h>
25 #include <log/log.h>
26 #include <utils/Errors.h>
27 
28 #define PLOGE(fmt, ...)                                                     \
29     do {                                                                    \
30         auto savedErrno = errno;                                            \
31         ALOGE(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
32     } while (0)
33 #define PLOGF(fmt, ...)                                                                \
34     do {                                                                               \
35         auto savedErrno = errno;                                                       \
36         LOG_ALWAYS_FATAL(fmt ": %s" __VA_OPT__(, ) __VA_ARGS__, strerror(savedErrno)); \
37     } while (0)
38 
39 /* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */
40 #ifndef TEMP_FAILURE_RETRY
41 /* Used to retry syscalls that can return EINTR. */
42 #define TEMP_FAILURE_RETRY(exp)                \
43     ({                                         \
44         __typeof__(exp) _rc;                   \
45         do {                                   \
46             _rc = (exp);                       \
47         } while (_rc == -1 && errno == EINTR); \
48         _rc;                                   \
49     })
50 #endif
51 
52 #define TEST_AND_RETURN(value, expr)            \
53     do {                                        \
54         if (!(expr)) {                          \
55             ALOGE("Failed to call: %s", #expr); \
56             return value;                       \
57         }                                       \
58     } while (0)
59 
60 namespace android {
61 
62 /**
63  * Get the size of a statically initialized array.
64  *
65  * \param N the array to get the size of.
66  * \return the size of the array.
67  */
68 template <typename T, size_t N>
countof(T (&)[N])69 constexpr size_t countof(T (&)[N]) {
70     return N;
71 }
72 
73 // avoid optimizations
74 void zeroMemory(uint8_t* data, size_t size);
75 
76 // View of contiguous sequence. Similar to std::span.
77 template <typename T>
78 struct Span {
79     T* data = nullptr;
80     size_t size = 0;
81 
byteSizeSpan82     size_t byteSize() { return size * sizeof(T); }
83 
toIovecSpan84     iovec toIovec() { return {const_cast<std::remove_const_t<T>*>(data), byteSize()}; }
85 
86     // Truncates `this` to a length of `offset` and returns a span with the
87     // remainder.
88     //
89     // `std::nullopt` iff offset > size.
splitOffSpan90     std::optional<Span<T>> splitOff(size_t offset) {
91         if (offset > size) {
92             return std::nullopt;
93         }
94         Span<T> rest = {data + offset, size - offset};
95         size = offset;
96         return rest;
97     }
98 
99     // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
100     template <typename U>
reinterpretSpan101     std::optional<Span<U>> reinterpret() const {
102         // Only allow casting from bytes for simplicity.
103         static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
104         if (size % sizeof(U) != 0) {
105             return std::nullopt;
106         }
107         return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
108     }
109 };
110 
111 // Converts binary data into a hexString.
112 //
113 // Hex values are printed in order, e.g. 0xDEAD will result in 'adde' because
114 // Android is little-endian.
115 LIBBINDER_INTERNAL_EXPORTED std::string HexString(const void* bytes, size_t len);
116 
117 }   // namespace android
118