1/* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as fs from 'fs'; 17import { createPtsCollectionCtor, IPtsCollection, PtsCollectionType } from './PtsDS'; 18import { NodeID } from '../../core/graph/BaseExplicitGraph'; 19 20export enum PtaAnalysisScale { 21 WholeProgram = 0, 22 MethodLevel = 1, 23} 24 25export class PointerAnalysisConfig { 26 private static instance: PointerAnalysisConfig; 27 28 public kLimit: number; 29 public outputDirectory: string; 30 public detectTypeDiff: boolean; 31 public dotDump: boolean; 32 public unhandledFuncDump: boolean; 33 public analysisScale: PtaAnalysisScale; 34 public ptsCollectionType: PtsCollectionType; 35 public ptsCollectionCtor: new () => IPtsCollection<NodeID>; 36 37 /* 38 * Note: DO NOT use `new PointerAnalysisConfig` to initialize ptaconfig 39 * Use PointerAnalysisConfig.create() for singleton pattern 40 */ 41 constructor( 42 kLimit: number, 43 outputDirectory: string, 44 detectTypeDiff: boolean = false, 45 dotDump: boolean = false, 46 unhandledFuncDump: boolean = false, 47 analysisScale: PtaAnalysisScale = PtaAnalysisScale.WholeProgram, 48 ptsCoType = PtsCollectionType.Set 49 ) { 50 if (kLimit > 5) { 51 throw new Error('K Limit too large'); 52 } 53 this.kLimit = kLimit; 54 this.outputDirectory = outputDirectory; 55 this.detectTypeDiff = detectTypeDiff; 56 this.dotDump = dotDump; 57 this.unhandledFuncDump = unhandledFuncDump; 58 this.analysisScale = analysisScale; 59 this.ptsCollectionType = ptsCoType; 60 this.ptsCollectionCtor = createPtsCollectionCtor<NodeID>(ptsCoType); 61 62 if (!fs.existsSync(outputDirectory)) { 63 fs.mkdirSync(outputDirectory, { recursive: true }); 64 } 65 } 66 67 /* 68 * Set static field to be null, then all related objects could be freed by GC. 69 * Class PointerAnalysisConfig has been exported by ArkAnalyzer, the dispose method should be called by users themselves before free this class. 70 */ 71 public static dispose(): void { 72 // @ts-expect-error: only be used to free the memory 73 this.instance = null; 74 } 75 76 /* 77 * Create Singleton instance 78 * The instance can be created multi-times and be overwrited 79 */ 80 public static create( 81 kLimit: number, 82 outputDirectory: string, 83 detectTypeDiff: boolean = false, 84 dotDump: boolean = false, 85 unhandledFuncDump: boolean = false, 86 analysisScale: PtaAnalysisScale = PtaAnalysisScale.WholeProgram, 87 ptsCoType = PtsCollectionType.Set 88 ): PointerAnalysisConfig { 89 PointerAnalysisConfig.instance = new PointerAnalysisConfig( 90 kLimit, 91 outputDirectory, 92 detectTypeDiff, 93 dotDump, 94 unhandledFuncDump, 95 analysisScale, 96 ptsCoType 97 ); 98 return PointerAnalysisConfig.instance; 99 } 100 101 /* 102 * Get Singleton instance 103 */ 104 public static getInstance(): PointerAnalysisConfig { 105 if (!PointerAnalysisConfig.instance) { 106 throw new Error('PTA config: instance is not existing'); 107 } 108 return PointerAnalysisConfig.instance; 109 } 110} 111