Skip to content

Commit 11ed210

Browse files
committed
refactor: moving bytecode related magic constants to Compiler/Common.hpp
1 parent fe7712a commit 11ed210

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

include/Ark/Compiler/Common.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string_view>
1717
#include <vector>
1818
#include <cinttypes>
19+
#include <Ark/Constants.hpp>
1920

2021
namespace Ark
2122
{
@@ -24,6 +25,21 @@ namespace Ark
2425

2526
namespace Ark::internal
2627
{
28+
namespace bytecode
29+
{
30+
constexpr std::array Magic = { 'a', 'r', 'k', '\0' };
31+
constexpr std::array Version = {
32+
ARK_VERSION_MAJOR & 0xff00,
33+
ARK_VERSION_MAJOR & 0x00ff,
34+
ARK_VERSION_MINOR & 0xff00,
35+
ARK_VERSION_MINOR & 0x00ff,
36+
ARK_VERSION_PATCH & 0xff00,
37+
ARK_VERSION_PATCH & 0x00ff
38+
};
39+
constexpr std::size_t TimestampLength = 8;
40+
constexpr std::size_t HeaderSize = Magic.size() + Version.size() + TimestampLength;
41+
}
42+
2743
/// The different node types available
2844
enum class NodeType
2945
{

src/arkreactor/Compiler/BytecodeReader.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ namespace Ark
4040

4141
bool BytecodeReader::checkMagic() const
4242
{
43-
return m_bytecode.size() >= 4 && m_bytecode[0] == 'a' &&
44-
m_bytecode[1] == 'r' && m_bytecode[2] == 'k' &&
45-
m_bytecode[3] == internal::Instruction::NOP;
43+
return m_bytecode.size() >= bytecode::Magic.size() &&
44+
m_bytecode[0] == bytecode::Magic[0] &&
45+
m_bytecode[1] == bytecode::Magic[1] &&
46+
m_bytecode[2] == bytecode::Magic[2] &&
47+
m_bytecode[3] == bytecode::Magic[3];
4648
}
4749

4850
const bytecode_t& BytecodeReader::bytecode() noexcept
@@ -52,7 +54,7 @@ namespace Ark
5254

5355
Version BytecodeReader::version() const
5456
{
55-
if (!checkMagic() || m_bytecode.size() < 10)
57+
if (!checkMagic() || m_bytecode.size() < bytecode::Magic.size() + bytecode::Version.size())
5658
return Version { 0, 0, 0 };
5759

5860
return Version {
@@ -65,7 +67,7 @@ namespace Ark
6567
unsigned long long BytecodeReader::timestamp() const
6668
{
6769
// 4 (ark\0) + version (2 bytes / number) + timestamp = 18 bytes
68-
if (!checkMagic() || m_bytecode.size() < 18)
70+
if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize)
6971
return 0;
7072

7173
// reading the timestamp in big endian
@@ -82,27 +84,27 @@ namespace Ark
8284

8385
std::vector<unsigned char> BytecodeReader::sha256() const
8486
{
85-
if (!checkMagic() || m_bytecode.size() < 18 + picosha2::k_digest_size)
87+
if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size)
8688
return {};
8789

8890
std::vector<unsigned char> sha(picosha2::k_digest_size);
8991
for (std::size_t i = 0; i < picosha2::k_digest_size; ++i)
90-
sha[i] = m_bytecode[18 + i];
92+
sha[i] = m_bytecode[bytecode::HeaderSize + i];
9193
return sha;
9294
}
9395

9496
Symbols BytecodeReader::symbols() const
9597
{
96-
if (!checkMagic() || m_bytecode.size() < 18 + picosha2::k_digest_size ||
97-
m_bytecode[18 + picosha2::k_digest_size] != SYM_TABLE_START)
98+
if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size ||
99+
m_bytecode[bytecode::HeaderSize + picosha2::k_digest_size] != SYM_TABLE_START)
98100
return {};
99101

100-
std::size_t i = 18 + picosha2::k_digest_size + 1;
102+
std::size_t i = bytecode::HeaderSize + picosha2::k_digest_size + 1;
101103
const uint16_t size = readNumber(i);
102104
i++;
103105

104106
Symbols block;
105-
block.start = 18 + picosha2::k_digest_size;
107+
block.start = bytecode::HeaderSize + picosha2::k_digest_size;
106108
block.symbols.reserve(size);
107109

108110
for (uint16_t j = 0; j < size; ++j)
@@ -209,7 +211,7 @@ namespace Ark
209211
{
210212
if (!checkMagic())
211213
{
212-
fmt::print("Invalid format");
214+
fmt::println("Invalid format");
213215
return;
214216
}
215217

src/arkreactor/Compiler/IntermediateRepresentation/IRCompiler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ namespace Ark::internal
4141
m_bytecode.push_back(0_u8);
4242
}
4343

44-
constexpr std::size_t header_size = 18;
45-
4644
// generate a hash of the tables + bytecode
4745
std::vector<unsigned char> hash_out(picosha2::k_digest_size);
48-
picosha2::hash256(m_bytecode.begin() + header_size, m_bytecode.end(), hash_out);
49-
m_bytecode.insert(m_bytecode.begin() + header_size, hash_out.begin(), hash_out.end());
46+
picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize, m_bytecode.end(), hash_out);
47+
m_bytecode.insert(m_bytecode.begin() + bytecode::HeaderSize, hash_out.begin(), hash_out.end());
5048

5149
m_logger.traceEnd();
5250
}

src/arkreactor/VM/State.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace Ark
155155
const auto bytecode_hash = bcr.sha256();
156156

157157
std::vector<unsigned char> hash(picosha2::k_digest_size);
158-
picosha2::hash256(m_bytecode.begin() + 18 + picosha2::k_digest_size, m_bytecode.end(), hash);
158+
picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize + picosha2::k_digest_size, m_bytecode.end(), hash);
159159
// checking integrity
160160
for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
161161
{

tests/unittests/BytecodeReaderSuite.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <Ark/Compiler/BytecodeReader.hpp>
44
#include <Ark/Compiler/Welder.hpp>
5+
#include <picosha2.h>
56

67
#include <string>
78
#include <chrono>
@@ -31,7 +32,8 @@ ut::suite<"BytecodeReader"> bcr_suite = [] {
3132
.count());
3233

3334
Ark::BytecodeReader bcr;
34-
bcr.feed(welder.bytecode());
35+
const auto bytecode = welder.bytecode();
36+
bcr.feed(bytecode);
3537

3638
"bytecode"_test = [&] {
3739
should("find the version") = [bcr] {
@@ -47,14 +49,12 @@ ut::suite<"BytecodeReader"> bcr_suite = [] {
4749
expect(that % time <= time_end);
4850
};
4951

50-
should("find the sha256") = [bcr] {
52+
should("find the sha256") = [bcr, bytecode] {
5153
const auto sha256 = bcr.sha256();
52-
const auto expected_sha = std::vector<unsigned char> {
53-
0xcf, 0x79, 0x82, 0x6b, 0x81, 0x5c, 0xe4, 0x11,
54-
0xce, 0x25, 0xbe, 0xc3, 0x05, 0x91, 0x21, 0x7f,
55-
0x6c, 0x70, 0x54, 0x70, 0xd8, 0x8b, 0x2b, 0x90,
56-
0x82, 0xcd, 0x70, 0x2e, 0xeb, 0x51, 0xb2, 0x75
57-
};
54+
std::vector<unsigned char> expected_sha(picosha2::k_digest_size);
55+
// compute sha256 after header + sha
56+
picosha2::hash256(bytecode.begin() + Ark::internal::bytecode::HeaderSize + 32, bytecode.end(), expected_sha);
57+
5858
expect(that % sha256 == expected_sha);
5959
};
6060

0 commit comments

Comments
 (0)