-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16071][SQL] Checks size limit when doubling the array size in BufferHolder #13829
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,13 @@ public BufferHolder(UnsafeRow row) { | |
| } | ||
|
|
||
| public BufferHolder(UnsafeRow row, int initialSize) { | ||
| this.fixedSize = UnsafeRow.calculateBitSetWidthInBytes(row.numFields()) + 8 * row.numFields(); | ||
| int bitsetWidthInBytes = UnsafeRow.calculateBitSetWidthInBytes(row.numFields()); | ||
| if (row.numFields() > (Integer.MAX_VALUE - initialSize - bitsetWidthInBytes) / 8) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand this, we are trying to avoid overflow of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| throw new UnsupportedOperationException( | ||
| "Cannot create BufferHolder for input UnsafeRow because there are " + | ||
| "too many fields (number of fields: " + row.numFields() + ")"); | ||
| } | ||
| this.fixedSize = bitsetWidthInBytes + 8 * row.numFields(); | ||
| this.buffer = new byte[fixedSize + initialSize]; | ||
| this.row = row; | ||
| this.row.pointTo(buffer, buffer.length); | ||
|
|
@@ -55,10 +61,16 @@ public BufferHolder(UnsafeRow row, int initialSize) { | |
| * Grows the buffer by at least neededSize and points the row to the buffer. | ||
| */ | ||
| public void grow(int neededSize) { | ||
| if (neededSize > Integer.MAX_VALUE - totalSize()) { | ||
| throw new UnsupportedOperationException( | ||
| "Cannot grow BufferHolder by size " + neededSize + " because the size after growing " + | ||
| "exceeds size limitation " + Integer.MAX_VALUE); | ||
| } | ||
| final int length = totalSize() + neededSize; | ||
| if (buffer.length < length) { | ||
| // This will not happen frequently, because the buffer is re-used. | ||
| final byte[] tmp = new byte[length * 2]; | ||
| int newLength = length < Integer.MAX_VALUE / 2 ? length * 2 : Integer.MAX_VALUE; | ||
| final byte[] tmp = new byte[newLength]; | ||
| Platform.copyMemory( | ||
| buffer, | ||
| Platform.BYTE_ARRAY_OFFSET, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions.codegen | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.expressions.UnsafeRow | ||
|
|
||
| class BufferHolderSuite extends SparkFunSuite { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good starting point! Most of the low-level codegen utils are not tested, e.g. |
||
|
|
||
| test("SPARK-16071 Check the size limit to avoid integer overflow") { | ||
| var e = intercept[UnsupportedOperationException] { | ||
| new BufferHolder(new UnsafeRow(Int.MaxValue / 8)) | ||
| } | ||
| assert(e.getMessage.contains("too many fields")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this string be defined in BufferHolder and referenced here so that the test wouldn't break if the exception message is modified ? |
||
|
|
||
| val holder = new BufferHolder(new UnsafeRow(1000)) | ||
| holder.reset() | ||
| holder.grow(1000) | ||
| e = intercept[UnsupportedOperationException] { | ||
| holder.grow(Integer.MAX_VALUE) | ||
| } | ||
| assert(e.getMessage.contains("exceeds size limitation")) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
nullBitsLengthis a better name?