Skip to content

Commit

Permalink
Fix bug: When using dynamic linking, auth will be loaded repeatedly.
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 committed Dec 8, 2023
1 parent 899f837 commit d6656ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
15 changes: 10 additions & 5 deletions c/ckb_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,24 @@ typedef int (*ckb_auth_validate_t)(uint8_t auth_algorithm_id,
uint32_t pubkey_hash_size);

static uint8_t g_code_buff[300 * 1024] __attribute__((aligned(RISCV_PGSIZE)));
static void* g_code_handle;

int ckb_auth(CkbEntryType *entry, CkbAuthType *id, const uint8_t *signature,
uint32_t signature_size, const uint8_t *message32) {
int err = 0;
if (entry->entry_category == EntryCategoryDynamicLinking) {
void *handle = NULL;
size_t consumed_size = 0;
err = ckb_dlopen2(entry->code_hash, entry->hash_type, g_code_buff,
sizeof(g_code_buff), &handle, &consumed_size);
if (err != 0) return err;

if (!g_code_handle) {
err = ckb_dlopen2(entry->code_hash, entry->hash_type, g_code_buff,
sizeof(g_code_buff), &g_code_handle, &consumed_size);
if (err != 0) {
return err;
}
}

ckb_auth_validate_t func =
(ckb_auth_validate_t)ckb_dlsym(handle, "ckb_auth_validate");
(ckb_auth_validate_t)ckb_dlsym(g_code_handle, "ckb_auth_validate");
if (func == 0) {
return CKB_INVALID_DATA;
}
Expand Down
11 changes: 9 additions & 2 deletions tests/auth-c-lock/auth_c_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ int main() {
break;
}

return ckb_auth(&entry, &auth, lock_bytes_seg.ptr, lock_bytes_seg.size,
msg32);
ret = ckb_auth(&entry, &auth, lock_bytes_seg.ptr, lock_bytes_seg.size, msg32);
if (ret) {
return ret;
}
ret = ckb_auth(&entry, &auth, lock_bytes_seg.ptr, lock_bytes_seg.size, msg32);
if (ret) {
return ret;
}
return 0;
}
1 change: 1 addition & 0 deletions tests/auth-rust-lock/contracts/auth-rust-demo/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn main() -> Result<(), Error> {
.unwrap(),
};

ckb_auth(&entry, &id, &signature, &message)?;
ckb_auth(&entry, &id, &signature, &message)?;

Ok(())
Expand Down

0 comments on commit d6656ec

Please sign in to comment.