Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support clang #9

Merged
merged 8 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/clang.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

name: Clang

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

steps:
- 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/
litecoin-cli --help
- name: Install monero wallet cli for tests
run: |
tarball=monero-wallet-cli.tar.gz
wget -O "$tarball" https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.2.2.tar.bz2
tar xvaf "$tarball"
sudo cp -r monero-*/* /usr/local/bin/
monero-wallet-cli --help
- name: Install solana cli for tests
run: |
tarball=solana-release-x86_64-unknown-linux-gnu.tar.bz2
wget -O "$tarball" https://github.com/solana-labs/solana/releases/download/v1.16.1/solana-release-x86_64-unknown-linux-gnu.tar.bz2
tar xvaf "$tarball"
sudo cp -r solana-*/* /usr/local/
solana --help
solana-keygen new --force --no-bip39-passphrase
- name: Build contract with clang
run: make -f Makefile.clang all
- name: Build example with clang
run: make -f examples/auth-demo/Makefile.clang all
- name: Run auth_rust tests
run: cd tests/auth_rust && cargo test
- name: Install ckb-debugger
run: cd tests/auth_spawn_rust && make install
- name: Run auth_spawn_rust tests
run: cd tests/auth_spawn_rust && make all
- name: Install cardano tools
run: cd tests/cardano_lock && make install_cardano_tools
- name: Run cardano tests
run: cd tests/cardano_lock && make all
152 changes: 152 additions & 0 deletions Makefile.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
CC := clang-16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ?= instead of :=. Give a chance to set CC from environment.

LD := ld.lld-16
OBJCOPY := llvm-objcopy-16
AR := llvm-ar-16
RANLIB := llvm-ranlib-16
LLVM_CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs \
-Wno-error=unused-but-set-variable \
-Wno-error=unused-command-line-argument \
-Wno-error=bitwise-instead-of-logical
LLVM_AUTH_CFLAGS := -DCKB_NO_ENTRY_GP
LDFLAGS := -static -Wl,--gc-sections
AUTH_DYN_LIST := --dynamic-list c/auth.syms

CFLAGS := -g -O3 -fPIC \
-Wall -Werror -Wno-nonnull -Wno-unused-function \
-fno-builtin-printf -fno-builtin-memcmp \
-nostdinc -nostdlib -fvisibility=hidden \
-fdata-sections -ffunction-sections \
$(GCC_CFLAGS) $(LLVM_CFLAGS)

INCLUDE_SECP256K1_CFLAGS = \
-I deps/secp256k1-20210801/src -I deps/secp256k1-20210801
INCLUDE_CKB_STD_CFLAGS = \
-I deps/ckb-c-stdlib-2023 -I deps/ckb-c-stdlib-2023/libc -I deps/ckb-c-stdlib-2023/molecule
INCLUDE_CFLAGS := $(INCLUDE_SECP256K1_CFLAGS) $(INCLUDE_CKB_STD_CFLAGS) \
-I c -I build -I deps/mbedtls/include -I deps/ed25519/src -I c/cardano/nanocbor

AUTH_CFLAGS := $(CFLAGS) $(INCLUDE_CFLAGS) -Wno-array-bounds
PASSED_MBEDTLS_CFLAGS := $(CFLAGS) -DCKB_DECLARATION_ONLY -I ../../ckb-c-stdlib-2023/libc

SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h

CFLAGS_LIBECC := $(CFLAGS) \
-DUSER_NN_BIT_LEN=256 -DWORDSIZE=64 -DWITH_STDLIB -DWITH_CKB -DCKB_DECLARATION_ONLY \
-Wno-unused-variable
LIBECC_OPTIMIZED_PATH := deps/libecc
LIBECC_OPTIMIZED_FILES := \
${LIBECC_OPTIMIZED_PATH}/build/libarith.a \
${LIBECC_OPTIMIZED_PATH}/build/libec.a \
${LIBECC_OPTIMIZED_PATH}/build/libsign.a
CFLAGS_LIBECC_OPTIMIZED = \
-I ../ckb-c-stdlib-2023 \
-I ../ckb-c-stdlib-2023/libc \
-I ../ckb-c-stdlib-2023/molecule \
$(CFLAGS_LIBECC) \
-DWITH_LL_U256_MONT
CFLAGS_LINK_TO_LIBECC_OPTIMIZED := \
-DWORDSIZE=64 -DWITH_STDLIB -DWITH_CKB \
-I ${LIBECC_OPTIMIZED_PATH}/src -I ${LIBECC_OPTIMIZED_PATH}/src/external_deps

all: \
build/secp256k1_data_info_20210801.h \
$(SECP256K1_SRC_20210801) \
deps/mbedtls/library/libmbedcrypto.a \
build/auth_libecc \
build/auth \
build/always_success

