Skip to content
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

c++ stat crash in dr_client_main in Windows private libraries #7220

Open
yangyixiaof opened this issue Jan 27, 2025 · 2 comments
Open

c++ stat crash in dr_client_main in Windows private libraries #7220

yangyixiaof opened this issue Jan 27, 2025 · 2 comments

Comments

@yangyixiaof
Copy link

yangyixiaof commented Jan 27, 2025

To get the file size, I do not want to open that file.
So, I use c++ "stat" function.
Here is the total function I use to get a file size without opening a file.

size_t get_file_size(const std::string& file_path)
{
size_t res = 0;
struct stat buf;
int result;
result = stat(file_path.c_str(), &buf);
if (result != 0) {
// cout << "file info error, file may not be existed." << endl;
}
else {
res = buf.st_size;
}
return res;
}

When I invoke this function in dr_client_main, the dynamorio crashes.
The crash point is when the stat is invoked.
I tested it in normal visual studio console applicaiton, the above function runs normally.

The Operating System is Windows 10.
DynamoRIO version is DynamoRIO-Windows-11.3.0-1.

Using -debug option, the output is:
Internal Error: DynamoRIO debug check failure: D:\a\dynamorio\dynamorio\core\win32\loader.c:777 !is_dynamo_address(dcontext->app_fls_data)
(Error occurred @0 frags in tid 27296)
version 11.3.0, build 1
It seems some loading dll error happens when I use "stat" function in client.

@derekbruening
Copy link
Contributor

To ensure stability it is best to avoid external library routines in clients and use the DR API: here dr_file_size(). While DR tries to provide private versions of libraries, that is getting more and more difficult to maintain on not just Windows but Linux as well, and it may be impossible on Mac.

There likely are no team developer resources able to investigate whatever is going wrong in the Windows private libraries causing this issue with stat. We already know that several Windows system library copies fail to fully initialize, so this may be related to that. If you would like to investigate the root cause and submit a fix we would be happy to accept it. But if you are not able to help with that we suggest using dr_file_size(), or possibly adding a new DR API routine that gets the size without opening the file utilizing internal DR operations (you would need to drill down to Nt syscall wrappers on Windows), or filling in whatever Windows system libraries are missing in the drwinapi implementation that this stat call ends up using so that it avoids the private library copies.

@derekbruening derekbruening changed the title c++ stat crash in dr_client_main c++ stat crash in dr_client_main in Windows private libraries Jan 27, 2025
@yangyixiaof
Copy link
Author

To ensure stability it is best to avoid external library routines in clients and use the DR API: here dr_file_size(). While DR tries to provide private versions of libraries, that is getting more and more difficult to maintain on not just Windows but Linux as well, and it may be impossible on Mac.

There likely are no team developer resources able to investigate whatever is going wrong in the Windows private libraries causing this issue with stat. We already know that several Windows system library copies fail to fully initialize, so this may be related to that. If you would like to investigate the root cause and submit a fix we would be happy to accept it. But if you are not able to help with that we suggest using dr_file_size(), or possibly adding a new DR API routine that gets the size without opening the file utilizing internal DR operations (you would need to drill down to Nt syscall wrappers on Windows), or filling in whatever Windows system libraries are missing in the drwinapi implementation that this stat call ends up using so that it avoids the private library copies.

OK, I have used DynamoRIO for 2.5 years and I have read quite a few DynamoRIO source codes. My understanding of DynamoRIO is roughly sufficient to write a simple drwrap library by myself. Now it is difficult for me to dig deeply into DynamoRIO to figure out what happens to stat but I will keep trying to solve this issue. I will keep updating this post if I have made any progress.

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

No branches or pull requests

2 participants