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

[netcore beta] Trying to export methods -> call them from Java - am I doing something wrong? #125

Closed
DevSnobo opened this issue Nov 22, 2019 · 8 comments

Comments

@DevSnobo
Copy link

Hello there,
I'm still hoping that I'm just doing something wrong when configuring the solution, but I'm not making any progress.

I have a student project where I shall implement some kind of plugin system to integrate .NET into a monolithic Java product.
After implementing a prototype with RGiesecke's nuget package, it somehow broke (not finding il(d)asm.exe anymore) and I'm now trying to implement the same thing with your extended solution - and targeting netcore instead of framework.

Problem:

When trying to execute a C# method from Java, there's an Invalid memory access error.

Exception in thread "main" java.lang.Error: Invalid memory access
	at com.sun.jna.Native.invokeInt(Native Method)
	at com.sun.jna.Function.invoke(Function.java:426)
	at com.sun.jna.Function.invoke(Function.java:361)
	at com.sun.jna.Library$Handler.invoke(Library.java:265)
	at com.sun.proxy.$Proxy0.Test(Unknown Source)
	at bla.bli.blub.dlltest.DllTest.main(DllTest.java:14)

When my configuration still worked with RGiesecke's package, the setup below worked (though, target was framework). Now when trying to do this with your extension for netcore projects, there seems to be a problem.
I tried to provide all the info I have to reproduce this issue below and also attached the projects here:
Lib.zip
JavaExample.zip

If you need more information, please let me know.

Now to my code:


C# .NET Core Library project ("Lib")

namespace Lib
{
    public class LibClass
    {
        [DllExport]
        public static int Test(int input)
        {
            return 40 + input;
        }
    }
}

Java Maven project

import com.sun.jna.Library;
import com.sun.jna.Native;

public class DllTest {
    public static void main(String[] args) {

        ILib libClass = Native.load(
"path\\to\\project\\Testing\\Lib\\bin\\x64\\Debug\\netcoreapp3.0\\Lib",
    	    	ILib.class);

    	int result = libClass.Test(2);
    	System.out.println(result);
    }

    public interface ILib extends Library {
    	public int Test(int input);
    }
}

with the Maven dependency

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.5.0</version>
</dependency>

Steps to configure the solution:

- add nuget package (1.7-beta) to C# project
- place DllExport.bat into solution folder
- execute command "DllExport.bat -action Configure"
- check the project "Lib\Lib.csproj"
- select "x64" radio button
- check "PE Check IL code"
- check "Use our IL Assembler (...)"
The other values were left on their defaults.
@3F
Copy link
Owner

3F commented Nov 22, 2019

Thanks for the report and especially for the demo projects!

@DevSnobo, I already voiced about Invalid memory access, for example, here: #26 (comment)

Thus please check this out!

Also I remember some problems exactly with JNA implementation, but I don't remember where I wrote about this. Hmm, here's other related issue anyway: #30

I'll try to inspect your files later. But before, can you also try something:

  • Try to check it with C/C++ project just to isolate JVM/JNA/... problems.
  • Try with .NET Framework target platform to specify .NET Core problems in 1.7-beta release.
  • Try with unchecked option "Use our IL Assembler (...)" to exclude problems with my custom assembler on coreclr
  • And the same items but for x86 architecture.

Thanks!

@DevSnobo
Copy link
Author

Thanks for your reply!
I will try to work my way through your list today.

For a start, changing the platform to x86 did result in an

UnsatisfiedLinkError: (...) "%1 is not a valid Win32 application."

Next I tried unchecking the option "Use our IL Assembler", but the result after changing back to x64 and rebuilding resulted in ildasm.exe not being found.
(trying the same with x86 resulted in the UnsatisfiedLinkError again)

Now I got to test this with a simple .NET Framework project:

  • exact same method and naming
  • x64 platform
  • configured with DllExport, the options x64 and "Use our IL Assembler" checked

Result: it works, the method returns 42

It seems to me there is a problem in the 1.7-beta release. Would you agree to this?

@3F
Copy link
Owner

3F commented Nov 25, 2019 via email

@DevSnobo
Copy link
Author

Thank you for taking the time to reply!

I have Visual Studio Professional 2019 (VS 16) installed.

Command output here:

