Skip to content

Fix Solaris 10 build: missing libproc.h #15525

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

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion Zend/Zend.m4
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ dnl
AC_DEFUN([ZEND_INIT], [dnl
AC_REQUIRE([AC_PROG_CC])

AC_CHECK_HEADERS([cpuid.h])
AC_CHECK_HEADERS(m4_normalize([
cpuid.h
libproc.h
]))

dnl Check for library functions.
AC_CHECK_FUNCS(m4_normalize([
Expand Down
14 changes: 10 additions & 4 deletions Zend/zend_call_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ typedef int boolean_t;
#include <sys/syscall.h>
#endif
#ifdef __sun
#define _STRUCTURED_PROC 1
#include <sys/lwp.h>
#include <sys/procfs.h>
#include <libproc.h>
# ifdef HAVE_LIBPROC_H
Copy link
Member

@devnexen devnexen Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think the related api usage needs to be guarded, well more likely the full function in fact (by line 704). In other words, if no libproc then the function returns false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a patch for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my lack of clarity, what I meant is something like this

static bool zend_call_stack_get_solaris_proc_maps(zend_call_stack *stack)
{
#ifdef HAVE_LIBPROC_H
	char buffer[4096];
	uintptr_t addr_on_stack = (uintptr_t) zend_call_stack_position();
	bool found = false, r = false;
	struct ps_prochandle *proc;
	prmap_t *map, *orig;
	struct rlimit rlim;
	char path[PATH_MAX];
	size_t size;
	ssize_t len = -1;
	pid_t pid;
	int error, fd;

	pid = getpid();
	proc = Pgrab(pid, PGRAB_RDONLY, &error);
	if (!proc) {
		return false;
	}

	size = (1 << 20);
	snprintf(path, sizeof(path), "/proc/%d/map", (int)pid);

	if ((fd = open(path, O_RDONLY)) == -1) {
		Prelease(proc, 0);
		return false;
	}

	orig = malloc(size);
	if (!orig) {
		Prelease(proc, 0);
		close(fd);
		return false;
	}

	while (size > 0 && (len = pread(fd, orig, size, 0)) == size) {
		prmap_t *tmp;
		size <<= 1;
		tmp = realloc(orig, size);
		if (!tmp) {
			goto end;
		}
		orig = tmp;
	}

	for (map = orig; len > 0; ++map) {
		if ((uintptr_t)map->pr_vaddr <= addr_on_stack && (uintptr_t)map->pr_vaddr + map->pr_size >= addr_on_stack) {
			found = true;
			break;
		}
		len -= sizeof(*map);
	}

	if (!found) {
		goto end;
	}

	error = getrlimit(RLIMIT_STACK, &rlim);
	if (error || rlim.rlim_cur == RLIM_INFINITY) {
		goto end;
	}

	stack->base = (void *)map->pr_vaddr + map->pr_size;
	stack->max_size = rlim.rlim_cur;
	r = true;

end:
	free(orig);
	Prelease(proc, 0);
	close(fd);
	return r;
#else
    return false;
#endif
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect thanks. :D Appended in the next commit. This looks fine?

Copy link
Member

@devnexen devnexen Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems, let me know if it builds, I no longer have any solaris stuff needs to recreate my VMs :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, I know. Setting Solaris 10 development environment is not trivial at all. There is even a bug in GCC 5 so the GCC 4.9 needs to be used. And the main issue is getting the re2c newer than 1.0.3 and other dependencies (libxml ...).

It builds successfully with this patch in the current PR.

# define _STRUCTURED_PROC 1
# include <sys/lwp.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, sys/lwp.h is still needed outside I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm now a bit confused because when fixing the stack grow check direction in Zend/Zend.m4 then there is

Fatal error: Maximum call stack size of 0 bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression in /projects/php-src/run-tests.php on line 35

But the sys/lwp.h fix appended in the next commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes surely zend stack tests do not pass now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't ok then yet. Not that tests don't pass, but any PHP file cannot be run. Because zend_stack_limit_error throws error. Something more then should be adjusted here 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this ? if this still does not work, we might need to implement the call without libproc api, there might be an older system to be used.

#ifdef HAVE_LIBPROC_H
static bool zend_call_stack_get_solaris_proc_maps(zend_call_stack *stack)
{
	char buffer[4096];
	uintptr_t addr_on_stack = (uintptr_t) zend_call_stack_position();
	bool found = false, r = false;
	struct ps_prochandle *proc;
	prmap_t *map, *orig;
	struct rlimit rlim;
	char path[PATH_MAX];
	size_t size;
	ssize_t len = -1;
	pid_t pid;
	int error, fd;

	pid = getpid();
	proc = Pgrab(pid, PGRAB_RDONLY, &error);
	if (!proc) {
		return false;
	}

	size = (1 << 20);
	snprintf(path, sizeof(path), "/proc/%d/map", (int)pid);

	if ((fd = open(path, O_RDONLY)) == -1) {
		Prelease(proc, 0);
		return false;
	}

	orig = malloc(size);
	if (!orig) {
		Prelease(proc, 0);
		close(fd);
		return false;
	}

	while (size > 0 && (len = pread(fd, orig, size, 0)) == size) {
		prmap_t *tmp;
		size <<= 1;
		tmp = realloc(orig, size);
		if (!tmp) {
			goto end;
		}
		orig = tmp;
	}

	for (map = orig; len > 0; ++map) {
		if ((uintptr_t)map->pr_vaddr <= addr_on_stack && (uintptr_t)map->pr_vaddr + map->pr_size >= addr_on_stack) {
			found = true;
			break;
		}
		len -= sizeof(*map);
	}

	if (!found) {
		goto end;
	}

	error = getrlimit(RLIMIT_STACK, &rlim);
	if (error || rlim.rlim_cur == RLIM_INFINITY) {
		goto end;
	}

	stack->base = (void *)map->pr_vaddr + map->pr_size;
	stack->max_size = rlim.rlim_cur;
	r = true;

end:
	free(orig);
	Prelease(proc, 0);
	close(fd);
	return r;
}

static bool zend_call_stack_get_solaris(zend_call_stack *stack)
{
	if (_lwp_self() == 1) {
		return zend_call_stack_get_solaris_proc_maps(stack);
	}
	return zend_call_stack_get_solaris_pthread(stack);
}
#else
static bool zend_call_stack_get_solaris(zend_call_stack *stack)
{
	return zend_call_stack_get_solaris_pthread(stack);
}
#endif

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a bit different checks. With this above that was still happening. But the following commit seems to now work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your change is simpler, nice.

# include <sys/procfs.h>
# include <libproc.h>
# endif
#include <thread.h>
#endif

Expand Down Expand Up @@ -701,6 +703,7 @@ static bool zend_call_stack_get_solaris_pthread(zend_call_stack *stack)

static bool zend_call_stack_get_solaris_proc_maps(zend_call_stack *stack)
{
#ifdef HAVE_LIBPROC_H
char buffer[4096];
uintptr_t addr_on_stack = (uintptr_t) zend_call_stack_position();
bool found = false, r = false;
Expand Down Expand Up @@ -770,6 +773,9 @@ static bool zend_call_stack_get_solaris_proc_maps(zend_call_stack *stack)
Prelease(proc, 0);
close(fd);
return r;
#else
return false;
#endif
}

static bool zend_call_stack_get_solaris(zend_call_stack *stack)
Expand Down
Loading