1 /*
  2  * Copyright (c) 2020, 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.
  8  *
  9  *  This code is distributed in the hope that it will be useful, but WITHOUT
 10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  *  version 2 for more details (a copy is included in the LICENSE file that
 13  *  accompanied this code).
 14  *
 15  *  You should have received a copy of the GNU General Public License version
 16  *  2 along with this work; if not, write to the Free Software Foundation,
 17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 /*
 26  * @test
 27  * @run testng TestRebase
 28  */
 29 
 30 import jdk.incubator.foreign.MemoryAccess;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemorySegment;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import org.testng.annotations.DataProvider;
 35 import org.testng.annotations.Test;
 36 
 37 import java.util.ArrayList;
 38 import java.util.List;
 39 import java.util.function.IntFunction;
 40 
 41 import static org.testng.Assert.*;
 42 
 43 public class TestRebase {
 44 
 45     @Test(dataProvider = "slices")
 46     public void testRebase(SegmentSlice s1, SegmentSlice s2) {
 47         if (s1.contains(s2)) {
 48             //check that an address and its rebased counterpart point to same element
 49             MemoryAddress base = s2.segment.address();
 50             long offset = base.segmentOffset(s1.segment);
 51             for (int i = 0; i < s2.size(); i++) {
 52                 int expected = MemoryAccess.getByteAtOffset(s2.segment, i);
 53                 int found = (int)MemoryAccess.getByteAtOffset(s1.segment, i + offset);
 54                 assertEquals(found, expected);
 55             }
 56         } else if (s1.kind != s2.kind) {
 57             // check that rebase s1 to s2 fails
 58             try {
 59                 s1.segment.address().segmentOffset(s2.segment);
 60                 fail("Rebase unexpectedly passed!");
 61             } catch (IllegalArgumentException ex) {
 62                 assertTrue(true);
 63             }
 64         } else if (!s2.contains(s1)) {
 65             //disjoint segments - check that rebased address is out of bounds
 66             MemoryAddress base = s2.segment.address();
 67             long offset = base.segmentOffset(s1.segment);
 68             for (int i = 0; i < s2.size(); i++) {
 69                 MemoryAccess.getByteAtOffset(s2.segment, i);
 70                 try {
 71                     MemoryAccess.getByteAtOffset(s1.segment, i + offset);
 72                     fail("Rebased address on a disjoint segment is not out of bounds!");
 73                 } catch (IndexOutOfBoundsException ex) {
 74                     assertTrue(true);
 75                 }
 76             }
 77         }
 78     }
 79 
 80     static class SegmentSlice {
 81 
 82         enum Kind {
 83             NATIVE(i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope())),
 84             ARRAY(i -> MemorySegment.ofArray(new byte[i]));
 85 
 86             final IntFunction<MemorySegment> segmentFactory;
 87 
 88             Kind(IntFunction<MemorySegment> segmentFactory) {
 89                 this.segmentFactory = segmentFactory;
 90             }
 91 
 92             MemorySegment makeSegment(int elems) {
 93                 return segmentFactory.apply(elems);
 94             }
 95         }
 96 
 97         final Kind kind;
 98         final int first;
 99         final int last;
100         final MemorySegment segment;
101 
102         public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) {
103             this.kind = kind;
104             this.first = first;
105             this.last = last;
106             this.segment = segment;
107         }
108 
109         boolean contains(SegmentSlice other) {
110             return kind == other.kind &&
111                     first <= other.first &&
112                     last >= other.last;
113         }
114 
115         int size() {
116             return last - first + 1;
117         }
118     }
119 
120     @DataProvider(name = "slices")
121     static Object[][] slices() {
122         int[] sizes = { 16, 8, 4, 2, 1 };
123         List<SegmentSlice> slices = new ArrayList<>();
124         for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) {
125             //init root segment
126             MemorySegment segment = kind.makeSegment(16);
127             for (int i = 0 ; i < 16 ; i++) {
128                 MemoryAccess.setByteAtOffset(segment, i, (byte)i);
129             }
130             //compute all slices
131             for (int size : sizes) {
132                 for (int index = 0 ; index < 16 ; index += size) {
133                     MemorySegment slice = segment.asSlice(index, size);
134                     slices.add(new SegmentSlice(kind, index, index + size - 1, slice));
135                 }
136             }
137         }
138         Object[][] sliceArray = new Object[slices.size() * slices.size()][];
139         for (int i = 0 ; i < slices.size() ; i++) {
140             for (int j = 0 ; j < slices.size() ; j++) {
141                 sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) };
142             }
143         }
144         return sliceArray;
145     }
146 }