projects/guix·blob python-team · refs

305 lines · 13.0 KiB

1From a52c4ef44c0553a399a8a47e528db92e3bf51c6c Mon Sep 17 00:00:00 2001
2From: Alan Bateman <[email protected]>
3Date: Wed, 29 Apr 2020 08:38:28 +0100
4Subject: [PATCH] 8243666: ModuleHashes attribute generated for JMOD and JAR
5 files depends on timestamps
6
7Reviewed-by: mchung
8---
9
10--- orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashesBuilder.java 2022-04-13 19:24:10.655695284 +0200
11+++ jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashesBuilder.java 2022-04-14 02:43:48.610326492 +0200
12@@ -27,9 +27,8 @@
13
14 import java.io.PrintStream;
15 import java.lang.module.Configuration;
16+import java.lang.module.ModuleReference;
17 import java.lang.module.ResolvedModule;
18-import java.net.URI;
19-import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.util.ArrayDeque;
22 import java.util.Collections;
23@@ -39,8 +38,8 @@
24 import java.util.LinkedList;
25 import java.util.Map;
26 import java.util.Set;
27+import java.util.TreeMap;
28 import java.util.function.Consumer;
29-import java.util.function.Function;
30 import java.util.stream.Stream;
31 import static java.util.stream.Collectors.*;
32
33@@ -101,7 +100,7 @@
34 // the modules to record the hashes - it is the first matching
35 // module and has not been hashed during the traversal.
36 Set<String> mods = new HashSet<>();
37- Map<String, ModuleHashes> hashes = new HashMap<>();
38+ Map<String, ModuleHashes> hashes = new TreeMap<>();
39 builder.build()
40 .orderedNodes()
41 .filter(mn -> roots.contains(mn) && !mods.contains(mn))
42@@ -116,27 +115,17 @@
43 mods.addAll(ns);
44
45 if (!ns.isEmpty()) {
46- Map<String, Path> moduleToPath = ns.stream()
47- .collect(toMap(Function.identity(), this::moduleToPath));
48- hashes.put(mn, ModuleHashes.generate(moduleToPath, "SHA-256"));
49+ Set<ModuleReference> mrefs = ns.stream()
50+ .map(name -> configuration.findModule(name)
51+ .orElseThrow(InternalError::new))
52+ .map(ResolvedModule::reference)
53+ .collect(toSet());
54+ hashes.put(mn, ModuleHashes.generate(mrefs, "SHA-256"));
55 }
56 });
57 return hashes;
58 }
59
60- private Path moduleToPath(String name) {
61- ResolvedModule rm = configuration.findModule(name).orElseThrow(
62- () -> new InternalError("Selected module " + name + " not on module path"));
63-
64- URI uri = rm.reference().location().get();
65- Path path = Paths.get(uri);
66- String fn = path.getFileName().toString();
67- if (!fn.endsWith(".jar") && !fn.endsWith(".jmod")) {
68- throw new UnsupportedOperationException(path + " is not a modular JAR or jmod file");
69- }
70- return path;
71- }
72-
73 /*
74 * Utility class
75 */diff -ru orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java
76--- orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java 1970-01-01 01:00:01.000000000 +0100
77+++ jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java 2022-04-12 16:58:05.639985936 +0200
78@@ -26,17 +26,21 @@
79 package jdk.internal.module;
80
81 import java.io.IOException;
82+import java.io.InputStream;
83 import java.io.UncheckedIOException;
84-import java.nio.ByteBuffer;
85-import java.nio.channels.FileChannel;
86-import java.nio.file.Path;
87+import java.lang.module.ModuleReader;
88+import java.lang.module.ModuleReference;
89+import java.nio.charset.StandardCharsets;
90 import java.security.MessageDigest;
91 import java.security.NoSuchAlgorithmException;
92+import java.util.Arrays;
93 import java.util.Collections;
94 import java.util.HashMap;
95 import java.util.Map;
96 import java.util.Objects;
97 import java.util.Set;
98+import java.util.TreeMap;
99+import java.util.function.Supplier;
100
101 /**
102 * The result of hashing the contents of a number of module artifacts.
103@@ -60,8 +64,8 @@
104 * @param algorithm the algorithm used to create the hashes
105 * @param nameToHash the map of module name to hash value
106 */
107- public ModuleHashes(String algorithm, Map<String, byte[]> nameToHash) {
108- this.algorithm = algorithm;
109+ ModuleHashes(String algorithm, Map<String, byte[]> nameToHash) {
110+ this.algorithm = Objects.requireNonNull(algorithm);
111 this.nameToHash = Collections.unmodifiableMap(nameToHash);
112 }
113
114@@ -95,54 +99,125 @@
115 }
116
117 /**
118- * Computes the hash for the given file with the given message digest
119- * algorithm.
120+ * Computes a hash from the names and content of a module.
121 *
122+ * @param reader the module reader to access the module content
123+ * @param algorithm the name of the message digest algorithm to use
124+ * @return the hash
125+ * @throws IllegalArgumentException if digest algorithm is not supported
126 * @throws UncheckedIOException if an I/O error occurs
127 * @throws RuntimeException if the algorithm is not available
128 */
129- public static byte[] computeHash(Path file, String algorithm) {
130+ private static byte[] computeHash(ModuleReader reader, String algorithm) {
131+ MessageDigest md;
132 try {
133- MessageDigest md = MessageDigest.getInstance(algorithm);
134-
135- // Ideally we would just mmap the file but this consumes too much
136- // memory when jlink is running concurrently on very large jmods
137- try (FileChannel fc = FileChannel.open(file)) {
138- ByteBuffer bb = ByteBuffer.allocate(32*1024);
139- while (fc.read(bb) > 0) {
140- bb.flip();
141- md.update(bb);
142- assert bb.remaining() == 0;
143- bb.clear();
144- }
145- }
146-
147- return md.digest();
148+ md = MessageDigest.getInstance(algorithm);
149 } catch (NoSuchAlgorithmException e) {
150- throw new RuntimeException(e);
151+ throw new IllegalArgumentException(e);
152+ }
153+ try {
154+ byte[] buf = new byte[32*1024];
155+ reader.list().sorted().forEach(rn -> {
156+ md.update(rn.getBytes(StandardCharsets.UTF_8));
157+ try (InputStream in = reader.open(rn).orElseThrow(java.util.NoSuchElementException::new)) {
158+ int n;
159+ while ((n = in.read(buf)) > 0) {
160+ md.update(buf, 0, n);
161+ }
162+ } catch (IOException ioe) {
163+ throw new UncheckedIOException(ioe);
164+ }
165+ });
166 } catch (IOException ioe) {
167 throw new UncheckedIOException(ioe);
168 }
169+ return md.digest();
170 }
171
172 /**
173- * Computes the hash for every entry in the given map, returning a
174- * {@code ModuleHashes} to encapsulate the result. The map key is
175- * the entry name, typically the module name. The map value is the file
176- * path to the entry (module artifact).
177+ * Computes a hash from the names and content of a module.
178 *
179+ * @param supplier supplies the module reader to access the module content
180+ * @param algorithm the name of the message digest algorithm to use
181+ * @return the hash
182+ * @throws IllegalArgumentException if digest algorithm is not supported
183+ * @throws UncheckedIOException if an I/O error occurs
184+ */
185+ static byte[] computeHash(Supplier<ModuleReader> supplier, String algorithm) {
186+ try (ModuleReader reader = supplier.get()) {
187+ return computeHash(reader, algorithm);
188+ } catch (IOException ioe) {
189+ throw new UncheckedIOException(ioe);
190+ }
191+ }
192+
193+ /**
194+ * Computes the hash from the names and content of a set of modules. Returns
195+ * a {@code ModuleHashes} to encapsulate the result.
196+ * @param mrefs the set of modules
197+ * @param algorithm the name of the message digest algorithm to use
198 * @return ModuleHashes that encapsulates the hashes
199+ * @throws IllegalArgumentException if digest algorithm is not supported
200+ * @throws UncheckedIOException if an I/O error occurs
201 */
202- public static ModuleHashes generate(Map<String, Path> map, String algorithm) {
203+ static ModuleHashes generate(Set<ModuleReference> mrefs, String algorithm) {
204 Map<String, byte[]> nameToHash = new HashMap<>();
205- for (Map.Entry<String, Path> entry: map.entrySet()) {
206- String name = entry.getKey();
207- Path path = entry.getValue();
208- nameToHash.put(name, computeHash(path, algorithm));
209+ for (ModuleReference mref : mrefs) {
210+ try (ModuleReader reader = mref.open()) {
211+ byte[] hash = computeHash(reader, algorithm);
212+ nameToHash.put(mref.descriptor().name(), hash);
213+ } catch (IOException ioe) {
214+ throw new UncheckedIOException(ioe);
215+ }
216 }
217 return new ModuleHashes(algorithm, nameToHash);
218 }
219
220+ @Override
221+ public int hashCode() {
222+ int h = algorithm.hashCode();
223+ for (Map.Entry<String, byte[]> e : nameToHash.entrySet()) {
224+ h = h * 31 + e.getKey().hashCode();
225+ h = h * 31 + Arrays.hashCode(e.getValue());
226+ }
227+ return h;
228+ }
229+
230+ @Override
231+ public boolean equals(Object obj) {
232+ if (!(obj instanceof ModuleHashes))
233+ return false;
234+ ModuleHashes other = (ModuleHashes) obj;
235+ if (!algorithm.equals(other.algorithm)
236+ || nameToHash.size() != other.nameToHash.size())
237+ return false;
238+ for (Map.Entry<String, byte[]> e : nameToHash.entrySet()) {
239+ String name = e.getKey();
240+ byte[] hash = e.getValue();
241+ if (!Arrays.equals(hash, other.nameToHash.get(name)))
242+ return false;
243+ }
244+ return true;
245+ }
246+
247+ @Override
248+ public String toString() {
249+ StringBuilder sb = new StringBuilder(algorithm);
250+ sb.append(" ");
251+ nameToHash.entrySet()
252+ .stream()
253+ .sorted(Map.Entry.comparingByKey())
254+ .forEach(e -> {
255+ sb.append(e.getKey());
256+ sb.append("=");
257+ byte[] ba = e.getValue();
258+ for (byte b : ba) {
259+ sb.append(String.format("%02x", b & 0xff));
260+ }
261+ });
262+ return sb.toString();
263+ }
264+
265 /**
266 * This is used by jdk.internal.module.SystemModules class
267 * generated at link time.
268diff -ru orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java
269--- orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java 1970-01-01 01:00:01.000000000 +0100
270+++ jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java 2022-04-12 16:43:12.967868689 +0200
271@@ -1,5 +1,5 @@
272 /*
273- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
274+ * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
275 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
276 *
277 * This code is free software; you can redistribute it and/or modify it
278diff -ru orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleReferences.java jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleReferences.java
279--- orig/jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleReferences.java 1970-01-01 01:00:01.000000000 +0100
280+++ jdk-3cc80be736f2/src/java.base/share/classes/jdk/internal/module/ModuleReferences.java 2022-04-12 16:43:12.971868797 +0200
281@@ -1,5 +1,5 @@
282 /*
283- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
284+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
285 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286 *
287 * This code is free software; you can redistribute it and/or modify it
288@@ -95,7 +95,7 @@
289 Path file) {
290 URI uri = file.toUri();
291 Supplier<ModuleReader> supplier = () -> new JarModuleReader(file, uri);
292- HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a);
293+ HashSupplier hasher = (a) -> ModuleHashes.computeHash(supplier, a);
294 return newModule(attrs, uri, supplier, patcher, hasher);
295 }
296
297@@ -105,7 +105,7 @@
298 static ModuleReference newJModModule(ModuleInfo.Attributes attrs, Path file) {
299 URI uri = file.toUri();
300 Supplier<ModuleReader> supplier = () -> new JModModuleReader(file, uri);
301- HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a);
302+ HashSupplier hasher = (a) -> ModuleHashes.computeHash(supplier, a);
303 return newModule(attrs, uri, supplier, null, hasher);
304 }
305