build/always_success: c/always_success.c
$(CC) $(AUTH_CFLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/secp256k1_data_info_20210801.h: build/dump_secp256k1_data_20210801
$<

build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c $(SECP256K1_SRC_20210801)
mkdir -p build
gcc -I deps/ckb-c-stdlib-2023 -I deps/secp256k1-20210801/src -I deps/secp256k1-20210801 -o $@ $<

$(SECP256K1_SRC_20210801):
cd deps/secp256k1-20210801 && \
./autogen.sh && \
CC=$(CC) LD=$(LD) ./configure --with-bignum=no --with-asm=no \
--enable-ecmult-static-precomputation --enable-endomorphism --enable-module-recovery \
&& \
make src/ecmult_static_pre_context.h src/ecmult_static_context.h

$(LIBECC_OPTIMIZED_FILES): libecc

libecc:
make -C ${LIBECC_OPTIMIZED_PATH} LIBECC_WITH_LL_U256_MONT=1 \
CC=${CC} LD=${LD} AR=$(AR) RANLIB=$(RANLIB) CFLAGS="$(CFLAGS_LIBECC_OPTIMIZED)"

deps/mbedtls/library/libmbedcrypto.a:
cp deps/mbedtls-config-template.h deps/mbedtls/include/mbedtls/config.h
make -C deps/mbedtls/library \
APPLE_BUILD=0 AR=$(AR) CC=${CC} LD=${LD} CFLAGS="${PASSED_MBEDTLS_CFLAGS}" \
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) -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) -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 \
c/ripple.h \
deps/mbedtls/library/libmbedcrypto.a \
build/libed25519.a \
build/libnanocbor.a
$(CC) $(AUTH_CFLAGS) $(LLVM_AUTH_CFLAGS) -c -o $@.o c/auth.c
$(LD) --shared --gc-sections $(AUTH_DYN_LIST) \
-o $@ \
$@.o \
deps/mbedtls/library/libmbedcrypto.a \
build/libed25519.a \
build/libnanocbor.a
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@
ls -lh $@

build/auth_libecc: c/auth_libecc.c $(LIBECC_OPTIMIZED_FILES)
$(CC) $(AUTH_CFLAGS) $(LLVM_AUTH_CFLAGS) $(CFLAGS_LINK_TO_LIBECC_OPTIMIZED) -c -o $@.o c/auth_libecc.c
$(LD) --shared --gc-sections $(AUTH_DYN_LIST) \
-o $@ \
$@.o \
$(LIBECC_OPTIMIZED_FILES)
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@
ls -lh $@

fmt:
clang-format -i -style="{BasedOnStyle: Google, IndentWidth: 4}" c/*.c c/*.h

clean:
rm -rf build/*.debug
rm -f build/auth build/auth_libecc build/auth_demo
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
rm -rf build/ed25519 build/libed25519.a build/nanocbor build/libnanocbor.a
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean
make -C deps/mbedtls/library clean
make -C deps/libecc clean

.PHONY: all

1 change: 1 addition & 0 deletions c/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
63 changes: 59 additions & 4 deletions c/ckb_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
#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

enum AuthErrorCodeType {
ERROR_NOT_IMPLEMENTED = 100,
ERROR_MISMATCHED,
Expand Down Expand Up @@ -187,17 +197,41 @@ int setup_elf() {
uint64_t *phoff = (uint64_t *)OFFSETOF(Elf64_Ehdr, e_phoff);
uint16_t *phnum = (uint16_t *)OFFSETOF(Elf64_Ehdr, e_phnum);
Elf64_Phdr *program_headers = (Elf64_Phdr *)(*phoff);

for (int i = 0; i < *phnum; i++) {
Elf64_Phdr *program_header = &program_headers[i];
if (program_header->p_type == PT_DYNAMIC) {
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) {
rela_address = d->value;
} else if (d->type == 0x6ffffff9) {
rela_count = d->value;
switch (d->type) {
case DT_RELA:
rela_address = d->value;
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 @@ -212,6 +246,27 @@ int setup_elf() {
(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
12 changes: 11 additions & 1 deletion examples/auth-demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ 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
CFLAGS := -fPIC -O3 -g \
-Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function \
-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
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

Expand Down
31 changes: 31 additions & 0 deletions examples/auth-demo/Makefile.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CC := clang-16
LD := ld.lld-16
OBJCOPY := llvm-objcopy-16
AR := llvm-ar-16
RANLIB := llvm-ranlib-16
LLVM_CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs \
-Wno-error=unused-but-set-variable \
-Wno-error=unused-command-line-argument \
-Wno-error=bitwise-instead-of-logical

CFLAGS := $(LLVM_CFLAGS) $(GCC_CFLAGS) \
-O3 -g -Wall -Werror -Wno-nonnull -Wno-unused-function \
-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
LDFLAGS := -Wl,-static -Wl,--gc-sections
AUTH_CFLAGS=$(subst ckb-c-std-lib,ckb-c-stdlib-2023,$(CFLAGS)) -Wno-array-bounds

all: build/auth_demo

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

clean:
rm -rf build/auth_demo
3 changes: 2 additions & 1 deletion tests/auth_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,7 @@ impl Auth for SolanaAuth {
let _keypair_json = solana_sdk::signer::keypair::write_keypair(&self.key_pair, child_stdin)
.expect("Must write keypair");
// Close stdin to finish and avoid indefinite blocking
#[allow(dropping_references)]
drop(child_stdin);

let output = child.wait_with_output().expect("Wait for output");
Expand Down Expand Up @@ -1788,7 +1789,7 @@ impl Auth for Secp256r1Auth {
use p256::ecdsa::{signature::Signer, Signature};

let pub_key = self.get_pub_key_bytes();
let hash = calculate_sha256(msg.as_bytes());
let _hash = calculate_sha256(msg.as_bytes());

// Note by default, p256 will sign the sha256 hash of the message.
// So we don't need to do any hashing here.
Expand Down