Skip to content

Allow emscripten to use the full ubsan runtime - #8651

Merged
quantum5 merged 34 commits into
emscripten-core:incomingfrom
quantum5:ubsan-full
May 30, 2019
Merged

quantum5 merged 34 commits into
emscripten-core:incomingfrom
quantum5:ubsan-full

Conversation

@quantum5

@quantum5 quantum5 commented May 22, 2019 •

Copy link
Copy Markdown
Member

Changes:

  • Made sanitizer_common and ubsan runtimes work:
    • Add SANITIZER_EMSCRIPTEN platform
    • Add sanitizer_emscripten.cc for Emscripten-specific implementations of
      helper and abstraction functions
    • Stub out functions that do not work on Emscripten. Using these will be
      abort the program. These will be implemented later.
  • Add emscripten_get_module_name to get the WASM module name
  • Changed embuilder to build ubsan and sanitizer_common runtimes
  • Add ubsan-related unit tests
  • Remove old JS-based ubsan hack and its unit tests

Known limitations:

  • Stack traces do not work.

@quantum5
quantum5 requested review from aheejin, kripken and tlively May 22, 2019 22:44
@aheejin

aheejin commented May 22, 2019

Copy link
Copy Markdown
Member
  • Which version or the llvm is this PR based on? Could you add a readme file that says this, like other libraries? And if possible, how about getting the latest release (at the moment it is 8.0.0), rather than the tip of the trunk?

  • Could you point out which part of the library you had to modify to make this work? @kripken, do we have any way to say which part of the third party code was modified locally? Do we comment?

@quantum5

Copy link
Copy Markdown
Member Author

I will put in the LLVM version later. As for which parts of the sanitizers were modified, see here.

@aheejin

aheejin commented May 22, 2019 •

Copy link
Copy Markdown
Member
  • How about getting the latest release (at the moment it is 8.0.0), rather than the tip of the trunk?
  • This is hard to review, because basically we don't know which part is modified on top of the vanilla runtime. How about create a PR for the vanilla sanitizer lib separately first and add your changes in the next PR? Having to look at your commit diff link feels a bit fragile way to do it, and also that's gonna change as you add more commits.

@quantum5

Copy link
Copy Markdown
Member Author

The link will continue to work with new commits pushed, since it references the branch of this pull request. Unless we rebase the commits onto a different version of LLVM, of course.

@aheejin

aheejin commented May 23, 2019 •

Copy link
Copy Markdown
Member
  • Can I ask one more time, how about getting the latest release (at the moment it is 8.0.0), which should be more stable, rather than the tip of the trunk? If you don't like the idea, could you please explain why?
  • I still think splitting PR is better. Smaller PRs are generally easier to review, and especially big PR like this, having to see your custom-provided link to review a PR does not seem to be a good idea.

@quantum5

Copy link
Copy Markdown
Member Author

Okay, I'll make a PR that pulls in the latest LLVM release and base this on that.

@quantum5

Copy link
Copy Markdown
Member Author

This won't work until https://reviews.llvm.org/D62210 has been merged into LLVM.

quantum5 added a commit that referenced this pull request May 23, 2019
@quantum5
quantum5 marked this pull request as ready for review May 24, 2019 00:12
Comment thread tools/system_libs.py Outdated
add_library(system_libs_map[d])

for d in lib.js_deps:
shared.Settings.EXPORTED_FUNCTIONS.append('_%s' % d)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you explain this a litte? It looks like it just used for memalign? Is memalign implemented in JS? Why to do need js_deps now but we've managed to get away without them previously?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is __syscall192 (a JS function) depending on memalign (a C function), but we only add back dependencies to user code, not libraries.

Here, the sanitizer common library depends on __syscall192, but memalign is not pulled in because the user code doesn't use __syscall192.

I am not sure if this is the best approach, but I am doing this for now so that it at least works.

Comment thread tools/system_libs.py

if shared.Settings.UBSAN_RUNTIME == 1:
libs_to_link.append((shared.Cache.get('libubsan_minimal_rt_wasm.a', lambda: create_wasm_ubsan_minimal_rt('libubsan_minimal_rt_wasm.a')), False))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How does ubsan work on regular systems? Presumably you need to add a linker flag otherwise you'll get undefined symbols? I guess we can use our dependency system to do one better but I think requiring the linker flag is also ok.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

clang automatically adds the ubsan library to the linker command line.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But only if you specify the -fsanitize.. flag no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yea. The code you commented on was the old code that parses the command line for -fsanitize, but @kripken wanted to use the dependency system, and so it's now doing that.

-------- T _ZN11__sanitizer23internal_simple_strtollEPKcPS1_i

sanitizer_allocator.cc.o:
-------- d .L.str

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't see these local symbols on the other .symbols file. Is there something we are doing differently here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am not sure how these files are generated. I just did llvm-nm and replaced the addresses with - with perl.

llvm-git-migration pushed a commit to llvm/llvm-project that referenced this pull request May 29, 2019
Summary:
After emscripten-core/emscripten#8651, Emscripten
supports the full UBSan runtime. This includes the VPtr sanitizer.

This diff allows clang to generate code that uses the VPtr sanitizer for
Emscripten.

Patch by Guanzhong Chen

Reviewers: tlively, aheejin

Reviewed By: aheejin

Subscribers: dschuff, sbc100, jgravelle-google, sunfish, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D62559

llvm-svn: 362004
dtzWill pushed a commit to llvm-mirror/clang that referenced this pull request May 29, 2019
Summary:
After emscripten-core/emscripten#8651, Emscripten
supports the full UBSan runtime. This includes the VPtr sanitizer.

This diff allows clang to generate code that uses the VPtr sanitizer for
Emscripten.

Patch by Guanzhong Chen

Reviewers: tlively, aheejin

Reviewed By: aheejin

Subscribers: dschuff, sbc100, jgravelle-google, sunfish, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D62559

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@362004 91177308-0d34-0410-b5e6-96231b3b80d8
spurious pushed a commit to spurious/clang-mirror that referenced this pull request May 29, 2019
Summary:
After emscripten-core/emscripten#8651, Emscripten
supports the full UBSan runtime. This includes the VPtr sanitizer.

This diff allows clang to generate code that uses the VPtr sanitizer for
Emscripten.

Patch by Guanzhong Chen

Reviewers: tlively, aheejin

Reviewed By: aheejin

Subscribers: dschuff, sbc100, jgravelle-google, sunfish, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D62559

git-svn-id: http://llvm.org/svn/llvm-project/cfe/trunk@362004 91177308-0d34-0410-b5e6-96231b3b80d8
@tlively

tlively commented May 29, 2019

Copy link
Copy Markdown
Member

@sbc100 do you have any other concerns?

Comment thread ChangeLog.md Resolved
Comment thread emcc.py Resolved
}

char **GetEnviron() {
return fake_envp;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why fake this? Can't you use emscripten_get_env()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For some strange reason, the sanitizers want a function to get the entire environment block, as in a sequence of null-terminated strings going NAME=VALUE. It doesn't appear to be used anywhere that makes a difference yet, so I am stubbing it out.

U abort
U emscripten_return_address
U strlen
U write

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again, I think if we revert to the old behavior of linking if and only if the user specific -fsantize we can avoid these extra symbols files. Maybe wait for @kripken to respond because I know he explicitly advised you to go this route.

@kripken

kripken commented May 30, 2019

Copy link
Copy Markdown
Member

I remove my objection to duplicating the sanitizer flags, if you and @sbc100 both think that's better. So can avoid using the undefined symbols.

Comment thread emcc.py Resolved
@@ -1,1392 +0,0 @@

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you delete these .symbols files now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They have been deleted.

@quantum5
quantum5 merged commit 28b85b9 into emscripten-core:incoming May 30, 2019
Comment thread src/library.js
},

emscripten_get_module_name: function(buf, length) {
return stringToUTF8(wasmBinaryFile, buf, length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for the late review!

wasmBinaryFile will not exist in wasm2js mode, or if the wasm was embedded in the JS. But maybe it's not worth trying to support more cases than the common one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It appears to exist in wasm2js mode, which is why I did this:

$ emcc -fsanitize=null null-read.c -s WASM=0
emcc:WARNING: emcc: JS support in the upstream LLVM+wasm2js path is very experimental currently (best to use fastcomp for asm.js for now)
emscripten:WARNING: Wasm source map won't be usable in a browser without --source-map-base
$ grep wasmBinaryFile a.out.js 
var wasmBinaryFile = 'a.out.wasm';
if (!isDataURI(wasmBinaryFile)) {
  wasmBinaryFile = locateFile(wasmBinaryFile);
      return Module['readBinary'](wasmBinaryFile);
    return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
        throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
        !isDataURI(wasmBinaryFile) &&
      return WebAssembly.instantiateStreaming(fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
      return stringToUTF8(wasmBinaryFile, buf, length);
$ node a.out.js 
null-read.c:3:9: runtime error: load of null pointer of type 'int'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand? The file doesn't exist, but the variable does, yes. Is just the variable name enough?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it just wants a name.

belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
Changes:

* Made `sanitizer_common` and `ubsan` runtimes work:
  * Add `SANITIZER_EMSCRIPTEN` platform
  * Add `sanitizer_emscripten.cc` for Emscripten-specific implementations of
    helper and abstraction functions
  * Stub out functions that do not work on Emscripten. Using these will be
    abort the program. These will be implemented later.
* Add `emscripten_get_module_name` to get the WASM module name
* Changed `embuilder` to build `ubsan` and `sanitizer_common` runtimes
* Add `ubsan`-related unit tests
* Remove old JS-based `ubsan` hack and its unit tests

Known limitations:

* Stack traces do not work.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in emscripten-core#8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in emscripten-core#8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in emscripten-core#8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in emscripten-core#8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in emscripten-core#8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
sbc100 added a commit that referenced this pull request Apr 25, 2024
This helper was added when we implement sanitizes in #8651.  It was
never added to any public header file and should only have these two
internal users.

Instead of this function we can just use the existing internal
`_emscripten_get_progname` function, which can in turn then be
implemented in terms of the existing `getExecutableName` JavaScript
function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants