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

solver: make digest computation for ops deterministic again #5363

Open
wants to merge 1 commit into
base: master
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
17 changes: 13 additions & 4 deletions solver/llbsolver/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ func recomputeDigests(ctx context.Context, all map[digest.Digest]*pb.Op, visited
return dgst, nil
}

dt, err := proto.Marshal(op)
newDgst, err := opDigest(op)
if err != nil {
return "", err
}
newDgst := digest.FromBytes(dt)
visited[dgst] = newDgst
all[newDgst] = op
delete(all, dgst)
Expand Down Expand Up @@ -263,11 +262,10 @@ func loadLLB(ctx context.Context, def *pb.Definition, polEngine SourcePolicyEval
return solver.Edge{}, errors.Wrap(err, "error evaluating the source policy")
}
if mutated {
dtMutated, err := proto.Marshal(&op)
dgstMutated, err := opDigest(&op)
if err != nil {
return solver.Edge{}, err
}
dgstMutated := digest.FromBytes(dtMutated)
mutatedDigests[dgst] = dgstMutated
dgst = dgstMutated
}
Expand Down Expand Up @@ -397,3 +395,14 @@ func fileOpName(actions []*pb.FileAction) string {

return strings.Join(names, ", ")
}

func opDigest(op *pb.Op) (digest.Digest, error) {
// TODO: relying on a consistent byte order for protobuf serialization
// isn't reliable and isn't guaranteed by the specification. This works
// with this specific implementation but is subject to change.
dt, err := proto.MarshalOptions{Deterministic: true}.Marshal(op)
if err != nil {
return "", err
}
return digest.FromBytes(dt), nil
}
4 changes: 2 additions & 2 deletions solver/pb/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ func (m *Definition) IsNil() bool {
}

func (m *Definition) Marshal() ([]byte, error) {
return proto.Marshal(m)
return proto.MarshalOptions{Deterministic: true}.Marshal(m)
}

func (m *Definition) Unmarshal(dAtA []byte) error {
return proto.Unmarshal(dAtA, m)
}

func (m *Op) Marshal() ([]byte, error) {
return proto.Marshal(m)
return proto.MarshalOptions{Deterministic: true}.Marshal(m)
}

func (m *Op) Unmarshal(dAtA []byte) error {
Expand Down
Loading