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

feat: Add Dafny standard libraries subset and partial streaming support to StandardLibrary #1321

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ build
doc/build
AwsCryptographicMaterialProviders/runtimes/python/doc/generated/*

# Dafny build artifacts
bin

# Duvet output
specification_compliance_report.html

Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
url = https://github.com/dafny-lang/libraries.git
[submodule "smithy-dafny"]
path = smithy-dafny
url = https://robin-aws@github.com/smithy-lang/smithy-dafny.git
url = https://github.com/smithy-lang/smithy-dafny.git
[submodule "aws-encryption-sdk-specification"]
path = aws-encryption-sdk-specification
url = https://github.com/awslabs/aws-encryption-sdk-specification.git
4 changes: 2 additions & 2 deletions AwsCryptographicMaterialProviders/runtimes/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ readme = "README.md"

[dependencies]
aws-config = "1.5.15"
aws-lc-rs = "1.12.2"
aws-lc-sys = "0.25.0"
aws-lc-rs = "=1.12.2"
aws-lc-sys = "0.25.1"
aws-sdk-dynamodb = "1.55.0"
aws-sdk-kms = "1.51.0"
aws-smithy-runtime-api = {version = "1.7.3", features = ["client"] }
Expand Down
4 changes: 2 additions & 2 deletions AwsCryptographyPrimitives/runtimes/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ rust-version = "1.80.0"

[dependencies]
aws-config = "1.5.15"
aws-lc-rs = "1.12.2"
aws-lc-sys = "0.25.0"
aws-lc-rs = "=1.12.2"
aws-lc-sys = "0.25.1"
aws-smithy-runtime-api = "1.7.3"
aws-smithy-types = "1.2.12"
chrono = "0.4.39"
Expand Down
46 changes: 44 additions & 2 deletions StandardLibrary/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,46 @@ LIBRARIES :=
# However, this means this it could not build...
# So we empty the variable here
# and check in the Makefile
transpile_implementation: STD_LIBRARY=
transpile_test: STD_LIBRARY=
transpile_dependencies: STD_LIBRARY=

# Override SharedMakefile's transpile_implementation to not reference
# StandardLibrary as a Library
transpile_implementation: SRC_INDEX_TRANSPILE=$(if $(SRC_INDEX),$(SRC_INDEX),src)
transpile_implementation:
find ./dafny/**/$(SRC_INDEX_TRANSPILE)/ ./$(SRC_INDEX_TRANSPILE)/ -name 'Index.dfy' | sed -e 's/^/include "/' -e 's/$$/"/' | dafny \
translate $(TARGET) \
--stdin \
--no-verify \
--cores:$(CORES) \
--optimize-erasable-datatype-wrapper:false \
--unicode-char:false \
--function-syntax:3 \
--output $(OUT) \
$(if $(USE_DAFNY_STANDARD_LIBRARIES), ./bin/DafnyStandardLibraries-smithy-dafny-subset.doo, ) \
$(DAFNY_OPTIONS) \
$(DAFNY_OTHER_FILES) \
$(TRANSPILE_MODULE_NAME)

transpile_test:
dafny --version
find ./dafny/**/$(TEST_INDEX_TRANSPILE) ./$(TEST_INDEX_TRANSPILE) -name "*.dfy" -name '*.dfy' | sed -e 's/^/include "/' -e 's/$$/"/' | dafny \
translate $(TARGET) \
--stdin \
--no-verify \
--cores:$(CORES) \
--optimize-erasable-datatype-wrapper:false \
--unicode-char:false \
--function-syntax:3 \
--output $(OUT) \
$(DAFNY_OPTIONS) \
$(DAFNY_OTHER_FILES) \
$(if $(strip $(STD_LIBRARY)) , --library:$(PROJECT_ROOT)/$(STD_LIBRARY)/src/Index.dfy, ) \
$(if $(USE_DAFNY_STANDARD_LIBRARIES) , --library:./bin/DafnyStandardLibraries-smithy-dafny-subset.doo, ) \
$(TRANSLATION_RECORD) \
$(SOURCE_TRANSLATION_RECORD) \
$(TRANSPILE_MODULE_NAME) \
$(TRANSPILE_DEPENDENCIES) \

