Skip to content
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

VerifierError: Call to wrong <init> method #2113

Closed
ycuicui opened this issue Jun 29, 2020 · 1 comment
Closed

VerifierError: Call to wrong <init> method #2113

ycuicui opened this issue Jun 29, 2020 · 1 comment

Comments

@ycuicui
Copy link

ycuicui commented Jun 29, 2020

The following test code exibit a java.lang.VerifyError: Call to wrong method

Kotlin version 1.3.72
Coroutines version 1.3.6

class VerifierErrorTest {

private val lock = ReentrantLock()

@Volatile
private var job: Job? = null

fun verifierErrorOK() {
    lock.withLock {
        if (job == null)
            job = GlobalScope.launch {
                try {
                    // Do something usefull outside lock
                    withContext(Dispatchers.Main) {
                        // Do something in the main thread
                    }
                } finally {
                    lock.withLock {
                        job = null
                    }
                }
            }
    }
}

fun verifierErrorKO() {
    lock.withLock {
        if (job == null)
            job = GlobalScope.launch {
                try {
                    // Do something usefull outside lock
                } finally {
                    // This create the problem
                    // Note: Removing the try/finally block clear the VerifierError
                    withContext(Dispatchers.Main) {
                        // Do something in the main thread
                    }
                    lock.withLock {
                        job = null
                    }
                }
            }
    }
}

companion object {
    @JvmStatic
    fun main(argv: Array<String>) {
        VerifierErrorTest().verifierErrorOK() // No PB
        VerifierErrorTest().verifierErrorKO() // Verifier error!
    }
}
}

The problem occurs when withContext is in the finally block

The error is:

Exception in thread "main" java.lang.VerifyError: Call to wrong <init> method
Exception Details:
  Location:
    test/VerifierErrorTest$verifierErrorKO$$inlined$withLock$lambda$1.invokeSuspend(Ljava/lang/Object;)Ljava/lang/Object; @183: invokespecial
  Reason:
    Type 'test/VerifierErrorTest$verifierErrorKO$$inlined$withLock$lambda$1$2' (constant pool 97) is not assignable to 'test/VerifierErrorTest$verifierErrorKO$$inlined$withLock$lambda$1$1' (constant pool 61)
  Bytecode:
    0x0000000: b800 264d 2ab4 0028 aa00 0000 0000 0133
    0x0000010: 0000 0000 0000 0002 0000 001c 0000 0051
    0x0000020: 0000 00d5 2bb8 002e 2ab4 0030 4e00 b800
    0x0000030: 36c0 0038 bb00 3a59 01b7 003d c000 072a
    0x0000040: 2a2d b500 3f2a 04b5 0028 b800 4559 2ca6
    0x0000050: 0017 2c3a 0457 1904 b02a b400 3fc0 0047
    0x0000060: 4e2b b800 2e2b 572a b400 19b8 004b c000
    0x0000070: 4d3a 0503 3606 1905 b900 5001 0000 0336
    0x0000080: 072a b400 1901 c000 52b8 0056 b200 5c3a
    0x0000090: 0719 05b9 005f 0100 a700 0f3a 0719 05b9
    0x00000a0: 005f 0100 1907 bfa7 0090 3a05 b800 36c0
    0x00000b0: 0038 bb00 6159 01b7 003d c000 072a 2a2d
    0x00000c0: b500 3f2a 1905 b500 632a 05b5 0028 b800
    0x00000d0: 4559 2ca6 0020 2c3a 0457 1904 b02a b400
    0x00000e0: 63c0 0065 3a05 2ab4 003f c000 474e 2bb8
    0x00000f0: 002e 2b57 2ab4 0019 b800 4bc0 004d 3a06
    0x0000100: 0336 0719 06b9 0050 0100 0003 3608 2ab4
    0x0000110: 0019 01c0 0052 b800 56b2 005c 3a08 1906
    0x0000120: b900 5f01 00a7 000f 3a08 1906 b900 5f01
    0x0000130: 0019 08bf 1905 bfb2 005c b0bb 0067 5912
    0x0000140: 69b7 006c bf                           
  Exception Handler Table:
    bci [125, 145] => handler: 155
    bci [155, 157] => handler: 155
    bci [266, 286] => handler: 296
    bci [296, 298] => handler: 296
    bci [45, 46] => handler: 170
    bci [170, 172] => handler: 170
  Stackmap Table:
    append_frame(@36,Object[#115])
    same_frame(@89)
    full_frame(@102,{Object[#2],Object[#115],Object[#115],Object[#71]},{Object[#115]})
    full_frame(@155,{Object[#2],Object[#115],Object[#115],Object[#71],Top,Object[#77],Integer},{Object[#101]})
    append_frame(@167,Object[#88])
    full_frame(@170,{Object[#2],Object[#115],Object[#115],Object[#71]},{Object[#101]})
    chop_frame(@221,1)
    full_frame(@243,{Object[#2],Object[#115],Object[#115],Object[#71],Top,Object[#101]},{Object[#115]})
    full_frame(@296,{Object[#2],Object[#115],Object[#115],Object[#71],Top,Object[#101],Object[#77],Integer},{Object[#101]})
    append_frame(@308,Object[#88])
    full_frame(@311,{Object[#2],Object[#115],Object[#115],Object[#71],Top,Object[#77],Integer,Object[#88]},{})
    full_frame(@315,{Object[#2],Object[#115],Object[#115]},{})

	at test.VerifierErrorTest.verifierErrorKO(VerifierErrorTest.kt:35)
	at test.VerifierErrorTest$Companion.main(VerifierErrorTest.kt:56)
	at test.VerifierErrorTest.main(VerifierErrorTest.kt)
@qwwdfsad
Copy link
Collaborator

Unfortunately, this is a bug in Kotlin compiler related to function with contracts that were added in #2030.
A workaround is to extract withContext calls from catch and finally blocks to separate functions.

The issue is fixed in 1.4-M2 compiler: https://youtrack.jetbrains.com/issue/KT-39298, please try it out and see if that helps

Closing as duplicate of #2050

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants