|
| 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.jdk.management; |
| 26 | + |
| 27 | +import static com.oracle.svm.core.c.function.CEntryPointOptions.NoEpilogue; |
| 28 | +import static com.oracle.svm.core.c.function.CEntryPointOptions.NoPrologue; |
| 29 | +import static com.oracle.svm.core.c.function.CEntryPointOptions.Publish; |
| 30 | +import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION; |
| 31 | + |
| 32 | +import org.graalvm.nativeimage.IsolateThread; |
| 33 | +import org.graalvm.nativeimage.StackValue; |
| 34 | +import org.graalvm.nativeimage.c.function.CEntryPoint; |
| 35 | +import org.graalvm.nativeimage.c.function.CFunction; |
| 36 | +import org.graalvm.nativeimage.c.function.CLibrary; |
| 37 | +import org.graalvm.nativeimage.c.type.CCharPointer; |
| 38 | +import org.graalvm.word.UnsignedWord; |
| 39 | +import org.graalvm.word.WordFactory; |
| 40 | + |
| 41 | +import com.oracle.svm.core.annotate.Uninterruptible; |
| 42 | +import com.oracle.svm.core.c.CGlobalData; |
| 43 | +import com.oracle.svm.core.c.CGlobalDataFactory; |
| 44 | +import com.oracle.svm.core.c.function.CEntryPointOptions; |
| 45 | +import com.oracle.svm.core.headers.LibC; |
| 46 | + |
| 47 | +public final class LibManagementExtSupport { |
| 48 | + private static final CGlobalData<CCharPointer> ERRMSG_FORMAT = CGlobalDataFactory.createCString("errno: %d error: %s\n"); |
| 49 | + |
| 50 | + /** |
| 51 | + * Reimplementation of the native {@code throw_internal_error} function in Java. |
| 52 | + */ |
| 53 | + @Uninterruptible(reason = "No Java context.") |
| 54 | + @CEntryPoint(name = "throw_internal_error", include = CEntryPoint.NotIncludedAutomatically.class) |
| 55 | + @CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.SymbolOnly) |
| 56 | + private static void throwInternalError(IsolateThread env, CCharPointer msg) { |
| 57 | + /* |
| 58 | + * Ported from `src/jdk.management/share/native/libmanagement_ext/management_ext.c`. |
| 59 | + */ |
| 60 | + CCharPointer errmsg = StackValue.get(128); |
| 61 | + |
| 62 | + snprintf(errmsg, WordFactory.unsigned(128), ERRMSG_FORMAT.get(), LibC.errno(), msg); |
| 63 | + jnuThrowInternalError(env, errmsg); |
| 64 | + } |
| 65 | + |
| 66 | + @CFunction(transition = NO_TRANSITION) |
| 67 | + private static native int snprintf(CCharPointer str, UnsignedWord size, CCharPointer format, int errno, CCharPointer msg); |
| 68 | + |
| 69 | + @CLibrary(value = "java", requireStatic = true) |
| 70 | + @CFunction(value = "JNU_ThrowInternalError", transition = NO_TRANSITION) |
| 71 | + private static native void jnuThrowInternalError(IsolateThread env, CCharPointer msg); |
| 72 | +} |
0 commit comments