Skip to content

Commit cb77f3e

Browse files
committed
Discover commands within shell arithmetic
a398822 masked arithmetic expressions without visiting their nested command substitutions. ARM's KBSS_SZ query therefore lost its nm input and returned zero for the decompressor's BSS size. Discover those commands while keeping arithmetic itself as argument data and preserving source positions. The compression regression now covers arithmetic wrapping and the transitive Image-to-vmlinux prerequisite path.
1 parent 9744c60 commit cb77f3e

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎internal/cmd/kconfig_parse/deferred_content_selection_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ func TestSelectedKbuildCompressionQueryFollowsGeneratedAssemblyPrerequisites(t *
2424
const directory = "arch/x86/boot/compressed"
2525
const compressed = directory + "/vmlinux.bin.lz4"
2626
write("Makefile", `
27-
all: vmlinux
27+
all: Image
2828
$(MAKE) -f $(srctree)/scripts/Makefile.build obj=arch/x86/boot/compressed arch/x86/boot/compressed/vmlinux
29+
Image: vmlinux
30+
cp $< $@
2931
vmlinux: input.bin
3032
cp $< $@
3133
`)
@@ -47,7 +49,7 @@ if-changed-cond = 1
4749
if_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
4850
real-prereqs = $(filter-out FORCE,$^)
4951
size_append = printf $(shell \
50-
dec_size=$$(wc -c vmlinux | awk '{print $$1}'); \
52+
dec_size=$$(( $$(wc -c vmlinux | awk '{print $$1}') )); \
5153
for F in $(real-prereqs); do \
5254
fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \
5355
dec_size=$$(expr $$dec_size + $$fsize); \

‎internal/kconfig/kbuild_rule_action_plan.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,8 +4593,8 @@ func compactKbuildRecipeTextHasPipeline(value string) bool {
45934593
// compactKbuildCompoundProgramCommands extracts the executable word of every
45944594
// simple command in shell text which cannot be lowered by
45954595
// parseCompactKbuildRecipe. The shell connectors are grammar, not policy: no
4596-
// executable name is assigned special behavior here. The returned commands are
4597-
// used solely to validate and materialize path-based programs before scriptrun
4596+
// executable name is assigned special behavior here. The returned commands
4597+
// retain source positions for program and input discovery before scriptrun
45984598
// executes the original text.
45994599
func compactKbuildCompoundProgramCommands(value string) ([]compactKbuildRecipeCommand, error) {
46004600
ranges, arithmetic, err := compactKbuildCommandSubstitutionRanges(value)
@@ -4994,6 +4994,18 @@ func compactKbuildCommandSubstitutionRanges(value string) ([]compactKbuildComman
49944994
return nil, nil, err
49954995
}
49964996
arithmetic = append(arithmetic, compactKbuildCommandSubstitutionRange{start: index, end: end})
4997+
// Arithmetic is argument data, but command substitutions inside it
4998+
// still execute. Preserve their source offsets for command discovery.
4999+
contentStart := index + 3
5000+
nested, _, err := compactKbuildCommandSubstitutionRanges(value[contentStart : end-2])
5001+
if err != nil {
5002+
return nil, nil, err
5003+
}
5004+
for _, substitution := range nested {
5005+
ranges = append(ranges, compactKbuildCommandSubstitutionRange{
5006+
start: contentStart + substitution.start, end: contentStart + substitution.end,
5007+
})
5008+
}
49975009
index = end - 1
49985010
continue
49995011
}

‎internal/kconfig/kbuild_rule_action_plan_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,16 +1046,20 @@ func TestCompoundProgramDiscoveryRejectsCommandSubstitutionInProgramHead(t *test
10461046
}
10471047

10481048
func TestCompoundProgramDiscoveryTreatsArithmeticExpansionAsArgumentData(t *testing.T) {
1049-
recipe := "outer pre$((1 + (2 * 3)))post argument; next done"
1049+
recipe := "outer pre$((1 + (2 * 3)))post $(( $(inner input) + $(( $(other value) )) )); next done"
10501050
commands, err := compactKbuildCompoundProgramCommands(recipe)
10511051
if err != nil {
10521052
t.Fatal(err)
10531053
}
10541054
programs := make([]string, len(commands))
10551055
for index, command := range commands {
10561056
programs[index] = command.program
1057+
if command.programStart < 0 || command.programEnd > len(recipe) ||
1058+
recipe[command.programStart:command.programEnd] != command.program {
1059+
t.Fatalf("command %d has invalid head provenance %#v in %q", index, command, recipe)
1060+
}
10571061
}
1058-
if want := []string{"outer", "next"}; !slices.Equal(programs, want) {
1062+
if want := []string{"outer", "inner", "other", "next"}; !slices.Equal(programs, want) {
10591063
t.Fatalf("arithmetic expansion programs=%q, want %q", programs, want)
10601064
}
10611065
}

0 commit comments

Comments
 (0)