Skip to content

Commit 94e0525

Browse files
authored
Merge pull request #158 from struct/separate_tagging_tests
separate and cleanup all the tagged pointer tests
2 parents cc2787e + 90b613d commit 94e0525

8 files changed

Lines changed: 99 additions & 21 deletions

File tree

Makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,6 @@ cpp_library_debug: clean c_library_objects_debug
304304
tests: clean library_debug_unit_tests
305305
@echo "make tests"
306306
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/rand_freelist.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/rand_freelist $(LDFLAGS)
307-
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/tagged_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test $(LDFLAGS)
308-
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/uaf_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf_tag_ptr_test $(LDFLAGS)
309-
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/bad_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/bad_tag_ptr_test $(LDFLAGS)
310-
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/verify_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/verify_tag_ptr_test $(LDFLAGS)
311307
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tests $(LDFLAGS)
312308
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/uaf.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf $(LDFLAGS)
313309
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/interfaces_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/interfaces_test $(LDFLAGS)
@@ -326,9 +322,16 @@ tests: clean library_debug_unit_tests
326322
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/uninit_read.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uninit_read $(LDFLAGS)
327323
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/sized_free.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/sized_free $(LDFLAGS)
328324
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/pool_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/pool_test $(LDFLAGS)
329-
330325
utils/run_tests.sh
331326

