1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "random_parcel_jni.h"
18 #include <android_util_Binder.h>
19 #include <android_os_Parcel.h>
20 #include <fuzzbinder/libbinder_driver.h>
21 #include <fuzzbinder/random_parcel.h>
22 #include <fuzzer/FuzzedDataProvider.h>
23 using namespace android;
24 
25 // JNI interface for fuzzService
Java_randomparcel_FuzzBinder_fuzzServiceInternal(JNIEnv * env,jobject thiz,jobject javaBinder,jbyteArray fuzzData)26 JNIEXPORT void JNICALL Java_randomparcel_FuzzBinder_fuzzServiceInternal(JNIEnv *env, jobject thiz, jobject javaBinder, jbyteArray fuzzData) {
27     size_t len = static_cast<size_t>(env->GetArrayLength(fuzzData));
28     uint8_t data[len];
29     env->GetByteArrayRegion(fuzzData, 0, len, reinterpret_cast<jbyte*>(data));
30 
31     FuzzedDataProvider provider(data, len);
32     sp<IBinder> binder = android::ibinderForJavaObject(env, javaBinder);
33     fuzzService(binder, std::move(provider));
34 }
35 
36 // API used by AIDL fuzzers to access JNI functions from libandroid_runtime.
Java_randomparcel_FuzzBinder_registerNatives(JNIEnv * env)37 JNIEXPORT jint JNICALL Java_randomparcel_FuzzBinder_registerNatives(JNIEnv* env) {
38     return registerFrameworkNatives(env);
39 }
40 
Java_randomparcel_FuzzBinder_fillParcelInternal(JNIEnv * env,jobject thiz,jobject jparcel,jbyteArray fuzzData)41 JNIEXPORT void JNICALL Java_randomparcel_FuzzBinder_fillParcelInternal(JNIEnv *env, jobject thiz, jobject jparcel, jbyteArray fuzzData) {
42     size_t len = static_cast<size_t>(env->GetArrayLength(fuzzData));
43     uint8_t data[len];
44     env->GetByteArrayRegion(fuzzData, 0, len, reinterpret_cast<jbyte*>(data));
45 
46     FuzzedDataProvider provider(data, len);
47     RandomParcelOptions options;
48 
49     Parcel* parcel = parcelForJavaObject(env, jparcel);
50     fillRandomParcel(parcel, std::move(provider), &options);
51 }
52