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

Add a proxy for ber scanf/printf varargs native methods. #50540

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
$(LibrariesAllBinArtifactsPath)*.pdb"
IsNative="" />
<ExcludeNativeLibrariesRuntimeFiles Condition="'$(IncludeOOBLibraries)' != 'true'"
Include="$(LibrariesNativeArtifactsPath)libSystem.IO.Ports.Native.*" />
Include="$(LibrariesNativeArtifactsPath)libSystem.IO.Ports.Native.*;$(LibrariesNativeArtifactsPath)*System.DirectoryServices.Native.*" />
<LibrariesRuntimeFiles Include="
$(LibrariesNativeArtifactsPath)*.dll;
$(LibrariesNativeArtifactsPath)*.dylib;
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/scripts/superpmi_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"superpmi-shim-counter.dll",
"superpmi-shim-simple.dll",
"System.IO.Compression.Native.dll",
"System.DirectoryServices.Native.dll",
"ucrtbase.dll",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal static partial class Libraries
internal const string Odbc32 = "libodbc.so.2";
internal const string OpenLdap = "libldap-2.4.so.2";
internal const string MsQuic = "libmsquic.so";
internal const string DirectoryServicesNative = "libSystem.DirectoryServices.Native";
}
}
32 changes: 16 additions & 16 deletions src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ internal static partial class Ldap
[DllImport(Libraries.OpenLdap, EntryPoint = "ber_free", CharSet = CharSet.Ansi)]
public static extern IntPtr ber_free([In] IntPtr berelement, int option);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_printf", CharSet = CharSet.Ansi)]
public static extern int ber_printf_emptyarg(SafeBerHandle berElement, string format);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_printf_proxy_emptyarg", CharSet = CharSet.Ansi)]
public static extern int ber_printf_proxy_emptyarg(SafeBerHandle berElement, string format);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_printf", CharSet = CharSet.Ansi)]
public static extern int ber_printf_int(SafeBerHandle berElement, string format, int value);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_printf_proxy_int", CharSet = CharSet.Ansi)]
public static extern int ber_printf_proxy_int(SafeBerHandle berElement, string format, int value);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_printf", CharSet = CharSet.Ansi)]
public static extern int ber_printf_bytearray(SafeBerHandle berElement, string format, HGlobalMemHandle value, int length);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_printf_proxy_bytearray", CharSet = CharSet.Ansi)]
public static extern int ber_printf_proxy_bytearray(SafeBerHandle berElement, string format, HGlobalMemHandle value, int length);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_printf", CharSet = CharSet.Ansi)]
public static extern int ber_printf_berarray(SafeBerHandle berElement, string format, IntPtr value);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_printf_proxy_berarray", CharSet = CharSet.Ansi)]
public static extern int ber_printf_proxy_berarray(SafeBerHandle berElement, string format, IntPtr value);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_flatten", CharSet = CharSet.Ansi)]
public static extern int ber_flatten(SafeBerHandle berElement, ref IntPtr value);
Expand All @@ -39,16 +39,16 @@ internal static partial class Ldap
[DllImport(Libraries.OpenLdap, EntryPoint = "ber_bvecfree", CharSet = CharSet.Ansi)]
public static extern int ber_bvecfree(IntPtr value);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_scanf", CharSet = CharSet.Ansi)]
public static extern int ber_scanf(SafeBerHandle berElement, string format);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_scanf_proxy", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_proxy(SafeBerHandle berElement, string format);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_scanf", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_int(SafeBerHandle berElement, string format, ref int value);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_scanf_proxy_int", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_proxy_int(SafeBerHandle berElement, string format, ref int value);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_scanf", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_bitstring(SafeBerHandle berElement, string format, ref IntPtr value, ref int bitLength);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_scanf_proxy_bitstring", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_proxy_bitstring(SafeBerHandle berElement, string format, ref IntPtr value, ref int bitLength);

