This source file includes following definitions.
- FuzzExe
- FuzzByte
- FuzzBits
- TryAssemble
- TEST_F
- main
#include "base/test/test_suite.h"
#include "courgette/base_test_unittest.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"
class DecodeFuzzTest : public BaseTest {
public:
void FuzzExe(const char *) const;
private:
void FuzzByte(const std::string& buffer, const std::string& output,
size_t index) const;
void FuzzBits(const std::string& buffer, const std::string& output,
size_t index, int bits_to_flip) const;
bool TryAssemble(const std::string& buffer, std::string* output) const;
};
void DecodeFuzzTest::FuzzExe(const char* file_name) const {
std::string file1 = FileContents(file_name);
const void* original_buffer = file1.c_str();
size_t original_length = file1.length();
courgette::AssemblyProgram* program = NULL;
const courgette::Status parse_status =
courgette::ParseDetectedExecutable(original_buffer, original_length,
&program);
EXPECT_EQ(courgette::C_OK, parse_status);
courgette::EncodedProgram* encoded = NULL;
const courgette::Status encode_status = Encode(program, &encoded);
EXPECT_EQ(courgette::C_OK, encode_status);
DeleteAssemblyProgram(program);
courgette::SinkStreamSet sinks;
const courgette::Status write_status = WriteEncodedProgram(encoded, &sinks);
EXPECT_EQ(courgette::C_OK, write_status);
DeleteEncodedProgram(encoded);
courgette::SinkStream sink;
bool can_collect = sinks.CopyTo(&sink);
EXPECT_TRUE(can_collect);
size_t length = sink.Length();
std::string base_buffer(reinterpret_cast<const char*>(sink.Buffer()), length);
std::string base_output;
bool ok = TryAssemble(base_buffer, &base_output);
EXPECT_TRUE(ok);
size_t position = 0;
for ( ; position < 100 && position < length; position += 1) {
FuzzByte(base_buffer, base_output, position);
}
for ( ; position < length; position += 900) {
FuzzByte(base_buffer, base_output, position);
}
}
void DecodeFuzzTest::FuzzByte(const std::string& base_buffer,
const std::string& base_output,
size_t index) const {
printf("Fuzzing position %d\n", static_cast<int>(index));
FuzzBits(base_buffer, base_output, index, 0xFF);
FuzzBits(base_buffer, base_output, index, 0x7F);
FuzzBits(base_buffer, base_output, index, 0x80);
FuzzBits(base_buffer, base_output, index, 0x40);
FuzzBits(base_buffer, base_output, index, 0x20);
FuzzBits(base_buffer, base_output, index, 0x10);
FuzzBits(base_buffer, base_output, index, 0x08);
FuzzBits(base_buffer, base_output, index, 0x04);
FuzzBits(base_buffer, base_output, index, 0x02);
FuzzBits(base_buffer, base_output, index, 0x01);
}
void DecodeFuzzTest::FuzzBits(const std::string& base_buffer,
const std::string& base_output,
size_t index, int bits_to_flip) const {
std::string modified_buffer = base_buffer;
std::string modified_output;
modified_buffer[index] ^= bits_to_flip;
bool ok = TryAssemble(modified_buffer, &modified_output);
if (ok) {
size_t base_length = base_output.length();
size_t modified_length = modified_output.length();
ptrdiff_t diff = base_length - modified_length;
if (diff < -200 || diff > 200) {
EXPECT_EQ(base_length, modified_length);
}
size_t changed_byte_count = 0;
for (size_t i = 0; i < base_length && i < modified_length; ++i) {
changed_byte_count += (base_output[i] != modified_output[i]);
}
if (index > 60) {
EXPECT_NE(0U, changed_byte_count);
}
EXPECT_GE(45000U, changed_byte_count);
}
}
bool DecodeFuzzTest::TryAssemble(const std::string& buffer,
std::string* output) const {
courgette::EncodedProgram *encoded = NULL;
bool result = false;
courgette::SourceStreamSet sources;
bool can_get_source_streams = sources.Init(buffer.c_str(), buffer.length());
if (can_get_source_streams) {
const courgette::Status read_status =
ReadEncodedProgram(&sources, &encoded);
if (read_status == courgette::C_OK) {
courgette::SinkStream assembled;
const courgette::Status assemble_status = Assemble(encoded, &assembled);
if (assemble_status == courgette::C_OK) {
const void* assembled_buffer = assembled.Buffer();
size_t assembled_length = assembled.Length();
output->clear();
output->assign(reinterpret_cast<const char*>(assembled_buffer),
assembled_length);
result = true;
}
}
}
DeleteEncodedProgram(encoded);
return result;
}
TEST_F(DecodeFuzzTest, All) {
FuzzExe("setup1.exe");
FuzzExe("elf-32-1.exe");
}
int main(int argc, char** argv) {
return base::TestSuite(argc, argv).Run();
}