Skip to content

Commit

Permalink
7903656: Variadic invoker class names are not mangled
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee
  • Loading branch information
mcimadamore committed Feb 9, 2024
1 parent e57efa8 commit 41b3ab6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/openjdk/jextract/impl/HeaderFileBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ private static class \{holderClass} {
}
""");
} else {
String invokerClassName = newHolderClassName(javaName);
String paramExprs = paramExprs(declType, finalParamNames, isVarArg);
String varargsParam = finalParamNames.get(finalParamNames.size() - 1);
appendBlankLine();
emitDocComment(decl, "Variadic invoker interface for:");
appendLines(STR."""
public interface \{javaName} {
public interface \{invokerClassName} {
\{retType} apply(\{paramExprs});
/**
Expand All @@ -210,7 +211,7 @@ public interface \{javaName} {
""");
emitDocComment(decl, "Variadic invoker factory for:");
appendLines(STR."""
public static \{javaName} \{javaName}(MemoryLayout... layouts) {
public static \{invokerClassName} \{javaName}(MemoryLayout... layouts) {
FunctionDescriptor baseDesc$ = \{functionDescriptorString(2, decl.type())};
var mh$ = \{runtimeHelperName()}.downcallHandleVariadic("\{nativeName}", baseDesc$, layouts);
return (\{paramExprs}) -> {
Expand All @@ -219,7 +220,7 @@ public interface \{javaName} {
traceDowncall(\{traceArgList});
}
\{returnWithCast}mh$.invokeExact(\{paramList});
} catch(IllegalArgumentException ex$) {
} catch(IllegalArgumentException | ClassCastException ex$) {
throw ex$; // rethrow IAE from passing wrong number/type of args
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
Expand Down Expand Up @@ -452,7 +453,7 @@ private void emitGlobalArraySetter(String holderClass, IndexList indexList,

private String emitVarHolderClass(Declaration.Variable var, String javaName) {
Type varType = var.type();
String mangledName = newHolderClassName(javaName);
String mangledName = newHolderClassName(STR."\{javaName}$constants");
String layoutType = Utils.layoutCarrierFor(varType).getSimpleName();
if (varType instanceof Type.Array) {
List<Long> dimensions = Utils.dimensions(varType);
Expand Down Expand Up @@ -589,7 +590,7 @@ private void emitPrimitiveTypedefLayout(String javaName, Type type, Declaration
}

private String newHolderClassName(String javaName) {
String holderClassName = STR."\{javaName}$constants";
String holderClassName = javaName;
while (!holderClassNames.add(holderClassName.toLowerCase())) {
holderClassName += "$";
}
Expand Down
11 changes: 5 additions & 6 deletions test/jtreg/generator/testPrintf/TestPrintf.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;

import static org.testng.Assert.assertEquals;
import static test.jextract.printf.printf_h.*;
Expand Down Expand Up @@ -63,7 +64,7 @@ public void testsPrintfInvoker(String fmt, Object[] args, String expected, Memor
}
}

@Test(dataProvider = "wrongArgsCases", expectedExceptions = IllegalArgumentException.class)
@Test(dataProvider = "wrongArgsCases", expectedExceptions = { IllegalArgumentException.class, ClassCastException.class })
public void testsPrintfInvokerWrongArgs(String fmt, MemoryLayout[] layouts, Object[] args) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment s = arena.allocate(1024);
Expand Down Expand Up @@ -95,11 +96,9 @@ public Object[][] cases() {
@DataProvider
public Object[][] wrongArgsCases() {
return new Object[][] {
{
"%d", new MemoryLayout[] {C_INT}, new Object[0], // too few args
"%d", new MemoryLayout[] {C_INT}, new Object[] { 1, 2 }, // too many args
"%.2f", new MemoryLayout[] {C_DOUBLE}, new Object[] { 1 }, // wrong type
}
{ "%d", new MemoryLayout[] {C_INT}, new Object[0], /* too few args */ },
{ "%d", new MemoryLayout[] {C_INT}, new Object[] { 1, 2 }, /* too many args */ },
{ "%.2f", new MemoryLayout[] {C_POINTER}, new Object[] { 1 }, /* wrong type */ },
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.jextract.test.toolprovider.variadicNames;

import java.nio.file.Path;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import testlib.JextractToolRunner;

import static org.testng.Assert.*;

public class TestMangledVariadicNames extends JextractToolRunner {

Loader loader;

@BeforeClass
public void before() {
Path output = getOutputFilePath("TestMangledVariadicNames-variadic_names.h");
Path input = getInputFilePath("variadic_names.h");
runAndCompile(output, input.toString());
loader = classLoader(output);
}

@Test
public void testMangledVariadicNames() {
Class<?> headerClass = loader.loadClass("variadic_names_h");
assertNotNull(headerClass);
assertNotNull(findNestedClass(headerClass, "f"));
assertNotNull(findNestedClass(headerClass, "F$"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

void f(int n, ...);
void F(int n, ...);

0 comments on commit 41b3ab6

Please sign in to comment.