aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/tests/fuzz_ops/lib/vm_parse.cpp
blob: a03f5aef53438646f66cae26e9af51dfe5af780f (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "vm_parse.h"

namespace NSc::NUt {
#define Y_TRY_READ_BITS(state, out, bits, res) do { if (!state.Input.Read(out, bits)) { return res; } } while (false)
#define Y_TRY_READ_BITS_BOOL(state, out, bits) Y_TRY_READ_BITS(state, out, bits, false)
#define Y_TRY_READ_BITS_MAYBE(state, out, bits) Y_TRY_READ_BITS(state, out, bits, Nothing())

    TMaybe<TIdx> ParseIdx(TVMState& st) {
        static_assert(IsPowerOf2(TIdx::ValueCount));
        static const auto bits = GetCountWidth(TIdx::ValueCount);

        TIdx idx;
        if (!st.Input.Read(idx.Idx, bits)) {
            return Nothing();
        }
        return idx;
    }

    namespace {
        bool DoParsePos(TVMState& st, TPos& pos) {
            static const auto bits = GetCountWidth(TPos::ValueCount);
            const ui32 sz = st.Memory.size();

            ui32 neg = -1;
            Y_TRY_READ_BITS_BOOL(st, neg, 1);

            ui32 delta = -1;
            Y_TRY_READ_BITS_BOOL(st, delta, bits);

            if (neg) {
                if (st.Pos < delta) {
                    return false;
                }
                pos.Pos = st.Pos - delta;
            } else {
                if (st.Pos + delta >= sz) {
                    return false;
                }
                pos.Pos = st.Pos + delta;
            }

            return true;
        }

        template <class T>
        bool DoParseType(TVMState& state, T& res) {
            static const auto bits = GetCountWidth(T::TypeCount);

            ui32 type = -1;
            if (state.Input.Read(type, bits) && type < T::TypeCount) {
                res.Type = (typename T::EType)(type);
                return true;
            } else {
                return false;
            }
        }
    }

    TMaybe<TPos> ParsePos(TVMState& state) {
        TPos res;
        if (DoParsePos(state, res)) {
            return res;
        }
        return Nothing();
    }

    TMaybe<TRef> ParseRef(TVMState& state) {
        TRef ref;
        if (!DoParseType(state, ref)) {
            return Nothing();
        }

        switch (ref.Type) {
        case TRef::T_REF__POS:
            if (!DoParsePos(state, ref)) {
                return Nothing();
            }
            [[fallthrough]];
        case TRef::T_CREATE_FRONT:
        case TRef::T_CREATE_BACK:
            return ref;
        default:
            Y_FAIL();
        }
    }

    TMaybe<TSrc> ParseSrc(TVMState& state) {
        TSrc src;
        if (!DoParseType(state, src)) {
            return Nothing();
        }

        switch (src.Type) {
        case TSrc::T_LREF__POS:
        case TSrc::T_CREF__POS:
        case TSrc::T_RREF__POS:
            if (!DoParsePos(state, src)) {
                return Nothing();
            }
            return src;
        default:
            Y_FAIL();
        }
    }

    TMaybe<TDst> ParseDst(TVMState& state) {
        TDst dst;
        if (!DoParseType(state, dst)) {
            return Nothing();
        }

        switch (dst.Type) {
        case TDst::T_LREF__POS:
        case TDst::T_CREF__POS:
        case TDst::T_RREF__POS:
            if (!DoParsePos(state, dst)) {
                return Nothing();
            }
            [[fallthrough]];
        case TDst::T_CREATE_FRONT_LREF:
        case TDst::T_CREATE_FRONT_CREF:
        case TDst::T_CREATE_FRONT_RREF:
        case TDst::T_CREATE_BACK_LREF:
        case TDst::T_CREATE_BACK_CREF:
        case TDst::T_CREATE_BACK_RREF:
            return dst;
        default:
            Y_FAIL();
        }
    }

    TMaybe<TPath> ParsePath(TVMState& state) {
        static const ui32 bits = GetCountWidth(TPath::MaxLength);
        TPath path;

        ui32 len = -1;
        Y_TRY_READ_BITS_MAYBE(state, len, bits);
        while (len--) {
            ui8 c;
            Y_TRY_READ_BITS_MAYBE(state, c, 8);
            path.Path.push_back(c);
        }
        return path;
    }

    TMaybe<TVMAction> ParseNextAction(TVMState& state) {
        TVMAction res;

        if (!DoParseType(state, res)) {
            return Nothing();
        }

        switch (res.Type) {
        case VMA_CREATE_BACK:
        case VMA_CREATE_FRONT:
        case VMA_DESTROY_BACK:
        case VMA_DESTROY_FRONT:

        case VMA_SET_DICT:
        case VMA_SET_ARRAY:
        case VMA_SET_NULL:
        case VMA_GET_JSON:

        case VMA_ARRAY_CLEAR:
        case VMA_ARRAY_PUSH:
        case VMA_DICT_CLEAR:
            return res;

        case VMA_JMP__POS:
        case VMA_MERGE_UPDATE__POS:
        case VMA_MERGE_REVERSE__POS:
        case VMA_MERGE_COPY_FROM__POS:
        case VMA_SWAP__POS:
            if (res.SetArg(ParsePos(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_ARRAY_POP__REF:
            if (res.SetArg(ParseRef(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_SET_STRING__IDX:
        case VMA_SET_INT_NUMBER__IDX:
        case VMA_ARRAY_INSERT__IDX:
        case VMA_ARRAY_GET_OR_ADD__IDX:
        case VMA_DICT_GET_OR_ADD__IDX:
            if (res.SetArg(ParseIdx(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_CREATE_BACK__SRC:
        case VMA_CREATE_FRONT__SRC:
        case VMA_ASSIGN__SRC:
        case VMA_ARRAY_PUSH__SRC:
            if (res.SetArg(ParseSrc(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_ARRAY_PUSH__DST:
            if (res.SetArg(ParseDst(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_ARRAY_DELETE__IDX_REF:
        case VMA_ARRAY_GET__IDX_REF:
        case VMA_ARRAY_GET_NO_ADD__IDX_REF:
        case VMA_DICT_DELETE__IDX_REF:
        case VMA_DICT_GET__IDX_REF:
        case VMA_DICT_GET_NO_ADD__IDX_REF:
            if (res.SetArg(ParseIdx(state)) && res.SetArg(ParseRef(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_ARRAY_INSERT__IDX_SRC:
        case VMA_ARRAY_GET_OR_ADD__IDX_SRC:
        case VMA_DICT_GET_OR_ADD__IDX_SRC:
            if (res.SetArg(ParseIdx(state)) && res.SetArg(ParseSrc(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_ARRAY_INSERT__IDX_DST:
        case VMA_ARRAY_GET_OR_ADD__IDX_DST:
        case VMA_DICT_GET_OR_ADD__IDX_DST:
            if (res.SetArg(ParseIdx(state)) && res.SetArg(ParseDst(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_EQUAL__POS_POS:
            if (res.SetArg(ParsePos(state)) && res.SetArg(ParsePos(state))) {
                return res;
            } else {
                return Nothing();
            }

        case VMA_SELECT_NO_ADD__PATH_REF:
        case VMA_SELECT_OR_ADD__PATH_REF:
        case VMA_SELECT_AND_DELETE__PATH_REF:
            if (res.SetArg(ParsePath(state)) && res.SetArg(ParseRef(state))) {
                return res;
            } else {
                return Nothing();
            }

        default:
            return Nothing();
        }
    }
}