diff --git a/classparser/jdk_constructor_code_generate_test.go b/classparser/jdk_constructor_code_generate_test.go index b6381d8f..1cfe428e 100644 --- a/classparser/jdk_constructor_code_generate_test.go +++ b/classparser/jdk_constructor_code_generate_test.go @@ -50,7 +50,7 @@ func TestGenerateJDKConstructorCodeArchive(t *testing.T) { Classes map[string]json.RawMessage } } - if err = json.Unmarshal(jdkInvocationCatalogJSON, &declared); err != nil { + if err = decodeJDKInvocationCatalog(&declared); err != nil { t.Fatal(err) } expected := map[int]string{} diff --git a/classparser/jdk_constructor_code_test.go b/classparser/jdk_constructor_code_test.go index 1e9078e1..d15b631f 100644 --- a/classparser/jdk_constructor_code_test.go +++ b/classparser/jdk_constructor_code_test.go @@ -2,7 +2,6 @@ package javaclassparser import ( "crypto/sha256" - "encoding/json" "fmt" "sort" "testing" @@ -26,7 +25,7 @@ func TestAdversarialPlatformInterfaceDeclarationsUsePinnedOriginalFlags(t *testi } } } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &catalog); err != nil { + if err := decodeJDKInvocationCatalog(&catalog); err != nil { t.Fatal(err) } for _, profile := range catalog.Profiles { @@ -73,7 +72,7 @@ func TestAdversarialPlatformConstructorCodeIdentityAndEffectProof(t *testing.T) References map[string]struct{ Archive, SHA256 string } `json:"reference_provenance"` } } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &declared); err != nil { + if err := decodeJDKInvocationCatalog(&declared); err != nil { t.Fatal(err) } for _, profile := range declared.Profiles { diff --git a/classparser/jdk_metadata_catalog.go b/classparser/jdk_metadata_catalog.go index b52bf8c0..aa12568e 100644 --- a/classparser/jdk_metadata_catalog.go +++ b/classparser/jdk_metadata_catalog.go @@ -1,8 +1,6 @@ package javaclassparser import ( - _ "embed" - "encoding/json" "sync" "github.com/yaklang/javajive/classparser/decompiler/core/callbinding" @@ -11,10 +9,6 @@ import ( // These are full method tables extracted from specific trusted JDK8/9/11/16/17/21 // classfiles, not guessed overload-name uniqueness rules. See the adjacent // provenance document and generator. No installed JDK is consulted at runtime. -// -//go:embed jdk_metadata_catalog.json -var jdkInvocationCatalogJSON []byte - type jdkCatalogProvenance struct { Archive string `json:"archive"` SHA256 string `json:"sha256"` @@ -48,7 +42,7 @@ func loadJDKInvocationCatalog() { ReferenceParents map[string][]string `json:"reference_hierarchy"` } `json:"profiles"` } - if json.Unmarshal(jdkInvocationCatalogJSON, &document) != nil || document.Schema != 3 { + if decodeJDKInvocationCatalog(&document) != nil || document.Schema != 3 { return } profiles := make(map[int]map[string]callbinding.Class) diff --git a/classparser/jdk_metadata_catalog.json.zst b/classparser/jdk_metadata_catalog.json.zst new file mode 100644 index 00000000..bed397fb Binary files /dev/null and b/classparser/jdk_metadata_catalog.json.zst differ diff --git a/classparser/jdk_metadata_catalog.md b/classparser/jdk_metadata_catalog.md index bba18449..fb0ed547 100644 --- a/classparser/jdk_metadata_catalog.md +++ b/classparser/jdk_metadata_catalog.md @@ -90,8 +90,16 @@ python3 classparser/jdk_metadata_catalog_generate.py \ --jdk8 /path/to/jdk8 --jdk9 /path/to/jdk9 --jdk11 /path/to/jdk11 \ --jdk16 /path/to/jdk16 --jdk17 /path/to/jdk17 --jdk21 /path/to/jdk21 \ --out classparser/jdk_metadata_catalog.json +go generate ./classparser ``` +The reviewed JSON remains the source of truth. Applications embed only +`jdk_metadata_catalog.json.zst`; the catalog is decompressed on its first +required lookup, within the existing one-time loader. The constructor classfile +ZIP and all exact-profile/provenance checks remain unchanged. The required +corpus tests compare decompressed bytes with the JSON source so regeneration +cannot silently omit a catalog update. + ## Evidence and limits Before this change, the unchanged `TestT22_C06_noRegression` rejected LongTest diff --git a/classparser/jdk_metadata_catalog_data.go b/classparser/jdk_metadata_catalog_data.go new file mode 100644 index 00000000..33929bb4 --- /dev/null +++ b/classparser/jdk_metadata_catalog_data.go @@ -0,0 +1,31 @@ +package javaclassparser + +import ( + _ "embed" + "encoding/json" + + "github.com/klauspost/compress/zstd" +) + +// Keep the reviewed JSON source for regeneration and provenance checks. Only +// its compressed representation is linked into applications. +// +//go:generate go run ../tools/compressjdk -input jdk_metadata_catalog.json +//go:embed jdk_metadata_catalog.json.zst +var jdkInvocationCatalogZstd []byte + +// The production caller runs this inside loadJDKInvocationCatalog's sync.Once. +// Neither package initialization nor unrelated class lookups decompress the +// catalog, and the temporary JSON buffer is released after parsing. +func decodeJDKInvocationCatalog(target any) error { + decoder, err := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1), zstd.WithDecoderLowmem(true)) + if err != nil { + return err + } + defer decoder.Close() + raw, err := decoder.DecodeAll(jdkInvocationCatalogZstd, nil) + if err != nil { + return err + } + return json.Unmarshal(raw, target) +} diff --git a/classparser/jdk_metadata_catalog_data_test.go b/classparser/jdk_metadata_catalog_data_test.go new file mode 100644 index 00000000..13a0020e --- /dev/null +++ b/classparser/jdk_metadata_catalog_data_test.go @@ -0,0 +1,28 @@ +package javaclassparser + +import ( + "bytes" + "os" + "testing" + + "github.com/klauspost/compress/zstd" +) + +func TestJDKCompressedCatalogMatchesSource(t *testing.T) { + want, err := os.ReadFile("jdk_metadata_catalog.json") + if err != nil { + t.Fatal(err) + } + decoder, err := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1)) + if err != nil { + t.Fatal(err) + } + defer decoder.Close() + got, err := decoder.DecodeAll(jdkInvocationCatalogZstd, nil) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatal("embedded JDK catalog differs from its reviewed source; run go generate ./classparser") + } +} diff --git a/classparser/jdk_metadata_catalog_test.go b/classparser/jdk_metadata_catalog_test.go index b44f0560..1ff7d199 100644 --- a/classparser/jdk_metadata_catalog_test.go +++ b/classparser/jdk_metadata_catalog_test.go @@ -2,7 +2,6 @@ package javaclassparser import ( "encoding/hex" - "encoding/json" "reflect" "sync" "testing" @@ -40,7 +39,7 @@ func TestJDKInvocationCatalogCompleteProfiles(t *testing.T) { } `json:"provenance"` } `json:"profiles"` } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &document); err != nil { + if err := decodeJDKInvocationCatalog(&document); err != nil { t.Fatal(err) } if len(document.Profiles) != 6 { @@ -177,7 +176,7 @@ func TestJDKCatalogModularNamespaceAndCheckedDeclarations(t *testing.T) { } `json:"provenance"` } `json:"profiles"` } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &document); err != nil { + if err := decodeJDKInvocationCatalog(&document); err != nil { t.Fatal(err) } for _, profile := range document.Profiles { @@ -407,7 +406,7 @@ func TestInvocationMetadataReferenceCatalogRequiresOriginalClosedProfilesAndNeve } `json:"reference_provenance"` } `json:"profiles"` } - if e := json.Unmarshal(jdkInvocationCatalogJSON, &document); e != nil { + if e := decodeJDKInvocationCatalog(&document); e != nil { t.Fatal(e) } if document.Schema != 3 || len(document.Profiles) != 6 { diff --git a/classparser/jdk_throwable_hierarchy_test.go b/classparser/jdk_throwable_hierarchy_test.go index 7839a5d4..d441700a 100644 --- a/classparser/jdk_throwable_hierarchy_test.go +++ b/classparser/jdk_throwable_hierarchy_test.go @@ -2,7 +2,6 @@ package javaclassparser import ( "encoding/hex" - "encoding/json" "github.com/yaklang/javajive/classparser/decompiler/core/callbinding" "testing" ) @@ -18,7 +17,7 @@ func TestJDKThrowableAncestryKeepsClassificationSeparateFromMembers(t *testing.T } `json:"throwable_provenance"` } `json:"profiles"` } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &document); err != nil { + if err := decodeJDKInvocationCatalog(&document); err != nil { t.Fatal(err) } for _, profile := range document.Profiles { diff --git a/classparser/native_platform_indexed_code_test.go b/classparser/native_platform_indexed_code_test.go index d2086223..a9571a29 100644 --- a/classparser/native_platform_indexed_code_test.go +++ b/classparser/native_platform_indexed_code_test.go @@ -2,7 +2,6 @@ package javaclassparser import ( "crypto/sha256" - "encoding/json" "fmt" "testing" ) @@ -17,7 +16,7 @@ func TestAdversarialPlatformIndexedDeclarationsRetainOriginalBytecode(t *testing Provenance map[string]struct{ Archive, SHA256 string } } } - if err := json.Unmarshal(jdkInvocationCatalogJSON, &catalog); err != nil { + if err := decodeJDKInvocationCatalog(&catalog); err != nil { t.Fatal(err) } for _, profile := range catalog.Profiles { diff --git a/go.mod b/go.mod index 3dffedbe..0f0f386f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.0 require ( github.com/go-viper/mapstructure/v2 v2.4.0 github.com/gobwas/glob v0.2.3 + github.com/klauspost/compress v1.17.9 github.com/samber/lo v1.38.1 github.com/segmentio/ksuid v1.0.4 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index 0db335a9..3d18aaab 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9L github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= diff --git a/javajive.go b/javajive.go index ab2def9c..430549a1 100644 --- a/javajive.go +++ b/javajive.go @@ -38,7 +38,7 @@ import ( ) // Version is the javajive library/CLI version. -const Version = "0.7.0" +const Version = "0.8.0" // --------------------------------------------------------------------------- // Re-exported types diff --git a/tools/compressjdk/main.go b/tools/compressjdk/main.go new file mode 100644 index 00000000..b9b2d548 --- /dev/null +++ b/tools/compressjdk/main.go @@ -0,0 +1,42 @@ +// Command compressjdk deterministically compresses the reviewed JDK catalog. +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/klauspost/compress/zstd" +) + +func main() { + input := flag.String("input", "", "reviewed JDK catalog JSON; output is .zst") + flag.Parse() + if *input == "" || flag.NArg() != 0 { + fmt.Fprintln(os.Stderr, "usage: compressjdk -input jdk_metadata_catalog.json") + os.Exit(2) + } + if err := compress(*input); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func compress(input string) error { + raw, err := os.ReadFile(input) + if err != nil { + return err + } + encoder, err := zstd.NewWriter(nil, + zstd.WithEncoderLevel(zstd.SpeedBestCompression), + zstd.WithEncoderConcurrency(1), + zstd.WithWindowSize(8<<20), + zstd.WithEncoderCRC(true), + ) + if err != nil { + return err + } + defer encoder.Close() + compressed := encoder.EncodeAll(raw, nil) + return os.WriteFile(input+".zst", compressed, 0644) +} diff --git a/tools/requiredci/main.go b/tools/requiredci/main.go index dd5435f7..74e25ec5 100644 --- a/tools/requiredci/main.go +++ b/tools/requiredci/main.go @@ -38,7 +38,7 @@ var requiredSuites = []suite{ {Group: "corpus", Package: "./classparser/jarwar", Tests: 18, TestHash: "b1195066bc29f6a35391695fb815b4eef9eb42a55ef53f7ba9786375a27afc25"}, {Group: "corpus", Package: "./classparser/decompiler/jar", Tests: 1, TestHash: "e84c19ac824483c172a763d735732079fb808d6dd0153c53e573b18513d23553"}, {Group: "corpus", Package: "./jimage", Tests: 16, TestHash: "f68b01a6c2a476c2a5921fc30f5ab3a6c0b700fe04748ac1e1d5d9573e283df8", Exclude: []string{"TestJLinkBigEndianImage", "TestJLinkCompactCPImage", "TestOpenJDKImage"}}, - {Group: "corpus", Package: "./classparser", Tests: 4176, TestHash: "1920a1d4ab6a93c33d503eaa29b825151063d1d728b63ef7bdaf687a53802c92", Include: []string{"TestDumpMethodsPointerFastPathKeepsDescriptor", "TestJoinRuleChainExactOutput", "TestSourceRewritePrecisionOuterGateSemantics", "TestRewriteSchedulerOrderMatchesDumperCallSites", "TestRewriteSchedulerPositiveAndNearMissExactOutput", "TestRewriteSchedulerPreservesRecordsDiagnosticsAndHashes", "TestRewriteSchedulerDecompileEquivalence", "TestJDKObjectMetadataMatchesCatalog", "TestAdversarialPlatformConstructorCodeIdentityAndEffectProof", "TestLoopAnalysisCacheMatchesUncachedDecompile", "FuzzDecompileWithLimitsNeverPanics"}}, + {Group: "corpus", Package: "./classparser", Tests: 4177, TestHash: "7a0f8f15a3694fc97e99ed38d37f3f621481161e23ea97755762cedf09a847bc", Include: []string{"TestDumpMethodsPointerFastPathKeepsDescriptor", "TestJoinRuleChainExactOutput", "TestSourceRewritePrecisionOuterGateSemantics", "TestRewriteSchedulerOrderMatchesDumperCallSites", "TestRewriteSchedulerPositiveAndNearMissExactOutput", "TestRewriteSchedulerPreservesRecordsDiagnosticsAndHashes", "TestRewriteSchedulerDecompileEquivalence", "TestJDKObjectMetadataMatchesCatalog", "TestJDKCompressedCatalogMatchesSource", "TestAdversarialPlatformConstructorCodeIdentityAndEffectProof", "TestLoopAnalysisCacheMatchesUncachedDecompile", "FuzzDecompileWithLimitsNeverPanics"}}, {Group: "algorithm", Package: "./classparser/decompiler/core", Tests: 413, TestHash: "cf788257ac2b6de14e9ba85f18a6fd70fcdc9f7f31d85b6538bb8b1344fff93b"}, {Group: "algorithm", Package: "./classparser/decompiler/core/callbinding", Tests: 17, TestHash: "e7eafd33ec36c2219f362b5c45872f126dde5a81dab84d931023fd1abc1836cc"},