Skip to content

Commit

Permalink
Use clang as the main compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Aug 2, 2023
1 parent 3ab66a5 commit 8143131
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install llvm 16
run: sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && sudo ./llvm.sh 16
- name: Install litecoin for tests
run: |
tarball=litecoin.tar.gz
wget -O "$tarball" https://download.litecoin.org/litecoin-0.21.2.2/linux/litecoin-0.21.2.2-x86_64-linux-gnu.tar.gz
tar xvzf "$tarball"
sudo cp -r litecoin-*/* /usr/local
- name: Build contract
run: make all-via-docker
run: make all
- name: Run auth_rust tests
run: cd tests/auth_rust && bash run.sh
- name: Install ckb-debugger
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
branch = schnorr
[submodule "deps/ckb-c-stdlib-2023"]
path = deps/ckb-c-stdlib-2023
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
url = https://github.com/xxuejie/ckb-c-stdlib.git
branch = master
[submodule "deps/mbedtls"]
path = deps/mbedtls
Expand Down
38 changes: 21 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
TARGET := riscv64-unknown-linux-gnu
CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy
AR := $(TARGET)-ar

CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1-20210801/src -I deps/secp256k1-20210801 -I deps/ckb-c-stdlib-2023 -I deps/ckb-c-stdlib-2023/libc -I deps/ckb-c-stdlib-2023/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -Wno-dangling-pointer -g
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
# TARGET := riscv64-unknown-linux-gnu
# CC := $(TARGET)-gcc
# LD := $(TARGET)-gcc
# OBJCOPY := $(TARGET)-objcopy
# AR := $(TARGET)-ar

CC := clang-16
LD := ld.lld-16
OBJCOPY := llvm-objcopy-16
AR := llvm-ar-16

CFLAGS := --target=riscv64 -march=rv64imc -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1-20210801/src -I deps/secp256k1-20210801 -I deps/ckb-c-stdlib-2023 -I deps/ckb-c-stdlib-2023/libc -I deps/ckb-c-stdlib-2023/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-unused-function -Wno-bitwise-instead-of-logical -g
LDFLAGS := -Wl,--gc-sections
SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h
AUTH_CFLAGS := $(CFLAGS) -I deps/mbedtls/include -I deps/ed25519/src -I c/cardano/nanocbor -Wno-array-bounds -Wno-stringop-overflow
AUTH_CFLAGS := $(CFLAGS) -I deps/mbedtls/include -I deps/ed25519/src -I c/cardano/nanocbor -Wno-array-bounds

# RSA/mbedtls
CFLAGS_MBEDTLS := $(subst ckb-c-std-lib,ckb-c-stdlib-2023,$(CFLAGS)) -I deps/mbedtls/include
LDFLAGS_MBEDTLS := $(LDFLAGS)
PASSED_MBEDTLS_CFLAGS := -O3 -fPIC -nostdinc -nostdlib -DCKB_DECLARATION_ONLY -I ../../ckb-c-stdlib-2023/libc -fdata-sections -ffunction-sections
PASSED_MBEDTLS_CFLAGS := --target=riscv64 -march=rv64imc -O3 -fPIC -nostdinc -nostdlib -DCKB_DECLARATION_ONLY -I ../../ckb-c-stdlib-2023/libc -fdata-sections -ffunction-sections -fvisibility=hidden

# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-jammy-20230214
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:d3f649ef8079395eb25a21ceaeb15674f47eaa2d8cc23adc8bcdae3d5abce6ec
Expand All @@ -39,7 +42,7 @@ build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c $(SECP256K1
$(SECP256K1_SRC_20210801):
cd deps/secp256k1-20210801 && \
./autogen.sh && \
CC=$(CC) LD=$(LD) ./configure --with-bignum=no --enable-ecmult-static-precomputation --enable-endomorphism --enable-module-recovery --host=$(TARGET) && \
./configure --with-bignum=no --enable-ecmult-static-precomputation --enable-endomorphism --enable-module-recovery --with-asm=no && \
make src/ecmult_static_pre_context.h src/ecmult_static_context.h

deps/mbedtls/library/libmbedcrypto.a:
Expand All @@ -48,18 +51,19 @@ deps/mbedtls/library/libmbedcrypto.a:

build/nanocbor/%.o: c/cardano/nanocbor/%.c
mkdir -p build/nanocbor
$(CC) -c -DCKB_DECLARATION_ONLY -I c/cardano -I c/cardano/nanocbor $(AUTH_CFLAGS) $(LDFLAGS) -o $@ $^
$(CC) -c -DCKB_DECLARATION_ONLY -I c/cardano -I c/cardano/nanocbor $(AUTH_CFLAGS) -o $@ $^
build/libnanocbor.a: build/nanocbor/encoder.o build/nanocbor/decoder.o
$(AR) cr $@ $^
build/ed25519/%.o: deps/ed25519/src/%.c
mkdir -p build/ed25519
$(CC) -c -DCKB_DECLARATION_ONLY $(AUTH_CFLAGS) $(LDFLAGS) -o $@ $^
$(CC) -c -DCKB_DECLARATION_ONLY $(AUTH_CFLAGS) -o $@ $^
build/libed25519.a: build/ed25519/sign.o build/ed25519/verify.o build/ed25519/sha512.o build/ed25519/sc.o build/ed25519/keypair.o \
build/ed25519/key_exchange.o build/ed25519/ge.o build/ed25519/fe.o build/ed25519/add_scalar.o
$(AR) cr $@ $^

build/auth: c/auth.c c/cardano/cardano_lock_inc.h deps/mbedtls/library/libmbedcrypto.a build/libed25519.a build/libnanocbor.a
$(CC) $(AUTH_CFLAGS) $(LDFLAGS) -fPIC -fPIE -pie -Wl,--dynamic-list c/auth.syms -o $@ $^
build/auth: c/auth.c deps/mbedtls/library/libmbedcrypto.a build/libed25519.a build/libnanocbor.a
$(CC) $(AUTH_CFLAGS) -DCKB_NO_ENTRY_GP -fPIC -fPIE -c -o build/auth.o c/auth.c
$(LD) --shared --gc-sections --dynamic-list c/auth.syms -o $@ build/auth.o deps/mbedtls/library/libmbedcrypto.a build/libed25519.a build/libnanocbor.a
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

Expand Down
57 changes: 55 additions & 2 deletions c/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define ENABLE_MODULE_EXTRAKEYS
#define ENABLE_MODULE_SCHNORRSIG
#define SECP256K1_BUILD
#define SECP256K1_API
// in secp256k1_ctz64_var: we don't have __builtin_ctzl in gcc for RISC-V
#define __builtin_ctzl secp256k1_ctz64_var_debruijn

Expand Down Expand Up @@ -778,6 +779,15 @@ __attribute__((visibility("default"))) int ckb_auth_validate(
#define OFFSETOF(TYPE, ELEMENT) ((size_t) & (((TYPE *)0)->ELEMENT))
#define PT_DYNAMIC 2

/* See https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-42444.html for details */
#define DT_RELA 7
#define DT_RELACOUNT 0x6ffffff9
#define DT_JMPREL 23
#define DT_PLTRELSZ 2
#define DT_PLTREL 20
#define DT_SYMTAB 6
#define DT_SYMENT 11

