-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure a 6 MiB stack size by default (#1131)
* Configure a 4 MiB stack size by default Previously this was unbounded, which meant that Zipline would scribble over the heap if the stack size was exceeded. * Higher expecations for deep recursion failures * Lower limit to not crash * Bump QuickJS' own limit to 6 MiB * Create the Zipline instance on the Zipline thread
- Loading branch information
1 parent
fee35f7
commit cbd4514
Showing
3 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
zipline-testing/src/jsMain/kotlin/app/cash/zipline/testing/recursing.kt
This file contains 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,33 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed 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 app.cash.zipline.testing | ||
|
||
import app.cash.zipline.Zipline | ||
|
||
private val zipline by lazy { Zipline.get() } | ||
|
||
class RecursingEchoService : EchoService { | ||
override fun echo(request: EchoRequest): EchoResponse { | ||
val recurseCount = request.message.toInt() | ||
if (recurseCount > 0) echo(EchoRequest((recurseCount - 1).toString())) | ||
return EchoResponse("recursed $recurseCount times!") | ||
} | ||
} | ||
|
||
@JsExport | ||
fun prepareRecursingService() { | ||
zipline.bind<EchoService>("recursingService", RecursingEchoService()) | ||
} |
This file contains 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
78 changes: 78 additions & 0 deletions
78
zipline/src/jniTest/kotlin/app/cash/zipline/ZiplineStackSizeTest.kt
This file contains 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,78 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed 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 app.cash.zipline | ||
|
||
import app.cash.zipline.testing.EchoRequest | ||
import app.cash.zipline.testing.EchoResponse | ||
import app.cash.zipline.testing.EchoService | ||
import app.cash.zipline.testing.loadTestingJs | ||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import java.util.concurrent.Executors | ||
import kotlin.test.assertFailsWith | ||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Ignore | ||
import org.junit.Test | ||
|
||
class ZiplineStackSizeTest { | ||
/** An executor service that uses 8 MiB thread stacks. */ | ||
private val executorService = Executors.newSingleThreadExecutor { runnable -> | ||
Thread(null, runnable, "Treehouse", 8 * 1024 * 1024) | ||
} | ||
private val dispatcher = executorService.asCoroutineDispatcher() | ||
private lateinit var zipline: Zipline | ||
|
||
@Before | ||
fun setUp() = runBlocking(dispatcher) { | ||
zipline = Zipline.create(dispatcher) | ||
zipline.loadTestingJs() | ||
} | ||
|
||
@After | ||
fun tearDown() = runBlocking(dispatcher) { | ||
zipline.close() | ||
} | ||
|
||
@Test | ||
fun deepRecursionDoesntCrash() { | ||
runBlocking(dispatcher) { | ||
zipline.quickJs.evaluate("testing.app.cash.zipline.testing.prepareRecursingService()") | ||
|
||
val recurseCount = 500 | ||
val service = zipline.take<EchoService>("recursingService") | ||
val echoResponse = service.echo(EchoRequest("$recurseCount")) | ||
assertThat(echoResponse).isEqualTo(EchoResponse("recursed $recurseCount times!")) | ||
} | ||
} | ||
|
||
@Test | ||
@Ignore("https://github.com/cashapp/zipline/issues/1130") | ||
fun veryDeepRecursionFailsGracefully() { | ||
runBlocking(dispatcher) { | ||
zipline.quickJs.evaluate("testing.app.cash.zipline.testing.prepareRecursingService()") | ||
|
||
val recurseCount = 2000 | ||
val service = zipline.take<EchoService>("recursingService") | ||
val e = assertFailsWith<QuickJsException> { | ||
service.echo(EchoRequest("$recurseCount")) | ||
} | ||
assertThat(e.message).isEqualTo("stack overflow") | ||
} | ||
} | ||
} |