aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/tests/fuzz_ops/lib/vm_apply.cpp
blob: 1a6400457f83e5cc5556e6646cad620b9cc24772 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "vm_apply.h"

namespace NSc::NUt {

#define Y_GEN_TRY_OP(op) \
    if (!op) { return false; }

#define Y_GEN_SRC_OP(op, arg, st, act) \
    switch (act.GetSrc(arg).Type) { \
    case TSrc::T_LREF__POS: \
        op(st.LRef(act.GetSrc(arg).Pos)); \
        break; \
    case TSrc::T_CREF__POS: \
        op(st.CRef(act.GetSrc(arg).Pos)); \
        break; \
    case TSrc::T_RREF__POS: \
        op(st.RRef(act.GetSrc(arg).Pos)); \
        break; \
    default: \
        Y_ABORT(); \
    }


#define Y_GEN_SRC_TRY_OP(op, arg, st, act) \
    if (st.CRef(act.GetSrc(arg).Pos).IsSameOrAncestorOf(st.Current())) { \
        return false; \
    } \
    Y_GEN_SRC_OP(op, arg, st, act);


#define Y_GEN_DST_OP(op, arg, st, act) \
    switch (act.GetDst(arg).Type) { \
    case TDst::T_CREATE_BACK_LREF: \
        Y_GEN_TRY_OP(st.TryPushBack(op)) \
        break; \
    case TDst::T_CREATE_BACK_CREF: \
        Y_GEN_TRY_OP(st.TryPushBack(std::as_const(op))) \
        break; \
    case TDst::T_CREATE_BACK_RREF: \
        Y_GEN_TRY_OP(st.TryPushBack(std::move(op))) \
        break; \
    case TDst::T_CREATE_FRONT_LREF: \
        Y_GEN_TRY_OP(st.TryPushFront(op)) \
        break; \
    case TDst::T_CREATE_FRONT_CREF: \
        Y_GEN_TRY_OP(st.TryPushFront(std::as_const(op))) \
        break; \
    case TDst::T_CREATE_FRONT_RREF: \
        Y_GEN_TRY_OP(st.TryPushFront(std::move(op))) \
        break; \
    case TDst::T_LREF__POS: \
        st.LRef(act.GetDst(arg).Pos) = op; \
        break; \
    case TDst::T_CREF__POS: \
        st.LRef(act.GetDst(arg).Pos) = std::as_const(op); \
        break; \
    case TDst::T_RREF__POS: \
        st.LRef(act.GetDst(arg).Pos) = std::move(op); \
        break; \
    default: \
        Y_ABORT(); \
    }


#define Y_GEN_REF_OP(op, arg, st, act) \
    switch (act.GetRef(arg).Type) { \
    case TRef::T_CREATE_BACK: \
        Y_GEN_TRY_OP(st.TryPushBack(op)) \
        break; \
    case TRef::T_CREATE_FRONT: \
        Y_GEN_TRY_OP(st.TryPushFront(op)) \
        break; \
    case TRef::T_REF__POS: \
        st.LRef(act.GetRef(arg).Pos) = op; \
        break; \
    default: \
        Y_ABORT(); \
    }

#define Y_GEN_PTR_OP(op, arg, st, act) \
    if (auto* r = (op)) { \
        switch (act.GetRef(arg).Type) { \
        case TRef::T_CREATE_BACK: \
            Y_GEN_TRY_OP(st.TryPushBack(*r)) \
            break; \
        case TRef::T_CREATE_FRONT: \
            Y_GEN_TRY_OP(st.TryPushFront(*r)) \
            break; \
        case TRef::T_REF__POS: \
            st.LRef(act.GetRef(arg).Pos) = *r; \
            break; \
        default: \
            Y_ABORT(); \
        } \
    }

    bool ApplyNextAction(TVMState& st, TVMAction act) {
        switch (act.Type) {
        case VMA_JMP__POS:
            st.Pos = act.GetPos(0);
            return true;

        case VMA_CREATE_BACK:
            Y_GEN_TRY_OP(st.TryPushBack())
            return true;

        case VMA_CREATE_BACK__SRC:
            switch (act.GetSrc(0).Type) {
            case TSrc::T_LREF__POS:
                Y_GEN_TRY_OP(st.TryPushBack(st.LRef(act.GetSrc(0).Pos)))
                break;
            case TSrc::T_CREF__POS:
                Y_GEN_TRY_OP(st.TryPushBack(st.CRef(act.GetSrc(0).Pos)))
                break;
            case TSrc::T_RREF__POS:
                Y_GEN_TRY_OP(st.TryPushBack(st.RRef(act.GetSrc(0).Pos)))
                break;
            default:
                Y_ABORT();
            }

            return true;

        case VMA_CREATE_FRONT:
            Y_GEN_TRY_OP(st.TryPushFront())
            return true;

        case VMA_CREATE_FRONT__SRC:
            switch (act.GetSrc(0).Type) {
            case TSrc::T_LREF__POS:
                Y_GEN_TRY_OP(st.TryPushFront(st.LRef(act.GetSrc(0).Pos)))
                break;
            case TSrc::T_CREF__POS:
                Y_GEN_TRY_OP(st.TryPushFront(st.CRef(act.GetSrc(0).Pos)))
                break;
            case TSrc::T_RREF__POS:
                Y_GEN_TRY_OP(st.TryPushFront(st.RRef(act.GetSrc(0).Pos)))
                break;
            default:
                Y_ABORT();
            }
            return true;

        case VMA_DESTROY_BACK:
            return st.TryPopBack();

        case VMA_DESTROY_FRONT:
            return st.TryPopFront();

        case VMA_ASSIGN__SRC:
            Y_GEN_SRC_OP(st.Current() = , 0, st, act);
            return true;

        case VMA_SET_STRING__IDX:
            st.Current().SetString(act.GetString(0));
            return true;

        case VMA_SET_INT_NUMBER__IDX:
            st.Current().SetIntNumber(act.GetIntNumber(0));
            return true;

        case VMA_SET_DICT:
            st.Current().SetDict();
            return true;

        case VMA_SET_ARRAY:
            st.Current().SetArray();
            return true;

        case VMA_SET_NULL:
            st.Current().SetNull();
            return true;

        case VMA_GET_JSON:
            st.Current().ToJson();
            return true;

        case VMA_ARRAY_CLEAR:
            st.Current().ClearArray();
            return true;

        case VMA_ARRAY_PUSH:
            st.Current().Push();
            return true;

        case VMA_ARRAY_PUSH__SRC:
            Y_GEN_SRC_TRY_OP(st.Current().Push, 0, st, act);
            return true;

        case VMA_ARRAY_PUSH__DST:
            Y_GEN_DST_OP(st.Current().Push(), 0, st, act);
            return true;

        case VMA_ARRAY_POP__REF:
            Y_GEN_REF_OP(st.Current().Pop(), 0, st, act);
            return true;

        case VMA_ARRAY_INSERT__IDX:
            st.Current().Insert(act.GetIdx(0));
            return true;

        case VMA_ARRAY_INSERT__IDX_SRC:
            Y_GEN_SRC_TRY_OP(st.Current().Insert(act.GetIdx(0)) = , 1, st, act);
            return true;

        case VMA_ARRAY_INSERT__IDX_DST:
            Y_GEN_DST_OP(st.Current().Insert(act.GetIdx(0)), 1, st, act);
            return true;

        case VMA_ARRAY_DELETE__IDX_REF:
            Y_GEN_REF_OP(st.Current().Delete(act.GetIdx(0)), 1, st, act);
            return true;

        case VMA_ARRAY_GET__IDX_REF:
            Y_GEN_REF_OP(st.Current().Get(act.GetIdx(0)), 1, st, act);
            return true;

        case VMA_ARRAY_GET_OR_ADD__IDX:
            st.Current().GetOrAdd(act.GetIdx(0));
            return true;

        case VMA_ARRAY_GET_OR_ADD__IDX_SRC:
            Y_GEN_SRC_TRY_OP(st.Current().GetOrAdd(act.GetIdx(0)) = , 1, st, act);
            return true;

        case VMA_ARRAY_GET_OR_ADD__IDX_DST:
            Y_GEN_DST_OP(st.Current().GetOrAdd(act.GetIdx(0)), 1, st, act);
            return true;

        case VMA_ARRAY_GET_NO_ADD__IDX_REF:
            Y_GEN_PTR_OP(st.Current().GetNoAdd(act.GetIdx(0)), 1, st, act);
            return true;

        case VMA_DICT_CLEAR:
            st.Current().ClearDict();
            return true;

        case VMA_DICT_DELETE__IDX:
            st.Current().Delete(act.GetKey(0));
            return true;

        case VMA_DICT_DELETE__IDX_REF:
            Y_GEN_REF_OP(st.Current().Delete(act.GetKey(0)), 1, st, act);
            return true;

        case VMA_DICT_GET__IDX_REF:
            Y_GEN_REF_OP(st.Current().Get(act.GetKey(0)), 1, st, act);
            return true;

        case VMA_DICT_GET_OR_ADD__IDX:
            st.Current().GetOrAdd(act.GetKey(0));
            return true;

        case VMA_DICT_GET_OR_ADD__IDX_SRC:
            Y_GEN_SRC_TRY_OP(st.Current().GetOrAdd(act.GetKey(0)) = , 1, st, act);
            return true;

        case VMA_DICT_GET_OR_ADD__IDX_DST:
            Y_GEN_DST_OP(st.Current().GetOrAdd(act.GetKey(0)), 1, st, act);
            return true;

        case VMA_DICT_GET_NO_ADD__IDX_REF:
            Y_GEN_PTR_OP(st.Current().GetNoAdd(act.GetKey(0)), 1, st, act);
            return true;

        case VMA_MERGE_UPDATE__POS:
            st.Current().MergeUpdate(st.LRef(act.GetPos(0)));
            return true;

        case VMA_MERGE_REVERSE__POS:
            st.Current().ReverseMerge(st.LRef(act.GetPos(0)));
            return true;

        case VMA_MERGE_COPY_FROM__POS:
            st.Current().CopyFrom(st.LRef(act.GetPos(0)));
            return true;

        case VMA_SWAP__POS:
            st.Current().Swap(st.LRef(act.GetPos(0)));
            return true;

        case VMA_EQUAL__POS_POS:
            TValue::Equal(st.CRef(act.GetPos(0)), st.CRef(act.GetPos(1)));
            return true;

        case VMA_SELECT_NO_ADD__PATH_REF:
            Y_GEN_REF_OP(st.Current().TrySelect(act.GetPath(0)), 1, st, act);
            return true;

        case VMA_SELECT_OR_ADD__PATH_REF:
            Y_GEN_PTR_OP(st.Current().TrySelectOrAdd(act.GetPath(0)), 1, st, act);
            return true;

        case VMA_SELECT_AND_DELETE__PATH_REF:
            Y_GEN_REF_OP(st.Current().TrySelectAndDelete(act.GetPath(0)), 1, st, act);
            return true;

        default:
            Y_ABORT();
        }
    }
}