Skip to content

Commit

Permalink
Adding support for Java 8 static linking. (#50)
Browse files Browse the repository at this point in the history
* Adding support for Java 8 static linking.

I'm not really sure of an easy way to test this ATM, since the library would have to be statically linked with the JVM. I think for now, we should just get this in and cross the testing bridge when we get there.

Fixes #33
  • Loading branch information
Nathan Mittler committed Jan 23, 2017
1 parent f3a26b6 commit 160e730
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 69 deletions.
17 changes: 9 additions & 8 deletions .travis.yml
Expand Up @@ -71,15 +71,16 @@ before_script:
- ninja
- popd

# TODO(nathanmittler): Need to figure out how to make 32-bit builds work
# Build BoringSSL for 32-bit
- if [[ "$TRAVIS_OS_NAME" == "linux" ]];
then
mkdir $BORINGSSL_HOME/build32;
pushd $BORINGSSL_HOME/build32;
cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS="-Wa,--noexecstack -m32 -msse2" -GNinja ..;
ninja;
popd;
fi
# - if [[ "$TRAVIS_OS_NAME" == "linux" ]];
# then
# mkdir $BORINGSSL_HOME/build32;
# pushd $BORINGSSL_HOME/build32;
# cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS="-Wa,--noexecstack -m32 -msse2" -GNinja ..;
# ninja;
# popd;
# fi

# newest Android NDK
- if [[ "$TRAVIS_OS_NAME" == "linux" ]];
Expand Down
15 changes: 8 additions & 7 deletions appveyor.yml
Expand Up @@ -15,7 +15,7 @@ shallow_clone: true
os: Visual Studio 2015

platform:
- x86
# - x86
- x64

version: '{branch}.{build}'
Expand Down Expand Up @@ -70,12 +70,13 @@ before_build:
# Set up Visual Studio in 32-bit mode
- call "C:\Program Files (x86)\Microsoft Visual Studio %MSVC%\VC\vcvarsall.bat"

# TODO(nathanmittler): Need to figure out how to make 32-bit builds work
# Build BoringSSL in 32-bit
- cd "%BORINGSSL_HOME%"
- mkdir build32
- cd build32
- cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE=/MT -DCMAKE_CXX_FLAGS_RELEASE=/MT -GNinja ..
- ninja
# - cd "%BORINGSSL_HOME%"
# - mkdir build32
# - cd build32
# - cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE=/MT -DCMAKE_CXX_FLAGS_RELEASE=/MT -GNinja ..
# - ninja

# Go to project directory
- cd C:\projects\conscrypt
Expand All @@ -100,7 +101,7 @@ test_script:
after_test:
- ps: |
$url = "https://ci.appveyor.com/api/testresults/junit/$($env:APPVEYOR_JOB_ID)"
$files = Get-ChildItem '.\openjdk\build\test-results\test\TEST-*.xml'
$files = Get-ChildItem '.\openjdk\build\test-results\windows_x86_64Test\TEST-*.xml'
ForEach ($file in $files) {
(New-Object 'System.Net.WebClient').UploadFile($url, (Resolve-Path $file))
}
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Expand Up @@ -5,7 +5,6 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
classpath 'com.android.tools.build:gradle:2.2.2' // jcenter has the latest
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
#include "Errors.h"
#include "JniConstants.h"
#include "JniUtil.h"
#include "NativeCrypto.h"
#include "NetFd.h"
#include "NetworkUtil.h"
#include "OpenSslError.h"
Expand Down Expand Up @@ -8871,6 +8872,12 @@ static int NativeCrypto_ENGINE_SSL_write_heap(JNIEnv* env, jclass, jlong sslRef,
return result;
}

#define CONSCRYPT_NATIVE_METHOD(className, functionName, signature) \
{ \
(char*)#functionName, (char*)(signature), \
reinterpret_cast<void*>(className##_##functionName) \
}

#define FILE_DESCRIPTOR "Ljava/io/FileDescriptor;"
#define SSL_CALLBACKS \
"L" TO_STRING(JNI_JARJAR_PREFIX) "org/conscrypt/NativeCrypto$SSLHandshakeCallbacks;"
Expand Down Expand Up @@ -9148,28 +9155,10 @@ static JNINativeMethod sNativeCryptoMethods[] = {
CONSCRYPT_NATIVE_METHOD(NativeCrypto, ENGINE_SSL_shutdown, "(J" SSL_CALLBACKS ")V"),
};

#ifdef STATIC_LIB
// Give client libs everything they need to initialize our JNI
jint libconscrypt_JNI_OnLoad(JavaVM* vm, void*) {
#else
// Use JNI_OnLoad for when we're standalone
CONSCRYPT_PUBLIC jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
JNI_TRACE("JNI_OnLoad NativeCrypto");
#endif
JNIEnv *env;
if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
ALOGE("Could not get JNIEnv");
return JNI_ERR;
}

JniConstants::init(vm, env);

void NativeCrypto::registerNativeMethods(JNIEnv* env) {
JniUtil::jniRegisterNativeMethods(env,
TO_STRING(JNI_JARJAR_PREFIX) "org/conscrypt/NativeCrypto",
sNativeCryptoMethods, NELEM(sNativeCryptoMethods));

CompatibilityCloseMonitor::init();
return JNI_VERSION_1_6;
}

/* Local Variables: */
Expand Down
66 changes: 66 additions & 0 deletions common/src/jni/main/cpp/jni_load.cpp
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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
*
* http://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 "CompatibilityCloseMonitor.h"
#include "JniConstants.h"
#include "NativeCrypto.h"
#include "macros.h"

#ifndef CONSCRYPT_JNI_VERSION
#define CONSCRYPT_JNI_VERSION JNI_VERSION_1_6
#endif // !CONSCRYPT_JNI_VERSION

using namespace conscrypt;

// Give client libs everything they need to initialize our JNI
jint libconscrypt_JNI_OnLoad(JavaVM* vm, void*) {
JNIEnv* env;
if (vm->GetEnv((void**)&env, CONSCRYPT_JNI_VERSION) != JNI_OK) {
ALOGE("Could not get JNIEnv");
return JNI_ERR;
}

// Initialize the JNI constants.
JniConstants::init(vm, env);

// Register all of the native JNI methods.
NativeCrypto::registerNativeMethods(env);

// Perform static initialization of the close monitor (if required on this platform).
CompatibilityCloseMonitor::init();
return CONSCRYPT_JNI_VERSION;
}

#ifdef STATIC_LIB

// A version of OnLoad called when the Conscrypt library has been statically linked to the JVM (For
// Java >= 1.8). The manner in which the library is statically linked is implementation specific.
//
// See http://openjdk.java.net/jeps/178
CONSCRYPT_PUBLIC jint JNI_OnLoad_conscrypt(JavaVM* vm, void* reserved) {
return libconscrypt_JNI_OnLoad(vm, reserved);
}

#else // !STATIC_LIB

// Method called by the JVM when the Conscrypt shared library is loaded.
CONSCRYPT_PUBLIC jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
return libconscrypt_JNI_OnLoad(vm, reserved);
}

#endif // !STATIC_LIB
5 changes: 1 addition & 4 deletions common/src/jni/main/include/JniUtil.h
Expand Up @@ -93,9 +93,8 @@ class JniUtil {
/**
* Register one or more native methods with a particular class.
* "className" looks like "java/lang/String". Aborts on failure.
* TODO: fix all callers and change the return type to void.
*/
static int jniRegisterNativeMethods(JNIEnv* env, const char* className,
static void jniRegisterNativeMethods(JNIEnv* env, const char* className,
const JNINativeMethod* gMethods, int numMethods) {
ALOGV("Registering %s's %d native methods...", className, numMethods);

Expand All @@ -112,8 +111,6 @@ class JniUtil {
(void)asprintf(&msg, "RegisterNatives failed for '%s'; aborting...", className);
env->FatalError(msg);
}

return 0;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions common/src/jni/main/include/NativeCrypto.h
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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
*
* http://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.
*/

#ifndef CONSCRYPT_NATIVECRYPTO_H_
#define CONSCRYPT_NATIVECRYPTO_H_

#include <jni.h>

namespace conscrypt {

/**
* Provides registration support for all native methods exported by Conscrypt.
*/
class NativeCrypto {
private:
NativeCrypto() {}
~NativeCrypto() {}

public:
/**
* Register all native JNI methods exported by Conscrypt.
*/
static void registerNativeMethods(JNIEnv* env);
};

} // namespace conscrypt

#endif // CONSCRYPT_NATIVECRYPTO_H_
6 changes: 0 additions & 6 deletions common/src/jni/main/include/macros.h
Expand Up @@ -93,12 +93,6 @@
#define CONSCRYPT_ATTRIBUTE_1(value)
#endif

#define CONSCRYPT_NATIVE_METHOD(className, functionName, signature) \
{ \
(char*)#functionName, (char*)(signature), \
reinterpret_cast<void*>(className##_##functionName) \
}

#ifndef NELEM
#define NELEM(x) ((int)(sizeof(x) / sizeof((x)[0])))
#endif
Expand Down

0 comments on commit 160e730

Please sign in to comment.