Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in WASM app because of calls to filesystem functions #58

Closed
bserdar opened this issue Mar 1, 2022 · 15 comments
Closed

Crash in WASM app because of calls to filesystem functions #58

bserdar opened this issue Mar 1, 2022 · 15 comments

Comments

@bserdar
Copy link

bserdar commented Mar 1, 2022

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.

bserdar added a commit to bserdar/jsonschema that referenced this issue Mar 1, 2022
@santhosh-tekuri
Copy link
Owner

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

@bserdar
Copy link
Author

bserdar commented Mar 3, 2022

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.

@santhosh-tekuri
Copy link
Owner

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

@santhosh-tekuri
Copy link
Owner

BTW, I am using safari on mac

@santhosh-tekuri
Copy link
Owner

I tested on chrome on mac. it works fine.

@bserdar
Copy link
Author

bserdar commented Mar 3, 2022

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.

@santhosh-tekuri
Copy link
Owner

Are you using V5 version

@bserdar
Copy link
Author

bserdar commented Mar 3, 2022

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

@santhosh-tekuri
Copy link
Owner

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

@bserdar
Copy link
Author

bserdar commented Mar 3, 2022

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,

@santhosh-tekuri
Copy link
Owner

Without PR it works for relative json also

@bserdar
Copy link
Author

bserdar commented Mar 3, 2022

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

@santhosh-tekuri
Copy link
Owner

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.

@manuschillerdev
Copy link

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>

@bserdar
Copy link
Author

bserdar commented Mar 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants