Skip to content

Make header parsing "safe" #20562

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

Merged
merged 6 commits into from
Apr 13, 2020
Merged

Conversation

benaadams
Copy link
Member

@benaadams benaadams commented Apr 6, 2020

To @blowdart with ❤

Contributes to #4720

/cc @davidfowl

Results #20562 (comment)

Before:

Method Mean Error StdDev Op/s
PlaintextTechEmpower 4,214.6 ns 81.241 ns 72.018 ns 237,268.2
JsonTechEmpower 271.9 ns 1.483 ns 1.314 ns 3,677,397.6

After:

Method Mean Error StdDev Op/s
PlaintextTechEmpower 3,766.7 ns 4.94 ns 3.86 ns 265,486.6
JsonTechEmpower 252.5 ns 0.55 ns 0.48 ns 3,960,332.8

run it against the JsonPlatform benchmark and it gives something around +3-5k RPS

@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@blowdart
Copy link
Contributor

blowdart commented Apr 6, 2020

How did you get Ben's password hacker person?

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Starting 'Default' pipelined plaintext benchmark with session ID '0418e4f483af43ab88ed6c514379f11b'. This could take up to 30 minutes...

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Baseline

Starting baseline run on '23a1b8b3c412513eff0ffa8b0c55afdfa94b722a'...
RequestsPerSecond:           340,766
Max CPU (%):                 99
WorkingSet (MB):             89
Avg. Latency (ms):           6.8
Startup (ms):                505
First Request (ms):          120.32
Latency (ms):                0.77
Total Requests:              5,145,605
Duration: (ms)               15,100
Socket Errors:               0
Bad Responses:               0
Build Time (ms):             10,004
Published Size (KB):         120,289
SDK:                         5.0.100-preview.2.20120.3
Runtime:                     5.0.0-preview.4.20201.1
ASP.NET Core:                5.0.0-preview.4.20203.11


PR

Starting PR run on 'd65ec3482aaf32e98ef02545cbdd4a20ba5f4579'...
| Description |     RPS | CPU (%) | Memory (MB) | Avg. Latency (ms) | Startup (ms) | Build Time (ms) | Published Size (KB) | First Request (ms) | Latency (ms) | Errors | Ratio |
| ----------- | ------- | ------- | ----------- | ----------------- | ------------ | --------------- | ------------------- | ------------------ | ------------ | ------ | ----- |
|      Before | 340,766 |      99 |          89 |               6.8 |          505 |           10004 |              120289 |             120.32 |         0.77 |      0 |  1.00 |
|       After | 336,138 |      99 |          87 |              6.87 |          511 |            7502 |              120289 |             128.36 |         0.54 |      0 |  0.99 |


@benaadams
Copy link
Member Author

How did you get Ben's password hacker person?

Hmm... I think it can go faster...

@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Starting 'Default' pipelined plaintext benchmark with session ID 'e14a4ddadca14a32a69d2c229c35de81'. This could take up to 30 minutes...

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Baseline

Starting baseline run on '23a1b8b3c412513eff0ffa8b0c55afdfa94b722a'...
RequestsPerSecond:           339,301
Max CPU (%):                 99
WorkingSet (MB):             88
Avg. Latency (ms):           6.84
Startup (ms):                481
First Request (ms):          119.93
Latency (ms):                0.66
Total Requests:              5,122,198
Duration: (ms)               15,100
Socket Errors:               0
Bad Responses:               0
Build Time (ms):             10,003
Published Size (KB):         120,289
SDK:                         5.0.100-preview.2.20120.3
Runtime:                     5.0.0-preview.4.20201.1
ASP.NET Core:                5.0.0-preview.4.20203.11


PR

Starting PR run on '10fd8cfaa66aab8124e7296493a932af7f40c943'...
| Description |     RPS | CPU (%) | Memory (MB) | Avg. Latency (ms) | Startup (ms) | Build Time (ms) | Published Size (KB) | First Request (ms) | Latency (ms) | Errors | Ratio |
| ----------- | ------- | ------- | ----------- | ----------------- | ------------ | --------------- | ------------------- | ------------------ | ------------ | ------ | ----- |
|      Before | 339,301 |      99 |          88 |              6.84 |          481 |           10003 |              120289 |             119.93 |         0.66 |      0 |  1.00 |
|       After | 333,614 |      99 |          89 |                 7 |          480 |            7508 |              120289 |             124.77 |         0.75 |      0 |  0.98 |


