uber / h3

Hexagonal hierarchical geospatial indexing system
https://h3geo.org
Apache License 2.0
4.83k stars 459 forks source link

SIGSEGV in cellsToMultiPolygon for 104 cells #753

Closed jogoussard closed 1 year ago

jogoussard commented 1 year ago

Hi - not sure you will be able to help on this one, it might be very environment specific, I'm mostly posting in case you can reproduce on your side given the code to generate the fault is quite trivial (see below).

Using H3.cellsToMultiPolygon on Mac OS X, with the Java bindings, on a specific set of 104 cells, creates a SIGSEGV.

Code (kotlin)

Using com.uber:h3:4.1.1 maven dependency

        val cells = listOf(
            617683643010646015, 617683648070287359, 617683642951663615, 617683648070287359, 617683648070549503, 617683643014840319, 617683643013791743, 617683642951663615, 617683642951663615, 617683648065044479,
            617683648070549503, 617683643010383871, 617683643010646015, 617683643013791743, 617683643008024575, 617683643014840319, 617683643010383871, 617683642941177855, 617683642941177855, 617683642941439999,
            617683642951663615, 617683642950615039, 617683642950877183, 617683648065044479, 617683648070549503, 617683648057180159, 617683648065044479, 617683648064520191, 617683643013791743, 617683643014316031,
            617683643008548863, 617683643007238143, 617683643008024575, 617683643010383871, 617683642953498623, 617683642941177855, 617683643014316031, 617683642953760767, 617683642941439999, 617683642951139327,
            617683642950615039, 617683642951663615, 617683648065044479, 617683642950877183, 617683648064258047, 617683648063471615, 617683648066093055, 617683648057180159, 617683648064520191, 617683648057180159,
            617683648070549503, 617683648066093055, 617683643019296767, 617683643008548863, 617683643010383871, 617683643018510335, 617683643017723903, 617683642953760767, 617683642954285055, 617683642953236479,
            617683642953498623, 617683642953498623, 617683642940129279, 617683642941177855, 617683642950877183, 617683644212576255, 617683644211003391, 617683644212314111, 617683648064258047, 617683648057180159,
            617683648057442303, 617683648068190207, 617683648057180159, 617683648064520191, 617683648064782335, 617683648060850175, 617683648057442303, 617683648070811647, 617683648071335935, 617683648070287359,
            617683648070549503, 617683648131366911, 617683648070811647, 617683648066093055, 617683648067141631, 617683642944585727, 617683642947469311, 617683642940915711, 617683642940129279, 617683642953498623,
            617683642945372159, 617683642944323583, 617683642944585727, 617683648065568767, 617683648066093055, 617683648065830911, 617683648065830911, 617683648068190207, 617683648067665919, 617683648028344319,
            617683648065830911, 617683648067665919, 617683648068976639, 617683648028606463
        )
        H3.cellsToMultiPolygon(cells, true)

JVM error

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000000106dc3af4, pid=16185, tid=4867
#
# JRE version: Java(TM) SE Runtime Environment (17.0.1+12) (build 17.0.1+12-LTS-39)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (17.0.1+12-LTS-39, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-amd64)
# Problematic frame:
# C  [libh3-java17233222403992256207.dylib+0x3af4]  ConvertLinkedGeoPolygonToManaged+0x154
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# hs_err_pid16185.log
AHE@0x0000600002bf0c00: 0xb i2c: 0x00000001101a6400 c2i: 0x00000001101a6459 c2iUV: 0x00000001101a6424 c2iNCI: 0x00000001101a6496
Compiled method (c1)    1432 1140       2       java.util.ArrayList::<init> (12 bytes)

hs_err log:

Host: "MacBookPro16,1" x86_64 2600 MHz, 12 cores, 32G, Darwin 22.3.0, macOS 13.2 (22D49)
Time: Mon Feb 13 11:28:36 2023 EST elapsed time: 9.893696 seconds (0d 0h 0m 9s)

