0006-Quit-using-ValueOrDie.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. diff --git a/BUILD.gn b/BUILD.gn
  2. index 2f929064ff924..55152038d3c19 100644
  3. --- a/BUILD.gn
  4. +++ b/BUILD.gn
  5. @@ -106,7 +106,6 @@ if (is_chromeos_ash) {
  6. "src/prng/chacha_prng_util.cc",
  7. "src/prng/single_thread_chacha_prng.cc",
  8. "src/relinearization_key.cc",
  9. - "src/statusor.cc",
  10. ]
  11. public_deps = [
  12. ":serialization_proto",
  13. @@ -163,7 +162,6 @@ if (is_chromeos_ash) {
  14. "src/relinearization_key_test.cc",
  15. "src/sample_error_test.cc",
  16. "src/status_macros_test.cc",
  17. - "src/statusor_test.cc",
  18. "src/symmetric_encryption_test.cc",
  19. "src/symmetric_encryption_with_prng_test.cc",
  20. "src/testing/coefficient_polynomial_ciphertext_test.cc",
  21. diff --git a/patches/0006-Quit-using-ValueOrDie.patch b/patches/0006-Quit-using-ValueOrDie.patch
  22. new file mode 100644
  23. index 0000000000000..e69de29bb2d1d
  24. diff --git a/src/montgomery_test.cc b/src/montgomery_test.cc
  25. index 2d952ddfbdad4..97444fc6c15c2 100644
  26. --- a/src/montgomery_test.cc
  27. +++ b/src/montgomery_test.cc
  28. @@ -768,7 +768,7 @@ TYPED_TEST(MontgomeryTest, BatchOperations) {
  29. std::vector<TypeParam> expected_add, expected_sub, expected_mul;
  30. TypeParam scalar =
  31. TypeParam::ImportRandom(prng.get(), modulus_params.get())
  32. - .ValueOrDie();
  33. + .value();
  34. auto scalar_constants_tuple = scalar.GetConstant(modulus_params.get());
  35. auto scalar_constant = std::get<0>(scalar_constants_tuple);
  36. auto scalar_constant_barrett = std::get<1>(scalar_constants_tuple);
  37. @@ -776,9 +776,9 @@ TYPED_TEST(MontgomeryTest, BatchOperations) {
  38. expected_mul_scalar;
  39. for (size_t i = 0; i < length; i++) {
  40. a.push_back(TypeParam::ImportRandom(prng.get(), modulus_params.get())
  41. - .ValueOrDie());
  42. + .value());
  43. b.push_back(TypeParam::ImportRandom(prng.get(), modulus_params.get())
  44. - .ValueOrDie());
  45. + .value());
  46. auto constants_tuple = b[i].GetConstant(modulus_params.get());
  47. auto constant = std::get<0>(constants_tuple);
  48. auto constant_barrett = std::get<1>(constants_tuple);
  49. diff --git a/src/polynomial_test.cc b/src/polynomial_test.cc
  50. index 6d5112e67c6b0..e246788d09bcc 100644
  51. --- a/src/polynomial_test.cc
  52. +++ b/src/polynomial_test.cc
  53. @@ -55,7 +55,7 @@ template <typename Prng>
  54. class PolynomialTest : public ::testing::Test {
  55. protected:
  56. PolynomialTest()
  57. - : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).ValueOrDie()),
  58. + : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).value()),
  59. zero_(uint_m::ImportZero(params14_.get())) {}
  60. void SetUp() override { srand(0); }
  61. @@ -98,7 +98,7 @@ class PolynomialTest : public ::testing::Test {
  62. }
  63. std::unique_ptr<Prng> MakePrng(absl::string_view seed) {
  64. - auto prng = Prng::Create(seed.substr(0, Prng::SeedLength())).ValueOrDie();
  65. + auto prng = Prng::Create(seed.substr(0, Prng::SeedLength())).value();
  66. return prng;
  67. }
  68. diff --git a/src/status_macros.h b/src/status_macros.h
  69. index d297bfe0ed682..2622f6e544b8e 100644
  70. --- a/src/status_macros.h
  71. +++ b/src/status_macros.h
  72. @@ -34,7 +34,7 @@
  73. if (ABSL_PREDICT_FALSE(!statusor.ok())) { \
  74. return std::move(statusor).status(); \
  75. } \
  76. - lhs = std::move(statusor).ValueOrDie()
  77. + lhs = std::move(statusor).value()
  78. // Internal helper for concatenating macro values.
  79. #define RLWE_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
  80. diff --git a/src/status_macros_test.cc b/src/status_macros_test.cc
  81. index d43b55c9bb800..1f96d59e5d3b7 100644
  82. --- a/src/status_macros_test.cc
  83. +++ b/src/status_macros_test.cc
  84. @@ -27,8 +27,8 @@ namespace {
  85. TEST(StatusMacrosTest, TestAssignOrReturn) {
  86. StatusOr<StatusOr<int>> a(StatusOr<int>(2));
  87. auto f = [&]() -> absl::Status {
  88. - RLWE_ASSIGN_OR_RETURN(StatusOr<int> status_or_a, a.ValueOrDie());
  89. - EXPECT_EQ(2, status_or_a.ValueOrDie());
  90. + RLWE_ASSIGN_OR_RETURN(StatusOr<int> status_or_a, a.value());
  91. + EXPECT_EQ(2, status_or_a.value());
  92. return absl::OkStatus();
  93. };
  94. auto status = f();
  95. diff --git a/src/statusor.h b/src/statusor.h
  96. index b7ada09372c9f..2d3ecd810c49c 100644
  97. --- a/src/statusor.h
  98. +++ b/src/statusor.h
  99. @@ -17,239 +17,13 @@
  100. #ifndef RLWE_STATUSOR_H_
  101. #define RLWE_STATUSOR_H_
  102. -#include <cassert>
  103. -
  104. -#include "absl/base/attributes.h"
  105. -#include "absl/status/status.h"
  106. -#include "absl/types/optional.h"
  107. +#include "absl/status/statusor.h"
  108. #include "third_party/shell-encryption/base/shell_encryption_export.h"
  109. namespace rlwe {
  110. template <typename T>
  111. -class SHELL_ENCRYPTION_EXPORT StatusOr {
  112. - public:
  113. - // Construct a new StatusOr with Status::UNKNOWN status
  114. - StatusOr();
  115. -
  116. - // Construct a new StatusOr with the given non-ok status. After calling
  117. - // this constructor, calls to value() will CHECK-fail.
  118. - //
  119. - // NOTE: Not explicit - we want to use StatusOr<T> as a return
  120. - // value, so it is convenient and sensible to be able to do 'return
  121. - // Status()' when the return type is StatusOr<T>.
  122. - //
  123. - // REQUIRES: status != Status::OK. This requirement is DCHECKed.
  124. - // In optimized builds, passing Status::OK here will have the effect
  125. - // of passing PosixErrorSpace::EINVAL as a fallback.
  126. - StatusOr(const absl::Status& status);
  127. -
  128. - // Construct a new StatusOr with the given value. If T is a plain pointer,
  129. - // value must not be NULL. After calling this constructor, calls to
  130. - // value() will succeed, and calls to status() will return OK.
  131. - //
  132. - // NOTE: Not explicit - we want to use StatusOr<T> as a return type
  133. - // so it is convenient and sensible to be able to do 'return T()'
  134. - // when the return type is StatusOr<T>.
  135. - //
  136. - // REQUIRES: if T is a plain pointer, value != NULL. This requirement is
  137. - // DCHECKed. In optimized builds, passing a NULL pointer here will have
  138. - // the effect of passing absl::StatusCode::kInternal as a fallback.
  139. - StatusOr(const T& value);
  140. -
  141. - // Copy constructor.
  142. - StatusOr(const StatusOr& other);
  143. -
  144. - // Assignment operator.
  145. - StatusOr& operator=(const StatusOr& other);
  146. -
  147. - // Move constructor and move-assignment operator.
  148. - StatusOr(StatusOr&& other) = default;
  149. - StatusOr& operator=(StatusOr&& other) = default;
  150. -
  151. - // Rvalue-reference overloads of the other constructors and assignment
  152. - // operators, to support move-only types and avoid unnecessary copying.
  153. - StatusOr(T&& value);
  154. -
  155. - // Returns a reference to our status. If this contains a T, then
  156. - // returns Status::OK.
  157. - const absl::Status& status() const;
  158. -
  159. - // Returns this->status().ok()
  160. - bool ok() const;
  161. -
  162. - // Returns a reference to our current value, or CHECK-fails if !this->ok().
  163. - const T& ValueOrDie() const&;
  164. - T& ValueOrDie() &;
  165. - const T&& ValueOrDie() const&&;
  166. - T&& ValueOrDie() &&;
  167. -
  168. - // Returns a reference to our current value, or CHECK-fails if !this->ok().
  169. - const T& value() const&;
  170. - T& value() &;
  171. - const T&& value() const&&;
  172. - T&& value() &&;
  173. -
  174. - // Ignores any errors. This method does nothing except potentially suppress
  175. - // complaints from any tools that are checking that errors are not dropped on
  176. - // the floor.
  177. - void IgnoreError() const {}
  178. -
  179. - operator absl::Status() const { return status(); }
  180. -
  181. - template <template <typename> class OtherStatusOrType>
  182. - operator OtherStatusOrType<T>() {
  183. - if (value_) {
  184. - return OtherStatusOrType<T>(std::move(value_.value()));
  185. - } else {
  186. - return OtherStatusOrType<T>(status());
  187. - }
  188. - }
  189. -
  190. - private:
  191. - absl::Status status_;
  192. - absl::optional<T> value_;
  193. -};
  194. -
  195. -namespace internal {
  196. -
  197. -class SHELL_ENCRYPTION_EXPORT StatusOrHelper {
  198. - public:
  199. - // Move type-agnostic error handling to the .cc.
  200. - static SHELL_ENCRYPTION_EXPORT absl::Status HandleInvalidStatusCtorArg();
  201. - static SHELL_ENCRYPTION_EXPORT absl::Status HandleNullObjectCtorArg();
  202. - static SHELL_ENCRYPTION_EXPORT void Crash(const absl::Status& status);
  203. -
  204. - // Customized behavior for StatusOr<T> vs. StatusOr<T*>
  205. - template <typename T>
  206. - struct Specialize;
  207. -};
  208. -
  209. -template <typename T>
  210. -struct SHELL_ENCRYPTION_EXPORT StatusOrHelper::Specialize {
  211. - // For non-pointer T, a reference can never be NULL.
  212. - static inline bool IsValueNull(const T& t) { return false; }
  213. -};
  214. -
  215. -template <typename T>
  216. -struct SHELL_ENCRYPTION_EXPORT StatusOrHelper::Specialize<T*> {
  217. - static inline bool IsValueNull(const T* t) { return t == nullptr; }
  218. -};
  219. -
  220. -} // namespace internal
  221. -
  222. -template <typename T>
  223. -inline StatusOr<T>::StatusOr()
  224. - : status_(absl::UnknownError("")), value_(absl::nullopt) {}
  225. -
  226. -template <typename T>
  227. -inline StatusOr<T>::StatusOr(const absl::Status& status)
  228. - : status_(status), value_(absl::nullopt) {
  229. - if (status.ok()) {
  230. - status_ = internal::StatusOrHelper::HandleInvalidStatusCtorArg();
  231. - }
  232. -}
  233. -
  234. -template <typename T>
  235. -inline StatusOr<T>::StatusOr(const T& value)
  236. - : status_(absl::OkStatus()), value_(value) {
  237. - if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value)) {
  238. - status_ = internal::StatusOrHelper::HandleNullObjectCtorArg();
  239. - }
  240. -}
  241. -
  242. -template <typename T>
  243. -inline StatusOr<T>::StatusOr(const StatusOr& other)
  244. - : status_(other.status_), value_(other.value_) {}
  245. -
  246. -template <typename T>
  247. -inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<T>& other) {
  248. - status_ = other.status_;
  249. - value_.reset(other.value_);
  250. - return *this;
  251. -}
  252. -
  253. -template <typename T>
  254. -inline StatusOr<T>::StatusOr(T&& value)
  255. - : status_(absl::OkStatus()), value_(std::forward<T>(value)) {
  256. - if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value_.value())) {
  257. - status_ = internal::StatusOrHelper::HandleNullObjectCtorArg();
  258. - }
  259. -}
  260. -
  261. -template <typename T>
  262. -inline const absl::Status& StatusOr<T>::status() const {
  263. - return status_;
  264. -}
  265. -
  266. -template <typename T>
  267. -inline bool StatusOr<T>::ok() const {
  268. - return status_.ok();
  269. -}
  270. -
  271. -template <typename T>
  272. -inline const T& StatusOr<T>::ValueOrDie() const& {
  273. - if (!value_) {
  274. - internal::StatusOrHelper::Crash(status());
  275. - }
  276. - return value_.value();
  277. -}
  278. -
  279. -template <typename T>
  280. -inline T& StatusOr<T>::ValueOrDie() & {
  281. - if (!value_) {
  282. - internal::StatusOrHelper::Crash(status());
  283. - }
  284. - return value_.value();
  285. -}
  286. -
  287. -template <typename T>
  288. -inline const T&& StatusOr<T>::ValueOrDie() const&& {
  289. - if (!value_) {
  290. - internal::StatusOrHelper::Crash(status());
  291. - }
  292. - return std::move(value_.value());
  293. -}
  294. -
  295. -template <typename T>
  296. -inline T&& StatusOr<T>::ValueOrDie() && {
  297. - if (!value_) {
  298. - internal::StatusOrHelper::Crash(status());
  299. - }
  300. - return std::move(value_.value());
  301. -}
  302. -
  303. -template <typename T>
  304. -inline const T& StatusOr<T>::value() const& {
  305. - if (!value_) {
  306. - internal::StatusOrHelper::Crash(status());
  307. - }
  308. - return value_.value();
  309. -}
  310. -
  311. -template <typename T>
  312. -inline T& StatusOr<T>::value() & {
  313. - if (!value_) {
  314. - internal::StatusOrHelper::Crash(status());
  315. - }
  316. - return value_.value();
  317. -}
  318. -
  319. -template <typename T>
  320. -inline const T&& StatusOr<T>::value() const&& {
  321. - if (!value_) {
  322. - internal::StatusOrHelper::Crash(status());
  323. - }
  324. - return std::move(value_.value());
  325. -}
  326. -
  327. -template <typename T>
  328. -inline T&& StatusOr<T>::value() && {
  329. - if (!value_) {
  330. - internal::StatusOrHelper::Crash(status());
  331. - }
  332. - return std::move(value_.value());
  333. -}
  334. +using StatusOr = absl::StatusOr<T>;
  335. } // namespace rlwe
  336. diff --git a/src/testing/coefficient_polynomial_test.cc b/src/testing/coefficient_polynomial_test.cc
  337. index bf43ee6f4ae02..e2a36c3d7d6bf 100644
  338. --- a/src/testing/coefficient_polynomial_test.cc
  339. +++ b/src/testing/coefficient_polynomial_test.cc
  340. @@ -43,7 +43,7 @@ unsigned int seed = 0;
  341. class PolynomialTest : public ::testing::Test {
  342. protected:
  343. PolynomialTest()
  344. - : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).ValueOrDie()),
  345. + : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).value()),
  346. one_(uint_m::ImportOne(params14_.get())),
  347. zero_(uint_m::ImportZero(params14_.get())) {}
  348. diff --git a/src/testing/status_testing.h b/src/testing/status_testing.h
  349. index 27b8c0ed63073..6adc0278c98d2 100644
  350. --- a/src/testing/status_testing.h
  351. +++ b/src/testing/status_testing.h
  352. @@ -43,6 +43,6 @@
  353. #define RLWE_ASSERT_OK_AND_ASSIGN_IMPL_(statusor, lhs, rexpr) \
  354. auto statusor = (rexpr); \
  355. ASSERT_THAT(statusor.ok(), ::testing::Eq(true)); \
  356. - lhs = std::move(statusor).ValueOrDie()
  357. + lhs = std::move(statusor).value()
  358. #endif // RLWE_TESTING_STATUS_TESTING_H_