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

Add secp256k1-sys with prefixed sources #169

Merged
merged 6 commits into from
Dec 5, 2019

Conversation

stevenroose
Copy link
Contributor

I'm adding a secp256k1-sys crate with just the FFI wrapper of the C libsecp.

And then I added a script (thanks @jonasnick) to prefix the secp sources with a rust-secp specific tag and a version tag, so that we don't suffer from naming conflicts anymore.

@stevenroose stevenroose changed the title Add secp256k1-sys WIP: Add secp256k1-sys Oct 21, 2019
@elichai
Copy link
Member

elichai commented Oct 21, 2019

idk about this.
but first of all you brought back all the malloc functions we removed in #130
And even about the prefix, there are people who do want to compile this code as libsecp (i.e. bitcoin-consensus and stuff @TheBlueMatt is working on). although in theory LTO should take care of the duplicates.

Even if other people do want this i'll split it to ~3 PRs. because right now it's not auditable.
(i.e. one PR creating the *-sys crate, another move only another adds the prefix)

@stevenroose
Copy link
Contributor Author

but first of all you brought back all the malloc functions we removed in #130

That was an accident. Fixed that with patch files.

Even if other people do want this i'll split it to ~3 PRs. because right now it's not auditable.

What's wrong with one PR with 3 commits? The creating the -sys crate and moving the dep is not possible to do in 2 commits without having a giant diff of first adding the entire depend folder a second time in another place and then removing the original in a second commit instead of having git treat them as a move.

I can do that if that is favorable, but that was the reason I did the -sys crate together with migrating secp to -sys in the same commit.

@stevenroose stevenroose changed the title WIP: Add secp256k1-sys Add secp256k1-sys Oct 23, 2019
@stevenroose stevenroose changed the title Add secp256k1-sys Add secp256k1-sys with prefixed sources Oct 23, 2019
Copy link
Collaborator

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

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

Good catch @elichai, bitcoin-consensus relies on the secp symbol names (because it links libbitcoinsensus with libsecp which has been provided with rust-secp). Not sure what's the best way to solve this.

Fwiw I don't find this PR impossible to review and not sure how 3 PRs would help.

How would LTO take care of this? It doesn't, by default. Also there could be multiple versions of the library in play which can't be solved automatically.

@stevenroose travis doesn't run the -sys tests. It's just a meaningless single test, but would be better to add to travis, so we don't forget it in the future.

secp256k1-sys/vendor-libsecp.sh Outdated Show resolved Hide resolved
@@ -764,3 +785,4 @@ mod tests {
assert_eq!(orig.len(), unsafe {strlen(test.as_ptr())});
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

is that intentional? you've done that a couple of times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the ending newline? no idea, I think my vim might not like files without them perhaps? can I consider this a nit? :)

secp256k1-sys/src/macros.rs Show resolved Hide resolved
@elichai
Copy link
Member

elichai commented Oct 24, 2019

@jonasnick I forgot about reviewing commit by commit, I'll try that. Sorry.

Isn't the whole point of LTO is to remove code duplication? So if you link against two versions of libsecp but with different symbols then most of the duplication should disappear it you use LTO.

@@ -2,13 +2,13 @@ ACLOCAL_AMFLAGS = -I build-aux/m4

Copy link
Member

@elichai elichai Oct 24, 2019

Choose a reason for hiding this comment

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

FYI, we don't use the makefile/autotools. so there's no actual reason to change anything there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is just the auto replace at work.

@@ -5,8 +5,8 @@ import sys
load("group_prover.sage")
Copy link
Member

Choose a reason for hiding this comment

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

Sage too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This basically checks out libsecp in the depends/secp256k1 folder and then changes the variables and removes the allocation code. Nothing more. So yeah some unused artifacts are there.

@jonasnick
Copy link
Collaborator

jonasnick commented Oct 24, 2019

@elichai just to clarify, are you aware that this PR contains a script secp256k1-sys/vendor-libsecp.sh that just replaces all references of secp256k1 with rustsecp256k1_v0_1_0?

@elichai
Copy link
Member

elichai commented Oct 24, 2019

@jonasnick well we still need to review that the script did it's job currently and then the person who ran it didn't change anything.

Is it possible in C to have private functions? or all functions are "public" so we need to mangle all their names? even inner functions

@jonasnick
Copy link
Collaborator

then the person who ran it didn't change anything.

You can verify the script then run it yourself. But okay.

Just renaming may introduce bugs that are not caught by the compiler. But that seems highly unlikely and will also be difficult to notice in manual review.

even inner functions

Are you suggesting to have a more complicated script that only looks at the exported symbols? Why?

@stevenroose
Copy link
Contributor Author

@elichai so you can run the following command to verify the depends folder:

cd ./secp256k1-sys/
./vendor-libsecp.sh depend 0_1_0 143dc6e9ee31852a60321b23eea407d2006171da

The commit has is committed in the ./secp256k1-sys/depend/secp256k1-HEAD-revision.txt file.

Copy link
Collaborator

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

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

Approach ACK

My feeling is that the entire approach needs some README file or similar. What is done here, and the reason why it's done is really not trivial. People not familiar which the project will think we're crazy.

secp256k1-sys/src/lib.rs Outdated Show resolved Hide resolved
secp256k1-sys/vendor-libsecp.sh Outdated Show resolved Hide resolved
secp256k1-sys/vendor-libsecp.sh Outdated Show resolved Hide resolved
secp256k1-sys/vendor-libsecp.sh Show resolved Hide resolved
@real-or-random
Copy link
Collaborator

If we anyway clone secp256k1, could we remove it entirely? Probably not because then you need to be online for building and we trust Github more? It's just a wild thought, please ignore me if this is stupid.

@elichai
Copy link
Member

elichai commented Oct 29, 2019

@stevenroose I tried to link this against upstream and ecdsa_signature_parse_der_lax is missing.
I guess we should build that. because it's not an official part of upstream.

@TheBlueMatt How are you fixing this here? (I tried turning LTO on but I still get link errors)


# Use this feature to not compile the bundled libsecp256k1 C symbols,
# but use external ones.
external-symbols = ["secp256k1-sys/external-symbols"]
Copy link
Member

Choose a reason for hiding this comment

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

Can you just not depend on secp256k1-sys at all if you build with external-symbols? pulling in a dummy crate as a dependency sucks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The crate is still defining the C interface and the FFI objects used in it. In fact it's not doing less than when built without external-symbols, it's just the build.rs construction is no longer utilized.

Copy link
Member

Choose a reason for hiding this comment

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

At least we should add a scarier warning.

Also, as you noted in another channel, this has the potential to cause UB a user is using an old version of rust-secp, as well as this one with external-symbols enabled...so we can't depend on cargo failing if this is enabled on a cargo build.

@stevenroose
Copy link
Contributor Author

If we anyway clone secp256k1, could we remove it entirely? Probably not because then you need to be online for building and we trust Github more? It's just a wild thought, please ignore me if this is stupid.

We're only cloning it when we revendor. That only needs to happen when updated to a new version. The sources are still required for being able to build.

@apoelstra
Copy link
Member

When we merge this let's be sure to get #162 in as well.

@stevenroose
Copy link
Contributor Author

Any updates on this?

@@ -365,7 +386,7 @@ unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
/// Rust doesn't promise what pointers does it give to ZST (https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
/// In case the type is empty this trait will give a NULL pointer, which should be handled in C.
///
pub(crate) trait CPtr {
pub trait CPtr {
Copy link
Member

Choose a reason for hiding this comment

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

In a late PR I'll try move this to secp256k1 and not -sys and make it pub(crate) again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this not used in -sys? In that case I can move it.. The idea is to try and reduce the times we need to bump a -sys version. So I think better make breaking changes now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, seems to be used in macros as well. Yeah I think I'd prefer to merge this as is :)

Copy link
Member

Choose a reason for hiding this comment

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

Merge != release.
let's not change this PR any more so it can get merged at some point :)

elichai
elichai previously approved these changes Nov 12, 2019
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK code review.
ACK reproduced the same changes by the script.
0093a5d

I'm still not so sure about this script but I guess that's the best we can do right now.

elichai
elichai previously approved these changes Nov 13, 2019
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK on the diff

elichai
elichai previously approved these changes Nov 27, 2019
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK on latest diff

apoelstra
apoelstra previously approved these changes Nov 27, 2019
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK. I was suspicious about this script but it's actually pretty great.

@real-or-random
Copy link
Collaborator

needs rebase :/

Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK
Reviewed the diff from my last ACK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants