Skip to content

Commit 4756716

Browse files
muhomorrthestinger
authored andcommitted
memtag_test: move SEGV code checks to device-side binary
1 parent a3bf742 commit 4756716

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

androidtest/memtag/memtag_test.cc

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,54 +204,63 @@ u8* alloc_default() {
204204
}
205205
}
206206

207-
volatile u8 u8_var;
207+
int expected_segv_code;
208+
209+
#define expect_segv(exp, segv_code) ({\
210+
expected_segv_code = segv_code; \
211+
volatile auto val = exp; \
212+
(void) val; \
213+
do_context_switch(); \
214+
fprintf(stderr, "didn't receive SEGV code %i", segv_code); \
215+
exit(1); })
216+
217+
// it's expected that the device is configured to use asymm MTE tag checking mode (sync read checks,
218+
// async write checks)
219+
#define expect_read_segv(exp) expect_segv(exp, SEGV_MTESERR)
220+
#define expect_write_segv(exp) expect_segv(exp, SEGV_MTEAERR)
208221

209222
void read_after_free() {
210223
u8 *p = alloc_default();
211224
free(p);
212-
volatile u8 v = p[0];
213-
(void) v;
225+
expect_read_segv(p[0]);
214226
}
215227

216228
void write_after_free() {
217229
u8 *p = alloc_default();
218230
free(p);
219-
p[0] = 1;
231+
expect_write_segv(p[0] = 1);
220232
}
221233

222234
void underflow_read() {
223235
u8 *p = alloc_default();
224-
volatile u8 v = p[-1];
225-
(void) v;
236+
expect_read_segv(p[-1]);
226237
}
227238

228239
void underflow_write() {
229240
u8 *p = alloc_default();
230-
p[-1] = 1;
241+
expect_write_segv(p[-1] = 1);
231242
}
232243

233244
void overflow_read() {
234245
u8 *p = alloc_default();
235-
volatile u8 v = p[DEFAULT_ALLOC_SIZE + CANARY_SIZE];
236-
(void) v;
246+
expect_read_segv(p[DEFAULT_ALLOC_SIZE + CANARY_SIZE]);
237247
}
238248

239249
void overflow_write() {
240250
u8 *p = alloc_default();
241-
p[DEFAULT_ALLOC_SIZE + CANARY_SIZE] = 1;
251+
expect_write_segv(p[DEFAULT_ALLOC_SIZE + CANARY_SIZE] = 1);
242252
}
243253

244254
void untagged_read() {
245255
u8 *p = alloc_default();
246256
p = (u8 *) untag_pointer(p);
247-
volatile u8 v = p[0];
248-
(void) v;
257+
expect_read_segv(p[0]);
249258
}
250259

251260
void untagged_write() {
252261
u8 *p = alloc_default();
253262
p = (u8 *) untag_pointer(p);
254-
p[0] = 1;
263+
expect_write_segv(p[0] = 1);
255264
}
256265

257266
map<string, function<void()>> tests = {
@@ -269,8 +278,12 @@ map<string, function<void()>> tests = {
269278
};
270279

271280
void segv_handler(int, siginfo_t *si, void *) {
272-
fprintf(stderr, "SEGV_CODE %i", si->si_code);
273-
exit(139); // standard exit code for SIGSEGV
281+
if (expected_segv_code == 0 || expected_segv_code != si->si_code) {
282+
fprintf(stderr, "received unexpected SEGV_CODE %i", si->si_code);
283+
exit(139); // standard exit code for SIGSEGV
284+
}
285+
286+
exit(0);
274287
}
275288

276289
int main(int argc, char **argv) {

androidtest/src/grapheneos/hmalloc/MemtagTest.java

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,89 +7,68 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99

10-
import java.io.IOException;
1110
import java.util.ArrayList;
1211

1312
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.fail;
1513

1614
@RunWith(DeviceJUnit4ClassRunner.class)
1715
public class MemtagTest extends BaseHostJUnit4Test {
18-
1916
private static final String TEST_BINARY = "/data/local/tmp/memtag_test";
2017

21-
enum Result {
22-
SUCCESS(0, ""),
23-
// it's expected that the device is configured to use asymm MTE tag checking mode
24-
ASYNC_MTE_ERROR(139, "SEGV_CODE 8"),
25-
SYNC_MTE_ERROR(139, "SEGV_CODE 9"),
26-
;
27-
28-
public final int exitCode;
29-
public final String stderr;
30-
31-
Result(int exitCode, String stderr) {
32-
this.exitCode = exitCode;
33-
this.stderr = stderr;
34-
}
35-
}
36-
37-
private static final int SEGV_EXIT_CODE = 139;
38-
39-
private void runTest(String name, Result expectedResult) throws DeviceNotAvailableException {
18+
private void runTest(String name) throws DeviceNotAvailableException {
4019
var args = new ArrayList<String>();
4120
args.add(TEST_BINARY);
4221
args.add(name);
4322
String cmdLine = String.join(" ", args);
4423

4524
var result = getDevice().executeShellV2Command(cmdLine);
4625

47-
assertEquals("process exit code", expectedResult.exitCode, result.getExitCode().intValue());
48-
assertEquals("stderr", expectedResult.stderr, result.getStderr());
26+
assertEquals("stderr", "", result.getStderr());
27+
assertEquals("process exit code", 0, result.getExitCode().intValue());
4928
}
5029

5130
@Test
5231
public void tag_distinctness() throws DeviceNotAvailableException {
53-
runTest("tag_distinctness", Result.SUCCESS);
32+
runTest("tag_distinctness");
5433
}
5534

5635
@Test
5736
public void read_after_free() throws DeviceNotAvailableException {
58-
runTest("read_after_free", Result.SYNC_MTE_ERROR);
37+
runTest("read_after_free");
5938
}
6039

6140
@Test
6241
public void write_after_free() throws DeviceNotAvailableException {
63-
runTest("write_after_free", Result.ASYNC_MTE_ERROR);
42+
runTest("write_after_free");
6443
}
6544

6645
@Test
6746
public void underflow_read() throws DeviceNotAvailableException {
68-
runTest("underflow_read", Result.SYNC_MTE_ERROR);
47+
runTest("underflow_read");
6948
}
7049

7150
@Test
7251
public void underflow_write() throws DeviceNotAvailableException {
73-
runTest("underflow_write", Result.ASYNC_MTE_ERROR);
52+
runTest("underflow_write");
7453
}
7554

7655
@Test
7756
public void overflow_read() throws DeviceNotAvailableException {
78-
runTest("overflow_read", Result.SYNC_MTE_ERROR);
57+
runTest("overflow_read");
7958
}
8059

8160
@Test
8261
public void overflow_write() throws DeviceNotAvailableException {
83-
runTest("overflow_write", Result.ASYNC_MTE_ERROR);
62+
runTest("overflow_write");
8463
}
8564

8665
@Test
8766
public void untagged_read() throws DeviceNotAvailableException {
88-
runTest("untagged_read", Result.SYNC_MTE_ERROR);
67+
runTest("untagged_read");
8968
}
9069

9170
@Test
9271
public void untagged_write() throws DeviceNotAvailableException {
93-
runTest("untagged_write", Result.ASYNC_MTE_ERROR);
72+
runTest("untagged_write");
9473
}
9574
}

0 commit comments

Comments
 (0)