runtime: Better diagnostic for native .dll loading errors (pass through dlerror)

Hi I am trying to write a .NET Core wrapper for the LLVM C Frontend. However I am having trouble with the runtime finding the dylib.

Failed   LLVMWrap.Tests.BasicTests.TestBasicSumMethod
Error Message:
 System.DllNotFoundException : Unable to load DLL 'libLLVM.dylib': The specified module could not be found.
 (Exception from HRESULT: 0x8007007E)
Stack Trace:
   at LLVMWrap.LLVMAPI.LLVMInt32Type()
   at LLVMWrap.Core.Types.LLVMInt32Type..ctor() in /Users/alexander/github/LLVMWrap/LLVMWrapCore/Core/Types/LLVMInt32Type.cs:line 11
   at LLVMWrap.Core.Types.LLVMInt32Type.<>c.<.cctor>b__4_0() in /Users/alexander/github/LLVMWrap/LLVMWrapCore/Core/Types/LLVMInt32Type.cs:line 7
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at LLVMWrap.Core.Types.LLVMInt32Type.get_Instance() in /Users/alexander/github/LLVMWrap/LLVMWrapCore/Core/Types/LLVMInt32Type.cs:line 14
   at LLVMWrap.Tests.BasicTests.TestBasicSumMethod() in /Users/alexander/github/LLVMWrap/LLVMWrap.Tests/BasicTests.cs:line 15

I’ve tried the project paths, the bin/debug/target/ path and nothing. Any clues about what I might be missing? or is this a bug?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 29 (25 by maintainers)

Most upvoted comments

We encountered

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'MonoPosixHelper': The specified module or one of its dependencies could not be found.
(Exception from HRESULT: 0x8007007E)
   at Mono.Unix.Native.Syscall.lstat(String file_name, Stat& buf)
   at Mono.Unix.UnixFileSystemInfo.TryGetFileSystemEntry(String path, UnixFileSystemInfo& entry)
   at Mono.Unix.UnixFileSystemInfo.GetFileSystemEntry(String path)
   at TestMonoPosix.Program.Main(String[] args) in d:\Projects\TestMonoPosix\Program.cs:line 13
Aborted (core dumped)

When running a program on a newly mounted disk. The application worked fine on the main disk, but it would fail as above when the application was copied to the mounted disk.

Using strace, gave insight into why the assembly couldn’t be loaded:

open("/datadisks/disk1/TestMonoPosix/runtimes/linux-x64/native/libMonoPosixHelper.so", O_RDONLY|O_CLOEXEC) = 24
read(24, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\271\0\0\0\0\0\0"..., 832) = 832
fstat(24, {st_mode=S_IFREG|0744, st_size=900791, ...}) = 0
mmap(NULL, 2362920, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 24, 0) = -1 EPERM (Operation not permitted)
close(24)  

From reading the ‘man mmap’ pages:

EPERM The prot argument asks for PROT_EXEC but the mapped area belongs to a file on a filesystem that was mounted no-exec.

Which was the problem - the disk was mounted no-exec. Remounting it with the exec option allowed the application to start running again.

Better diagnostics info in the exception would have helped speed up this investigation and narrow the problem sooner.

I think I have suggested a long time ago to change the error message to “The specified module or one of its dependencies could not be found.” That way it would hint the user to verify that the reported .dylib / .so has all the dependencies it needs. On Linux, “ldd -r” is the best way. On OSX, the “otool -L” command should work. @alexanderuv would it have helped you to pinpoint the issue if the message was modified like I’ve said?