Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
cache: true
- run: go run ./tools/requiredci -check
- run: CGO_ENABLED=0 go build ./...
- run: CGO_ENABLED=0 go build -tags javajive_full_jdk ./...
- run: go run ./tools/requiredci -run contract

corpus:
Expand All @@ -44,6 +45,7 @@ jobs:
go-version-file: go.mod
cache: true
- run: go run ./tools/requiredci -run corpus
- run: go test -tags javajive_full_jdk ./classparser -run '^(TestJDKCompressedCatalogMatchesSource|TestJDKCompressedConstructorCodeMatchesSource|TestAdversarialPlatformConstructorCodeIdentityAndEffectProof|TestAdversarialPlatformIndexedDeclarationsRetainOriginalBytecode)$'

algorithm:
name: Go algorithm regression
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ go build -o javajive ./cmd/javajive

Requires Go 1.22+.

### Build tags (`javajive_full_jdk`)

By default, JavaJive embeds only the compact JDK invocation/hierarchy metadata
catalog (`classparser/jdk_metadata_catalog.json.zst`, ~132 KiB, covering JDK
8/9/11/16/17/21 method tables, generic signatures, exceptions, throwable
hierarchy, and reference supertypes) and **omits** the 7.7 MiB full JDK
constructor bytecode bundle (`jdk_constructor_code.bundle.zst`). This saves
~8.4 MiB of embedded binary/zip size in downstream consumers such as `yaklang`
while preserving full readability, type inference, and SSA/static-analysis
quality.

If you need strict offline `javac` round-trip constructor-capture and
annotation-retention proofs without supplying an external JDK declaration
resolver, opt in with the `javajive_full_jdk` build tag:

```bash
go build -tags javajive_full_jdk -o javajive ./cmd/javajive
go test -tags javajive_full_jdk ./classparser
```

Callers can inspect `javajive.HasEmbeddedJDKConstructorBundle()` (or
`javaclassparser.HasEmbeddedJDKConstructorBundle()`) at runtime. See
[classparser/jdk_metadata_catalog.md](classparser/jdk_metadata_catalog.md) for
details.

## CLI

```text
Expand Down
13 changes: 13 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ go build -o javajive ./cmd/javajive

需要 Go 1.22+。

### 构建标签(`javajive_full_jdk`)

默认构建下,JavaJive 仅内嵌精简后的 JDK 元数据目录(`classparser/jdk_metadata_catalog.json.zst`,约 132 KiB,完整保留 JDK 8/9/11/16/17/21 的核心方法表、泛型签名、异常表、Throwable 继承树与引用类型继承树),**默认不内嵌** 7.7 MiB 的完整 JDK 字节码包(`jdk_constructor_code.bundle.zst`)。这可以为 `yaklang` 等下游集成方直接节省约 8.4 MiB 的二进制与压缩包体积,同时保持反编译可读性、类型推导与 SSA 静态分析质量。

如果需要在无外部 JDK 声明解析器的离线场景下启用严格的 `javac` 无损重编译构造捕获与注解保留证明,可通过 `javajive_full_jdk` 构建标签开启完整内嵌包:

```bash
go build -tags javajive_full_jdk -o javajive ./cmd/javajive
go test -tags javajive_full_jdk ./classparser
```

运行时可通过 `javajive.HasEmbeddedJDKConstructorBundle()`(或 `javaclassparser.HasEmbeddedJDKConstructorBundle()`)检查当前二进制是否包含完整 JDK 字节码包。更多细节见 [classparser/jdk_metadata_catalog.md](classparser/jdk_metadata_catalog.md)。

## 命令行

```text
Expand Down
217 changes: 13 additions & 204 deletions classparser/jdk_constructor_code.go
Original file line number Diff line number Diff line change
@@ -1,94 +1,33 @@
package javaclassparser

import (
"crypto/sha256"
_ "embed"
"encoding/binary"
"encoding/hex"
"encoding/json"
"strings"
"sync"

"github.com/klauspost/compress/zstd"
)

const (
constructorBundleMagic = "JJCC0001"
constructorBundleMaxManifest = 4 << 20
constructorBundleMaxChunkRaw = 4 << 20
constructorBundleMaxClassSize = 2 << 20
)
import "strings"

