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

[promise-polyfill] initial integration #10581

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions projects/promise-polyfill/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 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 gcr.io/oss-fuzz-base/base-builder-javascript

COPY build.sh $SRC/

RUN git clone --depth 1 https://github.com/taylorhakes/promise-polyfill.git

COPY fuzz.js $SRC/promise-polyfill
COPY babel.config.json $SRC/promise-polyfill

WORKDIR $SRC/promise-polyfill
4 changes: 4 additions & 0 deletions projects/promise-polyfill/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["@babel/plugin-transform-modules-commonjs"],
"ignore": ["**/@jazzer.js", "**/@babel", "**/istanbul-reports"]
}
61 changes: 61 additions & 0 deletions projects/promise-polyfill/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash -eu
# Copyright 2023 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.
#
################################################################################

function change_type_to_commonjs() {
# Find all package.json files inside the node_modules directory
find "$1" -name "package.json" -type f | while read -r package_file; do
# Check if the file contains the "type" field
if grep -q '"type": "module"' "$package_file"; then
# Replace "type": "module" with "type": "commonjs"
sed -i 's/"type": "module"/"type": "commonjs"/' "$package_file"
echo "Updated $package_file"
fi
done
}

function transform_dir_into_commonjs() {
babel "$1" --keep-file-extension -D -d "$1"_commonjs
rm -r "$1"
mv "$1"_commonjs "$1"
}

function remove_dev_dependencies() {
package_json=$(cat package.json)

# Remove the "devDependencies" item from package.json
new_package_json=$(echo "$package_json" | jq 'del(.devDependencies)')

# Overwrite the package.json file with the updated content
echo "$new_package_json" >package.json

}

# Install dependencies.
remove_dev_dependencies

npm install -g @babel/cli
npm install --save-dev @babel/core @babel/plugin-transform-modules-commonjs

transform_dir_into_commonjs "$SRC/promise-polyfill/src"
transform_dir_into_commonjs "$SRC/promise-polyfill/node_modules"

npm install --save-dev @jazzer.js/core

change_type_to_commonjs "$SRC/promise-polyfill"

# Build Fuzzers.
compile_javascript_fuzzer promise-polyfill fuzz -i promise-polyfill
89 changes: 89 additions & 0 deletions projects/promise-polyfill/fuzz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2023 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.
//
////////////////////////////////////////////////////////////////////////////////

const Promise = require('./src/index.js').default;

module.exports.fuzz = async function(data) {
try {
const promise = new Promise((resolve, reject) => {
if (data.toString() === 'reject') {
reject(new Error('rejected'));
} else {
resolve(data.toString());
}
});

promise.then((result) => {
Promise.resolve('');
result.toUpperCase();
}, (_error) => {
})
.catch((_error) => {
})
.finally(() => {
});

Promise.resolve(data.toString())
.then((result) => {
result.toUpperCase();
}, (_error) => {
})
.catch((_error) => {
})
.finally(() => {
});

Promise.reject(new Error('rejected'))
.catch((_error) => {
})
.finally(() => {
});

const promises = [
Promise.resolve(data.toString()),
Promise.reject(new Error('rejected')),
Promise.resolve(data.toString())
];
Promise.all(promises)
.then((_results) => {
Promise.resolve('');
}, (_error) => {
})
.catch((_error) => {
})
.finally(() => {
});

Promise.race(promises)
.then((_result) => {
Promise.resolve('');
}, (_error) => {
})
.catch((_error) => {
})
.finally(() => {
});

} catch (error) {
if (!ignoredError(error)) throw error;
}
};

function ignoredError(error) {
return !!ignored.find((message) => error.message.indexOf(message) !== -1);
}

const ignored = [];
15 changes: 15 additions & 0 deletions projects/promise-polyfill/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
homepage: https://github.com/taylorhakes/promise-polyfill
language: javascript
main_repo: https://github.com/taylorhakes/promise-polyfill
fuzzing_engines:
- libfuzzer
sanitizers:
- none
vendor_ccs:
- "[email protected]"
- "[email protected]"
- "[email protected]"
- "[email protected]"
- "[email protected]"
- "[email protected]"
- "[email protected]"