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

Conversation

petk
Copy link
Member

@petk petk commented Aug 21, 2024

The libproc.h header file was added on Solaris as of 11.4.

The libproc.h header file was added on Solaris as of 11.4.
@@ -67,7 +67,9 @@ typedef int boolean_t;
#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.

@@ -67,7 +67,9 @@ typedef int boolean_t;
#define _STRUCTURED_PROC 1
Copy link
Member

Choose a reason for hiding this comment

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

optional: if we want to be "nitpicky", sys/procfs.h is also not used then in case the libproc api is not used. Can be guarded as well alongside with the #define _STRUCTURED_PROC 1 (which is for the sys/procfs.h to enable the newer procfs api). But again, it s being pedantic here :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in the next commit. Builds successfully.

#include <libproc.h>
# ifdef HAVE_LIBPROC_H
# 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.

@petk
Copy link
Member Author

petk commented Aug 22, 2024

Then I'll be merging this soon. Change in the PR looks ok?

Copy link
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

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

LCTM

@petk petk merged commit f952263 into php:master Aug 22, 2024
10 checks passed
@petk petk deleted the patch-solaris-10-libproc-h branch August 22, 2024 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants