Skip to content

Enable ByteCode verification by default#5774

Merged
jpbempel merged 2 commits into
masterfrom
jpbempel/enable-bytecode-verification
Aug 30, 2023
Merged

Enable ByteCode verification by default#5774
jpbempel merged 2 commits into
masterfrom
jpbempel/enable-bytecode-verification

Conversation

@jpbempel

Copy link
Copy Markdown
Member

What Does This Do

Once we have generated the new classfile we run a static analysis of the bytecode using the ASM library's analyzer. It helps find out before sending back the modified classfile to the JVM if the bytecode is not correct and then cancelling the instrumentation to not impact the user code.

Motivation

avoid verifier errors preventing class to be loaded

Additional Notes

@jpbempel
jpbempel requested review from a team as code owners August 24, 2023 10:57
@jpbempel
jpbempel requested review from cimi and ygree and removed request for a team August 24, 2023 10:57
@jpbempel jpbempel added the comp: debugger Dynamic Instrumentation label Aug 24, 2023
@pr-commenter

pr-commenter Bot commented Aug 24, 2023

Copy link
Copy Markdown

Benchmarks

Startup

Parameters

Baseline Candidate
commit 1.20.0-SNAPSHOT~be378f24bc 1.20.0-SNAPSHOT~460ad10f3f
config baseline candidate
See matching parameters
Baseline Candidate
application insecure-bank insecure-bank
module Agent Agent
parent None None
variant appsec appsec

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 62 cases.

Startup time reports for petclinic
gantt
    title petclinic - global startup overhead: candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc

    dateFormat X
    axisFormat %s
section tracing
Agent [baseline] (986.592 ms) : 0, 986592
Total [baseline] (9.156 s) : 0, 9156286
Agent [candidate] (987.838 ms) : 0, 987838
Total [candidate] (9.122 s) : 0, 9121949
section appsec
Agent [baseline] (1.071 s) : 0, 1071036
Total [baseline] (9.253 s) : 0, 9253178
Agent [candidate] (1.069 s) : 0, 1069262
Total [candidate] (9.235 s) : 0, 9234853
section iast
Agent [baseline] (1.09 s) : 0, 1089821
Total [baseline] (9.387 s) : 0, 9386729
Agent [candidate] (1.09 s) : 0, 1089570
Total [candidate] (9.392 s) : 0, 9392332
section profiling
Agent [baseline] (1.16 s) : 0, 1160058
Total [baseline] (9.412 s) : 0, 9411552
Agent [candidate] (1.158 s) : 0, 1157600
Total [candidate] (9.49 s) : 0, 9489991
Loading
  • baseline results
Module Variant Duration Δ tracing
Agent tracing 986.592 ms -
Agent appsec 1.071 s 84.443 ms (8.6%)
Agent iast 1.09 s 103.229 ms (10.5%)
Agent profiling 1.16 s 173.466 ms (17.6%)
Total tracing 9.156 s -
Total appsec 9.253 s 96.892 ms (1.1%)
Total iast 9.387 s 230.443 ms (2.5%)
Total profiling 9.412 s 255.266 ms (2.8%)
  • candidate results
Module Variant Duration Δ tracing
Agent tracing 987.838 ms -
Agent appsec 1.069 s 81.423 ms (8.2%)
Agent iast 1.09 s 101.732 ms (10.3%)
Agent profiling 1.158 s 169.762 ms (17.2%)
Total tracing 9.122 s -
Total appsec 9.235 s 112.904 ms (1.2%)
Total iast 9.392 s 270.383 ms (3.0%)
Total profiling 9.49 s 368.042 ms (4.0%)
gantt
    title petclinic - break down per module: candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc

    dateFormat X
    axisFormat %s
