public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: DJ Delorie <dj@redhat.com>
Cc: libffi-discuss@sourceware.org
Subject: Re: segfault in ffi_data_to_code_pointer
Date: Thu, 04 Jul 2019 12:35:00 -0000	[thread overview]
Message-ID: <87r276j7uq.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <xntvcct6b0.fsf@greed.delorie.com> (DJ Delorie's message of "Wed,	26 Jun 2019 16:55:47 -0400")

* DJ Delorie:

> In src/closures.c, ffi_data_to_code_pointer() calls segment_holding()
> to get a pointer to the code segment for a data segment.  It doesn't
> check for a NULL return, and I've got a test case where I run Ruby's
> test suite (on a non-selinux aarch64 machine, if that matters) and
> segment_holding() returns NULL and much hilarity ensues.
>
> The following patch fixes the segfault, but I don't know if
> segment_holding() returning NULL is an expected case, or a symptom of
> problems elsewhere?
>
>> diff -rup a/src/closures.c b/src/closures.c
>> --- a/src/closures.c	2019-06-25 21:21:06.738743440 -0400
>> +++ b/src/closures.c	2019-06-25 21:22:00.769716129 -0400
>> @@ -621,7 +621,10 @@ void *
>>  ffi_data_to_code_pointer (void *data)
>>  {
>>    msegmentptr seg = segment_holding (gm, data);
>> -  return add_segment_exec_offset (data, seg);
>> +  if (seg)
>> +    return add_segment_exec_offset (data, seg);
>> +  else
>> +    return data;
>>  }

I think you also need to fix the aarch64 code to avoid a null pointer
dereference, like below.  Also submitted as
<https://github.com/libffi/libffi/pull/499>.

I have verified that this fixes the Ruby build failure.

Thanks,
Florian

diff --git a/src/aarch64/ffi.c b/src/aarch64/ffi.c
index 6f6aac4..69e04d6 100644
--- a/src/aarch64/ffi.c
+++ b/src/aarch64/ffi.c
@@ -777,7 +777,8 @@ ffi_prep_closure_loc (ffi_closure *closure,
 
   /* Also flush the cache for code mapping.  */
   unsigned char *tramp_code = ffi_data_to_code_pointer (tramp);
-  ffi_clear_cache (tramp_code, tramp_code + FFI_TRAMPOLINE_SIZE);
+  if (tramp_code != NULL)
+    ffi_clear_cache (tramp_code, tramp_code + FFI_TRAMPOLINE_SIZE);
 #endif
 
   closure->cif = cif;
diff --git a/src/closures.c b/src/closures.c
index 4d7f945..18d3913 100644
--- a/src/closures.c
+++ b/src/closures.c
@@ -925,6 +925,8 @@ void *
 ffi_data_to_code_pointer (void *data)
 {
   msegmentptr seg = segment_holding (gm, data);
+  if (seg == NULL)
+    return NULL;
   return add_segment_exec_offset (data, seg);
 }
 

      parent reply	other threads:[~2019-07-04 12:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-26 20:55 DJ Delorie
2019-06-30 11:46 ` Anthony Green
2019-07-02 23:47   ` DJ Delorie
2019-07-03 22:28     ` DJ Delorie
2019-07-03 22:47       ` Anthony Green
2019-07-03 22:54         ` DJ Delorie
2019-07-03 23:14           ` Anthony Green
2019-07-04  0:19             ` DJ Delorie
2019-07-04 12:35 ` Florian Weimer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r276j7uq.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=dj@redhat.com \
    --cc=libffi-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).