fix(fs): extend Node arg-validation to closeSync/readSync/writeSync/fchmodSync/chmodSync/renameSync (#2013)#2328
Merged
Conversation
…chmodSync/chmodSync/renameSync (#2013) PR #2035 added the validation pattern for `fs.readFileSync` / `fs.accessSync` / `fs.statSync` / `fs.openSync`; the issue's follow-up call-out lists ~27 fs functions that still hit "Missing expected exception" because they don't yet validate. This gap-fills the fd-only sync surface (`closeSync`, `readSync`, `readvSync`, `writeSync`, `writevSync`, `fchmodSync`, `fsyncSync`, `fdatasyncSync`, `fchownSync`) and the path-mutator sync surface (`chmodSync`, `renameSync`) so each: - throws `TypeError [ERR_INVALID_ARG_TYPE]` on a non-fd / non-path first argument (matches Node); - throws `Error [EBADF]` on a numeric fd that isn't open in perry-runtime's `FD_REGISTRY` (matches Node's syscall surface). The new `validate_fd_open(value, syscall)` helper combines the existing `validate_fd` type+range probe with an `FD_REGISTRY` membership check + EBADF throw. Callers that need the validation order preserved (`fchownSync`: validate uid+gid BEFORE EBADF, per Node's `validateInteger` ordering) keep the bare `validate_fd` and issue the EBADF after the rest of the int32 validations. `validate_fd` itself stays single-purpose so the FileHandle wrappers — which can legitimately hold a `-1` sentinel from a failed open — keep their silent no-op fallback. The parity test (`test-parity/node-suite/fs/sync/arg-validation.ts`) gets 13 new probes covering each new shape against `node --experimental-strip-types`; the existing legacy block stays byte-identical. `cargo test -p perry-runtime --lib` still 746/746.
3 tasks
proggeramlug
pushed a commit
that referenced
this pull request
May 28, 2026
…hrtime (#2013) Follow-up to PR #2328 (fs slice). Each process-surface entry point now throws the Node-matching `TypeError [ERR_INVALID_ARG_TYPE]` (or `ERR_UNKNOWN_SIGNAL` for `process.kill`'s second arg) on a wrong-type argument: - `process.chdir(123)` → ERR_INVALID_ARG_TYPE (was: garbage-deref to ENOENT). A new `js_process_chdir_jsv(value)` runtime entry takes the full NaN-boxed value, validates, then re-dispatches to the existing string-only `js_process_chdir`. The codegen lowering for `Expr::ProcessChdir` now emits the JSV entry instead of `unbox_to_i64` → bare `*const StringHeader`. - `process.kill('abc', 0)` → ERR_INVALID_ARG_TYPE on `pid`, `process.kill(0, {})` → ERR_UNKNOWN_SIGNAL on the signal arg (object/array/boolean don't reach the numeric coerce path). - `process.exit('foo')` → ERR_INVALID_ARG_TYPE on the code arg (null/undefined still pass through to the existing default). - `process.cpuUsage('abc')` → ERR_INVALID_ARG_TYPE on a non-object `prior`; null/undefined still pass through. - `process.hrtime('abc')` → ERR_INVALID_ARG_TYPE on a non-array `prior`. A new local `is_array_value` helper does the GC-type-tag probe so the heap-pointer-but-not-array shape (Object/Buffer/...) is also caught. The fs validation helpers (`describe_received`, `throw_type_error_with_code`, `is_numeric`) get crate-wide visibility so process can reuse the Node-shaped error formatting without duplicating the helpers. Parity test `test-parity/node-suite/process/cwd/arg-validation.ts` covers 14 probes byte-identical to `node --experimental-strip-types`. The `(process.chdir as any)(123)` indirection path still goes through typed-feedback (which doesn't validate); that's a separate gap in the method-value-via-cast dispatch family and is tracked under #1343.
proggeramlug
added a commit
that referenced
this pull request
May 29, 2026
…hrtime (#2013) (#2332) Follow-up to PR #2328 (fs slice). Each process-surface entry point now throws the Node-matching `TypeError [ERR_INVALID_ARG_TYPE]` (or `ERR_UNKNOWN_SIGNAL` for `process.kill`'s second arg) on a wrong-type argument: - `process.chdir(123)` → ERR_INVALID_ARG_TYPE (was: garbage-deref to ENOENT). A new `js_process_chdir_jsv(value)` runtime entry takes the full NaN-boxed value, validates, then re-dispatches to the existing string-only `js_process_chdir`. The codegen lowering for `Expr::ProcessChdir` now emits the JSV entry instead of `unbox_to_i64` → bare `*const StringHeader`. - `process.kill('abc', 0)` → ERR_INVALID_ARG_TYPE on `pid`, `process.kill(0, {})` → ERR_UNKNOWN_SIGNAL on the signal arg (object/array/boolean don't reach the numeric coerce path). - `process.exit('foo')` → ERR_INVALID_ARG_TYPE on the code arg (null/undefined still pass through to the existing default). - `process.cpuUsage('abc')` → ERR_INVALID_ARG_TYPE on a non-object `prior`; null/undefined still pass through. - `process.hrtime('abc')` → ERR_INVALID_ARG_TYPE on a non-array `prior`. A new local `is_array_value` helper does the GC-type-tag probe so the heap-pointer-but-not-array shape (Object/Buffer/...) is also caught. The fs validation helpers (`describe_received`, `throw_type_error_with_code`, `is_numeric`) get crate-wide visibility so process can reuse the Node-shaped error formatting without duplicating the helpers. Parity test `test-parity/node-suite/process/cwd/arg-validation.ts` covers 14 probes byte-identical to `node --experimental-strip-types`. The `(process.chdir as any)(123)` indirection path still goes through typed-feedback (which doesn't validate); that's a separate gap in the method-value-via-cast dispatch family and is tracked under #1343. Co-authored-by: Ralph Küpper <ralph@skelpo.com>
This was referenced May 29, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
PR #2035 added the validation pattern for
fs.readFileSync/fs.accessSync/fs.statSync/fs.openSync; the issue's follow-up call-out lists ~27 fs functions that still hit "Missing expected exception" because they don't yet validate. This gap-fills the fd-only sync surface and the path-mutator sync surface so each:TypeError [ERR_INVALID_ARG_TYPE]on a non-fd / non-path first argument (matches Node);Error [EBADF]on a numeric fd that isn't open in perry-runtime'sFD_REGISTRY(matches Node's syscall surface).Touched entry points:
closeSync,readSync,readvSync,writeSync(string + buffer),writevSync,fchmodSync,fsyncSync,fdatasyncSync,fchownSync— fd validation + EBADFchmodSync,renameSync— path validationNew
validate_fd_open(value, syscall)helper combines the existingvalidate_fdtype+range probe with anFD_REGISTRYmembership check + EBADF throw. Callers that need the validation order preserved (fchownSync: validate uid+gid BEFORE EBADF, per Node'svalidateIntegerordering) keep the barevalidate_fdand issue the EBADF after the rest of the int32 validations.validate_fditself stays single-purpose so the FileHandle wrappers — which can legitimately hold a-1sentinel from a failed open — keep their silent no-op fallback.Mode validation in
fchmodSyncandchmodSyncis deliberately omitted: Node usesparseFileModewhich throwsERR_INVALID_ARG_VALUE, not thevalidate_int32shape — left as a follow-up to keep the diff focused.Test plan
cargo fmt --all -- --checkclean.cargo test --release -p perry-runtime --lib— 746/746 green (no regressions).test-parity/node-suite/fs/sync/arg-validation.tsextended with 13 new probes (closeSync, readSync, readvSync, writeSync ×2, fchmodSync ×2, chmodSync, renameSync ×2 covering both type-error and EBADF branches). Byte-identical againstnode --experimental-strip-types; the existing legacy block stays byte-identical.Closes the fd-only + path-mutator slice of #2013; the remaining ~80 tests across buffer / net / zlib / http / process / crypto / url stay tracked under the same issue.