Skip to content

Commit 6a0d283

Browse files
author
Jonathan Beaudoin
committed
Finished OS X memory reading/writing
1 parent 96059b2 commit 6a0d283

1 file changed

Lines changed: 78 additions & 43 deletions

File tree

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,103 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Jonathan Beaudoin
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
package com.beaudoin.jmm.process.impl.mac;
226

27+
import com.beaudoin.jmm.misc.Cacheable;
328
import com.beaudoin.jmm.misc.MemoryBuffer;
29+
import com.beaudoin.jmm.natives.mac.mac;
30+
import com.beaudoin.jmm.natives.unix.unix;
431
import com.beaudoin.jmm.process.Module;
532
import com.beaudoin.jmm.process.NativeProcess;
633
import com.sun.jna.Pointer;
734

835
import java.util.HashMap;
936
import java.util.Map;
1037

38+
import static com.beaudoin.jmm.misc.Cacheable.INT_BY_REF;
39+
1140
/**
1241
* Created by Jonathan on 1/10/2016.
1342
*/
1443
public final class MacProcess implements NativeProcess {
1544

16-
private final int id;
17-
private final int task;
18-
private Map<String, Module> modules = new HashMap<>();
19-
20-
public MacProcess(int id, int mach_task) {
21-
this.id = id;
22-
this.task = mach_task;
23-
initModules();
24-
}
45+
private final int id;
46+
private final int task;
47+
private Map<String, Module> modules = new HashMap<>();
2548

26-
public int task() {
27-
return task;
28-
}
49+
public MacProcess(int id, int mach_task) {
50+
this.id = id;
51+
this.task = mach_task;
52+
initModules();
53+
}
2954

30-
@Override
31-
public int id() {
32-
return id;
33-
}
55+
public int task() {
56+
return task;
57+
}
3458

35-
@Override
36-
public void initModules() {
37-
//TODO
38-
}
59+
@Override
60+
public int id() {
61+
return id;
62+
}
3963

40-
@Override
41-
public Module findModule(String moduleName) {
42-
//TODO
43-
return null;
44-
}
64+
@Override
65+
public void initModules() {
66+
//TODO
67+
}
4568

46-
@Override
47-
public MemoryBuffer read(Pointer address, int size) {
69+
@Override
70+
public Module findModule(String moduleName) {
71+
//TODO
72+
return null;
73+
}
4874

49-
return null;
50-
}
75+
@Override
76+
public MemoryBuffer read(Pointer address, int size) {
77+
MemoryBuffer buffer = Cacheable.buffer(size);
78+
if (mac.vm_read(task(), address, size, buffer, INT_BY_REF) != 0 || INT_BY_REF.getValue() != size) {
79+
throw new RuntimeException("Read memory failed at address " + Pointer.nativeValue(address) + " size " + size);
80+
}
81+
Pointer.nativeValue(buffer, Pointer.nativeValue(buffer.getPointer(0)));
82+
return buffer;
83+
}
5184

52-
@Override
53-
public NativeProcess write(Pointer address, MemoryBuffer buffer) {
54-
//TODO
55-
return null;
56-
}
85+
@Override
86+
public NativeProcess write(Pointer address, MemoryBuffer buffer) {
87+
if (mac.vm_write(task(), address, buffer, buffer.size()) != 0) {
88+
throw new RuntimeException("Write memory failed at address " + Pointer.nativeValue(address) + " size " + buffer.size());
89+
}
90+
return this;
91+
}
5792

58-
@Override
59-
public boolean canRead(Pointer address, int size) {
60-
try {
61-
read(address, size);
62-
return true;
63-
} catch (Exception e) {
64-
return false;
65-
}
66-
}
93+
@Override
94+
public boolean canRead(Pointer address, int size) {
95+
try {
96+
read(address, size);
97+
return true;
98+
} catch (Exception e) {
99+
return false;
100+
}
101+
}
67102

68103
}

0 commit comments

Comments
 (0)