1*700637cbSDimitry Andric //===------ InProcessMemoryAccess.cpp - Direct, in-process mem access -----===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric
9*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/InProcessMemoryAccess.h"
10*700637cbSDimitry Andric
11*700637cbSDimitry Andric #define DEBUG_TYPE "orc"
12*700637cbSDimitry Andric
13*700637cbSDimitry Andric namespace llvm::orc {
14*700637cbSDimitry Andric
15*700637cbSDimitry Andric MemoryAccess::~MemoryAccess() = default;
16*700637cbSDimitry Andric
writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,WriteResultFn OnWriteComplete)17*700637cbSDimitry Andric void InProcessMemoryAccess::writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
18*700637cbSDimitry Andric WriteResultFn OnWriteComplete) {
19*700637cbSDimitry Andric for (auto &W : Ws)
20*700637cbSDimitry Andric *W.Addr.toPtr<uint8_t *>() = W.Value;
21*700637cbSDimitry Andric OnWriteComplete(Error::success());
22*700637cbSDimitry Andric }
23*700637cbSDimitry Andric
writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,WriteResultFn OnWriteComplete)24*700637cbSDimitry Andric void InProcessMemoryAccess::writeUInt16sAsync(
25*700637cbSDimitry Andric ArrayRef<tpctypes::UInt16Write> Ws, WriteResultFn OnWriteComplete) {
26*700637cbSDimitry Andric for (auto &W : Ws)
27*700637cbSDimitry Andric *W.Addr.toPtr<uint16_t *>() = W.Value;
28*700637cbSDimitry Andric OnWriteComplete(Error::success());
29*700637cbSDimitry Andric }
30*700637cbSDimitry Andric
writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,WriteResultFn OnWriteComplete)31*700637cbSDimitry Andric void InProcessMemoryAccess::writeUInt32sAsync(
32*700637cbSDimitry Andric ArrayRef<tpctypes::UInt32Write> Ws, WriteResultFn OnWriteComplete) {
33*700637cbSDimitry Andric for (auto &W : Ws)
34*700637cbSDimitry Andric *W.Addr.toPtr<uint32_t *>() = W.Value;
35*700637cbSDimitry Andric OnWriteComplete(Error::success());
36*700637cbSDimitry Andric }
37*700637cbSDimitry Andric
writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,WriteResultFn OnWriteComplete)38*700637cbSDimitry Andric void InProcessMemoryAccess::writeUInt64sAsync(
39*700637cbSDimitry Andric ArrayRef<tpctypes::UInt64Write> Ws, WriteResultFn OnWriteComplete) {
40*700637cbSDimitry Andric for (auto &W : Ws)
41*700637cbSDimitry Andric *W.Addr.toPtr<uint64_t *>() = W.Value;
42*700637cbSDimitry Andric OnWriteComplete(Error::success());
43*700637cbSDimitry Andric }
44*700637cbSDimitry Andric
writePointersAsync(ArrayRef<tpctypes::PointerWrite> Ws,WriteResultFn OnWriteComplete)45*700637cbSDimitry Andric void InProcessMemoryAccess::writePointersAsync(
46*700637cbSDimitry Andric ArrayRef<tpctypes::PointerWrite> Ws, WriteResultFn OnWriteComplete) {
47*700637cbSDimitry Andric if (IsArch64Bit) {
48*700637cbSDimitry Andric for (auto &W : Ws)
49*700637cbSDimitry Andric *W.Addr.toPtr<uint64_t *>() = W.Value.getValue();
50*700637cbSDimitry Andric } else {
51*700637cbSDimitry Andric for (auto &W : Ws)
52*700637cbSDimitry Andric *W.Addr.toPtr<uint32_t *>() = static_cast<uint32_t>(W.Value.getValue());
53*700637cbSDimitry Andric }
54*700637cbSDimitry Andric
55*700637cbSDimitry Andric OnWriteComplete(Error::success());
56*700637cbSDimitry Andric }
57*700637cbSDimitry Andric
writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,WriteResultFn OnWriteComplete)58*700637cbSDimitry Andric void InProcessMemoryAccess::writeBuffersAsync(
59*700637cbSDimitry Andric ArrayRef<tpctypes::BufferWrite> Ws, WriteResultFn OnWriteComplete) {
60*700637cbSDimitry Andric for (auto &W : Ws)
61*700637cbSDimitry Andric memcpy(W.Addr.toPtr<char *>(), W.Buffer.data(), W.Buffer.size());
62*700637cbSDimitry Andric OnWriteComplete(Error::success());
63*700637cbSDimitry Andric }
64*700637cbSDimitry Andric
readUInt8sAsync(ArrayRef<ExecutorAddr> Rs,OnReadUIntsCompleteFn<uint8_t> OnComplete)65*700637cbSDimitry Andric void InProcessMemoryAccess::readUInt8sAsync(
66*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadUIntsCompleteFn<uint8_t> OnComplete) {
67*700637cbSDimitry Andric ReadUIntsResult<uint8_t> Result;
68*700637cbSDimitry Andric Result.reserve(Rs.size());
69*700637cbSDimitry Andric for (auto &R : Rs)
70*700637cbSDimitry Andric Result.push_back(*R.toPtr<uint8_t *>());
71*700637cbSDimitry Andric OnComplete(std::move(Result));
72*700637cbSDimitry Andric }
73*700637cbSDimitry Andric
readUInt16sAsync(ArrayRef<ExecutorAddr> Rs,OnReadUIntsCompleteFn<uint16_t> OnComplete)74*700637cbSDimitry Andric void InProcessMemoryAccess::readUInt16sAsync(
75*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadUIntsCompleteFn<uint16_t> OnComplete) {
76*700637cbSDimitry Andric ReadUIntsResult<uint16_t> Result;
77*700637cbSDimitry Andric Result.reserve(Rs.size());
78*700637cbSDimitry Andric for (auto &R : Rs)
79*700637cbSDimitry Andric Result.push_back(*R.toPtr<uint16_t *>());
80*700637cbSDimitry Andric OnComplete(std::move(Result));
81*700637cbSDimitry Andric }
82*700637cbSDimitry Andric
readUInt32sAsync(ArrayRef<ExecutorAddr> Rs,OnReadUIntsCompleteFn<uint32_t> OnComplete)83*700637cbSDimitry Andric void InProcessMemoryAccess::readUInt32sAsync(
84*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadUIntsCompleteFn<uint32_t> OnComplete) {
85*700637cbSDimitry Andric ReadUIntsResult<uint32_t> Result;
86*700637cbSDimitry Andric Result.reserve(Rs.size());
87*700637cbSDimitry Andric for (auto &R : Rs)
88*700637cbSDimitry Andric Result.push_back(*R.toPtr<uint32_t *>());
89*700637cbSDimitry Andric OnComplete(std::move(Result));
90*700637cbSDimitry Andric }
91*700637cbSDimitry Andric
readUInt64sAsync(ArrayRef<ExecutorAddr> Rs,OnReadUIntsCompleteFn<uint64_t> OnComplete)92*700637cbSDimitry Andric void InProcessMemoryAccess::readUInt64sAsync(
93*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadUIntsCompleteFn<uint64_t> OnComplete) {
94*700637cbSDimitry Andric ReadUIntsResult<uint64_t> Result;
95*700637cbSDimitry Andric Result.reserve(Rs.size());
96*700637cbSDimitry Andric for (auto &R : Rs)
97*700637cbSDimitry Andric Result.push_back(*R.toPtr<uint64_t *>());
98*700637cbSDimitry Andric OnComplete(std::move(Result));
99*700637cbSDimitry Andric }
100*700637cbSDimitry Andric
readPointersAsync(ArrayRef<ExecutorAddr> Rs,OnReadPointersCompleteFn OnComplete)101*700637cbSDimitry Andric void InProcessMemoryAccess::readPointersAsync(
102*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadPointersCompleteFn OnComplete) {
103*700637cbSDimitry Andric ReadPointersResult Result;
104*700637cbSDimitry Andric Result.reserve(Rs.size());
105*700637cbSDimitry Andric for (auto &R : Rs)
106*700637cbSDimitry Andric Result.push_back(ExecutorAddr::fromPtr(*R.toPtr<void **>()));
107*700637cbSDimitry Andric OnComplete(std::move(Result));
108*700637cbSDimitry Andric }
109*700637cbSDimitry Andric
readBuffersAsync(ArrayRef<ExecutorAddrRange> Rs,OnReadBuffersCompleteFn OnComplete)110*700637cbSDimitry Andric void InProcessMemoryAccess::readBuffersAsync(
111*700637cbSDimitry Andric ArrayRef<ExecutorAddrRange> Rs, OnReadBuffersCompleteFn OnComplete) {
112*700637cbSDimitry Andric ReadBuffersResult Result;
113*700637cbSDimitry Andric Result.reserve(Rs.size());
114*700637cbSDimitry Andric for (auto &R : Rs) {
115*700637cbSDimitry Andric Result.push_back({});
116*700637cbSDimitry Andric Result.back().resize(R.size());
117*700637cbSDimitry Andric memcpy(Result.back().data(), R.Start.toPtr<char *>(), R.size());
118*700637cbSDimitry Andric }
119*700637cbSDimitry Andric OnComplete(std::move(Result));
120*700637cbSDimitry Andric }
121*700637cbSDimitry Andric
readStringsAsync(ArrayRef<ExecutorAddr> Rs,OnReadStringsCompleteFn OnComplete)122*700637cbSDimitry Andric void InProcessMemoryAccess::readStringsAsync(
123*700637cbSDimitry Andric ArrayRef<ExecutorAddr> Rs, OnReadStringsCompleteFn OnComplete) {
124*700637cbSDimitry Andric ReadStringsResult Result;
125*700637cbSDimitry Andric Result.reserve(Rs.size());
126*700637cbSDimitry Andric for (auto &R : Rs) {
127*700637cbSDimitry Andric Result.push_back({});
128*700637cbSDimitry Andric for (auto *P = R.toPtr<char *>(); *P; ++P)
129*700637cbSDimitry Andric Result.back().push_back(*P);
130*700637cbSDimitry Andric }
131*700637cbSDimitry Andric OnComplete(std::move(Result));
132*700637cbSDimitry Andric }
133*700637cbSDimitry Andric
134*700637cbSDimitry Andric } // end namespace llvm::orc
135