Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 85e481a

Browse files
Preprocess contracts (#152)
* Call endpoints * Fix LLVM backend crash
1 parent 06018e6 commit 85e481a

38 files changed

+876
-40
lines changed

Makefile

+55
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ UKM_NO_CONTRACT_TESTING_OUTPUT_DIR ::= .build/ukm-no-contract/output
8282
UKM_NO_CONTRACT_TESTING_INPUTS ::= $(wildcard $(UKM_NO_CONTRACT_TESTING_INPUT_DIR)/*.run)
8383
UKM_NO_CONTRACT_TESTING_OUTPUTS ::= $(patsubst $(UKM_NO_CONTRACT_TESTING_INPUT_DIR)/%,$(UKM_NO_CONTRACT_TESTING_OUTPUT_DIR)/%.executed.kore,$(UKM_NO_CONTRACT_TESTING_INPUTS))
8484

85+
UKM_WITH_CONTRACT_TESTING_INPUT_DIR ::= tests/ukm-with-contract
86+
UKM_WITH_CONTRACT_TESTING_OUTPUT_DIR ::= .build/ukm-with-contract/output
87+
UKM_WITH_CONTRACT_TESTING_INPUTS ::= $(wildcard $(UKM_WITH_CONTRACT_TESTING_INPUT_DIR)/*.run)
88+
UKM_WITH_CONTRACT_TESTING_OUTPUTS ::= $(patsubst $(UKM_WITH_CONTRACT_TESTING_INPUT_DIR)/%,$(UKM_WITH_CONTRACT_TESTING_OUTPUT_DIR)/%.executed.kore,$(UKM_WITH_CONTRACT_TESTING_INPUTS))
89+
8590
.PHONY: clean build build-legacy test test-legacy syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test mx-rust-two-contracts-test demos-test ukm-no-contracts-test
8691

8792
all: build test
@@ -127,6 +132,8 @@ demos-test: $(DEMOS_TESTING_OUTPUTS)
127132

128133
ukm-no-contracts-test: $(UKM_NO_CONTRACT_TESTING_OUTPUTS)
129134

135+
ukm-with-contracts-test: $(UKM_WITH_CONTRACT_TESTING_OUTPUTS)
136+
130137
$(RUST_PREPROCESSING_TIMESTAMP): $(RUST_SEMANTICS_FILES)
131138
# Workaround for https://github.com/runtimeverification/k/issues/4141
132139
-rm -r $(RUST_PREPROCESSING_KOMPILED)
@@ -424,3 +431,51 @@ $(UKM_NO_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
424431
-pTEST=$(CURDIR)/parsers/test-ukm-testing-execution.sh
425432
cat [email protected] | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
426433
434+
435+
436+
# TODO: Add $(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs
437+
# as a dependency
438+
$(UKM_WITH_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
439+
$(UKM_WITH_CONTRACT_TESTING_INPUT_DIR)/%.run \
440+
$(UKM_CONTRACTS_TESTING_INPUT_DIR)/bytes_hooks.rs \
441+
$(UKM_CONTRACTS_TESTING_INPUT_DIR)/ukm.rs \
442+
$(UKM_TESTING_TIMESTAMP) \
443+
$(wildcard parsers/inc-*.sh) \
444+
parsers/crates-ukm-testing-execution.sh \
445+
parsers/test-ukm-testing-execution.sh
446+
mkdir -p $(UKM_WITH_CONTRACT_TESTING_OUTPUT_DIR)
447+
448+
echo "<(<" > [email protected]
449+
echo "::bytes_hooks" >> [email protected]
450+
echo "<|>" >> [email protected]
451+
cat $(UKM_CONTRACTS_TESTING_INPUT_DIR)/bytes_hooks.rs >> [email protected]
452+
echo ">)>" >> [email protected]
453+
454+
echo "<(<" >> [email protected]
455+
echo "::state_hooks" >> [email protected]
456+
echo "<|>" >> [email protected]
457+
cat $(UKM_CONTRACTS_TESTING_INPUT_DIR)/state_hooks.rs >> [email protected]
458+
echo ">)>" >> [email protected]
459+
460+
echo "<(<" >> [email protected]
461+
echo "::ukm" >> [email protected]
462+
echo "<|>" >> [email protected]
463+
cat $(UKM_CONTRACTS_TESTING_INPUT_DIR)/ukm.rs >> [email protected]
464+
echo ">)>" >> [email protected]
465+
466+
echo "<(<" >> [email protected]
467+
echo "$<" | sed 's%^.*/%%' | sed 's/\..*//' | sed 's/^/::/' >> [email protected]
468+
echo "<|>" >> [email protected]
469+
cat "$(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs" >> [email protected]
470+
echo ">)>" >> [email protected]
471+
472+
krun \
473+
474+
--parser $(CURDIR)/parsers/crates-ukm-testing-execution.sh \
475+
--definition $(UKM_TESTING_KOMPILED) \
476+
--output kore \
477+
--output-file [email protected] \
478+
-cTEST='$(shell cat $<)' \
479+
-pTEST=$(CURDIR)/parsers/test-ukm-testing-execution.sh
480+
cat [email protected] | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
481+

mx-rust-semantics/main/preprocessing/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module MX-RUST-PREPROCESSED-CONTRACT-TRAIT-CONFIGURATION
1717
imports RUST-SHARED-SYNTAX
1818
1919
configuration
20-
<mx-rust-contract-trait> (#token("no#path", "Identifier"):Identifier):TypePath </mx-rust-contract-trait> // String to Identifier
20+
<mx-rust-contract-trait> (#token("no#path", "Identifier"):Identifier):TypePath </mx-rust-contract-trait>
2121
endmodule
2222
2323
module MX-RUST-PREPROCESSED-PROXIES-CONFIGURATION

rust-semantics/error.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module RUST-ERROR-SYNTAX
55
66
syntax ValueListOrError ::= concat(ValueOrError, ValueListOrError) [function, total]
77
syntax PtrListOrError ::= concat(Ptr, PtrListOrError) [function, total]
8+
syntax StringOrError ::= concat(StringOrError, StringOrError) [function, total]
9+
syntax NonEmptyStatementsOrError ::= concat(NonEmptyStatementsOrError, NonEmptyStatementsOrError) [function, total]
810
syntax PtrValueOrError ::= wrapPtrValueOrError(Ptr, ValueOrError) [function, total]
911
syntax TypePathOrError ::= doubleColonOrError(TypePathSegmentsOrError) [function, total]
1012
syntax TypePathOrError ::= injectOrError(TypePathSegmentsOrError) [function, total]
@@ -22,10 +24,18 @@ module RUST-ERROR
2224
rule concat(V:Value, L:ValueList) => V , L
2325
rule concat(_:Value, E:SemanticsError) => E
2426
rule concat(E:SemanticsError, _:ValueListOrError) => E
25-
rule concat(E:ValueOrError, L:ValueListOrError)
26-
=> error("unexpected branch (concat(ValueOrError, ValueListOrError))", ListItem(E) ListItem(L))
27+
28+
rule concat(S1:String:StringOrError, S2:String:StringOrError) => S1 +String S2
29+
rule concat(_:String:StringOrError, E:SemanticsError:StringOrError) => E
30+
rule concat(E:SemanticsError:StringOrError, _:StringOrError) => E
31+
rule concat(S1:StringOrError, S2:StringOrError)
32+
=> error("concat(StringOrError, StringOrError): Unknown error", ListItem(S1) ListItem(S2))
2733
[owise]
2834
35+
rule concat(S1:NonEmptyStatements, S2:NonEmptyStatements) => concatNonEmptyStatements(S1, S2)
36+
rule concat(_:NonEmptyStatements, E:SemanticsError) => E
37+
rule concat(E:SemanticsError, _:NonEmptyStatementsOrError) => E
38+
2939
rule wrapPtrValueOrError(P:Ptr, V:Value) => ptrValue(P, V)
3040
rule wrapPtrValueOrError(_:Ptr, E:SemanticsError) => E
3141
@@ -38,9 +48,9 @@ module RUST-ERROR
3848
rule concat(_:TypePathSegment, E) => E
3949
rule concat(S:TypePathSegment, Ss:TypePathSegments) => S :: Ss
4050
41-
rule andOrError(_:ExpressionOrError, E:SemanticsError) => E
42-
rule andOrError(E:SemanticsError, _:Expression) => E
43-
rule andOrError(E1:Expression, E2:Expression) => E1 && E2
51+
rule andOrError(_:ExpressionOrError, e(E:SemanticsError)) => e(E)
52+
rule andOrError(e(E:SemanticsError), v(_:Expression)) => e(E)
53+
rule andOrError(v(E1:Expression), v(E2:Expression)) => v(E1 && E2)
4454
4555
rule tupleOrError(L:ValueList) => tuple(L)
4656
rule tupleOrError(E:SemanticsError) => E

rust-semantics/expression/comparisons.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@ module RUST-EXPRESSION-STRUCT-COMPARISONS
2323
rule (ptrValue(_, struct(StructName:TypePath, FirstFields:Map))
2424
== ptrValue(_, struct(StructName, SecondFields:Map))
2525
):Expression
26-
=> allPtrEquality
27-
( listToPtrList(values(FirstFields))
28-
, listToPtrList(values(SecondFields))
26+
=> unwrap
27+
( allPtrEquality
28+
( listToPtrList(values(FirstFields))
29+
, listToPtrList(values(SecondFields))
30+
)
2931
)
3032
[owise]
3133
3234
syntax ExpressionOrError ::= allPtrEquality(PtrListOrError, PtrListOrError)
3335
[function, total]
3436
35-
rule allPtrEquality(E:SemanticsError, _:PtrListOrError) => E
36-
rule allPtrEquality(_:PtrList, E:SemanticsError) => E
37+
rule allPtrEquality(E:SemanticsError, _:PtrListOrError) => e(E)
38+
rule allPtrEquality(_:PtrList, E:SemanticsError) => e(E)
3739
38-
rule allPtrEquality(.PtrList, .PtrList) => true
40+
rule allPtrEquality(.PtrList, .PtrList) => v(true)
3941
rule allPtrEquality((P:Ptr , Ps:PtrList), (Q:Ptr , Qs:PtrList))
40-
=> andOrError(P == Q , allPtrEquality(Ps, Qs))
42+
=> andOrError(v(P == Q) , allPtrEquality(Ps, Qs))
4143
4244
rule allPtrEquality(.PtrList, (_:Ptr , _:PtrList) #as Ps:PtrList)
43-
=> error("allPtrEquality: Second list too long", ListItem(Ps))
45+
=> e(error("allPtrEquality: Second list too long", ListItem(Ps)))
4446
rule allPtrEquality((_:Ptr , _:PtrList) #as Ps:PtrList, .PtrList)
45-
=> error("zip(PtrList): First list too long", ListItem(Ps))
47+
=> e(error("zip(PtrList): First list too long", ListItem(Ps)))
4648
4749
endmodule
4850

rust-semantics/expression/struct.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ module RUST-EXPRESSION-STRUCT
5757
rule <k>
5858
fromStructExpressionWithAssignmentsBuildFieldsMap(
5959
Name:TypePath,
60-
((FieldName:Identifier : LE:Expression):StructExprField, RS):StructExprFields,
60+
((FieldName:Identifier : Le:Expression):StructExprField, RS):StructExprFields,
6161
FieldsMap:Map)
62-
=> LE ~> FieldName ~> fromStructExpressionWithAssignmentsBuildFieldsMap(Name, RS, FieldsMap)
62+
=> Le ~> FieldName ~> fromStructExpressionWithAssignmentsBuildFieldsMap(Name, RS, FieldsMap)
6363
...
6464
</k>
6565

rust-semantics/helpers.md

+3
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ module RUST-HELPERS
4242
rule isSignedInt(u128) => true
4343
rule isSignedInt(&T => T)
4444
45+
rule concatNonEmptyStatements(.NonEmptyStatements, S:NonEmptyStatements) => S
46+
rule concatNonEmptyStatements(S:Statement Ss1:NonEmptyStatements, Ss2:NonEmptyStatements)
47+
=> S concatNonEmptyStatements(Ss1, Ss2)
4548
endmodule
4649
```

rust-semantics/preprocessing/configuration.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ module RUST-PREPROCESSING-CONFIGURATION
44
imports private RUST-REPRESENTATION
55
imports private RUST-SHARED-SYNTAX
66
7-
syntax Identifier ::= "my_identifier" [token]
8-
97
configuration
108
<preprocessed>
119
<constants>
@@ -17,7 +15,7 @@ module RUST-PREPROCESSING-CONFIGURATION
1715
<struct-list> .List </struct-list>
1816
<structs>
1917
<struct multiplicity="*" type="Map">
20-
<struct-path> my_identifier:TypePath </struct-path>
18+
<struct-path> #token("not#initialized", "Identifier"):Identifier:TypePath </struct-path>
2119
<field-list> .List </field-list> // List of Identifier
2220
<fields>
2321
<field multiplicity="*" type="Map">
@@ -30,14 +28,14 @@ module RUST-PREPROCESSING-CONFIGURATION
3028
<trait-list> .List </trait-list> // List of TypePath
3129
<traits>
3230
<trait multiplicity="*" type="Map">
33-
<trait-path> my_identifier:TypePath </trait-path>
31+
<trait-path> #token("not#initialized", "Identifier"):Identifier:TypePath </trait-path>
3432
<trait-attributes> `emptyOuterAttributes`(.KList):OuterAttributes </trait-attributes>
3533
<method-list> .List </method-list> // List of Identifier
3634
</trait>
3735
</traits>
3836
<methods>
3937
<method multiplicity="*" type="Map">
40-
<method-name> my_identifier:PathInExpression </method-name>
38+
<method-name> #token("not#initialized", "Identifier"):Identifier:PathInExpression </method-name>
4139
<method-params> .NormalizedFunctionParameterList </method-params>
4240
<method-return-type> ():Type </method-return-type>
4341
<method-implementation> empty:FunctionBodyRepresentation </method-implementation>

rust-semantics/representation.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ module RUST-REPRESENTATION
9898
syntax MapOrError ::= Map | SemanticsError
9999
100100
syntax Expression ::= Ptr
101-
syntax ExpressionOrError ::= Expression | SemanticsError
101+
syntax ExpressionOrError ::= v(Expression) | e(SemanticsError)
102+
syntax KItem ::= unwrap(ExpressionOrError) [function, total]
103+
rule unwrap(v(E:Expression)) => E
104+
rule unwrap(e(E:SemanticsError)) => E
102105
103106
syntax NormalizedFunctionParameterListOrError ::= NormalizedFunctionParameterList | SemanticsError
104107
@@ -129,7 +132,10 @@ module RUST-REPRESENTATION
129132
syntax IntOrError ::= valueToInteger(Value) [function, total]
130133
syntax ValueOrError ::= integerToValue(Int, Type) [function, total]
131134
135+
syntax StringOrError ::= String | SemanticsError
136+
132137
syntax String ::= IdentifierToString(Identifier) [function, total, hook(STRING.token2string)]
138+
syntax Identifier ::= StringToIdentifier(String) [function, total, hook(STRING.string2token)]
133139
134140
syntax CallParamsList ::= reverse(CallParamsList, CallParamsList) [function, total]
135141
@@ -175,6 +181,9 @@ module RUST-REPRESENTATION
175181
176182
syntax TypePath ::= append(MaybeTypePath, Identifier) [function, total]
177183
184+
syntax NonEmptyStatementsOrError ::= NonEmptyStatements | SemanticsError
185+
syntax NonEmptyStatements ::= concatNonEmptyStatements(NonEmptyStatements, NonEmptyStatements) [function, total]
186+
178187
endmodule
179188
180189
```

rust-semantics/rust-common-syntax.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ https://doc.rust-lang.org/reference/items/extern-crates.html
197197
syntax Struct ::= StructStruct | TupleStruct
198198
syntax TupleStruct ::= "TODO: not needed yet, not implementing"
199199
syntax StructStruct ::= "struct" Identifier MaybeGenericParams MaybeWhereClause "{" MaybeStructFields "}"
200-
syntax MaybeStructFields ::= "" | StructFields
201-
syntax StructFields ::= List{StructField, ","}
200+
syntax MaybeStructFields ::= "" | StructFields | StructFields ","
201+
syntax StructFields ::= NeList{StructField, ","}
202202
syntax StructField ::= OuterAttributes MaybeVisibility Identifier ":" Type //TODO: Outerfields and visibility for
203203
//struct fields is not yet supported.
204204

rust-semantics/test/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module RUST-EXECUTION-TEST-CONFIGURATION
66
configuration
77
<rust-test>
88
<test-stack> .List </test-stack>
9+
<mocks> .Map </mocks>
910
</rust-test>
1011
endmodule
1112

rust-semantics/test/execution.md

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module RUST-EXECUTION-TEST-PARSING-SYNTAX
1313
| "return_value_to_arg"
1414
| "check_eq" Expression [strict]
1515
| "push" Expression [strict]
16+
syntax KItem ::= mock(KItem, K)
1617
endmodule
1718
1819
module RUST-EXECUTION-TEST
@@ -92,6 +93,17 @@ module RUST-EXECUTION-TEST
9293
<values> VALUES:Map => VALUES[NVI <- V] </values>
9394
<next-value-id> NVI:Int => NVI +Int 1 </next-value-id>
9495
96+
syntax KItem ::= wrappedK(K)
97+
syntax Mockable
98+
99+
rule
100+
<k> mock(Mocked:Mockable, Result:K) => .K ... </k>
101+
<mocks> M:Map => M[Mocked <- wrappedK(Result)] </mocks>
102+
103+
rule
104+
<k> (Mocked:Mockable => Result) ...</k>
105+
<mocks> Mocked |-> wrappedK(Result:K) ...</mocks>
106+
[priority(10)]
95107
endmodule
96108
97109
```

tests/ukm-contracts/bytes_hooks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern "C" {
66
fn append_u64(bytes_id: u64, value: u64) -> u64;
77
fn append_u32(bytes_id: u64, value: u32) -> u64;
88
fn append_u16(bytes_id: u64, value: u16) -> u64;
9-
fn append_u7(bytes_id: u64, value: u8) -> u64;
9+
fn append_u8(bytes_id: u64, value: u8) -> u64;
1010
fn append_str(bytes_id: u64, value: &str) -> u64;
1111

1212
fn decode_u128(bytes_id: u64) -> (u64, u128);

tests/ukm-contracts/state_hooks.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern "C" {
2+
fn setStatus(status: u64);
3+
fn setOutput(bytes_id: u64);
4+
}

tests/ukm-contracts/ukm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std]
22

33
// TODO: Support structs and figure out the content of MessageResult
4-
struct MessageResult { }
4+
struct MessageResult { gas: u64 }
55

66
pub const EVMC_REJECTED: u64 = 0_u64;
77
pub const EVMC_INTERNAL_ERROR: u64 = 1_u64;
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
call :: bytes_hooks :: empty;
2+
return_value_to_arg;
3+
push "myEndpoint(Uint64)";
4+
call :: bytes_hooks :: append_str;
5+
return_value_to_arg;
6+
push 123_u64;
7+
call :: bytes_hooks :: append_u64;
8+
return_value;
9+
mock CallData;
10+
11+
call_contract 12345;
12+
return_value;
13+
check_eq ();
14+
15+
push_status;
16+
check_eq 2;
17+
18+
output_to_arg;
19+
call :: endpoints :: decode_single_u64;
20+
return_value;
21+
22+
check_eq 126_u64
23+

tests/ukm-with-contract/endpoints.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![no_std]
2+
3+
#[allow(unused_imports)]
4+
use ukm::*;
5+
6+
#[ukm::contract]
7+
pub trait Endpoints {
8+
#[init]
9+
fn init(&self) {}
10+
11+
#[endpoint(myEndpoint)]
12+
fn endpoint(&self, value: u64) -> u64 {
13+
value + 3_u64
14+
}
15+
}
16+
17+
fn decode_single_u64(bytes_id: u64) -> u64 {
18+
let (remaining_id, value) = :: bytes_hooks :: decode_u64(bytes_id);
19+
if :: bytes_hooks :: length(remaining_id) > 0_u32 {
20+
fail();
21+
};
22+
value
23+
}

ukm-semantics/main/common-tools.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```k
2+
3+
module UKM-COMMON-TOOLS-SYNTAX
4+
syntax Identifier ::= "dispatcherMethodIdentifier" [function, total]
5+
endmodule
6+
7+
module UKM-COMMON-TOOLS
8+
imports private UKM-COMMON-TOOLS-SYNTAX
9+
10+
rule dispatcherMethodIdentifier => #token("ukm#dispatch#method", "Identifier")
11+
endmodule
12+
13+
```

ukm-semantics/main/configuration.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
```k
22
33
requires "hooks/bytes.md"
4+
requires "hooks/state.md"
45
56
module UKM-CONFIGURATION
67
imports UKM-HOOKS-BYTES-CONFIGURATION
8+
imports UKM-HOOKS-STATE-CONFIGURATION
79
configuration
810
<ukm>
911
<ukm-bytes/>
12+
<ukm-state/>
1013
</ukm>
1114
endmodule
1215

0 commit comments

Comments
 (0)