Copy link
Member

@gfoidl gfoidl left a comment

Choose a reason for hiding this comment

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

Nits

@benaadams
Copy link
Member Author

Meh skipping bounds check for one space doesn't make a great difference and doesn't look very pretty; so reverted

@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Starting 'Default' pipelined plaintext benchmark with session ID 'd03e9c0016c24d1dbe7c5d2298b203f8'. This could take up to 30 minutes...

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 6, 2020

Baseline

Starting baseline run on '23a1b8b3c412513eff0ffa8b0c55afdfa94b722a'...
RequestsPerSecond:           335,344
Max CPU (%):                 99
WorkingSet (MB):             88
Avg. Latency (ms):           6.88
Startup (ms):                507
First Request (ms):          157.9
Latency (ms):                0.59
Total Requests:              5,062,897
Duration: (ms)               15,100
Socket Errors:               0
Bad Responses:               0
Build Time (ms):             21,509
Published Size (KB):         120,289
SDK:                         5.0.100-preview.2.20120.3
Runtime:                     5.0.0-preview.4.20201.1
ASP.NET Core:                5.0.0-preview.4.20203.11


PR

Starting PR run on 'e31989b274e627c654b563b2bda3f92ec90eb460'...
| Description |     RPS | CPU (%) | Memory (MB) | Avg. Latency (ms) | Startup (ms) | Build Time (ms) | Published Size (KB) | First Request (ms) | Latency (ms) | Errors | Ratio |
| ----------- | ------- | ------- | ----------- | ----------------- | ------------ | --------------- | ------------------- | ------------------ | ------------ | ------ | ----- |
|      Before | 335,344 |      99 |          88 |              6.88 |          507 |           21509 |              120289 |              157.9 |         0.59 |      0 |  1.00 |
|       After | 342,796 |      99 |          88 |              6.73 |          492 |            7503 |              120289 |             126.63 |         1.58 |      0 |  1.02 |


@benaadams
Copy link
Member Author

How did you get Ben's password hacker person?

@blowdart you'll be pleased to know normal service has been resumed 😉

Before | 335,344
After  | 342,796

@benaadams benaadams force-pushed the safe-header-parsing branch from 6f6f452 to 515fb08 Compare April 7, 2020 02:26
@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 7, 2020

Starting 'Default' pipelined plaintext benchmark with session ID 'fdcf5ba5ae7b4edabd81a28624c093b3'. This could take up to 30 minutes...

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 7, 2020

Baseline

Starting baseline run on 'a5ee4fd34dd65bee73f78a08776d231ef6b893f0'...
RequestsPerSecond:           341,860
Max CPU (%):                 99
WorkingSet (MB):             92
Avg. Latency (ms):           6.82
Startup (ms):                482
First Request (ms):          119.82
Latency (ms):                0.83
Total Requests:              5,159,530
Duration: (ms)               15,090
Socket Errors:               0
Bad Responses:               0
Build Time (ms):             9,503
Published Size (KB):         120,289
SDK:                         5.0.100-preview.2.20120.3
Runtime:                     5.0.0-preview.4.20201.1
ASP.NET Core:                5.0.0-preview.4.20203.11


PR

Starting PR run on '515fb08f80d8fa20f7894580fdd4b7e17e837bb5'...
| Description |     RPS | CPU (%) | Memory (MB) | Avg. Latency (ms) | Startup (ms) | Build Time (ms) | Published Size (KB) | First Request (ms) | Latency (ms) | Errors | Ratio |
| ----------- | ------- | ------- | ----------- | ----------------- | ------------ | --------------- | ------------------- | ------------------ | ------------ | ------ | ----- |
|      Before | 341,860 |      99 |          92 |              6.82 |          482 |            9503 |              120289 |             119.82 |         0.83 |      0 |  1.00 |
|       After | 338,369 |      99 |          92 |              6.82 |          510 |            7513 |              120289 |             126.52 |         0.57 |      0 |  0.99 |


@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 7, 2020

Starting 'Default' pipelined plaintext benchmark with session ID 'ab1c5b8a298040fbacbbd70f1a2c2c90'. This could take up to 30 minutes...

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 7, 2020