GO_MODULE_NAME="github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library"

# Python
Expand All @@ -32,3 +68,9 @@ RUST_OTHER_FILES := \

polymorph_rust:
@echo no polymorph needed for StandardLibrary

polymorph_dafny: $(if $(USE_DAFNY_STANDARD_LIBRARIES), build_dafny_stdlibs, )

build_dafny_stdlibs:
dafny build -t:lib --output $(LIBRARY_ROOT)/bin/DafnyStandardLibraries-smithy-dafny-subset.doo \
../smithy-dafny/TestModels/dafny-dependencies/StandardLibrary/DafnyStandardLibraries-smithy-dafny-subset.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

from _dafny import Seq
from smithy_python.interfaces.blobs import ByteStream
from smithy_dafny_standard_library.internaldafny.generated.Std_Streams import ByteStream as DafnyByteStream, RewindableByteStream as DafnyRewindableByteStream
from smithy_dafny_standard_library.internaldafny.generated.Std_Enumerators import Enumerator
from smithy_dafny_standard_library.internaldafny.generated.Std_Wrappers import Option, Option_Some, Option_None

# Adaptor classes for wrapping up Python-native types as their
# corresponding Dafny interfaces, and vice-versa.
# These are the equivalent of type conversions,
# but avoiding having to load all data into memory at once.

class DafnyByteStreamAsByteStream(ByteStream):
"""Wrapper class adapting a Dafny ByteStream as a native ByteStream."""

def __init__(self, dafny_byte_stream):
self.dafny_byte_stream = dafny_byte_stream

def read(self, size: int = -1) -> bytes:
# TODO: assert size is -1, buffer,
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to resolve this TODO at this time?

# or define a more specialized Action<int, bytes> type for streams.
next = self.dafny_byte_stream.Next()
while next.is_Some and len(next.value) == 0:
next = self.dafny_byte_stream.Next()
# Do NOT return None, because that indicates "no data right now, might be more later"
return bytes(next.value) if next.is_Some else bytes()


class RewindableDafnyByteStreamAsByteStream(DafnyByteStreamAsByteStream):
"""Wrapper class adapting a Dafny RewindableByteStream as a native ByteStream
that supports tell and seek.
"""

def __init__(self, dafny_byte_stream):
if not isinstance(dafny_byte_stream, DafnyRewindableByteStream):
raise ValueError("Rewindable stream required")
super().__init__(dafny_byte_stream)

def tell(self) -> int:
return self.dafny_byte_stream.Position()

def seek(self, offset, whence=0):
match whence:
case 0:
position = offset
case 1:
position = self.dafny_byte_stream.Position() + offset
case 2:
position = self.dafny_byte_stream.ContentLength() + offset
return self.dafny_byte_stream.Seek(position)


class StreamingBlobAsDafnyDataStream(DafnyByteStream):
"""Wrapper class adapting a native StreamingBlob as a Dafny ByteStream."""

def __init__(self, streaming_blob):
self.streaming_blob = streaming_blob

def Next(self):
return Enumerator.Next(self)

def Invoke(self, _) -> Option:
next = self.streaming_blob.read()
if next:
return Option_Some(Seq(next))
else:
return Option_None()
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ wrapped-client = []

