Skip to content

Commit f1f7a3d

Browse files
committed
Reject mismatched Bedrock2 target word sizes
1 parent 5691ca0 commit f1f7a3d

34 files changed

Lines changed: 63 additions & 2 deletions

Makefile.examples

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ EXTRA_C_FILES := inversion/c/*_test.c
144144

145145
ALL_C_FILES := $(patsubst %,$(C_DIR)%.c,$(ALL_BASE_FILES))
146146
ALL_BEDROCK2_FILES := $(patsubst %,$(BEDROCK2_DIR)%.c,$(filter-out $(INVALID_BEDROCK2_BASE_FILES),$(ALL_BASE_FILES)))
147+
BEDROCK2_CC_BITWIDTH = $(shell printf '#ifdef __SIZEOF_POINTER__\n__SIZEOF_POINTER__\n#endif\n' | $(CC) $(CFLAGS) -dM -E -x c - 2>/dev/null | awk '$$2 == "__SIZEOF_POINTER__" { print $$3 * 8 }')
148+
NATIVE_BEDROCK2_FILES = $(filter %_$(BEDROCK2_CC_BITWIDTH).c,$(ALL_BEDROCK2_FILES))
149+
NONNATIVE_BEDROCK2_FILE = $(firstword $(filter-out $(NATIVE_BEDROCK2_FILES),$(ALL_BEDROCK2_FILES)))
147150
ALL_RUST_FILES := $(patsubst %,$(RUST_DIR)%.rs,$(ALL_BASE_FILES))
148151
ALL_RUST_MODS := $(ALL_BASE_FILES)
149152
ALL_GO_FILES := $(patsubst %,$(GO_DIR)%.go,$(call GO_RENAME_TO_FILE,$(filter-out $(INVALID_GO_BASE_FILES),$(ALL_BASE_FILES))))
@@ -232,7 +235,11 @@ $(ALL_BEDROCK2_FILES) : $(BEDROCK2_DIR)%.c : $$(BEDROCK2_$$($$*_BINARY_NAME))
232235
test-bedrock2-files: $(ALL_BEDROCK2_FILES)
233236

234237
test-bedrock2-files only-test-bedrock2-files:
235-
$(CC) -Wall -Wno-unused-function -Werror $(BEDROCK2_EXTRA_CFLAGS) $(CFLAGS) -c $(ALL_BEDROCK2_FILES)
238+
@test -n "$(BEDROCK2_CC_BITWIDTH)" || { echo "Could not determine $(CC)'s pointer width" >&2; exit 1; }
239+
$(CC) -Wall -Wno-unused-function -Werror $(BEDROCK2_EXTRA_CFLAGS) $(CFLAGS) -fsyntax-only $(NATIVE_BEDROCK2_FILES)
240+
@output="$$( $(CC) -Wall -Wno-unused-function -Werror $(BEDROCK2_EXTRA_CFLAGS) $(CFLAGS) -fsyntax-only $(NONNATIVE_BEDROCK2_FILE) 2>&1 )"; status=$$?; \
241+
if [ $$status -eq 0 ]; then echo "Expected $(NONNATIVE_BEDROCK2_FILE) to reject $(BEDROCK2_CC_BITWIDTH)-bit compilation" >&2; exit 1; fi; \
242+
printf '%s\n' "$$output" | grep -q "target word size does not match synthesis word size" || { printf '%s\n' "$$output" >&2; exit 1; }
236243

237244
$(ALL_RUST_FILES) : $(RUST_DIR)%.rs : $$($$($$*_BINARY_NAME))
238245
$(SHOW)'SYNTHESIZE > $@'

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ This binary takes arguments for the strategy:
162162

163163
Passing no arguments, or passing `-h` or `--help` (or any other invalid arguments) will result in a usage message being printed. These binaries output bedrock2/C code on stdout.
164164

165+
Bedrock2 represents both addresses and machine words with `uintptr_t`, so the
166+
synthesis word size must match the target's pointer width. In particular,
167+
compile `_32.c` outputs only for 32-bit targets and `_64.c` outputs only for
168+
64-bit targets; generated files enforce this requirement with a static
169+
assertion.
170+
165171
Here are some examples of ways to invoke the binaries (from the directories that they live in):
166172

167173
# Generate code for 2^255-19

fiat-bedrock2/src/curve25519_32.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/curve25519_64.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/curve25519_scalar_32.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/curve25519_scalar_64.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/curve25519_solinas_64.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/p224_32.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/p224_64.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fiat-bedrock2/src/p256_32.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)