Visual Studio Locator version 2.7.1+180c706d56 [query version 2.3.2200.14893]
Copyright (C) Microsoft Corporation. All rights reserved.

instanceId: cb55b3bc
installDate: 11/11/2019 11:43:37
installationName: VisualStudio/16.3.9+29509.3
installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional
installationVersion: 16.3.29509.3
productId: Microsoft.VisualStudio.Product.Professional
productPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe
state: 4294967295
isComplete: 1
isLaunchable: 1
isPrerelease: 0
isRebootRequired: 0
displayName: Visual Studio Professional 2019
description: Professional IDE best suited to small teams
channelId: VisualStudio.16.Release
channelUri: https://aka.ms/vs/16/release/channel
enginePath: C:\program files (x86)\microsoft visual studio\installer\resources\app\ServiceHub\Services\Microsoft.VisualStudio.Setup.Service
releaseNotes: https://go.microsoft.com/fwlink/?LinkId=660893#16.3.9
thirdPartyNotices: https://go.microsoft.com/fwlink/?LinkId=660909
updateDate: 2019-11-20T10:15:56.9560767Z
catalog_buildBranch: d16.3
catalog_buildVersion: 16.3.29509.3
catalog_id: VisualStudio/16.3.9+29509.3
catalog_localBuild: build-lab
catalog_manifestName: VisualStudio
catalog_manifestType: installer
catalog_productDisplayVersion: 16.3.9
catalog_productLine: Dev16
catalog_productLineVersion: 2019
catalog_productMilestone: RTW
catalog_productMilestoneIsPreRelease: False
catalog_productName: Visual Studio
catalog_productPatchVersion: 9
catalog_productPreReleaseMilestoneSuffix: 1.0
catalog_productSemanticVersion: 16.3.9+29509.3
catalog_requiredEngineVersion: 2.3.2283.19382
properties_campaignId: 318456033.1561541929
properties_channelManifestId: VisualStudio.16.Release/16.3.9+29509.3
properties_nickname: 2
properties_setupEngineFilePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe

Currently, I can't check with a C/C++ project since I'm writing this at work. Maybe in the next few days, but you'd probably be faster. I have very little experience with C/C++ and even less with calling managed code.

I appreciate your help a lot

@3F
Copy link
Owner

3F commented Nov 25, 2019 via email

@3F
Copy link
Owner

3F commented Dec 3, 2019

Well, the problem due to incorrect processing in my coreclr IL Assembler and more like others official MS versions that implements resolving of System.Private.CoreLib on the assembly stage for the PE module.

This is what I'm talking about, in #90 I already voiced about various base for system Object and this is it:

PE_dotnet_AssemblyRef_0x000A_coreclr_ilasm

As we can see, the correct number is 0x0006 (I mean, of course, only current assembly above) because AssemblyRef encoded into the low 2 bits of the number due to ECMA-335:

If e is a coded index that points into table ti out of n possible tables t0, ...tn-1, 
then it is stored as e << (log n) | tag{ t0, ...tn-1}[ti] using 2 bytes if the maximum 
number of rows of tables t0, ...tn-1, is less than 2^(16 – (log n)), 
and using 4 bytes otherwise.

In general, I confirm the bug.

We need to fix related processing in IL Assembler for our DllExport. I'll try to fix this week, maybe next. More probably I already know where to look this part of logic. Follow the news.

@3F 3F added this to the 1.7 milestone Dec 3, 2019
3F added a commit to 3F/coreclr that referenced this issue Dec 8, 2019
* NEW: Implemented new `/REBASE` key feature for IL Assembler. Issue #7.
       It will try to rebase system object: `netstandard` } `System.Runtime` } `mscorlib`

       Full explanation in related issue:
       3F/DllExport#125 (comment)

* CHANGED: Removed any use of the cvtres via CLR system directory.

* CHANGED: Updated coreclr 3.1.0 RTM
@3F
Copy link
Owner

3F commented Dec 8, 2019

New version of my IL Assembler is just released. Now we can process this case via optional rebasing of the System Object in assembly!

I've already checked this case manually via 4.700.2 and all works like a charm. Soon I will implement the use of this new feature in our DllExport.

gurasaa pushed a commit to Jie2GG/Native.Framework that referenced this issue Dec 11, 2019
当前版本因DllExport问题暂无法使用,预计星期内DllExport释出修正版本;

