This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMakefile
309 lines (242 loc) · 11.4 KB
/
Makefile
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
sgxsd_srcdir = sgxsd_enclave
includedir = include
patchdir = patches
builddir = build
targetdir = $(builddir)/target
resourcedir = ../service/kbupd/res
RUSTC ?= rustc
CARGO ?= cargo
RUSTUP ?= rustup
BINDGEN ?= bindgen
DOCKER ?= docker
INSTALL ?= install
RUSTUP_TOOLCHAIN_UNSTABLE ?= nightly
FEATURES ?=
include docker/deps.mk
INSTALL_PROGRAM = $(INSTALL) -m 755 $(INSTALL_PROGRAM_FLAGS)
INSTALL_DATA = $(INSTALL) -m 644
DESTDIR ?=
CFLAGS = \
-m64 -O2 -ggdb -march=skylake -pipe -fPIC \
-D_FORTIFY_SOURCE=2 -std=c11 -D_DEFAULT_SOURCE \
-fstack-protector-strong -fcf-protection \
-Wall -Werror=all -Wextra -Wno-unused-parameter -Wno-missing-field-initializers \
-I$(includedir) -I$(includedir)/bearssl -I$(SGX_INCLUDEDIR)
LDFLAGS =
ENCLAVE_RUSTFLAGS = -C opt-level=2 -C debuginfo=1 -C codegen-units=1 -C panic=abort -C llvm-args=-max-jump-table-size=1 -C llvm-args=-disable-tail-duplicate -C no-redzone
BINDGENFLAGS =
CARGOBUILDFLAGS =
export CARGO_TARGET_DIR = $(CURDIR)/$(targetdir)
export CARGO_HOME = $(CURDIR)/$(builddir)/cargo
TEST_CFLAGS += $(CFLAGS) \
-DUNIT_TESTING -fsanitize=address -static-libasan -fsanitize=undefined -static-libubsan
##
## sgxsd
##
TEST_SGXSD_TARGET := $(builddir)/sgxsd-enclave-test
BEARSSL_SOURCES := \
$(sgxsd_srcdir)/bearssl/gcm.c $(sgxsd_srcdir)/bearssl/ghash_pclmul.c \
$(sgxsd_srcdir)/bearssl/sha2small.c $(sgxsd_srcdir)/bearssl/dec32be.c $(sgxsd_srcdir)/bearssl/enc32be.c \
$(sgxsd_srcdir)/bearssl/aes_x86ni_ctr.c $(sgxsd_srcdir)/bearssl/aes_x86ni.c
BEARSSL_OBJECTS := $(addprefix $(builddir)/,$(BEARSSL_SOURCES:.c=.o))
SGXSD_SOURCES := $(sgxsd_srcdir)/sgxsd-enclave.c $(sgxsd_srcdir)/curve25519-donna-c64.c $(BEARSSL_SOURCES) \
$(sgxsd_srcdir)/sgx-tcrypto-stub.c
SGXSD_OBJECTS := $(addprefix $(builddir)/,$(SGXSD_SOURCES:.c=.o))
TEST_SGXSD_SOURCES := $(sgxsd_srcdir)/sgxsd-enclave.c $(sgxsd_srcdir)/curve25519-donna-c64.c $(sgxsd_srcdir)/sgxsd-enclave-test.c $(sgxsd_srcdir)/cmockery.c
TEST_SGXSD_OBJECTS := $(addprefix $(builddir)/test/,$(TEST_SGXSD_SOURCES:.c=.o))
TEST_LDFLAGS += $(TEST_CFLAGS)
##
## kbupd
##
KBUPD_ENCLAVE_NAME := libkbupd_enclave.hardened
KBUPD_ENCLAVE_TARGET := $(builddir)/libkbupd_enclave.unstripped.so
KBUPD_ENCLAVE_RUST_STATICLIB := $(targetdir)/release/libkbupd_enclave.a
##
## targets
##
.PHONY: default docker-install all unstripped hardened unsigned llvm-bolt doc check test test-asan benchmark clippy bindgen protobuf debug sign install edger8r distclean clean docker
.SUFFIXES:
.SUFFIXES: .c .o
default: docker-install
include sgx_enclave.mk
docker-install: docker install
all: $(KBUPD_ENCLAVE_TARGET) $(builddir)/$(KBUPD_ENCLAVE_NAME).unstripped.so $(builddir)/$(KBUPD_ENCLAVE_NAME).unsigned.so $(builddir)/$(KBUPD_ENCLAVE_NAME).debug.so $(builddir)/$(KBUPD_ENCLAVE_NAME).signdata $(builddir)/$(KBUPD_ENCLAVE_NAME).mrenclave
unstripped: $(KBUPD_ENCLAVE_TARGET)
hardened: $(builddir)/$(KBUPD_ENCLAVE_NAME).unstripped.so
unsigned: $(builddir)/$(KBUPD_ENCLAVE_NAME).unsigned.so
llvm-bolt: $(LLVM_BOLT)
doc:
env -u CFLAGS RUSTFLAGS="$(ENCLAVE_RUSTFLAGS)" \
$(CARGO) doc --package=kbupd_enclave --release --document-private-items --lib
check:
$(CARGO) check --all --exclude=kbupd_enclave
$(CARGO) check --manifest-path=kbupd_enclave/Cargo.toml --lib --tests --features test,$(if $(FEATURES),$(FEATURES))
test: $(TEST_SGXSD_TARGET)
ASAN_OPTIONS="detect_leaks=0:$(ASAN_OPTIONS)" ./$(TEST_SGXSD_TARGET)
env -u CFLAGS \
RUST_BACKTRACE=full \
RUST_TEST_THREADS=1 \
$(CARGO) test --all --exclude=kbupd_enclave -- --test-threads=1
env -u CFLAGS \
RUST_BACKTRACE=full \
RUST_TEST_THREADS=1 \
$(CARGO) test --manifest-path=kbupd_enclave/Cargo.toml --lib --bins --features test,$(if $(FEATURES),$(FEATURES)) -- --test-threads=1
test-asan: $(TEST_SGXSD_TARGET)
./$(TEST_SGXSD_TARGET)
env -u CFLAGS \
RUST_BACKTRACE=full \
RUSTFLAGS="-Z sanitizer=address" RUST_TEST_THREADS=1 \
ASAN_OPTIONS="detect_odr_violation=1:detect_stack_use_after_return=true:check_initialization_order=true:strict_init_order=true:halt_on_error=false:$(ASAN_OPTIONS)" \
LSAN_OPTIONS="suppressions=$(CURDIR)/kbupd_enclave/lsan-ignore-test.txt:$(LSAN_OPTIONS)" \
$(RUSTUP) run $(RUSTUP_TOOLCHAIN_UNSTABLE) \
$(CARGO) test --all --exclude=kbupd_enclave -- --test-threads=1
env -u CFLAGS \
RUST_BACKTRACE=full \
RUSTFLAGS="-Z sanitizer=address" RUST_TEST_THREADS=1 \
ASAN_OPTIONS="detect_odr_violation=1:detect_stack_use_after_return=true:check_initialization_order=true:strict_init_order=true:halt_on_error=false:$(ASAN_OPTIONS)" \
LSAN_OPTIONS="suppressions=$(CURDIR)/kbupd_enclave/lsan-ignore-test.txt:$(LSAN_OPTIONS)" \
$(RUSTUP) run $(RUSTUP_TOOLCHAIN_UNSTABLE) \
$(CARGO) test --manifest-path=kbupd_enclave/Cargo.toml --lib --bins --tests --features test,$(if $(FEATURES),$(FEATURES)) -- --test-threads=1
clippy:
$(CARGO) clippy --all --exclude=kbupd_enclave
$(CARGO) clippy --manifest-path=kbupd_enclave/Cargo.toml --features test,$(if $(FEATURES),$(FEATURES))
benchmark:
$(CARGO) bench --all
bindgen:
$(BINDGEN) --no-include-path-detection -o sgx_ffi/src/bindgen_wrapper.rs \
--rust-target 1.33 --use-core --ctypes-prefix libc --with-derive-default --with-derive-eq --no-prepend-enum-name \
sgx_ffi/src/bindgen_wrapper.h -- \
$(filter-out -fvisibility=hidden,$(filter-out -std=%,$(CFLAGS) $(ENCLAVE_CFLAGS)))
$(BINDGEN) --no-include-path-detection -o sgxsd_ffi/src/bindgen_wrapper.rs \
--rust-target 1.33 --use-core --ctypes-prefix libc --with-derive-default --with-derive-eq --no-prepend-enum-name \
sgxsd_ffi/src/bindgen_wrapper.h -- \
$(filter-out -fvisibility=hidden,$(filter-out -std=%,$(CFLAGS) $(ENCLAVE_CFLAGS)))
$(BINDGEN) --no-include-path-detection -o kbupd_enclave/src/ffi/bindgen_wrapper.rs \
--rust-target 1.33 --use-core --ctypes-prefix libc --with-derive-default --with-derive-eq --no-prepend-enum-name \
kbupd_enclave/src/ffi/bindgen_wrapper.h -- \
$(filter-out -fvisibility=hidden,$(filter-out -std=%,$(CFLAGS) $(ENCLAVE_CFLAGS)))
$(builddir)/bin/bindgen-%:
$(CARGO) install --locked --force --version $* --bin bindgen bindgen
mkdir -p $(builddir)/bin
cp $(builddir)/cargo/bin/bindgen $@
protobuf: | $(targetdir)/debug/prostc
mkdir -p kbupd_enclave/src/protobufs
OUT_DIR=kbupd_enclave/src/protobufs $(targetdir)/debug/prostc kbupd_enclave/src/protobufs.proto kbupd_enclave/src/ $(includedir)/
mv kbupd_enclave/src/protobufs/protobufs.rs kbupd_enclave/src/protobufs/mod.rs
cd kbupd_enclave/src/protobufs; for file in protobufs.*.rs; do newfile=$${file#protobufs.}; mv $$file $$newfile; echo "pub mod $${newfile%.rs};" >> mod.rs; done
debug: $(builddir)/$(KBUPD_ENCLAVE_NAME).unsigned.so $(builddir)/$(KBUPD_ENCLAVE_NAME).debug.so
sign: $(builddir)/$(KBUPD_ENCLAVE_NAME).signed.so $(builddir)/$(KBUPD_ENCLAVE_NAME).test.signed.so
install:
$(INSTALL_DATA) $(builddir)/$(KBUPD_ENCLAVE_NAME).debug.so \
$(resourcedir)/enclave/$$(cat $(builddir)/$(KBUPD_ENCLAVE_NAME).mrenclave).so
if [ -e $(builddir)/$(KBUPD_ENCLAVE_NAME).signed.so ]; then \
cp $(builddir)/$(KBUPD_ENCLAVE_NAME).signed.so \
$(resourcedir)/enclave/$$(cat $(builddir)/$(KBUPD_ENCLAVE_NAME).mrenclave).so; \
fi
edger8r: $(includedir)/kbupd_enclave.edl | $(SGX_EDGER8R)
$(SGX_EDGER8R) --untrusted --untrusted-dir $(includedir) \
--trusted --trusted-dir $(includedir) \
--search-path $(SGX_INCLUDEDIR) \
--search-path $(includedir) \
$(includedir)/kbupd_enclave.edl
distclean: clean
clean:
rm -f $(builddir)/$(KBUPD_ENCLAVE_NAME).debug.signdata \
$(builddir)/$(KBUPD_ENCLAVE_NAME).debug.key \
$(builddir)/$(KBUPD_ENCLAVE_NAME).debug.pub \
$(builddir)/$(KBUPD_ENCLAVE_NAME).debug.sig \
$(builddir)/$(KBUPD_ENCLAVE_NAME).test.sig \
$(builddir)/$(KBUPD_ENCLAVE_NAME).test.signdata \
$(builddir)/$(KBUPD_ENCLAVE_NAME).sig \
$(builddir)/$(KBUPD_ENCLAVE_NAME).signdata \
$(builddir)/$(KBUPD_ENCLAVE_NAME).mrenclave \
$(builddir)/*.o \
$(builddir)/*.a \
$(builddir)/*.so \
$(SGXSD_OBJECTS) $(TEST_SGXSD_TARGET) $(TEST_SGXSD_OBJECTS) \
debian/debhelper-build-stamp \
debian/kbupd-enclave.substvars \
debian/files \
debian/*.deb
-rm -rf $(targetdir)/release/ \
$(targetdir)/debug/ \
$(builddir)/bolt/build \
$(builddir)/cargo/bin \
debian/.debhelper/ \
debian/kbupd-enclave/
-$(CARGO) clean --release
## rust
.PHONY: FORCE
FORCE:
$(targetdir)/debug/prostc: FORCE
env -u CFLAGS $(CARGO) build --manifest-path=prostc/Cargo.toml --bin=prostc
$(targetdir)/release/lib%.a: FORCE
env CFLAGS="-mno-red-zone" RUSTFLAGS="$(ENCLAVE_RUSTFLAGS)" \
$(CARGO) build -vv --release --manifest-path=$*/Cargo.toml --lib $(if $(FEATURES),--features $(FEATURES))
## sgxsd
$(BEARSSL_OBJECTS): $(wildcard $(includedir)/bearssl/%.h)
$(SGXSD_OBJECTS): $(builddir)/%.o: %.c $(includedir)/sgxsd.h $(includedir)/sgxsd-enclave.h
@mkdir -p $(dir $@)
$(CC) -o $@ $(CFLAGS) $(ENCLAVE_CFLAGS) -c $<
$(TEST_SGXSD_TARGET): $(TEST_SGXSD_OBJECTS)
$(CC) -o $@ $(TEST_SGXSD_OBJECTS) $(TEST_LDFLAGS)
$(TEST_SGXSD_OBJECTS): $(builddir)/test/%.o: %.c $(includedir)/sgxsd.h $(includedir)/sgxsd-enclave.h $(includedir)/cmockery.h
@mkdir -p $(dir $@)
$(CC) -o $@ $(CFLAGS) $(TEST_CFLAGS) -c $<
## kbupd
$(includedir)/kbupd_enclave_t.h $(includedir)/kbupd_enclave_u.h: $(includedir)/sgxsd.edl
$(builddir)/kbupd_enclave_t.o: $(includedir)/kbupd_enclave_t.c
$(CC) -o $@ $(CFLAGS) $(ENCLAVE_CFLAGS) -c $<
$(builddir)/kbupd_enclave_u.o: $(includedir)/kbupd_enclave_u.c
$(CC) -o $@ $(CFLAGS) -c $<
$(builddir)/libkbupd_enclave_u.a: $(builddir)/kbupd_enclave_u.o
$(AR) r $@ $<
$(KBUPD_ENCLAVE_TARGET): $(SGXSD_OBJECTS) $(KBUPD_ENCLAVE_RUST_STATICLIB)
$(KBUPD_ENCLAVE_TARGET): LDFLAGS := -L$(dir $(KBUPD_ENCLAVE_RUST_STATICLIB))
$(KBUPD_ENCLAVE_TARGET): LDLIBS := -lkbupd_enclave
## Reproducible enclave build via debian package
MAKETARGET ?= bindgen debuild sign
docker: DOCKER_EXTRA=$(shell [ -L build ] && P=$$(readlink build) && echo -v $$P/:$$P )
docker:
$(DOCKER) build -t kbupd-enclave-builder ./docker
$(DOCKER) run --rm -it --user $$(id -u):$$(id -g) --cap-add SYS_PTRACE \
-v `pwd`/:/home/rust/src $(DOCKER_EXTRA) \
--env MAKEFLAGS="$(MAKEFLAGS)" \
kbupd-enclave-builder \
sh -c "cd src; make $(MAKETARGET)"
docker_%: DOCKER_EXTRA=$(shell [ -L build ] && P=$$(readlink build) && echo -v $$P/:$$P )
docker_%:
$(DOCKER) build -t kbupd-enclave-builder ./docker
$(DOCKER) run --rm --user $$(id -u):$$(id -g) --cap-add SYS_PTRACE \
-v `pwd`/:/home/rust/src $(DOCKER_EXTRA) \
--env MAKEFLAGS="$*" \
kbupd-enclave-builder \
sh -c "set -x; cd src && mkdir -p build && ln -s /home/rust/rust_deps build/rust_deps && make $*"
.PHONY: debuild
debuild:
env -u LANG LC_ALL=C debuild --preserve-envvar=PATH --no-lintian --build=binary -uc -us -j1
mv ../*.buildinfo debian/buildinfo
mv ../*.deb debian/
.PHONY: debuild-kbupd-enclave-build
debuild-kbupd-enclave-build: all
.PHONY: debuild-kbupd-enclave-install
debuild-kbupd-enclave-install: $(builddir)/$(KBUPD_ENCLAVE_NAME).unsigned.so
mkdir -p $(DESTDIR)/usr/lib/kbupd/enclave/
$(INSTALL_DATA) $(builddir)/$(KBUPD_ENCLAVE_NAME).unsigned.so $(DESTDIR)/usr/lib/kbupd/enclave/
.PHONY: debuild-kbupd-enclave-test
debuild-kbupd-enclave-test:
echo "not running tests in debuild" 1>&2
.PHONY: tar
tar:
tar -cjf $(builddir)/$(KBUPD_ENCLAVE_NAME).build.tar.bz2 \
$(LLVM_BOLT) \
--anchored --exclude-vcs \
--exclude='$(builddir)/bolt' \
--exclude='$(builddir)/cargo/registry/cache' \
--exclude='$(builddir)/cargo/registry/index' \
--exclude='$(builddir)/cargo/git/db' \
--exclude='*.git' \
--no-wildcards-match-slash \
--exclude='$(builddir)/*.tar.bz2' \
--verbose --totals \
'$(builddir)/'