type constructorCodeProfile struct {
Release int `json:"release"`
ArchiveSHA256 string `json:"archive_sha256"`
Classes map[string]string `json:"classes"`
}

type constructorCodeDocument struct {
Schema int `json:"schema"`
Profiles []constructorCodeProfile `json:"profiles"`
}

type constructorBundleEntry struct {
SHA256 string `json:"s"`
Chunk int `json:"c"`
Offset uint32 `json:"o"`
Length uint32 `json:"l"`
}

type constructorBundleProfile struct {
Release int `json:"release"`
ArchiveSHA256 string `json:"archive_sha256"`
Classes map[string]constructorBundleEntry `json:"classes"`
}

type constructorBundleIndex struct {
Schema int `json:"schema"`
Profiles []constructorBundleProfile `json:"profiles"`
}

type constructorChunkMeta struct {
compOffset uint32
compLen uint32
rawLen uint32
once sync.Once
raw []byte
ok bool
}

// Original classfiles, extracted from the exact platform archives pinned by
// the declaration catalog. They are read only as metadata/bytecode: never
// loaded by a JVM or used as emitted archive-owned classes. Selection by the
// offline generator is an index optimization; the request must still prove
// the original constructor's effects, field identity and all paths. Public
// platform annotation declarations also retain their original Retention/Target
// attributes; their presence is evidence, not permission to rewrite a use.
// Indexed declarations from each pinned primary archive retain their original
// code and ancestry. Interfaces retain abstract/default/static declarations for
// functional-target ownership proofs; class membership grants no effect permission.
// Public extensible APIs with complete original ancestry are retained even
// when their constructors observe THIS. Independent declaration/reference
// provenance pins every indexed class; code membership is never an effect proof.
// HasEmbeddedJDKConstructorBundle reports whether this binary was built with
// `-tags javajive_full_jdk`, which embeds the 7.7 MiB chunked-zstd corpus of
// original JDK classfiles (jdk_constructor_code.bundle.zst) for strict javac
// constructor-capture and annotation-retention round-trip proofs.
//
// Keep the reviewed ZIP source for regeneration and provenance checks. Only its
// deterministic chunked-zstd representation is linked into applications.
//
//go:generate go run ../tools/compressjdk -constructor-zip jdk_constructor_code.zip
//go:embed jdk_constructor_code.bundle.zst
var jdkConstructorCodeBundle []byte

var jdkConstructorCode struct {
once sync.Once
chunks []constructorChunkMeta
chunkData []byte
entries map[int]map[string]constructorBundleEntry
hashes map[int]map[string]string
// Default builds omit this 7.7 MiB bundle and retain only the compact (~132 KiB)
// JDK invocation/hierarchy metadata catalog (jdk_metadata_catalog.json.zst).
func HasEmbeddedJDKConstructorBundle() bool {
return embeddedJDKConstructorBundleEnabled
}

// jdkConstructorNameCandidate is a conservative namespace gate over the
// embedded constructor corpus. A same-package identity probe for an application
// class (for example com/example/String) can never resolve here, and must not
// pay the one-time catalog + bundle index cost.
// embedded JDK metadata and constructor corpus. A same-package identity probe
// for an application class (for example com/example/String) can never resolve
// here, and must not pay the one-time catalog + bundle index cost.
func jdkConstructorNameCandidate(name string) bool {
switch {
case strings.HasPrefix(name, "java/"), strings.HasPrefix(name, "javax/"),
Expand All @@ -100,133 +39,3 @@ func jdkConstructorNameCandidate(name string) bool {
}
return false
}

func decodeConstructorZstd(src []byte, maxDecoded int) ([]byte, error) {
decoder, err := zstd.NewReader(nil,
zstd.WithDecoderConcurrency(1),
zstd.WithDecoderLowmem(true),
zstd.WithDecoderMaxMemory(uint64(maxDecoded)*2),
)
if err != nil {
return nil, err
}
defer decoder.Close()
raw, err := decoder.DecodeAll(src, nil)
if err != nil || len(raw) > maxDecoded {
return nil, err
}
return raw, nil
}

func jdkConstructorChunkBytes(chunkIdx int) ([]byte, bool) {
if chunkIdx < 0 || chunkIdx >= len(jdkConstructorCode.chunks) {
return nil, false
}
chunk := &jdkConstructorCode.chunks[chunkIdx]
chunk.once.Do(func() {
start := uint64(chunk.compOffset)
end := start + uint64(chunk.compLen)
if end > uint64(len(jdkConstructorCode.chunkData)) || chunk.rawLen > constructorBundleMaxChunkRaw {
return
}
raw, err := decodeConstructorZstd(jdkConstructorCode.chunkData[start:end], int(chunk.rawLen))
if err != nil || uint32(len(raw)) != chunk.rawLen {
return
}
chunk.raw = raw
chunk.ok = true
})
return chunk.raw, chunk.ok
}

func jdkConstructorClassBytes(name string, release int) ([]byte, bool) {
if !jdkConstructorNameCandidate(name) {
return nil, false
}
jdkConstructorCode.once.Do(func() {
buf := jdkConstructorCodeBundle
if len(buf) < 16 || string(buf[:8]) != constructorBundleMagic {
return
}
manifestCompLen := binary.LittleEndian.Uint32(buf[8:12])
chunkCount := binary.LittleEndian.Uint32(buf[12:16])
if manifestCompLen == 0 || chunkCount == 0 || chunkCount > 4096 {
return
}
tableEnd := uint64(16) + uint64(chunkCount)*12
manifestEnd := tableEnd + uint64(manifestCompLen)
if manifestEnd > uint64(len(buf)) {
return
}
chunks := make([]constructorChunkMeta, chunkCount)
chunkData := buf[manifestEnd:]
for i := uint32(0); i < chunkCount; i++ {
off := 16 + int(i)*12
compOffset := binary.LittleEndian.Uint32(buf[off : off+4])
compLen := binary.LittleEndian.Uint32(buf[off+4 : off+8])
rawLen := binary.LittleEndian.Uint32(buf[off+8 : off+12])
if uint64(compOffset)+uint64(compLen) > uint64(len(chunkData)) || rawLen > constructorBundleMaxChunkRaw {
return
}
chunks[i].compOffset = compOffset
chunks[i].compLen = compLen
chunks[i].rawLen = rawLen
}
manifestRaw, err := decodeConstructorZstd(buf[tableEnd:manifestEnd], constructorBundleMaxManifest)
if err != nil || len(manifestRaw) == 0 {
return
}
var idx constructorBundleIndex
if json.Unmarshal(manifestRaw, &idx) != nil || idx.Schema != 1 || len(idx.Profiles) == 0 {
return
}
entries := make(map[int]map[string]constructorBundleEntry, len(idx.Profiles))
hashes := make(map[int]map[string]string, len(idx.Profiles))
for _, profile := range idx.Profiles {
if entries[profile.Release] != nil || len(profile.ArchiveSHA256) != 64 || len(profile.Classes) == 0 {
return
}
table := make(map[string]constructorBundleEntry, len(profile.Classes))
hashTable := make(map[string]string, len(profile.Classes))
for className, entry := range profile.Classes {
digest, err := hex.DecodeString(entry.SHA256)
if err != nil || len(digest) != 32 {
return
}
if entry.Chunk < 0 || entry.Chunk >= len(chunks) || entry.Length > constructorBundleMaxClassSize {
return
}
if uint64(entry.Offset)+uint64(entry.Length) > uint64(chunks[entry.Chunk].rawLen) {
return
}
table[className] = entry
hashTable[className] = entry.SHA256
}
entries[profile.Release] = table
hashes[profile.Release] = hashTable
}
jdkConstructorCode.chunks = chunks
jdkConstructorCode.chunkData = chunkData
jdkConstructorCode.entries = entries
jdkConstructorCode.hashes = hashes
})
entry, ok := jdkConstructorCode.entries[release][name]
if !ok {
return nil, false
}
chunk, ok := jdkConstructorChunkBytes(entry.Chunk)
if !ok {
return nil, false
}
end := uint64(entry.Offset) + uint64(entry.Length)
if end > uint64(len(chunk)) {
return nil, false
}
raw := make([]byte, entry.Length)
copy(raw, chunk[entry.Offset:end])
digest := sha256.Sum256(raw)
if hex.EncodeToString(digest[:]) != jdkConstructorCode.hashes[release][name] {
return nil, false
}
return raw, true
}
2 changes: 2 additions & 0 deletions classparser/jdk_constructor_code_data_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build javajive_full_jdk

package javaclassparser

import (
Expand Down
Loading
Loading