• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2/**
3* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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// Autogenerated file -- DO NOT EDIT!
18
19#include "source_languages.h"
20#include "verification/absint/panda_types.h"
21#include "verification/jobs/job.h"
22
23#ifndef VERIFIER_LANG_SPECIFICS_H
24#define VERIFIER_LANG_SPECIFICS_H
25
26namespace panda::verifier {
27
28% Common::plugins.each do |plugin_lang, plugin_opts|
29% if plugin_lang == "JAVA"
30
31    inline TypeRelationship GetRelationship(const Type &type1, const Type &type2)
32    {
33        if (type1 == type2) {
34            return TypeRelationship::SAME;
35        } else if (type1 <= type2) {
36            return TypeRelationship::DESCENDANT;
37        } else {
38            return TypeRelationship::OTHER;
39        }
40    }
41
42    inline TypeRelationship GetRelationship(const Job &job, PandaTypes &types,
43                                            const CachedClass &class1,
44                                            const CachedClass &class2)
45    {
46        auto lang = job.JobCachedMethod().GetSourceLang();
47        if (lang == panda::panda_file::SourceLang::JAVA_8) {
48            TypeRelationship relationship = GetRelationship(types.TypeOf(class1), types.TypeOf(class2));
49            if (relationship != TypeRelationship::SAME && class1.IsSamePackage(class2)) {
50                // Member access levels strength:
51                // SAME (same class) << NEIGHBOUR (same package) << DESCENDANT (subclass) << OTHER
52                return TypeRelationship::NEIGHBOUR;
53            }
54            return relationship;
55        }
56        // For non-java language context return TypeRelationship::SAME.
57        // Though it is not semantically correct, the TypeRelationship is used in access
58        // checks only, and SAME helps to pass the access checks for non-java context.
59        return TypeRelationship::SAME;
60    }
61
62    // works for both fields and methods
63    template <typename T>
64    AccessModifier GetAccessMode(const T *x)
65    {
66        if (x->flags[T::Flag::PRIVATE]) {
67            return AccessModifier::PRIVATE;
68        } else if (x->flags[T::Flag::PROTECTED]) {
69            return AccessModifier::PROTECTED;
70        } else if (x->flags[T::Flag::PUBLIC]) {
71            return AccessModifier::PUBLIC;
72        } else {
73            return AccessModifier::PACKAGE;
74        }
75    }
76
77% end
78% end
79
80    inline const CheckResult& CheckFieldAccessViolation([[maybe_unused]] const CachedField *field,
81                                                        [[maybe_unused]] const Job &currentJob,
82                                                        [[maybe_unused]] PandaTypes &types) {
83% Common::plugins.each do |plugin_lang, plugin_opts|
84% if plugin_lang == "JAVA"
85        auto relation = GetRelationship(currentJob, types, currentJob.JobCachedMethod().klass, field->klass);
86        AccessModifier access_mode = GetAccessMode(field);
87        return panda::verifier::CheckFieldAccess(relation, access_mode);
88% end
89% end
90        return CheckResult::ok;
91    }
92
93    inline const CheckResult& CheckMethodAccessViolation([[maybe_unused]] const CachedMethod *method,
94                                                         [[maybe_unused]] const Job &currentJob,
95                                                         [[maybe_unused]] PandaTypes &types) {
96% Common::plugins.each do |plugin_lang, plugin_opts|
97% if plugin_lang == "JAVA"
98        auto relation = GetRelationship(currentJob, types, currentJob.JobCachedMethod().klass, method->klass);
99        auto access_mode = GetAccessMode(method);
100        return panda::verifier::CheckCall(relation, access_mode);
101% end
102% end
103        return CheckResult::ok;
104    }
105
106    inline const CheckResult& CheckClassAccessViolation([[maybe_unused]] const CachedClass *cached_class,
107                                                        [[maybe_unused]] const Job &currentJob,
108                                                        [[maybe_unused]] PandaTypes &types) {
109% Common::plugins.each do |plugin_lang, plugin_opts|
110% if plugin_lang == "JAVA"
111        auto relation = GetRelationship(currentJob, types, currentJob.JobCachedMethod().klass, *cached_class);
112        if (relation == TypeRelationship::OTHER) {
113            auto is_public = cached_class->flags[CachedClass::Flag::PUBLIC];
114            if (!is_public) {
115                return CheckResult::protected_class;
116            }
117        }
118% end
119% end
120        return CheckResult::ok;
121    }
122}
123
124#endif // VERIFIER_LANG_SPECIFICS_H
125