Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/llvm-krun
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ expanded_input_file="$(mktemp tmp.in.XXXXXXXXXX)"
parser_file="$(mktemp tmp.parse.XXXXXXXXXX)"
temp_inputs=()

# shellcheck disable=SC2329
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, why is this needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shell check CI workflow kept failing, see for example this action: https://github.com/runtimeverification/llvm-backend/actions/runs/17319617915/job/49169763898

If I understand correctly, it's a false positive because the function is used in the following line. So I marked it to skip the check.

It's not related to this PR, I think, but maybe some CI flakiness caused by a shell check update.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks for clarifying it!

cleanup () {
# shellcheck disable=SC2317
rm -rf "$input_file" "$expanded_input_file" "$output_file" "$parser_file" "${temp_inputs[@]}"
Expand Down
19 changes: 19 additions & 0 deletions runtime/strings/bytes.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -162,6 +163,24 @@ SortBytes hook_BYTES_string2bytes(SortString s) {
return hook_BYTES_bytes2string(s);
}

// 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<char, 16> 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<string *>(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_b * 2);
return result;
}

SortBytes hook_BYTES_substr(SortBytes input, SortInt start, SortInt end) {
uint64_t ustart = GET_UI(start);
uint64_t uend = GET_UI(end);
Expand Down
14 changes: 14 additions & 0 deletions unittests/runtime-strings/bytestest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_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);
Expand Down Expand Up @@ -171,6 +172,19 @@ BOOST_AUTO_TEST_CASE(bytes2string) {
BOOST_CHECK_EQUAL(0, memcmp(_1234->data, "1234", 4));
}

BOOST_AUTO_TEST_CASE(bytes2hex) {
auto empty = make_string("");
auto res = hook_BYTES_bytes2hex(empty);
BOOST_CHECK(res != empty);
BOOST_CHECK_EQUAL(len(res), 0);

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, "01ef", 4));
}

BOOST_AUTO_TEST_CASE(string2bytes) {
auto empty = make_string("");
auto res = hook_BYTES_string2bytes(empty);
Expand Down