fastboot driver: RunAndReadBuffer don't allocate too much mem

Instead of allocating the buffer for the whole
upload (which can be arbitrary number of bytes as
the device determines), read 1 MiB at a time.

Test: pass
Bug: 173654501
Change-Id: Ib601b0341b10b7dccbb429cd21aad86a2d3bfda8
This commit is contained in:
Yifan Hong 2021-02-18 15:15:46 -08:00
parent bbf374dbd7
commit 17d469bd57
3 changed files with 25 additions and 6 deletions

View file

@ -76,6 +76,7 @@ cc_library_host_static {
header_libs: [
"bootimg_headers",
"libstorage_literals_headers",
],
export_header_lib_headers: [
@ -275,6 +276,9 @@ cc_library_host_static {
// Only version the final binaries
use_version_lib: false,
static_libs: ["libbuildversion"],
header_libs: [
"libstorage_literals_headers",
],
generated_headers: ["platform_tools_version"],

View file

@ -45,11 +45,13 @@
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <storage_literals/storage_literals.h>
#include "constants.h"
#include "transport.h"
using android::base::StringPrintf;
using namespace android::storage_literals;
namespace fastboot {
@ -316,12 +318,20 @@ RetCode FastBootDriver::RunAndReadBuffer(
return BAD_DEV_RESP;
}
std::vector<char> data(dsize);
if ((ret = ReadBuffer(data.data(), data.size())) != SUCCESS) {
return ret;
}
if ((ret = write_fn(data.data(), data.size())) != SUCCESS) {
return ret;
const uint64_t total_size = dsize;
const uint64_t buf_size = std::min<uint64_t>(total_size, 1_MiB);
std::vector<char> data(buf_size);
uint64_t current_offset = 0;
while (current_offset < total_size) {
uint64_t remaining = total_size - current_offset;
uint64_t chunk_size = std::min(buf_size, remaining);
if ((ret = ReadBuffer(data.data(), chunk_size)) != SUCCESS) {
return ret;
}
if ((ret = write_fn(data.data(), chunk_size)) != SUCCESS) {
return ret;
}
current_offset += chunk_size;
}
return HandleResponse(response, info);
}

View file

@ -8,4 +8,9 @@ cc_library_headers {
host_supported: true,
recovery_available: true,
export_include_dirs: ["."],
target: {
windows: {
enabled: true,
},
},
}