Registry Mirror
Skip to content

Conversation

@proggeramlug

Copy link
Copy Markdown
Contributor

JSON.parse(<short runtime string>) segfaults. Minimal repro:

JSON.parse('e' + 'n');              // SIGSEGV
JSON.parse('enabled'.slice(0, 2));  // SIGSEGV
JSON.parse(String(1));              // SIGSEGV

Cause

The JSON.parse codegen (expr/instance_misc1.rs) lowered its operand with unbox_to_i64, which just extracts the raw NaN-boxed bits. For a heap string that's the StringHeader* (works); for an SSO short string the bits are the inline character bytes, handed to js_json_parse, which dereferences them as a *StringHeader and reads byte_len off a garbage address (the fault address is literally the inline chars, e.g. 0x…6e65 = "ne").

Heap strings (string literals, large concatenations) happened to work, so it hid until a workload parsed many short runtime values. Found bringing the Skelpo CMS up on Perry: @perryts/mysql decodes a JSON column, the app re-JSON.parses the short results, and the whole HTTP service segfaults on GET /api/v1/settings.

Fix

Lower both the generic and typed (js_json_parse_typed_array) JSON.parse arms through js_get_string_pointer_unified — it returns the heap pointer for heap strings and materializes SSO / number receivers to the heap first (the same coercion already used at other string FFI call sites).

Verified

JSON.parse('e'+'n') now throws a catchable SyntaxError instead of crashing; '1'/'42'/'true' parse correctly. End-to-end: the CMS's /api/v1/settings (≈940 KB of JSON-column data) now returns 200 and matches Node, and a full 15-route Node-vs-Perry sweep is identical.

`JSON.parse(s)` lowered the operand with `unbox_to_i64`, which extracts the
raw NaN-boxed bits. For a heap string that's the `StringHeader*` (fine), but
for an SSO short string it's the INLINE character bytes — handed to
`js_json_parse`, which dereferences them as a `*StringHeader` and reads
`byte_len` off a garbage address → SIGSEGV.

So any short, runtime-built string crashed: `JSON.parse('e' + 'n')`,
`JSON.parse('enabled'.slice(0,2))`, `JSON.parse(String(1))`, etc. Heap strings
(string literals, large concatenations) happened to work, which is why it hid
until a real workload parsed many short values — e.g. @perryts/mysql decodes a
JSON column and the consumer re-`JSON.parse`s the short results, segfaulting a
whole HTTP service.

Lower both the generic and typed (`js_json_parse_typed_array`) arms through
`js_get_string_pointer_unified`, which returns the heap pointer for heap
strings and materializes SSO / number receivers to the heap first. Verified:
`JSON.parse('e'+'n')` now throws a catchable SyntaxError instead of crashing;
short valid inputs (`'1'`, `'42'`, `'true'`) parse correctly.
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

Successfully merging this pull request may close these issues.

1 participant