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

Patching WSL2-imported .so #10

Open
mostm opened this issue Dec 1, 2021 · 3 comments
Open

Patching WSL2-imported .so #10

mostm opened this issue Dec 1, 2021 · 3 comments

Comments

@mostm
Copy link

mostm commented Dec 1, 2021

Hi!
Since I have system's DLL patched correctly (your patcher doesn't seem to pick up even 472.12, that's last patched driver by keylase/nvidia-patch), and from Windows itself invoking ffmpeg w/ nvenc accel doesn't result in session limiting, yet I still see session limiter in WSL itself:
image
I tried your patcher on /usr/lib/wsl/lib/libnvidia-encode.so.1 that's imported from host's %windir%\System32\DriverStore\FileRepository\nvmdi*\libnvidia-encode.so.1, and to my surprise, it seems as though this .so doesn't use libnvcuvid.so:
image

I looked at other issues in the repository, and it seems like providing .so would be helpful, so here it is:
libnvidia-encode.so.1.zip

@lars18th
Copy link

Hi,

Good news! I've prepared a simple patch that almost works with nvenc. Here my dirty patch:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5fa8620..aa75df1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,7 +16,8 @@ project(nvlax CXX)
 include(ExternalProject)
 include(cmake/FetchCPM.cmake)

-CPMAddPackage("gh:zyantific/zydis#master")
+#CPMAddPackage("gh:zyantific/zydis#master")
+CPMAddPackage("gh:zyantific/zydis#55dd08c210722aed81b38132f5fd4a04ec1943b5")

 CPMAddPackage(
         NAME LIEF_SRC
diff --git a/src/lax_encode.cc b/src/lax_encode.cc
index 0967a6c..64b194c 100644
--- a/src/lax_encode.cc
+++ b/src/lax_encode.cc
@@ -28,7 +28,10 @@ patch_linux (LIEF::ELF::Binary *bin)
 {
     using namespace LIEF::ELF;

-    PPK_ASSERT_ERROR(bin->imported_libraries().at(0) == "libnvcuvid.so.1");
+//    In WSL2 we can't use this to check the library binary.
+//    The "libnvcuvid.so.1" is no listed with the 'ldd' command,
+//    but it works 'strings /usr/lib/wsl/lib/libnvidia-encode.so.1 | grep libnvcuvid.so.1'
+//    PPK_ASSERT_ERROR(bin->imported_libraries().at(0) == "libnvcuvid.so.1");

     std::cout << "[+] libnvidia-encode.so\n";

@@ -67,6 +70,7 @@ patch_linux (LIEF::ELF::Binary *bin)
     }

     PPK_ASSERT_ERROR(found);
+    std::cout << "[+] Section 1 OK\n";
     found = false;

     {
@@ -83,7 +87,7 @@ patch_linux (LIEF::ELF::Binary *bin)
             if (instr.mnemonic == ZYDIS_MNEMONIC_TEST &&
                 instr.operands[0].reg.value == ZYDIS_REGISTER_EAX &&
                 instr.operands[1].reg.value == ZYDIS_REGISTER_EAX &&
-                (++n) > 1)
+                (++n) > 0)
             {
                 offset += (data - v_func_bytes.data());
                 found = true;
@@ -96,6 +100,7 @@ patch_linux (LIEF::ELF::Binary *bin)
     }

     PPK_ASSERT_ERROR(found);
+    std::cout << "[+] Section 2 OK\n";

     // test eax, eax -> xor eax, eax
     bin->patch_address(offset, 0x31, 0x1);

Feel free to add it to the repository! You only need to add some parameter to enable for WSL.
Regards.

@lars18th
Copy link

Hi,

My updated patch to apply the last fixes from @tytan652:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5fa8620..a013971 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,11 +16,13 @@ project(nvlax CXX)
 include(ExternalProject)
 include(cmake/FetchCPM.cmake)

-CPMAddPackage("gh:zyantific/zydis#master")
+#CPMAddPackage("gh:zyantific/zydis#master")
+CPMAddPackage("gh:zyantific/zydis#55dd08c210722aed81b38132f5fd4a04ec1943b5")

 CPMAddPackage(
         NAME LIEF_SRC
-        GIT_TAG master
+#       GIT_TAG master
+        GIT_TAG b65e7cca03ec4cd91f1d7125e717d01635ea81ba
         GITHUB_REPOSITORY lief-project/LIEF
         DOWNLOAD_ONLY YES
 )
@@ -59,7 +61,8 @@ message(STATUS "LIEF library: ${LIEF_LIBRARIES}")

 CPMAddPackage(
         NAME PPK_ASSERT
-        GIT_TAG master
+#       GIT_TAG master
+        GIT_TAG 833b8b7ea49aea540a49f07ad08bf0bae1faac32
         GITHUB_REPOSITORY gpakosz/PPK_ASSERT
         DOWNLOAD_ONLY YES
 )
diff --git a/src/lax_encode.cc b/src/lax_encode.cc
index 0967a6c..64b194c 100644
--- a/src/lax_encode.cc
+++ b/src/lax_encode.cc
@@ -28,7 +28,10 @@ patch_linux (LIEF::ELF::Binary *bin)
 {
     using namespace LIEF::ELF;

-    PPK_ASSERT_ERROR(bin->imported_libraries().at(0) == "libnvcuvid.so.1");
+//    In WSL2 we can't use this to check the library binary.
+//    The "libnvcuvid.so.1" is no listed with the 'ldd' command,
+//    but it works 'strings /usr/lib/wsl/lib/libnvidia-encode.so.1 | grep libnvcuvid.so.1'
+//    PPK_ASSERT_ERROR(bin->imported_libraries().at(0) == "libnvcuvid.so.1");

     std::cout << "[+] libnvidia-encode.so\n";

@@ -67,6 +70,7 @@ patch_linux (LIEF::ELF::Binary *bin)
     }

     PPK_ASSERT_ERROR(found);
+    std::cout << "[+] Section 1 OK\n";
     found = false;

     {
@@ -83,7 +87,7 @@ patch_linux (LIEF::ELF::Binary *bin)
             if (instr.mnemonic == ZYDIS_MNEMONIC_TEST &&
                 instr.operands[0].reg.value == ZYDIS_REGISTER_EAX &&

And I confirm that with a recent (Jun'23) driver like 515.65.01 it works in WSL2 with Win10 22H2 (without patching at all the stock Windows driver).

@ImVexed
Copy link

ImVexed commented Mar 19, 2023

@lars18th Did you cut off the last few lines of that git patch? It's complaining about being corrupt.

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

No branches or pull requests

3 participants