Skip to content

Commit

Permalink
DarwinKit for x86_64: build mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
YungRaj committed Jan 15, 2025
1 parent 6432998 commit e267dc7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 18 deletions.
40 changes: 35 additions & 5 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ load("@build_bazel_rules_apple//apple:macos.bzl", "macos_dynamic_framework")
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_static_framework")

config_setting(
name = "arm64_cpu",
values = {"macos_cpus": "arm64"},
name = "arm64",
values = {
"macos_cpus": "arm64",
},
)

config_setting(
name = "x86_64_cpu",
name = "arm64e",
values = {
"macos_cpus": "arm64e",
},
)

config_setting(
name = "x86_64",
values = {"macos_cpus": "x86_64"},
)

Expand Down Expand Up @@ -158,7 +167,16 @@ cc_library(
deps = [":DarwinKit_user_iokit",],
srcs = glob(["user/*.cc"]) +
glob(["darwinkit/*.cc"]) +
glob(["arm64/*.s"]) +
select({
":arm64": glob([
"arm64/*.s"
]),
":arm64e": glob([
"arm64/*.s"
]),
":x86_64": glob([
# "x86_64/*.s"
])}) +
glob(["arm64/*.cc"]) +
glob(["x86_64/*.cc"]),
hdrs = glob(["user/*.h"]) + glob(["darwinkit/*.h"]) + glob(["arm64/*.h"]) + glob(["x86_64/*.h"]) + glob(["capstone/include/capstone/*.h"]),
Expand Down Expand Up @@ -262,7 +280,19 @@ cc_library(
],
srcs = glob(["kernel/*.cc"]) +
glob(["darwinkit/*.cc"]) +
glob(["arm64/*.s"]) +
select({
":arm64": glob([
"arm64/*.s"
]),
":arm64e": glob([
"arm64/*.s"
]),
":x86_64": glob([
# "x86_64/*.s"
]),
"//conditions:default": glob([
"arm64/*.s"
])}) +
glob(["arm64/*.cc"]) +
glob(["x86_64/*.cc"]),
hdrs = glob(["kernel/*.h"]) + glob(["darwinkit/*.h"]) + glob(["arm64/*.h"]) + glob(["x86_64/*.h"]) + glob(["capstone/include/capstone/*.h"]),
Expand Down
2 changes: 1 addition & 1 deletion kernel/kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ xnu::mach::VmAddress Kernel::VmAllocate(Size size, UInt32 flags, xnu::mach::VmPr

address = 0x1000;

map = read64(GetSymbolAddressByName("_g_kext_map"));
map = Read64(GetSymbolAddressByName("_g_kext_map"));

UInt64 vmEnterArgs[13] = {map,
(UInt64)&address,
Expand Down
8 changes: 5 additions & 3 deletions user/fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ void Harness::GetEntryPointFromKC(xnu::mach::VmAddress kc, xnu::mach::VmAddress*
reinterpret_cast<struct unixthread_command*>(load_command);

DARWIN_KIT_LOG("MacRK::LC_UNIXTHREAD\n");


#ifdef __arm64__
if (thread_command->flavor == ARM_THREAD_STATE64) {
struct arm_thread_state64 {
__uint64_t x[29]; /* General purpose registers x0-x28 */
Expand All @@ -456,6 +457,7 @@ void Harness::GetEntryPointFromKC(xnu::mach::VmAddress kc, xnu::mach::VmAddress*

*entryPoint = state->pc;
}
#endif
}

q += load_command->cmdsize;
Expand Down Expand Up @@ -559,11 +561,11 @@ void Harness::StartKernel() {

GetEntryPointFromKC((xnu::mach::VmAddress)fuzzBinary->base, &entryPoint);

printf("start = 0x%llx\n", entryPoint);

#ifdef __arm64__
hypervisor = new darwin::vm::Hypervisor(
this, (xnu::mach::VmAddress)fuzzBinary->originalBase,
(xnu::mach::VmAddress)fuzzBinary->base, fuzzBinary->size, entryPoint);
#endif
}

void Harness::CallFunctionInKernel(const char* funcname) {
Expand Down
4 changes: 4 additions & 0 deletions user/hypervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef __arm64__

#include "hypervisor.h"

#include "kernel_macho.h"
Expand Down Expand Up @@ -1019,3 +1021,5 @@ void Hypervisor::Destroy() {

}
}

#endif
4 changes: 4 additions & 0 deletions user/hypervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#ifdef __arm64__

#include <assert.h>
#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -186,3 +188,5 @@ class Hypervisor {

} // namespace vm
} // namespace darwin

#endif
4 changes: 4 additions & 0 deletions user/kernel_macho.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ bool KernelMachO::ParseLoadCommands() {
struct unixthread_command* thread_command =
reinterpret_cast<struct unixthread_command*>(load_command);

#ifdef __arm64__
if (thread_command->flavor == ARM_THREAD_STATE64) {
typedef struct arm64_thread_state {
__uint64_t x[29]; /* General purpose registers x0-x28 */
Expand All @@ -314,6 +315,7 @@ bool KernelMachO::ParseLoadCommands() {
DARWIN_KIT_LOG("DarwinKit::LC_UNIXTHREAD\n");
DARWIN_KIT_LOG("DarwinKit::\tEntry Point = 0x%llx\n", state->pc);
}
#endif

break;
}
Expand Down Expand Up @@ -578,6 +580,7 @@ bool KernelCacheMachO::ParseLoadCommands() {

DARWIN_KIT_LOG("DarwinKit::LC_UNIXTHREAD\n");

#ifdef __arm64__
if (thread_command->flavor == ARM_THREAD_STATE64) {
struct arm_thread_state64 {
__uint64_t x[29]; /* General purpose registers x0-x28 */
Expand All @@ -593,6 +596,7 @@ bool KernelCacheMachO::ParseLoadCommands() {

DARWIN_KIT_LOG("DarwinKit::\tstate->pc = 0x%llx\n", state->pc);
}
#endif

break;
}
Expand Down
8 changes: 4 additions & 4 deletions user/objc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,10 @@ Method::Method(ObjC* object, struct _objc_2_method* method) {
#include <arm64/isa_arm64.h>
#include <arm64/patch_finder_arm64.h>

#elif
#elif __x86_64__

#include <x86_64/isa_x86_64.h>
#include <x86_64/patch_Finder_x86_64.h>
#include <x86_64/patch_finder_x86_64.h>

#endif

Expand Down Expand Up @@ -778,9 +778,9 @@ UInt64 FindSelectorsBase(darwin::MachOUserspace* macho) {

UInt64 mov = arch::x86_64::patchfinder::StepBack64(libobjc, add, 0x100, "mov", nullptr);

arch::x86_64::disassemble(mov, arch::x86_64::MaxInstruction, &insn);
// arch::x86_64::Disassemble(mov, arch::x86_64::MaxInstruction, &insn);

UInt64 selectors = insn.detail.x86->operands[1].mem.disp + mov;
// UInt64 selectors = insn.detail.x86->operands[1].mem.disp + mov;

return selectors;

Expand Down
9 changes: 4 additions & 5 deletions user/swift.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#ifdef __arm64__
#include <arm64/isa_arm64.h>
#include <arm64/patch_finder_arm64.h>
#elif
#elif __x86_64__
#include <x86_64/isa_x86_64.h>
#include <x86_64/patch_finder_x86_64.h>
#endif
Expand Down Expand Up @@ -210,16 +210,15 @@ UInt64 SwiftABI::GetTypeMetadata(struct TypeDescriptor* typeDescriptor) {
return typeMetadata;

#elif __x86_64__

using namespace arch::x86_64;

cs_insn insn;

UInt64 mov = arch::x86_64::patchfinder::Step64(macho, accessFunction, add, 0x100, "mov", nullptr);
UInt64 mov = arch::x86_64::patchfinder::Step64(macho, accessFunction, 0x100, "mov", nullptr);

arch::x86_64::disassemble(mov, arch::x86_64::MaxInstruction, &insn);
// arch::x86_64::Disassemble(mov, arch::x86_64::MaxInstruction, &insn);

typeMetadata = insn.detail.x86->operands[1].mem.disp + mov;
// typeMetadata = insn.detail.x86->operands[1].mem.disp + mov;

return typeMetadata;

Expand Down
2 changes: 2 additions & 0 deletions user/virt.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifdef __arm64__
#ifndef __VIRT_H_
#define __VIRT_H_

Expand Down Expand Up @@ -1395,4 +1396,5 @@ static inline void vfp_set_fpcr(CPUARMState *env, uint32_t val)
#define PSTATE_MODE_EL1t 4
#define PSTATE_MODE_EL0t 0

#endif
#endif

0 comments on commit e267dc7

Please sign in to comment.