diff --git a/.clang-tidy b/.clang-tidy index 86a39f963..af7fcd282 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,7 +1,7 @@ --- -Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,google-runtime-int,llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-non-const-parameter,-readability-avoid-const-params-in-decls,-readability-else-after-return,-readability-isolate-declaration,-readability-uppercase-literal-suffix,-bugprone-sizeof-expression' +Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,google-runtime-int,llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-non-const-parameter,-readability-avoid-const-params-in-decls,-readability-else-after-return,-readability-isolate-declaration,-readability-uppercase-literal-suffix,-bugprone-sizeof-expression,-bugprone-easily-swappable-parameters,-readability-identifier-length,-misc-no-recursion,-readability-function-cognitive-complexity,-readability-magic-numbers' WarningsAsErrors: '*' -HeaderFilterRegex: '.*\.[h|inl]$' +HeaderFilterRegex: '^(?!.*external).*\.[h|inl]$' FormatStyle: 'file' CheckOptions: - key: readability-braces-around-statements.ShortStatementLines diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0fcf4b134..0f8b49cca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ on: - 'main' env: - BUILDER_VERSION: v0.9.43 + BUILDER_VERSION: v0.9.62 BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net BUILDER_SOURCE: releases PACKAGE_NAME: aws-c-common @@ -125,7 +125,7 @@ jobs: python builder.pyz build -p ${{ env.PACKAGE_NAME }} --target windows-${{ matrix.arch }} --compiler msvc-16 windows-vc15: - runs-on: windows-2022 # latest + runs-on: windows-2019 # windows-2019 is last env with Visual Studio 2017 (v15.0) strategy: matrix: arch: [x86, x64] @@ -174,8 +174,8 @@ jobs: echo "Starting to run AppVerifier on all tests found by CTest" python .\aws-c-common\scripts\appverifier_ctest.py --build_directory .\aws-c-common\build\aws-c-common - osx: - runs-on: macos-12 # latest + macos-x64: + runs-on: macos-14-large # latest steps: - name: Build ${{ env.PACKAGE_NAME }} + consumers run: | @@ -183,8 +183,17 @@ jobs: chmod a+x builder ./builder build -p ${{ env.PACKAGE_NAME }} - osx-no-cpu-extensions: - runs-on: macos-12 # latest + macos: + runs-on: macos-14 # latest + steps: + - name: Build ${{ env.PACKAGE_NAME }} + consumers + run: | + python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')" + chmod a+x builder + ./builder build -p ${{ env.PACKAGE_NAME }} + + macos-no-cpu-extensions: + runs-on: macos-14 # latest steps: - name: Build ${{ env.PACKAGE_NAME }} + consumers run: | @@ -197,10 +206,9 @@ jobs: steps: - uses: actions/checkout@v3 - name: Build ${{ env.PACKAGE_NAME }} + consumers - uses: cross-platform-actions/action@v0.10.0 + uses: cross-platform-actions/action@v0.20.0 with: operating_system: openbsd - architecture: x86-64 version: '7.2' shell: bash run: | @@ -209,6 +217,23 @@ jobs: chmod a+x builder ./builder build -p ${{ env.PACKAGE_NAME }} + freebsd: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Build ${{ env.PACKAGE_NAME }} + consumers + uses: cross-platform-actions/action@v0.20.0 + with: + operating_system: freebsd + version: '13.2' + run: | + sudo pkg install -y python3 devel/py-pip net/py-urllib3 cmake + python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz', 'builder')" + chmod a+x builder + ./builder build -p ${{ env.PACKAGE_NAME }} + cross_compile: name: Cross Compile ${{matrix.arch}} runs-on: ubuntu-20.04 # latest @@ -239,8 +264,8 @@ jobs: python -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder.pyz')" python builder.pyz build -p ${{ env.PACKAGE_NAME }} --config Debug - osx-debug: - runs-on: macos-12 # latest + macos-debug: + runs-on: macos-14 # latest steps: - name: Build ${{ env.PACKAGE_NAME }} + consumers run: | diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index d914a0955..36388d688 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -5,15 +5,12 @@ on: [push] jobs: clang-format: - runs-on: ubuntu-20.04 # latest + runs-on: ubuntu-24.04 # latest steps: - name: Checkout Sources - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: clang-format lint - uses: DoozyX/clang-format-lint-action@v0.3.1 - with: - # List of extensions to check - extensions: c,h,inl - source: 'source include tests verification' + run: | + ./format-check.py diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index b022f7e4d..3967514d5 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -7,7 +7,7 @@ jobs: strategy: matrix: - host: [ubuntu-20.04] # latest + host: [ubuntu-22.04] # latest runs-on: ${{ matrix.host }} diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 7a1adff09..317354e59 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -4,7 +4,7 @@ on: push: env: - BUILDER_VERSION: v0.9.37 + BUILDER_VERSION: v0.9.55 BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net BUILDER_SOURCE: releases PACKAGE_NAME: aws-c-common diff --git a/.github/workflows/handle-stale-discussions.yml b/.github/workflows/handle-stale-discussions.yml new file mode 100644 index 000000000..2b89f2da1 --- /dev/null +++ b/.github/workflows/handle-stale-discussions.yml @@ -0,0 +1,18 @@ +name: HandleStaleDiscussions +on: + schedule: + - cron: '0 */4 * * *' + discussion_comment: + types: [created] + +jobs: + handle-stale-discussions: + name: Handle stale discussions + runs-on: ubuntu-latest + permissions: + discussions: write + steps: + - name: Stale discussions action + uses: aws-github-ops/handle-stale-discussions@v1 + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} \ No newline at end of file diff --git a/.github/workflows/proof_ci.yaml b/.github/workflows/proof_ci.yaml index 19330147e..f8fb8ece2 100644 --- a/.github/workflows/proof_ci.yaml +++ b/.github/workflows/proof_ci.yaml @@ -63,7 +63,7 @@ jobs: run: | # Search within 5 most recent releases for latest available package CBMC_REL="https://api.github.com/repos/diffblue/cbmc/releases?page=1&per_page=5" - CBMC_DEB=$(curl -s $CBMC_REL | jq -r '.[].assets[].browser_download_url' | grep -e 'ubuntu-20.04' | head -n 1) + CBMC_DEB=$(curl -s $CBMC_REL --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | jq -r '.[].assets[].browser_download_url' | grep -e 'ubuntu-20.04' | head -n 1) CBMC_ARTIFACT_NAME=$(basename $CBMC_DEB) curl -o $CBMC_ARTIFACT_NAME -L $CBMC_DEB sudo dpkg -i $CBMC_ARTIFACT_NAME @@ -81,7 +81,7 @@ jobs: shell: bash run: | CBMC_VIEWER_REL="https://api.github.com/repos/model-checking/cbmc-viewer/releases/latest" - CBMC_VIEWER_VERSION=$(curl -s $CBMC_VIEWER_REL | jq -r .name | sed 's/viewer-//') + CBMC_VIEWER_VERSION=$(curl -s $CBMC_VIEWER_REL --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | jq -r .name | sed 's/viewer-//') pip3 install cbmc-viewer==$CBMC_VIEWER_VERSION - name: Install CBMC viewer ${{ env.CBMC_VIEWER_VERSION }} if: ${{ env.CBMC_VIEWER_VERSION != 'latest' }} @@ -97,7 +97,7 @@ jobs: run: | # Search within 5 most recent releases for latest available package LITANI_REL="https://api.github.com/repos/awslabs/aws-build-accumulator/releases?page=1&per_page=5" - LITANI_DEB=$(curl -s $LITANI_REL | jq -r '.[].assets[0].browser_download_url' | head -n 1) + LITANI_DEB=$(curl -s $LITANI_REL --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | jq -r '.[].assets[0].browser_download_url' | head -n 1) DBN_PKG_FILENAME=$(basename $LITANI_DEB) curl -L $LITANI_DEB -o $DBN_PKG_FILENAME sudo apt-get update @@ -119,7 +119,7 @@ jobs: if ${{ env.KISSAT_TAG == 'latest' }} then KISSAT_REL="https://api.github.com/repos/arminbiere/kissat/releases/latest" - KISSAT_TAG_NAME=$(curl -s $KISSAT_REL | jq -r '.tag_name') + KISSAT_TAG_NAME=$(curl -s $KISSAT_REL --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | jq -r '.tag_name') else KISSAT_TAG_NAME=${{ env.KISSAT_TAG }} fi @@ -138,7 +138,7 @@ jobs: if ${{ env.CADICAL_TAG == 'latest' }} then CADICAL_REL="https://api.github.com/repos/arminbiere/cadical/releases/latest" - CADICAL_TAG_NAME=$(curl -s $CADICAL_REL | jq -r '.tag_name') + CADICAL_TAG_NAME=$(curl -s $CADICAL_REL --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | jq -r '.tag_name') else CADICAL_TAG_NAME=${{ env.CADICAL_TAG }} fi diff --git a/.github/workflows/proof_ci_resources/config.yaml b/.github/workflows/proof_ci_resources/config.yaml index d2273314b..83407e8d9 100644 --- a/.github/workflows/proof_ci_resources/config.yaml +++ b/.github/workflows/proof_ci_resources/config.yaml @@ -1,7 +1,8 @@ -cadical-tag: latest -cbmc-version: latest -cbmc-viewer-version: latest -kissat-tag: latest -litani-version: latest +# Use exact versions (instead of "latest") so we're not broken by surprise upgrades. +cadical-tag: "rel-2.0.0" # tag of latest release: https://github.com/arminbiere/cadical/releases +cbmc-version: "6.1.0" # semver of latest release: https://github.com/diffblue/cbmc/releases +cbmc-viewer-version: "3.9" # semver of latest release: https://github.com/model-checking/cbmc-viewer/releases +kissat-tag: "rel-3.1.1" # tag of latest release: https://github.com/arminbiere/kissat/releases +litani-version: "1.29.0" # semver of latest release: https://github.com/awslabs/aws-build-accumulator/releases proofs-dir: verification/cbmc/proofs run-cbmc-proofs-command: ./run-cbmc-proofs.py diff --git a/.github/workflows/stale_issue.yml b/.github/workflows/stale_issue.yml index 5624eec4b..10aa552ec 100644 --- a/.github/workflows/stale_issue.yml +++ b/.github/workflows/stale_issue.yml @@ -9,6 +9,9 @@ jobs: cleanup: runs-on: ubuntu-latest name: Stale issue job + permissions: + issues: write + pull-requests: write steps: - uses: aws-actions/stale-issue-cleanup@v3 with: @@ -30,8 +33,8 @@ jobs: closed-for-staleness-label: closed-for-staleness # Issue timing - days-before-stale: 2 - days-before-close: 5 + days-before-stale: 10 + days-before-close: 4 days-before-ancient: 36500 # If you don't want to mark a issue as being ancient based on a diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d484d7f9..d7cedeef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ # SPDX-License-Identifier: Apache-2.0. cmake_minimum_required(VERSION 3.0) -option(STATIC_CRT "Windows specific option that to specify static/dynamic run-time library" OFF) option(ALLOW_CROSS_COMPILED_TESTS "Allow tests to be compiled via cross compile, for use with qemu" OFF) project(aws-c-common LANGUAGES C VERSION 0.1.0) @@ -25,7 +24,6 @@ include(AwsFeatureTests) include(AwsSanitizers) include(AwsThreadAffinity) include(AwsThreadName) -include(AwsSIMD) include(CTest) set(GENERATED_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") @@ -38,8 +36,8 @@ file(GLOB AWS_COMMON_HEADERS "include/aws/common/*.inl" ) -file (GLOB AWS_COMMON_EXTERNAL_HEADERS - "include/aws/common/external/*.h") +file (GLOB AWS_COMMON_EXTERNAL_INSTALLED_HEADERS + "include/aws/common/external/ittnotify.h") file(GLOB AWS_TEST_HEADERS "include/aws/testing/*.h" @@ -55,12 +53,17 @@ file(GLOB AWS_COMMON_SRC ) file (GLOB AWS_COMMON_EXTERNAL_SRC - "source/external/*.c") + "source/external/*.c" + "source/external/libcbor/*.c" + "source/external/libcbor/cbor/*.c" + "source/external/libcbor/cbor/internal/*.c" + ) + option(AWS_NUM_CPU_CORES "Number of CPU cores of the target machine. Useful when cross-compiling." 0) if (WIN32) - set(WINDOWS_KERNEL_LIB "Kernel32" CACHE STRING "The name of the kernel library to link against (default: Kernel32)") + set(WINDOWS_KERNEL_LIB "kernel32" CACHE STRING "The name of the kernel library to link against (default: kernel32)") file(GLOB AWS_COMMON_OS_HEADERS "include/aws/common/windows/*" @@ -68,6 +71,7 @@ if (WIN32) file(GLOB AWS_COMMON_OS_SRC "source/windows/*.c" + "source/platform_fallback_stubs/system_info.c" ) if (MSVC) @@ -79,7 +83,10 @@ if (WIN32) endif () list(APPEND PLATFORM_DEFINES WINDOWS_KERNEL_LIB=${WINDOWS_KERNEL_LIB}) - list(APPEND PLATFORM_LIBS BCrypt ${WINDOWS_KERNEL_LIB} Ws2_32 Shlwapi) + # PSAPI_VERSION=1 is needed to support GetProcessMemoryInfo on both pre and + # post Win7 OS's. + list(APPEND PLATFORM_DEFINES PSAPI_VERSION=1) + list(APPEND PLATFORM_LIBS bcrypt ${WINDOWS_KERNEL_LIB} ws2_32 shlwapi psapi) else () file(GLOB AWS_COMMON_OS_HEADERS "include/aws/common/posix/*" @@ -106,19 +113,26 @@ else () # Don't add the exact path to CoreFoundation as this would hardcode the SDK version list(APPEND PLATFORM_LIBS dl Threads::Threads "-framework CoreFoundation") list (APPEND AWS_COMMON_OS_SRC "source/darwin/*.c") # OS specific includes + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") # Android does not link to libpthread nor librt, so this is fine list(APPEND PLATFORM_LIBS dl m Threads::Threads rt) list (APPEND AWS_COMMON_OS_SRC "source/linux/*.c") # OS specific includes elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") list(APPEND PLATFORM_LIBS dl m thr execinfo) + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD") list(APPEND PLATFORM_LIBS dl m Threads::Threads execinfo) + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") list(APPEND PLATFORM_LIBS m Threads::Threads execinfo) + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") elseif(CMAKE_SYSTEM_NAME STREQUAL "Android") list(APPEND PLATFORM_LIBS log) file(GLOB ANDROID_SRC "source/android/*.c") list(APPEND AWS_COMMON_OS_SRC "${ANDROID_SRC}") + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") + else() + list (APPEND AWS_COMMON_OS_SRC "source/platform_fallback_stubs/system_info.c") endif() endif() @@ -143,13 +157,17 @@ if (USE_CPU_EXTENSIONS) ) endif() elseif (AWS_ARCH_ARM64 OR AWS_ARCH_ARM32) - if (MSVC) + if (WIN32) file(GLOB AWS_COMMON_ARCH_SRC - "source/arch/arm/msvc/*.c" + "source/arch/arm/windows/*.c" ) - elseif (AWS_HAVE_AUXV) + elseif(APPLE) file(GLOB AWS_COMMON_ARCH_SRC - "source/arch/arm/asm/*.c" + "source/arch/arm/darwin/*.c" + ) + else() + file(GLOB AWS_COMMON_ARCH_SRC + "source/arch/arm/auxv/*.c" ) endif() endif() @@ -161,7 +179,6 @@ file(GLOB COMMON_HEADERS ${AWS_COMMON_HEADERS} ${AWS_COMMON_OS_HEADERS} ${AWS_COMMON_PRIV_HEADERS} - ${AWS_COMMON_EXTERNAL_HEADERS} ${AWS_TEST_HEADERS} ) @@ -201,6 +218,10 @@ endif() set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.0.0) set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 1) +# libcbor files do includes like: #include "cbor/cbor_export.h" +# To make these paths work, add the location we're storing them as a search path. +target_include_directories(${PROJECT_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/source/external/libcbor) target_include_directories(${PROJECT_NAME} PUBLIC $ $) @@ -211,17 +232,14 @@ target_include_directories(${PROJECT_NAME} PUBLIC target_compile_definitions(${PROJECT_NAME} PRIVATE -DCJSON_HIDE_SYMBOLS) -# Enable SIMD encoder if the compiler supports the right features -simd_add_definitions(${PROJECT_NAME}) - -if (HAVE_AVX2_INTRINSICS) +if (AWS_HAVE_AVX2_INTRINSICS) target_compile_definitions(${PROJECT_NAME} PRIVATE -DUSE_SIMD_ENCODING) - simd_add_source_avx2(${PROJECT_NAME} "source/arch/intel/encoding_avx2.c") + simd_append_source_and_features(${PROJECT_NAME} "source/arch/intel/encoding_avx2.c" ${AWS_AVX2_FLAG}) message(STATUS "Building SIMD base64 decoder") endif() # Preserve subdirectories when installing headers -foreach(HEADER_SRCPATH IN ITEMS ${AWS_COMMON_HEADERS} ${AWS_COMMON_OS_HEADERS} ${GENERATED_CONFIG_HEADER} ${AWS_TEST_HEADERS}) +foreach(HEADER_SRCPATH IN ITEMS ${AWS_COMMON_HEADERS} ${AWS_COMMON_OS_HEADERS} ${GENERATED_CONFIG_HEADER} ${AWS_TEST_HEADERS} ${AWS_COMMON_EXTERNAL_INSTALLED_HEADERS}) get_filename_component(HEADER_DIR ${HEADER_SRCPATH} DIRECTORY) # Note: We need to replace the generated include directory component first, otherwise if the build # directory is located inside the source tree, we'll partially rewrite the path and fail to replace it @@ -296,6 +314,7 @@ configure_file(${CONFIG_HEADER_TEMPLATE} if (ALLOW_CROSS_COMPILED_TESTS OR NOT CMAKE_CROSSCOMPILING) if (BUILD_TESTING) add_subdirectory(tests) + add_subdirectory(bin/system_info) endif() endif() diff --git a/README.md b/README.md index fcedee607..95ab31533 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Or on windows, * -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. * -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). * -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. -* -DSTATIC_CRT=ON - On MSVC, use /MT(d) to link MSVCRT +* -DAWS_STATIC_MSVC_RUNTIME_LIBRARY=ON - Windows-only. Turn ON to use the statically-linked MSVC runtime lib, instead of the DLL. ### API style and conventions Every API has a specific set of styles and conventions. We'll outline them here. These conventions are followed in every @@ -121,7 +121,7 @@ have pre-slotted log subjects & error codes for each library. The currently allo | [0x3400, 0x3800) | aws-c-iot | | [0x3800, 0x3C00) | aws-c-s3 | | [0x3C00, 0x4000) | aws-c-sdkutils | -| [0x4000, 0x4400) | (reserved for future project) | +| [0x4000, 0x4400) | aws-crt-kotlin | | [0x4400, 0x4800) | (reserved for future project) | 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 @@ -158,7 +158,7 @@ Example: * Avoid C99 features in header files. For some types such as bool, uint32_t etc..., these are defined if not available for the language standard being used in `aws/common/common.h`, so feel free to use them. * For C++ compatibility, don't put const members in structs. -* Avoid C++ style comments e.g. `//`. +* 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. * All public API functions need C++ guards and Windows dll semantics. * Use Unix line endings. * Where implementation hiding is desired for either ABI or runtime polymorphism reasons, use the `void *impl` pattern. v-tables @@ -197,7 +197,7 @@ Example: Not this: typedef int(*fn_name_fn)(void *); - + * If a callback may be async, then always have it be async. Callbacks that are sometimes async and sometimes sync are hard to code around and lead to bugs (see [this blog post](https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/)). @@ -239,7 +239,7 @@ platform, you have more liberty on this. * When checking more than one error condition, check and log each condition separately with a unique message. Do this: - + if (options->callback == NULL) { AWS_LOGF_ERROR(AWS_LS_SOME_SUBJECT, "Invalid options - callback is null"); return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); @@ -251,7 +251,7 @@ platform, you have more liberty on this. } Not this: - + if (options->callback == NULL || options->allocator == NULL) { AWS_LOGF_ERROR(AWS_LS_SOME_SUBJECT, "Invalid options - something is null"); return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); diff --git a/THIRD-PARTY-LICENSES.txt b/THIRD-PARTY-LICENSES.txt new file mode 100644 index 000000000..4a3e34675 --- /dev/null +++ b/THIRD-PARTY-LICENSES.txt @@ -0,0 +1,83 @@ +** ittapi ittnotify.h; version v3.24.2 -- https://github.com/intel/ittapi/blob/master/include/ittnotify.h +Copyright (C) 2005-2019 Intel Corporation +SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause + +Amazon.com has chosen to use this file under the terms of the BSD-3-Clause +license. + +Copyright (c) 2019 Intel Corporation. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------ + +** cJSON; version 1.7.16 -- https://github.com/DaveGamble/cJSON +Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +------ + +** libcbor; version 0.11.0 -- https://github.com/PJK/libcbor +Copyright (c) 2014-2017 Pavel Kalvoda + +MIT License + +Copyright (c) 2014-2017 Pavel Kalvoda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/bin/system_info/CMakeLists.txt b/bin/system_info/CMakeLists.txt new file mode 100644 index 000000000..cb4ca0081 --- /dev/null +++ b/bin/system_info/CMakeLists.txt @@ -0,0 +1,18 @@ +project(print-sys-info C) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake") + +file(GLOB SI_SRC + "*.c" + ) + +set(SI_PROJECT_NAME print-sys-info) +add_executable(${SI_PROJECT_NAME} ${SI_SRC}) +aws_set_common_properties(${SI_PROJECT_NAME}) + + +target_include_directories(${SI_PROJECT_NAME} PUBLIC + $ + $) + +target_link_libraries(${SI_PROJECT_NAME} PRIVATE aws-c-common) diff --git a/bin/system_info/print_system_info.c b/bin/system_info/print_system_info.c new file mode 100644 index 000000000..aac1b570f --- /dev/null +++ b/bin/system_info/print_system_info.c @@ -0,0 +1,62 @@ + + +#include +#include +#include +#include + +int main(void) { + struct aws_allocator *allocator = aws_default_allocator(); + aws_common_library_init(allocator); + struct aws_logger_standard_options options = { + .file = stderr, + .level = AWS_LOG_LEVEL_TRACE, + }; + + struct aws_logger logger; + aws_logger_init_standard(&logger, allocator, &options); + aws_logger_set(&logger); + + struct aws_system_environment *env = aws_system_environment_load(allocator); + + fprintf(stdout, "crt-detected env: {\n"); + + struct aws_byte_cursor virtualization_vendor = aws_system_environment_get_virtualization_vendor(env); + fprintf( + stdout, + " 'virtualization vendor': '" PRInSTR "',\n", + (int)virtualization_vendor.len, + virtualization_vendor.ptr); + struct aws_byte_cursor product_name = aws_system_environment_get_virtualization_product_name(env); + fprintf(stdout, " 'product name': '" PRInSTR "',\n", (int)product_name.len, product_name.ptr); + fprintf( + stdout, " 'number of processors': '%lu',\n", (unsigned long)aws_system_environment_get_processor_count(env)); + size_t numa_nodes = aws_system_environment_get_cpu_group_count(env); + + if (numa_nodes > 1) { + fprintf(stdout, " 'numa architecture': 'true',\n"); + fprintf(stdout, " 'number of numa nodes': '%lu'\n", (unsigned long)numa_nodes); + } else { + fprintf(stdout, " 'numa architecture': 'false'\n"); + } + + fprintf(stdout, " 'cpu_capabilities': {\n"); + fprintf(stdout, " 'arm_crc': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_CRC) ? "true" : "false"); + fprintf(stdout, " 'arm_pmull': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_PMULL) ? "true" : "false"); + fprintf(stdout, " 'arm_crypto': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_CRYPTO) ? "true" : "false"); + fprintf(stdout, " 'amd_sse4_1': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_1) ? "true" : "false"); + fprintf(stdout, " 'amd_sse4_2': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_2) ? "true" : "false"); + fprintf(stdout, " 'amd_clmul': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_CLMUL) ? "true" : "false"); + fprintf(stdout, " 'amd_vpclmulqdq': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_VPCLMULQDQ) ? "true" : "false"); + fprintf(stdout, " 'amd_avx2': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX2) ? "true" : "false"); + fprintf(stdout, " 'amd_avx512': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX512) ? "true" : "false"); + fprintf(stdout, " 'amd_bmi2': %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_BMI2) ? "true" : "false"); + fprintf(stdout, " }\n"); + + fprintf(stdout, "}\n"); + aws_system_environment_release(env); + aws_logger_clean_up(&logger); + + aws_common_library_clean_up(); + return 0; +} diff --git a/cmake/AwsCFlags.cmake b/cmake/AwsCFlags.cmake index b717bcab6..470f6db18 100644 --- a/cmake/AwsCFlags.cmake +++ b/cmake/AwsCFlags.cmake @@ -10,6 +10,9 @@ option(AWS_ENABLE_LTO "Enables LTO on libraries. Ensure this is set on all consu option(LEGACY_COMPILER_SUPPORT "This enables builds with compiler versions such as gcc 4.1.2. This is not a 'supported' feature; it's just a best effort." OFF) option(AWS_SUPPORT_WIN7 "Restricts WINAPI calls to Win7 and older (This will have implications in downstream libraries that use TLS especially)" OFF) option(AWS_WARNINGS_ARE_ERRORS "Compiler warning is treated as an error. Try turning this off when observing errors on a new or uncommon compiler" OFF) +option(AWS_ENABLE_TRACING "Enable tracing macros" OFF) +option(AWS_STATIC_MSVC_RUNTIME_LIBRARY "Windows-only. Turn ON to use the statically-linked MSVC runtime lib, instead of the DLL" OFF) +option(STATIC_CRT "Deprecated. Use AWS_STATIC_MSVC_RUNTIME_LIBRARY instead" OFF) # Check for Posix Large Files Support (LFS). # On most 64bit systems, LFS is enabled by default. @@ -79,21 +82,19 @@ function(aws_set_common_properties target) list(APPEND AWS_C_FLAGS /DAWS_SUPPORT_WIN7=1) endif() - string(TOUPPER "${CMAKE_BUILD_TYPE}" _CMAKE_BUILD_TYPE) - if(STATIC_CRT) - string(REPLACE "/MD" "/MT" _FLAGS "${CMAKE_C_FLAGS_${_CMAKE_BUILD_TYPE}}") + # Set MSVC runtime libary. + # Note: there are other ways of doing this if we bump our CMake minimum to 3.14+ + # See: https://cmake.org/cmake/help/latest/policy/CMP0091.html + if (AWS_STATIC_MSVC_RUNTIME_LIBRARY OR STATIC_CRT) + list(APPEND AWS_C_FLAGS "/MT$<$:d>") else() - string(REPLACE "/MT" "/MD" _FLAGS "${CMAKE_C_FLAGS_${_CMAKE_BUILD_TYPE}}") + list(APPEND AWS_C_FLAGS "/MD$<$:d>") endif() - string(REPLACE " " ";" _FLAGS "${_FLAGS}") - list(APPEND AWS_C_FLAGS "${_FLAGS}") else() list(APPEND AWS_C_FLAGS -Wall -Wstrict-prototypes) - if (NOT CMAKE_BUILD_TYPE STREQUAL Release) - list(APPEND AWS_C_FLAGS -fno-omit-frame-pointer) - endif() + list(APPEND AWS_C_FLAGS $<$>:-fno-omit-frame-pointer>) if(AWS_WARNINGS_ARE_ERRORS) list(APPEND AWS_C_FLAGS -Werror) @@ -222,25 +223,28 @@ function(aws_set_common_properties target) list(APPEND AWS_C_DEFINES_PRIVATE -DHAVE_SYSCONF) endif() - if(CMAKE_BUILD_TYPE STREQUAL "" OR CMAKE_BUILD_TYPE MATCHES Debug) - list(APPEND AWS_C_DEFINES_PRIVATE -DDEBUG_BUILD) - else() # release build + list(APPEND AWS_C_DEFINES_PRIVATE $<$:DEBUG_BUILD>) + + if ((NOT SET_PROPERTIES_NO_LTO) AND AWS_ENABLE_LTO) + # enable except in Debug builds + set(_ENABLE_LTO_EXPR $>) + + # try to check whether compiler supports LTO/IPO if (POLICY CMP0069) - if ((NOT SET_PROPERTIES_NO_LTO) AND AWS_ENABLE_LTO) - cmake_policy(SET CMP0069 NEW) # Enable LTO/IPO if available in the compiler - include(CheckIPOSupported OPTIONAL RESULT_VARIABLE ipo_check_exists) - if (ipo_check_exists) - check_ipo_supported(RESULT ipo_supported) - if (ipo_supported) - message(STATUS "Enabling IPO/LTO for Release builds") - set(AWS_ENABLE_LTO ON) - else() - message(STATUS "AWS_ENABLE_LTO is enabled, but cmake/compiler does not support it, disabling") - set(AWS_ENABLE_LTO OFF) - endif() + cmake_policy(SET CMP0069 NEW) + include(CheckIPOSupported OPTIONAL RESULT_VARIABLE ipo_check_exists) + if (ipo_check_exists) + check_ipo_supported(RESULT ipo_supported) + if (ipo_supported) + message(STATUS "Enabling IPO/LTO for Release builds") + else() + message(STATUS "AWS_ENABLE_LTO is enabled, but cmake/compiler does not support it, disabling") + set(_ENABLE_LTO_EXPR OFF) endif() endif() endif() + else() + set(_ENABLE_LTO_EXPR OFF) endif() if(BUILD_SHARED_LIBS) @@ -250,10 +254,14 @@ function(aws_set_common_properties target) endif() endif() + if(AWS_ENABLE_TRACING) + target_link_libraries(${target} PRIVATE ittnotify) + else() + # Disable intel notify api if tracing is not enabled + list(APPEND AWS_C_DEFINES_PRIVATE -DINTEL_NO_ITTNOTIFY_API) + endif() target_compile_options(${target} PRIVATE ${AWS_C_FLAGS}) target_compile_definitions(${target} PRIVATE ${AWS_C_DEFINES_PRIVATE} PUBLIC ${AWS_C_DEFINES_PUBLIC}) set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C C_STANDARD 99 C_STANDARD_REQUIRED ON) - if (AWS_ENABLE_LTO) - set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) - endif() + set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${_ENABLE_LTO_EXPR}>) endfunction() diff --git a/cmake/AwsFeatureTests.cmake b/cmake/AwsFeatureTests.cmake index 6b02be718..813a3c9f8 100644 --- a/cmake/AwsFeatureTests.cmake +++ b/cmake/AwsFeatureTests.cmake @@ -17,6 +17,10 @@ if(MINGW) set(USE_CPU_EXTENSIONS OFF) endif() +if (USE_CPU_EXTENSIONS) + set(AWS_USE_CPU_EXTENSIONS ON) +endif() + if(NOT CMAKE_CROSSCOMPILING) check_c_source_runs(" #include @@ -35,7 +39,7 @@ if(NOT CMAKE_CROSSCOMPILING) endif() check_c_source_compiles(" - #include + #include #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) int main() { return 0; @@ -54,6 +58,15 @@ check_c_source_compiles(" } " AWS_ARCH_INTEL) +check_c_source_compiles(" + int main() { +#if !(defined(__x86_64__) || defined(_M_X64)) +# error \"not intel\" +#endif + return 0; + } +" AWS_ARCH_INTEL_X64) + check_c_source_compiles(" int main() { #if !(defined(__aarch64__) || defined(_M_ARM64)) @@ -117,3 +130,7 @@ if(MSVC) return 0; }" AWS_HAVE_MSVC_INTRINSICS_X64) endif() + +# This does a lot to detect when intrinsics are available and has to set cflags to do so. +# leave it in its own file for ease of managing it. +include(AwsSIMD) diff --git a/cmake/AwsSIMD.cmake b/cmake/AwsSIMD.cmake index c69fc465e..65fce96c7 100644 --- a/cmake/AwsSIMD.cmake +++ b/cmake/AwsSIMD.cmake @@ -4,22 +4,39 @@ include(CheckCCompilerFlag) include(CheckIncludeFile) +if (MSVC) + set(AWS_AVX2_FLAG "/arch:AVX2") + set(AWS_AVX512_FLAG "/arch:AVX512") + set(AWS_AVX512vL_FLAG "") + set(AWS_CLMUL_FLAG "") + set(AWS_SSE4_2_FLAG "") + set(AWS_ARMv8_1_FLAG "/arch:arm8.1") + set(WERROR_FLAG "") +else() + set(AWS_AVX2_FLAG "-mavx -mavx2") + set(AWS_AVX512_FLAG "-mavx512f -mvpclmulqdq") + set(AWS_AVX512vL_FLAG "-mavx512vl") + set(AWS_CLMUL_FLAG "-mpclmul") + set(AWS_SSE4_2_FLAG "-msse4.2") + set(AWS_ARMv8_1_FLAG "-march=armv8-a+crc+crypto -mtune=neoverse-v1") + set(WERROR_FLAG "-Werror") +endif() + if (USE_CPU_EXTENSIONS) - if (MSVC) - check_c_compiler_flag("/arch:AVX2" HAVE_M_AVX2_FLAG) - if (HAVE_M_AVX2_FLAG) - set(AVX2_CFLAGS "/arch:AVX2") - endif() - else() - check_c_compiler_flag(-mavx2 HAVE_M_AVX2_FLAG) - if (HAVE_M_AVX2_FLAG) - set(AVX2_CFLAGS "-mavx -mavx2") - endif() + set(AVX_CFLAGS ${AWS_SSE4_2_FLAG}) + + check_c_compiler_flag(${AWS_AVX2_FLAG} HAVE_M_AVX2_FLAG) + if (HAVE_M_AVX2_FLAG) + set(AVX_CFLAGS "${AWS_AVX2_FLAG} ${AVX_CFLAGS}") endif() + check_c_compiler_flag("${AWS_AVX512_FLAG} ${AWS_CLMUL_FLAG}" HAVE_M_AVX512_FLAG) + if (HAVE_M_AVX512_FLAG) + set(AVX_CFLAGS "${AWS_AVX512_FLAG} ${AWS_CLMUL_FLAG} ${AVX_CFLAGS}") + endif() set(old_flags "${CMAKE_REQUIRED_FLAGS}") - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${AVX2_CFLAGS}") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${AVX_CFLAGS} ${WERROR_FLAG}") check_c_source_compiles(" #include @@ -35,7 +52,15 @@ if (USE_CPU_EXTENSIONS) _mm256_permutevar8x32_epi32(vec, vec); return 0; - }" HAVE_AVX2_INTRINSICS) + }" AWS_HAVE_AVX2_INTRINSICS) + + check_c_source_compiles(" + #include + + int main() { + __m512 a = _mm512_setzero_ps(); + return 0; + }" AWS_HAVE_AVX512_INTRINSICS) check_c_source_compiles(" #include @@ -45,30 +70,65 @@ if (USE_CPU_EXTENSIONS) __m256i vec; memset(&vec, 0, sizeof(vec)); return (int)_mm256_extract_epi64(vec, 2); - }" HAVE_MM256_EXTRACT_EPI64) + }" AWS_HAVE_MM256_EXTRACT_EPI64) + + check_c_source_compiles(" + #include + #include + int main() { + __m128i a = _mm_setzero_si128(); + __m128i b = _mm_setzero_si128(); + __m128i result = _mm_clmulepi64_si128(a, b, 0x00); + (void)result; + return 0; + }" AWS_HAVE_CLMUL) + + set(CMAKE_REQUIRED_FLAGS "${old_flags} ${AWS_ARMv8_1_FLAG} ${WERROR_FLAG}") + check_c_source_compiles(" + #include + int main() { + int crc = __crc32d(0, 1); + return 0; + }" AWS_HAVE_ARM32_CRC) + + check_c_source_compiles(" + #include + int main() { + _Atomic int var = 0; + atomic_fetch_add_explicit(&var, 1, memory_order_relaxed); + return 0; + }" AWS_HAVE_ARMv8_1) set(CMAKE_REQUIRED_FLAGS "${old_flags}") + endif() # USE_CPU_EXTENSIONS -macro(simd_add_definition_if target definition) - if(${definition}) - target_compile_definitions(${target} PRIVATE -D${definition}) - endif(${definition}) -endmacro(simd_add_definition_if) - -# Configure private preprocessor definitions for SIMD-related features -# Does not set any processor feature codegen flags -function(simd_add_definitions target) - simd_add_definition_if(${target} HAVE_AVX2_INTRINSICS) - simd_add_definition_if(${target} HAVE_MM256_EXTRACT_EPI64) -endfunction(simd_add_definitions) - -# Adds source files only if AVX2 is supported. These files will be built with -# avx2 intrinsics enabled. -# Usage: simd_add_source_avx2(target file1.c file2.c ...) -function(simd_add_source_avx2 target) +# The part where the definition is added to the compiler flags has been moved to config.h.in +# see git history for more details. + +# Adds AVX flags, if any, that are supported. These files will be built with +# available avx intrinsics enabled. +# Usage: simd_add_source_avx(target file1.c file2.c ...) +function(simd_add_source_avx target) foreach(file ${ARGN}) target_sources(${target} PRIVATE ${file}) - set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS "${AVX2_CFLAGS}") + set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS " ${AVX_CFLAGS}") + endforeach() +endfunction(simd_add_source_avx) + +# The part where the definition is added to the compiler flags has been moved to config.h.in +# see git history for more details. + +# Adds compiler flags to the source and adds the source to target. +# Unfortunately the flags have to be passed as strings. Predefined flags are +# at the top of this file. +# Usage: simd_append_source_and_features(target file1.c ${AWS_AVX512_FLAG} ${AWS_AVX2_FLAG} ...) +function(simd_append_source_and_features target file) + set(CC_FLAGS "") + foreach(flag ${ARGN}) + set(CC_FLAGS "${CC_FLAGS} ${flag}") endforeach() -endfunction(simd_add_source_avx2) + + target_sources(${target} PRIVATE ${file}) + set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS " ${CC_FLAGS}") +endfunction(simd_append_source_and_features) diff --git a/cmake/AwsSanitizers.cmake b/cmake/AwsSanitizers.cmake index c078575c1..54202006e 100644 --- a/cmake/AwsSanitizers.cmake +++ b/cmake/AwsSanitizers.cmake @@ -11,13 +11,15 @@ set(SANITIZERS "address;undefined" CACHE STRING "List of sanitizers to build wit # sanitizer: The sanitizer to check # out_variable: The variable to assign the result to. Defaults to HAS_SANITIZER_${sanitizer} function(aws_check_sanitizer sanitizer) - - if(NOT ${ARGN}) - set(out_variable "${ARGN}") - else() + list(LENGTH ARGN extra_count) + if(${extra_count} EQUAL 0) set(out_variable HAS_SANITIZER_${sanitizer}) # Sanitize the variable name to remove illegal characters string(MAKE_C_IDENTIFIER ${out_variable} out_variable) + elseif(${extra_count} EQUAL 1) + set(out_variable ${ARGN}) + else() + message(FATAL_ERROR "Error: aws_check_sanitizer() called with multiple out variables") endif() if(ENABLE_SANITIZERS) diff --git a/cmake/AwsTestHarness.cmake b/cmake/AwsTestHarness.cmake index 7eadfbd2b..484b66635 100644 --- a/cmake/AwsTestHarness.cmake +++ b/cmake/AwsTestHarness.cmake @@ -11,8 +11,8 @@ define_property(GLOBAL PROPERTY AWS_TEST_CASES BRIEF_DOCS "Test Cases" FULL_DOCS set(AWS_TEST_CASES "" CACHE INTERNAL "Test cases valid for this configuration") # The return value for the skipped test cases. Refer to the return code defined in aws_test_harness.h: -# #define SKIP (1) -set(SKIP_RETURN_CODE_VALUE 1) +# #define SKIP (103) +set(SKIP_RETURN_CODE_VALUE 103) # Registers a test case by name (the first argument to the AWS_TEST_CASE macro in aws_test_harness.h) macro(add_test_case name) @@ -54,18 +54,12 @@ function(generate_test_driver driver_exe_name) target_link_libraries(${driver_exe_name} PRIVATE ${PROJECT_NAME}) set_target_properties(${driver_exe_name} PROPERTIES LINKER_LANGUAGE C C_STANDARD 99) - if (MSVC) - if(STATIC_CRT) - target_compile_options(${driver_exe_name} PRIVATE "/MT$<$:d>") - else() - target_compile_options(${driver_exe_name} PRIVATE "/MD$<$:d>") - endif() - endif() target_compile_definitions(${driver_exe_name} PRIVATE AWS_UNSTABLE_TESTING_API=1) target_include_directories(${driver_exe_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR}) foreach(name IN LISTS TEST_CASES) add_test(${name} ${driver_exe_name} "${name}") + set_tests_properties("${name}" PROPERTIES SKIP_RETURN_CODE ${SKIP_RETURN_CODE_VALUE}) endforeach() # Clear test cases in case another driver needs to be generated @@ -80,7 +74,7 @@ function(generate_cpp_test_driver driver_exe_name) set_target_properties(${driver_exe_name} PROPERTIES LINKER_LANGUAGE CXX) if (MSVC) - if(STATIC_CRT) + if(AWS_STATIC_MSVC_RUNTIME_LIBRARY OR STATIC_CRT) target_compile_options(${driver_exe_name} PRIVATE "/MT$<$:d>") else() target_compile_options(${driver_exe_name} PRIVATE "/MD$<$:d>") diff --git a/format-check.py b/format-check.py new file mode 100755 index 000000000..b9e3520cc --- /dev/null +++ b/format-check.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +import argparse +import os +from pathlib import Path +import re +from subprocess import list2cmdline, run +from tempfile import NamedTemporaryFile + +CLANG_FORMAT_VERSION = '18.1.6' + +INCLUDE_REGEX = re.compile( + r'^(include|source|tests|verification)/.*\.(c|h|inl)$') +EXCLUDE_REGEX = re.compile(r'^$') + +arg_parser = argparse.ArgumentParser(description="Check with clang-format") +arg_parser.add_argument('-i', '--inplace-edit', action='store_true', + help="Edit files inplace") +args = arg_parser.parse_args() + +os.chdir(Path(__file__).parent) + +# create file containing list of all files to format +filepaths_file = NamedTemporaryFile(delete=False) +for dirpath, dirnames, filenames in os.walk('.'): + for filename in filenames: + # our regexes expect filepath to use forward slash + filepath = Path(dirpath, filename).as_posix() + if not INCLUDE_REGEX.match(filepath): + continue + if EXCLUDE_REGEX.match(filepath): + continue + + filepaths_file.write(f"{filepath}\n".encode()) +filepaths_file.close() + +# use pipx to run clang-format from PyPI +# this is a simple way to run the same clang-format version regardless of OS +cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}', + f'--files={filepaths_file.name}'] +if args.inplace_edit: + cmd += ['-i'] +else: + cmd += ['--Werror', '--dry-run'] + +print(f"{Path.cwd()}$ {list2cmdline(cmd)}") +if run(cmd).returncode: + exit(1) diff --git a/format-check.sh b/format-check.sh deleted file mode 100755 index d292e9d7f..000000000 --- a/format-check.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -if [[ -z $CLANG_FORMAT ]] ; then - CLANG_FORMAT=clang-format -fi - -if ! type $CLANG_FORMAT 2> /dev/null ; then - echo "No appropriate clang-format found." - exit 1 -fi - -FAIL=0 -SOURCE_FILES=`find source include tests verification -type f \( -name '*.h' -o -name '*.c' -o -name '*.inl' \)` -for i in $SOURCE_FILES -do - $CLANG_FORMAT -output-replacements-xml $i | grep -c " /dev/null - if [ $? -ne 1 ] - then - echo "$i failed clang-format check." - FAIL=1 - fi -done - -exit $FAIL diff --git a/include/aws/common/allocator.h b/include/aws/common/allocator.h index a9ba14fdc..8d092055f 100644 --- a/include/aws/common/allocator.h +++ b/include/aws/common/allocator.h @@ -12,6 +12,22 @@ AWS_PUSH_SANE_WARNING_LEVEL AWS_EXTERN_C_BEGIN +/* + * Quick guide to allocators: + * CRT offers several flavours of allocators: + * - default: basic allocator that invokes system one directly. + * - aligned: basic allocator that aligns small allocations on 8 byte + * boundary and big buffers on 32/64 byte (system dependent) boundary. + * Aligned mem can improve perf on some operations, like memcpy or hashes. + * Depending on a system, can result in higher peak memory count in heavy + * acquire/free scenarios (ex. s3), due to memory fragmentation related to how + * aligned allocators work (over allocate, find aligned offset, release extra memory) + * - wrapped_cf: wraps MacOS's Security Framework allocator. + * - mem_tracer: wraps any allocator and provides tracing functionality to allocations + * - small_block_allocator: pools smaller allocations into preallocated buckets. + * Not actively maintained. Avoid if possible. + */ + /* Allocator structure. An instance of this will be passed around for anything needing memory allocation */ struct aws_allocator { void *(*mem_acquire)(struct aws_allocator *allocator, size_t size); @@ -32,9 +48,16 @@ bool aws_allocator_is_valid(const struct aws_allocator *alloc); AWS_COMMON_API struct aws_allocator *aws_default_allocator(void); +/* + * Allocator that align small allocations on 8 byte boundary and big allocations + * on 32/64 byte boundary. + */ +AWS_COMMON_API +struct aws_allocator *aws_aligned_allocator(void); + #ifdef __MACH__ /* Avoid pulling in CoreFoundation headers in a header file. */ -struct __CFAllocator; +struct __CFAllocator; /* NOLINT(bugprone-reserved-identifier) */ typedef const struct __CFAllocator *CFAllocatorRef; /** diff --git a/include/aws/common/array_list.h b/include/aws/common/array_list.h index 493d3eaa3..4ef924456 100644 --- a/include/aws/common/array_list.h +++ b/include/aws/common/array_list.h @@ -12,7 +12,7 @@ AWS_PUSH_SANE_WARNING_LEVEL -#define AWS_ARRAY_LIST_DEBUG_FILL 0xDD +enum { AWS_ARRAY_LIST_DEBUG_FILL = 0xDD }; struct aws_array_list { struct aws_allocator *alloc; diff --git a/include/aws/common/atomics.h b/include/aws/common/atomics.h index ef64f985a..7f5445334 100644 --- a/include/aws/common/atomics.h +++ b/include/aws/common/atomics.h @@ -79,14 +79,12 @@ enum aws_memory_order { /** * Statically initializes an aws_atomic_var to a given size_t value. */ -#define AWS_ATOMIC_INIT_INT(x) \ - { .value = (void *)(uintptr_t)(x) } +#define AWS_ATOMIC_INIT_INT(x) {.value = (void *)(uintptr_t)(x)} /** * Statically initializes an aws_atomic_var to a given void * value. */ -#define AWS_ATOMIC_INIT_PTR(x) \ - { .value = (void *)(x) } +#define AWS_ATOMIC_INIT_PTR(x) {.value = (void *)(x)} AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/atomics_msvc.inl b/include/aws/common/atomics_msvc.inl index 516385f27..d1f3ba54c 100644 --- a/include/aws/common/atomics_msvc.inl +++ b/include/aws/common/atomics_msvc.inl @@ -20,8 +20,8 @@ AWS_EXTERN_C_BEGIN -#if !(defined(_M_IX86) || defined(_M_X64)) -# error Atomics are not currently supported for non-x86 MSVC platforms +#if !(defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64)) +# error Atomics are not currently supported for non-x86 or ARM64 MSVC platforms /* * In particular, it's not clear that seq_cst will work properly on non-x86 @@ -63,6 +63,24 @@ AWS_EXTERN_C_BEGIN * this use case. */ +/** + * Some general notes about ARM environments: + * ARM processors uses a weak memory model as opposed to the strong memory model used by Intel processors + * This means more permissible memory ordering allowed between stores and loads. + * + * Thus ARM port will need more hardware fences/barriers to assure developer intent. + * Memory barriers will prevent reordering stores and loads accross them depending on their type + * (read write, write only, read only ...) + * + * For more information about ARM64 memory ordering, + * see https://developer.arm.com/documentation/102336/0100/Memory-ordering + * For more information about Memory barriers, + * see https://developer.arm.com/documentation/102336/0100/Memory-barriers + * For more information about Miscosoft Interensic ARM64 APIs, + * see https://learn.microsoft.com/en-us/cpp/intrinsics/arm64-intrinsics?view=msvc-170 + * Note: wrt _Interlocked[Op]64 is the same for ARM64 and x64 processors + */ + #ifdef _M_IX86 # define AWS_INTERLOCKED_INT(x) _Interlocked##x typedef long aws_atomic_impl_int_t; @@ -71,6 +89,32 @@ typedef long aws_atomic_impl_int_t; typedef long long aws_atomic_impl_int_t; #endif +#ifdef _M_ARM64 +/* Hardware Read Write barrier, prevents all memory operations to cross the barrier in both directions */ +# define AWS_RW_BARRIER() __dmb(_ARM64_BARRIER_SY) +/* Hardware Read barrier, prevents all memory operations to cross the barrier upwards */ +# define AWS_R_BARRIER() __dmb(_ARM64_BARRIER_LD) +/* Hardware Write barrier, prevents all memory operations to cross the barrier downwards */ +# define AWS_W_BARRIER() __dmb(_ARM64_BARRIER_ST) +/* Software barrier, prevents the compiler from reodering the operations across the barrier */ +# define AWS_SW_BARRIER() _ReadWriteBarrier(); +#else +/* hardware barriers, do nothing on x86 since it has a strong memory model + * as described in the section above: some general notes + */ +# define AWS_RW_BARRIER() +# define AWS_R_BARRIER() +# define AWS_W_BARRIER() +/* + * x86: only a compiler barrier is required. For seq_cst, we must use some form of interlocked operation for + * writes, but that's the caller's responsibility. + * + * Volatile ops may or may not imply this barrier, depending on the /volatile: switch, but adding an extra + * barrier doesn't hurt. + */ +# define AWS_SW_BARRIER() _ReadWriteBarrier(); /* software barrier */ +#endif + static inline void aws_atomic_priv_check_order(enum aws_memory_order order) { #ifndef NDEBUG switch (order) { @@ -107,14 +151,8 @@ static inline void aws_atomic_priv_barrier_before(enum aws_memory_order order, e return; } - /* - * x86: only a compiler barrier is required. For seq_cst, we must use some form of interlocked operation for - * writes, but that's the caller's responsibility. - * - * Volatile ops may or may not imply this barrier, depending on the /volatile: switch, but adding an extra - * barrier doesn't hurt. - */ - _ReadWriteBarrier(); + AWS_RW_BARRIER(); + AWS_SW_BARRIER(); } static inline void aws_atomic_priv_barrier_after(enum aws_memory_order order, enum aws_atomic_mode_priv mode) { @@ -131,11 +169,8 @@ static inline void aws_atomic_priv_barrier_after(enum aws_memory_order order, en return; } - /* - * x86: only a compiler barrier is required. For seq_cst, we must use some form of interlocked operation for - * writes, but that's the caller's responsibility. - */ - _ReadWriteBarrier(); + AWS_RW_BARRIER(); + AWS_SW_BARRIER(); } /** @@ -344,9 +379,16 @@ void aws_atomic_thread_fence(enum aws_memory_order order) { AWS_INTERLOCKED_INT(Exchange)(&x, 1); break; case aws_memory_order_release: + AWS_W_BARRIER(); + AWS_SW_BARRIER(); + break; case aws_memory_order_acquire: + AWS_R_BARRIER(); + AWS_SW_BARRIER(); + break; case aws_memory_order_acq_rel: - _ReadWriteBarrier(); + AWS_RW_BARRIER(); + AWS_SW_BARRIER(); break; case aws_memory_order_relaxed: /* no-op */ @@ -354,6 +396,12 @@ void aws_atomic_thread_fence(enum aws_memory_order order) { } } +/* prevent conflicts with other files that might pick the same names */ +#undef AWS_RW_BARRIER +#undef AWS_R_BARRIER +#undef AWS_W_BARRIER +#undef AWS_SW_BARRIER + #define AWS_ATOMICS_HAVE_THREAD_FENCE AWS_EXTERN_C_END #endif diff --git a/include/aws/common/byte_buf.h b/include/aws/common/byte_buf.h index 17b0ae59b..11efd3f78 100644 --- a/include/aws/common/byte_buf.h +++ b/include/aws/common/byte_buf.h @@ -61,7 +61,7 @@ struct aws_byte_cursor { * Helper Macro for initializing a byte cursor from a string literal */ #define AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(literal) \ - { .ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1 } + {.ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1} /** * Signature for function argument to trim APIs @@ -135,6 +135,18 @@ AWS_COMMON_API int aws_byte_buf_init_copy( AWS_COMMON_API int aws_byte_buf_init_from_file(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename); +/** + * Same as aws_byte_buf_init_from_file(), but for reading "special files" like /proc/cpuinfo. + * These files don't accurately report their size, so size_hint is used as initial buffer size, + * and the buffer grows until the while file is read. + */ +AWS_COMMON_API +int aws_byte_buf_init_from_file_with_size_hint( + struct aws_byte_buf *out_buf, + struct aws_allocator *alloc, + const char *filename, + size_t size_hint); + /** * Evaluates the set of properties that define the shape of all valid aws_byte_buf structures. * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). diff --git a/include/aws/common/cbor.h b/include/aws/common/cbor.h new file mode 100644 index 000000000..1c220ecb7 --- /dev/null +++ b/include/aws/common/cbor.h @@ -0,0 +1,449 @@ +#ifndef AWS_COMMON_CBOR_H +#define AWS_COMMON_CBOR_H + +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include + +AWS_PUSH_SANE_WARNING_LEVEL +AWS_EXTERN_C_BEGIN + +/** + * The types use by APIs, not 1:1 with major type. + * It's an extension for cbor major type in RFC8949 section 3.1 + * Major type 0 - AWS_CBOR_TYPE_UINT + * Major type 1 - AWS_CBOR_TYPE_NEGINT + * Major type 2 - AWS_CBOR_TYPE_BYTES/AWS_CBOR_TYPE_INDEF_BYTES_START + * Major type 3 - AWS_CBOR_TYPE_TEXT/AWS_CBOR_TYPE_INDEF_TEXT_START + * Major type 4 - AWS_CBOR_TYPE_ARRAY_START/AWS_CBOR_TYPE_INDEF_ARRAY_START + * Major type 5 - AWS_CBOR_TYPE_MAP_START/AWS_CBOR_TYPE_INDEF_MAP_START + * Major type 6 - AWS_CBOR_TYPE_TAG + * Major type 7 + * - 20/21 - AWS_CBOR_TYPE_BOOL + * - 22 - AWS_CBOR_TYPE_NULL + * - 23 - AWS_CBOR_TYPE_UNDEFINED + * - 25/26/27 - AWS_CBOR_TYPE_FLOAT + * - 31 - AWS_CBOR_TYPE_BREAK + * - rest of value are not supported. + */ +enum aws_cbor_type { + AWS_CBOR_TYPE_UNKNOWN = 0, + + AWS_CBOR_TYPE_UINT, + AWS_CBOR_TYPE_NEGINT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_BYTES, + AWS_CBOR_TYPE_TEXT, + + AWS_CBOR_TYPE_ARRAY_START, + AWS_CBOR_TYPE_MAP_START, + + AWS_CBOR_TYPE_TAG, + + AWS_CBOR_TYPE_BOOL, + AWS_CBOR_TYPE_NULL, + AWS_CBOR_TYPE_UNDEFINED, + AWS_CBOR_TYPE_BREAK, + + AWS_CBOR_TYPE_INDEF_BYTES_START, + AWS_CBOR_TYPE_INDEF_TEXT_START, + AWS_CBOR_TYPE_INDEF_ARRAY_START, + AWS_CBOR_TYPE_INDEF_MAP_START, +}; + +/** + * The common tags, refer to RFC8949 section 3.4 + * Expected value type followed by the tag: + * AWS_CBOR_TAG_STANDARD_TIME - AWS_CBOR_TYPE_TEXT + * AWS_CBOR_TAG_EPOCH_TIME - AWS_CBOR_TYPE_UINT/AWS_CBOR_TYPE_NEGINT/AWS_CBOR_TYPE_FLOAT + * AWS_CBOR_TAG_UNSIGNED_BIGNUM - AWS_CBOR_TYPE_BYTES + * AWS_CBOR_TAG_NEGATIVE_BIGNUM - AWS_CBOR_TYPE_BYTES + * AWS_CBOR_TAG_DECIMAL_FRACTION - AWS_CBOR_TYPE_ARRAY_START/AWS_CBOR_TYPE_INDEF_ARRAY_START + **/ +#define AWS_CBOR_TAG_STANDARD_TIME 0 +#define AWS_CBOR_TAG_EPOCH_TIME 1 +#define AWS_CBOR_TAG_UNSIGNED_BIGNUM 2 +#define AWS_CBOR_TAG_NEGATIVE_BIGNUM 3 +#define AWS_CBOR_TAG_DECIMAL_FRACTION 4 + +struct aws_cbor_encoder; +struct aws_cbor_decoder; + +/******************************************************************************* + * ENCODE + ******************************************************************************/ + +/* Return c-string for aws_cbor_type */ +AWS_COMMON_API +const char *aws_cbor_type_cstr(enum aws_cbor_type type); + +/** + * @brief Create a new cbor encoder. Creating a encoder with a temporay buffer. + * Every aws_cbor_encoder_write_* will encode directly into the buffer to follow the encoded data. + * + * @param allocator + * @return aws_cbor_encoder + */ +AWS_COMMON_API +struct aws_cbor_encoder *aws_cbor_encoder_new(struct aws_allocator *allocator); + +AWS_COMMON_API +struct aws_cbor_encoder *aws_cbor_encoder_destroy(struct aws_cbor_encoder *encoder); + +/** + * @brief Get the current encoded data from encoder. The encoded data has the same lifetime as the encoder, and once + * any other function call invoked for the encoder, the encoded data is no longer valid. + * + * @param encoder + * @return struct aws_byte_cursor from the encoder buffer. + */ +AWS_COMMON_API +struct aws_byte_cursor aws_cbor_encoder_get_encoded_data(const struct aws_cbor_encoder *encoder); + +/** + * @brief Clear the current encoded buffer from encoder. + * + * @param encoder + */ +AWS_COMMON_API +void aws_cbor_encoder_reset(struct aws_cbor_encoder *encoder); + +/** + * @brief Encode a AWS_CBOR_TYPE_UINT value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1 + * + * TODO: maybe add a width of the encoded value. + * + * @param encoder + * @param value value to encode. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_uint(struct aws_cbor_encoder *encoder, uint64_t value); + +/** + * @brief Encode a AWS_CBOR_TYPE_NEGINT value to "smallest possible" in encoder's buffer. + * It represents (-1 - value). + * Referring to RFC8949 section 4.2.1 + * + * + * @param encoder + * @param value The argument to encode to negative integer, which is (-1 - expected_val) + */ +AWS_COMMON_API +void aws_cbor_encoder_write_negint(struct aws_cbor_encoder *encoder, uint64_t value); + +/** + * @brief Encode a AWS_CBOR_TYPE_FLOAT value to "smallest possible", but will not be encoded into half-precision float, + * as it's not well supported cross languages. + * + * To be more specific, it will be encoded into integer/negative/float + * (Order with priority) when the conversation will not cause precision loss. + * + * @param encoder + * @param value value to encode. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_float(struct aws_cbor_encoder *encoder, double value); + +/** + * @brief Encode a AWS_CBOR_TYPE_BYTES value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1, the length of "from" will be encoded first and then the value of "from" will + * be followed. + * + * @param encoder + * @param from value to encode. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_bytes(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from); + +/** + * @brief Encode a AWS_CBOR_TYPE_TEXT value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1, the length of "from" will be encoded first and then the value of "from" will + * be followed. + * + * @param encoder + * @param from value to encode. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_text(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from); + +/** + * @brief Encode a AWS_CBOR_TYPE_ARRAY_START value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1 + * The "number_entries" is the cbor data items should be followed as the content of the array. + * Notes: it's user's responsibility to keep the integrity of the array to be encoded. + * + * @param encoder + * @param number_entries The number of data item in array. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_array_start(struct aws_cbor_encoder *encoder, size_t number_entries); + +/** + * @brief Encode a AWS_CBOR_TYPE_MAP_START value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1 + * The "number_entries" is the number of pair of cbor data items as key and value should be followed as the content of + * the map. + * + * Notes: it's user's responsibility to keep the integrity of the map to be encoded. + * + * @param encoder + * @param number_entries The number of data item in map. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_map_start(struct aws_cbor_encoder *encoder, size_t number_entries); + +/** + * @brief Encode a AWS_CBOR_TYPE_TAG value to "smallest possible" in encoder's buffer. + * Referring to RFC8949 section 4.2.1 + * The following cbor data item will be the content of the tagged value. + * Notes: it's user's responsibility to keep the integrity of the tagged value to follow the RFC8949 section 3.4 + * + * @param encoder + * @param tag_number The tag value to encode. + */ +AWS_COMMON_API +void aws_cbor_encoder_write_tag(struct aws_cbor_encoder *encoder, uint64_t tag_number); + +/** + * @brief Encode a simple value AWS_CBOR_TYPE_NULL + * + * @param encoder + */ +AWS_COMMON_API +void aws_cbor_encoder_write_null(struct aws_cbor_encoder *encoder); + +/** + * @brief Encode a simple value AWS_CBOR_TYPE_UNDEFINED + * + * @param encoder + */ +AWS_COMMON_API +void aws_cbor_encoder_write_undefined(struct aws_cbor_encoder *encoder); + +/** + * @brief Encode a simple value AWS_CBOR_TYPE_BOOL + * + * @param encoder + */ +AWS_COMMON_API +void aws_cbor_encoder_write_bool(struct aws_cbor_encoder *encoder, bool value); + +/** + * @brief Encode a simple value AWS_CBOR_TYPE_BREAK + * + * Notes: no error checking, it's user's responsibility to track the break + * to close the corresponding indef_start + */ +AWS_COMMON_API +void aws_cbor_encoder_write_break(struct aws_cbor_encoder *encoder); + +/** + * @brief Encode a AWS_CBOR_TYPE_INDEF_BYTES_START + * + * Notes: no error checking, it's user's responsibility to add corresponding data and the break + * to close the indef_start + */ +AWS_COMMON_API +void aws_cbor_encoder_write_indef_bytes_start(struct aws_cbor_encoder *encoder); +/** + * @brief Encode a AWS_CBOR_TYPE_INDEF_TEXT_START + * + * Notes: no error checking, it's user's responsibility to add corresponding data + * and the break to close the indef_start + */ +AWS_COMMON_API +void aws_cbor_encoder_write_indef_text_start(struct aws_cbor_encoder *encoder); +/** + * @brief Encode a AWS_CBOR_TYPE_INDEF_ARRAY_START + * + * Notes: no error checking, it's user's responsibility to add corresponding data + * and the break to close the indef_start + */ +AWS_COMMON_API +void aws_cbor_encoder_write_indef_array_start(struct aws_cbor_encoder *encoder); +/** + * @brief Encode a AWS_CBOR_TYPE_INDEF_MAP_START + * + * Notes: no error checking, it's user's responsibility to add corresponding data + * and the break to close the indef_start + */ +AWS_COMMON_API +void aws_cbor_encoder_write_indef_map_start(struct aws_cbor_encoder *encoder); + +/******************************************************************************* + * DECODE + ******************************************************************************/ + +/** + * @brief Create a cbor decoder to take src to decode. + * The typical usage of decoder will be: + * - If the next element type only accept what expected, `aws_cbor_decoder_pop_next_*` + * - If the next element type accept different type, invoke `aws_cbor_decoder_peek_type` first, then based on the type + * to invoke corresponding `aws_cbor_decoder_pop_next_*` + * - If the next element type doesn't have corrsponding value, specifically: AWS_CBOR_TYPE_NULL, + * AWS_CBOR_TYPE_UNDEFINED, AWS_CBOR_TYPE_INF_*_START, AWS_CBOR_TYPE_BREAK, call + * `aws_cbor_decoder_consume_next_single_element` to consume it and continues for further decoding. + * - To ignore the next data item (the element and the content of it), `aws_cbor_decoder_consume_next_whole_data_item` + * + * Note: it's caller's responsibilty to keep the src outlive the decoder. + * + * @param allocator + * @param src The src data to decode from. + * @return decoder + */ +AWS_COMMON_API +struct aws_cbor_decoder *aws_cbor_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor src); + +AWS_COMMON_API +struct aws_cbor_decoder *aws_cbor_decoder_destroy(struct aws_cbor_decoder *decoder); + +/** + * @brief Get the length of the remaining bytes of the source. Once the source was decoded, it will be consumed, + * and result in decrease of the remaining length of bytes. + * + * @param decoder + * @return The length of bytes remaining of the decoder source. + */ +AWS_COMMON_API +size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *decoder); + +/** + * @brief Decode the next element and store it in the decoder cache if there was no element cached. + * If there was element cached, just return the type of the cached element. + * + * @param decoder + * @param out_type + * @return AWS_OP_SUCCESS if succeed, AWS_OP_ERR for any decoding error and corresponding error code will be raised. + */ +AWS_COMMON_API +int aws_cbor_decoder_peek_type(struct aws_cbor_decoder *decoder, enum aws_cbor_type *out_type); + +/** + * @brief Consume the next data item, includes all the content within the data item. + * + * As an example for the following cbor, this function will consume all the data + * as it's only one cbor data item, an indefinite map with 2 key, value pair: + * 0xbf6346756ef563416d7421ff + * BF -- Start indefinite-length map + * 63 -- First key, UTF-8 string length 3 + * 46756e -- "Fun" + * F5 -- First value, true + * 63 -- Second key, UTF-8 string length 3 + * 416d74 -- "Amt" + * 21 -- Second value, -2 + * FF -- "break" + * + * Notes: this function will not ensure the data item is well-formed. + * + * @param src The src to parse data from + * @return AWS_OP_SUCCESS successfully consumed the next data item, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_consume_next_whole_data_item(struct aws_cbor_decoder *decoder); + +/** + * @brief Consume the next single element, without the content followed by the element. + * + * As an example for the following cbor, this function will only consume the + * 0xBF, "Start indefinite-length map", not any content of the map represented. + * The next element to decode will start from 0x63 + * 0xbf6346756ef563416d7421ff + * BF -- Start indefinite-length map + * 63 -- First key, UTF-8 string length 3 + * 46756e -- "Fun" + * F5 -- First value, true + * 63 -- Second key, UTF-8 string length 3 + * 416d74 -- "Amt" + * 21 -- Second value, -2 + * FF -- "break" + * + * @param decoder The decoder to parse data from + * @return AWS_OP_SUCCESS successfully consumed the next element, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_consume_next_single_element(struct aws_cbor_decoder *decoder); + +/** + * @brief Get the next element based on the type. If the next element doesn't match the expected type. Error will be + * raised. If the next element already been cached, it will consume the cached item when no error was returned. + * Specifically: + * AWS_CBOR_TYPE_UINT - aws_cbor_decoder_pop_next_unsigned_int_val + * AWS_CBOR_TYPE_NEGINT - aws_cbor_decoder_pop_next_negative_int_val, it represents (-1 - *out) + * AWS_CBOR_TYPE_FLOAT - aws_cbor_decoder_pop_next_double_val + * AWS_CBOR_TYPE_BYTES - aws_cbor_decoder_pop_next_bytes_val + * AWS_CBOR_TYPE_TEXT - aws_cbor_decoder_pop_next_text_val + * + * @param decoder + * @param out + * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_pop_next_unsigned_int_val(struct aws_cbor_decoder *decoder, uint64_t *out); +AWS_COMMON_API +int aws_cbor_decoder_pop_next_negative_int_val(struct aws_cbor_decoder *decoder, uint64_t *out); +AWS_COMMON_API +int aws_cbor_decoder_pop_next_float_val(struct aws_cbor_decoder *decoder, double *out); +AWS_COMMON_API +int aws_cbor_decoder_pop_next_boolean_val(struct aws_cbor_decoder *decoder, bool *out); +AWS_COMMON_API +int aws_cbor_decoder_pop_next_bytes_val(struct aws_cbor_decoder *decoder, struct aws_byte_cursor *out); +AWS_COMMON_API +int aws_cbor_decoder_pop_next_text_val(struct aws_cbor_decoder *decoder, struct aws_byte_cursor *out); + +/** + * @brief Get the next AWS_CBOR_TYPE_ARRAY_START element. Only consume the AWS_CBOR_TYPE_ARRAY_START element and set the + * size of array to *out_size, not the content of the array. The next *out_size cbor data items will be the content of + * the array for a valid cbor data, + * + * Notes: For indefinite-length, this function will fail with "AWS_ERROR_CBOR_UNEXPECTED_TYPE". The designed way to + * handle indefinite-length is: + * - Get AWS_CBOR_TYPE_INDEF_ARRAY_START from _peek_type + * - call `aws_cbor_decoder_consume_next_single_element` to pop the indefinite-length start. + * - Decode the next data item until AWS_CBOR_TYPE_BREAK read. + * + * @param decoder + * @param out_size store the size of array if succeed. + * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_pop_next_array_start(struct aws_cbor_decoder *decoder, uint64_t *out_size); + +/** + * @brief Get the next AWS_CBOR_TYPE_MAP_START element. Only consume the AWS_CBOR_TYPE_MAP_START element and set the + * size of array to *out_size, not the content of the map. The next *out_size pair of cbor data items as key and value + * will be the content of the array for a valid cbor data, + * + * Notes: For indefinite-length, this function will fail with "AWS_ERROR_CBOR_UNEXPECTED_TYPE". The designed way to + * handle indefinite-length is: + * - Get AWS_CBOR_TYPE_INDEF_MAP_START from _peek_type + * - call `aws_cbor_decoder_consume_next_single_element` to pop the indefinite-length start. + * - Decode the next data item until AWS_CBOR_TYPE_BREAK read. + * + * @param decoder + * @param out_size store the size of map if succeed. + * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_pop_next_map_start(struct aws_cbor_decoder *decoder, uint64_t *out_size); + +/** + * @brief Get the next AWS_CBOR_TYPE_TAG element. Only consume the AWS_CBOR_TYPE_TAG element and set the + * tag value to *out_tag_val, not the content of the tagged. The next cbor data item will be the content of the tagged + * value for a valid cbor data. + * + * @param decoder + * @param out_size store the size of map if succeed. + * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_cbor_decoder_pop_next_tag_val(struct aws_cbor_decoder *decoder, uint64_t *out_tag_val); + +AWS_EXTERN_C_END +AWS_POP_SANE_WARNING_LEVEL + +#endif // AWS_COMMON_CBOR_H diff --git a/include/aws/common/condition_variable.h b/include/aws/common/condition_variable.h index 317dedb9c..82431dd4f 100644 --- a/include/aws/common/condition_variable.h +++ b/include/aws/common/condition_variable.h @@ -33,15 +33,13 @@ struct aws_condition_variable { * You can do something like struct aws_condition_variable var = * AWS_CONDITION_VARIABLE_INIT; * - * If on Windows and you get an error about AWS_CONDITION_VARIABLE_INIT being undefined, please include Windows.h to get + * If on Windows and you get an error about AWS_CONDITION_VARIABLE_INIT being undefined, please include windows.h to get * CONDITION_VARIABLE_INIT. */ #ifdef _WIN32 -# define AWS_CONDITION_VARIABLE_INIT \ - { .condition_handle = NULL, .initialized = true } +# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = NULL, .initialized = true} #else -# define AWS_CONDITION_VARIABLE_INIT \ - { .condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true } +# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/config.h.in b/include/aws/common/config.h.in index fd9be764d..381d99c99 100644 --- a/include/aws/common/config.h.in +++ b/include/aws/common/config.h.in @@ -19,5 +19,15 @@ #cmakedefine AWS_HAVE_EXECINFO #cmakedefine AWS_HAVE_WINAPI_DESKTOP #cmakedefine AWS_HAVE_LINUX_IF_LINK_H +#cmakedefine AWS_HAVE_AVX2_INTRINSICS +#cmakedefine AWS_HAVE_AVX512_INTRINSICS +#cmakedefine AWS_HAVE_MM256_EXTRACT_EPI64 +#cmakedefine AWS_HAVE_CLMUL +#cmakedefine AWS_HAVE_ARM32_CRC +#cmakedefine AWS_HAVE_ARMv8_1 +#cmakedefine AWS_ARCH_ARM64 +#cmakedefine AWS_ARCH_INTEL +#cmakedefine AWS_ARCH_INTEL_X64 +#cmakedefine AWS_USE_CPU_EXTENSIONS #endif diff --git a/include/aws/common/cpuid.h b/include/aws/common/cpuid.h index eb0ef6736..9ab7d5059 100644 --- a/include/aws/common/cpuid.h +++ b/include/aws/common/cpuid.h @@ -17,6 +17,9 @@ enum aws_cpu_feature_name { AWS_CPU_FEATURE_AVX512, AWS_CPU_FEATURE_ARM_CRC, AWS_CPU_FEATURE_BMI2, + AWS_CPU_FEATURE_VPCLMULQDQ, + AWS_CPU_FEATURE_ARM_PMULL, + AWS_CPU_FEATURE_ARM_CRYPTO, AWS_CPU_FEATURE_COUNT, }; diff --git a/include/aws/common/cross_process_lock.h b/include/aws/common/cross_process_lock.h new file mode 100644 index 000000000..99ecc5cb6 --- /dev/null +++ b/include/aws/common/cross_process_lock.h @@ -0,0 +1,35 @@ +#ifndef AWS_COMMON_CROSS_PROCESS_LOCK_H +#define AWS_COMMON_CROSS_PROCESS_LOCK_H +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include +#include + +struct aws_cross_process_lock; +AWS_EXTERN_C_BEGIN + +/** + * Attempts to acquire a system-wide (not per process or per user) lock scoped by instance_nonce. + * For any given unique nonce, a lock will be returned by the first caller. Subsequent calls will + * return NULL and raise AWS_ERROR_MUTEX_CALLER_NOT_OWNER + * until the either the process owning the lock exits or the program owning the lock + * calls aws_cross_process_lock_release() explicitly. + * + * If the process exits before the lock is released, the kernel will unlock it for the next consumer. + */ +AWS_COMMON_API +struct aws_cross_process_lock *aws_cross_process_lock_try_acquire( + struct aws_allocator *allocator, + struct aws_byte_cursor instance_nonce); + +/** + * Releases the lock so the next caller (may be another process) can get an instance of the lock. + */ +AWS_COMMON_API +void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock); + +AWS_EXTERN_C_END + +#endif /* AWS_COMMON_CROSS_PROCESS_LOCK_H */ diff --git a/include/aws/common/date_time.h b/include/aws/common/date_time.h index 1e38fa7d5..72ca09a86 100644 --- a/include/aws/common/date_time.h +++ b/include/aws/common/date_time.h @@ -10,8 +10,10 @@ AWS_PUSH_SANE_WARNING_LEVEL -#define AWS_DATE_TIME_STR_MAX_LEN 100 -#define AWS_DATE_TIME_STR_MAX_BASIC_LEN 20 +enum { + AWS_DATE_TIME_STR_MAX_LEN = 100, + AWS_DATE_TIME_STR_MAX_BASIC_LEN = 20, +}; struct aws_byte_buf; struct aws_byte_cursor; @@ -78,11 +80,14 @@ AWS_COMMON_API void aws_date_time_init_epoch_secs(struct aws_date_time *dt, doub * Initializes dt to be the time represented by date_str in format 'fmt'. Returns AWS_OP_SUCCESS if the * string was successfully parsed, returns AWS_OP_ERR if parsing failed. * + * The parser is lenient regarding AWS_DATE_FORMAT_ISO_8601 vs AWS_DATE_FORMAT_ISO_8601_BASIC. + * Regardless of which you pass in, both "2002-10-02T08:05:09Z" and "20021002T080509Z" would be accepted. + * * Notes for AWS_DATE_FORMAT_RFC822: * If no time zone information is provided, it is assumed to be local time (please don't do this). * - * If the time zone is something other than something indicating Universal Time (e.g. Z, UT, UTC, or GMT) or an offset - * from UTC (e.g. +0100, -0700), parsing will fail. + * Only time zones indicating Universal Time (e.g. Z, UT, UTC, or GMT), + * or offsets from UTC (e.g. +0100, -0700), are accepted. * * Really, it's just better if you always use Universal Time. */ diff --git a/include/aws/common/error.h b/include/aws/common/error.h index 2c103d763..a798b6677 100644 --- a/include/aws/common/error.h +++ b/include/aws/common/error.h @@ -20,7 +20,7 @@ AWS_PUSH_SANE_WARNING_LEVEL /* Each library gets space for 2^^10 error entries */ #define AWS_ERROR_ENUM_STRIDE_BITS 10 #define AWS_ERROR_ENUM_STRIDE (1U << AWS_ERROR_ENUM_STRIDE_BITS) -#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x)*AWS_ERROR_ENUM_STRIDE) +#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x) * AWS_ERROR_ENUM_STRIDE) #define AWS_ERROR_ENUM_END_RANGE(x) (((x) + 1) * AWS_ERROR_ENUM_STRIDE - 1) struct aws_error_info { @@ -38,7 +38,10 @@ struct aws_error_info_list { #define AWS_DEFINE_ERROR_INFO(C, ES, LN) \ { \ - .literal_name = #C, .error_code = (C), .error_str = (ES), .lib_name = (LN), \ + .literal_name = #C, \ + .error_code = (C), \ + .error_str = (ES), \ + .lib_name = (LN), \ .formatted_name = LN ": " #C ", " ES, \ } @@ -130,6 +133,14 @@ void aws_register_error_info(const struct aws_error_info_list *error_info); AWS_COMMON_API void aws_unregister_error_info(const struct aws_error_info_list *error_info); +/** + * Convert a c library io error into an aws error, and raise it. + * If no conversion is found, fallback_aws_error_code is raised. + * Always returns AWS_OP_ERR. + */ +AWS_COMMON_API +int aws_translate_and_raise_io_error_or(int error_no, int fallback_aws_error_code); + /** * Convert a c library io error into an aws error, and raise it. * If no conversion is found, AWS_ERROR_SYS_CALL_FAILURE is raised. @@ -202,6 +213,11 @@ enum aws_common_error { AWS_ERROR_INVALID_UTF8, AWS_ERROR_GET_HOME_DIRECTORY_FAILED, AWS_ERROR_INVALID_XML, + AWS_ERROR_FILE_OPEN_FAILURE, + AWS_ERROR_FILE_READ_FAILURE, + AWS_ERROR_FILE_WRITE_FAILURE, + AWS_ERROR_INVALID_CBOR, + AWS_ERROR_CBOR_UNEXPECTED_TYPE, AWS_ERROR_END_COMMON_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_COMMON_PACKAGE_ID) }; diff --git a/include/aws/common/external/ittnotify.h b/include/aws/common/external/ittnotify.h new file mode 100644 index 000000000..cde08c81f --- /dev/null +++ b/include/aws/common/external/ittnotify.h @@ -0,0 +1,4668 @@ +/* + Copyright (C) 2005-2019 Intel Corporation + + SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause +*/ +/* + Amazon.com has chosen to use this file under the terms of the BSD-3-Clause license. +*/ +#ifndef _ITTNOTIFY_H_ +#define _ITTNOTIFY_H_ + +/** +@file +@brief Public User API functions and types +@mainpage + +The Instrumentation and Tracing Technology API (ITT API) is used to +annotate a user's program with additional information +that can be used by correctness and performance tools. The user inserts +calls in their program. Those calls generate information that is collected +at runtime, and used by Intel(R) Threading Tools. + +@section API Concepts +The following general concepts are used throughout the API. + +@subsection Unicode Support +Many API functions take character string arguments. On Windows, there +are two versions of each such function. The function name is suffixed +by W if Unicode support is enabled, and by A otherwise. Any API function +that takes a character string argument adheres to this convention. + +@subsection Conditional Compilation +Many users prefer having an option to modify ITT API code when linking it +inside their runtimes. ITT API header file provides a mechanism to replace +ITT API function names inside your code with empty strings. To do this, +define the macros INTEL_NO_ITTNOTIFY_API during compilation and remove the +static library from the linker script. + +@subsection Domains +[see domains] +Domains provide a way to separate notification for different modules or +libraries in a program. Domains are specified by dotted character strings, +e.g. TBB.Internal.Control. + +A mechanism (to be specified) is provided to enable and disable +domains. By default, all domains are enabled. +@subsection Named Entities and Instances +Named entities (frames, regions, tasks, and markers) communicate +information about the program to the analysis tools. A named entity often +refers to a section of program code, or to some set of logical concepts +that the programmer wants to group together. + +Named entities relate to the programmer's static view of the program. When +the program actually executes, many instances of a given named entity +may be created. + +The API annotations denote instances of named entities. The actual +named entities are displayed using the analysis tools. In other words, +the named entities come into existence when instances are created. + +Instances of named entities may have instance identifiers (IDs). Some +API calls use instance identifiers to create relationships between +different instances of named entities. Other API calls associate data +with instances of named entities. + +Some named entities must always have instance IDs. In particular, regions +and frames always have IDs. Task and markers need IDs only if the ID is +needed in another API call (such as adding a relation or metadata). + +The lifetime of instance IDs is distinct from the lifetime of +instances. This allows various relationships to be specified separate +from the actual execution of instances. This flexibility comes at the +expense of extra API calls. + +The same ID may not be reused for different instances, unless a previous +[ref] __itt_id_destroy call for that ID has been issued. +*/ + +/** @cond exclude_from_documentation */ +#ifndef ITT_OS_WIN +# define ITT_OS_WIN 1 +#endif /* ITT_OS_WIN */ + +#ifndef ITT_OS_LINUX +# define ITT_OS_LINUX 2 +#endif /* ITT_OS_LINUX */ + +#ifndef ITT_OS_MAC +# define ITT_OS_MAC 3 +#endif /* ITT_OS_MAC */ + +#ifndef ITT_OS_FREEBSD +# define ITT_OS_FREEBSD 4 +#endif /* ITT_OS_FREEBSD */ + +#ifndef ITT_OS_OPENBSD +# define ITT_OS_OPENBSD 5 +#endif /* ITT_OS_OPENBSD */ + +#ifndef ITT_OS +# if defined WIN32 || defined _WIN32 +# define ITT_OS ITT_OS_WIN +# elif defined( __APPLE__ ) && defined( __MACH__ ) +# define ITT_OS ITT_OS_MAC +# elif defined( __FreeBSD__ ) +# define ITT_OS ITT_OS_FREEBSD +# elif defined( __OpenBSD__) +# define ITT_OS ITT_OS_OPENBSD +# else +# define ITT_OS ITT_OS_LINUX +# endif +#endif /* ITT_OS */ + +#ifndef ITT_PLATFORM_WIN +# define ITT_PLATFORM_WIN 1 +#endif /* ITT_PLATFORM_WIN */ + +#ifndef ITT_PLATFORM_POSIX +# define ITT_PLATFORM_POSIX 2 +#endif /* ITT_PLATFORM_POSIX */ + +#ifndef ITT_PLATFORM_MAC +# define ITT_PLATFORM_MAC 3 +#endif /* ITT_PLATFORM_MAC */ + +#ifndef ITT_PLATFORM_FREEBSD +# define ITT_PLATFORM_FREEBSD 4 +#endif /* ITT_PLATFORM_FREEBSD */ + +#ifndef ITT_PLATFORM_OPENBSD +# define ITT_PLATFORM_OPENBSD 5 +#endif /* ITT_PLATFORM_OPENBSD */ + +#ifndef ITT_PLATFORM +# if ITT_OS==ITT_OS_WIN +# define ITT_PLATFORM ITT_PLATFORM_WIN +# elif ITT_OS==ITT_OS_MAC +# define ITT_PLATFORM ITT_PLATFORM_MAC +# elif ITT_OS==ITT_OS_FREEBSD +# define ITT_PLATFORM ITT_PLATFORM_FREEBSD +# elif ITT_OS==ITT_OS_OPENBSD +# define ITT_PLATFORM ITT_PLATFORM_OPENBSD +# else +# define ITT_PLATFORM ITT_PLATFORM_POSIX +# endif +#endif /* ITT_PLATFORM */ + +#if defined(_UNICODE) && !defined(UNICODE) +#define UNICODE +#endif + +#include +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#include +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#include +#if defined(UNICODE) || defined(_UNICODE) +#include +#endif /* UNICODE || _UNICODE */ +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef ITTAPI_CDECL +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# define ITTAPI_CDECL __cdecl +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# if defined _M_IX86 || defined __i386__ +# define ITTAPI_CDECL __attribute__ ((cdecl)) +# else /* _M_IX86 || __i386__ */ +# define ITTAPI_CDECL /* actual only on x86 platform */ +# endif /* _M_IX86 || __i386__ */ +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* ITTAPI_CDECL */ + +#ifndef STDCALL +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# define STDCALL __stdcall +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# if defined _M_IX86 || defined __i386__ +# define STDCALL __attribute__ ((stdcall)) +# else /* _M_IX86 || __i386__ */ +# define STDCALL /* supported only on x86 platform */ +# endif /* _M_IX86 || __i386__ */ +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* STDCALL */ + +#define ITTAPI ITTAPI_CDECL +#define LIBITTAPI ITTAPI_CDECL + +/* TODO: Temporary for compatibility! */ +#define ITTAPI_CALL ITTAPI_CDECL +#define LIBITTAPI_CALL ITTAPI_CDECL + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +/* use __forceinline (VC++ specific) */ +#if defined(__MINGW32__) && !defined(__cplusplus) +#define ITT_INLINE static __inline__ __attribute__((__always_inline__,__gnu_inline__)) +#else +#define ITT_INLINE static __forceinline +#endif /* __MINGW32__ */ + +#define ITT_INLINE_ATTRIBUTE /* nothing */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +/* + * Generally, functions are not inlined unless optimization is specified. + * For functions declared inline, this attribute inlines the function even + * if no optimization level was specified. + */ +#ifdef __STRICT_ANSI__ +#define ITT_INLINE static +#define ITT_INLINE_ATTRIBUTE __attribute__((unused)) +#else /* __STRICT_ANSI__ */ +#define ITT_INLINE static inline +#define ITT_INLINE_ATTRIBUTE __attribute__((always_inline, unused)) +#endif /* __STRICT_ANSI__ */ +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +/** @endcond */ + +#ifdef INTEL_ITTNOTIFY_ENABLE_LEGACY +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# pragma message("WARNING!!! Deprecated API is used. Please undefine INTEL_ITTNOTIFY_ENABLE_LEGACY macro") +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# warning "Deprecated API is used. Please undefine INTEL_ITTNOTIFY_ENABLE_LEGACY macro" +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# include "legacy/ittnotify.h" +#endif /* INTEL_ITTNOTIFY_ENABLE_LEGACY */ + +/** @cond exclude_from_documentation */ +/* Helper macro for joining tokens */ +#define ITT_JOIN_AUX(p,n) p##n +#define ITT_JOIN(p,n) ITT_JOIN_AUX(p,n) + +#ifdef ITT_MAJOR +#undef ITT_MAJOR +#endif +#ifdef ITT_MINOR +#undef ITT_MINOR +#endif +#define ITT_MAJOR 3 +#define ITT_MINOR 0 + +/* Standard versioning of a token with major and minor version numbers */ +#define ITT_VERSIONIZE(x) \ + ITT_JOIN(x, \ + ITT_JOIN(_, \ + ITT_JOIN(ITT_MAJOR, \ + ITT_JOIN(_, ITT_MINOR)))) + +#ifndef INTEL_ITTNOTIFY_PREFIX +# define INTEL_ITTNOTIFY_PREFIX __itt_ +#endif /* INTEL_ITTNOTIFY_PREFIX */ +#ifndef INTEL_ITTNOTIFY_POSTFIX +# define INTEL_ITTNOTIFY_POSTFIX _ptr_ +#endif /* INTEL_ITTNOTIFY_POSTFIX */ + +#define ITTNOTIFY_NAME_AUX(n) ITT_JOIN(INTEL_ITTNOTIFY_PREFIX,n) +#define ITTNOTIFY_NAME(n) ITT_VERSIONIZE(ITTNOTIFY_NAME_AUX(ITT_JOIN(n,INTEL_ITTNOTIFY_POSTFIX))) + +#define ITTNOTIFY_VOID(n) (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n) +#define ITTNOTIFY_DATA(n) (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n) + +#define ITTNOTIFY_VOID_D0(n,d) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d) +#define ITTNOTIFY_VOID_D1(n,d,x) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x) +#define ITTNOTIFY_VOID_D2(n,d,x,y) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y) +#define ITTNOTIFY_VOID_D3(n,d,x,y,z) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z) +#define ITTNOTIFY_VOID_D4(n,d,x,y,z,a) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a) +#define ITTNOTIFY_VOID_D5(n,d,x,y,z,a,b) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b) +#define ITTNOTIFY_VOID_D6(n,d,x,y,z,a,b,c) (d == NULL) ? (void)0 : (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b,c) +#define ITTNOTIFY_DATA_D0(n,d) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d) +#define ITTNOTIFY_DATA_D1(n,d,x) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x) +#define ITTNOTIFY_DATA_D2(n,d,x,y) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y) +#define ITTNOTIFY_DATA_D3(n,d,x,y,z) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z) +#define ITTNOTIFY_DATA_D4(n,d,x,y,z,a) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a) +#define ITTNOTIFY_DATA_D5(n,d,x,y,z,a,b) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b) +#define ITTNOTIFY_DATA_D6(n,d,x,y,z,a,b,c) (d == NULL) ? 0 : (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b,c) + +#ifdef ITT_STUB +#undef ITT_STUB +#endif +#ifdef ITT_STUBV +#undef ITT_STUBV +#endif +#define ITT_STUBV(api,type,name,args) \ + typedef type (api* ITT_JOIN(ITTNOTIFY_NAME(name),_t)) args; \ + extern ITT_JOIN(ITTNOTIFY_NAME(name),_t) ITTNOTIFY_NAME(name); +#define ITT_STUB ITT_STUBV +/** @endcond */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** @cond exclude_from_gpa_documentation */ +/** + * @defgroup public Public API + * @{ + * @} + */ + +/** + * @defgroup control Collection Control + * @ingroup public + * General behavior: application continues to run, but no profiling information is being collected + * + * Pausing occurs not only for the current thread but for all process as well as spawned processes + * - Intel(R) Parallel Inspector and Intel(R) Inspector XE: + * - Does not analyze or report errors that involve memory access. + * - Other errors are reported as usual. Pausing data collection in + * Intel(R) Parallel Inspector and Intel(R) Inspector XE + * only pauses tracing and analyzing memory access. + * It does not pause tracing or analyzing threading APIs. + * . + * - Intel(R) Parallel Amplifier and Intel(R) VTune(TM) Amplifier XE: + * - Does continue to record when new threads are started. + * . + * - Other effects: + * - Possible reduction of runtime overhead. + * . + * @{ + */ +/** @brief Pause collection */ +void ITTAPI __itt_pause(void); +/** @brief Resume collection */ +void ITTAPI __itt_resume(void); +/** @brief Detach collection */ +void ITTAPI __itt_detach(void); + +/** + * @enum __itt_collection_scope + * @brief Enumerator for collection scopes + */ +typedef enum { + __itt_collection_scope_host = 1 << 0, + __itt_collection_scope_offload = 1 << 1, + __itt_collection_scope_all = 0x7FFFFFFF +} __itt_collection_scope; + +/** @brief Pause scoped collection */ +void ITTAPI __itt_pause_scoped(__itt_collection_scope); +/** @brief Resume scoped collection */ +void ITTAPI __itt_resume_scoped(__itt_collection_scope); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, pause, (void)) +ITT_STUBV(ITTAPI, void, pause_scoped, (__itt_collection_scope)) +ITT_STUBV(ITTAPI, void, resume, (void)) +ITT_STUBV(ITTAPI, void, resume_scoped, (__itt_collection_scope)) +ITT_STUBV(ITTAPI, void, detach, (void)) +#define __itt_pause ITTNOTIFY_VOID(pause) +#define __itt_pause_ptr ITTNOTIFY_NAME(pause) +#define __itt_pause_scoped ITTNOTIFY_VOID(pause_scoped) +#define __itt_pause_scoped_ptr ITTNOTIFY_NAME(pause_scoped) +#define __itt_resume ITTNOTIFY_VOID(resume) +#define __itt_resume_ptr ITTNOTIFY_NAME(resume) +#define __itt_resume_scoped ITTNOTIFY_VOID(resume_scoped) +#define __itt_resume_scoped_ptr ITTNOTIFY_NAME(resume_scoped) +#define __itt_detach ITTNOTIFY_VOID(detach) +#define __itt_detach_ptr ITTNOTIFY_NAME(detach) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_pause() +#define __itt_pause_ptr 0 +#define __itt_pause_scoped(scope) +#define __itt_pause_scoped_ptr 0 +#define __itt_resume() +#define __itt_resume_ptr 0 +#define __itt_resume_scoped(scope) +#define __itt_resume_scoped_ptr 0 +#define __itt_detach() +#define __itt_detach_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_pause_ptr 0 +#define __itt_pause_scoped_ptr 0 +#define __itt_resume_ptr 0 +#define __itt_resume_scoped_ptr 0 +#define __itt_detach_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} control group */ +/** @endcond */ + +/** + * @defgroup Intel Processor Trace control + * API from this group provides control over collection and analysis of Intel Processor Trace (Intel PT) data + * Information about Intel Processor Trace technology can be found here (Volume 3 chapter 35): + * https://software.intel.com/sites/default/files/managed/39/c5/325462-sdm-vol-1-2abcd-3abcd.pdf + * Use this API to mark particular code regions for loading detailed performance statistics. + * This mode makes your analysis faster and more accurate. + * @{ +*/ +typedef unsigned char __itt_pt_region; + +/** + * @brief function saves a region name marked with Intel PT API and returns a region id. + * Only 7 names can be registered. Attempts to register more names will be ignored and a region id with auto names will be returned. + * For automatic naming of regions pass NULL as function parameter +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_pt_region ITTAPI __itt_pt_region_createA(const char *name); +__itt_pt_region ITTAPI __itt_pt_region_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_pt_region_create __itt_pt_region_createW +#else /* UNICODE */ +# define __itt_pt_region_create __itt_pt_region_createA +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_pt_region ITTAPI __itt_pt_region_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_pt_region, pt_region_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_pt_region, pt_region_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_pt_region, pt_region_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_pt_region_createA ITTNOTIFY_DATA(pt_region_createA) +#define __itt_pt_region_createA_ptr ITTNOTIFY_NAME(pt_region_createA) +#define __itt_pt_region_createW ITTNOTIFY_DATA(pt_region_createW) +#define __itt_pt_region_createW_ptr ITTNOTIFY_NAME(pt_region_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_pt_region_create ITTNOTIFY_DATA(pt_region_create) +#define __itt_pt_region_create_ptr ITTNOTIFY_NAME(pt_region_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_pt_region_createA(name) (__itt_pt_region)0 +#define __itt_pt_region_createA_ptr 0 +#define __itt_pt_region_createW(name) (__itt_pt_region)0 +#define __itt_pt_region_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_pt_region_create(name) (__itt_pt_region)0 +#define __itt_pt_region_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_pt_region_createA_ptr 0 +#define __itt_pt_region_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_pt_region_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief function contains a special code pattern identified on the post-processing stage and + * marks the beginning of a code region targeted for Intel PT analysis + * @param[in] region - region id, 0 <= region < 8 +*/ +void __itt_mark_pt_region_begin(__itt_pt_region region); +/** + * @brief function contains a special code pattern identified on the post-processing stage and + * marks the end of a code region targeted for Intel PT analysis + * @param[in] region - region id, 0 <= region < 8 +*/ +void __itt_mark_pt_region_end(__itt_pt_region region); +/** @} Intel PT control group*/ + +/** + * @defgroup threads Threads + * @ingroup public + * Give names to threads + * @{ + */ +/** + * @brief Sets thread name of calling thread + * @param[in] name - name of thread + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_thread_set_nameA(const char *name); +void ITTAPI __itt_thread_set_nameW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_thread_set_name __itt_thread_set_nameW +# define __itt_thread_set_name_ptr __itt_thread_set_nameW_ptr +#else /* UNICODE */ +# define __itt_thread_set_name __itt_thread_set_nameA +# define __itt_thread_set_name_ptr __itt_thread_set_nameA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_thread_set_name(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, thread_set_nameA, (const char *name)) +ITT_STUBV(ITTAPI, void, thread_set_nameW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, thread_set_name, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA ITTNOTIFY_VOID(thread_set_nameA) +#define __itt_thread_set_nameA_ptr ITTNOTIFY_NAME(thread_set_nameA) +#define __itt_thread_set_nameW ITTNOTIFY_VOID(thread_set_nameW) +#define __itt_thread_set_nameW_ptr ITTNOTIFY_NAME(thread_set_nameW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name ITTNOTIFY_VOID(thread_set_name) +#define __itt_thread_set_name_ptr ITTNOTIFY_NAME(thread_set_name) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA(name) +#define __itt_thread_set_nameA_ptr 0 +#define __itt_thread_set_nameW(name) +#define __itt_thread_set_nameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name(name) +#define __itt_thread_set_name_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA_ptr 0 +#define __itt_thread_set_nameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** + * @brief Mark current thread as ignored from this point on, for the duration of its existence. + */ +void ITTAPI __itt_thread_ignore(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, thread_ignore, (void)) +#define __itt_thread_ignore ITTNOTIFY_VOID(thread_ignore) +#define __itt_thread_ignore_ptr ITTNOTIFY_NAME(thread_ignore) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_thread_ignore() +#define __itt_thread_ignore_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_thread_ignore_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} threads group */ + +/** + * @defgroup suppress Error suppression + * @ingroup public + * General behavior: application continues to run, but errors are suppressed + * + * @{ + */ + +/*****************************************************************//** + * @name group of functions used for error suppression in correctness tools + *********************************************************************/ +/** @{ */ +/** + * @hideinitializer + * @brief possible value for suppression mask + */ +#define __itt_suppress_all_errors 0x7fffffff + +/** + * @hideinitializer + * @brief possible value for suppression mask (suppresses errors from threading analysis) + */ +#define __itt_suppress_threading_errors 0x000000ff + +/** + * @hideinitializer + * @brief possible value for suppression mask (suppresses errors from memory analysis) + */ +#define __itt_suppress_memory_errors 0x0000ff00 + +/** + * @brief Start suppressing errors identified in mask on this thread + */ +void ITTAPI __itt_suppress_push(unsigned int mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_push, (unsigned int mask)) +#define __itt_suppress_push ITTNOTIFY_VOID(suppress_push) +#define __itt_suppress_push_ptr ITTNOTIFY_NAME(suppress_push) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_push(mask) +#define __itt_suppress_push_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_push_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Undo the effects of the matching call to __itt_suppress_push + */ +void ITTAPI __itt_suppress_pop(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_pop, (void)) +#define __itt_suppress_pop ITTNOTIFY_VOID(suppress_pop) +#define __itt_suppress_pop_ptr ITTNOTIFY_NAME(suppress_pop) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_pop() +#define __itt_suppress_pop_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_pop_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @enum __itt_suppress_mode + * @brief Enumerator for the suppressing modes + */ +typedef enum __itt_suppress_mode { + __itt_unsuppress_range, + __itt_suppress_range +} __itt_suppress_mode_t; + +/** + * @enum __itt_collection_state + * @brief Enumerator for collection state. + */ +typedef enum { + __itt_collection_uninitialized = 0, /* uninitialized */ + __itt_collection_init_fail = 1, /* failed to init */ + __itt_collection_collector_absent = 2, /* non work state collector is absent */ + __itt_collection_collector_exists = 3, /* work state collector exists */ + __itt_collection_init_successful = 4 /* success to init */ +} __itt_collection_state; + +/** + * @brief Mark a range of memory for error suppression or unsuppression for error types included in mask + */ +void ITTAPI __itt_suppress_mark_range(__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_mark_range, (__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size)) +#define __itt_suppress_mark_range ITTNOTIFY_VOID(suppress_mark_range) +#define __itt_suppress_mark_range_ptr ITTNOTIFY_NAME(suppress_mark_range) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_mark_range(mask) +#define __itt_suppress_mark_range_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_mark_range_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Undo the effect of a matching call to __itt_suppress_mark_range. If not matching + * call is found, nothing is changed. + */ +void ITTAPI __itt_suppress_clear_range(__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_clear_range, (__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size)) +#define __itt_suppress_clear_range ITTNOTIFY_VOID(suppress_clear_range) +#define __itt_suppress_clear_range_ptr ITTNOTIFY_NAME(suppress_clear_range) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_clear_range(mask) +#define __itt_suppress_clear_range_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_clear_range_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ +/** @} suppress group */ + +/** + * @defgroup sync Synchronization + * @ingroup public + * Indicate user-written synchronization code + * @{ + */ +/** + * @hideinitializer + * @brief possible value of attribute argument for sync object type + */ +#define __itt_attr_barrier 1 + +/** + * @hideinitializer + * @brief possible value of attribute argument for sync object type + */ +#define __itt_attr_mutex 2 + +/** +@brief Name a synchronization object +@param[in] addr Handle for the synchronization object. You should +use a real address to uniquely identify the synchronization object. +@param[in] objtype null-terminated object type string. If NULL is +passed, the name will be "User Synchronization". +@param[in] objname null-terminated object name string. If NULL, +no name will be assigned to the object. +@param[in] attribute one of [#__itt_attr_barrier, #__itt_attr_mutex] + */ + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_sync_createA(void *addr, const char *objtype, const char *objname, int attribute); +void ITTAPI __itt_sync_createW(void *addr, const wchar_t *objtype, const wchar_t *objname, int attribute); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_sync_create __itt_sync_createW +# define __itt_sync_create_ptr __itt_sync_createW_ptr +#else /* UNICODE */ +# define __itt_sync_create __itt_sync_createA +# define __itt_sync_create_ptr __itt_sync_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_sync_create (void *addr, const char *objtype, const char *objname, int attribute); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, sync_createA, (void *addr, const char *objtype, const char *objname, int attribute)) +ITT_STUBV(ITTAPI, void, sync_createW, (void *addr, const wchar_t *objtype, const wchar_t *objname, int attribute)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, sync_create, (void *addr, const char* objtype, const char* objname, int attribute)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA ITTNOTIFY_VOID(sync_createA) +#define __itt_sync_createA_ptr ITTNOTIFY_NAME(sync_createA) +#define __itt_sync_createW ITTNOTIFY_VOID(sync_createW) +#define __itt_sync_createW_ptr ITTNOTIFY_NAME(sync_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create ITTNOTIFY_VOID(sync_create) +#define __itt_sync_create_ptr ITTNOTIFY_NAME(sync_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA(addr, objtype, objname, attribute) +#define __itt_sync_createA_ptr 0 +#define __itt_sync_createW(addr, objtype, objname, attribute) +#define __itt_sync_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create(addr, objtype, objname, attribute) +#define __itt_sync_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA_ptr 0 +#define __itt_sync_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** +@brief Rename a synchronization object + +You can use the rename call to assign or reassign a name to a given +synchronization object. +@param[in] addr handle for the synchronization object. +@param[in] name null-terminated object name string. +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_sync_renameA(void *addr, const char *name); +void ITTAPI __itt_sync_renameW(void *addr, const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_sync_rename __itt_sync_renameW +# define __itt_sync_rename_ptr __itt_sync_renameW_ptr +#else /* UNICODE */ +# define __itt_sync_rename __itt_sync_renameA +# define __itt_sync_rename_ptr __itt_sync_renameA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_sync_rename(void *addr, const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, sync_renameA, (void *addr, const char *name)) +ITT_STUBV(ITTAPI, void, sync_renameW, (void *addr, const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, sync_rename, (void *addr, const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA ITTNOTIFY_VOID(sync_renameA) +#define __itt_sync_renameA_ptr ITTNOTIFY_NAME(sync_renameA) +#define __itt_sync_renameW ITTNOTIFY_VOID(sync_renameW) +#define __itt_sync_renameW_ptr ITTNOTIFY_NAME(sync_renameW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename ITTNOTIFY_VOID(sync_rename) +#define __itt_sync_rename_ptr ITTNOTIFY_NAME(sync_rename) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA(addr, name) +#define __itt_sync_renameA_ptr 0 +#define __itt_sync_renameW(addr, name) +#define __itt_sync_renameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename(addr, name) +#define __itt_sync_rename_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA_ptr 0 +#define __itt_sync_renameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + @brief Destroy a synchronization object. + @param addr Handle for the synchronization object. + */ +void ITTAPI __itt_sync_destroy(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_destroy, (void *addr)) +#define __itt_sync_destroy ITTNOTIFY_VOID(sync_destroy) +#define __itt_sync_destroy_ptr ITTNOTIFY_NAME(sync_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_destroy(addr) +#define __itt_sync_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/*****************************************************************//** + * @name group of functions is used for performance measurement tools + *********************************************************************/ +/** @{ */ +/** + * @brief Enter spin loop on user-defined sync object + */ +void ITTAPI __itt_sync_prepare(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_prepare, (void *addr)) +#define __itt_sync_prepare ITTNOTIFY_VOID(sync_prepare) +#define __itt_sync_prepare_ptr ITTNOTIFY_NAME(sync_prepare) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_prepare(addr) +#define __itt_sync_prepare_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_prepare_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Quit spin loop without acquiring spin object + */ +void ITTAPI __itt_sync_cancel(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_cancel, (void *addr)) +#define __itt_sync_cancel ITTNOTIFY_VOID(sync_cancel) +#define __itt_sync_cancel_ptr ITTNOTIFY_NAME(sync_cancel) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_cancel(addr) +#define __itt_sync_cancel_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_cancel_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Successful spin loop completion (sync object acquired) + */ +void ITTAPI __itt_sync_acquired(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_acquired, (void *addr)) +#define __itt_sync_acquired ITTNOTIFY_VOID(sync_acquired) +#define __itt_sync_acquired_ptr ITTNOTIFY_NAME(sync_acquired) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_acquired(addr) +#define __itt_sync_acquired_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_acquired_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Start sync object releasing code. Is called before the lock release call. + */ +void ITTAPI __itt_sync_releasing(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_releasing, (void *addr)) +#define __itt_sync_releasing ITTNOTIFY_VOID(sync_releasing) +#define __itt_sync_releasing_ptr ITTNOTIFY_NAME(sync_releasing) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_releasing(addr) +#define __itt_sync_releasing_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_releasing_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ + +/** @} sync group */ + +/**************************************************************//** + * @name group of functions is used for correctness checking tools + ******************************************************************/ +/** @{ */ +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_prepare(void* addr); + */ +void ITTAPI __itt_fsync_prepare(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_prepare, (void *addr)) +#define __itt_fsync_prepare ITTNOTIFY_VOID(fsync_prepare) +#define __itt_fsync_prepare_ptr ITTNOTIFY_NAME(fsync_prepare) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_prepare(addr) +#define __itt_fsync_prepare_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_prepare_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_cancel(void *addr); + */ +void ITTAPI __itt_fsync_cancel(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_cancel, (void *addr)) +#define __itt_fsync_cancel ITTNOTIFY_VOID(fsync_cancel) +#define __itt_fsync_cancel_ptr ITTNOTIFY_NAME(fsync_cancel) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_cancel(addr) +#define __itt_fsync_cancel_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_cancel_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_acquired(void *addr); + */ +void ITTAPI __itt_fsync_acquired(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_acquired, (void *addr)) +#define __itt_fsync_acquired ITTNOTIFY_VOID(fsync_acquired) +#define __itt_fsync_acquired_ptr ITTNOTIFY_NAME(fsync_acquired) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_acquired(addr) +#define __itt_fsync_acquired_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_acquired_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_releasing(void* addr); + */ +void ITTAPI __itt_fsync_releasing(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_releasing, (void *addr)) +#define __itt_fsync_releasing ITTNOTIFY_VOID(fsync_releasing) +#define __itt_fsync_releasing_ptr ITTNOTIFY_NAME(fsync_releasing) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_releasing(addr) +#define __itt_fsync_releasing_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_releasing_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ + +/** + * @defgroup model Modeling by Intel(R) Parallel Advisor + * @ingroup public + * This is the subset of itt used for modeling by Intel(R) Parallel Advisor. + * This API is called ONLY using annotate.h, by "Annotation" macros + * the user places in their sources during the parallelism modeling steps. + * + * site_begin/end and task_begin/end take the address of handle variables, + * which are writeable by the API. Handles must be 0 initialized prior + * to the first call to begin, or may cause a run-time failure. + * The handles are initialized in a multi-thread safe way by the API if + * the handle is 0. The commonly expected idiom is one static handle to + * identify a site or task. If a site or task of the same name has already + * been started during this collection, the same handle MAY be returned, + * but is not required to be - it is unspecified if data merging is done + * based on name. These routines also take an instance variable. Like + * the lexical instance, these must be 0 initialized. Unlike the lexical + * instance, this is used to track a single dynamic instance. + * + * API used by the Intel(R) Parallel Advisor to describe potential concurrency + * and related activities. User-added source annotations expand to calls + * to these procedures to enable modeling of a hypothetical concurrent + * execution serially. + * @{ + */ +#if !defined(_ADVISOR_ANNOTATE_H_) || defined(ANNOTATE_EXPAND_NULL) + +typedef void* __itt_model_site; /*!< @brief handle for lexical site */ +typedef void* __itt_model_site_instance; /*!< @brief handle for dynamic instance */ +typedef void* __itt_model_task; /*!< @brief handle for lexical site */ +typedef void* __itt_model_task_instance; /*!< @brief handle for dynamic instance */ + +/** + * @enum __itt_model_disable + * @brief Enumerator for the disable methods + */ +typedef enum { + __itt_model_disable_observation, + __itt_model_disable_collection +} __itt_model_disable; + +#endif /* !_ADVISOR_ANNOTATE_H_ || ANNOTATE_EXPAND_NULL */ + +/** + * @brief ANNOTATE_SITE_BEGIN/ANNOTATE_SITE_END support. + * + * site_begin/end model a potential concurrency site. + * site instances may be recursively nested with themselves. + * site_end exits the most recently started but unended site for the current + * thread. The handle passed to end may be used to validate structure. + * Instances of a site encountered on different threads concurrently + * are considered completely distinct. If the site name for two different + * lexical sites match, it is unspecified whether they are treated as the + * same or different for data presentation. + */ +void ITTAPI __itt_model_site_begin(__itt_model_site *site, __itt_model_site_instance *instance, const char *name); +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_model_site_beginW(const wchar_t *name); +#endif +void ITTAPI __itt_model_site_beginA(const char *name); +void ITTAPI __itt_model_site_beginAL(const char *name, size_t siteNameLen); +void ITTAPI __itt_model_site_end (__itt_model_site *site, __itt_model_site_instance *instance); +void ITTAPI __itt_model_site_end_2(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_site_begin, (__itt_model_site *site, __itt_model_site_instance *instance, const char *name)) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, model_site_beginW, (const wchar_t *name)) +#endif +ITT_STUBV(ITTAPI, void, model_site_beginA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_site_beginAL, (const char *name, size_t siteNameLen)) +ITT_STUBV(ITTAPI, void, model_site_end, (__itt_model_site *site, __itt_model_site_instance *instance)) +ITT_STUBV(ITTAPI, void, model_site_end_2, (void)) +#define __itt_model_site_begin ITTNOTIFY_VOID(model_site_begin) +#define __itt_model_site_begin_ptr ITTNOTIFY_NAME(model_site_begin) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW ITTNOTIFY_VOID(model_site_beginW) +#define __itt_model_site_beginW_ptr ITTNOTIFY_NAME(model_site_beginW) +#endif +#define __itt_model_site_beginA ITTNOTIFY_VOID(model_site_beginA) +#define __itt_model_site_beginA_ptr ITTNOTIFY_NAME(model_site_beginA) +#define __itt_model_site_beginAL ITTNOTIFY_VOID(model_site_beginAL) +#define __itt_model_site_beginAL_ptr ITTNOTIFY_NAME(model_site_beginAL) +#define __itt_model_site_end ITTNOTIFY_VOID(model_site_end) +#define __itt_model_site_end_ptr ITTNOTIFY_NAME(model_site_end) +#define __itt_model_site_end_2 ITTNOTIFY_VOID(model_site_end_2) +#define __itt_model_site_end_2_ptr ITTNOTIFY_NAME(model_site_end_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_site_begin(site, instance, name) +#define __itt_model_site_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW(name) +#define __itt_model_site_beginW_ptr 0 +#endif +#define __itt_model_site_beginA(name) +#define __itt_model_site_beginA_ptr 0 +#define __itt_model_site_beginAL(name, siteNameLen) +#define __itt_model_site_beginAL_ptr 0 +#define __itt_model_site_end(site, instance) +#define __itt_model_site_end_ptr 0 +#define __itt_model_site_end_2() +#define __itt_model_site_end_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_site_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW_ptr 0 +#endif +#define __itt_model_site_beginA_ptr 0 +#define __itt_model_site_beginAL_ptr 0 +#define __itt_model_site_end_ptr 0 +#define __itt_model_site_end_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_TASK_BEGIN/ANNOTATE_TASK_END support + * + * task_begin/end model a potential task, which is contained within the most + * closely enclosing dynamic site. task_end exits the most recently started + * but unended task. The handle passed to end may be used to validate + * structure. It is unspecified if bad dynamic nesting is detected. If it + * is, it should be encoded in the resulting data collection. The collector + * should not fail due to construct nesting issues, nor attempt to directly + * indicate the problem. + */ +void ITTAPI __itt_model_task_begin(__itt_model_task *task, __itt_model_task_instance *instance, const char *name); +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_model_task_beginW(const wchar_t *name); +void ITTAPI __itt_model_iteration_taskW(const wchar_t *name); +#endif +void ITTAPI __itt_model_task_beginA(const char *name); +void ITTAPI __itt_model_task_beginAL(const char *name, size_t taskNameLen); +void ITTAPI __itt_model_iteration_taskA(const char *name); +void ITTAPI __itt_model_iteration_taskAL(const char *name, size_t taskNameLen); +void ITTAPI __itt_model_task_end (__itt_model_task *task, __itt_model_task_instance *instance); +void ITTAPI __itt_model_task_end_2(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_task_begin, (__itt_model_task *task, __itt_model_task_instance *instance, const char *name)) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, model_task_beginW, (const wchar_t *name)) +ITT_STUBV(ITTAPI, void, model_iteration_taskW, (const wchar_t *name)) +#endif +ITT_STUBV(ITTAPI, void, model_task_beginA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_task_beginAL, (const char *name, size_t taskNameLen)) +ITT_STUBV(ITTAPI, void, model_iteration_taskA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_iteration_taskAL, (const char *name, size_t taskNameLen)) +ITT_STUBV(ITTAPI, void, model_task_end, (__itt_model_task *task, __itt_model_task_instance *instance)) +ITT_STUBV(ITTAPI, void, model_task_end_2, (void)) +#define __itt_model_task_begin ITTNOTIFY_VOID(model_task_begin) +#define __itt_model_task_begin_ptr ITTNOTIFY_NAME(model_task_begin) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW ITTNOTIFY_VOID(model_task_beginW) +#define __itt_model_task_beginW_ptr ITTNOTIFY_NAME(model_task_beginW) +#define __itt_model_iteration_taskW ITTNOTIFY_VOID(model_iteration_taskW) +#define __itt_model_iteration_taskW_ptr ITTNOTIFY_NAME(model_iteration_taskW) +#endif +#define __itt_model_task_beginA ITTNOTIFY_VOID(model_task_beginA) +#define __itt_model_task_beginA_ptr ITTNOTIFY_NAME(model_task_beginA) +#define __itt_model_task_beginAL ITTNOTIFY_VOID(model_task_beginAL) +#define __itt_model_task_beginAL_ptr ITTNOTIFY_NAME(model_task_beginAL) +#define __itt_model_iteration_taskA ITTNOTIFY_VOID(model_iteration_taskA) +#define __itt_model_iteration_taskA_ptr ITTNOTIFY_NAME(model_iteration_taskA) +#define __itt_model_iteration_taskAL ITTNOTIFY_VOID(model_iteration_taskAL) +#define __itt_model_iteration_taskAL_ptr ITTNOTIFY_NAME(model_iteration_taskAL) +#define __itt_model_task_end ITTNOTIFY_VOID(model_task_end) +#define __itt_model_task_end_ptr ITTNOTIFY_NAME(model_task_end) +#define __itt_model_task_end_2 ITTNOTIFY_VOID(model_task_end_2) +#define __itt_model_task_end_2_ptr ITTNOTIFY_NAME(model_task_end_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_task_begin(task, instance, name) +#define __itt_model_task_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW(name) +#define __itt_model_task_beginW_ptr 0 +#endif +#define __itt_model_task_beginA(name) +#define __itt_model_task_beginA_ptr 0 +#define __itt_model_task_beginAL(name, siteNameLen) +#define __itt_model_task_beginAL_ptr 0 +#define __itt_model_iteration_taskA(name) +#define __itt_model_iteration_taskA_ptr 0 +#define __itt_model_iteration_taskAL(name, siteNameLen) +#define __itt_model_iteration_taskAL_ptr 0 +#define __itt_model_task_end(task, instance) +#define __itt_model_task_end_ptr 0 +#define __itt_model_task_end_2() +#define __itt_model_task_end_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_task_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW_ptr 0 +#endif +#define __itt_model_task_beginA_ptr 0 +#define __itt_model_task_beginAL_ptr 0 +#define __itt_model_iteration_taskA_ptr 0 +#define __itt_model_iteration_taskAL_ptr 0 +#define __itt_model_task_end_ptr 0 +#define __itt_model_task_end_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_LOCK_ACQUIRE/ANNOTATE_LOCK_RELEASE support + * + * lock_acquire/release model a potential lock for both lockset and + * performance modeling. Each unique address is modeled as a separate + * lock, with invalid addresses being valid lock IDs. Specifically: + * no storage is accessed by the API at the specified address - it is only + * used for lock identification. Lock acquires may be self-nested and are + * unlocked by a corresponding number of releases. + * (These closely correspond to __itt_sync_acquired/__itt_sync_releasing, + * but may not have identical semantics.) + */ +void ITTAPI __itt_model_lock_acquire(void *lock); +void ITTAPI __itt_model_lock_acquire_2(void *lock); +void ITTAPI __itt_model_lock_release(void *lock); +void ITTAPI __itt_model_lock_release_2(void *lock); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_lock_acquire, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_acquire_2, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_release, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_release_2, (void *lock)) +#define __itt_model_lock_acquire ITTNOTIFY_VOID(model_lock_acquire) +#define __itt_model_lock_acquire_ptr ITTNOTIFY_NAME(model_lock_acquire) +#define __itt_model_lock_acquire_2 ITTNOTIFY_VOID(model_lock_acquire_2) +#define __itt_model_lock_acquire_2_ptr ITTNOTIFY_NAME(model_lock_acquire_2) +#define __itt_model_lock_release ITTNOTIFY_VOID(model_lock_release) +#define __itt_model_lock_release_ptr ITTNOTIFY_NAME(model_lock_release) +#define __itt_model_lock_release_2 ITTNOTIFY_VOID(model_lock_release_2) +#define __itt_model_lock_release_2_ptr ITTNOTIFY_NAME(model_lock_release_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_lock_acquire(lock) +#define __itt_model_lock_acquire_ptr 0 +#define __itt_model_lock_acquire_2(lock) +#define __itt_model_lock_acquire_2_ptr 0 +#define __itt_model_lock_release(lock) +#define __itt_model_lock_release_ptr 0 +#define __itt_model_lock_release_2(lock) +#define __itt_model_lock_release_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_lock_acquire_ptr 0 +#define __itt_model_lock_acquire_2_ptr 0 +#define __itt_model_lock_release_ptr 0 +#define __itt_model_lock_release_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_RECORD_ALLOCATION/ANNOTATE_RECORD_DEALLOCATION support + * + * record_allocation/deallocation describe user-defined memory allocator + * behavior, which may be required for correctness modeling to understand + * when storage is not expected to be actually reused across threads. + */ +void ITTAPI __itt_model_record_allocation (void *addr, size_t size); +void ITTAPI __itt_model_record_deallocation(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_record_allocation, (void *addr, size_t size)) +ITT_STUBV(ITTAPI, void, model_record_deallocation, (void *addr)) +#define __itt_model_record_allocation ITTNOTIFY_VOID(model_record_allocation) +#define __itt_model_record_allocation_ptr ITTNOTIFY_NAME(model_record_allocation) +#define __itt_model_record_deallocation ITTNOTIFY_VOID(model_record_deallocation) +#define __itt_model_record_deallocation_ptr ITTNOTIFY_NAME(model_record_deallocation) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_record_allocation(addr, size) +#define __itt_model_record_allocation_ptr 0 +#define __itt_model_record_deallocation(addr) +#define __itt_model_record_deallocation_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_record_allocation_ptr 0 +#define __itt_model_record_deallocation_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_INDUCTION_USES support + * + * Note particular storage is inductive through the end of the current site + */ +void ITTAPI __itt_model_induction_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_induction_uses, (void *addr, size_t size)) +#define __itt_model_induction_uses ITTNOTIFY_VOID(model_induction_uses) +#define __itt_model_induction_uses_ptr ITTNOTIFY_NAME(model_induction_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_induction_uses(addr, size) +#define __itt_model_induction_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_induction_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_REDUCTION_USES support + * + * Note particular storage is used for reduction through the end + * of the current site + */ +void ITTAPI __itt_model_reduction_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_reduction_uses, (void *addr, size_t size)) +#define __itt_model_reduction_uses ITTNOTIFY_VOID(model_reduction_uses) +#define __itt_model_reduction_uses_ptr ITTNOTIFY_NAME(model_reduction_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_reduction_uses(addr, size) +#define __itt_model_reduction_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_reduction_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_OBSERVE_USES support + * + * Have correctness modeling record observations about uses of storage + * through the end of the current site + */ +void ITTAPI __itt_model_observe_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_observe_uses, (void *addr, size_t size)) +#define __itt_model_observe_uses ITTNOTIFY_VOID(model_observe_uses) +#define __itt_model_observe_uses_ptr ITTNOTIFY_NAME(model_observe_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_observe_uses(addr, size) +#define __itt_model_observe_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_observe_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_CLEAR_USES support + * + * Clear the special handling of a piece of storage related to induction, + * reduction or observe_uses + */ +void ITTAPI __itt_model_clear_uses(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_clear_uses, (void *addr)) +#define __itt_model_clear_uses ITTNOTIFY_VOID(model_clear_uses) +#define __itt_model_clear_uses_ptr ITTNOTIFY_NAME(model_clear_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_clear_uses(addr) +#define __itt_model_clear_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_clear_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_DISABLE_*_PUSH/ANNOTATE_DISABLE_*_POP support + * + * disable_push/disable_pop push and pop disabling based on a parameter. + * Disabling observations stops processing of memory references during + * correctness modeling, and all annotations that occur in the disabled + * region. This allows description of code that is expected to be handled + * specially during conversion to parallelism or that is not recognized + * by tools (e.g. some kinds of synchronization operations.) + * This mechanism causes all annotations in the disabled region, other + * than disable_push and disable_pop, to be ignored. (For example, this + * might validly be used to disable an entire parallel site and the contained + * tasks and locking in it for data collection purposes.) + * The disable for collection is a more expensive operation, but reduces + * collector overhead significantly. This applies to BOTH correctness data + * collection and performance data collection. For example, a site + * containing a task might only enable data collection for the first 10 + * iterations. Both performance and correctness data should reflect this, + * and the program should run as close to full speed as possible when + * collection is disabled. + */ +void ITTAPI __itt_model_disable_push(__itt_model_disable x); +void ITTAPI __itt_model_disable_pop(void); +void ITTAPI __itt_model_aggregate_task(size_t x); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_disable_push, (__itt_model_disable x)) +ITT_STUBV(ITTAPI, void, model_disable_pop, (void)) +ITT_STUBV(ITTAPI, void, model_aggregate_task, (size_t x)) +#define __itt_model_disable_push ITTNOTIFY_VOID(model_disable_push) +#define __itt_model_disable_push_ptr ITTNOTIFY_NAME(model_disable_push) +#define __itt_model_disable_pop ITTNOTIFY_VOID(model_disable_pop) +#define __itt_model_disable_pop_ptr ITTNOTIFY_NAME(model_disable_pop) +#define __itt_model_aggregate_task ITTNOTIFY_VOID(model_aggregate_task) +#define __itt_model_aggregate_task_ptr ITTNOTIFY_NAME(model_aggregate_task) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_disable_push(x) +#define __itt_model_disable_push_ptr 0 +#define __itt_model_disable_pop() +#define __itt_model_disable_pop_ptr 0 +#define __itt_model_aggregate_task(x) +#define __itt_model_aggregate_task_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_disable_push_ptr 0 +#define __itt_model_disable_pop_ptr 0 +#define __itt_model_aggregate_task_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} model group */ + +/** + * @defgroup heap Heap + * @ingroup public + * Heap group + * @{ + */ + +typedef void* __itt_heap_function; + +/** + * @brief Create an identification for heap function + * @return non-zero identifier or NULL + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_heap_function ITTAPI __itt_heap_function_createA(const char* name, const char* domain); +__itt_heap_function ITTAPI __itt_heap_function_createW(const wchar_t* name, const wchar_t* domain); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_heap_function_create __itt_heap_function_createW +# define __itt_heap_function_create_ptr __itt_heap_function_createW_ptr +#else +# define __itt_heap_function_create __itt_heap_function_createA +# define __itt_heap_function_create_ptr __itt_heap_function_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_heap_function ITTAPI __itt_heap_function_create(const char* name, const char* domain); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_createA, (const char* name, const char* domain)) +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_createW, (const wchar_t* name, const wchar_t* domain)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_create, (const char* name, const char* domain)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA ITTNOTIFY_DATA(heap_function_createA) +#define __itt_heap_function_createA_ptr ITTNOTIFY_NAME(heap_function_createA) +#define __itt_heap_function_createW ITTNOTIFY_DATA(heap_function_createW) +#define __itt_heap_function_createW_ptr ITTNOTIFY_NAME(heap_function_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create ITTNOTIFY_DATA(heap_function_create) +#define __itt_heap_function_create_ptr ITTNOTIFY_NAME(heap_function_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_createA_ptr 0 +#define __itt_heap_function_createW(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA_ptr 0 +#define __itt_heap_function_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an allocation begin occurrence. + */ +void ITTAPI __itt_heap_allocate_begin(__itt_heap_function h, size_t size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_allocate_begin, (__itt_heap_function h, size_t size, int initialized)) +#define __itt_heap_allocate_begin ITTNOTIFY_VOID(heap_allocate_begin) +#define __itt_heap_allocate_begin_ptr ITTNOTIFY_NAME(heap_allocate_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_allocate_begin(h, size, initialized) +#define __itt_heap_allocate_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_allocate_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an allocation end occurrence. + */ +void ITTAPI __itt_heap_allocate_end(__itt_heap_function h, void** addr, size_t size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_allocate_end, (__itt_heap_function h, void** addr, size_t size, int initialized)) +#define __itt_heap_allocate_end ITTNOTIFY_VOID(heap_allocate_end) +#define __itt_heap_allocate_end_ptr ITTNOTIFY_NAME(heap_allocate_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_allocate_end(h, addr, size, initialized) +#define __itt_heap_allocate_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_allocate_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record a free begin occurrence. + */ +void ITTAPI __itt_heap_free_begin(__itt_heap_function h, void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_free_begin, (__itt_heap_function h, void* addr)) +#define __itt_heap_free_begin ITTNOTIFY_VOID(heap_free_begin) +#define __itt_heap_free_begin_ptr ITTNOTIFY_NAME(heap_free_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_free_begin(h, addr) +#define __itt_heap_free_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_free_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record a free end occurrence. + */ +void ITTAPI __itt_heap_free_end(__itt_heap_function h, void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_free_end, (__itt_heap_function h, void* addr)) +#define __itt_heap_free_end ITTNOTIFY_VOID(heap_free_end) +#define __itt_heap_free_end_ptr ITTNOTIFY_NAME(heap_free_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_free_end(h, addr) +#define __itt_heap_free_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_free_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record a reallocation begin occurrence. + */ +void ITTAPI __itt_heap_reallocate_begin(__itt_heap_function h, void* addr, size_t new_size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reallocate_begin, (__itt_heap_function h, void* addr, size_t new_size, int initialized)) +#define __itt_heap_reallocate_begin ITTNOTIFY_VOID(heap_reallocate_begin) +#define __itt_heap_reallocate_begin_ptr ITTNOTIFY_NAME(heap_reallocate_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reallocate_begin(h, addr, new_size, initialized) +#define __itt_heap_reallocate_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reallocate_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record a reallocation end occurrence. + */ +void ITTAPI __itt_heap_reallocate_end(__itt_heap_function h, void* addr, void** new_addr, size_t new_size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reallocate_end, (__itt_heap_function h, void* addr, void** new_addr, size_t new_size, int initialized)) +#define __itt_heap_reallocate_end ITTNOTIFY_VOID(heap_reallocate_end) +#define __itt_heap_reallocate_end_ptr ITTNOTIFY_NAME(heap_reallocate_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reallocate_end(h, addr, new_addr, new_size, initialized) +#define __itt_heap_reallocate_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reallocate_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief internal access begin */ +void ITTAPI __itt_heap_internal_access_begin(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_internal_access_begin, (void)) +#define __itt_heap_internal_access_begin ITTNOTIFY_VOID(heap_internal_access_begin) +#define __itt_heap_internal_access_begin_ptr ITTNOTIFY_NAME(heap_internal_access_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_internal_access_begin() +#define __itt_heap_internal_access_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_internal_access_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief internal access end */ +void ITTAPI __itt_heap_internal_access_end(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_internal_access_end, (void)) +#define __itt_heap_internal_access_end ITTNOTIFY_VOID(heap_internal_access_end) +#define __itt_heap_internal_access_end_ptr ITTNOTIFY_NAME(heap_internal_access_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_internal_access_end() +#define __itt_heap_internal_access_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_internal_access_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief record memory growth begin */ +void ITTAPI __itt_heap_record_memory_growth_begin(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record_memory_growth_begin, (void)) +#define __itt_heap_record_memory_growth_begin ITTNOTIFY_VOID(heap_record_memory_growth_begin) +#define __itt_heap_record_memory_growth_begin_ptr ITTNOTIFY_NAME(heap_record_memory_growth_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record_memory_growth_begin() +#define __itt_heap_record_memory_growth_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_memory_growth_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief record memory growth end */ +void ITTAPI __itt_heap_record_memory_growth_end(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record_memory_growth_end, (void)) +#define __itt_heap_record_memory_growth_end ITTNOTIFY_VOID(heap_record_memory_growth_end) +#define __itt_heap_record_memory_growth_end_ptr ITTNOTIFY_NAME(heap_record_memory_growth_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record_memory_growth_end() +#define __itt_heap_record_memory_growth_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_memory_growth_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Specify the type of heap detection/reporting to modify. + */ +/** + * @hideinitializer + * @brief Report on memory leaks. + */ +#define __itt_heap_leaks 0x00000001 + +/** + * @hideinitializer + * @brief Report on memory growth. + */ +#define __itt_heap_growth 0x00000002 + + +/** @brief heap reset detection */ +void ITTAPI __itt_heap_reset_detection(unsigned int reset_mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reset_detection, (unsigned int reset_mask)) +#define __itt_heap_reset_detection ITTNOTIFY_VOID(heap_reset_detection) +#define __itt_heap_reset_detection_ptr ITTNOTIFY_NAME(heap_reset_detection) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reset_detection() +#define __itt_heap_reset_detection_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reset_detection_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief report */ +void ITTAPI __itt_heap_record(unsigned int record_mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record, (unsigned int record_mask)) +#define __itt_heap_record ITTNOTIFY_VOID(heap_record) +#define __itt_heap_record_ptr ITTNOTIFY_NAME(heap_record) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record() +#define __itt_heap_record_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} heap group */ +/** @endcond */ +/* ========================================================================== */ + +/** + * @defgroup domains Domains + * @ingroup public + * Domains group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_domain +{ + volatile int flags; /*!< Zero if disabled, non-zero if enabled. The meaning of different non-zero values is reserved to the runtime */ + const char* nameA; /*!< Copy of original name in ASCII. */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* nameW; /*!< Copy of original name in UNICODE. */ +#else /* UNICODE || _UNICODE */ + void* nameW; +#endif /* UNICODE || _UNICODE */ + int extra1; /*!< Reserved to the runtime */ + void* extra2; /*!< Reserved to the runtime */ + struct ___itt_domain* next; +} __itt_domain; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup domains + * @brief Create a domain. + * Create domain using some domain name: the URI naming style is recommended. + * Because the set of domains is expected to be static over the application's + * execution time, there is no mechanism to destroy a domain. + * Any domain can be accessed by any thread in the process, regardless of + * which thread created the domain. This call is thread-safe. + * @param[in] name name of domain + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_domain* ITTAPI __itt_domain_createA(const char *name); +__itt_domain* ITTAPI __itt_domain_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_domain_create __itt_domain_createW +# define __itt_domain_create_ptr __itt_domain_createW_ptr +#else /* UNICODE */ +# define __itt_domain_create __itt_domain_createA +# define __itt_domain_create_ptr __itt_domain_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_domain* ITTAPI __itt_domain_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_domain*, domain_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_domain*, domain_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_domain*, domain_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA ITTNOTIFY_DATA(domain_createA) +#define __itt_domain_createA_ptr ITTNOTIFY_NAME(domain_createA) +#define __itt_domain_createW ITTNOTIFY_DATA(domain_createW) +#define __itt_domain_createW_ptr ITTNOTIFY_NAME(domain_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create ITTNOTIFY_DATA(domain_create) +#define __itt_domain_create_ptr ITTNOTIFY_NAME(domain_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA(name) (__itt_domain*)0 +#define __itt_domain_createA_ptr 0 +#define __itt_domain_createW(name) (__itt_domain*)0 +#define __itt_domain_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create(name) (__itt_domain*)0 +#define __itt_domain_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA_ptr 0 +#define __itt_domain_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} domains group */ + +/** + * @defgroup ids IDs + * @ingroup public + * IDs group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_id +{ + unsigned long long d1, d2, d3; +} __itt_id; + +#pragma pack(pop) +/** @endcond */ + +static const __itt_id __itt_null = { 0, 0, 0 }; + +/** + * @ingroup ids + * @brief A convenience function is provided to create an ID without domain control. + * @brief This is a convenience function to initialize an __itt_id structure. This function + * does not affect the collector runtime in any way. After you make the ID with this + * function, you still must create it with the __itt_id_create function before using the ID + * to identify a named entity. + * @param[in] addr The address of object; high QWORD of the ID value. + * @param[in] extra The extra data to unique identify object; low QWORD of the ID value. + */ + +ITT_INLINE __itt_id ITTAPI __itt_id_make(void* addr, unsigned long long extra) ITT_INLINE_ATTRIBUTE; +ITT_INLINE __itt_id ITTAPI __itt_id_make(void* addr, unsigned long long extra) +{ + __itt_id id = __itt_null; + id.d1 = (unsigned long long)((uintptr_t)addr); + id.d2 = (unsigned long long)extra; + id.d3 = (unsigned long long)0; /* Reserved. Must be zero */ + return id; +} + +/** + * @ingroup ids + * @brief Create an instance of identifier. + * This establishes the beginning of the lifetime of an instance of + * the given ID in the trace. Once this lifetime starts, the ID + * can be used to tag named entity instances in calls such as + * __itt_task_begin, and to specify relationships among + * identified named entity instances, using the \ref relations APIs. + * Instance IDs are not domain specific! + * @param[in] domain The domain controlling the execution of this call. + * @param[in] id The ID to create. + */ +void ITTAPI __itt_id_create(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_create, (const __itt_domain *domain, __itt_id id)) +#define __itt_id_create(d,x) ITTNOTIFY_VOID_D1(id_create,d,x) +#define __itt_id_create_ptr ITTNOTIFY_NAME(id_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_create(domain,id) +#define __itt_id_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup ids + * @brief Destroy an instance of identifier. + * This ends the lifetime of the current instance of the given ID value in the trace. + * Any relationships that are established after this lifetime ends are invalid. + * This call must be performed before the given ID value can be reused for a different + * named entity instance. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] id The ID to destroy. + */ +void ITTAPI __itt_id_destroy(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_destroy, (const __itt_domain *domain, __itt_id id)) +#define __itt_id_destroy(d,x) ITTNOTIFY_VOID_D1(id_destroy,d,x) +#define __itt_id_destroy_ptr ITTNOTIFY_NAME(id_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_destroy(domain,id) +#define __itt_id_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} ids group */ + +/** + * @defgroup handless String Handles + * @ingroup public + * String Handles group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_string_handle +{ + const char* strA; /*!< Copy of original string in ASCII. */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* strW; /*!< Copy of original string in UNICODE. */ +#else /* UNICODE || _UNICODE */ + void* strW; +#endif /* UNICODE || _UNICODE */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_string_handle* next; +} __itt_string_handle; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup handles + * @brief Create a string handle. + * Create and return handle value that can be associated with a string. + * Consecutive calls to __itt_string_handle_create with the same name + * return the same value. Because the set of string handles is expected to remain + * static during the application's execution time, there is no mechanism to destroy a string handle. + * Any string handle can be accessed by any thread in the process, regardless of which thread created + * the string handle. This call is thread-safe. + * @param[in] name The input string + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_string_handle* ITTAPI __itt_string_handle_createA(const char *name); +__itt_string_handle* ITTAPI __itt_string_handle_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_string_handle_create __itt_string_handle_createW +# define __itt_string_handle_create_ptr __itt_string_handle_createW_ptr +#else /* UNICODE */ +# define __itt_string_handle_create __itt_string_handle_createA +# define __itt_string_handle_create_ptr __itt_string_handle_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_string_handle* ITTAPI __itt_string_handle_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA ITTNOTIFY_DATA(string_handle_createA) +#define __itt_string_handle_createA_ptr ITTNOTIFY_NAME(string_handle_createA) +#define __itt_string_handle_createW ITTNOTIFY_DATA(string_handle_createW) +#define __itt_string_handle_createW_ptr ITTNOTIFY_NAME(string_handle_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create ITTNOTIFY_DATA(string_handle_create) +#define __itt_string_handle_create_ptr ITTNOTIFY_NAME(string_handle_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA(name) (__itt_string_handle*)0 +#define __itt_string_handle_createA_ptr 0 +#define __itt_string_handle_createW(name) (__itt_string_handle*)0 +#define __itt_string_handle_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create(name) (__itt_string_handle*)0 +#define __itt_string_handle_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA_ptr 0 +#define __itt_string_handle_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} handles group */ + +/** @cond exclude_from_documentation */ +typedef unsigned long long __itt_timestamp; +/** @endcond */ + +#define __itt_timestamp_none ((__itt_timestamp)-1LL) + +/** @cond exclude_from_gpa_documentation */ + +/** + * @ingroup timestamps + * @brief Return timestamp corresponding to the current moment. + * This returns the timestamp in the format that is the most relevant for the current + * host or platform (RDTSC, QPC, and others). You can use the "<" operator to + * compare __itt_timestamp values. + */ +__itt_timestamp ITTAPI __itt_get_timestamp(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_timestamp, get_timestamp, (void)) +#define __itt_get_timestamp ITTNOTIFY_DATA(get_timestamp) +#define __itt_get_timestamp_ptr ITTNOTIFY_NAME(get_timestamp) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_get_timestamp() +#define __itt_get_timestamp_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_get_timestamp_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} timestamps */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** + * @defgroup regions Regions + * @ingroup public + * Regions group + * @{ + */ +/** + * @ingroup regions + * @brief Begin of region instance. + * Successive calls to __itt_region_begin with the same ID are ignored + * until a call to __itt_region_end with the same ID + * @param[in] domain The domain for this region instance + * @param[in] id The instance ID for this region instance. Must not be __itt_null + * @param[in] parentid The instance ID for the parent of this region instance, or __itt_null + * @param[in] name The name of this region + */ +void ITTAPI __itt_region_begin(const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name); + +/** + * @ingroup regions + * @brief End of region instance. + * The first call to __itt_region_end with a given ID ends the + * region. Successive calls with the same ID are ignored, as are + * calls that do not have a matching __itt_region_begin call. + * @param[in] domain The domain for this region instance + * @param[in] id The instance ID for this region instance + */ +void ITTAPI __itt_region_end(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, region_begin, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, region_end, (const __itt_domain *domain, __itt_id id)) +#define __itt_region_begin(d,x,y,z) ITTNOTIFY_VOID_D3(region_begin,d,x,y,z) +#define __itt_region_begin_ptr ITTNOTIFY_NAME(region_begin) +#define __itt_region_end(d,x) ITTNOTIFY_VOID_D1(region_end,d,x) +#define __itt_region_end_ptr ITTNOTIFY_NAME(region_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_region_begin(d,x,y,z) +#define __itt_region_begin_ptr 0 +#define __itt_region_end(d,x) +#define __itt_region_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_region_begin_ptr 0 +#define __itt_region_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} regions group */ + +/** + * @defgroup frames Frames + * @ingroup public + * Frames are similar to regions, but are intended to be easier to use and to implement. + * In particular: + * - Frames always represent periods of elapsed time + * - By default, frames have no nesting relationships + * @{ + */ + +/** + * @ingroup frames + * @brief Begin a frame instance. + * Successive calls to __itt_frame_begin with the + * same ID are ignored until a call to __itt_frame_end with the same ID. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL + */ +void ITTAPI __itt_frame_begin_v3(const __itt_domain *domain, __itt_id *id); + +/** + * @ingroup frames + * @brief End a frame instance. + * The first call to __itt_frame_end with a given ID + * ends the frame. Successive calls with the same ID are ignored, as are + * calls that do not have a matching __itt_frame_begin call. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL for current + */ +void ITTAPI __itt_frame_end_v3(const __itt_domain *domain, __itt_id *id); + +/** + * @ingroup frames + * @brief Submits a frame instance. + * Successive calls to __itt_frame_begin or __itt_frame_submit with the + * same ID are ignored until a call to __itt_frame_end or __itt_frame_submit + * with the same ID. + * Passing special __itt_timestamp_none value as "end" argument means + * take the current timestamp as the end timestamp. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL + * @param[in] begin Timestamp of the beginning of the frame + * @param[in] end Timestamp of the end of the frame + */ +void ITTAPI __itt_frame_submit_v3(const __itt_domain *domain, __itt_id *id, + __itt_timestamp begin, __itt_timestamp end); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, frame_begin_v3, (const __itt_domain *domain, __itt_id *id)) +ITT_STUBV(ITTAPI, void, frame_end_v3, (const __itt_domain *domain, __itt_id *id)) +ITT_STUBV(ITTAPI, void, frame_submit_v3, (const __itt_domain *domain, __itt_id *id, __itt_timestamp begin, __itt_timestamp end)) +#define __itt_frame_begin_v3(d,x) ITTNOTIFY_VOID_D1(frame_begin_v3,d,x) +#define __itt_frame_begin_v3_ptr ITTNOTIFY_NAME(frame_begin_v3) +#define __itt_frame_end_v3(d,x) ITTNOTIFY_VOID_D1(frame_end_v3,d,x) +#define __itt_frame_end_v3_ptr ITTNOTIFY_NAME(frame_end_v3) +#define __itt_frame_submit_v3(d,x,b,e) ITTNOTIFY_VOID_D3(frame_submit_v3,d,x,b,e) +#define __itt_frame_submit_v3_ptr ITTNOTIFY_NAME(frame_submit_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_frame_begin_v3(domain,id) +#define __itt_frame_begin_v3_ptr 0 +#define __itt_frame_end_v3(domain,id) +#define __itt_frame_end_v3_ptr 0 +#define __itt_frame_submit_v3(domain,id,begin,end) +#define __itt_frame_submit_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_frame_begin_v3_ptr 0 +#define __itt_frame_end_v3_ptr 0 +#define __itt_frame_submit_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} frames group */ +/** @endcond */ + +/** + * @defgroup taskgroup Task Group + * @ingroup public + * Task Group + * @{ + */ +/** + * @ingroup task_groups + * @brief Denotes a task_group instance. + * Successive calls to __itt_task_group with the same ID are ignored. + * @param[in] domain The domain for this task_group instance + * @param[in] id The instance ID for this task_group instance. Must not be __itt_null. + * @param[in] parentid The instance ID for the parent of this task_group instance, or __itt_null. + * @param[in] name The name of this task_group + */ +void ITTAPI __itt_task_group(const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_group, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +#define __itt_task_group(d,x,y,z) ITTNOTIFY_VOID_D3(task_group,d,x,y,z) +#define __itt_task_group_ptr ITTNOTIFY_NAME(task_group) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_group(d,x,y,z) +#define __itt_task_group_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_group_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} taskgroup group */ + +/** + * @defgroup tasks Tasks + * @ingroup public + * A task instance represents a piece of work performed by a particular + * thread for a period of time. A call to __itt_task_begin creates a + * task instance. This becomes the current instance for that task on that + * thread. A following call to __itt_task_end on the same thread ends the + * instance. There may be multiple simultaneous instances of tasks with the + * same name on different threads. If an ID is specified, the task instance + * receives that ID. Nested tasks are allowed. + * + * Note: The task is defined by the bracketing of __itt_task_begin and + * __itt_task_end on the same thread. If some scheduling mechanism causes + * task switching (the thread executes a different user task) or task + * switching (the user task switches to a different thread) then this breaks + * the notion of current instance. Additional API calls are required to + * deal with that possibility. + * @{ + */ + +/** + * @ingroup tasks + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] taskid The instance ID for this task instance, or __itt_null + * @param[in] parentid The parent instance to which this task instance belongs, or __itt_null + * @param[in] name The name of this task + */ +void ITTAPI __itt_task_begin(const __itt_domain *domain, __itt_id taskid, __itt_id parentid, __itt_string_handle *name); + +/** + * @ingroup tasks + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] taskid The identifier for this task instance (may be 0) + * @param[in] parentid The parent of this task (may be 0) + * @param[in] fn The pointer to the function you are tracing + */ +void ITTAPI __itt_task_begin_fn(const __itt_domain *domain, __itt_id taskid, __itt_id parentid, void* fn); + +/** + * @ingroup tasks + * @brief End the current task instance. + * @param[in] domain The domain for this task + */ +void ITTAPI __itt_task_end(const __itt_domain *domain); + +/** + * @ingroup tasks + * @brief Begin an overlapped task instance. + * @param[in] domain The domain for this task. + * @param[in] taskid The identifier for this task instance, *cannot* be __itt_null. + * @param[in] parentid The parent of this task, or __itt_null. + * @param[in] name The name of this task. + */ +void ITTAPI __itt_task_begin_overlapped(const __itt_domain* domain, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup tasks + * @brief End an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] taskid Explicit ID of finished task + */ +void ITTAPI __itt_task_end_overlapped(const __itt_domain *domain, __itt_id taskid); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_begin_fn, (const __itt_domain *domain, __itt_id id, __itt_id parentid, void* fn)) +ITT_STUBV(ITTAPI, void, task_end, (const __itt_domain *domain)) +ITT_STUBV(ITTAPI, void, task_begin_overlapped, (const __itt_domain *domain, __itt_id taskid, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_end_overlapped, (const __itt_domain *domain, __itt_id taskid)) +#define __itt_task_begin(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin,d,x,y,z) +#define __itt_task_begin_ptr ITTNOTIFY_NAME(task_begin) +#define __itt_task_begin_fn(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin_fn,d,x,y,z) +#define __itt_task_begin_fn_ptr ITTNOTIFY_NAME(task_begin_fn) +#define __itt_task_end(d) ITTNOTIFY_VOID_D0(task_end,d) +#define __itt_task_end_ptr ITTNOTIFY_NAME(task_end) +#define __itt_task_begin_overlapped(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin_overlapped,d,x,y,z) +#define __itt_task_begin_overlapped_ptr ITTNOTIFY_NAME(task_begin_overlapped) +#define __itt_task_end_overlapped(d,x) ITTNOTIFY_VOID_D1(task_end_overlapped,d,x) +#define __itt_task_end_overlapped_ptr ITTNOTIFY_NAME(task_end_overlapped) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin(domain,id,parentid,name) +#define __itt_task_begin_ptr 0 +#define __itt_task_begin_fn(domain,id,parentid,fn) +#define __itt_task_begin_fn_ptr 0 +#define __itt_task_end(domain) +#define __itt_task_end_ptr 0 +#define __itt_task_begin_overlapped(domain,taskid,parentid,name) +#define __itt_task_begin_overlapped_ptr 0 +#define __itt_task_end_overlapped(domain,taskid) +#define __itt_task_end_overlapped_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_ptr 0 +#define __itt_task_begin_fn_ptr 0 +#define __itt_task_end_ptr 0 +#define __itt_task_begin_overlapped_ptr 0 +#define __itt_task_end_overlapped_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} tasks group */ + + +/** + * @defgroup markers Markers + * Markers represent a single discreet event in time. Markers have a scope, + * described by an enumerated type __itt_scope. Markers are created by + * the API call __itt_marker. A marker instance can be given an ID for use in + * adding metadata. + * @{ + */ + +/** + * @brief Describes the scope of an event object in the trace. + */ +typedef enum +{ + __itt_scope_unknown = 0, + __itt_scope_global, + __itt_scope_track_group, + __itt_scope_track, + __itt_scope_task, + __itt_scope_marker +} __itt_scope; + +/** @cond exclude_from_documentation */ +#define __itt_marker_scope_unknown __itt_scope_unknown +#define __itt_marker_scope_global __itt_scope_global +#define __itt_marker_scope_process __itt_scope_track_group +#define __itt_marker_scope_thread __itt_scope_track +#define __itt_marker_scope_task __itt_scope_task +/** @endcond */ + +/** + * @ingroup markers + * @brief Create a marker instance + * @param[in] domain The domain for this marker + * @param[in] id The instance ID for this marker or __itt_null + * @param[in] name The name for this marker + * @param[in] scope The scope for this marker + */ +void ITTAPI __itt_marker(const __itt_domain *domain, __itt_id id, __itt_string_handle *name, __itt_scope scope); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, marker, (const __itt_domain *domain, __itt_id id, __itt_string_handle *name, __itt_scope scope)) +#define __itt_marker(d,x,y,z) ITTNOTIFY_VOID_D3(marker,d,x,y,z) +#define __itt_marker_ptr ITTNOTIFY_NAME(marker) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_marker(domain,id,name,scope) +#define __itt_marker_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_marker_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} markers group */ + +/** + * @defgroup metadata Metadata + * The metadata API is used to attach extra information to named + * entities. Metadata can be attached to an identified named entity by ID, + * or to the current entity (which is always a task). + * + * Conceptually metadata has a type (what kind of metadata), a key (the + * name of the metadata), and a value (the actual data). The encoding of + * the value depends on the type of the metadata. + * + * The type of metadata is specified by an enumerated type __itt_metdata_type. + * @{ + */ + +/** + * @ingroup parameters + * @brief describes the type of metadata + */ +typedef enum { + __itt_metadata_unknown = 0, + __itt_metadata_u64, /**< Unsigned 64-bit integer */ + __itt_metadata_s64, /**< Signed 64-bit integer */ + __itt_metadata_u32, /**< Unsigned 32-bit integer */ + __itt_metadata_s32, /**< Signed 32-bit integer */ + __itt_metadata_u16, /**< Unsigned 16-bit integer */ + __itt_metadata_s16, /**< Signed 16-bit integer */ + __itt_metadata_float, /**< Signed 32-bit floating-point */ + __itt_metadata_double /**< SIgned 64-bit floating-point */ +} __itt_metadata_type; + +/** + * @ingroup parameters + * @brief Add metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + * @param[in] key The name of the metadata + * @param[in] type The type of the metadata + * @param[in] count The number of elements of the given type. If count == 0, no metadata will be added. + * @param[in] data The metadata itself +*/ +void ITTAPI __itt_metadata_add(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, metadata_add, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data)) +#define __itt_metadata_add(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(metadata_add,d,x,y,z,a,b) +#define __itt_metadata_add_ptr ITTNOTIFY_NAME(metadata_add) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_metadata_add(d,x,y,z,a,b) +#define __itt_metadata_add_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_metadata_add_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add string metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + * @param[in] key The name of the metadata + * @param[in] data The metadata itself + * @param[in] length The number of characters in the string, or -1 if the length is unknown but the string is null-terminated +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_metadata_str_addA(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length); +void ITTAPI __itt_metadata_str_addW(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const wchar_t *data, size_t length); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_metadata_str_add __itt_metadata_str_addW +# define __itt_metadata_str_add_ptr __itt_metadata_str_addW_ptr +#else /* UNICODE */ +# define __itt_metadata_str_add __itt_metadata_str_addA +# define __itt_metadata_str_add_ptr __itt_metadata_str_addA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_metadata_str_add(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length); +#endif + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, metadata_str_addA, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length)) +ITT_STUBV(ITTAPI, void, metadata_str_addW, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const wchar_t *data, size_t length)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, metadata_str_add, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_addA,d,x,y,z,a) +#define __itt_metadata_str_addA_ptr ITTNOTIFY_NAME(metadata_str_addA) +#define __itt_metadata_str_addW(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_addW,d,x,y,z,a) +#define __itt_metadata_str_addW_ptr ITTNOTIFY_NAME(metadata_str_addW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add,d,x,y,z,a) +#define __itt_metadata_str_add_ptr ITTNOTIFY_NAME(metadata_str_add) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA(d,x,y,z,a) +#define __itt_metadata_str_addA_ptr 0 +#define __itt_metadata_str_addW(d,x,y,z,a) +#define __itt_metadata_str_addW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add(d,x,y,z,a) +#define __itt_metadata_str_add_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA_ptr 0 +#define __itt_metadata_str_addW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] scope The scope of the instance to which the metadata is to be added + + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + + * @param[in] key The name of the metadata + * @param[in] type The type of the metadata + * @param[in] count The number of elements of the given type. If count == 0, no metadata will be added. + * @param[in] data The metadata itself +*/ +void ITTAPI __itt_metadata_add_with_scope(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, metadata_add_with_scope, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data)) +#define __itt_metadata_add_with_scope(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(metadata_add_with_scope,d,x,y,z,a,b) +#define __itt_metadata_add_with_scope_ptr ITTNOTIFY_NAME(metadata_add_with_scope) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_metadata_add_with_scope(d,x,y,z,a,b) +#define __itt_metadata_add_with_scope_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_metadata_add_with_scope_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add string metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] scope The scope of the instance to which the metadata is to be added + + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + + * @param[in] key The name of the metadata + * @param[in] data The metadata itself + * @param[in] length The number of characters in the string, or -1 if the length is unknown but the string is null-terminated +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_metadata_str_add_with_scopeA(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length); +void ITTAPI __itt_metadata_str_add_with_scopeW(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const wchar_t *data, size_t length); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_metadata_str_add_with_scope __itt_metadata_str_add_with_scopeW +# define __itt_metadata_str_add_with_scope_ptr __itt_metadata_str_add_with_scopeW_ptr +#else /* UNICODE */ +# define __itt_metadata_str_add_with_scope __itt_metadata_str_add_with_scopeA +# define __itt_metadata_str_add_with_scope_ptr __itt_metadata_str_add_with_scopeA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_metadata_str_add_with_scope(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length); +#endif + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scopeA, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length)) +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scopeW, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const wchar_t *data, size_t length)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scope, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scopeA,d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeA_ptr ITTNOTIFY_NAME(metadata_str_add_with_scopeA) +#define __itt_metadata_str_add_with_scopeW(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scopeW,d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeW_ptr ITTNOTIFY_NAME(metadata_str_add_with_scopeW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scope,d,x,y,z,a) +#define __itt_metadata_str_add_with_scope_ptr ITTNOTIFY_NAME(metadata_str_add_with_scope) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA(d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeA_ptr 0 +#define __itt_metadata_str_add_with_scopeW(d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope(d,x,y,z,a) +#define __itt_metadata_str_add_with_scope_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA_ptr 0 +#define __itt_metadata_str_add_with_scopeW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} metadata group */ + +/** + * @defgroup relations Relations + * Instances of named entities can be explicitly associated with other + * instances using instance IDs and the relationship API calls. + * + * @{ + */ + +/** + * @ingroup relations + * @brief The kind of relation between two instances is specified by the enumerated type __itt_relation. + * Relations between instances can be added with an API call. The relation + * API uses instance IDs. Relations can be added before or after the actual + * instances are created and persist independently of the instances. This + * is the motivation for having different lifetimes for instance IDs and + * the actual instances. + */ +typedef enum +{ + __itt_relation_is_unknown = 0, + __itt_relation_is_dependent_on, /**< "A is dependent on B" means that A cannot start until B completes */ + __itt_relation_is_sibling_of, /**< "A is sibling of B" means that A and B were created as a group */ + __itt_relation_is_parent_of, /**< "A is parent of B" means that A created B */ + __itt_relation_is_continuation_of, /**< "A is continuation of B" means that A assumes the dependencies of B */ + __itt_relation_is_child_of, /**< "A is child of B" means that A was created by B (inverse of is_parent_of) */ + __itt_relation_is_continued_by, /**< "A is continued by B" means that B assumes the dependencies of A (inverse of is_continuation_of) */ + __itt_relation_is_predecessor_to /**< "A is predecessor to B" means that B cannot start until A completes (inverse of is_dependent_on) */ +} __itt_relation; + +/** + * @ingroup relations + * @brief Add a relation to the current task instance. + * The current task instance is the head of the relation. + * @param[in] domain The domain controlling this call + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_to_current(const __itt_domain *domain, __itt_relation relation, __itt_id tail); + +/** + * @ingroup relations + * @brief Add a relation between two instance identifiers. + * @param[in] domain The domain controlling this call + * @param[in] head The ID for the head of the relation + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add(const __itt_domain *domain, __itt_id head, __itt_relation relation, __itt_id tail); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, relation_add_to_current, (const __itt_domain *domain, __itt_relation relation, __itt_id tail)) +ITT_STUBV(ITTAPI, void, relation_add, (const __itt_domain *domain, __itt_id head, __itt_relation relation, __itt_id tail)) +#define __itt_relation_add_to_current(d,x,y) ITTNOTIFY_VOID_D2(relation_add_to_current,d,x,y) +#define __itt_relation_add_to_current_ptr ITTNOTIFY_NAME(relation_add_to_current) +#define __itt_relation_add(d,x,y,z) ITTNOTIFY_VOID_D3(relation_add,d,x,y,z) +#define __itt_relation_add_ptr ITTNOTIFY_NAME(relation_add) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_relation_add_to_current(d,x,y) +#define __itt_relation_add_to_current_ptr 0 +#define __itt_relation_add(d,x,y,z) +#define __itt_relation_add_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_relation_add_to_current_ptr 0 +#define __itt_relation_add_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} relations group */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_clock_info +{ + unsigned long long clock_freq; /*!< Clock domain frequency */ + unsigned long long clock_base; /*!< Clock domain base timestamp */ +} __itt_clock_info; + +#pragma pack(pop) +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef void (ITTAPI *__itt_get_clock_info_fn)(__itt_clock_info* clock_info, void* data); +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_clock_domain +{ + __itt_clock_info info; /*!< Most recent clock domain info */ + __itt_get_clock_info_fn fn; /*!< Callback function pointer */ + void* fn_data; /*!< Input argument for the callback function */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_clock_domain* next; +} __itt_clock_domain; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup clockdomains + * @brief Create a clock domain. + * Certain applications require the capability to trace their application using + * a clock domain different than the CPU, for instance the instrumentation of events + * that occur on a GPU. + * Because the set of domains is expected to be static over the application's execution time, + * there is no mechanism to destroy a domain. + * Any domain can be accessed by any thread in the process, regardless of which thread created + * the domain. This call is thread-safe. + * @param[in] fn A pointer to a callback function which retrieves alternative CPU timestamps + * @param[in] fn_data Argument for a callback function; may be NULL + */ +__itt_clock_domain* ITTAPI __itt_clock_domain_create(__itt_get_clock_info_fn fn, void* fn_data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_clock_domain*, clock_domain_create, (__itt_get_clock_info_fn fn, void* fn_data)) +#define __itt_clock_domain_create ITTNOTIFY_DATA(clock_domain_create) +#define __itt_clock_domain_create_ptr ITTNOTIFY_NAME(clock_domain_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_clock_domain_create(fn,fn_data) (__itt_clock_domain*)0 +#define __itt_clock_domain_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_clock_domain_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomains + * @brief Recalculate clock domains frequencies and clock base timestamps. + */ +void ITTAPI __itt_clock_domain_reset(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, clock_domain_reset, (void)) +#define __itt_clock_domain_reset ITTNOTIFY_VOID(clock_domain_reset) +#define __itt_clock_domain_reset_ptr ITTNOTIFY_NAME(clock_domain_reset) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_clock_domain_reset() +#define __itt_clock_domain_reset_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_clock_domain_reset_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Create an instance of identifier. This establishes the beginning of the lifetime of + * an instance of the given ID in the trace. Once this lifetime starts, the ID can be used to + * tag named entity instances in calls such as __itt_task_begin, and to specify relationships among + * identified named entity instances, using the \ref relations APIs. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The ID to create. + */ +void ITTAPI __itt_id_create_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id); + +/** + * @ingroup clockdomain + * @brief Destroy an instance of identifier. This ends the lifetime of the current instance of the + * given ID value in the trace. Any relationships that are established after this lifetime ends are + * invalid. This call must be performed before the given ID value can be reused for a different + * named entity instance. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The ID to destroy. + */ +void ITTAPI __itt_id_destroy_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_create_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id)) +ITT_STUBV(ITTAPI, void, id_destroy_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id)) +#define __itt_id_create_ex(d,x,y,z) ITTNOTIFY_VOID_D3(id_create_ex,d,x,y,z) +#define __itt_id_create_ex_ptr ITTNOTIFY_NAME(id_create_ex) +#define __itt_id_destroy_ex(d,x,y,z) ITTNOTIFY_VOID_D3(id_destroy_ex,d,x,y,z) +#define __itt_id_destroy_ex_ptr ITTNOTIFY_NAME(id_destroy_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_create_ex(domain,clock_domain,timestamp,id) +#define __itt_id_create_ex_ptr 0 +#define __itt_id_destroy_ex(domain,clock_domain,timestamp,id) +#define __itt_id_destroy_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_create_ex_ptr 0 +#define __itt_id_destroy_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The instance ID for this task instance, or __itt_null + * @param[in] parentid The parent instance to which this task instance belongs, or __itt_null + * @param[in] name The name of this task + */ +void ITTAPI __itt_task_begin_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup clockdomain + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The identifier for this task instance, or __itt_null + * @param[in] parentid The parent of this task, or __itt_null + * @param[in] fn The pointer to the function you are tracing + */ +void ITTAPI __itt_task_begin_fn_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, void* fn); + +/** + * @ingroup clockdomain + * @brief End the current task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + */ +void ITTAPI __itt_task_end_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_begin_fn_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_id parentid, void* fn)) +ITT_STUBV(ITTAPI, void, task_end_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp)) +#define __itt_task_begin_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_ex,d,x,y,z,a,b) +#define __itt_task_begin_ex_ptr ITTNOTIFY_NAME(task_begin_ex) +#define __itt_task_begin_fn_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_fn_ex,d,x,y,z,a,b) +#define __itt_task_begin_fn_ex_ptr ITTNOTIFY_NAME(task_begin_fn_ex) +#define __itt_task_end_ex(d,x,y) ITTNOTIFY_VOID_D2(task_end_ex,d,x,y) +#define __itt_task_end_ex_ptr ITTNOTIFY_NAME(task_end_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin_ex(domain,clock_domain,timestamp,id,parentid,name) +#define __itt_task_begin_ex_ptr 0 +#define __itt_task_begin_fn_ex(domain,clock_domain,timestamp,id,parentid,fn) +#define __itt_task_begin_fn_ex_ptr 0 +#define __itt_task_end_ex(domain,clock_domain,timestamp) +#define __itt_task_end_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_ex_ptr 0 +#define __itt_task_begin_fn_ex_ptr 0 +#define __itt_task_end_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @defgroup counters Counters + * @ingroup public + * Counters are user-defined objects with a monotonically increasing + * value. Counter values are 64-bit unsigned integers. + * Counters have names that can be displayed in + * the tools. + * @{ + */ + +/** + * @brief opaque structure for counter identification + */ +/** @cond exclude_from_documentation */ + +typedef struct ___itt_counter* __itt_counter; + +/** + * @brief Create an unsigned 64 bits integer counter with given name/domain + * + * After __itt_counter_create() is called, __itt_counter_inc(id), __itt_counter_inc_delta(id, delta), + * __itt_counter_set_value(id, value_ptr) or __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) + * can be used to change the value of the counter, where value_ptr is a pointer to an unsigned 64 bits integer + * + * The call is equal to __itt_counter_create_typed(name, domain, __itt_metadata_u64) + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_counter ITTAPI __itt_counter_createA(const char *name, const char *domain); +__itt_counter ITTAPI __itt_counter_createW(const wchar_t *name, const wchar_t *domain); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_counter_create __itt_counter_createW +# define __itt_counter_create_ptr __itt_counter_createW_ptr +#else /* UNICODE */ +# define __itt_counter_create __itt_counter_createA +# define __itt_counter_create_ptr __itt_counter_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_counter ITTAPI __itt_counter_create(const char *name, const char *domain); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_counter, counter_createA, (const char *name, const char *domain)) +ITT_STUB(ITTAPI, __itt_counter, counter_createW, (const wchar_t *name, const wchar_t *domain)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_counter, counter_create, (const char *name, const char *domain)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA ITTNOTIFY_DATA(counter_createA) +#define __itt_counter_createA_ptr ITTNOTIFY_NAME(counter_createA) +#define __itt_counter_createW ITTNOTIFY_DATA(counter_createW) +#define __itt_counter_createW_ptr ITTNOTIFY_NAME(counter_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create ITTNOTIFY_DATA(counter_create) +#define __itt_counter_create_ptr ITTNOTIFY_NAME(counter_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA(name, domain) +#define __itt_counter_createA_ptr 0 +#define __itt_counter_createW(name, domain) +#define __itt_counter_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create(name, domain) +#define __itt_counter_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA_ptr 0 +#define __itt_counter_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Increment the unsigned 64 bits integer counter value + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_inc(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc, (__itt_counter id)) +#define __itt_counter_inc ITTNOTIFY_VOID(counter_inc) +#define __itt_counter_inc_ptr ITTNOTIFY_NAME(counter_inc) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc(id) +#define __itt_counter_inc_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** + * @brief Increment the unsigned 64 bits integer counter value with x + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_inc_delta(__itt_counter id, unsigned long long value); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc_delta, (__itt_counter id, unsigned long long value)) +#define __itt_counter_inc_delta ITTNOTIFY_VOID(counter_inc_delta) +#define __itt_counter_inc_delta_ptr ITTNOTIFY_NAME(counter_inc_delta) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc_delta(id, value) +#define __itt_counter_inc_delta_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_delta_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Decrement the unsigned 64 bits integer counter value + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_dec(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec, (__itt_counter id)) +#define __itt_counter_dec ITTNOTIFY_VOID(counter_dec) +#define __itt_counter_dec_ptr ITTNOTIFY_NAME(counter_dec) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec(id) +#define __itt_counter_dec_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** + * @brief Decrement the unsigned 64 bits integer counter value with x + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_dec_delta(__itt_counter id, unsigned long long value); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec_delta, (__itt_counter id, unsigned long long value)) +#define __itt_counter_dec_delta ITTNOTIFY_VOID(counter_dec_delta) +#define __itt_counter_dec_delta_ptr ITTNOTIFY_NAME(counter_dec_delta) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec_delta(id, value) +#define __itt_counter_dec_delta_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_delta_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup counters + * @brief Increment a counter by one. + * The first call with a given name creates a counter by that name and sets its + * value to zero. Successive calls increment the counter value. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + */ +void ITTAPI __itt_counter_inc_v3(const __itt_domain *domain, __itt_string_handle *name); + +/** + * @ingroup counters + * @brief Increment a counter by the value specified in delta. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + * @param[in] delta The amount by which to increment the counter + */ +void ITTAPI __itt_counter_inc_delta_v3(const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc_v3, (const __itt_domain *domain, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, counter_inc_delta_v3, (const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta)) +#define __itt_counter_inc_v3(d,x) ITTNOTIFY_VOID_D1(counter_inc_v3,d,x) +#define __itt_counter_inc_v3_ptr ITTNOTIFY_NAME(counter_inc_v3) +#define __itt_counter_inc_delta_v3(d,x,y) ITTNOTIFY_VOID_D2(counter_inc_delta_v3,d,x,y) +#define __itt_counter_inc_delta_v3_ptr ITTNOTIFY_NAME(counter_inc_delta_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc_v3(domain,name) +#define __itt_counter_inc_v3_ptr 0 +#define __itt_counter_inc_delta_v3(domain,name,delta) +#define __itt_counter_inc_delta_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_v3_ptr 0 +#define __itt_counter_inc_delta_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + + +/** + * @ingroup counters + * @brief Decrement a counter by one. + * The first call with a given name creates a counter by that name and sets its + * value to zero. Successive calls decrement the counter value. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + */ +void ITTAPI __itt_counter_dec_v3(const __itt_domain *domain, __itt_string_handle *name); + +/** + * @ingroup counters + * @brief Decrement a counter by the value specified in delta. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + * @param[in] delta The amount by which to decrement the counter + */ +void ITTAPI __itt_counter_dec_delta_v3(const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec_v3, (const __itt_domain *domain, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, counter_dec_delta_v3, (const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta)) +#define __itt_counter_dec_v3(d,x) ITTNOTIFY_VOID_D1(counter_dec_v3,d,x) +#define __itt_counter_dec_v3_ptr ITTNOTIFY_NAME(counter_dec_v3) +#define __itt_counter_dec_delta_v3(d,x,y) ITTNOTIFY_VOID_D2(counter_dec_delta_v3,d,x,y) +#define __itt_counter_dec_delta_v3_ptr ITTNOTIFY_NAME(counter_dec_delta_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec_v3(domain,name) +#define __itt_counter_dec_v3_ptr 0 +#define __itt_counter_dec_delta_v3(domain,name,delta) +#define __itt_counter_dec_delta_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_v3_ptr 0 +#define __itt_counter_dec_delta_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} counters group */ + + +/** + * @brief Set the counter value + */ +void ITTAPI __itt_counter_set_value(__itt_counter id, void *value_ptr); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_set_value, (__itt_counter id, void *value_ptr)) +#define __itt_counter_set_value ITTNOTIFY_VOID(counter_set_value) +#define __itt_counter_set_value_ptr ITTNOTIFY_NAME(counter_set_value) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_set_value(id, value_ptr) +#define __itt_counter_set_value_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_set_value_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Set the counter value + */ +void ITTAPI __itt_counter_set_value_ex(__itt_counter id, __itt_clock_domain *clock_domain, unsigned long long timestamp, void *value_ptr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_set_value_ex, (__itt_counter id, __itt_clock_domain *clock_domain, unsigned long long timestamp, void *value_ptr)) +#define __itt_counter_set_value_ex ITTNOTIFY_VOID(counter_set_value_ex) +#define __itt_counter_set_value_ex_ptr ITTNOTIFY_NAME(counter_set_value_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) +#define __itt_counter_set_value_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_set_value_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Create a typed counter with given name/domain + * + * After __itt_counter_create_typed() is called, __itt_counter_inc(id), __itt_counter_inc_delta(id, delta), + * __itt_counter_set_value(id, value_ptr) or __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) + * can be used to change the value of the counter + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_counter ITTAPI __itt_counter_create_typedA(const char *name, const char *domain, __itt_metadata_type type); +__itt_counter ITTAPI __itt_counter_create_typedW(const wchar_t *name, const wchar_t *domain, __itt_metadata_type type); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_counter_create_typed __itt_counter_create_typedW +# define __itt_counter_create_typed_ptr __itt_counter_create_typedW_ptr +#else /* UNICODE */ +# define __itt_counter_create_typed __itt_counter_create_typedA +# define __itt_counter_create_typed_ptr __itt_counter_create_typedA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_counter ITTAPI __itt_counter_create_typed(const char *name, const char *domain, __itt_metadata_type type); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_counter, counter_create_typedA, (const char *name, const char *domain, __itt_metadata_type type)) +ITT_STUB(ITTAPI, __itt_counter, counter_create_typedW, (const wchar_t *name, const wchar_t *domain, __itt_metadata_type type)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_counter, counter_create_typed, (const char *name, const char *domain, __itt_metadata_type type)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA ITTNOTIFY_DATA(counter_create_typedA) +#define __itt_counter_create_typedA_ptr ITTNOTIFY_NAME(counter_create_typedA) +#define __itt_counter_create_typedW ITTNOTIFY_DATA(counter_create_typedW) +#define __itt_counter_create_typedW_ptr ITTNOTIFY_NAME(counter_create_typedW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed ITTNOTIFY_DATA(counter_create_typed) +#define __itt_counter_create_typed_ptr ITTNOTIFY_NAME(counter_create_typed) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA(name, domain, type) +#define __itt_counter_create_typedA_ptr 0 +#define __itt_counter_create_typedW(name, domain, type) +#define __itt_counter_create_typedW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed(name, domain, type) +#define __itt_counter_create_typed_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA_ptr 0 +#define __itt_counter_create_typedW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Destroy the counter identified by the pointer previously returned by __itt_counter_create() or + * __itt_counter_create_typed() + */ +void ITTAPI __itt_counter_destroy(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_destroy, (__itt_counter id)) +#define __itt_counter_destroy ITTNOTIFY_VOID(counter_destroy) +#define __itt_counter_destroy_ptr ITTNOTIFY_NAME(counter_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_destroy(id) +#define __itt_counter_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} counters group */ + +/** + * @ingroup markers + * @brief Create a marker instance. + * @param[in] domain The domain for this marker + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The instance ID for this marker, or __itt_null + * @param[in] name The name for this marker + * @param[in] scope The scope for this marker + */ +void ITTAPI __itt_marker_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_string_handle *name, __itt_scope scope); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, marker_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_string_handle *name, __itt_scope scope)) +#define __itt_marker_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(marker_ex,d,x,y,z,a,b) +#define __itt_marker_ex_ptr ITTNOTIFY_NAME(marker_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_marker_ex(domain,clock_domain,timestamp,id,name,scope) +#define __itt_marker_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_marker_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Add a relation to the current task instance. + * The current task instance is the head of the relation. + * @param[in] domain The domain controlling this call + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_to_current_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_relation relation, __itt_id tail); + +/** + * @ingroup clockdomain + * @brief Add a relation between two instance identifiers. + * @param[in] domain The domain controlling this call + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] head The ID for the head of the relation + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id head, __itt_relation relation, __itt_id tail); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, relation_add_to_current_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_relation relation, __itt_id tail)) +ITT_STUBV(ITTAPI, void, relation_add_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id head, __itt_relation relation, __itt_id tail)) +#define __itt_relation_add_to_current_ex(d,x,y,z,a) ITTNOTIFY_VOID_D4(relation_add_to_current_ex,d,x,y,z,a) +#define __itt_relation_add_to_current_ex_ptr ITTNOTIFY_NAME(relation_add_to_current_ex) +#define __itt_relation_add_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(relation_add_ex,d,x,y,z,a,b) +#define __itt_relation_add_ex_ptr ITTNOTIFY_NAME(relation_add_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_relation_add_to_current_ex(domain,clock_domain,timestame,relation,tail) +#define __itt_relation_add_to_current_ex_ptr 0 +#define __itt_relation_add_ex(domain,clock_domain,timestamp,head,relation,tail) +#define __itt_relation_add_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_relation_add_to_current_ex_ptr 0 +#define __itt_relation_add_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef enum ___itt_track_group_type +{ + __itt_track_group_type_normal = 0 +} __itt_track_group_type; +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_track_group +{ + __itt_string_handle* name; /*!< Name of the track group */ + struct ___itt_track* track; /*!< List of child tracks */ + __itt_track_group_type tgtype; /*!< Type of the track group */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_track_group* next; +} __itt_track_group; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Placeholder for custom track types. Currently, "normal" custom track + * is the only available track type. + */ +typedef enum ___itt_track_type +{ + __itt_track_type_normal = 0 +#ifdef INTEL_ITTNOTIFY_API_PRIVATE + , __itt_track_type_queue +#endif /* INTEL_ITTNOTIFY_API_PRIVATE */ +} __itt_track_type; + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_track +{ + __itt_string_handle* name; /*!< Name of the track group */ + __itt_track_group* group; /*!< Parent group to a track */ + __itt_track_type ttype; /*!< Type of the track */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_track* next; +} __itt_track; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Create logical track group. + */ +__itt_track_group* ITTAPI __itt_track_group_create(__itt_string_handle* name, __itt_track_group_type track_group_type); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_track_group*, track_group_create, (__itt_string_handle* name, __itt_track_group_type track_group_type)) +#define __itt_track_group_create ITTNOTIFY_DATA(track_group_create) +#define __itt_track_group_create_ptr ITTNOTIFY_NAME(track_group_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_track_group_create(name) (__itt_track_group*)0 +#define __itt_track_group_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_track_group_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Create logical track. + */ +__itt_track* ITTAPI __itt_track_create(__itt_track_group* track_group, __itt_string_handle* name, __itt_track_type track_type); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_track*, track_create, (__itt_track_group* track_group,__itt_string_handle* name, __itt_track_type track_type)) +#define __itt_track_create ITTNOTIFY_DATA(track_create) +#define __itt_track_create_ptr ITTNOTIFY_NAME(track_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_track_create(track_group,name,track_type) (__itt_track*)0 +#define __itt_track_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_track_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Set the logical track. + */ +void ITTAPI __itt_set_track(__itt_track* track); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, set_track, (__itt_track *track)) +#define __itt_set_track ITTNOTIFY_VOID(set_track) +#define __itt_set_track_ptr ITTNOTIFY_NAME(set_track) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_set_track(track) +#define __itt_set_track_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_set_track_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/* ========================================================================== */ +/** @cond exclude_from_gpa_documentation */ +/** + * @defgroup events Events + * @ingroup public + * Events group + * @{ + */ +/** @brief user event type */ +typedef int __itt_event; + +/** + * @brief Create an event notification + * @note name or namelen being null/name and namelen not matching, user event feature not enabled + * @return non-zero event identifier upon success and __itt_err otherwise + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_event LIBITTAPI __itt_event_createA(const char *name, int namelen); +__itt_event LIBITTAPI __itt_event_createW(const wchar_t *name, int namelen); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_event_create __itt_event_createW +# define __itt_event_create_ptr __itt_event_createW_ptr +#else +# define __itt_event_create __itt_event_createA +# define __itt_event_create_ptr __itt_event_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_event LIBITTAPI __itt_event_create(const char *name, int namelen); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(LIBITTAPI, __itt_event, event_createA, (const char *name, int namelen)) +ITT_STUB(LIBITTAPI, __itt_event, event_createW, (const wchar_t *name, int namelen)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(LIBITTAPI, __itt_event, event_create, (const char *name, int namelen)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA ITTNOTIFY_DATA(event_createA) +#define __itt_event_createA_ptr ITTNOTIFY_NAME(event_createA) +#define __itt_event_createW ITTNOTIFY_DATA(event_createW) +#define __itt_event_createW_ptr ITTNOTIFY_NAME(event_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create ITTNOTIFY_DATA(event_create) +#define __itt_event_create_ptr ITTNOTIFY_NAME(event_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA(name, namelen) (__itt_event)0 +#define __itt_event_createA_ptr 0 +#define __itt_event_createW(name, namelen) (__itt_event)0 +#define __itt_event_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create(name, namelen) (__itt_event)0 +#define __itt_event_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA_ptr 0 +#define __itt_event_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an event occurrence. + * @return __itt_err upon failure (invalid event id/user event feature not enabled) + */ +int LIBITTAPI __itt_event_start(__itt_event event); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(LIBITTAPI, int, event_start, (__itt_event event)) +#define __itt_event_start ITTNOTIFY_DATA(event_start) +#define __itt_event_start_ptr ITTNOTIFY_NAME(event_start) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_event_start(event) (int)0 +#define __itt_event_start_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_event_start_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an event end occurrence. + * @note It is optional if events do not have durations. + * @return __itt_err upon failure (invalid event id/user event feature not enabled) + */ +int LIBITTAPI __itt_event_end(__itt_event event); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(LIBITTAPI, int, event_end, (__itt_event event)) +#define __itt_event_end ITTNOTIFY_DATA(event_end) +#define __itt_event_end_ptr ITTNOTIFY_NAME(event_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_event_end(event) (int)0 +#define __itt_event_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_event_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} events group */ + + +/** + * @defgroup arrays Arrays Visualizer + * @ingroup public + * Visualize arrays + * @{ + */ + +/** + * @enum __itt_av_data_type + * @brief Defines types of arrays data (for C/C++ intrinsic types) + */ +typedef enum +{ + __itt_e_first = 0, + __itt_e_char = 0, /* 1-byte integer */ + __itt_e_uchar, /* 1-byte unsigned integer */ + __itt_e_int16, /* 2-byte integer */ + __itt_e_uint16, /* 2-byte unsigned integer */ + __itt_e_int32, /* 4-byte integer */ + __itt_e_uint32, /* 4-byte unsigned integer */ + __itt_e_int64, /* 8-byte integer */ + __itt_e_uint64, /* 8-byte unsigned integer */ + __itt_e_float, /* 4-byte floating */ + __itt_e_double, /* 8-byte floating */ + __itt_e_last = __itt_e_double +} __itt_av_data_type; + +/** + * @brief Save an array data to a file. + * Output format is defined by the file extension. The csv and bmp formats are supported (bmp - for 2-dimensional array only). + * @param[in] data - pointer to the array data + * @param[in] rank - the rank of the array + * @param[in] dimensions - pointer to an array of integers, which specifies the array dimensions. + * The size of dimensions must be equal to the rank + * @param[in] type - the type of the array, specified as one of the __itt_av_data_type values (for intrinsic types) + * @param[in] filePath - the file path; the output format is defined by the file extension + * @param[in] columnOrder - defines how the array is stored in the linear memory. + * It should be 1 for column-major order (e.g. in FORTRAN) or 0 - for row-major order (e.g. in C). + */ + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_av_saveA(void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder); +int ITTAPI __itt_av_saveW(void *data, int rank, const int *dimensions, int type, const wchar_t *filePath, int columnOrder); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_av_save __itt_av_saveW +# define __itt_av_save_ptr __itt_av_saveW_ptr +#else /* UNICODE */ +# define __itt_av_save __itt_av_saveA +# define __itt_av_save_ptr __itt_av_saveA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_av_save(void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, av_saveA, (void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder)) +ITT_STUB(ITTAPI, int, av_saveW, (void *data, int rank, const int *dimensions, int type, const wchar_t *filePath, int columnOrder)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, av_save, (void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA ITTNOTIFY_DATA(av_saveA) +#define __itt_av_saveA_ptr ITTNOTIFY_NAME(av_saveA) +#define __itt_av_saveW ITTNOTIFY_DATA(av_saveW) +#define __itt_av_saveW_ptr ITTNOTIFY_NAME(av_saveW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save ITTNOTIFY_DATA(av_save) +#define __itt_av_save_ptr ITTNOTIFY_NAME(av_save) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA(name) +#define __itt_av_saveA_ptr 0 +#define __itt_av_saveW(name) +#define __itt_av_saveW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save(name) +#define __itt_av_save_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA_ptr 0 +#define __itt_av_saveW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +void ITTAPI __itt_enable_attach(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, enable_attach, (void)) +#define __itt_enable_attach ITTNOTIFY_VOID(enable_attach) +#define __itt_enable_attach_ptr ITTNOTIFY_NAME(enable_attach) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_enable_attach() +#define __itt_enable_attach_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_enable_attach_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** @} arrays group */ + +/** @endcond */ + +/** + * @brief Module load notification + * This API is used to report necessary information in case of bypassing default system loader. + * Notification should be done immidiatelly after this module is loaded to process memory. + * @param[in] start_addr - module start address + * @param[in] end_addr - module end address + * @param[in] path - file system full path to the module + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_module_loadA(void *start_addr, void *end_addr, const char *path); +void ITTAPI __itt_module_loadW(void *start_addr, void *end_addr, const wchar_t *path); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_module_load __itt_module_loadW +# define __itt_module_load_ptr __itt_module_loadW_ptr +#else /* UNICODE */ +# define __itt_module_load __itt_module_loadA +# define __itt_module_load_ptr __itt_module_loadA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_module_load(void *start_addr, void *end_addr, const char *path); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, void, module_loadA, (void *start_addr, void *end_addr, const char *path)) +ITT_STUB(ITTAPI, void, module_loadW, (void *start_addr, void *end_addr, const wchar_t *path)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, void, module_load, (void *start_addr, void *end_addr, const char *path)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA ITTNOTIFY_VOID(module_loadA) +#define __itt_module_loadA_ptr ITTNOTIFY_NAME(module_loadA) +#define __itt_module_loadW ITTNOTIFY_VOID(module_loadW) +#define __itt_module_loadW_ptr ITTNOTIFY_NAME(module_loadW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load ITTNOTIFY_VOID(module_load) +#define __itt_module_load_ptr ITTNOTIFY_NAME(module_load) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA(start_addr, end_addr, path) +#define __itt_module_loadA_ptr 0 +#define __itt_module_loadW(start_addr, end_addr, path) +#define __itt_module_loadW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load(start_addr, end_addr, path) +#define __itt_module_load_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA_ptr 0 +#define __itt_module_loadW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Report module unload + * This API is used to report necessary information in case of bypassing default system loader. + * Notification should be done just before the module is unloaded from process memory. + * @param[in] addr - base address of loaded module + */ +void ITTAPI __itt_module_unload(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, module_unload, (void *addr)) +#define __itt_module_unload ITTNOTIFY_VOID(module_unload) +#define __itt_module_unload_ptr ITTNOTIFY_NAME(module_unload) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_module_unload(addr) +#define __itt_module_unload_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_module_unload_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef enum +{ + __itt_module_type_unknown = 0, + __itt_module_type_elf, + __itt_module_type_coff +} __itt_module_type; +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef enum +{ + itt_section_type_unknown, + itt_section_type_bss, /* notifies that the section contains uninitialized data. These are the relevant section types and the modules that contain them: + * ELF module: SHT_NOBITS section type + * COFF module: IMAGE_SCN_CNT_UNINITIALIZED_DATA section type + */ + itt_section_type_data, /* notifies that section contains initialized data. These are the relevant section types and the modules that contain them: + * ELF module: SHT_PROGBITS section type + * COFF module: IMAGE_SCN_CNT_INITIALIZED_DATA section type + */ + itt_section_type_text /* notifies that the section contains executable code. These are the relevant section types and the modules that contain them: + * ELF module: SHT_PROGBITS section type + * COFF module: IMAGE_SCN_CNT_CODE section type + */ +} __itt_section_type; +/** @endcond */ + +/** + * @hideinitializer + * @brief bit-mask, detects a section attribute that indicates whether a section can be executed as code: + * These are the relevant section attributes and the modules that contain them: + * ELF module: PF_X section attribute + * COFF module: IMAGE_SCN_MEM_EXECUTE attribute + */ +#define __itt_section_exec 0x20000000 + +/** + * @hideinitializer + * @brief bit-mask, detects a section attribute that indicates whether a section can be read. + * These are the relevant section attributes and the modules that contain them: + * ELF module: PF_R attribute + * COFF module: IMAGE_SCN_MEM_READ attribute + */ +#define __itt_section_read 0x40000000 + +/** + * @hideinitializer + * @brief bit-mask, detects a section attribute that indicates whether a section can be written to. + * These are the relevant section attributes and the modules that contain them: + * ELF module: PF_W attribute + * COFF module: IMAGE_SCN_MEM_WRITE attribute + */ +#define __itt_section_write 0x80000000 + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_section_info +{ + const char* name; /*!< Section name in UTF8 */ + __itt_section_type type; /*!< Section content and semantics description */ + size_t flags; /*!< Section bit flags that describe attributes using bit mask + * Zero if disabled, non-zero if enabled + */ + void* start_addr; /*!< Section load(relocated) start address */ + size_t size; /*!< Section file offset */ + size_t file_offset; /*!< Section size */ +} __itt_section_info; + +#pragma pack(pop) +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_module_object +{ + unsigned int version; /*!< API version*/ + __itt_id module_id; /*!< Unique identifier. This is unchanged for sections that belong to the same module */ + __itt_module_type module_type; /*!< Binary module format */ + const char* module_name; /*!< Unique module name or path to module in UTF8 + * Contains module name when module_bufer and module_size exist + * Contains module path when module_bufer and module_size absent + * module_name remains the same for the certain module_id + */ + void* module_buffer; /*!< Module buffer content */ + size_t module_size; /*!< Module buffer size */ + /*!< If module_buffer and module_size exist, the binary module is dumped onto the system. + * If module_buffer and module_size do not exist, + * the binary module exists on the system already. + * The module_name parameter contains the path to the module. + */ + __itt_section_info* section_array; /*!< Reference to section information */ + size_t section_number; +} __itt_module_object; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Load module content and its loaded(relocated) sections. + * This API is useful to save a module, or specify its location on the system and report information about loaded sections. + * The target module is saved on the system if module buffer content and size are available. + * If module buffer content and size are unavailable, the module name contains the path to the existing binary module. + * @param[in] module_obj - provides module and section information, along with unique module identifiers (name,module ID) + * which bind the binary module to particular sections. + */ +void ITTAPI __itt_module_load_with_sections(__itt_module_object* module_obj); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, module_load_with_sections, (__itt_module_object* module_obj)) +#define __itt_module_load_with_sections ITTNOTIFY_VOID(module_load_with_sections) +#define __itt_module_load_with_sections_ptr ITTNOTIFY_NAME(module_load_with_sections) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_module_load_with_sections(module_obj) +#define __itt_module_load_with_sections_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_module_load_with_sections_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Unload a module and its loaded(relocated) sections. + * This API notifies that the module and its sections were unloaded. + * @param[in] module_obj - provides module and sections information, along with unique module identifiers (name,module ID) + * which bind the binary module to particular sections. + */ +void ITTAPI __itt_module_unload_with_sections(__itt_module_object* module_obj); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, module_unload_with_sections, (__itt_module_object* module_obj)) +#define __itt_module_unload_with_sections ITTNOTIFY_VOID(module_unload_with_sections) +#define __itt_module_unload_with_sections_ptr ITTNOTIFY_NAME(module_unload_with_sections) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_module_unload_with_sections(module_obj) +#define __itt_module_unload_with_sections_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_module_unload_with_sections_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_histogram +{ + const __itt_domain* domain; /*!< Domain of the histogram*/ + const char* nameA; /*!< Name of the histogram */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* nameW; +#else /* UNICODE || _UNICODE */ + void* nameW; +#endif /* UNICODE || _UNICODE */ + __itt_metadata_type x_type; /*!< Type of the histogram X axis */ + __itt_metadata_type y_type; /*!< Type of the histogram Y axis */ + int extra1; /*!< Reserved to the runtime */ + void* extra2; /*!< Reserved to the runtime */ + struct ___itt_histogram* next; +} __itt_histogram; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Create a typed histogram instance with given name/domain. + * @param[in] domain The domain controlling the call. + * @param[in] name The name of the histogram. + * @param[in] x_type The type of the X axis in histogram (may be 0 to calculate batch statistics). + * @param[in] y_type The type of the Y axis in histogram. +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_histogram* ITTAPI __itt_histogram_createA(const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type); +__itt_histogram* ITTAPI __itt_histogram_createW(const __itt_domain* domain, const wchar_t* name, __itt_metadata_type x_type, __itt_metadata_type y_type); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_histogram_create __itt_histogram_createW +# define __itt_histogram_create_ptr __itt_histogram_createW_ptr +#else /* UNICODE */ +# define __itt_histogram_create __itt_histogram_createA +# define __itt_histogram_create_ptr __itt_histogram_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_histogram* ITTAPI __itt_histogram_create(const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_histogram*, histogram_createA, (const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type)) +ITT_STUB(ITTAPI, __itt_histogram*, histogram_createW, (const __itt_domain* domain, const wchar_t* name, __itt_metadata_type x_type, __itt_metadata_type y_type)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_histogram*, histogram_create, (const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_histogram_createA ITTNOTIFY_DATA(histogram_createA) +#define __itt_histogram_createA_ptr ITTNOTIFY_NAME(histogram_createA) +#define __itt_histogram_createW ITTNOTIFY_DATA(histogram_createW) +#define __itt_histogram_createW_ptr ITTNOTIFY_NAME(histogram_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_histogram_create ITTNOTIFY_DATA(histogram_create) +#define __itt_histogram_create_ptr ITTNOTIFY_NAME(histogram_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_histogram_createA(domain, name, x_type, y_type) (__itt_histogram*)0 +#define __itt_histogram_createA_ptr 0 +#define __itt_histogram_createW(domain, name, x_type, y_type) (__itt_histogram*)0 +#define __itt_histogram_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_histogram_create(domain, name, x_type, y_type) (__itt_histogram*)0 +#define __itt_histogram_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_histogram_createA_ptr 0 +#define __itt_histogram_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_histogram_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Submit statistics for a histogram instance. + * @param[in] hist Pointer to the histogram instance to which the histogram statistic is to be dumped. + * @param[in] length The number of elements in dumped axis data array. + * @param[in] x_data The X axis dumped data itself (may be NULL to calculate batch statistics). + * @param[in] y_data The Y axis dumped data itself. +*/ +void ITTAPI __itt_histogram_submit(__itt_histogram* hist, size_t length, void* x_data, void* y_data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, histogram_submit, (__itt_histogram* hist, size_t length, void* x_data, void* y_data)) +#define __itt_histogram_submit ITTNOTIFY_VOID(histogram_submit) +#define __itt_histogram_submit_ptr ITTNOTIFY_NAME(histogram_submit) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_histogram_submit(hist, length, x_data, y_data) +#define __itt_histogram_submit_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_histogram_submit_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ + +/** +* @brief function allows to obtain the current collection state at the moment +* @return collection state as a enum __itt_collection_state +*/ +__itt_collection_state __itt_get_collection_state(void); + +/** +* @brief function releases resources allocated by ITT API static part +* this API should be called from the library destructor +* @return void +*/ +void __itt_release_resources(void); +/** @endcond */ + +/** + * @brief Create a typed counter with given domain pointer, string name and counter type +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_counter ITTAPI __itt_counter_createA_v3(const __itt_domain* domain, const char* name, __itt_metadata_type type); +__itt_counter ITTAPI __itt_counter_createW_v3(const __itt_domain* domain, const wchar_t* name, __itt_metadata_type type); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_counter_create_v3 __itt_counter_createW_v3 +# define __itt_counter_create_v3_ptr __itt_counter_createW_v3_ptr +#else /* UNICODE */ +# define __itt_counter_create_v3 __itt_counter_createA_v3 +# define __itt_counter_create_v3_ptr __itt_counter_createA_v3_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_counter ITTAPI __itt_counter_create_v3(const __itt_domain* domain, const char* name, __itt_metadata_type type); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_counter, counter_createA_v3, (const __itt_domain* domain, const char* name, __itt_metadata_type type)) +ITT_STUB(ITTAPI, __itt_counter, counter_createW_v3, (const __itt_domain* domain, const wchar_t* name, __itt_metadata_type type)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_counter, counter_create_v3, (const __itt_domain* domain, const char* name, __itt_metadata_type type)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA_v3 ITTNOTIFY_DATA(counter_createA_v3) +#define __itt_counter_createA_v3_ptr ITTNOTIFY_NAME(counter_createA_v3) +#define __itt_counter_createW_v3 ITTNOTIFY_DATA(counter_createW_v3) +#define __itt_counter_createW_v3_ptr ITTNOTIFY_NAME(counter_createW_v3) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_v3 ITTNOTIFY_DATA(counter_create_v3) +#define __itt_counter_create_v3_ptr ITTNOTIFY_NAME(counter_create_v3) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA_v3(domain, name, type) (__itt_counter)0 +#define __itt_counter_createA_v3_ptr 0 +#define __itt_counter_createW_v3(domain, name, type) (__itt_counter)0 +#define __itt_counter_create_typedW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_v3(domain, name, type) (__itt_counter)0 +#define __itt_counter_create_v3_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA_v3_ptr 0 +#define __itt_counter_createW_v3_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_v3_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Set the counter value api + */ +void ITTAPI __itt_counter_set_value_v3(__itt_counter counter, void *value_ptr); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_set_value_v3, (__itt_counter counter, void *value_ptr)) +#define __itt_counter_set_value_v3 ITTNOTIFY_VOID(counter_set_value_v3) +#define __itt_counter_set_value_v3_ptr ITTNOTIFY_NAME(counter_set_value_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_set_value_v3(counter, value_ptr) +#define __itt_counter_set_value_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_set_value_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief describes the type of context metadata +*/ +typedef enum { + __itt_context_unknown = 0, /*!< Undefined type */ + __itt_context_nameA, /*!< ASCII string char* type */ + __itt_context_nameW, /*!< Unicode string wchar_t* type */ + __itt_context_deviceA, /*!< ASCII string char* type */ + __itt_context_deviceW, /*!< Unicode string wchar_t* type */ + __itt_context_unitsA, /*!< ASCII string char* type */ + __itt_context_unitsW, /*!< Unicode string wchar_t* type */ + __itt_context_pci_addrA, /*!< ASCII string char* type */ + __itt_context_pci_addrW, /*!< Unicode string wchar_t* type */ + __itt_context_tid, /*!< Unsigned 64-bit integer type */ + __itt_context_max_val, /*!< Unsigned 64-bit integer type */ + __itt_context_bandwidth_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_latency_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_occupancy_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_on_thread_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_is_abs_val_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_cpu_instructions_flag, /*!< Unsigned 64-bit integer type */ + __itt_context_cpu_cycles_flag /*!< Unsigned 64-bit integer type */ +} __itt_context_type; + +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_context_name __itt_context_nameW +# define __itt_context_device __itt_context_deviceW +# define __itt_context_units __itt_context_unitsW +# define __itt_context_pci_addr __itt_context_pci_addrW +#else /* UNICODE || _UNICODE */ +# define __itt_context_name __itt_context_nameA +# define __itt_context_device __itt_context_deviceA +# define __itt_context_units __itt_context_unitsA +# define __itt_context_pci_addr __itt_context_pci_addrA +#endif /* UNICODE || _UNICODE */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_context_metadata +{ + __itt_context_type type; /*!< Type of the context metadata value */ + void* value; /*!< Pointer to context metadata value itself */ +} __itt_context_metadata; + +#pragma pack(pop) +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_counter_metadata +{ + __itt_counter counter; /*!< Associated context metadata counter */ + __itt_context_type type; /*!< Type of the context metadata value */ + const char* str_valueA; /*!< String context metadata value */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* str_valueW; +#else /* UNICODE || _UNICODE */ + void* str_valueW; +#endif /* UNICODE || _UNICODE */ + unsigned long long value; /*!< Numeric context metadata value */ + int extra1; /*!< Reserved to the runtime */ + void* extra2; /*!< Reserved to the runtime */ + struct ___itt_counter_metadata* next; +} __itt_counter_metadata; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Bind context metadata to counter instance + * @param[in] counter Pointer to the counter instance to which the context metadata is to be associated. + * @param[in] length The number of elements in context metadata array. + * @param[in] metadata The context metadata itself. +*/ +void ITTAPI __itt_bind_context_metadata_to_counter(__itt_counter counter, size_t length, __itt_context_metadata* metadata); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, bind_context_metadata_to_counter, (__itt_counter counter, size_t length, __itt_context_metadata* metadata)) +#define __itt_bind_context_metadata_to_counter ITTNOTIFY_VOID(bind_context_metadata_to_counter) +#define __itt_bind_context_metadata_to_counter_ptr ITTNOTIFY_NAME(bind_context_metadata_to_counter) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_bind_context_metadata_to_counter(counter, length, metadata) +#define __itt_bind_context_metadata_to_counter_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_bind_context_metadata_to_counter_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _ITTNOTIFY_H_ */ + +#ifdef INTEL_ITTNOTIFY_API_PRIVATE + +#ifndef _ITTNOTIFY_PRIVATE_ +#define _ITTNOTIFY_PRIVATE_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @ingroup clockdomain + * @brief Begin an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The identifier for this task instance, *cannot* be __itt_null. + * @param[in] parentid The parent of this task, or __itt_null. + * @param[in] name The name of this task. + */ +void ITTAPI __itt_task_begin_overlapped_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup clockdomain + * @brief End an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid Explicit ID of finished task + */ +void ITTAPI __itt_task_end_overlapped_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin_overlapped_ex, (const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name)) +ITT_STUBV(ITTAPI, void, task_end_overlapped_ex, (const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid)) +#define __itt_task_begin_overlapped_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_overlapped_ex,d,x,y,z,a,b) +#define __itt_task_begin_overlapped_ex_ptr ITTNOTIFY_NAME(task_begin_overlapped_ex) +#define __itt_task_end_overlapped_ex(d,x,y,z) ITTNOTIFY_VOID_D3(task_end_overlapped_ex,d,x,y,z) +#define __itt_task_end_overlapped_ex_ptr ITTNOTIFY_NAME(task_end_overlapped_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin_overlapped_ex(domain,clock_domain,timestamp,taskid,parentid,name) +#define __itt_task_begin_overlapped_ex_ptr 0 +#define __itt_task_end_overlapped_ex(domain,clock_domain,timestamp,taskid) +#define __itt_task_end_overlapped_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_overlapped_ex_ptr 0 +#define __itt_task_end_overlapped_ptr 0 +#define __itt_task_end_overlapped_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @defgroup makrs_internal Marks + * @ingroup internal + * Marks group + * @warning Internal API: + * - It is not shipped to outside of Intel + * - It is delivered to internal Intel teams using e-mail or SVN access only + * @{ + */ +/** @brief user mark type */ +typedef int __itt_mark_type; + +/** + * @brief Creates a user mark type with the specified name using char or Unicode string. + * @param[in] name - name of mark to create + * @return Returns a handle to the mark type + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_mark_type ITTAPI __itt_mark_createA(const char *name); +__itt_mark_type ITTAPI __itt_mark_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark_create __itt_mark_createW +# define __itt_mark_create_ptr __itt_mark_createW_ptr +#else /* UNICODE */ +# define __itt_mark_create __itt_mark_createA +# define __itt_mark_create_ptr __itt_mark_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_mark_type ITTAPI __itt_mark_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_mark_type, mark_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_mark_type, mark_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_mark_type, mark_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA ITTNOTIFY_DATA(mark_createA) +#define __itt_mark_createA_ptr ITTNOTIFY_NAME(mark_createA) +#define __itt_mark_createW ITTNOTIFY_DATA(mark_createW) +#define __itt_mark_createW_ptr ITTNOTIFY_NAME(mark_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create ITTNOTIFY_DATA(mark_create) +#define __itt_mark_create_ptr ITTNOTIFY_NAME(mark_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA(name) (__itt_mark_type)0 +#define __itt_mark_createA_ptr 0 +#define __itt_mark_createW(name) (__itt_mark_type)0 +#define __itt_mark_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create(name) (__itt_mark_type)0 +#define __itt_mark_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA_ptr 0 +#define __itt_mark_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Creates a "discrete" user mark type of the specified type and an optional parameter using char or Unicode string. + * + * - The mark of "discrete" type is placed to collection results in case of success. It appears in overtime view(s) as a special tick sign. + * - The call is "synchronous" - function returns after mark is actually added to results. + * - This function is useful, for example, to mark different phases of application + * (beginning of the next mark automatically meand end of current region). + * - Can be used together with "continuous" marks (see below) at the same collection session + * @param[in] mt - mark, created by __itt_mark_create(const char* name) function + * @param[in] parameter - string parameter of mark + * @return Returns zero value in case of success, non-zero value otherwise. + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_markA(__itt_mark_type mt, const char *parameter); +int ITTAPI __itt_markW(__itt_mark_type mt, const wchar_t *parameter); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark __itt_markW +# define __itt_mark_ptr __itt_markW_ptr +#else /* UNICODE */ +# define __itt_mark __itt_markA +# define __itt_mark_ptr __itt_markA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_mark(__itt_mark_type mt, const char *parameter); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, markA, (__itt_mark_type mt, const char *parameter)) +ITT_STUB(ITTAPI, int, markW, (__itt_mark_type mt, const wchar_t *parameter)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, mark, (__itt_mark_type mt, const char *parameter)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA ITTNOTIFY_DATA(markA) +#define __itt_markA_ptr ITTNOTIFY_NAME(markA) +#define __itt_markW ITTNOTIFY_DATA(markW) +#define __itt_markW_ptr ITTNOTIFY_NAME(markW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark ITTNOTIFY_DATA(mark) +#define __itt_mark_ptr ITTNOTIFY_NAME(mark) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA(mt, parameter) (int)0 +#define __itt_markA_ptr 0 +#define __itt_markW(mt, parameter) (int)0 +#define __itt_markW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark(mt, parameter) (int)0 +#define __itt_mark_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA_ptr 0 +#define __itt_markW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Use this if necessary to create a "discrete" user event type (mark) for process + * rather then for one thread + * @see int __itt_mark(__itt_mark_type mt, const char* parameter); + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_mark_globalA(__itt_mark_type mt, const char *parameter); +int ITTAPI __itt_mark_globalW(__itt_mark_type mt, const wchar_t *parameter); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark_global __itt_mark_globalW +# define __itt_mark_global_ptr __itt_mark_globalW_ptr +#else /* UNICODE */ +# define __itt_mark_global __itt_mark_globalA +# define __itt_mark_global_ptr __itt_mark_globalA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_mark_global(__itt_mark_type mt, const char *parameter); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, mark_globalA, (__itt_mark_type mt, const char *parameter)) +ITT_STUB(ITTAPI, int, mark_globalW, (__itt_mark_type mt, const wchar_t *parameter)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, mark_global, (__itt_mark_type mt, const char *parameter)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA ITTNOTIFY_DATA(mark_globalA) +#define __itt_mark_globalA_ptr ITTNOTIFY_NAME(mark_globalA) +#define __itt_mark_globalW ITTNOTIFY_DATA(mark_globalW) +#define __itt_mark_globalW_ptr ITTNOTIFY_NAME(mark_globalW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global ITTNOTIFY_DATA(mark_global) +#define __itt_mark_global_ptr ITTNOTIFY_NAME(mark_global) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA(mt, parameter) (int)0 +#define __itt_mark_globalA_ptr 0 +#define __itt_mark_globalW(mt, parameter) (int)0 +#define __itt_mark_globalW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global(mt, parameter) (int)0 +#define __itt_mark_global_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA_ptr 0 +#define __itt_mark_globalW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Creates an "end" point for "continuous" mark with specified name. + * + * - Returns zero value in case of success, non-zero value otherwise. + * Also returns non-zero value when preceding "begin" point for the + * mark with the same name failed to be created or not created. + * - The mark of "continuous" type is placed to collection results in + * case of success. It appears in overtime view(s) as a special tick + * sign (different from "discrete" mark) together with line from + * corresponding "begin" mark to "end" mark. + * @note Continuous marks can overlap and be nested inside each other. + * Discrete mark can be nested inside marked region + * @param[in] mt - mark, created by __itt_mark_create(const char* name) function + * @return Returns zero value in case of success, non-zero value otherwise. + */ +int ITTAPI __itt_mark_off(__itt_mark_type mt); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, int, mark_off, (__itt_mark_type mt)) +#define __itt_mark_off ITTNOTIFY_DATA(mark_off) +#define __itt_mark_off_ptr ITTNOTIFY_NAME(mark_off) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_mark_off(mt) (int)0 +#define __itt_mark_off_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_mark_off_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Use this if necessary to create an "end" point for mark of process + * @see int __itt_mark_off(__itt_mark_type mt); + */ +int ITTAPI __itt_mark_global_off(__itt_mark_type mt); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, int, mark_global_off, (__itt_mark_type mt)) +#define __itt_mark_global_off ITTNOTIFY_DATA(mark_global_off) +#define __itt_mark_global_off_ptr ITTNOTIFY_NAME(mark_global_off) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_mark_global_off(mt) (int)0 +#define __itt_mark_global_off_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_mark_global_off_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} marks group */ + +/** + * @defgroup counters_internal Counters + * @ingroup internal + * Counters group + * @{ + */ + + +/** + * @defgroup stitch Stack Stitching + * @ingroup internal + * Stack Stitching group + * @{ + */ +/** + * @brief opaque structure for counter identification + */ +typedef struct ___itt_caller *__itt_caller; + +/** + * @brief Create the stitch point e.g. a point in call stack where other stacks should be stitched to. + * The function returns a unique identifier which is used to match the cut points with corresponding stitch points. + */ +__itt_caller ITTAPI __itt_stack_caller_create(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_caller, stack_caller_create, (void)) +#define __itt_stack_caller_create ITTNOTIFY_DATA(stack_caller_create) +#define __itt_stack_caller_create_ptr ITTNOTIFY_NAME(stack_caller_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_caller_create() (__itt_caller)0 +#define __itt_stack_caller_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_caller_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Destroy the information about stitch point identified by the pointer previously returned by __itt_stack_caller_create() + */ +void ITTAPI __itt_stack_caller_destroy(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_caller_destroy, (__itt_caller id)) +#define __itt_stack_caller_destroy ITTNOTIFY_VOID(stack_caller_destroy) +#define __itt_stack_caller_destroy_ptr ITTNOTIFY_NAME(stack_caller_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_caller_destroy(id) +#define __itt_stack_caller_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_caller_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Sets the cut point. Stack from each event which occurs after this call will be cut + * at the same stack level the function was called and stitched to the corresponding stitch point. + */ +void ITTAPI __itt_stack_callee_enter(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_callee_enter, (__itt_caller id)) +#define __itt_stack_callee_enter ITTNOTIFY_VOID(stack_callee_enter) +#define __itt_stack_callee_enter_ptr ITTNOTIFY_NAME(stack_callee_enter) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_callee_enter(id) +#define __itt_stack_callee_enter_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_callee_enter_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief This function eliminates the cut point which was set by latest __itt_stack_callee_enter(). + */ +void ITTAPI __itt_stack_callee_leave(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_callee_leave, (__itt_caller id)) +#define __itt_stack_callee_leave ITTNOTIFY_VOID(stack_callee_leave) +#define __itt_stack_callee_leave_ptr ITTNOTIFY_NAME(stack_callee_leave) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_callee_leave(id) +#define __itt_stack_callee_leave_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_callee_leave_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} stitch group */ + +/* ***************************************************************************************************************************** */ + +#include + +/** @cond exclude_from_documentation */ +typedef enum __itt_error_code +{ + __itt_error_success = 0, /*!< no error */ + __itt_error_no_module = 1, /*!< module can't be loaded */ + /* %1$s -- library name; win: %2$d -- system error code; unx: %2$s -- system error message. */ + __itt_error_no_symbol = 2, /*!< symbol not found */ + /* %1$s -- library name, %2$s -- symbol name. */ + __itt_error_unknown_group = 3, /*!< unknown group specified */ + /* %1$s -- env var name, %2$s -- group name. */ + __itt_error_cant_read_env = 4, /*!< GetEnvironmentVariable() failed */ + /* %1$s -- env var name, %2$d -- system error. */ + __itt_error_env_too_long = 5, /*!< variable value too long */ + /* %1$s -- env var name, %2$d -- actual length of the var, %3$d -- max allowed length. */ + __itt_error_system = 6 /*!< pthread_mutexattr_init or pthread_mutex_init failed */ + /* %1$s -- function name, %2$d -- errno. */ +} __itt_error_code; + +typedef void (__itt_error_handler_t)(__itt_error_code code, va_list); +__itt_error_handler_t* __itt_set_error_handler(__itt_error_handler_t*); + +const char* ITTAPI __itt_api_version(void); +/** @endcond */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#define __itt_error_handler ITT_JOIN(INTEL_ITTNOTIFY_PREFIX, error_handler) +void __itt_error_handler(__itt_error_code code, va_list args); +extern const int ITTNOTIFY_NAME(err); +#define __itt_err ITTNOTIFY_NAME(err) +ITT_STUB(ITTAPI, const char*, api_version, (void)) +#define __itt_api_version ITTNOTIFY_DATA(api_version) +#define __itt_api_version_ptr ITTNOTIFY_NAME(api_version) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_api_version() (const char*)0 +#define __itt_api_version_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_api_version_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _ITTNOTIFY_PRIVATE_ */ + +#endif /* INTEL_ITTNOTIFY_API_PRIVATE */ diff --git a/include/aws/common/file.h b/include/aws/common/file.h index 842b777d8..23937e866 100644 --- a/include/aws/common/file.h +++ b/include/aws/common/file.h @@ -191,6 +191,8 @@ bool aws_path_exists(const struct aws_string *path); * fseeko() on linux * * whence can either be SEEK_SET or SEEK_END + * + * Returns AWS_OP_SUCCESS, or AWS_OP_ERR (after an error has been raised). */ AWS_COMMON_API int aws_fseek(FILE *file, int64_t offset, int whence); diff --git a/include/aws/common/hash_table.h b/include/aws/common/hash_table.h index 370b1c38d..c2cfadd77 100644 --- a/include/aws/common/hash_table.h +++ b/include/aws/common/hash_table.h @@ -12,9 +12,11 @@ AWS_PUSH_SANE_WARNING_LEVEL -#define AWS_COMMON_HASH_TABLE_ITER_CONTINUE (1 << 0) -#define AWS_COMMON_HASH_TABLE_ITER_DELETE (1 << 1) -#define AWS_COMMON_HASH_TABLE_ITER_ERROR (1 << 2) +enum { + AWS_COMMON_HASH_TABLE_ITER_CONTINUE = (1 << 0), + AWS_COMMON_HASH_TABLE_ITER_DELETE = (1 << 1), + AWS_COMMON_HASH_TABLE_ITER_ERROR = (1 << 2), +}; /** * Hash table data structure. This module provides an automatically resizing @@ -101,8 +103,9 @@ typedef uint64_t(aws_hash_fn)(const void *key); * keys, but note that the same type is used for a function that compares * two hash table values in aws_hash_table_eq. * - * Equality functions used in a hash table must be reflexive (i.e., a == b if - * and only if b == a), and must be consistent with the hash function in use. + * Equality functions used in a hash table must be be reflexive (a == a), + * symmetric (a == b => b == a), transitive (a == b, b == c => a == c) + * and consistent (result does not change with time). */ typedef bool(aws_hash_callback_eq_fn)(const void *a, const void *b); @@ -273,6 +276,7 @@ int aws_hash_table_create( * * Returns AWS_OP_SUCCESS if an item was found or created. * Raises AWS_ERROR_OOM if hash table expansion was required and memory + * allocation failed. */ AWS_COMMON_API int aws_hash_table_put(struct aws_hash_table *map, const void *key, void *value, int *was_created); @@ -430,6 +434,20 @@ bool aws_hash_table_is_valid(const struct aws_hash_table *map); AWS_COMMON_API bool aws_hash_iter_is_valid(const struct aws_hash_iter *iter); +/** + * Helper function to hash keys that are uint64_t values. + * + * The function is not a strong hash function in any sense; it merely reflects + * the uint64 value back. Do not use this function as a hash if you need + * the properties of a strong hash function. + */ +AWS_COMMON_API uint64_t aws_hash_uint64_t_by_identity(const void *item); + +/** + * Helper function to compare hash keys that are uint64_t values. + */ +AWS_COMMON_API bool aws_hash_compare_uint64_t_eq(const void *a, const void *b); + AWS_EXTERN_C_END AWS_POP_SANE_WARNING_LEVEL diff --git a/include/aws/common/host_utils.h b/include/aws/common/host_utils.h new file mode 100644 index 000000000..0b8285d7a --- /dev/null +++ b/include/aws/common/host_utils.h @@ -0,0 +1,28 @@ +#ifndef AWS_COMMON_HOST_UTILS_H +#define AWS_COMMON_HOST_UTILS_H +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +struct aws_byte_cursor; + +AWS_PUSH_SANE_WARNING_LEVEL +AWS_EXTERN_C_BEGIN + +/* + * Determine whether host cursor is IPv4 string. + */ +AWS_COMMON_API bool aws_host_utils_is_ipv4(struct aws_byte_cursor host); + +/* + * Determine whether host cursor is IPv6 string. + * Supports checking for uri encoded strings and scoped literals. + */ +AWS_COMMON_API bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded); + +AWS_EXTERN_C_END +AWS_POP_SANE_WARNING_LEVEL + +#endif /* AWS_COMMON_HOST_UTILS_H */ diff --git a/include/aws/common/json.h b/include/aws/common/json.h index b614387c0..b8c4e6cfe 100644 --- a/include/aws/common/json.h +++ b/include/aws/common/json.h @@ -400,7 +400,7 @@ int aws_byte_buf_append_json_string(const struct aws_json_value *value, struct a * @param value The aws_json_value to format. * @param output The destination for the JSON string * @return AWS_OP_SUCCESS if the JSON string was allocated to output without any errors - * Will return AWS_ERROR_INVALID_ARGUMENT if the value passed is not an aws_json_value or if there + * Will return AWS_OP_ERR if the value passed is not an aws_json_value or if there * aws an error appending the JSON into the byte buffer. */ AWS_COMMON_API diff --git a/include/aws/common/linked_list.h b/include/aws/common/linked_list.h index 3f550c6fe..cb0ce655c 100644 --- a/include/aws/common/linked_list.h +++ b/include/aws/common/linked_list.h @@ -182,6 +182,11 @@ AWS_STATIC_IMPL void aws_linked_list_move_all_front( struct aws_linked_list *AWS_RESTRICT dst, struct aws_linked_list *AWS_RESTRICT src); +/** + * Returns true if the node is currently in a list, false otherwise. + */ +AWS_STATIC_IMPL bool aws_linked_list_node_is_in_list(struct aws_linked_list_node *node); + #ifndef AWS_NO_STATIC_IMPL # include #endif /* AWS_NO_STATIC_IMPL */ diff --git a/include/aws/common/linked_list.inl b/include/aws/common/linked_list.inl index 99604834a..4d7e54dd0 100644 --- a/include/aws/common/linked_list.inl +++ b/include/aws/common/linked_list.inl @@ -431,6 +431,10 @@ AWS_STATIC_IMPL void aws_linked_list_move_all_front( AWS_POSTCONDITION(aws_linked_list_is_valid(dst)); } +AWS_STATIC_IMPL bool aws_linked_list_node_is_in_list(struct aws_linked_list_node *node) { + return aws_linked_list_node_prev_is_valid(node) && aws_linked_list_node_next_is_valid(node); +} + AWS_EXTERN_C_END #endif /* AWS_COMMON_LINKED_LIST_INL */ diff --git a/include/aws/common/logging.h b/include/aws/common/logging.h index 87b956811..444465aa1 100644 --- a/include/aws/common/logging.h +++ b/include/aws/common/logging.h @@ -58,9 +58,11 @@ enum aws_log_level { typedef uint32_t aws_log_subject_t; /* Each library gets space for 2^^10 log subject entries */ -#define AWS_LOG_SUBJECT_STRIDE_BITS 10 +enum { + AWS_LOG_SUBJECT_STRIDE_BITS = 10, +}; #define AWS_LOG_SUBJECT_STRIDE (1U << AWS_LOG_SUBJECT_STRIDE_BITS) -#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x)*AWS_LOG_SUBJECT_STRIDE) +#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x) * AWS_LOG_SUBJECT_STRIDE) #define AWS_LOG_SUBJECT_END_RANGE(x) (((x) + 1) * AWS_LOG_SUBJECT_STRIDE - 1) struct aws_log_subject_info { @@ -70,7 +72,7 @@ struct aws_log_subject_info { }; #define DEFINE_LOG_SUBJECT_INFO(id, name, desc) \ - { .subject_id = (id), .subject_name = (name), .subject_description = (desc) } + {.subject_id = (id), .subject_name = (name), .subject_description = (desc)} struct aws_log_subject_info_list { struct aws_log_subject_info *subject_list; @@ -87,6 +89,7 @@ enum aws_common_log_subject { AWS_LS_COMMON_BUS, AWS_LS_COMMON_TEST, AWS_LS_COMMON_JSON_PARSER, + AWS_LS_COMMON_CBOR, AWS_LS_COMMON_LAST = AWS_LOG_SUBJECT_END_RANGE(AWS_C_COMMON_PACKAGE_ID) }; diff --git a/include/aws/common/macros.h b/include/aws/common/macros.h index 648e39022..03e50a218 100644 --- a/include/aws/common/macros.h +++ b/include/aws/common/macros.h @@ -61,7 +61,7 @@ AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1) == 1); AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2) == 2); AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3); -#define AWS_CACHE_LINE 64 +enum { AWS_CACHE_LINE = 64 }; /** * Format macro for strings of a specified length. * Allows non null-terminated strings to be used with the printf family of functions. @@ -131,6 +131,24 @@ AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3); # define AWS_SUPPRESS_TSAN #endif +#if defined(__has_feature) +# if __has_feature(undefined_behavior_sanitizer) +# define AWS_SUPPRESS_UBSAN __attribute__((no_sanitize("undefined"))) +# endif +#elif defined(__SANITIZE_UNDEFINED__) +# if defined(__GNUC__) +# define AWS_SUPPRESS_UBSAN __attribute__((no_sanitize_undefined)) +# else +# define AWS_SUPPRESS_UBSAN +# endif +#else +# define AWS_SUPPRESS_UBSAN +#endif + +#if !defined(AWS_SUPPRESS_UBSAN) +# define AWS_SUPPRESS_UBSAN +#endif + /* If this is C++, restrict isn't supported. If this is not at least C99 on gcc and clang, it isn't supported. * If visual C++ building in C mode, the restrict definition is __restrict. * This just figures all of that out based on who's including this header file. */ @@ -160,6 +178,6 @@ AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3); * this will get you back to the pointer of the object. member is the name of * the instance of struct aws_linked_list_node in your struct. */ -#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr)-offsetof(type, member))) +#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) #endif /* AWS_COMMON_MACROS_H */ diff --git a/include/aws/common/mutex.h b/include/aws/common/mutex.h index b64e5e32f..b59a58474 100644 --- a/include/aws/common/mutex.h +++ b/include/aws/common/mutex.h @@ -8,7 +8,7 @@ #include #ifdef _WIN32 -/* NOTE: Do not use this macro before including Windows.h */ +/* NOTE: Do not use this macro before including windows.h */ # define AWSMUTEX_TO_WINDOWS(pMutex) (PSRWLOCK) & (pMutex)->mutex_handle #else # include @@ -26,11 +26,9 @@ struct aws_mutex { }; #ifdef _WIN32 -# define AWS_MUTEX_INIT \ - { .mutex_handle = NULL, .initialized = true } +# define AWS_MUTEX_INIT {.mutex_handle = NULL, .initialized = true} #else -# define AWS_MUTEX_INIT \ - { .mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true } +# define AWS_MUTEX_INIT {.mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/package.h b/include/aws/common/package.h index 0d7b3c2a5..c93fd5109 100644 --- a/include/aws/common/package.h +++ b/include/aws/common/package.h @@ -10,7 +10,7 @@ * Preliminary cap on the number of possible aws-c-libraries participating in shared enum ranges for * errors, log subjects, and other cross-library enums. Expandable as needed */ -#define AWS_PACKAGE_SLOTS 16 +#define AWS_PACKAGE_SLOTS 32 /* * Each aws-c-* and aws-crt-* library has a unique package id starting from zero. These are used to macro-calculate diff --git a/include/aws/common/priority_queue.h b/include/aws/common/priority_queue.h index 4478b95d0..26b35e991 100644 --- a/include/aws/common/priority_queue.h +++ b/include/aws/common/priority_queue.h @@ -162,6 +162,12 @@ int aws_priority_queue_remove(struct aws_priority_queue *queue, void *item, cons AWS_COMMON_API int aws_priority_queue_top(const struct aws_priority_queue *queue, void **item); +/** + * Removes all elements from the queue, but does not free internal memory. + */ +AWS_COMMON_API +void aws_priority_queue_clear(struct aws_priority_queue *queue); + /** * Current number of elements in the queue */ @@ -175,6 +181,24 @@ size_t aws_priority_queue_size(const struct aws_priority_queue *queue); AWS_COMMON_API size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue); +/** + * Initializes a queue node to a default value that indicates the node is not in the queue. + * + * @param node priority queue node to initialize with a default value + */ +AWS_COMMON_API +void aws_priority_queue_node_init(struct aws_priority_queue_node *node); + +/** + * Checks if a priority queue node is currently in a priority queue. + * + * @param node priority queue node to check usage for + * + * @return true if the node is in a queue, false otherwise + */ +AWS_COMMON_API +bool aws_priority_queue_node_is_in_queue(const struct aws_priority_queue_node *node); + AWS_EXTERN_C_END AWS_POP_SANE_WARNING_LEVEL diff --git a/include/aws/common/private/byte_buf.h b/include/aws/common/private/byte_buf.h index 81c08cbba..a9eebf034 100644 --- a/include/aws/common/private/byte_buf.h +++ b/include/aws/common/private/byte_buf.h @@ -15,4 +15,23 @@ */ AWS_COMMON_API size_t aws_nospec_mask(size_t index, size_t bound); +/** + * Expand the buffer appropriately to meet the requested capacity. + * + * If the the buffer's capacity is currently larger than the request capacity, the + * function does nothing (no shrink is performed). + */ +AWS_COMMON_API +int aws_byte_buf_reserve_smart(struct aws_byte_buf *buffer, size_t requested_capacity); + +/** + * Convenience function that attempts to increase the capacity of a buffer relative to the current + * length appropriately. + * + * If the the buffer's capacity is currently larger than the request capacity, the + * function does nothing (no shrink is performed). + */ +AWS_COMMON_API +int aws_byte_buf_reserve_smart_relative(struct aws_byte_buf *buffer, size_t additional_length); + #endif /* AWS_COMMON_PRIVATE_BYTE_BUF_H */ diff --git a/include/aws/common/private/json_impl.h b/include/aws/common/private/external_module_impl.h similarity index 64% rename from include/aws/common/private/json_impl.h rename to include/aws/common/private/external_module_impl.h index df9d81a5c..d3f75be39 100644 --- a/include/aws/common/private/json_impl.h +++ b/include/aws/common/private/external_module_impl.h @@ -1,5 +1,5 @@ -#ifndef AWS_COMMON_PRIVATE_JSON_IMPL_H -#define AWS_COMMON_PRIVATE_JSON_IMPL_H +#ifndef AWS_COMMON_PRIVATE_EXTERNAL_MODULE_IMPL_H +#define AWS_COMMON_PRIVATE_EXTERNAL_MODULE_IMPL_H /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. @@ -19,4 +19,8 @@ void aws_json_module_init(struct aws_allocator *allocator); */ void aws_json_module_cleanup(void); -#endif // AWS_COMMON_PRIVATE_JSON_IMPL_H +void aws_cbor_module_init(struct aws_allocator *allocator); + +void aws_cbor_module_cleanup(void); + +#endif // AWS_COMMON_PRIVATE_EXTERNAL_MODULE_IMPL_H diff --git a/include/aws/common/private/system_info_priv.h b/include/aws/common/private/system_info_priv.h new file mode 100644 index 000000000..27b1d4ad1 --- /dev/null +++ b/include/aws/common/private/system_info_priv.h @@ -0,0 +1,37 @@ +#ifndef AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H +#define AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include +#include +#include +#include + +struct aws_system_environment { + struct aws_allocator *allocator; + struct aws_ref_count ref_count; + struct aws_byte_buf virtualization_vendor; + struct aws_byte_buf product_name; + enum aws_platform_os os; + size_t cpu_count; + size_t cpu_group_count; + void *impl; +}; + +/** + * For internal implementors. Fill in info in env that you're able to grab, such as dmi info, os version strings etc... + * in here. The default just returns AWS_OP_SUCCESS. This is currently only implemented for linux. + * + * Returns AWS_OP_ERR if the implementation wasn't able to fill in required information for the platform. + */ +int aws_system_environment_load_platform_impl(struct aws_system_environment *env); + +/** + * For internal implementors. Cleans up anything allocated in aws_system_environment_load_platform_impl, + * but does not release the memory for env. + */ +void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env); + +#endif // AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H diff --git a/include/aws/common/promise.h b/include/aws/common/promise.h deleted file mode 100644 index 37f930ee1..000000000 --- a/include/aws/common/promise.h +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0. - */ - -#ifndef AWS_COMMON_PROMISE_H -#define AWS_COMMON_PROMISE_H - -#include - -AWS_PUSH_SANE_WARNING_LEVEL - -/* - * Standard promise interface. Promise can be waited on by multiple threads, and as long as it is - * ref-counted correctly, will provide the resultant value/error code to all waiters. - * All promise API calls are internally thread-safe. - */ -struct aws_promise; - -AWS_EXTERN_C_BEGIN - -/* - * Creates a new promise - */ -AWS_COMMON_API -struct aws_promise *aws_promise_new(struct aws_allocator *allocator); - -/* - * Indicate a new reference to a promise. At minimum, each new thread making use of the promise should - * acquire it. - */ -AWS_COMMON_API -struct aws_promise *aws_promise_acquire(struct aws_promise *promise); - -/* - * Releases a reference on the promise. When the refcount hits 0, the promise is cleaned up and freed. - */ -AWS_COMMON_API -void aws_promise_release(struct aws_promise *promise); - -/* - * Waits infinitely for the promise to be completed - */ -AWS_COMMON_API -void aws_promise_wait(struct aws_promise *promise); -/* - * Waits for the requested time in nanoseconds. Returns true if the promise was completed. - */ -AWS_COMMON_API -bool aws_promise_wait_for(struct aws_promise *promise, size_t nanoseconds); - -/* - * Completes the promise and stores the result along with an optional destructor. If the value - * is not taken via `aws_promise_take_value`, it will be destroyed when the promise's reference - * count reaches zero. - * NOTE: Promise cannot be completed twice - */ -AWS_COMMON_API -void aws_promise_complete(struct aws_promise *promise, void *value, void (*dtor)(void *)); - -/* - * Completes the promise and stores the error code - * NOTE: Promise cannot be completed twice - */ -AWS_COMMON_API -void aws_promise_fail(struct aws_promise *promise, int error_code); - -/* - * Returns whether or not the promise has completed (regardless of success or failure) - */ -AWS_COMMON_API -bool aws_promise_is_complete(struct aws_promise *promise); - -/* - * Returns the error code recorded if the promise failed, or 0 if it succeeded - * NOTE: It is fatal to attempt to retrieve the error code before the promise is completed - */ -AWS_COMMON_API -int aws_promise_error_code(struct aws_promise *promise); - -/* - * Returns the value provided to the promise if it succeeded, or NULL if none was provided - * or the promise failed. Check `aws_promise_error_code` to be sure. - * NOTE: The ownership of the value is retained by the promise. - * NOTE: It is fatal to attempt to retrieve the value before the promise is completed - */ -AWS_COMMON_API -void *aws_promise_value(struct aws_promise *promise); - -/* - * Returns the value provided to the promise if it succeeded, or NULL if none was provided - * or the promise failed. Check `aws_promise_error_code` to be sure. - * NOTE: The promise relinquishes ownership of the value, the caller is now responsible for - * freeing any resources associated with the value - * NOTE: It is fatal to attempt to take the value before the promise is completed - */ -AWS_COMMON_API -void *aws_promise_take_value(struct aws_promise *promise); - -AWS_EXTERN_C_END -AWS_POP_SANE_WARNING_LEVEL - -#endif // AWS_COMMON_PROMISE_H diff --git a/include/aws/common/rw_lock.h b/include/aws/common/rw_lock.h index f8d86ae2a..3ee778537 100644 --- a/include/aws/common/rw_lock.h +++ b/include/aws/common/rw_lock.h @@ -8,7 +8,7 @@ #include #ifdef _WIN32 -/* NOTE: Do not use this macro before including Windows.h */ +/* NOTE: Do not use this macro before including windows.h */ # define AWSSRW_TO_WINDOWS(pCV) (PSRWLOCK) pCV #else # include @@ -25,11 +25,9 @@ struct aws_rw_lock { }; #ifdef _WIN32 -# define AWS_RW_LOCK_INIT \ - { .lock_handle = NULL } +# define AWS_RW_LOCK_INIT {.lock_handle = NULL} #else -# define AWS_RW_LOCK_INIT \ - { .lock_handle = PTHREAD_RWLOCK_INITIALIZER } +# define AWS_RW_LOCK_INIT {.lock_handle = PTHREAD_RWLOCK_INITIALIZER} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/statistics.h b/include/aws/common/statistics.h index 3ad06a429..efeed7228 100644 --- a/include/aws/common/statistics.h +++ b/include/aws/common/statistics.h @@ -18,9 +18,12 @@ struct aws_array_list; typedef uint32_t aws_crt_statistics_category_t; /* Each library gets space for 2^^8 category entries */ -#define AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS 8 +enum { + AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS = 8, +}; + #define AWS_CRT_STATISTICS_CATEGORY_STRIDE (1U << AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS) -#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x)*AWS_CRT_STATISTICS_CATEGORY_STRIDE) +#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x) * AWS_CRT_STATISTICS_CATEGORY_STRIDE) #define AWS_CRT_STATISTICS_CATEGORY_END_RANGE(x) (((x) + 1) * AWS_CRT_STATISTICS_CATEGORY_STRIDE - 1) /** diff --git a/include/aws/common/system_info.h b/include/aws/common/system_info.h index fe7604120..91da41f9d 100644 --- a/include/aws/common/system_info.h +++ b/include/aws/common/system_info.h @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0. */ +#include #include AWS_PUSH_SANE_WARNING_LEVEL @@ -21,8 +22,54 @@ struct aws_cpu_info { bool suspected_hyper_thread; }; +struct aws_system_environment; + AWS_EXTERN_C_BEGIN +/** + * Allocates and initializes information about the system the current process is executing on. + * If successful returns an instance of aws_system_environment. If it fails, it will return NULL. + * + * Note: This api is used internally and is still early in its evolution. + * It may change in incompatible ways in the future. + */ +AWS_COMMON_API +struct aws_system_environment *aws_system_environment_load(struct aws_allocator *allocator); + +AWS_COMMON_API +struct aws_system_environment *aws_system_environment_acquire(struct aws_system_environment *env); + +AWS_COMMON_API +void aws_system_environment_release(struct aws_system_environment *env); + +/** + * Returns the virtualization vendor for the specified compute environment, e.g. "Xen, Amazon EC2, etc..." + * + * The return value may be empty and in that case no vendor was detected. + */ +AWS_COMMON_API +struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(const struct aws_system_environment *env); + +/** + * Returns the product name for the specified compute environment. For example, the Amazon EC2 Instance type. + * + * The return value may be empty and in that case no vendor was detected. + */ +AWS_COMMON_API +struct aws_byte_cursor aws_system_environment_get_virtualization_product_name(const struct aws_system_environment *env); + +/** + * Returns the number of processors for the specified compute environment. + */ +AWS_COMMON_API +size_t aws_system_environment_get_processor_count(struct aws_system_environment *env); + +/** + * Returns the number of separate cpu groupings (multi-socket configurations or NUMA). + */ +AWS_COMMON_API +size_t aws_system_environment_get_cpu_group_count(const struct aws_system_environment *env); + /* Returns the OS this was built under */ AWS_COMMON_API enum aws_platform_os aws_get_platform_build_os(void); diff --git a/include/aws/common/system_resource_util.h b/include/aws/common/system_resource_util.h new file mode 100644 index 000000000..ffe8d53e4 --- /dev/null +++ b/include/aws/common/system_resource_util.h @@ -0,0 +1,30 @@ +#ifndef AWS_COMMON_SYSTEM_RESOURCE_UTIL_H +#define AWS_COMMON_SYSTEM_RESOURCE_UTIL_H + +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +AWS_PUSH_SANE_WARNING_LEVEL + +AWS_EXTERN_C_BEGIN + +struct aws_memory_usage_stats { + size_t maxrss; /* max resident set size in kilobytes since program start */ + size_t page_faults; /* num of page faults since program start */ + + size_t _reserved[8]; +}; + +/* + * Get memory usage for current process. + * Raises AWS_ERROR_SYS_CALL_FAILURE on failure. + */ +AWS_COMMON_API int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage); + +AWS_EXTERN_C_END +AWS_POP_SANE_WARNING_LEVEL + +#endif /* AWS_COMMON_SYSTEM_RESOURCE_UTIL_H */ diff --git a/include/aws/common/thread.h b/include/aws/common/thread.h index 90f0bc363..84180e690 100644 --- a/include/aws/common/thread.h +++ b/include/aws/common/thread.h @@ -90,8 +90,7 @@ struct aws_thread_options { typedef union { void *ptr; } aws_thread_once; -# define AWS_THREAD_ONCE_STATIC_INIT \ - { NULL } +# define AWS_THREAD_ONCE_STATIC_INIT {NULL} typedef unsigned long aws_thread_id_t; #else typedef pthread_once_t aws_thread_once; diff --git a/include/aws/common/uri.h b/include/aws/common/uri.h index 0ac1fd9b3..137ba7df8 100644 --- a/include/aws/common/uri.h +++ b/include/aws/common/uri.h @@ -23,7 +23,7 @@ struct aws_uri { struct aws_byte_cursor user; struct aws_byte_cursor password; struct aws_byte_cursor host_name; - uint16_t port; + uint32_t port; struct aws_byte_cursor path; struct aws_byte_cursor query_string; struct aws_byte_cursor path_and_query; @@ -49,7 +49,7 @@ struct aws_uri_builder_options { struct aws_byte_cursor scheme; struct aws_byte_cursor path; struct aws_byte_cursor host_name; - uint16_t port; + uint32_t port; struct aws_array_list *query_params; struct aws_byte_cursor query_string; }; @@ -107,13 +107,35 @@ AWS_COMMON_API const struct aws_byte_cursor *aws_uri_host_name(const struct aws_ * Returns the port portion of the authority if it was present, otherwise, returns 0. * If this is 0, it is the users job to determine the correct port based on scheme and protocol. */ -AWS_COMMON_API uint16_t aws_uri_port(const struct aws_uri *uri); +AWS_COMMON_API uint32_t aws_uri_port(const struct aws_uri *uri); /** * Returns the path and query portion of the uri (i.e., the thing you send across the wire). */ AWS_COMMON_API const struct aws_byte_cursor *aws_uri_path_and_query(const struct aws_uri *uri); +/** + * For iterating over the params in the query string. + * `param` is an in/out argument used to track progress, it MUST be zeroed out to start. + * If true is returned, `param` contains the value of the next param. + * If false is returned, there are no further params. + * + * Edge cases: + * 1) Entries without '=' sign are treated as having a key and no value. + * Example: First param in query string "a&b=c" has key="a" value="" + * + * 2) Blank entries are skipped. + * Example: The only param in query string "&&a=b" is key="a" value="b" + */ +AWS_COMMON_API bool aws_query_string_next_param(struct aws_byte_cursor query_string, struct aws_uri_param *param); + +/** + * Parses query string and stores the parameters in 'out_params'. Returns AWS_OP_SUCCESS on success and + * AWS_OP_ERR on failure. The user is responsible for initializing out_params with item size of struct aws_query_param. + * The user is also responsible for cleaning up out_params when finished. + */ +AWS_COMMON_API int aws_query_string_params(struct aws_byte_cursor query_string, struct aws_array_list *out_params); + /** * For iterating over the params in the uri query string. * `param` is an in/out argument used to track progress, it MUST be zeroed out to start. diff --git a/include/aws/common/uuid.h b/include/aws/common/uuid.h index 4bd92c832..e3d9b1ade 100644 --- a/include/aws/common/uuid.h +++ b/include/aws/common/uuid.h @@ -17,7 +17,7 @@ struct aws_uuid { }; /* 36 bytes for the UUID plus one more for the null terminator. */ -#define AWS_UUID_STR_LEN 37 +enum { AWS_UUID_STR_LEN = 37 }; AWS_EXTERN_C_BEGIN diff --git a/include/aws/testing/aws_test_harness.h b/include/aws/testing/aws_test_harness.h index 92427c2cc..769e75fc9 100644 --- a/include/aws/testing/aws_test_harness.h +++ b/include/aws/testing/aws_test_harness.h @@ -42,6 +42,7 @@ the AWS_UNSTABLE_TESTING_API compiler flag /** Prints a message to AWS_TESTING_REPORT_FD using printf format that appends the function, file and line number. * If format is null, returns 0 without printing anything; otherwise returns 1. + * If function or file are null, the function, file and line number are not appended. */ static int s_cunit_failure_message0( const char *prefix, @@ -61,7 +62,11 @@ static int s_cunit_failure_message0( vfprintf(AWS_TESTING_REPORT_FD, format, ap); va_end(ap); - fprintf(AWS_TESTING_REPORT_FD, " [%s(): %s:%d]\n", function, file, line); + if (function && file) { + fprintf(AWS_TESTING_REPORT_FD, " [%s(): %s:%d]\n", function, file, line); + } else { + fprintf(AWS_TESTING_REPORT_FD, "\n"); + } return 1; } @@ -70,27 +75,13 @@ static int s_cunit_failure_message0( #define CUNIT_FAILURE_MESSAGE(func, file, line, format, ...) \ s_cunit_failure_message0(FAIL_PREFIX, func, file, line, format, #__VA_ARGS__) -static int total_failures; -static int total_skip; - #define SUCCESS (0) #define FAILURE (-1) -/* The skip code returned to ctest to indicate the test is skipped. Refer to cmake doc: - * https://cmake.org/cmake/help/latest/prop_test/SKIP_RETURN_CODE.html */ -#define SKIP (1) - -#define POSTSKIP_INTERNAL() \ - do { \ - total_skip++; \ - return SKIP; \ - } while (0) - -#define RETURN_SKIP(format, ...) \ - do { \ - printf(format, ##__VA_ARGS__); \ - printf("\n"); \ - POSTSKIP_INTERNAL(); \ - } while (0) +/* The exit code returned to ctest to indicate the test is skipped. Refer to cmake doc: + * https://cmake.org/cmake/help/latest/prop_test/SKIP_RETURN_CODE.html + * The value has no special meaning, it's just an arbitrary exit code reducing the chance of clashing with exit codes + * that may be returned from various tools (e.g. sanitizer). */ +#define SKIP (103) #define RETURN_SUCCESS(format, ...) \ do { \ @@ -104,9 +95,11 @@ static int total_skip; #define PRINT_FAIL_INTERNAL0(...) \ s_cunit_failure_message0(FAIL_PREFIX, __func__, __FILE__, __LINE__, ##__VA_ARGS__, (const char *)NULL) +#define PRINT_FAIL_WITHOUT_LOCATION(...) \ + s_cunit_failure_message0(FAIL_PREFIX, NULL, NULL, __LINE__, ##__VA_ARGS__, (const char *)NULL) + #define POSTFAIL_INTERNAL() \ do { \ - total_failures++; \ return FAILURE; \ } while (0) @@ -142,7 +135,7 @@ static int total_skip; if (assert_rv != AWS_OP_SUCCESS) { \ if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ PRINT_FAIL_INTERNAL0( \ - "Expected success at %s; got return value %d with last error 0x%04x\n", \ + "Expected success at %s; got return value %d with last error %d\n", \ #condition, \ assert_rv, \ aws_last_error()); \ @@ -157,7 +150,7 @@ static int total_skip; if (assert_rv != AWS_OP_ERR) { \ if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ PRINT_FAIL_INTERNAL0( \ - "Expected failure at %s; got return value %d with last error 0x%04x\n", \ + "Expected failure at %s; got return value %d with last error %d\n", \ #condition, \ assert_rv, \ aws_last_error()); \ @@ -174,7 +167,7 @@ static int total_skip; if (assert_rv != AWS_OP_ERR) { \ fprintf( \ AWS_TESTING_REPORT_FD, \ - "%sExpected error but no error occurred; rv=%d, aws_last_error=%04x (expected %04x): ", \ + "%sExpected error but no error occurred; rv=%d, aws_last_error=%d (expected %d): ", \ FAIL_PREFIX, \ assert_rv, \ assert_err, \ @@ -187,7 +180,7 @@ static int total_skip; if (assert_err != assert_err_expect) { \ fprintf( \ AWS_TESTING_REPORT_FD, \ - "%sIncorrect error code; aws_last_error=%04x (expected %04x): ", \ + "%sIncorrect error code; aws_last_error=%d (expected %d): ", \ FAIL_PREFIX, \ assert_err, \ assert_err_expect); \ @@ -463,7 +456,7 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) { test_res |= harness->on_after(allocator, setup_res, harness->ctx); } - if (test_res != AWS_OP_SUCCESS && test_res != AWS_OP_SKIP) { + if (test_res != AWS_OP_SUCCESS) { goto fail; } @@ -472,10 +465,12 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) { * but aws_mem_tracer_dump() needs a valid logger to be active */ aws_logger_set(&err_logger); - const size_t leaked_bytes = aws_mem_tracer_count(allocator); + const size_t leaked_allocations = aws_mem_tracer_count(allocator); + const size_t leaked_bytes = aws_mem_tracer_bytes(allocator); if (leaked_bytes) { aws_mem_tracer_dump(allocator); - PRINT_FAIL_INTERNAL0("Test leaked memory: %zu bytes", leaked_bytes); + PRINT_FAIL_WITHOUT_LOCATION( + "Test leaked memory: %zu bytes %zu allocations", leaked_bytes, leaked_allocations); goto fail; } @@ -485,21 +480,28 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) { aws_logger_set(NULL); aws_logger_clean_up(&err_logger); - if (test_res == AWS_OP_SUCCESS) { - RETURN_SUCCESS("%s [ \033[32mOK\033[0m ]", harness->test_name); - } else if (test_res == AWS_OP_SKIP) { - RETURN_SKIP("%s [ \033[32mSKIP\033[0m ]", harness->test_name); - } + RETURN_SUCCESS("%s [ \033[32mOK\033[0m ]", harness->test_name); fail: - FAIL("%s [ \033[31mFAILED\033[0m ]", harness->test_name); + if (test_res == AWS_OP_SKIP) { + fprintf(AWS_TESTING_REPORT_FD, "%s [ \033[32mSKIP\033[0m ]\n", harness->test_name); + } else { + PRINT_FAIL_WITHOUT_LOCATION("%s [ \033[31mFAILED\033[0m ]", harness->test_name); + } + /* Use _Exit() to terminate without cleaning up resources. + * This prevents LeakSanitizer spam (yes, we know failing tests don't bother cleaning up). + * It also prevents errors where threads that haven't cleaned are still using the logger declared in this fn. */ + fflush(AWS_TESTING_REPORT_FD); + fflush(stdout); + fflush(stderr); + _Exit(test_res == AWS_OP_SKIP ? SKIP : FAILURE); } /* Enables terminal escape sequences for text coloring on Windows. */ /* https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences */ #ifdef _WIN32 -# include +# include # ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING # define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 diff --git a/scripts/import_libcbor.py b/scripts/import_libcbor.py new file mode 100644 index 000000000..37b5df9a3 --- /dev/null +++ b/scripts/import_libcbor.py @@ -0,0 +1,129 @@ +import os +import tempfile +import shutil +import subprocess +import argparse +import re + + +def parse_version(version_string): + match = re.fullmatch(r'v(\d+)\.(\d+)\.(\d+)', version_string) + if not match: + raise ValueError("Invalid version string") + return match.group(1), match.group(2), match.group(3) + + +argument_parser = argparse.ArgumentParser( + description="Helper to import libcbor as external dependency.") + +argument_parser.add_argument("--version", + required=True, help="Version string to import") + +args = argument_parser.parse_args() +major_version, minor_version, patch_version = parse_version(args.version) + +GENERATED_NOTES = """/** + * DO NOT DIRECTLY MODIFY THIS FILE: + * + * The code in this file is generated from scripts/import_libcbor.py + * and any modifications should be in there. + */ +""" + +CBOR_EXPORT_H = """ +#ifndef CBOR_EXPORT_H +#define CBOR_EXPORT_H + +/* Don't export anything from libcbor */ +#define CBOR_EXPORT + +#endif /* CBOR_EXPORT_H */ +""" + +CONFIGURATION_H = f""" +#ifndef LIBCBOR_CONFIGURATION_H +#define LIBCBOR_CONFIGURATION_H + +#define CBOR_MAJOR_VERSION {major_version} +#define CBOR_MINOR_VERSION {minor_version} +#define CBOR_PATCH_VERSION {patch_version} + +#define CBOR_BUFFER_GROWTH 2 +#define CBOR_MAX_STACK_SIZE 2048 +#define CBOR_PRETTY_PRINTER 1 + +#if defined(_MSC_VER) +# define CBOR_RESTRICT_SPECIFIER +#else +# define CBOR_RESTRICT_SPECIFIER restrict +#endif + +#define CBOR_INLINE_SPECIFIER + +/* Ignore the compiler warnings for libcbor. */ +#ifdef _MSC_VER +# pragma warning(disable : 4028) +# pragma warning(disable : 4715) +# pragma warning(disable : 4232) +# pragma warning(disable : 4068) +# pragma warning(disable : 4244) +# pragma warning(disable : 4701) +# pragma warning(disable : 4703) +#endif + +#ifdef __clang__ +# pragma clang diagnostic ignored "-Wreturn-type" +#elif defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wreturn-type" +# pragma GCC diagnostic ignored "-Wunknown-pragmas" +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + +#endif // LIBCBOR_CONFIGURATION_H +""" + + +# Create a temporary directory for cloning the repository +temp_repo_dir = tempfile.mkdtemp() + +try: + # Clone the repository into the temporary directory + repo_url = "https://github.com/PJK/libcbor.git" + clone_command = f"git clone {repo_url} {temp_repo_dir}" + subprocess.run(clone_command, shell=True, check=True) + subprocess.run(["git", "checkout", "tags/" + args.version], + cwd=temp_repo_dir, check=True) + + # Create a separate folder for the copied files + output_dir = os.path.join( + os.path.dirname(__file__), "..", "source", "external", "libcbor") + shutil.rmtree(output_dir, ignore_errors=True) + os.makedirs(output_dir, exist_ok=True) + + # Copy files ending with .c and .h from the /src directory + src_dir = os.path.join(temp_repo_dir, "src") + for root, dirs, files in os.walk(src_dir): + dir_name = os.path.basename(root) + for file in files: + if file.endswith((".c", ".h")): + # copy the source code to ../source/external/libcbor + src_file = os.path.join(root, file) + rel_path = os.path.relpath(src_file, src_dir) + dest_file = os.path.join(output_dir, rel_path) + os.makedirs(os.path.dirname(dest_file), exist_ok=True) + shutil.copy(src_file, dest_file) + + # Use our customized configurations + with open(os.path.join(output_dir, "cbor/cbor_export.h"), "w") as file: + file.write(GENERATED_NOTES) + file.write(CBOR_EXPORT_H) + with open(os.path.join(output_dir, "cbor/configuration.h"), "w") as file: + file.write(GENERATED_NOTES) + file.write(CONFIGURATION_H) + +except Exception as e: + print(f"An error occurred: {e}") + +finally: + # Remove the temporary directory + shutil.rmtree(temp_repo_dir, ignore_errors=True) diff --git a/scripts/latest_submodules.py b/scripts/latest_submodules.py index 47bfeaf90..a602b5a24 100755 --- a/scripts/latest_submodules.py +++ b/scripts/latest_submodules.py @@ -53,7 +53,7 @@ def get_submodules(): return sorted(submodules, key=lambda x: x['name']) -def get_release_tags(): +def get_release_tags(prefix='v'): """ Return list of release tags for current repo, sorted high to low. @@ -69,7 +69,7 @@ def get_release_tags(): for line in git_output.splitlines(): # line looks like: "e18f041a0c8d17189f2eae2a32f16e0a7a3f0f1c refs/tags/v0.5.18" match = re.match( - r'([a-f0-9]+)\s+refs/tags/(v([0-9]+)\.([0-9]+)\.([0-9]+))$', line) + r'([a-f0-9]+)\s+refs/tags/(' + prefix + r'([0-9]+)\.([0-9]+)\.([0-9]+))$', line) if not match: # skip malformed release tags continue @@ -118,7 +118,12 @@ def main(): os.chdir(os.path.join(root_path, submodule['path'])) - tags = get_release_tags() + version_prefix = 'v' + # aws-crt-java uses FIPS releases of aws-lc + if name == 'aws-lc' and 'aws-crt-java' in root_path: + version_prefix = 'AWS-LC-FIPS-' + + tags = get_release_tags(version_prefix) current_commit = get_current_commit() current_tag = get_tag_for_commit(tags, current_commit) sync_from = current_tag['version'] if current_tag else current_commit diff --git a/source/allocator.c b/source/allocator.c index 67e869599..71f7a64b0 100644 --- a/source/allocator.c +++ b/source/allocator.c @@ -12,7 +12,7 @@ #include #ifdef _WIN32 -# include +# include #endif #ifdef __MACH__ @@ -34,7 +34,7 @@ bool aws_allocator_is_valid(const struct aws_allocator *alloc) { return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release; } -static void *s_default_malloc(struct aws_allocator *allocator, size_t size) { +static void *s_aligned_malloc(struct aws_allocator *allocator, size_t size) { (void)allocator; /* larger allocations should be aligned so that AVX and friends can avoid * the extra preamble during unaligned versions of memcpy/memset on big buffers @@ -48,7 +48,7 @@ static void *s_default_malloc(struct aws_allocator *allocator, size_t size) { * We use PAGE_SIZE as the boundary because we are not aware of any allocations of * this size or greater that are not data buffers */ - const size_t alignment = sizeof(void *) * (size > PAGE_SIZE ? 8 : 2); + const size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2); #if !defined(_WIN32) void *result = NULL; int err = posix_memalign(&result, alignment, size); @@ -62,7 +62,7 @@ static void *s_default_malloc(struct aws_allocator *allocator, size_t size) { #endif } -static void s_default_free(struct aws_allocator *allocator, void *ptr) { +static void s_aligned_free(struct aws_allocator *allocator, void *ptr) { (void)allocator; #if !defined(_WIN32) free(ptr); @@ -71,7 +71,7 @@ static void s_default_free(struct aws_allocator *allocator, void *ptr) { #endif } -static void *s_default_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) { +static void *s_aligned_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) { (void)allocator; (void)oldsize; AWS_FATAL_PRECONDITION(newsize); @@ -82,41 +82,92 @@ static void *s_default_realloc(struct aws_allocator *allocator, void *ptr, size_ } /* newsize is > oldsize, need more memory */ - void *new_mem = s_default_malloc(allocator, newsize); - AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_default_malloc"); + void *new_mem = s_aligned_malloc(allocator, newsize); + AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_aligned_malloc"); if (ptr) { memcpy(new_mem, ptr, oldsize); - s_default_free(allocator, ptr); + s_aligned_free(allocator, ptr); } return new_mem; #else - const size_t alignment = sizeof(void *) * (newsize > PAGE_SIZE ? 8 : 2); + const size_t alignment = sizeof(void *) * (newsize > (size_t)PAGE_SIZE ? 8 : 2); void *new_mem = _aligned_realloc(ptr, newsize, alignment); AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in _aligned_realloc"); return new_mem; #endif } -static void *s_default_calloc(struct aws_allocator *allocator, size_t num, size_t size) { - void *mem = s_default_malloc(allocator, num * size); - AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_default_malloc"); +static void *s_aligned_calloc(struct aws_allocator *allocator, size_t num, size_t size) { + void *mem = s_aligned_malloc(allocator, num * size); + AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_aligned_calloc"); memset(mem, 0, num * size); return mem; } +static void *s_non_aligned_malloc(struct aws_allocator *allocator, size_t size) { + (void)allocator; + void *result = malloc(size); + AWS_PANIC_OOM(result, "malloc failed to allocate memory"); + return result; +} + +static void s_non_aligned_free(struct aws_allocator *allocator, void *ptr) { + (void)allocator; + free(ptr); +} + +static void *s_non_aligned_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) { + (void)allocator; + (void)oldsize; + AWS_FATAL_PRECONDITION(newsize); + + if (newsize <= oldsize) { + return ptr; + } + + /* newsize is > oldsize, need more memory */ + void *new_mem = s_non_aligned_malloc(allocator, newsize); + AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_non_aligned_realloc"); + + if (ptr) { + memcpy(new_mem, ptr, oldsize); + s_non_aligned_free(allocator, ptr); + } + + return new_mem; +} + +static void *s_non_aligned_calloc(struct aws_allocator *allocator, size_t num, size_t size) { + (void)allocator; + void *mem = calloc(num, size); + AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_non_aligned_calloc"); + return mem; +} + static struct aws_allocator default_allocator = { - .mem_acquire = s_default_malloc, - .mem_release = s_default_free, - .mem_realloc = s_default_realloc, - .mem_calloc = s_default_calloc, + .mem_acquire = s_non_aligned_malloc, + .mem_release = s_non_aligned_free, + .mem_realloc = s_non_aligned_realloc, + .mem_calloc = s_non_aligned_calloc, }; struct aws_allocator *aws_default_allocator(void) { return &default_allocator; } +static struct aws_allocator aligned_allocator = { + .mem_acquire = s_aligned_malloc, + .mem_release = s_aligned_free, + .mem_realloc = s_aligned_realloc, + .mem_calloc = s_aligned_calloc, +}; + +struct aws_allocator *aws_aligned_allocator(void) { + return &aligned_allocator; +} + void *aws_mem_acquire(struct aws_allocator *allocator, size_t size) { AWS_FATAL_PRECONDITION(allocator != NULL); AWS_FATAL_PRECONDITION(allocator->mem_acquire != NULL); @@ -156,7 +207,7 @@ void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) { return mem; } -#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment)-1)) & ~((alignment)-1)) +#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1)) void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...) { @@ -289,7 +340,7 @@ static void *s_cf_allocator_reallocate(void *ptr, CFIndex new_size, CFOptionFlag memcpy(&original_size, original_allocation, sizeof(size_t)); aws_mem_realloc(allocator, &original_allocation, original_size, (size_t)new_size); - + AWS_FATAL_ASSERT(original_allocation); size_t new_allocation_size = (size_t)new_size; memcpy(original_allocation, &new_allocation_size, sizeof(size_t)); @@ -306,7 +357,7 @@ static CFIndex s_cf_allocator_preferred_size(CFIndex size, CFOptionFlags hint, v (void)hint; (void)info; - return size + sizeof(size_t); + return (CFIndex)(size + sizeof(size_t)); } CFAllocatorRef aws_wrapped_cf_allocator_new(struct aws_allocator *allocator) { diff --git a/source/allocator_sba.c b/source/allocator_sba.c index 91a478f4f..ab1e833f0 100644 --- a/source/allocator_sba.c +++ b/source/allocator_sba.c @@ -44,7 +44,7 @@ #define AWS_SBA_TAG_VALUE 0x736f6d6570736575ULL /* list of sizes of bins, must be powers of 2, and less than AWS_SBA_PAGE_SIZE * 0.5 */ -#define AWS_SBA_BIN_COUNT 5 +enum { AWS_SBA_BIN_COUNT = 5 }; static const size_t s_bin_sizes[AWS_SBA_BIN_COUNT] = {32, 64, 128, 256, 512}; static const size_t s_max_bin_size = 512; @@ -348,7 +348,7 @@ static void s_sba_free_to_bin(struct sba_bin *bin, void *addr) { uint8_t *page_start = (uint8_t *)page + sizeof(struct page_header); uint8_t *page_end = page_start + AWS_SBA_PAGE_SIZE; /* Remove all chunks in the page from the free list */ - intptr_t chunk_idx = bin->free_chunks.length; + intptr_t chunk_idx = (intptr_t)bin->free_chunks.length; for (; chunk_idx >= 0; --chunk_idx) { uint8_t *chunk = NULL; aws_array_list_get_at(&bin->free_chunks, &chunk, chunk_idx); diff --git a/source/android/logging.c b/source/android/logging.c index 06247efaf..303bf36bf 100644 --- a/source/android/logging.c +++ b/source/android/logging.c @@ -5,9 +5,11 @@ #include +#include #include #include +#include #include #define LOGCAT_MAX_BUFFER_SIZE (4 * 1024) @@ -58,6 +60,18 @@ static int s_logcat_format(struct logcat_format_data *formatting_data, va_list a current_index = s_advance_and_clamp_index(current_index, thread_id_written, fake_total_length); } + if (current_index < fake_total_length) { + uint64_t now = 0; + aws_high_res_clock_get_ticks(&now); + + int current_time_written = snprintf( + formatting_data->buffer + current_index, fake_total_length - current_index, "(HRC:%" PRIu64 ") ", now); + if (current_time_written < 0) { + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } + current_index = s_advance_and_clamp_index(current_index, current_time_written, fake_total_length); + } + if (current_index < fake_total_length) { int separator_written = snprintf(formatting_data->buffer + current_index, fake_total_length - current_index, " - "); @@ -93,7 +107,9 @@ static int s_logcat_format(struct logcat_format_data *formatting_data, va_list a return AWS_OP_SUCCESS; } -static struct aws_logger_logcat { enum aws_log_level level; } s_logcat_impl; +static struct aws_logger_logcat { + enum aws_log_level level; +} s_logcat_impl; static int s_logcat_log( struct aws_logger *logger, diff --git a/source/arch/arm/asm/cpuid.c b/source/arch/arm/auxv/cpuid.c similarity index 86% rename from source/arch/arm/asm/cpuid.c rename to source/arch/arm/auxv/cpuid.c index 6a306df98..10499da73 100644 --- a/source/arch/arm/asm/cpuid.c +++ b/source/arch/arm/auxv/cpuid.c @@ -29,7 +29,9 @@ struct cap_bits { # if (defined(__aarch64__)) struct cap_bits s_check_cap[AWS_CPU_FEATURE_COUNT] = { - [AWS_CPU_FEATURE_ARM_CRC] = {0, 1 << 7 /* HWCAP_CRC */}, + [AWS_CPU_FEATURE_ARM_CRC] = {0, 1 << 7 /* HWCAP_CRC32 */}, + [AWS_CPU_FEATURE_ARM_PMULL] = {0, 1 << 4 /* HWCAP_PMULL */}, + [AWS_CPU_FEATURE_ARM_CRYPTO] = {0, 1 << 3 /* HWCAP_AES */}, }; # else struct cap_bits s_check_cap[AWS_CPU_FEATURE_COUNT] = { @@ -67,6 +69,10 @@ bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) { switch (feature_name) { case AWS_CPU_FEATURE_ARM_CRC: +# if (defined(__aarch64__)) + case AWS_CPU_FEATURE_ARM_PMULL: + case AWS_CPU_FEATURE_ARM_CRYPTO: +# endif // (defined(__aarch64__)) return s_hwcap[s_check_cap[feature_name].cap] & s_check_cap[feature_name].bit; default: return false; diff --git a/source/arch/arm/darwin/cpuid.c b/source/arch/arm/darwin/cpuid.c new file mode 100644 index 000000000..7552d4220 --- /dev/null +++ b/source/arch/arm/darwin/cpuid.c @@ -0,0 +1,40 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +#include + +#include + +bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) { + int64_t ret = 0; + size_t size = sizeof(ret); + + switch (feature_name) { + case AWS_CPU_FEATURE_ARM_PMULL: + if (sysctlbyname("hw.optional.arm.FEAT_PMULL", &ret, &size, NULL, 0) != -1) { + return ret == 1; + } + case AWS_CPU_FEATURE_ARM_CRC: + if (sysctlbyname("hw.optional.armv8_crc32", &ret, &size, NULL, 0) != -1) { + return ret == 1; + } + case AWS_CPU_FEATURE_ARM_CRYPTO: + if (sysctlbyname("hw.optional.arm.FEAT_AES", &ret, &size, NULL, 0) != -1) { + return ret == 1; + } + default: + return false; + } +} diff --git a/source/arch/arm/msvc/cpuid.c b/source/arch/arm/windows/cpuid.c similarity index 57% rename from source/arch/arm/msvc/cpuid.c rename to source/arch/arm/windows/cpuid.c index c10c5d15c..b7da0053f 100644 --- a/source/arch/arm/msvc/cpuid.c +++ b/source/arch/arm/windows/cpuid.c @@ -13,9 +13,18 @@ * permissions and limitations under the License. */ +#include #include -#include bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) { - return false; + switch (feature_name) { + case AWS_CPU_FEATURE_ARM_CRC: + return IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) != 0; + // this is the best we've got on windows as they don't separate PMULL and AES from each other. + case AWS_CPU_FEATURE_ARM_PMULL: + case AWS_CPU_FEATURE_ARM_CRYPTO: + return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0; + default: + return false; + } } diff --git a/source/arch/intel/cpuid.c b/source/arch/intel/cpuid.c index 1d8c2c860..465fccd17 100644 --- a/source/arch/intel/cpuid.c +++ b/source/arch/intel/cpuid.c @@ -113,6 +113,18 @@ static bool s_has_bmi2(void) { return true; } +static bool s_has_vpclmulqdq(void) { + uint32_t abcd[4]; + /* Check VPCLMULQDQ: + * CPUID.(EAX=07H, ECX=0H):ECX.VPCLMULQDQ[bit 10]==1 */ + uint32_t vpclmulqdq_mask = (1 << 10); + aws_run_cpuid(7, 0, abcd); + if ((abcd[2] & vpclmulqdq_mask) != vpclmulqdq_mask) { + return false; + } + return true; +} + has_feature_fn *s_check_cpu_feature[AWS_CPU_FEATURE_COUNT] = { [AWS_CPU_FEATURE_CLMUL] = s_has_clmul, [AWS_CPU_FEATURE_SSE_4_1] = s_has_sse41, @@ -120,6 +132,7 @@ has_feature_fn *s_check_cpu_feature[AWS_CPU_FEATURE_COUNT] = { [AWS_CPU_FEATURE_AVX2] = s_has_avx2, [AWS_CPU_FEATURE_AVX512] = s_has_avx512, [AWS_CPU_FEATURE_BMI2] = s_has_bmi2, + [AWS_CPU_FEATURE_VPCLMULQDQ] = s_has_vpclmulqdq, }; bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) { diff --git a/source/arch/intel/encoding_avx2.c b/source/arch/intel/encoding_avx2.c index ebae86138..92c4abdfa 100644 --- a/source/arch/intel/encoding_avx2.c +++ b/source/arch/intel/encoding_avx2.c @@ -179,7 +179,7 @@ static inline bool decode(const unsigned char *in, unsigned char *out) { * so we'll just copy right out of the vector as a fallback */ -#ifdef HAVE_MM256_EXTRACT_EPI64 +#ifdef AWS_HAVE_MM256_EXTRACT_EPI64 uint64_t hi = _mm256_extract_epi64(vec, 2); const uint64_t *p_hi = &hi; #else @@ -194,13 +194,13 @@ static inline bool decode(const unsigned char *in, unsigned char *out) { size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len) { if (len % 4) { - return (size_t)-1; + return SIZE_MAX; } size_t outlen = 0; while (len > 32) { if (!decode(in, out)) { - return (size_t)-1; + return SIZE_MAX; } len -= 32; in += 32; @@ -230,13 +230,13 @@ size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned } if (!decode(tmp_in, tmp_out)) { - return (size_t)-1; + return SIZE_MAX; } /* Check that there are no trailing ones bits */ for (size_t i = final_out; i < sizeof(tmp_out); i++) { if (tmp_out[i]) { - return (size_t)-1; + return SIZE_MAX; } } diff --git a/source/array_list.c b/source/array_list.c index b6ea5a090..a890115e9 100644 --- a/source/array_list.c +++ b/source/array_list.c @@ -184,9 +184,12 @@ static void aws_array_list_mem_swap(void *AWS_RESTRICT item1, void *AWS_RESTRICT } size_t remainder = item_size & (SLICE - 1); /* item_size % SLICE */ - memcpy((void *)temp, (void *)item1, remainder); - memcpy((void *)item1, (void *)item2, remainder); - memcpy((void *)item2, (void *)temp, remainder); + + if (remainder) { + memcpy((void *)temp, (void *)item1, remainder); + memcpy((void *)item1, (void *)item2, remainder); + memcpy((void *)item2, (void *)temp, remainder); + } } void aws_array_list_swap(struct aws_array_list *AWS_RESTRICT list, size_t a, size_t b) { diff --git a/source/byte_buf.c b/source/byte_buf.c index b815b6bfb..da3748e81 100644 --- a/source/byte_buf.c +++ b/source/byte_buf.c @@ -816,6 +816,25 @@ int aws_byte_buf_reserve_relative(struct aws_byte_buf *buffer, size_t additional return aws_byte_buf_reserve(buffer, requested_capacity); } +int aws_byte_buf_reserve_smart(struct aws_byte_buf *buffer, size_t requested_capacity) { + + if (requested_capacity <= buffer->capacity) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); + return AWS_OP_SUCCESS; + } + size_t double_current_capacity = aws_add_size_saturating(buffer->capacity, buffer->capacity); + size_t new_capacity = aws_max_size(requested_capacity, double_current_capacity); + return aws_byte_buf_reserve(buffer, new_capacity); +} + +int aws_byte_buf_reserve_smart_relative(struct aws_byte_buf *buffer, size_t additional_length) { + size_t requested_capacity = 0; + if (AWS_UNLIKELY(aws_add_size_checked(buffer->len, additional_length, &requested_capacity))) { + return AWS_OP_ERR; + } + return aws_byte_buf_reserve_smart(buffer, requested_capacity); +} + struct aws_byte_cursor aws_byte_cursor_right_trim_pred( const struct aws_byte_cursor *source, aws_byte_predicate_fn *predicate) { diff --git a/source/cbor.c b/source/cbor.c new file mode 100644 index 000000000..b6ec4f239 --- /dev/null +++ b/source/cbor.c @@ -0,0 +1,647 @@ +#include "external/libcbor/cbor.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +static bool s_aws_cbor_module_initialized = false; + +const static size_t s_cbor_element_width_64bit = 9; +const static size_t s_cbor_element_width_32bit = 5; + +enum s_cbor_simple_val { + AWS_CBOR_SIMPLE_VAL_FALSE = 20, + AWS_CBOR_SIMPLE_VAL_TRUE = 21, + AWS_CBOR_SIMPLE_VAL_NULL = 22, + AWS_CBOR_SIMPLE_VAL_UNDEFINED = 23, + AWS_CBOR_SIMPLE_VAL_BREAK = 31, +}; + +void aws_cbor_module_init(struct aws_allocator *allocator) { + (void)allocator; + if (!s_aws_cbor_module_initialized) { + /* Not allow any allocation from libcbor */ + cbor_set_allocs(NULL, NULL, NULL); + s_aws_cbor_module_initialized = true; + } +} + +void aws_cbor_module_cleanup(void) { + if (s_aws_cbor_module_initialized) { + s_aws_cbor_module_initialized = false; + } +} + +/* Return c-string for aws_cbor_type */ +const char *aws_cbor_type_cstr(enum aws_cbor_type type) { + /* clang-format off */ + switch (type) { + case (AWS_CBOR_TYPE_UINT): return "AWS_CBOR_TYPE_UINT"; + case (AWS_CBOR_TYPE_NEGINT): return "AWS_CBOR_TYPE_NEGINT"; + case (AWS_CBOR_TYPE_FLOAT): return "AWS_CBOR_TYPE_FLOAT"; + case (AWS_CBOR_TYPE_BYTES): return "AWS_CBOR_TYPE_BYTES"; + case (AWS_CBOR_TYPE_TEXT): return "AWS_CBOR_TYPE_TEXT"; + case (AWS_CBOR_TYPE_ARRAY_START): return "AWS_CBOR_TYPE_ARRAY_START"; + case (AWS_CBOR_TYPE_MAP_START): return "AWS_CBOR_TYPE_MAP_START"; + case (AWS_CBOR_TYPE_TAG): return "AWS_CBOR_TYPE_TAG"; + case (AWS_CBOR_TYPE_BOOL): return "AWS_CBOR_TYPE_BOOL"; + case (AWS_CBOR_TYPE_NULL): return "AWS_CBOR_TYPE_NULL"; + case (AWS_CBOR_TYPE_UNDEFINED): return "AWS_CBOR_TYPE_UNDEFINED"; + case (AWS_CBOR_TYPE_BREAK): return "AWS_CBOR_TYPE_BREAK"; + case (AWS_CBOR_TYPE_INDEF_BYTES_START): return "AWS_CBOR_TYPE_INDEF_BYTES_START"; + case (AWS_CBOR_TYPE_INDEF_TEXT_START): return "AWS_CBOR_TYPE_INDEF_TEXT_START"; + case (AWS_CBOR_TYPE_INDEF_ARRAY_START): return "AWS_CBOR_TYPE_INDEF_ARRAY_START"; + case (AWS_CBOR_TYPE_INDEF_MAP_START): return "AWS_CBOR_TYPE_INDEF_MAP_START"; + default: return ""; + } + /* clang-format on */ +} + +/******************************************************************************* + * ENCODE + ******************************************************************************/ + +struct aws_cbor_encoder { + struct aws_allocator *allocator; + struct aws_byte_buf encoded_buf; +}; + +struct aws_cbor_encoder *aws_cbor_encoder_new(struct aws_allocator *allocator) { + struct aws_cbor_encoder *encoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_cbor_encoder)); + encoder->allocator = allocator; + aws_byte_buf_init(&encoder->encoded_buf, allocator, 256); + + return encoder; +} + +struct aws_cbor_encoder *aws_cbor_encoder_destroy(struct aws_cbor_encoder *encoder) { + aws_byte_buf_clean_up(&encoder->encoded_buf); + aws_mem_release(encoder->allocator, encoder); + return NULL; +} + +struct aws_byte_cursor aws_cbor_encoder_get_encoded_data(const struct aws_cbor_encoder *encoder) { + return aws_byte_cursor_from_buf(&encoder->encoded_buf); +} + +void aws_cbor_encoder_reset(struct aws_cbor_encoder *encoder) { + aws_byte_buf_reset(&encoder->encoded_buf, false); +} + +static uint8_t *s_get_encoder_current_position(struct aws_cbor_encoder *encoder) { + return encoder->encoded_buf.buffer + encoder->encoded_buf.len; +} + +static size_t s_get_encoder_remaining_len(struct aws_cbor_encoder *encoder) { + return encoder->encoded_buf.capacity - encoder->encoded_buf.len; +} + +/** + * @brief Marcos to ensure the encoder have enough space to encode the value into the buffer using given `fn`, and then + * encode it. + */ +#define ENCODE_THROUGH_LIBCBOR(encoder, length_to_reserve, value, fn) \ + do { \ + int error = aws_byte_buf_reserve_smart_relative(&(encoder)->encoded_buf, length_to_reserve); \ + (void)error; \ + AWS_FATAL_ASSERT(error == AWS_ERROR_SUCCESS); \ + size_t encoded_len = fn(value, s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); \ + AWS_FATAL_ASSERT((encoded_len) != 0); \ + (encoder)->encoded_buf.len += (encoded_len); \ + } while (false) + +void aws_cbor_encoder_write_uint(struct aws_cbor_encoder *encoder, uint64_t value) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_uint); +} + +void aws_cbor_encoder_write_negint(struct aws_cbor_encoder *encoder, uint64_t value) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_negint); +} + +void aws_cbor_encoder_write_single_float(struct aws_cbor_encoder *encoder, float value) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_32bit, value, cbor_encode_single); +} + +void aws_cbor_encoder_write_float(struct aws_cbor_encoder *encoder, double value) { + /** + * As suggested by AWS SDK SEP, write the float value as small as possible. But, do not encode to half-float. + * Convert the float value to integer if the conversion will not cause any precision loss. + */ + if (!isfinite(value)) { + /* For special value: NAN/INFINITY, type cast to float and encode into single float. */ + aws_cbor_encoder_write_single_float(encoder, (float)value); + return; + } + /* Conversation from int to floating-type is implementation defined if loss of precision */ + if (value <= (double)INT64_MAX && value >= (double)INT64_MIN) { + /** + * A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion + * truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot + * be represented in the destination type. + * Check against the INT64 range to avoid undefined behavior + * + * Comparing against INT64_MAX instead of UINT64_MAX to simplify the code, which may loss the opportunity to + * convert the UINT64 range from double to uint64_t. However, converting double to uint64_t will not benefit the + * total length encoded. + **/ + int64_t int_value = (int64_t)value; + if (value == (double)int_value) { + if (int_value < 0) { + aws_cbor_encoder_write_negint(encoder, (uint64_t)(-1 - int_value)); + } else { + aws_cbor_encoder_write_uint(encoder, (uint64_t)(int_value)); + } + return; + } + } + if (value <= FLT_MAX && value >= -FLT_MAX) { + /* Only try to convert the value within the range of float. */ + float float_value = (float)value; + double converted_value = (double)float_value; + /* Try to cast a round trip to detect any precision loss. */ + if (value == converted_value) { + aws_cbor_encoder_write_single_float(encoder, float_value); + return; + } + } + + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_double); +} + +void aws_cbor_encoder_write_map_start(struct aws_cbor_encoder *encoder, size_t number_entries) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, number_entries, cbor_encode_map_start); +} + +void aws_cbor_encoder_write_tag(struct aws_cbor_encoder *encoder, uint64_t tag_number) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, tag_number, cbor_encode_tag); +} + +void aws_cbor_encoder_write_array_start(struct aws_cbor_encoder *encoder, size_t number_entries) { + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, number_entries, cbor_encode_array_start); +} + +void aws_cbor_encoder_write_bytes(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from) { + /* Reserve the bytes for the byte string start cbor item and the actual bytes */ + /* Encode the first cbor item for byte string */ + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit + from.len, from.len, cbor_encode_bytestring_start); + /* Append the actual bytes to follow the cbor item */ + aws_byte_buf_append(&encoder->encoded_buf, &from); +} + +void aws_cbor_encoder_write_text(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from) { + /* Reserve the bytes for the byte string start cbor item and the actual string */ + /* Encode the first cbor item for byte string */ + ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit + from.len, from.len, cbor_encode_string_start); + /* Append the actual string to follow the cbor item */ + aws_byte_buf_append(&encoder->encoded_buf, &from); +} + +void aws_cbor_encoder_write_bool(struct aws_cbor_encoder *encoder, bool value) { + /* Major type 7 (simple), value 20 (false) and 21 (true) */ + uint8_t ctrl_value = value == true ? AWS_CBOR_SIMPLE_VAL_TRUE : AWS_CBOR_SIMPLE_VAL_FALSE; + ENCODE_THROUGH_LIBCBOR(encoder, 1, ctrl_value, cbor_encode_ctrl); +} + +void aws_cbor_encoder_write_null(struct aws_cbor_encoder *encoder) { + /* Major type 7 (simple), value 22 (null) */ + ENCODE_THROUGH_LIBCBOR(encoder, 1, AWS_CBOR_SIMPLE_VAL_NULL /*null*/, cbor_encode_ctrl); +} + +void aws_cbor_encoder_write_undefined(struct aws_cbor_encoder *encoder) { + /* Major type 7 (simple), value 23 (undefined) */ + ENCODE_THROUGH_LIBCBOR(encoder, 1, AWS_CBOR_SIMPLE_VAL_UNDEFINED /*undefined*/, cbor_encode_ctrl); +} + +static void s_cbor_encoder_write_type_only(struct aws_cbor_encoder *encoder, enum aws_cbor_type type) { + /* All inf start takes 1 byte only */ + aws_byte_buf_reserve_smart_relative(&encoder->encoded_buf, 1); + size_t encoded_len = 0; + switch (type) { + case AWS_CBOR_TYPE_INDEF_BYTES_START: + encoded_len = cbor_encode_indef_bytestring_start( + s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); + break; + case AWS_CBOR_TYPE_INDEF_TEXT_START: + encoded_len = cbor_encode_indef_string_start( + s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); + break; + case AWS_CBOR_TYPE_INDEF_ARRAY_START: + encoded_len = cbor_encode_indef_array_start( + s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); + break; + case AWS_CBOR_TYPE_INDEF_MAP_START: + encoded_len = cbor_encode_indef_map_start( + s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); + break; + case AWS_CBOR_TYPE_BREAK: + encoded_len = + cbor_encode_break(s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); + break; + + default: + AWS_ASSERT(false); + break; + } + AWS_ASSERT(encoded_len == 1); + encoder->encoded_buf.len += encoded_len; +} +void aws_cbor_encoder_write_indef_bytes_start(struct aws_cbor_encoder *encoder) { + s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_BYTES_START); +} + +void aws_cbor_encoder_write_indef_text_start(struct aws_cbor_encoder *encoder) { + s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_TEXT_START); +} + +void aws_cbor_encoder_write_indef_array_start(struct aws_cbor_encoder *encoder) { + s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_ARRAY_START); +} + +void aws_cbor_encoder_write_indef_map_start(struct aws_cbor_encoder *encoder) { + s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_MAP_START); +} + +void aws_cbor_encoder_write_break(struct aws_cbor_encoder *encoder) { + s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_BREAK); +} + +/******************************************************************************* + * DECODE + ******************************************************************************/ + +struct aws_cbor_decoder_context { + enum aws_cbor_type type; + + /* All the values only valid when the type is set to corresponding type. */ + union { + uint64_t unsigned_int_val; + uint64_t negative_int_val; + double float_val; + uint64_t tag_val; + bool boolean_val; + struct aws_byte_cursor bytes_val; + struct aws_byte_cursor text_val; + uint64_t map_start; + uint64_t array_start; + } u; +}; + +struct aws_cbor_decoder { + struct aws_allocator *allocator; + + struct aws_byte_cursor src; + + struct aws_cbor_decoder_context cached_context; + + /* Error code during decoding. Fail the decoding process without recovering, */ + int error_code; +}; + +struct aws_cbor_decoder *aws_cbor_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor src) { + + struct aws_cbor_decoder *decoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_cbor_decoder)); + decoder->allocator = allocator; + decoder->src = src; + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + return decoder; +} + +struct aws_cbor_decoder *aws_cbor_decoder_destroy(struct aws_cbor_decoder *decoder) { + aws_mem_release(decoder->allocator, decoder); + return NULL; +} + +size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *decoder) { + return decoder->src.len; +} + +#define LIBCBOR_VALUE_CALLBACK(field, callback_type, cbor_type) \ + static void s_##field##_callback(void *ctx, callback_type val) { \ + struct aws_cbor_decoder *decoder = ctx; \ + AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); \ + (decoder)->cached_context.u.field = val; \ + (decoder)->cached_context.type = cbor_type; \ + } + +LIBCBOR_VALUE_CALLBACK(unsigned_int_val, uint64_t, AWS_CBOR_TYPE_UINT) +LIBCBOR_VALUE_CALLBACK(negative_int_val, uint64_t, AWS_CBOR_TYPE_NEGINT) +LIBCBOR_VALUE_CALLBACK(boolean_val, bool, AWS_CBOR_TYPE_BOOL) +LIBCBOR_VALUE_CALLBACK(float_val, double, AWS_CBOR_TYPE_FLOAT) +LIBCBOR_VALUE_CALLBACK(map_start, uint64_t, AWS_CBOR_TYPE_MAP_START) +LIBCBOR_VALUE_CALLBACK(array_start, uint64_t, AWS_CBOR_TYPE_ARRAY_START) +LIBCBOR_VALUE_CALLBACK(tag_val, uint64_t, AWS_CBOR_TYPE_TAG) + +static void s_uint8_callback(void *ctx, uint8_t data) { + s_unsigned_int_val_callback(ctx, (uint64_t)data); +} + +static void s_uint16_callback(void *ctx, uint16_t data) { + s_unsigned_int_val_callback(ctx, (uint64_t)data); +} + +static void s_uint32_callback(void *ctx, uint32_t data) { + s_unsigned_int_val_callback(ctx, (uint64_t)data); +} + +static void s_negint8_callback(void *ctx, uint8_t data) { + s_negative_int_val_callback(ctx, (uint64_t)data); +} + +static void s_negint16_callback(void *ctx, uint16_t data) { + s_negative_int_val_callback(ctx, (uint64_t)data); +} + +static void s_negint32_callback(void *ctx, uint32_t data) { + s_negative_int_val_callback(ctx, (uint64_t)data); +} + +static void s_float_callback(void *ctx, float data) { + s_float_val_callback(ctx, (double)data); +} + +static void s_bytes_callback(void *ctx, const unsigned char *cbor_data, uint64_t length) { + struct aws_cbor_decoder *decoder = ctx; + AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); + if (length > SIZE_MAX) { + AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "Decoded a bytes with %" PRIu64 " bytes causing overflow .", length); + decoder->error_code = AWS_ERROR_OVERFLOW_DETECTED; + return; + } + decoder->cached_context.type = AWS_CBOR_TYPE_BYTES; + decoder->cached_context.u.bytes_val.ptr = (uint8_t *)cbor_data; + decoder->cached_context.u.bytes_val.len = (size_t)length; +} + +static void s_str_callback(void *ctx, const unsigned char *cbor_data, uint64_t length) { + struct aws_cbor_decoder *decoder = ctx; + AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); + if (length > SIZE_MAX) { + AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "Decoded a string with %" PRIu64 " bytes causing overflow .", length); + decoder->error_code = AWS_ERROR_OVERFLOW_DETECTED; + return; + } + decoder->cached_context.type = AWS_CBOR_TYPE_TEXT; + decoder->cached_context.u.text_val.ptr = (uint8_t *)cbor_data; + decoder->cached_context.u.text_val.len = (size_t)length; +} + +#define LIBCBOR_SIMPLE_CALLBACK(field, cbor_type) \ + static void s_##field##_callback(void *ctx) { \ + struct aws_cbor_decoder *decoder = ctx; \ + AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); \ + (decoder)->cached_context.type = cbor_type; \ + } + +LIBCBOR_SIMPLE_CALLBACK(inf_bytes, AWS_CBOR_TYPE_INDEF_BYTES_START) +LIBCBOR_SIMPLE_CALLBACK(inf_str, AWS_CBOR_TYPE_INDEF_TEXT_START) +LIBCBOR_SIMPLE_CALLBACK(inf_array, AWS_CBOR_TYPE_INDEF_ARRAY_START) +LIBCBOR_SIMPLE_CALLBACK(inf_map, AWS_CBOR_TYPE_INDEF_MAP_START) + +LIBCBOR_SIMPLE_CALLBACK(inf_break, AWS_CBOR_TYPE_BREAK) +LIBCBOR_SIMPLE_CALLBACK(undefined, AWS_CBOR_TYPE_UNDEFINED) +LIBCBOR_SIMPLE_CALLBACK(null, AWS_CBOR_TYPE_NULL) + +static struct cbor_callbacks s_callbacks = { + /** Unsigned int */ + .uint64 = s_unsigned_int_val_callback, + /** Unsigned int */ + .uint32 = s_uint32_callback, + /** Unsigned int */ + .uint16 = s_uint16_callback, + /** Unsigned int */ + .uint8 = s_uint8_callback, + + /** Negative int */ + .negint64 = s_negative_int_val_callback, + /** Negative int */ + .negint32 = s_negint32_callback, + /** Negative int */ + .negint16 = s_negint16_callback, + /** Negative int */ + .negint8 = s_negint8_callback, + + /** Indefinite byte string start */ + .byte_string_start = s_inf_bytes_callback, + /** Definite byte string */ + .byte_string = s_bytes_callback, + + /** Definite string */ + .string = s_str_callback, + /** Indefinite string start */ + .string_start = s_inf_str_callback, + + /** Definite array */ + .indef_array_start = s_inf_array_callback, + /** Indefinite array */ + .array_start = s_array_start_callback, + + /** Definite map */ + .indef_map_start = s_inf_map_callback, + /** Indefinite map */ + .map_start = s_map_start_callback, + + /** Tags */ + .tag = s_tag_val_callback, + + /** Half float */ + .float2 = s_float_callback, + /** Single float */ + .float4 = s_float_callback, + /** Double float */ + .float8 = s_float_val_callback, + /** Undef */ + .undefined = s_undefined_callback, + /** Null */ + .null = s_null_callback, + /** Bool */ + .boolean = s_boolean_val_callback, + + /** Indefinite item break */ + .indef_break = s_inf_break_callback, +}; + +/** + * decode the next element to the cached_content. + */ +static int s_cbor_decode_next_element(struct aws_cbor_decoder *decoder) { + struct cbor_decoder_result result = cbor_stream_decode(decoder->src.ptr, decoder->src.len, &s_callbacks, decoder); + switch (result.status) { + case CBOR_DECODER_NEDATA: + AWS_LOGF_ERROR( + AWS_LS_COMMON_CBOR, + "The decoder doesn't have enough data to decode the next element. At least %zu bytes more needed.", + result.required); + decoder->error_code = AWS_ERROR_INVALID_CBOR; + break; + case CBOR_DECODER_ERROR: + AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "The cbor data is malformed to decode."); + decoder->error_code = AWS_ERROR_INVALID_CBOR; + break; + default: + break; + } + + if (decoder->error_code) { + /* Error happened during decoding */ + return aws_raise_error(decoder->error_code); + } + + aws_byte_cursor_advance(&decoder->src, result.read); + + return AWS_OP_SUCCESS; +} + +#define GET_NEXT_ITEM(field, out_type, expected_cbor_type) \ + /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ + int aws_cbor_decoder_pop_next_##field(struct aws_cbor_decoder *decoder, out_type *out) { \ + if ((decoder)->error_code) { \ + /* Error happened during decoding */ \ + return aws_raise_error((decoder)->error_code); \ + } \ + if ((decoder)->cached_context.type != AWS_CBOR_TYPE_UNKNOWN) { \ + /* There was a cached context, check if the cached one meets the expected. */ \ + goto decode_done; \ + } \ + if (s_cbor_decode_next_element(decoder)) { \ + return AWS_OP_ERR; \ + } \ + decode_done: \ + if ((decoder)->cached_context.type != (expected_cbor_type)) { \ + AWS_LOGF_ERROR( \ + AWS_LS_COMMON_CBOR, \ + "The decoder got unexpected type: %d (%s), while expecting type: %d (%s).", \ + (decoder)->cached_context.type, \ + aws_cbor_type_cstr((decoder)->cached_context.type), \ + (expected_cbor_type), \ + aws_cbor_type_cstr(expected_cbor_type)); \ + return aws_raise_error(AWS_ERROR_CBOR_UNEXPECTED_TYPE); \ + } else { \ + /* Clear the cache as we give it out. */ \ + (decoder)->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; \ + *out = (decoder)->cached_context.u.field; \ + } \ + return AWS_OP_SUCCESS; \ + } + +GET_NEXT_ITEM(unsigned_int_val, uint64_t, AWS_CBOR_TYPE_UINT) +GET_NEXT_ITEM(negative_int_val, uint64_t, AWS_CBOR_TYPE_NEGINT) +GET_NEXT_ITEM(float_val, double, AWS_CBOR_TYPE_FLOAT) +GET_NEXT_ITEM(boolean_val, bool, AWS_CBOR_TYPE_BOOL) +GET_NEXT_ITEM(text_val, struct aws_byte_cursor, AWS_CBOR_TYPE_TEXT) +GET_NEXT_ITEM(bytes_val, struct aws_byte_cursor, AWS_CBOR_TYPE_BYTES) +GET_NEXT_ITEM(map_start, uint64_t, AWS_CBOR_TYPE_MAP_START) +GET_NEXT_ITEM(array_start, uint64_t, AWS_CBOR_TYPE_ARRAY_START) +GET_NEXT_ITEM(tag_val, uint64_t, AWS_CBOR_TYPE_TAG) + +int aws_cbor_decoder_peek_type(struct aws_cbor_decoder *decoder, enum aws_cbor_type *out_type) { + if (decoder->error_code) { + /* Error happened during decoding */ + return aws_raise_error(decoder->error_code); + } + + if (decoder->cached_context.type != AWS_CBOR_TYPE_UNKNOWN) { + /* There was a cached context, return the type. */ + *out_type = decoder->cached_context.type; + return AWS_OP_SUCCESS; + } + + /* Decode */ + if (s_cbor_decode_next_element(decoder)) { + return AWS_OP_ERR; + } + *out_type = decoder->cached_context.type; + return AWS_OP_SUCCESS; +} + +int aws_cbor_decoder_consume_next_whole_data_item(struct aws_cbor_decoder *decoder) { + if (decoder->error_code) { + /* Error happened during decoding */ + return aws_raise_error(decoder->error_code); + } + + if (decoder->cached_context.type == AWS_CBOR_TYPE_UNKNOWN) { + /* There was no cache, decode the next item */ + if (s_cbor_decode_next_element(decoder)) { + return AWS_OP_ERR; + } + } + switch (decoder->cached_context.type) { + case AWS_CBOR_TYPE_TAG: + /* Read the next data item */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) { + return AWS_OP_ERR; + } + break; + case AWS_CBOR_TYPE_MAP_START: { + uint64_t num_map_item = decoder->cached_context.u.map_start; + /* Reset type */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + for (uint64_t i = 0; i < num_map_item; i++) { + /* Key */ + if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) { + return AWS_OP_ERR; + } + /* Value */ + if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) { + return AWS_OP_ERR; + } + } + break; + } + case AWS_CBOR_TYPE_ARRAY_START: { + uint64_t num_array_item = decoder->cached_context.u.array_start; + /* Reset type */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + for (uint64_t i = 0; i < num_array_item; i++) { + /* item */ + if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) { + return AWS_OP_ERR; + } + } + break; + } + case AWS_CBOR_TYPE_INDEF_BYTES_START: + case AWS_CBOR_TYPE_INDEF_TEXT_START: + case AWS_CBOR_TYPE_INDEF_ARRAY_START: + case AWS_CBOR_TYPE_INDEF_MAP_START: { + enum aws_cbor_type next_type; + /* Reset the cache for the tag val */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + if (aws_cbor_decoder_peek_type(decoder, &next_type)) { + return AWS_OP_ERR; + } + while (next_type != AWS_CBOR_TYPE_BREAK) { + if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) { + return AWS_OP_ERR; + } + if (aws_cbor_decoder_peek_type(decoder, &next_type)) { + return AWS_OP_ERR; + } + } + break; + } + + default: + break; + } + + /* Done, just reset the cache */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + return AWS_OP_SUCCESS; +} + +int aws_cbor_decoder_consume_next_single_element(struct aws_cbor_decoder *decoder) { + enum aws_cbor_type out_type = 0; + if (aws_cbor_decoder_peek_type(decoder, &out_type)) { + return AWS_OP_ERR; + } + /* Reset the type to clear the cache. */ + decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; + return AWS_OP_SUCCESS; +} diff --git a/source/common.c b/source/common.c index 79e301539..1822f93e2 100644 --- a/source/common.c +++ b/source/common.c @@ -7,14 +7,14 @@ #include #include #include -#include +#include #include #include #include #ifdef _WIN32 -# include +# include #else # include #endif @@ -80,7 +80,7 @@ void aws_secure_zero(void *pBuf, size_t bufsize) { #endif // #else not windows } -#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C)-0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common") +#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C) - 0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common") /* clang-format off */ static struct aws_error_info errors[] = { AWS_DEFINE_ERROR_INFO_COMMON( @@ -220,7 +220,7 @@ static struct aws_error_info errors[] = { ), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_SYS_CALL_FAILURE, - "System call failure"), + "System call failure."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_FILE_INVALID_PATH, "Invalid file path."), @@ -232,7 +232,7 @@ static struct aws_error_info errors[] = { "User does not have permission to perform the requested action."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_STREAM_UNSEEKABLE, - "Stream does not support seek operations"), + "Stream does not support seek operations."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_C_STRING_BUFFER_NOT_NULL_TERMINATED, "A c-string like buffer was passed but a null terminator was not found within the bounds of the buffer."), @@ -244,7 +244,7 @@ static struct aws_error_info errors[] = { "Attempt to divide a number by zero."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_INVALID_FILE_HANDLE, - "Invalid file handle"), + "Invalid file handle."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_OPERATION_INTERUPTED, "The operation was interrupted." @@ -255,16 +255,31 @@ static struct aws_error_info errors[] = { ), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_PLATFORM_NOT_SUPPORTED, - "Feature not supported on this platform"), + "Feature not supported on this platform."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_INVALID_UTF8, - "Invalid UTF-8"), + "Invalid UTF-8."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_GET_HOME_DIRECTORY_FAILED, - "Failed to get home directory"), + "Failed to get home directory."), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_INVALID_XML, - "Invalid XML document"), + "Invalid XML document."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_FILE_OPEN_FAILURE, + "Failed opening file."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_FILE_READ_FAILURE, + "Failed reading from file."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_FILE_WRITE_FAILURE, + "Failed writing to file."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_CBOR, + "Malformed cbor data."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_CBOR_UNEXPECTED_TYPE, + "Unexpected cbor type encountered."), }; /* clang-format on */ @@ -288,6 +303,8 @@ static struct aws_log_subject_info s_common_log_subject_infos[] = { DEFINE_LOG_SUBJECT_INFO(AWS_LS_COMMON_IO, "common-io", "Common IO utilities"), DEFINE_LOG_SUBJECT_INFO(AWS_LS_COMMON_BUS, "bus", "Message bus"), DEFINE_LOG_SUBJECT_INFO(AWS_LS_COMMON_TEST, "test", "Unit/integration testing"), + DEFINE_LOG_SUBJECT_INFO(AWS_LS_COMMON_JSON_PARSER, "json-parser", "Subject for json parser specific logging"), + DEFINE_LOG_SUBJECT_INFO(AWS_LS_COMMON_CBOR, "cbor", "Subject for CBOR encode and decode"), }; static struct aws_log_subject_info_list s_common_log_subject_list = { @@ -306,6 +323,7 @@ void aws_common_library_init(struct aws_allocator *allocator) { aws_register_log_subject_info_list(&s_common_log_subject_list); aws_thread_initialize_thread_management(); aws_json_module_init(allocator); + aws_cbor_module_init(allocator); /* NUMA is funky and we can't rely on libnuma.so being available. We also don't want to take a hard dependency on it, * try and load it if we can. */ @@ -314,15 +332,15 @@ void aws_common_library_init(struct aws_allocator *allocator) { assumptions due to the way loaders and dlload are often implemented and those symbols are defined by things like libpthread.so on some unix distros. Sorry about the memory usage here, but it's our only safe choice. Also, please don't do numa configurations if memory is your economic bottleneck. */ - g_libnuma_handle = dlopen("libnuma.so", RTLD_LOCAL); + g_libnuma_handle = dlopen("libnuma.so", RTLD_LAZY | RTLD_LOCAL); /* turns out so versioning is really inconsistent these days */ if (!g_libnuma_handle) { - g_libnuma_handle = dlopen("libnuma.so.1", RTLD_LOCAL); + g_libnuma_handle = dlopen("libnuma.so.1", RTLD_LAZY | RTLD_LOCAL); } if (!g_libnuma_handle) { - g_libnuma_handle = dlopen("libnuma.so.2", RTLD_LOCAL); + g_libnuma_handle = dlopen("libnuma.so.2", RTLD_LAZY | RTLD_LOCAL); } if (g_libnuma_handle) { @@ -376,6 +394,7 @@ void aws_common_library_clean_up(void) { aws_unregister_error_info(&s_list); aws_unregister_log_subject_info_list(&s_common_log_subject_list); aws_json_module_cleanup(); + aws_cbor_module_cleanup(); #ifdef AWS_OS_LINUX if (g_libnuma_handle) { dlclose(g_libnuma_handle); diff --git a/source/date_time.c b/source/date_time.c index cee4a90d8..0c1869bce 100644 --- a/source/date_time.c +++ b/source/date_time.c @@ -131,7 +131,7 @@ static bool is_utc_time_zone(const char *str) { size_t len = strlen(str); if (len > 0) { - if (str[0] == 'Z') { + if (tolower((uint8_t)str[0]) == 'z') { return true; } @@ -207,232 +207,7 @@ enum parser_state { FINISHED, }; -static int s_parse_iso_8601_basic(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { - size_t index = 0; - size_t state_start_index = 0; - enum parser_state state = ON_YEAR; - bool error = false; - - AWS_ZERO_STRUCT(*parsed_time); - - while (state < FINISHED && !error && index < date_str_cursor->len) { - char c = date_str_cursor->ptr[index]; - size_t sub_index = index - state_start_index; - switch (state) { - case ON_YEAR: - if (aws_isdigit(c)) { - parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); - if (sub_index == 3) { - state = ON_MONTH; - state_start_index = index + 1; - parsed_time->tm_year -= 1900; - } - } else { - error = true; - } - break; - - case ON_MONTH: - if (aws_isdigit(c)) { - parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_MONTH_DAY; - state_start_index = index + 1; - parsed_time->tm_mon -= 1; - } - } else { - error = true; - } - break; - - case ON_MONTH_DAY: - if (c == 'T' && sub_index == 2) { - state = ON_HOUR; - state_start_index = index + 1; - } else if (aws_isdigit(c)) { - parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); - } else { - error = true; - } - break; - - case ON_HOUR: - if (aws_isdigit(c)) { - parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_MINUTE; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_MINUTE: - if (aws_isdigit(c)) { - parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_SECOND; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_SECOND: - if (aws_isdigit(c)) { - parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_TZ; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_TZ: - if (c == 'Z' && (sub_index == 0 || sub_index == 3)) { - state = FINISHED; - } else if (!aws_isdigit(c) || sub_index > 3) { - error = true; - } - break; - - default: - error = true; - break; - } - - index++; - } - - /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ - return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; -} - -static int s_parse_iso_8601(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { - size_t index = 0; - size_t state_start_index = 0; - enum parser_state state = ON_YEAR; - bool error = false; - bool advance = true; - - AWS_ZERO_STRUCT(*parsed_time); - - while (state < FINISHED && !error && index < date_str_cursor->len) { - char c = date_str_cursor->ptr[index]; - switch (state) { - case ON_YEAR: - if (c == '-' && index - state_start_index == 4) { - state = ON_MONTH; - state_start_index = index + 1; - parsed_time->tm_year -= 1900; - } else if (aws_isdigit(c)) { - parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_MONTH: - if (c == '-' && index - state_start_index == 2) { - state = ON_MONTH_DAY; - state_start_index = index + 1; - parsed_time->tm_mon -= 1; - } else if (aws_isdigit(c)) { - parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_MONTH_DAY: - if (c == 'T' && index - state_start_index == 2) { - state = ON_HOUR; - state_start_index = index + 1; - } else if (aws_isdigit(c)) { - parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); - } else { - error = true; - } - break; - /* note: no time portion is spec compliant. */ - case ON_HOUR: - /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ - if (index - state_start_index == 2) { - state = ON_MINUTE; - state_start_index = index + 1; - if (aws_isdigit(c)) { - state_start_index = index; - advance = false; - } else if (c != ':') { - error = true; - } - } else if (aws_isdigit(c)) { - parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_MINUTE: - /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ - if (index - state_start_index == 2) { - state = ON_SECOND; - state_start_index = index + 1; - if (aws_isdigit(c)) { - state_start_index = index; - advance = false; - } else if (c != ':') { - error = true; - } - } else if (aws_isdigit(c)) { - parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_SECOND: - if (c == 'Z' && index - state_start_index == 2) { - state = FINISHED; - state_start_index = index + 1; - } else if (c == '.' && index - state_start_index == 2) { - state = ON_TZ; - state_start_index = index + 1; - } else if (aws_isdigit(c)) { - parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_TZ: - if (c == 'Z') { - state = FINISHED; - state_start_index = index + 1; - } else if (!aws_isdigit(c)) { - error = true; - } - break; - default: - error = true; - break; - } - - if (advance) { - index++; - } else { - advance = true; - } - } - - /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ - return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; -} - -static int s_parse_rfc_822( +static bool s_parse_rfc_822( const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time, struct aws_date_time *dt) { @@ -446,7 +221,7 @@ static int s_parse_rfc_822( AWS_ZERO_STRUCT(*parsed_time); while (!error && index < len) { - char c = date_str_cursor->ptr[index]; + char c = (char)date_str_cursor->ptr[index]; switch (state) { /* week day abbr is optional. */ @@ -564,7 +339,186 @@ static int s_parse_rfc_822( } } - return error || state != ON_TZ ? AWS_OP_ERR : AWS_OP_SUCCESS; + return error || state != ON_TZ ? false : true; +} + +/* Returns true if the next N characters are digits, advancing the string and getting their numeric value */ +static bool s_read_n_digits(struct aws_byte_cursor *str, size_t n, int *out_val) { + int val = 0; + if (str->len < n) { + return false; + } + + for (size_t i = 0; i < n; ++i) { + uint8_t c = str->ptr[i]; + if (aws_isdigit(c)) { + val = val * 10 + (c - '0'); + } else { + return false; + } + } + + aws_byte_cursor_advance(str, n); + *out_val = val; + return true; +} + +/* Returns true if there's 1 more character, advancing the string and getting the character's value. */ +static bool s_read_1_char(struct aws_byte_cursor *str, uint8_t *out_c) { + if (str->len == 0) { + return false; + } + + *out_c = str->ptr[0]; + aws_byte_cursor_advance(str, 1); + return true; +} + +/* Returns true (and advances str) if next character is c */ +static bool s_advance_if_next_char_is(struct aws_byte_cursor *str, uint8_t c) { + if (str->len == 0 || str->ptr[0] != c) { + return false; + } + + aws_byte_cursor_advance(str, 1); + return true; +} + +/* If the (optional) fractional seconds (".123" or ",123") are next, str is advanced. + * Returns false if there was an error */ +static bool s_skip_optional_fractional_seconds(struct aws_byte_cursor *str) { + if (str->len == 0) { + return true; + } + + uint8_t c = str->ptr[0]; + if (c != '.' && c != ',') { + return true; + } + + size_t num_digits = 0; + for (size_t i = 1; i < str->len; ++i) { + if (aws_isdigit(str->ptr[i])) { + ++num_digits; + } else { + break; + } + } + + if (num_digits == 0) { + return false; + } + + aws_byte_cursor_advance(str, 1 + num_digits); + return true; +} + +/* Parses ISO 8601, both extended and basic format are accepted. + * Returns true if successful. */ +static bool s_parse_iso_8601(struct aws_byte_cursor str, struct tm *parsed_time, time_t *seconds_offset) { + AWS_ZERO_STRUCT(*parsed_time); + *seconds_offset = 0; + uint8_t c = 0; + + /* read year */ + if (!s_read_n_digits(&str, 4, &parsed_time->tm_year)) { + return false; + } + parsed_time->tm_year -= 1900; + + /* be lenient, allow date with separator or not */ + bool has_date_separator = s_advance_if_next_char_is(&str, '-'); + + /* read month */ + if (!s_read_n_digits(&str, 2, &parsed_time->tm_mon)) { + return false; + } + parsed_time->tm_mon -= 1; + + if (has_date_separator) { + if (!s_read_1_char(&str, &c) || c != '-') { + return false; + } + } + + /* read month-day */ + if (!s_read_n_digits(&str, 2, &parsed_time->tm_mday)) { + return false; + } + + /* ISO8601 supports date only with no time portion */ + if (str.len == 0) { + return true; + } + + /* followed by T or space (allowed by rfc3339#section-5.6) */ + if (!s_read_1_char(&str, &c) || (tolower(c) != 't' && c != ' ')) { + return false; + } + + /* read hours */ + if (!s_read_n_digits(&str, 2, &parsed_time->tm_hour)) { + return false; + } + + /* be lenient, allow time with separator or not */ + bool has_time_separator = s_advance_if_next_char_is(&str, ':'); + + /* read minutes */ + if (!s_read_n_digits(&str, 2, &parsed_time->tm_min)) { + return false; + } + + if (has_time_separator) { + if (!s_read_1_char(&str, &c) || c != ':') { + return false; + } + } + + /* read seconds */ + if (!s_read_n_digits(&str, 2, &parsed_time->tm_sec)) { + return false; + } + + /* fractional seconds are optional (discard value since tm struct has no corresponding field) */ + if (!s_skip_optional_fractional_seconds(&str)) { + return false; + } + + /* read final Z, or (+/-) indicating there will be an offset */ + if (!s_read_1_char(&str, &c)) { + return false; + } + + if (tolower(c) == 'z') { + /* Success! */ + return true; + } + + if (c != '+' && c != '-') { + return false; + } + + bool negative_offset = c == '-'; + + /* read hours offset */ + int hours_offset = 0; + if (!s_read_n_digits(&str, 2, &hours_offset)) { + return false; + } + + /* be lenient, allow offset with separator or not */ + s_advance_if_next_char_is(&str, ':'); + + /* read minutes offset */ + int minutes_offset = 0; + if (!s_read_n_digits(&str, 2, &minutes_offset)) { + return false; + } + + /* Success! */ + *seconds_offset = (time_t)(hours_offset * 3600 + minutes_offset * 60) * (negative_offset ? -1 : 1); + return true; } int aws_date_time_init_from_str_cursor( @@ -579,22 +533,16 @@ int aws_date_time_init_from_str_cursor( bool successfully_parsed = false; time_t seconds_offset = 0; - if (fmt == AWS_DATE_FORMAT_ISO_8601 || fmt == AWS_DATE_FORMAT_AUTO_DETECT) { - if (!s_parse_iso_8601(date_str_cursor, &parsed_time)) { - dt->utc_assumed = true; - successfully_parsed = true; - } - } - - if (fmt == AWS_DATE_FORMAT_ISO_8601_BASIC || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { - if (!s_parse_iso_8601_basic(date_str_cursor, &parsed_time)) { + if (fmt == AWS_DATE_FORMAT_ISO_8601 || fmt == AWS_DATE_FORMAT_ISO_8601_BASIC || + fmt == AWS_DATE_FORMAT_AUTO_DETECT) { + if (s_parse_iso_8601(*date_str_cursor, &parsed_time, &seconds_offset)) { dt->utc_assumed = true; successfully_parsed = true; } } if (fmt == AWS_DATE_FORMAT_RFC822 || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { - if (!s_parse_rfc_822(date_str_cursor, &parsed_time, dt)) { + if (s_parse_rfc_822(date_str_cursor, &parsed_time, dt)) { successfully_parsed = true; if (dt->utc_assumed) { diff --git a/source/encoding.c b/source/encoding.c index 9ca5ca4fb..97b99145d 100644 --- a/source/encoding.c +++ b/source/encoding.c @@ -23,7 +23,7 @@ static inline size_t aws_common_private_base64_decode_sse41(const unsigned char (void)out; (void)len; AWS_ASSERT(false); - return (size_t)-1; /* unreachable */ + return SIZE_MAX; /* unreachable */ } static inline void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len) { (void)in; @@ -185,7 +185,7 @@ int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct /* if the buffer isn't even, prepend a 0 to the buffer. */ if (AWS_UNLIKELY(to_decode->len & 0x01)) { i = 1; - if (s_hex_decode_char_to_int(to_decode->ptr[0], &low_value)) { + if (s_hex_decode_char_to_int((char)to_decode->ptr[0], &low_value)) { return aws_raise_error(AWS_ERROR_INVALID_HEX_STR); } @@ -361,7 +361,7 @@ int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, stru if (aws_common_private_has_avx2()) { size_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); - if (result == -1) { + if (result == SIZE_MAX) { return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); } diff --git a/source/error.c b/source/error.c index ad3cec869..239b318bd 100644 --- a/source/error.c +++ b/source/error.c @@ -194,11 +194,19 @@ void aws_unregister_error_info(const struct aws_error_info_list *error_info) { } int aws_translate_and_raise_io_error(int error_no) { + return aws_translate_and_raise_io_error_or(error_no, AWS_ERROR_SYS_CALL_FAILURE); +} + +int aws_translate_and_raise_io_error_or(int error_no, int fallback_aws_error_code) { switch (error_no) { case EINVAL: - return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); - case ESPIPE: - return aws_raise_error(AWS_ERROR_STREAM_UNSEEKABLE); + /* If useful fallback code provided, raise that instead of AWS_ERROR_INVALID_ARGUMENT, + * which isn't very useful when it bubbles out from deep within some complex system. */ + if (fallback_aws_error_code != AWS_ERROR_SYS_CALL_FAILURE) { + return aws_raise_error(fallback_aws_error_code); + } else { + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } case EPERM: case EACCES: return aws_raise_error(AWS_ERROR_NO_PERMISSION); @@ -217,6 +225,6 @@ int aws_translate_and_raise_io_error(int error_no) { case ENOTEMPTY: return aws_raise_error(AWS_ERROR_DIRECTORY_NOT_EMPTY); default: - return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); + return aws_raise_error(fallback_aws_error_code); } } diff --git a/source/external/cJSON.c b/source/external/cJSON.c index d6e49d9a8..8eac43afd 100644 --- a/source/external/cJSON.c +++ b/source/external/cJSON.c @@ -1,25 +1,33 @@ /* -Copyright (c) 2009-2017 Dave Gamble and cJSON contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. */ +/* + * This file has been modified from its original version by Amazon: + * (1) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe. + * (2) Add NOLINTBEGIN/NOLINTEND so clang-tidy ignores file. + * (3) Replace sprintf() with snprintf() to make compilers happier. + */ +/* NOLINTBEGIN */ + /* cJSON */ /* JSON parser in C. */ @@ -56,7 +64,7 @@ THE SOFTWARE. #pragma GCC visibility pop #endif -#include +#include "cJSON.h" /* define our own boolean type */ #ifdef true @@ -86,87 +94,95 @@ THE SOFTWARE. #endif typedef struct { - const unsigned char *json; - size_t position; + const unsigned char *json; + size_t position; } error; +#if 0 /* Amazon edit */ +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} +#endif /* Amazon edit */ CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) { - if (!cJSON_IsString(item)) - { - return NULL; - } + if (!cJSON_IsString(item)) + { + return NULL; + } - return item->valuestring; + return item->valuestring; } CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) { - if (!cJSON_IsNumber(item)) - { - return (double) NAN; - } + if (!cJSON_IsNumber(item)) + { + return (double) NAN; + } - return item->valuedouble; + return item->valuedouble; } /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ -#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 15) - #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif CJSON_PUBLIC(const char*) cJSON_Version(void) { - static char version[15]; - snprintf(version, sizeof(version) / sizeof(char), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + static char version[15]; + snprintf(version, sizeof(version), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); /* Amazon edit */ - return version; + return version; } /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) { - if ((string1 == NULL) || (string2 == NULL)) - { - return 1; - } + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } - if (string1 == string2) - { - return 0; - } + if (string1 == string2) + { + return 0; + } - for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) - { - if (*string1 == '\0') - { - return 0; - } - } + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } - return tolower(*string1) - tolower(*string2); + return tolower(*string1) - tolower(*string2); } typedef struct internal_hooks { - void *(CJSON_CDECL *allocate)(size_t size); - void (CJSON_CDECL *deallocate)(void *pointer); - void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); } internal_hooks; #if defined(_MSC_VER) /* work around MSVC error C2322: '...' address of dllimport '...' is not static */ static void * CJSON_CDECL internal_malloc(size_t size) { - return malloc(size); + return malloc(size); } static void CJSON_CDECL internal_free(void *pointer) { - free(pointer); + free(pointer); } static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) { - return realloc(pointer, size); + return realloc(pointer, size); } #else #define internal_malloc malloc @@ -177,844 +193,856 @@ static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) /* strlen of character literals resolved at compile time */ #define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) -static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; // NOLINT +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; -static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) // NOLINT +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) { - size_t length = 0; - unsigned char *copy = NULL; + size_t length = 0; + unsigned char *copy = NULL; - if (string == NULL) - { - return NULL; - } + if (string == NULL) + { + return NULL; + } - length = strlen((const char*)string) + sizeof(""); - copy = (unsigned char*)hooks->allocate(length); - if (copy == NULL) - { - return NULL; - } - memcpy(copy, string, length); + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); - return copy; + return copy; } -CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) // NOLINT +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) { - if (hooks == NULL) - { - /* Reset hooks */ - global_hooks.allocate = malloc; - global_hooks.deallocate = free; - global_hooks.reallocate = realloc; - return; - } + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } - global_hooks.allocate = malloc; - if (hooks->malloc_fn != NULL) - { - global_hooks.allocate = hooks->malloc_fn; - } + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } - global_hooks.deallocate = free; - if (hooks->free_fn != NULL) - { - global_hooks.deallocate = hooks->free_fn; - } + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } - /* use realloc only if both free and malloc are used */ - global_hooks.reallocate = NULL; - if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) - { - global_hooks.reallocate = realloc; - } + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } } /* Internal constructor. */ static cJSON *cJSON_New_Item(const internal_hooks * const hooks) { - cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); - if (node) - { - memset(node, '\0', sizeof(cJSON)); - } + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } - return node; + return node; } /* Delete a cJSON structure. */ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) { - cJSON *next = NULL; - while (item != NULL) - { - next = item->next; - if (!(item->type & cJSON_IsReference) && (item->child != NULL)) - { - cJSON_Delete(item->child); - } - if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) - { - global_hooks.deallocate(item->valuestring); - } - if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) - { - global_hooks.deallocate(item->string); - } - global_hooks.deallocate(item); - item = next; - } + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + item->valuestring = NULL; + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + item->string = NULL; + } + global_hooks.deallocate(item); + item = next; + } } /* get the decimal point character of the current locale */ static unsigned char get_decimal_point(void) { #ifdef ENABLE_LOCALES - struct lconv *lconv = localeconv(); - return (unsigned char) lconv->decimal_point[0]; + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; #else - return '.'; + return '.'; #endif } typedef struct { - const unsigned char *content; - size_t length; - size_t offset; - size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ - internal_hooks hooks; + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; } parse_buffer; /* check if the given size is left to read in a given parse buffer (starting with 1) */ -#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) // NOLINT +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) /* check if the buffer can be accessed at the given index (starting with 0) */ -#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) // NOLINT +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) /* get a pointer to the buffer at the position */ #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) /* Parse the input text to generate a number, and populate the result into item. */ -static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) // NOLINT -{ - double number = 0; - unsigned char *after_end = NULL; - unsigned char number_c_string[64]; - unsigned char decimal_point = get_decimal_point(); // NOLINT - size_t i = 0; - - if ((input_buffer == NULL) || (input_buffer->content == NULL)) - { - return false; // NOLINT - } - - /* copy the number into a temporary buffer and replace '.' with the decimal point - * of the current locale (for strtod) - * This also takes care of '\0' not necessarily being available for marking the end of the input */ - for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) - { - switch (buffer_at_offset(input_buffer)[i]) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '+': - case '-': - case 'e': - case 'E': - number_c_string[i] = buffer_at_offset(input_buffer)[i]; - break; - - case '.': - number_c_string[i] = decimal_point; - break; - - default: - goto loop_end; - } - } +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + + case '.': + number_c_string[i] = decimal_point; + break; + + default: + goto loop_end; + } + } loop_end: - number_c_string[i] = '\0'; + number_c_string[i] = '\0'; - number = strtod((const char*)number_c_string, (char**)&after_end); - if (number_c_string == after_end) - { - return false; /* parse_error */ // NOLINT - } + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + return false; /* parse_error */ + } - item->valuedouble = number; + item->valuedouble = number; - /* use saturation in case of overflow */ - if (number >= INT_MAX) - { // NOLINT - item->valueint = INT_MAX; - } - else if (number <= (double)INT_MIN) - { - item->valueint = INT_MIN; - } - else - { - item->valueint = (int)number; - } + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } - item->type = cJSON_Number; // NOLINT + item->type = cJSON_Number; - input_buffer->offset += (size_t)(after_end - number_c_string); - return true; // NOLINT + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; } /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ -CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) // NOLINT +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) { - if (number >= INT_MAX) - { - object->valueint = INT_MAX; - } - else if (number <= (double)INT_MIN) - { - object->valueint = INT_MIN; - } - else - { - object->valueint = (int)number; - } + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } - return object->valuedouble = number; + return object->valuedouble = number; } +/* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) { - char *copy = NULL; - /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ - if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) - { - return NULL; - } - if (strlen(valuestring) <= strlen(object->valuestring)) - { - size_t value_length = strlen(valuestring) + sizeof(""); - memcpy(object->valuestring, valuestring, value_length); - return object->valuestring; - } - copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); - if (copy == NULL) - { - return NULL; - } - if (object->valuestring != NULL) - { - cJSON_free(object->valuestring); - } - object->valuestring = copy; - - return copy; + char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + /* return NULL if the object is corrupted or valuestring is NULL */ + if (object->valuestring == NULL || valuestring == NULL) + { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) + { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; } typedef struct { - unsigned char *buffer; - size_t length; - size_t offset; - size_t depth; /* current nesting depth (for formatted printing) */ - cJSON_bool noalloc; - cJSON_bool format; /* is this print a formatted print */ - internal_hooks hooks; + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; } printbuffer; /* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char* ensure(printbuffer * const p, size_t needed) // NOLINT -{ - unsigned char *newbuffer = NULL; - size_t newsize = 0; - - if ((p == NULL) || (p->buffer == NULL)) - { - return NULL; - } - - if ((p->length > 0) && (p->offset >= p->length)) - { - /* make sure that offset is valid */ - return NULL; - } - - if (needed > INT_MAX) - { - /* sizes bigger than INT_MAX are currently not supported */ - return NULL; - } - - needed += p->offset + 1; - if (needed <= p->length) - { - return p->buffer + p->offset; - } - - if (p->noalloc) { - return NULL; - } - - /* calculate new buffer size */ - if (needed > (INT_MAX / 2)) - { - /* overflow of int, use INT_MAX if possible */ - if (needed <= INT_MAX) - { - newsize = INT_MAX; - } - else - { - return NULL; - } - } - else - { - newsize = needed * 2; - } - - if (p->hooks.reallocate != NULL) - { - /* reallocate with realloc if available */ - newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); - if (newbuffer == NULL) - { - p->hooks.deallocate(p->buffer); - p->length = 0; - p->buffer = NULL; - - return NULL; - } - } - else - { - /* otherwise reallocate manually */ - newbuffer = (unsigned char*)p->hooks.allocate(newsize); - if (!newbuffer) - { - p->hooks.deallocate(p->buffer); - p->length = 0; - p->buffer = NULL; - - return NULL; - } - - memcpy(newbuffer, p->buffer, p->offset + 1); - p->hooks.deallocate(p->buffer); - } - p->length = newsize; - p->buffer = newbuffer; - - return newbuffer + p->offset; +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; } /* calculate the new length of the string in a printbuffer and update the offset */ -static void update_offset(printbuffer * const buffer) // NOLINT +static void update_offset(printbuffer * const buffer) { - const unsigned char *buffer_pointer = NULL; - if ((buffer == NULL) || (buffer->buffer == NULL)) - { - return; - } - buffer_pointer = buffer->buffer + buffer->offset; + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; - buffer->offset += strlen((const char*)buffer_pointer); + buffer->offset += strlen((const char*)buffer_pointer); } /* securely comparison of floating-point variables */ -static cJSON_bool compare_double(double a, double b) // NOLINT +static cJSON_bool compare_double(double a, double b) { - double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); - return (fabs(a - b) <= maxVal * DBL_EPSILON); + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); } /* Render the number nicely from the given item into a string. */ -static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) // NOLINT -{ - unsigned char *output_pointer = NULL; - double d = item->valuedouble; - int length = 0; - size_t i = 0; - unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ - unsigned char decimal_point = get_decimal_point(); // NOLINT - double test = 0.0; - - if (output_buffer == NULL) - { - return false; - } - - /* This checks for NaN and Infinity */ - if (isnan(d) || isinf(d)) - { - length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "null"); - } - else - { - /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ - length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.15g", d); - - /* Check whether the original double can be recovered */ - if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) - { - /* If not, print with 17 decimal places of precision */ - length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.17g", d); - } - } - - /* sprintf failed or buffer overrun occurred */ - if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) - { - return false; - } - - /* reserve appropriate space in the output */ - output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); - if (output_pointer == NULL) - { - return false; - } - - /* copy the printed number to the output and replace locale - * dependent decimal point with '.' */ - for (i = 0; i < ((size_t)length); i++) - { - if (number_buffer[i] == decimal_point) - { - output_pointer[i] = '.'; - continue; - } - - output_pointer[i] = number_buffer[i]; - } - output_pointer[i] = '\0'; - - output_buffer->offset += (size_t)length; - - return true; +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = snprintf((char*)number_buffer, sizeof(number_buffer), "null"); /* Amazon edit */ + } + else if(d == (double)item->valueint) + { + length = snprintf((char*)number_buffer, sizeof(number_buffer), "%d", item->valueint); /* Amazon edit */ + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.15g", d); /* Amazon edit */ + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.17g", d); /* Amazon edit */ + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; } /* parse 4 digit hexadecimal number */ static unsigned parse_hex4(const unsigned char * const input) { - unsigned int h = 0; - size_t i = 0; - - for (i = 0; i < 4; i++) - { - /* parse digit */ - if ((input[i] >= '0') && (input[i] <= '9')) - { - h += (unsigned int) input[i] - '0'; - } - else if ((input[i] >= 'A') && (input[i] <= 'F')) - { - h += (unsigned int) 10 + input[i] - 'A'; - } - else if ((input[i] >= 'a') && (input[i] <= 'f')) - { - h += (unsigned int) 10 + input[i] - 'a'; - } - else /* invalid */ - { - return 0; - } - - if (i < 3) - { - /* shift left to make place for the next nibble */ - h = h << 4; - } - } - - return h; + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; } /* converts a UTF-16 literal to UTF-8 -* A literal can be one or two sequences of the form \uXXXX */ + * A literal can be one or two sequences of the form \uXXXX */ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) { - long unsigned int codepoint = 0; - unsigned int first_code = 0; - const unsigned char *first_sequence = input_pointer; - unsigned char utf8_length = 0; - unsigned char utf8_position = 0; - unsigned char sequence_length = 0; - unsigned char first_byte_mark = 0; - - if ((input_end - first_sequence) < 6) - { - /* input ends unexpectedly */ - goto fail; - } - - /* get the first utf16 sequence */ - first_code = parse_hex4(first_sequence + 2); - - /* check that the code is valid */ - if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) - { - goto fail; - } - - /* UTF16 surrogate pair */ - if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) - { - const unsigned char *second_sequence = first_sequence + 6; - unsigned int second_code = 0; - sequence_length = 12; /* \uXXXX\uXXXX */ - - if ((input_end - second_sequence) < 6) - { - /* input ends unexpectedly */ - goto fail; - } - - if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) - { - /* missing second half of the surrogate pair */ - goto fail; - } - - /* get the second utf16 sequence */ - second_code = parse_hex4(second_sequence + 2); - /* check that the code is valid */ - if ((second_code < 0xDC00) || (second_code > 0xDFFF)) - { - /* invalid second half of the surrogate pair */ - goto fail; - } - - - /* calculate the unicode codepoint from the surrogate pair */ - codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); - } - else - { - sequence_length = 6; /* \uXXXX */ - codepoint = first_code; - } - - /* encode as UTF-8 - * takes at maximum 4 bytes to encode: - * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ - if (codepoint < 0x80) - { - /* normal ascii, encoding 0xxxxxxx */ - utf8_length = 1; - } - else if (codepoint < 0x800) - { - /* two bytes, encoding 110xxxxx 10xxxxxx */ - utf8_length = 2; - first_byte_mark = 0xC0; /* 11000000 */ - } - else if (codepoint < 0x10000) - { - /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ - utf8_length = 3; - first_byte_mark = 0xE0; /* 11100000 */ - } - else if (codepoint <= 0x10FFFF) - { - /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ - utf8_length = 4; - first_byte_mark = 0xF0; /* 11110000 */ - } - else - { - /* invalid unicode codepoint */ - goto fail; - } - - /* encode as utf8 */ - for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) - { - /* 10xxxxxx */ - (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); - codepoint >>= 6; - } - /* encode first byte */ - if (utf8_length > 1) - { - (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); - } - else - { - (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); - } - - *output_pointer += utf8_length; - - return sequence_length; + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; fail: - return 0; + return 0; } /* Parse the input text into an unescaped cinput, and populate item. */ -static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) // NOLINT -{ - const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; - const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; - unsigned char *output_pointer = NULL; - unsigned char *output = NULL; - - /* not a string */ - if (buffer_at_offset(input_buffer)[0] != '\"') - { - goto fail; - } - - { - /* calculate approximate size of the output (overestimate) */ - size_t allocation_length = 0; - size_t skipped_bytes = 0; - while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) - { - /* is escape sequence */ - if (input_end[0] == '\\') - { - if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) - { - /* prevent buffer overflow when last input character is a backslash */ - goto fail; - } - skipped_bytes++; - input_end++; - } - input_end++; - } - if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) - { - goto fail; /* string ended unexpectedly */ - } - - /* This is at most how much we need for the output */ - allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; - output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); - if (output == NULL) - { - goto fail; /* allocation failure */ - } - } - - output_pointer = output; - /* loop through the string literal */ - while (input_pointer < input_end) - { - if (*input_pointer != '\\') - { - *output_pointer++ = *input_pointer++; - } - /* escape sequence */ - else - { - unsigned char sequence_length = 2; - if ((input_end - input_pointer) < 1) - { - goto fail; - } - - switch (input_pointer[1]) - { - case 'b': - *output_pointer++ = '\b'; - break; - case 'f': - *output_pointer++ = '\f'; - break; - case 'n': - *output_pointer++ = '\n'; - break; - case 'r': - *output_pointer++ = '\r'; - break; - case 't': - *output_pointer++ = '\t'; - break; - case '\"': - case '\\': - case '/': - *output_pointer++ = input_pointer[1]; - break; - - /* UTF-16 literal */ - case 'u': - sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); - if (sequence_length == 0) - { - /* failed to convert UTF16-literal to UTF-8 */ - goto fail; - } - break; - - default: - goto fail; - } - input_pointer += sequence_length; - } - } - - /* zero terminate the output */ - *output_pointer = '\0'; - - item->type = cJSON_String; - item->valuestring = (char*)output; - - input_buffer->offset = (size_t) (input_end - input_buffer->content); - input_buffer->offset++; - - return true; +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; fail: - if (output != NULL) - { - input_buffer->hooks.deallocate(output); - } + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + output = NULL; + } - if (input_pointer != NULL) - { - input_buffer->offset = (size_t)(input_pointer - input_buffer->content); - } + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } - return false; + return false; } /* Render the cstring provided to an escaped version that can be printed. */ -static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) // NOLINT -{ - const unsigned char *input_pointer = NULL; - unsigned char *output = NULL; - unsigned char *output_pointer = NULL; - size_t output_length = 0; - /* numbers of additional characters needed for escaping */ - size_t escape_characters = 0; - - if (output_buffer == NULL) - { - return false; - } - - /* empty string */ - if (input == NULL) - { - output = ensure(output_buffer, sizeof("\"\"")); - if (output == NULL) - { - return false; - } - memcpy(output, "\"\"", 3); /* NOLINT */ - - return true; - } - - /* set "flag" to 1 if something needs to be escaped */ - for (input_pointer = input; *input_pointer; input_pointer++) - { - switch (*input_pointer) - { - case '\"': - case '\\': - case '\b': - case '\f': - case '\n': - case '\r': - case '\t': - /* one character escape sequence */ - escape_characters++; - break; - default: - if (*input_pointer < 32) - { - /* UTF-16 escape sequence uXXXX */ - escape_characters += 5; - } - break; - } - } - output_length = (size_t)(input_pointer - input) + escape_characters; - - output = ensure(output_buffer, output_length + sizeof("\"\"")); - if (output == NULL) - { - return false; - } - - /* no characters have to be escaped */ - if (escape_characters == 0) - { - output[0] = '\"'; - memcpy(output + 1, input, output_length); - output[output_length + 1] = '\"'; - output[output_length + 2] = '\0'; - - return true; - } - - output[0] = '\"'; - output_pointer = output + 1; - /* copy the string */ - for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) - { - if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) - { - /* normal character, copy */ - *output_pointer = *input_pointer; - } - else - { - /* character needs to be escaped */ - *output_pointer++ = '\\'; - switch (*input_pointer) - { - case '\\': - *output_pointer = '\\'; - break; - case '\"': - *output_pointer = '\"'; - break; - case '\b': - *output_pointer = 'b'; - break; - case '\f': - *output_pointer = 'f'; - break; - case '\n': - *output_pointer = 'n'; - break; - case '\r': - *output_pointer = 'r'; - break; - case '\t': - *output_pointer = 't'; - break; - default: - /* escape and print as unicode codepoint */ - snprintf((char*)output_pointer, 6 * sizeof(char), "u%04x", *input_pointer); - output_pointer += 4; - break; - } - } - } - output[output_length + 1] = '\"'; - output[output_length + 2] = '\0'; - - return true; +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + snprintf((char*)output_pointer, 6, "u%04x", *input_pointer); /* Amazon edit */ + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; } /* Invoke print_string_ptr (which is useful) on an item. */ static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) { - return print_string_ptr((unsigned char*)item->valuestring, p); + return print_string_ptr((unsigned char*)item->valuestring, p); } /* Predeclare these prototypes. */ @@ -1026,2077 +1054,2106 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); /* Utility to jump whitespace and cr/lf */ -static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) // NOLINT +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) { - if ((buffer == NULL) || (buffer->content == NULL)) - { - return NULL; - } + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } - if (cannot_access_at_index(buffer, 0)) - { - return buffer; - } + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } - while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) - { - buffer->offset++; - } + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } - if (buffer->offset == buffer->length) - { - buffer->offset--; - } + if (buffer->offset == buffer->length) + { + buffer->offset--; + } - return buffer; + return buffer; } /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ -static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) // NOLINT +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) { - if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) - { - return NULL; - } + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } - if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) - { - buffer->offset += 3; - } + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } - return buffer; + return buffer; } CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) { - size_t buffer_length; + size_t buffer_length; - if (NULL == value) - { - return NULL; - } + if (NULL == value) + { + return NULL; + } - /* Adding null character size due to require_null_terminated. */ - buffer_length = strlen(value) + sizeof(""); + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); - return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); } /* Parse an object - create a new root, and populate. */ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; - cJSON *item = NULL; - - if (value == NULL || 0 == buffer_length) - { - goto fail; - } - - buffer.content = (const unsigned char*)value; - buffer.length = buffer_length; - buffer.offset = 0; - buffer.hooks = global_hooks; - - item = cJSON_New_Item(&global_hooks); - if (item == NULL) /* memory fail */ - { - goto fail; - } - - if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) - { - /* parse failure. ep is set. */ - goto fail; - } - - /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ - if (require_null_terminated) - { - buffer_skip_whitespace(&buffer); - if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') - { - goto fail; - } - } - if (return_parse_end) - { - *return_parse_end = (const char*)buffer_at_offset(&buffer); - } - - return item; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + +#if 0 /* Amazon edit */ + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; +#endif /* Amazon edit */ + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; fail: - if (item != NULL) - { - cJSON_Delete(item); - } - - if (value != NULL) - { - error local_error; - local_error.json = (const unsigned char*)value; - local_error.position = 0; - - if (buffer.offset < buffer.length) - { - local_error.position = buffer.offset; - } - else if (buffer.length > 0) - { - local_error.position = buffer.length - 1; - } - - if (return_parse_end != NULL) - { - *return_parse_end = (const char*)local_error.json + local_error.position; - } - - } - - return NULL; + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + +#if 0 /* Amazon edit */ + global_error = local_error; +#endif /* Amazon edit */ + } + + return NULL; } /* Default options for cJSON_Parse */ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) { - return cJSON_ParseWithOpts(value, 0, 0); + return cJSON_ParseWithOpts(value, 0, 0); } CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) { - return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); } #define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) { - static const size_t default_buffer_size = 256; - printbuffer buffer[1]; - unsigned char *printed = NULL; - - memset(buffer, 0, sizeof(buffer)); - - /* create buffer */ - buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); - buffer->length = default_buffer_size; - buffer->format = format; - buffer->hooks = *hooks; - if (buffer->buffer == NULL) - { - goto fail; - } - - /* print the value */ - if (!print_value(item, buffer)) - { - goto fail; - } - update_offset(buffer); - - /* check if reallocate is available */ - if (hooks->reallocate != NULL) - { - printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); - if (printed == NULL) { - goto fail; - } - buffer->buffer = NULL; - } - else /* otherwise copy the JSON over to a new buffer */ - { - printed = (unsigned char*) hooks->allocate(buffer->offset + 1); - if (printed == NULL) - { - goto fail; - } - memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); - printed[buffer->offset] = '\0'; /* just to be sure */ - - /* free the buffer */ - hooks->deallocate(buffer->buffer); - } - - return printed; + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + return printed; fail: - if (buffer->buffer != NULL) - { - hooks->deallocate(buffer->buffer); - } + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } - if (printed != NULL) - { - hooks->deallocate(printed); - } + if (printed != NULL) + { + hooks->deallocate(printed); + printed = NULL; + } - return NULL; + return NULL; } /* Render a cJSON item/entity/structure to text. */ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) { - return (char*)print(item, true, &global_hooks); + return (char*)print(item, true, &global_hooks); } CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) { - return (char*)print(item, false, &global_hooks); + return (char*)print(item, false, &global_hooks); } CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) { - printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - if (prebuffer < 0) - { - return NULL; - } + if (prebuffer < 0) + { + return NULL; + } - p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); - if (!p.buffer) - { - return NULL; - } + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } - p.length = (size_t)prebuffer; - p.offset = 0; - p.noalloc = false; - p.format = fmt; - p.hooks = global_hooks; + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; - if (!print_value(item, &p)) - { - global_hooks.deallocate(p.buffer); - return NULL; - } + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + p.buffer = NULL; + return NULL; + } - return (char*)p.buffer; + return (char*)p.buffer; } CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) { - printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - if ((length < 0) || (buffer == NULL)) - { - return false; - } + if ((length < 0) || (buffer == NULL)) + { + return false; + } - p.buffer = (unsigned char*)buffer; - p.length = (size_t)length; - p.offset = 0; - p.noalloc = true; - p.format = format; - p.hooks = global_hooks; + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; - return print_value(item, &p); + return print_value(item, &p); } /* Parser core - when encountering text, process appropriately. */ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) { - if ((input_buffer == NULL) || (input_buffer->content == NULL)) - { - return false; /* no input */ - } - - /* parse the different types of values */ - /* null */ - if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) - { - item->type = cJSON_NULL; - input_buffer->offset += 4; - return true; - } - /* false */ - if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) - { - item->type = cJSON_False; - input_buffer->offset += 5; - return true; - } - /* true */ - if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) - { - item->type = cJSON_True; - item->valueint = 1; - input_buffer->offset += 4; - return true; - } - /* string */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) - { - return parse_string(item, input_buffer); - } - /* number */ - if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) - { - return parse_number(item, input_buffer); - } - /* array */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) - { - return parse_array(item, input_buffer); - } - /* object */ - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) - { - return parse_object(item, input_buffer); - } - - return false; + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; } /* Render a value to text. */ static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) { - unsigned char *output = NULL; - - if ((item == NULL) || (output_buffer == NULL)) - { - return false; - } - - switch ((item->type) & 0xFF) - { - case cJSON_NULL: - output = ensure(output_buffer, 5); - if (output == NULL) - { - return false; - } - memcpy(output, "null", 5); /* NOLINT */ - return true; - - case cJSON_False: - output = ensure(output_buffer, 6); - if (output == NULL) - { - return false; - } - memcpy(output, "false", 6); /* NOLINT */ - return true; - - case cJSON_True: - output = ensure(output_buffer, 5); - if (output == NULL) - { - return false; - } - memcpy(output, "true", 5); /* NOLINT */ - return true; - - case cJSON_Number: - return print_number(item, output_buffer); - - case cJSON_Raw: - { - size_t raw_length = 0; - if (item->valuestring == NULL) - { - return false; - } - - raw_length = strlen(item->valuestring) + sizeof(""); - output = ensure(output_buffer, raw_length); - if (output == NULL) - { - return false; - } - memcpy(output, item->valuestring, raw_length); - return true; - } - - case cJSON_String: - return print_string(item, output_buffer); - - case cJSON_Array: - return print_array(item, output_buffer); - - case cJSON_Object: - return print_object(item, output_buffer); - - default: - return false; - } + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } } /* Build an array from input text. */ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) { - cJSON *head = NULL; /* head of the linked list */ - cJSON *current_item = NULL; - - if (input_buffer->depth >= CJSON_NESTING_LIMIT) - { - return false; /* to deeply nested */ - } - input_buffer->depth++; - - if (buffer_at_offset(input_buffer)[0] != '[') - { - /* not an array */ - goto fail; - } - - input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) - { - /* empty array */ - goto success; - } - - /* check if we skipped to the end of the buffer */ - if (cannot_access_at_index(input_buffer, 0)) - { - input_buffer->offset--; - goto fail; - } - - /* step back to character in front of the first element */ - input_buffer->offset--; - /* loop through the comma separated array elements */ - do - { - /* allocate next item */ - cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); - if (new_item == NULL) - { - goto fail; /* allocation failure */ - } - - /* attach next item to list */ - if (head == NULL) - { - /* start the linked list */ - current_item = head = new_item; - } - else - { - /* add to the end and advance */ - current_item->next = new_item; - new_item->prev = current_item; - current_item = new_item; - } - - /* parse next value */ - input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) - { - goto fail; /* failed to parse value */ - } - buffer_skip_whitespace(input_buffer); - } - while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); - - if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') - { - goto fail; /* expected end of array */ - } + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } success: - input_buffer->depth--; + input_buffer->depth--; - if (head != NULL) { - head->prev = current_item; - } + if (head != NULL) { + head->prev = current_item; + } - item->type = cJSON_Array; - item->child = head; + item->type = cJSON_Array; + item->child = head; - input_buffer->offset++; + input_buffer->offset++; - return true; + return true; fail: - if (head != NULL) - { - cJSON_Delete(head); - } + if (head != NULL) + { + cJSON_Delete(head); + } - return false; + return false; } /* Render an array to text */ static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) { - unsigned char *output_pointer = NULL; - size_t length = 0; - cJSON *current_element = item->child; - - if (output_buffer == NULL) - { - return false; - } - - /* Compose the output array. */ - /* opening square bracket */ - output_pointer = ensure(output_buffer, 1); - if (output_pointer == NULL) - { - return false; - } - - *output_pointer = '['; - output_buffer->offset++; - output_buffer->depth++; - - while (current_element != NULL) - { - if (!print_value(current_element, output_buffer)) - { - return false; - } - update_offset(output_buffer); - if (current_element->next) - { - length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length + 1); - if (output_pointer == NULL) - { - return false; - } - *output_pointer++ = ','; - if(output_buffer->format) - { - *output_pointer++ = ' '; - } - *output_pointer = '\0'; - output_buffer->offset += length; - } - current_element = current_element->next; - } - - output_pointer = ensure(output_buffer, 2); - if (output_pointer == NULL) - { - return false; - } - *output_pointer++ = ']'; - *output_pointer = '\0'; - output_buffer->depth--; - - return true; + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; } /* Build an object from the text. */ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) { - cJSON *head = NULL; /* linked list head */ - cJSON *current_item = NULL; - - if (input_buffer->depth >= CJSON_NESTING_LIMIT) - { - return false; /* to deeply nested */ - } - input_buffer->depth++; - - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) - { - goto fail; /* not an object */ - } - - input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) - { - goto success; /* empty object */ - } - - /* check if we skipped to the end of the buffer */ - if (cannot_access_at_index(input_buffer, 0)) - { - input_buffer->offset--; - goto fail; - } - - /* step back to character in front of the first element */ - input_buffer->offset--; - /* loop through the comma separated array elements */ - do - { - /* allocate next item */ - cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); - if (new_item == NULL) - { - goto fail; /* allocation failure */ - } - - /* attach next item to list */ - if (head == NULL) - { - /* start the linked list */ - current_item = head = new_item; - } - else - { - /* add to the end and advance */ - current_item->next = new_item; - new_item->prev = current_item; - current_item = new_item; - } - - /* parse the name of the child */ - input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_string(current_item, input_buffer)) - { - goto fail; /* failed to parse name */ - } - buffer_skip_whitespace(input_buffer); - - /* swap valuestring and string, because we parsed the name */ - current_item->string = current_item->valuestring; - current_item->valuestring = NULL; - - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) - { - goto fail; /* invalid object */ - } - - /* parse the value */ - input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) - { - goto fail; /* failed to parse value */ - } - buffer_skip_whitespace(input_buffer); - } - while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); - - if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) - { - goto fail; /* expected end of object */ - } + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + if (cannot_access_at_index(input_buffer, 1)) + { + goto fail; /* nothing comes after the comma */ + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } success: - input_buffer->depth--; + input_buffer->depth--; - if (head != NULL) { - head->prev = current_item; - } + if (head != NULL) { + head->prev = current_item; + } - item->type = cJSON_Object; - item->child = head; + item->type = cJSON_Object; + item->child = head; - input_buffer->offset++; - return true; + input_buffer->offset++; + return true; fail: - if (head != NULL) - { - cJSON_Delete(head); - } + if (head != NULL) + { + cJSON_Delete(head); + } - return false; + return false; } /* Render an object to text. */ static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) { - unsigned char *output_pointer = NULL; - size_t length = 0; - cJSON *current_item = item->child; - - if (output_buffer == NULL) - { - return false; - } - - /* Compose the output: */ - length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ - output_pointer = ensure(output_buffer, length + 1); - if (output_pointer == NULL) - { - return false; - } - - *output_pointer++ = '{'; - output_buffer->depth++; - if (output_buffer->format) - { - *output_pointer++ = '\n'; - } - output_buffer->offset += length; - - while (current_item) - { - if (output_buffer->format) - { - size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth); - if (output_pointer == NULL) - { - return false; - } - for (i = 0; i < output_buffer->depth; i++) - { - *output_pointer++ = '\t'; - } - output_buffer->offset += output_buffer->depth; - } - - /* print key */ - if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) - { - return false; - } - update_offset(output_buffer); - - length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length); - if (output_pointer == NULL) - { - return false; - } - *output_pointer++ = ':'; - if (output_buffer->format) - { - *output_pointer++ = '\t'; - } - output_buffer->offset += length; - - /* print value */ - if (!print_value(current_item, output_buffer)) - { - return false; - } - update_offset(output_buffer); - - /* print comma if not last */ - length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); - output_pointer = ensure(output_buffer, length + 1); - if (output_pointer == NULL) - { - return false; - } - if (current_item->next) - { - *output_pointer++ = ','; - } - - if (output_buffer->format) - { - *output_pointer++ = '\n'; - } - *output_pointer = '\0'; - output_buffer->offset += length; - - current_item = current_item->next; - } - - output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); - if (output_pointer == NULL) - { - return false; - } - if (output_buffer->format) - { - size_t i; - for (i = 0; i < (output_buffer->depth - 1); i++) - { - *output_pointer++ = '\t'; - } - } - *output_pointer++ = '}'; - *output_pointer = '\0'; - output_buffer->depth--; - - return true; + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; } /* Get Array size/item / object item. */ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) { - cJSON *child = NULL; - size_t size = 0; + cJSON *child = NULL; + size_t size = 0; - if (array == NULL) - { - return 0; - } + if (array == NULL) + { + return 0; + } - child = array->child; + child = array->child; - while(child != NULL) - { - size++; - child = child->next; - } + while(child != NULL) + { + size++; + child = child->next; + } - /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ - return (int)size; + return (int)size; } static cJSON* get_array_item(const cJSON *array, size_t index) { - cJSON *current_child = NULL; + cJSON *current_child = NULL; - if (array == NULL) - { - return NULL; - } + if (array == NULL) + { + return NULL; + } - current_child = array->child; - while ((current_child != NULL) && (index > 0)) - { - index--; - current_child = current_child->next; - } + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } - return current_child; + return current_child; } CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) { - if (index < 0) - { - return NULL; - } + if (index < 0) + { + return NULL; + } - return get_array_item(array, (size_t)index); + return get_array_item(array, (size_t)index); } static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) { - cJSON *current_element = NULL; + cJSON *current_element = NULL; - if ((object == NULL) || (name == NULL)) - { - return NULL; - } + if ((object == NULL) || (name == NULL)) + { + return NULL; + } - current_element = object->child; - if (case_sensitive) - { - while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) - { - current_element = current_element->next; - } - } - else - { - while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) - { - current_element = current_element->next; - } - } + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } - if ((current_element == NULL) || (current_element->string == NULL)) { - return NULL; - } + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } - return current_element; + return current_element; } CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) { - return get_object_item(object, string, false); + return get_object_item(object, string, false); } CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) { - return get_object_item(object, string, true); + return get_object_item(object, string, true); } CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) { - return cJSON_GetObjectItem(object, string) ? 1 : 0; + return cJSON_GetObjectItem(object, string) ? 1 : 0; } /* Utility for array list handling. */ static void suffix_object(cJSON *prev, cJSON *item) { - prev->next = item; - item->prev = prev; + prev->next = item; + item->prev = prev; } /* Utility for handling references. */ static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) { - cJSON *reference = NULL; - if (item == NULL) - { - return NULL; - } + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } - reference = cJSON_New_Item(hooks); - if (reference == NULL) - { - return NULL; - } + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } - memcpy(reference, item, sizeof(cJSON)); - reference->string = NULL; - reference->type |= cJSON_IsReference; - reference->next = reference->prev = NULL; - return reference; + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; } static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) { - cJSON *child = NULL; - - if ((item == NULL) || (array == NULL) || (array == item)) - { - return false; - } - - child = array->child; - /* - * To find the last item in array quickly, we use prev in array - */ - if (child == NULL) - { - /* list is empty, start new one */ - array->child = item; - item->prev = item; - item->next = NULL; - } - else - { - /* append to the end */ - if (child->prev) - { - suffix_object(child->prev, item); - array->child->prev = item; - } - } - - return true; + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; } /* Add item to array/object. */ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { - return add_item_to_array(array, item); + return add_item_to_array(array, item); } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) - #pragma GCC diagnostic push + #pragma GCC diagnostic push #endif #ifdef __GNUC__ - #if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 1))) - #pragma GCC diagnostic ignored "-Wcast-qual" - #endif +#pragma GCC diagnostic ignored "-Wcast-qual" #endif /* helper function to cast away const */ static void* cast_away_const(const void* string) { - return (void*)string; + return (void*)string; } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) - #pragma GCC diagnostic pop + #pragma GCC diagnostic pop #endif static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) { - char *new_key = NULL; - int new_type = cJSON_Invalid; + char *new_key = NULL; + int new_type = cJSON_Invalid; - if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) - { - return false; - } + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } - if (constant_key) - { - new_key = (char*)cast_away_const(string); - new_type = item->type | cJSON_StringIsConst; - } - else - { - new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); - if (new_key == NULL) - { - return false; - } + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } - new_type = item->type & ~cJSON_StringIsConst; - } + new_type = item->type & ~cJSON_StringIsConst; + } - if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) - { - hooks->deallocate(item->string); - } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } - item->string = new_key; - item->type = new_type; + item->string = new_key; + item->type = new_type; - return add_item_to_array(object, item); + return add_item_to_array(object, item); } CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { - return add_item_to_object(object, string, item, &global_hooks, false); + return add_item_to_object(object, string, item, &global_hooks, false); } /* Add an item to an object with constant string as key */ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { - return add_item_to_object(object, string, item, &global_hooks, true); + return add_item_to_object(object, string, item, &global_hooks, true); } CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) { - if (array == NULL) - { - return false; - } + if (array == NULL) + { + return false; + } - return add_item_to_array(array, create_reference(item, &global_hooks)); + return add_item_to_array(array, create_reference(item, &global_hooks)); } CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) { - if ((object == NULL) || (string == NULL)) - { - return false; - } + if ((object == NULL) || (string == NULL)) + { + return false; + } - return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); } CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) { - cJSON *null = cJSON_CreateNull(); - if (add_item_to_object(object, name, null, &global_hooks, false)) - { - return null; - } + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } - cJSON_Delete(null); - return NULL; + cJSON_Delete(null); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) { - cJSON *true_item = cJSON_CreateTrue(); - if (add_item_to_object(object, name, true_item, &global_hooks, false)) - { - return true_item; - } + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } - cJSON_Delete(true_item); - return NULL; + cJSON_Delete(true_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) { - cJSON *false_item = cJSON_CreateFalse(); - if (add_item_to_object(object, name, false_item, &global_hooks, false)) - { - return false_item; - } + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } - cJSON_Delete(false_item); - return NULL; + cJSON_Delete(false_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) { - cJSON *bool_item = cJSON_CreateBool(boolean); - if (add_item_to_object(object, name, bool_item, &global_hooks, false)) - { - return bool_item; - } + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } - cJSON_Delete(bool_item); - return NULL; + cJSON_Delete(bool_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) { - cJSON *number_item = cJSON_CreateNumber(number); - if (add_item_to_object(object, name, number_item, &global_hooks, false)) - { - return number_item; - } + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } - cJSON_Delete(number_item); - return NULL; + cJSON_Delete(number_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) { - cJSON *string_item = cJSON_CreateString(string); - if (add_item_to_object(object, name, string_item, &global_hooks, false)) - { - return string_item; - } + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } - cJSON_Delete(string_item); - return NULL; + cJSON_Delete(string_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) { - cJSON *raw_item = cJSON_CreateRaw(raw); - if (add_item_to_object(object, name, raw_item, &global_hooks, false)) - { - return raw_item; - } + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } - cJSON_Delete(raw_item); - return NULL; + cJSON_Delete(raw_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) { - cJSON *object_item = cJSON_CreateObject(); - if (add_item_to_object(object, name, object_item, &global_hooks, false)) - { - return object_item; - } + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } - cJSON_Delete(object_item); - return NULL; + cJSON_Delete(object_item); + return NULL; } CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) { - cJSON *array = cJSON_CreateArray(); - if (add_item_to_object(object, name, array, &global_hooks, false)) - { - return array; - } + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } - cJSON_Delete(array); - return NULL; + cJSON_Delete(array); + return NULL; } CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) { - if ((parent == NULL) || (item == NULL)) - { - return NULL; - } - - if (item != parent->child) - { - /* not the first element */ - item->prev->next = item->next; - } - if (item->next != NULL) - { - /* not the last element */ - item->next->prev = item->prev; - } - - if (item == parent->child) - { - /* first element */ - parent->child = item->next; - } - else if (item->next == NULL) - { - /* last element */ - parent->child->prev = item->prev; - } - - /* make sure the detached item doesn't point anywhere anymore */ - item->prev = NULL; - item->next = NULL; - - return item; + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; } CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) { - if (which < 0) - { - return NULL; - } + if (which < 0) + { + return NULL; + } - return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); } CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) { - cJSON_Delete(cJSON_DetachItemFromArray(array, which)); + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); } CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) { - cJSON *to_detach = cJSON_GetObjectItem(object, string); + cJSON *to_detach = cJSON_GetObjectItem(object, string); - return cJSON_DetachItemViaPointer(object, to_detach); + return cJSON_DetachItemViaPointer(object, to_detach); } CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) { - cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); - return cJSON_DetachItemViaPointer(object, to_detach); + return cJSON_DetachItemViaPointer(object, to_detach); } CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) { - cJSON_Delete(cJSON_DetachItemFromObject(object, string)); + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); } CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) { - cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); } /* Replace array/object items with new ones. */ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { - cJSON *after_inserted = NULL; - - if (which < 0) - { - return false; - } - - after_inserted = get_array_item(array, (size_t)which); - if (after_inserted == NULL) - { - return add_item_to_array(array, newitem); - } - - newitem->next = after_inserted; - newitem->prev = after_inserted->prev; - after_inserted->prev = newitem; - if (after_inserted == array->child) - { - array->child = newitem; - } - else - { - newitem->prev->next = newitem; - } - return true; + cJSON *after_inserted = NULL; + + if (which < 0 || newitem == NULL) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + if (after_inserted != array->child && after_inserted->prev == NULL) { + /* return false if after_inserted is a corrupted array item */ + return false; + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; } CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) { - if ((parent == NULL) || (replacement == NULL) || (item == NULL)) - { - return false; - } - - if (replacement == item) - { - return true; - } - - replacement->next = item->next; - replacement->prev = item->prev; - - if (replacement->next != NULL) - { - replacement->next->prev = replacement; - } - if (parent->child == item) - { - if (parent->child->prev == parent->child) - { - replacement->prev = replacement; - } - parent->child = replacement; - } - else - { /* - * To find the last item in array quickly, we use prev in array. - * We can't modify the last item's next pointer where this item was the parent's child - */ - if (replacement->prev != NULL) - { - replacement->prev->next = replacement; - } - if (replacement->next == NULL) - { - parent->child->prev = replacement; - } - } - - item->next = NULL; - item->prev = NULL; - cJSON_Delete(item); - - return true; + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; } CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { - if (which < 0) - { - return false; - } + if (which < 0) + { + return false; + } - return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); } static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) { - if ((replacement == NULL) || (string == NULL)) - { - return false; - } + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } - /* replace the name in the replacement */ - if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) - { - cJSON_free(replacement->string); - } - replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); - replacement->type &= ~cJSON_StringIsConst; + replacement->type &= ~cJSON_StringIsConst; - return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); } CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { - return replace_item_in_object(object, string, newitem, false); + return replace_item_in_object(object, string, newitem, false); } CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) { - return replace_item_in_object(object, string, newitem, true); + return replace_item_in_object(object, string, newitem, true); } /* Create basic types: */ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_NULL; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_True; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_False; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = boolean ? cJSON_True : cJSON_False; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_Number; - item->valuedouble = num; - - /* use saturation in case of overflow */ - if (num >= INT_MAX) - { - item->valueint = INT_MAX; - } - else if (num <= (double)INT_MIN) - { - item->valueint = INT_MIN; - } - else - { - item->valueint = (int)num; - } - } - - return item; + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_String; - item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); - if(!item->valuestring) - { - cJSON_Delete(item); - return NULL; - } - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) { - cJSON *item = cJSON_New_Item(&global_hooks); - if (item != NULL) - { - item->type = cJSON_String | cJSON_IsReference; - item->valuestring = (char*)cast_away_const(string); - } + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) { - cJSON *item = cJSON_New_Item(&global_hooks); - if (item != NULL) { - item->type = cJSON_Object | cJSON_IsReference; - item->child = (cJSON*)cast_away_const(child); - } + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { - cJSON *item = cJSON_New_Item(&global_hooks); - if (item != NULL) { - item->type = cJSON_Array | cJSON_IsReference; - item->child = (cJSON*)cast_away_const(child); - } + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_Raw; - item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); - if(!item->valuestring) - { - cJSON_Delete(item); - return NULL; - } - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type=cJSON_Array; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } - return item; + return item; } CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { - cJSON *item = cJSON_New_Item(&global_hooks); - if (item) - { - item->type = cJSON_Object; - } + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } - return item; + return item; } /* Create Arrays: */ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) { - size_t i = 0; - cJSON *n = NULL; - cJSON *p = NULL; - cJSON *a = NULL; - - if ((count < 0) || (numbers == NULL)) - { - return NULL; - } - - a = cJSON_CreateArray(); - - for(i = 0; a && (i < (size_t)count); i++) - { - n = cJSON_CreateNumber(numbers[i]); - if (!n) - { - cJSON_Delete(a); - return NULL; - } - if(!i) - { - a->child = n; - } - else - { - suffix_object(p, n); - } - p = n; - } - - if (a && a->child) { - a->child->prev = n; - } - - return a; + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; } CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) { - size_t i = 0; - cJSON *n = NULL; - cJSON *p = NULL; - cJSON *a = NULL; - - if ((count < 0) || (numbers == NULL)) - { - return NULL; - } - - a = cJSON_CreateArray(); - - for(i = 0; a && (i < (size_t)count); i++) - { - n = cJSON_CreateNumber((double)numbers[i]); - if(!n) - { - cJSON_Delete(a); - return NULL; - } - if(!i) - { - a->child = n; - } - else - { - suffix_object(p, n); - } - p = n; - } - - if (a && a->child) { - a->child->prev = n; - } - - return a; + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; } CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) { - size_t i = 0; - cJSON *n = NULL; - cJSON *p = NULL; - cJSON *a = NULL; - - if ((count < 0) || (numbers == NULL)) - { - return NULL; - } - - a = cJSON_CreateArray(); - - for(i = 0; a && (i < (size_t)count); i++) - { - n = cJSON_CreateNumber(numbers[i]); - if(!n) - { - cJSON_Delete(a); - return NULL; - } - if(!i) - { - a->child = n; - } - else - { - suffix_object(p, n); - } - p = n; - } - - if (a && a->child) { - a->child->prev = n; - } - - return a; + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; } CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) { - size_t i = 0; - cJSON *n = NULL; - cJSON *p = NULL; - cJSON *a = NULL; - - if ((count < 0) || (strings == NULL)) - { - return NULL; - } - - a = cJSON_CreateArray(); - - for (i = 0; a && (i < (size_t)count); i++) - { - n = cJSON_CreateString(strings[i]); - if(!n) - { - cJSON_Delete(a); - return NULL; - } - if(!i) - { - a->child = n; - } - else - { - suffix_object(p,n); - } - p = n; - } - - if (a && a->child) { - a->child->prev = n; - } - - return a; + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; } /* Duplication */ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) { - cJSON *newitem = NULL; - cJSON *child = NULL; - cJSON *next = NULL; - cJSON *newchild = NULL; - - /* Bail on bad ptr */ - if (!item) - { - goto fail; - } - /* Create new item */ - newitem = cJSON_New_Item(&global_hooks); - if (!newitem) - { - goto fail; - } - /* Copy over all vars */ - newitem->type = item->type & (~cJSON_IsReference); - newitem->valueint = item->valueint; - newitem->valuedouble = item->valuedouble; - if (item->valuestring) - { - newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); - if (!newitem->valuestring) - { - goto fail; - } - } - if (item->string) - { - newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); - if (!newitem->string) - { - goto fail; - } - } - /* If non-recursive, then we're done! */ - if (!recurse) - { - return newitem; - } - /* Walk the ->next chain for the child. */ - child = item->child; - while (child != NULL) - { - newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ - if (!newchild) - { - goto fail; - } - if (next != NULL) - { - /* If newitem->child already set, then crosswire ->prev and ->next and move on */ - next->next = newchild; - newchild->prev = next; - next = newchild; - } - else - { - /* Set newitem->child and move to it */ - newitem->child = newchild; - next = newchild; - } - child = child->next; - } - if (newitem && newitem->child) - { - newitem->child->prev = newchild; - } - - return newitem; + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } + + return newitem; fail: - if (newitem != NULL) - { - cJSON_Delete(newitem); - } + if (newitem != NULL) + { + cJSON_Delete(newitem); + } - return NULL; + return NULL; } static void skip_oneline_comment(char **input) { - *input += static_strlen("//"); + *input += static_strlen("//"); - for (; (*input)[0] != '\0'; ++(*input)) - { - if ((*input)[0] == '\n') { - *input += static_strlen("\n"); - return; - } - } + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } } static void skip_multiline_comment(char **input) { - *input += static_strlen("/*"); + *input += static_strlen("/*"); - for (; (*input)[0] != '\0'; ++(*input)) - { - if (((*input)[0] == '*') && ((*input)[1] == '/')) - { - *input += static_strlen("*/"); - return; - } - } + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } } static void minify_string(char **input, char **output) { - (*output)[0] = (*input)[0]; - *input += static_strlen("\""); - *output += static_strlen("\""); + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); - for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { - (*output)[0] = (*input)[0]; + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; - if ((*input)[0] == '\"') { - (*output)[0] = '\"'; - *input += static_strlen("\""); - *output += static_strlen("\""); - return; - } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { - (*output)[1] = (*input)[1]; - *input += static_strlen("\""); - *output += static_strlen("\""); - } - } + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } } CJSON_PUBLIC(void) cJSON_Minify(char *json) { - char *into = json; - - if (json == NULL) - { - return; - } - - while (json[0] != '\0') - { - switch (json[0]) - { - case ' ': - case '\t': - case '\r': - case '\n': - json++; - break; - - case '/': - if (json[1] == '/') - { - skip_oneline_comment(&json); - } - else if (json[1] == '*') - { - skip_multiline_comment(&json); - } else { - json++; - } - break; - - case '\"': - minify_string(&json, (char**)&into); - break; - - default: - into[0] = json[0]; - json++; - into++; - } - } - - /* and null-terminate. */ - *into = '\0'; + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; } CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_Invalid; + return (item->type & 0xFF) == cJSON_Invalid; } CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_False; + return (item->type & 0xFF) == cJSON_False; } CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xff) == cJSON_True; + return (item->type & 0xff) == cJSON_True; } CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & (cJSON_True | cJSON_False)) != 0; + return (item->type & (cJSON_True | cJSON_False)) != 0; } CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_NULL; + return (item->type & 0xFF) == cJSON_NULL; } CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_Number; + return (item->type & 0xFF) == cJSON_Number; } CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_String; + return (item->type & 0xFF) == cJSON_String; } CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_Array; + return (item->type & 0xFF) == cJSON_Array; } CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_Object; + return (item->type & 0xFF) == cJSON_Object; } CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) { - if (item == NULL) - { - return false; - } + if (item == NULL) + { + return false; + } - return (item->type & 0xFF) == cJSON_Raw; + return (item->type & 0xFF) == cJSON_Raw; } CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) { - if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) - { - return false; - } - - /* check if type is valid */ - switch (a->type & 0xFF) - { - case cJSON_False: - case cJSON_True: - case cJSON_NULL: - case cJSON_Number: - case cJSON_String: - case cJSON_Raw: - case cJSON_Array: - case cJSON_Object: - break; - - default: - return false; - } - - /* identical objects are equal */ - if (a == b) - { - return true; - } - - switch (a->type & 0xFF) - { - /* in these cases and equal type is enough */ - case cJSON_False: - case cJSON_True: - case cJSON_NULL: - return true; - - case cJSON_Number: - if (compare_double(a->valuedouble, b->valuedouble)) - { - return true; - } - return false; - - case cJSON_String: - case cJSON_Raw: - if ((a->valuestring == NULL) || (b->valuestring == NULL)) - { - return false; - } - if (strcmp(a->valuestring, b->valuestring) == 0) - { - return true; - } - - return false; - - case cJSON_Array: - { - cJSON *a_element = a->child; - cJSON *b_element = b->child; - - for (; (a_element != NULL) && (b_element != NULL);) - { - if (!cJSON_Compare(a_element, b_element, case_sensitive)) - { - return false; - } - - a_element = a_element->next; - b_element = b_element->next; - } - - /* one of the arrays is longer than the other */ - if (a_element != b_element) { - return false; - } - - return true; - } - - case cJSON_Object: - { - cJSON *a_element = NULL; - cJSON *b_element = NULL; - cJSON_ArrayForEach(a_element, a) - { - /* TODO This has O(n^2) runtime, which is horrible! */ - b_element = get_object_item(b, a_element->string, case_sensitive); - if (b_element == NULL) - { - return false; - } - - if (!cJSON_Compare(a_element, b_element, case_sensitive)) - { - return false; - } - } - - /* doing this twice, once on a and b to prevent true comparison if a subset of b - * TODO: Do this the proper way, this is just a fix for now */ - cJSON_ArrayForEach(b_element, b) - { - a_element = get_object_item(a, b_element->string, case_sensitive); - if (a_element == NULL) - { - return false; - } - - if (!cJSON_Compare(b_element, a_element, case_sensitive)) - { - return false; - } - } - - return true; - } - - default: - return false; - } + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } } CJSON_PUBLIC(void *) cJSON_malloc(size_t size) { - return global_hooks.allocate(size); + return global_hooks.allocate(size); } CJSON_PUBLIC(void) cJSON_free(void *object) { - global_hooks.deallocate(object); + global_hooks.deallocate(object); + object = NULL; } +/* Amazon edit */ +/* NOLINTEND */ diff --git a/include/aws/common/external/cJSON.h b/source/external/cJSON.h similarity index 75% rename from include/aws/common/external/cJSON.h rename to source/external/cJSON.h index 569591477..48cdda9ac 100644 --- a/include/aws/common/external/cJSON.h +++ b/source/external/cJSON.h @@ -1,37 +1,34 @@ /* -Copyright (c) 2009-2017 Dave Gamble and cJSON contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. */ /* * This file has been modified from its original version by Amazon: - * (1) Address clang-tidy errors by renaming function parameters in a number of places - * to match their .c counterparts. - * (2) Misc tweaks to unchecked writes to make security static analysis happier - * (3) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe. + * (1) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe. + * (2) Add NOLINTBEGIN/NOLINTEND so clang-tidy ignores file. */ +/* NOLINTBEGIN */ -/* clang-format off */ - -#ifndef AWS_COMMON_EXTERNAL_CJSON_H // NOLINT -#define AWS_COMMON_EXTERNAL_CJSON_H // NOLINT +#ifndef cJSON__h +#define cJSON__h #ifdef __cplusplus extern "C" @@ -91,7 +88,7 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ /* project version */ #define CJSON_VERSION_MAJOR 1 #define CJSON_VERSION_MINOR 7 -#define CJSON_VERSION_PATCH 15 +#define CJSON_VERSION_PATCH 18 #include @@ -112,37 +109,37 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ /* The cJSON structure: */ typedef struct cJSON { - /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ - struct cJSON *next; - struct cJSON *prev; - /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ - struct cJSON *child; - - /* The type of the item, as above. */ - int type; - - /* The item's string, if type==cJSON_String and type == cJSON_Raw */ - char *valuestring; - /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ - int valueint; - /* The item's number, if type==cJSON_Number */ - double valuedouble; - - /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ - char *string; + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; } cJSON; typedef struct cJSON_Hooks { - /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ - void *(CJSON_CDECL *malloc_fn)(size_t sz); // NOLINT - void (CJSON_CDECL *free_fn)(void *ptr); + /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void (CJSON_CDECL *free_fn)(void *ptr); } cJSON_Hooks; typedef int cJSON_bool; /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. -* This is to prevent stack overflows. */ + * This is to prevent stack overflows. */ #ifndef CJSON_NESTING_LIMIT #define CJSON_NESTING_LIMIT 1000 #endif @@ -182,6 +179,10 @@ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +#if 0 /* Amazon edit */ +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); +#endif /* Amazon edit */ /* Check item type and return its value */ CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); @@ -212,15 +213,15 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); /* Create a string where valuestring references a string so -* it will not be freed by cJSON_Delete */ + * it will not be freed by cJSON_Delete */ CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); /* Create an object/array that only references it's elements so -* they will not be freed by cJSON_Delete */ + * they will not be freed by cJSON_Delete */ CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); /* These utilities create an Array of count items. -* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); @@ -230,8 +231,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. -* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before -* writing to `item->string` */ + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); @@ -256,19 +257,19 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,co /* Duplicate a cJSON item */ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will -* need to be released. With recurse!=0, it will duplicate any children connected to the item. -* The item->next and ->prev pointers are always zero on return from Duplicate. */ + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ /* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. -* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ -CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); // NOLINT + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); /* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. -* The input pointer json cannot point to a read-only address area, such as a string constant, -* but should point to a readable and writable address area. */ + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ CJSON_PUBLIC(void) cJSON_Minify(char *json); /* Helper functions for creating and adding items to an object at the same time. -* They return the added item or NULL on failure. */ + * They return the added item or NULL on failure. */ CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); @@ -280,15 +281,22 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); /* When assigning an integer value, it needs to be propagated to valuedouble too. */ -#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) //NOLINT +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) /* helper for the cJSON_SetNumberValue macro */ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); -#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) //NOLINT +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) /* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + cJSON_Invalid\ +) + /* Macro for iterating over an array or object */ -#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) //NOLINT +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) /* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ CJSON_PUBLIC(void *) cJSON_malloc(size_t size); @@ -297,5 +305,6 @@ CJSON_PUBLIC(void) cJSON_free(void *object); #ifdef __cplusplus } #endif - +/* Amazon edit */ +/* NOLINTEND */ #endif diff --git a/source/external/libcbor/allocators.c b/source/external/libcbor/allocators.c new file mode 100644 index 000000000..43c5440dd --- /dev/null +++ b/source/external/libcbor/allocators.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "cbor/common.h" + +CBOR_EXPORT _cbor_malloc_t _cbor_malloc = malloc; +CBOR_EXPORT _cbor_realloc_t _cbor_realloc = realloc; +CBOR_EXPORT _cbor_free_t _cbor_free = free; + +void cbor_set_allocs(_cbor_malloc_t custom_malloc, + _cbor_realloc_t custom_realloc, _cbor_free_t custom_free) { + _cbor_malloc = custom_malloc; + _cbor_realloc = custom_realloc; + _cbor_free = custom_free; +} diff --git a/source/external/libcbor/cbor.c b/source/external/libcbor/cbor.c new file mode 100644 index 000000000..a8b4bcd7a --- /dev/null +++ b/source/external/libcbor/cbor.c @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "cbor.h" +#include "cbor/internal/builder_callbacks.h" +#include "cbor/internal/loaders.h" + +#pragma clang diagnostic push +cbor_item_t *cbor_load(cbor_data source, size_t source_size, + struct cbor_load_result *result) { + /* Context stack */ + static struct cbor_callbacks callbacks = { + .uint8 = &cbor_builder_uint8_callback, + .uint16 = &cbor_builder_uint16_callback, + .uint32 = &cbor_builder_uint32_callback, + .uint64 = &cbor_builder_uint64_callback, + + .negint8 = &cbor_builder_negint8_callback, + .negint16 = &cbor_builder_negint16_callback, + .negint32 = &cbor_builder_negint32_callback, + .negint64 = &cbor_builder_negint64_callback, + + .byte_string = &cbor_builder_byte_string_callback, + .byte_string_start = &cbor_builder_byte_string_start_callback, + + .string = &cbor_builder_string_callback, + .string_start = &cbor_builder_string_start_callback, + + .array_start = &cbor_builder_array_start_callback, + .indef_array_start = &cbor_builder_indef_array_start_callback, + + .map_start = &cbor_builder_map_start_callback, + .indef_map_start = &cbor_builder_indef_map_start_callback, + + .tag = &cbor_builder_tag_callback, + + .null = &cbor_builder_null_callback, + .undefined = &cbor_builder_undefined_callback, + .boolean = &cbor_builder_boolean_callback, + .float2 = &cbor_builder_float2_callback, + .float4 = &cbor_builder_float4_callback, + .float8 = &cbor_builder_float8_callback, + .indef_break = &cbor_builder_indef_break_callback}; + + if (source_size == 0) { + result->error.code = CBOR_ERR_NODATA; + return NULL; + } + struct _cbor_stack stack = _cbor_stack_init(); + + /* Target for callbacks */ + struct _cbor_decoder_context context = (struct _cbor_decoder_context){ + .stack = &stack, .creation_failed = false, .syntax_error = false}; + struct cbor_decoder_result decode_result; + *result = + (struct cbor_load_result){.read = 0, .error = {.code = CBOR_ERR_NONE}}; + + do { + if (source_size > result->read) { /* Check for overflows */ + decode_result = + cbor_stream_decode(source + result->read, source_size - result->read, + &callbacks, &context); + } else { + result->error = (struct cbor_error){.code = CBOR_ERR_NOTENOUGHDATA, + .position = result->read}; + goto error; + } + + switch (decode_result.status) { + case CBOR_DECODER_FINISHED: + /* Everything OK */ + { + result->read += decode_result.read; + break; + } + case CBOR_DECODER_NEDATA: + /* Data length doesn't match MTB expectation */ + { + result->error.code = CBOR_ERR_NOTENOUGHDATA; + goto error; + } + case CBOR_DECODER_ERROR: + /* Reserved/malformed item */ + { + result->error.code = CBOR_ERR_MALFORMATED; + goto error; + } + } + + if (context.creation_failed) { + /* Most likely unsuccessful allocation - our callback has failed */ + result->error.code = CBOR_ERR_MEMERROR; + goto error; + } else if (context.syntax_error) { + result->error.code = CBOR_ERR_SYNTAXERROR; + goto error; + } + } while (stack.size > 0); + + return context.root; + +error: + result->error.position = result->read; + // debug_print("Failed with decoder error %d at %d\n", result->error.code, + // result->error.position); cbor_describe(stack.top->item, stdout); + /* Free the stack */ + while (stack.size > 0) { + cbor_decref(&stack.top->item); + _cbor_stack_pop(&stack); + } + return NULL; +} + +static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) { + cbor_item_t *res; + switch (cbor_int_get_width(item)) { + case CBOR_INT_8: + res = cbor_build_uint8(cbor_get_uint8(item)); + break; + case CBOR_INT_16: + res = cbor_build_uint16(cbor_get_uint16(item)); + break; + case CBOR_INT_32: + res = cbor_build_uint32(cbor_get_uint32(item)); + break; + case CBOR_INT_64: + res = cbor_build_uint64(cbor_get_uint64(item)); + break; + } + + if (negative) cbor_mark_negint(res); + + return res; +} + +static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) { + // cppcheck-suppress missingReturn + switch (cbor_float_get_width(item)) { + case CBOR_FLOAT_0: + return cbor_build_ctrl(cbor_ctrl_value(item)); + case CBOR_FLOAT_16: + return cbor_build_float2(cbor_float_get_float2(item)); + case CBOR_FLOAT_32: + return cbor_build_float4(cbor_float_get_float4(item)); + case CBOR_FLOAT_64: + return cbor_build_float8(cbor_float_get_float8(item)); + } +} + +cbor_item_t *cbor_copy(cbor_item_t *item) { + // cppcheck-suppress missingReturn + switch (cbor_typeof(item)) { + case CBOR_TYPE_UINT: + return _cbor_copy_int(item, false); + case CBOR_TYPE_NEGINT: + return _cbor_copy_int(item, true); + case CBOR_TYPE_BYTESTRING: + if (cbor_bytestring_is_definite(item)) { + return cbor_build_bytestring(cbor_bytestring_handle(item), + cbor_bytestring_length(item)); + } else { + cbor_item_t *res = cbor_new_indefinite_bytestring(); + if (res == NULL) { + return NULL; + } + + for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) { + cbor_item_t *chunk_copy = + cbor_copy(cbor_bytestring_chunks_handle(item)[i]); + if (chunk_copy == NULL) { + cbor_decref(&res); + return NULL; + } + if (!cbor_bytestring_add_chunk(res, chunk_copy)) { + cbor_decref(&chunk_copy); + cbor_decref(&res); + return NULL; + } + cbor_decref(&chunk_copy); + } + return res; + } + case CBOR_TYPE_STRING: + if (cbor_string_is_definite(item)) { + return cbor_build_stringn((const char *)cbor_string_handle(item), + cbor_string_length(item)); + } else { + cbor_item_t *res = cbor_new_indefinite_string(); + if (res == NULL) { + return NULL; + } + + for (size_t i = 0; i < cbor_string_chunk_count(item); i++) { + cbor_item_t *chunk_copy = + cbor_copy(cbor_string_chunks_handle(item)[i]); + if (chunk_copy == NULL) { + cbor_decref(&res); + return NULL; + } + if (!cbor_string_add_chunk(res, chunk_copy)) { + cbor_decref(&chunk_copy); + cbor_decref(&res); + return NULL; + } + cbor_decref(&chunk_copy); + } + return res; + } + case CBOR_TYPE_ARRAY: { + cbor_item_t *res; + if (cbor_array_is_definite(item)) { + res = cbor_new_definite_array(cbor_array_size(item)); + } else { + res = cbor_new_indefinite_array(); + } + if (res == NULL) { + return NULL; + } + + for (size_t i = 0; i < cbor_array_size(item); i++) { + cbor_item_t *entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i))); + if (entry_copy == NULL) { + cbor_decref(&res); + return NULL; + } + if (!cbor_array_push(res, entry_copy)) { + cbor_decref(&entry_copy); + cbor_decref(&res); + return NULL; + } + cbor_decref(&entry_copy); + } + return res; + } + case CBOR_TYPE_MAP: { + cbor_item_t *res; + if (cbor_map_is_definite(item)) { + res = cbor_new_definite_map(cbor_map_size(item)); + } else { + res = cbor_new_indefinite_map(); + } + if (res == NULL) { + return NULL; + } + + struct cbor_pair *it = cbor_map_handle(item); + for (size_t i = 0; i < cbor_map_size(item); i++) { + cbor_item_t *key_copy = cbor_copy(it[i].key); + if (key_copy == NULL) { + cbor_decref(&res); + return NULL; + } + cbor_item_t *value_copy = cbor_copy(it[i].value); + if (value_copy == NULL) { + cbor_decref(&res); + cbor_decref(&key_copy); + return NULL; + } + if (!cbor_map_add(res, (struct cbor_pair){.key = key_copy, + .value = value_copy})) { + cbor_decref(&res); + cbor_decref(&key_copy); + cbor_decref(&value_copy); + return NULL; + } + cbor_decref(&key_copy); + cbor_decref(&value_copy); + } + return res; + } + case CBOR_TYPE_TAG: { + cbor_item_t *item_copy = cbor_copy(cbor_move(cbor_tag_item(item))); + if (item_copy == NULL) { + return NULL; + } + cbor_item_t *tag = cbor_build_tag(cbor_tag_value(item), item_copy); + cbor_decref(&item_copy); + return tag; + } + case CBOR_TYPE_FLOAT_CTRL: + return _cbor_copy_float_ctrl(item); + } +} + +#if CBOR_PRETTY_PRINTER + +#include +#include +#include + +#define __STDC_FORMAT_MACROS + +static int _pow(int b, int ex) { + if (ex == 0) return 1; + int res = b; + while (--ex > 0) res *= b; + return res; +} + +static void _cbor_type_marquee(FILE *out, char *label, int indent) { + fprintf(out, "%*.*s[%s] ", indent, indent, " ", label); +} + +static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) { + const int indent_offset = 4; + switch (cbor_typeof(item)) { + case CBOR_TYPE_UINT: { + _cbor_type_marquee(out, "CBOR_TYPE_UINT", indent); + fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item))); + fprintf(out, "Value: %" PRIu64 "\n", cbor_get_int(item)); + break; + } + case CBOR_TYPE_NEGINT: { + _cbor_type_marquee(out, "CBOR_TYPE_NEGINT", indent); + fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item))); + fprintf(out, "Value: -%" PRIu64 " - 1\n", cbor_get_int(item)); + break; + } + case CBOR_TYPE_BYTESTRING: { + _cbor_type_marquee(out, "CBOR_TYPE_BYTESTRING", indent); + if (cbor_bytestring_is_indefinite(item)) { + fprintf(out, "Indefinite, Chunks: %zu, Chunk data:\n", + cbor_bytestring_chunk_count(item)); + for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) + _cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out, + indent + indent_offset); + } else { + const unsigned char *data = cbor_bytestring_handle(item); + fprintf(out, "Definite, Length: %zuB, Data:\n", + cbor_bytestring_length(item)); + fprintf(out, "%*s", indent + indent_offset, " "); + for (size_t i = 0; i < cbor_bytestring_length(item); i++) + fprintf(out, "%02x", (int)(data[i] & 0xff)); + fprintf(out, "\n"); + } + break; + } + case CBOR_TYPE_STRING: { + _cbor_type_marquee(out, "CBOR_TYPE_STRING", indent); + if (cbor_string_is_indefinite(item)) { + fprintf(out, "Indefinite, Chunks: %zu, Chunk data:\n", + cbor_string_chunk_count(item)); + for (size_t i = 0; i < cbor_string_chunk_count(item); i++) + _cbor_nested_describe(cbor_string_chunks_handle(item)[i], out, + indent + indent_offset); + } else { + fprintf(out, "Definite, Length: %zuB, Codepoints: %zu, Data:\n", + cbor_string_length(item), cbor_string_codepoint_count(item)); + fprintf(out, "%*s", indent + indent_offset, " "); + // Note: The string is not escaped, whitespace and control character + // will be printed in verbatim and take effect. + fwrite(cbor_string_handle(item), sizeof(unsigned char), + cbor_string_length(item), out); + fprintf(out, "\n"); + } + break; + } + case CBOR_TYPE_ARRAY: { + _cbor_type_marquee(out, "CBOR_TYPE_ARRAY", indent); + if (cbor_array_is_definite(item)) { + fprintf(out, "Definite, Size: %zu, Contents:\n", cbor_array_size(item)); + } else { + fprintf(out, "Indefinite, Size: %zu, Contents:\n", + cbor_array_size(item)); + } + + for (size_t i = 0; i < cbor_array_size(item); i++) + _cbor_nested_describe(cbor_array_handle(item)[i], out, + indent + indent_offset); + break; + } + case CBOR_TYPE_MAP: { + _cbor_type_marquee(out, "CBOR_TYPE_MAP", indent); + if (cbor_map_is_definite(item)) { + fprintf(out, "Definite, Size: %zu, Contents:\n", cbor_map_size(item)); + } else { + fprintf(out, "Indefinite, Size: %zu, Contents:\n", cbor_map_size(item)); + } + + // TODO: Label and group keys and values + for (size_t i = 0; i < cbor_map_size(item); i++) { + fprintf(out, "%*sMap entry %zu\n", indent + indent_offset, " ", i); + _cbor_nested_describe(cbor_map_handle(item)[i].key, out, + indent + 2 * indent_offset); + _cbor_nested_describe(cbor_map_handle(item)[i].value, out, + indent + 2 * indent_offset); + } + break; + } + case CBOR_TYPE_TAG: { + _cbor_type_marquee(out, "CBOR_TYPE_TAG", indent); + fprintf(out, "Value: %" PRIu64 "\n", cbor_tag_value(item)); + _cbor_nested_describe(cbor_move(cbor_tag_item(item)), out, + indent + indent_offset); + break; + } + case CBOR_TYPE_FLOAT_CTRL: { + _cbor_type_marquee(out, "CBOR_TYPE_FLOAT_CTRL", indent); + if (cbor_float_ctrl_is_ctrl(item)) { + if (cbor_is_bool(item)) + fprintf(out, "Bool: %s\n", cbor_get_bool(item) ? "true" : "false"); + else if (cbor_is_undef(item)) + fprintf(out, "Undefined\n"); + else if (cbor_is_null(item)) + fprintf(out, "Null\n"); + else + fprintf(out, "Simple value: %d\n", cbor_ctrl_value(item)); + } else { + fprintf(out, "Width: %dB, ", _pow(2, cbor_float_get_width(item))); + fprintf(out, "Value: %lf\n", cbor_float_get_float(item)); + } + break; + } + } +} + +void cbor_describe(cbor_item_t *item, FILE *out) { + _cbor_nested_describe(item, out, 0); +} + +#endif diff --git a/source/external/libcbor/cbor.h b/source/external/libcbor/cbor.h new file mode 100644 index 000000000..46ef8f267 --- /dev/null +++ b/source/external/libcbor/cbor.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_H_ +#define LIBCBOR_H_ + +#include "cbor/common.h" +#include "cbor/data.h" + +#include "cbor/arrays.h" +#include "cbor/bytestrings.h" +#include "cbor/floats_ctrls.h" +#include "cbor/ints.h" +#include "cbor/maps.h" +#include "cbor/strings.h" +#include "cbor/tags.h" + +#include "cbor/callbacks.h" +#include "cbor/cbor_export.h" +#include "cbor/encoding.h" +#include "cbor/serialization.h" +#include "cbor/streaming.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * High level decoding + * ============================================================================ + */ + +/** Loads data item from a buffer + * + * @param source The buffer + * @param source_size + * @param[out] result Result indicator. #CBOR_ERR_NONE on success + * @return Decoded CBOR item. The item's reference count is initialized to one. + * @return `NULL` on failure. In that case, \p result contains the location and + * description of the error. + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_load( + cbor_data source, size_t source_size, struct cbor_load_result* result); + +/** Take a deep copy of an item + * + * All items this item points to (array and map members, string chunks, tagged + * items) will be copied recursively using #cbor_copy. The new item doesn't + * alias or point to any items from the original \p item. All the reference + * counts in the new structure are set to one. + * + * @param item item to copy + * @return Reference to the new item. The item's reference count is initialized + * to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_copy(cbor_item_t* item); + +#if CBOR_PRETTY_PRINTER +#include + +CBOR_EXPORT void cbor_describe(cbor_item_t* item, FILE* out); +#endif + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_H_ diff --git a/source/external/libcbor/cbor/arrays.c b/source/external/libcbor/cbor/arrays.c new file mode 100644 index 000000000..a23bbe3cd --- /dev/null +++ b/source/external/libcbor/cbor/arrays.c @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "arrays.h" +#include +#include "internal/memory_utils.h" + +size_t cbor_array_size(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_array(item)); + return item->metadata.array_metadata.end_ptr; +} + +size_t cbor_array_allocated(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_array(item)); + return item->metadata.array_metadata.allocated; +} + +cbor_item_t *cbor_array_get(const cbor_item_t *item, size_t index) { + return cbor_incref(((cbor_item_t **)item->data)[index]); +} + +bool cbor_array_set(cbor_item_t *item, size_t index, cbor_item_t *value) { + if (index == item->metadata.array_metadata.end_ptr) { + return cbor_array_push(item, value); + } else if (index < item->metadata.array_metadata.end_ptr) { + return cbor_array_replace(item, index, value); + } else { + return false; + } +} + +bool cbor_array_replace(cbor_item_t *item, size_t index, cbor_item_t *value) { + if (index >= item->metadata.array_metadata.end_ptr) return false; + /* We cannot use cbor_array_get as that would increase the refcount */ + cbor_intermediate_decref(((cbor_item_t **)item->data)[index]); + ((cbor_item_t **)item->data)[index] = cbor_incref(value); + return true; +} + +bool cbor_array_push(cbor_item_t *array, cbor_item_t *pushee) { + CBOR_ASSERT(cbor_isa_array(array)); + struct _cbor_array_metadata *metadata = + (struct _cbor_array_metadata *)&array->metadata; + cbor_item_t **data = (cbor_item_t **)array->data; + if (cbor_array_is_definite(array)) { + /* Do not reallocate definite arrays */ + if (metadata->end_ptr >= metadata->allocated) { + return false; + } + data[metadata->end_ptr++] = pushee; + } else { + /* Exponential realloc */ + if (metadata->end_ptr >= metadata->allocated) { + // Check for overflows first + if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, metadata->allocated)) { + return false; + } + + size_t new_allocation = metadata->allocated == 0 + ? 1 + : CBOR_BUFFER_GROWTH * metadata->allocated; + + unsigned char *new_data = _cbor_realloc_multiple( + array->data, sizeof(cbor_item_t *), new_allocation); + if (new_data == NULL) { + return false; + } + + array->data = new_data; + metadata->allocated = new_allocation; + } + ((cbor_item_t **)array->data)[metadata->end_ptr++] = pushee; + } + cbor_incref(pushee); + return true; +} + +bool cbor_array_is_definite(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_array(item)); + return item->metadata.array_metadata.type == _CBOR_METADATA_DEFINITE; +} + +bool cbor_array_is_indefinite(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_array(item)); + return item->metadata.array_metadata.type == _CBOR_METADATA_INDEFINITE; +} + +cbor_item_t **cbor_array_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_array(item)); + return (cbor_item_t **)item->data; +} + +cbor_item_t *cbor_new_definite_array(size_t size) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + cbor_item_t **data = _cbor_alloc_multiple(sizeof(cbor_item_t *), size); + _CBOR_DEPENDENT_NOTNULL(item, data); + + for (size_t i = 0; i < size; i++) { + data[i] = NULL; + } + + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_ARRAY, + .metadata = {.array_metadata = {.type = _CBOR_METADATA_DEFINITE, + .allocated = size, + .end_ptr = 0}}, + .data = (unsigned char *)data}; + + return item; +} + +cbor_item_t *cbor_new_indefinite_array(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_ARRAY, + .metadata = {.array_metadata = {.type = _CBOR_METADATA_INDEFINITE, + .allocated = 0, + .end_ptr = 0}}, + .data = NULL /* Can be safely realloc-ed */ + }; + return item; +} diff --git a/source/external/libcbor/cbor/arrays.h b/source/external/libcbor/cbor/arrays.h new file mode 100644 index 000000000..db19e59d0 --- /dev/null +++ b/source/external/libcbor/cbor/arrays.h @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_ARRAYS_H +#define LIBCBOR_ARRAYS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Get the number of members + * + * @param item An array + * @return The number of members + */ +_CBOR_NODISCARD +CBOR_EXPORT size_t cbor_array_size(const cbor_item_t* item); + +/** Get the size of the allocated storage + * + * @param item An array + * @return The size of the allocated storage (number of items) + */ +_CBOR_NODISCARD +CBOR_EXPORT size_t cbor_array_allocated(const cbor_item_t* item); + +/** Get item by index + * + * @param item An array + * @param index The index (zero-based) + * @return Reference to the item, or `NULL` in case of boundary violation. + * + * Increases the reference count of the underlying item. The returned reference + * must be released using #cbor_decref. + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t* cbor_array_get(const cbor_item_t* item, size_t index); + +/** Set item by index + * + * If the index is out of bounds, the array is not modified and false is + * returned. Creating arrays with holes is not possible. + * + * @param item An array + * @param value The item to assign + * @param index The index (zero-based) + * @return `true` on success, `false` on allocation failure. + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_array_set(cbor_item_t* item, size_t index, + cbor_item_t* value); + +/** Replace item at an index + * + * The reference to the item being replaced will be released using #cbor_decref. + * + * @param item An array + * @param value The item to assign. Its reference count will be increased by + * one. + * @param index The index (zero-based) + * @return true on success, false on allocation failure. + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_array_replace(cbor_item_t* item, size_t index, + cbor_item_t* value); + +/** Is the array definite? + * + * @param item An array + * @return Is the array definite? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_array_is_definite(const cbor_item_t* item); + +/** Is the array indefinite? + * + * @param item An array + * @return Is the array indefinite? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_array_is_indefinite(const cbor_item_t* item); + +/** Get the array contents + * + * The items may be reordered and modified as long as references remain + * consistent. + * + * @param item An array item + * @return An array of #cbor_item_t pointers of size #cbor_array_size. + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t** cbor_array_handle(const cbor_item_t* item); + +/** Create new definite array + * + * @param size Number of slots to preallocate + * @return Reference to the new array item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t* cbor_new_definite_array(size_t size); + +/** Create new indefinite array + * + * @return Reference to the new array item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t* cbor_new_indefinite_array(void); + +/** Append to the end + * + * For indefinite items, storage may be reallocated. For definite items, only + * the preallocated capacity is available. + * + * @param array An array + * @param pushee The item to push. Its reference count will be increased by + * one. + * @return `true` on success, `false` on failure + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_array_push(cbor_item_t* array, cbor_item_t* pushee); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_ARRAYS_H diff --git a/source/external/libcbor/cbor/bytestrings.c b/source/external/libcbor/cbor/bytestrings.c new file mode 100644 index 000000000..528937179 --- /dev/null +++ b/source/external/libcbor/cbor/bytestrings.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "bytestrings.h" +#include +#include "internal/memory_utils.h" + +size_t cbor_bytestring_length(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + return item->metadata.bytestring_metadata.length; +} + +unsigned char *cbor_bytestring_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + return item->data; +} + +bool cbor_bytestring_is_definite(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + return item->metadata.bytestring_metadata.type == _CBOR_METADATA_DEFINITE; +} + +bool cbor_bytestring_is_indefinite(const cbor_item_t *item) { + return !cbor_bytestring_is_definite(item); +} + +cbor_item_t *cbor_new_definite_bytestring(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_BYTESTRING, + .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_DEFINITE, + .length = 0}}}; + return item; +} + +cbor_item_t *cbor_new_indefinite_bytestring(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_BYTESTRING, + .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE, + .length = 0}}, + .data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))}; + _CBOR_DEPENDENT_NOTNULL(item, item->data); + *((struct cbor_indefinite_string_data *)item->data) = + (struct cbor_indefinite_string_data){ + .chunk_count = 0, + .chunk_capacity = 0, + .chunks = NULL, + }; + return item; +} + +cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length) { + cbor_item_t *item = cbor_new_definite_bytestring(); + _CBOR_NOTNULL(item); + void *content = _cbor_malloc(length); + _CBOR_DEPENDENT_NOTNULL(item, content); + memcpy(content, handle, length); + cbor_bytestring_set_handle(item, content, length); + return item; +} + +void cbor_bytestring_set_handle(cbor_item_t *item, + cbor_mutable_data CBOR_RESTRICT_POINTER data, + size_t length) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + CBOR_ASSERT(cbor_bytestring_is_definite(item)); + item->data = data; + item->metadata.bytestring_metadata.length = length; +} + +cbor_item_t **cbor_bytestring_chunks_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + CBOR_ASSERT(cbor_bytestring_is_indefinite(item)); + return ((struct cbor_indefinite_string_data *)item->data)->chunks; +} + +size_t cbor_bytestring_chunk_count(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + CBOR_ASSERT(cbor_bytestring_is_indefinite(item)); + return ((struct cbor_indefinite_string_data *)item->data)->chunk_count; +} + +bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + CBOR_ASSERT(cbor_bytestring_is_indefinite(item)); + CBOR_ASSERT(cbor_isa_bytestring(chunk)); + CBOR_ASSERT(cbor_bytestring_is_definite(chunk)); + struct cbor_indefinite_string_data *data = + (struct cbor_indefinite_string_data *)item->data; + if (data->chunk_count == data->chunk_capacity) { + if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) { + return false; + } + + size_t new_chunk_capacity = + data->chunk_capacity == 0 ? 1 + : CBOR_BUFFER_GROWTH * (data->chunk_capacity); + + cbor_item_t **new_chunks_data = _cbor_realloc_multiple( + data->chunks, sizeof(cbor_item_t *), new_chunk_capacity); + + if (new_chunks_data == NULL) { + return false; + } + data->chunk_capacity = new_chunk_capacity; + data->chunks = new_chunks_data; + } + data->chunks[data->chunk_count++] = cbor_incref(chunk); + return true; +} diff --git a/source/external/libcbor/cbor/bytestrings.h b/source/external/libcbor/cbor/bytestrings.h new file mode 100644 index 000000000..cacd1adf9 --- /dev/null +++ b/source/external/libcbor/cbor/bytestrings.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_BYTESTRINGS_H +#define LIBCBOR_BYTESTRINGS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * Byte string manipulation + * ============================================================================ + */ + +/** Returns the length of the binary data + * + * For definite byte strings only + * + * @param item a definite bytestring + * @return length of the binary data. Zero if no chunk has been attached yet + */ +_CBOR_NODISCARD +CBOR_EXPORT size_t cbor_bytestring_length(const cbor_item_t *item); + +/** Is the byte string definite? + * + * @param item a byte string + * @return Is the byte string definite? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_bytestring_is_definite(const cbor_item_t *item); + +/** Is the byte string indefinite? + * + * @param item a byte string + * @return Is the byte string indefinite? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_bytestring_is_indefinite(const cbor_item_t *item); + +/** Get the handle to the binary data + * + * Definite items only. Modifying the data is allowed. In that case, the caller + * takes responsibility for the effect on items this item might be a part of + * + * @param item A definite byte string + * @return The address of the underlying binary data + * @return `NULL` if no data have been assigned + * yet. + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_mutable_data cbor_bytestring_handle(const cbor_item_t *item); + +/** Set the handle to the binary data + * + * @param item A definite byte string + * @param data The memory block. The caller gives up the ownership of the block. + * libcbor will deallocate it when appropriate using the `free` implementation + * configured using #cbor_set_allocs + * @param length Length of the data block + */ +CBOR_EXPORT void cbor_bytestring_set_handle( + cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, + size_t length); + +/** Get the handle to the array of chunks + * + * Manipulations with the memory block (e.g. sorting it) are allowed, but the + * validity and the number of chunks must be retained. + * + * @param item A indefinite byte string + * @return array of #cbor_bytestring_chunk_count definite bytestrings + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t **cbor_bytestring_chunks_handle( + const cbor_item_t *item); + +/** Get the number of chunks this string consist of + * + * @param item A indefinite bytestring + * @return The chunk count. 0 for freshly created items. + */ +_CBOR_NODISCARD +CBOR_EXPORT size_t cbor_bytestring_chunk_count(const cbor_item_t *item); + +/** Appends a chunk to the bytestring + * + * Indefinite byte strings only. + * + * May realloc the chunk storage. + * + * @param item An indefinite byte string + * @param chunk A definite byte string. Its reference count will be be increased + * by one. + * @return true on success, false on realloc failure. In that case, the refcount + * of `chunk` is not increased and the `item` is left intact. + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_bytestring_add_chunk(cbor_item_t *item, + cbor_item_t *chunk); + +/** Creates a new definite byte string + * + * The handle is initialized to `NULL` and length to 0 + * + * @return Reference to the new bytestring item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t *cbor_new_definite_bytestring(void); + +/** Creates a new indefinite byte string + * + * The chunks array is initialized to `NULL` and chunk count to 0 + * + * @return Reference to the new bytestring item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t *cbor_new_indefinite_bytestring(void); + +/** Creates a new byte string and initializes it + * + * The `handle` will be copied to a newly allocated block + * + * @param handle Block of binary data + * @param length Length of `data` + * @return Reference to the new bytestring item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_BYTESTRINGS_H diff --git a/source/external/libcbor/cbor/callbacks.c b/source/external/libcbor/cbor/callbacks.c new file mode 100644 index 000000000..bdf3f79ee --- /dev/null +++ b/source/external/libcbor/cbor/callbacks.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "callbacks.h" + +void cbor_null_uint8_callback(void *_CBOR_UNUSED(_ctx), + uint8_t _CBOR_UNUSED(_val)) {} + +void cbor_null_uint16_callback(void *_CBOR_UNUSED(_ctx), + uint16_t _CBOR_UNUSED(_val)) {} + +void cbor_null_uint32_callback(void *_CBOR_UNUSED(_ctx), + uint32_t _CBOR_UNUSED(_val)) {} + +void cbor_null_uint64_callback(void *_CBOR_UNUSED(_ctx), + uint64_t _CBOR_UNUSED(_val)) {} + +void cbor_null_negint8_callback(void *_CBOR_UNUSED(_ctx), + uint8_t _CBOR_UNUSED(_val)) {} + +void cbor_null_negint16_callback(void *_CBOR_UNUSED(_ctx), + uint16_t _CBOR_UNUSED(_val)) {} + +void cbor_null_negint32_callback(void *_CBOR_UNUSED(_ctx), + uint32_t _CBOR_UNUSED(_val)) {} + +void cbor_null_negint64_callback(void *_CBOR_UNUSED(_ctx), + uint64_t _CBOR_UNUSED(_val)) {} + +void cbor_null_string_callback(void *_CBOR_UNUSED(_ctx), + cbor_data _CBOR_UNUSED(_val), + uint64_t _CBOR_UNUSED(_val2)) {} + +void cbor_null_string_start_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_byte_string_callback(void *_CBOR_UNUSED(_ctx), + cbor_data _CBOR_UNUSED(_val), + uint64_t _CBOR_UNUSED(_val2)) {} + +void cbor_null_byte_string_start_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_array_start_callback(void *_CBOR_UNUSED(_ctx), + uint64_t _CBOR_UNUSED(_val)) {} + +void cbor_null_indef_array_start_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_map_start_callback(void *_CBOR_UNUSED(_ctx), + uint64_t _CBOR_UNUSED(_val)) {} + +void cbor_null_indef_map_start_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_tag_callback(void *_CBOR_UNUSED(_ctx), + uint64_t _CBOR_UNUSED(_val)) {} + +void cbor_null_float2_callback(void *_CBOR_UNUSED(_ctx), + float _CBOR_UNUSED(_val)) {} + +void cbor_null_float4_callback(void *_CBOR_UNUSED(_ctx), + float _CBOR_UNUSED(_val)) {} + +void cbor_null_float8_callback(void *_CBOR_UNUSED(_ctx), + double _CBOR_UNUSED(_val)) {} + +void cbor_null_null_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_undefined_callback(void *_CBOR_UNUSED(_ctx)) {} + +void cbor_null_boolean_callback(void *_CBOR_UNUSED(_ctx), + bool _CBOR_UNUSED(_val)) {} + +void cbor_null_indef_break_callback(void *_CBOR_UNUSED(_ctx)) {} + +CBOR_EXPORT const struct cbor_callbacks cbor_empty_callbacks = { + /* Type 0 - Unsigned integers */ + .uint8 = cbor_null_uint8_callback, + .uint16 = cbor_null_uint16_callback, + .uint32 = cbor_null_uint32_callback, + .uint64 = cbor_null_uint64_callback, + + /* Type 1 - Negative integers */ + .negint8 = cbor_null_negint8_callback, + .negint16 = cbor_null_negint16_callback, + .negint32 = cbor_null_negint32_callback, + .negint64 = cbor_null_negint64_callback, + + /* Type 2 - Byte strings */ + .byte_string_start = cbor_null_byte_string_start_callback, + .byte_string = cbor_null_byte_string_callback, + + /* Type 3 - Strings */ + .string_start = cbor_null_string_start_callback, + .string = cbor_null_string_callback, + + /* Type 4 - Arrays */ + .indef_array_start = cbor_null_indef_array_start_callback, + .array_start = cbor_null_array_start_callback, + + /* Type 5 - Maps */ + .indef_map_start = cbor_null_indef_map_start_callback, + .map_start = cbor_null_map_start_callback, + + /* Type 6 - Tags */ + .tag = cbor_null_tag_callback, + + /* Type 7 - Floats & misc */ + /* Type names cannot be member names */ + .float2 = cbor_null_float2_callback, + /* 2B float is not supported in standard C */ + .float4 = cbor_null_float4_callback, + .float8 = cbor_null_float8_callback, + .undefined = cbor_null_undefined_callback, + .null = cbor_null_null_callback, + .boolean = cbor_null_boolean_callback, + + /* Shared indefinites */ + .indef_break = cbor_null_indef_break_callback, +}; diff --git a/source/external/libcbor/cbor/callbacks.h b/source/external/libcbor/cbor/callbacks.h new file mode 100644 index 000000000..c7ae20568 --- /dev/null +++ b/source/external/libcbor/cbor/callbacks.h @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_CALLBACKS_H +#define LIBCBOR_CALLBACKS_H + +#include + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Callback prototype */ +typedef void (*cbor_int8_callback)(void *, uint8_t); + +/** Callback prototype */ +typedef void (*cbor_int16_callback)(void *, uint16_t); + +/** Callback prototype */ +typedef void (*cbor_int32_callback)(void *, uint32_t); + +/** Callback prototype */ +typedef void (*cbor_int64_callback)(void *, uint64_t); + +/** Callback prototype */ +typedef void (*cbor_simple_callback)(void *); + +/** Callback prototype */ +typedef void (*cbor_string_callback)(void *, cbor_data, uint64_t); + +/** Callback prototype */ +typedef void (*cbor_collection_callback)(void *, uint64_t); + +/** Callback prototype */ +typedef void (*cbor_float_callback)(void *, float); + +/** Callback prototype */ +typedef void (*cbor_double_callback)(void *, double); + +/** Callback prototype */ +typedef void (*cbor_bool_callback)(void *, bool); + +/** Callback bundle -- passed to the decoder */ +struct cbor_callbacks { + /** Unsigned int */ + cbor_int8_callback uint8; + /** Unsigned int */ + cbor_int16_callback uint16; + /** Unsigned int */ + cbor_int32_callback uint32; + /** Unsigned int */ + cbor_int64_callback uint64; + + /** Negative int */ + cbor_int64_callback negint64; + /** Negative int */ + cbor_int32_callback negint32; + /** Negative int */ + cbor_int16_callback negint16; + /** Negative int */ + cbor_int8_callback negint8; + + /** Definite byte string */ + cbor_simple_callback byte_string_start; + /** Indefinite byte string start */ + cbor_string_callback byte_string; + + /** Definite string */ + cbor_string_callback string; + /** Indefinite string start */ + cbor_simple_callback string_start; + + /** Definite array */ + cbor_simple_callback indef_array_start; + /** Indefinite array */ + cbor_collection_callback array_start; + + /** Definite map */ + cbor_simple_callback indef_map_start; + /** Indefinite map */ + cbor_collection_callback map_start; + + /** Tags */ + cbor_int64_callback tag; + + /** Half float */ + cbor_float_callback float2; + /** Single float */ + cbor_float_callback float4; + /** Double float */ + cbor_double_callback float8; + /** Undef */ + cbor_simple_callback undefined; + /** Null */ + cbor_simple_callback null; + /** Bool */ + cbor_bool_callback boolean; + + /** Indefinite item break */ + cbor_simple_callback indef_break; +}; + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_uint8_callback(void *, uint8_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_uint16_callback(void *, uint16_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_uint32_callback(void *, uint32_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_uint64_callback(void *, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_negint8_callback(void *, uint8_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_negint16_callback(void *, uint16_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_negint32_callback(void *, uint32_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_negint64_callback(void *, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_string_callback(void *, cbor_data, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_string_start_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_byte_string_callback(void *, cbor_data, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_byte_string_start_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_array_start_callback(void *, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_indef_array_start_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_map_start_callback(void *, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_indef_map_start_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_tag_callback(void *, uint64_t); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_float2_callback(void *, float); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_float4_callback(void *, float); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_float8_callback(void *, double); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_null_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_undefined_callback(void *); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_boolean_callback(void *, bool); + +/** Dummy callback implementation - does nothing */ +CBOR_EXPORT void cbor_null_indef_break_callback(void *); + +/** Dummy callback bundle - does nothing */ +CBOR_EXPORT extern const struct cbor_callbacks cbor_empty_callbacks; + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_CALLBACKS_H diff --git a/source/external/libcbor/cbor/cbor_export.h b/source/external/libcbor/cbor/cbor_export.h new file mode 100644 index 000000000..b739bb32b --- /dev/null +++ b/source/external/libcbor/cbor/cbor_export.h @@ -0,0 +1,14 @@ +/** + * DO NOT DIRECTLY MODIFY THIS FILE: + * + * The code in this file is generated from scripts/import_libcbor.py + * and any modifications should be in there. + */ + +#ifndef CBOR_EXPORT_H +#define CBOR_EXPORT_H + +/* Don't export anything from libcbor */ +#define CBOR_EXPORT + +#endif /* CBOR_EXPORT_H */ diff --git a/source/external/libcbor/cbor/common.c b/source/external/libcbor/cbor/common.c new file mode 100644 index 000000000..efbd37ed7 --- /dev/null +++ b/source/external/libcbor/cbor/common.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "cbor/common.h" +#include "arrays.h" +#include "bytestrings.h" +#include "data.h" +#include "floats_ctrls.h" +#include "ints.h" +#include "maps.h" +#include "strings.h" +#include "tags.h" + +#ifdef DEBUG +bool _cbor_enable_assert = true; +#endif + +bool cbor_isa_uint(const cbor_item_t *item) { + return item->type == CBOR_TYPE_UINT; +} + +bool cbor_isa_negint(const cbor_item_t *item) { + return item->type == CBOR_TYPE_NEGINT; +} + +bool cbor_isa_bytestring(const cbor_item_t *item) { + return item->type == CBOR_TYPE_BYTESTRING; +} + +bool cbor_isa_string(const cbor_item_t *item) { + return item->type == CBOR_TYPE_STRING; +} + +bool cbor_isa_array(const cbor_item_t *item) { + return item->type == CBOR_TYPE_ARRAY; +} + +bool cbor_isa_map(const cbor_item_t *item) { + return item->type == CBOR_TYPE_MAP; +} + +bool cbor_isa_tag(const cbor_item_t *item) { + return item->type == CBOR_TYPE_TAG; +} + +bool cbor_isa_float_ctrl(const cbor_item_t *item) { + return item->type == CBOR_TYPE_FLOAT_CTRL; +} + +cbor_type cbor_typeof(const cbor_item_t *item) { return item->type; } + +bool cbor_is_int(const cbor_item_t *item) { + return cbor_isa_uint(item) || cbor_isa_negint(item); +} + +bool cbor_is_bool(const cbor_item_t *item) { + return cbor_isa_float_ctrl(item) && + (cbor_ctrl_value(item) == CBOR_CTRL_FALSE || + cbor_ctrl_value(item) == CBOR_CTRL_TRUE); +} + +bool cbor_is_null(const cbor_item_t *item) { + return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL; +} + +bool cbor_is_undef(const cbor_item_t *item) { + return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF; +} + +bool cbor_is_float(const cbor_item_t *item) { + return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item); +} + +cbor_item_t *cbor_incref(cbor_item_t *item) { + item->refcount++; + return item; +} + +void cbor_decref(cbor_item_t **item_ref) { + cbor_item_t *item = *item_ref; + CBOR_ASSERT(item->refcount > 0); + if (--item->refcount == 0) { + switch (item->type) { + case CBOR_TYPE_UINT: + /* Fallthrough */ + case CBOR_TYPE_NEGINT: + /* Combined allocation, freeing the item suffices */ + { break; } + case CBOR_TYPE_BYTESTRING: { + if (cbor_bytestring_is_definite(item)) { + _cbor_free(item->data); + } else { + /* We need to decref all chunks */ + cbor_item_t **handle = cbor_bytestring_chunks_handle(item); + for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) + cbor_decref(&handle[i]); + _cbor_free( + ((struct cbor_indefinite_string_data *)item->data)->chunks); + _cbor_free(item->data); + } + break; + } + case CBOR_TYPE_STRING: { + if (cbor_string_is_definite(item)) { + _cbor_free(item->data); + } else { + /* We need to decref all chunks */ + cbor_item_t **handle = cbor_string_chunks_handle(item); + for (size_t i = 0; i < cbor_string_chunk_count(item); i++) + cbor_decref(&handle[i]); + _cbor_free( + ((struct cbor_indefinite_string_data *)item->data)->chunks); + _cbor_free(item->data); + } + break; + } + case CBOR_TYPE_ARRAY: { + /* Get all items and decref them */ + cbor_item_t **handle = cbor_array_handle(item); + size_t size = cbor_array_size(item); + for (size_t i = 0; i < size; i++) + if (handle[i] != NULL) cbor_decref(&handle[i]); + _cbor_free(item->data); + break; + } + case CBOR_TYPE_MAP: { + struct cbor_pair *handle = cbor_map_handle(item); + for (size_t i = 0; i < item->metadata.map_metadata.end_ptr; + i++, handle++) { + cbor_decref(&handle->key); + if (handle->value != NULL) cbor_decref(&handle->value); + } + _cbor_free(item->data); + break; + } + case CBOR_TYPE_TAG: { + if (item->metadata.tag_metadata.tagged_item != NULL) + cbor_decref(&item->metadata.tag_metadata.tagged_item); + _cbor_free(item->data); + break; + } + case CBOR_TYPE_FLOAT_CTRL: { + /* Floats have combined allocation */ + break; + } + } + _cbor_free(item); + *item_ref = NULL; + } +} + +void cbor_intermediate_decref(cbor_item_t *item) { cbor_decref(&item); } + +size_t cbor_refcount(const cbor_item_t *item) { return item->refcount; } + +cbor_item_t *cbor_move(cbor_item_t *item) { + item->refcount--; + return item; +} diff --git a/source/external/libcbor/cbor/common.h b/source/external/libcbor/cbor/common.h new file mode 100644 index 000000000..1d0b426cf --- /dev/null +++ b/source/external/libcbor/cbor/common.h @@ -0,0 +1,339 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_COMMON_H +#define LIBCBOR_COMMON_H + +#include +#include +#include +#include +#include + +#include "cbor/cbor_export.h" +#include "cbor/configuration.h" +#include "data.h" + +#ifdef __cplusplus +extern "C" { + +/** + * C99 is not a subset of C++ -- 'restrict' qualifier is not a part of the + * language. This is a workaround to keep it in C headers -- compilers allow + * linking non-restrict signatures with restrict implementations. + * + * If you know a nicer way, please do let me know. + */ +#define CBOR_RESTRICT_POINTER + +#else + +// MSVC + C++ workaround +#define CBOR_RESTRICT_POINTER CBOR_RESTRICT_SPECIFIER + +#endif + +static const uint8_t cbor_major_version = CBOR_MAJOR_VERSION; +static const uint8_t cbor_minor_version = CBOR_MINOR_VERSION; +static const uint8_t cbor_patch_version = CBOR_PATCH_VERSION; + +#define CBOR_VERSION \ + _CBOR_TO_STR(CBOR_MAJOR_VERSION) \ + "." _CBOR_TO_STR(CBOR_MINOR_VERSION) "." _CBOR_TO_STR(CBOR_PATCH_VERSION) +#define CBOR_HEX_VERSION \ + ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION) + +/* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing + */ +#ifdef DEBUG +#include +#define _cbor_debug_print(fmt, ...) \ + do { \ + if (DEBUG) \ + fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, \ + __VA_ARGS__); \ + } while (0) +extern bool _cbor_enable_assert; +// Like `assert`, but can be dynamically disabled in tests to allow testing +// invalid behaviors. +#define CBOR_ASSERT(e) assert(!_cbor_enable_assert || (e)) +#define _CBOR_TEST_DISABLE_ASSERT(block) \ + do { \ + _cbor_enable_assert = false; \ + block _cbor_enable_assert = true; \ + } while (0) +#else +#define debug_print(fmt, ...) \ + do { \ + } while (0) +#define CBOR_ASSERT(e) +#define _CBOR_TEST_DISABLE_ASSERT(block) \ + do { \ + block \ + } while (0) +#endif + +#define _CBOR_TO_STR_(x) #x +#define _CBOR_TO_STR(x) _CBOR_TO_STR_(x) /* enables proper double expansion */ + +#ifdef __GNUC__ +#define _CBOR_UNUSED(x) __attribute__((__unused__)) x +// TODO(https://github.com/PJK/libcbor/issues/247): Prefer [[nodiscard]] if +// available +#define _CBOR_NODISCARD __attribute__((warn_unused_result)) +#elif defined(_MSC_VER) +#define _CBOR_UNUSED(x) __pragma(warning(suppress : 4100 4101)) x +#define _CBOR_NODISCARD +#else +#define _CBOR_UNUSED(x) x +#define _CBOR_NODISCARD +#endif + +typedef void *(*_cbor_malloc_t)(size_t); +typedef void *(*_cbor_realloc_t)(void *, size_t); +typedef void (*_cbor_free_t)(void *); + +CBOR_EXPORT extern _cbor_malloc_t _cbor_malloc; +CBOR_EXPORT extern _cbor_realloc_t _cbor_realloc; +CBOR_EXPORT extern _cbor_free_t _cbor_free; + +// Macro to short-circuit builder functions when memory allocation fails +#define _CBOR_NOTNULL(cbor_item) \ + do { \ + if (cbor_item == NULL) { \ + return NULL; \ + } \ + } while (0) + +// Macro to short-circuit builders when memory allocation of nested data fails +#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer) \ + do { \ + if (pointer == NULL) { \ + _cbor_free(cbor_item); \ + return NULL; \ + } \ + } while (0) + +/** Sets the memory management routines to use. + * + * By default, libcbor will use the standard library `malloc`, `realloc`, and + * `free`. + * + * \rst + * .. warning:: This function modifies the global state and should therefore be + * used accordingly. Changing the memory handlers while allocated items exist + * will result in a ``free``/``malloc`` mismatch. This function is not thread + * safe with respect to both itself and all the other *libcbor* functions that + * work with the heap. + * + * .. note:: `realloc` implementation must correctly support `NULL` reallocation + * (see e.g. http://en.cppreference.com/w/c/memory/realloc) + * \endrst + * + * @param custom_malloc malloc implementation + * @param custom_realloc realloc implementation + * @param custom_free free implementation + */ +CBOR_EXPORT void cbor_set_allocs(_cbor_malloc_t custom_malloc, + _cbor_realloc_t custom_realloc, + _cbor_free_t custom_free); + +/* + * ============================================================================ + * Type manipulation + * ============================================================================ + */ + +/** Get the type of the item + * + * @param item + * @return The type + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_type cbor_typeof( + const cbor_item_t *item); /* Will be inlined iff link-time opt is enabled */ + +/* Standard CBOR Major item types */ + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item an #CBOR_TYPE_UINT? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_uint(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_NEGINT? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_negint(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_BYTESTRING? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_bytestring(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_STRING? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_string(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item an #CBOR_TYPE_ARRAY? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_array(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_MAP? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_map(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_TAG? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_tag(const cbor_item_t *item); + +/** Does the item have the appropriate major type? + * @param item the item + * @return Is the item a #CBOR_TYPE_FLOAT_CTRL? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_isa_float_ctrl(const cbor_item_t *item); + +/* Practical types with respect to their semantics (but not tag values) */ + +/** Is the item an integer, either positive or negative? + * @param item the item + * @return Is the item an integer, either positive or negative? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_is_int(const cbor_item_t *item); + +/** Is the item an a floating point number? + * @param item the item + * @return Is the item a floating point number? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_is_float(const cbor_item_t *item); + +/** Is the item an a boolean? + * @param item the item + * @return Is the item a boolean? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_is_bool(const cbor_item_t *item); + +/** Does this item represent `null` + * + * \rst + * .. warning:: This is in no way related to the value of the pointer. Passing a + * null pointer will most likely result in a crash. + * \endrst + * + * @param item the item + * @return Is the item (CBOR logical) null? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_is_null(const cbor_item_t *item); + +/** Does this item represent `undefined` + * + * \rst + * .. warning:: Care must be taken to distinguish nulls and undefined values in + * C. + * \endrst + * + * @param item the item + * @return Is the item (CBOR logical) undefined? + */ +_CBOR_NODISCARD +CBOR_EXPORT bool cbor_is_undef(const cbor_item_t *item); + +/* + * ============================================================================ + * Memory management + * ============================================================================ + */ + +/** Increases the item's reference count by one + * + * Constant complexity; items referring to this one or items being referred to + * are not updated. + * + * This function can be used to extend reference counting to client code. + * + * @param item Reference to an item + * @return The input \p item + */ +CBOR_EXPORT cbor_item_t *cbor_incref(cbor_item_t *item); + +/** Decreases the item's reference count by one, deallocating the item if needed + * + * In case the item is deallocated, the reference count of all items this item + * references will also be #cbor_decref 'ed recursively. + * + * @param item Reference to an item. Will be set to `NULL` if deallocated + */ +CBOR_EXPORT void cbor_decref(cbor_item_t **item); + +/** Decreases the item's reference count by one, deallocating the item if needed + * + * Convenience wrapper for #cbor_decref when its set-to-null behavior is not + * needed + * + * @param item Reference to an item + */ +CBOR_EXPORT void cbor_intermediate_decref(cbor_item_t *item); + +/** Get the item's reference count + * + * \rst + * .. warning:: This does *not* account for transitive references. + * \endrst + * + * @todo Add some inline examples for reference counting + * + * @param item the item + * @return the reference count + */ +_CBOR_NODISCARD +CBOR_EXPORT size_t cbor_refcount(const cbor_item_t *item); + +/** Provides CPP-like move construct + * + * Decreases the reference count by one, but does not deallocate the item even + * if its refcount reaches zero. This is useful for passing intermediate values + * to functions that increase reference count. Should only be used with + * functions that `incref` their arguments. + * + * \rst + * .. warning:: If the item is moved without correctly increasing the reference + * count afterwards, the memory will be leaked. + * \endrst + * + * @param item Reference to an item + * @return the item with reference count decreased by one + */ +_CBOR_NODISCARD +CBOR_EXPORT cbor_item_t *cbor_move(cbor_item_t *item); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_COMMON_H diff --git a/source/external/libcbor/cbor/configuration.h b/source/external/libcbor/cbor/configuration.h new file mode 100644 index 000000000..83fe90bd3 --- /dev/null +++ b/source/external/libcbor/cbor/configuration.h @@ -0,0 +1,46 @@ +/** + * DO NOT DIRECTLY MODIFY THIS FILE: + * + * The code in this file is generated from scripts/import_libcbor.py + * and any modifications should be in there. + */ + +#ifndef LIBCBOR_CONFIGURATION_H +#define LIBCBOR_CONFIGURATION_H + +#define CBOR_MAJOR_VERSION 0 +#define CBOR_MINOR_VERSION 11 +#define CBOR_PATCH_VERSION 0 + +#define CBOR_BUFFER_GROWTH 2 +#define CBOR_MAX_STACK_SIZE 2048 +#define CBOR_PRETTY_PRINTER 1 + +#if defined(_MSC_VER) +# define CBOR_RESTRICT_SPECIFIER +#else +# define CBOR_RESTRICT_SPECIFIER restrict +#endif + +#define CBOR_INLINE_SPECIFIER + +/* Ignore the compiler warnings for libcbor. */ +#ifdef _MSC_VER +# pragma warning(disable : 4028) +# pragma warning(disable : 4715) +# pragma warning(disable : 4232) +# pragma warning(disable : 4068) +# pragma warning(disable : 4244) +# pragma warning(disable : 4701) +# pragma warning(disable : 4703) +#endif + +#ifdef __clang__ +# pragma clang diagnostic ignored "-Wreturn-type" +#elif defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wreturn-type" +# pragma GCC diagnostic ignored "-Wunknown-pragmas" +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + +#endif // LIBCBOR_CONFIGURATION_H diff --git a/source/external/libcbor/cbor/data.h b/source/external/libcbor/cbor/data.h new file mode 100644 index 000000000..a12e92f20 --- /dev/null +++ b/source/external/libcbor/cbor/data.h @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_DATA_H +#define LIBCBOR_DATA_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef const unsigned char* cbor_data; +typedef unsigned char* cbor_mutable_data; + +/** Specifies the Major type of ::cbor_item_t */ +typedef enum cbor_type { + CBOR_TYPE_UINT /** 0 - positive integers */ + , + CBOR_TYPE_NEGINT /** 1 - negative integers*/ + , + CBOR_TYPE_BYTESTRING /** 2 - byte strings */ + , + CBOR_TYPE_STRING /** 3 - strings */ + , + CBOR_TYPE_ARRAY /** 4 - arrays */ + , + CBOR_TYPE_MAP /** 5 - maps */ + , + CBOR_TYPE_TAG /** 6 - tags */ + , + CBOR_TYPE_FLOAT_CTRL /** 7 - decimals and special values (true, false, nil, + ...) */ +} cbor_type; + +/** Possible decoding errors */ +typedef enum { + CBOR_ERR_NONE, + CBOR_ERR_NOTENOUGHDATA, + CBOR_ERR_NODATA, + // TODO: Should be "malformed" or at least "malformatted". Retained for + // backwards compatibility. + CBOR_ERR_MALFORMATED, + CBOR_ERR_MEMERROR /** Memory error - item allocation failed. Is it too big for + your allocator? */ + , + CBOR_ERR_SYNTAXERROR /** Stack parsing algorithm failed */ +} cbor_error_code; + +/** Possible widths of #CBOR_TYPE_UINT items */ +typedef enum { + CBOR_INT_8, + CBOR_INT_16, + CBOR_INT_32, + CBOR_INT_64 +} cbor_int_width; + +/** Possible widths of #CBOR_TYPE_FLOAT_CTRL items */ +typedef enum { + CBOR_FLOAT_0 /** Internal use - ctrl and special values */ + , + CBOR_FLOAT_16 /** Half float */ + , + CBOR_FLOAT_32 /** Single float */ + , + CBOR_FLOAT_64 /** Double */ +} cbor_float_width; + +/** Metadata for dynamically sized types */ +typedef enum { + _CBOR_METADATA_DEFINITE, + _CBOR_METADATA_INDEFINITE +} _cbor_dst_metadata; + +/** Semantic mapping for CTRL simple values */ +typedef enum { + CBOR_CTRL_NONE = 0, + CBOR_CTRL_FALSE = 20, + CBOR_CTRL_TRUE = 21, + CBOR_CTRL_NULL = 22, + CBOR_CTRL_UNDEF = 23 +} _cbor_ctrl; + +// Metadata items use size_t (instead of uint64_t) because items in memory take +// up at least 1B per entry or string byte, so if size_t is narrower than +// uint64_t, we wouldn't be able to create them in the first place and can save +// some space. + +/** Integers specific metadata */ +struct _cbor_int_metadata { + cbor_int_width width; +}; + +/** Bytestrings specific metadata */ +struct _cbor_bytestring_metadata { + size_t length; + _cbor_dst_metadata type; +}; + +/** Strings specific metadata */ +struct _cbor_string_metadata { + size_t length; + size_t codepoint_count; /* Sum of chunks' codepoint_counts for indefinite + strings */ + _cbor_dst_metadata type; +}; + +/** Arrays specific metadata */ +struct _cbor_array_metadata { + size_t allocated; + size_t end_ptr; + _cbor_dst_metadata type; +}; + +/** Maps specific metadata */ +struct _cbor_map_metadata { + size_t allocated; + size_t end_ptr; + _cbor_dst_metadata type; +}; + +/** Arrays specific metadata + * + * The pointer is included - cbor_item_metadata is + * 2 * sizeof(size_t) + sizeof(_cbor_string_type_metadata), + * lets use the space + */ +struct _cbor_tag_metadata { + struct cbor_item_t* tagged_item; + uint64_t value; +}; + +/** Floats specific metadata - includes CTRL values */ +struct _cbor_float_ctrl_metadata { + cbor_float_width width; + uint8_t ctrl; +}; + +/** Raw memory casts helper */ +union _cbor_float_helper { + float as_float; + uint32_t as_uint; +}; + +/** Raw memory casts helper */ +union _cbor_double_helper { + double as_double; + uint64_t as_uint; +}; + +/** Union of metadata across all possible types - discriminated in #cbor_item_t + */ +union cbor_item_metadata { + struct _cbor_int_metadata int_metadata; + struct _cbor_bytestring_metadata bytestring_metadata; + struct _cbor_string_metadata string_metadata; + struct _cbor_array_metadata array_metadata; + struct _cbor_map_metadata map_metadata; + struct _cbor_tag_metadata tag_metadata; + struct _cbor_float_ctrl_metadata float_ctrl_metadata; +}; + +/** The item handle */ +typedef struct cbor_item_t { + /** Discriminated by type */ + union cbor_item_metadata metadata; + /** Reference count - initialize to 0 */ + size_t refcount; + /** Major type discriminator */ + cbor_type type; + /** Raw data block - interpretation depends on metadata */ + unsigned char* data; +} cbor_item_t; + +/** Defines cbor_item_t#data structure for indefinite strings and bytestrings + * + * Used to cast the raw representation for a sane manipulation + */ +struct cbor_indefinite_string_data { + size_t chunk_count; + size_t chunk_capacity; + cbor_item_t** chunks; +}; + +/** High-level decoding error */ +struct cbor_error { + /** Approximate position */ + size_t position; + /** Description */ + cbor_error_code code; +}; + +/** Simple pair of items for use in maps */ +struct cbor_pair { + cbor_item_t *key, *value; +}; + +/** High-level decoding result */ +struct cbor_load_result { + /** Error indicator */ + struct cbor_error error; + /** Number of bytes read */ + size_t read; +}; + +/** Streaming decoder result - status */ +enum cbor_decoder_status { + /** Decoding finished successfully (a callback has been invoked) + * + * Note that this does *not* mean that the buffer has been fully decoded; + * there may still be unread bytes for which no callback has been involved. + */ + CBOR_DECODER_FINISHED, + /** Not enough data to invoke a callback */ + // TODO: The name is inconsistent with CBOR_ERR_NOTENOUGHDATA. Retained for + // backwards compatibility. + CBOR_DECODER_NEDATA, + /** Bad data (reserved MTB, malformed value, etc.) */ + CBOR_DECODER_ERROR +}; + +/** Streaming decoder result */ +struct cbor_decoder_result { + /** Input bytes read/consumed + * + * If this is less than the size of input buffer, the client will likely + * resume parsing starting at the next byte (e.g. `buffer + result.read`). + * + * Set to 0 if the #status is not #CBOR_DECODER_FINISHED. + */ + size_t read; + + /** The decoding status */ + enum cbor_decoder_status status; + + /** Number of bytes in the input buffer needed to resume parsing + * + * Set to 0 unless the result status is #CBOR_DECODER_NEDATA. If it is, then: + * - If at least one byte was passed, #required will be set to the minimum + * number of bytes needed to invoke a decoded callback on the current + * prefix. + * + * For example: Attempting to decode a 1B buffer containing `0x19` will + * set #required to 3 as `0x19` signals a 2B integer item, so we need at + * least 3B to continue (the `0x19` MTB byte and two bytes of data needed + * to invoke #cbor_callbacks.uint16). + * + * - If there was no data at all, #read will always be set to 1 + */ + size_t required; +}; + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_DATA_H diff --git a/source/external/libcbor/cbor/encoding.c b/source/external/libcbor/cbor/encoding.c new file mode 100644 index 000000000..9d931d175 --- /dev/null +++ b/source/external/libcbor/cbor/encoding.c @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "encoding.h" +#include "internal/encoders.h" + +size_t cbor_encode_uint8(uint8_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint8(value, buffer, buffer_size, 0x00); +} + +size_t cbor_encode_uint16(uint16_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint16(value, buffer, buffer_size, 0x00); +} + +size_t cbor_encode_uint32(uint32_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint32(value, buffer, buffer_size, 0x00); +} + +size_t cbor_encode_uint64(uint64_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint64(value, buffer, buffer_size, 0x00); +} + +size_t cbor_encode_uint(uint64_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint(value, buffer, buffer_size, 0x00); +} + +size_t cbor_encode_negint8(uint8_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint8(value, buffer, buffer_size, 0x20); +} + +size_t cbor_encode_negint16(uint16_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint16(value, buffer, buffer_size, 0x20); +} + +size_t cbor_encode_negint32(uint32_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint32(value, buffer, buffer_size, 0x20); +} + +size_t cbor_encode_negint64(uint64_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint64(value, buffer, buffer_size, 0x20); +} + +size_t cbor_encode_negint(uint64_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint(value, buffer, buffer_size, 0x20); +} + +size_t cbor_encode_bytestring_start(size_t length, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x40); +} + +size_t _cbor_encode_byte(uint8_t value, unsigned char *buffer, + size_t buffer_size) { + if (buffer_size >= 1) { + buffer[0] = value; + return 1; + } else + return 0; +} + +size_t cbor_encode_indef_bytestring_start(unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_byte(0x5F, buffer, buffer_size); +} + +size_t cbor_encode_string_start(size_t length, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x60); +} + +size_t cbor_encode_indef_string_start(unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_byte(0x7F, buffer, buffer_size); +} + +size_t cbor_encode_array_start(size_t length, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x80); +} + +size_t cbor_encode_indef_array_start(unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_byte(0x9F, buffer, buffer_size); +} + +size_t cbor_encode_map_start(size_t length, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0xA0); +} + +size_t cbor_encode_indef_map_start(unsigned char *buffer, size_t buffer_size) { + return _cbor_encode_byte(0xBF, buffer, buffer_size); +} + +size_t cbor_encode_tag(uint64_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint(value, buffer, buffer_size, 0xC0); +} + +size_t cbor_encode_bool(bool value, unsigned char *buffer, size_t buffer_size) { + return value ? _cbor_encode_byte(0xF5, buffer, buffer_size) + : _cbor_encode_byte(0xF4, buffer, buffer_size); +} + +size_t cbor_encode_null(unsigned char *buffer, size_t buffer_size) { + return _cbor_encode_byte(0xF6, buffer, buffer_size); +} + +size_t cbor_encode_undef(unsigned char *buffer, size_t buffer_size) { + return _cbor_encode_byte(0xF7, buffer, buffer_size); +} + +size_t cbor_encode_half(float value, unsigned char *buffer, + size_t buffer_size) { + /* Assuming value is normalized */ + uint32_t val = ((union _cbor_float_helper){.as_float = value}).as_uint; + uint16_t res; + uint8_t exp = (uint8_t)((val & 0x7F800000u) >> + 23u); /* 0b0111_1111_1000_0000_0000_0000_0000_0000 */ + uint32_t mant = + val & 0x7FFFFFu; /* 0b0000_0000_0111_1111_1111_1111_1111_1111 */ + if (exp == 0xFF) { /* Infinity or NaNs */ + if (value != value) { + // We discard information bits in half-float NaNs. This is + // not required for the core CBOR protocol (it is only a suggestion in + // Section 3.9). + // See https://github.com/PJK/libcbor/issues/215 + res = (uint16_t)0x007e00; + } else { + // If the mantissa is non-zero, we have a NaN, but those are handled + // above. See + // https://en.wikipedia.org/wiki/Half-precision_floating-point_format + CBOR_ASSERT(mant == 0u); + res = (uint16_t)((val & 0x80000000u) >> 16u | 0x7C00u); + } + } else if (exp == 0x00) { /* Zeroes or subnorms */ + res = (uint16_t)((val & 0x80000000u) >> 16u | mant >> 13u); + } else { /* Normal numbers */ + int8_t logical_exp = (int8_t)(exp - 127); + CBOR_ASSERT(logical_exp == exp - 127); + + // Now we know that 2^exp <= 0 logically + if (logical_exp < -24) { + /* No unambiguous representation exists, this float is not a half float + and is too small to be represented using a half, round off to zero. + Consistent with the reference implementation. */ + res = 0; + } else if (logical_exp < -14) { + /* Offset the remaining decimal places by shifting the significand, the + value is lost. This is an implementation decision that works around the + absence of standard half-float in the language. */ + res = (uint16_t)((val & 0x80000000u) >> 16u) | // Extract sign bit + ((uint16_t)(1u << (24u + logical_exp)) + + (uint16_t)(((mant >> (-logical_exp - 2)) + 1) >> + 1)); // Round half away from zero for simplicity + } else { + res = (uint16_t)((val & 0x80000000u) >> 16u | + ((((uint8_t)logical_exp) + 15u) << 10u) | + (uint16_t)(mant >> 13u)); + } + } + return _cbor_encode_uint16(res, buffer, buffer_size, 0xE0); +} + +size_t cbor_encode_single(float value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint32( + ((union _cbor_float_helper){.as_float = value}).as_uint, buffer, + buffer_size, 0xE0); +} + +size_t cbor_encode_double(double value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint64( + ((union _cbor_double_helper){.as_double = value}).as_uint, buffer, + buffer_size, 0xE0); +} + +size_t cbor_encode_break(unsigned char *buffer, size_t buffer_size) { + return _cbor_encode_byte(0xFF, buffer, buffer_size); +} + +size_t cbor_encode_ctrl(uint8_t value, unsigned char *buffer, + size_t buffer_size) { + return _cbor_encode_uint8(value, buffer, buffer_size, 0xE0); +} diff --git a/source/external/libcbor/cbor/encoding.h b/source/external/libcbor/cbor/encoding.h new file mode 100644 index 000000000..bcc04f8a9 --- /dev/null +++ b/source/external/libcbor/cbor/encoding.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_ENCODING_H +#define LIBCBOR_ENCODING_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * All cbor_encode_* methods take 2 or 3 arguments: + * - a logical `value` to encode (except for trivial items such as NULLs) + * - an output `buffer` pointer + * - a `buffer_size` specification + * + * They serialize the `value` into one or more bytes and write the bytes to the + * output `buffer` and return either the number of bytes written, or 0 if the + * `buffer_size` was too small to small to fit the serialized value (in which + * case it is not modified). + */ + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint8(uint8_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint16(uint16_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint32(uint32_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint64(uint64_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint(uint64_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint8(uint8_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint16(uint16_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint32(uint32_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint64(uint64_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_encode_indef_bytestring_start(unsigned char *, size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_string_start(size_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_encode_indef_string_start(unsigned char *, size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_array_start(size_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_encode_indef_array_start(unsigned char *, size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_map_start(size_t, + unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_null(unsigned char *, size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_undef(unsigned char *, size_t); + +/** Encodes a half-precision float + * + * Since there is no native representation or semantics for half floats + * in the language, we use single-precision floats, as every value that + * can be expressed as a half-float can also be expressed as a float. + * + * This however means that not all floats passed to this function can be + * unambiguously encoded. The behavior is as follows: + * - Infinity, NaN are preserved + * - Zero is preserved + * - Denormalized numbers keep their sign bit and 10 most significant bit of + * the significand + * - All other numbers + * - If the logical value of the exponent is < -24, the output is zero + * - If the logical value of the exponent is between -23 and -14, the output + * is cut off to represent the 'magnitude' of the input, by which we + * mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is + * lost. + * - In all other cases, the sign bit, the exponent, and 10 most significant + * bits of the significand are kept + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_half(float, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_single(float, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_double(double, unsigned char *, + size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_break(unsigned char *, size_t); + +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char *, + size_t); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_ENCODING_H diff --git a/source/external/libcbor/cbor/floats_ctrls.c b/source/external/libcbor/cbor/floats_ctrls.c new file mode 100644 index 000000000..57bf477d4 --- /dev/null +++ b/source/external/libcbor/cbor/floats_ctrls.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "floats_ctrls.h" +#include +#include "assert.h" + +cbor_float_width cbor_float_get_width(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_float_ctrl(item)); + return item->metadata.float_ctrl_metadata.width; +} + +uint8_t cbor_ctrl_value(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_float_ctrl(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0); + return item->metadata.float_ctrl_metadata.ctrl; +} + +bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_float_ctrl(item)); + return cbor_float_get_width(item) == CBOR_FLOAT_0; +} + +float cbor_float_get_float2(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16); + return *(float *)item->data; +} + +float cbor_float_get_float4(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32); + return *(float *)item->data; +} + +double cbor_float_get_float8(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64); + return *(double *)item->data; +} + +double cbor_float_get_float(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_float(item)); + // cppcheck-suppress missingReturn + switch (cbor_float_get_width(item)) { + case CBOR_FLOAT_0: + return NAN; + case CBOR_FLOAT_16: + return cbor_float_get_float2(item); + case CBOR_FLOAT_32: + return cbor_float_get_float4(item); + case CBOR_FLOAT_64: + return cbor_float_get_float8(item); + } +} + +bool cbor_get_bool(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_bool(item)); + return item->metadata.float_ctrl_metadata.ctrl == CBOR_CTRL_TRUE; +} + +void cbor_set_float2(cbor_item_t *item, float value) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16); + *((float *)item->data) = value; +} + +void cbor_set_float4(cbor_item_t *item, float value) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32); + *((float *)item->data) = value; +} + +void cbor_set_float8(cbor_item_t *item, double value) { + CBOR_ASSERT(cbor_is_float(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64); + *((double *)item->data) = value; +} + +void cbor_set_ctrl(cbor_item_t *item, uint8_t value) { + CBOR_ASSERT(cbor_isa_float_ctrl(item)); + CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0); + item->metadata.float_ctrl_metadata.ctrl = value; +} + +void cbor_set_bool(cbor_item_t *item, bool value) { + CBOR_ASSERT(cbor_is_bool(item)); + item->metadata.float_ctrl_metadata.ctrl = + value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE; +} + +cbor_item_t *cbor_new_ctrl(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .type = CBOR_TYPE_FLOAT_CTRL, + .data = NULL, + .refcount = 1, + .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0, + .ctrl = CBOR_CTRL_NONE}}}; + return item; +} + +cbor_item_t *cbor_new_float2(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .type = CBOR_TYPE_FLOAT_CTRL, + .data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}}; + return item; +} + +cbor_item_t *cbor_new_float4(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .type = CBOR_TYPE_FLOAT_CTRL, + .data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}}; + return item; +} + +cbor_item_t *cbor_new_float8(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .type = CBOR_TYPE_FLOAT_CTRL, + .data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}}; + return item; +} + +cbor_item_t *cbor_new_null(void) { + cbor_item_t *item = cbor_new_ctrl(); + _CBOR_NOTNULL(item); + cbor_set_ctrl(item, CBOR_CTRL_NULL); + return item; +} + +cbor_item_t *cbor_new_undef(void) { + cbor_item_t *item = cbor_new_ctrl(); + _CBOR_NOTNULL(item); + cbor_set_ctrl(item, CBOR_CTRL_UNDEF); + return item; +} + +cbor_item_t *cbor_build_bool(bool value) { + return cbor_build_ctrl(value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE); +} + +cbor_item_t *cbor_build_float2(float value) { + cbor_item_t *item = cbor_new_float2(); + _CBOR_NOTNULL(item); + cbor_set_float2(item, value); + return item; +} + +cbor_item_t *cbor_build_float4(float value) { + cbor_item_t *item = cbor_new_float4(); + _CBOR_NOTNULL(item); + cbor_set_float4(item, value); + return item; +} + +cbor_item_t *cbor_build_float8(double value) { + cbor_item_t *item = cbor_new_float8(); + _CBOR_NOTNULL(item); + cbor_set_float8(item, value); + return item; +} + +cbor_item_t *cbor_build_ctrl(uint8_t value) { + cbor_item_t *item = cbor_new_ctrl(); + _CBOR_NOTNULL(item); + cbor_set_ctrl(item, value); + return item; +} diff --git a/source/external/libcbor/cbor/floats_ctrls.h b/source/external/libcbor/cbor/floats_ctrls.h new file mode 100644 index 000000000..335eab832 --- /dev/null +++ b/source/external/libcbor/cbor/floats_ctrls.h @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_FLOATS_CTRLS_H +#define LIBCBOR_FLOATS_CTRLS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * Float manipulation + * ============================================================================ + */ + +/** Is this a ctrl value? + * + * @param item A float or ctrl item + * @return Is this a ctrl value? + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_float_ctrl_is_ctrl( + const cbor_item_t *item); + +/** Get the float width + * + * @param item A float or ctrl item + * @return The width. + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_float_width +cbor_float_get_width(const cbor_item_t *item); + +/** Get a half precision float + * + * The item must have the corresponding width + * + * @param item A half precision float + * @return half precision value + */ +_CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float2( + const cbor_item_t *item); + +/** Get a single precision float + * + * The item must have the corresponding width + * + * @param item A single precision float + * @return single precision value + */ +_CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float4( + const cbor_item_t *item); + +/** Get a double precision float + * + * The item must have the corresponding width + * + * @param item A double precision float + * @return double precision value + */ +_CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float8( + const cbor_item_t *item); + +/** Get the float value represented as double + * + * Can be used regardless of the width. + * + * @param item Any float + * @return double precision value + */ +_CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float( + const cbor_item_t *item); + +/** Get value from a boolean ctrl item + * + * @param item A ctrl item + * @return boolean value + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item); + +/** Constructs a new ctrl item + * + * The width cannot be changed once the item is created + * + * @return Reference to the new ctrl item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_ctrl(void); + +/** Constructs a new float item + * + * The width cannot be changed once the item is created + * + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float2(void); + +/** Constructs a new float item + * + * The width cannot be changed once the item is created + * + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float4(void); + +/** Constructs a new float item + * + * The width cannot be changed once the item is created + * + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float8(void); + +/** Constructs new null ctrl item + * + * @return Reference to the new null item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_null(void); + +/** Constructs new undef ctrl item + * + * @return Reference to the new undef item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_undef(void); + +/** Constructs new boolean ctrl item + * + * @param value The value to use + * @return Reference to the new boolean item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value); + +/** Assign a control value + * + * \rst + * .. warning:: It is possible to produce an invalid CBOR value by assigning a + * invalid value using this mechanism. Please consult the standard before use. + * \endrst + * + * @param item A ctrl item + * @param value The simple value to assign. Please consult the standard for + * allowed values + */ +CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value); + +/** Assign a boolean value to a boolean ctrl item + * + * @param item A ctrl item + * @param value The simple value to assign. + */ +CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value); + +/** Assigns a float value + * + * @param item A half precision float + * @param value The value to assign + */ +CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value); + +/** Assigns a float value + * + * @param item A single precision float + * @param value The value to assign + */ +CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value); + +/** Assigns a float value + * + * @param item A double precision float + * @param value The value to assign + */ +CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value); + +/** Reads the control value + * + * @param item A ctrl item + * @return the simple value + */ +_CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item); + +/** Constructs a new float + * + * @param value the value to use + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float2(float value); + +/** Constructs a new float + * + * @param value the value to use + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float4(float value); + +/** Constructs a new float + * + * @param value the value to use + * @return Reference to the new float item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float8(double value); + +/** Constructs a ctrl item + * + * @param value the value to use + * @return Reference to the new ctrl item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_FLOATS_CTRLS_H diff --git a/source/external/libcbor/cbor/internal/builder_callbacks.c b/source/external/libcbor/cbor/internal/builder_callbacks.c new file mode 100644 index 000000000..257cef3ad --- /dev/null +++ b/source/external/libcbor/cbor/internal/builder_callbacks.c @@ -0,0 +1,422 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "builder_callbacks.h" + +#include + +#include "../arrays.h" +#include "../bytestrings.h" +#include "../common.h" +#include "../floats_ctrls.h" +#include "../ints.h" +#include "../maps.h" +#include "../strings.h" +#include "../tags.h" +#include "unicode.h" + +// `_cbor_builder_append` takes ownership of `item`. If adding the item to +// parent container fails, `item` will be deallocated to prevent memory. +void _cbor_builder_append(cbor_item_t *item, + struct _cbor_decoder_context *ctx) { + if (ctx->stack->size == 0) { + /* Top level item */ + ctx->root = item; + return; + } + /* Part of a bigger structure */ + switch (ctx->stack->top->item->type) { + // Handle Arrays and Maps since they can contain subitems of any type. + // Byte/string construction from chunks is handled in the respective chunk + // handlers. + case CBOR_TYPE_ARRAY: { + if (cbor_array_is_definite(ctx->stack->top->item)) { + // We don't need an explicit check for whether the item still belongs + // into this array because if there are extra items, they will cause a + // syntax error when decoded. + CBOR_ASSERT(ctx->stack->top->subitems > 0); + // This should never happen since the definite array should be + // preallocated for the expected number of items. + if (!cbor_array_push(ctx->stack->top->item, item)) { + ctx->creation_failed = true; + cbor_decref(&item); + break; + } + cbor_decref(&item); + ctx->stack->top->subitems--; + if (ctx->stack->top->subitems == 0) { + cbor_item_t *stack_item = ctx->stack->top->item; + _cbor_stack_pop(ctx->stack); + _cbor_builder_append(stack_item, ctx); + } + } else { + /* Indefinite array, don't bother with subitems */ + if (!cbor_array_push(ctx->stack->top->item, item)) { + ctx->creation_failed = true; + } + cbor_decref(&item); + } + break; + } + case CBOR_TYPE_MAP: { + // Handle both definite and indefinite maps the same initially. + // Note: We use 0 and 1 subitems to distinguish between keys and values in + // indefinite items + if (ctx->stack->top->subitems % 2) { + // Odd record, this is a value. + ctx->creation_failed = + !_cbor_map_add_value(ctx->stack->top->item, item); + // Adding a value never fails since the memory is allocated when the + // key is added + CBOR_ASSERT(!ctx->creation_failed); + } else { + // Even record, this is a key. + if (!_cbor_map_add_key(ctx->stack->top->item, item)) { + ctx->creation_failed = true; + cbor_decref(&item); + break; + } + } + cbor_decref(&item); + if (cbor_map_is_definite(ctx->stack->top->item)) { + CBOR_ASSERT(ctx->stack->top->subitems > 0); + ctx->stack->top->subitems--; + if (ctx->stack->top->subitems == 0) { + cbor_item_t *map_entry = ctx->stack->top->item; + _cbor_stack_pop(ctx->stack); + _cbor_builder_append(map_entry, ctx); + } + } else { + ctx->stack->top->subitems ^= + 1; /* Flip the indicator for indefinite items */ + } + break; + } + case CBOR_TYPE_TAG: { + CBOR_ASSERT(ctx->stack->top->subitems == 1); + cbor_tag_set_item(ctx->stack->top->item, item); + cbor_decref(&item); /* Give up on our reference */ + cbor_item_t *tagged_item = ctx->stack->top->item; + _cbor_stack_pop(ctx->stack); + _cbor_builder_append(tagged_item, ctx); + break; + } + // We have an item to append but nothing to append it to. + default: { + cbor_decref(&item); + ctx->syntax_error = true; + } + } +} + +#define CHECK_RES(ctx, res) \ + do { \ + if (res == NULL) { \ + ctx->creation_failed = true; \ + return; \ + } \ + } while (0) + +// Check that the length fits into size_t. If not, we cannot possibly allocate +// the required memory and should fail fast. +#define CHECK_LENGTH(ctx, length) \ + do { \ + if (length > SIZE_MAX) { \ + ctx->creation_failed = true; \ + return; \ + } \ + } while (0) + +#define PUSH_CTX_STACK(ctx, res, subitems) \ + do { \ + if (_cbor_stack_push(ctx->stack, res, subitems) == NULL) { \ + cbor_decref(&res); \ + ctx->creation_failed = true; \ + } \ + } while (0) + +void cbor_builder_uint8_callback(void *context, uint8_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int8(); + CHECK_RES(ctx, res); + cbor_mark_uint(res); + cbor_set_uint8(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_uint16_callback(void *context, uint16_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int16(); + CHECK_RES(ctx, res); + cbor_mark_uint(res); + cbor_set_uint16(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_uint32_callback(void *context, uint32_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int32(); + CHECK_RES(ctx, res); + cbor_mark_uint(res); + cbor_set_uint32(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_uint64_callback(void *context, uint64_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int64(); + CHECK_RES(ctx, res); + cbor_mark_uint(res); + cbor_set_uint64(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_negint8_callback(void *context, uint8_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int8(); + CHECK_RES(ctx, res); + cbor_mark_negint(res); + cbor_set_uint8(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_negint16_callback(void *context, uint16_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int16(); + CHECK_RES(ctx, res); + cbor_mark_negint(res); + cbor_set_uint16(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_negint32_callback(void *context, uint32_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int32(); + CHECK_RES(ctx, res); + cbor_mark_negint(res); + cbor_set_uint32(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_negint64_callback(void *context, uint64_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_int64(); + CHECK_RES(ctx, res); + cbor_mark_negint(res); + cbor_set_uint64(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_byte_string_callback(void *context, cbor_data data, + uint64_t length) { + struct _cbor_decoder_context *ctx = context; + CHECK_LENGTH(ctx, length); + unsigned char *new_handle = _cbor_malloc(length); + if (new_handle == NULL) { + ctx->creation_failed = true; + return; + } + + memcpy(new_handle, data, length); + cbor_item_t *new_chunk = cbor_new_definite_bytestring(); + + if (new_chunk == NULL) { + _cbor_free(new_handle); + ctx->creation_failed = true; + return; + } + + cbor_bytestring_set_handle(new_chunk, new_handle, length); + + // If an indef bytestring is on the stack, extend it (if it were closed, it + // would have been popped). Handle any syntax errors upstream. + if (ctx->stack->size > 0 && cbor_isa_bytestring(ctx->stack->top->item) && + cbor_bytestring_is_indefinite(ctx->stack->top->item)) { + if (!cbor_bytestring_add_chunk(ctx->stack->top->item, new_chunk)) { + ctx->creation_failed = true; + } + cbor_decref(&new_chunk); + } else { + _cbor_builder_append(new_chunk, ctx); + } +} + +void cbor_builder_byte_string_start_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_indefinite_bytestring(); + CHECK_RES(ctx, res); + PUSH_CTX_STACK(ctx, res, 0); +} + +void cbor_builder_string_callback(void *context, cbor_data data, + uint64_t length) { + struct _cbor_decoder_context *ctx = context; + CHECK_LENGTH(ctx, length); + + unsigned char *new_handle = _cbor_malloc(length); + if (new_handle == NULL) { + ctx->creation_failed = true; + return; + } + + memcpy(new_handle, data, length); + cbor_item_t *new_chunk = cbor_new_definite_string(); + if (new_chunk == NULL) { + _cbor_free(new_handle); + ctx->creation_failed = true; + return; + } + cbor_string_set_handle(new_chunk, new_handle, length); + + // If an indef string is on the stack, extend it (if it were closed, it would + // have been popped). Handle any syntax errors upstream. + if (ctx->stack->size > 0 && cbor_isa_string(ctx->stack->top->item) && + cbor_string_is_indefinite(ctx->stack->top->item)) { + if (!cbor_string_add_chunk(ctx->stack->top->item, new_chunk)) { + ctx->creation_failed = true; + } + cbor_decref(&new_chunk); + } else { + _cbor_builder_append(new_chunk, ctx); + } +} + +void cbor_builder_string_start_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_indefinite_string(); + CHECK_RES(ctx, res); + PUSH_CTX_STACK(ctx, res, 0); +} + +void cbor_builder_array_start_callback(void *context, uint64_t size) { + struct _cbor_decoder_context *ctx = context; + CHECK_LENGTH(ctx, size); + cbor_item_t *res = cbor_new_definite_array(size); + CHECK_RES(ctx, res); + if (size > 0) { + PUSH_CTX_STACK(ctx, res, size); + } else { + _cbor_builder_append(res, ctx); + } +} + +void cbor_builder_indef_array_start_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_indefinite_array(); + CHECK_RES(ctx, res); + PUSH_CTX_STACK(ctx, res, 0); +} + +void cbor_builder_indef_map_start_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_indefinite_map(); + CHECK_RES(ctx, res); + PUSH_CTX_STACK(ctx, res, 0); +} + +void cbor_builder_map_start_callback(void *context, uint64_t size) { + struct _cbor_decoder_context *ctx = context; + CHECK_LENGTH(ctx, size); + cbor_item_t *res = cbor_new_definite_map(size); + CHECK_RES(ctx, res); + if (size > 0) { + PUSH_CTX_STACK(ctx, res, size * 2); + } else { + _cbor_builder_append(res, ctx); + } +} + +/** + * Is the (partially constructed) item indefinite? + */ +bool _cbor_is_indefinite(cbor_item_t *item) { + switch (item->type) { + case CBOR_TYPE_BYTESTRING: + return cbor_bytestring_is_indefinite(item); + case CBOR_TYPE_STRING: + return cbor_string_is_indefinite(item); + case CBOR_TYPE_ARRAY: + return cbor_array_is_indefinite(item); + case CBOR_TYPE_MAP: + return cbor_map_is_indefinite(item); + default: + // Should never happen since a non-nested item cannot be on top of the + // stack. + return false; + } +} + +void cbor_builder_indef_break_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + /* There must be an item to break out of*/ + if (ctx->stack->size > 0) { + cbor_item_t *item = ctx->stack->top->item; + if (_cbor_is_indefinite( + item) && /* Only indefinite items can be terminated by 0xFF */ + /* Special case: we cannot append up if an indefinite map is incomplete + (we are expecting a value). */ + (item->type != CBOR_TYPE_MAP || ctx->stack->top->subitems % 2 == 0)) { + _cbor_stack_pop(ctx->stack); + _cbor_builder_append(item, ctx); + return; + } + } + + ctx->syntax_error = true; +} + +void cbor_builder_float2_callback(void *context, float value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_float2(); + CHECK_RES(ctx, res); + cbor_set_float2(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_float4_callback(void *context, float value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_float4(); + CHECK_RES(ctx, res); + cbor_set_float4(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_float8_callback(void *context, double value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_float8(); + CHECK_RES(ctx, res); + cbor_set_float8(res, value); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_null_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_null(); + CHECK_RES(ctx, res); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_undefined_callback(void *context) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_undef(); + CHECK_RES(ctx, res); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_boolean_callback(void *context, bool value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_build_bool(value); + CHECK_RES(ctx, res); + _cbor_builder_append(res, ctx); +} + +void cbor_builder_tag_callback(void *context, uint64_t value) { + struct _cbor_decoder_context *ctx = context; + cbor_item_t *res = cbor_new_tag(value); + CHECK_RES(ctx, res); + PUSH_CTX_STACK(ctx, res, 1); +} diff --git a/source/external/libcbor/cbor/internal/builder_callbacks.h b/source/external/libcbor/cbor/internal/builder_callbacks.h new file mode 100644 index 000000000..7893960e4 --- /dev/null +++ b/source/external/libcbor/cbor/internal/builder_callbacks.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_BUILDER_CALLBACKS_H +#define LIBCBOR_BUILDER_CALLBACKS_H + +#include "../callbacks.h" +#include "cbor/common.h" +#include "stack.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** High-level decoding context */ +struct _cbor_decoder_context { + /** Callback creating the last item has failed */ + bool creation_failed; + /** Stack expectation mismatch */ + bool syntax_error; + cbor_item_t *root; + struct _cbor_stack *stack; +}; + +/** Internal helper: Append item to the top of the stack while handling errors. + */ +void _cbor_builder_append(cbor_item_t *item, struct _cbor_decoder_context *ctx); + +void cbor_builder_uint8_callback(void *, uint8_t); + +void cbor_builder_uint16_callback(void *, uint16_t); + +void cbor_builder_uint32_callback(void *, uint32_t); + +void cbor_builder_uint64_callback(void *, uint64_t); + +void cbor_builder_negint8_callback(void *, uint8_t); + +void cbor_builder_negint16_callback(void *, uint16_t); + +void cbor_builder_negint32_callback(void *, uint32_t); + +void cbor_builder_negint64_callback(void *, uint64_t); + +void cbor_builder_string_callback(void *, cbor_data, uint64_t); + +void cbor_builder_string_start_callback(void *); + +void cbor_builder_byte_string_callback(void *, cbor_data, uint64_t); + +void cbor_builder_byte_string_start_callback(void *); + +void cbor_builder_array_start_callback(void *, uint64_t); + +void cbor_builder_indef_array_start_callback(void *); + +void cbor_builder_map_start_callback(void *, uint64_t); + +void cbor_builder_indef_map_start_callback(void *); + +void cbor_builder_tag_callback(void *, uint64_t); + +void cbor_builder_float2_callback(void *, float); + +void cbor_builder_float4_callback(void *, float); + +void cbor_builder_float8_callback(void *, double); + +void cbor_builder_null_callback(void *); + +void cbor_builder_undefined_callback(void *); + +void cbor_builder_boolean_callback(void *, bool); + +void cbor_builder_indef_break_callback(void *); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_BUILDER_CALLBACKS_H diff --git a/source/external/libcbor/cbor/internal/encoders.c b/source/external/libcbor/cbor/internal/encoders.c new file mode 100644 index 000000000..49d4d7f33 --- /dev/null +++ b/source/external/libcbor/cbor/internal/encoders.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "encoders.h" +#include + +size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset) { + if (value <= 23) { + if (buffer_size >= 1) { + buffer[0] = value + offset; + return 1; + } + } else { + if (buffer_size >= 2) { + buffer[0] = 0x18 + offset; + buffer[1] = value; + return 2; + } + } + return 0; +} + +size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset) { + if (buffer_size >= 3) { + buffer[0] = 0x19 + offset; + +#ifdef IS_BIG_ENDIAN + memcpy(buffer + 1, &value, 2); +#else + buffer[1] = (unsigned char)(value >> 8); + buffer[2] = (unsigned char)value; +#endif + + return 3; + } else + return 0; +} + +size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset) { + if (buffer_size >= 5) { + buffer[0] = 0x1A + offset; + +#ifdef IS_BIG_ENDIAN + memcpy(buffer + 1, &value, 4); +#else + buffer[1] = (unsigned char)(value >> 24); + buffer[2] = (unsigned char)(value >> 16); + buffer[3] = (unsigned char)(value >> 8); + buffer[4] = (unsigned char)value; +#endif + + return 5; + } else + return 0; +} + +size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset) { + if (buffer_size >= 9) { + buffer[0] = 0x1B + offset; + +#ifdef IS_BIG_ENDIAN + memcpy(buffer + 1, &value, 8); +#else + buffer[1] = (unsigned char)(value >> 56); + buffer[2] = (unsigned char)(value >> 48); + buffer[3] = (unsigned char)(value >> 40); + buffer[4] = (unsigned char)(value >> 32); + buffer[5] = (unsigned char)(value >> 24); + buffer[6] = (unsigned char)(value >> 16); + buffer[7] = (unsigned char)(value >> 8); + buffer[8] = (unsigned char)value; +#endif + + return 9; + } else + return 0; +} + +size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset) { + if (value <= UINT16_MAX) + if (value <= UINT8_MAX) + return _cbor_encode_uint8((uint8_t)value, buffer, buffer_size, offset); + else + return _cbor_encode_uint16((uint16_t)value, buffer, buffer_size, offset); + else if (value <= UINT32_MAX) + return _cbor_encode_uint32((uint32_t)value, buffer, buffer_size, offset); + else + return _cbor_encode_uint64((uint64_t)value, buffer, buffer_size, offset); +} diff --git a/source/external/libcbor/cbor/internal/encoders.h b/source/external/libcbor/cbor/internal/encoders.h new file mode 100644 index 000000000..7eadb7121 --- /dev/null +++ b/source/external/libcbor/cbor/internal/encoders.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_ENCODERS_H +#define LIBCBOR_ENCODERS_H + +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +_CBOR_NODISCARD +size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset); + +_CBOR_NODISCARD +size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset); + +_CBOR_NODISCARD +size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset); + +_CBOR_NODISCARD +size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset); + +_CBOR_NODISCARD +size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer, + size_t buffer_size, uint8_t offset); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_ENCODERS_H diff --git a/source/external/libcbor/cbor/internal/loaders.c b/source/external/libcbor/cbor/internal/loaders.c new file mode 100644 index 000000000..cfa173de7 --- /dev/null +++ b/source/external/libcbor/cbor/internal/loaders.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "loaders.h" +#include +#include + +uint8_t _cbor_load_uint8(cbor_data source) { return (uint8_t)*source; } + +uint16_t _cbor_load_uint16(const unsigned char *source) { +#ifdef IS_BIG_ENDIAN + uint16_t result; + memcpy(&result, source, 2); + return result; +#else + return ((uint16_t) * (source + 0) << 8) + (uint8_t) * (source + 1); +#endif +} + +uint32_t _cbor_load_uint32(const unsigned char *source) { +#ifdef IS_BIG_ENDIAN + uint32_t result; + memcpy(&result, source, 4); + return result; +#else + return ((uint32_t) * (source + 0) << 0x18) + + ((uint32_t) * (source + 1) << 0x10) + + ((uint16_t) * (source + 2) << 0x08) + (uint8_t) * (source + 3); +#endif +} + +uint64_t _cbor_load_uint64(const unsigned char *source) { +#ifdef IS_BIG_ENDIAN + uint64_t result; + memcpy(&result, source, 8); + return result; +#else + return ((uint64_t) * (source + 0) << 0x38) + + ((uint64_t) * (source + 1) << 0x30) + + ((uint64_t) * (source + 2) << 0x28) + + ((uint64_t) * (source + 3) << 0x20) + + ((uint32_t) * (source + 4) << 0x18) + + ((uint32_t) * (source + 5) << 0x10) + + ((uint16_t) * (source + 6) << 0x08) + (uint8_t) * (source + 7); +#endif +} + +/* As per https://www.rfc-editor.org/rfc/rfc8949.html#name-half-precision */ +float _cbor_decode_half(unsigned char *halfp) { + int half = (halfp[0] << 8) + halfp[1]; + int exp = (half >> 10) & 0x1f; + int mant = half & 0x3ff; + double val; + if (exp == 0) + val = ldexp(mant, -24); + else if (exp != 31) + val = ldexp(mant + 1024, exp - 25); + else + val = mant == 0 ? INFINITY : NAN; + return (float)(half & 0x8000 ? -val : val); +} + +float _cbor_load_half(cbor_data source) { + /* Discard const */ + return _cbor_decode_half((unsigned char *)source); +} + +float _cbor_load_float(cbor_data source) { + union _cbor_float_helper helper = {.as_uint = _cbor_load_uint32(source)}; + return helper.as_float; +} + +double _cbor_load_double(cbor_data source) { + union _cbor_double_helper helper = {.as_uint = _cbor_load_uint64(source)}; + return helper.as_double; +} diff --git a/source/external/libcbor/cbor/internal/loaders.h b/source/external/libcbor/cbor/internal/loaders.h new file mode 100644 index 000000000..ce37563a3 --- /dev/null +++ b/source/external/libcbor/cbor/internal/loaders.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_LOADERS_H +#define LIBCBOR_LOADERS_H + +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Read the given uint from the given location, no questions asked */ +_CBOR_NODISCARD +uint8_t _cbor_load_uint8(const unsigned char *source); + +_CBOR_NODISCARD +uint16_t _cbor_load_uint16(const unsigned char *source); + +_CBOR_NODISCARD +uint32_t _cbor_load_uint32(const unsigned char *source); + +_CBOR_NODISCARD +uint64_t _cbor_load_uint64(const unsigned char *source); + +_CBOR_NODISCARD +float _cbor_load_half(cbor_data source); + +_CBOR_NODISCARD +float _cbor_load_float(cbor_data source); + +_CBOR_NODISCARD +double _cbor_load_double(cbor_data source); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_LOADERS_H diff --git a/source/external/libcbor/cbor/internal/memory_utils.c b/source/external/libcbor/cbor/internal/memory_utils.c new file mode 100644 index 000000000..bbea63cb9 --- /dev/null +++ b/source/external/libcbor/cbor/internal/memory_utils.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "memory_utils.h" +#include "cbor/common.h" + +// TODO: Consider builtins +// (https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html) + +/** Highest on bit position */ +size_t _cbor_highest_bit(size_t number) { + size_t bit = 0; + while (number != 0) { + bit++; + number >>= 1; + } + + return bit; +} + +bool _cbor_safe_to_multiply(size_t a, size_t b) { + if (a <= 1 || b <= 1) return true; + return _cbor_highest_bit(a) + _cbor_highest_bit(b) <= sizeof(size_t) * 8; +} + +bool _cbor_safe_to_add(size_t a, size_t b) { + // Unsigned integer overflow doesn't constitute UB + size_t sum = a + b; + return sum >= a && sum >= b; +} + +size_t _cbor_safe_signaling_add(size_t a, size_t b) { + if (a == 0 || b == 0) return 0; + if (_cbor_safe_to_add(a, b)) return a + b; + return 0; +} + +void* _cbor_alloc_multiple(size_t item_size, size_t item_count) { + if (_cbor_safe_to_multiply(item_size, item_count)) { + return _cbor_malloc(item_size * item_count); + } else { + return NULL; + } +} + +void* _cbor_realloc_multiple(void* pointer, size_t item_size, + size_t item_count) { + if (_cbor_safe_to_multiply(item_size, item_count)) { + return _cbor_realloc(pointer, item_size * item_count); + } else { + return NULL; + } +} diff --git a/source/external/libcbor/cbor/internal/memory_utils.h b/source/external/libcbor/cbor/internal/memory_utils.h new file mode 100644 index 000000000..696f67800 --- /dev/null +++ b/source/external/libcbor/cbor/internal/memory_utils.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_MEMORY_UTILS_H +#define LIBCBOR_MEMORY_UTILS_H + +#include +#include + +#include "cbor/common.h" + +/** Can `a` and `b` be multiplied without overflowing size_t? */ +_CBOR_NODISCARD +bool _cbor_safe_to_multiply(size_t a, size_t b); + +/** Can `a` and `b` be added without overflowing size_t? */ +_CBOR_NODISCARD +bool _cbor_safe_to_add(size_t a, size_t b); + +/** Adds `a` and `b`, propagating zeros and returning 0 on overflow. */ +_CBOR_NODISCARD +size_t _cbor_safe_signaling_add(size_t a, size_t b); + +/** Overflow-proof contiguous array allocation + * + * @param item_size + * @param item_count + * @return Region of item_size * item_count bytes, or NULL if the total size + * overflows size_t or the underlying allocator failed + */ +void* _cbor_alloc_multiple(size_t item_size, size_t item_count); + +/** Overflow-proof contiguous array reallocation + * + * This implements the OpenBSD `reallocarray` functionality. + * + * @param pointer + * @param item_size + * @param item_count + * @return Realloc'd of item_size * item_count bytes, or NULL if the total size + * overflows size_t or the underlying allocator failed + */ +void* _cbor_realloc_multiple(void* pointer, size_t item_size, + size_t item_count); + +#endif // LIBCBOR_MEMORY_UTILS_H diff --git a/source/external/libcbor/cbor/internal/stack.c b/source/external/libcbor/cbor/internal/stack.c new file mode 100644 index 000000000..2db03cbbf --- /dev/null +++ b/source/external/libcbor/cbor/internal/stack.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "stack.h" + +struct _cbor_stack _cbor_stack_init(void) { + return (struct _cbor_stack){.top = NULL, .size = 0}; +} + +void _cbor_stack_pop(struct _cbor_stack *stack) { + struct _cbor_stack_record *top = stack->top; + stack->top = stack->top->lower; + _cbor_free(top); + stack->size--; +} + +struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *stack, + cbor_item_t *item, + size_t subitems) { + if (stack->size == CBOR_MAX_STACK_SIZE) return NULL; + struct _cbor_stack_record *new_top = + _cbor_malloc(sizeof(struct _cbor_stack_record)); + if (new_top == NULL) return NULL; + + *new_top = (struct _cbor_stack_record){stack->top, item, subitems}; + stack->top = new_top; + stack->size++; + return new_top; +} diff --git a/source/external/libcbor/cbor/internal/stack.h b/source/external/libcbor/cbor/internal/stack.h new file mode 100644 index 000000000..cf2206b40 --- /dev/null +++ b/source/external/libcbor/cbor/internal/stack.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_STACK_H +#define LIBCBOR_STACK_H + +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Simple stack record for the parser */ +struct _cbor_stack_record { + /** Pointer to the parent stack frame */ + struct _cbor_stack_record *lower; + /** Item under construction */ + cbor_item_t *item; + /** + * How many outstanding subitems are expected. + * + * For example, when we see a new definite array, `subitems` is initialized to + * the array length. With every item added, the counter is decreased. When it + * reaches zero, the stack is popped and the complete item is propagated + * upwards. + */ + size_t subitems; +}; + +/** Stack handle - contents and size */ +struct _cbor_stack { + struct _cbor_stack_record *top; + size_t size; +}; + +_CBOR_NODISCARD +struct _cbor_stack _cbor_stack_init(void); + +void _cbor_stack_pop(struct _cbor_stack *); + +_CBOR_NODISCARD +struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *, cbor_item_t *, + size_t); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_STACK_H diff --git a/source/external/libcbor/cbor/internal/unicode.c b/source/external/libcbor/cbor/internal/unicode.c new file mode 100644 index 000000000..f87b746a3 --- /dev/null +++ b/source/external/libcbor/cbor/internal/unicode.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "unicode.h" +#include + +#define UTF8_ACCEPT 0 +#define UTF8_REJECT 1 + +static const uint8_t utf8d[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00..1f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20..3f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40..5f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60..7f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, /* 80..9f */ + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* a0..bf */ + 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* c0..df */ + 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, + 0x3, 0x3, 0x4, 0x3, 0x3, /* e0..ef */ + 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, + 0x8, 0x8, 0x8, 0x8, 0x8, /* f0..ff */ + 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, + 0x6, 0x1, 0x1, 0x1, 0x1, /* s0..s0 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, /* s1..s2 */ + 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, /* s3..s4 */ + 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, /* s5..s6 */ + 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, + 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* s7..s8 */ +}; + +/* Copyright of this function: (c) 2008-2009 Bjoern Hoehrmann + * */ +/* See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. */ +uint32_t _cbor_unicode_decode(uint32_t* state, uint32_t* codep, uint32_t byte) { + uint32_t type = utf8d[byte]; + + *codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6) + : (0xff >> type) & (byte); + + *state = utf8d[256 + *state * 16 + type]; + return *state; +} + +size_t _cbor_unicode_codepoint_count(cbor_data source, size_t source_length, + struct _cbor_unicode_status* status) { + *status = + (struct _cbor_unicode_status){.location = 0, .status = _CBOR_UNICODE_OK}; + uint32_t codepoint, state = UTF8_ACCEPT, res; + size_t pos = 0, count = 0; + + for (; pos < source_length; pos++) { + res = _cbor_unicode_decode(&state, &codepoint, source[pos]); + + if (res == UTF8_ACCEPT) { + count++; + } else if (res == UTF8_REJECT) { + goto error; + } + } + + /* Unfinished multibyte codepoint */ + if (state != UTF8_ACCEPT) goto error; + + return count; + +error: + *status = (struct _cbor_unicode_status){.location = pos, + .status = _CBOR_UNICODE_BADCP}; + return 0; +} diff --git a/source/external/libcbor/cbor/internal/unicode.h b/source/external/libcbor/cbor/internal/unicode.h new file mode 100644 index 000000000..81d03d007 --- /dev/null +++ b/source/external/libcbor/cbor/internal/unicode.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_UNICODE_H +#define LIBCBOR_UNICODE_H + +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum _cbor_unicode_status_error { _CBOR_UNICODE_OK, _CBOR_UNICODE_BADCP }; + +/** Signals unicode validation error and possibly its location */ +struct _cbor_unicode_status { + enum _cbor_unicode_status_error status; + size_t location; +}; + +_CBOR_NODISCARD +size_t _cbor_unicode_codepoint_count(cbor_data source, size_t source_length, + struct _cbor_unicode_status* status); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_UNICODE_H diff --git a/source/external/libcbor/cbor/ints.c b/source/external/libcbor/cbor/ints.c new file mode 100644 index 000000000..b4d035a18 --- /dev/null +++ b/source/external/libcbor/cbor/ints.c @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "ints.h" + +cbor_int_width cbor_int_get_width(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + return item->metadata.int_metadata.width; +} + +uint8_t cbor_get_uint8(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); + return *item->data; +} + +uint16_t cbor_get_uint16(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); + return *(uint16_t *)item->data; +} + +uint32_t cbor_get_uint32(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); + return *(uint32_t *)item->data; +} + +uint64_t cbor_get_uint64(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); + return *(uint64_t *)item->data; +} + +uint64_t cbor_get_int(const cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + // cppcheck-suppress missingReturn + switch (cbor_int_get_width(item)) { + case CBOR_INT_8: + return cbor_get_uint8(item); + case CBOR_INT_16: + return cbor_get_uint16(item); + case CBOR_INT_32: + return cbor_get_uint32(item); + case CBOR_INT_64: + return cbor_get_uint64(item); + } +} + +void cbor_set_uint8(cbor_item_t *item, uint8_t value) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); + *item->data = value; +} + +void cbor_set_uint16(cbor_item_t *item, uint16_t value) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); + *(uint16_t *)item->data = value; +} + +void cbor_set_uint32(cbor_item_t *item, uint32_t value) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); + *(uint32_t *)item->data = value; +} + +void cbor_set_uint64(cbor_item_t *item, uint64_t value) { + CBOR_ASSERT(cbor_is_int(item)); + CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); + *(uint64_t *)item->data = value; +} + +void cbor_mark_uint(cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + item->type = CBOR_TYPE_UINT; +} + +void cbor_mark_negint(cbor_item_t *item) { + CBOR_ASSERT(cbor_is_int(item)); + item->type = CBOR_TYPE_NEGINT; +} + +cbor_item_t *cbor_new_int8(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 1); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.int_metadata = {.width = CBOR_INT_8}}, + .type = CBOR_TYPE_UINT}; + return item; +} + +cbor_item_t *cbor_new_int16(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 2); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.int_metadata = {.width = CBOR_INT_16}}, + .type = CBOR_TYPE_UINT}; + return item; +} + +cbor_item_t *cbor_new_int32(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.int_metadata = {.width = CBOR_INT_32}}, + .type = CBOR_TYPE_UINT}; + return item; +} + +cbor_item_t *cbor_new_int64(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), + .refcount = 1, + .metadata = {.int_metadata = {.width = CBOR_INT_64}}, + .type = CBOR_TYPE_UINT}; + return item; +} + +cbor_item_t *cbor_build_uint8(uint8_t value) { + cbor_item_t *item = cbor_new_int8(); + _CBOR_NOTNULL(item); + cbor_set_uint8(item, value); + cbor_mark_uint(item); + return item; +} + +cbor_item_t *cbor_build_uint16(uint16_t value) { + cbor_item_t *item = cbor_new_int16(); + _CBOR_NOTNULL(item); + cbor_set_uint16(item, value); + cbor_mark_uint(item); + return item; +} + +cbor_item_t *cbor_build_uint32(uint32_t value) { + cbor_item_t *item = cbor_new_int32(); + _CBOR_NOTNULL(item); + cbor_set_uint32(item, value); + cbor_mark_uint(item); + return item; +} + +cbor_item_t *cbor_build_uint64(uint64_t value) { + cbor_item_t *item = cbor_new_int64(); + _CBOR_NOTNULL(item); + cbor_set_uint64(item, value); + cbor_mark_uint(item); + return item; +} + +cbor_item_t *cbor_build_negint8(uint8_t value) { + cbor_item_t *item = cbor_new_int8(); + _CBOR_NOTNULL(item); + cbor_set_uint8(item, value); + cbor_mark_negint(item); + return item; +} + +cbor_item_t *cbor_build_negint16(uint16_t value) { + cbor_item_t *item = cbor_new_int16(); + _CBOR_NOTNULL(item); + cbor_set_uint16(item, value); + cbor_mark_negint(item); + return item; +} + +cbor_item_t *cbor_build_negint32(uint32_t value) { + cbor_item_t *item = cbor_new_int32(); + _CBOR_NOTNULL(item); + cbor_set_uint32(item, value); + cbor_mark_negint(item); + return item; +} + +cbor_item_t *cbor_build_negint64(uint64_t value) { + cbor_item_t *item = cbor_new_int64(); + _CBOR_NOTNULL(item); + cbor_set_uint64(item, value); + cbor_mark_negint(item); + return item; +} diff --git a/source/external/libcbor/cbor/ints.h b/source/external/libcbor/cbor/ints.h new file mode 100644 index 000000000..006aa428e --- /dev/null +++ b/source/external/libcbor/cbor/ints.h @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_INTS_H +#define LIBCBOR_INTS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * Integer (uints and negints) manipulation + * ============================================================================ + */ + +/** Extracts the integer value + * + * @param item positive or negative integer + * @return the value + */ +_CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item); + +/** Extracts the integer value + * + * @param item positive or negative integer + * @return the value + */ +_CBOR_NODISCARD CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item); + +/** Extracts the integer value + * + * @param item positive or negative integer + * @return the value + */ +_CBOR_NODISCARD CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item); + +/** Extracts the integer value + * + * @param item positive or negative integer + * @return the value + */ +_CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item); + +/** Extracts the integer value + * + * @param item positive or negative integer + * @return the value, extended to `uint64_t` + */ +_CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item); + +/** Assigns the integer value + * + * @param item positive or negative integer item + * @param value the value to assign. For negative integer, the logical value is + * `-value - 1` + */ +CBOR_EXPORT void cbor_set_uint8(cbor_item_t *item, uint8_t value); + +/** Assigns the integer value + * + * @param item positive or negative integer item + * @param value the value to assign. For negative integer, the logical value is + * `-value - 1` + */ +CBOR_EXPORT void cbor_set_uint16(cbor_item_t *item, uint16_t value); + +/** Assigns the integer value + * + * @param item positive or negative integer item + * @param value the value to assign. For negative integer, the logical value is + * `-value - 1` + */ +CBOR_EXPORT void cbor_set_uint32(cbor_item_t *item, uint32_t value); + +/** Assigns the integer value + * + * @param item positive or negative integer item + * @param value the value to assign. For negative integer, the logical value is + * `-value - 1` + */ +CBOR_EXPORT void cbor_set_uint64(cbor_item_t *item, uint64_t value); + +/** Queries the integer width + * + * @param item positive or negative integer item + * @return the width + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_int_width +cbor_int_get_width(const cbor_item_t *item); + +/** Marks the integer item as a positive integer + * + * The data value is not changed + * + * @param item positive or negative integer item + */ +CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item); + +/** Marks the integer item as a negative integer + * + * The data value is not changed + * + * @param item positive or negative integer item + */ +CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item); + +/** Allocates new integer with 1B width + * + * The width cannot be changed once allocated + * + * @return **new** positive integer or `NULL` on memory allocation failure. The + * value is not initialized + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int8(void); + +/** Allocates new integer with 2B width + * + * The width cannot be changed once allocated + * + * @return **new** positive integer or `NULL` on memory allocation failure. The + * value is not initialized + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int16(void); + +/** Allocates new integer with 4B width + * + * The width cannot be changed once allocated + * + * @return **new** positive integer or `NULL` on memory allocation failure. The + * value is not initialized + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int32(void); + +/** Allocates new integer with 8B width + * + * The width cannot be changed once allocated + * + * @return **new** positive integer or `NULL` on memory allocation failure. The + * value is not initialized + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int64(void); + +/** Constructs a new positive integer + * + * @param value the value to use + * @return **new** positive integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value); + +/** Constructs a new positive integer + * + * @param value the value to use + * @return **new** positive integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value); + +/** Constructs a new positive integer + * + * @param value the value to use + * @return **new** positive integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value); + +/** Constructs a new positive integer + * + * @param value the value to use + * @return **new** positive integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value); + +/** Constructs a new negative integer + * + * @param value the value to use + * @return **new** negative integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value); + +/** Constructs a new negative integer + * + * @param value the value to use + * @return **new** negative integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value); + +/** Constructs a new negative integer + * + * @param value the value to use + * @return **new** negative integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value); + +/** Constructs a new negative integer + * + * @param value the value to use + * @return **new** negative integer or `NULL` on memory allocation failure + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_INTS_H diff --git a/source/external/libcbor/cbor/maps.c b/source/external/libcbor/cbor/maps.c new file mode 100644 index 000000000..8a3bd6075 --- /dev/null +++ b/source/external/libcbor/cbor/maps.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "maps.h" +#include "internal/memory_utils.h" + +size_t cbor_map_size(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_map(item)); + return item->metadata.map_metadata.end_ptr; +} + +size_t cbor_map_allocated(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_map(item)); + return item->metadata.map_metadata.allocated; +} + +cbor_item_t *cbor_new_definite_map(size_t size) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_MAP, + .metadata = {.map_metadata = {.allocated = size, + .type = _CBOR_METADATA_DEFINITE, + .end_ptr = 0}}, + .data = _cbor_alloc_multiple(sizeof(struct cbor_pair), size)}; + _CBOR_DEPENDENT_NOTNULL(item, item->data); + + return item; +} + +cbor_item_t *cbor_new_indefinite_map(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_MAP, + .metadata = {.map_metadata = {.allocated = 0, + .type = _CBOR_METADATA_INDEFINITE, + .end_ptr = 0}}, + .data = NULL}; + + return item; +} + +bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key) { + CBOR_ASSERT(cbor_isa_map(item)); + struct _cbor_map_metadata *metadata = + (struct _cbor_map_metadata *)&item->metadata; + if (cbor_map_is_definite(item)) { + struct cbor_pair *data = cbor_map_handle(item); + if (metadata->end_ptr >= metadata->allocated) { + /* Don't realloc definite preallocated map */ + return false; + } + + data[metadata->end_ptr].key = key; + data[metadata->end_ptr++].value = NULL; + } else { + if (metadata->end_ptr >= metadata->allocated) { + /* Exponential realloc */ + // Check for overflows first + if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, metadata->allocated)) { + return false; + } + + size_t new_allocation = metadata->allocated == 0 + ? 1 + : CBOR_BUFFER_GROWTH * metadata->allocated; + + unsigned char *new_data = _cbor_realloc_multiple( + item->data, sizeof(struct cbor_pair), new_allocation); + + if (new_data == NULL) { + return false; + } + + item->data = new_data; + metadata->allocated = new_allocation; + } + struct cbor_pair *data = cbor_map_handle(item); + data[metadata->end_ptr].key = key; + data[metadata->end_ptr++].value = NULL; + } + cbor_incref(key); + return true; +} + +bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value) { + CBOR_ASSERT(cbor_isa_map(item)); + cbor_incref(value); + cbor_map_handle(item)[ + /* Move one back since we are assuming _add_key (which increased the ptr) + * was the previous operation on this object */ + item->metadata.map_metadata.end_ptr - 1] + .value = value; + return true; +} + +// TODO: Add a more convenient API like add(item, key, val) +bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair) { + CBOR_ASSERT(cbor_isa_map(item)); + if (!_cbor_map_add_key(item, pair.key)) return false; + return _cbor_map_add_value(item, pair.value); +} + +bool cbor_map_is_definite(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_map(item)); + return item->metadata.map_metadata.type == _CBOR_METADATA_DEFINITE; +} + +bool cbor_map_is_indefinite(const cbor_item_t *item) { + return !cbor_map_is_definite(item); +} + +struct cbor_pair *cbor_map_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_map(item)); + return (struct cbor_pair *)item->data; +} diff --git a/source/external/libcbor/cbor/maps.h b/source/external/libcbor/cbor/maps.h new file mode 100644 index 000000000..5c05b542b --- /dev/null +++ b/source/external/libcbor/cbor/maps.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_MAPS_H +#define LIBCBOR_MAPS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * Map manipulation + * ============================================================================ + */ + +/** Get the number of pairs + * + * @param item A map + * @return The number of pairs + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_size(const cbor_item_t *item); + +/** Get the size of the allocated storage + * + * @param item A map + * @return Allocated storage size (as the number of #cbor_pair items) + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_allocated(const cbor_item_t *item); + +/** Create a new definite map + * + * @param size The number of slots to preallocate + * @return Reference to the new map item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_map(size_t size); + +/** Create a new indefinite map + * + * @return Reference to the new map item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_map(void); + +/** Add a pair to the map + * + * For definite maps, items can only be added to the preallocated space. For + * indefinite maps, the storage will be expanded as needed + * + * @param item A map + * @param pair The key-value pair to add. Reference count of the #cbor_pair.key + * and #cbor_pair.value will be increased by one. + * @return `true` on success, `false` if memory allocation failed (indefinite + * maps) or the preallocated storage is full (definite maps) + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_add(cbor_item_t *item, + struct cbor_pair pair); + +/** Add a key to the map + * + * Sets the value to `NULL`. Internal API. + * + * @param item A map + * @param key The key, Its reference count will be be increased by one. + * @return `true` on success, `false` if either reallocation failed or the + * preallocated storage is full + */ +_CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_key(cbor_item_t *item, + cbor_item_t *key); + +/** Add a value to the map + * + * Assumes that #_cbor_map_add_key has been called. Internal API. + * + * @param item A map + * @param value The value. Its reference count will be be increased by one. + * @return `true` on success, `false` if either reallocation failed or the + * preallocated storage is full + */ +_CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_value(cbor_item_t *item, + cbor_item_t *value); + +/** Is this map definite? + * + * @param item A map + * @return Is this map definite? + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_definite(const cbor_item_t *item); + +/** Is this map indefinite? + * + * @param item A map + * @return Is this map indefinite? + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_indefinite( + const cbor_item_t *item); + +/** Get the pairs storage + * + * @param item A map + * @return Array of #cbor_map_size pairs. Manipulation is possible as long as + * references remain valid. + */ +_CBOR_NODISCARD CBOR_EXPORT struct cbor_pair *cbor_map_handle( + const cbor_item_t *item); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_MAPS_H diff --git a/source/external/libcbor/cbor/serialization.c b/source/external/libcbor/cbor/serialization.c new file mode 100644 index 000000000..40f4c531d --- /dev/null +++ b/source/external/libcbor/cbor/serialization.c @@ -0,0 +1,368 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "serialization.h" +#include +#include "cbor/arrays.h" +#include "cbor/bytestrings.h" +#include "cbor/floats_ctrls.h" +#include "cbor/ints.h" +#include "cbor/maps.h" +#include "cbor/strings.h" +#include "cbor/tags.h" +#include "encoding.h" +#include "internal/memory_utils.h" + +size_t cbor_serialize(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + // cppcheck-suppress missingReturn + switch (cbor_typeof(item)) { + case CBOR_TYPE_UINT: + return cbor_serialize_uint(item, buffer, buffer_size); + case CBOR_TYPE_NEGINT: + return cbor_serialize_negint(item, buffer, buffer_size); + case CBOR_TYPE_BYTESTRING: + return cbor_serialize_bytestring(item, buffer, buffer_size); + case CBOR_TYPE_STRING: + return cbor_serialize_string(item, buffer, buffer_size); + case CBOR_TYPE_ARRAY: + return cbor_serialize_array(item, buffer, buffer_size); + case CBOR_TYPE_MAP: + return cbor_serialize_map(item, buffer, buffer_size); + case CBOR_TYPE_TAG: + return cbor_serialize_tag(item, buffer, buffer_size); + case CBOR_TYPE_FLOAT_CTRL: + return cbor_serialize_float_ctrl(item, buffer, buffer_size); + } +} + +/** Largest integer that can be encoded as embedded in the item leading byte. */ +const uint64_t kMaxEmbeddedInt = 23; + +/** How many bytes will a tag for a nested item of a given `size` take when + * encoded.*/ +size_t _cbor_encoded_header_size(uint64_t size) { + if (size <= kMaxEmbeddedInt) + return 1; + else if (size <= UINT8_MAX) + return 2; + else if (size <= UINT16_MAX) + return 3; + else if (size <= UINT32_MAX) + return 5; + else + return 9; +} + +size_t cbor_serialized_size(const cbor_item_t *item) { + // cppcheck-suppress missingReturn + switch (cbor_typeof(item)) { + case CBOR_TYPE_UINT: + case CBOR_TYPE_NEGINT: + switch (cbor_int_get_width(item)) { + case CBOR_INT_8: + if (cbor_get_uint8(item) <= kMaxEmbeddedInt) return 1; + return 2; + case CBOR_INT_16: + return 3; + case CBOR_INT_32: + return 5; + case CBOR_INT_64: + return 9; + } + // Note: We do not _cbor_safe_signaling_add zero-length definite strings, + // they would cause zeroes to propagate. All other items are at least one + // byte. + case CBOR_TYPE_BYTESTRING: { + if (cbor_bytestring_is_definite(item)) { + size_t header_size = + _cbor_encoded_header_size(cbor_bytestring_length(item)); + if (cbor_bytestring_length(item) == 0) return header_size; + return _cbor_safe_signaling_add(header_size, + cbor_bytestring_length(item)); + } + size_t indef_bytestring_size = 2; // Leading byte + break + cbor_item_t **chunks = cbor_bytestring_chunks_handle(item); + for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) { + indef_bytestring_size = _cbor_safe_signaling_add( + indef_bytestring_size, cbor_serialized_size(chunks[i])); + } + return indef_bytestring_size; + } + case CBOR_TYPE_STRING: { + if (cbor_string_is_definite(item)) { + size_t header_size = + _cbor_encoded_header_size(cbor_string_length(item)); + if (cbor_string_length(item) == 0) return header_size; + return _cbor_safe_signaling_add(header_size, cbor_string_length(item)); + } + size_t indef_string_size = 2; // Leading byte + break + cbor_item_t **chunks = cbor_string_chunks_handle(item); + for (size_t i = 0; i < cbor_string_chunk_count(item); i++) { + indef_string_size = _cbor_safe_signaling_add( + indef_string_size, cbor_serialized_size(chunks[i])); + } + return indef_string_size; + } + case CBOR_TYPE_ARRAY: { + size_t array_size = cbor_array_is_definite(item) + ? _cbor_encoded_header_size(cbor_array_size(item)) + : 2; // Leading byte + break + cbor_item_t **items = cbor_array_handle(item); + for (size_t i = 0; i < cbor_array_size(item); i++) { + array_size = _cbor_safe_signaling_add(array_size, + cbor_serialized_size(items[i])); + } + return array_size; + } + case CBOR_TYPE_MAP: { + size_t map_size = cbor_map_is_definite(item) + ? _cbor_encoded_header_size(cbor_map_size(item)) + : 2; // Leading byte + break + struct cbor_pair *items = cbor_map_handle(item); + for (size_t i = 0; i < cbor_map_size(item); i++) { + map_size = _cbor_safe_signaling_add( + map_size, + _cbor_safe_signaling_add(cbor_serialized_size(items[i].key), + cbor_serialized_size(items[i].value))); + } + return map_size; + } + case CBOR_TYPE_TAG: { + return _cbor_safe_signaling_add( + _cbor_encoded_header_size(cbor_tag_value(item)), + cbor_serialized_size(cbor_move(cbor_tag_item(item)))); + } + case CBOR_TYPE_FLOAT_CTRL: + switch (cbor_float_get_width(item)) { + case CBOR_FLOAT_0: + return _cbor_encoded_header_size(cbor_ctrl_value(item)); + case CBOR_FLOAT_16: + return 3; + case CBOR_FLOAT_32: + return 5; + case CBOR_FLOAT_64: + return 9; + } + } +} + +size_t cbor_serialize_alloc(const cbor_item_t *item, unsigned char **buffer, + size_t *buffer_size) { + *buffer = NULL; + size_t serialized_size = cbor_serialized_size(item); + if (serialized_size == 0) { + if (buffer_size != NULL) *buffer_size = 0; + return 0; + } + *buffer = _cbor_malloc(serialized_size); + if (*buffer == NULL) { + if (buffer_size != NULL) *buffer_size = 0; + return 0; + } + + size_t written = cbor_serialize(item, *buffer, serialized_size); + CBOR_ASSERT(written == serialized_size); + if (buffer_size != NULL) *buffer_size = serialized_size; + return written; +} + +size_t cbor_serialize_uint(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_uint(item)); + // cppcheck-suppress missingReturn + switch (cbor_int_get_width(item)) { + case CBOR_INT_8: + return cbor_encode_uint8(cbor_get_uint8(item), buffer, buffer_size); + case CBOR_INT_16: + return cbor_encode_uint16(cbor_get_uint16(item), buffer, buffer_size); + case CBOR_INT_32: + return cbor_encode_uint32(cbor_get_uint32(item), buffer, buffer_size); + case CBOR_INT_64: + return cbor_encode_uint64(cbor_get_uint64(item), buffer, buffer_size); + } +} + +size_t cbor_serialize_negint(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_negint(item)); + // cppcheck-suppress missingReturn + switch (cbor_int_get_width(item)) { + case CBOR_INT_8: + return cbor_encode_negint8(cbor_get_uint8(item), buffer, buffer_size); + case CBOR_INT_16: + return cbor_encode_negint16(cbor_get_uint16(item), buffer, buffer_size); + case CBOR_INT_32: + return cbor_encode_negint32(cbor_get_uint32(item), buffer, buffer_size); + case CBOR_INT_64: + return cbor_encode_negint64(cbor_get_uint64(item), buffer, buffer_size); + } +} + +size_t cbor_serialize_bytestring(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_bytestring(item)); + if (cbor_bytestring_is_definite(item)) { + size_t length = cbor_bytestring_length(item); + size_t written = cbor_encode_bytestring_start(length, buffer, buffer_size); + if (written > 0 && (buffer_size - written >= length)) { + memcpy(buffer + written, cbor_bytestring_handle(item), length); + return written + length; + } + return 0; + } else { + CBOR_ASSERT(cbor_bytestring_is_indefinite(item)); + size_t chunk_count = cbor_bytestring_chunk_count(item); + size_t written = cbor_encode_indef_bytestring_start(buffer, buffer_size); + if (written == 0) return 0; + + cbor_item_t **chunks = cbor_bytestring_chunks_handle(item); + for (size_t i = 0; i < chunk_count; i++) { + size_t chunk_written = cbor_serialize_bytestring( + chunks[i], buffer + written, buffer_size - written); + if (chunk_written == 0) return 0; + written += chunk_written; + } + + size_t break_written = + cbor_encode_break(buffer + written, buffer_size - written); + if (break_written == 0) return 0; + return written + break_written; + } +} + +size_t cbor_serialize_string(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_string(item)); + if (cbor_string_is_definite(item)) { + size_t length = cbor_string_length(item); + size_t written = cbor_encode_string_start(length, buffer, buffer_size); + if (written && (buffer_size - written >= length)) { + memcpy(buffer + written, cbor_string_handle(item), length); + return written + length; + } + return 0; + } else { + CBOR_ASSERT(cbor_string_is_indefinite(item)); + size_t chunk_count = cbor_string_chunk_count(item); + size_t written = cbor_encode_indef_string_start(buffer, buffer_size); + if (written == 0) return 0; + + cbor_item_t **chunks = cbor_string_chunks_handle(item); + for (size_t i = 0; i < chunk_count; i++) { + size_t chunk_written = cbor_serialize_string(chunks[i], buffer + written, + buffer_size - written); + if (chunk_written == 0) return 0; + written += chunk_written; + } + + size_t break_written = + cbor_encode_break(buffer + written, buffer_size - written); + if (break_written == 0) return 0; + return written + break_written; + } +} + +size_t cbor_serialize_array(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_array(item)); + size_t size = cbor_array_size(item), written = 0; + cbor_item_t **handle = cbor_array_handle(item); + if (cbor_array_is_definite(item)) { + written = cbor_encode_array_start(size, buffer, buffer_size); + } else { + CBOR_ASSERT(cbor_array_is_indefinite(item)); + written = cbor_encode_indef_array_start(buffer, buffer_size); + } + if (written == 0) return 0; + + for (size_t i = 0; i < size; i++) { + size_t item_written = + cbor_serialize(*(handle++), buffer + written, buffer_size - written); + if (item_written == 0) return 0; + written += item_written; + } + + if (cbor_array_is_definite(item)) { + return written; + } else { + CBOR_ASSERT(cbor_array_is_indefinite(item)); + size_t break_written = + cbor_encode_break(buffer + written, buffer_size - written); + if (break_written == 0) return 0; + return written + break_written; + } +} + +size_t cbor_serialize_map(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_map(item)); + size_t size = cbor_map_size(item), written = 0; + struct cbor_pair *handle = cbor_map_handle(item); + + if (cbor_map_is_definite(item)) { + written = cbor_encode_map_start(size, buffer, buffer_size); + } else { + CBOR_ASSERT(cbor_map_is_indefinite(item)); + written = cbor_encode_indef_map_start(buffer, buffer_size); + } + if (written == 0) return 0; + + for (size_t i = 0; i < size; i++) { + size_t item_written = + cbor_serialize(handle->key, buffer + written, buffer_size - written); + if (item_written == 0) { + return 0; + } + written += item_written; + item_written = cbor_serialize((handle++)->value, buffer + written, + buffer_size - written); + if (item_written == 0) return 0; + written += item_written; + } + + if (cbor_map_is_definite(item)) { + return written; + } else { + CBOR_ASSERT(cbor_map_is_indefinite(item)); + size_t break_written = + cbor_encode_break(buffer + written, buffer_size - written); + if (break_written == 0) return 0; + return written + break_written; + } +} + +size_t cbor_serialize_tag(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_tag(item)); + size_t written = cbor_encode_tag(cbor_tag_value(item), buffer, buffer_size); + if (written == 0) return 0; + + size_t item_written = cbor_serialize(cbor_move(cbor_tag_item(item)), + buffer + written, buffer_size - written); + if (item_written == 0) return 0; + return written + item_written; +} + +size_t cbor_serialize_float_ctrl(const cbor_item_t *item, unsigned char *buffer, + size_t buffer_size) { + CBOR_ASSERT(cbor_isa_float_ctrl(item)); + // cppcheck-suppress missingReturn + switch (cbor_float_get_width(item)) { + case CBOR_FLOAT_0: + /* CTRL - special treatment */ + return cbor_encode_ctrl(cbor_ctrl_value(item), buffer, buffer_size); + case CBOR_FLOAT_16: + return cbor_encode_half(cbor_float_get_float2(item), buffer, buffer_size); + case CBOR_FLOAT_32: + return cbor_encode_single(cbor_float_get_float4(item), buffer, + buffer_size); + case CBOR_FLOAT_64: + return cbor_encode_double(cbor_float_get_float8(item), buffer, + buffer_size); + } +} diff --git a/source/external/libcbor/cbor/serialization.h b/source/external/libcbor/cbor/serialization.h new file mode 100644 index 000000000..228ae75d6 --- /dev/null +++ b/source/external/libcbor/cbor/serialization.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_SERIALIZATION_H +#define LIBCBOR_SERIALIZATION_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * High level encoding + * ============================================================================ + */ + +/** Serialize the given item + * + * @param item A data item + * @param buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result. 0 on failure. + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item, + cbor_mutable_data buffer, + size_t buffer_size); + +/** Compute the length (in bytes) of the item when serialized using + * `cbor_serialize`. + * + * Time complexity is proportional to the number of nested items. + * + * @param item A data item + * @return Length (>= 1) of the item when serialized. 0 if the length overflows + * `size_t`. + */ +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_serialized_size(const cbor_item_t *item); + +/** Serialize the given item, allocating buffers as needed + * + * Since libcbor v0.10, the return value is always the same as `buffer_size` (if + * provided, see https://github.com/PJK/libcbor/pull/251/). New clients should + * ignore the return value. + * + * \rst + * .. warning:: It is the caller's responsibility to free the buffer using an + * appropriate ``free`` implementation. + * \endrst + * + * @param item A data item + * @param[out] buffer Buffer containing the result + * @param[out] buffer_size Size of the \p buffer, or 0 on memory allocation + * failure. + * @return Length of the result in bytes + * @return 0 on memory allocation failure, in which case \p buffer is `NULL`. + */ +CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item, + unsigned char **buffer, + size_t *buffer_size); + +/** Serialize an uint + * + * @param item A uint + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *item, + cbor_mutable_data buffer, + size_t buffer_size); + +/** Serialize a negint + * + * @param item A negint + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_negint( + const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); + +/** Serialize a bytestring + * + * @param item A bytestring + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may + * still be modified + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_bytestring( + const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); + +/** Serialize a string + * + * @param item A string + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may + * still be modified + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_string( + const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); +/** Serialize an array + * + * @param item An array + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may + * still be modified + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_array( + const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); + +/** Serialize a map + * + * @param item A map + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may + * still be modified + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *item, + cbor_mutable_data buffer, + size_t buffer_size); + +/** Serialize a tag + * + * @param item A tag + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may + * still be modified + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *item, + cbor_mutable_data buffer, + size_t buffer_size); + +/** Serialize a + * + * @param item A float or ctrl + * @param[out] buffer Buffer to serialize to + * @param buffer_size Size of the \p buffer + * @return Length of the result + * @return 0 if the \p buffer_size doesn't fit the result + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_float_ctrl( + const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_SERIALIZATION_H diff --git a/source/external/libcbor/cbor/streaming.c b/source/external/libcbor/cbor/streaming.c new file mode 100644 index 000000000..4b3770114 --- /dev/null +++ b/source/external/libcbor/cbor/streaming.c @@ -0,0 +1,600 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "streaming.h" +#include "internal/loaders.h" + +static bool claim_bytes(size_t required, size_t provided, + struct cbor_decoder_result *result) { + if (required > (provided - result->read)) { + result->required = required + result->read; + result->read = 0; + result->status = CBOR_DECODER_NEDATA; + return false; + } else { + result->read += required; + result->required = 0; + return true; + } +} + +// Use implicit capture as an exception to avoid the super long parameter list +#define CLAIM_BYTES_AND_INVOKE(callback_name, length, source_extra_offset) \ + do { \ + if (claim_bytes(length, source_size, &result)) { \ + callbacks->callback_name(context, source + 1 + source_extra_offset, \ + length); \ + } \ + } while (0) + +#define READ_CLAIM_INVOKE(callback_name, length_reader, length_bytes) \ + do { \ + if (claim_bytes(length_bytes, source_size, &result)) { \ + uint64_t length = length_reader(source + 1); \ + CLAIM_BYTES_AND_INVOKE(callback_name, length, length_bytes); \ + } \ + return result; \ + } while (0) + +struct cbor_decoder_result cbor_stream_decode( + cbor_data source, size_t source_size, + const struct cbor_callbacks *callbacks, void *context) { + // Attempt to claim the initial MTB byte + struct cbor_decoder_result result = {.status = CBOR_DECODER_FINISHED}; + if (!claim_bytes(1, source_size, &result)) { + return result; + } + + switch (*source) { + case 0x00: /* Fallthrough */ + case 0x01: /* Fallthrough */ + case 0x02: /* Fallthrough */ + case 0x03: /* Fallthrough */ + case 0x04: /* Fallthrough */ + case 0x05: /* Fallthrough */ + case 0x06: /* Fallthrough */ + case 0x07: /* Fallthrough */ + case 0x08: /* Fallthrough */ + case 0x09: /* Fallthrough */ + case 0x0A: /* Fallthrough */ + case 0x0B: /* Fallthrough */ + case 0x0C: /* Fallthrough */ + case 0x0D: /* Fallthrough */ + case 0x0E: /* Fallthrough */ + case 0x0F: /* Fallthrough */ + case 0x10: /* Fallthrough */ + case 0x11: /* Fallthrough */ + case 0x12: /* Fallthrough */ + case 0x13: /* Fallthrough */ + case 0x14: /* Fallthrough */ + case 0x15: /* Fallthrough */ + case 0x16: /* Fallthrough */ + case 0x17: + /* Embedded one byte unsigned integer */ + { + callbacks->uint8(context, _cbor_load_uint8(source)); + return result; + } + case 0x18: + /* One byte unsigned integer */ + { + if (claim_bytes(1, source_size, &result)) { + callbacks->uint8(context, _cbor_load_uint8(source + 1)); + } + return result; + } + case 0x19: + /* Two bytes unsigned integer */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->uint16(context, _cbor_load_uint16(source + 1)); + } + return result; + } + case 0x1A: + /* Four bytes unsigned integer */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->uint32(context, _cbor_load_uint32(source + 1)); + } + return result; + } + case 0x1B: + /* Eight bytes unsigned integer */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->uint64(context, _cbor_load_uint64(source + 1)); + } + return result; + } + case 0x1C: /* Fallthrough */ + case 0x1D: /* Fallthrough */ + case 0x1E: /* Fallthrough */ + case 0x1F: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0x20: /* Fallthrough */ + case 0x21: /* Fallthrough */ + case 0x22: /* Fallthrough */ + case 0x23: /* Fallthrough */ + case 0x24: /* Fallthrough */ + case 0x25: /* Fallthrough */ + case 0x26: /* Fallthrough */ + case 0x27: /* Fallthrough */ + case 0x28: /* Fallthrough */ + case 0x29: /* Fallthrough */ + case 0x2A: /* Fallthrough */ + case 0x2B: /* Fallthrough */ + case 0x2C: /* Fallthrough */ + case 0x2D: /* Fallthrough */ + case 0x2E: /* Fallthrough */ + case 0x2F: /* Fallthrough */ + case 0x30: /* Fallthrough */ + case 0x31: /* Fallthrough */ + case 0x32: /* Fallthrough */ + case 0x33: /* Fallthrough */ + case 0x34: /* Fallthrough */ + case 0x35: /* Fallthrough */ + case 0x36: /* Fallthrough */ + case 0x37: + /* Embedded one byte negative integer */ + { + callbacks->negint8(context, + _cbor_load_uint8(source) - 0x20); /* 0x20 offset */ + return result; + } + case 0x38: + /* One byte negative integer */ + { + if (claim_bytes(1, source_size, &result)) { + callbacks->negint8(context, _cbor_load_uint8(source + 1)); + } + return result; + } + case 0x39: + /* Two bytes negative integer */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->negint16(context, _cbor_load_uint16(source + 1)); + } + return result; + } + case 0x3A: + /* Four bytes negative integer */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->negint32(context, _cbor_load_uint32(source + 1)); + } + return result; + } + case 0x3B: + /* Eight bytes negative integer */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->negint64(context, _cbor_load_uint64(source + 1)); + } + return result; + } + case 0x3C: /* Fallthrough */ + case 0x3D: /* Fallthrough */ + case 0x3E: /* Fallthrough */ + case 0x3F: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0x40: /* Fallthrough */ + case 0x41: /* Fallthrough */ + case 0x42: /* Fallthrough */ + case 0x43: /* Fallthrough */ + case 0x44: /* Fallthrough */ + case 0x45: /* Fallthrough */ + case 0x46: /* Fallthrough */ + case 0x47: /* Fallthrough */ + case 0x48: /* Fallthrough */ + case 0x49: /* Fallthrough */ + case 0x4A: /* Fallthrough */ + case 0x4B: /* Fallthrough */ + case 0x4C: /* Fallthrough */ + case 0x4D: /* Fallthrough */ + case 0x4E: /* Fallthrough */ + case 0x4F: /* Fallthrough */ + case 0x50: /* Fallthrough */ + case 0x51: /* Fallthrough */ + case 0x52: /* Fallthrough */ + case 0x53: /* Fallthrough */ + case 0x54: /* Fallthrough */ + case 0x55: /* Fallthrough */ + case 0x56: /* Fallthrough */ + case 0x57: + /* Embedded length byte string */ + { + uint64_t length = _cbor_load_uint8(source) - 0x40; /* 0x40 offset */ + CLAIM_BYTES_AND_INVOKE(byte_string, length, 0); + return result; + } + case 0x58: + /* One byte length byte string */ + READ_CLAIM_INVOKE(byte_string, _cbor_load_uint8, 1); + case 0x59: + /* Two bytes length byte string */ + READ_CLAIM_INVOKE(byte_string, _cbor_load_uint16, 2); + case 0x5A: + /* Four bytes length byte string */ + READ_CLAIM_INVOKE(byte_string, _cbor_load_uint32, 4); + case 0x5B: + /* Eight bytes length byte string */ + READ_CLAIM_INVOKE(byte_string, _cbor_load_uint64, 8); + case 0x5C: /* Fallthrough */ + case 0x5D: /* Fallthrough */ + case 0x5E: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0x5F: + /* Indefinite byte string */ + { + callbacks->byte_string_start(context); + return result; + } + case 0x60: /* Fallthrough */ + case 0x61: /* Fallthrough */ + case 0x62: /* Fallthrough */ + case 0x63: /* Fallthrough */ + case 0x64: /* Fallthrough */ + case 0x65: /* Fallthrough */ + case 0x66: /* Fallthrough */ + case 0x67: /* Fallthrough */ + case 0x68: /* Fallthrough */ + case 0x69: /* Fallthrough */ + case 0x6A: /* Fallthrough */ + case 0x6B: /* Fallthrough */ + case 0x6C: /* Fallthrough */ + case 0x6D: /* Fallthrough */ + case 0x6E: /* Fallthrough */ + case 0x6F: /* Fallthrough */ + case 0x70: /* Fallthrough */ + case 0x71: /* Fallthrough */ + case 0x72: /* Fallthrough */ + case 0x73: /* Fallthrough */ + case 0x74: /* Fallthrough */ + case 0x75: /* Fallthrough */ + case 0x76: /* Fallthrough */ + case 0x77: + /* Embedded one byte length string */ + { + uint64_t length = _cbor_load_uint8(source) - 0x60; /* 0x60 offset */ + CLAIM_BYTES_AND_INVOKE(string, length, 0); + return result; + } + case 0x78: + /* One byte length string */ + READ_CLAIM_INVOKE(string, _cbor_load_uint8, 1); + case 0x79: + /* Two bytes length string */ + READ_CLAIM_INVOKE(string, _cbor_load_uint16, 2); + case 0x7A: + /* Four bytes length string */ + READ_CLAIM_INVOKE(string, _cbor_load_uint32, 4); + case 0x7B: + /* Eight bytes length string */ + READ_CLAIM_INVOKE(string, _cbor_load_uint64, 8); + case 0x7C: /* Fallthrough */ + case 0x7D: /* Fallthrough */ + case 0x7E: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0x7F: + /* Indefinite length string */ + { + callbacks->string_start(context); + return result; + } + case 0x80: /* Fallthrough */ + case 0x81: /* Fallthrough */ + case 0x82: /* Fallthrough */ + case 0x83: /* Fallthrough */ + case 0x84: /* Fallthrough */ + case 0x85: /* Fallthrough */ + case 0x86: /* Fallthrough */ + case 0x87: /* Fallthrough */ + case 0x88: /* Fallthrough */ + case 0x89: /* Fallthrough */ + case 0x8A: /* Fallthrough */ + case 0x8B: /* Fallthrough */ + case 0x8C: /* Fallthrough */ + case 0x8D: /* Fallthrough */ + case 0x8E: /* Fallthrough */ + case 0x8F: /* Fallthrough */ + case 0x90: /* Fallthrough */ + case 0x91: /* Fallthrough */ + case 0x92: /* Fallthrough */ + case 0x93: /* Fallthrough */ + case 0x94: /* Fallthrough */ + case 0x95: /* Fallthrough */ + case 0x96: /* Fallthrough */ + case 0x97: + /* Embedded one byte length array */ + { + callbacks->array_start( + context, _cbor_load_uint8(source) - 0x80); /* 0x40 offset */ + return result; + } + case 0x98: + /* One byte length array */ + { + if (claim_bytes(1, source_size, &result)) { + callbacks->array_start(context, _cbor_load_uint8(source + 1)); + } + return result; + } + case 0x99: + /* Two bytes length array */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->array_start(context, _cbor_load_uint16(source + 1)); + } + return result; + } + case 0x9A: + /* Four bytes length array */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->array_start(context, _cbor_load_uint32(source + 1)); + } + return result; + } + case 0x9B: + /* Eight bytes length array */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->array_start(context, _cbor_load_uint64(source + 1)); + } + return result; + } + case 0x9C: /* Fallthrough */ + case 0x9D: /* Fallthrough */ + case 0x9E: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0x9F: + /* Indefinite length array */ + { + callbacks->indef_array_start(context); + return result; + } + case 0xA0: /* Fallthrough */ + case 0xA1: /* Fallthrough */ + case 0xA2: /* Fallthrough */ + case 0xA3: /* Fallthrough */ + case 0xA4: /* Fallthrough */ + case 0xA5: /* Fallthrough */ + case 0xA6: /* Fallthrough */ + case 0xA7: /* Fallthrough */ + case 0xA8: /* Fallthrough */ + case 0xA9: /* Fallthrough */ + case 0xAA: /* Fallthrough */ + case 0xAB: /* Fallthrough */ + case 0xAC: /* Fallthrough */ + case 0xAD: /* Fallthrough */ + case 0xAE: /* Fallthrough */ + case 0xAF: /* Fallthrough */ + case 0xB0: /* Fallthrough */ + case 0xB1: /* Fallthrough */ + case 0xB2: /* Fallthrough */ + case 0xB3: /* Fallthrough */ + case 0xB4: /* Fallthrough */ + case 0xB5: /* Fallthrough */ + case 0xB6: /* Fallthrough */ + case 0xB7: + /* Embedded one byte length map */ + { + callbacks->map_start(context, + _cbor_load_uint8(source) - 0xA0); /* 0xA0 offset */ + return result; + } + case 0xB8: + /* One byte length map */ + { + if (claim_bytes(1, source_size, &result)) { + callbacks->map_start(context, _cbor_load_uint8(source + 1)); + } + return result; + } + case 0xB9: + /* Two bytes length map */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->map_start(context, _cbor_load_uint16(source + 1)); + } + return result; + } + case 0xBA: + /* Four bytes length map */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->map_start(context, _cbor_load_uint32(source + 1)); + } + return result; + } + case 0xBB: + /* Eight bytes length map */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->map_start(context, _cbor_load_uint64(source + 1)); + } + return result; + } + case 0xBC: /* Fallthrough */ + case 0xBD: /* Fallthrough */ + case 0xBE: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0xBF: + /* Indefinite length map */ + { + callbacks->indef_map_start(context); + return result; + } + /* See https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml for tag + * assignment. All well-formed tags are processed regardless of validity + * since maintaining the known mapping would be impractical. + * + * Moreover, even tags in the reserved "standard" range are not assigned + * but may get assigned in the future (see e.g. + * https://github.com/PJK/libcbor/issues/307), so processing all tags + * improves forward compatibility. + */ + case 0xC0: /* Fallthrough */ + case 0xC1: /* Fallthrough */ + case 0xC2: /* Fallthrough */ + case 0xC3: /* Fallthrough */ + case 0xC4: /* Fallthrough */ + case 0xC5: /* Fallthrough */ + case 0xC6: /* Fallthrough */ + case 0xC7: /* Fallthrough */ + case 0xC8: /* Fallthrough */ + case 0xC9: /* Fallthrough */ + case 0xCA: /* Fallthrough */ + case 0xCB: /* Fallthrough */ + case 0xCC: /* Fallthrough */ + case 0xCD: /* Fallthrough */ + case 0xCE: /* Fallthrough */ + case 0xCF: /* Fallthrough */ + case 0xD0: /* Fallthrough */ + case 0xD1: /* Fallthrough */ + case 0xD2: /* Fallthrough */ + case 0xD3: /* Fallthrough */ + case 0xD4: /* Fallthrough */ + case 0xD5: /* Fallthrough */ + case 0xD6: /* Fallthrough */ + case 0xD7: /* Fallthrough */ + { + callbacks->tag(context, (uint64_t)(_cbor_load_uint8(source) - + 0xC0)); /* 0xC0 offset */ + return result; + } + case 0xD8: /* 1B tag */ + { + if (claim_bytes(1, source_size, &result)) { + callbacks->tag(context, _cbor_load_uint8(source + 1)); + } + return result; + } + case 0xD9: /* 2B tag */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->tag(context, _cbor_load_uint16(source + 1)); + } + return result; + } + case 0xDA: /* 4B tag */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->tag(context, _cbor_load_uint32(source + 1)); + } + return result; + } + case 0xDB: /* 8B tag */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->tag(context, _cbor_load_uint64(source + 1)); + } + return result; + } + case 0xDC: /* Fallthrough */ + case 0xDD: /* Fallthrough */ + case 0xDE: /* Fallthrough */ + case 0xDF: /* Reserved */ + { + return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; + } + case 0xE0: /* Fallthrough */ + case 0xE1: /* Fallthrough */ + case 0xE2: /* Fallthrough */ + case 0xE3: /* Fallthrough */ + case 0xE4: /* Fallthrough */ + case 0xE5: /* Fallthrough */ + case 0xE6: /* Fallthrough */ + case 0xE7: /* Fallthrough */ + case 0xE8: /* Fallthrough */ + case 0xE9: /* Fallthrough */ + case 0xEA: /* Fallthrough */ + case 0xEB: /* Fallthrough */ + case 0xEC: /* Fallthrough */ + case 0xED: /* Fallthrough */ + case 0xEE: /* Fallthrough */ + case 0xEF: /* Fallthrough */ + case 0xF0: /* Fallthrough */ + case 0xF1: /* Fallthrough */ + case 0xF2: /* Fallthrough */ + case 0xF3: /* Simple value - unassigned */ + { + return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; + } + case 0xF4: + /* False */ + { + callbacks->boolean(context, false); + return result; + } + case 0xF5: + /* True */ + { + callbacks->boolean(context, true); + return result; + } + case 0xF6: + /* Null */ + { + callbacks->null(context); + return result; + } + case 0xF7: + /* Undefined */ + { + callbacks->undefined(context); + return result; + } + case 0xF8: + /* 1B simple value, unassigned */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0xF9: + /* 2B float */ + { + if (claim_bytes(2, source_size, &result)) { + callbacks->float2(context, _cbor_load_half(source + 1)); + } + return result; + } + case 0xFA: + /* 4B float */ + { + if (claim_bytes(4, source_size, &result)) { + callbacks->float4(context, _cbor_load_float(source + 1)); + } + return result; + } + case 0xFB: + /* 8B float */ + { + if (claim_bytes(8, source_size, &result)) { + callbacks->float8(context, _cbor_load_double(source + 1)); + } + return result; + } + case 0xFC: /* Fallthrough */ + case 0xFD: /* Fallthrough */ + case 0xFE: + /* Reserved */ + { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; } + case 0xFF: + /* Break */ + callbacks->indef_break(context); + // Never happens, the switch statement is exhaustive on the 1B range; make + // compiler happy + default: + return result; + } +} diff --git a/source/external/libcbor/cbor/streaming.h b/source/external/libcbor/cbor/streaming.h new file mode 100644 index 000000000..cb908e17e --- /dev/null +++ b/source/external/libcbor/cbor/streaming.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_STREAMING_H +#define LIBCBOR_STREAMING_H + +#include "callbacks.h" +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Stateless decoder + * + * Will try parsing the \p source and will invoke the appropriate callback on + * success. Decodes one item at a time. No memory allocations occur. + * + * @param source Input buffer + * @param source_size Length of the buffer + * @param callbacks The callback bundle + * @param context An arbitrary pointer to allow for maintaining context. + */ +_CBOR_NODISCARD CBOR_EXPORT struct cbor_decoder_result cbor_stream_decode( + cbor_data source, size_t source_size, + const struct cbor_callbacks* callbacks, void* context); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_STREAMING_H diff --git a/source/external/libcbor/cbor/strings.c b/source/external/libcbor/cbor/strings.c new file mode 100644 index 000000000..6ae96545c --- /dev/null +++ b/source/external/libcbor/cbor/strings.c @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "strings.h" +#include +#include "internal/memory_utils.h" +#include "internal/unicode.h" + +cbor_item_t *cbor_new_definite_string(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_STRING, + .metadata = {.string_metadata = {_CBOR_METADATA_DEFINITE, 0}}}; + return item; +} + +cbor_item_t *cbor_new_indefinite_string(void) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_STRING, + .metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE, + .length = 0}}, + .data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))}; + _CBOR_DEPENDENT_NOTNULL(item, item->data); + *((struct cbor_indefinite_string_data *)item->data) = + (struct cbor_indefinite_string_data){ + .chunk_count = 0, + .chunk_capacity = 0, + .chunks = NULL, + }; + return item; +} + +cbor_item_t *cbor_build_string(const char *val) { + cbor_item_t *item = cbor_new_definite_string(); + _CBOR_NOTNULL(item); + size_t len = strlen(val); + unsigned char *handle = _cbor_malloc(len); + _CBOR_DEPENDENT_NOTNULL(item, handle); + memcpy(handle, val, len); + cbor_string_set_handle(item, handle, len); + return item; +} + +cbor_item_t *cbor_build_stringn(const char *val, size_t length) { + cbor_item_t *item = cbor_new_definite_string(); + _CBOR_NOTNULL(item); + unsigned char *handle = _cbor_malloc(length); + _CBOR_DEPENDENT_NOTNULL(item, handle); + memcpy(handle, val, length); + cbor_string_set_handle(item, handle, length); + return item; +} + +void cbor_string_set_handle(cbor_item_t *item, + cbor_mutable_data CBOR_RESTRICT_POINTER data, + size_t length) { + CBOR_ASSERT(cbor_isa_string(item)); + CBOR_ASSERT(cbor_string_is_definite(item)); + item->data = data; + item->metadata.string_metadata.length = length; + struct _cbor_unicode_status unicode_status; + size_t codepoint_count = + _cbor_unicode_codepoint_count(data, length, &unicode_status); + CBOR_ASSERT(codepoint_count <= length); + if (unicode_status.status == _CBOR_UNICODE_OK) { + item->metadata.string_metadata.codepoint_count = codepoint_count; + } else { + item->metadata.string_metadata.codepoint_count = 0; + } +} + +cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + CBOR_ASSERT(cbor_string_is_indefinite(item)); + return ((struct cbor_indefinite_string_data *)item->data)->chunks; +} + +size_t cbor_string_chunk_count(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + CBOR_ASSERT(cbor_string_is_indefinite(item)); + return ((struct cbor_indefinite_string_data *)item->data)->chunk_count; +} + +bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) { + CBOR_ASSERT(cbor_isa_string(item)); + CBOR_ASSERT(cbor_string_is_indefinite(item)); + struct cbor_indefinite_string_data *data = + (struct cbor_indefinite_string_data *)item->data; + if (data->chunk_count == data->chunk_capacity) { + if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) { + return false; + } + + size_t new_chunk_capacity = + data->chunk_capacity == 0 ? 1 + : CBOR_BUFFER_GROWTH * (data->chunk_capacity); + cbor_item_t **new_chunks_data = _cbor_realloc_multiple( + data->chunks, sizeof(cbor_item_t *), new_chunk_capacity); + + if (new_chunks_data == NULL) { + return false; + } + + data->chunk_capacity = new_chunk_capacity; + data->chunks = new_chunks_data; + } + data->chunks[data->chunk_count++] = cbor_incref(chunk); + return true; +} + +size_t cbor_string_length(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + return item->metadata.string_metadata.length; +} + +unsigned char *cbor_string_handle(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + return item->data; +} + +size_t cbor_string_codepoint_count(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + return item->metadata.string_metadata.codepoint_count; +} + +bool cbor_string_is_definite(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_string(item)); + return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE; +} + +bool cbor_string_is_indefinite(const cbor_item_t *item) { + return !cbor_string_is_definite(item); +} diff --git a/source/external/libcbor/cbor/strings.h b/source/external/libcbor/cbor/strings.h new file mode 100644 index 000000000..3e03f8138 --- /dev/null +++ b/source/external/libcbor/cbor/strings.h @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_STRINGS_H +#define LIBCBOR_STRINGS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * String manipulation + * ============================================================================ + */ + +/** Returns the length of the underlying string in bytes + * + * There can be fewer unicode character than bytes (see + * `cbor_string_codepoint_count`). For definite strings only. + * + * @param item a definite string + * @return length of the string. Zero if no chunk has been attached yet + */ +_CBOR_NODISCARD CBOR_EXPORT size_t cbor_string_length(const cbor_item_t *item); + +/** The number of codepoints in this string + * + * Might differ from `cbor_string_length` if there are multibyte codepoints. + * If the string data is not valid UTF-8, returns 0. + * + * @param item A string + * @return The number of codepoints in this string + */ +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_string_codepoint_count(const cbor_item_t *item); + +/** Is the string definite? + * + * @param item a string + * @return Is the string definite? + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_definite( + const cbor_item_t *item); + +/** Is the string indefinite? + * + * @param item a string + * @return Is the string indefinite? + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_indefinite( + const cbor_item_t *item); + +/** Get the handle to the underlying string + * + * Definite items only. Modifying the data is allowed. In that case, the caller + * takes responsibility for the effect on items this item might be a part of + * + * @param item A definite string + * @return The address of the underlying string. + * @return `NULL` if no data have been assigned yet. + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_mutable_data +cbor_string_handle(const cbor_item_t *item); + +/** Set the handle to the underlying string + * + * The data is assumed to be a valid UTF-8 string. If the string is non-empty + * and invalid, `cbor_string_codepoint_count` will return 0. + * + * \rst + * .. warning:: Using a pointer to a stack allocated constant is a common + * mistake. Lifetime of the string will expire when it goes out of scope and + * the CBOR item will be left inconsistent. + * \endrst + * + * @param item A definite string + * @param data The memory block. The caller gives up the ownership of the block. + * libcbor will deallocate it when appropriate using its free function + * @param length Length of the data block + */ +CBOR_EXPORT void cbor_string_set_handle( + cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, + size_t length); + +/** Get the handle to the array of chunks + * + * Manipulations with the memory block (e.g. sorting it) are allowed, but the + * validity and the number of chunks must be retained. + * + * @param item A indefinite string + * @return array of #cbor_string_chunk_count definite strings + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t **cbor_string_chunks_handle( + const cbor_item_t *item); + +/** Get the number of chunks this string consist of + * + * @param item A indefinite string + * @return The chunk count. 0 for freshly created items. + */ +_CBOR_NODISCARD CBOR_EXPORT size_t +cbor_string_chunk_count(const cbor_item_t *item); + +/** Appends a chunk to the string + * + * Indefinite strings only. + * + * May realloc the chunk storage. + * + * @param item An indefinite string + * @param chunk A definite string item. Its reference count will be increased + * by one. + * @return `true` on success. `false` on memory allocation failure. In that + * case, the refcount of @p `chunk` is not increased and the @p `item` is left + * intact. + */ +_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_add_chunk(cbor_item_t *item, + cbor_item_t *chunk); + +/** Creates a new definite string + * + * The handle is initialized to `NULL` and length to 0 + * + * @return Reference to the new string item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_string(void); + +/** Creates a new indefinite string + * + * The chunks array is initialized to `NULL` and chunkcount to 0 + * + * @return Reference to the new string item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_string(void); + +/** Creates a new string and initializes it + * + * The data from `val` will be copied to a newly allocated memory block. + * + * Note that valid UTF-8 strings do not contain null bytes, so this routine is + * correct for all valid inputs. If the input is not guaranteed to be valid, + * use `cbor_build_stringn` instead. + * + * @param val A null-terminated UTF-8 string + * @return Reference to the new string item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_string(const char *val); + +/** Creates a new string and initializes it + * + * The data from `handle` will be copied to a newly allocated memory block. + * + * All @p `length` bytes will be stored in the string, even if there are null + * bytes or invalid UTF-8 sequences. + * + * @param val A UTF-8 string, at least @p `length` bytes long + * @param length Length (in bytes) of the string passed in @p `val`. + * @return Reference to the new string item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_stringn(const char *val, + size_t length); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_STRINGS_H diff --git a/source/external/libcbor/cbor/tags.c b/source/external/libcbor/cbor/tags.c new file mode 100644 index 000000000..3f3edb0b0 --- /dev/null +++ b/source/external/libcbor/cbor/tags.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "tags.h" + +cbor_item_t *cbor_new_tag(uint64_t value) { + cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); + _CBOR_NOTNULL(item); + + *item = (cbor_item_t){ + .refcount = 1, + .type = CBOR_TYPE_TAG, + .metadata = {.tag_metadata = {.value = value, .tagged_item = NULL}}, + .data = NULL /* Never used */ + }; + return item; +} + +cbor_item_t *cbor_tag_item(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_tag(item)); + return cbor_incref(item->metadata.tag_metadata.tagged_item); +} + +uint64_t cbor_tag_value(const cbor_item_t *item) { + CBOR_ASSERT(cbor_isa_tag(item)); + return item->metadata.tag_metadata.value; +} + +void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item) { + CBOR_ASSERT(cbor_isa_tag(item)); + cbor_incref(tagged_item); + item->metadata.tag_metadata.tagged_item = tagged_item; +} + +cbor_item_t *cbor_build_tag(uint64_t value, cbor_item_t *item) { + cbor_item_t *res = cbor_new_tag(value); + if (res == NULL) { + return NULL; + } + cbor_tag_set_item(res, item); + return res; +} diff --git a/source/external/libcbor/cbor/tags.h b/source/external/libcbor/cbor/tags.h new file mode 100644 index 000000000..a7365df10 --- /dev/null +++ b/source/external/libcbor/cbor/tags.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIBCBOR_TAGS_H +#define LIBCBOR_TAGS_H + +#include "cbor/cbor_export.h" +#include "cbor/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * ============================================================================ + * Tag manipulation + * ============================================================================ + */ + +/** Create a new tag + * + * @param value The tag value. Please consult the tag repository + * @return Reference to the new tag item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_tag(uint64_t value); + +/** Get the tagged item + * + * @param item A tag + * @return Reference to the tagged item + * + * Increases the reference count of the underlying item. The returned reference + * must be released using #cbor_decref. + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_tag_item(const cbor_item_t *item); + +/** Get tag value + * + * @param item A tag + * @return The tag value. Please consult the tag repository + */ +_CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_tag_value(const cbor_item_t *item); + +/** Set the tagged item + * + * @param item A tag + * @param tagged_item The item to tag. Its reference count will be increased + * by one. + */ +CBOR_EXPORT void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item); + +/** Build a new tag + * + * @param item The item to tag. Its reference count will be increased by + * one. + * @param value Tag value + * @return Reference to the new tag item. The item's reference count is + * initialized to one. + * @return `NULL` if memory allocation fails + */ +_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_tag(uint64_t value, + cbor_item_t *item); + +#ifdef __cplusplus +} +#endif + +#endif // LIBCBOR_TAGS_H diff --git a/source/file.c b/source/file.c index 01eb0a6af..504e547f5 100644 --- a/source/file.c +++ b/source/file.c @@ -11,6 +11,16 @@ #include +/* For "special files", the OS often lies about size. + * For example, on Amazon Linux 2: + * /proc/cpuinfo: size is 0, but contents are several KB of data. + * /sys/devices/virtual/dmi/id/product_name: size is 4096, but contents are "c5.2xlarge" + * + * Therefore, we may need to grow the buffer as we read until EOF. + * This is the min/max step size for growth. */ +#define MIN_BUFFER_GROWTH_READING_FILES 32 +#define MAX_BUFFER_GROWTH_READING_FILES 4096 + FILE *aws_fopen(const char *file_path, const char *mode) { if (!file_path || strlen(file_path) == 0) { AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to open file. path is empty"); @@ -34,55 +44,117 @@ FILE *aws_fopen(const char *file_path, const char *mode) { return file; } -int aws_byte_buf_init_from_file(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename) { +/* Helper function used by aws_byte_buf_init_from_file() and aws_byte_buf_init_from_file_with_size_hint() */ +static int s_byte_buf_init_from_file_impl( + struct aws_byte_buf *out_buf, + struct aws_allocator *alloc, + const char *filename, + bool use_file_size_as_hint, + size_t size_hint) { AWS_ZERO_STRUCT(*out_buf); FILE *fp = aws_fopen(filename, "rb"); + if (fp == NULL) { + goto error; + } - if (fp) { - if (fseek(fp, 0L, SEEK_END)) { - int errno_value = errno; /* Always cache errno before potential side-effect */ - AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to seek file %s with errno %d", filename, errno_value); - fclose(fp); - return aws_translate_and_raise_io_error(errno_value); + if (use_file_size_as_hint) { + int64_t len64 = 0; + if (aws_file_get_length(fp, &len64)) { + AWS_LOGF_ERROR( + AWS_LS_COMMON_IO, + "static: Failed to get file length. file:'%s' error:%s", + filename, + aws_error_name(aws_last_error())); + goto error; + } + + if (len64 >= SIZE_MAX) { + aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + AWS_LOGF_ERROR( + AWS_LS_COMMON_IO, + "static: File too large to read into memory. file:'%s' error:%s", + filename, + aws_error_name(aws_last_error())); + goto error; } - size_t allocation_size = (size_t)ftell(fp) + 1; - /* Tell the user that we allocate here and if success they're responsible for the free. */ - if (aws_byte_buf_init(out_buf, alloc, allocation_size)) { - fclose(fp); - return AWS_OP_ERR; + /* Leave space for null terminator at end of buffer */ + size_hint = (size_t)len64 + 1; + } + + aws_byte_buf_init(out_buf, alloc, size_hint); + + /* Read in a loop until we hit EOF */ + while (true) { + /* Expand buffer if necessary (at a reasonable rate) */ + if (out_buf->len == out_buf->capacity) { + size_t additional_capacity = out_buf->capacity; + additional_capacity = aws_max_size(MIN_BUFFER_GROWTH_READING_FILES, additional_capacity); + additional_capacity = aws_min_size(MAX_BUFFER_GROWTH_READING_FILES, additional_capacity); + if (aws_byte_buf_reserve_relative(out_buf, additional_capacity)) { + AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to grow buffer for file:'%s'", filename); + goto error; + } } - /* Ensure compatibility with null-terminated APIs, but don't consider - * the null terminator part of the length of the payload */ - out_buf->len = out_buf->capacity - 1; - out_buf->buffer[out_buf->len] = 0; - - if (fseek(fp, 0L, SEEK_SET)) { - int errno_value = errno; /* Always cache errno before potential side-effect */ - AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to seek file %s with errno %d", filename, errno_value); - aws_byte_buf_clean_up(out_buf); - fclose(fp); - return aws_translate_and_raise_io_error(errno_value); + size_t space_available = out_buf->capacity - out_buf->len; + size_t bytes_read = fread(out_buf->buffer + out_buf->len, 1, space_available, fp); + out_buf->len += bytes_read; + + /* If EOF, we're done! */ + if (feof(fp)) { + break; } - size_t read = fread(out_buf->buffer, 1, out_buf->len, fp); - int errno_cpy = errno; /* Always cache errno before potential side-effect */ - fclose(fp); - if (read < out_buf->len) { - AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to read file %s with errno %d", filename, errno_cpy); - aws_secure_zero(out_buf->buffer, out_buf->len); - aws_byte_buf_clean_up(out_buf); - return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); + /* If no EOF but we read 0 bytes, there's been an error or at least we need + * to treat it like one because we can't just infinitely loop. */ + if (bytes_read == 0) { + int errno_value = ferror(fp) ? errno : 0; /* Always cache errno before potential side-effect */ + aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_READ_FAILURE); + AWS_LOGF_ERROR( + AWS_LS_COMMON_IO, + "static: Failed reading file:'%s' errno:%d aws-error:%s", + filename, + errno_value, + aws_error_name(aws_last_error())); + goto error; } + } - return AWS_OP_SUCCESS; + /* A null terminator is appended, but is not included as part of the length field. */ + if (out_buf->len == out_buf->capacity) { + if (aws_byte_buf_reserve_relative(out_buf, 1)) { + AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to grow buffer for file:'%s'", filename); + goto error; + } } + out_buf->buffer[out_buf->len] = 0; + fclose(fp); + return AWS_OP_SUCCESS; + +error: + if (fp) { + fclose(fp); + } + aws_byte_buf_clean_up_secure(out_buf); return AWS_OP_ERR; } +int aws_byte_buf_init_from_file(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename) { + return s_byte_buf_init_from_file_impl(out_buf, alloc, filename, true /*use_file_size_as_hint*/, 0 /*size_hint*/); +} + +int aws_byte_buf_init_from_file_with_size_hint( + struct aws_byte_buf *out_buf, + struct aws_allocator *alloc, + const char *filename, + size_t size_hint) { + + return s_byte_buf_init_from_file_impl(out_buf, alloc, filename, false /*use_file_size_as_hint*/, size_hint); +} + bool aws_is_any_directory_separator(char value) { return value == '\\' || value == '/'; } diff --git a/source/hash_table.c b/source/hash_table.c index 88926e48f..bd5d04a3f 100644 --- a/source/hash_table.c +++ b/source/hash_table.c @@ -973,7 +973,7 @@ uint64_t aws_hash_byte_cursor_ptr(const void *item) { /* first digits of pi in hex */ uint32_t b = 0x3243F6A8, c = 0x885A308D; hashlittle2(cur->ptr, cur->len, &c, &b); - AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_byte_cursor_is_valid(cur)); + AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_byte_cursor_is_valid(cur)); /* NOLINT */ } uint64_t aws_hash_ptr(const void *item) { @@ -1104,3 +1104,11 @@ int hash_table_state_required_bytes(size_t size, size_t *required_bytes) { return AWS_OP_SUCCESS; } + +uint64_t aws_hash_uint64_t_by_identity(const void *item) { + return *(uint64_t *)item; +} + +bool aws_hash_compare_uint64_t_eq(const void *a, const void *b) { + return *(uint64_t *)a == *(uint64_t *)b; +} diff --git a/source/host_utils.c b/source/host_utils.c new file mode 100644 index 000000000..f20f3dd5e --- /dev/null +++ b/source/host_utils.c @@ -0,0 +1,114 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include +#include +#include + +#ifdef _MSC_VER /* Disable sscanf warnings on windows. */ +# pragma warning(disable : 4204) +# pragma warning(disable : 4706) +# pragma warning(disable : 4996) +#endif + +/* 4 octets of 3 chars max + 3 separators + null terminator */ +#define AWS_IPV4_STR_LEN 16 +#define IP_CHAR_FMT "%03" SCNu16 + +static bool s_is_ipv6_char(uint8_t value) { + return aws_isxdigit(value) || value == ':'; +} + +static bool s_ends_with(struct aws_byte_cursor cur, uint8_t ch) { + return cur.len > 0 && cur.ptr[cur.len - 1] == ch; +} + +bool aws_host_utils_is_ipv4(struct aws_byte_cursor host) { + if (host.len > AWS_IPV4_STR_LEN - 1) { + return false; + } + + char copy[AWS_IPV4_STR_LEN] = {0}; + memcpy(copy, host.ptr, host.len); + + uint16_t octet[4] = {0}; + char remainder[2] = {0}; + if (4 != sscanf( + copy, + IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "%1s", + &octet[0], + &octet[1], + &octet[2], + &octet[3], + remainder)) { + return false; + } + + for (size_t i = 0; i < 4; ++i) { + if (octet[i] > 255) { + return false; + } + } + + return true; +} + +/* actual encoding is %25, but % is omitted for simplicity, since split removes it */ +static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("25"); +/* + * IPv6 format: + * 8 groups of 4 hex chars separated by colons (:) + * leading 0s in each group can be skipped + * 2 or more consecutive zero groups can be replaced by double colon (::), + * but only once. + * ipv6 literal can be scoped by to zone by appending % followed by zone name + * ( does not look like there is length reqs on zone name length. this + * implementation enforces that its > 1 ) + * ipv6 can be embedded in url, in which case % must be uri encoded as %25. + * Implementation is fairly trivial and just iterates through the string + * keeping track of the spec above. + */ +bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) { + if (host.len == 0) { + return false; + } + + struct aws_byte_cursor substr = {0}; + /* first split is required ipv6 part */ + bool is_split = aws_byte_cursor_next_split(&host, '%', &substr); + AWS_ASSERT(is_split); /* function is guaranteed to return at least one split */ + + if (!is_split || substr.len == 0 || s_ends_with(substr, ':') || + !aws_byte_cursor_satisfies_pred(&substr, s_is_ipv6_char)) { + return false; + } + + uint8_t group_count = 0; + bool has_double_colon = false; + struct aws_byte_cursor group = {0}; + while (aws_byte_cursor_next_split(&substr, ':', &group)) { + ++group_count; + + if (group_count > 8 || /* too many groups */ + group.len > 4 || /* too many chars in group */ + (has_double_colon && group.len == 0 && group_count > 2)) { /* only one double colon allowed */ + return false; + } + + has_double_colon = has_double_colon || group.len == 0; + } + + /* second split is optional zone part */ + if (aws_byte_cursor_next_split(&host, '%', &substr)) { + if ((is_uri_encoded && + (substr.len < 3 || + !aws_byte_cursor_starts_with(&substr, &s_percent_uri_enc))) || /* encoding for % + 1 extra char */ + (!is_uri_encoded && substr.len == 0) || /* at least 1 char */ + !aws_byte_cursor_satisfies_pred(&substr, aws_isalnum)) { + return false; + } + } + + return has_double_colon ? group_count < 7 : group_count == 8; +} diff --git a/source/json.c b/source/json.c index 0131ea116..28524a88c 100644 --- a/source/json.c +++ b/source/json.c @@ -7,9 +7,9 @@ #include #include -#include +#include -#include +#include "external/cJSON.h" static struct aws_allocator *s_aws_json_module_allocator = NULL; static bool s_aws_json_module_initialized = false; @@ -397,7 +397,7 @@ int aws_byte_buf_append_json_string(const struct aws_json_value *value, struct a char *tmp = cJSON_PrintUnformatted(cjson); if (tmp == NULL) { - return AWS_OP_ERR; + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); } // Append the text to the byte buffer @@ -415,7 +415,7 @@ int aws_byte_buf_append_json_string_formatted(const struct aws_json_value *value char *tmp = cJSON_Print(cjson); if (tmp == NULL) { - return AWS_OP_ERR; + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); } // Append the text to the byte buffer diff --git a/source/linux/system_info.c b/source/linux/system_info.c new file mode 100644 index 000000000..2d9c5a120 --- /dev/null +++ b/source/linux/system_info.c @@ -0,0 +1,24 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include +#include + +int aws_system_environment_load_platform_impl(struct aws_system_environment *env) { + /* provide size_hint when reading "special files", since some platforms mis-report these files' size as 4KB */ + aws_byte_buf_init_from_file_with_size_hint( + &env->virtualization_vendor, env->allocator, "/sys/devices/virtual/dmi/id/sys_vendor", 32 /*size_hint*/); + + /* whether this one works depends on if this is a sysfs filesystem. If it fails, it will just be empty + * and these APIs are a best effort at the moment. We can add fallbacks as the loaders get more complicated. */ + aws_byte_buf_init_from_file_with_size_hint( + &env->product_name, env->allocator, "/sys/devices/virtual/dmi/id/product_name", 32 /*size_hint*/); + + return AWS_OP_SUCCESS; +} + +void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) { + aws_byte_buf_clean_up(&env->virtualization_vendor); + aws_byte_buf_clean_up(&env->product_name); +} diff --git a/source/log_formatter.c b/source/log_formatter.c index d4be0c0c6..b6e74e407 100644 --- a/source/log_formatter.c +++ b/source/log_formatter.c @@ -20,15 +20,17 @@ # pragma warning(disable : 4204) /* non-constant aggregate initializer */ #endif -/* (max) strlen of "[]" */ -#define LOG_LEVEL_PREFIX_PADDING 7 +enum { + /* (max) strlen of "[]" */ + LOG_LEVEL_PREFIX_PADDING = 7, -/* (max) strlen of "[]" */ -#define THREAD_ID_PREFIX_PADDING 22 + /* (max) strlen of "[]" */ + THREAD_ID_PREFIX_PADDING = 22, -/* strlen of (user-content separator) " - " + "\n" + spaces between prefix fields + brackets around timestamp + 1 + - subject_name padding */ -#define MISC_PADDING 15 + /* strlen of (user-content separator) " - " + "\n" + spaces between prefix fields + brackets around timestamp + 1 + + subject_name padding */ + MISC_PADDING = 15, +}; #define MAX_LOG_LINE_PREFIX_SIZE \ (LOG_LEVEL_PREFIX_PADDING + THREAD_ID_PREFIX_PADDING + MISC_PADDING + AWS_DATE_TIME_STR_MAX_LEN) @@ -203,7 +205,7 @@ static int s_default_aws_log_formatter_format( struct aws_default_log_formatter_impl *impl = formatter->impl; if (formatted_output == NULL) { - return AWS_OP_ERR; + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); } /* diff --git a/source/log_writer.c b/source/log_writer.c index 5f5bc4f6f..9f5090627 100644 --- a/source/log_writer.c +++ b/source/log_writer.c @@ -27,8 +27,8 @@ static int s_aws_file_writer_write(struct aws_log_writer *writer, const struct a size_t length = output->len; if (fwrite(output->bytes, 1, length, impl->log_file) < length) { - int errno_value = errno; /* Always cache errno before potential side-effect */ - return aws_translate_and_raise_io_error(errno_value); + int errno_value = ferror(impl->log_file) ? errno : 0; /* Always cache errno before potential side-effect */ + return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_WRITE_FAILURE); } return AWS_OP_SUCCESS; diff --git a/source/logging.c b/source/logging.c index fdc29576d..46f5e7dbe 100644 --- a/source/logging.c +++ b/source/logging.c @@ -337,11 +337,11 @@ int aws_thread_id_t_to_string(aws_thread_id_t thread_id, char *buffer, size_t bu unsigned char c = bytes[i - 1]; int written = snprintf(buffer + current_index, bufsz - current_index, "%02x", c); if (written < 0) { - return AWS_OP_ERR; + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); } current_index += written; if (bufsz <= current_index) { - return AWS_OP_ERR; + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); } } return AWS_OP_SUCCESS; @@ -454,7 +454,7 @@ static enum aws_log_level s_noalloc_stderr_logger_get_log_level(struct aws_logge return (enum aws_log_level)aws_atomic_load_int(&impl->level); } -#define MAXIMUM_NO_ALLOC_LOG_LINE_SIZE 8192 +enum { MAXIMUM_NO_ALLOC_LOG_LINE_SIZE = 8192 }; static int s_noalloc_stderr_logger_log( struct aws_logger *logger, @@ -502,8 +502,8 @@ static int s_noalloc_stderr_logger_log( int write_result = AWS_OP_SUCCESS; if (fwrite(format_buffer, 1, format_data.amount_written, impl->file) < format_data.amount_written) { - int errno_value = errno; /* Always cache errno before potential side-effect */ - aws_translate_and_raise_io_error(errno_value); + int errno_value = ferror(impl->file) ? errno : 0; /* Always cache errno before potential side-effect */ + aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_WRITE_FAILURE); write_result = AWS_OP_ERR; } diff --git a/source/memtrace.c b/source/memtrace.c index 651fd9361..ee1f291d7 100644 --- a/source/memtrace.c +++ b/source/memtrace.c @@ -57,7 +57,7 @@ struct alloc_tracer { }; /* number of frames to skip in call stacks (s_alloc_tracer_track, and the vtable function) */ -#define FRAMES_TO_SKIP 2 +enum { FRAMES_TO_SKIP = 2 }; static void *s_trace_mem_acquire(struct aws_allocator *allocator, size_t size); static void s_trace_mem_release(struct aws_allocator *allocator, void *ptr); @@ -131,29 +131,43 @@ static void s_alloc_tracer_track(struct alloc_tracer *tracer, void *ptr, size_t aws_high_res_clock_get_ticks(&alloc->time); if (tracer->level == AWS_MEMTRACE_STACKS) { - /* capture stack frames, skip 2 for this function and the allocation vtable function */ + /* capture stack frames, + * skip 2 for this function and the allocation vtable function if we have a full stack trace + * and otherwise just capture what ever stack trace we got + */ AWS_VARIABLE_LENGTH_ARRAY(void *, stack_frames, (FRAMES_TO_SKIP + tracer->frames_per_stack)); size_t stack_depth = aws_backtrace(stack_frames, FRAMES_TO_SKIP + tracer->frames_per_stack); - if (stack_depth) { - /* hash the stack pointers */ - struct aws_byte_cursor stack_cursor = - aws_byte_cursor_from_array(stack_frames, stack_depth * sizeof(void *)); - uint64_t stack_id = aws_hash_byte_cursor_ptr(&stack_cursor); - alloc->stack = stack_id; /* associate the stack with the alloc */ - - aws_mutex_lock(&tracer->mutex); - struct aws_hash_element *item = NULL; - int was_created = 0; - AWS_FATAL_ASSERT( - AWS_OP_SUCCESS == - aws_hash_table_create(&tracer->stacks, (void *)(uintptr_t)stack_id, &item, &was_created)); - /* If this is a new stack, save it to the hash */ - if (was_created) { - struct stack_trace *stack = aws_mem_calloc( - aws_default_allocator(), - 1, - sizeof(struct stack_trace) + (sizeof(void *) * tracer->frames_per_stack)); - AWS_FATAL_ASSERT(stack); + AWS_FATAL_ASSERT(stack_depth > 0); + + /* hash the stack pointers */ + struct aws_byte_cursor stack_cursor = aws_byte_cursor_from_array(stack_frames, stack_depth * sizeof(void *)); + uint64_t stack_id = aws_hash_byte_cursor_ptr(&stack_cursor); + alloc->stack = stack_id; /* associate the stack with the alloc */ + + aws_mutex_lock(&tracer->mutex); + struct aws_hash_element *item = NULL; + int was_created = 0; + AWS_FATAL_ASSERT( + AWS_OP_SUCCESS == aws_hash_table_create(&tracer->stacks, (void *)(uintptr_t)stack_id, &item, &was_created)); + /* If this is a new stack, save it to the hash */ + if (was_created) { + struct stack_trace *stack = aws_mem_calloc( + aws_default_allocator(), 1, sizeof(struct stack_trace) + (sizeof(void *) * tracer->frames_per_stack)); + AWS_FATAL_ASSERT(stack); + /** + * Optimizations can affect the number of frames we get and in pathological cases we can + * get fewer than FRAMES_TO_SKIP frames, but always at least 1 because code has to start somewhere. + * (looking at you gcc with -O3 on aarch64) + * With optimizations on we cannot trust the stack trace too much. + * Memtracer makes an assumption that stack trace will be available in all cases if stack trace api + * works. So in the pathological case of stack_depth <= FRAMES_TO_SKIP lets record all the frames we + * have, to at least have an anchor for where allocation is comming from, however inaccurate it is. + */ + if (stack_depth <= FRAMES_TO_SKIP) { + memcpy((void **)&stack->frames[0], &stack_frames[0], (stack_depth) * sizeof(void *)); + stack->depth = stack_depth; + item->value = stack; + } else { memcpy( (void **)&stack->frames[0], &stack_frames[FRAMES_TO_SKIP], @@ -161,8 +175,9 @@ static void s_alloc_tracer_track(struct alloc_tracer *tracer, void *ptr, size_t stack->depth = stack_depth - FRAMES_TO_SKIP; item->value = stack; } - aws_mutex_unlock(&tracer->mutex); } + + aws_mutex_unlock(&tracer->mutex); } aws_mutex_lock(&tracer->mutex); @@ -438,11 +453,18 @@ static void s_trace_mem_release(struct aws_allocator *allocator, void *ptr) { static void *s_trace_mem_realloc(struct aws_allocator *allocator, void *old_ptr, size_t old_size, size_t new_size) { struct alloc_tracer *tracer = allocator->impl; void *new_ptr = old_ptr; - if (aws_mem_realloc(tracer->traced_allocator, &new_ptr, old_size, new_size)) { - return NULL; - } + /* + * Careful with the ordering of state clean up here. + * Tracer keeps a hash table (alloc ptr as key) of meta info about each allocation. + * To avoid race conditions during realloc state update needs to be done in + * following order to avoid race conditions: + * - remove meta info (other threads cant reuse that key, cause ptr is still valid ) + * - realloc (cant fail, ptr might remain the same) + * - add meta info for reallocated mem + */ s_alloc_tracer_untrack(tracer, old_ptr); + aws_mem_realloc(tracer->traced_allocator, &new_ptr, old_size, new_size); s_alloc_tracer_track(tracer, new_ptr, new_size); return new_ptr; diff --git a/source/platform_fallback_stubs/system_info.c b/source/platform_fallback_stubs/system_info.c new file mode 100644 index 000000000..2b81469a8 --- /dev/null +++ b/source/platform_fallback_stubs/system_info.c @@ -0,0 +1,21 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +#include + +int aws_system_environment_load_platform_impl(struct aws_system_environment *env) { + (void)env; + AWS_LOGF_DEBUG( + AWS_LS_COMMON_GENERAL, + "id=%p: platform specific environment loading is not implemented for this platform.", + (void *)env); + + return AWS_OP_SUCCESS; +} + +void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) { + (void)env; +} diff --git a/source/posix/clock.c b/source/posix/clock.c index 90e213ea7..076f4d835 100644 --- a/source/posix/clock.c +++ b/source/posix/clock.c @@ -43,11 +43,11 @@ static int s_legacy_get_time(uint64_t *timestamp) { # if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT; -static int (*s_gettime_fn)(clockid_t __clock_id, struct timespec *__tp) = NULL; +static int (*s_gettime_fn)(clockid_t clock_id, struct timespec *tp) = NULL; static void s_do_osx_loads(void *user_data) { (void)user_data; - s_gettime_fn = (int (*)(clockid_t __clock_id, struct timespec * __tp)) dlsym(RTLD_DEFAULT, "clock_gettime"); + s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec *tp))dlsym(RTLD_DEFAULT, "clock_gettime"); } int aws_high_res_clock_get_ticks(uint64_t *timestamp) { diff --git a/source/posix/condition_variable.c b/source/posix/condition_variable.c index ca321c6bf..64edb45b8 100644 --- a/source/posix/condition_variable.c +++ b/source/posix/condition_variable.c @@ -93,12 +93,10 @@ int aws_condition_variable_wait_for( return AWS_OP_ERR; } - time_to_wait += current_sys_time; - struct timespec ts; uint64_t remainder = 0; - ts.tv_sec = - (time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder); + ts.tv_sec = (time_t)aws_timestamp_convert( + (uint64_t)(time_to_wait + current_sys_time), AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder); ts.tv_nsec = (long)remainder; int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts); diff --git a/source/posix/cross_process_lock.c b/source/posix/cross_process_lock.c new file mode 100644 index 000000000..1ef5d2b5f --- /dev/null +++ b/source/posix/cross_process_lock.c @@ -0,0 +1,141 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +struct aws_cross_process_lock { + struct aws_allocator *allocator; + int locked_fd; +}; + +struct aws_cross_process_lock *aws_cross_process_lock_try_acquire( + struct aws_allocator *allocator, + struct aws_byte_cursor instance_nonce) { + + /* validate we don't have a directory slash. */ + struct aws_byte_cursor to_find = aws_byte_cursor_from_c_str("/"); + struct aws_byte_cursor found; + AWS_ZERO_STRUCT(found); + if (aws_byte_cursor_find_exact(&instance_nonce, &to_find, &found) != AWS_OP_ERR && + aws_last_error() != AWS_ERROR_STRING_MATCH_NOT_FOUND) { + AWS_LOGF_ERROR( + AWS_LS_COMMON_GENERAL, + "static: Lock " PRInSTR "creation has illegal character /", + AWS_BYTE_CURSOR_PRI(instance_nonce)); + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + return NULL; + } + + /* + * The unix standard says /tmp has to be there and be writable. However, while it may be tempting to just use the + * /tmp/ directory, it often has the sticky bit set which would prevent a subprocess from being able to call open + * with create on the file. The solution is simple, just write it to a subdirectory inside + * /tmp and override umask via. chmod of 0777. + */ + struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("/tmp/aws_crt_cross_process_lock/"); + struct aws_string *path_to_create = aws_string_new_from_cursor(allocator, &path_prefix); + + /* It's probably there already and we don't care if it is. */ + if (!aws_directory_exists(path_to_create)) { + /* if this call fails just let it fail on open below. */ + aws_directory_create(path_to_create); + /* bypass umask by setting the perms we actually requested */ + chmod(aws_string_c_str(path_to_create), S_IRWXU | S_IRWXG | S_IRWXO); + } + aws_string_destroy(path_to_create); + + struct aws_byte_cursor path_suffix = aws_byte_cursor_from_c_str(".lock"); + + struct aws_byte_buf nonce_buf; + aws_byte_buf_init_copy_from_cursor(&nonce_buf, allocator, path_prefix); + aws_byte_buf_append_dynamic(&nonce_buf, &instance_nonce); + aws_byte_buf_append_dynamic(&nonce_buf, &path_suffix); + aws_byte_buf_append_null_terminator(&nonce_buf); + + struct aws_cross_process_lock *instance_lock = NULL; + + errno = 0; + int fd = open((const char *)nonce_buf.buffer, O_CREAT | O_RDWR, 0666); + if (fd < 0) { + AWS_LOGF_DEBUG( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s failed to open with errno %d", + (const char *)nonce_buf.buffer, + errno); + + aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED); + + if (aws_last_error() == AWS_ERROR_NO_PERMISSION) { + AWS_LOGF_DEBUG( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s couldn't be opened due to file ownership permissions. Attempting to open as read " + "only", + (const char *)nonce_buf.buffer); + + errno = 0; + fd = open((const char *)nonce_buf.buffer, O_RDONLY); + + if (fd < 0) { + AWS_LOGF_ERROR( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s failed to open with read-only permissions with errno %d", + (const char *)nonce_buf.buffer, + errno); + aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED); + goto cleanup; + } + } else { + AWS_LOGF_ERROR( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s failed to open. The lock cannot be acquired.", + (const char *)nonce_buf.buffer); + goto cleanup; + } + } + + if (flock(fd, LOCK_EX | LOCK_NB) == -1) { + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s already acquired by another instance", + (const char *)nonce_buf.buffer); + close(fd); + aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER); + goto cleanup; + } + + instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock)); + instance_lock->locked_fd = fd; + instance_lock->allocator = allocator; + + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "static: Lock file %s acquired by this instance with fd %d", + (const char *)nonce_buf.buffer, + fd); + +cleanup: + aws_byte_buf_clean_up(&nonce_buf); + + return instance_lock; +} + +void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) { + if (instance_lock) { + flock(instance_lock->locked_fd, LOCK_UN); + close(instance_lock->locked_fd); + AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock file released for fd %d", instance_lock->locked_fd); + aws_mem_release(instance_lock->allocator, instance_lock); + } +} diff --git a/source/posix/device_random.c b/source/posix/device_random.c index 23ab1cd7b..53f063ddf 100644 --- a/source/posix/device_random.c +++ b/source/posix/device_random.c @@ -51,7 +51,8 @@ int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) { /* read() can fail if N is too large (e.g. x64 macos fails if N > INT32_MAX), * so work in reasonably sized chunks. */ while (n > 0) { - size_t capped_n = aws_min_size(n, 1024 * 1024 * 1024 * 1 /* 1GiB */); + size_t capped_n = aws_min_size( + n, 1024 * 1024 * 1024 * 1 /* 1GiB */); /* NOLINT(bugprone-implicit-widening-of-multiplication-result) */ ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, capped_n); diff --git a/source/posix/file.c b/source/posix/file.c index f8dbbd7e1..868112955 100644 --- a/source/posix/file.c +++ b/source/posix/file.c @@ -18,7 +18,7 @@ FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string FILE *f = fopen(aws_string_c_str(file_path), aws_string_c_str(mode)); if (!f) { int errno_cpy = errno; /* Always cache errno before potential side-effect */ - aws_translate_and_raise_io_error(errno_cpy); + aws_translate_and_raise_io_error_or(errno_cpy, AWS_ERROR_FILE_OPEN_FAILURE); AWS_LOGF_ERROR( AWS_LS_COMMON_IO, "static: Failed to open file. path:'%s' mode:'%s' errno:%d aws-error:%d(%s)", @@ -285,7 +285,7 @@ int aws_fseek(FILE *file, int64_t offset, int whence) { int errno_value = errno; /* Always cache errno before potential side-effect */ if (result != 0) { - return aws_translate_and_raise_io_error(errno_value); + return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_STREAM_UNSEEKABLE); } return AWS_OP_SUCCESS; diff --git a/source/posix/mutex.c b/source/posix/mutex.c index 2cbf2db66..b5c865700 100644 --- a/source/posix/mutex.c +++ b/source/posix/mutex.c @@ -23,9 +23,11 @@ int aws_mutex_init(struct aws_mutex *mutex) { int return_code = AWS_OP_SUCCESS; if (!err_code) { - if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) || - (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) { - + err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); + if (!err_code) { + err_code = pthread_mutex_init(&mutex->mutex_handle, &attr); + } + if (err_code) { return_code = aws_private_convert_and_raise_error_code(err_code); } pthread_mutexattr_destroy(&attr); diff --git a/source/posix/system_info.c b/source/posix/system_info.c index 54bb502d8..b5a0edc87 100644 --- a/source/posix/system_info.c +++ b/source/posix/system_info.c @@ -50,10 +50,10 @@ size_t aws_system_info_processor_count(void) { uint16_t aws_get_cpu_group_count(void) { if (g_numa_num_configured_nodes_ptr) { - return (uint16_t)g_numa_num_configured_nodes_ptr(); + return aws_max_u16(1, (uint16_t)g_numa_num_configured_nodes_ptr()); } - return 1u; + return 1U; } size_t aws_get_cpu_count_for_group(uint16_t group_idx) { @@ -242,7 +242,7 @@ int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info * if (function_len >= (sizeof(frame->function) - 1)) { function_len = sizeof(frame->function) - 1; } - strncpy(frame->function, function_start, function_end - function_start); + strncpy(frame->function, function_start, function_len); /* find base addr for library/exe */ Dl_info addr_info; diff --git a/source/posix/system_resource_utils.c b/source/posix/system_resource_utils.c new file mode 100644 index 000000000..68165072b --- /dev/null +++ b/source/posix/system_resource_utils.c @@ -0,0 +1,32 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include + +#include + +int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage) { + AWS_PRECONDITION(memory_usage); + + AWS_ZERO_STRUCT(*memory_usage); + struct rusage usage; + + if (getrusage(RUSAGE_SELF, &usage)) { + return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); + } + +#if defined(AWS_OS_APPLE) + /* + * For some reason Apple switched to reporting this in bytes instead of KB + * around MacOS 10.6. + * Make it back to KB. Result might be slightly off due to rounding. + */ + memory_usage->maxrss = usage.ru_maxrss / 1024; +#else + memory_usage->maxrss = usage.ru_maxrss; +#endif + memory_usage->page_faults = usage.ru_majflt; + return AWS_OP_SUCCESS; +} diff --git a/source/posix/thread.c b/source/posix/thread.c index 57d48aa9c..34b5dbe94 100644 --- a/source/posix/thread.c +++ b/source/posix/thread.c @@ -4,7 +4,7 @@ */ #if !defined(__MACH__) -# define _GNU_SOURCE +# define _GNU_SOURCE /* NOLINT(bugprone-reserved-identifier) */ #endif #include @@ -275,6 +275,25 @@ int aws_thread_launch( if (attr_return) { goto cleanup; } + } else if (!options->stack_size) { + /** + * On some systems, the default stack size is too low (128KB on musl at the time of writing this), which can + * cause stack overflow when the dependency chain is long. Increase the stack size to at + * least 1MB, which is the default on Windows. + */ + size_t min_stack_size = (size_t)1 * 1024 * 1024; + size_t current_stack_size; + attr_return = pthread_attr_getstacksize(attributes_ptr, ¤t_stack_size); + if (attr_return) { + goto cleanup; + } + + if (current_stack_size < min_stack_size) { + attr_return = pthread_attr_setstacksize(attributes_ptr, min_stack_size); + if (attr_return) { + goto cleanup; + } + } } /* AFAIK you can't set thread affinity on apple platforms, and it doesn't really matter since all memory @@ -296,9 +315,9 @@ int aws_thread_launch( attr_return = pthread_attr_setaffinity_np(attributes_ptr, sizeof(cpuset), &cpuset); if (attr_return) { - AWS_LOGF_ERROR( + AWS_LOGF_WARN( AWS_LS_COMMON_THREAD, - "id=%p: pthread_attr_setaffinity_np() failed with %d.", + "id=%p: pthread_attr_setaffinity_np() failed with %d. Continuing without cpu affinity", (void *)thread, attr_return); goto cleanup; @@ -382,7 +401,20 @@ int aws_thread_launch( if (attr_return) { s_thread_wrapper_destroy(wrapper); - + if (options && options->cpu_id >= 0) { + /* + * `pthread_create` can fail with an `EINVAL` error or `EDEADLK` on freebasd if the `cpu_id` is + * restricted/invalid. Since the pinning to a particular `cpu_id` is supposed to be best-effort, try to + * launch a thread again without pinning to a specific cpu_id. + */ + AWS_LOGF_INFO( + AWS_LS_COMMON_THREAD, + "id=%p: Attempting to launch the thread again without pinning to a cpu_id", + (void *)thread); + struct aws_thread_options new_options = *options; + new_options.cpu_id = -1; + return aws_thread_launch(thread, func, arg, &new_options); + } switch (attr_return) { case EINVAL: return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS); diff --git a/source/priority_queue.c b/source/priority_queue.c index 86a91feb3..2157ffd0a 100644 --- a/source/priority_queue.c +++ b/source/priority_queue.c @@ -7,7 +7,7 @@ #include -#define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0) +#define PARENT_OF(index) (((index) & 1) ? (index) >> 1 : (index) > 1 ? ((index) - 2) >> 1 : 0) #define LEFT_OF(index) (((index) << 1) + 1) #define RIGHT_OF(index) (((index) << 1) + 2) @@ -400,3 +400,27 @@ size_t aws_priority_queue_size(const struct aws_priority_queue *queue) { size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) { return aws_array_list_capacity(&queue->container); } + +void aws_priority_queue_clear(struct aws_priority_queue *queue) { + AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); + size_t backpointer_count = aws_array_list_length(&queue->backpointers); + for (size_t i = 0; i < backpointer_count; ++i) { + struct aws_priority_queue_node *node = NULL; + aws_array_list_get_at(&queue->backpointers, &node, i); + if (node != NULL) { + node->current_index = SIZE_MAX; + } + } + + aws_array_list_clear(&queue->backpointers); + aws_array_list_clear(&queue->container); + AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); +} + +void aws_priority_queue_node_init(struct aws_priority_queue_node *node) { + node->current_index = SIZE_MAX; +} + +bool aws_priority_queue_node_is_in_queue(const struct aws_priority_queue_node *node) { + return node->current_index != SIZE_MAX; +} diff --git a/source/process_common.c b/source/process_common.c index ef432374b..24a25094b 100644 --- a/source/process_common.c +++ b/source/process_common.c @@ -9,7 +9,7 @@ #include #include -#define MAX_BUFFER_SIZE (2048) +enum { MAX_BUFFER_SIZE = 2048 }; int aws_run_command_result_init(struct aws_allocator *allocator, struct aws_run_command_result *result) { if (!allocator || !result) { diff --git a/source/promise.c b/source/promise.c deleted file mode 100644 index 7c8572457..000000000 --- a/source/promise.c +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0. - */ - -#include -#include -#include -#include - -struct aws_promise { - struct aws_allocator *allocator; - struct aws_mutex mutex; - struct aws_condition_variable cv; - struct aws_ref_count rc; - bool complete; - int error_code; - void *value; - - /* destructor for value, will be invoked if the value is not taken */ - void (*dtor)(void *); -}; - -static void s_aws_promise_dtor(void *ptr) { - struct aws_promise *promise = ptr; - aws_condition_variable_clean_up(&promise->cv); - aws_mutex_clean_up(&promise->mutex); - if (promise->value && promise->dtor) { - promise->dtor(promise->value); - } - aws_mem_release(promise->allocator, promise); -} - -struct aws_promise *aws_promise_new(struct aws_allocator *allocator) { - struct aws_promise *promise = aws_mem_calloc(allocator, 1, sizeof(struct aws_promise)); - promise->allocator = allocator; - aws_ref_count_init(&promise->rc, promise, s_aws_promise_dtor); - aws_mutex_init(&promise->mutex); - aws_condition_variable_init(&promise->cv); - return promise; -} - -struct aws_promise *aws_promise_acquire(struct aws_promise *promise) { - aws_ref_count_acquire(&promise->rc); - return promise; -} - -void aws_promise_release(struct aws_promise *promise) { - aws_ref_count_release(&promise->rc); -} - -static bool s_promise_completed(void *user_data) { - struct aws_promise *promise = user_data; - return promise->complete; -} - -void aws_promise_wait(struct aws_promise *promise) { - aws_mutex_lock(&promise->mutex); - aws_condition_variable_wait_pred(&promise->cv, &promise->mutex, s_promise_completed, promise); - aws_mutex_unlock(&promise->mutex); -} - -bool aws_promise_wait_for(struct aws_promise *promise, size_t nanoseconds) { - aws_mutex_lock(&promise->mutex); - aws_condition_variable_wait_for_pred( - &promise->cv, &promise->mutex, (int64_t)nanoseconds, s_promise_completed, promise); - const bool complete = promise->complete; - aws_mutex_unlock(&promise->mutex); - return complete; -} - -bool aws_promise_is_complete(struct aws_promise *promise) { - aws_mutex_lock(&promise->mutex); - const bool complete = promise->complete; - aws_mutex_unlock(&promise->mutex); - return complete; -} - -void aws_promise_complete(struct aws_promise *promise, void *value, void (*dtor)(void *)) { - aws_mutex_lock(&promise->mutex); - AWS_FATAL_ASSERT(!promise->complete && "aws_promise_complete: cannot complete a promise more than once"); - promise->value = value; - promise->dtor = dtor; - promise->complete = true; - /* Notify before unlocking to prevent a race condition where the recipient spuriously - * awakens after the unlock, sees a fulfilled promise, and attempts to free its resources - * before the notification has actually occured. */ - aws_condition_variable_notify_all(&promise->cv); - aws_mutex_unlock(&promise->mutex); -} - -void aws_promise_fail(struct aws_promise *promise, int error_code) { - AWS_FATAL_ASSERT(error_code != 0 && "aws_promise_fail: cannot fail a promise with a 0 error_code"); - aws_mutex_lock(&promise->mutex); - AWS_FATAL_ASSERT(!promise->complete && "aws_promise_fail: cannot complete a promise more than once"); - promise->error_code = error_code; - promise->complete = true; - aws_condition_variable_notify_all(&promise->cv); - aws_mutex_unlock(&promise->mutex); -} - -int aws_promise_error_code(struct aws_promise *promise) { - AWS_FATAL_ASSERT(aws_promise_is_complete(promise)); - return promise->error_code; -} - -void *aws_promise_value(struct aws_promise *promise) { - AWS_FATAL_ASSERT(aws_promise_is_complete(promise)); - return promise->value; -} - -void *aws_promise_take_value(struct aws_promise *promise) { - AWS_FATAL_ASSERT(aws_promise_is_complete(promise)); - void *value = promise->value; - promise->value = NULL; - promise->dtor = NULL; - return value; -} diff --git a/source/string.c b/source/string.c index a3d2c204e..2fd791230 100644 --- a/source/string.c +++ b/source/string.c @@ -189,11 +189,8 @@ struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, co struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size; - if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { - return NULL; - } - struct aws_string *str = aws_mem_acquire(allocator, malloc_size); + + struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len + 1])); if (!str) { return NULL; } diff --git a/source/system_info.c b/source/system_info.c new file mode 100644 index 000000000..4b721f63a --- /dev/null +++ b/source/system_info.c @@ -0,0 +1,80 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +#include + +void s_destroy_env(void *arg) { + struct aws_system_environment *env = arg; + + if (env) { + aws_system_environment_destroy_platform_impl(env); + aws_mem_release(env->allocator, env); + } +} + +struct aws_system_environment *aws_system_environment_load(struct aws_allocator *allocator) { + struct aws_system_environment *env = aws_mem_calloc(allocator, 1, sizeof(struct aws_system_environment)); + env->allocator = allocator; + aws_ref_count_init(&env->ref_count, env, s_destroy_env); + + if (aws_system_environment_load_platform_impl(env)) { + AWS_LOGF_ERROR( + AWS_LS_COMMON_GENERAL, + "id=%p: failed to load system environment with error %s.", + (void *)env, + aws_error_debug_str(aws_last_error())); + goto error; + } + + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "id=%p: virtualization vendor detected as \"" PRInSTR "\"", + (void *)env, + AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(env))); + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "id=%p: virtualization product name detected as \"" PRInSTR " \"", + (void *)env, + AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(env))); + + env->os = aws_get_platform_build_os(); + env->cpu_count = aws_system_info_processor_count(); + env->cpu_group_count = aws_get_cpu_group_count(); + + return env; +error: + s_destroy_env(env); + return NULL; +} + +struct aws_system_environment *aws_system_environment_acquire(struct aws_system_environment *env) { + aws_ref_count_acquire(&env->ref_count); + return env; +} + +void aws_system_environment_release(struct aws_system_environment *env) { + aws_ref_count_release(&env->ref_count); +} + +struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(const struct aws_system_environment *env) { + struct aws_byte_cursor vendor_string = aws_byte_cursor_from_buf(&env->virtualization_vendor); + return aws_byte_cursor_trim_pred(&vendor_string, aws_char_is_space); +} + +struct aws_byte_cursor aws_system_environment_get_virtualization_product_name( + const struct aws_system_environment *env) { + struct aws_byte_cursor product_name_str = aws_byte_cursor_from_buf(&env->product_name); + return aws_byte_cursor_trim_pred(&product_name_str, aws_char_is_space); +} + +size_t aws_system_environment_get_processor_count(struct aws_system_environment *env) { + return env->cpu_count; +} + +AWS_COMMON_API +size_t aws_system_environment_get_cpu_group_count(const struct aws_system_environment *env) { + return env->cpu_group_count; +} diff --git a/source/task_scheduler.c b/source/task_scheduler.c index 4467b1249..9076ae249 100644 --- a/source/task_scheduler.c +++ b/source/task_scheduler.c @@ -33,7 +33,7 @@ const char *aws_task_status_to_c_str(enum aws_task_status status) { void aws_task_run(struct aws_task *task, enum aws_task_status status) { AWS_ASSERT(task->fn); - AWS_LOGF_DEBUG( + AWS_LOGF_TRACE( AWS_LS_COMMON_TASK_SCHEDULER, "id=%p: Running %s task with %s status", (void *)task, @@ -129,13 +129,13 @@ void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struc AWS_ASSERT(task); AWS_ASSERT(task->fn); - AWS_LOGF_DEBUG( + AWS_LOGF_TRACE( AWS_LS_COMMON_TASK_SCHEDULER, "id=%p: Scheduling %s task for immediate execution", (void *)task, task->type_tag); - task->priority_queue_node.current_index = SIZE_MAX; + aws_priority_queue_node_init(&task->priority_queue_node); aws_linked_list_node_reset(&task->node); task->timestamp = 0; @@ -152,7 +152,7 @@ void aws_task_scheduler_schedule_future( AWS_ASSERT(task); AWS_ASSERT(task->fn); - AWS_LOGF_DEBUG( + AWS_LOGF_TRACE( AWS_LS_COMMON_TASK_SCHEDULER, "id=%p: Scheduling %s task for future execution at time %" PRIu64, (void *)task, @@ -161,7 +161,7 @@ void aws_task_scheduler_schedule_future( task->timestamp = time_to_run; - task->priority_queue_node.current_index = SIZE_MAX; + aws_priority_queue_node_init(&task->priority_queue_node); aws_linked_list_node_reset(&task->node); int err = aws_priority_queue_push_ref(&scheduler->timed_queue, &task, &task->priority_queue_node); if (AWS_UNLIKELY(err)) { diff --git a/source/thread_scheduler.c b/source/thread_scheduler.c index 7999344b7..4948e3c5d 100644 --- a/source/thread_scheduler.c +++ b/source/thread_scheduler.c @@ -32,7 +32,7 @@ struct cancellation_node { static void s_destroy_callback(void *arg) { struct aws_thread_scheduler *scheduler = arg; - aws_atomic_store_int(&scheduler->should_exit, 1u); + aws_atomic_store_int(&scheduler->should_exit, 1U); aws_condition_variable_notify_all(&scheduler->thread_data.c_var); aws_thread_join(&scheduler->thread); aws_task_scheduler_clean_up(&scheduler->scheduler); @@ -138,7 +138,7 @@ struct aws_thread_scheduler *aws_thread_scheduler_new( } scheduler->allocator = allocator; - aws_atomic_init_int(&scheduler->should_exit, 0u); + aws_atomic_init_int(&scheduler->should_exit, 0U); aws_ref_count_init(&scheduler->ref_count, scheduler, s_destroy_callback); aws_linked_list_init(&scheduler->thread_data.scheduling_queue); aws_linked_list_init(&scheduler->thread_data.cancel_queue); @@ -182,7 +182,7 @@ void aws_thread_scheduler_schedule_future( aws_condition_variable_notify_one(&scheduler->thread_data.c_var); } void aws_thread_scheduler_schedule_now(struct aws_thread_scheduler *scheduler, struct aws_task *task) { - aws_thread_scheduler_schedule_future(scheduler, task, 0u); + aws_thread_scheduler_schedule_future(scheduler, task, 0U); } void aws_thread_scheduler_cancel_task(struct aws_thread_scheduler *scheduler, struct aws_task *task) { diff --git a/source/thread_shared.c b/source/thread_shared.c index cbceb428e..bd479a16e 100644 --- a/source/thread_shared.c +++ b/source/thread_shared.c @@ -106,7 +106,7 @@ int aws_thread_join_all_managed(void) { aws_condition_variable_wait_for_pred( &s_managed_thread_signal, &s_managed_thread_lock, - wait_ns, + (int64_t)wait_ns, s_one_or_fewer_managed_threads_unjoined, NULL); } else { diff --git a/source/uri.c b/source/uri.c index cefe57e87..d2d6f77a1 100644 --- a/source/uri.c +++ b/source/uri.c @@ -30,6 +30,9 @@ struct uri_parser { enum parser_state state; }; +/* strlen of UINT32_MAX "4294967295" is 10, plus 1 for '\0' */ +#define PORT_BUFFER_SIZE 11 + typedef void(parse_fn)(struct uri_parser *parser, struct aws_byte_cursor *str); static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str); @@ -101,8 +104,7 @@ int aws_uri_init_from_builder_options( buffer_size += options->host_name.len; if (options->port) { - /* max strlen of a 16 bit integer is 5 */ - buffer_size += 6; + buffer_size += PORT_BUFFER_SIZE; } buffer_size += options->path.len; @@ -142,8 +144,8 @@ int aws_uri_init_from_builder_options( struct aws_byte_cursor port_app = aws_byte_cursor_from_c_str(":"); if (options->port) { aws_byte_buf_append(&uri->uri_str, &port_app); - char port_arr[6] = {0}; - snprintf(port_arr, sizeof(port_arr), "%" PRIu16, options->port); + char port_arr[PORT_BUFFER_SIZE] = {0}; + snprintf(port_arr, sizeof(port_arr), "%" PRIu32, options->port); struct aws_byte_cursor port_csr = aws_byte_cursor_from_c_str(port_arr); aws_byte_buf_append(&uri->uri_str, &port_csr); } @@ -208,11 +210,11 @@ const struct aws_byte_cursor *aws_uri_host_name(const struct aws_uri *uri) { return &uri->host_name; } -uint16_t aws_uri_port(const struct aws_uri *uri) { +uint32_t aws_uri_port(const struct aws_uri *uri) { return uri->port; } -bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param) { +bool aws_query_string_next_param(struct aws_byte_cursor query_string, struct aws_uri_param *param) { /* If param is zeroed, then this is the first run. */ bool first_run = param->value.ptr == NULL; @@ -230,7 +232,7 @@ bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_p /* The do-while is to skip over any empty substrings */ do { - if (!aws_byte_cursor_next_split(&uri->query_string, '&', &substr)) { + if (!aws_byte_cursor_next_split(&query_string, '&', &substr)) { /* no more splits, done iterating */ return false; } @@ -252,10 +254,10 @@ bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_p return true; } -int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) { +int aws_query_string_params(struct aws_byte_cursor query_string_cursor, struct aws_array_list *out_params) { struct aws_uri_param param; AWS_ZERO_STRUCT(param); - while (aws_uri_query_string_next_param(uri, ¶m)) { + while (aws_query_string_next_param(query_string_cursor, ¶m)) { if (aws_array_list_push_back(out_params, ¶m)) { return AWS_OP_ERR; } @@ -264,6 +266,14 @@ int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list return AWS_OP_SUCCESS; } +bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param) { + return aws_query_string_next_param(uri->query_string, param); +} + +int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) { + return aws_query_string_params(uri->query_string, out_params); +} + static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str) { const uint8_t *location_of_colon = memchr(str->ptr, ':', str->len); @@ -354,7 +364,9 @@ static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor * IPv6 literals and only search for port delimiter after closing bracket.*/ const uint8_t *port_search_start = authority_parse_csr.ptr; size_t port_search_len = authority_parse_csr.len; + bool is_IPv6_literal = false; if (authority_parse_csr.len > 0 && authority_parse_csr.ptr[0] == '[') { + is_IPv6_literal = true; port_search_start = memchr(authority_parse_csr.ptr, ']', authority_parse_csr.len); if (!port_search_start) { parser->state = ERROR; @@ -365,43 +377,42 @@ static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor } const uint8_t *port_delim = memchr(port_search_start, ':', port_search_len); - + /* + * RFC-3986 section 3.2.2: A host identified by an IPv6 literal address is represented inside square + * brackets. + * Ignore the square brackets. + */ + parser->uri->host_name = authority_parse_csr; + if (is_IPv6_literal) { + aws_byte_cursor_advance(&parser->uri->host_name, 1); + parser->uri->host_name.len--; + } if (!port_delim) { parser->uri->port = 0; - parser->uri->host_name = authority_parse_csr; return; } - parser->uri->host_name.ptr = authority_parse_csr.ptr; - parser->uri->host_name.len = port_delim - authority_parse_csr.ptr; - - size_t port_len = authority_parse_csr.len - parser->uri->host_name.len - 1; + size_t host_name_length_correction = is_IPv6_literal ? 2 : 0; + parser->uri->host_name.len = port_delim - authority_parse_csr.ptr - host_name_length_correction; + size_t port_len = authority_parse_csr.len - parser->uri->host_name.len - 1 - host_name_length_correction; port_delim += 1; - for (size_t i = 0; i < port_len; ++i) { - if (!aws_isdigit(port_delim[i])) { + + uint64_t port_u64 = 0; + if (port_len > 0) { + struct aws_byte_cursor port_cursor = aws_byte_cursor_from_array(port_delim, port_len); + if (aws_byte_cursor_utf8_parse_u64(port_cursor, &port_u64)) { + parser->state = ERROR; + aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); + return; + } + if (port_u64 > UINT32_MAX) { parser->state = ERROR; aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); return; } } - if (port_len > 5) { - parser->state = ERROR; - aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); - return; - } - - /* why 6? because the port is a 16-bit unsigned integer*/ - char atoi_buf[6] = {0}; - memcpy(atoi_buf, port_delim, port_len); - int port_int = atoi(atoi_buf); - if (port_int > UINT16_MAX) { - parser->state = ERROR; - aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); - return; - } - - parser->uri->port = (uint16_t)port_int; + parser->uri->port = (uint32_t)port_u64; } } diff --git a/source/windows/clock.c b/source/windows/clock.c index b7b0b3820..11a5abb62 100644 --- a/source/windows/clock.c +++ b/source/windows/clock.c @@ -3,8 +3,8 @@ * SPDX-License-Identifier: Apache-2.0. */ -#include #include +#include static const uint64_t FILE_TIME_TO_NS = 100; static const uint64_t EC_TO_UNIX_EPOCH = 11644473600LL; diff --git a/source/windows/condition_variable.c b/source/windows/condition_variable.c index 5a989ead0..07d6a0cdd 100644 --- a/source/windows/condition_variable.c +++ b/source/windows/condition_variable.c @@ -8,7 +8,7 @@ #include #include -#include +#include #define AWSCV_TO_WINDOWS(pCV) (PCONDITION_VARIABLE) & (pCV)->condition_handle diff --git a/source/windows/cross_process_lock.c b/source/windows/cross_process_lock.c new file mode 100644 index 000000000..4a6abfb57 --- /dev/null +++ b/source/windows/cross_process_lock.c @@ -0,0 +1,93 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include + +#include +#include +#include +#include +#include + +struct aws_cross_process_lock { + struct aws_allocator *allocator; + HANDLE mutex; +}; + +struct aws_cross_process_lock *aws_cross_process_lock_try_acquire( + struct aws_allocator *allocator, + struct aws_byte_cursor instance_nonce) { + + /* validate we don't have a directory slash. */ + struct aws_byte_cursor to_find = aws_byte_cursor_from_c_str("\\"); + struct aws_byte_cursor found; + AWS_ZERO_STRUCT(found); + if (aws_byte_cursor_find_exact(&instance_nonce, &to_find, &found) != AWS_OP_ERR && + aws_last_error() != AWS_ERROR_STRING_MATCH_NOT_FOUND) { + AWS_LOGF_ERROR( + AWS_LS_COMMON_GENERAL, + "static: Lock " PRInSTR " creation has illegal character \\", + AWS_BYTE_CURSOR_PRI(instance_nonce)); + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + return NULL; + } + + struct aws_cross_process_lock *instance_lock = NULL; + + /* "Local\" prefix, per the docs, specifies user session scope (rather than "Global\" to the system). */ + struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("Local\\aws_crt_cross_process_lock/"); + struct aws_byte_buf nonce_buf; + aws_byte_buf_init_copy_from_cursor(&nonce_buf, allocator, path_prefix); + aws_byte_buf_append_dynamic(&nonce_buf, &instance_nonce); + aws_byte_buf_append_null_terminator(&nonce_buf); + + HANDLE mutex = CreateMutexA(NULL, FALSE, (LPCSTR)nonce_buf.buffer); + + if (!mutex) { + AWS_LOGF_WARN( + AWS_LS_COMMON_GENERAL, + "static: Lock %s creation failed with error %" PRIx32, + (const char *)nonce_buf.buffer, + GetLastError()); + aws_translate_and_raise_io_error_or(GetLastError(), AWS_ERROR_MUTEX_FAILED); + goto cleanup; + } + + /* from the docs: + * If the mutex is a named mutex and the object existed before this function call, the return value is a handle + * to the existing object, and the GetLastError function returns ERROR_ALREADY_EXISTS. */ + if (GetLastError() == ERROR_ALREADY_EXISTS) { + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "static: Lock %s is already acquired by another instance", + (const char *)nonce_buf.buffer); + CloseHandle(mutex); + aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER); + goto cleanup; + } + + instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock)); + instance_lock->mutex = mutex; + instance_lock->allocator = allocator; + + AWS_LOGF_TRACE( + AWS_LS_COMMON_GENERAL, + "static: Lock %s acquired by this instance with HANDLE %p", + (const char *)nonce_buf.buffer, + (void *)mutex); + +cleanup: + + /* we could do this once above but then we'd lose logging for the buffer. */ + aws_byte_buf_clean_up(&nonce_buf); + return instance_lock; +} + +void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) { + if (instance_lock) { + CloseHandle(instance_lock->mutex); + AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock released for handle %p", (void *)instance_lock->mutex); + aws_mem_release(instance_lock->allocator, instance_lock); + } +} diff --git a/source/windows/device_random.c b/source/windows/device_random.c index 7eb461c32..534fd3564 100644 --- a/source/windows/device_random.c +++ b/source/windows/device_random.c @@ -6,7 +6,8 @@ #include -#include +#include + #include int aws_device_random_buffer(struct aws_byte_buf *output) { diff --git a/source/windows/file.c b/source/windows/file.c index 47e874f07..b3dfdf92c 100644 --- a/source/windows/file.c +++ b/source/windows/file.c @@ -8,9 +8,9 @@ #include #include -#include #include #include +#include #include #include @@ -44,7 +44,7 @@ FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string aws_wstring_destroy(w_file_path); if (error) { - aws_translate_and_raise_io_error(error); + aws_translate_and_raise_io_error_or(error, AWS_ERROR_FILE_OPEN_FAILURE); AWS_LOGF_ERROR( AWS_LS_COMMON_IO, "static: Failed to open file. path:'%s' mode:'%s' errno:%d aws-error:%d(%s)", @@ -508,7 +508,7 @@ bool aws_path_exists(const struct aws_string *path) { int aws_fseek(FILE *file, int64_t offset, int whence) { if (_fseeki64(file, offset, whence)) { int errno_value = errno; /* Always cache errno before potential side-effect */ - return aws_translate_and_raise_io_error(errno_value); + return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_STREAM_UNSEEKABLE); } return AWS_OP_SUCCESS; diff --git a/source/windows/mutex.c b/source/windows/mutex.c index aedefd2af..75e7a23d1 100644 --- a/source/windows/mutex.c +++ b/source/windows/mutex.c @@ -6,7 +6,7 @@ #include #include -#include +#include /* Convert a string from a macro to a wide string */ #define WIDEN2(s) L## #s diff --git a/source/windows/rw_lock.c b/source/windows/rw_lock.c index 36cc81aa9..72a6e4378 100644 --- a/source/windows/rw_lock.c +++ b/source/windows/rw_lock.c @@ -6,7 +6,8 @@ #include #include -#include +#include + #include /* Convert a string from a macro to a wide string */ diff --git a/source/windows/system_info.c b/source/windows/system_info.c index ad0206ee1..a32c5110c 100644 --- a/source/windows/system_info.c +++ b/source/windows/system_info.c @@ -24,7 +24,7 @@ size_t aws_system_info_processor_count(void) { /* the next three functions need actual implementations before we can have proper numa alignment on windows. * For now leave them stubbed out. */ uint16_t aws_get_cpu_group_count(void) { - return 1u; + return 1U; } size_t aws_get_cpu_count_for_group(uint16_t group_idx) { diff --git a/source/windows/system_resource_utils.c b/source/windows/system_resource_utils.c new file mode 100644 index 000000000..618a9e25b --- /dev/null +++ b/source/windows/system_resource_utils.c @@ -0,0 +1,31 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include + +#include + +#include + +int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage) { + AWS_PRECONDITION(memory_usage); + + AWS_ZERO_STRUCT(*memory_usage); + HANDLE hProcess = GetCurrentProcess(); + + PROCESS_MEMORY_COUNTERS pmc; + + BOOL ret = GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)); + CloseHandle(hProcess); + + if (!ret) { + return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); + } + + memory_usage->maxrss = pmc.PeakWorkingSetSize; + memory_usage->page_faults = pmc.PageFaultCount; + + return AWS_OP_SUCCESS; +} diff --git a/source/windows/thread.c b/source/windows/thread.c index 9124afefb..6606c05be 100644 --- a/source/windows/thread.c +++ b/source/windows/thread.c @@ -10,7 +10,7 @@ #include #include -#include +#include #include @@ -21,6 +21,7 @@ static struct aws_thread_options s_default_options = { /* zero will make sure whatever the default for that version of windows is used. */ .stack_size = 0, + .cpu_id = -1, .join_strategy = AWS_TJS_MANUAL, }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f8d9a8d44..d3556c5f7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,6 +40,7 @@ add_test_case(aws_load_error_strings_test) add_test_case(aws_assume_compiles_test) add_test_case(thread_creation_join_test) +add_test_case(thread_creation_join_invalid_cpu_id_test) add_test_case(thread_atexit_test) add_test_case(test_managed_thread_join) add_test_case(test_managed_thread_join_timeout) @@ -94,6 +95,7 @@ add_test_case(priority_queue_remove_root_test) add_test_case(priority_queue_remove_leaf_test) add_test_case(priority_queue_remove_interior_sift_up_test) add_test_case(priority_queue_remove_interior_sift_down_test) +add_test_case(priority_queue_clear_backpointers_test) add_test_case(linked_list_push_back_pop_front) add_test_case(linked_list_push_front_pop_back) @@ -103,6 +105,7 @@ add_test_case(linked_list_reverse_iteration) add_test_case(linked_list_swap_contents) add_test_case(linked_list_move_all_back) add_test_case(linked_list_move_all_front) +add_test_case(linked_list_node_in_list) add_test_case(hex_encoding_test_case_empty_test) add_test_case(hex_encoding_test_case_f_test) @@ -279,6 +282,8 @@ add_test_case(test_byte_buf_append_lookup_failure) add_test_case(test_byte_buf_reserve) add_test_case(test_byte_buf_reserve_initial_capacity_zero) add_test_case(test_byte_buf_reserve_relative) +add_test_case(test_byte_buf_reserve_smart) +add_test_case(test_byte_buf_reserve_smart_relative) add_test_case(test_byte_buf_reset) add_test_case(test_byte_cursor_compare_lexical) add_test_case(test_byte_cursor_compare_lookup) @@ -294,7 +299,7 @@ add_test_case(test_byte_cursor_utf8_parse_u64_hex) add_test_case(byte_swap_test) -if(HAVE_AVX2_INTRINSICS) +if(AWS_HAVE_AVX2_INTRINSICS) add_test_case(alignment32_test) else() add_test_case(alignment16_test) @@ -304,6 +309,7 @@ add_test_case(test_cpu_count_at_least_works_superficially) add_test_case(test_stack_trace_decoding) add_test_case(test_platform_build_os) add_test_case(test_sanity_check_numa_discovery) +add_test_case(test_sanity_check_environment_loader) add_test_case(test_realloc_fallback) add_test_case(test_realloc_passthrough) @@ -317,6 +323,10 @@ add_test_case(sba_threaded_allocs_and_frees) add_test_case(sba_threaded_reallocs) add_test_case(sba_churn) add_test_case(sba_metrics) +add_test_case(default_threaded_reallocs) +add_test_case(default_threaded_allocs_and_frees) +add_test_case(aligned_threaded_reallocs) +add_test_case(aligned_threaded_allocs_and_frees) add_test_case(test_memtrace_none) add_test_case(test_memtrace_count) @@ -354,13 +364,12 @@ add_test_case(rfc822_utc_dos_prevented) add_test_case(rfc822_invalid_format) add_test_case(rfc822_invalid_tz) add_test_case(rfc822_invalid_auto_format) -add_test_case(iso8601_utc_parsing) +add_test_case(iso8601_parsing) add_test_case(iso8601_basic_utc_parsing) add_test_case(iso8601_utc_parsing_auto_detect) add_test_case(iso8601_basic_utc_parsing_auto_detect) add_test_case(iso8601_date_only_parsing) add_test_case(iso8601_basic_date_only_parsing) -add_test_case(iso8601_utc_no_colon_parsing) add_test_case(iso8601_utc_dos_prevented) add_test_case(iso8601_invalid_format) add_test_case(iso8601_invalid_auto_format) @@ -399,6 +408,8 @@ add_test_case(ring_buffer_acquire_up_to_multi_threaded_test) add_test_case(string_to_log_level_success_test) add_test_case(string_to_log_level_failure_test) +add_test_case(test_memory_usage_maxrss) + if(NOT ANDROID) add_test_case(test_logging_filter_at_AWS_LL_NONE_s_logf_all_levels) add_test_case(test_logging_filter_at_AWS_LL_FATAL_s_logf_all_levels) @@ -478,12 +489,7 @@ add_test_case(directory_move_src_non_existent_test) add_test_case(test_home_directory_not_null) add_test_case(test_normalize_posix_directory_separator) add_test_case(test_normalize_windows_directory_separator) - -add_test_case(promise_test_wait_forever) -add_test_case(promise_test_wait_for_a_bit) -add_test_case(promise_test_finish_immediately) -add_test_case(promise_test_finish_before_wait) -add_test_case(promise_test_multiple_waiters) +add_test_case(test_byte_buf_init_from_file) add_test_case(test_json_parse_from_string) add_test_case(test_json_parse_to_string) @@ -512,6 +518,24 @@ add_test_case(test_uri_encode_path_rfc3986) add_test_case(test_uri_encode_query) add_test_case(test_uri_decode) +add_test_case(test_cross_process_lock_works_in_proc) +add_test_case(test_cross_process_lock_works_cross_proc) +#this one is here for use by test_instance_lock_works_cross_proc +add_test_case(cross_process_lock_mp_test_runner) +add_test_case(test_cross_process_lock_invalid_nonce_fails) + +add_test_case(host_util_is_ipv4) +add_test_case(host_util_is_ipv6) + +add_test_case(cbor_encode_decode_int_test) +add_test_case(cbor_encode_decode_double_test) +add_test_case(cbor_encode_decode_bool_test) +add_test_case(cbor_encode_decode_bytesstr_str_test) +add_test_case(cbor_encode_decode_array_map_test) +add_test_case(cbor_encode_decode_simple_value_test) +add_test_case(cbor_encode_decode_indef_test) +add_test_case(cbor_decode_error_handling_test) + generate_test_driver(${PROJECT_NAME}-tests) if(NOT MSVC AND NOT LEGACY_COMPILER_SUPPORT) diff --git a/tests/alloc_test.c b/tests/alloc_test.c index 0891e89ec..364b3f319 100644 --- a/tests/alloc_test.c +++ b/tests/alloc_test.c @@ -48,10 +48,12 @@ static int s_test_alloc_nothing_fn(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; - struct aws_allocator test_allocator = {.mem_acquire = s_test_alloc_acquire, - .mem_release = s_test_alloc_release, - .mem_realloc = s_test_realloc, - .mem_calloc = s_test_calloc}; + struct aws_allocator test_allocator = { + .mem_acquire = s_test_alloc_acquire, + .mem_release = s_test_alloc_release, + .mem_realloc = s_test_realloc, + .mem_calloc = s_test_calloc, + }; /* realloc should handle the case correctly, return null, and free the memory */ void *p = aws_mem_acquire(&test_allocator, 12); @@ -126,39 +128,42 @@ static int s_sba_random_reallocs(struct aws_allocator *allocator, void *ctx) { } AWS_TEST_CASE(sba_random_reallocs, s_sba_random_reallocs) -struct sba_thread_test_data { - struct aws_allocator *sba; +struct allocator_thread_test_data { + struct aws_allocator *test_allocator; uint32_t thread_idx; }; -static void s_sba_threaded_alloc_worker(void *user_data) { - struct aws_allocator *sba = ((struct sba_thread_test_data *)user_data)->sba; +static void s_threaded_alloc_worker(void *user_data) { + struct aws_allocator *test_allocator = ((struct allocator_thread_test_data *)user_data)->test_allocator; void *allocs[NUM_TEST_ALLOCS]; for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) { size_t size = aws_max_size(rand() % 512, 1); - void *alloc = aws_mem_acquire(sba, size); + void *alloc = aws_mem_acquire(test_allocator, size); AWS_FATAL_ASSERT(alloc); allocs[count] = alloc; } for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) { void *alloc = allocs[count]; - aws_mem_release(sba, alloc); + aws_mem_release(test_allocator, alloc); } } -static void s_sba_thread_test(struct aws_allocator *allocator, void (*thread_fn)(void *), struct aws_allocator *sba) { +static void s_thread_test( + struct aws_allocator *allocator, + void (*thread_fn)(void *), + struct aws_allocator *test_allocator) { const struct aws_thread_options *thread_options = aws_default_thread_options(); struct aws_thread threads[NUM_TEST_THREADS]; - struct sba_thread_test_data thread_data[NUM_TEST_THREADS]; + struct allocator_thread_test_data thread_data[NUM_TEST_THREADS]; AWS_ZERO_ARRAY(threads); AWS_ZERO_ARRAY(thread_data); for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) { struct aws_thread *thread = &threads[thread_idx]; aws_thread_init(thread, allocator); - struct sba_thread_test_data *data = &thread_data[thread_idx]; - data->sba = sba; + struct allocator_thread_test_data *data = &thread_data[thread_idx]; + data->test_allocator = test_allocator; data->thread_idx = (uint32_t)thread_idx; aws_thread_launch(thread, thread_fn, data, thread_options); } @@ -175,7 +180,7 @@ static int s_sba_threaded_allocs_and_frees(struct aws_allocator *allocator, void struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true); - s_sba_thread_test(allocator, s_sba_threaded_alloc_worker, sba); + s_thread_test(allocator, s_threaded_alloc_worker, sba); aws_small_block_allocator_destroy(sba); @@ -183,9 +188,9 @@ static int s_sba_threaded_allocs_and_frees(struct aws_allocator *allocator, void } AWS_TEST_CASE(sba_threaded_allocs_and_frees, s_sba_threaded_allocs_and_frees) -static void s_sba_threaded_realloc_worker(void *user_data) { - struct sba_thread_test_data *thread_data = user_data; - struct aws_allocator *sba = thread_data->sba; +static void s_threaded_realloc_worker(void *user_data) { + struct allocator_thread_test_data *thread_data = user_data; + struct aws_allocator *test_allocator = thread_data->test_allocator; void *alloc = NULL; size_t size = 0; for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) { @@ -194,7 +199,7 @@ static void s_sba_threaded_realloc_worker(void *user_data) { if (old_size) { AWS_FATAL_ASSERT(0 == memcmp(alloc, &thread_data->thread_idx, 1)); } - AWS_FATAL_ASSERT(0 == aws_mem_realloc(sba, &alloc, old_size, size)); + AWS_FATAL_ASSERT(0 == aws_mem_realloc(test_allocator, &alloc, old_size, size)); /* If there was a value, make sure it's still there */ if (old_size && size) { AWS_FATAL_ASSERT(0 == memcmp(alloc, &thread_data->thread_idx, 1)); @@ -203,7 +208,7 @@ static void s_sba_threaded_realloc_worker(void *user_data) { memset(alloc, (int)thread_data->thread_idx, size); } } - AWS_FATAL_ASSERT(0 == aws_mem_realloc(sba, &alloc, size, 0)); + AWS_FATAL_ASSERT(0 == aws_mem_realloc(test_allocator, &alloc, size, 0)); } static int s_sba_threaded_reallocs(struct aws_allocator *allocator, void *ctx) { @@ -212,7 +217,7 @@ static int s_sba_threaded_reallocs(struct aws_allocator *allocator, void *ctx) { struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true); - s_sba_thread_test(allocator, s_sba_threaded_realloc_worker, sba); + s_thread_test(allocator, s_threaded_realloc_worker, sba); aws_small_block_allocator_destroy(sba); @@ -313,10 +318,66 @@ static int s_sba_metrics_test(struct aws_allocator *allocator, void *ctx) { ASSERT_INT_EQUALS(0, aws_small_block_allocator_bytes_active(sba)); - /* after freeing everything, we should have reliniquished all but one page in each bin */ + /* after freeing everything, we should have relinquished all but one page in each bin */ ASSERT_INT_EQUALS(5 * aws_small_block_allocator_page_size(sba), aws_small_block_allocator_bytes_reserved(sba)); aws_small_block_allocator_destroy(sba); return 0; } AWS_TEST_CASE(sba_metrics, s_sba_metrics_test) + +/* + * Default allocator tests. + */ +static int s_default_threaded_reallocs(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + srand(15); + + s_thread_test(allocator, s_threaded_realloc_worker, allocator); + + return 0; +} +AWS_TEST_CASE(default_threaded_reallocs, s_default_threaded_reallocs) + +static int s_default_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + srand(99); + + s_thread_test(allocator, s_threaded_alloc_worker, allocator); + + return 0; +} +AWS_TEST_CASE(default_threaded_allocs_and_frees, s_default_threaded_allocs_and_frees) + +/* + * No align allocator tests. + */ +static int s_aligned_threaded_reallocs(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + srand(15); + + struct aws_allocator *alloc = aws_mem_tracer_new(aws_aligned_allocator(), NULL, AWS_MEMTRACE_STACKS, 8); + + s_thread_test(alloc, s_threaded_realloc_worker, alloc); + + aws_mem_tracer_destroy(alloc); + + return 0; +} +AWS_TEST_CASE(aligned_threaded_reallocs, s_aligned_threaded_reallocs) + +static int s_aligned_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + srand(99); + + struct aws_allocator *alloc = aws_mem_tracer_new(aws_aligned_allocator(), NULL, AWS_MEMTRACE_STACKS, 8); + + s_thread_test(alloc, s_threaded_alloc_worker, alloc); + + aws_mem_tracer_destroy(alloc); + + return 0; +} +AWS_TEST_CASE(aligned_threaded_allocs_and_frees, s_aligned_threaded_allocs_and_frees) diff --git a/tests/assert_test.c b/tests/assert_test.c index 52a4794f5..430a3f9e4 100644 --- a/tests/assert_test.c +++ b/tests/assert_test.c @@ -65,6 +65,7 @@ int side_effect(void) { } /* NOLINTNEXTLINE(readability-function-size) */ +/* clang-format off */ int test_asserts(int *index) { TEST_SUCCESS(null_test) {} TEST_FAILURE(null_failure_test) { @@ -160,6 +161,7 @@ int test_asserts(int *index) { return NO_MORE_TESTS; } +/* clang-format on */ void reset(void) { g_cur_testname = "UNKNOWN"; diff --git a/tests/atomics_test.c b/tests/atomics_test.c index f1cf15848..16590a794 100644 --- a/tests/atomics_test.c +++ b/tests/atomics_test.c @@ -272,7 +272,7 @@ static int run_races( int *participant_indexes = alloca(n_participants_local * sizeof(*participant_indexes)); struct aws_thread *threads = alloca(n_participants_local * sizeof(struct aws_thread)); - *last_race = (size_t)-1; + *last_race = SIZE_MAX; n_participants = n_participants_local; done_racing = false; aws_atomic_init_int(&last_race_index, 0); @@ -293,7 +293,7 @@ static int run_races( *last_race = n_races; } else { *last_race = (size_t)aws_atomic_load_int_explicit(&last_race_index, aws_memory_order_relaxed); - if (*last_race == (size_t)-1) { + if (*last_race == SIZE_MAX) { /* We didn't even see the first race complete */ *last_race = 0; } diff --git a/tests/byte_buf_test.c b/tests/byte_buf_test.c index 545ec4a0e..035653853 100644 --- a/tests/byte_buf_test.c +++ b/tests/byte_buf_test.c @@ -4,7 +4,7 @@ */ #include - +#include #include #include @@ -798,6 +798,63 @@ static int s_test_byte_buf_reserve_relative(struct aws_allocator *allocator, voi } AWS_TEST_CASE(test_byte_buf_reserve_relative, s_test_byte_buf_reserve_relative) +static int s_test_byte_buf_reserve_smart(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + (void)allocator; + + struct aws_byte_buf buffer; + size_t base = 10; + aws_byte_buf_init(&buffer, allocator, base); + ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 2 * base) == AWS_OP_SUCCESS); + ASSERT_UINT_EQUALS(2 * base, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 3 * base) == AWS_OP_SUCCESS); + /* double the previous capacity instead of just expand the capacity to meet the requirement to reduce the number of + * allocations */ + ASSERT_UINT_EQUALS(4 * base, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 5 * base) == AWS_OP_SUCCESS); + /* double the previous capacity instead of just expand the capacity to meet the requirement to reduce the number of + * allocations */ + ASSERT_UINT_EQUALS(8 * base, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 5 * base) == AWS_OP_SUCCESS); + ASSERT_UINT_EQUALS(8 * base, buffer.capacity); + + aws_byte_buf_clean_up(&buffer); + + return 0; +} +AWS_TEST_CASE(test_byte_buf_reserve_smart, s_test_byte_buf_reserve_smart) + +static int s_test_byte_buf_reserve_smart_relative(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + (void)allocator; + + struct aws_byte_buf buffer; + aws_byte_buf_init(&buffer, allocator, 1); + + struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_reserve_test_prefix); + size_t length = prefix_cursor.len; + + ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS); + ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS); + ASSERT_UINT_EQUALS(length, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS); + ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS); + ASSERT_UINT_EQUALS(2 * length, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS); + ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS); + /* 4 times base as it's expanded to twice the original capacity to prevent too many allocation */ + ASSERT_UINT_EQUALS(4 * length, buffer.capacity); + ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS); + ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS); + /* still 4 times base as it doesn't need to expand again */ + ASSERT_UINT_EQUALS(4 * length, buffer.capacity); + + aws_byte_buf_clean_up(&buffer); + + return 0; +} +AWS_TEST_CASE(test_byte_buf_reserve_smart_relative, s_test_byte_buf_reserve_smart_relative) + static int s_test_byte_cursor_starts_with(struct aws_allocator *allocator, void *ctx) { (void)ctx; (void)allocator; diff --git a/tests/cbor_test.c b/tests/cbor_test.c new file mode 100644 index 000000000..585ffcca1 --- /dev/null +++ b/tests/cbor_test.c @@ -0,0 +1,489 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include +#include + +#include + +#define CBOR_TEST_CASE(NAME) \ + AWS_TEST_CASE(NAME, s_test_##NAME); \ + static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) + +CBOR_TEST_CASE(cbor_encode_decode_int_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + enum { VALUE_NUM = 6 }; + + /** + * Less than 24 only take 1 byte, + * 24 to uint8_t max takes 2 bytes + * uint8_t max to uint16_t max takes 3 bytes + * uint16_t max to uint32_t maxx takes 5 bytes + * uint32_t max to uint64_t max takes 9 bytes + */ + uint64_t values[VALUE_NUM] = {23, 24, UINT8_MAX + 1, UINT16_MAX + 1U, UINT32_MAX + 1LLU, UINT64_MAX}; + uint64_t expected_encoded_len[VALUE_NUM] = {1, 2, 3, 5, 9, 9}; + + size_t encoded_len = 0; + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + /* Unsigned int */ + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_uint(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + /* Negative int */ + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_negint(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + /* Unsigned int */ + for (size_t i = 0; i < VALUE_NUM; i++) { + uint64_t result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result)); + ASSERT_UINT_EQUALS(values[i], result); + } + /* Negative int */ + for (size_t i = 0; i < VALUE_NUM; i++) { + uint64_t result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result)); + ASSERT_UINT_EQUALS(values[i], result); + } + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_encode_decode_double_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + enum { VALUE_NUM = 10 }; + + /** + * 1 as unsigned int, takes 1 byte + * -1 as negative int, takes 1 byte + * 1.1 double, takes 9 bytes + * 1.1f is float, takes 5 bytes + * -1.1f is float, takes 5 bytes + * INFINITY will be float, takes 5 bytes + * FLT_MAX still a float, take 5 bytes + * DBL_MAX will be a double takes 9 bytes + * DBL_MIN will be a double takes 9 bytes + * HUGE_VAL + */ + double values[VALUE_NUM] = {1.0, -1.0, 1.1, 1.1f, -1.1f, INFINITY, FLT_MAX, DBL_MAX, DBL_MIN, HUGE_VAL}; + uint64_t expected_encoded_len[VALUE_NUM] = {1, 1, 9, 5, 5, 5, 5, 9, 9, 5}; + + int expected_encoded_type[VALUE_NUM] = { + AWS_CBOR_TYPE_UINT, + AWS_CBOR_TYPE_NEGINT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + AWS_CBOR_TYPE_FLOAT, + }; + + size_t encoded_len = 0; + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_float(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + /* Unsigned int, 1 */ + size_t index = 0; + uint64_t result = 0; + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result)); + ASSERT_TRUE(values[index++] == result); + /* negative int, -1 */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result)); + /* Convert the decode val to expected val. */ + ASSERT_TRUE((-1 - values[index++]) == result); + /* 1.1 double */ + double double_result = 0; + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* 1.1 float */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* -1.1 float */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* INFINITY */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* FLT_MAX */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* DBL_MAX */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* DBL_MIN */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + /* HUGE_VAL */ + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, expected_encoded_type[index]); + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_float_val(decoder, &double_result)); + ASSERT_TRUE(values[index++] == double_result); + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_encode_decode_bool_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + enum { VALUE_NUM = 2 }; + bool values[VALUE_NUM] = {true, false}; + uint64_t expected_encoded_len[VALUE_NUM] = {1, 1}; + + size_t encoded_len = 0; + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_bool(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + for (size_t i = 0; i < VALUE_NUM; i++) { + bool result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_boolean_val(decoder, &result)); + ASSERT_UINT_EQUALS(values[i], result); + } + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_encode_decode_bytesstr_str_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); + struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); + + enum { VALUE_NUM = 2 }; + struct aws_byte_cursor values[VALUE_NUM] = {val_1, val_2}; + uint64_t expected_encoded_len[VALUE_NUM] = {1 + val_1.len, 1 + val_2.len}; + + size_t encoded_len = 0; + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_text(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_bytes(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + for (size_t i = 0; i < VALUE_NUM; i++) { + struct aws_byte_cursor result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_text_val(decoder, &result)); + ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); + } + for (size_t i = 0; i < VALUE_NUM; i++) { + struct aws_byte_cursor result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_bytes_val(decoder, &result)); + ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); + } + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_encode_decode_array_map_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); + struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); + + enum { VALUE_NUM = 2 }; + struct aws_byte_cursor values[VALUE_NUM] = {val_1, val_2}; + uint64_t expected_encoded_len[VALUE_NUM] = {1 + val_1.len, 1 + val_2.len}; + + size_t encoded_len = 0; + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + + /* Array with 2 elements */ + aws_cbor_encoder_write_array_start(encoder, 2); + struct aws_byte_cursor encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); + /* Array start with 2 element only takes 1 byte */ + ASSERT_UINT_EQUALS(encoded_len + 1, encoded_cursor.len); + encoded_len = encoded_cursor.len; + + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_text(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + + /* Map with 1 element */ + aws_cbor_encoder_write_map_start(encoder, 1); + encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); + /* Map start with 1 (key, value pair) only takes 1 byte */ + ASSERT_UINT_EQUALS(encoded_len + 1, encoded_cursor.len); + encoded_len = encoded_cursor.len; + for (size_t i = 0; i < VALUE_NUM; i++) { + aws_cbor_encoder_write_bytes(encoder, values[i]); + struct aws_byte_cursor cursor = aws_cbor_encoder_get_encoded_data(encoder); + ASSERT_UINT_EQUALS(encoded_len + expected_encoded_len[i], cursor.len); + encoded_len = cursor.len; + } + + /* Map with a lot element, not closure. */ + aws_cbor_encoder_write_array_start(encoder, UINT16_MAX + 1); + encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); + /* The size takes 4 bytes and one more for the cbor start item */ + ASSERT_UINT_EQUALS(encoded_len + 5, encoded_cursor.len); + encoded_len = encoded_cursor.len; + + aws_cbor_encoder_write_map_start(encoder, UINT16_MAX + 1); + encoded_cursor = aws_cbor_encoder_get_encoded_data(encoder); + /* The size takes 4 bytes and one more for the cbor start item */ + ASSERT_UINT_EQUALS(encoded_len + 5, encoded_cursor.len); + encoded_len = encoded_cursor.len; + + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + + uint64_t element_size = 0; + aws_cbor_decoder_pop_next_array_start(decoder, &element_size); + ASSERT_UINT_EQUALS(element_size, 2); + for (size_t i = 0; i < VALUE_NUM; i++) { + struct aws_byte_cursor result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_text_val(decoder, &result)); + ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); + } + aws_cbor_decoder_pop_next_map_start(decoder, &element_size); + ASSERT_UINT_EQUALS(element_size, 1); + for (size_t i = 0; i < VALUE_NUM; i++) { + struct aws_byte_cursor result; + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_bytes_val(decoder, &result)); + ASSERT_TRUE(aws_byte_cursor_eq(&result, &values[i])); + } + aws_cbor_decoder_pop_next_array_start(decoder, &element_size); + ASSERT_UINT_EQUALS(element_size, UINT16_MAX + 1); + aws_cbor_decoder_pop_next_map_start(decoder, &element_size); + ASSERT_UINT_EQUALS(element_size, UINT16_MAX + 1); + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_encode_decode_simple_value_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + aws_cbor_encoder_write_null(encoder); + aws_cbor_encoder_write_undefined(encoder); + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + /* in total 2 bytes for two simple value */ + ASSERT_UINT_EQUALS(2, final_cursor.len); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_NULL); + ASSERT_SUCCESS(aws_cbor_decoder_consume_next_single_element(decoder)); + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_UNDEFINED); + ASSERT_SUCCESS(aws_cbor_decoder_consume_next_single_element(decoder)); + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +/* Test a complicate multiple stacks encode and decode */ +CBOR_TEST_CASE(cbor_encode_decode_indef_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); + struct aws_byte_cursor val_2 = aws_byte_cursor_from_c_str("write more tests"); + + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + + /* Create a non-sense stack of inf collections. */ + aws_cbor_encoder_write_indef_map_start(encoder); + /* Key */ + aws_cbor_encoder_write_text(encoder, val_1); + /* Value */ + aws_cbor_encoder_write_indef_array_start(encoder); + /* element 1 in array */ + aws_cbor_encoder_write_indef_text_start(encoder); + aws_cbor_encoder_write_text(encoder, val_1); + aws_cbor_encoder_write_text(encoder, val_2); + aws_cbor_encoder_write_break(encoder); + /* element 2 in array */ + aws_cbor_encoder_write_indef_bytes_start(encoder); + aws_cbor_encoder_write_bytes(encoder, val_1); + aws_cbor_encoder_write_bytes(encoder, val_2); + aws_cbor_encoder_write_break(encoder); + /* element 3 as a tag in array */ + aws_cbor_encoder_write_tag(encoder, AWS_CBOR_TAG_DECIMAL_FRACTION); + aws_cbor_encoder_write_indef_array_start(encoder); + aws_cbor_encoder_write_indef_bytes_start(encoder); + aws_cbor_encoder_write_bytes(encoder, val_1); + aws_cbor_encoder_write_bytes(encoder, val_2); + aws_cbor_encoder_write_break(encoder); + aws_cbor_encoder_write_break(encoder); + /* Closure for the array */ + aws_cbor_encoder_write_break(encoder); + /* Closure for the map */ + aws_cbor_encoder_write_break(encoder); + + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(out_type, AWS_CBOR_TYPE_INDEF_MAP_START); + + /* Get rid of the whole inf map with all the data content */ + ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); + + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} + +CBOR_TEST_CASE(cbor_decode_error_handling_test) { + (void)allocator; + (void)ctx; + aws_common_library_init(allocator); + /* Major type 7 with argument 30, 11111110, malformed CBOR */ + uint8_t invalid_data[] = {0xFE}; + struct aws_byte_cursor invalid_cbor = aws_byte_cursor_from_array(invalid_data, sizeof(invalid_data)); + + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + + /* 1. Malformed cbor data */ + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, invalid_cbor); + ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); + aws_cbor_decoder_destroy(decoder); + + /* 2. Empty cursor */ + struct aws_byte_cursor empty = {0}; + decoder = aws_cbor_decoder_new(allocator, empty); + ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); + aws_cbor_decoder_destroy(decoder); + + /* 3. Try get wrong type */ + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + uint64_t val = 1; + aws_cbor_encoder_write_uint(encoder, val); + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + decoder = aws_cbor_decoder_new(allocator, final_cursor); + uint64_t out = 0; + ASSERT_FAILS(aws_cbor_decoder_pop_next_array_start(decoder, &out)); + ASSERT_UINT_EQUALS(AWS_ERROR_CBOR_UNEXPECTED_TYPE, aws_last_error()); + /* But, we can still keep decoding for the right type */ + ASSERT_SUCCESS(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &out)); + ASSERT_UINT_EQUALS(val, out); + /* All the data has been consumed, now it's invalid */ + ASSERT_FAILS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); + ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error()); + aws_cbor_decoder_destroy(decoder); + + /* 4. Consume data items with size */ + struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test"); + aws_cbor_encoder_reset(encoder); + aws_cbor_encoder_write_map_start(encoder, 1); + /* Key */ + aws_cbor_encoder_write_text(encoder, val_1); + /* Value */ + aws_cbor_encoder_write_array_start(encoder, 1); + aws_cbor_encoder_write_tag(encoder, AWS_CBOR_TAG_NEGATIVE_BIGNUM); + aws_cbor_encoder_write_bytes(encoder, val_1); + final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + decoder = aws_cbor_decoder_new(allocator, final_cursor); + ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type)); + ASSERT_UINT_EQUALS(AWS_CBOR_TYPE_MAP_START, out_type); + ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder)); + ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder)); + aws_cbor_decoder_destroy(decoder); + + aws_cbor_encoder_destroy(encoder); + aws_common_library_clean_up(); + return AWS_OP_SUCCESS; +} diff --git a/tests/condition_variable_test.c b/tests/condition_variable_test.c index e7ddd5f25..a59e7c2ba 100644 --- a/tests/condition_variable_test.c +++ b/tests/condition_variable_test.c @@ -65,13 +65,15 @@ static int s_test_conditional_notify_one_fn(struct aws_allocator *allocator, voi struct condition_predicate_args predicate_args = {.call_count = 0}; - struct conditional_test_data test_data = {.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, - .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, - .mutex = AWS_MUTEX_INIT, - .predicate_args = &predicate_args, - .thread_1 = 0, - .thread_2 = 0, - .thread_3 = 0}; + struct conditional_test_data test_data = { + .condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, + .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, + .mutex = AWS_MUTEX_INIT, + .predicate_args = &predicate_args, + .thread_1 = 0, + .thread_2 = 0, + .thread_3 = 0, + }; ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); @@ -103,13 +105,15 @@ static int s_test_conditional_notify_all_fn(struct aws_allocator *allocator, voi struct condition_predicate_args predicate_args = {.call_count = 0}; - struct conditional_test_data test_data = {.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, - .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, - .mutex = AWS_MUTEX_INIT, - .predicate_args = &predicate_args, - .thread_1 = 0, - .thread_2 = 0, - .thread_3 = 0}; + struct conditional_test_data test_data = { + .condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, + .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, + .mutex = AWS_MUTEX_INIT, + .predicate_args = &predicate_args, + .thread_1 = 0, + .thread_2 = 0, + .thread_3 = 0, + }; ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); diff --git a/tests/cross_process_lock_tests.c b/tests/cross_process_lock_tests.c new file mode 100644 index 000000000..a94a92002 --- /dev/null +++ b/tests/cross_process_lock_tests.c @@ -0,0 +1,116 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ +#include +#include +#include +#include + +static int s_test_cross_process_lock_works_in_proc(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + aws_common_library_init(allocator); + + struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_nonce"); + struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NOT_NULL(instance_lock); + + struct aws_cross_process_lock *should_be_null = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NULL(should_be_null); + + aws_cross_process_lock_release(instance_lock); + struct aws_cross_process_lock *should_not_be_null = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NOT_NULL(should_not_be_null); + aws_cross_process_lock_release(should_not_be_null); + + aws_common_library_clean_up(); + + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(test_cross_process_lock_works_in_proc, s_test_cross_process_lock_works_in_proc) + +static int s_cross_process_lock_mp_test_runner(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + aws_common_library_init(allocator); + + /* so the test runner doesn't actually run the portion of the test we want to test unless we're invoking it as a + * subprocess. */ + struct aws_string *test_run_gate = aws_string_new_from_c_str(allocator, "aws_crt_test_run_gate"); + struct aws_string *output_val = NULL; + if (aws_get_environment_value(allocator, test_run_gate, &output_val) == AWS_OP_SUCCESS && output_val) { + aws_string_destroy(output_val); + struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_mp_nonce"); + struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NOT_NULL(instance_lock); + + aws_cross_process_lock_release(instance_lock); + } + aws_string_destroy(test_run_gate); + aws_common_library_clean_up(); + + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(cross_process_lock_mp_test_runner, s_cross_process_lock_mp_test_runner) + +static int s_test_cross_process_lock_works_cross_proc(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + aws_common_library_init(allocator); + + struct aws_string *test_run_gate = aws_string_new_from_c_str(allocator, "aws_crt_test_run_gate"); + struct aws_string *test_run_gate_val = aws_string_new_from_c_str(allocator, "ON"); + + /* so the test runner doesn't actually run the portion of the test we want to test unless we're invoking it from + * here. */ + ASSERT_SUCCESS(aws_set_environment_value(test_run_gate, test_run_gate_val)); + aws_string_destroy(test_run_gate_val); + aws_string_destroy(test_run_gate); + + /* Invoke the test runner in a new process for ease so cmake automatically does the work for us. */ + struct aws_run_command_options command_options = { +#ifdef _WIN32 + .command = "aws-c-common-tests cross_process_lock_mp_test_runner", +#else + .command = "./aws-c-common-tests cross_process_lock_mp_test_runner", +#endif /* _WIN32 */ + }; + + struct aws_run_command_result result; + AWS_ZERO_STRUCT(result); + + ASSERT_SUCCESS(aws_run_command(allocator, &command_options, &result)); + ASSERT_TRUE(result.ret_code == 0); + aws_run_command_result_cleanup(&result); + AWS_ZERO_STRUCT(result); + struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("lock_mp_nonce"); + struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NOT_NULL(instance_lock); + + ASSERT_SUCCESS(aws_run_command(allocator, &command_options, &result)); + ASSERT_FALSE(result.ret_code == 0); + aws_run_command_result_cleanup(&result); + + aws_cross_process_lock_release(instance_lock); + aws_common_library_clean_up(); + + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(test_cross_process_lock_works_cross_proc, s_test_cross_process_lock_works_cross_proc) + +static int s_test_cross_process_lock_invalid_nonce(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + aws_common_library_init(allocator); +#ifdef _WIN32 + struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("invalid\\lock_nonce"); +#else + struct aws_byte_cursor lock_nonce = aws_byte_cursor_from_c_str("invalid/lock_nonce"); +#endif /* _WIN32 */ + + struct aws_cross_process_lock *instance_lock = aws_cross_process_lock_try_acquire(allocator, lock_nonce); + ASSERT_NULL(instance_lock); + ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error()); + + aws_common_library_clean_up(); + + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(test_cross_process_lock_invalid_nonce_fails, s_test_cross_process_lock_invalid_nonce) diff --git a/tests/date_time_test.c b/tests/date_time_test.c index 8272a4fd5..69f0417f0 100644 --- a/tests/date_time_test.c +++ b/tests/date_time_test.c @@ -19,7 +19,7 @@ static int s_test_rfc822_utc_parsing_fn(struct aws_allocator *allocator, void *c "Wed, 02 Oct 2002 08:05:09 UTC", }; - for (size_t i = 0; i < 4; ++i) { + for (size_t i = 0; i < AWS_ARRAY_SIZE(valid_utc_dates); ++i) { struct aws_date_time date_time; const char *date_str = valid_utc_dates[i]; struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); @@ -284,52 +284,73 @@ static int s_test_rfc822_invalid_auto_format_fn(struct aws_allocator *allocator, AWS_TEST_CASE(rfc822_invalid_auto_format, s_test_rfc822_invalid_auto_format_fn) -static int s_test_iso8601_utc_parsing_fn(struct aws_allocator *allocator, void *ctx) { +static int s_test_iso8601_parsing_fn(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; - struct aws_date_time date_time; - const char *date_str = "2002-10-02T08:05:09.000Z"; - struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); + /* array of {"date", "description"} */ + const char *valid_dates[][2] = { + {"2002-10-02T08:05:09.000Z", "extended format"}, + {"2002-10-02t08:05:09.000z", "lowercase T and Z"}, + {"2002-10-02 08:05:09Z", "space instead of T"}, /*allowed per NOTE in RFC3339#section-5.6 */ + {"2002-10-02T08:05:09+00:00", "offset instead of Z"}, + {"2002-10-01T19:31:09-12:34", "western offset"}, + {"2002-10-02T20:39:09+12:34", "eastern offset"}, + {"2002-10-02T08:05:09.000+00:00", "fractional seconds and offset"}, + {"20021002T080509.000Z", "basic format"}, + {"20021002T203909+1234", "basic format with fractional seconds"}, + {"2002-10-02T080509.000Z", "weird mix of extended date but basic time"}, + {"20021002T08:05:09.000Z", "weird mix of basic date but extended time"}, + {"2002-10-02T20:39:09+1234", "weird mix of extended date and time but basic fractional seconds"}, + }; - ASSERT_SUCCESS(aws_date_time_init_from_str(&date_time, &date_buf, AWS_DATE_FORMAT_ISO_8601)); - ASSERT_INT_EQUALS(AWS_DATE_DAY_OF_WEEK_WEDNESDAY, aws_date_time_day_of_week(&date_time, false)); - ASSERT_UINT_EQUALS(2, aws_date_time_month_day(&date_time, false)); - ASSERT_UINT_EQUALS(AWS_DATE_MONTH_OCTOBER, aws_date_time_month(&date_time, false)); - ASSERT_UINT_EQUALS(2002, aws_date_time_year(&date_time, false)); - ASSERT_UINT_EQUALS(8, aws_date_time_hour(&date_time, false)); - ASSERT_UINT_EQUALS(5, aws_date_time_minute(&date_time, false)); - ASSERT_UINT_EQUALS(9, aws_date_time_second(&date_time, false)); + for (size_t i = 0; i < AWS_ARRAY_SIZE(valid_dates); ++i) { + const char *date_str = valid_dates[i][0]; + const char *description = valid_dates[i][1]; + printf("checking date[%zu] \"%s\" (%s)\n", i, date_str, description); - uint8_t date_output[AWS_DATE_TIME_STR_MAX_LEN]; - AWS_ZERO_ARRAY(date_output); - struct aws_byte_buf str_output = aws_byte_buf_from_array(date_output, sizeof(date_output)); - str_output.len = 0; - ASSERT_SUCCESS(aws_date_time_to_utc_time_str(&date_time, AWS_DATE_FORMAT_ISO_8601, &str_output)); + struct aws_date_time date_time; + struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); - const char *expected_date_str = "2002-10-02T08:05:09Z"; - struct aws_byte_buf expected_date_buf = aws_byte_buf_from_c_str(expected_date_str); - ASSERT_BIN_ARRAYS_EQUALS(expected_date_buf.buffer, expected_date_buf.len, str_output.buffer, str_output.len); + ASSERT_SUCCESS(aws_date_time_init_from_str(&date_time, &date_buf, AWS_DATE_FORMAT_ISO_8601)); + ASSERT_INT_EQUALS(AWS_DATE_DAY_OF_WEEK_WEDNESDAY, aws_date_time_day_of_week(&date_time, false)); + ASSERT_UINT_EQUALS(2, aws_date_time_month_day(&date_time, false)); + ASSERT_UINT_EQUALS(AWS_DATE_MONTH_OCTOBER, aws_date_time_month(&date_time, false)); + ASSERT_UINT_EQUALS(2002, aws_date_time_year(&date_time, false)); + ASSERT_UINT_EQUALS(8, aws_date_time_hour(&date_time, false)); + ASSERT_UINT_EQUALS(5, aws_date_time_minute(&date_time, false)); + ASSERT_UINT_EQUALS(9, aws_date_time_second(&date_time, false)); - AWS_ZERO_ARRAY(date_output); - str_output.len = 0; - ASSERT_SUCCESS(aws_date_time_to_utc_time_short_str(&date_time, AWS_DATE_FORMAT_ISO_8601, &str_output)); + uint8_t date_output[AWS_DATE_TIME_STR_MAX_LEN]; + AWS_ZERO_ARRAY(date_output); + struct aws_byte_buf str_output = aws_byte_buf_from_array(date_output, sizeof(date_output)); + str_output.len = 0; + ASSERT_SUCCESS(aws_date_time_to_utc_time_str(&date_time, AWS_DATE_FORMAT_ISO_8601, &str_output)); - const char *expected_short_str = "2002-10-02"; - struct aws_byte_buf expected_short_buf = aws_byte_buf_from_c_str(expected_short_str); + const char *expected_date_str = "2002-10-02T08:05:09Z"; + struct aws_byte_buf expected_date_buf = aws_byte_buf_from_c_str(expected_date_str); + ASSERT_BIN_ARRAYS_EQUALS(expected_date_buf.buffer, expected_date_buf.len, str_output.buffer, str_output.len); - ASSERT_BIN_ARRAYS_EQUALS(expected_short_buf.buffer, expected_short_buf.len, str_output.buffer, str_output.len); + AWS_ZERO_ARRAY(date_output); + str_output.len = 0; + ASSERT_SUCCESS(aws_date_time_to_utc_time_short_str(&date_time, AWS_DATE_FORMAT_ISO_8601, &str_output)); + + const char *expected_short_str = "2002-10-02"; + struct aws_byte_buf expected_short_buf = aws_byte_buf_from_c_str(expected_short_str); + + ASSERT_BIN_ARRAYS_EQUALS(expected_short_buf.buffer, expected_short_buf.len, str_output.buffer, str_output.len); + } return AWS_OP_SUCCESS; } -AWS_TEST_CASE(iso8601_utc_parsing, s_test_iso8601_utc_parsing_fn) +AWS_TEST_CASE(iso8601_parsing, s_test_iso8601_parsing_fn) static int s_test_iso8601_basic_utc_parsing_fn(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; struct aws_date_time date_time; - const char *date_str = "20021002T080509000Z"; + const char *date_str = "20021002T080509.000Z"; struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); ASSERT_SUCCESS(aws_date_time_init_from_str(&date_time, &date_buf, AWS_DATE_FORMAT_ISO_8601_BASIC)); @@ -401,7 +422,7 @@ static int s_test_iso8601_basic_utc_parsing_auto_detect_fn(struct aws_allocator (void)ctx; struct aws_date_time date_time; - const char *date_str = "20021002T080509000Z"; + const char *date_str = "20021002T080509,000Z"; struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); ASSERT_SUCCESS(aws_date_time_init_from_str(&date_time, &date_buf, AWS_DATE_FORMAT_AUTO_DETECT)); @@ -428,38 +449,6 @@ static int s_test_iso8601_basic_utc_parsing_auto_detect_fn(struct aws_allocator AWS_TEST_CASE(iso8601_basic_utc_parsing_auto_detect, s_test_iso8601_basic_utc_parsing_auto_detect_fn) -static int s_test_iso8601_utc_no_colon_parsing_fn(struct aws_allocator *allocator, void *ctx) { - (void)allocator; - (void)ctx; - - struct aws_date_time date_time; - const char *date_str = "2002-10-02T080509.000Z"; - struct aws_byte_buf date_buf = aws_byte_buf_from_c_str(date_str); - - ASSERT_SUCCESS(aws_date_time_init_from_str(&date_time, &date_buf, AWS_DATE_FORMAT_ISO_8601)); - ASSERT_INT_EQUALS(AWS_DATE_DAY_OF_WEEK_WEDNESDAY, aws_date_time_day_of_week(&date_time, false)); - ASSERT_UINT_EQUALS(2, aws_date_time_month_day(&date_time, false)); - ASSERT_UINT_EQUALS(AWS_DATE_MONTH_OCTOBER, aws_date_time_month(&date_time, false)); - ASSERT_UINT_EQUALS(2002, aws_date_time_year(&date_time, false)); - ASSERT_UINT_EQUALS(8, aws_date_time_hour(&date_time, false)); - ASSERT_UINT_EQUALS(5, aws_date_time_minute(&date_time, false)); - ASSERT_UINT_EQUALS(9, aws_date_time_second(&date_time, false)); - - uint8_t date_output[AWS_DATE_TIME_STR_MAX_LEN]; - AWS_ZERO_ARRAY(date_output); - struct aws_byte_buf str_output = aws_byte_buf_from_array(date_output, sizeof(date_output)); - str_output.len = 0; - ASSERT_SUCCESS(aws_date_time_to_utc_time_str(&date_time, AWS_DATE_FORMAT_ISO_8601, &str_output)); - - const char *expected_date_str = "2002-10-02T08:05:09Z"; - struct aws_byte_buf expected_date_buf = aws_byte_buf_from_c_str(expected_date_str); - ASSERT_BIN_ARRAYS_EQUALS(expected_date_buf.buffer, expected_date_buf.len, str_output.buffer, str_output.len); - - return AWS_OP_SUCCESS; -} - -AWS_TEST_CASE(iso8601_utc_no_colon_parsing, s_test_iso8601_utc_no_colon_parsing_fn) - static int s_test_iso8601_date_only_parsing_fn(struct aws_allocator *allocator, void *ctx) { (void)allocator; (void)ctx; diff --git a/tests/encoding_test.c b/tests/encoding_test.c index d9863951a..23cd084d0 100644 --- a/tests/encoding_test.c +++ b/tests/encoding_test.c @@ -1298,7 +1298,7 @@ static int s_utf8_validation_callback_always_fails(const uint32_t codepoint, voi static int s_utf8_validation_callback_always_passes(const uint32_t codepoint, void *user_data) { (void)codepoint; (void)user_data; - return AWS_ERROR_SUCCESS; + return AWS_OP_SUCCESS; } static struct utf8_example s_valid_utf8_examples_for_callback[] = { diff --git a/tests/error_test.c b/tests/error_test.c index b7a241ec8..409dc5a34 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -399,13 +399,15 @@ static void s_error_thread_fn(void *arg) { static int s_error_code_cross_thread_test_fn(struct aws_allocator *allocator, void *ctx) { (void)ctx; - struct error_thread_test_data test_data = {.thread_1_code = 0, - .thread_1_get_last_code = 0, - .thread_1_encountered_count = 0, - .thread_2_code = 0, - .thread_2_get_last_code = 0, - .thread_2_encountered_count = 0, - .thread_2_id = 0}; + struct error_thread_test_data test_data = { + .thread_1_code = 0, + .thread_1_get_last_code = 0, + .thread_1_encountered_count = 0, + .thread_2_code = 0, + .thread_2_get_last_code = 0, + .thread_2_encountered_count = 0, + .thread_2_id = 0, + }; test_data.thread_1_id = aws_thread_current_thread_id(); diff --git a/tests/file_test.c b/tests/file_test.c index 6eedd264e..6a3f4fe8b 100644 --- a/tests/file_test.c +++ b/tests/file_test.c @@ -2,6 +2,7 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ +#include #include #include @@ -439,3 +440,105 @@ static int s_test_normalize_windows_directory_separator(struct aws_allocator *al } AWS_TEST_CASE(test_normalize_windows_directory_separator, s_test_normalize_windows_directory_separator); + +static int s_check_byte_buf_from_file(const struct aws_byte_buf *buf, struct aws_byte_cursor expected_contents) { + ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected_contents, buf), "Contents should match"); + ASSERT_TRUE(buf->capacity > buf->len, "Buffer should end with null-terminator"); + ASSERT_UINT_EQUALS(0, buf->buffer[buf->len], "Buffer should end with null-terminator"); + return AWS_OP_SUCCESS; +} + +static int s_create_file_then_read_it(struct aws_allocator *allocator, struct aws_byte_cursor contents) { + /* create file */ + const char *filename = "testy"; + FILE *f = aws_fopen(filename, "wb"); + ASSERT_UINT_EQUALS(contents.len, fwrite(contents.ptr, 1, contents.len, f)); + ASSERT_INT_EQUALS(0, fclose(f)); + + struct aws_byte_buf buf; + + /* check aws_byte_buf_init_from_file() */ + ASSERT_SUCCESS(aws_byte_buf_init_from_file(&buf, allocator, filename)); + ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); + aws_byte_buf_clean_up(&buf); + + /* now check aws_byte_buf_init_from_file_with_size_hint() ... */ + + /* size_hint more then big enough */ + size_t size_hint = contents.len * 2; + ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); + ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); + aws_byte_buf_clean_up(&buf); + + /* size_hint not big enough for null-terminator */ + size_hint = contents.len; + ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); + ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); + aws_byte_buf_clean_up(&buf); + + /* size_hint 0 */ + size_hint = 0; + ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); + ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); + aws_byte_buf_clean_up(&buf); + + /* size_hint 1 */ + size_hint = 1; + ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); + ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); + aws_byte_buf_clean_up(&buf); + + remove(filename); + return AWS_OP_SUCCESS; +} + +/* Read an actual "special file" (if it exists on this machine) */ +static int s_read_special_file(struct aws_allocator *allocator, const char *filename) { + struct aws_string *filename_str = aws_string_new_from_c_str(allocator, filename); + bool exists = aws_path_exists(filename_str); + aws_string_destroy(filename_str); + if (!exists) { + return AWS_OP_SUCCESS; + } + + struct aws_byte_buf buf; + ASSERT_SUCCESS(aws_byte_buf_init_from_file(&buf, allocator, filename)); + ASSERT_TRUE(buf.capacity > buf.len, "Buffer should end with null-terminator"); + ASSERT_UINT_EQUALS(0, buf.buffer[buf.len], "Buffer should end with null-terminator"); + + if (strcmp("/dev/null", filename) == 0) { + ASSERT_UINT_EQUALS(0, buf.len, "expected /dev/null to be empty"); + } else { + ASSERT_TRUE(buf.len > 0, "expected special file to have data"); + } + + aws_byte_buf_clean_up(&buf); + return AWS_OP_SUCCESS; +} + +static int s_test_byte_buf_init_from_file(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + + /* simple text file */ + ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_c_str("asdf"))); + + /* empty file */ + ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_c_str(""))); + + /* large 3MB+1byte binary file */ + struct aws_byte_buf big_rando; + aws_byte_buf_init(&big_rando, allocator, (1024 * 1024 * 3) + 1); + ASSERT_SUCCESS(aws_device_random_buffer(&big_rando)); + ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_buf(&big_rando))); + aws_byte_buf_clean_up(&big_rando); + + /* test some "special files" (if they exist) */ + ASSERT_SUCCESS(s_read_special_file(allocator, "/proc/cpuinfo")); + ASSERT_SUCCESS(s_read_special_file(allocator, "/proc/net/tcp")); + ASSERT_SUCCESS(s_read_special_file(allocator, "/sys/devices/virtual/dmi/id/sys_vendor")); + ASSERT_SUCCESS(s_read_special_file(allocator, "/dev/null")); + + return AWS_OP_SUCCESS; +} + +AWS_TEST_CASE(test_byte_buf_init_from_file, s_test_byte_buf_init_from_file) diff --git a/tests/fuzz/cbor_decoding_transitive.c b/tests/fuzz/cbor_decoding_transitive.c new file mode 100644 index 000000000..3815db6cf --- /dev/null +++ b/tests/fuzz/cbor_decoding_transitive.c @@ -0,0 +1,87 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include + +/* NOLINTNEXTLINE(readability-identifier-naming) */ +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); + struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size); + aws_common_library_init(allocator); + + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, input); + union { + uint64_t unsigned_val; + uint64_t neg_val; + double double_val; + uint64_t tag_val; + bool boolean_val; + struct aws_byte_cursor str_val; + struct aws_byte_cursor bytes_val; + uint64_t map_start; + uint64_t array_start; + } cbor_data; + + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + while (aws_cbor_decoder_peek_type(decoder, &out_type) == AWS_OP_SUCCESS) { + switch (out_type) { + case AWS_CBOR_TYPE_UINT: + AWS_FATAL_ASSERT( + aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &cbor_data.unsigned_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_NEGINT: + AWS_FATAL_ASSERT( + aws_cbor_decoder_pop_next_negative_int_val(decoder, &cbor_data.neg_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_FLOAT: + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_float_val(decoder, &cbor_data.double_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_BYTES: + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_bytes_val(decoder, &cbor_data.bytes_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_TEXT: + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_text_val(decoder, &cbor_data.str_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_ARRAY_START: + AWS_FATAL_ASSERT( + aws_cbor_decoder_pop_next_array_start(decoder, &cbor_data.array_start) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_MAP_START: + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_map_start(decoder, &cbor_data.map_start) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_TAG: + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_tag_val(decoder, &cbor_data.tag_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_BOOL: + AWS_FATAL_ASSERT( + aws_cbor_decoder_pop_next_boolean_val(decoder, &cbor_data.boolean_val) == AWS_OP_SUCCESS); + break; + case AWS_CBOR_TYPE_NULL: + case AWS_CBOR_TYPE_UNDEFINED: + case AWS_CBOR_TYPE_BREAK: + case AWS_CBOR_TYPE_INDEF_BYTES_START: + case AWS_CBOR_TYPE_INDEF_TEXT_START: + case AWS_CBOR_TYPE_INDEF_ARRAY_START: + case AWS_CBOR_TYPE_INDEF_MAP_START: { + enum aws_cbor_type type = AWS_CBOR_TYPE_UNKNOWN; + AWS_FATAL_ASSERT(aws_cbor_decoder_peek_type(decoder, &type) == AWS_OP_SUCCESS); + AWS_FATAL_ASSERT(type == out_type); + AWS_FATAL_ASSERT(aws_cbor_decoder_consume_next_single_element(decoder) == AWS_OP_SUCCESS); + } break; + + default: + break; + } + } + aws_cbor_decoder_destroy(decoder); + + atexit(aws_common_library_clean_up); + + /* Check for leaks */ + AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0); + allocator = aws_mem_tracer_destroy(allocator); + return 0; +} diff --git a/tests/fuzz/cbor_double_encode_decode.c b/tests/fuzz/cbor_double_encode_decode.c new file mode 100644 index 000000000..744d1cff8 --- /dev/null +++ b/tests/fuzz/cbor_double_encode_decode.c @@ -0,0 +1,66 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include + +/* NOLINTNEXTLINE(readability-identifier-naming) */ +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); + struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size); + double val = 0; + if (!aws_byte_cursor_read_float_be64(&input, &val)) { + allocator = aws_mem_tracer_destroy(allocator); + /* Ignore the invalid double */ + return 0; + } + aws_common_library_init(allocator); + struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator); + aws_cbor_encoder_write_float(encoder, val); + + struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder); + struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, final_cursor); + enum aws_cbor_type out_type = AWS_CBOR_TYPE_UNKNOWN; + AWS_FATAL_ASSERT(aws_cbor_decoder_peek_type(decoder, &out_type) == 0); + switch (out_type) { + case AWS_CBOR_TYPE_UINT: { + uint64_t result = 0; + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_unsigned_int_val(decoder, &result) == 0); + AWS_FATAL_ASSERT((double)result == val); + break; + } + case AWS_CBOR_TYPE_NEGINT: { + uint64_t result = 0; + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_negative_int_val(decoder, &result) == 0); + int64_t expected_val = -1 - result; + AWS_FATAL_ASSERT(expected_val == (int64_t)val); + break; + } + case AWS_CBOR_TYPE_FLOAT: { + double result = 0; + AWS_FATAL_ASSERT(aws_cbor_decoder_pop_next_float_val(decoder, &result) == 0); + if (isnan(val)) { + AWS_FATAL_ASSERT(isnan(result)); + } else { + AWS_FATAL_ASSERT(result == val); + } + break; + } + + default: + AWS_FATAL_ASSERT(false); + break; + } + AWS_FATAL_ASSERT(aws_cbor_decoder_get_remaining_length(decoder) == 0); + aws_cbor_encoder_destroy(encoder); + aws_cbor_decoder_destroy(decoder); + + atexit(aws_common_library_clean_up); + + /* Check for leaks */ + AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0); + allocator = aws_mem_tracer_destroy(allocator); + return 0; +} diff --git a/tests/host_util_test.c b/tests/host_util_test.c new file mode 100644 index 000000000..1f35f8b7f --- /dev/null +++ b/tests/host_util_test.c @@ -0,0 +1,73 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include +#include + +AWS_TEST_CASE(host_util_is_ipv4, s_test_is_ipv4) +static int s_test_is_ipv4(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + + ASSERT_TRUE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("0.0.0.0"))); + ASSERT_TRUE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127.0.0.1"))); + ASSERT_TRUE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("255.255.255.255"))); + ASSERT_TRUE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("192.168.1.1"))); + + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("256.0.0.1"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127.0.0"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127.0"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str(""))); + + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("foo.com"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("a.b.c.d"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("a127.0.0.1"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127.0.0.1a"))); + ASSERT_FALSE(aws_host_utils_is_ipv4(aws_byte_cursor_from_c_str("127.0.0.1011"))); + + return AWS_OP_SUCCESS; +} + +AWS_TEST_CASE(host_util_is_ipv6, s_test_is_ipv6) +static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("0:0:0000:0000:0000:0:0:0"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:7334"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0DB8:0000:0000:0000:8a2e:0370:7334"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%en0"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::1"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("0:0:0:0:0:0:0:1"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fd00:ec2::23"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fd00:ec2:0:0:0:0:0:23"), false)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:7334"), true)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1"), true)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0"), true)); + ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348"), true)); + + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370"), false)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:"), false)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001::"), false)); + ASSERT_FALSE( + aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false)); + ASSERT_FALSE( + aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str(":2001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false)); + ASSERT_FALSE( + aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001::8a2e::8745"), false)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::2001:0db8:0000:0000:8a2e:0370:7334"), false)); + + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%en0"), true)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%24en0"), true)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%25en0"), true)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0]"), true)); + ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25"), true)); + + return AWS_OP_SUCCESS; +} diff --git a/tests/linked_list_test.c b/tests/linked_list_test.c index d9226c602..2b29a5645 100644 --- a/tests/linked_list_test.c +++ b/tests/linked_list_test.c @@ -518,3 +518,26 @@ AWS_TEST_CASE(linked_list_reverse_iteration, s_test_linked_list_reverse_iteratio AWS_TEST_CASE(linked_list_swap_contents, s_test_linked_list_swap_contents) AWS_TEST_CASE(linked_list_move_all_back, s_test_linked_list_move_all_back) AWS_TEST_CASE(linked_list_move_all_front, s_test_linked_list_move_all_front) + +static int s_linked_list_node_in_list_fn(struct aws_allocator *allocator, void *ctx) { + (void)allocator; + (void)ctx; + + struct aws_linked_list list; + aws_linked_list_init(&list); + + struct aws_linked_list_node node; + AWS_ZERO_STRUCT(node); + + ASSERT_FALSE(aws_linked_list_node_is_in_list(&node)); + + aws_linked_list_push_back(&list, &node); + ASSERT_TRUE(aws_linked_list_node_is_in_list(&node)); + aws_linked_list_remove(&node); + + ASSERT_FALSE(aws_linked_list_node_is_in_list(&node)); + + return AWS_OP_SUCCESS; +} + +AWS_TEST_CASE(linked_list_node_in_list, s_linked_list_node_in_list_fn) diff --git a/tests/math_test.c b/tests/math_test.c index d23dc7583..dc22d8265 100644 --- a/tests/math_test.c +++ b/tests/math_test.c @@ -257,21 +257,21 @@ static int s_test_mul_u32_checked_fn(struct aws_allocator *allocator, void *ctx) CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0, 0, 0); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0, 1, 0); - CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0, ~0u, 0); + CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0, ~0U, 0); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 4, 5, 20); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 1234, 4321, 5332114); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0xFFFFFFFF, 1, 0xFFFFFFFF); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0xFFFF, 1, 0xFFFF); - CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0xFFFF, 0xFFFF, 0xfffe0001u); - CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0x10000, 0xFFFF, 0xFFFF0000u); + CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0xFFFF, 0xFFFF, 0xfffe0001U); + CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0x10000, 0xFFFF, 0xFFFF0000U); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0x10001, 0xFFFF, 0xFFFFFFFFu); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0x10001, 0xFFFE, 0xFFFEFFFEu); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0x10002, 0xFFFE, 0xFFFFFFFCu); CHECK_OVF(aws_mul_u32_checked, uint32_t, 0x10003, 0xFFFE); CHECK_NO_OVF(aws_mul_u32_checked, uint32_t, 0xFFFE, 0xFFFE, 0xFFFC0004u); CHECK_OVF(aws_mul_u32_checked, uint32_t, 0x1FFFF, 0x1FFFF); - CHECK_OVF(aws_mul_u32_checked, uint32_t, ~0u, ~0u); + CHECK_OVF(aws_mul_u32_checked, uint32_t, ~0U, ~0U); return 0; } @@ -285,21 +285,21 @@ static int s_test_mul_size_checked_fn(struct aws_allocator *allocator, void *ctx #if SIZE_BITS == 32 CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, 0, 0); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, 1, 0); - CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, ~0u, 0); + CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, ~0U, 0); CHECK_NO_OVF(aws_mul_size_checked, size_t, 4, 5, 20); CHECK_NO_OVF(aws_mul_size_checked, size_t, 1234, 4321, 5332114); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0xFFFFFFFF, 1, 0xFFFFFFFF); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0xFFFF, 1, 0xFFFF); - CHECK_NO_OVF(aws_mul_size_checked, size_t, 0xFFFF, 0xFFFF, 0xfffe0001u); - CHECK_NO_OVF(aws_mul_size_checked, size_t, 0x10000, 0xFFFF, 0xFFFF0000u); + CHECK_NO_OVF(aws_mul_size_checked, size_t, 0xFFFF, 0xFFFF, 0xfffe0001U); + CHECK_NO_OVF(aws_mul_size_checked, size_t, 0x10000, 0xFFFF, 0xFFFF0000U); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0x10001, 0xFFFF, 0xFFFFFFFFu); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0x10001, 0xFFFE, 0xFFFEFFFEu); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0x10002, 0xFFFE, 0xFFFFFFFCu); CHECK_OVF(aws_mul_size_checked, size_t, 0x10003, 0xFFFE); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0xFFFE, 0xFFFE, 0xFFFC0004u); CHECK_OVF(aws_mul_size_checked, size_t, 0x1FFFF, 0x1FFFF); - CHECK_OVF(aws_mul_size_checked, size_t, ~0u, ~0u); + CHECK_OVF(aws_mul_size_checked, size_t, ~0U, ~0U); #elif SIZE_BITS == 64 CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, 0, 0); CHECK_NO_OVF(aws_mul_size_checked, size_t, 0, 1, 0); diff --git a/tests/memtrace_test.c b/tests/memtrace_test.c index 474d2a422..b3be1b7e9 100644 --- a/tests/memtrace_test.c +++ b/tests/memtrace_test.c @@ -63,32 +63,36 @@ static int s_test_memtrace_count(struct aws_allocator *allocator, void *ctx) { } AWS_TEST_CASE(test_memtrace_count, s_test_memtrace_count) -#if defined(__GNUC__) || defined(__clang__) -# define AWS_PREVENT_OPTIMIZATION __asm__ __volatile__("" ::: "memory") -#else -# define AWS_PREVENT_OPTIMIZATION +/* Disable optimization for tests to make sure those allocation function still exists. */ +#if defined(__clang__) +# pragma clang optimize off +#elif defined(__GNUC__) || defined(__GNUG__) +# pragma GCC push_options +# pragma GCC optimize("O0") #endif AWS_NO_INLINE void *s_alloc_1(struct aws_allocator *allocator, size_t size) { - AWS_PREVENT_OPTIMIZATION; return aws_mem_acquire(allocator, size); } AWS_NO_INLINE void *s_alloc_2(struct aws_allocator *allocator, size_t size) { - AWS_PREVENT_OPTIMIZATION; return aws_mem_acquire(allocator, size); } AWS_NO_INLINE void *s_alloc_3(struct aws_allocator *allocator, size_t size) { - AWS_PREVENT_OPTIMIZATION; return aws_mem_acquire(allocator, size); } AWS_NO_INLINE void *s_alloc_4(struct aws_allocator *allocator, size_t size) { - AWS_PREVENT_OPTIMIZATION; return aws_mem_acquire(allocator, size); } +#if defined(__clang__) +# pragma clang optimize on +#elif defined(__GNUC__) || defined(__GNUG__) +# pragma GCC pop_options +#endif + static struct aws_logger s_test_logger; static int s_test_memtrace_stacks(struct aws_allocator *allocator, void *ctx) { diff --git a/tests/priority_queue_test.c b/tests/priority_queue_test.c index 0e1257727..d0377b0f8 100644 --- a/tests/priority_queue_test.c +++ b/tests/priority_queue_test.c @@ -451,6 +451,41 @@ static int s_test_remove_interior_sift_down(struct aws_allocator *allocator, voi return 0; } +#define BACKPOINTER_CLEAR_NODE_COUNT 16 + +static int s_priority_queue_clear_backpointers_test(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + + struct aws_priority_queue queue; + + ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints)); + + struct aws_priority_queue_node queue_nodes[BACKPOINTER_CLEAR_NODE_COUNT]; + + for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) { + aws_priority_queue_node_init(&queue_nodes[i]); + } + + for (int i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; i++) { + aws_priority_queue_push_ref(&queue, &i, &queue_nodes[i]); + } + + for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) { + ASSERT_TRUE(aws_priority_queue_node_is_in_queue(&queue_nodes[i])); + } + + aws_priority_queue_clear(&queue); + ASSERT_INT_EQUALS(0, aws_priority_queue_size(&queue)); + + for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) { + ASSERT_FALSE(aws_priority_queue_node_is_in_queue(&queue_nodes[i])); + } + + aws_priority_queue_clean_up(&queue); + + return 0; +} + AWS_TEST_CASE(priority_queue_remove_interior_sift_down_test, s_test_remove_interior_sift_down); AWS_TEST_CASE(priority_queue_remove_interior_sift_up_test, s_test_remove_interior_sift_up); AWS_TEST_CASE(priority_queue_remove_leaf_test, s_test_remove_leaf); @@ -458,3 +493,4 @@ AWS_TEST_CASE(priority_queue_remove_root_test, s_test_remove_root); AWS_TEST_CASE(priority_queue_push_pop_order_test, s_test_priority_queue_preserves_order); AWS_TEST_CASE(priority_queue_random_values_test, s_test_priority_queue_random_values); AWS_TEST_CASE(priority_queue_size_and_capacity_test, s_test_priority_queue_size_and_capacity); +AWS_TEST_CASE(priority_queue_clear_backpointers_test, s_priority_queue_clear_backpointers_test); diff --git a/tests/promise_test.c b/tests/promise_test.c deleted file mode 100644 index 02f55ec85..000000000 --- a/tests/promise_test.c +++ /dev/null @@ -1,181 +0,0 @@ -/** - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0. - */ - -#include -#include -#include - -#include - -struct promise_test_work { - struct aws_allocator *allocator; - struct aws_promise *promise; - uint64_t work_time; - int error_code; - void *value; - void (*dtor)(void *); -}; - -static void s_promise_test_worker(void *data) { - struct promise_test_work *work = data; - aws_promise_acquire(work->promise); - aws_thread_current_sleep(work->work_time); - if (work->error_code) { - aws_promise_fail(work->promise, work->error_code); - } else { - aws_promise_complete(work->promise, work->value, work->dtor); - } - aws_promise_release(work->promise); -} - -static struct aws_thread s_promise_test_launch_worker(struct promise_test_work *work) { - const struct aws_thread_options *thread_options = aws_default_thread_options(); - AWS_FATAL_ASSERT(thread_options); - struct aws_thread worker_thread; - AWS_FATAL_ASSERT(aws_thread_init(&worker_thread, work->allocator) == AWS_OP_SUCCESS); - AWS_FATAL_ASSERT(aws_thread_launch(&worker_thread, s_promise_test_worker, work, thread_options) == AWS_OP_SUCCESS); - return worker_thread; -} - -struct pmr_payload { - struct aws_allocator *allocator; -}; - -void s_promise_test_free(void *ptr) { - struct pmr_payload *payload = ptr; - aws_mem_release(payload->allocator, payload); -} - -static int s_promise_test_wait_forever(struct aws_allocator *allocator, void *ctx) { - (void)ctx; - struct aws_promise *promise = aws_promise_new(allocator); - ASSERT_NOT_NULL(promise); - - struct pmr_payload *payload = aws_mem_acquire(allocator, 42); - payload->allocator = allocator; - - struct promise_test_work work = { - .allocator = allocator, - .promise = promise, - .work_time = 2 * 1000 * 1000, - .value = payload, - .dtor = s_promise_test_free, - }; - struct aws_thread worker_thread = s_promise_test_launch_worker(&work); - - aws_promise_wait(promise); - ASSERT_SUCCESS(aws_thread_join(&worker_thread)); - aws_promise_release(promise); - - return 0; -} - -AWS_TEST_CASE(promise_test_wait_forever, s_promise_test_wait_forever) - -static int s_promise_test_wait_for_a_bit(struct aws_allocator *allocator, void *ctx) { - (void)ctx; - struct aws_promise *promise = aws_promise_new(allocator); - ASSERT_NOT_NULL(promise); - - struct promise_test_work work = { - .allocator = allocator, - .promise = promise, - .work_time = 3 * 1000 * 1000, - }; - struct aws_thread worker_thread = s_promise_test_launch_worker(&work); - /* wait until the worker finishes, in 500ms intervals */ - while (!aws_promise_wait_for(promise, 500)) - ; - - ASSERT_TRUE(aws_promise_error_code(promise) == 0); - ASSERT_NULL(aws_promise_value(promise)); - - ASSERT_SUCCESS(aws_thread_join(&worker_thread)); - aws_promise_release(promise); - - return 0; -} - -AWS_TEST_CASE(promise_test_wait_for_a_bit, s_promise_test_wait_for_a_bit) - -static int s_promise_test_finish_immediately(struct aws_allocator *allocator, void *ctx) { - (void)ctx; - struct aws_promise *promise = aws_promise_new(allocator); - ASSERT_NOT_NULL(promise); - - struct promise_test_work work = { - .allocator = allocator, - .promise = promise, - .work_time = 0, - }; - struct aws_thread worker_thread = s_promise_test_launch_worker(&work); - aws_promise_wait(promise); - ASSERT_TRUE(aws_promise_error_code(promise) == 0); - ASSERT_NULL(aws_promise_value(promise)); - aws_promise_release(promise); - ASSERT_SUCCESS(aws_thread_join(&worker_thread)); - - return 0; -} - -AWS_TEST_CASE(promise_test_finish_immediately, s_promise_test_finish_immediately) - -static int s_promise_test_finish_before_wait(struct aws_allocator *allocator, void *ctx) { - (void)ctx; - struct aws_promise *promise = aws_promise_new(allocator); - ASSERT_NOT_NULL(promise); - - aws_promise_fail(promise, 1024); - aws_promise_wait(promise); - - ASSERT_TRUE(aws_promise_error_code(promise) == 1024); - ASSERT_NULL(aws_promise_value(promise)); - aws_promise_release(promise); - - return 0; -} - -AWS_TEST_CASE(promise_test_finish_before_wait, s_promise_test_finish_before_wait) - -void s_promise_test_waiter(void *data) { - struct promise_test_work *work = data; - aws_promise_acquire(work->promise); - /* sleep 0.2 seconds */ - aws_thread_current_sleep(1000 * 1000 * 2); - aws_promise_wait(work->promise); - AWS_FATAL_ASSERT(aws_promise_error_code(work->promise) == 0); - aws_promise_release(work->promise); -} - -static int s_promise_test_multiple_waiters(struct aws_allocator *allocator, void *ctx) { - (void)ctx; - struct aws_promise *promise = aws_promise_new(allocator); - ASSERT_NOT_NULL(promise); - - struct promise_test_work work = { - .allocator = allocator, - .promise = promise, - .work_time = 2 * 1000 * 1000, - .value = promise, - }; - struct aws_thread threads[8]; - const struct aws_thread_options *worker_options = aws_default_thread_options(); - for (int idx = 0; idx < AWS_ARRAY_SIZE(threads); ++idx) { - aws_thread_init(&threads[idx], allocator); - aws_thread_launch(&threads[idx], s_promise_test_waiter, &work, worker_options); - } - - aws_thread_current_sleep(1000 * 1000 * 4); - aws_promise_complete(promise, promise, NULL); - aws_promise_release(promise); - - for (int idx = 0; idx < AWS_ARRAY_SIZE(threads); ++idx) { - ASSERT_SUCCESS(aws_thread_join(&threads[idx])); - } - - return 0; -} - -AWS_TEST_CASE(promise_test_multiple_waiters, s_promise_test_multiple_waiters) diff --git a/tests/system_info_tests.c b/tests/system_info_tests.c index 8bd01ca4f..7a4324797 100644 --- a/tests/system_info_tests.c +++ b/tests/system_info_tests.c @@ -166,3 +166,22 @@ static int s_test_sanity_check_numa_discovery(struct aws_allocator *allocator, v } AWS_TEST_CASE(test_sanity_check_numa_discovery, s_test_sanity_check_numa_discovery) + +static int s_test_sanity_check_environment_loader(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + + aws_common_library_init(allocator); + struct aws_system_environment *env = aws_system_environment_load(allocator); + ASSERT_NOT_NULL(env); + struct aws_byte_cursor virt_vendor = aws_system_environment_get_virtualization_vendor(env); + ASSERT_TRUE(aws_byte_cursor_is_valid(&virt_vendor)); + struct aws_byte_cursor virt_product = aws_system_environment_get_virtualization_product_name(env); + ASSERT_TRUE(aws_byte_cursor_is_valid(&virt_product)); + + aws_system_environment_release(env); + + aws_common_library_clean_up(); + return 0; +} + +AWS_TEST_CASE(test_sanity_check_environment_loader, s_test_sanity_check_environment_loader) diff --git a/tests/system_resource_util_test.c b/tests/system_resource_util_test.c new file mode 100644 index 000000000..462be8574 --- /dev/null +++ b/tests/system_resource_util_test.c @@ -0,0 +1,37 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include +#include + +#include + +static int s_test_memory_usage_maxrss(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + + /* + * Note: mem usage apis currently rely on getrusage on posix systems. + * On freebsd maxrss seems to return current process rss based on testing, + * while on every other posix platform maxrss is high water mark for rss + * over the program lifetime. + * Workaround it by allocating a buffer first. Long term using procfs should + * avoid the issue. + */ + struct aws_byte_buf temp; + aws_byte_buf_init(&temp, allocator, 8 * 1024 * 1024); + ASSERT_SUCCESS(aws_device_random_buffer(&temp)); + + struct aws_memory_usage_stats mu; + ASSERT_SUCCESS(aws_init_memory_usage_for_current_process(&mu)); + + ASSERT_TRUE(mu.maxrss > 0); + + aws_byte_buf_clean_up_secure(&temp); + + return 0; +} + +AWS_TEST_CASE(test_memory_usage_maxrss, s_test_memory_usage_maxrss) diff --git a/tests/thread_test.c b/tests/thread_test.c index 4cf3278b9..1b1582131 100644 --- a/tests/thread_test.c +++ b/tests/thread_test.c @@ -70,6 +70,40 @@ static int s_test_thread_creation_join_fn(struct aws_allocator *allocator, void AWS_TEST_CASE(thread_creation_join_test, s_test_thread_creation_join_fn) +static int s_test_thread_creation_join_invalid_cpu_id_fn(struct aws_allocator *allocator, void *ctx) { + (void)ctx; + aws_common_library_init(allocator); + struct thread_test_data test_data = {.allocator = allocator}; + + struct aws_thread thread; + aws_thread_init(&thread, allocator); + + struct aws_thread_options thread_options = *aws_default_thread_options(); + /* invalid cpu_id. Ensure that the cpu_id is best-effort based. */ + thread_options.cpu_id = 512; + + ASSERT_SUCCESS( + aws_thread_launch(&thread, s_thread_fn, (void *)&test_data, &thread_options), "thread creation failed"); + ASSERT_INT_EQUALS( + AWS_THREAD_JOINABLE, aws_thread_get_detach_state(&thread), "thread state should have returned JOINABLE"); + ASSERT_SUCCESS(aws_thread_join(&thread), "thread join failed"); + ASSERT_TRUE( + aws_thread_thread_id_equal(test_data.thread_id, aws_thread_get_id(&thread)), + "get_thread_id should have returned the same id as the thread calling current_thread_id"); + ASSERT_INT_EQUALS( + AWS_THREAD_JOIN_COMPLETED, + aws_thread_get_detach_state(&thread), + "thread state should have returned JOIN_COMPLETED"); + + aws_string_destroy(test_data.thread_name); + aws_thread_clean_up(&thread); + aws_common_library_clean_up(); + + return 0; +} + +AWS_TEST_CASE(thread_creation_join_invalid_cpu_id_test, s_test_thread_creation_join_invalid_cpu_id_fn) + static uint32_t s_atexit_call_count = 0; static void s_thread_atexit_fn(void *user_data) { (void)user_data; @@ -209,7 +243,7 @@ static int s_test_managed_thread_join_timeout(struct aws_allocator *allocator, v /* * Increase the timeout and shut down */ - aws_thread_set_managed_join_timeout_ns(aws_timestamp_convert(5, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); + aws_thread_set_managed_join_timeout_ns(aws_timestamp_convert(10, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); aws_common_library_clean_up(); diff --git a/tests/uri_test.c b/tests/uri_test.c index 7823376df..410ff411d 100644 --- a/tests/uri_test.c +++ b/tests/uri_test.c @@ -62,7 +62,7 @@ static int s_test_uri_no_scheme_parse(struct aws_allocator *allocator, void *ctx struct aws_uri uri; ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr)); - ASSERT_UINT_EQUALS(0u, uri.scheme.len); + ASSERT_UINT_EQUALS(0U, uri.scheme.len); struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443"); ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len); @@ -194,7 +194,7 @@ static int s_test_uri_no_query_parse(struct aws_allocator *allocator, void *ctx) ASSERT_BIN_ARRAYS_EQUALS( expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -210,7 +210,7 @@ static int s_test_uri_minimal_parse(struct aws_allocator *allocator, void *ctx) struct aws_uri uri; ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr)); - ASSERT_UINT_EQUALS(0u, uri.scheme.len); + ASSERT_UINT_EQUALS(0U, uri.scheme.len); struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com"); ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len); @@ -227,7 +227,7 @@ static int s_test_uri_minimal_parse(struct aws_allocator *allocator, void *ctx) ASSERT_BIN_ARRAYS_EQUALS( expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -243,8 +243,8 @@ static int s_test_uri_path_and_query_only_parse(struct aws_allocator *allocator, struct aws_uri uri; ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr)); - ASSERT_UINT_EQUALS(0u, uri.scheme.len); - ASSERT_UINT_EQUALS(0u, uri.authority.len); + ASSERT_UINT_EQUALS(0U, uri.scheme.len); + ASSERT_UINT_EQUALS(0U, uri.authority.len); struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource"); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); @@ -287,7 +287,7 @@ static int s_test_uri_root_only_parse(struct aws_allocator *allocator, void *ctx struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -317,7 +317,7 @@ static int s_test_uri_root_slash_only_path_parse(struct aws_allocator *allocator struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/"); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -347,7 +347,7 @@ static int s_test_uri_userinfo_no_password_parse(struct aws_allocator *allocator struct aws_byte_cursor expected_user = aws_byte_cursor_from_c_str("some_name"); ASSERT_BIN_ARRAYS_EQUALS(expected_user.ptr, expected_user.len, uri.user.ptr, uri.user.len); - ASSERT_UINT_EQUALS(0u, uri.password.len); + ASSERT_UINT_EQUALS(0U, uri.password.len); struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com"); ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len); @@ -357,7 +357,7 @@ static int s_test_uri_userinfo_no_password_parse(struct aws_allocator *allocator struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -382,9 +382,9 @@ static int s_test_uri_empty_user_parse(struct aws_allocator *allocator, void *ct struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("@www.test.com"); ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len); - ASSERT_UINT_EQUALS(0u, uri.userinfo.len); - ASSERT_UINT_EQUALS(0u, uri.user.len); - ASSERT_UINT_EQUALS(0u, uri.password.len); + ASSERT_UINT_EQUALS(0U, uri.userinfo.len); + ASSERT_UINT_EQUALS(0U, uri.user.len); + ASSERT_UINT_EQUALS(0U, uri.password.len); struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com"); ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len); @@ -394,7 +394,7 @@ static int s_test_uri_empty_user_parse(struct aws_allocator *allocator, void *ct struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -428,7 +428,7 @@ static int s_test_uri_query_params(struct aws_allocator *allocator, void *ctx) { expected_key = aws_byte_cursor_from_c_str("testkeyonly"); ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[1].key.ptr, params[1].key.len); - ASSERT_UINT_EQUALS(0u, params[1].value.len); + ASSERT_UINT_EQUALS(0U, params[1].value.len); expected_key = aws_byte_cursor_from_c_str("test%20space"); expected_value = aws_byte_cursor_from_c_str("value%20space"); @@ -486,7 +486,7 @@ static int s_test_uri_ipv4_parse(struct aws_allocator *allocator, void *ctx) { struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -509,7 +509,7 @@ static int s_test_uri_ipv6_parse(struct aws_allocator *allocator, void *ctx) { aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]:443"); ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len); - struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]"); + struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0"); ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len); ASSERT_UINT_EQUALS(443, uri.port); @@ -517,7 +517,7 @@ static int s_test_uri_ipv6_parse(struct aws_allocator *allocator, void *ctx) { struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -540,13 +540,13 @@ static int s_test_uri_ipv6_no_port_parse(struct aws_allocator *allocator, void * aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]"); ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len); - struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]"); + struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0"); ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len); struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(""); ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len); - ASSERT_UINT_EQUALS(0u, uri.query_string.len); + ASSERT_UINT_EQUALS(0U, uri.query_string.len); aws_uri_clean_up(&uri); return AWS_OP_SUCCESS; @@ -582,7 +582,7 @@ AWS_TEST_CASE(uri_invalid_port_parse, s_test_uri_invalid_port_parse); static int s_test_uri_port_too_large_parse(struct aws_allocator *allocator, void *ctx) { (void)ctx; - const char *str_uri = "https://www.test.com:844356/path/to/" + const char *str_uri = "https://www.test.com:4294967296/path/to/" "resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3"; struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri); diff --git a/verification/cbmc/include/proof_helpers/aws_byte_cursor_read_common.h b/verification/cbmc/include/proof_helpers/aws_byte_cursor_read_common.h index ab159dcbd..2ff5b8919 100644 --- a/verification/cbmc/include/proof_helpers/aws_byte_cursor_read_common.h +++ b/verification/cbmc/include/proof_helpers/aws_byte_cursor_read_common.h @@ -6,7 +6,7 @@ #include #include -void aws_byte_cursor_read_common_harness() { +void aws_byte_cursor_read_common_harness(void) { /* parameters */ struct aws_byte_cursor cur; DEST_TYPE *dest = malloc(sizeof(*dest)); diff --git a/verification/cbmc/include/proof_helpers/make_common_data_structures.h b/verification/cbmc/include/proof_helpers/make_common_data_structures.h index dde63ae35..139a9f7f5 100644 --- a/verification/cbmc/include/proof_helpers/make_common_data_structures.h +++ b/verification/cbmc/include/proof_helpers/make_common_data_structures.h @@ -127,7 +127,7 @@ void hash_proof_destroy_noop(void *p); /** * Ensures a valid string is allocated, with as much nondet as possible */ -struct aws_string *ensure_string_is_allocated_nondet_length(); +struct aws_string *ensure_string_is_allocated_nondet_length(void); /** * Ensures a valid string is allocated, with as much nondet as possible, but len < max diff --git a/verification/cbmc/include/proof_helpers/nondet.h b/verification/cbmc/include/proof_helpers/nondet.h index 2fc4ac306..5a960ba3e 100644 --- a/verification/cbmc/include/proof_helpers/nondet.h +++ b/verification/cbmc/include/proof_helpers/nondet.h @@ -12,11 +12,11 @@ /** * Non-determinstic functions used in CBMC proofs */ -bool nondet_bool(); -int nondet_int(); -size_t nondet_size_t(); -uint16_t nondet_uint16_t(); -uint32_t nondet_uint32_t(); -uint64_t nondet_uint64_t(); -uint8_t nondet_uint8_t(); -void *nondet_voidp(); +bool nondet_bool(void); +int nondet_int(void); +size_t nondet_size_t(void); +uint16_t nondet_uint16_t(void); +uint32_t nondet_uint32_t(void); +uint64_t nondet_uint64_t(void); +uint8_t nondet_uint8_t(void); +void *nondet_voidp(void); diff --git a/verification/cbmc/proofs/Makefile.common b/verification/cbmc/proofs/Makefile.common index 1c05c2ef4..e0897c04d 100644 --- a/verification/cbmc/proofs/Makefile.common +++ b/verification/cbmc/proofs/Makefile.common @@ -465,7 +465,6 @@ COMMA :=, # Set C compiler defines CBMCFLAGS += --object-bits $(CBMC_OBJECT_BITS) -COMPILE_FLAGS += --object-bits $(CBMC_OBJECT_BITS) DEFINES += -DCBMC=1 DEFINES += -DCBMC_OBJECT_BITS=$(CBMC_OBJECT_BITS) diff --git a/verification/cbmc/sources/make_common_data_structures.c b/verification/cbmc/sources/make_common_data_structures.c index b2e4ef457..16bfb467d 100644 --- a/verification/cbmc/sources/make_common_data_structures.c +++ b/verification/cbmc/sources/make_common_data_structures.c @@ -189,7 +189,7 @@ bool hash_table_state_has_an_empty_slot(const struct hash_table_state *const sta */ void hash_proof_destroy_noop(void *p) {} -struct aws_string *ensure_string_is_allocated_nondet_length() { +struct aws_string *ensure_string_is_allocated_nondet_length(void) { /* Considers any size up to the maximum possible size for the array [bytes] in aws_string */ return nondet_allocate_string_bounded_length(SIZE_MAX - 1 - sizeof(struct aws_string)); } diff --git a/verification/cbmc/sources/utils.c b/verification/cbmc/sources/utils.c index 231ddc8f0..0c511ef91 100644 --- a/verification/cbmc/sources/utils.c +++ b/verification/cbmc/sources/utils.c @@ -170,4 +170,7 @@ uint64_t uninterpreted_hasher(const void *a) { return __CPROVER_uninterpreted_hasher(a); } -bool uninterpreted_predicate_fn(uint8_t value); +bool __CPROVER_uninterpreted_predicate_uint8_t(uint8_t); +bool uninterpreted_predicate_fn(uint8_t value) { + return __CPROVER_uninterpreted_predicate_uint8_t(value); +} diff --git a/verification/cbmc/stubs/abort_override_assert_false.c b/verification/cbmc/stubs/abort_override_assert_false.c index 79798c3bc..e3b3a4b8a 100644 --- a/verification/cbmc/stubs/abort_override_assert_false.c +++ b/verification/cbmc/stubs/abort_override_assert_false.c @@ -11,6 +11,6 @@ #include -void abort() { +void abort(void) { assert(0); }