section tracing
BytebuddyAgent [baseline] (611.838 ms) : 0, 611838
BytebuddyAgent [candidate] (612.206 ms) : 0, 612206
GlobalTracer [baseline] (283.731 ms) : 0, 283731
GlobalTracer [candidate] (284.421 ms) : 0, 284421
AppSec [baseline] (49.996 ms) : 0, 49996
AppSec [candidate] (50.233 ms) : 0, 50233
Remote Config [baseline] (666.688 µs) : 0, 667
Remote Config [candidate] (665.001 µs) : 0, 665
Telemetry [baseline] (5.988 ms) : 0, 5988
Telemetry [candidate] (6.056 ms) : 0, 6056
section appsec
BytebuddyAgent [baseline] (612.38 ms) : 0, 612380
BytebuddyAgent [candidate] (610.752 ms) : 0, 610752
GlobalTracer [baseline] (283.072 ms) : 0, 283072
GlobalTracer [candidate] (283.121 ms) : 0, 283121
AppSec [baseline] (134.872 ms) : 0, 134872
AppSec [candidate] (134.879 ms) : 0, 134879
Remote Config [baseline] (752.346 µs) : 0, 752
Remote Config [candidate] (721.675 µs) : 0, 722
Telemetry [baseline] (5.714 ms) : 0, 5714
Telemetry [candidate] (5.708 ms) : 0, 5708
section iast
BytebuddyAgent [baseline] (718.073 ms) : 0, 718073
BytebuddyAgent [candidate] (718.136 ms) : 0, 718136
GlobalTracer [baseline] (270.089 ms) : 0, 270089
GlobalTracer [candidate] (269.725 ms) : 0, 269725
AppSec [baseline] (46.664 ms) : 0, 46664
AppSec [candidate] (46.975 ms) : 0, 46975
Remote Config [baseline] (574.84 µs) : 0, 575
Remote Config [candidate] (566.1 µs) : 0, 566
Telemetry [baseline] (5.761 ms) : 0, 5761
Telemetry [candidate] (5.663 ms) : 0, 5663
IAST [baseline] (14.378 ms) : 0, 14378
IAST [candidate] (14.33 ms) : 0, 14330
section profiling
BytebuddyAgent [baseline] (626.258 ms) : 0, 626258
BytebuddyAgent [candidate] (625.041 ms) : 0, 625041
GlobalTracer [baseline] (347.33 ms) : 0, 347330
GlobalTracer [candidate] (347.086 ms) : 0, 347086
AppSec [baseline] (48.966 ms) : 0, 48966
AppSec [candidate] (49.218 ms) : 0, 49218
Remote Config [baseline] (636.507 µs) : 0, 637
Remote Config [candidate] (641.711 µs) : 0, 642
Telemetry [baseline] (5.959 ms) : 0, 5959
Telemetry [candidate] (5.983 ms) : 0, 5983
ProfilingAgent [baseline] (78.101 ms) : 0, 78101
ProfilingAgent [candidate] (76.978 ms) : 0, 76978
Profiling [baseline] (78.125 ms) : 0, 78125
Profiling [candidate] (77.002 ms) : 0, 77002
Loading
Startup time reports for insecure-bank
gantt
    title insecure-bank - global startup overhead: candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc

    dateFormat X
    axisFormat %s
section tracing
Agent [baseline] (987.127 ms) : 0, 987127
Total [baseline] (8.617 s) : 0, 8616542
Agent [candidate] (989.922 ms) : 0, 989922
Total [candidate] (8.625 s) : 0, 8625108
section appsec
Agent [baseline] (1.073 s) : 0, 1072672
Total [baseline] (8.681 s) : 0, 8680618
Agent [candidate] (1.073 s) : 0, 1072882
Total [candidate] (8.688 s) : 0, 8688409
section iast
Agent [baseline] (1.088 s) : 0, 1088349
Total [baseline] (9.148 s) : 0, 9147772
Agent [candidate] (1.097 s) : 0, 1097498
Total [candidate] (9.192 s) : 0, 9191601
section profiling
Agent [baseline] (1.164 s) : 0, 1164012
Total [baseline] (8.857 s) : 0, 8857142
Agent [candidate] (1.158 s) : 0, 1158157
Total [candidate] (8.849 s) : 0, 8849416
Loading
  • baseline results
Module Variant Duration Δ tracing
Agent tracing 987.127 ms -
Agent appsec 1.073 s 85.545 ms (8.7%)
Agent iast 1.088 s 101.222 ms (10.3%)
Agent profiling 1.164 s 176.885 ms (17.9%)
Total tracing 8.617 s -
Total appsec 8.681 s 64.076 ms (0.7%)
Total iast 9.148 s 531.23 ms (6.2%)
Total profiling 8.857 s 240.601 ms (2.8%)
  • candidate results
Module Variant Duration Δ tracing
Agent tracing 989.922 ms -
Agent appsec 1.073 s 82.96 ms (8.4%)
Agent iast 1.097 s 107.576 ms (10.9%)
Agent profiling 1.158 s 168.236 ms (17.0%)
Total tracing 8.625 s -
Total appsec 8.688 s 63.301 ms (0.7%)
Total iast 9.192 s 566.494 ms (6.6%)
Total profiling 8.849 s 224.309 ms (2.6%)
gantt
    title insecure-bank - break down per module: candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc

    dateFormat X
    axisFormat %s
section tracing
BytebuddyAgent [baseline] (612.119 ms) : 0, 612119
BytebuddyAgent [candidate] (614.135 ms) : 0, 614135
GlobalTracer [baseline] (284.317 ms) : 0, 284317
GlobalTracer [candidate] (284.775 ms) : 0, 284775
AppSec [baseline] (49.753 ms) : 0, 49753
AppSec [candidate] (50.032 ms) : 0, 50032
Remote Config [baseline] (660.779 µs) : 0, 661
Remote Config [candidate] (661.494 µs) : 0, 661
Telemetry [baseline] (5.962 ms) : 0, 5962
Telemetry [candidate] (5.973 ms) : 0, 5973
section appsec
BytebuddyAgent [baseline] (612.468 ms) : 0, 612468
BytebuddyAgent [candidate] (613.274 ms) : 0, 613274
GlobalTracer [baseline] (284.126 ms) : 0, 284126
GlobalTracer [candidate] (284.05 ms) : 0, 284050
AppSec [baseline] (135.263 ms) : 0, 135263
AppSec [candidate] (134.818 ms) : 0, 134818
Remote Config [baseline] (723.967 µs) : 0, 724
Remote Config [candidate] (720.605 µs) : 0, 721
Telemetry [baseline] (5.731 ms) : 0, 5731
Telemetry [candidate] (5.707 ms) : 0, 5707
section iast
BytebuddyAgent [baseline] (717.663 ms) : 0, 717663
BytebuddyAgent [candidate] (723.501 ms) : 0, 723501
GlobalTracer [baseline] (269.124 ms) : 0, 269124
GlobalTracer [candidate] (271.804 ms) : 0, 271804
AppSec [baseline] (46.657 ms) : 0, 46657
AppSec [candidate] (46.965 ms) : 0, 46965
Remote Config [baseline] (573.296 µs) : 0, 573
Remote Config [candidate] (578.719 µs) : 0, 579
Telemetry [baseline] (5.705 ms) : 0, 5705
Telemetry [candidate] (5.753 ms) : 0, 5753
IAST [baseline] (14.34 ms) : 0, 14340
IAST [candidate] (14.445 ms) : 0, 14445
section profiling
BytebuddyAgent [baseline] (628.766 ms) : 0, 628766
BytebuddyAgent [candidate] (624.87 ms) : 0, 624870
GlobalTracer [baseline] (349.495 ms) : 0, 349495
GlobalTracer [candidate] (347.09 ms) : 0, 347090
AppSec [baseline] (49.256 ms) : 0, 49256
AppSec [candidate] (49.058 ms) : 0, 49058
Remote Config [baseline] (663.215 µs) : 0, 663
Remote Config [candidate] (630.295 µs) : 0, 630
Telemetry [baseline] (6.019 ms) : 0, 6019
Telemetry [candidate] (5.873 ms) : 0, 5873
ProfilingAgent [baseline] (76.602 ms) : 0, 76602
ProfilingAgent [candidate] (78.012 ms) : 0, 78012
Profiling [baseline] (76.627 ms) : 0, 76627
Profiling [candidate] (78.036 ms) : 0, 78036
Loading

