Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
175 lines (147 loc) · 8.39 KB
/
Copy pathMakefile
File metadata and controls
175 lines (147 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
CC = cc
CFLAGS = -Wall -Wextra -std=c99 -D_GNU_SOURCE -O2
LDFLAGS =
# Library discovery via pkg-config (warn but do not fail if missing)
PKG_LIBS = libimobiledevice-1.0 libirecovery-1.0 libusb-1.0 \
libplist-2.0 openssl libcurl libssh2
CFLAGS += $(shell pkg-config --cflags $(PKG_LIBS) 2>/dev/null)
LDFLAGS += $(shell pkg-config --libs $(PKG_LIBS) 2>/dev/null)
$(foreach lib,$(PKG_LIBS),$(if $(shell pkg-config --exists $(lib) 2>/dev/null && echo ok),,$(warning Library $(lib) not found by pkg-config)))
# ------------------------------------------------------------------ #
# Feature probes for optional library symbols. #
# #
# libplist 2.3 added plist_mem_free() and a 4-arg plist_from_memory. #
# Older libirecovery lacked IRECV_SEND_OPT_DFU_NOTIFY_FINISH. #
# We probe by compiling a canary; on success we emit -D flags so the #
# include/compat/*.h shims pick the right branch. Results are also #
# written to .build-flags for `tr4mpass doctor` (task 005) to #
# surface at runtime. #
# ------------------------------------------------------------------ #
PROBE_CFLAGS := $(shell pkg-config --cflags libplist-2.0 libirecovery-1.0 2>/dev/null)
# `#` is a make comment even inside $(shell ...), so emit the `#include`
# via printf's octal escape (\043).
HAVE_PLIST_MEM_FREE := $(shell printf '\043include <plist/plist.h>\nint main(void){plist_mem_free((void*)0);return 0;}\n' | $(CC) $(PROBE_CFLAGS) -x c -c - -o /dev/null 2>/dev/null && echo 1 || echo 0)
HAVE_PLIST_FROM_MEM4 := $(shell printf '\043include <plist/plist.h>\nint main(void){plist_t p=0; plist_from_memory("",0,&p,(plist_format_t*)0);return 0;}\n' | $(CC) $(PROBE_CFLAGS) -x c -c - -o /dev/null 2>/dev/null && echo 1 || echo 0)
HAVE_IRECV_NOTIFY := $(shell printf '\043include <libirecovery.h>\nint main(void){return (int)IRECV_SEND_OPT_DFU_NOTIFY_FINISH;}\n' | $(CC) $(PROBE_CFLAGS) -x c -c - -o /dev/null 2>/dev/null && echo 1 || echo 0)
ifeq ($(HAVE_PLIST_MEM_FREE),1)
CFLAGS += -DTP_PLIST_HAS_MEM_FREE=1
endif
ifeq ($(HAVE_PLIST_FROM_MEM4),1)
CFLAGS += -DTP_PLIST_HAS_FMT_ARG=1
endif
ifeq ($(HAVE_IRECV_NOTIFY),1)
CFLAGS += -DTP_IRECV_HAS_NOTIFY_FINISH=1
endif
# Auto-discover all C sources under src/
SRCS = $(shell find src -name '*.c')
OBJS = $(SRCS:.c=.o)
TARGET = tr4mpass
all: .build-flags $(TARGET)
$(TARGET): .chip-db-canary $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -Iinclude -c -o $@ $<
# .build-flags is a diagnostic file consumed by `tr4mpass doctor`.
# It records which optional symbols the probes detected on this host.
.build-flags:
@printf 'HAVE_PLIST_MEM_FREE=%s\n' '$(HAVE_PLIST_MEM_FREE)' > $@
@printf 'HAVE_PLIST_FROM_MEM4=%s\n' '$(HAVE_PLIST_FROM_MEM4)' >> $@
@printf 'HAVE_IRECV_NOTIFY_FINISH=%s\n' '$(HAVE_IRECV_NOTIFY)' >> $@
clean:
find src -name '*.o' -delete
rm -f $(TARGET) $(TEST_TARGET) $(MOCK_TARGET) .build-flags $(CHIP_DB_CANARY_BIN)
find tests -name '*.o' -delete 2>/dev/null || true
# ------------------------------------------------------------------ #
# Chip DB integrity canary (T5, issue #53). #
# #
# Rebuilt and re-run on every $(TARGET) build. If any well-known #
# row in include/device/chip_db_table*.h diverges from tests/ #
# chip_db_canary.c's expected (name, marketing, checkm8) tuple, the #
# probe exits non-zero and the whole build fails -- guarding against #
# a repeat of the 0x8947/0x8960 mislabel bug where the CLI announced #
# the wrong device family. Bypass with STRICT_CHIP_DB=0 only when a #
# distro-packaged chip table has legitimately diverged. #
# ------------------------------------------------------------------ #
STRICT_CHIP_DB ?= 1
CHIP_DB_CANARY_BIN = tests/chip_db_canary.bin
.chip-db-canary: tests/chip_db_canary.c include/device/chip_db_table.h \
include/device/chip_db_table_rop.h include/device/chip_db.h
@$(CC) -Wall -Wextra -std=c99 -D_GNU_SOURCE -O2 -Iinclude \
-DSTRICT_CHIP_DB=$(STRICT_CHIP_DB) \
tests/chip_db_canary.c -o $(CHIP_DB_CANARY_BIN)
@./$(CHIP_DB_CANARY_BIN)
.PHONY: all clean test test-mocks .chip-db-canary .build-flags
# ------------------------------------------------------------------ #
# Test target: compile only hardware-independent units #
# No mocks, no real library linkage required for the SUT code. #
# ------------------------------------------------------------------ #
TEST_UNITS = src/device/chip_db.c src/bypass/bypass.c src/util/log.c \
src/util/env_config.c
# Sections linked by both `make test` (hw-independent) and `make test-mocks`.
TEST_SECT_CORE = tests/test_main.c tests/test_parsing.c tests/test_chip_db.c \
tests/test_bypass_probe.c tests/test_constants.c \
tests/test_cert_extract.c tests/test_signer.c \
tests/test_env_config.c
# Sections that require the mocked hardware libraries -- only used by
# `make test-mocks`. Include stub definitions of their entry points in
# TEST_SECT_STUBS so the hw-independent target still links.
TEST_SECT_MOCK = tests/test_ramdisk.c tests/test_ssh_jailbreak.c
# Aggregate for the mock build: real section bodies.
TEST_SECT = $(TEST_SECT_CORE) $(TEST_SECT_MOCK)
TEST_TARGET = tests/run_tests
TEST_CFLAGS = $(CFLAGS) -Iinclude -Itests -DUNIT_TEST
test: $(TEST_TARGET)
./$(TEST_TARGET)
# The hw-independent target replaces the mock-only sections with empty
# stubs so run_tests still finds run_ramdisk_tests / run_ssh_jailbreak_tests.
$(TEST_TARGET): $(TEST_SECT_CORE) $(TEST_UNITS) tests/mock_section_stubs.c
$(CC) $(TEST_CFLAGS) -o $@ $^
# ------------------------------------------------------------------ #
# Mock test target: full suite against mocked hardware libraries. #
# #
# Links the test sections + all tests/mocks/*.c + the production #
# sources we want to exercise (everything under src/ except main.c #
# and the hardware-specific checkm8/dfu exploit code). The real #
# hardware libs (libimobiledevice/libirecovery/libusb/libssh2/curl) #
# are NOT linked -- the mocks satisfy those symbols. libplist, #
# openssl, and pthread remain real. #
# ------------------------------------------------------------------ #
MOCK_SRCS = $(shell find tests/mocks -name '*.c')
# Phase 3: pick up end-to-end integration test sources. `find` is
# guarded so `make` still works before the directory exists.
INT_SRCS = $(shell find tests/integration -name '*.c' 2>/dev/null)
# Production sources to include in the mock build. We exclude:
# - src/main.c (has its own main())
# - src/exploit/* and src/device/usb_dfu.c (direct DFU/checkm8 h/w code)
MOCK_SUT_SRCS = $(shell find src -name '*.c' \
-not -path 'src/main.c' \
-not -path 'src/exploit/*' \
-not -path 'src/device/usb_dfu.c')
MOCK_ALL_SRCS = $(TEST_SECT) $(MOCK_SRCS) $(INT_SRCS) $(MOCK_SUT_SRCS)
MOCK_TARGET = tests/run_mock_tests
# Only link libs that the SUT still needs at runtime and we did NOT mock.
MOCK_PKG_LIBS = libplist-2.0 openssl
# Headers-only pkg-config libs: symbols are mocked so we do NOT link them,
# but SUT sources still #include their public headers, so we need the
# include paths at compile time. libusb-1.0 lives under
# <prefix>/include/libusb-1.0/ on every distro; without the -I flag the
# `#include <libusb.h>` in include/device/device.h cannot be resolved.
MOCK_HEADER_LIBS = libusb-1.0 libimobiledevice-1.0 libirecovery-1.0 libssh2 libcurl
MOCK_CFLAGS = -Wall -Wextra -std=c99 -D_GNU_SOURCE -O2 \
$(shell pkg-config --cflags $(MOCK_PKG_LIBS) $(MOCK_HEADER_LIBS) 2>/dev/null) \
-Iinclude -Itests -Itests/integration -Itests/mocks \
-DTEST_MODE -DUNIT_TEST
ifeq ($(HAVE_PLIST_MEM_FREE),1)
MOCK_CFLAGS += -DTP_PLIST_HAS_MEM_FREE=1
endif
ifeq ($(HAVE_PLIST_FROM_MEM4),1)
MOCK_CFLAGS += -DTP_PLIST_HAS_FMT_ARG=1
endif
ifeq ($(HAVE_IRECV_NOTIFY),1)
MOCK_CFLAGS += -DTP_IRECV_HAS_NOTIFY_FINISH=1
endif
MOCK_LDFLAGS = $(shell pkg-config --libs $(MOCK_PKG_LIBS) 2>/dev/null)
test-mocks: $(MOCK_TARGET)
./$(MOCK_TARGET)
$(MOCK_TARGET): $(MOCK_ALL_SRCS)
$(CC) $(MOCK_CFLAGS) -o $@ $^ $(MOCK_LDFLAGS)