Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove printf and vprintf tests #536

Changes from all commits
File filter
Filter by extension
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -39,6 +39,7 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
@@ -154,43 +155,61 @@ void test_vprintf(List<PrintfArg> args) throws Throwable {

static class StdLibHelper {

final static MethodHandle strcat = abi.downcallHandle(CLinker.systemLookup().lookup("strcat").get(),
final static SymbolLookup LOOKUP;

static {
SymbolLookup stdLibLookup = SymbolLookup.loaderLookup();
MemorySegment funcs = stdLibLookup.lookup("funcs").get()
.asSegment(C_POINTER.byteSize() * 3, ResourceScope.newImplicitScope());

SymbolLookup fallbackLookup = name -> switch (name) {
case "printf" -> Optional.of(MemoryAccess.getAddressAtIndex(funcs, 0));
case "vprintf" -> Optional.of(MemoryAccess.getAddressAtIndex(funcs, 1));
case "gmtime" -> Optional.of(MemoryAccess.getAddressAtIndex(funcs, 2));
default -> Optional.empty();

LOOKUP = name -> CLinker.systemLookup().lookup(name).or(() -> fallbackLookup.lookup(name));

final static MethodHandle strcat = abi.downcallHandle(LOOKUP.lookup("strcat").get(),
MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemoryAddress.class),
FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER));

final static MethodHandle strcmp = abi.downcallHandle(CLinker.systemLookup().lookup("strcmp").get(),
final static MethodHandle strcmp = abi.downcallHandle(LOOKUP.lookup("strcmp").get(),
MethodType.methodType(int.class, MemoryAddress.class, MemoryAddress.class),
FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER));

final static MethodHandle puts = abi.downcallHandle(CLinker.systemLookup().lookup("puts").get(),
final static MethodHandle puts = abi.downcallHandle(LOOKUP.lookup("puts").get(),
MethodType.methodType(int.class, MemoryAddress.class),
FunctionDescriptor.of(C_INT, C_POINTER));

final static MethodHandle strlen = abi.downcallHandle(CLinker.systemLookup().lookup("strlen").get(),
final static MethodHandle strlen = abi.downcallHandle(LOOKUP.lookup("strlen").get(),
MethodType.methodType(int.class, MemoryAddress.class),
FunctionDescriptor.of(C_INT, C_POINTER));

final static MethodHandle gmtime = abi.downcallHandle(CLinker.systemLookup().lookup("gmtime").get(),
final static MethodHandle gmtime = abi.downcallHandle(LOOKUP.lookup("gmtime").get(),
MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
FunctionDescriptor.of(C_POINTER, C_POINTER));

final static MethodHandle qsort = abi.downcallHandle(CLinker.systemLookup().lookup("qsort").get(),
final static MethodHandle qsort = abi.downcallHandle(LOOKUP.lookup("qsort").get(),
MethodType.methodType(void.class, MemoryAddress.class, long.class, long.class, MemoryAddress.class),
FunctionDescriptor.ofVoid(C_POINTER, C_LONG_LONG, C_LONG_LONG, C_POINTER));

final static FunctionDescriptor qsortComparFunction = FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER);

final static MethodHandle qsortCompar;

final static MethodHandle rand = abi.downcallHandle(CLinker.systemLookup().lookup("rand").get(),
final static MethodHandle rand = abi.downcallHandle(LOOKUP.lookup("rand").get(),

final static MethodHandle vprintf = abi.downcallHandle(CLinker.systemLookup().lookup("vprintf").get(),
final static MethodHandle vprintf = abi.downcallHandle(LOOKUP.lookup("vprintf").get(),
MethodType.methodType(int.class, MemoryAddress.class, VaList.class),
FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST));

final static MemoryAddress printfAddr = CLinker.systemLookup().lookup("printf").get();
final static MemoryAddress printfAddr = LOOKUP.lookup("printf").get();

final static FunctionDescriptor printfBase = FunctionDescriptor.of(C_INT, C_POINTER);

@@ -0,0 +1,39 @@
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* 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 if you need additional information or have any
* questions.

#include <stdio.h>
#include <time.h>

#ifdef _WIN64
#define EXPORT __declspec(dllexport)
#define EXPORT

// Forces generation of inline code on Windows
EXPORT void* funcs[] = {