Commit bc48a481 authored by qsr's avatar qsr Committed by Commit bot

mojo: Fixing array serialization for java bindings.

The num_bytes header for an array must contain the real size of the
array, not the 8 bytes aligned one.

R=ppi@chromium.org

Review URL: https://codereview.chromium.org/519673002

Cr-Commit-Position: refs/heads/master@{#292656}
parent 6e2f3e26
......@@ -58,6 +58,13 @@ public class BindingsHelper {
return (arrayNullability & ELEMENT_NULLABLE) > 0;
}
/**
* Align |size| on {@link BindingsHelper#ALIGNMENT}.
*/
public static long align(long size) {
return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
}
/**
* Compute the size in bytes of the given string encoded as utf8.
*/
......
......@@ -66,6 +66,9 @@ public class Decoder {
}
public void claimMemory(long start, long end) {
if (start % BindingsHelper.ALIGNMENT != 0) {
throw new DeserializationException("Incorrect starting alignment: " + start + ".");
}
if (start < mMinNextMemory) {
throw new DeserializationException("Trying to access memory out of order.");
}
......@@ -75,10 +78,7 @@ public class Decoder {
if (end > mMaxMemory) {
throw new DeserializationException("Trying to access out of range memory.");
}
if (start % BindingsHelper.ALIGNMENT != 0 || end % BindingsHelper.ALIGNMENT != 0) {
throw new DeserializationException("Incorrect alignment.");
}
mMinNextMemory = end;
mMinNextMemory = BindingsHelper.align(end);
}
}
......@@ -111,17 +111,17 @@ public class Decoder {
mMessage.buffer.order(ByteOrder.nativeOrder());
mBaseOffset = baseOffset;
mValidator = validator;
// Claim the memory for the header.
mValidator.claimMemory(mBaseOffset, mBaseOffset + DataHeader.HEADER_SIZE);
}
/**
* Deserializes a {@link DataHeader} at the given offset.
*/
public DataHeader readDataHeader() {
// Claim the memory for the header.
mValidator.claimMemory(mBaseOffset, mBaseOffset + DataHeader.HEADER_SIZE);
int size = readInt(DataHeader.SIZE_OFFSET);
int numFields = readInt(DataHeader.NUM_FIELDS_OFFSET);
// The memory for the header has already been claimed.
// Claim the remaining memory.
mValidator.claimMemory(mBaseOffset + DataHeader.HEADER_SIZE, mBaseOffset + size);
DataHeader res = new DataHeader(size, numFields);
return res;
......
......@@ -148,7 +148,7 @@ public class Encoder {
* (resizing the buffer if required).
*/
public void encode(DataHeader s) {
mEncoderState.claimMemory(s.size);
mEncoderState.claimMemory(BindingsHelper.align(s.size));
encode(s.size, DataHeader.SIZE_OFFSET);
encode(s.numFields, DataHeader.NUM_FIELDS_OFFSET);
}
......@@ -438,7 +438,7 @@ public class Encoder {
private Encoder encoderForArrayByTotalSize(int byteSize, int length, int offset) {
encodePointerToNextUnclaimedData(offset);
return getEncoderAtDataOffset(
new DataHeader(DataHeader.HEADER_SIZE + BindingsHelper.align(byteSize), length));
new DataHeader(DataHeader.HEADER_SIZE + byteSize, length));
}
private void encodeByteArray(byte[] bytes, int length, int offset) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment