Skip to content
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

Generalize and extend unit tests working with binary files #2372

Merged
merged 3 commits into from
Dec 3, 2024
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
4 changes: 2 additions & 2 deletions libpkg/binfmt_macho.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ read_min_version(const int fd, const bool swap, const uint32_t loadcmd,
return n;
}

static ssize_t
ssize_t
read_path(const int fd, const bool swap, const uint32_t loadcmdsize,
char **dest)
{
Expand All @@ -235,7 +235,7 @@ read_path(const int fd, const bool swap, const uint32_t loadcmdsize,
return n;
}

static ssize_t
ssize_t
read_dylib(const int fd, const bool swap, const uint32_t loadcmdsize,
dylib_t **dest)
{
Expand Down
2 changes: 1 addition & 1 deletion libpkg/elfhints.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ int
shlib_list_from_elf_hints(const char *hintsfile)
{
if (ctx.oi->ostype == OS_FREEBSD || ctx.oi->ostype == OS_DRAGONFLY)
read_elf_hints(hintsfile, 1);
read_elf_hints(hintsfile, false);

return (scan_dirs_for_shlibs(&shlibs, ndirs, dirs, true));
}
Expand Down
18 changes: 14 additions & 4 deletions libpkg/pkg_abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pkg_get_myarch_fromfile(struct os_info *oi)
ret = pkg_get_myarch_macho(fd, oi);
if (EPKG_OK != ret) {
pkg_emit_error(
"Unable to determine the ABI, %s cannot be parsed.",
"Unable to determine ABI, %s cannot be parsed.",
work_abi_file);
ret = EPKG_FATAL;
}
Expand Down Expand Up @@ -227,10 +227,20 @@ pkg_analyse_files(struct pkgdb *db __unused, struct pkg *pkg, const char *stage)
const char *lib;
bool failures = false;

int (*pkg_analyse_init)(const char *stage) = pkg_analyse_init_elf;
int (*pkg_analyse_init)(const char *stage);
int (*pkg_analyse)(const bool developer_mode, struct pkg *pkg,
const char *fpath) = pkg_analyse_elf;
int (*pkg_analyse_close)() = pkg_analyse_close_elf;
const char *fpath);
int (*pkg_analyse_close)();

if (0 == strncmp(pkg->abi, "Darwin", 6)) {
pkg_analyse_init=pkg_analyse_init_macho;
pkg_analyse=pkg_analyse_macho;
pkg_analyse_close=pkg_analyse_close_macho;
} else {
pkg_analyse_init=pkg_analyse_init_elf;
pkg_analyse=pkg_analyse_elf;
pkg_analyse_close=pkg_analyse_close_elf;
}

if (tll_length(pkg->shlibs_required) != 0) {
tll_free_and_free(pkg->shlibs_required, free);
Expand Down
187 changes: 172 additions & 15 deletions libpkg/pkg_abi_macho.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ static const fat_arch_t *
match_entry(macho_file_t *mf, struct os_info *oi)
{
const fat_arch_t *p = mf->arch;
// we can change the content of oi->abi freely
char *abisep = oi->abi;
char *abihint = xstrdup(oi->abi);
char *abisep = abihint;
/*const char *osname = */strsep(&abisep, ":");
/*const char *version_str = */ strsep(&abisep, ":");
const char *archname = strsep(&abisep, ":");
Expand All @@ -145,7 +145,7 @@ match_entry(macho_file_t *mf, struct os_info *oi)
CPU_SUBTYPE_ARM_ALL ||
p->cpu.subtype_arm ==
cpu_hint.subtype_arm) {
return p;
goto matched;
}
break;
case CPU_TYPE_POWERPC:
Expand All @@ -155,7 +155,7 @@ match_entry(macho_file_t *mf, struct os_info *oi)
CPU_SUBTYPE_POWERPC_ALL ||
p->cpu.subtype_ppc ==
cpu_hint.subtype_ppc) {
return p;
goto matched;
}
break;
case CPU_TYPE_X86:
Expand All @@ -165,7 +165,7 @@ match_entry(macho_file_t *mf, struct os_info *oi)
CPU_SUBTYPE_X86_ALL ||
p->cpu.subtype_x86 ==
cpu_hint.subtype_x86) {
return p;
goto matched;
}
break;
default:
Expand All @@ -176,13 +176,15 @@ match_entry(macho_file_t *mf, struct os_info *oi)
archname, cputype_to_freebsd_machine_arch(p->cpu));
p++;
}
pkg_emit_notice("Scanned %d entr%s, found none matching selector %s",
pkg_emit_notice("Scanned %"PRIu32" entr%s, found none matching selector %s",
mf->narch, mf->narch > 1 ? "ies" : "y", archname);
return 0;
p=0;
} else if (mf->narch > 1 ) {
pkg_debug(1,"Found %d entries in universal binary, picking first",
pkg_debug(1,"Found %"PRIu32" entries in universal binary, picking first",
mf->narch);
}
matched:
free(abihint);
return p;
}

