Debian Codegraph @

crt/aws-c-common/include/aws/common/math.cbmc.inl

raw
1 #ifndef AWS_COMMON_MATH_CBMC_INL
2 #define AWS_COMMON_MATH_CBMC_INL
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 /*
10 * This header is already included, but include it again to make editor
11 * highlighting happier.
12 */
13 #include <aws/common/common.h>
14
15 AWS_EXTERN_C_BEGIN
16
17 /* This header does safe operations. Supressing the checks within these functions
18 * avoids unnecessary CBMC assertions
19 */
20 #pragma CPROVER check push
21 #pragma CPROVER check disable "unsigned-overflow"
22
23 /**
24 * Multiplies a * b. If the result overflows, returns 2^64 - 1.
25 */
26 AWS_STATIC_IMPL uint64_t aws_mul_u64_saturating(uint64_t a, uint64_t b) {
27 if (__CPROVER_overflow_mult(a, b))
28 return UINT64_MAX;
29 return a * b;
30 }
31
32 /**
33 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
34 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
35 */
36 AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
37 if (__CPROVER_overflow_mult(a, b))
38 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
39 *r = a * b;
40 return AWS_OP_SUCCESS;
41 }
42
43 /**
44 * Multiplies a * b. If the result overflows, returns 2^32 - 1.
45 */
46 AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b) {
47 if (__CPROVER_overflow_mult(a, b))
48 return UINT32_MAX;
49 return a * b;
50 }
51
52 /**
53 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
54 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
55 */
56 AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
57 if (__CPROVER_overflow_mult(a, b))
58 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
59 *r = a * b;
60 return AWS_OP_SUCCESS;
61 }
62
63 /**
64 * Adds a + b. If the result overflows returns 2^64 - 1.
65 */
66 AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b) {
67 if (__CPROVER_overflow_plus(a, b))
68 return UINT64_MAX;
69 return a + b;
70 }
71
72 /**
73 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
74 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
75 */
76 AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
77 if (__CPROVER_overflow_plus(a, b))
78 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
79 *r = a + b;
80 return AWS_OP_SUCCESS;
81 }
82
83 /**
84 * Adds a + b. If the result overflows returns 2^32 - 1.
85 */
86 AWS_STATIC_IMPL uint32_t aws_add_u32_saturating(uint32_t a, uint32_t b) {
87 if (__CPROVER_overflow_plus(a, b))
88 return UINT32_MAX;
89 return a + b;
90 }
91
92 /**
93 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
94 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
95 */
96 AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
97 if (__CPROVER_overflow_plus(a, b))
98 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
99 *r = a + b;
100 return AWS_OP_SUCCESS;
101 }
102
103 #pragma CPROVER check pop
104
105 AWS_EXTERN_C_END
106
107 #endif /* AWS_COMMON_MATH_CBMC_INL */