diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-03-22 19:15:48 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-03-22 19:17:49 -0700 |
commit | c0bd71c5a596af6cf7f05a712232464623da0ba9 (patch) | |
tree | bf0d412f98f69dd163fc038258f23df9b19368f0 /tools/go_marshal/test | |
parent | 9e86dfc9c5b56eaa91485826bcf3f1f7617d2eb0 (diff) |
[lisa] Support dynamic types for all types.
We were only supporting dynamic struct types. With this change, users can make
any type dynamic. The tool (correctly) blindly just generates the remaining
methods needed to implement Marshallable using the 3 methods defined by the
user on the dynamic type.
This is helpful in situations like:
type StringArray []string
Added a test for such a use case.
PiperOrigin-RevId: 364463164
Diffstat (limited to 'tools/go_marshal/test')
-rw-r--r-- | tools/go_marshal/test/BUILD | 5 | ||||
-rw-r--r-- | tools/go_marshal/test/dynamic.go | 83 | ||||
-rw-r--r-- | tools/go_marshal/test/marshal_test.go | 33 | ||||
-rw-r--r-- | tools/go_marshal/test/test.go | 35 |
4 files changed, 113 insertions, 43 deletions
diff --git a/tools/go_marshal/test/BUILD b/tools/go_marshal/test/BUILD index cb2d4e6e3..5bceacd32 100644 --- a/tools/go_marshal/test/BUILD +++ b/tools/go_marshal/test/BUILD @@ -23,7 +23,10 @@ go_test( go_library( name = "test", testonly = 1, - srcs = ["test.go"], + srcs = [ + "dynamic.go", + "test.go", + ], marshal = True, visibility = ["//tools/go_marshal/test:__subpackages__"], deps = [ diff --git a/tools/go_marshal/test/dynamic.go b/tools/go_marshal/test/dynamic.go new file mode 100644 index 000000000..9a812efe9 --- /dev/null +++ b/tools/go_marshal/test/dynamic.go @@ -0,0 +1,83 @@ +// Copyright 2021 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test + +import "gvisor.dev/gvisor/pkg/marshal/primitive" + +// Type12Dynamic is a dynamically sized struct which depends on the +// autogenerator to generate some Marshallable methods for it. +// +// +marshal dynamic +type Type12Dynamic struct { + X primitive.Int64 + Y []primitive.Int64 +} + +// SizeBytes implements marshal.Marshallable.SizeBytes. +func (t *Type12Dynamic) SizeBytes() int { + return (len(t.Y) * 8) + t.X.SizeBytes() +} + +// MarshalBytes implements marshal.Marshallable.MarshalBytes. +func (t *Type12Dynamic) MarshalBytes(dst []byte) { + t.X.MarshalBytes(dst) + dst = dst[t.X.SizeBytes():] + for i, x := range t.Y { + x.MarshalBytes(dst[i*8 : (i+1)*8]) + } +} + +// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. +func (t *Type12Dynamic) UnmarshalBytes(src []byte) { + t.X.UnmarshalBytes(src) + if t.Y != nil { + t.Y = t.Y[:0] + } + for i := t.X.SizeBytes(); i < len(src); i += 8 { + var x primitive.Int64 + x.UnmarshalBytes(src[i:]) + t.Y = append(t.Y, x) + } +} + +// Type13Dynamic is a dynamically sized struct which depends on the +// autogenerator to generate some Marshallable methods for it. +// +// It represents a string in memory which is preceded by a uint32 indicating +// the string size. +// +// +marshal dynamic +type Type13Dynamic string + +// SizeBytes implements marshal.Marshallable.SizeBytes. +func (t *Type13Dynamic) SizeBytes() int { + return (*primitive.Uint32)(nil).SizeBytes() + len(*t) +} + +// MarshalBytes implements marshal.Marshallable.MarshalBytes. +func (t *Type13Dynamic) MarshalBytes(dst []byte) { + strLen := primitive.Uint32(len(*t)) + strLen.MarshalBytes(dst) + dst = dst[strLen.SizeBytes():] + copy(dst[:strLen], *t) +} + +// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. +func (t *Type13Dynamic) UnmarshalBytes(src []byte) { + var strLen primitive.Uint32 + strLen.UnmarshalBytes(src) + src = src[strLen.SizeBytes():] + *t = Type13Dynamic(src[:strLen]) +} diff --git a/tools/go_marshal/test/marshal_test.go b/tools/go_marshal/test/marshal_test.go index b0091dc64..733689c79 100644 --- a/tools/go_marshal/test/marshal_test.go +++ b/tools/go_marshal/test/marshal_test.go @@ -515,20 +515,39 @@ func TestLimitedSliceMarshalling(t *testing.T) { } } -func TestDynamicType(t *testing.T) { +func TestDynamicTypeStruct(t *testing.T) { t12 := test.Type12Dynamic{ X: 32, Y: []primitive.Int64{5, 6, 7}, } + var cc mockCopyContext + cc.setLimit(t12.SizeBytes()) - var m marshal.Marshallable - m = &t12 // Ensure that all methods were generated. - b := make([]byte, m.SizeBytes()) - m.MarshalBytes(b) + if _, err := t12.CopyOut(&cc, usermem.Addr(0)); err != nil { + t.Fatalf("cc.CopyOut faile: %v", err) + } - var res test.Type12Dynamic - res.UnmarshalBytes(b) + res := test.Type12Dynamic{ + Y: make([]primitive.Int64, len(t12.Y)), + } + res.CopyIn(&cc, usermem.Addr(0)) if !reflect.DeepEqual(t12, res) { t.Errorf("dynamic type is not same after marshalling and unmarshalling: before = %+v, after = %+v", t12, res) } } + +func TestDynamicTypeIdentifier(t *testing.T) { + s := test.Type13Dynamic("go_marshal") + var cc mockCopyContext + cc.setLimit(s.SizeBytes()) + + if _, err := s.CopyOut(&cc, usermem.Addr(0)); err != nil { + t.Fatalf("cc.CopyOut faile: %v", err) + } + + res := test.Type13Dynamic(make([]byte, len(s))) + res.CopyIn(&cc, usermem.Addr(0)) + if res != s { + t.Errorf("dynamic type is not same after marshalling and unmarshalling: before = %s, after = %s", s, res) + } +} diff --git a/tools/go_marshal/test/test.go b/tools/go_marshal/test/test.go index b8eb989d9..e7e3ed74a 100644 --- a/tools/go_marshal/test/test.go +++ b/tools/go_marshal/test/test.go @@ -16,8 +16,6 @@ package test import ( - "gvisor.dev/gvisor/pkg/marshal/primitive" - // We're intentionally using a package name alias here even though it's not // necessary to test the code generator's ability to handle package aliases. ex "gvisor.dev/gvisor/tools/go_marshal/test/external" @@ -200,36 +198,3 @@ type Type11 struct { ex.External y int64 } - -// Type12Dynamic is a dynamically sized struct which depends on the autogenerator -// to generate some Marshallable methods for it. -// -// +marshal dynamic -type Type12Dynamic struct { - X primitive.Int64 - Y []primitive.Int64 -} - -// SizeBytes implements marshal.Marshallable.SizeBytes. -func (t *Type12Dynamic) SizeBytes() int { - return (len(t.Y) * 8) + t.X.SizeBytes() -} - -// MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (t *Type12Dynamic) MarshalBytes(dst []byte) { - t.X.MarshalBytes(dst) - dst = dst[t.X.SizeBytes():] - for i, x := range t.Y { - x.MarshalBytes(dst[i*8 : (i+1)*8]) - } -} - -// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (t *Type12Dynamic) UnmarshalBytes(src []byte) { - t.X.UnmarshalBytes(src) - for i := t.X.SizeBytes(); i < len(src); i += 8 { - var x primitive.Int64 - x.UnmarshalBytes(src[i:]) - t.Y = append(t.Y, x) - } -} |