Expand Down Expand Up @@ -284,26 +286,27 @@ pkg_get_myarch_macho(int fd, struct os_info *oi)
oi->name = xstrdup("Darwin");
free(oi->version);
if (darwin.patch) {
xasprintf(&oi->version, "%d.%d.%d", darwin.major,
xasprintf(&oi->version, "%"PRIuFAST16".%"PRIuFAST16".%"PRIuFAST16"", darwin.major,
darwin.minor, darwin.patch);
} else {
xasprintf(&oi->version, "%d.%d", darwin.major,
xasprintf(&oi->version, "%"PRIuFAST16".%"PRIuFAST16"", darwin.major,
darwin.minor);
}
free(oi->version_major);
xasprintf(&oi->version_major, "%d", darwin.major);
xasprintf(&oi->version_major, "%"PRIuFAST16, darwin.major);
free(oi->version_minor);
xasprintf(&oi->version_minor, "%d", darwin.minor);
xasprintf(&oi->version_minor, "%"PRIuFAST16, darwin.minor);
free(oi->arch);
oi->arch = xstrdup(cputype_to_freebsd_machine_arch(mh.cpu));
snprintf(oi->abi, sizeof(oi->abi), "Darwin:%d:%s", darwin.major, cputype_to_freebsd_machine_arch(mh.cpu));
snprintf(oi->abi, sizeof(oi->abi), "Darwin:%"PRIuFAST16":%s", darwin.major, cputype_to_freebsd_machine_arch(mh.cpu));
// not populating oi->altabi, derived later by caller.
snprintf(oi->str_osversion, sizeof(oi->str_osversion), "%d",
oi->osversion);

ret = EPKG_OK;
} else {
pkg_emit_notice("No OS version information found in binary.");
ret = EPKG_WARN;
}

cleanup:
Expand All @@ -312,17 +315,171 @@ pkg_get_myarch_macho(int fd, struct os_info *oi)
return ret;
}

static const char * const system_dylib_prefixes[] = {
"/System/",
"/usr/lib/",
"/lib/",
};

static bool
system_dylib(const char *libname)
{
const char * const *p = system_dylib_prefixes;
const char * const *p_end = p + NELEM(system_dylib_prefixes);
while (p < p_end) {
if (strncmp(libname, *p, strlen(*p)) == 0) {
return true;
}
p++;
}
return false;
}

