diff options
Diffstat (limited to 'tests/libs')
-rw-r--r-- | tests/libs/Android.build.dlext_testzip.mk | 14 | ||||
-rw-r--r-- | tests/libs/Android.mk | 15 | ||||
-rw-r--r-- | tests/libs/bionic_tests_zipalign.cpp | 157 |
3 files changed, 180 insertions, 6 deletions
diff --git a/tests/libs/Android.build.dlext_testzip.mk b/tests/libs/Android.build.dlext_testzip.mk index c06f4a9bc..56be1e222 100644 --- a/tests/libs/Android.build.dlext_testzip.mk +++ b/tests/libs/Android.build.dlext_testzip.mk @@ -18,6 +18,8 @@ # Library used by dlext tests - zipped and aligned # ----------------------------------------------------------------------------- +BIONIC_TESTS_ZIPALIGN := $(HOST_OUT_EXECUTABLES)/bionic_tests_zipalign + include $(CLEAR_VARS) LOCAL_MODULE_CLASS := SHARED_LIBRARIES @@ -33,12 +35,12 @@ my_shared_libs := \ $($(bionic_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/libdlext_test_zip.so \ $($(bionic_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/libatest_simple_zip.so -$(LOCAL_BUILT_MODULE) : $(my_shared_libs) | $(ZIPALIGN) - @echo "Zipalign: $@" +$(LOCAL_BUILT_MODULE) : $(my_shared_libs) | $(BIONIC_TESTS_ZIPALIGN) + @echo "Aligning zip: $@" $(hide) rm -rf $(dir $@) && mkdir -p $(dir $@)/libdir $(hide) cp $^ $(dir $@)/libdir $(hide) (cd $(dir $@) && touch empty_file.txt && zip -qrD0 $(notdir $@).unaligned empty_file.txt libdir/*.so) - $(hide) $(ZIPALIGN) -p 4 $@.unaligned $@ + $(hide) $(BIONIC_TESTS_ZIPALIGN) 4096 $@.unaligned $@ include $(CLEAR_VARS) @@ -68,8 +70,8 @@ $(LOCAL_BUILT_MODULE) : PRIVATE_LIB_C := \ $($(bionic_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/libtest_dt_runpath_c.so $(LOCAL_BUILT_MODULE) : PRIVATE_LIB_X := \ $($(bionic_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/libtest_dt_runpath_x.so -$(LOCAL_BUILT_MODULE) : $(my_shared_libs) | $(ZIPALIGN) - @echo "Zipalign: $@" +$(LOCAL_BUILT_MODULE) : $(my_shared_libs) | $(BIONIC_TESTS_ZIPALIGN) + @echo "Aligning zip: $@" $(hide) rm -rf $(dir $@) && mkdir -p $(dir $@)/libdir && \ mkdir -p $(dir $@)/libdir/dt_runpath_a && mkdir -p $(dir $@)/libdir/dt_runpath_b_c_x $(hide) cp $(PRIVATE_LIB_D) $(dir $@)/libdir @@ -78,5 +80,5 @@ $(LOCAL_BUILT_MODULE) : $(my_shared_libs) | $(ZIPALIGN) $(hide) cp $(PRIVATE_LIB_C) $(dir $@)/libdir/dt_runpath_b_c_x $(hide) cp $(PRIVATE_LIB_X) $(dir $@)/libdir/dt_runpath_b_c_x $(hide) (cd $(dir $@) && touch empty_file.txt && zip -qrD0 $(notdir $@).unaligned empty_file.txt libdir) - $(hide) $(ZIPALIGN) -p 4 $@.unaligned $@ + $(hide) $(BIONIC_TESTS_ZIPALIGN) 4096 $@.unaligned $@ diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk index 93d95ee32..506d6f280 100644 --- a/tests/libs/Android.mk +++ b/tests/libs/Android.mk @@ -486,3 +486,18 @@ libtest_dlopen_from_ctor_main_shared_libraries := libtest_dlopen_from_ctor module := libtest_dlopen_from_ctor_main include $(LOCAL_PATH)/Android.build.testlib.mk + +# ----------------------------------------------------------------------------- +# Tool to use to align the shared libraries in a zip file. +# ----------------------------------------------------------------------------- +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := bionic_tests_zipalign.cpp +LOCAL_MODULE := bionic_tests_zipalign +LOCAL_CFLAGS := -Wall -Werror + +LOCAL_STATIC_LIBRARIES := libziparchive-host liblog libbase libz libutils + +LOCAL_MODULE_HOST_OS := darwin linux windows + +include $(BUILD_HOST_EXECUTABLE) diff --git a/tests/libs/bionic_tests_zipalign.cpp b/tests/libs/bionic_tests_zipalign.cpp new file mode 100644 index 000000000..dbbfd7ccd --- /dev/null +++ b/tests/libs/bionic_tests_zipalign.cpp @@ -0,0 +1,157 @@ +/* + * 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 <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#include <algorithm> +#include <string> +#include <vector> + +#include <ziparchive/zip_archive.h> +#include <ziparchive/zip_archive_stream_entry.h> +#include <ziparchive/zip_writer.h> + +static void usage() { + fprintf(stderr, "usage: bionic_tests_zipalign ALIGNMENT INPUT_ZIP_FILE OUTPUT_ZIP_FILE\n"); + fprintf(stderr, " ALIGNMENT:\n"); + fprintf(stderr, " The new alignment of all entries in the new zip file.\n"); + fprintf(stderr, " INPUT_ZIP_FILE:\n"); + fprintf(stderr, " The input zip file that will be read but left unmodified.\n"); + fprintf(stderr, " OUTPUT_ZIP_FILE:\n"); + fprintf(stderr, " The output zip file that will be created from the input file.\n"); +} + +typedef std::pair<ZipEntry*, ZipString*> ZipData; + +static bool GetEntries(ZipArchiveHandle handle, std::vector<ZipData>* entries) { + void* cookie; + int32_t return_value = StartIteration(handle, &cookie, nullptr, nullptr); + if (return_value != 0) { + fprintf(stderr, "Unable to iterate over entries: %s\n", ErrorCodeString(return_value)); + return false; + } + + ZipEntry entry; + ZipString name; + while ((return_value = Next(cookie, &entry, &name)) == 0) { + entries->push_back(std::make_pair(new ZipEntry(entry), new ZipString(name))); + } + if (return_value != -1) { + fprintf(stderr, "Error while iterating over zip entries: %s\n", ErrorCodeString(return_value)); + } else { + // Sort by offset. + std::sort(entries->begin(), entries->end(), + [](ZipData a, ZipData b) { return a.first->offset < b.first->offset; }); + } + + EndIteration(cookie); + return return_value == -1; +} + +static bool CreateAlignedZip(ZipArchiveHandle& handle, FILE* zip_dst, uint32_t alignment) { + std::vector<ZipData> entries; + // We will not free the memory created in entries since the program + // terminates right after this function is called. + if (!GetEntries(handle, &entries)) { + return false; + } + + ZipWriter writer(zip_dst); + + int32_t error; + for (auto& entry : entries) { + ZipEntry* zip_entry = entry.first; + ZipString* zip_str = entry.second; + + size_t flags = 0; + if ((zip_entry->method & kCompressDeflated) != 0) { + flags |= ZipWriter::kCompress; + } + std::string zip_name(reinterpret_cast<const char*>(zip_str->name), zip_str->name_length); + error = writer.StartAlignedEntry(zip_name.c_str(), flags, alignment); + if (error != 0) { + fprintf(stderr, "StartAlignedEntry failed: %s\n", ZipWriter::ErrorCodeString(error)); + return false; + } + std::unique_ptr<ZipArchiveStreamEntry> stream( + ZipArchiveStreamEntry::Create(handle, *zip_entry)); + const std::vector<uint8_t>* data; + while ((data = stream->Read()) != nullptr) { + error = writer.WriteBytes(data->data(), data->size()); + if (error != 0) { + fprintf(stderr, "WriteBytes failed: %s\n", ZipWriter::ErrorCodeString(error)); + return false; + } + } + if (!stream->Verify()) { + fprintf(stderr, "Failed to verify zip stream writer entry.\n"); + return false; + } + error = writer.FinishEntry(); + if (error != 0) { + fprintf(stderr, "FinishEntry failed: %s\n", ZipWriter::ErrorCodeString(error)); + } + } + + error = writer.Finish(); + if (error != 0) { + fprintf(stderr, "Finish failed: %s\n", ZipWriter::ErrorCodeString(error)); + return false; + } + return true; +} + +int main(int argc, char* argv[]) { + if (argc != 4) { + usage(); + return 1; + } + + char* end; + unsigned long int alignment = strtoul(argv[1], &end, 10); + if ((alignment == ULONG_MAX && errno == ERANGE) || *end != '\0') { + fprintf(stderr, "ALIGNMENT value is not a valid number: %s\n", argv[1]); + usage(); + return 1; + } + if (((alignment - 1) & alignment) != 0) { + fprintf(stderr, "ALIGNMENT value is not a power of 2: %s\n", argv[1]); + return 1; + } + + ZipArchiveHandle handle; + + int32_t return_value = OpenArchive(argv[2], &handle); + if (return_value != 0) { + fprintf(stderr, "Unable to open '%s': %s\n", argv[2], ErrorCodeString(return_value)); + return false; + } + + FILE* zip_dst = fopen(argv[3], "we"); + if (zip_dst == nullptr) { + fprintf(stderr, "Unable to create '%s': %s\n", argv[3], strerror(errno)); + return 1; + } + + bool success = CreateAlignedZip(handle, zip_dst, static_cast<uint32_t>(alignment)); + + CloseArchive(handle); + fclose(zip_dst); + + return success ? 0 : 1; +} |