From 6064005eec85f805b18e23ee69f0e6f767685a75 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Thu, 28 Aug 2025 16:45:35 +0200 Subject: [PATCH 01/11] WIP: bytes2hexstring --- runtime/strings/bytes.cpp | 15 +++++++++++++++ unittests/runtime-strings/bytestest.cpp | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index 93aee9a15..960417abd 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -162,6 +162,21 @@ SortBytes hook_BYTES_string2bytes(SortString s) { return hook_BYTES_bytes2string(s); } +string *bytes2hexstring(string *b, size_t len) { + static const char hexchars[] = "0123456789abcdef"; + auto *result = static_cast(kore_alloc_token(sizeof(string) + len * 2)); + for (size_t i = 0; i < len; i++) { + result->data[i * 2] = hexchars[(b->data[i] >> 4) & 0xf]; + result->data[i * 2 + 1] = hexchars[b->data[i] & 0xf]; + } + init_with_len(result, len * 2); + return result; +} + +SortString hook_BYTES_bytes2hexstring(SortBytes b) { + return bytes2hexstring(b, len(b)); +} + SortBytes hook_BYTES_substr(SortBytes input, SortInt start, SortInt end) { uint64_t ustart = GET_UI(start); uint64_t uend = GET_UI(end); diff --git a/unittests/runtime-strings/bytestest.cpp b/unittests/runtime-strings/bytestest.cpp index 42ecaa216..1c1d3cdc4 100644 --- a/unittests/runtime-strings/bytestest.cpp +++ b/unittests/runtime-strings/bytestest.cpp @@ -20,6 +20,7 @@ mpz_ptr hook_BYTES_bytes2int(string *b, uint64_t endianness, uint64_t signedness); string *hook_BYTES_int2bytes(mpz_t len, mpz_t i, uint64_t endianness); string *hook_BYTES_bytes2string(string *b); +string *hook_BYTES_bytes2hexstring(string *b); string *hook_BYTES_string2bytes(string *s); string *hook_BYTES_substr(string *b, mpz_t start, mpz_t end); string *hook_BYTES_replaceAt(string *b, mpz_t start, string *b2); @@ -171,6 +172,19 @@ BOOST_AUTO_TEST_CASE(bytes2string) { BOOST_CHECK_EQUAL(0, memcmp(_1234->data, "1234", 4)); } +BOOST_AUTO_TEST_CASE(bytes2hexstring) { + auto empty = make_string(""); + auto res = hook_BYTES_bytes2hexstring(empty); + BOOST_CHECK(res != empty); + BOOST_CHECK_EQUAL(len(res), 0); + + auto _00ff = make_string("\x00\xff", 2); + res = hook_BYTES_bytes2hexstring(_00ff); + BOOST_CHECK(res != _00ff); + BOOST_CHECK_EQUAL(len(res), 4); + BOOST_CHECK_EQUAL(0, memcmp(res->data, "00ff", 4)); +} + BOOST_AUTO_TEST_CASE(string2bytes) { auto empty = make_string(""); auto res = hook_BYTES_string2bytes(empty); From 2c3696c7f1b8c3d318e2d3b9283fa6a58bfb6eaa Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Thu, 28 Aug 2025 17:18:52 +0200 Subject: [PATCH 02/11] Run clang-format --- runtime/strings/bytes.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index 960417abd..3c3ca3e92 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -163,8 +163,9 @@ SortBytes hook_BYTES_string2bytes(SortString s) { } string *bytes2hexstring(string *b, size_t len) { - static const char hexchars[] = "0123456789abcdef"; - auto *result = static_cast(kore_alloc_token(sizeof(string) + len * 2)); + static char const hexchars[] = "0123456789abcdef"; + auto *result + = static_cast(kore_alloc_token(sizeof(string) + len * 2)); for (size_t i = 0; i < len; i++) { result->data[i * 2] = hexchars[(b->data[i] >> 4) & 0xf]; result->data[i * 2 + 1] = hexchars[b->data[i] & 0xf]; From bf40a46218eba93e3f6fbffd4a0d105ff35f993c Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 09:58:06 +0200 Subject: [PATCH 03/11] Use pointer arithmetic over array index syntax --- runtime/strings/bytes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index 3c3ca3e92..d85b18978 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -167,8 +167,8 @@ string *bytes2hexstring(string *b, size_t len) { auto *result = static_cast(kore_alloc_token(sizeof(string) + len * 2)); for (size_t i = 0; i < len; i++) { - result->data[i * 2] = hexchars[(b->data[i] >> 4) & 0xf]; - result->data[i * 2 + 1] = hexchars[b->data[i] & 0xf]; + result->data[i * 2] = hexchars[(*(b->data + i) >> 4) & 0xf]; + result->data[i * 2 + 1] = hexchars[(*(b->data + i)) & 0xf]; } init_with_len(result, len * 2); return result; From b4ee855820c1027b96308d6f26b7dbabcd095fa8 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 10:59:04 +0200 Subject: [PATCH 04/11] Fix coding style --- runtime/strings/bytes.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index d85b18978..d3da9c559 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -5,6 +5,7 @@ #include #include #include +#include // Add this include for std::array #include "runtime/alloc.h" #include "runtime/header.h" @@ -163,12 +164,12 @@ SortBytes hook_BYTES_string2bytes(SortString s) { } string *bytes2hexstring(string *b, size_t len) { - static char const hexchars[] = "0123456789abcdef"; - auto *result - = static_cast(kore_alloc_token(sizeof(string) + len * 2)); + static const std::array hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + auto *result = static_cast(kore_alloc_token(sizeof(string) + len * 2)); for (size_t i = 0; i < len; i++) { - result->data[i * 2] = hexchars[(*(b->data + i) >> 4) & 0xf]; - result->data[i * 2 + 1] = hexchars[(*(b->data + i)) & 0xf]; + result->data[i * 2] = hexchars.at((b->data[i] >> 4) & 0xf); + result->data[i * 2 + 1] = hexchars.at(b->data[i] & 0xf); } init_with_len(result, len * 2); return result; From 403ee78ae680ecf93f3726c4d8456e05274ce701 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 11:05:49 +0200 Subject: [PATCH 05/11] Fix coding style --- runtime/strings/bytes.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index d3da9c559..666dc61d0 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -1,11 +1,11 @@ #include +#include // Add this include for std::array #include #include #include #include #include #include -#include // Add this include for std::array #include "runtime/alloc.h" #include "runtime/header.h" @@ -164,9 +164,11 @@ SortBytes hook_BYTES_string2bytes(SortString s) { } string *bytes2hexstring(string *b, size_t len) { - static const std::array hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - auto *result = static_cast(kore_alloc_token(sizeof(string) + len * 2)); + static const std::array hexchars + = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + auto *result + = static_cast(kore_alloc_token(sizeof(string) + len * 2)); for (size_t i = 0; i < len; i++) { result->data[i * 2] = hexchars.at((b->data[i] >> 4) & 0xf); result->data[i * 2 + 1] = hexchars.at(b->data[i] & 0xf); From 058f00d1a325b51650dd9889c70a64f5f48c035c Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 11:24:32 +0200 Subject: [PATCH 06/11] Ignore shellcheck false positive --- bin/llvm-krun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/llvm-krun b/bin/llvm-krun index 6095401f6..c4a6c40ea 100755 --- a/bin/llvm-krun +++ b/bin/llvm-krun @@ -9,7 +9,7 @@ expanded_input_file="$(mktemp tmp.in.XXXXXXXXXX)" parser_file="$(mktemp tmp.parse.XXXXXXXXXX)" temp_inputs=() -cleanup () { +cleanup () { # shellcheck disable=SC2329 # shellcheck disable=SC2317 rm -rf "$input_file" "$expanded_input_file" "$output_file" "$parser_file" "${temp_inputs[@]}" } From bffdfa4c469d3d1c74d9fc19ae5a2631542c2bc0 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 11:40:24 +0200 Subject: [PATCH 07/11] Move shellcheck directive --- bin/llvm-krun | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/llvm-krun b/bin/llvm-krun index c4a6c40ea..a27aea80f 100755 --- a/bin/llvm-krun +++ b/bin/llvm-krun @@ -9,7 +9,8 @@ expanded_input_file="$(mktemp tmp.in.XXXXXXXXXX)" parser_file="$(mktemp tmp.parse.XXXXXXXXXX)" temp_inputs=() -cleanup () { # shellcheck disable=SC2329 +# shellcheck disable=SC2329 +cleanup () { # shellcheck disable=SC2317 rm -rf "$input_file" "$expanded_input_file" "$output_file" "$parser_file" "${temp_inputs[@]}" } From ba49a3dfed91322f9b430ae171c29716e0d814c6 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 14:25:35 +0200 Subject: [PATCH 08/11] Address PR feedback - Added code comment - Renamed hook: bytes2hexstring -> bytes2hex - Improved test case - Use safe length to avoid potential out-of-bounds error --- runtime/strings/bytes.cpp | 29 +++++++++++-------------- unittests/runtime-strings/bytestest.cpp | 14 ++++++------ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index 666dc61d0..274842f54 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -148,39 +148,36 @@ hook_BYTES_int2bytes(SortInt len, SortInt i, SortEndianness endianness_ptr) { return result; } -string *bytes2string(string *b, size_t len) { - auto *result = static_cast(kore_alloc_token(sizeof(string) + len)); - memcpy(result->data, b->data, len); - init_with_len(result, len); - return result; -} - SortString hook_BYTES_bytes2string(SortBytes b) { - return bytes2string(b, len(b)); + auto len_b = len(b); + auto *result = static_cast(kore_alloc_token(sizeof(string) + len_b)); + memcpy(result->data, b->data, len_b); + init_with_len(result, len_b); + return result; } SortBytes hook_BYTES_string2bytes(SortString s) { return hook_BYTES_bytes2string(s); } -string *bytes2hexstring(string *b, size_t len) { +// Convert a bytes array to its hexadecimal string representation +// For example, the bytes array b'\x01\xef' becomes the string "01ef" +// syntax String ::= Bytes2Hex( Bytes ) +SortString hook_BYTES_bytes2hex(SortBytes b) { static const std::array hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + auto len_b = len(b); auto *result - = static_cast(kore_alloc_token(sizeof(string) + len * 2)); - for (size_t i = 0; i < len; i++) { + = static_cast(kore_alloc_token(sizeof(string) + len_b * 2)); + for (size_t i = 0; i < len_b; i++) { result->data[i * 2] = hexchars.at((b->data[i] >> 4) & 0xf); result->data[i * 2 + 1] = hexchars.at(b->data[i] & 0xf); } - init_with_len(result, len * 2); + init_with_len(result, len_b * 2); return result; } -SortString hook_BYTES_bytes2hexstring(SortBytes b) { - return bytes2hexstring(b, len(b)); -} - SortBytes hook_BYTES_substr(SortBytes input, SortInt start, SortInt end) { uint64_t ustart = GET_UI(start); uint64_t uend = GET_UI(end); diff --git a/unittests/runtime-strings/bytestest.cpp b/unittests/runtime-strings/bytestest.cpp index 1c1d3cdc4..d334b14b3 100644 --- a/unittests/runtime-strings/bytestest.cpp +++ b/unittests/runtime-strings/bytestest.cpp @@ -20,7 +20,7 @@ mpz_ptr hook_BYTES_bytes2int(string *b, uint64_t endianness, uint64_t signedness); string *hook_BYTES_int2bytes(mpz_t len, mpz_t i, uint64_t endianness); string *hook_BYTES_bytes2string(string *b); -string *hook_BYTES_bytes2hexstring(string *b); +string *hook_BYTES_bytes2hex(string *b); string *hook_BYTES_string2bytes(string *s); string *hook_BYTES_substr(string *b, mpz_t start, mpz_t end); string *hook_BYTES_replaceAt(string *b, mpz_t start, string *b2); @@ -172,17 +172,17 @@ BOOST_AUTO_TEST_CASE(bytes2string) { BOOST_CHECK_EQUAL(0, memcmp(_1234->data, "1234", 4)); } -BOOST_AUTO_TEST_CASE(bytes2hexstring) { +BOOST_AUTO_TEST_CASE(bytes2hex) { auto empty = make_string(""); - auto res = hook_BYTES_bytes2hexstring(empty); + auto res = hook_BYTES_bytes2hex(empty); BOOST_CHECK(res != empty); BOOST_CHECK_EQUAL(len(res), 0); - auto _00ff = make_string("\x00\xff", 2); - res = hook_BYTES_bytes2hexstring(_00ff); - BOOST_CHECK(res != _00ff); + auto _01ef = make_string("\x01\xef", 2); + res = hook_BYTES_bytes2hex(_01ef); + BOOST_CHECK(res != _01ef); BOOST_CHECK_EQUAL(len(res), 4); - BOOST_CHECK_EQUAL(0, memcmp(res->data, "00ff", 4)); + BOOST_CHECK_EQUAL(0, memcmp(res->data, "01ef", 4)); } BOOST_AUTO_TEST_CASE(string2bytes) { From 97cde58b9818547c675c55708fbb5b5f49df4d81 Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 14:29:15 +0200 Subject: [PATCH 09/11] Run clang-format --- runtime/strings/bytes.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index 274842f54..c3bbe001b 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -150,7 +150,8 @@ hook_BYTES_int2bytes(SortInt len, SortInt i, SortEndianness endianness_ptr) { SortString hook_BYTES_bytes2string(SortBytes b) { auto len_b = len(b); - auto *result = static_cast(kore_alloc_token(sizeof(string) + len_b)); + auto *result + = static_cast(kore_alloc_token(sizeof(string) + len_b)); memcpy(result->data, b->data, len_b); init_with_len(result, len_b); return result; From a96f4464940849c2e50daa25bb5db7ed9f7eb0de Mon Sep 17 00:00:00 2001 From: Raoul Schaffranek Date: Fri, 29 Aug 2025 14:40:38 +0200 Subject: [PATCH 10/11] Undo changes to bytes2string --- runtime/strings/bytes.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index c3bbe001b..d44d4f0f6 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -148,15 +148,17 @@ hook_BYTES_int2bytes(SortInt len, SortInt i, SortEndianness endianness_ptr) { return result; } -SortString hook_BYTES_bytes2string(SortBytes b) { - auto len_b = len(b); - auto *result - = static_cast(kore_alloc_token(sizeof(string) + len_b)); - memcpy(result->data, b->data, len_b); - init_with_len(result, len_b); +string *bytes2string(string *b, size_t len) { + auto *result = static_cast(kore_alloc_token(sizeof(string) + len)); + memcpy(result->data, b->data, len); + init_with_len(result, len); return result; } +SortString hook_BYTES_bytes2string(SortBytes b) { + return bytes2string(b, len(b)); +} + SortBytes hook_BYTES_string2bytes(SortString s) { return hook_BYTES_bytes2string(s); } From a9729c0058f50ab043db1d57a5206d74de16820d Mon Sep 17 00:00:00 2001 From: Raoul <1412344+RaoulSchaffranek@users.noreply.github.com> Date: Fri, 29 Aug 2025 15:20:45 +0200 Subject: [PATCH 11/11] Remove code comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tamás Tóth --- runtime/strings/bytes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/strings/bytes.cpp b/runtime/strings/bytes.cpp index d44d4f0f6..4737228ed 100644 --- a/runtime/strings/bytes.cpp +++ b/runtime/strings/bytes.cpp @@ -1,5 +1,5 @@ #include -#include // Add this include for std::array +#include #include #include #include