diff --git a/README.md b/README.md index 263cd8f..b179087 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,13 @@ Account Meta information for accounts which are always the same given a Solana c Defines the parameter index for the destination instruction. If the account has been removed or replaced use a negative number. +A source instruction may arrive with fewer accounts than the index map covers: a client that follows a codama IDL's +omitted optional-account strategy leaves an absent trailing optional account out entirely (the Stake program's lockup +authority). This mapper maps such a source when every missing position is one the map drops, and refuses it, naming the +position and slot, when it would have to seat an account it was not given. The map does not say which dropped positions +are optional, so any dropped account may be missing; a missing one maps exactly as a supplied one would. The TypeScript +mapper (`ix-mapper-ts`) refuses every source shorter than its index map instead; the two agree on everything else. + ## Build & Tests ```shell diff --git a/ix-proxy/config/pitest/README.md b/ix-proxy/config/pitest/README.md index ab07545..92a060a 100644 --- a/ix-proxy/config/pitest/README.md +++ b/ix-proxy/config/pitest/README.md @@ -98,7 +98,7 @@ in HARDENING.md); the baseline CSVs carry the exact keys. normalizes a single-entry meta array back to the single-table form — the resulting transaction is identical; only the internal meta wrapper's identity differs, which no property-level assertion should pin. -- `# single-variant-guard` (2 rows, `IxMapConfig.createProxy` line 58): the +- `# single-variant-guard` (2 rows, `IxMapConfig.createProxy` line 67): the false direction of both operands of `proxyType != null && proxyType != ProxyType.PAYER`. `ProxyType` has a single constant, so "declared type is not PAYER" is unsatisfiable and the @@ -118,7 +118,7 @@ in HARDENING.md); the baseline CSVs carry the exact keys. diagnostics with no functional effect, and pinning it would mean asserting on a logging backend, a test that restates the implementation. - `# empty-copy-equivalent` (2 rows, `IxProxyRecord.mapInstructionUnchecked` - line 80): `len > 0` guards a payload `System.arraycopy`; at `len == 0` + line 103): `len > 0` guards a payload `System.arraycopy`; at `len == 0` (instruction data is exactly the discriminator) the copy is a zero-length no-op, so both the boundary flip and the forced-true direction are behaviourally identical. `len < 0` is unreachable — the proxy diff --git a/ix-proxy/config/pitest/ixProxy-accepted.csv b/ix-proxy/config/pitest/ixProxy-accepted.csv index 6d1c410..cb47915 100644 --- a/ix-proxy/config/pitest/ixProxy-accepted.csv +++ b/ix-proxy/config/pitest/ixProxy-accepted.csv @@ -3,8 +3,8 @@ systems.glam.ix.proxy.BaseIxProxy,validateMapping,RemoveConditionalMutator_EQUAL systems.glam.ix.proxy.ConfigLoader$Worker,get,VoidMethodCallMutator,SURVIVED # log-only # line 142 systems.glam.ix.proxy.IxMapConfig,createProxy,RemoveConditionalMutator_EQUAL_ELSE,SURVIVED # single-variant-guard # line 67 systems.glam.ix.proxy.IxMapConfig,createProxy,RemoveConditionalMutator_EQUAL_ELSE,SURVIVED # single-variant-guard # line 67 -systems.glam.ix.proxy.IxProxyRecord,mapInstructionUnchecked,ConditionalsBoundaryMutator,SURVIVED # empty-copy-equivalent # line 90 -systems.glam.ix.proxy.IxProxyRecord,mapInstructionUnchecked,RemoveConditionalMutator_ORDER_IF,SURVIVED # empty-copy-equivalent # line 90 +systems.glam.ix.proxy.IxProxyRecord,mapInstructionUnchecked,ConditionalsBoundaryMutator,SURVIVED # empty-copy-equivalent # line 103 +systems.glam.ix.proxy.IxProxyRecord,mapInstructionUnchecked,RemoveConditionalMutator_ORDER_IF,SURVIVED # empty-copy-equivalent # line 103 systems.glam.ix.proxy.ProgramProxyMap,mapTransaction,RemoveConditionalMutator_EQUAL_ELSE,SURVIVED # defensive-null-tables # line 60 systems.glam.ix.proxy.ProgramProxyMap,mapTransaction,RemoveConditionalMutator_EQUAL_IF,SURVIVED # defensive-null-tables # line 60 systems.glam.ix.proxy.ProgramProxyMap,mapTransactionWithTable,RemoveConditionalMutator_EQUAL_ELSE,SURVIVED # defensive-null-tables # line 89 diff --git a/ix-proxy/src/main/java/systems/glam/ix/proxy/IxProxyRecord.java b/ix-proxy/src/main/java/systems/glam/ix/proxy/IxProxyRecord.java index da25875..e2ea7e4 100644 --- a/ix-proxy/src/main/java/systems/glam/ix/proxy/IxProxyRecord.java +++ b/ix-proxy/src/main/java/systems/glam/ix/proxy/IxProxyRecord.java @@ -44,7 +44,20 @@ public Instruction mapInstructionUnchecked(final AccountMeta readCpiProgram, final Instruction instruction) { final var accounts = instruction.accounts(); final int numAccounts = accounts.size(); - final int numExtraAccounts = numAccounts - indexes.length; + // A source may arrive shorter than the index map: a client leaves an absent trailing + // optional account out under the omitted strategy. Each missing position must be one the + // map drops; one the proxy seats has no account to seat. The map does not say which + // dropped positions are optional, so any dropped account may be missing: a missing one + // maps exactly as a supplied one would, dropped. (The TypeScript mapper refuses every + // short source instead; README, index_map.) + for (int s = numAccounts; s < indexes.length; ++s) { + if (indexes[s] >= 0) { + throw new IllegalStateException(String.format( + "Instruction supplies %d accounts, but the proxy seats account %d at index %d.", numAccounts, s, indexes[s] + )); + } + } + final int numExtraAccounts = Math.max(0, numAccounts - indexes.length); final var mappedAccounts = new AccountMeta[this.numAccounts + numExtraAccounts]; for (final var dynamicAccount : dynamicAccounts) { @@ -75,7 +88,7 @@ public Instruction mapInstructionUnchecked(final AccountMeta readCpiProgram, } } - // Copy extra accounts. + // Copy extra accounts; a missing position above was dropped, so none was read. m = this.numAccounts; for (; s < numAccounts; ++s, ++m) { mappedAccounts[m] = accounts.get(s); diff --git a/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperFuzz.java b/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperFuzz.java index e8faebd..f61c11d 100644 --- a/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperFuzz.java +++ b/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperFuzz.java @@ -144,14 +144,20 @@ public static void fuzzerTestOneInput(final byte[] data) { final Instruction mapped; try { mapped = programProxy.mapInstruction(FEE_PAYER, null, instruction); - } catch (final RuntimeException tolerated) { - // rejection is in contract; the unchecked path must also fail closed + } catch (final IllegalStateException tolerated) { + // rejection is in contract, by message; the unchecked path must fail closed the same way, + // never with an index or array error, which a catch-all once hid try { programProxy.mapInstructionUnchecked(FEE_PAYER, null, instruction); - } catch (final RuntimeException alsoTolerated) { + } catch (final IllegalStateException alsoTolerated) { } return; } + for (final var account : mapped.accounts()) { + if (account == null) { + throw new AssertionError("A mapped instruction carries a null account."); + } + } final int cpiLength = ixProxy.cpiDiscriminator().length(); final int proxyLength = ixProxy.proxyDiscriminator().length(); diff --git a/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperTest.java b/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperTest.java index 39b72dc..1a08052 100644 --- a/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperTest.java +++ b/ix-proxy/src/test/java/systems/glam/ix/proxy/IxMapperTest.java @@ -316,6 +316,11 @@ void shouldReturnNullForUnsupportedDiscriminator() { assertNull(mapToGlamIx(source, glamState, glamSigner)); } + /// The TypeScript mapper refuses any source shorter than its index_map; this port refuses + /// only a short source that leaves out an account the proxy seats (the System transfer's + /// missing account is one), and maps one whose missing positions the map drops, because a + /// client following a codama IDL's omitted strategy leaves absent trailing optionals out. + /// See README, index_map. @Test void shouldRejectMalformedInstructionWithTooFewKeys() { final var source = Instruction.createInstruction( diff --git a/ix-proxy/src/test/java/systems/glam/ix/proxy/OmittedTrailingAccountsTests.java b/ix-proxy/src/test/java/systems/glam/ix/proxy/OmittedTrailingAccountsTests.java new file mode 100644 index 0000000..61e886b --- /dev/null +++ b/ix-proxy/src/test/java/systems/glam/ix/proxy/OmittedTrailingAccountsTests.java @@ -0,0 +1,145 @@ +package systems.glam.ix.proxy; + +import org.junit.jupiter.api.Test; +import software.sava.core.accounts.PublicKey; +import software.sava.core.accounts.meta.AccountMeta; +import software.sava.core.tx.Instruction; +import systems.comodal.jsoniter.JsonIterator; + +import java.util.HashMap; +import java.util.List; +import java.util.function.Function; + +import static org.junit.jupiter.api.Assertions.*; + +/// A source instruction may arrive with fewer accounts than the index map covers: under the +/// omitted optional-account strategy a client leaves an absent trailing optional out +/// entirely (the Stake program's lockup authority). The proxy maps what arrives when every +/// missing position is one the map drops, and refuses by message when it would have to seat +/// an account it was not given. +final class OmittedTrailingAccountsTests { + + private static final PublicKey CPI_PROGRAM = key(30); + private static final AccountMeta READ_CPI_PROGRAM = AccountMeta.createRead(CPI_PROGRAM); + private static final AccountMeta INVOKED_PROXY = AccountMeta.createInvoked(key(31)); + private static final AccountMeta FEE_PAYER = AccountMeta.createFeePayer(key(32)); + private static final PublicKey STATIC_BEFORE = key(33); + private static final PublicKey STATIC_AFTER = key(34); + private static final Function> FACTORY = + DynamicAccountConfig::createFeePayerAccount; + + private static PublicKey key(final int marker) { + final byte[] bytes = new byte[PublicKey.PUBLIC_KEY_LENGTH]; + bytes[0] = (byte) marker; + return PublicKey.createPubKey(bytes); + } + + /// Proxy layout: 0 fee payer, 1 static, 2 the source's first account, 3 static. The source + /// declares three accounts, the last two dropped (an authority the payer stands in for and + /// a trailing optional), the shape of a Stake authorize through GLAM, whose map is [4, -1, -1]. + private static IxProxy proxy() { + final var config = IxMapConfig.parseConfig(new HashMap<>(), new HashMap<>(), JsonIterator.parse(""" + { + "src_ix_name": "authorize", + "src_discriminator": [1, 0, 0, 0], + "dst_ix_name": "stake_authorize", + "dst_discriminator": [1, 2, 3, 4, 5, 6, 7, 8], + "dynamic_accounts": [ { "name": "glam_signer", "index": 0, "writable": true, "signer": true } ], + "static_accounts": [ + { "account": "%s", "index": 1, "writable": false, "signer": false }, + { "account": "%s", "index": 3, "writable": false, "signer": false } + ], + "index_map": [2, -1, -1] + } + """.formatted(STATIC_BEFORE.toBase58(), STATIC_AFTER.toBase58()))); + return config.createProxy(INVOKED_PROXY, FACTORY); + } + + private static Instruction source(final int numAccounts) { + final var accounts = new AccountMeta[numAccounts]; + for (int i = 0; i < numAccounts; ++i) { + accounts[i] = AccountMeta.createWrite(key(40 + i)); + } + return Instruction.createInstruction(CPI_PROGRAM, List.of(accounts), new byte[]{1, 0, 0, 0, 9}); + } + + private static List keys(final Instruction mapped) { + return mapped.accounts().stream().map(AccountMeta::publicKey).toList(); + } + + @Test + void aSourceWithEveryDeclaredAccountMapsAsBefore() { + final var mapped = proxy().mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(3)); + assertEquals(List.of(FEE_PAYER.publicKey(), STATIC_BEFORE, key(40), STATIC_AFTER), keys(mapped)); + } + + @Test + void aSourceThatLeavesTrailingDroppedAccountsOutMapsTheSame() { + for (final int supplied : new int[]{2, 1}) { + final var mapped = proxy().mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(supplied)); + assertEquals(List.of(FEE_PAYER.publicKey(), STATIC_BEFORE, key(40), STATIC_AFTER), keys(mapped), supplied + " accounts"); + assertEquals(4, mapped.accounts().size(), supplied + " accounts"); + } + } + + @Test + void aSourceWithAccountsBeyondTheMapPassesThemThroughAsRemaining() { + final var mapped = proxy().mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(4)); + assertEquals(List.of(FEE_PAYER.publicKey(), STATIC_BEFORE, key(40), STATIC_AFTER, key(43)), keys(mapped)); + } + + @Test + void aSourceMissingAnAccountTheProxySeatsIsRefusedByMessage() { + final var refused = assertThrows(IllegalStateException.class, + () -> proxy().mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(0))); + assertEquals("Instruction supplies 0 accounts, but the proxy seats account 0 at index 2.", refused.getMessage()); + } + /// Index zero is a seat like any other: a source that leaves out an account the proxy maps + /// there is refused, not treated as having nothing to seat. + @Test + void aRequiredAccountSeatedAtIndexZeroIsNotOmittable() { + final var config = IxMapConfig.parseConfig(new HashMap<>(), new HashMap<>(), JsonIterator.parse(""" + { + "src_ix_name": "pass", + "src_discriminator": [1, 0, 0, 0], + "dst_ix_name": "proxy_pass", + "dst_discriminator": [8, 7, 6, 5, 4, 3, 2, 1], + "dynamic_accounts": [ { "name": "glam_signer", "index": 1, "writable": true, "signer": true } ], + "static_accounts": [], + "index_map": [0] + } + """)); + final var proxy = config.createProxy(INVOKED_PROXY, FACTORY); + final var mapped = proxy.mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(1)); + assertEquals(List.of(key(40), FEE_PAYER.publicKey()), keys(mapped)); + final var refused = assertThrows(IllegalStateException.class, + () -> proxy.mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(0))); + assertEquals("Instruction supplies 0 accounts, but the proxy seats account 0 at index 0.", refused.getMessage()); + } + /// A dropped position ahead of a seated one does not hide it: what decides is which + /// positions are missing, not how many. A check by counts ("missing no more than dropped") + /// would return an instruction with a null account here. + @Test + void aDroppedMissingAccountAheadOfASeatedOneDoesNotHideIt() { + final var config = IxMapConfig.parseConfig(new HashMap<>(), new HashMap<>(), JsonIterator.parse(""" + { + "src_ix_name": "authorize", + "src_discriminator": [1, 0, 0, 0], + "dst_ix_name": "stake_authorize", + "dst_discriminator": [1, 2, 3, 4, 5, 6, 7, 8], + "dynamic_accounts": [ { "name": "glam_signer", "index": 0, "writable": true, "signer": true } ], + "static_accounts": [ + { "account": "%s", "index": 1, "writable": false, "signer": false }, + { "account": "%s", "index": 3, "writable": false, "signer": false } + ], + "index_map": [-1, -1, 2, -1] + } + """.formatted(STATIC_BEFORE.toBase58(), STATIC_AFTER.toBase58()))); + final var proxy = config.createProxy(INVOKED_PROXY, FACTORY); + final var mapped = proxy.mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(3)); + assertEquals(List.of(FEE_PAYER.publicKey(), STATIC_BEFORE, key(42), STATIC_AFTER), keys(mapped)); + final var refused = assertThrows(IllegalStateException.class, + () -> proxy.mapInstruction(READ_CPI_PROGRAM, FEE_PAYER, null, source(1))); + assertEquals("Instruction supplies 1 accounts, but the proxy seats account 2 at index 2.", refused.getMessage()); + } +} diff --git a/ix-proxy/src/test/resources/fuzz/README.md b/ix-proxy/src/test/resources/fuzz/README.md index 443c40a..e493e1b 100644 --- a/ix-proxy/src/test/resources/fuzz/README.md +++ b/ix-proxy/src/test/resources/fuzz/README.md @@ -30,6 +30,9 @@ path they pin: index). - `identity-fixed` / `identity-variable` — pass-through on each lookup shape. - `rewrite-variable` — a discriminator-and-data rewrite. +- `short-source-dropped` — program A with one account against a map whose + missing positions are dropped: the success path of a source shorter than + its index map. - `unknown-program` / `payer-no-accounts` / `short-discriminator` — the pass-through and rejection edges. diff --git a/ix-proxy/src/test/resources/fuzz/ixMapper/short-source-dropped b/ix-proxy/src/test/resources/fuzz/ixMapper/short-source-dropped new file mode 100644 index 0000000..0997a06 Binary files /dev/null and b/ix-proxy/src/test/resources/fuzz/ixMapper/short-source-dropped differ