Skip to content

Commit e0cd08a

Browse files
Fix libpulp not able to livepatch memcpy on ppc64le
On the ppc64le port of libpulp, a special stack is necessary to hold information between redirections. The original implementation called memcpy two times, one when the DEBUG macro is expanded and another when the ulp_stack overflows and a new stack is allocated. Hence, avoid this problem by not calling memcpy in this site. Signed-off-by: gbelinassi <gbelinassi@suse.de>
1 parent 4ba375e commit e0cd08a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/arch/powerpc64le/patch.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ void ulp_stack_helper(void)
256256
caller), so just assert it here. */
257257
libpulp_assert(ulp_stack[ULP_STACK_REAL_SIZE] <= ulp_stack[ULP_STACK_USED_SIZE]);
258258

259+
/* NOTE: be careful with the functions we call here. If we call a certain
260+
function here, then we may have problems livepatching it. */
261+
259262
/* Storage depleted, allocate a new stack. */
260263
unsigned long old_size = ulp_stack[ULP_STACK_REAL_SIZE];
261264

@@ -265,8 +268,6 @@ void ulp_stack_helper(void)
265268

266269
void *old = (void *)ulp_stack[ULP_STACK_PTR];
267270

268-
DEBUG("thread %lu: expanding stack to %lu bytes", pthread_self(), ulp_stack[ULP_STACK_REAL_SIZE]);
269-
270271
/* Allocate buffer for our stack. */
271272
void *new = mmap(NULL, ulp_stack[ULP_STACK_REAL_SIZE],
272273
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -280,14 +281,26 @@ void ulp_stack_helper(void)
280281

281282
/* In case we have a previous allocated buffer, then copy this. */
282283
if (old != NULL) {
283-
memcpy(new, old, old_size);
284+
285+
/* We can't use memcpy here, hence do our thing. */
286+
unsigned char *restrict oldp = old;
287+
unsigned char *restrict newp = new;
288+
unsigned long s = old_size;
289+
290+
while (s > 0) {
291+
*newp++ = *oldp++;
292+
s--;
293+
}
294+
284295
munmap(old, old_size);
285296
old = NULL;
286297
}
287298

288299
ulp_stack[ULP_STACK_PTR] = (unsigned long) new;
289300
libpulp_assert(ulp_stack[ULP_STACK_PTR] != 0L);
290301

302+
DEBUG("thread %lu: expanded stack to %lu bytes", pthread_self(), ulp_stack[ULP_STACK_REAL_SIZE]);
303+
291304
/* Setup destructor for mmap memory, so we don't leak memory when a thread
292305
is destroyed. */
293306
pthread_once(&ulp_once_control, ulp_pthread_key_init);

0 commit comments

Comments
 (0)