Can't find the bug

Gavin Ray ray.gavin97 at gmail.com
Wed Jan 4 01:12:43 UTC 2023


Maurizio, the code I was able to get out of the email is below:

public class StdLibTest {

    private static MethodHandle strlen; // C's strlen() function
    private static MethodHandle strcat; // C's strcat() function

    @BeforeAll
    public static void locateFunctions() {
        Linker linker = Linker.nativeLinker();
        strlen = linker.downcallHandle(linker.defaultLookup().find("strlen
").get(),
                FunctionDescriptor.of(JAVA_LONG, ADDRESS));
        strcat = linker.downcallHandle(linker.defaultLookup().find("strcat
").get(),
                FunctionDescriptor.of(ADDRESS, ADDRESS, ADDRESS));
    }


    @Test
    public void strlen() throws Throwable {
        try (Arena arena = Arena.openConfined()) {
            MemorySegment segment = arena.allocateUtf8String("Hello");
            int length = (int) (long) strlen.invoke(segment);
            Assertions.assertSame(5, length);
        }
    }


    @Test
    public void foo() throws Throwable {
        String result = strcat("hello", "world");
    }

    /**
     * This is a one to one copy of strcat test taken from  StdLibTest.java
test from OpenJDK
     */
    String strcat(String s1, String s2) throws Throwable {
        try (var arena = Arena.openConfined()) {
            MemorySegment buf = arena.allocate(s1.length() + s2.length() + 1
);
            buf.setUtf8String(0, s1);
            MemorySegment other = arena.allocateUtf8String(s2);
            return ((MemorySegment)strcat.invokeExact(buf, other)).
getUtf8String(0);
        }
    }
}

On Tue, Jan 3, 2023 at 12:55 PM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> Hi Bernd,
> this mailing list truncates attachments. If you want us to look at some
> code you will have to paste it somewhere :-)
>
> Cheers
> Maurizio
>
> On 03/01/2023 17:14, Bernd Müller wrote:
> > Dear all,
> >
> > I try to train FFM. I even master quick sort but became desperate on
> > strcon.
> >
> > Attached you will find a Maven project with the strcon test copied one
> > to one from
> > OpenJDK 20 sources.
> >
> > "mvn test" results in
> >
> > [ERROR] Errors:
> > [ERROR]   StdLibTest.foo:49->strcat:60 » IndexOutOfBounds Out of bound
> > access on segment MemorySegment{ array: Optional.empty
> > address:139636573277136 limit: 0 }; new offset = 0; new length = 1
> >
> > I am running OpenJDK 64-Bit Server VM (build 20-ea+29-2280, mixed
> > mode, sharing) on Fedora 36.
> >
> > Please, can someone help ?
> >
> > Kind Regards,
> >
> > Bernd
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230103/d895c5f9/attachment.htm>


More information about the panama-dev mailing list