From f75f05ce09c1e195ab17b74e50e01937749d61c0 Mon Sep 17 00:00:00 2001 From: Mischa Untaga <99098079+MischaU8@users.noreply.github.com> Date: Tue, 17 Oct 2023 09:49:53 +0200 Subject: view schema validation --- package.json | 3 ++- rtt-module.js | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 31c2ca9..70a9d6b 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dryRun": "jazzer rtt-module --sync -- -runs=100 -seed=123456789" }, "dependencies": { - "@jazzer.js/core": "^1.6.1" + "@jazzer.js/core": "^1.6.1", + "ajv": "^8.12.0" } } diff --git a/rtt-module.js b/rtt-module.js index ceb795d..fa363ca 100755 --- a/rtt-module.js +++ b/rtt-module.js @@ -1,5 +1,7 @@ "use strict" +const Ajv = require("ajv") +const ajv = new Ajv() const fs = require("fs") const { FuzzedDataProvider } = require("@jazzer.js/core") @@ -14,6 +16,12 @@ if (!fs.existsSync(RULES_JS_FILE)) { } const RULES = require(RULES_JS_FILE) +let rules_view_schema = null +if (RULES.VIEW_SCHEMA) { + console.log("View schema found; validating.") + rules_view_schema = ajv.compile(RULES.VIEW_SCHEMA) +} + module.exports.fuzz = function(fuzzerInputData) { let data = new FuzzedDataProvider(fuzzerInputData) if (data.remainingBytes < 16) { @@ -53,6 +61,14 @@ module.exports.fuzz = function(fuzzerInputData) { log_crash(game_setup, state, view, step, active) throw new RulesCrashError(e, e.stack) } + + if (rules_view_schema) { + if (!rules_view_schema(view)) { + log_crash(game_setup, state, view, step, active) + console.log(rules_view_schema.errors) + throw new SchemaValidationError("View data fails schema validation") + } + } if (step > MAX_STEPS) { log_crash(game_setup, state, view, step, active) @@ -102,7 +118,7 @@ module.exports.fuzz = function(fuzzerInputData) { } args = data.pickValue(args) } - // console.log(action, args) + // console.log(active, action, args) try { if (action !== "_resign") { state = RULES.action(state, active, action, args) @@ -161,3 +177,10 @@ class RulesCrashError extends Error { this.stack = stack } } + +class SchemaValidationError extends Error { + constructor(message) { + super(message) + this.name = "SchemaValidationError" + } +} -- cgit v1.2.3