Debian Codegraph @

crt/aws-c-common/README.md

raw symbols SCIP
1 ## AWS C Common
2
3
4 [![GitHub](https://img.shields.io/github/license/awslabs/aws-c-common.svg)](https://github.com/awslabs/aws-c-common/blob/main/LICENSE)
5
6 Core c99 package for AWS SDK for C. Includes cross-platform primitives, configuration, data structures, and error handling.
7
8 ## License
9
10 This library is licensed under the Apache 2.0 License.
11
12 ## Usage
13 ### Building
14 aws-c-common uses CMake for setting up build environments. This library has no non-kernel dependencies so the build is quite
15 simple.
16
17 For example:
18
19 git clone [email protected]:awslabs/aws-c-common.git aws-c-common
20 mkdir aws-c-common-build
21 cd aws-c-common-build
22 cmake ../aws-c-common
23 make -j 12
24 make test
25 sudo make install
26
27 Keep in mind that CMake supports multiple build systems, so for each platform you can pass your own build system
28 as the `-G` option. For example:
29
30 cmake -GNinja ../aws-c-common
31 ninja build
32 ninja test
33 sudo ninja install
34
35 Or on windows,
36
37 cmake -G "Visual Studio 14 2015 Win64" ../aws-c-common
38 msbuild.exe ALL_BUILD.vcproj
39
40 ### CMake Options
41 * -DCMAKE_CLANG_TIDY=/path/to/clang-tidy (or just clang-tidy or clang-tidy-7.0 if it is in your PATH) - Runs clang-tidy as part of your build.
42 * -DENABLE_SANITIZERS=ON - Enables gcc/clang sanitizers, by default this adds -fsanitizer=address,undefined to the compile flags for projects that call aws_add_sanitizers.
43 * -DENABLE_FUZZ_TESTS=ON - Includes fuzz tests in the unit test suite. Off by default, because fuzz tests can take a long time. Set -DFUZZ_TESTS_MAX_TIME=N to determine how long to run each fuzz test (default 60s).
44 * -DCMAKE_INSTALL_PREFIX=/path/to/install - Standard way of installing to a user defined path. If specified when configuring aws-c-common, ensure the same prefix is specified when configuring other aws-c-* SDKs.
45 * -DAWS_STATIC_MSVC_RUNTIME_LIBRARY=ON - Windows-only. Turn ON to use the statically-linked MSVC runtime lib, instead of the DLL.
46
47 ### API style and conventions
48 Every API has a specific set of styles and conventions. We'll outline them here. These conventions are followed in every
49 library in the AWS C SDK ecosystem.
50
51 #### Error handling
52 Every function that returns an `int` type, returns `AWS_OP_SUCCESS` ( 0 ) or `AWS_OP_ERR` (-1) on failure. To retrieve
53 the error code, use the function `aws_last_error()`. Each error code also has a corresponding error string that can be
54 accessed via the `aws_error_str()` function.
55
56 In addition, you can install both a global and a thread local error handler by using the `aws_set_global_error_handler_fn()`
57 and `aws_set_thread_local_error_handler_fn()` functions.
58
59 All error functions are in the `include/aws/common/error.h` header file.
60
61 #### Naming
62 Any function that allocates and initializes an object will be suffixed with `new` (e.g. `aws_myobj_new()`). Similarly, these objects will always
63 have a corresponding function with a `destroy` suffix. The `new` functions will return the allocated object
64 on success and `NULL` on failure. To respond to the error, call `aws_last_error()`. If several `new` or `destroy`
65 functions are available, the variants should be named like `new_x` or `destroy_x` (e.g. `aws_myobj_new_copy()` or `aws_myobj_destroy_secure()`).
66
67 Any function that initializes an existing object will be suffixed with `init` (e.g. `aws_myobj_init()`. These objects will have a corresponding
68 `clean_up` function if necessary. In these cases, you are responsible for making the decisions for how your object is
69 allocated. The `init` functions return `AWS_OP_SUCCESS` ( 0 ) or `AWS_OP_ERR` (-1) on failure. If several `init` or
70 `clean_up` functions are available, they should be named like `init_x` or `clean_up_x` (e.g. `aws_myobj_init_static()` or
71 `aws_myobj_clean_up_secure()`).
72
73 ## Contributing
74
75 If you are contributing to this code-base, first off, THANK YOU!. There are a few things to keep in mind to minimize the
76 pull request turn around time.
77
78 ### Coding "guidelines"
79 These "guidelines" are followed in every library in the AWS C SDK ecosystem.
80
81 #### Memory Management
82 * All APIs that need to be able to allocate memory, must take an instance of `aws_allocator` and use that. No `malloc()` or
83 `free()` calls should be made directly.
84 * If an API does not allocate the memory, it does not free it. All allocations and deallocations should take place at the same level.
85 For example, if a user allocates memory, the user is responsible for freeing it. There will inevitably be a few exceptions to this
86 rule, but they will need significant justification to make it through the code-review.
87 * All functions that allocate memory must raise an `AWS_ERROR_OOM` error code upon allocation failures. If it is a `new()` function
88 it should return NULL. If it is an `init()` function, it should return `AWS_OP_ERR`.
89
90 #### Threading
91 * Occasionally a thread is necessary. In those cases, prefer for memory not to be shared between threads. If memory must cross
92 a thread barrier it should be a complete ownership hand-off. Bias towards, "if I need a mutex, I'm doing it wrong".
93 * Do not sleep or block .... ever .... under any circumstances, in non-test-code.
94 * Do not expose blocking APIs.
95
96 ### Error Handling
97 * For APIs returning an `int` error code. The only acceptable return types are `AWS_OP_SUCCESS` and `AWS_OP_ERR`. Before
98 returning control to the caller, if you have an error to raise, use the `aws_raise_error()` function.
99 * For APIs returning an allocated instance of an object, return the memory on success, and `NULL` on failure. Before
100 returning control to the caller, if you have an error to raise, use the `aws_raise_error()` function.
101
102 See [error-handling.md](docs/error-handling.md) for a longer tutorial.
103
104 #### Log Subjects & Error Codes
105 The logging & error handling infrastructure is designed to support multiple libraries. For this to work, AWS maintained libraries
106 have pre-slotted log subjects & error codes for each library. The currently allocated ranges are:
107
108 | Range | Library Name |
109 | --- | --- |
110 | [0x0000, 0x0400) | aws-c-common |
111 | [0x0400, 0x0800) | aws-c-io |
112 | [0x0800, 0x0C00) | aws-c-http |
113 | [0x0C00, 0x1000) | aws-c-compression |
114 | [0x1000, 0x1400) | aws-c-eventstream |
115 | [0x1400, 0x1800) | aws-c-mqtt |
116 | [0x1800, 0x1C00) | aws-c-auth |
117 | [0x1C00, 0x2000) | aws-c-cal |
118 | [0x2000, 0x2400) | aws-crt-cpp |
119 | [0x2400, 0x2800) | aws-crt-java |
120 | [0x2800, 0x2C00) | aws-crt-python |
121 | [0x2C00, 0x3000) | aws-crt-nodejs |
122 | [0x3000, 0x3400) | aws-crt-dotnet |
123 | [0x3400, 0x3800) | aws-c-iot |
124 | [0x3800, 0x3C00) | aws-c-s3 |
125 | [0x3C00, 0x4000) | aws-c-sdkutils |
126 | [0x4000, 0x4400) | aws-crt-kotlin |
127 | [0x4400, 0x4800) | aws-crt-swift |
128 | [0x4800, 0x4C00) | (reserved for future project) |
129
130 Each library should begin its error and log subject values at the beginning of its range and follow in sequence (don't skip codes). Upon
131 adding an AWS maintained library, a new enum range must be approved and added to the above table.
132
133 ### Testing
134 We have a high bar for test coverage, and PRs fixing bugs or introducing new functionality need to have tests before
135 they will be accepted. A couple of tips:
136
137 #### Aws Test Harness
138 We provide a test harness for writing unit tests. This includes an allocator that will fail your test if you have any
139 memory leaks, as well as some `ASSERT` macros. To write a test:
140
141 * Create a *.c test file in the tests directory of the project.
142 * Implement one or more tests with the signature `int test_case_name(struct aws_allocator *, void *ctx)`
143 * Use the `AWS_TEST_CASE` macro to declare the test.
144 * Include your test in the `tests/main.c` file.
145 * Include your test in the `tests/CMakeLists.txt` file.
146
147 ### Coding Style
148 * No Tabs.
149 * Indent is 4 spaces.
150 * K & R style for braces.
151 * Space after if, before the `(`.
152 * `else` and `else if` stay on the same line as the closing brace.
153
154 Example:
155
156 if (condition) {
157 do_something();
158 } else {
159 do_something_else();
160 }
161 * Avoid C99 features in header files. For some types such as bool, uint32_t etc..., these are defined if not available for the language
162 standard being used in `aws/common/common.h`, so feel free to use them.
163 * For C++ compatibility, don't put const members in structs.
164 * Avoid C++ style comments e.g. `//` in header files and prefer block style (`/* */`) for long blocks of text. C++ style comments are fine in C files.
165 * All public API functions need C++ guards and Windows dll semantics.
166 * Use Unix line endings.
167 * Where implementation hiding is desired for either ABI or runtime polymorphism reasons, use the `void *impl` pattern. v-tables
168 should be the last member in the struct.
169 * For #ifdef, put a # as the first character on the line and then indent the compilation branches.
170
171 Example:
172
173
174 #ifdef FOO
175 do_something();
176
177 # ifdef BAR
178 do_something_else();
179 # endif
180 #endif
181
182
183 * For all error code names with the exception of aws-c-common, use `AWS_ERROR_<lib name>_<error name>`.
184 * All error strings should be written using correct English grammar.
185 * SNAKE_UPPER_CASE constants, macros, and enum members.
186 * snake_lower_case everything else.
187 * `static` (local file scope) variables that are not `const` are prefixed by `s_` and lower snake case.
188 * Global variables not prefixed as `const` are prefixed by `g_` and lower snake case.
189 * Thread local variables are prefixed as `tl_` and lower snake case.
190 * Macros and `const` variables are upper snake case.
191 * For constants, prefer anonymous enums.
192 * Don't typedef structs. It breaks forward declaration ability.
193 * Don't typedef enums. It breaks forward declaration ability.
194 * typedef function definitions for use as function pointers as values and suffixed with _fn.
195
196 Do this:
197
198 typedef int(fn_name_fn)(void *);
199
200 Not this:
201
202 typedef int(*fn_name_fn)(void *);
203
204 * If a callback may be async, then always have it be async.
205 Callbacks that are sometimes async and sometimes sync are hard to code around and lead to bugs
206 (see [this blog post](https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/)).
207 Unfortunately many callbacks in this codebase currently violate this rule,
208 so be careful. But do not add any more.
209 * Every source and header file must have a copyright header (The standard AWS one for apache 2).
210 * Use standard include guards (e.g. #IFNDEF HEADER_NAME #define HEADER_NAME etc...).
211 * Include order should be:
212 the header for the translation unit for the .c file
213 newline
214 header files in a directory in alphabetical order
215 newline
216 header files not in a directory (system and stdlib headers)
217 * Platform specifics should be handled in c files and partitioned by directory.
218 * Do not use `extern inline`. It's too unpredictable between compiler versions and language standards.
219 * Namespace all definitions in header files with `aws_<libname>?_<api>_<what it does>`. Lib name is
220 not always required if a conflict is not likely and it provides better ergonomics.
221 * `init`, `clean_up`, `new`, `destroy` are suffixed to the function names for their object.
222
223 Example:
224
225 AWS_COMMON_API
226 int aws_module_init(aws_module_t *module);
227 AWS_COMMON_API
228 void aws_module_clean_up(aws_module_t *module);
229 AWS_COMMON_API
230 aws_module_t *aws_module_new(aws_allocator_t *allocator);
231 AWS_COMMON_API
232 void aws_module_destroy(aws_module_t *module);
233
234 * Avoid c-strings, and don't write code that depends on `NULL` terminators.
235 * Pass strings via `struct aws_byte_cursor`. This is a non-owning view type. Pass it by value. Strings passed this way do not need a `NULL` terminator.
236 * Only pass `const char *` when thinly wrapping an OS function that *requires* a `NULL` terminator.
237 * Store const strings as `struct aws_string *`
238 * Store mutable string buffers as `struct aws_byte_buf`
239
240 * There is only one valid character encoding-- UTF-8. Try not to ever need to care about character encodings, but
241 where you do, the working assumption should always be UTF-8 unless it's something we don't get a choice in (e.g. a protocol
242 explicitly mandates a character set).
243 * If a function has many arguments, or any optional arguments use an options-struct.
244 * If a variable is time-related, always include the unit of time in its name (e.g. `timeout_ms`).
245 * If you are adding/using a compiler specific keyword, macro, or intrinsic, hide it behind a platform independent macro
246 definition. This mainly applies to header files. Obviously, if you are writing a file that will only be built on a certain
247 platform, you have more liberty on this.
248 * When checking more than one error condition, check and log each condition separately with a unique message.
249
250 Do this:
251
252 if (options->callback == NULL) {
253 AWS_LOGF_ERROR(AWS_LS_SOME_SUBJECT, "Invalid options - callback is null");
254 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
255 }
256
257 if (options->allocator == NULL) {
258 AWS_LOGF_ERROR(AWS_LS_SOME_SUBJECT, "Invalid options - allocator is null");
259 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
260 }
261
262 Not this:
263
264 if (options->callback == NULL || options->allocator == NULL) {
265 AWS_LOGF_ERROR(AWS_LS_SOME_SUBJECT, "Invalid options - something is null");
266 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
267 }
268
269 ## CBMC
270
271 To learn more about CBMC and proofs specifically, review the training material [here](https://model-checking.github.io/cbmc-training).
272
273 The `verification/cbmc/proofs` directory contains CBMC proofs.
274
275 In order to run these proofs you will need to install CBMC and other tools by following the instructions [here](https://model-checking.github.io/cbmc-training/installation.html).