summaryrefslogtreecommitdiff
path: root/rtt-module.js
blob: 0dfa6421acb5e62fb7fc1b8b07ecca951a34480e (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
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
"use strict"

const Ajv = require("ajv")
const ajv = new Ajv({allowUnionTypes: true})
const crypto = require('crypto')
const fs = require("fs")
const path = require("path")
const { FuzzedDataProvider } = require("@jazzer.js/core")

const RULES_JS_FILE = process.env.RTT_RULES || "rules.js"
const NO_ASSERT = process.env.NO_ASSERT === 'true'
const NO_CRASH = process.env.NO_CRASH === 'true'
const NO_SCHEMA = process.env.NO_SCHEMA === 'true'
const NO_UNDO = process.env.NO_UNDO === 'true'
const MAX_STEPS = parseInt(process.env.MAX_STEPS || 10000)
const TIMEOUT = parseInt(process.env.TIMEOUT || 250)

console.log(`Loading rtt-fuzzer RTT_RULES='${RULES_JS_FILE}'`)
if (!fs.existsSync(RULES_JS_FILE)) {
    throw Error("rules.js not found, specify via RTT_RULES environment variable.")
}
const RULES = require(RULES_JS_FILE)
const TITLE_ID = path.basename(path.dirname(RULES_JS_FILE))

let rules_view_schema = null
if (!NO_SCHEMA && RULES.VIEW_SCHEMA) {
    console.log("View schema found; validating.")
    rules_view_schema = ajv.compile(RULES.VIEW_SCHEMA)
}

module.exports.fuzz = function(fuzzerInputData) {
    const data = new FuzzedDataProvider(fuzzerInputData)
    if (data.remainingBytes < 16) {
        // insufficient bytes to start
        return
    }
    const seed = data.consumeIntegralInRange(1, 2**35-31)
    const scenarios = Array.isArray(RULES.scenarios) ? RULES.scenarios : Object.values(RULES.scenarios).flat()
    const scenario = data.pickValue(scenarios)
    // if (scenario.startsWith("Random"))
    //     return
    const timeout = TIMEOUT ? Date.now() + TIMEOUT : 0

    // TODO randomize options
    const options = {}

	let roles = RULES.roles
	if (typeof roles === "function")
		roles = roles(scenario, options)

    const ctx = {
        data: fuzzerInputData,
        player_count: roles.length,
        players: roles.map(r => ({role: r, name: "rtt-fuzzer"})),
        scenario,
        options,
        replay: [],
        state: {},
        active: null,
        step: 0,
    }
    ctx.replay.push([null, ".setup", [seed, scenario, options]])
    ctx.state = RULES.setup(seed, scenario, options)

    while (true) {
        if (data.remainingBytes < 16) {
            // insufficient bytes to continue
            return
        }

        if (MAX_STEPS < 0 && ctx.step > -MAX_STEPS) {
            // Skip & ignore if we reach the limit
            return
        }

        ctx.active = ctx.state.active
        if (ctx.active === 'Both' || ctx.active === 'All') {
            // If multiple players can act, we'll pick a random player to go first.
            ctx.active = data.pickValue(roles)
        }

        const state_freeze = JSON.stringify(ctx.state)

        ctx.view = {}
        try {
            ctx.view = RULES.view(ctx.state, ctx.active)
        } catch (e) {
            return log_crash(new RulesCrashError(e, e.stack), ctx)
        }

        if (MAX_STEPS > 0 && ctx.step > MAX_STEPS) {
            return log_crash(new MaxStepError(`MAX_STEPS ${MAX_STEPS} reached`), ctx)
        }

        if (TIMEOUT && (Date.now() > timeout)) {
            return log_crash(new TimeoutError(`TIMEOUT (${TIMEOUT}) reached`), ctx)
        }

        if (rules_view_schema && !rules_view_schema(ctx.view)) {
            return log_crash(new SchemaValidationError("View data fails schema validation: " + rules_view_schema.errors), ctx)
        }

        if (state_freeze !== JSON.stringify(ctx.state)) {
            try {
                RULES.view(deep_freeze(JSON.parse(state_freeze)), ctx.active)
            } catch (e) {
                return log_crash(new ViewStateMutationError(e, e.stack), ctx)
            }
            throw Error("ASSERT frozen state change should have been caught.")
        }

        if (ctx.state.state === 'game_over') {
            break
        }

        if (ctx.view.prompt && ctx.view.prompt.startsWith("Unknown state:")) {
            return log_crash(new UnknownStateError(ctx.view.prompt), ctx)
        }

        if (!ctx.view.actions) {
            return log_crash(new NoMoreActionsError("No actions defined"), ctx)
        }

        const actions = ctx.view.actions
        if (NO_UNDO && 'undo' in actions) {
            // remove `undo` from actions, useful to test for dead-ends
            delete actions['undo']
        }

        // Tor: view.actions["foo"] === 0 means the "foo" action is disabled (show the button in a disabled state)
        // Also ignoring the actions with `[]` as args, unsure about this but needed for Nevsky.
        for (const [key, value] of Object.entries(actions)) {
            if (value === false || value === 0 || value.length === 0) {
                delete actions[key]
            }
        }

        if (Object.keys(actions).length === 0) {
            return log_crash(new NoMoreActionsError(), ctx)
        }

        const action = data.pickValue(Object.keys(actions))
        let args = actions[action]
        const prev_seed = ctx.state.seed

        if (Array.isArray(args)) {
            // check for NaN as any suggested action argument and raise an error on those
            for (const arg of args) {
                if (isNaN(arg)) {
                    return log_crash(new InvalidActionArgument(`Action '${action}' argument has NaN value`), ctx)
                }
            }
            args = data.pickValue(args)
            ctx.replay.push([ctx.active, action, args])
        } else {
            args = undefined
            ctx.replay.push([ctx.active, action])
        }
        // console.log(ctx.state.state, ctx.active, action, args)
        try {
            ctx.state = RULES.action(ctx.state, ctx.active, action, args)
        } catch (e) {
            return log_crash(new RulesCrashError(e, e.stack), ctx, action, args)
        }

        if (action === "undo") {
            if (ctx.state.active !== ctx.active && ctx.state.active !== "Both") {
                return log_crash(new UndoActiveError(`undo caused active to switch from ${ctx.active} to ${ctx.state.active}`), ctx, action, args)
            }
            if (ctx.state.seed !== prev_seed) {
                return log_crash(new UndoSeedError(`undo caused seed change from ${prev_seed} to ${ctx.state.seed}`), ctx, action, args)
            }
        }

        if (!NO_ASSERT && RULES.assert_state) {
            try {
                RULES.assert_state(ctx.state)
            } catch (e) {
                return log_crash(new RulesAssertError(e, e.stack), ctx, action, args)
            }
        }

        ctx.step += 1
    }
}


function log_crash(error, ctx, action=undefined, args=undefined) {
    // console.log()
    // console.log("VIEW", ctx.view)
    let line = `ERROR=${error.name}`
    line += ` STEP=${ctx.step} ACTIVE=${ctx.active} STATE=${ctx.state?.state}`
    if (action !== undefined) {
        line += ` ACTION=${action}`
        if (args !== undefined)
            line += " ARGS=" + JSON.stringify(args)
    }
    const game = {
        setup: {
	    title_id: TITLE_ID,
            scenario: ctx.scenario,
            player_count: ctx.player_count,
            options: ctx.options,
        },
        players: ctx.players,
        state: ctx.state,
        replay: ctx.replay,
    }

    const data_checksum = crypto.createHash('sha1')
    data_checksum.update(ctx.data)
    const data_hash = data_checksum.digest('hex')

    const game_checksum = crypto.createHash('sha1')
    game_checksum.update(JSON.stringify(game))
    const game_hash = game_checksum.digest('hex')

    const crash_file = `crash-${data_hash}`
    const out_file = `crash-${game_hash}.json`
    line += " SETUP=" + JSON.stringify(ctx.replay[0][2])
    line += ` DATA=${data_hash} DUMP=${out_file}`
    if (error.message)
        line += " MSG=" + JSON.stringify(error.message.replace(/^Error: /, ''))
    if (error.stack) {
        const regex = /\/Users\/\w+\/Projects\/rtt\//gi
        line += " STACK=" + JSON.stringify(error.stack.replace(regex, ''))
    }

    if (!fs.existsSync(out_file)) {
        console.log(line)
        fs.writeFileSync(out_file, JSON.stringify(game))
        if (NO_CRASH)
            fs.writeFileSync(crash_file, ctx.data)
    } else if (!NO_CRASH) {
        console.log(line)
    }
    if (!NO_CRASH) {
        throw error
    }
}

function deep_freeze(object) {
    // Retrieve the property names defined on object
    const propNames = Reflect.ownKeys(object);
    // Freeze properties before freezing self
    for (const name of propNames) {
      const value = object[name];
      if ((value && typeof value === "object") || typeof value === "function") {
        deep_freeze(value);
      }
    }
    return Object.freeze(object);
}

// Custom Error classes, allowing us to ignore expected errors with -x
class UnknownStateError extends Error {
    constructor(message) {
      super(message)
      this.name = "UnknownStateError"
    }
}

class MaxStepError extends Error {
    constructor(message) {
      super(message)
      this.name = "MaxStepError"
    }
}

class NoMoreActionsError extends Error {
    constructor(message) {
      super(message)
      this.name = "NoMoreActionsError"
    }
}

class InvalidActionArgument extends Error {
    constructor(message) {
      super(message)
      this.name = "InvalidActionArgument"
    }
}

class UndoActiveError extends Error {
    constructor(message) {
      super(message)
      this.name = "UndoActiveError"
    }
}

class UndoSeedError extends Error {
    constructor(message) {
      super(message)
      this.name = "UndoSeedError"
    }
}

class RulesCrashError extends Error {
    constructor(message, stack) {
      super(message)
      this.name = "RulesCrashError";
      this.stack = stack
    }
}

class RulesAssertError extends Error {
    constructor(message, stack) {
      super(message)
      this.name = "RulesAssertError";
      this.stack = stack
    }
}

class SchemaValidationError extends Error {
    constructor(message) {
      super(message)
      this.name = "SchemaValidationError"
    }
}

class ViewStateMutationError extends Error {
    constructor(message, stack) {
      super(message)
      this.name = "ViewStateMutationError";
      this.stack = stack
    }
}

class TimeoutError extends Error {
    constructor(message) {
      super(message)
      this.name = "TimeoutError"
    }
}