已合并
feat: 增补UT覆盖率 (runtime/c/executor) #4101
feat: 增补UT覆盖率 (runtime/c/executor) #4101
已合并
likun104创建于 7月22日
1 个文件变更+443-0
@@ -2683,3 +2683,446 @@ TEST_F(UtestGEExecutorTest, ForDump_StaticTaskSize_Set_Normal) {
2683 FreeModelData(&modelData);2683 FreeModelData(&modelData);
2684 GeFinalize();2684 GeFinalize();
2685}2685}
2686+ 
2687+// Covers ge_executor.c lines 191-192, 200, 226-228:
2688+// ModelDescInfoDepthCopy dims ReSizeVector fail + GetModelDescInfo output copy fail path
2689+TEST_F(UtestGEExecutorTest, GeExecutorCaseModelDescInfoDepthCopy_DimsResizeFail) {
2690+ ExeModelBuilder modelBuilder;
2691+ std::vector<uint64_t> dims1 = {1, 2, 3};
2692+ std::vector<uint64_t> dims2 = {};
2693+ std::vector<std::pair<uint64_t, uint64_t>> shapeRange = {{1, 2}, {2, 5}};
2694+ modelBuilder
2695+ .AddPartition(PRE_MODEL_DESC,
2696+ [](std::vector<uint8_t> &model) {
2697+ ModelDesc desc = {
2698+ .task_num = 1,
2699+ .workspace_size = 100,
2700+ .weight_size = 103,
2701+ .weight_type = 0,
2702+ .profile_enable = 0,
2703+ .model_interrupt = 0,
2704+ };
2705+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
2706+ })
2707+ .AddInputDesc("op1", 10, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
2708+ .AddOutputDesc("op2", 8, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
2709+ .Build();
2710+ 
2711+ GeInitialize();
2712+ uint32_t model_id = 0;
2713+ ModelData model_data;
2714+ (void)memset_s(&model_data, sizeof(ModelData), 0, sizeof(ModelData));
2715+ model_data.modelData = modelBuilder.ModelData();
2716+ model_data.modelLen = modelBuilder.ModelLen();
2717+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelLoad(_, _)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2718+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelDestroy(_)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2719+ EXPECT_EQ(GeLoadModelFromData(&model_id, &model_data), SUCCESS);
2720+ 
2721+ ModelInOutInfo info;
2722+ (void)memset_s(&info, sizeof(ModelInOutInfo), 0, sizeof(ModelInOutInfo));
2723+ // mmMalloc calls: 1=input_desc resize, 2=input name, 3=input dims resize,
2724+ // 4=output_desc resize, 5=output name, 6=output dims resize (fail)
2725+ EXPECT_CALL(MmpaStubMock::GetInstance(), mmMalloc(_))
2726+ .Times(AnyNumber())
2727+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2728+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2729+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2730+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2731+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2732+ .WillOnce(Invoke(mmMalloc_Abnormal_Invoke))
2733+ .WillRepeatedly(Invoke(mmMalloc_Normal_Invoke));
2734+ EXPECT_EQ(GetModelDescInfo(model_id, &info), ACL_ERROR_GE_MEMORY_OPERATE_FAILED);
2735+ 
2736+ EXPECT_EQ(UnloadModel(model_id), SUCCESS);
2737+ GeFinalize();
2738+}
2739+ 
2740+// Covers model_executor.c lines 25-26:
2741+// ModelGetIoAddr realloc path when ioa_size < ioa_size && io_addr != NULL
2742+TEST_F(UtestGEExecutorTest, GeExecutorCaseModelExecIoAddrRealloc) {
2743+ ExeModelBuilder modelBuilder;
2744+ std::vector<uint64_t> dims1 = {1, 2, 3};
2745+ std::vector<uint64_t> dims2 = {};
2746+ std::vector<std::pair<uint64_t, uint64_t>> shapeRange = {{1, 2}, {2, 5}};
2747+ modelBuilder
2748+ .AddPartition(PRE_MODEL_DESC,
2749+ [](std::vector<uint8_t> &model) {
2750+ ModelDesc desc = {
2751+ .task_num = 1,
2752+ .workspace_size = 100,
2753+ .weight_size = 103,
2754+ .weight_type = 0,
2755+ .profile_enable = 0,
2756+ .model_interrupt = 0,
2757+ };
2758+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
2759+ })
2760+ .AddInputDesc("op1", 10, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
2761+ .AddOutputDesc("op2", 8, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
2762+ .Build();
2763+ 
2764+ GeInitialize();
2765+ uint32_t model_id = 0;
2766+ uint8_t *workspace = nullptr;
2767+ uint8_t *weightspace = nullptr;
2768+ (void)rtMalloc((void **)&workspace, sizeof(uint8_t) * 100, RT_MEMORY_DEFAULT, 0);
2769+ (void)rtMalloc((void **)&weightspace, sizeof(uint8_t) * 103, RT_MEMORY_DEFAULT, 0);
2770+ ModelData model_data;
2771+ (void)memset_s(&model_data, sizeof(ModelData), 0, sizeof(ModelData));
2772+ model_data.modelData = modelBuilder.ModelData();
2773+ model_data.modelLen = modelBuilder.ModelLen();
2774+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelLoad(_, _)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2775+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelDestroy(_)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2776+ EXPECT_EQ(GeLoadModelFromData(&model_id, &model_data), SUCCESS);
2777+ 
2778+ ExecHandleDesc execDesc;
2779+ execDesc.workPtr = workspace;
2780+ execDesc.workSize = 100;
2781+ InputData inputData;
2782+ InitVector(&inputData.blobs, sizeof(DataBuffer));
2783+ DataBlob inputBlob;
2784+ DataBuffer input;
2785+ uint32_t inputAddr = 0x1006;
2786+ input.data = (void *)(&inputAddr);
2787+ input.length = 50;
2788+ inputBlob.dataBuffer = &input;
2789+ EmplaceBackVector(&inputData.blobs, &inputBlob);
2790+ OutputData outputData;
2791+ InitVector(&outputData.blobs, sizeof(DataBuffer));
2792+ void *dummyIoAddr = nullptr;
2793+ aclrtMalloc(&dummyIoAddr, 64, ACL_MEM_MALLOC_HUGE_FIRST);
2794+ outputData.io_addr = (uint64_t *)dummyIoAddr;
2795+ outputData.io_addr_host = NULL;
2796+ outputData.ioa_size = 0;
2797+ DataBlob outputBlob;
2798+ DataBuffer output;
2799+ uint32_t outputAddr = 0x1008;
2800+ output.data = (void *)(&outputAddr);
2801+ output.length = 100;
2802+ outputBlob.dataBuffer = &output;
2803+ EmplaceBackVector(&outputData.blobs, &outputBlob);
2804+ EXPECT_CALL(RtStubMock::GetInstance(), rtStreamGetSqid(_, _)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2805+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelExecute(_)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2806+ EXPECT_EQ(ExecModel(model_id, &execDesc, true, &inputData, &outputData), SUCCESS);
2807+ 
2808+ EXPECT_EQ(UnloadModel(model_id), SUCCESS);
2809+ aclrtFree(workspace);
2810+ aclrtFree(weightspace);
2811+ DeInitVector(&inputData.blobs);
2812+ DeInitVector(&outputData.blobs);
2813+ if (outputData.io_addr != NULL) {
2814+ aclrtFree(outputData.io_addr);
2815+ }
2816+ if (outputData.io_addr_host != NULL) {
2817+ mmFree(outputData.io_addr_host);
2818+ }
2819+ outputData.ioa_size = 0;
2820+ GeFinalize();
2821+}
2822+ 
2823+// Covers model_loader.c lines 116-118 and model_manager.c lines 114-115:
2824+// AddModelDescRef fails when EmplaceSortVector returns NULL
2825+TEST_F(UtestGEExecutorTest, GeExecutorCaseLoadModel_EmplaceSortVectorFail) {
2826+ ExeModelBuilder modelBuilder;
2827+ modelBuilder
2828+ .AddPartition(PRE_MODEL_DESC,
2829+ [](std::vector<uint8_t> &model) {
2830+ ModelDesc desc = {
2831+ .task_num = 1,
2832+ .workspace_size = 100,
2833+ .weight_size = 103,
2834+ .weight_type = 0,
2835+ .profile_enable = 0,
2836+ .model_interrupt = 0,
2837+ };
2838+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
2839+ })
2840+ .Build();
2841+ 
2842+ GeInitialize();
2843+ uint32_t model_id = 0;
2844+ ModelData model_data;
2845+ (void)memset_s(&model_data, sizeof(ModelData), 0, sizeof(ModelData));
2846+ model_data.modelData = modelBuilder.ModelData();
2847+ model_data.modelLen = modelBuilder.ModelLen();
2848+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelLoad(_, _)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2849+ EXPECT_CALL(RtStubMock::GetInstance(), rtNanoModelDestroy(_)).Times(1).WillOnce(Return(RT_ERROR_NONE));
2850+ // mmMalloc calls: 1=CreateModelDescRefObj (succeed), 2=EmplaceSortVector->CapacityVector (fail)
2851+ EXPECT_CALL(MmpaStubMock::GetInstance(), mmMalloc(_))
2852+ .Times(AnyNumber())
2853+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
2854+ .WillOnce(Invoke(mmMalloc_Abnormal_Invoke))
2855+ .WillRepeatedly(Invoke(mmMalloc_Normal_Invoke));
2856+ EXPECT_EQ(GeLoadModelFromData(&model_id, &model_data), ACL_ERROR_GE_INTERNAL_ERROR);
2857+ GeFinalize();
2858+}
2859+ 
2860+// Covers model_parse.c line 238:
2861+// GetModelInOutDesc returns error when size < sizeof(uint32_t) at start
2862+void StubModelInOutPartitionSmallValue(std::vector<uint8_t> &model) {
2863+ static uint8_t stub[] = {
2864+ 0x00, 0x00, 0x00, 0x00, // type 0 (MODEL_INPUT_DESC)
2865+ 0x02, 0x00, 0x00, 0x00, // len 2
2866+ 0x00, 0x00, // 2 bytes of value (less than sizeof(uint32_t))
2867+ };
2868+ std::copy_n(stub, sizeof(stub), std::back_inserter(model));
2869+}
2870+ 
2871+TEST_F(UtestGEExecutorTest, ParseModelIoDescInfo_GetModelInOutDesc_SizeTooSmall) {
2872+ ExeModelBuilder modelBuilder;
2873+ modelBuilder
2874+ .AddPartition(PRE_MODEL_DESC,
2875+ [](std::vector<uint8_t> &model) {
2876+ ModelDesc desc = {
2877+ .task_num = 1,
2878+ .workspace_size = 100,
2879+ .weight_size = 103,
2880+ .weight_type = 0,
2881+ .profile_enable = 0,
2882+ .model_interrupt = 0,
2883+ };
2884+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
2885+ })
2886+ .AddPartition(MODEL_INOUT_INFO, [](std::vector<uint8_t> &model) { StubModelInOutPartitionSmallValue(model); })
2887+ .Build();
2888+ 
2889+ GeInitialize();
2890+ ModelData modelData;
2891+ (void)memset_s(&modelData, sizeof(ModelData), 0, sizeof(ModelData));
2892+ modelData.modelData = modelBuilder.ModelData();
2893+ modelData.modelLen = modelBuilder.ModelLen();
2894+ ModelInOutInfo info;
2895+ EXPECT_EQ(GetModelDescInfoFromMem(&modelData, &info), ACL_ERROR_GE_INTERNAL_ERROR);
2896+ GeFinalize();
2897+}
2898+ 
2899+// Covers model_parse.c line 248:
2900+// GetModelInOutDesc returns error when ResizeModelInOutTensorDesc fails (VectorSize != desc_num)
2901+TEST_F(UtestGEExecutorTest, ParseModelIoDescInfo_ResizeModelInOutTensorDescFail) {
2902+ ExeModelBuilder modelBuilder;
2903+ std::vector<uint64_t> dims1 = {1, 2, 3};
2904+ std::vector<uint64_t> dims2 = {};
2905+ std::vector<std::pair<uint64_t, uint64_t>> shapeRange = {};
2906+ modelBuilder
2907+ .AddPartition(PRE_MODEL_DESC,
2908+ [](std::vector<uint8_t> &model) {
2909+ ModelDesc desc = {
2910+ .task_num = 1,
2911+ .workspace_size = 100,
2912+ .weight_size = 103,
2913+ .weight_type = 0,
2914+ .profile_enable = 0,
2915+ .model_interrupt = 0,
2916+ };
2917+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
2918+ })
2919+ .AddInputDesc("op1", 10, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
2920+ .Build();
2921+ 
2922+ GeInitialize();
2923+ ModelData modelData;
2924+ (void)memset_s(&modelData, sizeof(ModelData), 0, sizeof(ModelData));
2925+ modelData.modelData = modelBuilder.ModelData();
2926+ modelData.modelLen = modelBuilder.ModelLen();
2927+ ModelInOutInfo info;
2928+ // mmMalloc call 1: ResizeModelInOutTensorDesc -> ReSizeVector -> CapacityVector (fail)
2929+ EXPECT_CALL(MmpaStubMock::GetInstance(), mmMalloc(_))
2930+ .Times(AnyNumber())
2931+ .WillOnce(Invoke(mmMalloc_Abnormal_Invoke))
2932+ .WillRepeatedly(Invoke(mmMalloc_Normal_Invoke));
2933+ EXPECT_EQ(GetModelDescInfoFromMem(&modelData, &info), ACL_ERROR_GE_DEVICE_MEMORY_OPERATE_FAILED);
2934+ GeFinalize();
2935+}
2936+ 
2937+// Covers model_parse.c lines 270-271:
2938+// GetModelInOutDesc returns error when CheckLenValid fails for tlvLen
2939+void StubModelInOutPartitionTlvLenExceed(std::vector<uint8_t> &model) {
2940+ static uint8_t stub[] = {
2941+ 0x00,
2942+ 0x00,
2943+ 0x00,
2944+ 0x00, // type 0 (MODEL_INPUT_DESC)
2945+ 0x24,
2946+ 0x00,
2947+ 0x00,
2948+ 0x00, // len 36 (4 + 32 = desc_num + ModelTensorDescBaseInfo)
2949+ 0x01,
2950+ 0x00,
2951+ 0x00,
2952+ 0x00, // desc_num 1
2953+ // ModelTensorDescBaseInfo (32 bytes)
2954+ 0x00,
2955+ 0x00,
2956+ 0x00,
2957+ 0x00,
2958+ 0x00,
2959+ 0x00,
2960+ 0x00,
2961+ 0x00, // size
2962+ 0x1C,
2963+ 0x00,
2964+ 0x00,
2965+ 0x00, // format
2966+ 0x10,
2967+ 0x00,
2968+ 0x00,
2969+ 0x00, // dt
2970+ 0x02,
2971+ 0x00,
2972+ 0x00,
2973+ 0x00, // name_len 2
2974+ 0x00,
2975+ 0x00,
2976+ 0x00,
2977+ 0x00, // dims_len 0
2978+ 0x00,
2979+ 0x00,
2980+ 0x00,
2981+ 0x00, // dimsV2_len 0
2982+ 0x00,
2983+ 0x00,
2984+ 0x00,
2985+ 0x00, // shape_range_len 0
2986+ };
2987+ std::copy_n(stub, sizeof(stub), std::back_inserter(model));
2988+}
2989+ 
2990+TEST_F(UtestGEExecutorTest, ParseModelIoDescInfo_TlvLenExceed) {
2991+ ExeModelBuilder modelBuilder;
2992+ modelBuilder
2993+ .AddPartition(PRE_MODEL_DESC,
2994+ [](std::vector<uint8_t> &model) {
2995+ ModelDesc desc = {
2996+ .task_num = 1,
2997+ .workspace_size = 100,
2998+ .weight_size = 103,
2999+ .weight_type = 0,
3000+ .profile_enable = 0,
3001+ .model_interrupt = 0,
3002+ };
3003+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
3004+ })
3005+ .AddPartition(MODEL_INOUT_INFO, [](std::vector<uint8_t> &model) { StubModelInOutPartitionTlvLenExceed(model); })
3006+ .Build();
3007+ 
3008+ GeInitialize();
3009+ ModelData modelData;
3010+ (void)memset_s(&modelData, sizeof(ModelData), 0, sizeof(ModelData));
3011+ modelData.modelData = modelBuilder.ModelData();
3012+ modelData.modelLen = modelBuilder.ModelLen();
3013+ ModelInOutInfo info;
3014+ EXPECT_EQ(GetModelDescInfoFromMem(&modelData, &info), ACL_ERROR_GE_MEMORY_OPERATE_FAILED);
3015+ GeFinalize();
3016+}
3017+ 
3018+// Covers model_parse.c lines 287-288:
3019+// GetModelInOutDesc returns error when ReSizeVector for dims fails
3020+TEST_F(UtestGEExecutorTest, ParseModelIoDescInfo_DimsResizeFail) {
3021+ ExeModelBuilder modelBuilder;
3022+ std::vector<uint64_t> dims1 = {1, 2, 3};
3023+ std::vector<uint64_t> dims2 = {};
3024+ std::vector<std::pair<uint64_t, uint64_t>> shapeRange = {};
3025+ modelBuilder
3026+ .AddPartition(PRE_MODEL_DESC,
3027+ [](std::vector<uint8_t> &model) {
3028+ ModelDesc desc = {
3029+ .task_num = 1,
3030+ .workspace_size = 100,
3031+ .weight_size = 103,
3032+ .weight_type = 0,
3033+ .profile_enable = 0,
3034+ .model_interrupt = 0,
3035+ };
3036+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
3037+ })
3038+ .AddInputDesc("op1", 10, FORMAT_CHWN, DT_FLOAT, dims1, dims2, shapeRange)
3039+ .Build();
3040+ 
3041+ GeInitialize();
3042+ ModelData modelData;
3043+ (void)memset_s(&modelData, sizeof(ModelData), 0, sizeof(ModelData));
3044+ modelData.modelData = modelBuilder.ModelData();
3045+ modelData.modelLen = modelBuilder.ModelLen();
3046+ ModelInOutInfo info;
3047+ // mmMalloc calls: 1=ResizeModelInOutTensorDesc (succeed), 2=desc name (succeed), 3=dims ReSizeVector (fail)
3048+ EXPECT_CALL(MmpaStubMock::GetInstance(), mmMalloc(_))
3049+ .Times(AnyNumber())
3050+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
3051+ .WillOnce(Invoke(mmMalloc_Normal_Invoke))
3052+ .WillOnce(Invoke(mmMalloc_Abnormal_Invoke))
3053+ .WillRepeatedly(Invoke(mmMalloc_Normal_Invoke));
3054+ EXPECT_EQ(GetModelDescInfoFromMem(&modelData, &info), ACL_ERROR_GE_MEMORY_OPERATE_FAILED);
3055+ GeFinalize();
3056+}
3057+ 
3058+// Covers model_parse.c lines 464-465:
3059+// ProcFifoInfo returns error when fifoNum > 0 but CheckLenValid for mem_size fails
3060+void StubExtendPartitionFifoNumExceedLen(std::vector<uint8_t> &model) {
3061+ static uint8_t stub[] = {
3062+ // ModelExtendHead
3063+ 0x48,
3064+ 0x4D,
3065+ 0x4F,
3066+ 0x44, // magic "HMOD"
3067+ 0x14,
3068+ 0x00,
3069+ 0x00,
3070+ 0x00,
3071+ 0x00,
3072+ 0x00,
3073+ 0x00,
3074+ 0x00, // len 20
3075+ // TlvHead
3076+ 0x00,
3077+ 0x00,
3078+ 0x00,
3079+ 0x00, // type 0
3080+ 0x0C,
3081+ 0x00,
3082+ 0x00,
3083+ 0x00, // len 12
3084+ // ModelGlobalDataInfo (12 bytes)
3085+ 0x10,
3086+ 0x00,
3087+ 0x00,
3088+ 0x00,
3089+ 0x00,
3090+ 0x00,
3091+ 0x00,
3092+ 0x00, // total_size 16
3093+ 0x02,
3094+ 0x00,
3095+ 0x00,
3096+ 0x00, // num 2
3097+ };
3098+ std::copy_n(stub, sizeof(stub), std::back_inserter(model));
3099+}
3100+ 
3101+TEST_F(UtestGEExecutorTest, GeExecutorCaseParseModelDescExtendFifoNumExceedLen) {
3102+ ExeModelBuilder modelBuilder;
3103+ modelBuilder
3104+ .AddPartition(PRE_MODEL_DESC,
3105+ [](std::vector<uint8_t> &model) {
3106+ ModelDesc desc = {
3107+ .task_num = 1,
3108+ .workspace_size = 100,
3109+ .weight_size = 103,
3110+ .weight_type = 0,
3111+ .profile_enable = 0,
3112+ .model_interrupt = 0,
3113+ };
3114+ std::copy_n((uint8_t *)(&desc), sizeof(ModelDesc), std::back_inserter(model));
3115+ })
3116+ .AddPartition(PRE_MODEL_DESC_EXTEND,
3117+ [](std::vector<uint8_t> &model) { StubExtendPartitionFifoNumExceedLen(model); })
3118+ .Build();
3119+ 
3120+ GeInitialize();
3121+ uint32_t model_id = 0;
3122+ ModelData model_data;
3123+ (void)memset_s(&model_data, sizeof(ModelData), 0, sizeof(ModelData));
3124+ model_data.modelData = modelBuilder.ModelData();
3125+ model_data.modelLen = modelBuilder.ModelLen();
3126+ EXPECT_EQ(GeLoadModelFromData(&model_id, &model_data), ACL_ERROR_GE_LOAD_MODEL);
3127+ GeFinalize();
3128+}