Use random data for benchmark instead of zeros.

If we always write zeros, we're leaving a giant pile of known
plaintext at an almost deterministic location on newly formatted
volumes.  To avoid this, repeat a 64K chunk of random data.

Bug: 22816936
Change-Id: Iedc067a519bd676a93b9d74ea4f9f77c84c8461c
This commit is contained in:
Jeff Sharkey 2015-07-29 09:14:21 -07:00
parent 20642ae71a
commit d46687ee5d
2 changed files with 28 additions and 30 deletions

View file

@ -31,6 +31,8 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <Utils.h>
namespace android { namespace android {
namespace vold { namespace vold {
@ -4024,35 +4026,32 @@ free(buf);
return 0; return 0;
} }
static status_t CreateFile(const char* name, size_t len) { static status_t CreateFile(const char* name, int len) {
status_t res = -1; int chunk = std::min(len, 65536);
int in = -1;
int out = -1; int out = -1;
std::string buf;
if ((in = TEMP_FAILURE_RETRY(open("/dev/zero", O_RDONLY))) < 0) { if (android::vold::ReadRandomBytes(chunk, buf) != OK) {
PLOG(ERROR) << "Failed to open"; LOG(ERROR) << "Failed to read random data";
goto done; return -EIO;
} }
if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) { if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
PLOG(ERROR) << "Failed to open " << name; PLOG(ERROR) << "Failed to open " << name;
goto done; return -errno;
} }
char buf[65536];
while (len > 0) { while (len > 0) {
int n = read(in, buf, std::min(len, sizeof(buf))); int n = write(out, buf.c_str(), std::min(len, chunk));
if (write(out, buf, n) != n) { if (n < 0) {
PLOG(ERROR) << "Failed to write"; PLOG(ERROR) << "Failed to write";
goto done; close(out);
return -errno;
} }
len -= n; len -= n;
} }
res = 0;
done:
close(in);
close(out); close(out);
return res; return OK;
} }
static status_t BenchmarkCreate() { static status_t BenchmarkCreate() {

View file

@ -159,6 +159,8 @@ with open("BenchmarkGen.h", 'w') as bench:
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <Utils.h>
namespace android { namespace android {
namespace vold { namespace vold {
@ -262,35 +264,32 @@ free(buf);
return 0; return 0;
} }
static status_t CreateFile(const char* name, size_t len) { static status_t CreateFile(const char* name, int len) {
status_t res = -1; int chunk = std::min(len, 65536);
int in = -1;
int out = -1; int out = -1;
std::string buf;
if ((in = TEMP_FAILURE_RETRY(open("/dev/zero", O_RDONLY))) < 0) { if (android::vold::ReadRandomBytes(chunk, buf) != OK) {
PLOG(ERROR) << "Failed to open"; LOG(ERROR) << "Failed to read random data";
goto done; return -EIO;
} }
if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) { if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
PLOG(ERROR) << "Failed to open " << name; PLOG(ERROR) << "Failed to open " << name;
goto done; return -errno;
} }
char buf[65536];
while (len > 0) { while (len > 0) {
int n = read(in, buf, std::min(len, sizeof(buf))); int n = write(out, buf.c_str(), std::min(len, chunk));
if (write(out, buf, n) != n) { if (n < 0) {
PLOG(ERROR) << "Failed to write"; PLOG(ERROR) << "Failed to write";
goto done; close(out);
return -errno;
} }
len -= n; len -= n;
} }
res = 0;
done:
close(in);
close(out); close(out);
return res; return OK;
} }
static status_t BenchmarkCreate() { static status_t BenchmarkCreate() {