typedef struct {
uint64_t type;
uint64_t value;
Expand Down Expand Up @@ -806,11 +816,34 @@ int main(int argc, char *argv[]) {
Elf64_Dynamic *d = (Elf64_Dynamic *)program_header->p_vaddr;
uint64_t rela_address = 0;
uint64_t rela_count = 0;
uint64_t jmprel_address = 0;
uint64_t pltrel_size = 0;
uint64_t pltrel = 0;
uint64_t symtab_address = 0;
uint64_t symtab_entry_size = 0;
while (d->type != 0) {
if (d->type == 0x7) {
switch (d->type) {
case DT_RELA:
rela_address = d->value;
} else if (d->type == 0x6ffffff9) {
break;
case DT_RELACOUNT:
rela_count = d->value;
break;
case DT_JMPREL:
jmprel_address = d->value;
break;
case DT_PLTRELSZ:
pltrel_size = d->value;
break;
case DT_PLTREL:
pltrel = d->value;
break;
case DT_SYMTAB:
symtab_address = d->value;
break;
case DT_SYMENT:
symtab_entry_size = d->value;
break;
}
d++;
}
Expand All @@ -825,6 +858,26 @@ int main(int argc, char *argv[]) {
(uint64_t)(relocation->r_addend);
}
}
if (jmprel_address > 0 && pltrel_size > 0 && pltrel == DT_RELA && symtab_address > 0) {
if (pltrel_size % sizeof(Elf64_Rela) != 0) {
return ERROR_INVALID_ELF;
}
if (symtab_entry_size != sizeof(Elf64_Sym)) {
return ERROR_INVALID_ELF;
}
Elf64_Rela *relocations = (Elf64_Rela *) jmprel_address;
Elf64_Sym *symbols = (Elf64_Sym *) symtab_address;
for (int j = 0; j < pltrel_size / sizeof(Elf64_Rela); j++) {
Elf64_Rela *relocation = &relocations[j];
uint32_t idx = (uint32_t) (relocation->r_info >> 32);
uint32_t t = (uint32_t) relocation->r_info;
if (t != R_RISCV_JUMP_SLOT) {
return ERROR_INVALID_ELF;
}
Elf64_Sym *sym = &symbols[idx];
*((uint64_t *)(relocation->r_offset)) = sym->st_value;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion deps/ckb-c-stdlib-2023
18 changes: 11 additions & 7 deletions examples/auth-demo/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
TARGET := riscv64-unknown-linux-gnu
CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-std-lib -I deps/ckb-c-std-lib/libc -I deps/ckb-c-std-lib/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -g
# TARGET := riscv64-unknown-linux-gnu
# CC := $(TARGET)-gcc
# LD := $(TARGET)-gcc
# OBJCOPY := $(TARGET)-objcopy

CC := clang-16
OBJCOPY := llvm-objcopy-16

CFLAGS := --target=riscv64 -march=rv64imc -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-std-lib -I deps/ckb-c-std-lib/libc -I deps/ckb-c-std-lib/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-unused-function -g
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
AUTH_CFLAGS=$(subst ckb-c-std-lib,ckb-c-stdlib-2023,$(CFLAGS)) -Wno-dangling-pointer -Wno-array-bounds -Wno-stringop-overflow
AUTH_CFLAGS=$(subst ckb-c-std-lib,ckb-c-stdlib-2023,$(CFLAGS)) -Wno-array-bounds

# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-jammy-20230214
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:d3f649ef8079395eb25a21ceaeb15674f47eaa2d8cc23adc8bcdae3d5abce6ec
Expand All @@ -15,7 +19,7 @@ all-via-docker:
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make -f examples/auth-demo/Makefile all"

build/auth_demo: examples/auth-demo/auth_demo.c c/ckb_auth.h
$(CC) $(AUTH_CFLAGS) $(LDFLAGS) -o $@ $^
$(CC) $(AUTH_CFLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

Expand Down
2 changes: 1 addition & 1 deletion tests/auth_rust/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd ../..
make -f examples/auth-demo/Makefile all-via-docker
make -f examples/auth-demo/Makefile all
cd tests/auth_rust
cargo test

0 comments on commit 8143131

Please sign in to comment.