Merge "LibNativeBridge: Version 2"
This commit is contained in:
commit
02b29c49d4
7 changed files with 272 additions and 6 deletions
|
|
@ -18,6 +18,7 @@
|
||||||
#define NATIVE_BRIDGE_H_
|
#define NATIVE_BRIDGE_H_
|
||||||
|
|
||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
|
#include <signal.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
@ -26,6 +27,12 @@ namespace android {
|
||||||
struct NativeBridgeRuntimeCallbacks;
|
struct NativeBridgeRuntimeCallbacks;
|
||||||
struct NativeBridgeRuntimeValues;
|
struct NativeBridgeRuntimeValues;
|
||||||
|
|
||||||
|
// Function pointer type for sigaction. This is mostly the signature of a signal handler, except
|
||||||
|
// for the return type. The runtime needs to know whether the signal was handled or should be given
|
||||||
|
// to the chain.
|
||||||
|
typedef bool (*NativeBridgeSignalHandlerFn)(int, siginfo_t*, void*);
|
||||||
|
|
||||||
|
|
||||||
// Open the native bridge, if any. Should be called by Runtime::Init(). A null library filename
|
// Open the native bridge, if any. Should be called by Runtime::Init(). A null library filename
|
||||||
// signals that we do not want to load a native bridge.
|
// signals that we do not want to load a native bridge.
|
||||||
bool LoadNativeBridge(const char* native_bridge_library_filename,
|
bool LoadNativeBridge(const char* native_bridge_library_filename,
|
||||||
|
|
@ -63,6 +70,16 @@ void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shor
|
||||||
// True if native library is valid and is for an ABI that is supported by native bridge.
|
// True if native library is valid and is for an ABI that is supported by native bridge.
|
||||||
bool NativeBridgeIsSupported(const char* libpath);
|
bool NativeBridgeIsSupported(const char* libpath);
|
||||||
|
|
||||||
|
// Returns the version number of the native bridge. This information is available after a
|
||||||
|
// successful LoadNativeBridge() and before closing it, that is, as long as NativeBridgeAvailable()
|
||||||
|
// returns true. Returns 0 otherwise.
|
||||||
|
uint32_t NativeBridgeGetVersion();
|
||||||
|
|
||||||
|
// Returns a signal handler that the bridge would like to be managed. Only valid for a native
|
||||||
|
// bridge supporting the version 2 interface. Will return null if the bridge does not support
|
||||||
|
// version 2, or if it doesn't have a signal handler it wants to be known.
|
||||||
|
NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal);
|
||||||
|
|
||||||
// Returns whether we have seen a native bridge error. This could happen because the library
|
// Returns whether we have seen a native bridge error. This could happen because the library
|
||||||
// was not found, rejected, could not be initialized and so on.
|
// was not found, rejected, could not be initialized and so on.
|
||||||
//
|
//
|
||||||
|
|
@ -127,6 +144,31 @@ struct NativeBridgeCallbacks {
|
||||||
// NULL if not supported by native bridge.
|
// NULL if not supported by native bridge.
|
||||||
// Otherwise, return all environment values to be set after fork.
|
// Otherwise, return all environment values to be set after fork.
|
||||||
const struct NativeBridgeRuntimeValues* (*getAppEnv)(const char* instruction_set);
|
const struct NativeBridgeRuntimeValues* (*getAppEnv)(const char* instruction_set);
|
||||||
|
|
||||||
|
// Added callbacks in version 2.
|
||||||
|
|
||||||
|
// Check whether the bridge is compatible with the given version. A bridge may decide not to be
|
||||||
|
// forwards- or backwards-compatible, and libnativebridge will then stop using it.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
// bridge_version [IN] the version of libnativebridge.
|
||||||
|
// Returns:
|
||||||
|
// true iff the native bridge supports the given version of libnativebridge.
|
||||||
|
bool (*isCompatibleWith)(uint32_t bridge_version);
|
||||||
|
|
||||||
|
// A callback to retrieve a native bridge's signal handler for the specified signal. The runtime
|
||||||
|
// will ensure that the signal handler is being called after the runtime's own handler, but before
|
||||||
|
// all chained handlers. The native bridge should not try to install the handler by itself, as
|
||||||
|
// that will potentially lead to cycles.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
// signal [IN] the signal for which the handler is asked for. Currently, only SIGSEGV is
|
||||||
|
// supported by the runtime.
|
||||||
|
// Returns:
|
||||||
|
// NULL if the native bridge doesn't use a handler or doesn't want it to be managed by the
|
||||||
|
// runtime.
|
||||||
|
// Otherwise, a pointer to the signal handler.
|
||||||
|
NativeBridgeSignalHandlerFn (*getSignalHandler)(int signal);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Runtime interfaces to native bridge.
|
// Runtime interfaces to native bridge.
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ static bool had_error = false;
|
||||||
static void* native_bridge_handle = nullptr;
|
static void* native_bridge_handle = nullptr;
|
||||||
// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
|
// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
|
||||||
// later.
|
// later.
|
||||||
static NativeBridgeCallbacks* callbacks = nullptr;
|
static const NativeBridgeCallbacks* callbacks = nullptr;
|
||||||
// Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
|
// Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
|
||||||
static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
|
static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ static char* app_code_cache_dir = nullptr;
|
||||||
// and hard code the directory name again here.
|
// and hard code the directory name again here.
|
||||||
static constexpr const char* kCodeCacheDir = "code_cache";
|
static constexpr const char* kCodeCacheDir = "code_cache";
|
||||||
|
|
||||||
static constexpr uint32_t kNativeBridgeCallbackVersion = 1;
|
static constexpr uint32_t kLibNativeBridgeVersion = 2;
|
||||||
|
|
||||||
// Characters allowed in a native bridge filename. The first character must
|
// Characters allowed in a native bridge filename. The first character must
|
||||||
// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
|
// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
|
||||||
|
|
@ -121,7 +121,9 @@ bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
|
||||||
// First character must be [a-zA-Z].
|
// First character must be [a-zA-Z].
|
||||||
if (!CharacterAllowed(*ptr, true)) {
|
if (!CharacterAllowed(*ptr, true)) {
|
||||||
// Found an invalid fist character, don't accept.
|
// Found an invalid fist character, don't accept.
|
||||||
ALOGE("Native bridge library %s has been rejected for first character %c", nb_library_filename, *ptr);
|
ALOGE("Native bridge library %s has been rejected for first character %c",
|
||||||
|
nb_library_filename,
|
||||||
|
*ptr);
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// For the rest, be more liberal.
|
// For the rest, be more liberal.
|
||||||
|
|
@ -139,8 +141,22 @@ bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool VersionCheck(NativeBridgeCallbacks* cb) {
|
static bool VersionCheck(const NativeBridgeCallbacks* cb) {
|
||||||
return cb != nullptr && cb->version == kNativeBridgeCallbackVersion;
|
// Libnativebridge is now designed to be forward-compatible. So only "0" is an unsupported
|
||||||
|
// version.
|
||||||
|
if (cb == nullptr || cb->version == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a v2+ bridge, it may not be forwards- or backwards-compatible. Check.
|
||||||
|
if (cb->version >= 2) {
|
||||||
|
if (!callbacks->isCompatibleWith(kLibNativeBridgeVersion)) {
|
||||||
|
// TODO: Scan which version is supported, and fall back to handle it.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CloseNativeBridge(bool with_error) {
|
static void CloseNativeBridge(bool with_error) {
|
||||||
|
|
@ -321,7 +337,7 @@ static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the environment for the bridged app.
|
// Set up the environment for the bridged app.
|
||||||
static void SetupEnvironment(NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) {
|
static void SetupEnvironment(const NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) {
|
||||||
// Need a JNIEnv* to do anything.
|
// Need a JNIEnv* to do anything.
|
||||||
if (env == nullptr) {
|
if (env == nullptr) {
|
||||||
ALOGW("No JNIEnv* to set up app environment.");
|
ALOGW("No JNIEnv* to set up app environment.");
|
||||||
|
|
@ -485,4 +501,18 @@ bool NativeBridgeIsSupported(const char* libpath) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t NativeBridgeGetVersion() {
|
||||||
|
if (NativeBridgeAvailable()) {
|
||||||
|
return callbacks->version;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
|
||||||
|
if (NativeBridgeInitialized() && callbacks->version >= 2) {
|
||||||
|
return callbacks->getSignalHandler(signal);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
}; // namespace android
|
}; // namespace android
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ test_src_files := \
|
||||||
CodeCacheExists_test.cpp \
|
CodeCacheExists_test.cpp \
|
||||||
CompleteFlow_test.cpp \
|
CompleteFlow_test.cpp \
|
||||||
InvalidCharsNativeBridge_test.cpp \
|
InvalidCharsNativeBridge_test.cpp \
|
||||||
|
NativeBridge2Signal_test.cpp \
|
||||||
|
NativeBridgeVersion_test.cpp \
|
||||||
NeedsNativeBridge_test.cpp \
|
NeedsNativeBridge_test.cpp \
|
||||||
PreInitializeNativeBridge_test.cpp \
|
PreInitializeNativeBridge_test.cpp \
|
||||||
PreInitializeNativeBridgeFail1_test.cpp \
|
PreInitializeNativeBridgeFail1_test.cpp \
|
||||||
|
|
|
||||||
|
|
@ -32,3 +32,39 @@ LOCAL_LDFLAGS := -ldl
|
||||||
LOCAL_MULTILIB := both
|
LOCAL_MULTILIB := both
|
||||||
|
|
||||||
include $(BUILD_HOST_SHARED_LIBRARY)
|
include $(BUILD_HOST_SHARED_LIBRARY)
|
||||||
|
|
||||||
|
|
||||||
|
# v2.
|
||||||
|
|
||||||
|
NATIVE_BRIDGE2_COMMON_SRC_FILES := \
|
||||||
|
DummyNativeBridge2.cpp
|
||||||
|
|
||||||
|
# Shared library for target
|
||||||
|
# ========================================================
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE:= libnativebridge2-dummy
|
||||||
|
|
||||||
|
LOCAL_SRC_FILES:= $(NATIVE_BRIDGE2_COMMON_SRC_FILES)
|
||||||
|
LOCAL_CLANG := true
|
||||||
|
LOCAL_CFLAGS += -Werror -Wall
|
||||||
|
LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
|
||||||
|
LOCAL_LDFLAGS := -ldl
|
||||||
|
LOCAL_MULTILIB := both
|
||||||
|
|
||||||
|
include $(BUILD_SHARED_LIBRARY)
|
||||||
|
|
||||||
|
# Shared library for host
|
||||||
|
# ========================================================
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE:= libnativebridge2-dummy
|
||||||
|
|
||||||
|
LOCAL_SRC_FILES:= $(NATIVE_BRIDGE2_COMMON_SRC_FILES)
|
||||||
|
LOCAL_CLANG := true
|
||||||
|
LOCAL_CFLAGS += -Werror -Wall
|
||||||
|
LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
|
||||||
|
LOCAL_LDFLAGS := -ldl
|
||||||
|
LOCAL_MULTILIB := both
|
||||||
|
|
||||||
|
include $(BUILD_HOST_SHARED_LIBRARY)
|
||||||
|
|
|
||||||
76
libnativebridge/tests/DummyNativeBridge2.cpp
Normal file
76
libnativebridge/tests/DummyNativeBridge2.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2014 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// A dummy implementation of the native-bridge interface.
|
||||||
|
|
||||||
|
#include "nativebridge/native_bridge.h"
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
// NativeBridgeCallbacks implementations
|
||||||
|
extern "C" bool native_bridge2_initialize(const android::NativeBridgeRuntimeCallbacks* /* art_cbs */,
|
||||||
|
const char* /* app_code_cache_dir */,
|
||||||
|
const char* /* isa */) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void* native_bridge2_loadLibrary(const char* /* libpath */, int /* flag */) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void* native_bridge2_getTrampoline(void* /* handle */, const char* /* name */,
|
||||||
|
const char* /* shorty */, uint32_t /* len */) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" bool native_bridge2_isSupported(const char* /* libpath */) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge2_getAppEnv(
|
||||||
|
const char* /* abi */) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" bool native_bridge2_is_compatible_compatible_with(uint32_t version) {
|
||||||
|
// For testing, allow 1 and 2, but disallow 3+.
|
||||||
|
return version <= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool native_bridge2_dummy_signal_handler(int, siginfo_t*, void*) {
|
||||||
|
// TODO: Implement something here. We'd either have to have a death test with a log here, or
|
||||||
|
// we'd have to be able to resume after the faulting instruction...
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" android::NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal) {
|
||||||
|
if (signal == SIGSEGV) {
|
||||||
|
return &native_bridge2_dummy_signal_handler;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
android::NativeBridgeCallbacks NativeBridgeItf {
|
||||||
|
.version = 2,
|
||||||
|
.initialize = &native_bridge2_initialize,
|
||||||
|
.loadLibrary = &native_bridge2_loadLibrary,
|
||||||
|
.getTrampoline = &native_bridge2_getTrampoline,
|
||||||
|
.isSupported = &native_bridge2_isSupported,
|
||||||
|
.getAppEnv = &native_bridge2_getAppEnv,
|
||||||
|
.isCompatibleWith = &native_bridge2_is_compatible_compatible_with,
|
||||||
|
.getSignalHandler = &native_bridge2_get_signal_handler
|
||||||
|
};
|
||||||
|
|
||||||
42
libnativebridge/tests/NativeBridge2Signal_test.cpp
Normal file
42
libnativebridge/tests/NativeBridge2Signal_test.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2014 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 "NativeBridgeTest.h"
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
constexpr const char* kNativeBridgeLibrary2 = "libnativebridge2-dummy.so";
|
||||||
|
|
||||||
|
TEST_F(NativeBridgeTest, V2_Signal) {
|
||||||
|
// Init
|
||||||
|
ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary2, nullptr));
|
||||||
|
ASSERT_TRUE(NativeBridgeAvailable());
|
||||||
|
ASSERT_TRUE(PreInitializeNativeBridge(".", "isa"));
|
||||||
|
ASSERT_TRUE(NativeBridgeAvailable());
|
||||||
|
ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
|
||||||
|
ASSERT_TRUE(NativeBridgeAvailable());
|
||||||
|
|
||||||
|
ASSERT_EQ(2U, NativeBridgeGetVersion());
|
||||||
|
ASSERT_NE(nullptr, NativeBridgeGetSignalHandler(SIGSEGV));
|
||||||
|
|
||||||
|
// Clean-up code_cache
|
||||||
|
ASSERT_EQ(0, rmdir(kCodeCache));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
38
libnativebridge/tests/NativeBridgeVersion_test.cpp
Normal file
38
libnativebridge/tests/NativeBridgeVersion_test.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2015 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 "NativeBridgeTest.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
TEST_F(NativeBridgeTest, Version) {
|
||||||
|
// When a bridge isn't loaded, we expect 0.
|
||||||
|
EXPECT_EQ(NativeBridgeGetVersion(), 0U);
|
||||||
|
|
||||||
|
// After our dummy bridge has been loaded, we expect 1.
|
||||||
|
ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
|
||||||
|
EXPECT_EQ(NativeBridgeGetVersion(), 1U);
|
||||||
|
|
||||||
|
// Unload
|
||||||
|
UnloadNativeBridge();
|
||||||
|
|
||||||
|
// Version information is gone.
|
||||||
|
EXPECT_EQ(NativeBridgeGetVersion(), 0U);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
Loading…
Add table
Reference in a new issue