github-advanced-security[bot] commented on code in PR #794: URL: https://github.com/apache/datasketches-java/pull/794#discussion_r4473202241
########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(bytes), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(new DoubleTupleSketch(MemorySegment.ofArray(bytes), DoubleSummary.Mode.Sum) Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [DoubleTupleSketch.DoubleTupleSketch](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/990) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(bytes), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(new DoubleTupleSketch(MemorySegment.ofArray(bytes), DoubleSummary.Mode.Sum) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + } + } + + @Test + public void checkUpdatableTableLargerThanNominal() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_CUR_BYTE] = (byte) (bytes[LG_NOM_BYTE] + 2); + checkRejected(bytes); + final byte[] bytes2 = sketch(10).toByteArray(); + bytes2[LG_NOM_BYTE] = 30; + checkRejected(bytes2); + final byte[] bytes3 = sketch(10).toByteArray(); + bytes3[LG_NOM_BYTE] = 27; //lgK above the Theta maximum of 26 + checkRejected(bytes3); + } + + @Test + public void checkConstructorNominalEntriesLimit() { + final DoubleSummaryFactory factory = new DoubleSummaryFactory(DoubleSummary.Mode.Sum); + assertEquals(new UpdatableTupleSketch<Double, DoubleSummary>(1 << 26, 3, 1f, factory).getNominalEntries(), 1 << 26); + expectThrows(SketchesArgumentException.class, + () -> new UpdatableTupleSketch<Double, DoubleSummary>((1 << 26) + 1, 3, 1f, factory)); + expectThrows(SketchesArgumentException.class, + () -> new UpdatableTupleSketch<Double, DoubleSummary>(0, 3, 1f, factory)); + expectThrows(SketchesArgumentException.class, () -> new TupleUnion<DoubleSummary>(1 << 27, null)); + expectThrows(SketchesArgumentException.class, + () -> new DoubleTupleSketch(27, 3, 1f, DoubleSummary.Mode.Sum)); + } + + @Test + public void checkUpdatableCountBeyondImage() { + final byte[] bytes = sketch(10).toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/994) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(bytes), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(new DoubleTupleSketch(MemorySegment.ofArray(bytes), DoubleSummary.Mode.Sum) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + } + } + + @Test + public void checkUpdatableTableLargerThanNominal() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_CUR_BYTE] = (byte) (bytes[LG_NOM_BYTE] + 2); + checkRejected(bytes); + final byte[] bytes2 = sketch(10).toByteArray(); + bytes2[LG_NOM_BYTE] = 30; + checkRejected(bytes2); + final byte[] bytes3 = sketch(10).toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/993) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(bytes), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(new DoubleTupleSketch(MemorySegment.ofArray(bytes), DoubleSummary.Mode.Sum) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + } + } + + @Test + public void checkUpdatableTableLargerThanNominal() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_CUR_BYTE] = (byte) (bytes[LG_NOM_BYTE] + 2); + checkRejected(bytes); + final byte[] bytes2 = sketch(10).toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/992) ########## src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterCorruptImageTest.java: ########## @@ -0,0 +1,67 @@ +/* + * 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.filters.bloomfilter; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.testng.annotations.Test; + +/** + * Checks that corrupt BloomFilter images are rejected, or handled, without large allocations. + */ +public class BloomFilterCorruptImageTest { + private static final int NUM_LONGS_INT = 16; + private static final int LARGE = 1 << 28; //2 GB of longs + + @Test + public void checkNonEmptyArrayBeyondImage() { + final BloomFilter bf = BloomFilterBuilder.createBySize(1000, 3, 123L); + bf.update(1L); + final byte[] bytes = bf.toByteArray(); + ByteArrayUtil.putIntLE(bytes, NUM_LONGS_INT, LARGE); + final long allocated = AllocationTracker.allocatedBytes(() -> BloomFilter.heapify(MemorySegment.ofArray(bytes))); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> BloomFilter.heapify(MemorySegment.ofArray(bytes))); + } + + @SuppressWarnings("deprecation") + @Test + public void checkEmptyImageIsUsable() { + final BloomFilter bf = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + assertFalse(bf.query(1L)); + bf.update(1L); + assertTrue(bf.query(1L)); + final BloomFilter bf2 = BloomFilter.heapify(MemorySegment.ofArray(bf.toByteArray())); + assertTrue(bf2.query(1L)); + final BloomFilter empty = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + empty.union(bf); + assertTrue(empty.query(1L)); + final BloomFilter empty2 = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + empty2.invert(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [BloomFilter.invert](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/986) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/989) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/988) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.compact().toByteArray()), deser) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + + @Test + public void checkUpdatableLargeTable() { + final byte[] bytes = sketch(10).toByteArray(); + bytes[LG_NOM_BYTE] = 25; + bytes[LG_CUR_BYTE] = 26; //far too large for 10 entries + checkRejected(bytes); + } + + @Test + public void checkSmallNominalEntriesStillWork() { + //for nominal entries below 16 the table starts at the minimum size, which is more than twice nominal + for (int lgK = 0; lgK <= 5; lgK++) { + for (int lgRF = 0; lgRF <= 3; lgRF++) { + for (final int n : new int[] {0, 1, 3, 100}) { + final DoubleTupleSketch sk = new DoubleTupleSketch(lgK, lgRF, 1.0F, DoubleSummary.Mode.Sum); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + final byte[] bytes = sk.toByteArray(); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(bytes), deser).getRetainedEntries(), + sk.getRetainedEntries()); + assertEquals(new DoubleTupleSketch(MemorySegment.ofArray(bytes), DoubleSummary.Mode.Sum) + .getRetainedEntries(), sk.getRetainedEntries()); + } + } + } + } + + @Test + public void checkUpdatableTableLargerThanNominal() { + final byte[] bytes = sketch(10).toByteArray(); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/991) ########## src/test/java/org/apache/datasketches/tuple/TupleCorruptImageTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.tuple; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks that corrupt tuple sketch images are rejected without large allocations. + */ +@SuppressWarnings("deprecation") +public class TupleCorruptImageTest { + private static final int LG_NOM_BYTE = 5; + private static final int LG_CUR_BYTE = 6; + private static final int COMPACT_COUNT_INT = 8; + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private static UpdatableTupleSketch<Double, DoubleSummary> sketch(final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(new DoubleSummaryFactory(DoubleSummary.Mode.Sum)).build(); + for (int i = 0; i < n; i++) { sk.update(i, 1.0); } + return sk; + } + + private void checkRejected(final byte[] bytes) { + final MemorySegment seg = MemorySegment.ofArray(bytes); + final long allocated = AllocationTracker.allocatedBytes(() -> TupleSketch.heapifySketch(seg, deser)); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + } + + @Test + public void checkValidImagesStillWork() { + for (final int n : new int[] {0, 1, 10, 100, 10_000}) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = sketch(n); + assertEquals(TupleSketch.heapifySketch(MemorySegment.ofArray(sk.toByteArray()), deser).getRetainedEntries(), Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/987) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