327+
tagging_tests: clean cpp_library_debug
328+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/tagged_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test $(LDFLAGS)
329+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/uaf_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf_tag_ptr_test $(LDFLAGS)
330+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/bad_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/bad_tag_ptr_test $(LDFLAGS)
331+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/verify_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/verify_tag_ptr_test $(LDFLAGS)
332+
$(CXX) -DMEMORY_TAGGING=1 $(CXXFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(EXE_CFLAGS) $(OS_FLAGS) tests/tagged_ptr_test.cpp $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test_cpp $(LDFLAGS)
333+
utils/run_tagging_tests.sh
334+
332335
init_test: clean library_debug_unit_tests
333336
@echo "make init_test"
334337
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/init_destroy.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/init_destroy $(LDFLAGS)
@@ -378,9 +381,7 @@ endif
378381
cpp_tests: clean cpp_library_debug
379382
@echo "make cpp_tests"
380383
$(CXX) $(CXXFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(EXE_CFLAGS) $(OS_FLAGS) tests/tests.cpp $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/cxx_tests $(LDFLAGS)
381-
$(CXX) $(CXXFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(EXE_CFLAGS) $(OS_FLAGS) tests/tagged_ptr_test.cpp $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test $(LDFLAGS)
382384
LD_LIBRARY_PATH=$(BUILD_DIR)/ $(BUILD_DIR)/cxx_tests
383-
LD_LIBRARY_PATH=$(BUILD_DIR)/ $(BUILD_DIR)/tagged_ptr_test
384385

385386
install:
386387
cp -pR build/$(LIBNAME) /usr/lib/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ The Makefile targets are very simple:
9999

100100
`make tests` - Builds and runs all tests
101101

102+
`make tagging_tests` - Builds and runs the memory tagging tests
103+
102104
`make libc_sanity_tests` - Builds the memcpy/memset libc hook sanity tests
103105

104106
`make perf_tests` - Builds and runs a simple performance test that uses gprof. Linux only

tests/tagged_ptr_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* iso_alloc tagged_ptr_test.c
22
* Copyright 2022 - chris.rohlf@gmail.com */
3-
4-
/* This test should successfully run with or
5-
* without MEMORY_TAGGING support */
6-
73
#include <stdio.h>
84
#include <string.h>
95
#include "iso_alloc.h"
106
#include "iso_alloc_internal.h"
117

8+
#if !MEMORY_TAGGING
9+
#error "This test intended to be run with -DMEMORY_TAGGING=1"
10+
#endif
11+
1212
#define SIZE 256
1313

1414
int main(int argc, char *argv[]) {

tests/tagged_ptr_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// iso_alloc tagged_ptr_test.cpp
22
// Copyright 2022 - chris.rohlf@gmail.com
33

4-
// This test should successfully run with or
5-
// without MEMORY_TAGGING support
6-
74
#include <memory>
85
#include <iostream>
96
#include <ostream>
107
#include <string.h>
118
#include "iso_alloc.h"
129

10+
#if !MEMORY_TAGGING
11+
#error "This test intended to be run with -DMEMORY_TAGGING=1"
12+
#endif
13+
1314
iso_alloc_zone_handle *_zone_handle;
1415
constexpr uint32_t _str_size = 32;
1516

tests/uaf_tag_ptr_test.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "iso_alloc.h"
77
#include "iso_alloc_internal.h"
88

9+
#if !MEMORY_TAGGING
10+
#error "This test intended to be run with -DMEMORY_TAGGING=1"
11+
#endif
12+
913
#define SIZE 256
1014

1115
int main(int argc, char *argv[]) {
@@ -26,11 +30,19 @@ int main(int argc, char *argv[]) {
2630
* result in a bad pointer */
2731
memset(p, 0x41, SIZE);
2832

29-
iso_alloc_destroy_zone(_zone_handle);
30-
31-
#if !MEMORY_TAGGING
32-
return -1;
33+
#if __aarch64__
34+
/* aarch64 systems with TBI enabled will succeed in
35+
* using the tagged pointer p. If p is still tagged
36+
* we abort here */
37+
if((uintptr_t) p & IS_TAGGED_PTR_MASK) {
38+
LOG_AND_ABORT("Write to tagged ptr %p succeeded. TBI may be enabled", p);
39+
}
40+
#else
41+
if((uintptr_t) p & IS_TAGGED_PTR_MASK) {
42+
LOG_AND_ABORT("Write to tagged ptr %p succeeded on x86_64 ?!", p);
43+
}
3344
#endif
3445

46+
iso_alloc_destroy_zone(_zone_handle);
3547
return 0;
3648
}

tests/verify_tag_ptr_test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "iso_alloc.h"
77
#include "iso_alloc_internal.h"
88

9+
#if !MEMORY_TAGGING
10+
#error "This test intended to be run with -DMEMORY_TAGGING=1"
11+
#endif
12+
913
#define SIZE 256
1014

1115
int main(int argc, char *argv[]) {

utils/run_tagging_tests.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
$(echo '' > tagging_test_output.txt)
3+
4+
tests=("tagged_ptr_test" "tagged_ptr_test_cpp")
5+
failure=0
6+
succeeded=0
7+
8+
$(ulimit -c 0)
9+
10+
export LD_LIBRARY_PATH=build/
11+
12+
for t in "${tests[@]}"; do
13+
echo -n "Running $t test"
14+
echo "Running $t test" >> tagging_test_output.txt 2>&1
15+
$(build/$t >> tagging_test_output.txt 2>&1)
16+
ret=$?
17+
18+
if [ $ret -ne 0 ]; then
19+
echo "... Failed"
20+
echo "... Failed" >> tagging_test_output.txt 2>&1
21+
failure=$((failure+1))
22+
else
23+
echo "... Succeeded"
24+
echo "... Succeeded" >> tagging_test_output.txt 2>&1
25+
succeeded=$((succeeded+1))
26+
fi
27+
done
28+
29+
fail_tests=("bad_tag_ptr_test" "verify_tag_ptr_test" "uaf_tag_ptr_test")
30+
31+
for t in "${fail_tests[@]}"; do
32+
echo -n "Running $t test"
33+
echo "Running $t test" >> tagging_test_output.txt 2>&1
34+
$(build/$t >> tagging_test_output.txt 2>&1)
35+
ret=$?
36+
37+
if [ $ret -ne 0 ]; then
38+
echo "... Succeeded"
39+
echo "... Succeeded" >> tagging_test_output.txt 2>&1
40+
succeeded=$((succeeded+1))
41+
else
42+
echo "... Failed"
43+
echo "... Failed" >> tagging_test_output.txt 2>&1
44+
failure=$((failure+1))
45+
fi
46+
done
47+
48+
echo "$succeeded Tests passed"
49+
echo "$failure Tests failed"
50+
51+
unset LD_LIBRARY_PATH
52+
unset LD_PRELOAD
53+
54+
if [ $failure -ne 0 ]; then
55+
cat tagging_test_output.txt
56+
exit -1
57+
else
58+
exit 0
59+
fi

utils/run_tests.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# examples of code that should crash
44
$(echo '' > test_output.txt)
55

6-
tests=("tests" "big_tests" "interfaces_test" "thread_tests" "tagged_ptr_test" "pool_test"
6+
tests=("tests" "big_tests" "interfaces_test" "thread_tests" "pool_test"
77
"rand_freelist")
88
failure=0
99
succeeded=0
@@ -31,8 +31,7 @@ done
3131

3232
fail_tests=("double_free" "big_double_free" "heap_overflow" "heap_underflow"
3333
"leaks_test" "wild_free" "unaligned_free" "incorrect_chunk_size_multiple"
34-
"big_canary_test" "zero_alloc" "sized_free" "bad_tag_ptr_test"
35-
"verify_tag_ptr_test")
34+
"big_canary_test" "zero_alloc" "sized_free")
3635

3736
for t in "${fail_tests[@]}"; do
3837
echo -n "Running $t test"

0 commit comments

Comments
 (0)