2017-07-05 18:23:32 -07:00
|
|
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-09-08 12:49:55 -07:00
|
|
|
#include "snapshot/elf/elf_image_reader.h"
|
2017-07-05 18:23:32 -07:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2017-07-29 17:18:12 -04:00
|
|
|
#include "build/build_config.h"
|
2017-07-05 18:23:32 -07:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "test/multiprocess.h"
|
2018-01-19 14:03:34 -08:00
|
|
|
#include "test/process_type.h"
|
2017-07-05 18:23:32 -07:00
|
|
|
#include "util/file/file_io.h"
|
2017-09-12 16:49:35 -07:00
|
|
|
#include "util/misc/address_types.h"
|
2017-07-05 18:23:32 -07:00
|
|
|
#include "util/misc/from_pointer_cast.h"
|
2018-01-19 14:03:34 -08:00
|
|
|
#include "util/process/process_memory_native.h"
|
|
|
|
|
|
|
|
#if defined(OS_FUCHSIA)
|
|
|
|
|
|
|
|
#include <link.h>
|
|
|
|
#include <zircon/syscalls.h>
|
|
|
|
|
|
|
|
#include "base/fuchsia/fuchsia_logging.h"
|
|
|
|
|
|
|
|
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
|
|
|
|
|
|
|
#include "util/linux/auxiliary_vector.h"
|
|
|
|
#include "util/linux/memory_map.h"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error Port.
|
|
|
|
|
|
|
|
#endif // OS_FUCHSIA
|
2017-07-05 18:23:32 -07:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
__attribute__((visibility("default"))) void
|
|
|
|
ElfImageReaderTestExportedSymbol(){};
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
|
|
|
|
#if defined(OS_FUCHSIA)
|
|
|
|
|
|
|
|
void LocateExecutable(ProcessType process,
|
|
|
|
ProcessMemory* memory,
|
|
|
|
bool is_64_bit,
|
|
|
|
VMAddress* elf_address) {
|
|
|
|
uintptr_t debug_address;
|
|
|
|
zx_status_t status = zx_object_get_property(process,
|
|
|
|
ZX_PROP_PROCESS_DEBUG_ADDR,
|
|
|
|
&debug_address,
|
|
|
|
sizeof(debug_address));
|
|
|
|
ASSERT_EQ(status, ZX_OK)
|
|
|
|
<< "zx_object_get_property: ZX_PROP_PROCESS_DEBUG_ADDR";
|
|
|
|
|
|
|
|
constexpr auto k_r_debug_map_offset = offsetof(r_debug, r_map);
|
|
|
|
uintptr_t map;
|
|
|
|
ASSERT_TRUE(
|
|
|
|
memory->Read(debug_address + k_r_debug_map_offset, sizeof(map), &map))
|
|
|
|
<< "read link_map";
|
|
|
|
|
|
|
|
constexpr auto k_link_map_addr_offset = offsetof(link_map, l_addr);
|
|
|
|
uintptr_t base;
|
|
|
|
ASSERT_TRUE(memory->Read(map + k_link_map_addr_offset, sizeof(base), &base))
|
|
|
|
<< "read base";
|
|
|
|
|
|
|
|
*elf_address = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
|
|
|
|
|
|
|
void LocateExecutable(ProcessType process,
|
|
|
|
ProcessMemory* memory,
|
|
|
|
bool is_64_bit,
|
|
|
|
VMAddress* elf_address) {
|
2017-07-05 18:23:32 -07:00
|
|
|
AuxiliaryVector aux;
|
2018-01-19 14:03:34 -08:00
|
|
|
ASSERT_TRUE(aux.Initialize(process, is_64_bit));
|
2017-07-05 18:23:32 -07:00
|
|
|
|
2017-09-12 16:49:35 -07:00
|
|
|
VMAddress phdrs;
|
2017-07-05 18:23:32 -07:00
|
|
|
ASSERT_TRUE(aux.GetValue(AT_PHDR, &phdrs));
|
|
|
|
|
|
|
|
MemoryMap memory_map;
|
2018-01-19 14:03:34 -08:00
|
|
|
ASSERT_TRUE(memory_map.Initialize(process));
|
2017-07-11 08:59:24 -07:00
|
|
|
const MemoryMap::Mapping* phdr_mapping = memory_map.FindMapping(phdrs);
|
|
|
|
ASSERT_TRUE(phdr_mapping);
|
|
|
|
const MemoryMap::Mapping* exe_mapping =
|
|
|
|
memory_map.FindFileMmapStart(*phdr_mapping);
|
2017-07-05 18:23:32 -07:00
|
|
|
ASSERT_TRUE(exe_mapping);
|
|
|
|
*elf_address = exe_mapping->range.Base();
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
#endif // OS_FUCHSIA
|
|
|
|
|
2017-12-05 11:21:14 -08:00
|
|
|
void ExpectSymbol(ElfImageReader* reader,
|
|
|
|
const std::string& symbol_name,
|
|
|
|
VMAddress expected_symbol_address) {
|
2017-09-12 16:49:35 -07:00
|
|
|
VMAddress symbol_address;
|
|
|
|
VMSize symbol_size;
|
2017-07-05 18:23:32 -07:00
|
|
|
ASSERT_TRUE(
|
2017-12-05 11:21:14 -08:00
|
|
|
reader->GetDynamicSymbol(symbol_name, &symbol_address, &symbol_size));
|
2017-07-05 18:23:32 -07:00
|
|
|
EXPECT_EQ(symbol_address, expected_symbol_address);
|
|
|
|
|
|
|
|
EXPECT_FALSE(
|
2017-12-05 11:21:14 -08:00
|
|
|
reader->GetDynamicSymbol("notasymbol", &symbol_address, &symbol_size));
|
2017-07-05 18:23:32 -07:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
void ReadThisExecutableInTarget(ProcessType process) {
|
2017-07-05 18:23:32 -07:00
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
|
|
|
constexpr bool am_64_bit = true;
|
|
|
|
#else
|
|
|
|
constexpr bool am_64_bit = false;
|
|
|
|
#endif // ARCH_CPU_64_BITS
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
ProcessMemoryNative memory;
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2017-12-05 11:21:14 -08:00
|
|
|
ProcessMemoryRange range;
|
|
|
|
ASSERT_TRUE(range.Initialize(&memory, am_64_bit));
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
VMAddress elf_address;
|
|
|
|
LocateExecutable(process, &memory, am_64_bit, &elf_address);
|
|
|
|
ASSERT_NO_FATAL_FAILURE();
|
|
|
|
|
2017-12-05 11:21:14 -08:00
|
|
|
ElfImageReader reader;
|
|
|
|
ASSERT_TRUE(reader.Initialize(range, elf_address));
|
|
|
|
|
|
|
|
ExpectSymbol(&reader,
|
|
|
|
"ElfImageReaderTestExportedSymbol",
|
|
|
|
FromPointerCast<VMAddress>(ElfImageReaderTestExportedSymbol));
|
|
|
|
|
|
|
|
ElfImageReader::NoteReader::Result result;
|
|
|
|
std::string note_name;
|
|
|
|
std::string note_desc;
|
|
|
|
ElfImageReader::NoteReader::NoteType note_type;
|
|
|
|
|
|
|
|
std::unique_ptr<ElfImageReader::NoteReader> notes = reader.Notes(-1);
|
|
|
|
while ((result = notes->NextNote(¬e_name, ¬e_type, ¬e_desc)) ==
|
|
|
|
ElfImageReader::NoteReader::Result::kSuccess) {
|
|
|
|
}
|
|
|
|
EXPECT_EQ(result, ElfImageReader::NoteReader::Result::kNoMoreNotes);
|
|
|
|
|
|
|
|
notes = reader.Notes(0);
|
|
|
|
EXPECT_EQ(notes->NextNote(¬e_name, ¬e_type, ¬e_desc),
|
|
|
|
ElfImageReader::NoteReader::Result::kNoMoreNotes);
|
|
|
|
|
|
|
|
// Find the note defined in elf_image_reader_test_note.S.
|
|
|
|
constexpr char kCrashpadNoteName[] = "Crashpad";
|
|
|
|
constexpr ElfImageReader::NoteReader::NoteType kCrashpadNoteType = 1;
|
|
|
|
constexpr uint32_t kCrashpadNoteDesc = 42;
|
|
|
|
notes = reader.NotesWithNameAndType(kCrashpadNoteName, kCrashpadNoteType, -1);
|
|
|
|
ASSERT_EQ(notes->NextNote(¬e_name, ¬e_type, ¬e_desc),
|
|
|
|
ElfImageReader::NoteReader::Result::kSuccess);
|
|
|
|
EXPECT_EQ(note_name, kCrashpadNoteName);
|
|
|
|
EXPECT_EQ(note_type, kCrashpadNoteType);
|
|
|
|
EXPECT_EQ(note_desc.size(), sizeof(kCrashpadNoteDesc));
|
|
|
|
EXPECT_EQ(*reinterpret_cast<decltype(kCrashpadNoteDesc)*>(¬e_desc[0]),
|
|
|
|
kCrashpadNoteDesc);
|
|
|
|
|
|
|
|
EXPECT_EQ(notes->NextNote(¬e_name, ¬e_type, ¬e_desc),
|
|
|
|
ElfImageReader::NoteReader::Result::kNoMoreNotes);
|
2017-07-05 18:23:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes that libc is loaded at the same address in this process as in the
|
|
|
|
// target, which it is for the fork test below.
|
2018-01-19 14:03:34 -08:00
|
|
|
void ReadLibcInTarget(ProcessType process) {
|
2017-07-05 18:23:32 -07:00
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
|
|
|
constexpr bool am_64_bit = true;
|
|
|
|
#else
|
|
|
|
constexpr bool am_64_bit = false;
|
|
|
|
#endif // ARCH_CPU_64_BITS
|
|
|
|
|
|
|
|
Dl_info info;
|
|
|
|
ASSERT_TRUE(dladdr(reinterpret_cast<void*>(getpid), &info)) << "dladdr:"
|
|
|
|
<< dlerror();
|
2017-09-12 16:49:35 -07:00
|
|
|
VMAddress elf_address = FromPointerCast<VMAddress>(info.dli_fbase);
|
2017-07-05 18:23:32 -07:00
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
ProcessMemoryNative memory;
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2017-12-05 11:21:14 -08:00
|
|
|
ProcessMemoryRange range;
|
|
|
|
ASSERT_TRUE(range.Initialize(&memory, am_64_bit));
|
|
|
|
|
|
|
|
ElfImageReader reader;
|
|
|
|
ASSERT_TRUE(reader.Initialize(range, elf_address));
|
|
|
|
|
|
|
|
ExpectSymbol(&reader, "getpid", FromPointerCast<VMAddress>(getpid));
|
2017-07-05 18:23:32 -07:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
TEST(ElfImageReader, MainExecutableSelf) {
|
|
|
|
ReadThisExecutableInTarget(GetSelfProcess());
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(OS_FUCHSIA) // TODO(scottmg): Port to MultiprocessExec.
|
2017-07-05 18:23:32 -07:00
|
|
|
class ReadExecutableChildTest : public Multiprocess {
|
|
|
|
public:
|
|
|
|
ReadExecutableChildTest() : Multiprocess() {}
|
|
|
|
~ReadExecutableChildTest() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void MultiprocessParent() { ReadThisExecutableInTarget(ChildPID()); }
|
|
|
|
void MultiprocessChild() { CheckedReadFileAtEOF(ReadPipeHandle()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ElfImageReader, MainExecutableChild) {
|
|
|
|
ReadExecutableChildTest test;
|
|
|
|
test.Run();
|
|
|
|
}
|
2018-01-19 14:03:34 -08:00
|
|
|
#endif // !OS_FUCHSIA
|
2017-07-05 18:23:32 -07:00
|
|
|
|
|
|
|
TEST(ElfImageReader, OneModuleSelf) {
|
2018-01-19 14:03:34 -08:00
|
|
|
ReadLibcInTarget(GetSelfProcess());
|
2017-07-05 18:23:32 -07:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:03:34 -08:00
|
|
|
#if !defined(OS_FUCHSIA) // TODO(scottmg): Port to MultiprocessExec.
|
2017-07-05 18:23:32 -07:00
|
|
|
class ReadLibcChildTest : public Multiprocess {
|
|
|
|
public:
|
|
|
|
ReadLibcChildTest() : Multiprocess() {}
|
|
|
|
~ReadLibcChildTest() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void MultiprocessParent() { ReadLibcInTarget(ChildPID()); }
|
|
|
|
void MultiprocessChild() { CheckedReadFileAtEOF(ReadPipeHandle()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ElfImageReader, OneModuleChild) {
|
|
|
|
ReadLibcChildTest test;
|
|
|
|
test.Run();
|
|
|
|
}
|
2018-01-19 14:03:34 -08:00
|
|
|
#endif // !OS_FUCHSIA
|
2017-07-05 18:23:32 -07:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|