Skip to content

Commit

Permalink
Add a new libjnf, which is the JavaNativeFoundation code from Apple.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed May 8, 2024
1 parent 2556609 commit fac3cb9
Show file tree
Hide file tree
Showing 39 changed files with 3,832 additions and 76 deletions.
8 changes: 8 additions & 0 deletions IKVM.sln
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ Project("{6DE1C62B-E8D7-451A-8734-87EAEB46E35B}") = "libj2pkcs11", "src\libj2pkc
EndProject
Project("{6DE1C62B-E8D7-451A-8734-87EAEB46E35B}") = "libjaas", "src\libjaas\libjaas.clangproj", "{2D69F3E3-D750-479B-975A-D8DE7FA3F7EE}"
EndProject
Project("{6DE1C62B-E8D7-451A-8734-87EAEB46E35B}") = "libjnf", "src\libjnf\libjnf.clangproj", "{606E173D-798D-4463-8EFD-0AD750831DD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1097,6 +1099,12 @@ Global
{2D69F3E3-D750-479B-975A-D8DE7FA3F7EE}.NoVariablesDefault|Any CPU.Build.0 = Release|Any CPU
{2D69F3E3-D750-479B-975A-D8DE7FA3F7EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D69F3E3-D750-479B-975A-D8DE7FA3F7EE}.Release|Any CPU.Build.0 = Release|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.NoVariablesDefault|Any CPU.ActiveCfg = Release|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.NoVariablesDefault|Any CPU.Build.0 = Release|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{606E173D-798D-4463-8EFD-0AD750831DD3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions src/IKVM.Image-bin/IKVM.Image-bin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libmanagement\libmanagement.clangproj" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libj2pkcs11\libj2pkcs11.clangproj" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libjaas\libjaas.clangproj" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libjnf\libjnf.clangproj" SupportedRuntimes="osx-x64;osx-arm64" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libosxapp\libosxapp.clangproj" SupportedRuntimes="osx-x64;osx-arm64" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libosx\libosx.clangproj" SupportedRuntimes="osx-x64;osx-arm64" />
<DotNetClangProjectReference Include="$(MSBuildThisFileDirectory)..\libkrb5\libkrb5.clangproj" SupportedRuntimes="win-x64;win-x86;win-arm64;osx-x64;osx-arm64" />
Expand Down
104 changes: 104 additions & 0 deletions src/libjnf/JavaNativeFoundation/JNFAssert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2008-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* --
*
* Assertions used by the JNF_COCOA_ENTER()/JNF_COCOA_EXIT() and class
* caching macros. When building debug builds, improper use of the caching
* macros will trigger warnings output to the console.
*/

#import <JavaNativeFoundation/JNFJNI.h>

#ifdef DEBUG
#define JAVA_ASSERTIONS_ON
#endif /* DEBUG */

// Use the WARN macro to send a message to stderr in the
// debug build. It gets removed from the optimized build
// during preprocessing.
#ifdef DEBUG
#define JNF_WARN JNFDebugWarning
#else
#define JNF_WARN if (0) JNFDebugWarning
#endif /* DEBUG */

__BEGIN_DECLS

JNF_EXPORT extern void JNFDebugWarning(const char *fmt, ...);

JNF_EXPORT extern void JNFAssertionFailure(const char *file, int line, const char *condition, const char *msg);

#ifdef JAVA_ASSERTIONS_ON

#define JNF_ASSERT_FAILURE(condition, msg) \
JNFAssertionFailure(__FILE__, __LINE__, condition, msg) \


#define JNF_ASSERT_MSG(condition, msg) \
do { \
if (!(condition)) { \
JNF_ASSERT_FAILURE(#condition, msg); \
} \
} while(0) \


#define JNF_ASSERT_COND(condition) \
JNF_ASSERT_MSG(condition, NULL) \


#define JNF_EXCEPTION_WARN(env, msg) \
do { \
(*(env))->ExceptionDescribe(env); \
JNF_ASSERT_FAILURE("Java exception thrown", msg); \
} while (0) \


#define JNF_ASSERT_NO_EXCEPTION_MSG(env, msg) \
if ((*(env))->ExceptionOccurred(env)) { \
JNF_EXCEPTION_WARN(env, msg); \
} \


#define JNF_ASSERT_NO_EXCEPTION(env) \
JNF_ASSERT_NO_EXCEPTION_MSG(env, NULL) \

#else

#define JNF_ASSERT_COND(condition)
#define JNF_ASSERT_MSG(condition, msg)
#define JNF_EXCEPTION_WARN(env, msg)
#define JNF_ASSERT_NO_EXCEPTION(env)
#define JNF_ASSERT_NO_EXCEPTION_MSG(env, msg)

#endif /* JAVA_ASSERTIONS_ON */

JNF_EXPORT extern void JNFDumpJavaStack(JNIEnv *env);

__END_DECLS
69 changes: 69 additions & 0 deletions src/libjnf/JavaNativeFoundation/JNFAssert.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2008-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#import "JNFJNI.h"
#import "JNFAssert.h"

#import "debug.h"

static void JNFDebugMessageV(const char *fmt, va_list args) {
// Prints a message and breaks into debugger.
fprintf(stderr, "JavaNativeFoundation: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}

static void JNFDebugMessage(const char *fmt, ...) {
// Takes printf args and then calls DebugBreak
va_list args;
va_start(args, fmt);
JNFDebugMessageV(fmt, args);
va_end(args);
}

void JNFDebugWarning(const char *fmt, ...) {
// Takes printf args and then calls DebugBreak
va_list args;
va_start(args, fmt);
JNFDebugMessageV(fmt, args);
va_end(args);
}

void JNFAssertionFailure(const char *file, int line, const char *condition, const char *msg) {
JNFDebugMessage("Assertion failure: %s", condition);
if (msg) JNFDebugMessage(msg);
JNFDebugMessage("File %s; Line %d", file, line);
}

void JNFDumpJavaStack(JNIEnv *env) {
static JNF_CLASS_CACHE(jc_Thread, "java/lang/Thread");
static JNF_STATIC_MEMBER_CACHE(jsm_Thread_dumpStack, jc_Thread, "dumpStack", "()V");
JNFCallVoidMethod(env, jc_Thread.cls, jsm_Thread_dumpStack);
}
52 changes: 52 additions & 0 deletions src/libjnf/JavaNativeFoundation/JNFAutoreleasePool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2008-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* --
*
* Utility class used by the JNF_COCOA_ENTER()/JNF_COCOA_EXIT() macros
* from JNFJNI.h. Do not use this class or releated functions directly.
*/

#import <Foundation/Foundation.h>

#import <JavaNativeFoundation/JNFJNI.h>

__BEGIN_DECLS

typedef void JNFAutoreleasePoolToken;

// JNFNativeMethodEnter - called on entry to each native method by the
// JNF_COCOA_ENTER(env) macro in JNFJNI.h
JNF_EXPORT extern JNFAutoreleasePoolToken *JNFNativeMethodEnter(void);

// JNFNativeMethodExit - called on exit from each native method by the
// JNF_COCOA_EXIT(env) macro in JNFJNI.h
JNF_EXPORT extern void JNFNativeMethodExit(JNFAutoreleasePoolToken *token);

__END_DECLS
102 changes: 102 additions & 0 deletions src/libjnf/JavaNativeFoundation/JNFAutoreleasePool.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2008-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* --
*
* The JNFAutoreleasePool manages setting up and tearing down autorelease
* pools for Java calls into the Cocoa frameworks.
*
* The external entry point into this machinery is JNFMethodEnter() and JNFMethodExit().
*/

#import "JNFAutoreleasePool.h"

#import <mach/mach_time.h>

// These are vended by the Objective-C runtime, but they are unfortunately
// not available as API in the macOS SDK. We are following suit with swift
// and clang in declaring them inline here. They canot be removed or changed
// in the OS without major bincompat ramifications.
//
// These were added in macOS 10.7.
void * _Nonnull objc_autoreleasePoolPush(void);
void objc_autoreleasePoolPop(void * _Nonnull context);

#if TIMED
static int64_t elapsedTime = 0;
#endif

#pragma mark -
#pragma mark External API

// JNFNativeMethodEnter - called on entry to each native method
//
// It sets up an autorelease pool, and will return a token if
// JNFNativeMethodExit should be called. It attempts to consider
// how much time has elapsed since the last autorelease pop.

JNFAutoreleasePoolToken *JNFNativeMethodEnter() {
#if TIMED
int64_t start = mach_absolute_time();
#endif

JNFAutoreleasePoolToken * const tokenToReturn = objc_autoreleasePoolPush();

#if TIMED
elapsedTime += (mach_absolute_time() - start);
#endif

return tokenToReturn;
}


// JNFNativeMethodExit - called on exit from native methods
//
// This method is only called on exit from the first
// native method to appear in the execution stack.
// This function does not need to be called on exit
// from the inner native methods (as an optimization).
// JNFNativeMethodEnter sets the token to non-nil if
// JNFNativeMethodExit needs to be called on exit.

void JNFNativeMethodExit(JNFAutoreleasePoolToken *token) {

#if TIMED
int64_t start = mach_absolute_time();
#endif

objc_autoreleasePoolPop(token);

#if TIMED
elapsedTime += (mach_absolute_time() - start);

NSLog(@"elapsedTime: %llu", elapsedTime);
// elapsedTime = 0;
#endif
}
Loading

0 comments on commit fac3cb9

Please sign in to comment.