java-native-access / java-native-access/jna

Windows: JNA fails to load libraries if SetDefaultDllDirectories() was used

Open
#1,413 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
8.9k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

  1. Version of JNA and related jars - any since 93ebb9f7
  2. Version and vendor of the java virtual machine - any
  3. Operating system - any Windows
  4. System architecture (CPU type, bitness of the JVM) - any

Example snippet:

public interface Dwmapi extends StdCallLibrary {
}

public interface Kernel32_2 extends StdCallLibrary {
	Pointer AddDllDirectory(WString NewDirectory);
	boolean SetDefaultDllDirectories(int DirectoryFlags);
}

public static void main(String[] args) {
	final Kernel32_2 kernel32 = Native.loadLibrary("kernel32", Kernel32_2.class);
	final int LOAD_LIBRARY_DEFAULT_DIRS = 0x00001000;

	// SetDefaultDllDirectories() is used to supply another DLL search path.
	// But it causes JNA to fail later.
	Path pluginsPath = Paths.get("plugins").toAbsolutePath();
	kernel32.SetDefaultDllDirectories(LOAD_LIBRARY_DEFAULT_DIRS);
	kernel32.AddDllDirectory(new WString(pluginsPath.toString()));

	// This is perfectly fine, but fails after SetDefaultDllDirectories().
	Native.load("dwmapi", Dwmapi.class);
}

Explanation:

  1. Since 93ebb9f7, JNA uses LOAD_WITH_ALTERED_SEARCH_PATH in LoadLibraryExW().
  2. MSDN says: If this value is used and lpFileName specifies a relative path, the behavior is undefined
  3. When library name is given like "dwmapi", this is a relative path.
  4. Windows logs this error, but still tries to proceed - set WinDBG breakpoint to see for yourself: bp ntdll!LdrpLogRelativePathWithAlteredSearchError "du @rcx" (here, even the function's name is already a proof)
  5. However, if SetDefaultDllDirectories() was also used, ntdll!LdrpGetDllPath considers this a fatal error and returns STATUS_INVALID_PARAMETER

TLDR: JNA uses LOAD_WITH_ALTERED_SEARCH_PATH and this is a bug to use it with non-absolute path. However, in most cases it still works, because Windows is kind to interlopers.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing JNA's native library-loading path from Native.load to its LoadLibraryExW call, then reproduce the provided example after SetDefaultDllDirectories() is used. Compare the relative-library-name behavior with Windows' documented search-path rules; done means the example loads successfully and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.