aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg-fuzz/main.cpp
blob: bbbf71708625f90dde450b02ec3aafba7b266b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <vcpkg/base/checks.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/unicode.h>

#include <vcpkg/platform-expression.h>

#include <string.h>

#include <iostream>
#include <sstream>
#include <utility>

using namespace vcpkg;

namespace
{
    enum class FuzzKind
    {
        None,
        Utf8Decoder,
        JsonParser,
        PlatformExpr,
    };

    struct FuzzArgs
    {
        FuzzArgs(int argc, char** argv)
        {
            if (argc <= 1)
            {
                print_help_and_exit();
            }

            char** it = argv + 1; // skip the name of the program
            char** last = argv + argc;

            for (; it != last; ++it)
            {
                auto arg = StringView(*it, strlen(*it));
                if (arg == "/?")
                {
                    print_help_and_exit();
                }

                auto pr = split_arg(arg);
                auto key = pr.first;
                auto value = pr.second;
                if (key == "h" || key == "help")
                {
                    print_help_and_exit();
                }

                if (key == "kind")
                {
                    if (value == "json")
                    {
                        kind = FuzzKind::JsonParser;
                    }
                    else if (value == "utf-8")
                    {
                        kind = FuzzKind::Utf8Decoder;
                    }
                    else if (value == "platform-expr")
                    {
                        kind = FuzzKind::PlatformExpr;
                    }
                    else
                    {
                        System::print2(System::Color::error, "Invalid kind: ", value, "\n");
                        System::print2(System::Color::error, "  Expected one of: utf-8, json, platform-expr\n\n");
                        print_help_and_exit(true);
                    }
                }
                else
                {
                    System::print2("Unknown option: ", key, "\n\n");
                    print_help_and_exit(true);
                }
            }
        }

        // returns {arg, ""} when there isn't an `=`
        // skips preceding `-`s
        std::pair<StringView, StringView> split_arg(StringView arg)
        {
            auto first = std::find_if(arg.begin(), arg.end(), [](char c) { return c != '-'; });
            auto division = std::find(first, arg.end(), '=');
            if (division == arg.end())
            {
                return {StringView(first, arg.end()), StringView(arg.end(), arg.end())};
            }
            else
            {
                return {StringView(first, division), StringView(division + 1, arg.end())};
            }
        }

        [[noreturn]] void print_help_and_exit(bool invalid = false)
        {
            constexpr auto help =
                R"(
Usage: vcpkg-fuzz <options>

Accepts input on stdin.

Options:
  --kind=...                One of {utf-8, json}
)";

            auto color = invalid ? System::Color::error : System::Color::success;

            System::print2(color, help);
            if (invalid)
            {
                Checks::exit_fail(VCPKG_LINE_INFO);
            }
            else
            {
                Checks::exit_success(VCPKG_LINE_INFO);
            }
        }

        FuzzKind kind;
    };

    std::string read_all_of_stdin()
    {
        std::stringstream ss;
        ss << std::cin.rdbuf();
        return std::move(ss).str();
    }

    [[noreturn]] void fuzz_json_and_exit(StringView text)
    {
        auto res = Json::parse(text);
        if (!res)
        {
            Checks::exit_with_message(VCPKG_LINE_INFO, res.error()->format());
        }

        Checks::exit_success(VCPKG_LINE_INFO);
    }

    [[noreturn]] void fuzz_utf8_and_exit(StringView text)
    {
        auto res = Unicode::Utf8Decoder(text.begin(), text.end());
        for (auto ch : res)
        {
            (void)ch;
        }

        Checks::exit_success(VCPKG_LINE_INFO);
    }

    [[noreturn]] void fuzz_platform_expr_and_exit(StringView text)
    {
        auto res1 =
            PlatformExpression::parse_platform_expression(text, PlatformExpression::MultipleBinaryOperators::Deny);
        auto res2 =
            PlatformExpression::parse_platform_expression(text, PlatformExpression::MultipleBinaryOperators::Allow);

        if (!res1)
        {
            Checks::exit_with_message(VCPKG_LINE_INFO, res1.error());
        }
        if (!res2)
        {
            Checks::exit_with_message(VCPKG_LINE_INFO, res2.error());
        }

        Checks::exit_success(VCPKG_LINE_INFO);
    }
}

int main(int argc, char** argv)
{
    auto args = FuzzArgs(argc, argv);

    if (args.kind == FuzzKind::None)
    {
        args.print_help_and_exit(true);
    }

    auto text = read_all_of_stdin();
    switch (args.kind)
    {
        case FuzzKind::JsonParser: fuzz_json_and_exit(text);
        case FuzzKind::Utf8Decoder: fuzz_utf8_and_exit(text);
        case FuzzKind::PlatformExpr: fuzz_platform_expr_and_exit(text);
        default: Checks::unreachable(VCPKG_LINE_INFO);
    }
}