Skip to content

Commit e1f63cb

Browse files
committed
pkg/aflow: infer the target architecture instead of hard-coding it
Since we have the target kernel config, we can technically decide if it's amd64 or arm64 or what not based on the presence of certain kernel config options. This introduces a generic helper to help figure that out and uses it for both build and reproduction.
1 parent 58e8c8f commit e1f63cb

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

pkg/aflow/action/crash/reproduce.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ func reproduce(ctx *aflow.Context, args reproduceArgs) (reproduceResult, error)
6262
if err := json.Unmarshal(args.VM, &vmConfig); err != nil {
6363
return fmt.Errorf("failed to parse VM config: %w", err)
6464
}
65-
vmConfig["kernel"] = filepath.Join(args.KernelObj, filepath.FromSlash(build.LinuxKernelImage(targets.AMD64)))
65+
arch, err := targets.ArchFromLinuxConfig(args.KernelConfig)
66+
if err != nil {
67+
return err
68+
}
69+
vmConfig["kernel"] = filepath.Join(args.KernelObj, filepath.FromSlash(build.LinuxKernelImage(arch)))
6670
vmCfg, err := json.Marshal(vmConfig)
6771
if err != nil {
6872
return fmt.Errorf("failed to serialize VM config: %w", err)
6973
}
7074
cfg := mgrconfig.DefaultValues()
71-
cfg.RawTarget = "linux/amd64"
75+
cfg.RawTarget = "linux/" + arch
7276
cfg.Workdir = filepath.Join(dir, "workdir")
7377
cfg.Syzkaller = args.Syzkaller
7478
cfg.KernelObj = args.KernelObj

pkg/aflow/action/kernel/build.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ func buildKernel(ctx *aflow.Context, args buildArgs) (buildResult, error) {
4040
if err := osutil.WriteFile(filepath.Join(dir, ".config"), []byte(args.KernelConfig)); err != nil {
4141
return err
4242
}
43-
target := targets.List[targets.Linux][targets.AMD64]
44-
image := filepath.FromSlash(build.LinuxKernelImage(targets.AMD64))
43+
arch, err := targets.ArchFromLinuxConfig(args.KernelConfig)
44+
if err != nil {
45+
return err
46+
}
47+
target := targets.List[targets.Linux][arch]
48+
image := filepath.FromSlash(build.LinuxKernelImage(arch))
4549
makeArgs := build.LinuxMakeArgs(target, targets.DefaultLLVMCompiler, targets.DefaultLLVMLinker,
4650
"ccache", dir, runtime.NumCPU())
4751
const compileCommands = "compile_commands.json"

sys/targets/targets.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Target struct {
4343
SyscallTrampolines map[string]string
4444
Addr2Line func() (string, error)
4545
KernelAddresses KernelAddresses
46+
KernelConfigs []string
4647

4748
init *sync.Once
4849
initOther *sync.Once
@@ -281,6 +282,7 @@ var List = map[string]map[string]*Target{
281282
DataStart: 0xffff880000000000,
282283
DataEnd: 0xffff890000000000,
283284
},
285+
KernelConfigs: []string{"CONFIG_X86_64"},
284286
},
285287
I386: {
286288
VMArch: AMD64,
@@ -291,13 +293,15 @@ var List = map[string]map[string]*Target{
291293
Triple: "x86_64-linux-gnu",
292294
KernelArch: "i386",
293295
KernelHeaderArch: "x86",
296+
KernelConfigs: []string{"CONFIG_X86_32"},
294297
},
295298
ARM64: {
296299
PtrSize: 8,
297300
PageSize: 4 << 10,
298301
Triple: "aarch64-linux-gnu",
299302
KernelArch: "arm64",
300303
KernelHeaderArch: "arm64",
304+
KernelConfigs: []string{"CONFIG_ARM64"},
301305
},
302306
ARM: {
303307
VMArch: ARM64,
@@ -306,6 +310,7 @@ var List = map[string]map[string]*Target{
306310
Triple: "arm-linux-gnueabi",
307311
KernelArch: "arm",
308312
KernelHeaderArch: "arm",
313+
KernelConfigs: []string{"CONFIG_ARM"},
309314
},
310315
MIPS64LE: {
311316
PtrSize: 8,
@@ -314,6 +319,7 @@ var List = map[string]map[string]*Target{
314319
Triple: "mips64el-linux-gnuabi64",
315320
KernelArch: "mips",
316321
KernelHeaderArch: "mips",
322+
KernelConfigs: []string{"CONFIG_MIPS", "CONFIG_64BIT", "CONFIG_CPU_LITTLE_ENDIAN"},
317323
},
318324
PPC64LE: {
319325
PtrSize: 8,
@@ -322,6 +328,7 @@ var List = map[string]map[string]*Target{
322328
Triple: "powerpc64le-linux-gnu",
323329
KernelArch: "powerpc",
324330
KernelHeaderArch: "powerpc",
331+
KernelConfigs: []string{"CONFIG_PPC64", "CONFIG_CPU_LITTLE_ENDIAN"},
325332
},
326333
S390x: {
327334
PtrSize: 8,
@@ -339,13 +346,15 @@ var List = map[string]map[string]*Target{
339346
// To work around this problem we therefore reroute the mmap syscall to the glibc mmap wrapper.
340347
"mmap": "mmap",
341348
},
349+
KernelConfigs: []string{"CONFIG_S390", "CONFIG_64BIT"},
342350
},
343351
RiscV64: {
344352
PtrSize: 8,
345353
PageSize: 4 << 10,
346354
Triple: "riscv64-linux-gnu",
347355
KernelArch: "riscv",
348356
KernelHeaderArch: "riscv",
357+
KernelConfigs: []string{"CONFIG_RISCV", "CONFIG_64BIT"},
349358
},
350359
},
351360
FreeBSD: {
@@ -1124,3 +1133,22 @@ int main() { printf("Hello, World!\n"); }
11241133
int main() { std::vector<int> v(10); }
11251134
`
11261135
)
1136+
1137+
func ArchFromLinuxConfig(cfg string) (string, error) {
1138+
for arch, target := range List[Linux] {
1139+
if len(target.KernelConfigs) == 0 {
1140+
continue
1141+
}
1142+
match := true
1143+
for _, conf := range target.KernelConfigs {
1144+
if !strings.Contains(cfg, conf+"=y") {
1145+
match = false
1146+
break
1147+
}
1148+
}
1149+
if match {
1150+
return arch, nil
1151+
}
1152+
}
1153+
return "", fmt.Errorf("can't determine architecture from config")
1154+
}

0 commit comments

Comments
 (0)