Skip to content

Commit

Permalink
8331865: Consolidate size and alignment checks in LayoutPath
Browse files Browse the repository at this point in the history
Reviewed-by: psandoz, jvernee
  • Loading branch information
mcimadamore committed May 29, 2024
1 parent 6d718ae commit c003c12
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 133 deletions.
36 changes: 18 additions & 18 deletions src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,12 @@ public sealed interface MemoryLayout
* (this layout), or an {@link IllegalArgumentException} is thrown. Note
* that the alignment constraint of the root layout can be more strict
* (but not less) than the alignment constraint of the selected value layout.</li>
* <li>The offset of the access operation (computed as above) must fall inside
* the spatial bounds of the accessed memory segment, or an
* {@link IndexOutOfBoundsException} is thrown. This is the case when
* {@code O + A <= S}, where {@code O} is the accessed offset (computed as above),
* {@code A} is the size of the selected layout and {@code S} is the size of the
* accessed memory segment.</li>
* <li>The access operation must fall inside the spatial bounds of the accessed
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
* {@code A} is the size of this layout and {@code S} is the size of the
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
* <li>If the provided layout path has an open path element whose size is {@code S},
* its corresponding trailing {@code long} coordinate value {@code I} must be
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
Expand Down Expand Up @@ -753,12 +753,12 @@ public sealed interface MemoryLayout
* (this layout), or an {@link IllegalArgumentException} is thrown. Note
* that the alignment constraint of the root layout can be more strict
* (but not less) than the alignment constraint of the selected value layout.</li>
* <li>The offset of the access operation (computed as above) must fall inside
* the spatial bounds of the accessed memory segment, or an
* {@link IndexOutOfBoundsException} is thrown. This is the case when
* {@code O + A <= S}, where {@code O} is the accessed offset (computed as above),
* {@code A} is the size of the selected layout and {@code S} is the size of the
* accessed memory segment.</li>
* <li>The access operation must fall inside the spatial bounds of the accessed
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
* {@code A} is the size of this layout and {@code S} is the size of the
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
* <li>If the provided layout path has an open path element whose size is {@code S},
* its corresponding trailing {@code long} coordinate value {@code I} must be
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
Expand Down Expand Up @@ -822,12 +822,12 @@ public sealed interface MemoryLayout
* (this layout), or an {@link IllegalArgumentException} will be issued. Note
* that the alignment constraint of the root layout can be more strict
* (but not less) than the alignment constraint of the selected layout.</li>
* <li>The start offset of the slicing operation (computed as above) must fall
* inside the spatial bounds of the accessed memory segment, or an
* {@link IndexOutOfBoundsException} is thrown. This is the case when
* {@code O + A <= S}, where {@code O} is the start offset of
* the slicing operation (computed as above), {@code A} is the size of the
* selected layout and {@code S} is the size of the accessed memory segment.</li>
* <li>The slicing operation must fall inside the spatial bounds of the accessed
* memory segment, or an {@link IndexOutOfBoundsException} is thrown. This is the case
* when {@code B + A <= S}, where {@code B} is the base offset (defined above),
* {@code A} is the size of this layout and {@code S} is the size of the
* accessed memory segment. Note that the size of this layout might be <em>bigger</em>
* than the size of the accessed layout (e.g. when accessing a struct member).</li>
* <li>If the provided layout path has an open path element whose size is {@code S},
* its corresponding trailing {@code long} coordinate value {@code I} must be
* {@code 0 <= I < S}, or an {@link IndexOutOfBoundsException} is thrown.</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 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
Expand All @@ -25,8 +25,6 @@

package java.lang.invoke;

import jdk.internal.foreign.Utils;

/**
* Base class for memory segment var handle view implementations.
*/
Expand All @@ -42,23 +40,15 @@ abstract sealed class VarHandleSegmentViewBase extends VarHandle permits
/** endianness **/
final boolean be;

/** access size (in bytes, computed from var handle carrier type) **/
final long length;

/** alignment constraint (in bytes, expressed as a bit mask) **/
final long alignmentMask;

VarHandleSegmentViewBase(VarForm form, boolean be, long length, long alignmentMask, boolean exact) {
VarHandleSegmentViewBase(VarForm form, boolean be, long alignmentMask, boolean exact) {
super(form, exact);
this.be = be;
this.length = length;
this.alignmentMask = alignmentMask;
}

static IllegalArgumentException newIllegalArgumentExceptionForMisalignedAccess(long address) {
return new IllegalArgumentException("Misaligned access at address: " + Utils.toHexString(address));
}

static UnsupportedOperationException newUnsupportedAccessModeForAlignment(long alignment) {
return new UnsupportedOperationException("Unsupported access mode for alignment: " + alignment);
}
Expand Down
20 changes: 11 additions & 9 deletions src/java.base/share/classes/java/lang/invoke/VarHandles.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
Expand Down Expand Up @@ -306,6 +306,9 @@ else if (viewComponentType == float.class) {
* The resulting var handle will take a memory segment as first argument (the segment to be dereferenced),
* and a {@code long} as second argument (the offset into the segment).
*
* Note: the returned var handle does not perform any size or alignment check. It is up to clients
* to adapt the returned var handle and insert the appropriate checks.
*
* @param carrier the Java carrier type.
* @param alignmentMask alignment requirement to be checked upon access. In bytes. Expressed as a mask.
* @param byteOrder the byte order.
Expand All @@ -316,24 +319,23 @@ static VarHandle memorySegmentViewHandle(Class<?> carrier, long alignmentMask,
if (!carrier.isPrimitive() || carrier == void.class || carrier == boolean.class) {
throw new IllegalArgumentException("Invalid carrier: " + carrier.getName());
}
long size = Utils.byteWidthOfPrimitive(carrier);
boolean be = byteOrder == ByteOrder.BIG_ENDIAN;
boolean exact = VAR_HANDLE_SEGMENT_FORCE_EXACT;

if (carrier == byte.class) {
return maybeAdapt(new VarHandleSegmentAsBytes(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsBytes(be, alignmentMask, exact));
} else if (carrier == char.class) {
return maybeAdapt(new VarHandleSegmentAsChars(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsChars(be, alignmentMask, exact));
} else if (carrier == short.class) {
return maybeAdapt(new VarHandleSegmentAsShorts(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsShorts(be, alignmentMask, exact));
} else if (carrier == int.class) {
return maybeAdapt(new VarHandleSegmentAsInts(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsInts(be, alignmentMask, exact));
} else if (carrier == float.class) {
return maybeAdapt(new VarHandleSegmentAsFloats(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsFloats(be, alignmentMask, exact));
} else if (carrier == long.class) {
return maybeAdapt(new VarHandleSegmentAsLongs(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsLongs(be, alignmentMask, exact));
} else if (carrier == double.class) {
return maybeAdapt(new VarHandleSegmentAsDoubles(be, size, alignmentMask, exact));
return maybeAdapt(new VarHandleSegmentAsDoubles(be, alignmentMask, exact));
} else {
throw new IllegalStateException("Cannot get here");
}
Expand Down
Loading

1 comment on commit c003c12

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.