2

Following code generates error which looks like an error in compiler, its has nothing to do with the kotlin code, or maybe I am missing something? I have JDK error on macos and some more informative error (like pasted at the end of this post) from online compiler. How can I fix it?

Code: https://pl.kotl.in/CfdXHeDyn

import kotlin.math.pow

fun calculateNumberOfContainers(data: String): Int {
    val containerSizes = mutableListOf<Int>()
    data.lines().forEach { containerSizes.add(it.toInt()) }
    
    var foundCorrect = 0
    val maxSubSets = (2.0).pow(20).toULong()
    
    for (n in 0UL until maxSubSets) {
        var total = 0
    
        for (k in 0 until 20) {
            val mask = 1UL shl k
            if ((mask and n) != 0UL) {
                total += containerSizes[k]
                if (total > 150)
                    break
            }
        }
        if (total == 150)
            foundCorrect++
    }
    return foundCorrect
}

fun runcode() {
    val data = 
            """
33
14
18
20
45
35
16
35
1
13
18
13
50
44
48
6
24
41
30
42
        """.trimIndent()
    calculateNumberOfContainers(data)
}

fun main() {
    runcode()
}

Error:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('#' (code 35)): was expecting double-quote to start field name
 at [Source: (String)"{#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fc646ef2eba, pid=1900, tid=0x00007fc6307f7700
#
# JRE version: OpenJDK Runtime Environment (8.0_201-b09) (build 1.8.0_201-b09)
# Java VM: OpenJDK 64-Bit Server VM (25.201-b09 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x382eba]
#
# Core dump written. Default location: /var/task/core or core.1900
#
# An error report file with more information is saved as:
# /tmp/"[truncated 195 chars]; line: 1, column: 3]
 at com.fasterxml.jackson.core.JsonParser._constructError (JsonParser.java:1851) 
 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError (ParserMinimalBase.java:707) 
 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar (ParserMinimalBase.java:632) 

Update:

it works with 1.4.30, fails with 1.5.21, so its a bug in compiler

mike
  • 1,670
  • 11
  • 21
  • That seems more the playground issue than the compiler issue itself – gtxtreme Aug 06 '21 at 08:26
  • 1
    Able to reproduce it with `Java VM: Java HotSpot(TM) 64-Bit Server VM 18.9 (11.0.12+8-LTS-237, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)`. But error is a bit different: `EXCEPTION_ACCESS_VIOLATION (0xc0000005)`, `Problematic frame: # V [jvm.dll+0x1b4551]`. I believe it's not a compiler, but a JVM error, because even if the compiler has generated bad bytecode, JVM shouldn't crash, but report a bytecode verification error. Moreover, the error is gone if I disable [JVM C2 compilation](https://www.baeldung.com/jvm-tiered-compilation) (`-XX:TieredStopAtLevel=3`) – Михаил Нафталь Aug 06 '21 at 11:41
  • "Access violation" in Windows is more or less the same thing as "segmentation fault" (sigsegv) in Linux so it's probably the same cause. – Thomas Aug 06 '21 at 11:45
  • 1
    So, the crash happens during C2-compilation of internal stdlib method [kotlin.UnsignedKt::ulongCompare](https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/unsigned/src/kotlin/UnsignedUtils.kt#L11). – Михаил Нафталь Aug 06 '21 at 12:39
  • Works fine on the `openjdk-16.0.2` – Михаил Нафталь Aug 06 '21 at 12:52

1 Answers1

0

I changed from Unsigned Long to Unsigned Int and problem went away, code after changes looks as follows:

https://pl.kotl.in/XedacixZK

import kotlin.math.pow

fun calculateNumberOfContainers(data: String): Int {
    val containerSizes = mutableListOf<Int>()
    data.lines().forEach { containerSizes.add(it.toInt()) }
    
    var foundCorrect = 0
    val maxSubSets = (2.0).pow(20).toUInt()
    
    for (n in 0U until maxSubSets) {
        var total = 0
    
        for (k in 0 until 20) {
            val mask = 1U shl k
            if ((mask and n) != 0U) {
                total += containerSizes[k]
                if (total > 150)
                    break
            }
        }
        if (total == 150)
            foundCorrect++
    }
    return foundCorrect
}

fun runcode() {
    val data = 
            """
33
14
18
20
45
35
16
35
1
13
18
13
50
44
48
6
24
41
30
42
        """.trimIndent()
    print(calculateNumberOfContainers(data))
}

fun main() {
    runcode()
}
mike
  • 1,670
  • 11
  • 21