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

BlockHound integration #1821

Merged
merged 15 commits into from
Mar 16, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean the stack traces from BlockHound artifacts
  • Loading branch information
dkhalanskyjb committed Mar 13, 2020
commit 46bbf2aa6b89df43281d32b118559352453a39d9
27 changes: 26 additions & 1 deletion kotlinx-coroutines-debug/test/StracktraceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null, f
}
}

/** Clean the stacktraces from artifacts of BlockHound instrumentation
*
* BlockHound works by switching a native call by a class generated with ByteBuddy, which, if the blocking
* call is allowed in this context, in turn calls the real native call that is now available under a
* different name.
*
* The traces thus undergo the following two changes when the execution is instrumented:
* - The original native call is replaced with a non-native one with the same FQN, and
* - An additional native call is placed on top of the stack, with the original name that also has
* `$$BlockHound$$_` prepended at the last component.
*/
private fun cleanBlockHoundTraces(frames: List<String>): List<String> {
var result = mutableListOf<String>()
val blockHoundSubstr = "\$\$BlockHound\$\$_"
var i = 0
while (i < frames.size) {
result.add(frames[i].replace(blockHoundSubstr, ""))
if (frames[i].contains(blockHoundSubstr)) {
i += 1
}
i += 1
}
return result
}

public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) {
val baos = ByteArrayOutputStream()
DebugProbes.dumpCoroutines(PrintStream(baos))
Expand All @@ -85,7 +110,7 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) {
expected.withIndex().forEach { (index, trace) ->
val actualTrace = actual[index].trimStackTrace().sanitizeAddresses()
val expectedTrace = trace.trimStackTrace().sanitizeAddresses()
val actualLines = actualTrace.split("\n")
val actualLines = cleanBlockHoundTraces(actualTrace.split("\n"))
val expectedLines = expectedTrace.split("\n")
for (i in expectedLines.indices) {
assertEquals(expectedLines[i], actualLines[i])
Expand Down