From 016ed4d1f11edc98e0678683563ecf9d2cbb5533 Mon Sep 17 00:00:00 2001 From: kelbon Date: Sat, 26 Jul 2025 10:41:31 +0500 Subject: [PATCH 1/3] sanitizers --- .github/workflows/build_and_test.yml | 26 ++++++++++++++++++++------ CMakeLists.txt | 10 ++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 856e514..9dadbb3 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -22,16 +22,30 @@ jobs: sudo ln -sf /usr/local/bin/ld /usr/bin/lld - name: Configure CMake run: | - cmake . -DHPACK_ENABLE_TESTING=ON \ - -DCMAKE_BUILD_TYPE=${{matrix.build_type}} \ - -DCMAKE_CXX_COMPILER=${{matrix.compiler}} \ - -DCMAKE_CXX_STANDARD=${{matrix.cpp_standard}} \ + cmake . -DHPACK_ENABLE_TESTING=ON \ + -DCMAKE_BUILD_TYPE=${{matrix.build_type}} \ + -DCMAKE_CXX_COMPILER=${{matrix.compiler}} \ + -DCMAKE_CXX_STANDARD=${{matrix.cpp_standard}} \ -B build -G "Ninja" - name: Build run: cmake --build build - - name: Test run: | cd build - ctest --output-on-failure -C ${{matrix.build_type}} -V + ctest --output-on-failure -C ${{matrix.build_type}} + - name: Configure CMake sanitizers + run: | + cmake . -DHPACK_ENABLE_TESTING=ON \ + -DCMAKE_BUILD_TYPE=${{matrix.build_type}} \ + -DCMAKE_CXX_COMPILER=${{matrix.compiler}} \ + -DCMAKE_CXX_STANDARD=${{matrix.cpp_standard}} \ + -DHPACK_ENABLE_SANITIZERS=ON \ + -B build_sanitizers -G "Ninja" + - name: Build + run: + cmake --build build_sanitizers + - name: Test sanitizers + run: | + cd build_sanitizers + ctest --output-on-failure -C ${{matrix.build_type}} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c6c89c..ed73517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,16 @@ project(HPACK LANGUAGES CXX) option(HPACK_ENABLE_TESTING "enables testing" OFF) option(HPACK_USE_CPM "turn off to not download boost" ON) +option(HPACK_ENABLE_SANITIZERS "enables tests with sanitizers. Ignored if HPACK_ENABLE_TESTING == 0" OFF) + +if (HPACK_ENABLE_SANITIZERS) + # enable sanitize flags globally, all targets require to be compiled with them + + # fno omit frame pointer to improve trace from sanitizers + add_compile_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer) + add_link_options(-fsanitize=address -fsanitize=undefined) + +endif() ### dependecies ### From c4dc40b3c934ef6cde2bb474d907daaaf2e92c18 Mon Sep 17 00:00:00 2001 From: kelbon Date: Sat, 26 Jul 2025 11:38:07 +0500 Subject: [PATCH 2/3] huffman table odr --- include/hpack/basic_types.hpp | 2 +- include/hpack/strings.hpp | 4 ++-- src/decoder.cpp | 3 --- src/huffman.cpp | 4 ++++ 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/hpack/basic_types.hpp b/include/hpack/basic_types.hpp index 23391a5..45c8757 100644 --- a/include/hpack/basic_types.hpp +++ b/include/hpack/basic_types.hpp @@ -27,7 +27,7 @@ struct sym_info_t { uint32_t bits; uint8_t bit_count; }; -extern const sym_info_t huffman_table[257]; +sym_info_t huffman_table_get(uint8_t index) noexcept; // uint16_t(-1) if not found uint16_t huffman_decode_table_find(sym_info_t info); diff --git a/include/hpack/strings.hpp b/include/hpack/strings.hpp index 2c5d651..23371bf 100644 --- a/include/hpack/strings.hpp +++ b/include/hpack/strings.hpp @@ -14,7 +14,7 @@ O encode_string_huffman(std::string_view str, O _out) { // (size should be before string and len in bits depends on 'len' value) size_type len_after_encode = 0; for (char c : str) - len_after_encode += huffman_table[uint8_t(c)].bit_count; + len_after_encode += huffman_table_get(uint8_t(c)).bit_count; *out = 0b1000'0000; // set H bit // % 8 for guarantee, that padding ALWAYS < 7 bits. Its makes no sense to add entire byte or more padding // https://datatracker.ietf.org/doc/html/rfc7541#section-5.2 @@ -33,7 +33,7 @@ O encode_string_huffman(std::string_view str, O _out) { } }; for (char c : str) { - sym_info_t bits = huffman_table[uint8_t(c)]; + sym_info_t bits = huffman_table_get(uint8_t(c)); for (int i = 0; i < bits.bit_count; ++i) push_bit(bits.bits & (1 << i)); } diff --git a/src/decoder.cpp b/src/decoder.cpp index ced305b..f86f653 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -28,9 +28,6 @@ namespace hpack { return size_t(huffman_str_len) * 8 / 5; } -// uint16_t(-1) if not found -uint16_t huffman_decode_table_find(sym_info_t info); - // precondition: in != e // note: 'len' must be decoded before calling this function template diff --git a/src/huffman.cpp b/src/huffman.cpp index 11b9355..8eb303a 100644 --- a/src/huffman.cpp +++ b/src/huffman.cpp @@ -23,6 +23,10 @@ constexpr sym_info_t huffman_table[257] = { #include "hpack/huffman_table.def" }; +sym_info_t huffman_table_get(uint8_t i) noexcept { + return huffman_table[i]; +} + uint16_t huffman_decode_table_find(sym_info_t info) { #define HUFFMAN_TABLE(index, bits, bitcount) \ case bits: \ From 941676227790ce7a15350ae692e1c09a78172bc6 Mon Sep 17 00:00:00 2001 From: kelbon Date: Sat, 26 Jul 2025 11:44:16 +0500 Subject: [PATCH 3/3] endl --- .github/workflows/build_and_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 9dadbb3..e4295ae 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -48,4 +48,4 @@ jobs: - name: Test sanitizers run: | cd build_sanitizers - ctest --output-on-failure -C ${{matrix.build_type}} \ No newline at end of file + ctest --output-on-failure -C ${{matrix.build_type}}