---------------  T H R E A D  ---------------

Current thread (0x00007fd54baab200):  JavaThread "http-nio-8080-exec-1" daemon [_thread_in_native, id=45315, stack(0x000070000c4a2000,0x000070000c5a2000)]

Stack: [0x000070000c4a2000,0x000070000c5a2000],  sp=0x000070000c59da80,  free space=1006k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libh3-java8548237689668314380.dylib+0x3af4]  ConvertLinkedGeoPolygonToManaged+0x154
C  [libh3-java8548237689668314380.dylib+0x3d18]  Java_com_uber_h3core_NativeMethods_cellsToLinkedMultiPolygon+0x68
...

Laptop

macbook pro - 2.6 GHz 6-Core Intel Core i7 - macOS 13.2 (22D49) - 32Gb RAM

isaacbrodsky commented 1 year ago

Thanks for reporting this. I was not able to reproduce this on the C library using the following test:

    TEST(issue753) {
        H3Index test[] = {
            617683643010646015, 617683648070287359, 617683642951663615,
            617683648070287359, 617683648070549503, 617683643014840319,
            617683643013791743, 617683642951663615, 617683642951663615,
            617683648065044479, 617683648070549503, 617683643010383871,
            617683643010646015, 617683643013791743, 617683643008024575,
            617683643014840319, 617683643010383871, 617683642941177855,
            617683642941177855, 617683642941439999, 617683642951663615,
            617683642950615039, 617683642950877183, 617683648065044479,
            617683648070549503, 617683648057180159, 617683648065044479,
            617683648064520191, 617683643013791743, 617683643014316031,
            617683643008548863, 617683643007238143, 617683643008024575,
            617683643010383871, 617683642953498623, 617683642941177855,
            617683643014316031, 617683642953760767, 617683642941439999,
            617683642951139327, 617683642950615039, 617683642951663615,
            617683648065044479, 617683642950877183, 617683648064258047,
            617683648063471615, 617683648066093055, 617683648057180159,
            617683648064520191, 617683648057180159, 617683648070549503,
            617683648066093055, 617683643019296767, 617683643008548863,
            617683643010383871, 617683643018510335, 617683643017723903,
            617683642953760767, 617683642954285055, 617683642953236479,
            617683642953498623, 617683642953498623, 617683642940129279,
            617683642941177855, 617683642950877183, 617683644212576255,
            617683644211003391, 617683644212314111, 617683648064258047,
            617683648057180159, 617683648057442303, 617683648068190207,
            617683648057180159, 617683648064520191, 617683648064782335,
            617683648060850175, 617683648057442303, 617683648070811647,
            617683648071335935, 617683648070287359, 617683648070549503,
            617683648131366911, 617683648070811647, 617683648066093055,
            617683648067141631, 617683642944585727, 617683642947469311,
            617683642940915711, 617683642940129279, 617683642953498623,
            617683642945372159, 617683642944323583, 617683642944585727,
            617683648065568767, 617683648066093055, 617683648065830911,
            617683648065830911, 617683648068190207, 617683648067665919,
            617683648028344319, 617683648065830911, 617683648067665919,
            617683648068976639, 617683648028606463};

        LinkedGeoPolygon polygon;
        t_assertSuccess(
            H3_EXPORT(cellsToLinkedMultiPolygon)(test, 104, &polygon));

        H3_EXPORT(destroyLinkedMultiPolygon)(&polygon);
    }

As the C library returns an error from the call to cellsToLinkedMultiPolygon. The reason the C library is returning an error is probably (although I didn't check this) because the input set has duplicates, which is not accepted by the algorithm.

Looking at the Java source, it seems in the JNI C adapter code we read the value from cellsToLinkedMultiPolygon before checking the error. This is probably the cause of the segfault. I've opened a PR uber/h3-java#129 to fix this.