Skip to content

Implementation of cwd function done and tested - #18856

Closed
impact-maker wants to merge 0 commit into
emscripten-core:mainfrom
impact-maker:main
Closed

impact-maker wants to merge 0 commit into
emscripten-core:mainfrom
impact-maker:main

Conversation

@impact-maker

Copy link
Copy Markdown

Description
Currently the mount function is copied from library_fs.js and needs a cleanup and some code changes and testing to be successfully implemented.

@impact-maker

Copy link
Copy Markdown
Author

@tlively @azakai I am trying to implement the mount function and new to this project. Would like to have your inputs and feedback which will help me implement and test the mount function and give it a closure.

@kripken

kripken commented Feb 27, 2023

Copy link
Copy Markdown
Member

I would say the next step is to find or add a test for this, and then to see that it works. There is likely an existing test for the old FS, which we should get working in WasmFS.

To find a test, you can search for FS.mount under test/. Then you can find which test uses that file by searching in test/*.py as all the code to run the tests is in those python files. Then if the test is test_foo you'd run it like ./tests/runner.py wasmfs.test_foo which runs it in WasmFS mode.

There are more details that might help in the main issue for this.

@kripken kripken mentioned this pull request Feb 27, 2023
@impact-maker

Copy link
Copy Markdown
Author

I would say the next step is to find or add a test for this, and then to see that it works. There is likely an existing test for the old FS, which we should get working in WasmFS.

To find a test, you can search for FS.mount under test/. Then you can find which test uses that file by searching in test/*.py as all the code to run the tests is in those python files. Then if the test is test_foo you'd run it like ./tests/runner.py wasmfs.test_foo which runs it in WasmFS mode.

There are more details that might help in the main issue for this.

@kripken The mount function implementation might use cwd function as well. I have tried to implement the cwd() function. Currently it requires testing which I am working on. After testing cwd function, will give a try to implement and test the mount function.

@impact-maker impact-maker changed the title Added the mount function signature Implementation of cwd done and tested Mar 7, 2023
@impact-maker

Copy link
Copy Markdown
Author

@kripken @tlively @sbc100 Implemented the cwd function and tested it too. Kindly review the PR and help me with the changes.

@impact-maker impact-maker changed the title Implementation of cwd done and tested Implementation of cwd function done and tested Mar 7, 2023
int __syscall_mremap(intptr_t old_addr, size_t old_size, size_t new_size, int flags, intptr_t new_addr);
int __syscall_poll(intptr_t fds, int nfds, int timeout);
int __syscall_getcwd(intptr_t buf, size_t size);
const char* __syscall_getcwd_woutargs();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible we should avoid adding a new syscall here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting an unidentified declaration error without adding this function in this class. Any alternate suggestions from your end could help.

Comment thread system/lib/wasmfs/syscalls.cpp Outdated

auto res = result.c_str();

return res;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are returning a pointer into result which is a std::string on the stack.. so the pointer won't be valid once you return.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is there some reason to create a new function that seems to duplicate the existing __syscall_getcwd ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 I refered the documention which states that the cwd() does not take any arguments so I had to make a new getcwd() function because the old one takes the path and size as the arguments. Do correct if I am thinking in the wrong direction or if the function can support arguments contrary to what the documentation states.

Kindly refer https://emscripten.org/docs/api_reference/Filesystem-API.html#new-file-system-wasmfs for the FS.cwd() function definition.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The high level FS.cwd() doesn't take any args but it can call the lower level __syscall_getcwd that does take args.

IIUC it can/should then read the result from the output pointer and return a JS string based on those results.

Comment thread src/library_wasmfs.js Resolved
Comment thread system/lib/wasmfs/js_api.cpp Outdated
free(state);
}

const char* __wasmfs_getcwd(){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention here seems to be a single underscore prefix.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will rectify this. Thank you

Comment thread src/library_wasmfs.js Outdated
// For file preloading, cwd should be '/' to begin with.
return '/';
return withStackSave(() => {
return __wasmfs_getcwd();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function is JS function that returns a string I don't think you want to just return a "char *" here since that will result in Number (pointer) in JavaScript.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 I have declared the return type for __wasmfs_getcwd() as const char. Could you check the function singature and help me rectify it.

@impact-maker
impact-maker force-pushed the main branch 2 times, most recently from 9c828ef to ed11a06 Compare March 9, 2023 19:00
@impact-maker

Copy link
Copy Markdown
Author

Description
Implemented the FS.cwd() function and made all the changes and build the code successfully.

Testing
Finally made changes in test_core.py to add the existing test case for cwd() and successfully ran the test/runner test_loop command which is running test_core successfully and showing ok message.

@impact-maker

Copy link
Copy Markdown
Author

@sbc100 Made all the changes suggested.
@tlively @kripken @sbc100 Kindly review my changes and provide me your valuable feedback to fully implement this function and help me give it a closure and get it merged.

Comment thread src/library_wasmfs.js Outdated
// For file preloading, cwd should be '/' to begin with.
return '/';
return withStackSave(() => {
var buffer = allocateUTF8OnStack(path);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is path being defined here?

The argument to allocateUTF8OnStack is a number of bytes so I would have expected something liuke {{{ PATH_MAX }}} here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 Thank you for your feeback. I had some counter queries

Is it possible to pass path argument to cwd() like the below code snippet
cwd: (path) => {
return withStackSave(() => {
var buffer = allocateUTF8OnStack(path);
var result = _wasmfs_getcwd(buffer,buffer.length);
return UTF8ToString(buffer);
});
},
In case it is not allowed how are we expected to add the path to allocateUTF function.
Also wanted to clarify if the PATH_MAX you are refering to, is same as the path I would pass in the FS.cwd() argument.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC FS.cwd() doesn't take an argument ever, it just returns the current working directory as a JS string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case you would want to use stackAlloc rather than allocateUTF8OnStack because you do not already have a string. The argument to stackAlloc would be the {{{ PATH_MAX }}} @sbc100 mentioned, and is the number of bytes necessary for the largest allowable path string.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlively @sbc100 Thanks for the clarification and being patient enough to answer my queries. I have made the changes in the latest commit. Kindly review the changes.

Meanwhile I had some few queries

  1. I was trying to understand different implementation of cwd() which made me come to an alternative to stackAlloc which is adding char buffer[PATH_MAX] in _wasmfs_getcwd() function and then passing it to the __syscall_getcwd call. Should I implement with that approach.
  2. To compile library_wasmfs.js I had to comment out FS__deps and set {{{ cDefine('S_IRUGO') }}} {{{ cDefine('S_IXUGO') }}} and {{{ cDefine('S_IWUGO') }}} to some dummy integer. Not sure if it the right approach but the build and testing seems to work after these changes. Can you some alternative to this error resolution.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Using stack space more sense to me in this case since it avoids permanntly allocating the space (you would need to write (since you would need to add static to char buffer[PATH_MAX] I believe).
  2. {{{ cDefine('S_IRUGO') }}} should work. If they don't then something odd is going on with your checkout. What errors do you see?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 Understood and it seems like a better approach to allocate the space.
I was getting an 'S_IRUGO' not defined error earlier but now suddenly its not occuring. Trying to figure out the root cause for it for future development and testing purposes. I reckon I missed ./emcc --clear-cache command. Will try to raise an issue for the same.
@kripken @kripken @sbc100 Since I have made all the required changes and the build and testing is successful. Kindly suggest me if I need to make any other changes in the code or the testing.
Also I had a curious question on how to make this code merge ready as I can see some of the checks blocked. Is there any documentation to pass all the checks with proper coding standards.

@jameshu15869 jameshu15869 Mar 11, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@impact-maker
I had a similar error with S_IRUGO and for me it was caused by settings WASMFS=TRUE in settings.js. Leaving WASMFS=FALSE and manually setting the flags in the test_core.py as shown below fixed the problem for me.

self.emcc_args += ['-sWASMFS']
self.emcc_args += ['-sFORCE_FILESYSTEM']

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % nits

Comment thread system/lib/wasmfs/js_api.cpp Outdated
}

int _wasmfs_getcwd(char* path, size_t size){
return __syscall_getcwd((intptr_t)path,size);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking like this file uses 2 spaces for indentation.

Can you add a space after the comma in the argument list?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 Remove the single space from the return statement and added spaces in the argument after comma

Comment thread src/library_wasmfs.js Resolved
Comment thread src/library_wasmfs.js Outdated
// For file preloading, cwd should be '/' to begin with.
return '/';
return withStackSave(() => {
var buffer = stackAlloc({{{PATH_MAX}}});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PATH_MAX is defined in C code, so this will not just work as it is. Instead, {{{ cDefine('PATH_MAX') }}} is what we want - that looks up a C define. Note that you must also add PATH_MAX to src/struct_info.json.

Comment thread src/struct_info.json Outdated
"S_IRWXUGO",
"S_IXUGO"
"S_IXUGO",
"PATH_MAX"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I assumed we were already exporting this defined.

Can you make a new block for limits.h (where MAX_PATH is defined).

You will also need to run ./test/runner other.test_gen_struct_info --rebase to update the test expectation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 Working on this. Since you mentioned one test here, I was curious enough to know what are the exact set of test commands one has to check locally before submitting the code patch and send the pull request here.

@impact-maker

impact-maker commented Mar 15, 2023 •

Copy link
Copy Markdown
Author

@sbc100 @kripken @tlively I have tried to give a fair amount of time to the cloud test cases. I have drawn the following inferences and would need your help to proceed further to resolve all the checks.

  1. test-browser-chrome test: __wasmfs_getcwd is not defined- I have added this function to the emcc.py exports and all the required areas but still encountering the error.
  2. test-browser-firefox test: encoutering the same error as test-browser--chrome.
  3. test-core3- Not able to figure out the reasons to why this test is failing
  4. test-mac- Not able to figure out the reasons to why this test is failing
  5. test-other- Only able to figure out the Test failing alloc() error

Also while testing locally what are mandatory tests one would recommend to use before pusing the code to github main repository. Do we need to use all the test cases mentioned in https://emscripten.org/docs/getting_started/test-suite.html documentation or just the subset of it . I hope these inputs will help me complete this pull request and guide me for the upcoming functions implementation contributions.

@sbc100

sbc100 commented Mar 15, 2023

Copy link
Copy Markdown
Collaborator

its up to you what tests you run locally. The CI systems will run all them either way, once you upload.

@tlively

tlively commented Mar 16, 2023

Copy link
Copy Markdown
Member

It's not immediately obvious to me why you are getting those errors on CI. @impact-maker, are you able to reproduce the errors when you test locally? Passing --verbose to the test runner will get you the exact build command for the test, which you can then use to dig deeper.

@impact-maker
impact-maker force-pushed the main branch 4 times, most recently from c14ca4a to ce18bc5 Compare March 17, 2023 20:02
@impact-maker

Copy link
Copy Markdown
Author

@tlively @kripken @sbc100 I am consistently trying to run the test case but still encountering errors in three test cases

  1. Test-core3: I have defined __wasmfs_getcwd in the exports but still it is treating the function as an undeclared variable
    ERROR - [JSC_UNDEFINED_VARIABLE] variable __wasmfs_getcwd is undeclared
    var buf = __wasmfs_getcwd(buffer, buffer.length);

  2. Test-browser-chrome and mozilla: These browser test cases are giving the same error
    AssertionError: Expected to find '/report_result?exit:0
    ' in '/report_result?exception:__wasmfs_getcwd is not defined

Kindly assist me to help complete this long due pull request or any workouts to resolve these specific function related errors.

Comment thread test/test_core.py Outdated
Comment on lines +5716 to +5717
self.emcc_args += ['-sWASMFS']
self.emcc_args += ['-sFORCE_FILESYSTEM']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of forcing this test to use WasmFS, it would be better to use the @also_with_wasmfs_js decorator. But looking at the test file, it doesn't look like it even uses the getcwd JS API, so we probably don't want to modify this test at all.

Is there another test we can reuse that already uses the JS API? If not, we should add a new test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlively config.yml is calling wasmfs.test_getcwd_with_non_ascii_name so that the reason why I did not write a new test. Is there any other place where this test needs to be called or should I go ahead to write a new test for this.

Comment thread src/library_wasmfs.js Outdated
'$allocateUTF8OnStack',
'$withStackSave',
'$readI53FromI64',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary whitespace.

Comment thread emcc.py Outdated
# JS API directly.
settings.REQUIRED_EXPORTS += [
'_wasmfs_write_file',
'_wasmfs_getcwd',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the failing wasmfs.test_emscripten_get_now test locally, and the problem is that this line here adds _wasmfs_getcwd only if FORCE_FILESYSTEM is enabled, but in library_wasmfs.js, FS.getcwd is included even without that option. So we need to add _wasmfs_getcwd to REQUIRED_EXPORTS up where _wasmfs_read_file is added rather than here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlively Thank you for helping me out with the error, almost all the test cases error are resolved. One last error is the browser test case
Expected to find '/report_result?0
' in '[no http server activity]
', diff:
This is happening on my local too. I am not able to find any solution to this browser error, neither I am able to find any workaround this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes that error message is caused by flaky CI. If you modify the command the test executes (which you can find with runner --verbose) to build to .html instead of .js, you should be able to serve the test to yourself with emrun --no_browser <path to build directory>. If you do that, does the test behave as expected in the browser locally?

Comment thread src/library_wasmfs.js Outdated
Comment on lines 108 to 128
//Implemented cwd
//test commit
// TODO: Remove dependency on FS.cwd().
// User code should not be using FS.cwd().
// For file preloading, cwd should be '/' to begin with.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all these comments should be removed.

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.

5 participants