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

Implement dynamic DLL resolution of NativeShims for .NET 4.7 x86/x64 architectures #154

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

DennisDyallo
Copy link
Collaborator

@DennisDyallo DennisDyallo commented Oct 21, 2024

Description

Quote: @romerod

Current Behavior
When the library is used in a project compiled with AnyCPU which is compiled on a x64 bit system the applications fails with a BadImageFormatException on 32bit PC as the Yubico.NativeShims.dll in the build output folder is the x64 version. The same thing happens when setting the Prefer32Bit flag.

The Yubico.NativeShims.targets file in the Yubico.NativeShims nuget package copies the file over depending of the architecture of the build system.

Expected Behavior
When a project is build with AnyCPU the build output should contain the x86 and the x64 bit of the dll, and the dll to load should be detected on runtime.

Fixes include:

  • Implementing dynamic DLL resolution based on process architecture (x86/x64)

Fixes: #151

Type of change

  • Refactor (non-breaking change which improves code quality or performance)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

I built a NET47 32 bit application according to the #151 issue and reproduced the bug. Loaded the SDK dlls into that application and proceeded testing and fixing.

Test configuration:

  • OS version: Windows 11
  • Firmware version: 5.4.3
  • Yubikey model1: 5C

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have run dotnet format to format my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Footnotes

  1. See Yubikey models (Multi-protocol, Security Key, FIPS, Bio, YubiHSM, YubiHSM FIPS)

Copy link

github-actions bot commented Oct 21, 2024

Test Results: Windows

    2 files      2 suites   4s ⏱️
3 703 tests 3 703 ✅ 0 💤 0 ❌
3 705 runs  3 705 ✅ 0 💤 0 ❌

Results for commit 211a94c.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Oct 21, 2024

Test Results: Ubuntu

    2 files      2 suites   9s ⏱️
3 695 tests 3 695 ✅ 0 💤 0 ❌
3 697 runs  3 697 ✅ 0 💤 0 ❌

Results for commit 211a94c.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Oct 21, 2024

Test Results: MacOS

    2 files      2 suites   4s ⏱️
3 695 tests 3 695 ✅ 0 💤 0 ❌
3 697 runs  3 697 ✅ 0 💤 0 ❌

Results for commit 211a94c.

♻️ This comment has been updated with latest results.

@DennisDyallo DennisDyallo self-assigned this Oct 22, 2024
@DennisDyallo DennisDyallo added the bug Something isn't working label Oct 22, 2024
@DennisDyallo DennisDyallo changed the title Fixes #151 Fix AnyCPU NativeShims library resolve on NET47 32bit systems Oct 22, 2024
@DennisDyallo DennisDyallo changed the title Fix AnyCPU NativeShims library resolve on NET47 32bit systems Implement dynamic DLL resolution of NativeShiims for .NET 4.7 x86/x64 architectures Oct 22, 2024
@DennisDyallo DennisDyallo changed the title Implement dynamic DLL resolution of NativeShiims for .NET 4.7 x86/x64 architectures Implement dynamic DLL resolution of NativeShims for .NET 4.7 x86/x64 architectures Oct 22, 2024
@DennisDyallo DennisDyallo force-pushed the dennisdyallo/bugfix/dllfix-151 branch from dcbd26c to d6f2685 Compare October 22, 2024 15:09
DennisDyallo

This comment was marked as off-topic.

@DennisDyallo DennisDyallo marked this pull request as ready for review October 22, 2024 15:10
@GregDomzalski
Copy link
Contributor

Just two final thoughts - otherwise looks good to me.

Copy link

Code Coverage

Package Line Rate Branch Rate Complexity Health
Yubico.Core 43% 32% 4221
Yubico.YubiKey 50% 47% 19158
Summary 49% (32194 / 66075) 44% (8197 / 18585) 23379

Minimum allowed line rate is 40%

@GregDomzalski
Copy link
Contributor

OK, ok - two last foods for thought. Take it if you like the idea.

Instead of #if !NET47 around the entire Libraries.cs file, let's scope it down to just the internal const string definition.

We can get rid of the EnsureInitialized method in the non NET47 file altogether, if we put an #if NET47 around the static NativeMethods() method decl in NativeMethods.cs.

I dunno. You're call. I think it's also fine as is. 😄

Consider this my sign-off.

@DennisDyallo DennisDyallo merged commit ab338de into develop Oct 24, 2024
12 checks passed
@DennisDyallo DennisDyallo deleted the dennisdyallo/bugfix/dllfix-151 branch October 24, 2024 09:58
@DennisDyallo DennisDyallo linked an issue Nov 8, 2024 that may be closed by this pull request
This was referenced Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
3 participants