Load

Parameters

Baseline Candidate
commit 1.20.0-SNAPSHOT~be378f24bc 1.20.0-SNAPSHOT~460ad10f3f
config baseline candidate
end_time 2023-08-29T16:33:48 2023-08-29T16:51:47
start_time 2023-08-29T16:33:30 2023-08-29T16:51:30
See matching parameters
Baseline Candidate
application insecure-bank insecure-bank
variant appsec appsec

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 cases.

Request duration reports for petclinic
gantt
    title petclinic - request duration [CI 0.99] : candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc
    dateFormat X
    axisFormat %s
section baseline
no_agent (1.318 ms) : 1299, 1337
.   : milestone, 1318,
appsec (1.681 ms) : 1656, 1706
.   : milestone, 1681,
iast (1.446 ms) : 1422, 1471
.   : milestone, 1446,
profiling (1.436 ms) : 1411, 1460
.   : milestone, 1436,
tracing (1.433 ms) : 1409, 1456
.   : milestone, 1433,
section candidate
no_agent (1.343 ms) : 1324, 1362
.   : milestone, 1343,
appsec (1.662 ms) : 1638, 1686
.   : milestone, 1662,
iast (1.467 ms) : 1443, 1491
.   : milestone, 1467,
profiling (1.46 ms) : 1436, 1484
.   : milestone, 1460,
tracing (1.453 ms) : 1429, 1476
.   : milestone, 1453,
Loading
  • baseline results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 1.318 ms [1.299 ms, 1.337 ms] -
appsec 1.681 ms [1.656 ms, 1.706 ms] 362.437 µs (27.5%)
iast 1.446 ms [1.422 ms, 1.471 ms] 127.95 µs (9.7%)
profiling 1.436 ms [1.411 ms, 1.46 ms] 117.074 µs (8.9%)
tracing 1.433 ms [1.409 ms, 1.456 ms] 114.152 µs (8.7%)
  • candidate results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 1.343 ms [1.324 ms, 1.362 ms] -
appsec 1.662 ms [1.638 ms, 1.686 ms] 318.715 µs (23.7%)
iast 1.467 ms [1.443 ms, 1.491 ms] 123.577 µs (9.2%)
profiling 1.46 ms [1.436 ms, 1.484 ms] 116.87 µs (8.7%)
tracing 1.453 ms [1.429 ms, 1.476 ms] 109.737 µs (8.2%)
Request duration reports for insecure-bank
gantt
    title insecure-bank - request duration [CI 0.99] : candidate=1.20.0-SNAPSHOT~460ad10f3f, baseline=1.20.0-SNAPSHOT~be378f24bc
    dateFormat X
    axisFormat %s
section baseline
no_agent (366.481 µs) : 346, 387
.   : milestone, 366,
appsec (654.94 µs) : 634, 676
.   : milestone, 655,
iast (444.806 µs) : 424, 465
.   : milestone, 445,
iast_FULL (512.142 µs) : 491, 534
.   : milestone, 512,
iast_INACTIVE (425.993 µs) : 405, 447
.   : milestone, 426,
profiling (425.899 µs) : 405, 447
.   : milestone, 426,
tracing (423.087 µs) : 402, 444
.   : milestone, 423,
section candidate
no_agent (361.048 µs) : 341, 381
.   : milestone, 361,
appsec (659.298 µs) : 638, 680
.   : milestone, 659,
iast (447.965 µs) : 427, 469
.   : milestone, 448,
iast_FULL (513.747 µs) : 493, 535
.   : milestone, 514,
iast_INACTIVE (433.225 µs) : 412, 454
.   : milestone, 433,
profiling (434.738 µs) : 413, 456
.   : milestone, 435,
tracing (426.725 µs) : 406, 447
.   : milestone, 427,
Loading
  • baseline results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 366.481 µs [345.893 µs, 387.068 µs] -
appsec 654.94 µs [634.237 µs, 675.643 µs] 288.459 µs (78.7%)
iast 444.806 µs [424.39 µs, 465.223 µs] 78.326 µs (21.4%)
iast_FULL 512.142 µs [490.622 µs, 533.661 µs] 145.661 µs (39.7%)
iast_INACTIVE 425.993 µs [404.581 µs, 447.405 µs] 59.513 µs (16.2%)
profiling 425.899 µs [404.876 µs, 446.923 µs] 59.419 µs (16.2%)
tracing 423.087 µs [402.021 µs, 444.152 µs] 56.606 µs (15.4%)
  • candidate results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 361.048 µs [341.022 µs, 381.074 µs] -
appsec 659.298 µs [638.34 µs, 680.257 µs] 298.25 µs (82.6%)
iast 447.965 µs [426.872 µs, 469.058 µs] 86.917 µs (24.1%)
iast_FULL 513.747 µs [492.798 µs, 534.696 µs] 152.7 µs (42.3%)
iast_INACTIVE 433.225 µs [412.346 µs, 454.103 µs] 72.177 µs (20.0%)
profiling 434.738 µs [413.387 µs, 456.088 µs] 73.69 µs (20.4%)
tracing 426.725 µs [406.391 µs, 447.059 µs] 65.677 µs (18.2%)

@shatzi shatzi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff + few comments

}

@Test
public void lineRangeErrorSimpleSpan() throws IOException, URISyntaxException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test, while verify the code changes here, is very hard to understand.

this test doesn't not verify we run the verification logic, but instead that we didn't generate a span. so this can hide bug and not protect against regression. For example, let assume we got a different code that fails the instrumentation, this test will pass. but we don't have a code that verify that we run the static code analysis.

Can we create a more specific test verify we call static code analysis function? or maybe there is a better test to verify our safeguards are on by default?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's true that this test does not specially test that the static analysis is run. but it's really its purpose it's more that if an error during instrumentation happens, we dont' blow up the program and it still run correctly.
So in a sense it's more an integration test than a unit test.
I don't really care if this is the static analysis that is run or not, just that the program run correctly with or without ant instrumentation failure. here this is one.

@ygree ygree Aug 28, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a log assertion for "Verify result" or "Verification of instrumented class {} failed" if possible?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an additional test that can only pass with bytecode verification.

Config config = mock(Config.class);
when(config.isDebuggerEnabled()).thenReturn(true);
when(config.isDebuggerClassFileDumpEnabled()).thenReturn(true);
when(config.isDebuggerVerifyByteCode()).thenReturn(true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we no longer use "true" here? but "true" in other tests? what is the logic here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also confused about this change and would appreciate an explanation.

@jpbempel jpbempel Aug 28, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary for those tests, and all tests passed without it.

Config config = mock(Config.class);
when(config.isDebuggerEnabled()).thenReturn(true);
when(config.isDebuggerClassFileDumpEnabled()).thenReturn(true);
when(config.isDebuggerVerifyByteCode()).thenReturn(true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if DEFAULT_DEBUGGER_VERIFY_BYTECODE = true; - do we still need this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes because the default mock have all its fields initialize with default value, i.e. false.

}

private void verifyByteCode(String classFilePath, byte[] classFile) {
if (config.isDebuggerVerifyByteCode()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the change is a bit distracting because it doesn't change anything.

@ygree ygree left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. One suggestion for adding a validation log record test assertion if possible.

Once we have generated the new classfile we run a static analysis of
the bytecode using the ASM library's analyzer. It helps find out
before sending back the modified classfile to the JVM if the bytecode
is not correct and then cancelling the instrumentation to not impact
the user code.
only pass test if verify bytecode is run
@jpbempel
jpbempel force-pushed the jpbempel/enable-bytecode-verification branch from d786e0a to 460ad10 Compare August 29, 2023 16:10
@jpbempel
jpbempel merged commit 4818319 into master Aug 30, 2023
@jpbempel
jpbempel deleted the jpbempel/enable-bytecode-verification branch August 30, 2023 07:08
@github-actions github-actions Bot added this to the 1.20.0 milestone Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: debugger Dynamic Instrumentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants