1 /* 2 * Copyright 2023 Code Intelligence GmbH 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 <iostream> 18 19 #include "slicer/writer.h" 20 21 class JazzerJvmtiAllocator : public dex::Writer::Allocator { 22 public: JazzerJvmtiAllocator(jvmtiEnv * jvmti_env)23 JazzerJvmtiAllocator(jvmtiEnv* jvmti_env) : jvmti_env_(jvmti_env) {} 24 Allocate(size_t size)25 virtual void* Allocate(size_t size) { 26 unsigned char* alloc = nullptr; 27 jvmtiError error_num = jvmti_env_->Allocate(size, &alloc); 28 29 if (error_num != JVMTI_ERROR_NONE) { 30 std::cerr << "JazzerJvmtiAllocator Allocation error. JVMTI error: " 31 << error_num << std::endl; 32 } 33 34 return (void*)alloc; 35 } 36 Free(void * ptr)37 virtual void Free(void* ptr) { 38 if (ptr == nullptr) { 39 return; 40 } 41 42 jvmtiError error_num = jvmti_env_->Deallocate((unsigned char*)ptr); 43 44 if (error_num != JVMTI_ERROR_NONE) { 45 std::cout << "JazzerJvmtiAllocator Free error. JVMTI error: " << error_num 46 << std::endl; 47 } 48 } 49 50 private: 51 jvmtiEnv* jvmti_env_; 52 }; 53