1 /*
2  * Copyright (C) 2024 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 
18 #include "../includes/common.h"
19 
20 #include <C2AllocatorIon.h>
21 #include <C2BufferPriv.h>
22 #include <Codec2BufferUtils.h>
23 #include <Codec2Mapper.h>
24 #include <codec2/hidl/client.h>
25 
26 #include "../includes/memutils.h"
27 
28 char enable_selective_overload = ENABLE_NONE;
29 
main()30 int main() {
31   // Fetch graphic block
32   std::shared_ptr<C2GraphicBlock> block;
33   std::shared_ptr<C2AllocatorStore> store =
34       android::GetCodec2PlatformAllocatorStore();
35   std::shared_ptr<C2Allocator> graphicAllocator;
36   store->fetchAllocator(C2AllocatorStore::DEFAULT_GRAPHIC, &graphicAllocator);
37   std::shared_ptr<C2BlockPool> graphicPool =
38       std::make_shared<C2PooledBlockPool>(graphicAllocator, 0 /* id */);
39   graphicPool->fetchGraphicBlock(
40       MINIMUM_ALIGNMENT + 2 /* width */, 2 /* height */,
41       HAL_PIXEL_FORMAT_YV12 /* pixel format */,
42       {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}, &block);
43   FAIL_CHECK(block->map().get().layout().numPlanes == 3);
44 
45   // Get C2GraphicView object from block
46   C2GraphicView view = block->map().get();
47 
48   // Set cropped dimension
49   C2Rect crop = C2Rect(0 /* width */, 0 /* height */);
50   view.setCrop_be(crop);
51   std::vector<uint8_t> conversionBuffer;
52   enable_selective_overload = ENABLE_ALL;
53   conversionBuffer.resize(
54       MINIMUM_ALIGNMENT); // as per MINIMUM_ALIGNMENT in memutils
55   enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
56   uint8_t *conversionBuff = (uint8_t *)conversionBuffer.data();
57 
58   // Without fix, block's width and height will be used for conversion. An OOB
59   // write will occur when 16th index of the conversion buffer is accessed.
60   // However with fix, 'crop width' and 'crop height' will be used for accessing
61   // conversion buffer. Hence no OOB access is observed
62   android::ConvertRGBToPlanarYUV(conversionBuff, 0 /* dstStride */,
63                                  0 /* dstVstride */,
64                                  MINIMUM_ALIGNMENT /* buffersize */, view);
65 }
66