2010-02-18 01:11:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 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.
|
|
|
|
*/
|
|
|
|
|
2016-12-15 19:15:06 +01:00
|
|
|
// See imgdiff.cpp in this directory for a description of the patch file
|
2010-02-18 01:11:44 +01:00
|
|
|
// format.
|
|
|
|
|
2016-12-15 19:15:06 +01:00
|
|
|
#include <applypatch/imgpatch.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <stdio.h>
|
2016-12-15 19:15:06 +01:00
|
|
|
#include <string.h>
|
2014-03-14 17:39:48 +01:00
|
|
|
#include <sys/cdefs.h>
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-05-18 07:41:55 +02:00
|
|
|
#include <memory>
|
2016-10-12 19:55:04 +02:00
|
|
|
#include <string>
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2017-05-18 07:41:55 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/memory.h>
|
2016-12-15 19:15:06 +01:00
|
|
|
#include <applypatch/applypatch.h>
|
|
|
|
#include <applypatch/imgdiff.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2017-10-09 20:03:38 +02:00
|
|
|
#include "edify/expr.h"
|
2018-04-26 02:19:20 +02:00
|
|
|
#include "otautil/print_sha1.h"
|
2017-10-09 20:03:38 +02:00
|
|
|
|
2017-03-07 23:44:14 +01:00
|
|
|
static inline int64_t Read8(const void *address) {
|
|
|
|
return android::base::get_unaligned<int64_t>(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t Read4(const void *address) {
|
|
|
|
return android::base::get_unaligned<int32_t>(address);
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-05-18 07:41:55 +02:00
|
|
|
// This function is a wrapper of ApplyBSDiffPatch(). It has a custom sink function to deflate the
|
|
|
|
// patched data and stream the deflated data to output.
|
|
|
|
static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len,
|
2017-11-10 20:49:53 +01:00
|
|
|
const Value& patch, size_t patch_offset,
|
2018-04-20 06:02:13 +02:00
|
|
|
const char* deflate_header, SinkFn sink) {
|
2017-05-18 07:41:55 +02:00
|
|
|
size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32));
|
2018-12-15 00:22:40 +01:00
|
|
|
CHECK_GT(expected_target_length, static_cast<size_t>(0));
|
2017-05-18 07:41:55 +02:00
|
|
|
int level = Read4(deflate_header + 40);
|
|
|
|
int method = Read4(deflate_header + 44);
|
|
|
|
int window_bits = Read4(deflate_header + 48);
|
|
|
|
int mem_level = Read4(deflate_header + 52);
|
|
|
|
int strategy = Read4(deflate_header + 56);
|
|
|
|
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
z_stream strm;
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
strm.avail_in = 0;
|
|
|
|
strm.next_in = nullptr;
|
|
|
|
int ret = deflateInit2(&strm, level, method, window_bits, mem_level, strategy);
|
2017-05-18 07:41:55 +02:00
|
|
|
if (ret != Z_OK) {
|
|
|
|
LOG(ERROR) << "Failed to init uncompressed data deflation: " << ret;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define a custom sink wrapper that feeds to bspatch. It deflates the available patch data on
|
|
|
|
// the fly and outputs the compressed data to the given sink.
|
|
|
|
size_t actual_target_length = 0;
|
|
|
|
size_t total_written = 0;
|
|
|
|
static constexpr size_t buffer_size = 32768;
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written,
|
2018-07-03 23:46:15 +02:00
|
|
|
&ret, &sink](const uint8_t* data, size_t len) -> size_t {
|
2017-05-18 07:41:55 +02:00
|
|
|
// The input patch length for an update never exceeds INT_MAX.
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
strm.avail_in = len;
|
|
|
|
strm.next_in = data;
|
2017-05-18 07:41:55 +02:00
|
|
|
do {
|
|
|
|
std::vector<uint8_t> buffer(buffer_size);
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
strm.avail_out = buffer_size;
|
|
|
|
strm.next_out = buffer.data();
|
2017-05-18 07:41:55 +02:00
|
|
|
if (actual_target_length + len < expected_target_length) {
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
ret = deflate(&strm, Z_NO_FLUSH);
|
2017-05-18 07:41:55 +02:00
|
|
|
} else {
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
ret = deflate(&strm, Z_FINISH);
|
2017-05-18 07:41:55 +02:00
|
|
|
}
|
|
|
|
if (ret != Z_OK && ret != Z_STREAM_END) {
|
|
|
|
LOG(ERROR) << "Failed to deflate stream: " << ret;
|
|
|
|
// zero length indicates an error in the sink function of bspatch().
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
size_t have = buffer_size - strm.avail_out;
|
2017-05-18 07:41:55 +02:00
|
|
|
total_written += have;
|
|
|
|
if (sink(buffer.data(), have) != have) {
|
|
|
|
LOG(ERROR) << "Failed to write " << have << " compressed bytes to output.";
|
|
|
|
return 0;
|
|
|
|
}
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
} while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
|
2017-05-18 07:41:55 +02:00
|
|
|
|
|
|
|
actual_target_length += len;
|
|
|
|
return len;
|
|
|
|
};
|
|
|
|
|
2018-04-20 06:02:13 +02:00
|
|
|
int bspatch_result = ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink);
|
applypatch: Fix a memory leak in ApplyImagePatch().
$ valgrind --leak-check=full out/host/linux-x86/nativetest64/recovery_host_test/recovery_host_test
==36755== 112 bytes in 1 blocks are definitely lost in loss record 4 of 16
==36755== at 0x40307C4: malloc (valgrind/coregrind/m_replacemalloc/vg_replace_malloc.c:270)
==36755== by 0x40C1669: operator new(unsigned long) (external/libcxxabi/src/cxa_new_delete.cpp:46)
==36755== by 0x18D6A8: ApplyImagePatch(unsigned char const*, unsigned long, Value const*, std::__1::function<unsigned long (unsigned char const*, unsigned long)>, sha_state_st*, Value const*) (bootable/recovery/applypatch/imgpatch.cpp:62)
==36755== by 0x18D02B: ApplyImagePatch(unsigned char const*, unsigned long, unsigned char const*, unsigned long, std::__1::function<unsigned long (unsigned char const*, unsigned long)>) (bootable/recovery/applypatch/imgpatch.cpp:134)
==36755== by 0x160D15: GenerateTarget(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (bootable/recovery/tests/component/imgdiff_test.cpp:85)
==36755== by 0x11FA7D: verify_patched_image(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (bootable/recovery/tests/component/imgdiff_test.cpp:96)
==36755== by 0x12966C: ImgdiffTest_zip_mode_smoke_trailer_zeros_Test::TestBody() (bootable/recovery/tests/component/imgdiff_test.cpp:295)
==36755== by 0x235EF9: testing::Test::Run() (external/googletest/googletest/src/gtest.cc:2455)
==36755== by 0x236CBF: testing::TestInfo::Run() (external/googletest/googletest/src/gtest.cc:2653)
==36755== by 0x2372D6: testing::TestCase::Run() (external/googletest/googletest/src/gtest.cc:2771)
==36755== by 0x23EEE6: testing::internal::UnitTestImpl::RunAllTests() (external/googletest/googletest/src/gtest.cc:4648)
==36755== by 0x23EB45: testing::UnitTest::Run() (external/googletest/googletest/src/gtest.cc:2455)
std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd);
Only the internally allocated buffers inside 'strm' would be free'd by
deflateEnd(), but not 'strm' itself.
This CL fixes the issue by moving 'strm' to stack variable. Note that we
only need to call deflateEnd() on successful return of deflateInit2().
Test: recovery_host_test && recovery_component_test
Change-Id: I39b9bdf62376b8029f95cab82c8542bfcb874009
2017-10-24 21:25:41 +02:00
|
|
|
deflateEnd(&strm);
|
|
|
|
|
|
|
|
if (bspatch_result != 0) {
|
2017-05-18 07:41:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != Z_STREAM_END) {
|
|
|
|
LOG(ERROR) << "ret is expected to be Z_STREAM_END, but it's " << ret;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_target_length != actual_target_length) {
|
|
|
|
LOG(ERROR) << "target length is expected to be " << expected_target_length << ", but it's "
|
|
|
|
<< actual_target_length;
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-03 23:46:15 +02:00
|
|
|
LOG(DEBUG) << "bspatch wrote " << total_written << " bytes in total to streaming output.";
|
2017-05-18 07:41:55 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:12:48 +02:00
|
|
|
int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data,
|
2017-02-01 19:20:10 +01:00
|
|
|
size_t patch_size, SinkFn sink) {
|
2018-06-20 00:56:49 +02:00
|
|
|
Value patch(Value::Type::BLOB,
|
|
|
|
std::string(reinterpret_cast<const char*>(patch_data), patch_size));
|
2018-04-20 06:02:13 +02:00
|
|
|
return ApplyImagePatch(old_data, old_size, patch, sink, nullptr);
|
2016-01-22 13:49:07 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:49:53 +01:00
|
|
|
int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
|
2018-04-20 06:02:13 +02:00
|
|
|
const Value* bonus_data) {
|
2017-11-10 20:49:53 +01:00
|
|
|
if (patch.data.size() < 12) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("patch too short to contain header\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-10 20:49:53 +01:00
|
|
|
// IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. (IMGDIFF1, which is no longer
|
|
|
|
// supported, used CHUNK_NORMAL and CHUNK_GZIP.)
|
|
|
|
const char* const patch_header = patch.data.data();
|
|
|
|
if (memcmp(patch_header, "IMGDIFF2", 8) != 0) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("corrupt patch file header (magic number)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-10 20:49:53 +01:00
|
|
|
int num_chunks = Read4(patch_header + 8);
|
|
|
|
size_t pos = 12;
|
2016-12-15 19:15:06 +01:00
|
|
|
for (int i = 0; i < num_chunks; ++i) {
|
|
|
|
// each chunk's header record starts with 4 bytes.
|
2017-11-10 20:49:53 +01:00
|
|
|
if (pos + 4 > patch.data.size()) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to read chunk %d record\n", i);
|
|
|
|
return -1;
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2017-11-10 20:49:53 +01:00
|
|
|
int type = Read4(patch_header + pos);
|
2016-12-15 19:15:06 +01:00
|
|
|
pos += 4;
|
|
|
|
|
|
|
|
if (type == CHUNK_NORMAL) {
|
2017-11-10 20:49:53 +01:00
|
|
|
const char* normal_header = patch_header + pos;
|
2016-12-15 19:15:06 +01:00
|
|
|
pos += 24;
|
2017-11-10 20:49:53 +01:00
|
|
|
if (pos > patch.data.size()) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to read chunk %d normal header data\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-07 23:44:14 +01:00
|
|
|
size_t src_start = static_cast<size_t>(Read8(normal_header));
|
|
|
|
size_t src_len = static_cast<size_t>(Read8(normal_header + 8));
|
|
|
|
size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16));
|
2016-12-15 19:15:06 +01:00
|
|
|
|
2017-03-28 00:12:48 +02:00
|
|
|
if (src_start + src_len > old_size) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("source data too short\n");
|
2010-02-18 01:11:44 +01:00
|
|
|
return -1;
|
2016-12-15 19:15:06 +01:00
|
|
|
}
|
2018-04-20 06:02:13 +02:00
|
|
|
if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink) != 0) {
|
2017-04-26 04:40:45 +02:00
|
|
|
printf("Failed to apply bsdiff patch.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-04-24 02:51:45 +02:00
|
|
|
|
|
|
|
LOG(DEBUG) << "Processed chunk type normal";
|
2016-12-15 19:15:06 +01:00
|
|
|
} else if (type == CHUNK_RAW) {
|
2017-11-10 20:49:53 +01:00
|
|
|
const char* raw_header = patch_header + pos;
|
2016-12-15 19:15:06 +01:00
|
|
|
pos += 4;
|
2017-11-10 20:49:53 +01:00
|
|
|
if (pos > patch.data.size()) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to read chunk %d raw header data\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-28 00:12:48 +02:00
|
|
|
size_t data_len = static_cast<size_t>(Read4(raw_header));
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-11-10 20:49:53 +01:00
|
|
|
if (pos + data_len > patch.data.size()) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to read chunk %d raw data\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-10 20:49:53 +01:00
|
|
|
if (sink(reinterpret_cast<const unsigned char*>(patch_header + pos), data_len) != data_len) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to write chunk %d raw data\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pos += data_len;
|
2018-04-24 02:51:45 +02:00
|
|
|
|
|
|
|
LOG(DEBUG) << "Processed chunk type raw";
|
2016-12-15 19:15:06 +01:00
|
|
|
} else if (type == CHUNK_DEFLATE) {
|
|
|
|
// deflate chunks have an additional 60 bytes in their chunk header.
|
2017-11-10 20:49:53 +01:00
|
|
|
const char* deflate_header = patch_header + pos;
|
2016-12-15 19:15:06 +01:00
|
|
|
pos += 60;
|
2017-11-10 20:49:53 +01:00
|
|
|
if (pos > patch.data.size()) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("failed to read chunk %d deflate header data\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 23:44:14 +01:00
|
|
|
size_t src_start = static_cast<size_t>(Read8(deflate_header));
|
|
|
|
size_t src_len = static_cast<size_t>(Read8(deflate_header + 8));
|
|
|
|
size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16));
|
|
|
|
size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24));
|
2016-12-15 19:15:06 +01:00
|
|
|
|
2017-03-28 00:12:48 +02:00
|
|
|
if (src_start + src_len > old_size) {
|
2016-12-15 19:15:06 +01:00
|
|
|
printf("source data too short\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decompress the source data; the chunk header tells us exactly
|
|
|
|
// how big we expect it to be when decompressed.
|
|
|
|
|
2018-06-20 00:56:49 +02:00
|
|
|
// Note: expanded_len will include the bonus data size if the patch was constructed with
|
|
|
|
// bonus data. The deflation will come up 'bonus_size' bytes short; these must be appended
|
|
|
|
// from the bonus_data value.
|
|
|
|
size_t bonus_size = (i == 1 && bonus_data != nullptr) ? bonus_data->data.size() : 0;
|
2016-12-15 19:15:06 +01:00
|
|
|
|
|
|
|
std::vector<unsigned char> expanded_source(expanded_len);
|
|
|
|
|
|
|
|
// inflate() doesn't like strm.next_out being a nullptr even with
|
|
|
|
// avail_out being zero (Z_STREAM_ERROR).
|
|
|
|
if (expanded_len != 0) {
|
|
|
|
z_stream strm;
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
strm.avail_in = src_len;
|
2017-01-19 19:46:39 +01:00
|
|
|
strm.next_in = old_data + src_start;
|
2016-12-15 19:15:06 +01:00
|
|
|
strm.avail_out = expanded_len;
|
|
|
|
strm.next_out = expanded_source.data();
|
|
|
|
|
|
|
|
int ret = inflateInit2(&strm, -15);
|
|
|
|
if (ret != Z_OK) {
|
|
|
|
printf("failed to init source inflation: %d\n", ret);
|
|
|
|
return -1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2016-12-15 19:15:06 +01:00
|
|
|
|
|
|
|
// Because we've provided enough room to accommodate the output
|
|
|
|
// data, we expect one call to inflate() to suffice.
|
|
|
|
ret = inflate(&strm, Z_SYNC_FLUSH);
|
|
|
|
if (ret != Z_STREAM_END) {
|
|
|
|
printf("source inflation returned %d\n", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// We should have filled the output buffer exactly, except
|
|
|
|
// for the bonus_size.
|
|
|
|
if (strm.avail_out != bonus_size) {
|
|
|
|
printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size);
|
|
|
|
return -1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2016-12-15 19:15:06 +01:00
|
|
|
inflateEnd(&strm);
|
|
|
|
|
|
|
|
if (bonus_size) {
|
2018-06-20 00:56:49 +02:00
|
|
|
memcpy(expanded_source.data() + (expanded_len - bonus_size), bonus_data->data.data(),
|
2016-12-15 19:15:06 +01:00
|
|
|
bonus_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 07:41:55 +02:00
|
|
|
if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch,
|
2018-04-20 06:02:13 +02:00
|
|
|
patch_offset, deflate_header, sink)) {
|
2017-05-18 07:41:55 +02:00
|
|
|
LOG(ERROR) << "Fail to apply streaming bspatch.";
|
2016-12-15 19:15:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-24 02:51:45 +02:00
|
|
|
LOG(DEBUG) << "Processed chunk type deflate";
|
2016-12-15 19:15:06 +01:00
|
|
|
} else {
|
|
|
|
printf("patch chunk %d is unknown type %d\n", i, type);
|
|
|
|
return -1;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2016-12-15 19:15:06 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-12-15 19:15:06 +01:00
|
|
|
return 0;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|