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

Pari initialization segfaults in Cygwin since #22633 #22810

Closed
embray opened this issue Apr 14, 2017 · 32 comments
Closed

Pari initialization segfaults in Cygwin since #22633 #22810

embray opened this issue Apr 14, 2017 · 32 comments

Comments

@embray
Copy link
Contributor

embray commented Apr 14, 2017

Sorry, I thought I had tested the last version of the patch in #22633. But I think I just tested it in PARI/GP, but not in Sage itself, since my Sage build was broken at the time for other reasons.

The offending line is here. It seems that MAP_FIXED is not working correctly for some reason, but the resulting call to pari_err segfaults because the pari mainstack is left in an invalid state.

I'm not sure if this is a bug in Cygwin or what, but I'm trying to put together a simplified test case.

Depends on #22675

Upstream: Not yet reported upstream; Will do shortly.

CC: @jdemeyer

Component: porting: Cygwin

Keywords: windows cygwin pari mmap

Author: Erik Bray

Branch: 00a5d73

Reviewer: Travis Scrimshaw, Jeroen Demeyer

Issue created by migration from https://trac.sagemath.org/ticket/22810

@embray embray added this to the sage-8.0 milestone Apr 14, 2017
@jdemeyer
Copy link

Replying to @embray:

It seems that MAP_FIXED is not working correctly for some reason

The question now becomes: why?

Does everything work fine if you remove the line if (res != addr) pari_err(e_MEM);

For debugging, it would be useful to print the values of addr, s and res after that mmap() call.

@embray
Copy link
Contributor Author

embray commented Apr 14, 2017

comment:2

Yes, that's what I'm trying to do now.

@jdemeyer
Copy link

comment:3

I guess that MAP_FIXED is not commonly used, so it wouldn't be so surprising that it is buggy in Cygwin.

@embray
Copy link
Contributor Author

embray commented Apr 14, 2017

comment:4

The problem is not so much MAP_FIXED itself--it works in a different context.

In this case the mmap in pari_mainstack_mreset is failing unless I call munmap(addr, s) first. If I understand correctly that should be fine, because we're not using that memory anyways right now if we're setting it to PROT_NONE.

@embray
Copy link
Contributor Author

embray commented Apr 14, 2017

comment:5

I also found some relevant discussion here: http://cygwin.1069669.n5.nabble.com/mmap-MAP-FIXED-vs-mprotect-td98125.html But I think it's fine as long as you munmap() first--that will set those pages to unused/unresreved, and then they can be re-reserved. The only problem is the munmap followed by mmap will not be atomic. But how likely is that to be a problem in practice?

@jdemeyer
Copy link

comment:6

Replying to @embray:

The only problem is the munmap followed by mmap will not be atomic. But how likely is that to be a problem in practice?

With multi-threading, there is a non-zero chance of things going wrong.

For the record, calling mmap() with MAP_FIXED on a already-mapped region is specified by POSIX to replace an existing mapping. So if Cygwin does not support that, I would consider that a bug.

@jdemeyer
Copy link

Dependencies: #22675

@embray
Copy link
Contributor Author

embray commented Apr 17, 2017

comment:7

Right, I think that would be a bug too--the thread I linked to above has some comment about that.

Honestly, I might just add a patch that reverts to the old mprotect-based approach on Cygwin.

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

comment:9

I spent some more time poking at this, and what I found is that it suffices on Cygwin to mprotect(..., PROT_NONE) rather than re-mmap with PROT_NONE and MAP_FIXED, which is in fact buggy (although a known issue).

This means, unfortunately, having to put an #ifdef __CYGWIN__ in pari_mainstack_mreset, to have it use mprotect instead of mmap, but other than that I think that is the only change needed. In Cygwin, mprotect(..., PROT_NONE) will actually decommit the memory, which is what we want.

@jdemeyer
Copy link

comment:10

Thanks for the analysis. Do you have a patch ready?

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

comment:11

Incoming.

@jdemeyer
Copy link

comment:12

Remember to base this ticket on top of #22675.

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

comment:13

Yep, that's the plan.

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

comment:14

Still waiting on some testing.


New commits:

543c4a1Upgrade PARI to version 2.9.2
ab20b1bAnother patch to Pari's stack allocation to fix a bug on Cygwin: https://github.com/sagemath/sage/issues/22810

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

Commit: ab20b1b

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

Author: Erik Bray

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

Branch: u/embray/cygwin/ticket-22810

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 21, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

00a5d73Another patch to Pari's stack allocation to fix a bug on Cygwin: https://github.com/sagemath/sage/issues/22810

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 21, 2017

Changed commit from ab20b1b to 00a5d73

@embray
Copy link
Contributor Author

embray commented Apr 21, 2017

comment:16

Seems to work now.

@tscrim
Copy link
Collaborator

tscrim commented Apr 23, 2017

Reviewer: Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Apr 23, 2017

comment:17

Jeroen, any more comments on this? Since this worked for me on Cygwin, I would set this to a positive review. However, you know much more about Pari than I do.

@jdemeyer
Copy link

comment:18

Replying to @tscrim:

Since this worked for me on Cygwin

Define "worked". Did you run the PARI testsuite? And the Sage doctests in src/sage/libs/*pari*?

If both of these pass, then it's fine for me.

@embray
Copy link
Contributor Author

embray commented Apr 24, 2017

comment:19

I did all of the above, but Travis should confirm if possible.

@embray
Copy link
Contributor Author

embray commented Apr 24, 2017

comment:20

Please by my CI, Travis (sorry, you probably get that all the time...)

@tscrim
Copy link
Collaborator

tscrim commented Apr 24, 2017

Changed reviewer from Travis Scrimshaw to Travis Scrimshaw, Jeroen Demeyer

@tscrim
Copy link
Collaborator

tscrim commented Apr 24, 2017

comment:21

I will be your CI. :P

Built okay and tests pass.

@vbraun
Copy link
Member

vbraun commented Apr 25, 2017

Changed branch from u/embray/cygwin/ticket-22810 to 00a5d73

@embray
Copy link
Contributor Author

embray commented Apr 28, 2017

Changed commit from 00a5d73 to none

@embray
Copy link
Contributor Author

embray commented Apr 28, 2017

Upstream: Not yet reported upstream; Will do shortly.

@embray
Copy link
Contributor Author

embray commented Apr 28, 2017

comment:23

This still needs to be reported upstream, however.

@jdemeyer
Copy link

comment:24

Right. Should I?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants