| // Copyright 2021 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 | |
| // | |
| // https://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. | |
| // parse calls a function through a pointer of a mismatched type. This is | |
| // detected by UBSAN's function check. | |
| #include "NativeUbsanFuncPtrFuzzTest.h" | |
| #include <cstddef> | |
| #include <cstdint> | |
| int parse_data(const uint16_t *data) { | |
| return data[0] + data[1]; | |
| } | |
| int (*mistyped_function_pointer)(const char *data); | |
| JNIEXPORT int JNICALL Java_com_example_NativeUbsanFuncPtrFuzzTest_parse( | |
| JNIEnv *env, jobject o, jstring bytes) { | |
| const char *input(env->GetStringUTFChars(bytes, nullptr)); | |
| mistyped_function_pointer = | |
| reinterpret_cast<int (*)(const char *)>(&parse_data); | |
| int result = mistyped_function_pointer(input); | |
| env->ReleaseStringUTFChars(bytes, input); | |
| return result; | |
| } |