Baseline

Starting baseline run on 'a5ee4fd34dd65bee73f78a08776d231ef6b893f0'...
RequestsPerSecond:           338,286
Max CPU (%):                 99
WorkingSet (MB):             91
Avg. Latency (ms):           6.92
Startup (ms):                510
First Request (ms):          155.42
Latency (ms):                0.79
Total Requests:              5,106,679
Duration: (ms)               15,100
Socket Errors:               0
Bad Responses:               0
Build Time (ms):             21,013
Published Size (KB):         120,330
SDK:                         5.0.100-preview.2.20120.3
Runtime:                     5.0.0-preview.4.20205.13
ASP.NET Core:                5.0.0-preview.4.20206.19


PR

Starting PR run on '9b867f2737c2225997134d167d9cb56b71796b2a'...
| Description |     RPS | CPU (%) | Memory (MB) | Avg. Latency (ms) | Startup (ms) | Build Time (ms) | Published Size (KB) | First Request (ms) | Latency (ms) | Errors | Ratio |
| ----------- | ------- | ------- | ----------- | ----------------- | ------------ | --------------- | ------------------- | ------------------ | ------------ | ------ | ----- |
|      Before | 338,286 |      99 |          91 |              6.92 |          510 |           21013 |              120330 |             155.42 |         0.79 |      0 |  1.00 |
|       After | 341,698 |      99 |          92 |              6.76 |          481 |            7505 |              120330 |             124.09 |          0.5 |      0 |  1.01 |


@benaadams
Copy link
Member Author

@aspnet-hello benchmark

@pr-benchmarks
Copy link

pr-benchmarks bot commented Apr 7, 2020

Starting 'Default' pipelined plaintext benchmark with session ID 'e38f2758627943d1824c07f26bba7134'. This could take up to 30 minutes...

@adamsitnik
Copy link
Member

I've fetched these changes and run them against the microbenchmarks from #20518

Before:

Method Mean Error StdDev Op/s
PlaintextTechEmpower 4,214.6 ns 81.241 ns 72.018 ns 237,268.2
JsonTechEmpower 271.9 ns 1.483 ns 1.314 ns 3,677,397.6

After:

Method Mean Error StdDev Op/s
PlaintextTechEmpower 3,766.7 ns 4.94 ns 3.86 ns 265,486.6
JsonTechEmpower 252.5 ns 0.55 ns 0.48 ns 3,960,332.8

I've also run it against the JsonPlatform benchmark and it gives something around +3-5k RPS improvement for JSON. @benaadams thank you!

@KrzysztofCwalina
Copy link
Member

Impressive!

@davidfowl
Copy link
Member

Safe and faster

@benaadams benaadams force-pushed the safe-header-parsing branch from 917e315 to 02debfc Compare April 9, 2020 01:56
@benaadams
Copy link
Member Author

Squashed to single commit and rebased

@benaadams
Copy link
Member Author

Errors from running out of space

npm WARN tar ENOSPC: no space left on device, write

And publish error which might be the same

Project new mvc --razor-runtime-compilation failed to publish.

@benaadams
Copy link
Member Author

Ubuntu System.IO.IOException: No space left on device

@@ -236,159 +234,229 @@ public unsafe bool ParseHeaders(TRequestHandler handler, ref SequenceReader<byte
// in the span to contain a header.
if (readAhead == 0)
{
length = span.IndexOf(ByteLF) + 1;
if (length > 0)
length = span.IndexOfAny(ByteCR, ByteLF);
Copy link
Member

Choose a reason for hiding this comment

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

👍

@benaadams benaadams force-pushed the safe-header-parsing branch from 5e58983 to 0ba3a52 Compare April 11, 2020 13:37
@benaadams
Copy link
Member Author

Rebased to see if ci is alive

@benaadams
Copy link
Member Author

It's alive 😎

@halter73 halter73 merged commit 6016e70 into dotnet:master Apr 13, 2020
@halter73
Copy link
Member

Thanks @benaadams!

@benaadams benaadams deleted the safe-header-parsing branch April 13, 2020 17:11
@benaadams
Copy link
Member Author

@anurse looks like release notes are done by milestones; want to stick one on this?

@analogrelay analogrelay added this to the 5.0.0-preview4 milestone Apr 23, 2020
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants