am 09a0b17d: Merge "Allow native bridge to work without a code cache"

* commit '09a0b17d863b919f109a6377a4b3e1e75bb67a98':
  Allow native bridge to work without a code cache
This commit is contained in:
Calin Juravle 2015-07-13 13:02:42 +00:00 committed by Android Git Automerger
commit ed5c99eacc
4 changed files with 68 additions and 10 deletions

View file

@ -109,6 +109,13 @@ static bool CharacterAllowed(char c, bool first) {
} }
} }
static void ReleaseAppCodeCacheDir() {
if (app_code_cache_dir != nullptr) {
delete[] app_code_cache_dir;
app_code_cache_dir = nullptr;
}
}
// We only allow simple names for the library. It is supposed to be a file in // We only allow simple names for the library. It is supposed to be a file in
// /system/lib or /vendor/lib. Only allow a small range of characters, that is // /system/lib or /vendor/lib. Only allow a small range of characters, that is
// names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z]. // names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
@ -162,8 +169,7 @@ static bool VersionCheck(const NativeBridgeCallbacks* cb) {
static void CloseNativeBridge(bool with_error) { static void CloseNativeBridge(bool with_error) {
state = NativeBridgeState::kClosed; state = NativeBridgeState::kClosed;
had_error |= with_error; had_error |= with_error;
delete[] app_code_cache_dir; ReleaseAppCodeCacheDir();
app_code_cache_dir = nullptr;
} }
bool LoadNativeBridge(const char* nb_library_filename, bool LoadNativeBridge(const char* nb_library_filename,
@ -406,16 +412,16 @@ bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
if (stat(app_code_cache_dir, &st) == -1) { if (stat(app_code_cache_dir, &st) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) { if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
ALOGE("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno)); ALOGW("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
CloseNativeBridge(true); ReleaseAppCodeCacheDir();
} }
} else { } else {
ALOGE("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno)); ALOGW("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
CloseNativeBridge(true); ReleaseAppCodeCacheDir();
} }
} else if (!S_ISDIR(st.st_mode)) { } else if (!S_ISDIR(st.st_mode)) {
ALOGE("Code cache is not a directory %s.", app_code_cache_dir); ALOGW("Code cache is not a directory %s.", app_code_cache_dir);
CloseNativeBridge(true); ReleaseAppCodeCacheDir();
} }
// If we're still PreInitialized (dind't fail the code cache checks) try to initialize. // If we're still PreInitialized (dind't fail the code cache checks) try to initialize.
@ -424,8 +430,7 @@ bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
SetupEnvironment(callbacks, env, instruction_set); SetupEnvironment(callbacks, env, instruction_set);
state = NativeBridgeState::kInitialized; state = NativeBridgeState::kInitialized;
// We no longer need the code cache path, release the memory. // We no longer need the code cache path, release the memory.
delete[] app_code_cache_dir; ReleaseAppCodeCacheDir();
app_code_cache_dir = nullptr;
} else { } else {
// Unload the library. // Unload the library.
dlclose(native_bridge_handle); dlclose(native_bridge_handle);

View file

@ -9,6 +9,7 @@ include $(CLEAR_VARS)
test_src_files := \ test_src_files := \
CodeCacheCreate_test.cpp \ CodeCacheCreate_test.cpp \
CodeCacheExists_test.cpp \ CodeCacheExists_test.cpp \
CodeCacheStatFail_test.cpp \
CompleteFlow_test.cpp \ CompleteFlow_test.cpp \
InvalidCharsNativeBridge_test.cpp \ InvalidCharsNativeBridge_test.cpp \
NativeBridge2Signal_test.cpp \ NativeBridge2Signal_test.cpp \

View file

@ -0,0 +1,51 @@
/*
* 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 <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
namespace android {
// Tests that the bridge is initialized without errors if the code_cache is
// existed as a file.
TEST_F(NativeBridgeTest, CodeCacheStatFail) {
int fd = creat(kCodeCache, O_RDWR);
ASSERT_NE(-1, fd);
close(fd);
struct stat st;
ASSERT_EQ(-1, stat(kCodeCacheStatFail, &st));
ASSERT_EQ(ENOTDIR, errno);
// Init
ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
ASSERT_TRUE(PreInitializeNativeBridge(kCodeCacheStatFail, "isa"));
ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
ASSERT_TRUE(NativeBridgeAvailable());
ASSERT_FALSE(NativeBridgeError());
// Clean up
UnloadNativeBridge();
ASSERT_FALSE(NativeBridgeError());
unlink(kCodeCache);
}
} // namespace android

View file

@ -24,6 +24,7 @@
constexpr const char* kNativeBridgeLibrary = "libnativebridge-dummy.so"; constexpr const char* kNativeBridgeLibrary = "libnativebridge-dummy.so";
constexpr const char* kCodeCache = "./code_cache"; constexpr const char* kCodeCache = "./code_cache";
constexpr const char* kCodeCacheStatFail = "./code_cache/temp";
namespace android { namespace android {