-
Notifications
You must be signed in to change notification settings - Fork 10
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
Implement directory mapping and WASI file functions #208
Conversation
ec68b20
to
2c9e383
Compare
@kulcsaradam
What is the main feature of mapping directories and where is this used? |
@clover2123 WASI by design has an idea, where for security reasons runtimes may not allow programs to freely move between host directories. By default they only allow files to manipulate in their own directories and in directories which they have handles to. Also, to obfuscate the system they are on, inside they use virtual directories like '/var' or '/home' which are actualy directories like 'home/user/walrus/test' for example. Use cases are any file manipulation operations like reading from a file or writing to one. I hope this is an acceptable answer! |
bcd1681
to
be1d9fb
Compare
e556f25
to
87359e0
Compare
ea81e17
to
478a393
Compare
478a393
to
27ad825
Compare
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.
LGTM
0ed0b74
to
88b6c6b
Compare
88b6c6b
to
1a02b92
Compare
The pr was closed by a mistake I caused while tinkering with Visual Studio. Reopened it now. |
b5c8f11
to
8e9d83b
Compare
08ed3ce
to
9c89d72
Compare
std::vector<std::string> variables = { | ||
// Common Unix environment variables. | ||
"PATH", | ||
"LIB", | ||
"PWD", | ||
"SHELL", |
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.
I wonder about how did you define these common env variables
Is there any reference?
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.
Yes, I've used these sites:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08
https://learn.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables
https://pureinfotech.com/list-environment-variables-windows-10/
Please tell me if there are any more that I should add or if I should remove some
preopen.real_path = reinterpret_cast<char*>(calloc(1, it->length() + 1)); | ||
it->nonstd::sv_lite::string_view::copy(const_cast<char*>(preopen.real_path), it->length()); | ||
|
||
it++; | ||
|
||
preopen.mapped_path = reinterpret_cast<char*>(calloc(1, it->length() + 1)); | ||
it->nonstd::sv_lite::string_view::copy(const_cast<char*>(preopen.mapped_path), it->length()); |
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.
Will these strings allocated by calloc
be correctly freed at the end of Walrus?
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.
No, I forgot about those. After running valgrind I fixed these and also added initialization to wasi envrionment variables, so valgrind did not return any errors.
c059163
to
fac5807
Compare
"SystemDrive", | ||
}; | ||
|
||
char** envp = (char**)malloc(sizeof(char**) * variables.size()); |
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.
What about simply allocating a vector of std::string
here?
This kind of manual memory alloc is so complicated to handle and hard for correct freeing
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.
After spending some time thinking how I would change it to be easily used with uvwasi, I could not think of a way. If you have an idea, it would be greatly appriciated.
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.
I see. I'll revise this later
src/wasi/Wasi.cpp
Outdated
if (init_options.preopenc > 0) { | ||
init_options.preopens = reinterpret_cast<uvwasi_preopen_t*>(calloc(init_options.preopenc, sizeof(uvwasi_preopen_s))); | ||
} | ||
for (unsigned i = 0; i < preopenDirs.size(); i++) { | ||
init_options.preopens[i].mapped_path = preopenDirs[i].mapped_path; | ||
init_options.preopens[i].real_path = preopenDirs[i].real_path; | ||
} |
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.
For this case, preopenDirs
already contains all the necessary information about directory mapping.
So, it seems that simply assigning the preopenDirs
into init_options.preopens
is possible like below
init_options.preopens = preopenDirs.data();
What do you think about this?
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.
You are right and it is also easier to understand so I changed it to be this way.
Refactor the importing of WASI functions to be clearer. Implement path_open, fd_seek, fd_read, environ_sizes_get, environ_get for file acces. Implement the mapping of real directories to virtual ones so that WASI can use different directories. Add flag '--mapdirs' 'real' 'virtual' for mapping directories. Add flag '--env' for sharing host envrionment variables. Add flag '--help' for printing available walrus options. Also improve random_get test to not have a result. Signed-off-by: Adam Laszlo Kulcsar <kuladam@inf.u-szeged.hu>
fac5807
to
7bf6c9d
Compare
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.
LGTM
Refactor the importing of WASI functions to be clearer.
Implement path_open, fd_seek, fd_read, environ_sizes_get, environ_get for file acces.
Implement the mapping of real directories to virtual ones so that WASI can use different directories.
Add flag '--mapdirs' '
real
' 'virtual
' for mapping directories.Add flag '--env' for sharing host envrionment variables.
Add flag '--help' for printing available walrus options.
Also improve random_get test to not have a result.