santhosh-tekuri/jsonschema

Crash in WASM app because of calls to filesystem functions

Closed this issue · 15 comments

JSON schema crashes at WASM app startup during the initialization of the drafts. The reason is the call to stat function, which is not implemented for the WASM platform. The call is in toAbs function where it calls filepath.Abs.

I tested the same and it works. below is the program I have tested:

  package main

  import (
      "encoding/json"
      "fmt"

      "github.com/santhosh-tekuri/jsonschema/v5"
  )

  func main() {
      schema := `{"type": "object"}`
      instance := `{"foo": "bar"}`

      sch, err := jsonschema.CompileString("mem://schema.json", schema)
      if err != nil {
          panic(err)
      }

      var v interface{}
      if err := json.Unmarshal([]byte(instance), &v); err != nil {
          panic(err)
      }

      err = sch.Validate(v)
      fmt.Println("error:", err)
  }

note that if you use "schema.json" instead of "mem://shema.json" then i get the same error that you got.

if no scheme is specified, then this library assumes that it is file. since wasm does not implement filesystem calls, we have to avoid this by using some dummy scheme say mem

I don't know how you got this to work as WASM, but it fails on chrome before main starts running. The problem is the relative references in the schema drafts. While initializing the drafts, it calls isAbs, which then goes on to call filesystem funcs to resolve directories.

relative urls are resolved against the document url. in case of draft, the document url is of type http url. so it never lands up using filesystem calls.

try the sample i mentioned above at your end and check if it still fails

BTW, I am using safari on mac

I tested on chrome on mac. it works fine.

As I said, in my case it is not even hittingmain. It crashed before that, in init(). I added some printlns, and the last thing it prints is one of the meta/ components of the schema drafts. It is a relative reference in the draft text.

Are you using V5 version

It is the v5 version. Here's the stack trace, obtained by commenting out my fix:

path is:  annotations.json  // This fmt.Println is just before this in isAbs(): if s, err = filepath.Abs(s); err != nil { 
wasm_exec.js:51 panic: stat .: not implemented on js
wasm_exec.js:51 
wasm_exec.js:51 goroutine 1 [running]:
wasm_exec.js:51 github.com/santhosh-tekuri/jsonschema/v5.MustCompileString({0x15dbc0, 0x10}, {0x15102e, 0x2})
wasm_exec.js:51 	/home/bserdar/github.com/cloudprivacylabs/lsa-playground/vendor/github.com/santhosh-tekuri/jsonschema/v5/compiler.go:68 +0x1c
wasm_exec.js:51 github.com/cloudprivacylabs/lsa/pkg/json.init()
wasm_exec.js:51 	/home/bserdar/github.com/cloudprivacylabs/lsa-playground/vendor/github.com/cloudprivacylabs/lsa/pkg/json/import.go:90 +0x2

MustCompileString is never called in the library. it must be getting called by your code. you can check this by looking at usages of MustCompilieString.

I guess the path annotations.json is coming from your code/schema

Turns out annotations.json is one of my files. And using mem://annotations.json fixes the problem.

So we can close this ticket and the PR if you want, or the PR can still stay so that it works for relative JSON schemas in WASM.

Sorry for the trouble,

Without PR it works for relative json also

How so? I have to change the filename to mem://annotations.json to make it work. annotations.json still crashes.

yes. you have to use absolute url like mem://annotations.json. because relative path is assumed to be file relative to current working directory.

but any relative paths used inside annotations.json are resolve against mem://annotations.json. Only the root resource is interpreted as file if relative path is given.

in case anybody else is wondering how to use this lib with wasm I'll leave those snippets here:
@bserdar did you happen to get this to work with tinygo insteand of go? The binary size of the generated .wasm file is around 4MB, which is a lot for the browser.

TinyGo compiles without errors on my machine, but the generated wasm file does not load in the browser because of an error reflect.Type.NumMethod() is unimplemented (see tinygo-org/tinygo#2660 for reference)

package main

import (
	"encoding/json"
	"syscall/js"

	"github.com/santhosh-tekuri/jsonschema/v5"
)

func main() {
	js.Global().Set("JSONSchemaValidate", js.FuncOf(validate))
	<-make(chan bool)
}

func validate(this js.Value, args []js.Value) interface{} {
	compiler := jsonschema.NewCompiler()
	compiler.Draft = jsonschema.Draft2020

	// get an object
	schema := args[0].String()
	instance := args[1].String()

	compiledSchema, err := jsonschema.CompileString("mem://schema.json", schema)
	if err != nil {
		panic(err)
	}

	var parsedJSON interface{}
	if err := json.Unmarshal([]byte(instance), &parsedJSON); err != nil {
		panic(err)
	}

	err = compiledSchema.Validate(parsedJSON)
	if err != nil {
		if validationError, ok := err.(*jsonschema.ValidationError); ok {
			b, _ := json.Marshal(validationError.DetailedOutput())
			return string(b)
		} else {
			panic(err)
		}

	}

	return nil
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Go wasm</title>
  </head>

  <body>
    <script src="wasm_exec.js"></script>
    <script>
      const go = new Go();

      WebAssembly.instantiateStreaming(
        fetch("./main.wasm"),
        go.importObject
      ).then((res) => {
        go.run(res.instance);

        console.time("validate");
        const result = JSONSchemaValidate(
          `{"properties": { "val": {"const": 10}, "val2": {"const": 5}}, "required": ["val", "val2"]}`,
          `{"val": 9, "val2": 1}`
        );
        console.timeEnd("validate");
        console.log({ result: JSON.parse(result) });
      });
    </script>
  </body>
</html>

@manuschillerdev I did not try this with tinygo. This is part of a rather large project, and my binary size is around 15M.