Mojom parser: Use Encoder.SetDeterministic(true) for stable encodings.
BUG=#448
R=azani@chromium.org
Review URL: https://codereview.chromium.org/1781643003 .
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
index f53362f..68f811c 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
@@ -1 +1 @@
-9e6911a9eccee6ea393b995c6c98efa23705b746
\ No newline at end of file
+0b34f8cb59b7aafe1d2616a95a640a4f791c4638
\ No newline at end of file
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
index 71ff272..e51e708 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
@@ -1 +1 @@
-98c2ff207f54471df774f6e10262f37e3ba5b92b
\ No newline at end of file
+4db51813e726ec83e85daf2307f18612f11792a5
\ No newline at end of file
diff --git a/mojom/mojom_parser/generated/mojom_files/mojom_files.mojom.go b/mojom/mojom_parser/generated/mojom_files/mojom_files.mojom.go
index 62b9b36..9b2253b 100644
--- a/mojom/mojom_parser/generated/mojom_files/mojom_files.mojom.go
+++ b/mojom/mojom_parser/generated/mojom_files/mojom_files.mojom.go
@@ -317,9 +317,20 @@
{
var keys0 []string
var values0 []MojomFile
- for key0, value0 := range s.Files {
- keys0 = append(keys0, key0)
- values0 = append(values0, value0)
+ if encoder.Deterministic() {
+ for key0, _ := range s.Files {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]MojomFile, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.Files[key]
+ }
+ } else {
+ for key0, value0 := range s.Files {
+ keys0 = append(keys0, key0)
+ values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
@@ -362,9 +373,20 @@
{
var keys0 []string
var values0 []mojom_types.UserDefinedType
- for key0, value0 := range s.ResolvedTypes {
- keys0 = append(keys0, key0)
- values0 = append(values0, value0)
+ if encoder.Deterministic() {
+ for key0, _ := range s.ResolvedTypes {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]mojom_types.UserDefinedType, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.ResolvedTypes[key]
+ }
+ } else {
+ for key0, value0 := range s.ResolvedTypes {
+ keys0 = append(keys0, key0)
+ values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
@@ -407,9 +429,20 @@
{
var keys0 []string
var values0 []mojom_types.UserDefinedValue
- for key0, value0 := range s.ResolvedValues {
- keys0 = append(keys0, key0)
- values0 = append(values0, value0)
+ if encoder.Deterministic() {
+ for key0, _ := range s.ResolvedValues {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]mojom_types.UserDefinedValue, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.ResolvedValues[key]
+ }
+ } else {
+ for key0, value0 := range s.ResolvedValues {
+ keys0 = append(keys0, key0)
+ values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
diff --git a/mojom/mojom_parser/generated/mojom_types/mojom_types.mojom.go b/mojom/mojom_parser/generated/mojom_types/mojom_types.mojom.go
index 149f3a1..a5f20a8 100644
--- a/mojom/mojom_parser/generated/mojom_types/mojom_types.mojom.go
+++ b/mojom/mojom_parser/generated/mojom_types/mojom_types.mojom.go
@@ -34,7 +34,6 @@
)
-
type BuiltinConstantValue int32
const (
@@ -47,7 +46,6 @@
)
-
type HandleType_Kind int32
const (
@@ -59,11 +57,11 @@
)
-
type StringType struct {
Nullable bool
}
+
func (s *StringType) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(8, 0)
if err := encoder.WriteBool(s.Nullable); err != nil {
@@ -111,13 +109,12 @@
return nil
}
-
-
type HandleType struct {
Nullable bool
Kind HandleType_Kind
}
+
func (s *HandleType) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(8, 0)
if err := encoder.WriteBool(s.Nullable); err != nil {
@@ -175,14 +172,13 @@
return nil
}
-
-
type ArrayType struct {
Nullable bool
FixedLength int32
ElementType Type
}
+
func (s *ArrayType) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(24, 0)
if err := encoder.WriteBool(s.Nullable); err != nil {
@@ -256,14 +252,13 @@
return nil
}
-
-
type MapType struct {
Nullable bool
KeyType Type
ValueType Type
}
+
func (s *MapType) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(40, 0)
if err := encoder.WriteBool(s.Nullable); err != nil {
@@ -343,8 +338,6 @@
return nil
}
-
-
type TypeReference struct {
Nullable bool
IsInterfaceRequest bool
@@ -352,6 +345,7 @@
TypeKey *string
}
+
func (s *TypeReference) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(24, 0)
if err := encoder.WriteBool(s.Nullable); err != nil {
@@ -461,8 +455,6 @@
return nil
}
-
-
type StructField struct {
DeclData *DeclarationData
Type Type
@@ -470,6 +462,7 @@
Offset int32
}
+
func (s *StructField) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(48, 0)
if s.DeclData == nil {
@@ -571,11 +564,10 @@
return nil
}
-
-
type DefaultKeyword struct {
}
+
func (s *DefaultKeyword) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(0, 0)
if err := encoder.Finish(); err != nil {
@@ -613,14 +605,13 @@
return nil
}
-
-
type StructVersion struct {
VersionNumber uint32
NumFields uint32
NumBytes uint32
}
+
func (s *StructVersion) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if err := encoder.WriteUint32(s.VersionNumber); err != nil {
@@ -688,14 +679,13 @@
return nil
}
-
-
type MojomStruct struct {
DeclData *DeclarationData
Fields []StructField
VersionInfo *[]StructVersion
}
+
func (s *MojomStruct) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(24, 0)
if s.DeclData == nil {
@@ -854,14 +844,13 @@
return nil
}
-
-
type UnionField struct {
DeclData *DeclarationData
Type Type
Tag uint32
}
+
func (s *UnionField) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(32, 0)
if s.DeclData == nil {
@@ -949,13 +938,12 @@
return nil
}
-
-
type MojomUnion struct {
DeclData *DeclarationData
Fields []UnionField
}
+
func (s *MojomUnion) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if s.DeclData == nil {
@@ -1063,8 +1051,6 @@
return nil
}
-
-
type EnumValue struct {
DeclData *DeclarationData
EnumTypeKey string
@@ -1072,6 +1058,7 @@
IntValue int32
}
+
func (s *EnumValue) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(40, 0)
if s.DeclData == nil {
@@ -1178,13 +1165,12 @@
return nil
}
-
-
type MojomEnum struct {
DeclData *DeclarationData
Values []EnumValue
}
+
func (s *MojomEnum) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if s.DeclData == nil {
@@ -1292,8 +1278,6 @@
return nil
}
-
-
type MojomMethod struct {
DeclData *DeclarationData
Parameters MojomStruct
@@ -1301,6 +1285,7 @@
Ordinal uint32
}
+
func (s *MojomMethod) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(32, 0)
if s.DeclData == nil {
@@ -1415,14 +1400,13 @@
return nil
}
-
-
type MojomInterface struct {
DeclData *DeclarationData
ServiceName *string
Methods map[uint32]MojomMethod
}
+
func (s *MojomInterface) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(24, 0)
if s.DeclData == nil {
@@ -1452,9 +1436,20 @@
{
var keys0 []uint32
var values0 []MojomMethod
- for key0, value0 := range s.Methods {
+ if encoder.Deterministic() {
+ for key0, _ := range s.Methods {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]MojomMethod, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.Methods[key]
+ }
+ } else {
+ for key0, value0 := range s.Methods {
keys0 = append(keys0, key0)
values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
@@ -1637,14 +1632,13 @@
return nil
}
-
-
type UserValueReference struct {
Identifier string
ValueKey *string
ResolvedConcreteValue Value
}
+
func (s *UserValueReference) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(32, 0)
if err := encoder.WritePointer(); err != nil {
@@ -1743,14 +1737,13 @@
return nil
}
-
-
type DeclaredConstant struct {
DeclData DeclarationData
Type Type
Value Value
}
+
func (s *DeclaredConstant) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(40, 0)
if err := encoder.WritePointer(); err != nil {
@@ -1839,13 +1832,12 @@
return nil
}
-
-
type Attribute struct {
Key string
Value LiteralValue
}
+
func (s *Attribute) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(24, 0)
if err := encoder.WritePointer(); err != nil {
@@ -1920,8 +1912,6 @@
return nil
}
-
-
type DeclarationData struct {
Attributes *[]Attribute
MinVersion int32
@@ -1934,6 +1924,7 @@
ContainerTypeKey *string
}
+
func (s *DeclarationData) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(64, 0)
if s.Attributes == nil {
@@ -2178,14 +2169,13 @@
return nil
}
-
-
type SourceFileInfo struct {
FileName string
LineNumber uint32
ColumnNumber uint32
}
+
func (s *SourceFileInfo) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if err := encoder.WritePointer(); err != nil {
@@ -2264,13 +2254,12 @@
return nil
}
-
-
type ContainedDeclarations struct {
Enums *[]string
Constants *[]string
}
+
func (s *ContainedDeclarations) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if s.Enums == nil {
@@ -2414,13 +2403,12 @@
return nil
}
-
-
type ServiceTypeInfo struct {
TopLevelInterface string
CompleteTypeSet []string
}
+
func (s *ServiceTypeInfo) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if err := encoder.WritePointer(); err != nil {
@@ -2527,13 +2515,12 @@
return nil
}
-
-
type RuntimeTypeInfo struct {
ServicesByName map[string]ServiceTypeInfo
TypeMap map[string]UserDefinedType
}
+
func (s *RuntimeTypeInfo) Encode(encoder *bindings.Encoder) error {
encoder.StartStruct(16, 0)
if err := encoder.WritePointer(); err != nil {
@@ -2543,9 +2530,20 @@
{
var keys0 []string
var values0 []ServiceTypeInfo
- for key0, value0 := range s.ServicesByName {
+ if encoder.Deterministic() {
+ for key0, _ := range s.ServicesByName {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]ServiceTypeInfo, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.ServicesByName[key]
+ }
+ } else {
+ for key0, value0 := range s.ServicesByName {
keys0 = append(keys0, key0)
values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
@@ -2588,9 +2586,20 @@
{
var keys0 []string
var values0 []UserDefinedType
- for key0, value0 := range s.TypeMap {
+ if encoder.Deterministic() {
+ for key0, _ := range s.TypeMap {
+ keys0 = append(keys0, key0)
+ }
+ bindings.SortMapKeys(&keys0)
+ values0 = make([]UserDefinedType, len(keys0))
+ for i, key := range keys0 {
+ values0[i] = s.TypeMap[key]
+ }
+ } else {
+ for key0, value0 := range s.TypeMap {
keys0 = append(keys0, key0)
values0 = append(values0, value0)
+ }
}
if err := encoder.WritePointer(); err != nil {
return err
@@ -2844,8 +2853,6 @@
return nil
}
-
-
type Type interface {
Tag() uint32
Interface() interface{}
@@ -2853,6 +2860,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __TypeReflect struct {
SimpleType SimpleType
StringType StringType
@@ -2956,6 +2965,8 @@
return nil
}
+
+
type TypeStringType struct { Value StringType }
func (u *TypeStringType) Tag() uint32 { return 1 }
func (u *TypeStringType) Interface() interface{} { return u.Value }
@@ -2990,6 +3001,8 @@
return nil
}
+
+
type TypeArrayType struct { Value ArrayType }
func (u *TypeArrayType) Tag() uint32 { return 2 }
func (u *TypeArrayType) Interface() interface{} { return u.Value }
@@ -3024,6 +3037,8 @@
return nil
}
+
+
type TypeMapType struct { Value MapType }
func (u *TypeMapType) Tag() uint32 { return 3 }
func (u *TypeMapType) Interface() interface{} { return u.Value }
@@ -3058,6 +3073,8 @@
return nil
}
+
+
type TypeHandleType struct { Value HandleType }
func (u *TypeHandleType) Tag() uint32 { return 4 }
func (u *TypeHandleType) Interface() interface{} { return u.Value }
@@ -3092,6 +3109,8 @@
return nil
}
+
+
type TypeTypeReference struct { Value TypeReference }
func (u *TypeTypeReference) Tag() uint32 { return 5 }
func (u *TypeTypeReference) Interface() interface{} { return u.Value }
@@ -3136,6 +3155,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __UserDefinedTypeReflect struct {
EnumType MojomEnum
StructType MojomStruct
@@ -3232,6 +3253,8 @@
return nil
}
+
+
type UserDefinedTypeStructType struct { Value MojomStruct }
func (u *UserDefinedTypeStructType) Tag() uint32 { return 1 }
func (u *UserDefinedTypeStructType) Interface() interface{} { return u.Value }
@@ -3266,6 +3289,8 @@
return nil
}
+
+
type UserDefinedTypeUnionType struct { Value MojomUnion }
func (u *UserDefinedTypeUnionType) Tag() uint32 { return 2 }
func (u *UserDefinedTypeUnionType) Interface() interface{} { return u.Value }
@@ -3300,6 +3325,8 @@
return nil
}
+
+
type UserDefinedTypeInterfaceType struct { Value MojomInterface }
func (u *UserDefinedTypeInterfaceType) Tag() uint32 { return 3 }
func (u *UserDefinedTypeInterfaceType) Interface() interface{} { return u.Value }
@@ -3344,6 +3371,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __DefaultFieldValueReflect struct {
Value Value
DefaultKeyword DefaultKeyword
@@ -3440,6 +3469,8 @@
return nil
}
+
+
type DefaultFieldValueDefaultKeyword struct { Value DefaultKeyword }
func (u *DefaultFieldValueDefaultKeyword) Tag() uint32 { return 1 }
func (u *DefaultFieldValueDefaultKeyword) Interface() interface{} { return u.Value }
@@ -3484,6 +3515,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __ValueReflect struct {
LiteralValue LiteralValue
UserValueReference UserValueReference
@@ -3588,6 +3621,8 @@
return nil
}
+
+
type ValueUserValueReference struct { Value UserValueReference }
func (u *ValueUserValueReference) Tag() uint32 { return 1 }
func (u *ValueUserValueReference) Interface() interface{} { return u.Value }
@@ -3622,6 +3657,8 @@
return nil
}
+
+
type ValueBuiltinValue struct { Value BuiltinConstantValue }
func (u *ValueBuiltinValue) Tag() uint32 { return 2 }
func (u *ValueBuiltinValue) Interface() interface{} { return u.Value }
@@ -3657,6 +3694,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __LiteralValueReflect struct {
BoolValue bool
DoubleValue float64
@@ -3808,6 +3847,8 @@
return nil
}
+
+
type LiteralValueDoubleValue struct { Value float64 }
func (u *LiteralValueDoubleValue) Tag() uint32 { return 1 }
func (u *LiteralValueDoubleValue) Interface() interface{} { return u.Value }
@@ -3833,6 +3874,8 @@
return nil
}
+
+
type LiteralValueFloatValue struct { Value float32 }
func (u *LiteralValueFloatValue) Tag() uint32 { return 2 }
func (u *LiteralValueFloatValue) Interface() interface{} { return u.Value }
@@ -3858,6 +3901,8 @@
return nil
}
+
+
type LiteralValueInt8Value struct { Value int8 }
func (u *LiteralValueInt8Value) Tag() uint32 { return 3 }
func (u *LiteralValueInt8Value) Interface() interface{} { return u.Value }
@@ -3883,6 +3928,8 @@
return nil
}
+
+
type LiteralValueInt16Value struct { Value int16 }
func (u *LiteralValueInt16Value) Tag() uint32 { return 4 }
func (u *LiteralValueInt16Value) Interface() interface{} { return u.Value }
@@ -3908,6 +3955,8 @@
return nil
}
+
+
type LiteralValueInt32Value struct { Value int32 }
func (u *LiteralValueInt32Value) Tag() uint32 { return 5 }
func (u *LiteralValueInt32Value) Interface() interface{} { return u.Value }
@@ -3933,6 +3982,8 @@
return nil
}
+
+
type LiteralValueInt64Value struct { Value int64 }
func (u *LiteralValueInt64Value) Tag() uint32 { return 6 }
func (u *LiteralValueInt64Value) Interface() interface{} { return u.Value }
@@ -3958,6 +4009,8 @@
return nil
}
+
+
type LiteralValueStringValue struct { Value string }
func (u *LiteralValueStringValue) Tag() uint32 { return 7 }
func (u *LiteralValueStringValue) Interface() interface{} { return u.Value }
@@ -3994,6 +4047,8 @@
return nil
}
+
+
type LiteralValueUint8Value struct { Value uint8 }
func (u *LiteralValueUint8Value) Tag() uint32 { return 8 }
func (u *LiteralValueUint8Value) Interface() interface{} { return u.Value }
@@ -4019,6 +4074,8 @@
return nil
}
+
+
type LiteralValueUint16Value struct { Value uint16 }
func (u *LiteralValueUint16Value) Tag() uint32 { return 9 }
func (u *LiteralValueUint16Value) Interface() interface{} { return u.Value }
@@ -4044,6 +4101,8 @@
return nil
}
+
+
type LiteralValueUint32Value struct { Value uint32 }
func (u *LiteralValueUint32Value) Tag() uint32 { return 10 }
func (u *LiteralValueUint32Value) Interface() interface{} { return u.Value }
@@ -4069,6 +4128,8 @@
return nil
}
+
+
type LiteralValueUint64Value struct { Value uint64 }
func (u *LiteralValueUint64Value) Tag() uint32 { return 11 }
func (u *LiteralValueUint64Value) Interface() interface{} { return u.Value }
@@ -4104,6 +4165,8 @@
Encode(encoder *bindings.Encoder) error
}
+
+
type __UserDefinedValueReflect struct {
EnumValue EnumValue
DeclaredConstant DeclaredConstant
@@ -4184,6 +4247,8 @@
return nil
}
+
+
type UserDefinedValueDeclaredConstant struct { Value DeclaredConstant }
func (u *UserDefinedValueDeclaredConstant) Tag() uint32 { return 1 }
func (u *UserDefinedValueDeclaredConstant) Interface() interface{} { return u.Value }
diff --git a/mojom/mojom_parser/serialization/serialization.go b/mojom/mojom_parser/serialization/serialization.go
index 502dea4..498f95a 100644
--- a/mojom/mojom_parser/serialization/serialization.go
+++ b/mojom/mojom_parser/serialization/serialization.go
@@ -52,6 +52,7 @@
debugString = myfmt.Sprintf("%#v", fileGraph)
}
encoder := bindings.NewEncoder()
+ encoder.SetDeterministic(true)
fileGraph.Encode(encoder)
bytes, _, err = encoder.Data()
@@ -207,6 +208,7 @@
// SerializedRuntimeTypeInfo
if emitSerializedRuntimeTypeInfo {
encoder := bindings.NewEncoder()
+ encoder.SetDeterministic(true)
typeInfo.Encode(encoder)
bytes, _, err := encoder.Data()
if err != nil {
diff --git a/mojom/mojom_parser/serialization/serialization_test.go b/mojom/mojom_parser/serialization/serialization_test.go
index 9723a2e..49748a9 100644
--- a/mojom/mojom_parser/serialization/serialization_test.go
+++ b/mojom/mojom_parser/serialization/serialization_test.go
@@ -1009,6 +1009,19 @@
continue
}
+ // Serialize again and check for consistency.
+ bytes2, _, err := serialize(descriptor, false, c.lineAndcolumnNumbers, false)
+ if err != nil {
+ t.Errorf("Serialization error for %s: %s", c.fileName, err.Error())
+ continue
+ }
+
+ if !reflect.DeepEqual(bytes, bytes2) {
+ t.Errorf("Inconsistent serialization for %s:\nbytes=%v\nbytes2=%v\n",
+ c.fileName, bytes, bytes2)
+ continue
+ }
+
// Deserialize
decoder := bindings.NewDecoder(bytes, nil)
fileGraph := mojom_files.MojomFileGraph{}