static int
analyse_macho(int fd, struct pkg *pkg, const bool baselibs)
{
ssize_t x;
pkg_error_t ret = EPKG_END;

macho_file_t *mf = 0;

if ((x = read_macho_file(fd, &mf)) < 0) {
goto cleanup;
}

const fat_arch_t *p = match_entry(mf, ctx.oi);

if (!p) {
goto cleanup;
}

if (-1 == (x = lseek(fd, p->offset, SEEK_SET))) {
goto cleanup;
}
size_t n = 0;
macho_header_t mh;
if ((x = read_macho_header(fd, &mh)) < 0) {
goto cleanup;
}
const bool swap = mh.swap;
n = 0;
for (uint32_t ui = mh.ncmds; ui-- > 0;) {
size_t n0 = n;
uint32_t loadcmdtype;
uint32_t loadcmdsize;
READ(u32, loadcmdtype);
READ(u32, loadcmdsize);
enum MachOLoadCommand loadcmd = loadcmdtype & ~LC_REQ_DYLD;
switch (loadcmd) {
case LC_RPATH:
case LC_LOAD_DYLINKER:;
char *dylinker = 0;
if ((x = read_path(fd, swap, loadcmdsize,
&dylinker)) < 0) {
goto cleanup;
}
n += x;
pkg_debug(3, "load_dylinker %d: %s\n", loadcmd, dylinker);
free(dylinker);
break;
case LC_ID_DYLIB: // provides
case LC_LOAD_DYLIB: // requires...
case LC_LOAD_WEAK_DYLIB:
case LC_REEXPORT_DYLIB:
case LC_LAZY_LOAD_DYLIB:
case LC_LOAD_UPWARD_DYLIB:;
dylib_t *dylib = 0;
if ((x = read_dylib(fd, swap, loadcmdsize,
&dylib)) < 0) {
goto cleanup;
}
n += x;
if (!baselibs && system_dylib(dylib->path)) {
pkg_debug(3,
"Skipping System dynamic library path: %s ts %"PRIu32" current(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16") compat(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16")\n",
dylib->path, dylib->timestamp,
dylib->current_version.major,
dylib->current_version.minor,
dylib->current_version.patch,
dylib->compatibility_version.major,
dylib->compatibility_version.minor,
dylib->compatibility_version.patch);
} else {
const char * basename = strrchr(dylib->path, '/');
if (basename) {
pkg_debug(3,
"Adding dynamic library path: %s ts %"PRIu32" current(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16") compat(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16")\n",
dylib->path, dylib->timestamp,
dylib->current_version.major,
dylib->current_version.minor,
dylib->current_version.patch,
dylib->compatibility_version.major,
dylib->compatibility_version.minor,
dylib->compatibility_version.patch);

basename++;

char *lib_with_version;
if (dylib->current_version.patch) {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor, dylib->current_version.patch);
} else {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor);
}
if (LC_ID_DYLIB == loadcmd) {
pkg_addshlib_provided(pkg, lib_with_version);
} else {
pkg_addshlib_required(pkg, lib_with_version);
}
free(lib_with_version);
}
}
free(dylib);
break;
default:
break;
}
const uint32_t fill = loadcmdsize - (n - n0);
if (fill && -1 == (x = lseek(fd, fill, SEEK_CUR))) {
goto cleanup;
}
n += fill;
if (n > mh.sizeofcmds) {
// we passed the frame boundary of the load commands
pkg_emit_error("Mach-O structure misread.");
errno = EINVAL;
goto cleanup;
}
}

cleanup:
free(mf);
return ret;
}

int
pkg_analyse_init_macho(__unused const char *stage)
{
return EPKG_OK;
}

int
pkg_analyse_macho(const bool developer_mode, __unused struct pkg *pkg, __unused const char *fpath)
pkg_analyse_macho(const bool developer_mode, struct pkg *pkg, const char *fpath)
{
int ret = EPKG_OK;
// int ret = analyse_macho(pkg, fpath);
bool baselibs = pkg_object_bool(pkg_config_get("ALLOW_BASE_SHLIBS"));
pkg_debug(1, "Analysing Mach-O %s %d", fpath, baselibs);

int fd = open(fpath, O_RDONLY);
if (-1 == fd) {
pkg_emit_errno("open", fpath);
ret = EPKG_FATAL;
} else {
ret = analyse_macho(fd, pkg, baselibs);
if (-1 == close(fd)) {
pkg_emit_errno("open", fpath);
ret = EPKG_FATAL;
}
}
if (developer_mode) {
if (ret != EPKG_OK && ret != EPKG_END) {
return EPKG_WARN;
Expand Down
4 changes: 3 additions & 1 deletion libpkg/pkg_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,9 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
memset(&oi, 0, sizeof(oi));
envabi = getenv("ABI");
if (envabi == NULL) {
pkg_get_myarch_with_legacy(&oi);
if (EPKG_OK != (err = pkg_get_myarch_with_legacy(&oi))) {
goto out;
}
} else {
strlcpy(oi.abi, envabi, sizeof(oi.abi));
pkg_arch_to_legacy(oi.abi, oi.altabi, sizeof(oi.abi));
Expand Down
Loading
Loading