详情:
3F/DllExport#125
3F added a commit that referenced this issue Dec 13, 2019
* FIXED: Fixed possible "Invalid memory access" (JNA/Java). Issue #125.
         For C/C++ such as "Unhandled exception at ... (KernelBase)"

         https://twitter.com/GitHub3F/status/1201904821093187585

* FIXED: Fixed 'Failed to resolve assembly...' when .NET Standard. Issue #127.

* FIXED: Fixed error when switching between .sln in UI. Wizard.

* FIXED: Fixed possible duplication of `PlatformTarget` during setup.

* NEW: Implemented "Rebase System Object" option for our assembler.

* CHANGED: New installation behavior through official NuGet.

* CHANGED: New wizard layout for 1.7.

* CHANGED: Updated CoreCLR 3.1.0 RTM \ IL Assembler 4.700.2.
           https://github.com/3F/coreclr/releases/tag/ILAsm.4.700.2

           Includes an implemented `/REBASE` feature for related issue #125.

* CHANGED: Updated Mono Cecil 0.11.1
           https://github.com/jbevain/cecil/releases/tag/0.11.1

* CHANGED: Updated Conari 1.4
           https://github.com/3F/Conari/releases/tag/1.4

* KNOWN: Possible syntax error at token 'inf'. Issue #128.
         #128

* KNOWN: You can finally get another System.Object in modified assembly:
         #90 (comment)

* NOTE: Please don't use beta releases in production!

* NOTE: Please report about something here: https://github.com/3F/DllExport/issues
@3F
Copy link
Owner

3F commented Dec 15, 2019

Please test applied solution through latest beta releases.

@3F 3F closed this as completed Dec 15, 2019
@3F 3F added the netcore label Dec 16, 2019
3F added a commit that referenced this issue Jan 31, 2020
* NEW: .NET Core based projects support.
       Issues: #90, #67
       PR: #123

* NEW: .NET Standard 1.1 and above targeting support.

* NEW: Implemented "Single + Double Inf/-Inf token patching" option.
       Related Issue #128.

* NEW: Implemented "Rebase System Object" option for our assembler.

        Helps to avoid possible "Invalid memory access" (JNA/Java). Issue #125.
        For C/C++ such as "Unhandled exception at ... (KernelBase)"

        https://twitter.com/GitHub3F/status/1201904821093187585
        Part of PR #123

* NEW: Implemented updater for wizard. Issue #109.

* NEW: Textual export of affected data to diag.

* CHANGED: Updated CoreCLR 3.1.0 LTS \ IL Assembler 4.700.2.
           https://github.com/3F/coreclr/releases/tag/ILAsm.4.700.2

           Includes an implemented `/REBASE` feature for related issue #125.

* CHANGED: New installation behavior through official NuGet.
           init.ps1 also adds optional copying of package data for our manager.

           Please note: This is not nuget support!
                        It just replaces known info-form that was in 1.6.x.

* CHANGED: New wizard layout for 1.7.

* CHANGED: Local scope when invoking .\DllExport manager in project files.

* CHANGED: Updated logic for an offline versions
            with optional converting to online if needed.

* CHANGED: Updated MvsSln 2.5.1
           https://github.com/3F/MvsSln/releases/tag/2.5.1

* CHANGED: Updated hMSBuild 2.3
           https://github.com/3F/hMSBuild/releases/tag/v2.3

* KNOWN: You can finally get another System.Object in modified assembly:
         #90 (comment)

* NOTE: You need an updated/modern manager to avoid problem
        when "Possible incorrect Sdk-based project types": PR #123
        3F/MvsSln#23

        Use it from official 1.7 release:
        https://github.com/3F/DllExport/releases/tag/v1.7
        https://www.nuget.org/packages/DllExport/1.7.0

        Otherwise you need provide manually msbuild instance
        that can process modern Sdk-types due to modern VS/dotnet sdk
        dependencies for netcore-based projects.

        For example, through hMSBuild https://github.com/3F/hMSBuild
        ```
        DllExport ... -msb hMSBuild
        ```

* NOTE: For command-line mode, To upgrade configured version to 1.7:
        ```
            DllExport -action Upgrade -dxp-version 1.7.0
        ```
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