-
Notifications
You must be signed in to change notification settings - Fork 125
Add support for C extension fuzzing on macOS #107
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
Open
mschwager
wants to merge
3
commits into
google:master
Choose a base branch
from
mschwager:mschwager-macos-c-ext
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <pybind11/pybind11.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <cstdlib> | ||
|
|
||
| namespace py = pybind11; | ||
|
|
||
| void process(py::bytes data) { | ||
| char* buf = nullptr; | ||
| Py_ssize_t size = 0; | ||
| if (PyBytes_AsStringAndSize(data.ptr(), &buf, &size) != 0) return; | ||
| if (size != 4) return; | ||
| const uint8_t* p = reinterpret_cast<const uint8_t*>(buf); | ||
| if (p[0] != 'F') return; | ||
| if (p[1] != 'U') return; | ||
| if (p[2] != 'Z') return; | ||
| if (p[3] != 'Z') return; | ||
|
|
||
| char* volatile ptr = static_cast<char*>(std::malloc(128)); | ||
| ptr[0] = 'x'; | ||
| std::free(ptr); | ||
| volatile char boom = ptr[0]; | ||
| (void)boom; | ||
| } | ||
|
|
||
| PYBIND11_MODULE(dummy, m) { | ||
| m.doc() = "Intentionally buggy native extension for Atheris demos."; | ||
| m.def("process", &process); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #!/usr/bin/python3 | ||
|
|
||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import sys | ||
|
|
||
| import atheris | ||
|
|
||
| import dummy | ||
|
|
||
|
|
||
| @atheris.instrument_func | ||
| def TestOneInput(data): | ||
| dummy.process(data) | ||
|
|
||
|
|
||
| atheris.Setup(sys.argv, TestOneInput) | ||
| atheris.Fuzz() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/python3 | ||
|
|
||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from setuptools import setup, Extension | ||
| import sys | ||
|
|
||
|
|
||
| class get_pybind_include(object): | ||
|
|
||
| def __str__(self): | ||
| import pybind11 | ||
| return pybind11.get_include() | ||
|
|
||
|
|
||
| extra_compile_args = ['-std=c++17'] | ||
| extra_link_args = [] | ||
| if sys.platform == 'darwin': | ||
| darwin_opts = ['-stdlib=libc++', '-mmacosx-version-min=10.7'] | ||
| extra_compile_args += darwin_opts | ||
| extra_link_args += darwin_opts | ||
|
|
||
| setup( | ||
| name='dummy', | ||
| version='0.0.1', | ||
| description='Intentionally buggy native extension for Atheris demos', | ||
| ext_modules=[ | ||
| Extension( | ||
| 'dummy', | ||
| ['dummy.cc'], | ||
| include_dirs=[get_pybind_include()], | ||
| language='c++', | ||
| extra_compile_args=extra_compile_args, | ||
| extra_link_args=extra_link_args, | ||
| ), | ||
| ], | ||
| setup_requires=['pybind11>=2.5.0'], | ||
| zip_safe=False, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,15 @@ cp -r . "${TMP_DIR?}" | |
| cd "${TMP_DIR?}" | ||
|
|
||
| # Set up virtual env | ||
| "$PYTHON" -m virtualenv . | ||
| "$PYTHON" -m venv . | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| source bin/activate # After this, use `python` to get the venv, not $PYTHON | ||
| python -m pip install setuptools pybind11 | ||
| python -m pip install setuptools pybind11 wheel | ||
| python -m pip install . --no-build-isolation | ||
| (cd contrib/libprotobuf_mutator && python -m pip install . --no-build-isolation) | ||
| if [[ "$(uname)" != "Darwin" ]]; then | ||
| # libprotobuf_mutator's bundled zlib build is currently broken on macOS | ||
| # SDKs (zlib's fdopen macro collides with stdio.h). Skip on Darwin. | ||
| (cd contrib/libprotobuf_mutator && python -m pip install . --no-build-isolation) | ||
| fi | ||
| python -m pip install PyInstaller | ||
| python -m pip install protobuf | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching to the stdlib's
venvlibrary avoids this third-party dependency.