• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 15453345670

04 Jun 2025 09:40PM UTC coverage: 86.735% (+0.1%) from 86.608%
15453345670

push

github

SuperFola
chore: updating fuzzing corpus

2 of 3 new or added lines in 2 files covered. (66.67%)

140 existing lines in 7 files now uncovered.

7258 of 8368 relevant lines covered (86.74%)

120482.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.17
/src/arkreactor/VM/State.cpp
1
#include <Ark/VM/State.hpp>
2

3
#include <Ark/Constants.hpp>
4
#include <Ark/Files.hpp>
5
#include <Ark/Compiler/Welder.hpp>
6

7
#ifdef _MSC_VER
8
#    pragma warning(push)
9
#    pragma warning(disable : 4996)
10
#endif
11

12
#include <Proxy/Picosha2.hpp>
13
#include <Ark/Compiler/BytecodeReader.hpp>
14
#include <fmt/core.h>
15
#include <fmt/color.h>
16

17
namespace Ark
18
{
19
    State::State(const std::vector<std::filesystem::path>& libenv) noexcept :
526✔
20
        m_debug_level(0),
263✔
21
        m_libenv(libenv),
263✔
22
        m_filename(ARK_NO_NAME_FILE)
263✔
23
    {
263✔
24
        // default value for builtin__sys:args is empty list
25
        const Value val(ValueType::List);
263✔
26
        m_binded[std::string(internal::Language::SysArgs)] = val;
263✔
27
    }
263✔
28

29
    bool State::feed(const std::string& bytecode_filename)
161✔
30
    {
161✔
31
        if (!Utils::fileExists(bytecode_filename))
161✔
UNCOV
32
            return false;
×
33

34
        return feed(Utils::readFileAsBytes(bytecode_filename));
161✔
35
    }
161✔
36

37
    bool State::feed(const bytecode_t& bytecode)
167✔
38
    {
167✔
39
        BytecodeReader bcr;
167✔
40
        bcr.feed(bytecode);
167✔
41
        if (!bcr.checkMagic())
167✔
UNCOV
42
            return false;
×
43

44
        m_bytecode = bytecode;
167✔
45

46
        try
47
        {
48
            configure(bcr);
167✔
49
            return true;
167✔
50
        }
×
51
        catch (const std::exception& e)  // FIXME I don't like this shit
52
        {
UNCOV
53
            fmt::println("{}", e.what());
×
UNCOV
54
            return false;
×
UNCOV
55
        }
×
56
    }
167✔
57

58
    bool State::compile(const std::string& file, const std::string& output, const uint16_t features) const
257✔
59
    {
257✔
60
        Welder welder(m_debug_level, m_libenv, features);
257✔
61
        for (const auto& p : m_binded)
514✔
62
            welder.registerSymbol(p.first);
257✔
63

64
        if (!welder.computeASTFromFile(file))
257✔
UNCOV
65
            return false;
×
66
        if (!welder.generateBytecode())
195✔
67
            return false;
×
68

69
        const std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
161✔
70
        if (!welder.saveBytecodeToFile(destination))
161✔
UNCOV
71
            return false;
×
72

73
        return true;
161✔
74
    }
257✔
75

76
    bool State::doFile(const std::string& file, const uint16_t features)
257✔
77
    {
257✔
78
        if (!Utils::fileExists(file))
257✔
79
        {
UNCOV
80
            fmt::print(fmt::fg(fmt::color::red), "Can not find file '{}'\n", file);
×
UNCOV
81
            return false;
×
82
        }
83
        m_filename = file;
257✔
84

85
        const bytecode_t bytecode = Utils::readFileAsBytes(file);
257✔
86
        BytecodeReader bcr;
257✔
87
        bcr.feed(bytecode);
257✔
88
        if (!bcr.checkMagic())  // couldn't read magic number, it's a source file
257✔
89
        {
90
            // check if it's in the arkscript cache
91
            const std::string short_filename = (std::filesystem::path(file)).filename().string();
257✔
92
            const std::string filename = short_filename.substr(0, short_filename.find_last_of('.')) + ".arkc";
257✔
93
            const std::filesystem::path directory = (std::filesystem::path(file)).parent_path() / ARK_CACHE_DIRNAME;
257✔
94
            const std::string path = (directory / filename).string();
257✔
95

96
            if (!exists(directory))  // create ark cache directory
257✔
97
                create_directory(directory);
15✔
98

99
            if (compile(file, path, features) && feed(path))
257✔
100
                return true;
161✔
101
        }
257✔
UNCOV
102
        else if (feed(bytecode))  // it's a bytecode file
×
UNCOV
103
            return true;
×
UNCOV
104
        return false;
×
105
    }
353✔
106

107
    bool State::doString(const std::string& code, const uint16_t features)
6✔
108
    {
6✔
109
        Welder welder(m_debug_level, m_libenv, features);
6✔
110
        for (const auto& p : m_binded)
16✔
111
            welder.registerSymbol(p.first);
10✔
112

113
        if (!welder.computeASTFromString(code))
6✔
UNCOV
114
            return false;
×
115
        if (!welder.generateBytecode())
6✔
UNCOV
116
            return false;
×
117
        return feed(welder.bytecode());
6✔
118
    }
6✔
119

120
    void State::loadFunction(const std::string& name, const Value::ProcType function) noexcept
4✔
121
    {
4✔
122
        m_binded[name] = Value(function);
4✔
123
    }
4✔
124

125
    void State::setArgs(const std::vector<std::string>& args) noexcept
10✔
126
    {
10✔
127
        Value val(ValueType::List);
10✔
128
        std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
13✔
129
            return Value(arg);
3✔
130
        });
