|
53 | 53 | import static com.oracle.graal.python.nodes.StringLiterals.T_EXT_SO; |
54 | 54 | import static com.oracle.graal.python.nodes.StringLiterals.T_NAME; |
55 | 55 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError; |
| 56 | +import static com.oracle.graal.python.util.PythonUtils.ARRAY_ACCESSOR; |
56 | 57 | import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; |
57 | 58 | import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached; |
58 | 59 | import static com.oracle.graal.python.util.PythonUtils.tsLiteral; |
|
61 | 62 | import java.util.List; |
62 | 63 | import java.util.concurrent.locks.ReentrantLock; |
63 | 64 |
|
| 65 | +import org.bouncycastle.crypto.macs.SipHash; |
| 66 | +import org.bouncycastle.crypto.params.KeyParameter; |
| 67 | + |
64 | 68 | import com.oracle.graal.python.PythonLanguage; |
65 | 69 | import com.oracle.graal.python.annotations.ArgumentClinic; |
66 | 70 | import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion; |
|
72 | 76 | import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins.Marshal.MarshalError; |
73 | 77 | import com.oracle.graal.python.builtins.objects.PNone; |
74 | 78 | import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary; |
75 | | -import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
76 | 79 | import com.oracle.graal.python.builtins.objects.bytes.PBytes; |
77 | 80 | import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext; |
78 | 81 | import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext.ModuleSpec; |
|
125 | 128 | import com.oracle.truffle.api.dsl.Specialization; |
126 | 129 | import com.oracle.truffle.api.frame.VirtualFrame; |
127 | 130 | import com.oracle.truffle.api.interop.InteropLibrary; |
| 131 | +import com.oracle.truffle.api.library.CachedLibrary; |
128 | 132 | import com.oracle.truffle.api.memory.ByteArraySupport; |
129 | 133 | import com.oracle.truffle.api.nodes.Node; |
130 | 134 | import com.oracle.truffle.api.source.Source; |
@@ -702,22 +706,28 @@ private static void raiseFrozenError(Node inliningTarget, PConstructAndRaiseNode |
702 | 706 | @ArgumentClinic(name = "source", conversion = ArgumentClinic.ClinicConversion.ReadableBuffer) |
703 | 707 | @GenerateNodeFactory |
704 | 708 | public abstract static class SourceHashNode extends PythonBinaryClinicBuiltinNode { |
705 | | - @TruffleBoundary |
706 | | - @Specialization |
| 709 | + @Specialization(limit = "2") |
707 | 710 | static PBytes run(long magicNumber, Object sourceBuffer, |
| 711 | + @CachedLibrary("sourceBuffer") PythonBufferAccessLibrary bufferLib, |
708 | 712 | @Bind PythonLanguage language) { |
709 | | - long sourceHash = BytesNodes.HashBufferNode.executeUncached(sourceBuffer); |
710 | | - return PFactory.createBytes(language, computeHash(magicNumber, sourceHash)); |
| 713 | + try { |
| 714 | + byte[] hash = hashSource(magicNumber, bufferLib.getInternalOrCopiedByteArray(sourceBuffer), bufferLib.getBufferLength(sourceBuffer)); |
| 715 | + return PFactory.createBytes(language, hash); |
| 716 | + } finally { |
| 717 | + bufferLib.release(sourceBuffer); |
| 718 | + } |
711 | 719 | } |
712 | 720 |
|
713 | 721 | @TruffleBoundary |
714 | | - private static byte[] computeHash(long magicNumber, long sourceHash) { |
715 | | - byte[] hash = new byte[Long.BYTES]; |
716 | | - long hashCode = magicNumber ^ sourceHash; |
717 | | - for (int i = 0; i < hash.length; i++) { |
718 | | - hash[i] = (byte) (hashCode << (8 * i)); |
719 | | - } |
720 | | - return hash; |
| 722 | + public static byte[] hashSource(long magicNumber, byte[] bytes, int length) { |
| 723 | + SipHash sipHash = new SipHash(1, 3); |
| 724 | + byte[] key = new byte[16]; |
| 725 | + ARRAY_ACCESSOR.putLong(key, 0, magicNumber); |
| 726 | + sipHash.init(new KeyParameter(key)); |
| 727 | + sipHash.update(bytes, 0, length); |
| 728 | + byte[] out = new byte[sipHash.getMacSize()]; |
| 729 | + sipHash.doFinal(out, 0); |
| 730 | + return out; |
721 | 731 | } |
722 | 732 |
|
723 | 733 | @Override |
|
0 commit comments