Skip to content

Emulate GNU ar's deterministic mode - #1537

Merged
tibbe merged 6 commits into
haskell:masterfrom
liyang:deterministic-ar
Nov 6, 2013
Merged

tibbe merged 6 commits into
haskell:masterfrom
liyang:deterministic-ar

Conversation

@liyang

@liyang liyang commented Oct 11, 2013

Copy link
Copy Markdown
Contributor

After invoking ar(1), replace each object file's metadata with zero for the mtime, UID and GID, and 0644 for the file mode. Do not touch the existing archive if the contents are the same.

Relevant discussion in #1177; we can probably close the previous attempts in #1410 and #1501.

@nh2 originally wrote this but he disappeared. We would like to see this in production eventually, so I cleaned it up and added more sanity checks.

Comment thread Cabal/Distribution/Simple/Program/Ar.hs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I forget, don't we already have utilities to create a temporary filename that's guaranteed to not be used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I saw one. I'll look into using some existing thing, but I remembered it giving a file Handle which I don't need, and I wasn't sure if I could just hClose it. (Also it should preferably be on the same FS as target.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I eventually realised withTempFile (& cetera) was just too painful to use and ended up with withTempDirectory instead.

@nh2

nh2 commented Oct 11, 2013

Copy link
Copy Markdown
Member

@liyang It would have been nicer if you had created another commit instead of amending my ones; I can't easily look at what's changed compared to my version this ways, or comment on it. Would you mind changing?

@liyang

liyang commented Oct 12, 2013

Copy link
Copy Markdown
Contributor Author

@nh2

git diff liyang/deterministic-ar nh2/avoid-linking -- Cabal/Distribution/Simple/Program/Ar.hs

@nh2

nh2 commented Oct 12, 2013

Copy link
Copy Markdown
Member

git diff liyang/deterministic-ar nh2/avoid-linking -- Cabal/Distribution/Simple/Program/Ar.hs

@liyang I'm aware. It still attributes code which I did not write to me (!), and as I said, forbids me to make github comments on what you changed since my last proposal.

@liyang

liyang commented Oct 13, 2013

Copy link
Copy Markdown
Contributor Author

@nh2 I essentially rewrote the whole thing. At the time, it seemed like a choice between crediting[0] you for the original code or just claiming that I wrote everything. (I generally dislike including refactors of earlier commits in a feature branch.)

Okay, I'll rebase my refactor on top of your original when I get in to work on Monday.

[0] Or blaming. ;)

@nh2

nh2 commented Oct 13, 2013

Copy link
Copy Markdown
Member

Much appreciated!

@liyang

liyang commented Oct 14, 2013

Copy link
Copy Markdown
Contributor Author

I think I've addressed most of the issues mentioned earlier. The one remaining task is to test this on Windows and OS X (and FreeBSD? I have never used FreeBSD. Someone else will have to do this, or we just trust Wikipedia.)

@liyang

liyang commented Oct 15, 2013

Copy link
Copy Markdown
Contributor Author

On OS X (with Homebrew removed from $PATH), I successfully built and installed Cabal followed by cabal-install.

However I noticed that the metadata for the symbol table (first entry in libHSCabal-1.19.0.a) are non-zero (or 0644) in the installed location, although not under dist/build. (Under Ubuntu/GNU/Linux it's fine in both locations.) I guess ranlib was invoked at some point during install. Will fix this when I get a chance.

Haven't checked with Homebrew enabled yet.

Will look at Windows when I'm home. (Windows GHC uses mingw32 and GNU tools, so I don't foresee any compatibility problems there.)

@23Skidoo

Copy link
Copy Markdown
Member

@liyang Thanks for doing that work.

@liyang

liyang commented Oct 16, 2013

Copy link
Copy Markdown
Contributor Author

How embarrassing. Under Windows I actually got some .o files of odd size, and found that I'd messed up. (Didn't seek forward 1 byte when offset was odd.) Have force-pushed and fixed that case.

Checking OS X (with and without Homebrew (again)) now.

liyang added a commit to liyang/cabal that referenced this pull request Oct 16, 2013
…X ld issues

∙ At one point under OS X ld required that the mtime of the symbol table to
  be not older than that of the archive itself. This means that even copying
  an .a file required invoking ranlib to update the symbol table mtime.
∙ However running ranlib renders pointless the deterministic mode of haskell#1537.
∙ As of at least 2008 according to the BUGS section of ranlib(1), this has
  no longer been necessary.

First introduction: 12b3590
Current implementation: af3da76
@liyang

liyang commented Oct 16, 2013

Copy link
Copy Markdown
Contributor Author

See comments for the latest commit. Under OS X, I managed to install cabal-install from a bare GHC install without encountering any complaints at all from ld, and the installed archives do indeed have 0 mtime throughout.

I think it's time to ship now.

@tibbe

tibbe commented Oct 16, 2013

Copy link
Copy Markdown
Contributor

Any chance we can add a test so we don't accidentally break this feature in the future?

@liyang

liyang commented Oct 17, 2013

Copy link
Copy Markdown
Contributor Author

Break which aspect of this feature? Could you be more specific?

I could add a test that builds a simple archive and checks that all the metadata in the headers are as expected, but I'm not sure what kind of breakage this would help to protect against.

@23Skidoo

Copy link
Copy Markdown
Member

@liyang Well, having at least some tests is better than no tests at all.

liyang added a commit to liyang/cabal that referenced this pull request Nov 5, 2013
…X ld issues

∙ At one point under OS X, ld required the mtime of the symbol table to be
  not older than that of the archive itself. This means that even copying an
  .a file required invoking ranlib to update the symbol table mtime.
∙ However running ranlib renders pointless the deterministic mode of haskell#1537.
∙ As of at least 2008 according to the BUGS section of ranlib(1), this has
  no longer been necessary.

First introduction: 12b3590
Current implementation: af3da76
nh2 and others added 5 commits November 5, 2013 15:56
We need them to compare against, so that we can decide if we want to
write new files or keep the old ones untouched.
We do this by not putting timestamps into the .a file,
and not touching it if is already there and the contents are the same.
After invoking ar(1), replace each object file's metadata with zero for the
mtime, UID and GID, and 0644 for the file mode. Do not touch the existing
archive if the contents are the same.

This rewrites the previous patch to,
 ∙ also wipe UID, GID and file mode;
 ∙ do a single read/write for each header, and less seeking in general;
 ∙ do its work inside a temporary directory, via 'withTempDirectory';
   ∘ Which also deletes the temporary target when it's identical to the old.
 ∙ use Distribution.Compat.CopyFile.filesEqual;
 ∙ use the standard 'dieWithLocation' instead of invoking 'error'.
…X ld issues

∙ At one point under OS X, ld required the mtime of the symbol table to be
  not older than that of the archive itself. This means that even copying an
  .a file required invoking ranlib to update the symbol table mtime.
∙ However running ranlib renders pointless the deterministic mode of haskell#1537.
∙ As of at least 2008 according to the BUGS section of ranlib(1), this has
  no longer been necessary.

First introduction: 12b3590
Current implementation: af3da76
@liyang

liyang commented Nov 5, 2013

Copy link
Copy Markdown
Contributor Author

Sorry for taking so long over this, had been busy with other things.

The latest commit builds and installs a dummy library and checks that the resulting archives in both the dist/build directory and the final installed location (user's ~/.cabal) have zero timestamps, UIDs, GIDs and 0644 modes.

I've tested this test by commenting out the call to wipeMetadata in Distribution.Simple.Program.Ar, and rebased on top of the current master.

Does it look good to merge now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How can assertFailure fail? In other words: why does this function exist?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It can't, but it has the type Assertion = IO () whereas I wanted a polymorphic IO a. If you like, I can rewrite it as:

assertFailure' msg = assertFailure msg >> return {-unpossible!-}undefined

and add a comment--I thought it was obvious what it was doing but maybe it's not.

@tibbe

tibbe commented Nov 5, 2013

Copy link
Copy Markdown
Contributor

One comment. Looks good to merge otherwise.

@tibbe
tibbe merged commit bc0e2a4 into haskell:master Nov 6, 2013
@liyang
liyang deleted the deterministic-ar branch November 11, 2013 10:08
@lukexi

lukexi commented Jan 27, 2014

Copy link
Copy Markdown
Collaborator

Hi guys,

This seems to be crashing me when using cabal with GHC iOS:

Resolving dependencies...
Configuring random-1.0.1.1...
Building random-1.0.1.1...
Preprocessing library random-1.0.1.1...
[1 of 1] Compiling System.Random    ( System/Random.hs, dist/arm/build/System/Random.o )
[1 of 1] Compiling System.Random    ( System/Random.hs, dist/arm/build/System/Random.p_o )
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: archive library: dist/arm/build/libHSrandom-1.0.1.1.a-90308/libHSrandom-1.0.1.1.a will be fat and ar(1) will not be able to operate on it
cabal: dist/arm/build/libHSrandom-1.0.1.1.a-90308/libHSrandom-1.0.1.1.a:
Distribution.Simple.Program.Ar.wipeMetadata: Bad global header
Failed to install random-1.0.1.1

I'm using the arm scripts from https://github.com/ghc-ios/ghc-ios-scripts.

Happy to help debug, or we can just skip this optimization if the metadata can't be parsed?

@nh2

nh2 commented Jan 27, 2014

Copy link
Copy Markdown
Member

Happy to help debug

The .a file should start with (see here):

archLF = "!<arch>\x0a" -- global magic, 8 bytes
...
   -- http://en.wikipedia.org/wiki/Ar_(Unix)#File_format_details
   wipeArchive :: Handle -> Integer -> IO ()
   wipeArchive h archiveSize = do
       global <- BS.hGet h (BS.length archLF)
       unless (global == archLF) $ wipeError "Bad global header"
       wipeHeader (toInteger $ BS.length archLF)

Could you check if your .a file starts with !<arch>\x0a (== !<arch>\n) or with something else?

23Skidoo added a commit that referenced this pull request Jan 30, 2014
@23Skidoo

Copy link
Copy Markdown
Member

@lukexi I've disabled this behaviour on ARM for now. Please tell whether my patch fixed the problem.

@lukexi

lukexi commented Feb 18, 2014

Copy link
Copy Markdown
Collaborator

Hi Mikhail,
I tested today and it seems to do the trick, thanks!
Luke

On Jan 29, 2014, at 4:02 PM, Mikhail Glushenkov [email protected] wrote:

@lukexi I've disabled this behaviour on ARM for now ( 58cdd44 ). Please tell whether my patch fixed the problem.

—
Reply to this email directly or view it on GitHub.

@23Skidoo

Copy link
Copy Markdown
Member

@lukexi

Thanks. Would be nice to know why it broke in the first place.

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.

5 participants