Skip to content

Commit

Permalink
Native Libraries: added API version to:
Browse files Browse the repository at this point in the history
- test whether native library matches the JAR (bad builds could e.g. ship a newer JAR with an older incompatible native library)
- invoke a method (to get API version) to check whether native library works correctly

if API version do not match, or method could not invoked correctly, disable usage of FlatLaf native library

Windows and macOS binaries built and signed locally in clean workspace
Linux binary built by GitHub Actions
  • Loading branch information
DevCharly committed Mar 24, 2024
1 parent 4e1f092 commit 32d102d
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ FlatLaf Change Log
- `FlatSVGIcon` color filters now support linear gradients. (PR #817)
- Added support for `JSplitPane.expandableSide` client property to
`FlatSplitPane`.
- Native libraries: Added API version check to test whether native library
matches the JAR (bad builds could e.g. ship a newer JAR with an older
incompatible native library) and to test whether native methods can be invoked
(some security software allows loading native library but blocks method
invocation).


## 3.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ class FlatNativeLibrary
private static boolean initialized;
private static NativeLibrary nativeLibrary;

private native static int getApiVersion();

/**
* Loads native library (if available) and returns whether loaded successfully.
* Returns {@code false} if no native library is available.
*/
static synchronized boolean isLoaded() {
initialize();
static synchronized boolean isLoaded( int apiVersion ) {
initialize( apiVersion );
return (nativeLibrary != null) ? nativeLibrary.isLoaded() : false;
}

private static void initialize() {
private static void initialize( int apiVersion ) {
if( initialized )
return;
initialized = true;
Expand Down Expand Up @@ -104,7 +106,26 @@ else if( SystemInfo.isX86_64 )
return; // no native library available for current OS or CPU architecture

// load native library
nativeLibrary = createNativeLibrary( classifier, ext );
NativeLibrary nativeLibrary = createNativeLibrary( classifier, ext );
if( !nativeLibrary.isLoaded() )
return;

// check API version (and check whether library works)
try {
int actualApiVersion = getApiVersion();
if( actualApiVersion != apiVersion ) {
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Wrong API version in native library (expected "
+ apiVersion + ", actual " + actualApiVersion + "). Ignoring native library.", null );
return;
}
} catch( Throwable ex ) {
// could be a UnsatisfiedLinkError in case that loading native library
// from temp directory was blocked by some OS security mechanism
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to get API version of native library. Ignoring native library.", ex );
return;
}

FlatNativeLibrary.nativeLibrary = nativeLibrary;
}

private static NativeLibrary createNativeLibrary( String classifier, String ext ) {
Expand All @@ -118,9 +139,9 @@ private static NativeLibrary createNativeLibrary( String classifier, String ext
if( library.isLoaded() )
return library;

LoggingFacade.INSTANCE.logSevere( "Did not find library '" + System.mapLibraryName( libraryName )
+ "' in java.library.path '" + System.getProperty( "java.library.path" )
+ "', using extracted library instead", null );
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '" + System.mapLibraryName( libraryName )
+ "' not found in java.library.path '" + System.getProperty( "java.library.path" )
+ "'. Using extracted native library instead.", null );
} else {
// try standard library naming scheme
// (same as in flatlaf.jar in package 'com/formdev/flatlaf/natives')
Expand All @@ -139,11 +160,11 @@ private static NativeLibrary createNativeLibrary( String classifier, String ext
return new NativeLibrary( libraryFile2, true );
}

LoggingFacade.INSTANCE.logSevere( "Did not find library '"
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '"
+ libraryFile.getName()
+ (libraryName2 != null ? ("' or '" + libraryName2) : "")
+ "' in '" + libraryFile.getParentFile().getAbsolutePath()
+ "', using extracted library instead", null );
+ "' not found in '" + libraryFile.getParentFile().getAbsolutePath()
+ "'. Using extracted native library instead.", null );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
*/
class FlatNativeLinuxLibrary
{
private static int API_VERSION_LINUX = 3001;

/**
* Checks whether native library is loaded/available.
* <p>
* <b>Note</b>: It is required to invoke this method before invoking any other
* method of this class. Otherwise, the native library may not be loaded.
*/
static boolean isLoaded() {
return SystemInfo.isLinux && FlatNativeLibrary.isLoaded();
return SystemInfo.isLinux && FlatNativeLibrary.isLoaded( API_VERSION_LINUX );
}

// direction for _NET_WM_MOVERESIZE message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@
*/
public class FlatNativeMacLibrary
{
private static int API_VERSION_MACOS = 2001;

/**
* Checks whether native library is loaded/available.
* <p>
* <b>Note</b>: It is required to invoke this method before invoking any other
* method of this class. Otherwise, the native library may not be loaded.
*/
public static boolean isLoaded() {
return SystemInfo.isMacOS && FlatNativeLibrary.isLoaded();
return SystemInfo.isMacOS && FlatNativeLibrary.isLoaded( API_VERSION_MACOS );
}

public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
public class FlatNativeWindowsLibrary
{
private static int API_VERSION_WINDOWS = 1001;

private static long osBuildNumber = Long.MIN_VALUE;

/**
Expand All @@ -39,7 +41,7 @@ public class FlatNativeWindowsLibrary
* method of this class. Otherwise, the native library may not be loaded.
*/
public static boolean isLoaded() {
return SystemInfo.isWindows && FlatNativeLibrary.isLoaded();
return SystemInfo.isWindows && FlatNativeLibrary.isLoaded( API_VERSION_WINDOWS );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static FlatNativeWindowBorder.Provider getInstance() {
return null;

// check whether native library was successfully loaded
if( !FlatNativeLibrary.isLoaded() )
if( !FlatNativeWindowsLibrary.isLoaded() )
return null;

// create new instance
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 4 additions & 1 deletion flatlaf-natives/flatlaf-natives-linux/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ plugins {
}

flatlafJniHeaders {
headers = listOf( "com_formdev_flatlaf_ui_FlatNativeLinuxLibrary.h" )
headers = listOf(
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
"com_formdev_flatlaf_ui_FlatNativeLinuxLibrary.h"
)
}

library {
Expand Down
37 changes: 37 additions & 0 deletions flatlaf-natives/flatlaf-natives-linux/src/main/cpp/ApiVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <jni.h>
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"

/**
* @author Karl Tauber
*/


// increase this version if changing API or functionality of native library
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeLinuxLibrary
#define API_VERSION_LINUX 3001


//---- JNI methods ------------------------------------------------------------

extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
( JNIEnv* env, jclass cls )
{
return API_VERSION_LINUX;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion flatlaf-natives/flatlaf-natives-macos/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ plugins {
}

flatlafJniHeaders {
headers = listOf( "com_formdev_flatlaf_ui_FlatNativeMacLibrary.h" )
headers = listOf(
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
"com_formdev_flatlaf_ui_FlatNativeMacLibrary.h"
)
}

library {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <jni.h>
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"

/**
* @author Karl Tauber
*/


// increase this version if changing API or functionality of native library
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeMacLibrary
#define API_VERSION_MACOS 2001


//---- JNI methods ------------------------------------------------------------

extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
( JNIEnv* env, jclass cls )
{
return API_VERSION_MACOS;
}
1 change: 1 addition & 0 deletions flatlaf-natives/flatlaf-natives-windows/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {

flatlafJniHeaders {
headers = listOf(
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
"com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h",
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder.h",
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <jni.h>
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"

/**
* @author Karl Tauber
*/


// increase this version if changing API or functionality of native library
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeWindowsLibrary
#define API_VERSION_WINDOWS 1001


//---- JNI methods ------------------------------------------------------------

extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
( JNIEnv* env, jclass cls )
{
return API_VERSION_WINDOWS;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32d102d

Please sign in to comment.