Make the native bridge API a C API.

In order to move the library into the runtime APEX.

Test: m and boot
Bug: 119840313
Bug: 122710865
Change-Id: I4aac5954d2fb5f9e3ba92e486d69f1e76614df92
This commit is contained in:
Nicolas Geoffray 2019-01-10 16:27:54 +00:00
parent 3ffb4ab82a
commit d9b4d9b16a
6 changed files with 122 additions and 26 deletions

View file

@ -17,6 +17,11 @@ cc_library {
"liblog",
],
stubs: {
symbol_file: "libnativebridge.map.txt",
versions: ["1"],
},
export_include_dirs: ["include"],
cflags: [

View file

@ -17,12 +17,17 @@
#ifndef NATIVE_BRIDGE_H_
#define NATIVE_BRIDGE_H_
#include "jni.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include "jni.h"
#ifdef __cplusplus
namespace android {
extern "C" {
#endif // __cplusplus
struct NativeBridgeRuntimeCallbacks;
struct NativeBridgeRuntimeValues;
@ -32,11 +37,10 @@ struct NativeBridgeRuntimeValues;
// 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
// signals that we do not want to load a native bridge.
bool LoadNativeBridge(const char* native_bridge_library_filename,
const NativeBridgeRuntimeCallbacks* runtime_callbacks);
const struct NativeBridgeRuntimeCallbacks* runtime_callbacks);
// Quick check whether a native bridge will be needed. This is based off of the instruction set
// of the process.
@ -138,19 +142,17 @@ bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Should not use in non-namespace scenario.
native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name,
const char* ld_library_path,
const char* default_library_path,
uint64_t type,
const char* permitted_when_isolated_path,
native_bridge_namespace_t* parent_ns);
struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent_ns);
// Creates a link which shares some libraries from one namespace to another.
// NativeBridge's peer of android_link_namespaces() of dynamic linker.
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Should not use in non-namespace scenario.
bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
struct native_bridge_namespace_t* to,
const char* shared_libs_sonames);
// Load a shared library with namespace key that is supported by the native bridge.
@ -159,10 +161,11 @@ bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_n
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Use NativeBridgeLoadLibrary() instead in non-namespace scenario.
void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns);
void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
struct native_bridge_namespace_t* ns);
// Returns vendor namespace if it is enabled for the device and null otherwise
native_bridge_namespace_t* NativeBridgeGetVendorNamespace();
struct native_bridge_namespace_t* NativeBridgeGetVendorNamespace();
// Native bridge interfaces to runtime.
struct NativeBridgeCallbacks {
@ -177,8 +180,8 @@ struct NativeBridgeCallbacks {
// runtime_cbs [IN] the pointer to NativeBridgeRuntimeCallbacks.
// Returns:
// true if initialization was successful.
bool (*initialize)(const NativeBridgeRuntimeCallbacks* runtime_cbs, const char* private_dir,
const char* instruction_set);
bool (*initialize)(const struct NativeBridgeRuntimeCallbacks* runtime_cbs,
const char* private_dir, const char* instruction_set);
// Load a shared library that is supported by the native bridge.
//
@ -314,12 +317,12 @@ struct NativeBridgeCallbacks {
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Should not use in non-namespace scenario.
native_bridge_namespace_t* (*createNamespace)(const char* name,
const char* ld_library_path,
const char* default_library_path,
uint64_t type,
const char* permitted_when_isolated_path,
native_bridge_namespace_t* parent_ns);
struct native_bridge_namespace_t* (*createNamespace)(const char* name,
const char* ld_library_path,
const char* default_library_path,
uint64_t type,
const char* permitted_when_isolated_path,
struct native_bridge_namespace_t* parent_ns);
// Creates a link which shares some libraries from one namespace to another.
// NativeBridge's peer of android_link_namespaces() of dynamic linker.
@ -334,8 +337,8 @@ struct NativeBridgeCallbacks {
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Should not use in non-namespace scenario.
bool (*linkNamespaces)(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
const char* shared_libs_sonames);
bool (*linkNamespaces)(struct native_bridge_namespace_t* from,
struct native_bridge_namespace_t* to, const char* shared_libs_sonames);
// Load a shared library within a namespace.
// NativeBridge's peer of android_dlopen_ext() of dynamic linker, only supports namespace
@ -350,7 +353,7 @@ struct NativeBridgeCallbacks {
//
// Starting with v3, NativeBridge has two scenarios: with/without namespace.
// Use loadLibrary instead in non-namespace scenario.
void* (*loadLibraryExt)(const char* libpath, int flag, native_bridge_namespace_t* ns);
void* (*loadLibraryExt)(const char* libpath, int flag, struct native_bridge_namespace_t* ns);
// Get native bridge version of vendor namespace.
// The vendor namespace is the namespace used to load vendor public libraries.
@ -359,7 +362,7 @@ struct NativeBridgeCallbacks {
//
// Returns:
// vendor namespace or null if it was not set up for the device
native_bridge_namespace_t* (*getVendorNamespace)();
struct native_bridge_namespace_t* (*getVendorNamespace)();
};
// Runtime interfaces to native bridge.
@ -396,6 +399,9 @@ struct NativeBridgeRuntimeCallbacks {
uint32_t method_count);
};
}; // namespace android
#ifdef __cplusplus
} // extern "C"
} // namespace android
#endif // __cplusplus
#endif // NATIVE_BRIDGE_H_

View file

@ -0,0 +1,45 @@
#
# Copyright (C) 2019 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.
#
# TODO(b/122710865): Most of these uses come from libnativeloader, which should be bundled
# together with libnativebridge in the APEX. Once this happens, prune this list.
LIBNATIVEBRIDGE_1 {
global:
NativeBridgeIsSupported;
NativeBridgeLoadLibrary;
NativeBridgeUnloadLibrary;
NativeBridgeGetError;
NativeBridgeIsPathSupported;
NativeBridgeCreateNamespace;
NativeBridgeGetVendorNamespace;
NativeBridgeLinkNamespaces;
NativeBridgeLoadLibraryExt;
NativeBridgeInitAnonymousNamespace;
NativeBridgeInitialized;
NativeBridgeGetTrampoline;
LoadNativeBridge;
PreInitializeNativeBridge;
InitializeNativeBridge;
NativeBridgeGetVersion;
NativeBridgeGetSignalHandler;
UnloadNativeBridge;
NativeBridgeAvailable;
NeedsNativeBridge;
NativeBridgeError;
NativeBridgeNameAcceptable;
local:
*;
};

View file

@ -33,6 +33,8 @@
namespace android {
extern "C" {
// Environment values required by the apps running with native bridge.
struct NativeBridgeRuntimeValues {
const char* os_arch;
@ -626,4 +628,6 @@ void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_na
return nullptr;
}
}; // namespace android
} // extern "C"
} // namespace android

View file

@ -86,3 +86,14 @@ cc_test {
],
header_libs: ["libbase_headers"],
}
// Build the test for the C API.
art_cc_test {
name: "libnativebridge-api-tests",
host_supported: true,
test_per_src: true,
srcs: [
"NativeBridgeApi.c",
],
header_libs: ["libnativebridge-dummy-headers"],
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (C) 2019 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.
*/
/* The main purpose of this test is to ensure this C header compiles in C, so
* that no C++ features inadvertently leak into the C ABI. */
#include "nativebridge/native_bridge.h"
int main(int argc, char** argv) {
(void)argc;
(void)argv;
return 0;
}