131

132
        m_binded[std::string(internal::Language::SysArgs)] = val;
10✔
133
    }
10✔
134

135
    void State::setDebug(const unsigned level) noexcept
×
UNCOV
136
    {
×
137
        m_debug_level = level;
×
138
    }
×
139

140
    void State::setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept
×
UNCOV
141
    {
×
UNCOV
142
        m_libenv = libenv;
×
UNCOV
143
    }
×
144

145
    void State::configure(const BytecodeReader& bcr)
167✔
146
    {
167✔
147
        using namespace internal;
148

149
        const auto [major, minor, patch] = bcr.version();
167✔
150
        if (major != ARK_VERSION_MAJOR)
167✔
151
        {
152
            std::string str_version = std::to_string(major) + "." +
×
153
                std::to_string(minor) + "." +
×
UNCOV
154
                std::to_string(patch);
×
UNCOV
155
            throwStateError(fmt::format("Compiler and VM versions don't match: got {} while running {}", str_version, ARK_VERSION));
×
UNCOV
156
        }
×
157

158
        const auto bytecode_hash = bcr.sha256();
167✔
159

160
        std::vector<unsigned char> hash(picosha2::k_digest_size);
167✔
161
        picosha2::hash256(m_bytecode.begin() + bytecode::HeaderSize + picosha2::k_digest_size, m_bytecode.end(), hash);
167✔
162
        // checking integrity
163
        for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
5,511✔
164
        {
165
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
166
            if (hash[j] != bytecode_hash[j])
5,344✔
UNCOV
167
                throwStateError("Integrity check failed");
×
168
#endif
169
        }
5,344✔
170

171
        const auto syms = bcr.symbols();
167✔
172
        const auto vals = bcr.values(syms);
167✔
173
        const auto files = bcr.filenames(vals);
167✔
174
        const auto inst_locs = bcr.instLocations(files);
167✔
175
        const auto [pages, _] = bcr.code(inst_locs);
334✔
176

177
        m_symbols = syms.symbols;
167✔
178
        m_constants = vals.values;
167✔
179
        m_filenames = files.filenames;
167✔
180
        m_inst_locations = inst_locs.locations;
167✔
181
        m_pages = pages;
167✔
182
    }
167✔
183

184
    void State::reset() noexcept
×
185
    {
×
186
        m_symbols.clear();
×
187
        m_constants.clear();
×
188
        m_filenames.clear();
×
189
        m_inst_locations.clear();
×
UNCOV
190
        m_pages.clear();
×
UNCOV
191
        m_binded.clear();
×
192

193
        // default value for builtin__sys:args is empty list
UNCOV
194
        const Value val(ValueType::List);
×
UNCOV
195
        m_binded[std::string(internal::Language::SysArgs)] = val;
×
UNCOV
196
    }
×
197
}
198

199
#ifdef _MSC_VER
200
#    pragma warning(pop)
201
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc