Skip to content

Commit f2f53a1

Browse files
committed
Merge #307: various FFI cleanups and soundness fixes
405487e ffi: move Raw* structs out of FFI crate (Andrew Poelstra) c42a375 ffi: don't use empty enums for opaque types (Andrew Poelstra) 2d8c56e ffi: remove c_set_rawElementsTapEnv (Andrew Poelstra) 4bc03bf ffi: remove c_set_rawElementsTransaction (Andrew Poelstra) b0e8484 ffi: remove c_set_rawBuffer method (Andrew Poelstra) 5dab248 ffi: delete the c_set_rawElementsInput method (Andrew Poelstra) 32c66e0 ffi: store pegin genesis hash in RawInputData (Andrew Poelstra) 224645a ffi: delete the c_set_rawElementsOutput method (Andrew Poelstra) 818b380 ffi: replace some "pointer to assumed length" types with optional references (Andrew Poelstra) e23a1b3 ffi: don't flatten CRawInput structure (Andrew Poelstra) 66a468c simplicity-sys: delete dead links to C code (Andrew Poelstra) a925cd9 simplicity-sys: move all the elements FFI stuff into its own module (Andrew Poelstra) 78568e6 simplicity_sys: rename c_env.rs to c_env/mod.rs (Andrew Poelstra) Pull request description: Currently our transaction environment construction is quite complicated, and (as a consequence) unsound in a couple of places. This was implemented in PRs #49 through #52 in the early days of this crate with very little review, using a strategy copied from Haskell but not really appropriate for Rust. In particular: * We do not need custom marshalling functions in Rust because we can directly define `repr(C)` structs * ...in doing so, we may use Rust references in place of raw pointers, as long as the C code upholds the Rust aliasing rules (which it does, at least for these read-only POD types); in fact we may use optional references in place of nullable pointers * ...and we can use arrays and pointers to arrays in place of pointers to primitive types which are supposed to point to a specific number of elements * ...but we must make sure that any data we produce a pointer or reference to outlives the pointer/reference! There was at least one instance where we were creating a temporary variable and extracting a pointer from it, which would then outlive the object it pointed into. (This occurred for the annex, though in dead code, and for the pegin genesis hash, in live code that has never been used.) As an aside, we used empty enums in a couple places for opaque types. We should not do this, according to the nomicon. ACKs for top commit: canndrew: ACK 405487e Tree-SHA512: 76e08be787fdfa99ccbe3778cf5553c671fa8b556f071658e9dd1c58c0d0dff8aa1a132b77f3e12eea018dbdb47906e0c0e4d3135c53948a2472d4d16c927237
2 parents 6bdcd04 + 405487e commit f2f53a1

File tree

9 files changed

+382
-507
lines changed

9 files changed

+382
-507
lines changed

simplicity-sys/depend/env.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,6 @@ const size_t rustsimplicity_0_5_c_alignof_rawElementsTransaction = alignof(rawEl
2525
const size_t rustsimplicity_0_5_c_alignof_rawElementsTapEnv = alignof(rawElementsTapEnv);
2626
const size_t rustsimplicity_0_5_c_alignof_txEnv = alignof(txEnv);
2727

28-
void rustsimplicity_0_5_c_set_rawElementsBuffer(rawElementsBuffer *result, const unsigned char *buf, unsigned int len)
29-
{
30-
*result = (rawElementsBuffer){.buf = buf, .len = len};
31-
}
32-
33-
void rustsimplicity_0_5_c_set_rawElementsOutput(rawElementsOutput *result, const unsigned char *asset, const unsigned char *value, const unsigned char *nonce, const rawElementsBuffer *scriptPubKey,
34-
const rawElementsBuffer *surjectionProof, const rawElementsBuffer *rangeProof)
35-
{
36-
*result = (rawElementsOutput){.asset = asset, .value = value, .nonce = nonce, .scriptPubKey = *scriptPubKey, .surjectionProof = *surjectionProof, .rangeProof = *rangeProof};
37-
}
38-
39-
void rustsimplicity_0_5_c_set_rawElementsInput(rawElementsInput *result, const rawElementsBuffer *annex, const unsigned char *pegin, const rawElementsBuffer *scriptSig,
40-
const unsigned char *prevTxid, unsigned int prevIx,
41-
const unsigned char *asset, const unsigned char *value, const rawElementsBuffer *scriptPubKey,
42-
unsigned int sequence,
43-
const unsigned char *blindingNonce, const unsigned char *assetEntropy, const unsigned char *amount, const unsigned char *inflationKeys,
44-
const rawElementsBuffer *amountRangePrf, const rawElementsBuffer *inflationKeysRangePrf)
45-
{
46-
*result = (rawElementsInput){.annex = annex, .scriptSig = *scriptSig, .prevTxid = prevTxid, .pegin = pegin, .issuance = {.blindingNonce = blindingNonce, .assetEntropy = assetEntropy, .amount = amount, .inflationKeys = inflationKeys, .amountRangePrf = *amountRangePrf, .inflationKeysRangePrf = *inflationKeysRangePrf}, .txo = {.asset = asset, .value = value, .scriptPubKey = *scriptPubKey}, .prevIx = prevIx, .sequence = sequence};
47-
}
48-
49-
void rustsimplicity_0_5_c_set_rawElementsTransaction(rawElementsTransaction *result, unsigned int version,
50-
const unsigned char *txid,
51-
const rawElementsInput *input, unsigned int numInputs,
52-
const rawElementsOutput *output, unsigned int numOutputs,
53-
unsigned int lockTime)
54-
{
55-
*result = (rawElementsTransaction){
56-
.version = version,
57-
.txid = txid,
58-
.input = input,
59-
.numInputs = numInputs,
60-
.output = output,
61-
.numOutputs = numOutputs,
62-
.lockTime = lockTime,
63-
};
64-
}
65-
66-
void rustsimplicity_0_5_c_set_rawElementsTapEnv(rawElementsTapEnv *result, const unsigned char *controlBlock, unsigned char pathLen, const unsigned char *scriptCMR)
67-
{
68-
*result = (rawElementsTapEnv){.controlBlock = controlBlock, .pathLen = pathLen, .scriptCMR = scriptCMR};
69-
}
70-
7128
void rustsimplicity_0_5_c_set_txEnv(txEnv *result, const elementsTransaction *tx, const elementsTapEnv *taproot, const unsigned char *genesisHash, unsigned int ix)
7229
{
7330
sha256_midstate genesis;

simplicity-sys/src/c_jets/c_env.rs

Lines changed: 0 additions & 296 deletions
This file was deleted.

0 commit comments

Comments
 (0)