forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds the Blake3Hasher and Blake3HashFunction classes to vfs and makes them available under the flag --digest_function=BLAKE3. This is a partial commit for bazelbuild#18658. Closes bazelbuild#18784. PiperOrigin-RevId: 550525978 Change-Id: Iedc0886c51755585d56b4d8f47676d3be5bbedba
- Loading branch information
1 parent
5a708f9
commit cc49d68
Showing
19 changed files
with
673 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/google/devtools/build/lib/vfs/bazel/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@rules_java//java:defs.bzl", "java_library") | ||
|
||
package( | ||
default_applicable_licenses = ["//:license"], | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = glob(["**"]), | ||
visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
java_library( | ||
name = "bazel", | ||
srcs = glob( | ||
[ | ||
"*.java", | ||
], | ||
), | ||
deps = [ | ||
"//src/main/java/com/google/devtools/build/lib/jni", | ||
"//src/main/java/com/google/devtools/build/lib/vfs", | ||
"//third_party:error_prone_annotations", | ||
"//third_party:guava", | ||
"//third_party:jsr305", | ||
], | ||
) |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/google/devtools/build/lib/vfs/bazel/BazelHashFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 com.google.devtools.build.lib.vfs.bazel; | ||
|
||
import com.google.devtools.build.lib.jni.JniLoader; | ||
import com.google.devtools.build.lib.vfs.DigestHashFunction; | ||
import java.security.Security; | ||
import javax.annotation.Nullable; | ||
|
||
/** Bazel specific {@link DigestHashFunction}s. */ | ||
public final class BazelHashFunctions { | ||
@Nullable public static final DigestHashFunction BLAKE3; | ||
|
||
static { | ||
DigestHashFunction hashFunction = null; | ||
|
||
if (JniLoader.isJniAvailable()) { | ||
try { | ||
Security.addProvider(new Blake3Provider()); | ||
hashFunction = DigestHashFunction.register(new Blake3HashFunction(), "BLAKE3"); | ||
} catch (UnsatisfiedLinkError ignored) { | ||
// This can happen if bazel was compiled manually (with compile.sh), | ||
// on windows. In that case jni is available, but missing the blake3 | ||
// symbols necessary to register the hasher. | ||
} | ||
} | ||
|
||
BLAKE3 = hashFunction; | ||
} | ||
|
||
public static void ensureRegistered() {} | ||
|
||
private BazelHashFunctions() {} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/google/devtools/build/lib/vfs/bazel/Blake3HashFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 com.google.devtools.build.lib.vfs.bazel; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkPositionIndexes; | ||
|
||
import com.google.common.hash.Funnel; | ||
import com.google.common.hash.HashCode; | ||
import com.google.common.hash.HashFunction; | ||
import com.google.common.hash.Hasher; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
|
||
/** A {@link HashFunction} for BLAKE3. */ | ||
public final class Blake3HashFunction implements HashFunction { | ||
@Override | ||
public int bits() { | ||
return 256; | ||
} | ||
|
||
@Override | ||
public Hasher newHasher() { | ||
return new Blake3Hasher(new Blake3MessageDigest()); | ||
} | ||
|
||
@Override | ||
public Hasher newHasher(int expectedInputSize) { | ||
checkArgument( | ||
expectedInputSize >= 0, "expectedInputSize must be >= 0 but was %s", expectedInputSize); | ||
return newHasher(); | ||
} | ||
|
||
/* The following methods implement the {HashFunction} interface. */ | ||
|
||
@Override | ||
public <T> HashCode hashObject(T instance, Funnel<? super T> funnel) { | ||
return newHasher().putObject(instance, funnel).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashUnencodedChars(CharSequence input) { | ||
int len = input.length(); | ||
return newHasher(len * 2).putUnencodedChars(input).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashString(CharSequence input, Charset charset) { | ||
return newHasher().putString(input, charset).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashInt(int input) { | ||
return newHasher(4).putInt(input).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashLong(long input) { | ||
return newHasher(8).putLong(input).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashBytes(byte[] input) { | ||
return hashBytes(input, 0, input.length); | ||
} | ||
|
||
@Override | ||
public HashCode hashBytes(byte[] input, int off, int len) { | ||
checkPositionIndexes(off, off + len, input.length); | ||
return newHasher(len).putBytes(input, off, len).hash(); | ||
} | ||
|
||
@Override | ||
public HashCode hashBytes(ByteBuffer input) { | ||
return newHasher(input.remaining()).putBytes(input).hash(); | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
src/main/java/com/google/devtools/build/lib/vfs/bazel/Blake3Hasher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 com.google.devtools.build.lib.vfs.bazel; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import com.google.common.hash.Funnel; | ||
import com.google.common.hash.HashCode; | ||
import com.google.common.hash.Hasher; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
|
||
/** A {@link Hasher} for BLAKE3. */ | ||
public final class Blake3Hasher implements Hasher { | ||
private final Blake3MessageDigest messageDigest; | ||
private boolean isDone = false; | ||
|
||
public Blake3Hasher(Blake3MessageDigest blake3MessageDigest) { | ||
messageDigest = blake3MessageDigest; | ||
} | ||
|
||
/* The following methods implement the {Hasher} interface. */ | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putBytes(ByteBuffer b) { | ||
messageDigest.engineUpdate(b); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putBytes(byte[] bytes, int off, int len) { | ||
messageDigest.engineUpdate(bytes, off, len); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putBytes(byte[] bytes) { | ||
messageDigest.engineUpdate(bytes, 0, bytes.length); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putByte(byte b) { | ||
messageDigest.engineUpdate(b); | ||
return this; | ||
} | ||
|
||
@Override | ||
public HashCode hash() { | ||
checkState(!isDone); | ||
isDone = true; | ||
|
||
return HashCode.fromBytes(messageDigest.engineDigest()); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public final Hasher putBoolean(boolean b) { | ||
return putByte(b ? (byte) 1 : (byte) 0); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public final Hasher putDouble(double d) { | ||
return putLong(Double.doubleToRawLongBits(d)); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public final Hasher putFloat(float f) { | ||
return putInt(Float.floatToRawIntBits(f)); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putUnencodedChars(CharSequence charSequence) { | ||
for (int i = 0, len = charSequence.length(); i < len; i++) { | ||
putChar(charSequence.charAt(i)); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putString(CharSequence charSequence, Charset charset) { | ||
return putBytes(charSequence.toString().getBytes(charset)); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putShort(short s) { | ||
putByte((byte) s); | ||
putByte((byte) (s >>> 8)); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putInt(int i) { | ||
putByte((byte) i); | ||
putByte((byte) (i >>> 8)); | ||
putByte((byte) (i >>> 16)); | ||
putByte((byte) (i >>> 24)); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putLong(long l) { | ||
for (int i = 0; i < 64; i += 8) { | ||
putByte((byte) (l >>> i)); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Hasher putChar(char c) { | ||
putByte((byte) c); | ||
putByte((byte) (c >>> 8)); | ||
return this; | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public <T> Hasher putObject(T instance, Funnel<? super T> funnel) { | ||
funnel.funnel(instance, this); | ||
return this; | ||
} | ||
} |
Oops, something went wrong.