[DllImport(Libraries.OpenLdap, EntryPoint = "ber_scanf", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_ptr(SafeBerHandle berElement, string format, ref IntPtr value);
[DllImport(Libraries.DirectoryServicesNative, EntryPoint = "ber_scanf_proxy_ptr", CharSet = CharSet.Ansi)]
public static extern int ber_scanf_proxy_ptr(SafeBerHandle berElement, string format, ref IntPtr value);
}
}
1 change: 1 addition & 0 deletions src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ internal static partial class Libraries
internal const string SystemConfigurationLibrary = "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration";
internal const string AppleCryptoNative = "libSystem.Security.Cryptography.Native.Apple";
internal const string MsQuic = "libmsquic.dylib";
internal const string DirectoryServicesNative = "libSystem.DirectoryServices.Native";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ internal static partial class Libraries
internal const string GlobalizationNative = "System.Globalization.Native";
internal const string MsQuic = "msquic.dll";
internal const string HostPolicy = "hostpolicy.dll";
internal const string DirectoryServicesNative = "System.DirectoryServices.Native";
}
}
32 changes: 16 additions & 16 deletions src/libraries/Common/src/Interop/Windows/Wldap32/Interop.Ber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ internal static partial class Ldap
[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_alloc_t", CharSet = CharSet.Unicode)]
public static extern IntPtr ber_alloc(int option);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf", CharSet = CharSet.Unicode)]
public static extern int ber_printf_emptyarg(SafeBerHandle berElement, string format);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf_proxy_emptyarg", CharSet = CharSet.Unicode)]
public static extern int ber_printf_proxy_emptyarg(SafeBerHandle berElement, string format);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf", CharSet = CharSet.Unicode)]
public static extern int ber_printf_int(SafeBerHandle berElement, string format, int value);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf_proxy_int", CharSet = CharSet.Unicode)]
public static extern int ber_printf_proxy_int(SafeBerHandle berElement, string format, int value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf", CharSet = CharSet.Unicode)]
public static extern int ber_printf_bytearray(SafeBerHandle berElement, string format, HGlobalMemHandle value, int length);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf_proxy_bytearray", CharSet = CharSet.Unicode)]
public static extern int ber_printf_proxy_bytearray(SafeBerHandle berElement, string format, HGlobalMemHandle value, int length);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf", CharSet = CharSet.Unicode)]
public static extern int ber_printf_berarray(SafeBerHandle berElement, string format, IntPtr value);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_printf_proxy_berarray", CharSet = CharSet.Unicode)]
public static extern int ber_printf_proxy_berarray(SafeBerHandle berElement, string format, IntPtr value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_flatten", CharSet = CharSet.Unicode)]
public static extern int ber_flatten(SafeBerHandle berElement, ref IntPtr value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_init", CharSet = CharSet.Unicode)]
public static extern IntPtr ber_init(berval value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf", CharSet = CharSet.Unicode)]
public static extern int ber_scanf(SafeBerHandle berElement, string format);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf_proxy", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_proxy(SafeBerHandle berElement, string format);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_int(SafeBerHandle berElement, string format, ref int value);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf_proxy_int", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_proxy_int(SafeBerHandle berElement, string format, ref int value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_ptr(SafeBerHandle berElement, string format, ref IntPtr value);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf_proxy_ptr", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_proxy_ptr(SafeBerHandle berElement, string format, ref IntPtr value);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_bitstring(SafeBerHandle berElement, string format, ref IntPtr value, ref int bitLength);
[DllImport(Libraries.DirectoryServicesNative, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_scanf_proxy_bitstring", CharSet = CharSet.Unicode)]
public static extern int ber_scanf_proxy_bitstring(SafeBerHandle berElement, string format, ref IntPtr value, ref int bitLength);

[DllImport(Libraries.Wldap32, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ber_bvfree", CharSet = CharSet.Unicode)]
public static extern int ber_bvfree(IntPtr value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "LdapProxyForVarArgs.h"
#include <stdio.h>

#ifdef _WIN32
FUNCTIONEXPORT ULONG FUNCTIONCALLINGCONVENCTION ber_scanf(BerElement* ber, char* fmt, ...);
FUNCTIONEXPORT INT FUNCTIONCALLINGCONVENCTION ber_printf(BerElement* ber, char* fmt, ...);
#else
FUNCTIONEXPORT long FUNCTIONCALLINGCONVENCTION ber_scanf(BerElement* ber, const char* fmt, ...);
FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf(BerElement* ber, const char* fmt, ...);
#endif

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy(BerElement *ber, char *fmt)
{
return (int)ber_scanf(ber, fmt);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_int(BerElement *ber, char *fmt, int* value)
{
return (int)ber_scanf(ber, fmt, value);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_bitstring(BerElement *ber, char *fmt, int** value, int* bitLength)
{
return (int)ber_scanf(ber, fmt, value, bitLength);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_ptr(BerElement *ber, char *fmt, int** value)
{
return (int)ber_scanf(ber, fmt, value);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_emptyarg(BerElement *ber, char *fmt)
{
return ber_printf(ber, fmt);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_int(BerElement *ber, char *fmt, int value)
{
return ber_printf(ber, fmt, value);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_bytearray(BerElement *ber, char *fmt, int* value, int length)
{
return ber_printf(ber, fmt, value, length);
}

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_berarray(BerElement *ber, char *fmt, int* value)
{
return ber_printf(ber, fmt, value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma once

#ifdef _WIN32
#include <stdint.h>
#include <windows.h>
#define FUNCTIONEXPORT extern
#define FUNCTIONCALLINGCONVENCTION __cdecl
#else
#include "pal_types.h"
#include "pal_compiler.h"
#define FUNCTIONEXPORT PALEXPORT
#define FUNCTIONCALLINGCONVENCTION
#endif

typedef struct BerElementStruct BerElement;

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy(BerElement* ber, char *fmt);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_int(BerElement *ber, char *fmt, int* value);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_bitstring(BerElement *ber, char *fmt, int** value, int* bitLength);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_scanf_proxy_ptr(BerElement *ber, char *fmt, int** value);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_emptyarg(BerElement *ber, char *fmt);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_int(BerElement *ber, char *fmt, int value);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_bytearray(BerElement *ber, char *fmt, int* value, int length);

FUNCTIONEXPORT int FUNCTIONCALLINGCONVENCTION ber_printf_proxy_berarray(BerElement *ber, char *fmt, int* value);
1 change: 1 addition & 0 deletions src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ else()
add_subdirectory(System.Globalization.Native)
add_subdirectory(System.Net.Security.Native)
add_subdirectory(System.Security.Cryptography.Native)
add_subdirectory(System.DirectoryServices.Native)
endif()

if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
project(System.DirectoryServices.Native C)

set (NATIVEPROTOCOLS_SOURCES
../../AnyOS/System.DirectoryServices.Native/LdapProxyForVarArgs.c
)

if(CLR_CMAKE_TARGET_OSX)
# Workaround for "'ber_scanf' is deprecated: first deprecated in macOS 10.11 - use OpenDirectory Framework".
add_compile_options(-Wno-deprecated-declarations)
endif()

if (GEN_SHARED_LIB)

add_library(System.DirectoryServices.Native
SHARED
${NATIVEPROTOCOLS_SOURCES}
${VERSION_FILE_PATH}
)

find_library(LIBLDAP NAMES ldap libldap.dylib libldap-2.4.so.2)
if(LIBLDAP STREQUAL LIBLDAP-NOTFOUND)
message(FATAL_ERROR "Cannot find OpenLdap")
else()
message(STATUS "OpenLdap found as ${LIBLDAP}")
endif()

target_link_libraries(System.DirectoryServices.Native ${LIBLDAP})

install_with_stripped_symbols (System.DirectoryServices.Native PROGRAMS .)
endif ()

add_library(System.DirectoryServices.Native-Static
STATIC
${NATIVEPROTOCOLS_SOURCES}
)

install (TARGETS System.IO.Ports.Native-Static DESTINATION ${STATIC_LIB_DESTINATION} COMPONENT libs)
1 change: 1 addition & 0 deletions src/libraries/Native/Windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ if(STATIC_LIBS_ONLY)
endif()

add_subdirectory(System.IO.Compression.Native)
add_subdirectory(System.DirectoryServices.Native)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
project(System.DirectoryServices.Native)

if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "Binary directory isn't being correctly set before calling Cmake. Tree must be built in separate directory from source.")
endif()

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if (GEN_SHARED_LIB)
include (GenerateExportHeader)
endif()

set (NATIVEPROTOCOLS_SOURCES
../../AnyOS/System.DirectoryServices.Native/LdapProxyForVarArgs.c
)

if (GEN_SHARED_LIB)
add_library(System.DirectoryServices.Native
SHARED
${NATIVEPROTOCOLS_SOURCES}
System.DirectoryServices.Native.def
# This will add versioning to the library
${CMAKE_REPO_ROOT}/artifacts/obj/NativeVersion.rc
)

endif()

list(APPEND __LinkLibraries Wldap32.dll)

add_library(System.DirectoryServices.Native-Static
STATIC
${NATIVEPROTOCOLS_SOURCES}
)

# Allow specification of arguments that should be passed to the linker
if (GEN_SHARED_LIB)
SET_TARGET_PROPERTIES(System.DirectoryServices.Native PROPERTIES LINK_OPTIONS "${__LinkArgs};${__SharedLinkArgs}")
endif()
SET_TARGET_PROPERTIES(System.DirectoryServices.Native-Static PROPERTIES STATIC_LIBRARY_OPTIONS "${__LinkArgs}")

# Allow specification of libraries that should be linked against
if (GEN_SHARED_LIB)
target_link_libraries(System.DirectoryServices.Native ${__LinkLibraries})
endif()
target_link_libraries(System.DirectoryServices.Native-Static ${__LinkLibraries})

if (GEN_SHARED_LIB)
GENERATE_EXPORT_HEADER( System.DirectoryServices.Native
BASE_NAME System.DirectoryServices.Native
EXPORT_MACRO_NAME System.DirectoryServices.Native_EXPORT
EXPORT_FILE_NAME System.DirectoryServices.Native_Export.h
STATIC_DEFINE System.DirectoryServices.Native_BUILT_AS_STATIC
)

install (TARGETS System.DirectoryServices.Native DESTINATION .)
install (FILES $<TARGET_PDB_FILE:System.DirectoryServices.Native> DESTINATION .)
endif()

install (TARGETS System.DirectoryServices.Native-Static DESTINATION ${STATIC_LIB_DESTINATION})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LIBRARY System.DirectoryServices.Native.dll

EXPORTS
ber_scanf_proxy
ber_scanf_proxy_int
ber_scanf_proxy_bitstring
ber_scanf_proxy_ptr
ber_printf_proxy_emptyarg
ber_printf_proxy_int
ber_printf_proxy_bytearray
ber_printf_proxy_berarray
Loading