16 Commits
Author SHA1 Message Date
Kelvin Zhang 384c435c87 Expose patch reader for consumption
Test: treehugger
Change-Id: I032e01c62ca7f188f02c584af20ebb026a5334dd
2021-06-14 15:47:51 -04:00
Amin Hassani 6e40d93475 bspatch_fuzzer: guard againts integer overflow with bad patch
oldpos is a signed integer and an invalid input can cause integer
overflow. This CL makes sure the interger overflow doesn't happen.

The error message was:
../../../../../../../tmp/portage/dev-util/bsdiff-9999/work/bsdiff-9999/platform2/bsdiff/bspatch.cc:366:12: runtime error: signed integer overflow: 251 + 9223372036854775807 cannot be represented in type 'long'

Bug: crbug.com/950591
Test: cros_fuzz --board=amd64-generic reproduce --fuzzer bspatch_fuzzer --testcase ~/trunk/clusterfuzz-testcase-bspatch_fuzzer-5689939906920448 --package bsdiff --build-type ubsan

Change-Id: If1253483bc073cfb08867b531121d835078544bb
2019-06-14 11:42:22 -07:00
Amin Hassani 1106bf7d9c bsdiff: Change all stderr and perror to LOG(ERROR)
We need to be able to turn off the logging at runtime. When performing fuzz
testing, the input data is mostly invalid. But the invalid input data causes all
the error logs in the output which is noisy, inefficient, and not
necessary. More information can be found in:
https://chromium.googlesource.com/chromium/src/+/master/testing/libfuzzer/getting_started.md#disable-noisy-error-message-logging

One option to turn off the logging at runtime is:

std::cerr.setstate(std::ios_base::failbit);

which works but not when we are doing fprintf to stderr. Another option
is in the lines of the following:

freopen("/dev/null", "w", stderr);

But, this seem to disable everything even useful needed logs.

This patch, changes all codes (except in bsdiff_main.cc) that logs using
fprintf() and perror() to the new uniform form of logging (defined in
logging.h). This new logging alternative allows universal manipulation of the
logging if necessary.

Bug: none
Test: unittests pass

Change-Id: I42bb3e5856c5c215dd82925ccdca44556124655d
2017-12-04 14:41:49 -08:00
Alex Deymo ea72d9f624 Add/remove includes to match used types.
uint64_t and similar types are defined in stdint.h, and size_t is
defined in stddef.h. While these headers are included virtually
everywhere anyways we should include what we use, specially in public
headers.

Bug: None
Test: still compiles.

Change-Id: I26846be960bf9c989372a05e9c4369caf02888cd
2017-10-27 13:52:40 +02:00
Tianjie Xu 6528812247 Add an interface for bspatch reader
Add a wrapper class to separate the patch read from data stream
decompression. Therefore, bspatch will be able to process the patch
that is compressed with various tools.

Test: unittest pass
Change-Id: I5214e0451bde80366e8a70b960703afb2b2a7d97
2017-10-26 14:50:34 -07:00
Alex Deymo dcd423bf89 Add the "bsdiff/" prefix to all include paths.
This prevents accidentally including a header file from another project
that happens to have the same name.

Test: `make checkbuild`; emerge-${BOARD} bsdiff
Bug: None

Change-Id: I8af132ed388738c30a8e3d7434de70b1e9d2f924
2017-09-25 10:56:24 +02:00
Alex Deymo ddf9db5293 Update Makefile to build shared libraries.
The Makefile didn't build libbspatch and libbsdiff. This patch includes
those libraries in the Makefile and moves the header files exposed by
those to an include/bsdiff subdirectory following the Android
convention so calling program can include "bsdiff/bspatch.h" avoiding
any collision with other libraries.

The Android.mk is updated to export the new include path while keeping
the old style until calling programs are updated.

The new Makefile allows Chromium OS to build the new libraries.

Bug: None
Test: FEATURES=test emerge-link bsdiff; sudo emerge bsdiff
Test: mmma external/bsdiff

Change-Id: I2ea9251eb224e9418fe236e4c4c9c5621853f590
2017-05-03 18:29:10 -07:00
Sen Jiang 62d5e48122 Add another overload of bspatch().
It accepts old and new filenames with extents and patch data and size.

Bug: 26982501
Test: mma
Change-Id: I812fe7441fe2ae768bbfc48c65c097af991c105b
2017-02-02 14:54:51 -08:00
Sen Jiang b552c79a0d bspatch: convert all err() and errx() calls to returns.
bspatch is used as a library now, it can't just exit on errors.

Bug: 29058789
Test: mma
Change-Id: I226574a534027d9dc8e5801bc71104dc6d1a410c
2017-02-02 14:54:36 -08:00
Sen Jiang 716d569273 Provide interface for in memory bspatch.
Renamed existing MemoryFile to BufferFile, and added a read only
MemoryFile to provide file interface to read file data already in
memory without extra copy (extra memory usage).

Added write only SinkFile to provide sink function support used in
applypatch. It will enable imgpatch the ability to stream output.

Test: mma
Bug: 26982501

Change-Id: I025ce4064c9f46623dd73f24d30e21e88f08a292
2017-02-02 14:42:45 -08:00
Sen Jiang b14bb55294 Fix infinite loop when using extents.
Clearing |buffer_| still actually makes sense because MemoryFile::Close()
is called twice: once explicitly from bspatch to check if Close()
succeeded, once from destructor to ensure that everything is closed. And
if we don't clear |buffer_|, on the second Close(), it will try to write
|buffer_| to |file_| again, and if |file_| is an ExtentsFile, it will
simply return true and set |bytes_written| to 0 on Write() because all
the extents are already written, now WriteAll() became an infinite
loop.
Although returning false if written is 0 in WriteAll() will fix this, but
I think we should not write the same buffer again in the first place.

Test: call bspatch with extents
Bug: 25773600

Change-Id: If3bddcc6e8ca6751c422c066e5b8b02f91086ed5
2016-04-11 16:29:28 -07:00
Sen Jiang 5b372b684a Stream the output to disk.
Now that update_engine no longer uses minor version 1, old file and
new file are always different in the device, so we can write the
output to disk right away to save memory.
The old behavior is still kept if the new file is overlapping with the
old file for paycheck.py usage.

Test: bsdiff_unittest
Test: apply a 3M patch overwriting old file, uses 25M memory.
Test: apply a 3M patch not overwriting old file, uses 9M memory.
Bug: 25773600

Change-Id: Ic289c9bcc6f1810d0f222a9a4241c05756084b1c
2016-04-11 13:44:15 -07:00
Sen Jiang d87c83566c Various fixes in bspatch and File class.
bspatch should add diff block and old file.

Fix crash on free(new_buf).

open() in bionic needs mode to be set if O_CREAT.

Fix typo in Seek().

Wrong GetSize() for regular file.

Bug: 24478450
Test: Delta update passes filesystem verification on target partition.

Change-Id: Ie53b7cf2b7467fb2cf78762d41e7f65674afa969
2015-11-24 10:58:07 -08:00
Alex Deymo 437b7afd23 bspatch: Re-enable extents support.
With the implementation of the ExtentsFile, we can now use the
FileInterface from bspatch to read and write files. This patch
re-enables the extent argument support and adds unittests for the
argument parsing.

Bug: 24478450
Test: make all test && ./unittest

Change-Id: If6837f250df10c24d3ff96f7ad8f0e0d98126788
2015-10-23 20:09:15 -07:00
Alex Deymo b870eb5805 bspatch: Ran clang-format on bspatch.cc.
This patch attempts to uniform the format on bspatch before we land
further changes there.

Bug: 24478450
Test: make all test && ./unittest

Change-Id: I45ec3d45ab4b081bddef1ac7f5df35ac7e6e554c
2015-10-23 20:09:15 -07:00
Alex Deymo 20891f9c24 Convert C sources to C++.
To help refactoring the FILE* access behind an interface and better
integrate with unittest code, this patch moves the C sources to C++.
Minor changes to make this conform to C++ were included.

Bug: 24478450
Test: `make all test && ./unittests`; mm

Change-Id: I27d7fb0660efc18ea0cb84ff544c9dd9c4803050
2015-10-13 02:15:02 -07:00