Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

fs.realpathSync.native fails when on a RAM disk on Windows #35

Closed
rvaidya opened this issue Feb 6, 2019 · 7 comments
Closed

fs.realpathSync.native fails when on a RAM disk on Windows #35

rvaidya opened this issue Feb 6, 2019 · 7 comments

Comments

@rvaidya
Copy link

rvaidya commented Feb 6, 2019

I see the comment further down wrapping a different part of the realpathSync function in a try/catch for this case, but the top part also fails when in this situation.

If I change the code to this:

function realpathSync(filepath) {
	if (typeof fs.realpathSync.native === 'function') {
		try {
			return fs.realpathSync.native(filepath);
		} catch (err) {
			/* Probably RAM-disk on windows. */
		}
	}

	const fsBinding = process.binding('fs');

	if (fsBinding.realpath) {
		try {
			return fsBinding.realpath(filepath, 'utf8');
		} catch (err) {
			/* Probably RAM-disk on windows. */
		}
	}

	return fs.realpathSync(filepath);
}

Then the 2nd part of the function (the one with the existing comment) makes node completely crash due to this line:

fsBinding.realpath(filepath, 'utf8');

npm[145404]: src\node_file.cc:1425: Assertion `(argc) >= (3)' failed.
 1: 00007FF6A7380EFA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4810
 2: 00007FF6A735A296 node::MakeCallback+4518
 3: 00007FF6A735A34F node::MakeCallback+4703
 4: 00007FF6A731E762 uv_loop_fork+31394
 5: 00007FF6A7B8ADD2 v8::internal::OptimizingCompileDispatcher::Unblock+60562
 6: 00007FF6A7B8C26D v8::internal::OptimizingCompileDispatcher::Unblock+65837
 7: 00007FF6A7B8B2C9 v8::internal::OptimizingCompileDispatcher::Unblock+61833
 8: 00007FF6A7B8B1AB v8::internal::OptimizingCompileDispatcher::Unblock+61547
 9: 000002F70B1DC5C1

Essentially, under this condition you'd want to skip both of the above attempts and fallback to fs.realpathSync(filepath)

@SimenB
Copy link
Owner

SimenB commented Feb 7, 2019

Oh, it aborts? 😞 Would you be able to put together a PR the prevents this?

@rvaidya
Copy link
Author

rvaidya commented Apr 9, 2019

New PR #39

@gigahunter
Copy link

It's work...

function realpathSync(filepath) {
    if (typeof fs.realpathSync.native === 'function') {
        try {
            return fs.realpathSync.native(filepath);
        } catch (err) {
            /* Probably RAM-disk on windows. */
            return fs.realpathSync(filepath);
        }
    }

    const fsBinding = process.binding('fs');

    if (fsBinding.realpath) {
        try {
            return fsBinding.realpath(filepath, 'utf8');
        } catch (err) {
            /* Probably RAM-disk on windows. */
        }
    }

    return fs.realpathSync(filepath);
}

@heavenkiller2018
Copy link

on one to fix the only bug?

@ilatypov
Copy link

ilatypov commented Apr 26, 2020

Apart from using a Windows RAM disk, this optimization module breaks under Contrast Security which hides fs.realpathSync.native. Versions of Node 10 and above changed their "process binding" realpath from sync to async, so getting to line 36 throws the hard abort.

realpath-native/index.js

Lines 27 to 42 in 7d20312

function realpathSync(filepath) {
if (typeof fs.realpathSync.native === 'function') {
return fs.realpathSync.native(filepath);
}
const fsBinding = process.binding('fs');
if (fsBinding.realpath) {
try {
return fsBinding.realpath(filepath, 'utf8');
} catch (err) {
/* Probably RAM-disk on windows. */
}
}
return fs.realpathSync(filepath);

$ nvm use 9.11.2
Now using node v9.11.2 (npm v5.6.0)

$ node -e 'fs = require("fs"); fsBinding = process.binding("fs"); console.log(fsBinding.realpath(".", "utf8"))'
/Users/USER/DIR

$ nvm use 10.0.0
Now using node v10.0.0 (npm v5.6.0)

$ node -e 'fs = require("fs"); fsBinding = process.binding("fs"); console.log(fsBinding.realpath(".", "utf8"))'
node[4727]: ../src/node_file.cc:1170:void node::fs::RealPath(const FunctionCallbackInfo<v8::Value> &): Assertion `(argc) >= (3)' failed.
1: node::Abort() [/Users/USER/.nvm/versions/node/v10.0.0/bin/node]
2: node::InternalCallbackScope::~InternalCallbackScope() [/Users/USER/.nvm/versions/node/v10.0.0/bin/node]
3: node::fs::RealPath(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/USER/.nvm/versions/node/v10.0.0/bin/node]
4: v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo*) [/Users/USER/.nvm/versions/node/v10.0.0/bin/node]
5: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/USER/.nvm/versions/node/v10.0.0/bin/node]
6: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/Users/latypil/.nvm/versions/node/v10.0.0/bin/node]
7: 0x3c81dc58427d
Abort trap: 6

https://github.com/nodejs/node/blob/v10.0.0/src/node_file.cc#L1170

Since it's impossible to catch the abort or to find the internal method's contract, I wonder if the optimization of using the native realpath should be dropped in the sync case? I.e. delete lines 32-40. (I don't know if Node has public mechanisms to turn an async method into a sync one).

@ilatypov
Copy link

ilatypov commented May 1, 2020

Never mind, the latest Contrast version seems to correctly expose fs.realpathSync.native.

@SimenB
Copy link
Owner

SimenB commented May 7, 2020

I've published a v3 and deprecated it - all current releases of node supports fs.realpath.native now, so this module is no longer needed.

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

No branches or pull requests

5 participants