Repository navigation
REQ and KLL: reject images with an inconsistent N; cap REQ sections at 48 #810
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
Merged
+285
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c699629
REQ and KLL: reject images whose N is inconsistent; cap REQ sections …
leerho e4059c5
KLL: reject a non-empty image that retains no items
leerho 9a5840a
REQ: reject images with more than 64 levels
leerho fa7d155
REQ: reject an overflowing total weight for any N
leerho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/test/java/org/apache/datasketches/kll/KllCorruptNTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * 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.datasketches.kll; | ||
|
|
||
| import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR; | ||
| import static org.apache.datasketches.kll.KllPreambleUtil.N_LONG_ADR; | ||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
| import static org.testng.Assert.expectThrows; | ||
|
|
||
| import java.lang.foreign.MemorySegment; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import org.apache.datasketches.common.ArrayOfStringsSerDe; | ||
| import org.apache.datasketches.common.ByteArrayUtil; | ||
| import org.apache.datasketches.common.SketchesArgumentException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * Checks that KLL images whose n is negative, or less than the number of retained items, are rejected, | ||
| * and that a non-empty image that retains no items is rejected. | ||
| */ | ||
| public class KllCorruptNTest { | ||
| private static final int K = 20; | ||
|
|
||
| private static byte[] withN(final byte[] image, final long n) { | ||
| final byte[] bytes = image.clone(); | ||
| ByteArrayUtil.putLongLE(bytes, N_LONG_ADR, n); | ||
| return bytes; | ||
| } | ||
|
|
||
| private static void checkNValues(final byte[] image, final int retained, | ||
| final Consumer<MemorySegment> heapify) { | ||
| heapify.accept(MemorySegment.ofArray(withN(image, retained))); //the least valid n | ||
| for (final long n : new long[] {retained - 1L, -1L, Long.MIN_VALUE}) { | ||
| final MemorySegment seg = MemorySegment.ofArray(withN(image, n)); | ||
| expectThrows(SketchesArgumentException.class, () -> heapify.accept(seg)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void doublesRejectsBadN() { | ||
| final KllDoublesSketch sk = KllDoublesSketch.newHeapInstance(K); | ||
| for (int i = 0; i < 1000; i++) { sk.update(i); } | ||
| final int retained = sk.getNumRetained(); | ||
| checkNValues(sk.toByteArray(), retained, KllDoublesSketch::heapify); | ||
| checkNValues(sk.toByteArray(), retained, KllDoublesSketch::wrap); | ||
| checkNValues(KllHelper.toByteArray(sk, true), retained, KllDoublesSketch::heapify); | ||
| checkNValues(KllHelper.toByteArray(sk, true), retained, KllDoublesSketch::wrap); | ||
| assertEquals(KllDoublesSketch.heapify(MemorySegment.ofArray(sk.toByteArray())).getN(), 1000); | ||
| } | ||
|
|
||
| @Test | ||
| public void floatsAndLongsRejectBadN() { | ||
| final KllFloatsSketch fsk = KllFloatsSketch.newHeapInstance(K); | ||
| final KllLongsSketch lsk = KllLongsSketch.newHeapInstance(K); | ||
| for (int i = 0; i < 1000; i++) { fsk.update(i); lsk.update(i); } | ||
| checkNValues(fsk.toByteArray(), fsk.getNumRetained(), KllFloatsSketch::heapify); | ||
| checkNValues(KllHelper.toByteArray(fsk, true), fsk.getNumRetained(), KllFloatsSketch::wrap); | ||
| checkNValues(lsk.toByteArray(), lsk.getNumRetained(), KllLongsSketch::heapify); | ||
| checkNValues(KllHelper.toByteArray(lsk, true), lsk.getNumRetained(), KllLongsSketch::wrap); | ||
| } | ||
|
|
||
| @Test | ||
| public void itemsRejectsBadN() { | ||
| final ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe(); | ||
| final KllItemsSketch<String> sk = KllItemsSketch.newHeapInstance(K, String::compareTo, serDe); | ||
| for (int i = 0; i < 1000; i++) { sk.update(Integer.toString(i)); } | ||
| checkNValues(sk.toByteArray(), sk.getNumRetained(), | ||
| seg -> KllItemsSketch.heapify(seg, String::compareTo, serDe)); | ||
| checkNValues(sk.toByteArray(), sk.getNumRetained(), | ||
| seg -> KllItemsSketch.wrap(seg, String::compareTo, serDe)); | ||
| } | ||
|
|
||
| //Sets levelsArr[0] of a single-level image to the capacity, so the image retains no items. | ||
| private static byte[] withNoItems(final byte[] image) { | ||
| final byte[] bytes = image.clone(); | ||
| ByteArrayUtil.putIntLE(bytes, DATA_START_ADR, K); //one level: capacity = k | ||
| return bytes; | ||
| } | ||
|
|
||
| private static void checkNoItemsRejected(final byte[] image, final Consumer<MemorySegment> heapify) { | ||
| heapify.accept(MemorySegment.ofArray(image)); //the unchanged image is valid | ||
| final MemorySegment seg = MemorySegment.ofArray(withNoItems(image)); | ||
| final SketchesArgumentException e = expectThrows(SketchesArgumentException.class, () -> heapify.accept(seg)); | ||
| assertTrue(e.getMessage().startsWith(KllMemorySegmentValidate.NOT_EMPTY_NO_ITEMS)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsNonEmptyWithNoItems() { | ||
| final int n = 10; //fewer than k: one level | ||
| final ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe(); | ||
| final KllDoublesSketch dsk = KllDoublesSketch.newHeapInstance(K); | ||
| final KllFloatsSketch fsk = KllFloatsSketch.newHeapInstance(K); | ||
| final KllLongsSketch lsk = KllLongsSketch.newHeapInstance(K); | ||
| final KllItemsSketch<String> isk = KllItemsSketch.newHeapInstance(K, String::compareTo, serDe); | ||
| for (int i = 0; i < n; i++) { dsk.update(i); fsk.update(i); lsk.update(i); isk.update(Integer.toString(i)); } | ||
| assertEquals(dsk.getNumLevels(), 1); | ||
| for (final boolean updatable : new boolean[] {false, true}) { | ||
| checkNoItemsRejected(KllHelper.toByteArray(dsk, updatable), KllDoublesSketch::heapify); | ||
| checkNoItemsRejected(KllHelper.toByteArray(dsk, updatable), KllDoublesSketch::wrap); | ||
| checkNoItemsRejected(KllHelper.toByteArray(fsk, updatable), KllFloatsSketch::heapify); | ||
| checkNoItemsRejected(KllHelper.toByteArray(lsk, updatable), KllLongsSketch::wrap); | ||
| } | ||
| checkNoItemsRejected(isk.toByteArray(), seg -> KllItemsSketch.heapify(seg, String::compareTo, serDe)); | ||
| checkNoItemsRejected(isk.toByteArray(), seg -> KllItemsSketch.wrap(seg, String::compareTo, serDe)); | ||
| } | ||
|
|
||
| //A compact full image with n = 0 and no items is not empty by its layout, so it is rejected too. | ||
| @Test | ||
| public void rejectsCompactFullWithZeroNAndNoItems() { | ||
| final KllDoublesSketch sk = KllDoublesSketch.newHeapInstance(K); | ||
| for (int i = 0; i < 10; i++) { sk.update(i); } | ||
| final MemorySegment seg = MemorySegment.ofArray(withN(withNoItems(sk.toByteArray()), 0)); | ||
| expectThrows(SketchesArgumentException.class, () -> KllDoublesSketch.heapify(seg)); | ||
| } | ||
|
|
||
| //An empty updatable image has n = 0 and no items, and stays valid. | ||
| @Test | ||
| public void acceptsEmptyUpdatable() { | ||
| final byte[] image = KllHelper.toByteArray(KllDoublesSketch.newHeapInstance(K), true); | ||
| assertTrue(KllDoublesSketch.heapify(MemorySegment.ofArray(image)).isEmpty()); | ||
| assertTrue(KllDoublesSketch.wrap(MemorySegment.ofArray(image)).isEmpty()); | ||
| final byte[] fimage = KllHelper.toByteArray(KllFloatsSketch.newHeapInstance(K), true); | ||
| assertTrue(KllFloatsSketch.wrap(MemorySegment.ofArray(fimage)).isEmpty()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
totalN also can be corrupted. So "weight = -1" logic can't be guarded here. how about throw exception in L215?
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.
You're right, thanks. An image with N = -1 got past the check. It now throws as soon as the total weight overflows (fa7d155), and the new test covers N = -1.