This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathscript-validate.hpp
More file actions
391 lines (312 loc) · 9.46 KB
/
script-validate.hpp
File metadata and controls
391 lines (312 loc) · 9.46 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright (C) 2003-2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <libscript/script.h>
#include "script-private.h"
// The state of the validate context is separated from the context for two
// reasons:
// 1. Validating bytecode should just mutate the state via the context
// methods, they never need to set up new state and validate recursively
// 2. The state can be reused between each bytecode validation step by the
// caller.
struct MCScriptValidateState
{
// Whilst validation of bytecode is successful, the error var remains
// false. When it becomes true, it means the bytecode at 'current_address'
// within the handler is invalid.
bool error;
// The module containins the handler being validated.
MCScriptModuleRef module;
// The handler being validated.
MCScriptHandlerDefinition *handler;
// The offset from the beginning of the module's bytecode to the current
// instruction being validated.
uindex_t current_address;
MCScriptBytecodeOp operation;
// The current instruction's argument count.
uindex_t argument_count;
// The current instruction's list of (decoded) arguments.
uindex_t arguments[256];
// The total number of registers required to run the bytecode.
uindex_t register_limit;
};
class MCScriptValidateContext
{
public:
// Constructors
//
MCScriptValidateContext(MCScriptValidateState& state);
// Return whether the context is valid.
bool IsValid(void) const;
// Return the maximum index of register used.
uindex_t GetRegisterLimit(void) const;
// Quering Methods
//
// These methods return information about the current bytecode operation
// being processed.
// Return the address of the current opcode
uindex_t GetAddress(void) const;
// Return the number of arguments to the current opcode
uindex_t GetArity(void) const;
// Get the argument at the given index as unsigned
uindex_t GetArgument(uindex_t index) const;
// Get the argument at the given index as signed
index_t GetSignedArgument(uindex_t index) const;
// Get the effective definition kind of the specified index
MCScriptDefinitionKind GetEffectiveKindOfDefinition(uindex_t index) const;
// Validating Methods
//
// These methods validate various aspects of the bytecode. The context
// stores whether a previous validation failed, and takes appropriate
// action. This means there is no need to check return values in a
// Validate method.
// Check the number of arguments is as specified.
void CheckArity(uindex_t expected_arity);
// Check the number of arguments is at least that specified.
void CheckMinimumArity(uindex_t minimum_expected_arity);
// Check the number of arguments is at most that specified.
void CheckMaximumArity(uindex_t maximum_expected_arity);
// Check the given address is valid - i.e. is within the current handler
// and targets the start of an opcode.
void CheckAddress(uindex_t target_address);
// Check the given index is a valid register.
// Note: At the point of checking, a maximum upper bound is used to check
// the register index is 'sane'. At the end of validation an extra check
// is performed to ensure that register indicies form a dense sequence from
// 0..n where n is the maximum register index specified in the bytecode.
void CheckRegister(uindex_t register_index);
// Check the given index is a valid constant index
void CheckValue(uindex_t constant_index);
// Check the given index is a valid handler index. A valid handler index
// can be of type Handler, ForeignHandler, or DefinitionGroup (of handlers).
void CheckHandler(uindex_t handler_index);
// Check the given index is a valid fetchable definition index. A fetchable
// definition can be of type Constant, Variable or Handler.
void CheckFetchable(uindex_t fetchable_index);
// Check the given index is a valid variable definition index.
void CheckVariable(uindex_t variable_index);
// Reporting Methods
//
// These report the validation errors which can occur.
void ReportInvalidArity(void);
void ReportInvalidAddress(void);
void ReportInvalidRegister(void);
void ReportInvalidValue(void);
void ReportInvalidDefinition(void);
void ReportInvalidHandler(void);
void ReportInvalidFetchable(void);
void ReportInvalidVariable(void);
private:
void ReportInvalid(void);
MCScriptValidateState& m_state;
};
inline MCScriptValidateContext::MCScriptValidateContext(MCScriptValidateState& state)
: m_state(state)
{
}
inline uindex_t MCScriptValidateContext::GetAddress(void) const
{
return m_state.current_address;
}
inline uindex_t MCScriptValidateContext::GetArity(void) const
{
return m_state.argument_count;
}
inline uindex_t MCScriptValidateContext::GetArgument(uindex_t p_index) const
{
__MCScriptAssert__(p_index < m_state.argument_count, "invalid argument index");
return m_state.arguments[p_index];
}
inline index_t MCScriptValidateContext::GetSignedArgument(uindex_t p_index) const
{
return MCScriptBytecodeDecodeSignedArgument(GetArgument(p_index));
}
inline MCScriptDefinitionKind MCScriptValidateContext::GetEffectiveKindOfDefinition(uindex_t p_index) const
{
__MCScriptAssert__(p_index < m_state.module->definition_count, "invalid definition index");
if (m_state.module->definitions[p_index]->kind == kMCScriptDefinitionKindExternal)
{
MCScriptExternalDefinition *t_ext_def =
static_cast<MCScriptExternalDefinition *>(m_state.module->definitions[p_index]);
return m_state.module->imported_definitions[t_ext_def->index].kind;
}
return m_state.module->definitions[p_index]->kind;
}
inline void MCScriptValidateContext::CheckArity(uindex_t p_expected_arity)
{
if (m_state.error)
return;
if (GetArity() != p_expected_arity)
{
ReportInvalidArity();
return;
}
}
inline void MCScriptValidateContext::CheckMinimumArity(uindex_t p_minimum_expected_arity)
{
if (m_state.error)
return;
if (GetArity() < p_minimum_expected_arity)
{
ReportInvalidArity();
return;
}
}
inline void MCScriptValidateContext::CheckMaximumArity(uindex_t p_maximum_expected_arity)
{
if (m_state.error)
return;
if (GetArity() > p_maximum_expected_arity)
{
ReportInvalidArity();
return;
}
}
inline void MCScriptValidateContext::CheckAddress(uindex_t p_address)
{
if (m_state.error)
return;
if (p_address < m_state.handler->start_address)
{
ReportInvalidAddress();
return;
}
if (p_address >= m_state.handler->finish_address)
{
ReportInvalidAddress();
return;
}
// TODO: Validate that p_address is the start of an opcode.
}
inline void MCScriptValidateContext::CheckRegister(uindex_t p_register)
{
if (m_state.error)
{
return;
}
if (p_register == UINDEX_MAX)
{
ReportInvalidRegister();
return;
}
if (p_register >= m_state.register_limit)
{
m_state.register_limit = p_register + 1;
}
}
inline void MCScriptValidateContext::CheckValue(uindex_t p_index)
{
if (m_state.error)
{
return;
}
if (p_index > m_state.module->value_count)
{
ReportInvalidValue();
return;
}
}
inline void MCScriptValidateContext::CheckHandler(uindex_t p_index)
{
if (m_state.error)
{
return;
}
if (p_index > m_state.module->definition_count)
{
ReportInvalidDefinition();
return;
}
MCScriptDefinitionKind t_kind =
GetEffectiveKindOfDefinition(p_index);
if (t_kind != kMCScriptDefinitionKindHandler &&
t_kind != kMCScriptDefinitionKindForeignHandler &&
t_kind != kMCScriptDefinitionKindDefinitionGroup)
{
ReportInvalidHandler();
return;
}
}
inline void MCScriptValidateContext::CheckFetchable(uindex_t p_index)
{
if (m_state.error)
{
return;
}
if (p_index > m_state.module->definition_count)
{
ReportInvalidDefinition();
return;
}
MCScriptDefinitionKind t_kind =
GetEffectiveKindOfDefinition(p_index);
if (t_kind != kMCScriptDefinitionKindConstant &&
t_kind != kMCScriptDefinitionKindVariable &&
t_kind != kMCScriptDefinitionKindHandler &&
t_kind != kMCScriptDefinitionKindForeignHandler)
{
ReportInvalidFetchable();
return;
}
}
inline void MCScriptValidateContext::CheckVariable(uindex_t p_index)
{
if (m_state.error)
{
return;
}
if (p_index > m_state.module->definition_count)
{
ReportInvalidDefinition();
return;
}
if (GetEffectiveKindOfDefinition(p_index) != kMCScriptDefinitionKindVariable)
{
ReportInvalidVariable();
return;
}
}
inline void MCScriptValidateContext::ReportInvalidArity(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidAddress(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidRegister(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidValue(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidDefinition(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidHandler(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidFetchable(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalidVariable(void)
{
ReportInvalid();
}
inline void MCScriptValidateContext::ReportInvalid(void)
{
m_state.error = true;
}