• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# DO NOT EDIT! This file is generated by generate_fuchsia_patch.py.
16#
17# To make changes, update and run ./generate_fuchsia_patch.py.
18
19# Patch the fit::function implementation for use in Pigweed:
20#
21#   - Use PW_ASSERT instead of __builtin_abort.
22#   - Temporarily disable sanitizers when invoking a function for b/241567321.
23#
24diff --git a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/internal/function.h b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/internal/function.h
25--- a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/internal/function.h
26+++ b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/internal/function.h
27@@ -18,6 +18,8 @@
28 #include <utility>
29
30 #include "../nullable.h"
31+#include "pw_assert/assert.h"
32+#include "pw_preprocessor/compiler.h"
33
34 namespace fit {
35 namespace internal {
36@@ -88,7 +90,7 @@ inline const void* unshared_target_type_id(void* /*bits*/, const void* impl_ops)
37 // elsewhere in the header as an inline variable.
38 template <typename Unused = void>
39 struct null_target {
40-  static void invoke(void* /*bits*/) { __builtin_abort(); }
41+  static void invoke(void* /*bits*/) { PW_ASSERT(false); }
42
43   static const target_ops<void> ops;
44
45@@ -507,7 +509,8 @@ class function_base<inline_target_size, require_inline, Result(Args...), Allocat
46   // Note that fit::callback will release the target immediately after
47   // invoke() (also affecting any share()d copies).
48   // Aborts if the function's target is empty.
49-  Result invoke(Args... args) const {
50+  // TODO: b/241567321 - Remove "no sanitize" after pw_protobuf is fixed.
51+  Result invoke(Args... args) const PW_NO_SANITIZE("function") {
52     // Down cast the ops to the derived type that this function was instantiated
53     // with, which includes the invoke function.
54     //
55@@ -537,7 +540,7 @@ class function_base<inline_target_size, require_inline, Result(Args...), Allocat
56   template <typename SharedFunction>
57   void copy_shared_target_to(SharedFunction& copy) {
58     copy.destroy_target();
59-    assert(base::ops() == &shared_target_type<SharedFunction>::ops);
60+    PW_ASSERT(base::ops() == &shared_target_type<SharedFunction>::ops);
61     shared_target_type<SharedFunction>::copy_shared_ptr(base::bits(), copy.bits());
62     copy.set_ops(base::ops());
63   }
64@@ -567,7 +570,7 @@ class function_base<inline_target_size, require_inline, Result(Args...), Allocat
65   void check_target_type() const {
66     if (target_type<Callable>::ops.target_type_id(nullptr, &target_type<Callable>::ops) !=
67         base::target_type_id()) {
68-      __builtin_abort();
69+      PW_ASSERT(false);
70     }
71   }
72 };
73diff --git a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/nullable.h b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/nullable.h
74--- a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/nullable.h
75+++ b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/nullable.h
76@@ -11,6 +11,8 @@
77 #include <type_traits>
78 #include <utility>
79
80+#include "pw_assert/assert.h"
81+
82 namespace fit {
83
84 // Determines whether a type can be compared with nullptr.
85@@ -130,25 +132,25 @@ class nullable<T, true> final {
86     if (has_value()) {
87       return value_;
88     }
89-    __builtin_abort();
90+    PW_ASSERT(false);
91   }
92   constexpr const T& value() const& {
93     if (has_value()) {
94       return value_;
95     }
96-    __builtin_abort();
97+    PW_ASSERT(false);
98   }
99   constexpr T&& value() && {
100     if (has_value()) {
101       return std::move(value_);
102     }
103-    __builtin_abort();
104+    PW_ASSERT(false);
105   }
106   constexpr const T&& value() const&& {
107     if (has_value()) {
108       return std::move(value_);
109     }
110-    __builtin_abort();
111+    PW_ASSERT(false);
112   }
113
114   template <typename U = T>
115diff --git a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/result.h b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/result.h
116--- a/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/result.h
117+++ b/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/result.h
118@@ -55,6 +55,8 @@
119 //                               // fit::result with a different "success" vluae type (or
120 //                               // fit::result<E>).
121
122+#include "pw_assert/assert.h"
123+
124 namespace fit {
125
126 // Convenience type to indicate failure without elaboration.
127@@ -286,25 +288,25 @@ class [[nodiscard]] result<E, T> {
128     if (is_error()) {
129       return storage_.error_or_value.error;
130     }
131-    __builtin_abort();
132+    PW_ASSERT(false);
133   }
134   constexpr const E& error_value() const& {
135     if (is_error()) {
136       return storage_.error_or_value.error;
137     }
138-    __builtin_abort();
139+    PW_ASSERT(false);
140   }
141   constexpr E&& error_value() && {
142     if (is_error()) {
143       return std::move(storage_.error_or_value.error);
144     }
145-    __builtin_abort();
146+    PW_ASSERT(false);
147   }
148   constexpr const E&& error_value() const&& {
149     if (is_error()) {
150       return std::move(storage_.error_or_value.error);
151     }
152-    __builtin_abort();
153+    PW_ASSERT(false);
154   }
155
156   // Moves the underlying error and returns it as an instance of fit::error, simplifying
157@@ -315,7 +317,7 @@ class [[nodiscard]] result<E, T> {
158     if (is_error()) {
159       return error<E>(std::move(storage_.error_or_value.error));
160     }
161-    __builtin_abort();
162+    PW_ASSERT(false);
163   }
164
165   // Accessors for the underlying value.
166@@ -325,25 +327,25 @@ class [[nodiscard]] result<E, T> {
167     if (is_ok()) {
168       return storage_.error_or_value.value;
169     }
170-    __builtin_abort();
171+    PW_ASSERT(false);
172   }
173   constexpr const T& value() const& {
174     if (is_ok()) {
175       return storage_.error_or_value.value;
176     }
177-    __builtin_abort();
178+    PW_ASSERT(false);
179   }
180   constexpr T&& value() && {
181     if (is_ok()) {
182       return std::move(storage_.error_or_value.value);
183     }
184-    __builtin_abort();
185+    PW_ASSERT(false);
186   }
187   constexpr const T&& value() const&& {
188     if (is_ok()) {
189       return std::move(storage_.error_or_value.value);
190     }
191-    __builtin_abort();
192+    PW_ASSERT(false);
193   }
194
195   // Moves the underlying value and returns it as an instance of fit::success, simplifying
196@@ -354,7 +356,7 @@ class [[nodiscard]] result<E, T> {
197     if (is_ok()) {
198       return success<T>(std::move(storage_.error_or_value.value));
199     }
200-    __builtin_abort();
201+    PW_ASSERT(false);
202   }
203
204   // Contingent accessors for the underlying value.
205@@ -383,13 +385,13 @@ class [[nodiscard]] result<E, T> {
206     if (is_ok()) {
207       return ::fit::internal::arrow_operator<T>::forward(storage_.error_or_value.value);
208     }
209-    __builtin_abort();
210+    PW_ASSERT(false);
211   }
212   constexpr decltype(auto) operator->() const {
213     if (is_ok()) {
214       return ::fit::internal::arrow_operator<T>::forward(storage_.error_or_value.value);
215     }
216-    __builtin_abort();
217+    PW_ASSERT(false);
218   }
219
220   // Accessors for the underlying value. This is a syntax sugar for value().
221@@ -412,7 +414,7 @@ class [[nodiscard]] result<E, T> {
222       storage_.error_or_value.error += std::move(error.value_);
223       return *this;
224     }
225-    __builtin_abort();
226+    PW_ASSERT(false);
227   }
228
229   // Maps a result<E, T> to a result<E2, T> by transforming the error through
230@@ -523,25 +525,25 @@ class [[nodiscard]] result<E> {
231     if (is_error()) {
232       return storage_.error_or_value.error;
233     }
234-    __builtin_abort();
235+    PW_ASSERT(false);
236   }
237   constexpr const E& error_value() const& {
238     if (is_error()) {
239       return storage_.error_or_value.error;
240     }
241-    __builtin_abort();
242+    PW_ASSERT(false);
243   }
244   constexpr E&& error_value() && {
245     if (is_error()) {
246       return std::move(storage_.error_or_value.error);
247     }
248-    __builtin_abort();
249+    PW_ASSERT(false);
250   }
251   constexpr const E&& error_value() const&& {
252     if (is_error()) {
253       return std::move(storage_.error_or_value.error);
254     }
255-    __builtin_abort();
256+    PW_ASSERT(false);
257   }
258
259   // Moves the underlying error and returns it as an instance of fit::error, simplifying
260@@ -552,7 +554,7 @@ class [[nodiscard]] result<E> {
261     if (is_error()) {
262       return error<E>(std::move(storage_.error_or_value.error));
263     }
264-    __builtin_abort();
265+    PW_ASSERT(false);
266   }
267
268   // Augments the error value of the result with the given value. The operator E::operator+=(F) must
269@@ -566,7 +568,7 @@ class [[nodiscard]] result<E> {
270       storage_.error_or_value.error += std::move(error.value_);
271       return *this;
272     }
273-    __builtin_abort();
274+    PW_ASSERT(false);
275   }
276
277   // Maps a result<E, T> to a result<E2, T> by transforming the error through
278diff --git a/third_party/fuchsia/repo/sdk/lib/fit/test/function_tests.cc b/third_party/fuchsia/repo/sdk/lib/fit/test/function_tests.cc
279--- a/third_party/fuchsia/repo/sdk/lib/fit/test/function_tests.cc
280+++ b/third_party/fuchsia/repo/sdk/lib/fit/test/function_tests.cc
281@@ -4,7 +4,6 @@
282
283 #include <lib/fit/function.h>
284 #include <lib/stdcompat/bit.h>
285-#include <zircon/compiler.h>
286
287 #include <algorithm>
288 #include <array>
289@@ -15,6 +14,8 @@
290
291 #include <zxtest/zxtest.h>
292
293+#include "pw_polyfill/language_feature_macros.h"
294+
295 namespace {
296
297 using ::std::size_t;
298@@ -1029,8 +1030,8 @@ TEST(FunctionTests, callback_with_custom_allocator) {
299   EXPECT_EQ(1, cbheapdestroy);
300 }
301
302-__CONSTINIT const fit::function<void()> kDefaultConstructed;
303-__CONSTINIT const fit::function<void()> kNullptrConstructed(nullptr);
304+PW_CONSTINIT const fit::function<void()> kDefaultConstructed;
305+PW_CONSTINIT const fit::function<void()> kNullptrConstructed(nullptr);
306
307 TEST(FunctionTests, null_constructors_are_constexpr) {
308   EXPECT_EQ(kDefaultConstructed, nullptr);
309