[dependencies]
aws-config = "1.5.15"
aws-lc-rs = "1.12.2"
aws-lc-sys = "0.25.0"
aws-lc-rs = "=1.12.2"
aws-lc-sys = "0.25.1"
aws-sdk-dynamodb = "1.55.0"
aws-sdk-kms = "1.51.0"
aws-smithy-runtime-api = {version = "1.7.3", features = ["client"] }
Expand Down
2 changes: 1 addition & 1 deletion smithy-dafny
Submodule smithy-dafny updated 52 files
+1 −0 .github/actions/build_dafny_from_source/action.yml
+0 −30 .github/not-grep.toml
+1 −4 .github/workflows/nightly_dafny.yml
+3 −3 .github/workflows/pull.yml
+2 −2 .github/workflows/push.yml
+2 −4 .github/workflows/smithy-polymorph.yml
+4 −0 .gitmodules
+10 −2 SmithyDafnyMakefile.mk
+1 −1 TestModels/Constraints/test/Helpers.dfy
+1 −0 TestModels/Dependencies/test/SimpleDependenciesImplTest.dfy
+26 −0 TestModels/Streaming/Makefile
+351 −0 TestModels/Streaming/Model/SimpleStreamingTypes.dfy
+59 −16 TestModels/Streaming/Model/Streaming.smithy
+4 −3 TestModels/Streaming/README.md
+94 −0 TestModels/Streaming/codegen-patches/dafny/dafny-4.9.0.patch
+26 −0 TestModels/Streaming/runtimes/python/pyproject.toml
+5 −0 TestModels/Streaming/runtimes/python/src/simple_streaming/__init__.py
+2 −0 TestModels/Streaming/runtimes/python/test/internaldafny/__init__.py
+22 −0 TestModels/Streaming/runtimes/python/test/internaldafny/extern/wrapped_simple_streaming.py
+24 −0 TestModels/Streaming/runtimes/python/test/internaldafny/test_dafny_wrapper.py
+13 −0 TestModels/Streaming/runtimes/python/tox.ini
+72 −0 TestModels/Streaming/src/Chunker.dfy
+31 −0 TestModels/Streaming/src/Index.dfy
+83 −0 TestModels/Streaming/src/SimpleStreamingImpl.dfy
+10 −0 TestModels/Streaming/src/WrappedSimpleStreamingImpl.dfy
+71 −0 TestModels/Streaming/test/SimpleStreamingImplTest.dfy
+15 −0 TestModels/Streaming/test/WrappedSimpleStreamingTest.dfy
+1 −0 TestModels/aws-sdks/s3/Makefile
+493 −0 TestModels/aws-sdks/s3/Model/custom-model.json
+2 −0 TestModels/aws-sdks/s3/runtimes/python/pyproject.toml
+26 −4 TestModels/aws-sdks/s3/test/TestComAmazonawsS3.dfy
+35 −0 TestModels/dafny-dependencies/StandardLibrary/DafnyStandardLibraries-smithy-dafny-subset.toml
+7 −2 TestModels/dafny-dependencies/StandardLibrary/Makefile
+67 −0 ...endencies/StandardLibrary/runtimes/python/src/smithy_dafny_standard_library/internaldafny/extern/streams.py
+2 −0 TestModels/dafny-dependencies/StandardLibrary/src/UInt.dfy
+1 −0 TestModels/dafny-dependencies/dafny
+19 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/TestModelTest.java
+3 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithydafny/DafnyTestModels.java
+3 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithydotnet/DotnetTestModels.java
+3 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithygo/GoTestModels.java
+3 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyjava/JavaTestModels.java
+5 −2 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithypython/PythonTestModels.java
+3 −1 codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyrust/RustTestModels.java
+16 −6 codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithydafny/DafnyApiCodegen.java
+36 −6 codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithydafny/DafnyNameResolver.java
+1 −0 codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java
+8 −4 ...gen/src/main/java/software/amazon/polymorph/smithypython/awssdk/shapevisitor/AwsSdkToDafnyShapeVisitor.java
+12 −1 ...gen/src/main/java/software/amazon/polymorph/smithypython/awssdk/shapevisitor/DafnyToAwsSdkShapeVisitor.java
+13 −2 ...zon/polymorph/smithypython/awssdk/shapevisitor/conversionwriters/AwsSdkToDafnyConversionFunctionWriter.java
+13 −2 ...zon/polymorph/smithypython/awssdk/shapevisitor/conversionwriters/DafnyToAwsSdkConversionFunctionWriter.java
+10 −1 .../java/software/amazon/polymorph/smithypython/localservice/shapevisitor/DafnyToLocalServiceShapeVisitor.java
+11 −2 .../java/software/amazon/polymorph/smithypython/localservice/shapevisitor/LocalServiceToDafnyShapeVisitor.java
Loading