Skip to content

Commit 824967b

Browse files
committed
[GR-41423] Add some markers for native platforms and refactor ExceptionSnippets and UnsafeMemorySupport.
PullRequest: graal/12923
2 parents e5119bb + f66408f commit 824967b

21 files changed

+230
-54
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package org.graalvm.nativeimage.impl;
43+
44+
import org.graalvm.nativeimage.ImageSingletons;
45+
46+
public interface UnsafeMemorySupport {
47+
48+
void unsafeCopyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
49+
50+
void unsafeCopySwapMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
51+
52+
void unsafeSetMemory(Object destBase, long destOffset, long bytes, byte bvalue);
53+
54+
static UnsafeMemorySupport get() {
55+
return ImageSingletons.lookup(UnsafeMemorySupport.class);
56+
}
57+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMemoryUtil.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,8 @@ public static void copyBackward(Pointer from, Pointer to, UnsignedWord size) {
267267
copyUnalignedLower(from, to, offset);
268268
}
269269

270-
/** Implementation of {@code Unsafe.copyMemory}. */
271-
public static void unsafeCopyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) {
272-
if (srcBase != null || destBase != null) {
273-
copyOnHeap(srcBase, WordFactory.unsigned(srcOffset), destBase, WordFactory.unsigned(destOffset), WordFactory.unsigned(bytes));
274-
} else {
275-
UnmanagedMemoryUtil.copy(WordFactory.pointer(srcOffset), WordFactory.pointer(destOffset), WordFactory.unsigned(bytes));
276-
}
277-
}
278-
279270
@Uninterruptible(reason = "Memory is on the heap, copying must not be interrupted.")
280-
private static void copyOnHeap(Object srcBase, UnsignedWord srcOffset, Object destBase, UnsignedWord destOffset, UnsignedWord size) {
271+
static void copyOnHeap(Object srcBase, UnsignedWord srcOffset, Object destBase, UnsignedWord destOffset, UnsignedWord size) {
281272
Word fromPtr = Word.objectToUntrackedPointer(srcBase).add(srcOffset);
282273
Word toPtr = Word.objectToUntrackedPointer(destBase).add(destOffset);
283274
UnmanagedMemoryUtil.copy(fromPtr, toPtr, size);
@@ -355,18 +346,8 @@ private static void fillUnalignedUpper(Pointer to, long value, UnsignedWord uppe
355346
}
356347
}
357348

358-
/** Implementation of {@code Unsafe.setMemory}. */
359-
public static void unsafeSetMemory(Object destBase, long destOffset, long bytes, byte bvalue) {
360-
// Can't use UnmanagedMemoryUtil.fill as that method doesn't guarantee atomicity.
361-
if (destBase != null) {
362-
fillOnHeap(destBase, destOffset, bytes, bvalue);
363-
} else {
364-
fill(WordFactory.pointer(destOffset), WordFactory.unsigned(bytes), bvalue);
365-
}
366-
}
367-
368349
@Uninterruptible(reason = "Accessed memory is on the heap, code must not be interrupted.")
369-
private static void fillOnHeap(Object destBase, long destOffset, long bytes, byte bvalue) {
350+
static void fillOnHeap(Object destBase, long destOffset, long bytes, byte bvalue) {
370351
Word fromPtr = Word.objectToUntrackedPointer(destBase).add(WordFactory.unsigned(destOffset));
371352
fill(fromPtr, WordFactory.unsigned(bytes), bvalue);
372353
}
@@ -397,21 +378,12 @@ public static void copySwap(Pointer from, Pointer to, UnsignedWord size, Unsigne
397378
}
398379

399380
@Uninterruptible(reason = "Accessed memory is on the heap, code must not be interrupted.")
400-
private static void copySwapOnHeap(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize) {
381+
static void copySwapOnHeap(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize) {
401382
Word fromPtr = Word.objectToUntrackedPointer(srcBase).add(WordFactory.unsigned(srcOffset));
402383
Word toPtr = Word.objectToUntrackedPointer(destBase).add(WordFactory.unsigned(destOffset));
403384
copySwap(fromPtr, toPtr, WordFactory.unsigned(bytes), WordFactory.unsigned(elemSize));
404385
}
405386

406-
/** Implementation of {@code Unsafe.copySwapMemory}. */
407-
public static void unsafeCopySwapMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize) {
408-
if (srcBase != null || destBase != null) {
409-
copySwapOnHeap(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
410-
} else {
411-
copySwap(WordFactory.unsigned(srcOffset), WordFactory.unsigned(destOffset), WordFactory.unsigned(bytes), WordFactory.unsigned(elemSize));
412-
}
413-
}
414-
415387
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
416388
private static void copySwap2(Pointer from, Pointer to, UnsignedWord size) {
417389
if (from.aboveThan(to)) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core;
26+
27+
import org.graalvm.nativeimage.Platforms;
28+
import org.graalvm.nativeimage.impl.InternalPlatform;
29+
import org.graalvm.nativeimage.impl.UnsafeMemorySupport;
30+
import org.graalvm.word.WordFactory;
31+
32+
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
33+
34+
@AutomaticallyRegisteredImageSingleton(UnsafeMemorySupport.class)
35+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
36+
public class UnsafeMemoryUtil implements UnsafeMemorySupport {
37+
38+
/** Implementation of {@code Unsafe.copyMemory}. */
39+
@Override
40+
public void unsafeCopyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) {
41+
if (srcBase != null || destBase != null) {
42+
JavaMemoryUtil.copyOnHeap(srcBase, WordFactory.unsigned(srcOffset), destBase, WordFactory.unsigned(destOffset), WordFactory.unsigned(bytes));
43+
} else {
44+
UnmanagedMemoryUtil.copy(WordFactory.pointer(srcOffset), WordFactory.pointer(destOffset), WordFactory.unsigned(bytes));
45+
}
46+
}
47+
48+
/** Implementation of {@code Unsafe.setMemory}. */
49+
@Override
50+
public void unsafeSetMemory(Object destBase, long destOffset, long bytes, byte bvalue) {
51+
// Can't use UnmanagedMemoryUtil.fill as that method doesn't guarantee atomicity.
52+
if (destBase != null) {
53+
JavaMemoryUtil.fillOnHeap(destBase, destOffset, bytes, bvalue);
54+
} else {
55+
JavaMemoryUtil.fill(WordFactory.pointer(destOffset), WordFactory.unsigned(bytes), bvalue);
56+
}
57+
}
58+
59+
/** Implementation of {@code Unsafe.copySwapMemory}. */
60+
@Override
61+
public void unsafeCopySwapMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize) {
62+
if (srcBase != null || destBase != null) {
63+
JavaMemoryUtil.copySwapOnHeap(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
64+
} else {
65+
JavaMemoryUtil.copySwap(WordFactory.unsigned(srcOffset), WordFactory.unsigned(destOffset), WordFactory.unsigned(bytes), WordFactory.unsigned(elemSize));
66+
}
67+
}
68+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CFunctionSnippets.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
4646
import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
4747
import org.graalvm.compiler.replacements.Snippets;
48+
import org.graalvm.nativeimage.Platforms;
4849
import org.graalvm.nativeimage.c.struct.SizeOf;
50+
import org.graalvm.nativeimage.impl.InternalPlatform;
4951
import org.graalvm.word.LocationIdentity;
5052

5153
import com.oracle.svm.core.FrameAccess;
@@ -247,6 +249,7 @@ private static void matchCallStructure(CFunctionPrologueNode prologueNode) {
247249
}
248250

249251
@AutomaticallyRegisteredFeature
252+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
250253
class CFunctionSnippetsFeature implements InternalFeature {
251254

252255
@Override

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/ExceptionSnippets.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
4646
import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
4747
import org.graalvm.compiler.replacements.Snippets;
48+
import org.graalvm.nativeimage.Platforms;
49+
import org.graalvm.nativeimage.impl.InternalPlatform;
4850
import org.graalvm.word.Pointer;
4951

5052
import com.oracle.svm.core.NeverInline;
53+
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
54+
import com.oracle.svm.core.feature.InternalFeature;
55+
import com.oracle.svm.core.graal.meta.RuntimeConfiguration;
5156
import com.oracle.svm.core.graal.nodes.ReadExceptionObjectNode;
5257
import com.oracle.svm.core.meta.SharedMethod;
5358
import com.oracle.svm.core.snippets.ExceptionUnwind;
@@ -110,3 +115,14 @@ public void lower(LoadExceptionObjectNode node, LoweringTool tool) {
110115
}
111116
}
112117
}
118+
119+
@AutomaticallyRegisteredFeature
120+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
121+
final class ExceptionFeature implements InternalFeature {
122+
123+
@Override
124+
public void registerLowerings(RuntimeConfiguration runtimeConfig, OptionValues options, Providers providers,
125+
Map<Class<? extends Node>, NodeLoweringProvider<?>> lowerings, boolean hosted) {
126+
ExceptionSnippets.registerLowerings(options, providers, lowerings);
127+
}
128+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/StackOverflowCheckImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import org.graalvm.nativeimage.ImageInfo;
6565
import org.graalvm.nativeimage.ImageSingletons;
6666
import org.graalvm.nativeimage.IsolateThread;
67+
import org.graalvm.nativeimage.Platforms;
68+
import org.graalvm.nativeimage.impl.InternalPlatform;
6769
import org.graalvm.word.UnsignedWord;
6870
import org.graalvm.word.WordFactory;
6971

@@ -494,6 +496,7 @@ public void lower(StackOverflowCheckNode node, LoweringTool tool) {
494496
}
495497

496498
@AutomaticallyRegisteredFeature
499+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
497500
final class StackOverflowCheckFeature implements InternalFeature {
498501

499502
@Override

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.graalvm.nativeimage.Platform;
6363
import org.graalvm.nativeimage.Platforms;
6464
import org.graalvm.nativeimage.c.function.CFunctionPointer;
65+
import org.graalvm.nativeimage.impl.InternalPlatform;
6566

6667
import com.oracle.svm.core.RuntimeAssertionsSupport;
6768
import com.oracle.svm.core.SubstrateUtil;
@@ -1103,12 +1104,14 @@ private static Method[] copyMethods(Method[] original) {
11031104
private native Constructor<?> getEnclosingConstructor();
11041105

11051106
@Substitute
1107+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
11061108
public static Class<?> forName(String className) throws Throwable {
11071109
Class<?> caller = Reflection.getCallerClass();
11081110
return forName(className, true, caller.getClassLoader());
11091111
}
11101112

11111113
@Substitute //
1114+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
11121115
public static Class<?> forName(@SuppressWarnings("unused") Module module, String className) throws Throwable {
11131116
/*
11141117
* The module system is not supported for now, therefore the module parameter is ignored and

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/FileSystemProviderSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
import com.oracle.svm.core.annotate.Substitute;
4646
import com.oracle.svm.core.annotate.TargetClass;
4747
import com.oracle.svm.core.annotate.TargetElement;
48+
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
4849
import com.oracle.svm.core.feature.InternalFeature;
4950
import com.oracle.svm.core.option.HostedOptionKey;
50-
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
5151

5252
public final class FileSystemProviderSupport {
5353

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangSubstitutions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@
5353
import org.graalvm.word.WordFactory;
5454

5555
import com.oracle.svm.core.Containers;
56+
import com.oracle.svm.core.NeverInline;
5657
import com.oracle.svm.core.SubstrateOptions;
5758
import com.oracle.svm.core.SubstrateUtil;
59+
import com.oracle.svm.core.Uninterruptible;
5860
import com.oracle.svm.core.annotate.Alias;
5961
import com.oracle.svm.core.annotate.AnnotateOriginal;
6062
import com.oracle.svm.core.annotate.Delete;
6163
import com.oracle.svm.core.annotate.KeepOriginal;
62-
import com.oracle.svm.core.NeverInline;
6364
import com.oracle.svm.core.annotate.RecomputeFieldValue;
6465
import com.oracle.svm.core.annotate.Substitute;
6566
import com.oracle.svm.core.annotate.TargetClass;
6667
import com.oracle.svm.core.annotate.TargetElement;
67-
import com.oracle.svm.core.Uninterruptible;
6868
import com.oracle.svm.core.hub.ClassForNameSupport;
6969
import com.oracle.svm.core.hub.DynamicHub;
7070
import com.oracle.svm.core.jdk.JavaLangSubstitutions.ClassValueSupport;
@@ -218,6 +218,7 @@ final class Target_java_lang_StringUTF16 {
218218
}
219219

220220
@TargetClass(java.lang.Throwable.class)
221+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
221222
@SuppressWarnings({"unused"})
222223
final class Target_java_lang_Throwable {
223224

@@ -423,6 +424,7 @@ public static double pow(double a, double b) {
423424
}
424425

425426
@TargetClass(java.lang.StrictMath.class)
427+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
426428
final class Target_java_lang_StrictMath {
427429

428430
@Substitute

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SecuritySubstitutions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.graalvm.nativeimage.Platform;
5050
import org.graalvm.nativeimage.Platforms;
5151
import org.graalvm.nativeimage.hosted.FieldValueTransformer;
52+
import org.graalvm.nativeimage.impl.InternalPlatform;
5253
import org.graalvm.word.Pointer;
5354

5455
import com.oracle.svm.core.NeverInline;
@@ -74,6 +75,7 @@
7475
*/
7576

7677
@TargetClass(java.security.AccessController.class)
78+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
7779
@SuppressWarnings({"unused"})
7880
final class Target_java_security_AccessController {
7981

@@ -203,6 +205,7 @@ static AccessControlContext checkContext(AccessControlContext context, Class<?>
203205
}
204206

205207
@TargetClass(SecurityManager.class)
208+
@Platforms(InternalPlatform.NATIVE_ONLY.class)
206209
@SuppressWarnings({"static-method", "unused"})
207210
final class Target_java_lang_SecurityManager {
208211
@Substitute

0 commit comments

Comments
 (0)