-
Notifications
You must be signed in to change notification settings - Fork 567
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
js: fix libyaml bindings #7918
Merged
Merged
js: fix libyaml bindings #7918
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,52 @@ open Js_of_ocaml | |
(see companion setter in Semgrep_js_shared.ml) *) | ||
external get_jsoo_mount_point : unit -> 'any list = "get_jsoo_mount_point" | ||
|
||
(* semgrep uses ocaml-yaml, which uses ocaml-ctypes to use the libyaml C library | ||
this is problematic for webassembly because it doesn't match the architecture | ||
of the build machine (e.g. most computers these days have a 64-bit address space, | ||
but WASM is currently 32-bit). cross-compilation ended up being too much work, so | ||
instead we exploit javascript's mutability to correct some memory offsets for WASM *) | ||
external override_yaml_ctypes_field_offset : | ||
('a, 'b) Yaml_bindings.T.field -> int -> unit | ||
= "override_yaml_ctypes_field_offset" | ||
|
||
let fix_libyaml_field_offsets_for_wasm () = | ||
(* mark *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Mark.line 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Mark.column 8; | ||
|
||
(* event *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.data 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.start_mark 32; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.start_mark 44; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. argh, typo |
||
|
||
(* version_directive *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Version_directive.minor 4; | ||
|
||
(* event_sequence_start *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Sequence_start.tag 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Sequence_start.implicit 8; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Sequence_start.style 12; | ||
|
||
(* event_scalar *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.tag 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.value 8; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.length 12; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.plain_implicit 16; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.quoted_implicit 20; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Scalar.style 24; | ||
|
||
(* event_mapping_start *) | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Mapping_start.tag 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Mapping_start.implicit 8; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Mapping_start.style 12; | ||
|
||
(* event_document_start *) | ||
override_yaml_ctypes_field_offset | ||
Yaml_types.M.Event.Document_start.tag_directives 4; | ||
override_yaml_ctypes_field_offset Yaml_types.M.Event.Document_start.implicit | ||
12 | ||
|
||
type jbool = bool Js.t | ||
type jstring = Js.js_string Js.t | ||
|
||
|
@@ -17,6 +63,8 @@ let _ = | |
from the web on demand when one select a language in the playground. | ||
old: Parsing_init.init (); | ||
*) | ||
fix_libyaml_field_offsets_for_wasm (); | ||
|
||
Js.export_all | ||
(object%js | ||
(* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<html> | ||
<body> | ||
<pre id="result">Loading...</pre> | ||
</body> | ||
<script> | ||
window.module = { exports: {} }; | ||
</script> | ||
<script src="../engine/dist/index.cjs"></script> | ||
<script> | ||
window.EngineFactory = module.exports.EngineFactory; | ||
</script> | ||
<script> | ||
const rules = JSON.stringify({ | ||
rules: [ | ||
{ | ||
id: "test", | ||
languages: ["yaml"], | ||
message: "test", | ||
pattern: "foo: $Y", | ||
severity: "ERROR", | ||
}, | ||
], | ||
}); | ||
|
||
(async () => { | ||
const engine = await EngineFactory(); | ||
|
||
engine.writeFile("/static/rules.json", rules); | ||
engine.writeFile("/static/test.py", `foo: bar`); | ||
document.getElementById("result").innerHTML = JSON.stringify( | ||
JSON.parse( | ||
engine.execute("yaml", "/static/rules.json", "/static/test.py") | ||
), | ||
null, | ||
2 | ||
); | ||
})(); | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you end up with all those magic constants below?
you disassembled the resulting WASM?
If tomorrow the bus hit you, how someone else can update this code if say the libyaml C library changes a little bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, when I was investigating this I created a macro that I used to print the offset of every struct field. I'll restructure and add more comments to make it clearer what's going on here.