Repository navigation
Keep the executable's zero-filled data smaller than a transparent huge page #47202
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
982b512
Keep the executable's zero-filled data smaller than a transparent hug…
robobun 84f8c37
[autofix.ci] apply automated fixes
autofix-ci[bot] 0e5a7a9
Report out of memory when the Huffman tables can't be allocated
robobun a549cc9
h3blast: define the function that lshpack reports out of memory through
robobun 765f81b
Read the program headers with the harness's ELF reader
robobun b6875a1
lshpack, lsqpack: load the address of the Huffman tables once per call
robobun bbc18df
Check at the link that the zero-filled data stays below 2 MiB
robobun 9fd3870
Test that the first HTTP/2 request fills no table in the zero-filled …
robobun 6f7a29e
verify-binary: refuse a PT_LOAD segment without its numbers
robobun 46dc434
Measure the first HTTP/2 request in a new process
robobun 20eea93
Merge remote-tracking branch 'origin/main' into robobun/2bacddea/bss-…
robobun aaeb1ae
Leave the size of the zero-filled data to the link and to start.test.ts
robobun 124e0db
Name the zero-filled data among the scans of the link
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,51 @@ | ||
| --- a/lsqpack.c 2026-10-28 13:33:26.015031842 +0000 | ||
| +++ b/lsqpack.c 2026-10-28 15:53:38.559339313 +0000 | ||
| @@ -69,6 +69,16 @@ | ||
| #include <xxhash.h> | ||
| #endif | ||
|
|
||
| +#if LS_QPACK_USE_LARGE_TABLES && LS_HPACK_LAZY_LARGE_TABLES | ||
| +/* Bun: share the hencs/hdecs that lshpack builds at its first init (same | ||
| + * RFC 7541 Huffman code) instead of carrying a second 768 KB copy in .rodata. */ | ||
| +struct henc { unsigned lens; uint32_t code; }; | ||
| +struct hdec { uint8_t lens; uint8_t out[3]; }; | ||
| +extern struct lshpack_huff_tables { struct henc encs[65536]; struct hdec decs[65536]; } *lshpack_huff_tables; | ||
| +#define hencs (__atomic_load_n(&lshpack_huff_tables, __ATOMIC_RELAXED)->encs) | ||
| +#define hdecs (__atomic_load_n(&lshpack_huff_tables, __ATOMIC_RELAXED)->decs) | ||
| +extern void lshpack_huff_tables_init(void); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| +#endif | ||
| #include "huff-tables.h" | ||
|
|
||
| #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
| @@ -486,6 +496,10 @@ | ||
| void | ||
| lsqpack_enc_preinit (struct lsqpack_enc *enc, void *logger_ctx) | ||
| { | ||
| +#if LS_QPACK_USE_LARGE_TABLES && LS_HPACK_LAZY_LARGE_TABLES | ||
| + /* An encoder that is only preinitialized encodes with the tables too. */ | ||
| + lshpack_huff_tables_init(); | ||
| +#endif | ||
| memset(enc, 0, sizeof(*enc)); | ||
| STAILQ_INIT(&enc->qpe_all_entries); | ||
| STAILQ_INIT(&enc->qpe_hinfo_arrs); | ||
| @@ -2775,6 +2789,9 @@ | ||
| unsigned dyn_table_size, unsigned max_risked_streams, | ||
| const struct lsqpack_dec_hset_if *dh_if, enum lsqpack_dec_opts opts) | ||
| { | ||
| +#if LS_QPACK_USE_LARGE_TABLES && LS_HPACK_LAZY_LARGE_TABLES | ||
| + lshpack_huff_tables_init(); | ||
| +#endif | ||
| unsigned i; | ||
| memset(dec, 0, sizeof(*dec)); | ||
| dec->qpd_opts = opts; | ||
| --- a/huff-tables.h 2026-10-28 13:33:26.023031842 +0000 | ||
| +++ b/huff-tables.h 2026-10-28 13:32:45.863030374 +0000 | ||
| @@ -5141,7 +5141,7 @@ | ||
| }, | ||
| }; | ||
|
|
||
| -#if LS_QPACK_USE_LARGE_TABLES | ||
| +#if LS_QPACK_USE_LARGE_TABLES && !LS_HPACK_LAZY_LARGE_TABLES | ||
| #if __BYTE_ORDER == __LITTLE_ENDIAN | ||
| #define I(i,j) ((j<<8)|i) | ||
| #else | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
| // Where /sys/kernel/mm/transparent_hugepage/enabled is `always`, the kernel backs a stretch of anonymous memory that has | ||
| // the size and alignment of a huge page (2 MB) with one huge page, at the first write to it. Bun opts its heaps out | ||
| // (MADV_NOHUGEPAGE). It can't do that in time for the executable's zero-filled data (.bss): the kernel maps it, and the | ||
| // dynamic loader writes to it (it copies environ, stdin, stdout and stderr there) before any code of Bun runs. | ||
| // | ||
| // A mapping smaller than a huge page holds no such stretch, wherever the link puts it. At 2 MB or more it depends on the | ||
| // address the link gave .bss whether every process takes 2 MB more, so any change to Bun can switch that on, and | ||
| // test/aot/perf/start.test.ts fails for that build alone. | ||
| // | ||
| // When this fails, allocate a large table at its first use instead of declaring it zero-filled. | ||
| import { expect, test } from "bun:test"; | ||
| import { bunExe, isASAN, isDebug, isLinux, readElf64ProgramHeaders } from "harness"; | ||
|
|
||
| const smallestPage = 4096n; | ||
| const hugePage = 2 * 1024 * 1024; | ||
|
|
||
| // The anonymous mappings that the kernel makes for an ELF executable: what each writable PT_LOAD segment has in memory | ||
| // past the last page of its data in the file. | ||
| function sizesOfZeroFilledMappings(path: string) { | ||
| const [PT_LOAD, PF_W] = [1, 2]; | ||
| const pageUp = (address: bigint) => ((address + smallestPage - 1n) / smallestPage) * smallestPage; | ||
| return readElf64ProgramHeaders(path) | ||
| .filter(segment => segment.type === PT_LOAD && (segment.flags & PF_W) !== 0) | ||
| .map(segment => Number(pageUp(segment.vaddr + segment.memsz) - pageUp(segment.vaddr + segment.filesz))) | ||
| .filter(size => size > 0); | ||
| } | ||
|
|
||
| // A debug build and AddressSanitizer make .bss several times larger, and nobody measures their memory. | ||
| test.skipIf(!isLinux || isDebug || isASAN)( | ||
| "the executable's zero-filled data is smaller than a transparent huge page", | ||
| () => { | ||
| const sizes = sizesOfZeroFilledMappings(bunExe()); | ||
| expect(sizes.length).toBeGreaterThan(0); | ||
| expect(Math.max(...sizes)).toBeLessThan(hugePage); | ||
| }, | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.