Skip to content

Successful cloning a repo using libgit2 compiled to JavaScript with Emscripten #4376

Closed

Description

@vadimkantorov

@petersalomonsen accomplished the feat in this issue

Very few edits to libgit2 were required:

  1. chmod in indexer.c, filebuf.c to explicitly give write permissions on the default read-only Emscripten's file system
  2. pwrite in indexer.c to explicitly give write permissions to a mmap which is also default read-only in Emscripten
  3. disabling http parsing of the response in transports/http.c
  4. disabling writing http headers in write_chunk in transports/http.c

Dropping this here in case anyone wants to prepare these for a proper feature at some point.

Activity

  1. petersalomonsen commented on Nov 8, 2017

    @petersalomonsen
    Contributor

    What would be required in order to merge the mentioned edits back to the upstream libgit2 repository?

  2. ethomson commented on Nov 8, 2017

    @ethomson
    Member

    Speaking generally, I'm interested in having emscripten support. I think that would be neat.

    As is noted in emscripten-core/emscripten#4902, that needs to be cleaned up. eg:

    1. Don't change the cmake defaults. Instead, in your build script, turn features on/off as you need them. (eg, cmake -DTHREADSAFE=OFF).
    2. Have you tried with the mmap emulation? (eg, define NO_MMAP)
    3. I also would like to see a custom transport that uses emscripten
  3. petersalomonsen commented on Nov 8, 2017

    @petersalomonsen
    Contributor

    Thanks for the pointers @ethomson

    Will have a look at it and see what I can do.

    Regarding 3, I changed to registering a tls stream for emscripten like this:

    git_stream_register_tls(git_open_emscripten_stream);

    But you'd rather prefer a full transport? Currently the http transport is used and works fine - just had to disable some lines of code. E.g. the http parser is not needed with emscripten as we already have one in the browser. Maybe better to introduce a configuration switch to turn off the http parser?

  4. CMCDragonkai commented on Nov 8, 2017

    @CMCDragonkai

    So the reason why there are those http issues is because the browser already performs some parsing?

    What happens if the same emscripten compiled output is run inside node, does it has the same problems? I intend to run libgit2 via emscripten on node primarily.

  5. CMCDragonkai commented on Nov 8, 2017

    @CMCDragonkai

    @ethomson The mmap emulation denies the use of MAP_SHARED

    libgit2/src/posix.c

    Lines 245 to 248 in 28c8950

    if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) {
    giterr_set(GITERR_OS, "trying to map shared-writeable");
    return -1;
    }

    Is there any part of libgit2 that relies on MAP_SHARED? A quick search over the codebase shows the indexer and fileops using MAP_SHARED. By using NO_MMAP, does this result in problems? (https://github.com/libgit2/libgit2/search?utf8=%E2%9C%93&q=GIT_MAP_SHARED&type=)

    I just discovered other people who have similar problems with mmap on libgit2:

    Also emscripten makes it that munmap will msync the mapped area, before deallocating it (it was allocated). This doesn't appear to be part of the standard. It doesn't seem possible for libgit2 to emulate MAP_SHARED because the call has to return an address back into the heap, and there's no way to intercept pointer dereferencing into the heap to make it actually address some page on disk, as this other guy said (https://groups.google.com/forum/#!topic/emscripten-discuss/63jY4CTbL6k) you would need to emulate your own memory management within libgit2. The end result is to enforce that all platforms running libgit2 needs to have some sort of mmap, or that to replace all uses of mmap with the standard read and write calls. Alternatively, one could also always use msync after every read/write into the mmaped area.

  6. petersalomonsen commented on Nov 8, 2017

    @petersalomonsen
    Contributor

    @CMCDragonkai Node also has a builtin http parser ( https://nodejs.org/api/http.html ) so you would also want to bypass the libgit2 http parser when running in Node. Would have to adjust to the Node http API, but the concept is more or less the same as in the browser.

  7. petersalomonsen commented on Nov 9, 2017

    @petersalomonsen
    Contributor
    1. Reverted CMAKE defaults and now using -DTHREADSAFE=OFF etc. ( see https://github.com/fintechneo/libgit2 )
    2. Tested using NO_MMAP, but results in:

    ERROR 2: trying to map shared-writeable:

    @CMCDragonkai @ethomson @vadimkantorov

  8. CMCDragonkai commented on Nov 10, 2017

    @CMCDragonkai

    I tried to figure out what would be the quickest way to create a soft-MMU for emscripten, and my experiments lead me to create https://github.com/MatrixAI/js-permaproxy It isn't impossible to replace/extend Emscripten's heap architecture with a soft-MMU based on ES6 proxies on Uint8Arrays that would properly emulate MAP_SHARED, but proxies are not very performant, I think possibly 2 orders of magnitude slower on my Chrome browser. There's also the issue that Uint8Arrays are just views, while ArrayBuffer is the actual data, and there's no way to Proxy mutations to ArrayBuffer. I think instead one should build a single "VirtualMemory" construct based on DataView instead, and rework all of the typed array accesses to be based on a single DataView. The best reference to do this fast would be QEMU's emulation (which has address translation buffers). But this is going to be a big task, and I'm not prepared to dive into this.

  9. petersalomonsen commented on Nov 10, 2017

    @petersalomonsen
    Contributor

    Did some adjustments and added a build option so that it shouldn't interfere with existing code/other target platforms. For now the NO_MMAP is not usable, so leaving my hack in there - but at least it's only active when using the emscripten build option.

    Hope you can use this @ethomson , and let me know if further adjustments are required.

    See pull request here:

    #4400

  10. petersalomonsen commented on Mar 21, 2018

    @petersalomonsen
    Contributor

    Now using webassembly and webworkers. A simple video demo can be seen here: https://youtu.be/rcBluzpUWE4

    Also started initial work on a simple file-explorer frontend: https://github.com/fintechneo/angular-git-filebrowser

    (which is the same as shown in the video)

  11. vadimkantorov commented on Mar 21, 2018

    @vadimkantorov
    Author

    With a proxy to bypass CORS?

  12. petersalomonsen commented on Mar 21, 2018

    @petersalomonsen
    Contributor

    @vadimkantorov Yes there's a proxy in that file-explorer project, so you can test it directly from there.

  13. petersalomonsen commented on Mar 22, 2018

    @petersalomonsen
    Contributor
  14. sbeaupre commented on Jun 10, 2018

    @sbeaupre

    @ethomson did you have time to evaluate the PR from @petersalomonsen above? I see that some CI checks have failed...

  15. petersalomonsen commented on Mar 11, 2019

    @petersalomonsen
    Contributor

    @CMCDragonkai I've added a http transport for nodejs now. Example of cloning a repo is here:

    https://github.com/fintechneo/libgit2/blob/emscripten_nodejs_http_transport/emscripten_hacks/jstests/nodejsclone.js

    The stream code is here:

    https://github.com/fintechneo/libgit2/blob/emscripten_nodejs_http_transport/src/streams/emscripten_nodejs.c

    I'm creating a child process for the http request with spawnSync to be able to make the request synchronously.

    Will merge this into my pull request soon (after some more testing)

  16. petersalomonsen commented on Mar 11, 2019

    @petersalomonsen
    Contributor

    @ethomson did you have time to evaluate the PR from @petersalomonsen above? I see that some CI checks have failed...

    @sbeaupre The CI checks pass just fine now: #4400 ( has been working for a while now ).

  17. petersalomonsen commented on Jul 1, 2019

    @petersalomonsen
    Contributor
  18. cspotcode commented on Aug 3, 2019

    @cspotcode

    Can this be combined with nodegit?

    Ideally I would like to use a node git library that does not have any native components, because this makes it more maintainable. Right now the latest nodegit from npm doesn't build against node 12. Plus, since it requires recompiling, it makes deployment a little trickier. At work I'm trying to webpack tools into a single JS file with a shebang, paired with a statically linked node binary, but I can't do that when nodegit is in the mix.

    Could nodegit use this precompiled WASM binary of libgit2 under the hood?

  19. petersalomonsen commented on Aug 3, 2019

    @petersalomonsen
    Contributor

    Not out of the box, you'd have to change the exposed API of the wasm library to match that of nodegit, or at least the functions your application is using.

    That said, if you know a little C, should be easy to tweak the API exposed to JS in this file https://github.com/fintechneo/libgit2/blob/master/emscripten_hacks/jslib.c to match your application. See the methods marked with EMSCRIPTEN_KEEPALIVE which is the way you can expose C methods to JS.

  20. ethomson commented on Apr 1, 2020

    @ethomson
    Member

    wasm-git is the answer. Thanks, @petersalomonsen!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions