Move output_stream files out of runtime.

Also move image_test and oat_test that are more writing tests.

Change-Id: I6af1400d8e745bbf87f626ca87dae3e2d85b40f1
diff --git a/compiler/Android.mk b/compiler/Android.mk
index 5caf688..3791946 100644
--- a/compiler/Android.mk
+++ b/compiler/Android.mk
@@ -87,6 +87,7 @@
 	elf_stripper.cc \
 	elf_writer.cc \
 	elf_writer_quick.cc \
+	file_output_stream.cc \
 	image_writer.cc \
 	oat_writer.cc \
 	vector_output_stream.cc
diff --git a/compiler/file_output_stream.cc b/compiler/file_output_stream.cc
new file mode 100644
index 0000000..0e4a294
--- /dev/null
+++ b/compiler/file_output_stream.cc
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "file_output_stream.h"
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "base/unix_file/fd_file.h"
+
+namespace art {
+
+FileOutputStream::FileOutputStream(File* file) : OutputStream(file->GetPath()), file_(file) {}
+
+bool FileOutputStream::WriteFully(const void* buffer, int64_t byte_count) {
+  return file_->WriteFully(buffer, byte_count);
+}
+
+off_t FileOutputStream::Seek(off_t offset, Whence whence) {
+  return lseek(file_->Fd(), offset, static_cast<int>(whence));
+}
+
+}  // namespace art
diff --git a/compiler/file_output_stream.h b/compiler/file_output_stream.h
new file mode 100644
index 0000000..bde9e68
--- /dev/null
+++ b/compiler/file_output_stream.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#ifndef ART_COMPILER_FILE_OUTPUT_STREAM_H_
+#define ART_COMPILER_FILE_OUTPUT_STREAM_H_
+
+#include "output_stream.h"
+
+#include "os.h"
+
+namespace art {
+
+class FileOutputStream : public OutputStream {
+ public:
+  explicit FileOutputStream(File* file);
+
+  virtual ~FileOutputStream() {}
+
+  virtual bool WriteFully(const void* buffer, int64_t byte_count);
+
+  virtual off_t Seek(off_t offset, Whence whence);
+
+ private:
+  File* const file_;
+
+  DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
+};
+
+}  // namespace art
+
+#endif  // ART_COMPILER_FILE_OUTPUT_STREAM_H_
diff --git a/compiler/image_test.cc b/compiler/image_test.cc
new file mode 100644
index 0000000..dcafc19
--- /dev/null
+++ b/compiler/image_test.cc
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2011 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.
+ */
+
+#include <string>
+#include <vector>
+
+#include "common_test.h"
+#include "compiler/elf_fixup.h"
+#include "compiler/image_writer.h"
+#include "compiler/oat_writer.h"
+#include "gc/space/image_space.h"
+#include "image.h"
+#include "signal_catcher.h"
+#include "UniquePtr.h"
+#include "utils.h"
+#include "vector_output_stream.h"
+
+namespace art {
+
+class ImageTest : public CommonTest {
+ protected:
+  virtual void SetUp() {
+    ReserveImageSpace();
+    CommonTest::SetUp();
+  }
+};
+
+TEST_F(ImageTest, WriteRead) {
+  ScratchFile tmp_elf;
+  {
+    {
+      jobject class_loader = NULL;
+      ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+      base::TimingLogger timings("ImageTest::WriteRead", false, false);
+      timings.StartSplit("CompileAll");
+#if defined(ART_USE_PORTABLE_COMPILER)
+      // TODO: we disable this for portable so the test executes in a reasonable amount of time.
+      //       We shouldn't need to do this.
+      runtime_->SetCompilerFilter(Runtime::kInterpretOnly);
+#endif
+      compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
+
+      ScopedObjectAccess soa(Thread::Current());
+      OatWriter oat_writer(class_linker->GetBootClassPath(),
+                           0, 0, "", compiler_driver_.get());
+      bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
+                                                !kIsTargetBuild,
+                                                class_linker->GetBootClassPath(),
+                                                oat_writer,
+                                                tmp_elf.GetFile());
+      ASSERT_TRUE(success);
+    }
+  }
+  // Workound bug that mcld::Linker::emit closes tmp_elf by reopening as tmp_oat.
+  UniquePtr<File> tmp_oat(OS::OpenFileReadWrite(tmp_elf.GetFilename().c_str()));
+  ASSERT_TRUE(tmp_oat.get() != NULL);
+
+  ScratchFile tmp_image;
+  const uintptr_t requested_image_base = ART_BASE_ADDRESS;
+  {
+    ImageWriter writer(*compiler_driver_.get());
+    bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
+                                      tmp_oat->GetPath(), tmp_oat->GetPath());
+    ASSERT_TRUE(success_image);
+    bool success_fixup = ElfFixup::Fixup(tmp_oat.get(), writer.GetOatDataBegin());
+    ASSERT_TRUE(success_fixup);
+  }
+
+  {
+    UniquePtr<File> file(OS::OpenFileForReading(tmp_image.GetFilename().c_str()));
+    ASSERT_TRUE(file.get() != NULL);
+    ImageHeader image_header;
+    file->ReadFully(&image_header, sizeof(image_header));
+    ASSERT_TRUE(image_header.IsValid());
+
+    gc::Heap* heap = Runtime::Current()->GetHeap();
+    ASSERT_EQ(1U, heap->GetContinuousSpaces().size());
+    gc::space::ContinuousSpace* space = heap->GetContinuousSpaces().front();
+    ASSERT_FALSE(space->IsImageSpace());
+    ASSERT_TRUE(space != NULL);
+    ASSERT_TRUE(space->IsDlMallocSpace());
+    ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
+  }
+
+  ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL);
+  CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses());
+
+  // Need to delete the compiler since it has worker threads which are attached to runtime.
+  compiler_driver_.reset();
+
+  // Tear down old runtime before making a new one, clearing out misc state.
+  runtime_.reset();
+  java_lang_dex_file_ = NULL;
+
+  UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
+  ASSERT_TRUE(dex.get() != NULL);
+
+  // Remove the reservation of the memory for use to load the image.
+  UnreserveImageSpace();
+
+  Runtime::Options options;
+  std::string image("-Ximage:");
+  image.append(tmp_image.GetFilename());
+  options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
+
+  if (!Runtime::Create(options, false)) {
+    LOG(FATAL) << "Failed to create runtime";
+    return;
+  }
+  runtime_.reset(Runtime::Current());
+  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
+  // give it away now and then switch to a more managable ScopedObjectAccess.
+  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
+  ScopedObjectAccess soa(Thread::Current());
+  ASSERT_TRUE(runtime_.get() != NULL);
+  class_linker_ = runtime_->GetClassLinker();
+
+  gc::Heap* heap = Runtime::Current()->GetHeap();
+  ASSERT_EQ(2U, heap->GetContinuousSpaces().size());
+  ASSERT_TRUE(heap->GetContinuousSpaces()[0]->IsImageSpace());
+  ASSERT_FALSE(heap->GetContinuousSpaces()[0]->IsDlMallocSpace());
+  ASSERT_FALSE(heap->GetContinuousSpaces()[1]->IsImageSpace());
+  ASSERT_TRUE(heap->GetContinuousSpaces()[1]->IsDlMallocSpace());
+
+  gc::space::ImageSpace* image_space = heap->GetImageSpace();
+  byte* image_begin = image_space->Begin();
+  byte* image_end = image_space->End();
+  CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
+  for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
+    const DexFile::ClassDef& class_def = dex->GetClassDef(i);
+    const char* descriptor = dex->GetClassDescriptor(class_def);
+    mirror::Class* klass = class_linker_->FindSystemClass(descriptor);
+    EXPECT_TRUE(klass != NULL) << descriptor;
+    EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
+    if (image_classes.find(descriptor) != image_classes.end()) {
+      // image classes should be located before the end of the image.
+      EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
+    } else {
+      // non image classes should be in a space after the image.
+      EXPECT_GT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
+    }
+    EXPECT_EQ(*klass->GetRawLockWordAddress(), 0);  // address should have been removed from monitor
+  }
+}
+
+}  // namespace art
diff --git a/compiler/oat_test.cc b/compiler/oat_test.cc
new file mode 100644
index 0000000..74b5da9
--- /dev/null
+++ b/compiler/oat_test.cc
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2011 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.
+ */
+
+#include "compiler/oat_writer.h"
+#include "mirror/art_method-inl.h"
+#include "mirror/class-inl.h"
+#include "mirror/object_array-inl.h"
+#include "mirror/object-inl.h"
+#include "oat_file.h"
+#include "vector_output_stream.h"
+
+#include "common_test.h"
+
+namespace art {
+
+class OatTest : public CommonTest {
+ protected:
+  void CheckMethod(mirror::ArtMethod* method,
+                   const OatFile::OatMethod& oat_method,
+                   const DexFile* dex_file)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    const CompiledMethod* compiled_method =
+        compiler_driver_->GetCompiledMethod(MethodReference(dex_file,
+                                                            method->GetDexMethodIndex()));
+
+    if (compiled_method == NULL) {
+      EXPECT_TRUE(oat_method.GetCode() == NULL) << PrettyMethod(method) << " "
+                                                << oat_method.GetCode();
+#if !defined(ART_USE_PORTABLE_COMPILER)
+      EXPECT_EQ(oat_method.GetFrameSizeInBytes(), static_cast<uint32_t>(kStackAlignment));
+      EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
+      EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
+#endif
+    } else {
+      const void* oat_code = oat_method.GetCode();
+      EXPECT_TRUE(oat_code != NULL) << PrettyMethod(method);
+      uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
+      oat_code = reinterpret_cast<const void*>(oat_code_aligned);
+
+      const std::vector<uint8_t>& code = compiled_method->GetCode();
+      size_t code_size = code.size() * sizeof(code[0]);
+      EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
+          << PrettyMethod(method) << " " << code_size;
+      CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
+#if !defined(ART_USE_PORTABLE_COMPILER)
+      EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
+      EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
+      EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
+#endif
+    }
+  }
+};
+
+TEST_F(OatTest, WriteRead) {
+  const bool compile = false;  // DISABLED_ due to the time to compile libcore
+  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+
+  // TODO: make selectable
+#if defined(ART_USE_PORTABLE_COMPILER)
+  CompilerBackend compiler_backend = kPortable;
+#else
+  CompilerBackend compiler_backend = kQuick;
+#endif
+  InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
+  compiler_driver_.reset(new CompilerDriver(compiler_backend, insn_set, false, NULL, 2, true));
+  jobject class_loader = NULL;
+  if (compile) {
+    base::TimingLogger timings("OatTest::WriteRead", false, false);
+    compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
+  }
+
+  ScopedObjectAccess soa(Thread::Current());
+  ScratchFile tmp;
+  OatWriter oat_writer(class_linker->GetBootClassPath(),
+                       42U,
+                       4096U,
+                       "lue.art",
+                       compiler_driver_.get());
+  bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
+                                            !kIsTargetBuild,
+                                            class_linker->GetBootClassPath(),
+                                            oat_writer,
+                                            tmp.GetFile());
+  ASSERT_TRUE(success);
+
+  if (compile) {  // OatWriter strips the code, regenerate to compare
+    base::TimingLogger timings("CommonTest::WriteRead", false, false);
+    compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
+  }
+  UniquePtr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false));
+  ASSERT_TRUE(oat_file.get() != NULL);
+  const OatHeader& oat_header = oat_file->GetOatHeader();
+  ASSERT_TRUE(oat_header.IsValid());
+  ASSERT_EQ(2U, oat_header.GetDexFileCount());  // core and conscrypt
+  ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
+  ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
+  ASSERT_EQ("lue.art", oat_header.GetImageFileLocation());
+
+  const DexFile* dex_file = java_lang_dex_file_;
+  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation());
+  CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
+  for (size_t i = 0; i < dex_file->NumClassDefs(); i++) {
+    const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
+    const byte* class_data = dex_file->GetClassData(class_def);
+    size_t num_virtual_methods =0;
+    if (class_data != NULL) {
+      ClassDataItemIterator it(*dex_file, class_data);
+      num_virtual_methods = it.NumVirtualMethods();
+    }
+    const char* descriptor = dex_file->GetClassDescriptor(class_def);
+
+    UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(i));
+
+    mirror::Class* klass = class_linker->FindClass(descriptor, NULL);
+
+    size_t method_index = 0;
+    for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
+      CheckMethod(klass->GetDirectMethod(i),
+                  oat_class->GetOatMethod(method_index), dex_file);
+    }
+    for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
+      CheckMethod(klass->GetVirtualMethod(i),
+                  oat_class->GetOatMethod(method_index), dex_file);
+    }
+  }
+}
+
+TEST_F(OatTest, OatHeaderSizeCheck) {
+  // If this test is failing and you have to update these constants,
+  // it is time to update OatHeader::kOatVersion
+  EXPECT_EQ(64U, sizeof(OatHeader));
+  EXPECT_EQ(28U, sizeof(OatMethodOffsets));
+}
+
+TEST_F(OatTest, OatHeaderIsValid) {
+    InstructionSet instruction_set = kX86;
+    std::vector<const DexFile*> dex_files;
+    uint32_t image_file_location_oat_checksum = 0;
+    uint32_t image_file_location_oat_begin = 0;
+    const std::string image_file_location;
+    OatHeader oat_header(instruction_set,
+                         &dex_files,
+                         image_file_location_oat_checksum,
+                         image_file_location_oat_begin,
+                         image_file_location);
+    ASSERT_TRUE(oat_header.IsValid());
+
+    char* magic = const_cast<char*>(oat_header.GetMagic());
+    strcpy(magic, "");  // bad magic
+    ASSERT_FALSE(oat_header.IsValid());
+    strcpy(magic, "oat\n000");  // bad version
+    ASSERT_FALSE(oat_header.IsValid());
+}
+
+}  // namespace art
diff --git a/compiler/output_stream.h b/compiler/output_stream.h
new file mode 100644
index 0000000..112dcfc
--- /dev/null
+++ b/compiler/output_stream.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#ifndef ART_COMPILER_OUTPUT_STREAM_H_
+#define ART_COMPILER_OUTPUT_STREAM_H_
+
+#include <stdint.h>
+
+#include <string>
+
+#include "base/macros.h"
+
+namespace art {
+
+enum Whence {
+  kSeekSet = SEEK_SET,
+  kSeekCurrent = SEEK_CUR,
+  kSeekEnd = SEEK_END,
+};
+
+class OutputStream {
+ public:
+  explicit OutputStream(const std::string& location) : location_(location) {}
+
+  virtual ~OutputStream() {}
+
+  const std::string& GetLocation() const {
+    return location_;
+  }
+
+  virtual bool WriteFully(const void* buffer, int64_t byte_count) = 0;
+
+  virtual off_t Seek(off_t offset, Whence whence) = 0;
+
+ private:
+  const std::string location_;
+
+  DISALLOW_COPY_AND_ASSIGN(OutputStream);
+};
+
+}  // namespace art
+
+#endif  // ART_COMPILER_OUTPUT_STREAM_H_
diff --git a/compiler/output_stream_test.cc b/compiler/output_stream_test.cc
new file mode 100644
index 0000000..d5e9755
--- /dev/null
+++ b/compiler/output_stream_test.cc
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "base/logging.h"
+#include "common_test.h"
+#include "file_output_stream.h"
+#include "vector_output_stream.h"
+
+namespace art {
+
+class OutputStreamTest : public CommonTest {
+ protected:
+  void CheckOffset(off_t expected) {
+    off_t actual = output_stream_->Seek(0, kSeekCurrent);
+    EXPECT_EQ(expected, actual);
+  }
+
+  void SetOutputStream(OutputStream& output_stream) {
+    output_stream_ = &output_stream;
+  }
+
+  void GenerateTestOutput() {
+    EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
+    CheckOffset(3);
+    EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
+    CheckOffset(2);
+    uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+    EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
+    CheckOffset(4);
+    EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
+    CheckOffset(6);
+    EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
+    CheckOffset(10);
+  }
+
+  void CheckTestOutput(const std::vector<uint8_t>& actual) {
+    uint8_t expected[] = {
+        0, 0, 1, 2, 0, 0, 1, 2, 3, 4
+    };
+    EXPECT_EQ(sizeof(expected), actual.size());
+    EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
+  }
+
+  OutputStream* output_stream_;
+};
+
+TEST_F(OutputStreamTest, File) {
+  ScratchFile tmp;
+  FileOutputStream output_stream(tmp.GetFile());
+  SetOutputStream(output_stream);
+  GenerateTestOutput();
+  UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
+  EXPECT_TRUE(in.get() != NULL);
+  std::vector<uint8_t> actual(in->GetLength());
+  bool readSuccess = in->ReadFully(&actual[0], actual.size());
+  EXPECT_TRUE(readSuccess);
+  CheckTestOutput(actual);
+}
+
+TEST_F(OutputStreamTest, Vector) {
+  std::vector<uint8_t> output;
+  VectorOutputStream output_stream("test vector output", output);
+  SetOutputStream(output_stream);
+  GenerateTestOutput();
+  CheckTestOutput(output);
+}
+
+}  // namespace art